use crate::signal::{Sent, Sig};
fn exiting_child() -> ::tokio::process::Child {
crate::test_spawn::spawn_tokio(
::tokio::process::Command::new("cmd")
.args(["/C", "exit 0"])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null()),
)
.expect("spawn")
}
#[skuld::test]
async fn a_kill_after_the_child_was_reaped_is_gone() {
let child = exiting_child();
let pid = child.id().expect("tokio holds an un-reaped child");
let mut proc = super::proc_source::ProcSource::new(child, pid);
proc.wait().await.expect("wait");
assert_eq!(proc.signal(Sig::Kill).expect("a kill of a reaped child"), Sent::Gone);
}
#[skuld::test]
async fn a_kill_of_an_exited_unreaped_child_is_a_success() {
use windows::Win32::Foundation::HANDLE;
use windows::Win32::System::Threading::{WaitForSingleObject, INFINITE};
let child = exiting_child();
let handle = child.raw_handle().expect("tokio holds the handle");
unsafe { WaitForSingleObject(HANDLE(handle), INFINITE) };
let pid = child.id().expect("tokio holds an un-reaped child");
let proc = super::proc_source::ProcSource::new(child, pid);
assert_eq!(
proc.signal(Sig::Kill).expect("a kill of an exited child"),
Sent::Delivered
);
}
#[skuld::test]
async fn a_kill_of_an_exited_child_through_an_unwaitable_handle_is_a_success() {
use super::proc_source::terminate_unreaped;
use windows::Win32::Foundation::{CloseHandle, DuplicateHandle, DUPLICATE_HANDLE_OPTIONS, HANDLE};
use windows::Win32::System::Threading::{
GetCurrentProcess, WaitForSingleObject, INFINITE, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_TERMINATE,
};
let child = exiting_child();
let handle = HANDLE(child.raw_handle().expect("tokio holds the handle"));
unsafe { WaitForSingleObject(handle, INFINITE) };
let mut limited = HANDLE::default();
unsafe {
DuplicateHandle(
GetCurrentProcess(),
handle,
GetCurrentProcess(),
&mut limited,
(PROCESS_TERMINATE | PROCESS_QUERY_LIMITED_INFORMATION).0,
false,
DUPLICATE_HANDLE_OPTIONS(0),
)
}
.expect("duplicate the child's handle without SYNCHRONIZE");
let result = terminate_unreaped(limited, PROCESS_TERMINATE | PROCESS_QUERY_LIMITED_INFORMATION);
unsafe { CloseHandle(limited) }.expect("close the duplicate");
assert_eq!(result.expect("a refusal on an exited child"), Sent::Delivered);
}
#[skuld::test]
fn a_terminate_failure_carries_its_win32_code() {
use super::proc_source::terminate_unreaped;
use std::os::windows::io::AsRawHandle;
use windows::Win32::Foundation::{ERROR_INVALID_HANDLE, HANDLE};
let event = crate::os_process::windows::cancel::new_cancel_event().expect("create an event");
let err = terminate_unreaped(
HANDLE(event.as_raw_handle()),
crate::os_process::windows::CREATE_PROCESS_ACCESS,
)
.expect_err("an event cannot be terminated");
let crate::error::Error::Io(err) = err else {
panic!("expected Io, got {err:?}");
};
assert_eq!(err.raw_os_error(), Some(ERROR_INVALID_HANDLE.0 as i32), "{err:?}");
}
#[skuld::test]
fn a_denied_terminate_of_an_unwaitable_handle_carries_its_win32_code() {
use super::proc_source::terminate_unreaped;
use windows::Win32::Foundation::{
CloseHandle, DuplicateHandle, DUPLICATE_HANDLE_OPTIONS, ERROR_ACCESS_DENIED, HANDLE,
};
use windows::Win32::System::Threading::{GetCurrentProcess, PROCESS_QUERY_LIMITED_INFORMATION};
let mut limited = HANDLE::default();
unsafe {
let me = GetCurrentProcess();
DuplicateHandle(
me,
me,
me,
&mut limited,
PROCESS_QUERY_LIMITED_INFORMATION.0,
false,
DUPLICATE_HANDLE_OPTIONS(0),
)
}
.expect("duplicate this process's handle with reduced access");
let result = terminate_unreaped(limited, PROCESS_QUERY_LIMITED_INFORMATION);
unsafe { CloseHandle(limited) }.expect("close the duplicate");
let err = result.expect_err("a handle with no terminate and no synchronize right is a denial");
let crate::error::Error::Io(err) = err else {
panic!("expected Io, got {err:?}");
};
assert_eq!(err.raw_os_error(), Some(ERROR_ACCESS_DENIED.0 as i32), "{err:?}");
assert_eq!(err.kind(), std::io::ErrorKind::PermissionDenied, "{err:?}");
}