bootstrap/utils/job.rs
1#[cfg(windows)]
2pub use for_windows::*;
3
4#[cfg(any(target_os = "haiku", target_os = "hermit", not(any(unix, windows))))]
5pub unsafe fn setup(_build: &mut crate::Build) {}
6
7#[cfg(all(unix, not(target_os = "haiku")))]
8pub unsafe fn setup(build: &mut crate::Build) {
9 if build.config.low_priority {
10 libc::setpriority(libc::PRIO_PGRP as _, 0, 10);
11 }
12}
13
14/// Job management on Windows for bootstrapping
15///
16/// Most of the time when you're running a build system (e.g., make) you expect
17/// Ctrl-C or abnormal termination to actually terminate the entire tree of
18/// processes in play. This currently works "by
19/// default" on Unix platforms because Ctrl-C actually sends a signal to the
20/// *process group* so everything will get torn
21/// down. On Windows, however, Ctrl-C is only sent to processes in the same console.
22/// If a process is detached or attached to another console, it won't receive the
23/// signal.
24///
25/// To achieve the same semantics on Windows we use Job Objects to ensure that
26/// all processes die at the same time. Job objects have a mode of operation
27/// where when all handles to the object are closed it causes all child
28/// processes associated with the object to be terminated immediately.
29/// Conveniently whenever a process in the job object spawns a new process the
30/// child will be associated with the job object as well. This means if we add
31/// ourselves to the job object we create then everything will get torn down!
32///
33/// Unfortunately most of the time the build system is actually called from a
34/// python wrapper (which manages things like building the build system) so this
35/// all doesn't quite cut it so far. To go the last mile we duplicate the job
36/// object handle into our parent process (a python process probably) and then
37/// close our own handle. This means that the only handle to the job object
38/// resides in the parent python process, so when python dies the whole build
39/// system dies (as one would probably expect!).
40///
41/// Note that this is a Windows specific module as none of this logic is required on Unix.
42#[cfg(windows)]
43mod for_windows {
44 use std::ffi::c_void;
45 use std::{io, mem};
46
47 use windows::Win32::Foundation::CloseHandle;
48 use windows::Win32::System::Diagnostics::Debug::{
49 SEM_NOGPFAULTERRORBOX, SetErrorMode, THREAD_ERROR_MODE,
50 };
51 use windows::Win32::System::JobObjects::{
52 AssignProcessToJobObject, CreateJobObjectW, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
53 JOB_OBJECT_LIMIT_PRIORITY_CLASS, JOBOBJECT_EXTENDED_LIMIT_INFORMATION,
54 JobObjectExtendedLimitInformation, SetInformationJobObject,
55 };
56 use windows::Win32::System::Threading::{BELOW_NORMAL_PRIORITY_CLASS, GetCurrentProcess};
57 use windows::core::PCWSTR;
58
59 use crate::Build;
60
61 pub unsafe fn setup(build: &mut Build) {
62 // Enable the Windows Error Reporting dialog which msys disables,
63 // so we can JIT debug rustc
64 let mode = SetErrorMode(THREAD_ERROR_MODE::default());
65 let mode = THREAD_ERROR_MODE(mode);
66 SetErrorMode(mode & !SEM_NOGPFAULTERRORBOX);
67
68 // Create a new job object for us to use
69 let job = CreateJobObjectW(None, PCWSTR::null()).unwrap();
70
71 // Indicate that when all handles to the job object are gone that all
72 // process in the object should be killed. Note that this includes our
73 // entire process tree by default because we've added ourselves and our
74 // children will reside in the job by default.
75 let mut info = JOBOBJECT_EXTENDED_LIMIT_INFORMATION::default();
76 info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
77 if build.config.low_priority {
78 info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_PRIORITY_CLASS;
79 info.BasicLimitInformation.PriorityClass = BELOW_NORMAL_PRIORITY_CLASS.0;
80 }
81 let r = SetInformationJobObject(
82 job,
83 JobObjectExtendedLimitInformation,
84 &info as *const _ as *const c_void,
85 mem::size_of_val(&info) as u32,
86 );
87 assert!(r.is_ok(), "{}", io::Error::last_os_error());
88
89 // Assign our process to this job object.
90 let r = AssignProcessToJobObject(job, GetCurrentProcess());
91 if r.is_err() {
92 CloseHandle(job).ok();
93 return;
94 }
95
96 // Note: we intentionally leak the job object handle. When our process exits
97 // (normally or abnormally) it will close the handle implicitly, causing all
98 // processes in the job to be cleaned up.
99 }
100}