Beispiel #1
0
        public void ShouldKillProcessOnTerminate()
        {
            using (var job = new JobObject())
            {
                job.SetLimits(new JobObjectLimits()
                {
                    Flags = JobObjectLimitFlags.DieOnUnhandledException | JobObjectLimitFlags.KillOnJobClose,
                });

                var psi = new ProcessStartInfo
                {
                    FileName        = "ping",
                    Arguments       = "127.0.0.1 -n 100",
                    UseShellExecute = true,
                    CreateNoWindow  = true,
                };

                using (var process = Process.Start(psi))
                {
                    Assert.False(process.WaitForExit(500)); // Ensure process is started

                    job.AssignProcess(process);
                    job.Terminate();

                    process.WaitForExit();
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Tries to kill the process and all child processes.
        /// </summary>
        /// <remarks>
        /// It's okay to call this method at any time; however, before process start and after process termination or disposing of
        /// this instance, it does nothing.
        /// </remarks>
        public void Kill(int exitCode)
        {
            // Notify the injected that the process is being killed
            m_processInjector?.OnKilled();

            var processHandle = m_processHandle;

            if (processHandle != null && !processHandle.IsInvalid)
            {
                // Ignore result, as there is a race with regular process termination that we cannot do anything about.
                m_killed = true;

                // No job object means that we are on an old OS; let's just terminate this process (we can't reliably terminate all child processes)
                Analysis.IgnoreResult(Native.Processes.ProcessUtilities.TerminateProcess(processHandle, exitCode));
            }

            JobObject jobObject = m_job;

            if (jobObject != null)
            {
                // Ignore result, as there is a race with regular shutdown.
                m_killed = true;

                Analysis.IgnoreResult(jobObject.Terminate(exitCode));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Tries to kill the process and all child processes.
        /// </summary>
        /// <remarks>
        /// It's okay to call this method at any time; however, before process start and after process termination or disposing of
        /// this instance, it does nothing.
        /// </remarks>
        public void Kill(int exitCode)
        {
            // Notify the injected that the process is being killed
            m_processInjector?.OnKilled();
            LogDiagnostics($"Process will be killed with exit code {exitCode}");

            var processHandle = m_processHandle;

            if (processHandle != null && !processHandle.IsInvalid)
            {
                // Ignore result, as there is a race with regular process termination that we cannot do anything about.
                m_killed = true;
                // No job object means that we are on an old OS; let's just terminate this process (we can't reliably terminate all child processes)
                Analysis.IgnoreResult(Native.Processes.ProcessUtilities.TerminateProcess(processHandle, exitCode));
                LogDiagnostics("DetouredProcess is killed using TerminateProcess");
            }

            using (m_queryJobDataLock.AcquireWriteLock())
            {
                JobObject jobObject = m_job;
                if (jobObject != null)
                {
                    // Ignore result, as there is a race with regular shutdown.
                    m_killed = true;
                    Analysis.IgnoreResult(jobObject.Terminate(exitCode));

                    LogDiagnostics("DetouredProcess is killed via JobObject termination");
                }
            }

            LogDiagnostics("Stack trace:");
            LogDiagnostics(Environment.StackTrace);
        }