| 82 | } // namespace internal { |
| 83 | |
| 84 | inline Subprocess::ParentHook Subprocess::ParentHook::CREATE_JOB() { |
| 85 | return Subprocess::ParentHook([](pid_t pid) -> Try<Nothing> { |
| 86 | // NOTE: There are two very important parts to this hook. First, Windows |
| 87 | // does not have a process hierarchy in the same sense that Unix does, so |
| 88 | // in order to be able to kill a task, we have to put it in a job object. |
| 89 | // Then, when we terminate the job object, it will terminate all the |
| 90 | // processes in the task (including any processes that were subsequently |
| 91 | // created by any process in this task). Second, the lifetime of the job |
| 92 | // object is greater than the lifetime of the processes it contains. Thus |
| 93 | // the job object handle is explicitly owned by the global job object |
| 94 | // manager process. |
| 95 | Try<std::wstring> name = os::name_job(pid); |
| 96 | if (name.isError()) { |
| 97 | return Error(name.error()); |
| 98 | } |
| 99 | |
| 100 | // This creates a named job object in the Windows kernel. |
| 101 | // This handle must remain in scope (and open) until |
| 102 | // a running process is assigned to it. |
| 103 | Try<SharedHandle> handle = os::create_job(name.get()); |
| 104 | if (handle.isError()) { |
| 105 | return Error(handle.error()); |
| 106 | } |
| 107 | |
| 108 | // This actually assigns the process `pid` to the job object. |
| 109 | Try<Nothing> result = os::assign_job(handle.get(), pid); |
| 110 | if (result.isError()) { |
| 111 | return Error(result.error()); |
| 112 | } |
| 113 | |
| 114 | // Save the handle to the job object to ensure the object remains |
| 115 | // open for the entire lifetime of the agent process, and is closed |
| 116 | // when the process is reaped. |
| 117 | dispatch( |
| 118 | process::internal::job_object_manager, |
| 119 | &process::internal::JobObjectManager::manage, |
| 120 | pid, |
| 121 | name.get(), |
| 122 | handle.get()); |
| 123 | |
| 124 | return Nothing(); |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | } // namespace process { |
| 129 |
nothing calls this directly
no test coverage detected