| | 1 | | using System; |
| | 2 | | using System.Diagnostics; |
| | 3 | | using System.IO; |
| | 4 | |
|
| | 5 | | namespace LockCheck.Linux; |
| | 6 | |
|
| | 7 | | [DebuggerDisplay("{HasError} {ProcessId} {ExecutableFullPath}")] |
| | 8 | | internal 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 | |
|
| 2 | 18 | | public int ProcessId { get; private set; } |
| 2 | 19 | | public DateTime StartTime { get; private set; } |
| 2 | 20 | | public int? ParentProcessId { get; private set; } |
| 2 | 21 | | public int SessionId { get; private set; } |
| 2 | 22 | | public string? ProcessName { get; private set; } |
| 2 | 23 | | public string? CommandLine { get; private set; } |
| 2 | 24 | | public string? CurrentDirectory { get; private set; } |
| 2 | 25 | | public string? ExecutableFullPath { get; private set; } |
| 2 | 26 | | public string? Owner { get; private set; } |
| 2 | 27 | | public bool HasError { get; private set; } |
| 2 | 28 | | public bool? IsCritical { get; private set; } |
| 2 | 29 | | public bool? IsKernelThread { get; private set; } |
| | 30 | |
|
| | 31 | | public void SetError(Exception? ex = null, int errorCode = 0) |
| | 32 | | { |
| 2 | 33 | | if (!HasError) |
| | 34 | | { |
| 2 | 35 | | 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 | | } |
| 2 | 46 | | } |
| | 47 | |
|
| 2 | 48 | | public ProcInfo(int processId) |
| | 49 | | { |
| 2 | 50 | | 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. |
| 2 | 54 | | if (!ProcFileSystem.Exists(processId)) |
| | 55 | | { |
| 0 | 56 | | SetError(); |
| 0 | 57 | | return; |
| | 58 | | } |
| | 59 | |
|
| 2 | 60 | | CommandLine = GetCommandLine(processId, this); |
| 2 | 61 | | CurrentDirectory = GetCurrentDirectory(processId, this); |
| 2 | 62 | | ExecutableFullPath = GetExecutablePath(processId, this); |
| 2 | 63 | | ProcessName = Path.GetFileName(ExecutableFullPath); |
| 2 | 64 | | Owner = GetProcessOwner(processId); |
| 2 | 65 | | StartTime = GetStartTime(processId, this); |
| | 66 | |
|
| 2 | 67 | | var stat = GetStat(processId, this); |
| 2 | 68 | | ParentProcessId = stat.ParentProcessId; |
| 2 | 69 | | SessionId = stat.SessionId; |
| 2 | 70 | | IsKernelThread = stat.IsKernelThread; |
| | 71 | |
|
| | 72 | | //ParentProcessId = GetParentProcessId(processId, this); |
| | 73 | | //SessionId = GetSessionId(processId, this); |
| | 74 | | //IsKernelThread = GetIsKernelThread(processId, this); |
| 2 | 75 | | 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. |
| 2 | 80 | | if (!string.IsNullOrEmpty(CurrentDirectory) && CurrentDirectory[CurrentDirectory.Length - 1] != '\\') |
| | 81 | | { |
| 2 | 82 | | CurrentDirectory += "/"; |
| | 83 | | } |
| 2 | 84 | | } |
| | 85 | |
|
| | 86 | | private static string? GetProcessOwner(int pid) |
| | 87 | | { |
| | 88 | | try |
| | 89 | | { |
| 2 | 90 | | return ProcFileSystem.GetProcessOwner(pid); |
| | 91 | | } |
| 0 | 92 | | catch (UnauthorizedAccessException) |
| | 93 | | { |
| 0 | 94 | | } |
| 0 | 95 | | 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. |
| 0 | 99 | | } |
| | 100 | |
|
| 0 | 101 | | return null; |
| 2 | 102 | | } |
| | 103 | |
|
| | 104 | | private static string? GetCommandLine(int pid, ProcInfo he) |
| | 105 | | { |
| | 106 | | try |
| | 107 | | { |
| 2 | 108 | | string[]? args = ProcFileSystem.GetProcessCommandLineArgs(pid); |
| | 109 | |
|
| 2 | 110 | | if (args == null) |
| | 111 | | { |
| 0 | 112 | | he.SetError(); |
| 0 | 113 | | return null; |
| | 114 | | } |
| | 115 | |
|
| 2 | 116 | | return string.Join(" ", args); |
| | 117 | | } |
| 0 | 118 | | catch (UnauthorizedAccessException ex) |
| | 119 | | { |
| 0 | 120 | | he.SetError(ex); |
| 0 | 121 | | return null; |
| | 122 | | } |
| 0 | 123 | | catch (IOException ex) |
| | 124 | | { |
| 0 | 125 | | he.SetError(ex); |
| 0 | 126 | | return null; |
| | 127 | | } |
| 2 | 128 | | } |
| | 129 | |
|
| | 130 | | private static string? GetCurrentDirectory(int pid, ProcInfo he) |
| | 131 | | { |
| | 132 | | try |
| | 133 | | { |
| 2 | 134 | | string? cwd = ProcFileSystem.GetProcessCurrentDirectory(pid); |
| | 135 | |
|
| 2 | 136 | | if (cwd == null) |
| | 137 | | { |
| 0 | 138 | | he.SetError(); |
| | 139 | | } |
| | 140 | |
|
| 2 | 141 | | return cwd; |
| | 142 | | } |
| 2 | 143 | | catch (UnauthorizedAccessException ex) |
| | 144 | | { |
| 2 | 145 | | he.SetError(ex); |
| 2 | 146 | | return null; |
| | 147 | | } |
| 0 | 148 | | catch (IOException ex) |
| | 149 | | { |
| 0 | 150 | | he.SetError(ex); |
| 0 | 151 | | return null; |
| | 152 | | } |
| 2 | 153 | | } |
| | 154 | |
|
| | 155 | | private static string? GetExecutablePath(int pid, ProcInfo he) |
| | 156 | | { |
| | 157 | | try |
| | 158 | | { |
| 2 | 159 | | string? name = ProcFileSystem.GetProcessExecutablePath(pid); |
| | 160 | |
|
| 2 | 161 | | if (name == null) |
| | 162 | | { |
| 0 | 163 | | name = ProcFileSystem.GetProcessExecutablePathFromCmdLine(pid); |
| | 164 | | } |
| | 165 | |
|
| 2 | 166 | | if (name == null) |
| | 167 | | { |
| 0 | 168 | | he.SetError(); |
| | 169 | | } |
| | 170 | |
|
| 2 | 171 | | return name; |
| | 172 | | } |
| 2 | 173 | | catch (UnauthorizedAccessException ex) |
| | 174 | | { |
| 2 | 175 | | he.SetError(ex); |
| 2 | 176 | | return null; |
| | 177 | | } |
| 0 | 178 | | catch (IOException ex) |
| | 179 | | { |
| 0 | 180 | | he.SetError(ex); |
| 0 | 181 | | return null; |
| | 182 | | } |
| 2 | 183 | | } |
| | 184 | |
|
| | 185 | | private static ProcFileSystem.Stat GetStat(int pid, ProcInfo he) |
| | 186 | | { |
| | 187 | | try |
| | 188 | | { |
| 2 | 189 | | return ProcFileSystem.GetStat(pid); |
| | 190 | | } |
| 0 | 191 | | catch (UnauthorizedAccessException) |
| | 192 | | { |
| 0 | 193 | | he.SetError(); |
| 0 | 194 | | return default; |
| | 195 | | } |
| 0 | 196 | | catch (IOException) |
| | 197 | | { |
| 0 | 198 | | he.SetError(); |
| 0 | 199 | | return default; |
| | 200 | | } |
| 2 | 201 | | } |
| | 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 | | { |
| 2 | 275 | | var startTime = ProcFileSystem.GetProcessStartTime(pid); |
| | 276 | |
|
| 2 | 277 | | if (startTime == default) |
| | 278 | | { |
| 0 | 279 | | he.SetError(); |
| | 280 | | } |
| | 281 | |
|
| 2 | 282 | | return startTime; |
| | 283 | | } |
| 0 | 284 | | catch (UnauthorizedAccessException ex) |
| | 285 | | { |
| 0 | 286 | | he.SetError(ex); |
| 0 | 287 | | return default; |
| | 288 | | } |
| 0 | 289 | | catch (IOException ex) |
| | 290 | | { |
| 0 | 291 | | he.SetError(ex); |
| 0 | 292 | | return default; |
| | 293 | | } |
| 2 | 294 | | } |
| | 295 | | } |