check_runner_sys/lib.rs
1//! Unsafe syscall wrappers for check-runner process management.
2//! Each function includes a // SAFETY: justification block.
3#![allow(unsafe_code)]
4
5/// Send SIGKILL to an entire process group.
6/// SAFETY: pgid must be a valid process group ID owned by this session.
7pub fn kill_process_group(pgid: libc::pid_t) {
8 // SAFETY: caller guarantees pgid is a valid process group.
9 // This is the only Unix mechanism to terminate an entire process group.
10 let _ = unsafe { libc::killpg(pgid, libc::SIGKILL) };
11}
12
13/// Check if a process exists (kill with signal 0).
14/// Returns true if the process exists, false otherwise.
15/// SAFETY: pid must be a valid process ID.
16pub fn process_exists(pid: libc::pid_t) -> bool {
17 // SAFETY: signal 0 checks process existence without sending a signal.
18 unsafe { libc::kill(pid, 0) == 0 }
19}
20
21/// Set an environment variable in the current process.
22/// Only used in test context.
23/// SAFETY: var and value must be valid C strings.
24pub fn set_env(var: &str, value: &str) {
25 // SAFETY: pointer lifetime is guaranteed by the caller's borrow.
26 let _ = unsafe {
27 libc::setenv(
28 var.as_ptr() as *const libc::c_char,
29 value.as_ptr() as *const libc::c_char,
30 1,
31 )
32 };
33}
34
35/// Remove an environment variable from the current process.
36/// Only used in test context.
37/// SAFETY: var must be a valid C string.
38pub fn remove_env(var: &str) {
39 // SAFETY: pointer lifetime is guaranteed by the caller's borrow.
40 let _ = unsafe { libc::unsetenv(var.as_ptr() as *const libc::c_char) };
41}