< Summary - Results for net8.0, Release

Information
Class: LockCheck.Linux.ProcInfo
Assembly: LockCheck
File(s): /home/runner/work/LockCheck/LockCheck/src/LockCheck/Linux/ProcInfo.cs
Tag: 96_11660771111
Line coverage
61%
Covered lines: 59
Uncovered lines: 37
Coverable lines: 96
Total lines: 295
Line coverage: 61.4%
Branch coverage
70%
Covered branches: 14
Total branches: 20
Branch coverage: 70%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_ProcessId()100%11100%
set_ProcessId(...)100%11100%
get_StartTime()100%11100%
set_StartTime(...)100%11100%
get_ParentProcessId()100%210%
set_ParentProcessId(...)100%11100%
get_SessionId()100%11100%
set_SessionId(...)100%11100%
get_ProcessName()100%210%
set_ProcessName(...)100%11100%
get_CommandLine()100%210%
set_CommandLine(...)100%11100%
get_CurrentDirectory()100%11100%
set_CurrentDirectory(...)100%11100%
get_ExecutableFullPath()100%11100%
set_ExecutableFullPath(...)100%11100%
get_Owner()100%11100%
set_Owner(...)100%11100%
get_HasError()100%11100%
set_HasError(...)100%11100%
get_IsCritical()100%11100%
set_IsCritical(...)100%11100%
get_IsKernelThread()100%11100%
set_IsKernelThread(...)100%11100%
SetError(...)100%22100%
.ctor(...)87.5%8.07889.47%
GetProcessOwner(...)100%1.36128.57%
GetCommandLine(...)50%3.19233.33%
GetCurrentDirectory(...)50%2.19263.64%
GetExecutablePath(...)50%4.91461.54%
GetStat(...)100%1.42125%
GetStartTime(...)50%3.03236.36%

File(s)

/home/runner/work/LockCheck/LockCheck/src/LockCheck/Linux/ProcInfo.cs

