-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Procgov returns the exit code of the launched monitored process (#66)
- Loading branch information
1 parent
fbeb991
commit 31304fe
Showing
3 changed files
with
35 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,24 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace ProcessGovernor; | ||
|
||
public sealed class Win32Job : IDisposable | ||
public record Win32Job(SafeHandle JobHandle, string JobName, SafeHandle? ProcessHandle = null, long ClockTimeLimitInMilliseconds = 0L) : IDisposable | ||
{ | ||
private readonly SafeHandle hJob; | ||
private readonly string jobName; | ||
private readonly Stopwatch? stopWatch; | ||
private readonly long clockTimeLimitInMilliseconds; | ||
private readonly DateTime startTimeUtc = DateTime.UtcNow; | ||
|
||
public Win32Job(SafeHandle hJob, string jobName, long clockTimeLimitInMilliseconds = 0L) | ||
{ | ||
this.hJob = hJob; | ||
this.jobName = jobName; | ||
|
||
this.clockTimeLimitInMilliseconds = clockTimeLimitInMilliseconds; | ||
stopWatch = clockTimeLimitInMilliseconds > 0 ? Stopwatch.StartNew() : null; | ||
} | ||
|
||
public SafeHandle JobHandle => hJob; | ||
|
||
public string JobName => jobName; | ||
public bool IsTimedOut => ClockTimeLimitInMilliseconds > 0 | ||
&& DateTime.UtcNow.Subtract(startTimeUtc).TotalMilliseconds >= ClockTimeLimitInMilliseconds; | ||
|
||
public bool IsTimedOut => stopWatch != null && stopWatch.ElapsedMilliseconds > clockTimeLimitInMilliseconds; | ||
// When we are monitoring only a specific process, we will wait for its termination. Otherwise, | ||
// we will wait for the job object to be signaled. | ||
public SafeHandle WaitHandle => ProcessHandle ?? JobHandle; | ||
|
||
public void Dispose() | ||
{ | ||
hJob.Dispose(); | ||
JobHandle.Dispose(); | ||
if (ProcessHandle is { } h && !h.IsInvalid) | ||
{ | ||
h.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters