예제 #1
0
        public Job()
        {
            handle = CreateJobObject(IntPtr.Zero, null);
            if (handle.IsInvalid)
            {
                throw new Win32Exception();
            }

            var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION
            {
                BasicLimitInformation = new JOBOBJECT_BASIC_LIMIT_INFORMATION
                {
                    LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
                }
            };

            int    cbJobObjectInfo = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
            IntPtr lpJobObjectInfo = Marshal.AllocHGlobal(cbJobObjectInfo);

            try
            {
                Marshal.StructureToPtr(extendedInfo, lpJobObjectInfo, false);

                if (!SetInformationJobObject(handle,
                                             JobObjectInfoType.ExtendedLimitInformation,
                                             ref extendedInfo, cbJobObjectInfo))
                {
                    throw new Win32Exception();
                }
            }
            finally
            {
                Marshal.FreeHGlobal(lpJobObjectInfo);
            }
        }
예제 #2
0
        public ChildProcessManager(ulong memoryLimit)
        {
            _handle = new SafeJobHandle(CreateJobObject(IntPtr.Zero, null));

            var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION
            {
                LimitFlags = 0x00000100
            };

            var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION
            {
                BasicLimitInformation = info,
                ProcessMemoryLimit    = (UIntPtr)memoryLimit
            };

            var length          = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
            var extendedInfoPtr = Marshal.AllocHGlobal(length);

            Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false);

            if (!SetInformationJobObject(_handle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr,
                                         (uint)length))
            {
                throw new InvalidOperationException($"Unable to set information.  Error: {Marshal.GetLastWin32Error()}");
            }
        }
예제 #3
0
        public WindowsProcessJob(Process process)
        {
            IntPtr newHandle = Native.CreateJobObject(IntPtr.Zero, null);

            if (newHandle == IntPtr.Zero)
            {
                throw new InvalidOperationException("Unable to create a job.  Error: " + Marshal.GetLastWin32Error());
            }

            this.jobHandle = new SafeJobHandle(newHandle);

            Native.JOBOBJECT_BASIC_LIMIT_INFORMATION info = new Native.JOBOBJECT_BASIC_LIMIT_INFORMATION
            {
                LimitFlags = 0x2000 // JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
            };

            Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION extendedInfo = new Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION
            {
                BasicLimitInformation = info
            };

            int length = Marshal.SizeOf(typeof(Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION));

            if (!Native.SetInformationJobObject(this.jobHandle, Native.JobObjectInfoType.ExtendedLimitInformation, ref extendedInfo, (uint)length))
            {
                throw new InvalidOperationException("Unable to configure the job.  Error: " + Marshal.GetLastWin32Error());
            }

            if (!Native.AssignProcessToJobObject(this.jobHandle, process.Handle))
            {
                throw new InvalidOperationException("Unable to add process to the job.  Error: " + Marshal.GetLastWin32Error());
            }
        }
        public void Dispose()
        {
            if (_disposed) return;

            _handle.Dispose();
            _handle = null;
            _disposed = true;
        }
예제 #5
0
        public static unsafe NativeJob Create(string jobName)
        {
            SafeJobHandle handle = CreateJobObjectW(null, jobName);

            if (handle.IsInvalid)
            {
                throw new Win32Exception();
            }
            return(new NativeJob(handle));
        }
예제 #6
0
        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                this.jobHandle.Dispose();
                this.jobHandle = null;

                this.disposed = true;
            }
        }
예제 #7
0
        public static unsafe NativeJob Open(string jobName, JobAccessRights accessRights = JobAccessRights.All, bool inheritHandle = false)
        {
            SafeJobHandle handle = OpenJobObjectW((uint)accessRights, inheritHandle, jobName);

            if (handle.IsInvalid)
            {
                throw new Win32Exception();
            }
            return(new NativeJob(handle));
        }
예제 #8
0
 public void Dispose()
 {
     if (_disposed)
     {
         return;
     }
     _handle.Dispose();
     _handle   = null;
     _disposed = true;
 }
예제 #9
0
    public void Dispose()
    {
        if (_disposed)
        {
            return;
        }

        SetHandle(NativeMethods.JobObjectLimitFlags.JOB_NONE);

        _handle.Close();
        _handle.Dispose();
        _handle   = null;
        _disposed = true;
    }
예제 #10
0
        public void Dispose()
        {
            if (m_disposed)
            {
                return;
            }

            try
            {
                if (Common.IsPosixEnvironment)
                {
                    foreach (WeakReference <Process> childProcessReference in m_childProcesses)
                    {
                        Process childProcess;

                        if (!childProcessReference.TryGetTarget(out childProcess))
                        {
                            continue;
                        }

                        try
                        {
                            childProcess.Kill();
                        }
                        catch (Exception ex)
                        {
                            TerminationException?.Invoke(this, new EventArgs <Exception>(ex));
                        }
                    }
                }

                m_jobHandle?.Dispose();
                m_jobHandle = null;
            }
            finally
            {
                m_disposed = true;
                GC.SuppressFinalize(this);
            }
        }