#LineLine coverage
 1using System;
 2using System.Diagnostics;
 3using System.IO;
 4
 5namespace LockCheck.Linux;
 6
 7[DebuggerDisplay("{HasError} {ProcessId} {ExecutableFullPath}")]
 8internal class ProcInfo : ILinuxProcessDetails, IHasErrorState
 9{
 10#if DEBUG
 11#pragma warning disable IDE0052
 12    private string? _errorStack;
 13    private Exception? _errorCause;
 14    private int _errorCode;
 15#pragma warning restore IDE0052
 16#endif
 17
 218    public int ProcessId { get; private set; }
 219    public DateTime StartTime { get; private set; }
 220    public int? ParentProcessId { get; private set; }
 221    public int SessionId { get; private set; }
 222    public string? ProcessName { get; private set; }
 223    public string? CommandLine { get; private set; }
 224    public string? CurrentDirectory { get; private set; }
 225    public string? ExecutableFullPath { get; private set; }
 226    public string? Owner { get; private set; }
 227    public bool HasError { get; private set; }
 228    public bool? IsCritical { get; private set; }
 229    public bool? IsKernelThread { get; private set; }
 30
 31    public void SetError(Exception? ex = null, int errorCode = 0)
 32    {
 233        if (!HasError)
 34        {
 235            HasError = true;
 36#if DEBUG
 37            if (Debugger.IsAttached)
 38            {
 39                // Support manual inspection at a later point
 40                _errorStack = Environment.StackTrace;
 41                _errorCause = ex;
 42                _errorCode = errorCode;
 43            }
 44#endif
 45        }
 246    }
 47
 248    public ProcInfo(int processId)
 49    {
 250        ProcessId = processId;
 51
 52        // Note: this is up front check. The process could vanish at any time later
 53        // while we attempt to get its properties.
 254        if (!ProcFileSystem.Exists(processId))
 55        {
 056            SetError();
 057            return;
 58        }
 59
 260        CommandLine = GetCommandLine(processId, this);
 261        CurrentDirectory = GetCurrentDirectory(processId, this);
 262        ExecutableFullPath = GetExecutablePath(processId, this);
 263        ProcessName = Path.GetFileName(ExecutableFullPath);
 264        Owner = GetProcessOwner(processId);
 265        StartTime = GetStartTime(processId, this);
 66
 267        var stat = GetStat(processId, this);
 268        ParentProcessId = stat.ParentProcessId;
 269        SessionId = stat.SessionId;
 270        IsKernelThread = stat.IsKernelThread;
 71
 72        //ParentProcessId = GetParentProcessId(processId, this);
 73        //SessionId = GetSessionId(processId, this);
 74        //IsKernelThread = GetIsKernelThread(processId, this);
 275        IsCritical = IsKernelThread.GetValueOrDefault() || NativeMethods.IsProcessCritical(ExecutableFullPath);
 76
 77        // Make sure that the current directory always ends with a slash. AFAICT that is never the case,
 78        // using DirectoryInfo and procfs. We do this for symmetry with the Windows code and also because
 79        // it makes "starts-with" checks easier.
 280        if (!string.IsNullOrEmpty(CurrentDirectory) && CurrentDirectory[CurrentDirectory.Length - 1] != '\\')
 81        {
 282            CurrentDirectory += "/";
 83        }
 284    }
 85
 86    private static string? GetProcessOwner(int pid)
 87    {
 88        try
 89        {
 290            return ProcFileSystem.GetProcessOwner(pid);
 91        }
 092        catch (UnauthorizedAccessException)
 93        {
 094        }
 095        catch (IOException)
 96        {
 97            // Don't set error state, just like the Windows version does not in this case.
 98            // The "Owner" field is not vital.
 099        }
 100
 0101        return null;
 2102    }
 103
 104    private static string? GetCommandLine(int pid, ProcInfo he)
 105    {
 106        try
 107        {
 2108            string[]? args = ProcFileSystem.GetProcessCommandLineArgs(pid);
 109
 2110            if (args == null)
 111            {
 0112                he.SetError();
 0113                return null;
 114            }
 115
 2116            return string.Join(" ", args);
 117        }
 0118        catch (UnauthorizedAccessException ex)
 119        {
 0120            he.SetError(ex);
 0121            return null;
 122        }
 0123        catch (IOException ex)
 124        {
 0125            he.SetError(ex);
 0126            return null;
 127        }
 2128    }
 129
 130    private static string? GetCurrentDirectory(int pid, ProcInfo he)
 131    {
 132        try
 133        {
 2134            string? cwd = ProcFileSystem.GetProcessCurrentDirectory(pid);
 135
 2136            if (cwd == null)
 137            {
 0138                he.SetError();
 139            }
 140
 2141            return cwd;
 142        }
 2143        catch (UnauthorizedAccessException ex)
 144        {
 2145            he.SetError(ex);
 2146            return null;
 147        }
 0148        catch (IOException ex)
 149        {
 0150            he.SetError(ex);
 0151            return null;
 152        }
 2153    }
 154
 155    private static string? GetExecutablePath(int pid, ProcInfo he)
 156    {
 157        try
 158        {
 2159            string? name = ProcFileSystem.GetProcessExecutablePath(pid);
 160
 2161            if (name == null)
 162            {
 0163                name = ProcFileSystem.GetProcessExecutablePathFromCmdLine(pid);
 164            }
 165
 2166            if (name == null)
 167            {
 0168                he.SetError();
 169            }
 170
 2171            return name;
 172        }
 2173        catch (UnauthorizedAccessException ex)
 174        {
 2175            he.SetError(ex);
 2176            return null;
 177        }
 0178        catch (IOException ex)
 179        {
 0180            he.SetError(ex);
 0181            return null;
 182        }
 2183    }
 184
 185    private static ProcFileSystem.Stat GetStat(int pid, ProcInfo he)
 186    {
 187        try
 188        {
 2189            return ProcFileSystem.GetStat(pid);
 190        }
 0191        catch (UnauthorizedAccessException)
 192        {
 0193            he.SetError();
 0194            return default;
 195        }
 0196        catch (IOException)
 197        {
 0198            he.SetError();
 0199            return default;
 200        }
 2201    }
 202
 203    //private static bool? GetIsKernelThread(int pid, ProcInfo he)
 204    //{
 205    //    try
 206    //    {
 207    //        return ProcFileSystem.IsKernelThread(pid);
 208    //    }
 209    //    catch (UnauthorizedAccessException)
 210    //    {
 211    //        he.SetError();
 212    //        return null;
 213    //    }
 214    //    catch (IOException)
 215    //    {
 216    //        he.SetError();
 217    //        return null;
 218    //    }
 219    //}
 220
 221    //private static int GetSessionId(int pid, ProcInfo he)
 222    //{
 223    //    try
 224    //    {
 225    //        int sessionId = ProcFileSystem.GetProcessSessionId(pid);
 226
 227    //        if (sessionId == -1)
 228    //        {
 229    //            he.SetError();
 230    //        }
 231
 232    //        return sessionId;
 233    //    }
 234    //    catch (UnauthorizedAccessException ex)
 235    //    {
 236    //        he.SetError(ex);
 237    //        return -1;
 238    //    }
 239    //    catch (IOException ex)
 240    //    {
 241    //        he.SetError(ex);
 242    //        return -1;
 243    //    }
 244    //}
 245
 246    //private static int? GetParentProcessId(int pid, ProcInfo he)
 247    //{
 248    //    try
 249    //    {
 250    //        var startTime = ProcFileSystem.GetParentProcessId(pid);
 251
 252    //        if (startTime == default)
 253    //        {
 254    //            he.SetError();
 255    //        }
 256
 257    //        return startTime;
 258    //    }
 259    //    catch (UnauthorizedAccessException ex)
 260    //    {
 261    //        he.SetError(ex);
 262    //        return default;
 263    //    }
 264    //    catch (IOException ex)
 265    //    {
 266    //        he.SetError(ex);
 267    //        return default;
 268    //    }
 269    //}
 270
 271    private static unsafe DateTime GetStartTime(int pid, ProcInfo he)
 272    {
 273        try
 274        {
 2275            var startTime = ProcFileSystem.GetProcessStartTime(pid);
 276
 2277            if (startTime == default)
 278            {
 0279                he.SetError();
 280            }
 281
 2282            return startTime;
 283        }
 0284        catch (UnauthorizedAccessException ex)
 285        {
 0286            he.SetError(ex);
 0287            return default;
 288        }
 0289        catch (IOException ex)
 290        {
 0291            he.SetError(ex);
 0292            return default;
 293        }
 2294    }
 295}