2015-11-19 15:20:12 -08:00
|
|
|
//! Job management on Windows for bootstrapping
|
|
|
|
|
//!
|
2018-11-27 02:59:49 +00:00
|
|
|
//! Most of the time when you're running a build system (e.g., make) you expect
|
2015-11-19 15:20:12 -08:00
|
|
|
//! Ctrl-C or abnormal termination to actually terminate the entire tree of
|
|
|
|
|
//! process in play, not just the one at the top. This currently works "by
|
|
|
|
|
//! default" on Unix platforms because Ctrl-C actually sends a signal to the
|
|
|
|
|
//! *process group* rather than the parent process, so everything will get torn
|
|
|
|
|
//! down. On Windows, however, this does not happen and Ctrl-C just kills the
|
|
|
|
|
//! parent process.
|
|
|
|
|
//!
|
|
|
|
|
//! To achieve the same semantics on Windows we use Job Objects to ensure that
|
|
|
|
|
//! all processes die at the same time. Job objects have a mode of operation
|
|
|
|
|
//! where when all handles to the object are closed it causes all child
|
|
|
|
|
//! processes associated with the object to be terminated immediately.
|
|
|
|
|
//! Conveniently whenever a process in the job object spawns a new process the
|
|
|
|
|
//! child will be associated with the job object as well. This means if we add
|
|
|
|
|
//! ourselves to the job object we create then everything will get torn down!
|
|
|
|
|
//!
|
|
|
|
|
//! Unfortunately most of the time the build system is actually called from a
|
|
|
|
|
//! python wrapper (which manages things like building the build system) so this
|
|
|
|
|
//! all doesn't quite cut it so far. To go the last mile we duplicate the job
|
|
|
|
|
//! object handle into our parent process (a python process probably) and then
|
|
|
|
|
//! close our own handle. This means that the only handle to the job object
|
|
|
|
|
//! resides in the parent python process, so when python dies the whole build
|
|
|
|
|
//! system dies (as one would probably expect!).
|
|
|
|
|
//!
|
|
|
|
|
//! Note that this module has a #[cfg(windows)] above it as none of this logic
|
|
|
|
|
//! is required on Unix.
|
|
|
|
|
|
2018-12-07 13:21:05 +01:00
|
|
|
use crate::Build;
|
2015-11-19 15:20:12 -08:00
|
|
|
use std::env;
|
2023-01-15 13:43:15 -05:00
|
|
|
use std::ffi::c_void;
|
2015-11-19 15:20:12 -08:00
|
|
|
use std::io;
|
|
|
|
|
use std::mem;
|
|
|
|
|
|
2023-01-15 13:43:15 -05:00
|
|
|
use windows::{
|
|
|
|
|
core::PCWSTR,
|
|
|
|
|
Win32::Foundation::{CloseHandle, DuplicateHandle, DUPLICATE_SAME_ACCESS, HANDLE},
|
|
|
|
|
Win32::System::Diagnostics::Debug::{SetErrorMode, SEM_NOGPFAULTERRORBOX, THREAD_ERROR_MODE},
|
|
|
|
|
Win32::System::JobObjects::{
|
|
|
|
|
AssignProcessToJobObject, CreateJobObjectW, JobObjectExtendedLimitInformation,
|
|
|
|
|
SetInformationJobObject, JOBOBJECT_EXTENDED_LIMIT_INFORMATION,
|
|
|
|
|
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, JOB_OBJECT_LIMIT_PRIORITY_CLASS,
|
|
|
|
|
},
|
|
|
|
|
Win32::System::Threading::{
|
|
|
|
|
GetCurrentProcess, OpenProcess, BELOW_NORMAL_PRIORITY_CLASS, PROCESS_DUP_HANDLE,
|
|
|
|
|
},
|
2020-01-04 16:46:47 -05:00
|
|
|
};
|
2015-11-19 15:20:12 -08:00
|
|
|
|
2017-03-23 22:57:29 +01:00
|
|
|
pub unsafe fn setup(build: &mut Build) {
|
2018-04-08 13:44:29 +02:00
|
|
|
// Enable the Windows Error Reporting dialog which msys disables,
|
|
|
|
|
// so we can JIT debug rustc
|
2023-01-15 13:43:15 -05:00
|
|
|
let mode = SetErrorMode(THREAD_ERROR_MODE::default());
|
|
|
|
|
let mode = THREAD_ERROR_MODE(mode);
|
2018-04-08 13:44:29 +02:00
|
|
|
SetErrorMode(mode & !SEM_NOGPFAULTERRORBOX);
|
2016-12-07 15:50:48 -08:00
|
|
|
|
2015-11-19 15:20:12 -08:00
|
|
|
// Create a new job object for us to use
|
2023-01-15 13:43:15 -05:00
|
|
|
let job = CreateJobObjectW(None, PCWSTR::null()).unwrap();
|
2015-11-19 15:20:12 -08:00
|
|
|
|
|
|
|
|
// Indicate that when all handles to the job object are gone that all
|
|
|
|
|
// process in the object should be killed. Note that this includes our
|
2016-05-05 21:11:41 +02:00
|
|
|
// entire process tree by default because we've added ourselves and our
|
2015-11-19 15:20:12 -08:00
|
|
|
// children will reside in the job by default.
|
2023-01-15 13:43:15 -05:00
|
|
|
let mut info = JOBOBJECT_EXTENDED_LIMIT_INFORMATION::default();
|
2015-11-19 15:20:12 -08:00
|
|
|
info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
|
2017-03-23 22:57:29 +01:00
|
|
|
if build.config.low_priority {
|
|
|
|
|
info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_PRIORITY_CLASS;
|
2023-01-15 13:43:15 -05:00
|
|
|
info.BasicLimitInformation.PriorityClass = BELOW_NORMAL_PRIORITY_CLASS.0;
|
2017-03-23 22:57:29 +01:00
|
|
|
}
|
2015-11-19 15:20:12 -08:00
|
|
|
let r = SetInformationJobObject(
|
|
|
|
|
job,
|
|
|
|
|
JobObjectExtendedLimitInformation,
|
2023-01-15 13:43:15 -05:00
|
|
|
&info as *const _ as *const c_void,
|
|
|
|
|
mem::size_of_val(&info) as u32,
|
|
|
|
|
)
|
|
|
|
|
.ok();
|
|
|
|
|
assert!(r.is_ok(), "{}", io::Error::last_os_error());
|
2015-11-19 15:20:12 -08:00
|
|
|
|
2016-02-11 20:46:47 -08:00
|
|
|
// Assign our process to this job object. Note that if this fails, one very
|
|
|
|
|
// likely reason is that we are ourselves already in a job object! This can
|
|
|
|
|
// happen on the build bots that we've got for Windows, or if just anyone
|
|
|
|
|
// else is instrumenting the build. In this case we just bail out
|
|
|
|
|
// immediately and assume that they take care of it.
|
|
|
|
|
//
|
|
|
|
|
// Also note that nested jobs (why this might fail) are supported in recent
|
|
|
|
|
// versions of Windows, but the version of Windows that our bots are running
|
|
|
|
|
// at least don't support nested job objects.
|
2023-01-15 13:43:15 -05:00
|
|
|
let r = AssignProcessToJobObject(job, GetCurrentProcess()).ok();
|
|
|
|
|
if r.is_err() {
|
2016-02-11 20:46:47 -08:00
|
|
|
CloseHandle(job);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-11-19 15:20:12 -08:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// If we've got a parent process (e.g., the python script that called us)
|
2015-11-19 15:20:12 -08:00
|
|
|
// then move ownership of this job object up to them. That way if the python
|
2018-11-27 02:59:49 +00:00
|
|
|
// script is killed (e.g., via ctrl-c) then we'll all be torn down.
|
2015-11-19 15:20:12 -08:00
|
|
|
//
|
2018-11-27 02:59:49 +00:00
|
|
|
// If we don't have a parent (e.g., this was run directly) then we
|
2015-11-19 15:20:12 -08:00
|
|
|
// intentionally leak the job object handle. When our process exits
|
|
|
|
|
// (normally or abnormally) it will close the handle implicitly, causing all
|
|
|
|
|
// processes in the job to be cleaned up.
|
|
|
|
|
let pid = match env::var("BOOTSTRAP_PARENT_ID") {
|
|
|
|
|
Ok(s) => s,
|
|
|
|
|
Err(..) => return,
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-15 13:43:15 -05:00
|
|
|
let parent = match OpenProcess(PROCESS_DUP_HANDLE, false, pid.parse().unwrap()).ok() {
|
|
|
|
|
Some(parent) => parent,
|
|
|
|
|
_ => {
|
|
|
|
|
// If we get a null parent pointer here, it is possible that either
|
|
|
|
|
// we have an invalid pid or the parent process has been closed.
|
|
|
|
|
// Since the first case rarely happens
|
|
|
|
|
// (only when wrongly setting the environmental variable),
|
|
|
|
|
// it might be better to improve the experience of the second case
|
|
|
|
|
// when users have interrupted the parent process and we haven't finish
|
|
|
|
|
// duplicating the handle yet. We just need close the job object if that occurs.
|
|
|
|
|
CloseHandle(job);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-07-16 17:56:51 +08:00
|
|
|
|
2023-01-15 13:43:15 -05:00
|
|
|
let mut parent_handle = HANDLE::default();
|
2015-11-19 15:20:12 -08:00
|
|
|
let r = DuplicateHandle(
|
|
|
|
|
GetCurrentProcess(),
|
|
|
|
|
job,
|
|
|
|
|
parent,
|
|
|
|
|
&mut parent_handle,
|
|
|
|
|
0,
|
2023-01-15 13:43:15 -05:00
|
|
|
false,
|
2015-11-19 15:20:12 -08:00
|
|
|
DUPLICATE_SAME_ACCESS,
|
2023-01-15 13:43:15 -05:00
|
|
|
)
|
|
|
|
|
.ok();
|
2015-11-19 15:20:12 -08:00
|
|
|
|
|
|
|
|
// If this failed, well at least we tried! An example of DuplicateHandle
|
2017-11-21 15:33:45 +01:00
|
|
|
// failing in the past has been when the wrong python2 package spawned this
|
2018-11-27 02:59:49 +00:00
|
|
|
// build system (e.g., the `python2` package in MSYS instead of
|
2015-11-19 15:20:12 -08:00
|
|
|
// `mingw-w64-x86_64-python2`. Not sure why it failed, but the "failure
|
|
|
|
|
// mode" here is that we only clean everything up when the build system
|
|
|
|
|
// dies, not when the python parent does, so not too bad.
|
2023-01-15 13:43:15 -05:00
|
|
|
if r.is_err() {
|
2015-11-19 15:20:12 -08:00
|
|
|
CloseHandle(job);
|
|
|
|
|
}
|
|
|
|
|
}
|