1 using Microsoft.Extensions.Logging;
3 using Mono.Unix.Native;
9 using System.Threading.Tasks;
32 readonly ILogger<PosixProcessFeatures>
logger;
42 this.lazyLoadedProcessExecutor = lazyLoadedProcessExecutor ??
throw new ArgumentNullException(nameof(lazyLoadedProcessExecutor));
43 this.ioManager = ioManager ??
throw new ArgumentNullException(nameof(ioManager));
44 this.logger = logger ??
throw new ArgumentNullException(nameof(logger));
52 var result = Syscall.kill(process.Id, Signum.SIGCONT);
54 throw new UnixIOException(result);
55 logger.LogTrace(
"Resumed PID {0}", process.Id);
59 logger.LogError(e,
"Failed to resume PID {0}!", process.Id);
69 var result = Syscall.kill(process.Id, Signum.SIGSTOP);
71 throw new UnixIOException(result);
72 logger.LogTrace(
"Resumed PID {0}", process.Id);
76 logger.LogError(e,
"Failed to suspend PID {0}!", process.Id);
82 public async Task<string>
GetExecutingUsername(global::System.Diagnostics.Process process, CancellationToken cancellationToken)
85 throw new ArgumentNullException(nameof(process));
91 var statusFile = ioManager.ConcatPath(
"/proc", pid.ToString(CultureInfo.InvariantCulture),
"status");
92 var statusBytes = await ioManager.ReadAllBytes(statusFile, cancellationToken).ConfigureAwait(
false);
93 var statusText = Encoding.UTF8.GetString(statusBytes);
94 var splits = statusText.Split(
'\n', StringSplitOptions.RemoveEmptyEntries);
95 var entry = splits.FirstOrDefault(x => x.Trim().StartsWith(
"Uid:", StringComparison.Ordinal));
101 .Split(
' ', StringSplitOptions.RemoveEmptyEntries)
102 .FirstOrDefault(x => !String.IsNullOrWhiteSpace(x))
107 public async Task
CreateDump(global::System.Diagnostics.Process process,
string outputFile, CancellationToken cancellationToken)
110 throw new ArgumentNullException(nameof(process));
111 if (outputFile == null)
112 throw new ArgumentNullException(nameof(outputFile));
114 const string GCorePath =
"/usr/bin/gcore";
115 if (!await ioManager.FileExists(GCorePath, cancellationToken).ConfigureAwait(
false))
118 var pid = process.Id;
121 using (var gcoreProc = lazyLoadedProcessExecutor.Value.LaunchProcess(
123 Environment.CurrentDirectory,
124 $
"-o {outputFile} {process.Id}",
129 using (cancellationToken.Register(() => gcoreProc.Terminate()))
130 exitCode = await gcoreProc.Lifetime.ConfigureAwait(
false);
132 output = gcoreProc.GetCombinedOutput();
133 logger.LogDebug(
"gcore output:{0}{1}", Environment.NewLine, output);
140 $
"Exit Code: {exitCode}{Environment.NewLine}Output:{Environment.NewLine}{output}"));
143 var generatedGCoreFile = $
"{outputFile}.{pid}";
144 await ioManager.MoveFile(generatedGCoreFile, outputFile, cancellationToken).ConfigureAwait(
false);
readonly IIOManager ioManager
The IIOManager for the PosixProcessFeatures.
readonly Lazy< IProcessExecutor > lazyLoadedProcessExecutor
Lazy<T> loaded IProcessExecutor.
ErrorCode
Types of ErrorMessages that the API may return.
Use server authentication
PosixProcessFeatures(Lazy< IProcessExecutor > lazyLoadedProcessExecutor, IIOManager ioManager, ILogger< PosixProcessFeatures > logger)
Initializes a new instance of the PosixProcessFeatures .
async Task CreateDump(global::System.Diagnostics.Process process, string outputFile, CancellationToken cancellationToken)
Create a dump file for a given process .
readonly ILogger< PosixProcessFeatures > logger
The ILogger<TCategoryName> for the PosixProcessFeatures.
Operation exceptions thrown from the context of a Models.Job
void SuspendProcess(global::System.Diagnostics.Process process)
Suspend a given process .
async Task< string > GetExecutingUsername(global::System.Diagnostics.Process process, CancellationToken cancellationToken)
Get the name of the user executing a given process .
Abstraction for suspending and resuming processes.
Interface for using filesystems
void ResumeProcess(global::System.Diagnostics.Process process)
Resume a given suspended Process.