wabbajack/Wabbajack.Common/ChildProcessTracker.cs

159 lines
5.2 KiB
C#
Raw Permalink Normal View History

2021-09-27 12:42:46 +00:00
using System;
2019-09-10 20:10:53 +00:00
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Common;
2021-09-27 12:42:46 +00:00
#if _WINDOWS
2021-10-23 16:51:17 +00:00
#region Windows
/// <summary>
/// Allows processes to be automatically killed if this parent process unexpectedly quits.
/// This feature requires Windows 8 or greater. On Windows 7, nothing is done.
/// </summary>
/// <remarks>
/// References:
/// https://stackoverflow.com/a/4657392/386091
/// https://stackoverflow.com/a/9164742/386091
/// </remarks>
public static class ChildProcessTracker
{
// Windows will automatically close any open job handles when our process terminates.
// This can be verified by using SysInternals' Handle utility. When the job handle
// is closed, the child processes will be killed.
private static readonly IntPtr s_jobHandle;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
static ChildProcessTracker()
2019-09-10 20:10:53 +00:00
{
2021-10-23 16:51:17 +00:00
// This feature requires Windows 8 or later. To support Windows 7 requires
// registry settings to be added if you are using Visual Studio plus an
// app.manifest change.
// https://stackoverflow.com/a/4232259/386091
// https://stackoverflow.com/a/9507862/386091
if (Environment.OSVersion.Version < new Version(6, 2))
return;
// The job name is optional (and can be null) but it helps with diagnostics.
// If it's not null, it has to be unique. Use SysInternals' Handle command-line
// utility: handle -a ChildProcessTracker
var jobName = "ChildProcessTracker" + Process.GetCurrentProcess().Id;
s_jobHandle = CreateJobObject(IntPtr.Zero, jobName);
var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION();
// This is the key flag. When our process is killed, Windows will automatically
// close the job handle, and when that happens, we want the child processes to
// be killed, too.
info.LimitFlags = JOBOBJECTLIMIT.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION();
extendedInfo.BasicLimitInformation = info;
var length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
var extendedInfoPtr = Marshal.AllocHGlobal(length);
try
2019-09-10 20:10:53 +00:00
{
2021-10-23 16:51:17 +00:00
Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false);
2019-09-10 20:10:53 +00:00
2021-10-23 16:51:17 +00:00
if (!SetInformationJobObject(s_jobHandle, JobObjectInfoType.ExtendedLimitInformation,
extendedInfoPtr, (uint) length))
throw new Win32Exception();
}
finally
2019-09-14 04:35:42 +00:00
{
2021-10-23 16:51:17 +00:00
Marshal.FreeHGlobal(extendedInfoPtr);
2019-09-14 04:35:42 +00:00
}
2019-09-10 20:10:53 +00:00
}
2021-10-23 16:51:17 +00:00
/// <summary>
/// Add the process to be tracked. If our current process is killed, the child processes
/// that we are tracking will be automatically killed, too. If the child process terminates
/// first, that's fine, too.
/// </summary>
/// <param name="process"></param>
public static void AddProcess(Process process)
2019-09-10 20:10:53 +00:00
{
2021-10-23 16:51:17 +00:00
if (s_jobHandle != IntPtr.Zero)
{
var success = AssignProcessToJobObject(s_jobHandle, process.Handle);
if (!success && !process.HasExited)
throw new Win32Exception();
}
2019-09-10 20:10:53 +00:00
}
2021-10-23 16:51:17 +00:00
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr CreateJobObject(IntPtr lpJobAttributes, string name);
2019-09-10 20:10:53 +00:00
2021-10-23 16:51:17 +00:00
[DllImport("kernel32.dll")]
private static extern bool SetInformationJobObject(IntPtr job, JobObjectInfoType infoType,
IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength);
2019-09-10 20:10:53 +00:00
2021-10-23 16:51:17 +00:00
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process);
}
2019-09-10 20:10:53 +00:00
2021-10-23 16:51:17 +00:00
public enum JobObjectInfoType
{
AssociateCompletionPortInformation = 7,
BasicLimitInformation = 2,
BasicUIRestrictions = 4,
EndOfJobTimeInformation = 6,
ExtendedLimitInformation = 9,
SecurityLimitInformation = 5,
GroupInformation = 11
}
[StructLayout(LayoutKind.Sequential)]
public struct JOBOBJECT_BASIC_LIMIT_INFORMATION
{
public long PerProcessUserTimeLimit;
public long PerJobUserTimeLimit;
public JOBOBJECTLIMIT LimitFlags;
public UIntPtr MinimumWorkingSetSize;
public UIntPtr MaximumWorkingSetSize;
public uint ActiveProcessLimit;
public long Affinity;
public uint PriorityClass;
public uint SchedulingClass;
}
[Flags]
public enum JOBOBJECTLIMIT : uint
{
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x2000
}
[StructLayout(LayoutKind.Sequential)]
public struct IO_COUNTERS
{
public ulong ReadOperationCount;
public ulong WriteOperationCount;
public ulong OtherOperationCount;
public ulong ReadTransferCount;
public ulong WriteTransferCount;
public ulong OtherTransferCount;
}
[StructLayout(LayoutKind.Sequential)]
public struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION
{
public JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation;
public IO_COUNTERS IoInfo;
public UIntPtr ProcessMemoryLimit;
public UIntPtr JobMemoryLimit;
public UIntPtr PeakProcessMemoryUsed;
public UIntPtr PeakJobMemoryUsed;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
#endregion
2021-09-27 12:42:46 +00:00
#else
public static class ChildProcessTracker
{
public static void AddProcess(Process process)
{
}
}
2021-10-23 16:51:17 +00:00
#endif