tgstation-server 5.12.7
The /tg/station 13 server suite
Loading...
Searching...
No Matches
PosixProcessFeatures.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4
5using Microsoft.Extensions.Logging;
6using Mono.Unix;
7using Mono.Unix.Native;
8
12
14{
17 {
21 readonly Lazy<IProcessExecutor> lazyLoadedProcessExecutor;
22
27
31 readonly ILogger<PosixProcessFeatures> logger;
32
39 public PosixProcessFeatures(Lazy<IProcessExecutor> lazyLoadedProcessExecutor, IIOManager ioManager, ILogger<PosixProcessFeatures> logger)
40 {
41 this.lazyLoadedProcessExecutor = lazyLoadedProcessExecutor ?? throw new ArgumentNullException(nameof(lazyLoadedProcessExecutor));
42 this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager));
43 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
44 }
45
47 public void ResumeProcess(global::System.Diagnostics.Process process)
48 {
49 var result = Syscall.kill(process.Id, Signum.SIGCONT);
50 if (result != 0)
51 throw new UnixIOException(Stdlib.GetLastError());
52 }
53
55 public void SuspendProcess(global::System.Diagnostics.Process process)
56 {
57 var result = Syscall.kill(process.Id, Signum.SIGSTOP);
58 if (result != 0)
59 throw new UnixIOException(Stdlib.GetLastError());
60 }
61
63 public Task<string> GetExecutingUsername(global::System.Diagnostics.Process process, CancellationToken cancellationToken)
64 => throw new NotSupportedException();
65
67 public async Task CreateDump(global::System.Diagnostics.Process process, string outputFile, CancellationToken cancellationToken)
68 {
69 ArgumentNullException.ThrowIfNull(process);
70 ArgumentNullException.ThrowIfNull(outputFile);
71
72 const string GCorePath = "/usr/bin/gcore";
73 if (!await ioManager.FileExists(GCorePath, cancellationToken))
74 throw new JobException(ErrorCode.MissingGCore);
75
76 int pid;
77 try
78 {
79 if (process.HasExited)
80 throw new JobException(ErrorCode.DreamDaemonOffline);
81
82 pid = process.Id;
83 }
84 catch (InvalidOperationException ex)
85 {
86 throw new JobException(ErrorCode.DreamDaemonOffline, ex);
87 }
88
89 string output;
90 int exitCode;
91 await using (var gcoreProc = await lazyLoadedProcessExecutor.Value.LaunchProcess(
92 GCorePath,
93 Environment.CurrentDirectory,
94 $"-o {outputFile} {process.Id}",
95 readStandardHandles: true,
96 noShellExecute: true))
97 {
98 using (cancellationToken.Register(() => gcoreProc.Terminate()))
99 exitCode = await gcoreProc.Lifetime;
100
101 output = await gcoreProc.GetCombinedOutput(cancellationToken);
102 logger.LogDebug("gcore output:{0}{1}", Environment.NewLine, output);
103 }
104
105 if (exitCode != 0)
106 throw new JobException(
107 ErrorCode.GCoreFailure,
108 new JobException(
109 $"Exit Code: {exitCode}{Environment.NewLine}Output:{Environment.NewLine}{output}"));
110
111 // gcore outputs name.pid so remove the pid part
112 var generatedGCoreFile = $"{outputFile}.{pid}";
113 await ioManager.MoveFile(generatedGCoreFile, outputFile, cancellationToken);
114 }
115 }
116}
Operation exceptions thrown from the context of a Models.Job.
Definition: JobException.cs:11
Task< string > GetExecutingUsername(global::System.Diagnostics.Process process, CancellationToken cancellationToken)
Get the name of the user executing a given process . The name of the user executing process .
void SuspendProcess(global::System.Diagnostics.Process process)
Suspend a given process .
readonly IIOManager ioManager
The IIOManager for the PosixProcessFeatures.
readonly Lazy< IProcessExecutor > lazyLoadedProcessExecutor
Lazy<T> loaded IProcessExecutor.
void ResumeProcess(global::System.Diagnostics.Process process)
Resume a given suspended Process.
PosixProcessFeatures(Lazy< IProcessExecutor > lazyLoadedProcessExecutor, IIOManager ioManager, ILogger< PosixProcessFeatures > logger)
Initializes a new instance of the PosixProcessFeatures class.
readonly ILogger< PosixProcessFeatures > logger
The ILogger<TCategoryName> for the PosixProcessFeatures.
async Task CreateDump(global::System.Diagnostics.Process process, string outputFile, CancellationToken cancellationToken)
Create a dump file for a given process . A Task representing the running operation.
Interface for using filesystems.
Definition: IIOManager.cs:13
Task MoveFile(string source, string destination, CancellationToken cancellationToken)
Moves a file at source to destination .
Task< bool > FileExists(string path, CancellationToken cancellationToken)
Check that the file at path exists.
Abstraction for suspending and resuming processes.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition: ErrorCode.cs:11