feat: add approval bound full maintenance script console and executors
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -194,9 +194,31 @@ pub fn run(
|
||||
seconds: u32,
|
||||
max_bytes: usize,
|
||||
cancel: &AtomicBool,
|
||||
) -> Result<Output> {
|
||||
run_observed(
|
||||
executable,
|
||||
arguments,
|
||||
cwd,
|
||||
Duration::from_secs(seconds.into()),
|
||||
max_bytes,
|
||||
cancel,
|
||||
|| Ok(()),
|
||||
)
|
||||
}
|
||||
pub(crate) fn run_observed(
|
||||
executable: &Path,
|
||||
arguments: &[OsString],
|
||||
cwd: &Path,
|
||||
timeout: Duration,
|
||||
max_bytes: usize,
|
||||
cancel: &AtomicBool,
|
||||
mut observe: impl FnMut() -> Result<()>,
|
||||
) -> Result<Output> {
|
||||
ensure!(
|
||||
executable.is_absolute() && cwd.is_absolute() && seconds > 0 && max_bytes <= 512 * 1024,
|
||||
executable.is_absolute()
|
||||
&& cwd.is_absolute()
|
||||
&& !timeout.is_zero()
|
||||
&& max_bytes <= 512 * 1024,
|
||||
"PROCESS_LIMITS"
|
||||
);
|
||||
let (stdout, out_write) = pipe()?;
|
||||
@@ -268,7 +290,17 @@ pub fn run(
|
||||
// Minimal environment: no inherited tokens, proxy secrets, device paths,
|
||||
// PSModulePath overrides, user profile or caller-controlled executable PATH.
|
||||
let mut environment = Vec::new();
|
||||
let system_text = system
|
||||
.to_str()
|
||||
.ok_or_else(|| anyhow::anyhow!("SYSTEM_DIRECTORY_ENCODING"))?;
|
||||
ensure!(
|
||||
system_text.as_bytes().get(1) == Some(&b':')
|
||||
&& system_text.as_bytes()[0].is_ascii_alphabetic(),
|
||||
"SYSTEM_DRIVE_REQUIRED"
|
||||
);
|
||||
let system_drive = &system_text[..2];
|
||||
for value in [
|
||||
format!("SystemDrive={system_drive}"),
|
||||
format!("PATH={}\\System32;{}", system.display(), system.display()),
|
||||
format!("SystemRoot={}", system.display()),
|
||||
format!("TEMP={}", cwd.display()),
|
||||
@@ -279,6 +311,11 @@ pub fn run(
|
||||
}
|
||||
environment.push(0);
|
||||
let mut process: PROCESS_INFORMATION = unsafe { zeroed() };
|
||||
observe()?;
|
||||
ensure!(
|
||||
!cancel.load(Ordering::SeqCst),
|
||||
"PROCESS_CANCELLED_BEFORE_START"
|
||||
);
|
||||
ensure!(
|
||||
unsafe {
|
||||
CreateProcessW(
|
||||
@@ -303,32 +340,101 @@ pub fn run(
|
||||
drop(stdin);
|
||||
drop(out_write);
|
||||
drop(err_write);
|
||||
let mut result = Output {
|
||||
stdout: Vec::new(),
|
||||
stderr: Vec::new(),
|
||||
exit_code: None,
|
||||
truncated: false,
|
||||
interrupted: false,
|
||||
cancelled: false,
|
||||
};
|
||||
let began = Instant::now();
|
||||
let mut remaining = max_bytes;
|
||||
loop {
|
||||
drain(
|
||||
&stdout,
|
||||
&mut result.stdout,
|
||||
&mut remaining,
|
||||
&mut result.truncated,
|
||||
)?;
|
||||
drain(
|
||||
&stderr,
|
||||
&mut result.stderr,
|
||||
&mut remaining,
|
||||
&mut result.truncated,
|
||||
)?;
|
||||
let mut accounting: JOBOBJECT_BASIC_ACCOUNTING_INFORMATION = unsafe { zeroed() };
|
||||
let result = (|| -> Result<Output> {
|
||||
let mut result = Output {
|
||||
stdout: Vec::new(),
|
||||
stderr: Vec::new(),
|
||||
exit_code: None,
|
||||
truncated: false,
|
||||
interrupted: false,
|
||||
cancelled: false,
|
||||
};
|
||||
let began = Instant::now();
|
||||
let mut remaining = max_bytes;
|
||||
loop {
|
||||
drain(
|
||||
&stdout,
|
||||
&mut result.stdout,
|
||||
&mut remaining,
|
||||
&mut result.truncated,
|
||||
)?;
|
||||
drain(
|
||||
&stderr,
|
||||
&mut result.stderr,
|
||||
&mut remaining,
|
||||
&mut result.truncated,
|
||||
)?;
|
||||
let mut accounting: JOBOBJECT_BASIC_ACCOUNTING_INFORMATION = unsafe { zeroed() };
|
||||
ensure!(
|
||||
unsafe {
|
||||
QueryInformationJobObject(
|
||||
job.as_raw_handle(),
|
||||
JobObjectBasicAccountingInformation,
|
||||
ptr::addr_of_mut!(accounting).cast(),
|
||||
size_of_val(&accounting) as u32,
|
||||
ptr::null_mut(),
|
||||
)
|
||||
} != 0,
|
||||
"PROCESS_TREE_STATE"
|
||||
);
|
||||
if accounting.ActiveProcesses == 0 {
|
||||
break;
|
||||
}
|
||||
let fenced = observe().is_err();
|
||||
if !result.interrupted
|
||||
&& (began.elapsed() >= timeout || cancel.load(Ordering::SeqCst) || fenced)
|
||||
{
|
||||
result.cancelled = cancel.load(Ordering::SeqCst) || fenced;
|
||||
result.interrupted = true;
|
||||
ensure!(
|
||||
unsafe { TerminateJobObject(job.as_raw_handle(), 1) } != 0,
|
||||
"PROCESS_TREE_TERMINATION"
|
||||
);
|
||||
// This wait proves termination, not absence of effects.
|
||||
ensure!(
|
||||
unsafe { WaitForSingleObject(process_handle.as_raw_handle(), 5000) } == 0,
|
||||
"PROCESS_TERMINATION_UNCONFIRMED"
|
||||
);
|
||||
// Keep this JobObject/process handle owner until *all* assigned
|
||||
// processes are observed inactive. Waiting for the leader alone
|
||||
// cannot release the helper lane or heal an interrupted outcome.
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
// No writer can outlive job destruction. Drain the bounded OS pipe buffers.
|
||||
for _ in 0..8 {
|
||||
drain(
|
||||
&stdout,
|
||||
&mut result.stdout,
|
||||
&mut remaining,
|
||||
&mut result.truncated,
|
||||
)?;
|
||||
drain(
|
||||
&stderr,
|
||||
&mut result.stderr,
|
||||
&mut remaining,
|
||||
&mut result.truncated,
|
||||
)?;
|
||||
}
|
||||
let mut code = 0;
|
||||
ensure!(
|
||||
unsafe {
|
||||
unsafe { GetExitCodeProcess(process_handle.as_raw_handle(), &mut code) } != 0
|
||||
&& code != STILL_ACTIVE as u32,
|
||||
"PROCESS_EXIT_UNCONFIRMED"
|
||||
);
|
||||
result.exit_code = Some(code as i32);
|
||||
Ok(result)
|
||||
})();
|
||||
if result.is_err() {
|
||||
// Keep exact owned handles and the caller's helper lane on any observer
|
||||
// or I/O error. Termination/leader exit alone cannot prove the whole
|
||||
// assigned tree inactive; never drop ownership and admit replacement.
|
||||
unsafe {
|
||||
TerminateJobObject(job.as_raw_handle(), 1);
|
||||
}
|
||||
loop {
|
||||
let mut accounting: JOBOBJECT_BASIC_ACCOUNTING_INFORMATION = unsafe { zeroed() };
|
||||
let ok = unsafe {
|
||||
QueryInformationJobObject(
|
||||
job.as_raw_handle(),
|
||||
JobObjectBasicAccountingInformation,
|
||||
@@ -336,52 +442,14 @@ pub fn run(
|
||||
size_of_val(&accounting) as u32,
|
||||
ptr::null_mut(),
|
||||
)
|
||||
} != 0,
|
||||
"PROCESS_TREE_STATE"
|
||||
);
|
||||
if accounting.ActiveProcesses == 0 {
|
||||
break;
|
||||
};
|
||||
if ok != 0 && accounting.ActiveProcesses == 0 {
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
if began.elapsed() >= Duration::from_secs(seconds.into()) || cancel.load(Ordering::SeqCst) {
|
||||
result.cancelled = cancel.load(Ordering::SeqCst);
|
||||
result.interrupted = true;
|
||||
ensure!(
|
||||
unsafe { TerminateJobObject(job.as_raw_handle(), 1) } != 0,
|
||||
"PROCESS_TREE_TERMINATION"
|
||||
);
|
||||
// This wait proves termination, not absence of effects.
|
||||
ensure!(
|
||||
unsafe { WaitForSingleObject(process_handle.as_raw_handle(), 5000) } == 0,
|
||||
"PROCESS_TERMINATION_UNCONFIRMED"
|
||||
);
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
// No writer can outlive job destruction. Drain the bounded OS pipe buffers.
|
||||
drop(job);
|
||||
for _ in 0..8 {
|
||||
drain(
|
||||
&stdout,
|
||||
&mut result.stdout,
|
||||
&mut remaining,
|
||||
&mut result.truncated,
|
||||
)?;
|
||||
drain(
|
||||
&stderr,
|
||||
&mut result.stderr,
|
||||
&mut remaining,
|
||||
&mut result.truncated,
|
||||
)?;
|
||||
}
|
||||
let mut code = 0;
|
||||
ensure!(
|
||||
unsafe { GetExitCodeProcess(process_handle.as_raw_handle(), &mut code) } != 0
|
||||
&& code != STILL_ACTIVE as u32,
|
||||
"PROCESS_EXIT_UNCONFIRMED"
|
||||
);
|
||||
result.exit_code = Some(code as i32);
|
||||
Ok(result)
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user