v2rayN/v2rayN/ServiceLib/Common/Job.cs

181 lines
5.1 KiB
C#
Raw Normal View History

2025-01-30 09:10:05 +00:00
using System.Diagnostics;
2019-10-11 06:15:20 +00:00
using System.Runtime.InteropServices;
namespace ServiceLib.Common;
2019-10-11 06:15:20 +00:00
/*
* See:
* http://stackoverflow.com/questions/6266820/working-example-of-createjobobject-setinformationjobobject-pinvoke-in-net
*/
2023-04-14 12:49:36 +00:00
2024-10-14 02:57:40 +00:00
public sealed class Job : IDisposable
2019-10-11 06:15:20 +00:00
{
private IntPtr handle = IntPtr.Zero;
public Job()
{
handle = CreateJobObject(IntPtr.Zero, null);
2025-03-06 02:46:41 +00:00
var extendedInfoPtr = IntPtr.Zero;
var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION
2019-10-11 06:15:20 +00:00
{
LimitFlags = 0x2000
};
2025-03-06 02:46:41 +00:00
var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION
2019-10-11 06:15:20 +00:00
{
BasicLimitInformation = info
};
try
{
2025-03-06 02:46:41 +00:00
var length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
2019-10-11 06:15:20 +00:00
extendedInfoPtr = Marshal.AllocHGlobal(length);
Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false);
if (!SetInformationJobObject(handle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr,
2023-01-01 11:42:01 +00:00
(uint)length))
2025-03-06 02:46:41 +00:00
{
2019-10-11 06:15:20 +00:00
throw new Exception(string.Format("Unable to set information. Error: {0}",
Marshal.GetLastWin32Error()));
2025-03-06 02:46:41 +00:00
}
2019-10-11 06:15:20 +00:00
}
finally
{
if (extendedInfoPtr != IntPtr.Zero)
{
Marshal.FreeHGlobal(extendedInfoPtr);
}
}
}
public bool AddProcess(IntPtr processHandle)
{
2025-03-06 02:46:41 +00:00
var succ = AssignProcessToJobObject(handle, processHandle);
2019-10-11 06:15:20 +00:00
if (!succ)
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog("Failed to call AssignProcessToJobObject! GetLastError=" + Marshal.GetLastWin32Error());
2019-10-11 06:15:20 +00:00
}
return succ;
}
public bool AddProcess(int processId)
{
return AddProcess(Process.GetProcessById(processId).Handle);
}
#region IDisposable
private bool disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
2024-10-14 02:57:40 +00:00
private void Dispose(bool disposing)
2019-10-11 06:15:20 +00:00
{
2025-01-30 09:10:05 +00:00
if (disposed)
2025-03-06 02:46:41 +00:00
{
2025-01-30 09:10:05 +00:00
return;
2025-03-06 02:46:41 +00:00
}
2019-10-11 06:15:20 +00:00
disposed = true;
if (disposing)
{
// no managed objects to free
}
if (handle != IntPtr.Zero)
{
CloseHandle(handle);
handle = IntPtr.Zero;
}
}
~Job()
{
Dispose(false);
}
2023-04-14 12:49:36 +00:00
#endregion IDisposable
2019-10-11 06:15:20 +00:00
#region Interop
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
2023-02-19 05:34:22 +00:00
private static extern IntPtr CreateJobObject(IntPtr a, string? lpName);
2019-10-11 06:15:20 +00:00
[DllImport("kernel32.dll", SetLastError = true)]
2025-03-06 02:46:41 +00:00
private static extern bool SetInformationJobObject(IntPtr hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength);
2019-10-11 06:15:20 +00:00
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr hObject);
2023-04-14 12:49:36 +00:00
#endregion Interop
2019-10-11 06:15:20 +00:00
}
#region Helper classes
[StructLayout(LayoutKind.Sequential)]
2023-04-14 12:49:36 +00:00
internal struct IO_COUNTERS
2019-10-11 06:15:20 +00:00
{
2025-03-06 02:46:41 +00:00
public ulong ReadOperationCount;
public ulong WriteOperationCount;
public ulong OtherOperationCount;
public ulong ReadTransferCount;
public ulong WriteTransferCount;
public ulong OtherTransferCount;
2019-10-11 06:15:20 +00:00
}
[StructLayout(LayoutKind.Sequential)]
2023-04-14 12:49:36 +00:00
internal struct JOBOBJECT_BASIC_LIMIT_INFORMATION
2019-10-11 06:15:20 +00:00
{
2025-03-06 02:46:41 +00:00
public long PerProcessUserTimeLimit;
public long PerJobUserTimeLimit;
public uint LimitFlags;
2019-10-11 06:15:20 +00:00
public UIntPtr MinimumWorkingSetSize;
public UIntPtr MaximumWorkingSetSize;
2025-03-06 02:46:41 +00:00
public uint ActiveProcessLimit;
2019-10-11 06:15:20 +00:00
public UIntPtr Affinity;
2025-03-06 02:46:41 +00:00
public uint PriorityClass;
public uint SchedulingClass;
2019-10-11 06:15:20 +00:00
}
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
2025-03-06 02:46:41 +00:00
public uint nLength;
2019-10-11 06:15:20 +00:00
public IntPtr lpSecurityDescriptor;
2025-03-06 02:46:41 +00:00
public int bInheritHandle;
2019-10-11 06:15:20 +00:00
}
[StructLayout(LayoutKind.Sequential)]
2023-04-14 12:49:36 +00:00
internal struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION
2019-10-11 06:15:20 +00:00
{
public JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation;
public IO_COUNTERS IoInfo;
public UIntPtr ProcessMemoryLimit;
public UIntPtr JobMemoryLimit;
public UIntPtr PeakProcessMemoryUsed;
public UIntPtr PeakJobMemoryUsed;
}
public enum JobObjectInfoType
{
AssociateCompletionPortInformation = 7,
BasicLimitInformation = 2,
BasicUIRestrictions = 4,
EndOfJobTimeInformation = 6,
ExtendedLimitInformation = 9,
SecurityLimitInformation = 5,
GroupInformation = 11
}
2023-04-14 12:49:36 +00:00
#endregion Helper classes