예제 #11
0
        /// <summary>
        /// Creates a new <see cref="ChildProcessManager"/>.
        /// </summary>
        public ChildProcessManager()
        {
            if (Common.IsPosixEnvironment)
            {
                // On non-Windows operating systems we just track associated processes
                m_childProcesses = new List <WeakReference <Process> >();
            }
            else
            {
                // Let safe handle manage terminations on Windows
                GC.SuppressFinalize(this);

                // On Windows we add child processes to a job object such that when the job
                // is terminated, so are the child processes. Since safe handle ensures proper
                // closing of job handle, child processes will be terminated even if parent
                // process is abnormally terminated
                m_jobHandle = new SafeJobHandle(CreateJobObject(IntPtr.Zero, null));

                JOBOBJECT_BASIC_LIMIT_INFORMATION info = new JOBOBJECT_BASIC_LIMIT_INFORMATION
                {
                    LimitFlags = 0x2000
                };

                JOBOBJECT_EXTENDED_LIMIT_INFORMATION extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION
                {
                    BasicLimitInformation = info
                };

                int length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));

                IntPtr extendedInfoPtr = Marshal.AllocHGlobal(length);
                Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false);

                if (!SetInformationJobObject(m_jobHandle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr, (uint)length))
                {
                    throw new InvalidOperationException($"Unable to set information for ChildProcessManager job. Error: {Marshal.GetLastWin32Error()}");
                }
            }
        }
예제 #12
0
        public ChildProcessManager()
        {
            _handle = new SafeJobHandle(CreateJobObject(IntPtr.Zero, null));

            var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION
            {
                LimitFlags = 0x2000
            };

            var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION
            {
                BasicLimitInformation = info
            };

            var length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
            var extendedInfoPtr = Marshal.AllocHGlobal(length);
            Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false);

            if (!SetInformationJobObject(_handle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr,
                    (uint)length))
            {
                throw new InvalidOperationException($"Unable to set information.  Error: {Marshal.GetLastWin32Error()}");
            }
        }
예제 #13
0
 internal static extern Boolean SetInformationJobObject(SafeJobHandle hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, UInt32 cbJobObjectInfoLength);
예제 #14
0
 public static extern bool SetInformationJobObject(SafeJobHandle jobHandle, JobObjectInfoType infoType, [In] ref JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobObjectInfo, uint jobObjectInfoLength);
예제 #15
0
 internal static unsafe extern bool AssignProcessToJobObject(SafeJobHandle jobHandle, SafeProcessHandle procHandle);
예제 #16
0
 internal static extern unsafe bool QueryInformationJobObject(SafeJobHandle handle, JobInformationClass informationClass, void *buffer, uint bufferLength, out uint returnLength);
예제 #17
0
 internal static extern unsafe bool SetInformationJobObject(SafeJobHandle handle, JobInformationClass informationClass, void *buffer, uint bufferLength);
예제 #18
0
 internal static extern unsafe bool TerminateJobObject(SafeJobHandle handle, UInt32 exitCode);
예제 #19
0
 private static extern bool AssignProcessToJobObject(SafeJobHandle job, SafeProcessHandle process);
예제 #20
0
 private NativeJob(SafeJobHandle handle)
 {
     this.handle = handle;
 }
예제 #21
0
 private static extern bool SetInformationJobObject(SafeJobHandle hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength);
 public static extern bool SetInformationJobObject(SafeJobHandle hJob, JobObjectInfoType infoType,
                                                   ref JOBOBJECT_EXTENDED_LIMIT_INFORMATION lpJobObjectInfo, int cbJobObjectInfoLength);
예제 #23
0
 private static extern bool SetInformationJobObject(SafeJobHandle jobHandle, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength);
예제 #24
0
 internal static extern unsafe bool IsProcessInJobNative(SafeProcessHandle processHandle, SafeJobHandle jobHandle, [MarshalAs(UnmanagedType.Bool)] out bool result);
예제 #25
0
 public InformationObjectManager()
 {
     _handle = new SafeJobHandle(NativeMethods.CreateJobObject(IntPtr.Zero, null));
     SetHandle(NativeMethods.JobObjectLimitFlags.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE);
 }
예제 #26
0
 internal static extern unsafe bool UserHandleGrantAccess(IntPtr userHandle, SafeJobHandle jobHandle, [MarshalAs(UnmanagedType.Bool)] bool grant);
예제 #27
0
 private static extern bool AssignProcessToJobObject(SafeJobHandle jobHandle, SafeProcessHandle process);
예제 #28
0
 internal static extern Boolean AssignProcessToJobObject(SafeJobHandle job, SafeProcessHandle process);
 public static extern bool AssignProcessToJobObject(SafeJobHandle job, IntPtr process);
예제 #30
0
 private JobObject(SafeJobHandle safeHandle) => SafeHandle = safeHandle;