| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore IAMNOTASIGNAL RTMAX RTMIN SIGIO SIGRTMAX GHSA CHLD SIGSTOP taskkill unreaped |
| 7 |
|
| 8 |
use regex::Regex; |
| 9 |
#[cfg(windows)] |
| 10 |
use std::io::Write; |
| 11 |
#[cfg(unix)] |
| 12 |
use std::os::unix::process::ExitStatusExt; |
| 13 |
#[cfg(windows)] |
| 14 |
use std::process::Stdio; |
| 15 |
use std::process::{Child, Command, ExitStatus}; |
| 16 |
#[cfg(windows)] |
| 17 |
use uucore::process::Job; |
| 18 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 19 |
use uucore::signals::realtime_signal_bounds; |
| 20 |
use uutests::new_ucmd; |
| 21 |
use uutests::util::get_tests_binary; |
| 22 |
|
| 23 |
// A child process the tests will try to kill: `<coreutils> sleep 30` via the |
| 24 |
// multicall test binary, which exists on every test platform (unlike `sleep` |
| 25 |
// from PATH on Windows). The natural death after 30s avoids hanging failing |
| 26 |
// tests. |
| 27 |
struct Target { |
| 28 |
child: Child, |
| 29 |
killed: bool, |
| 30 |
} |
| 31 |
|
| 32 |
impl Target { |
| 33 |
fn new() -> Self { |
| 34 |
Self { |
| 35 |
child: Command::new(get_tests_binary()) |
| 36 |
.args(["sleep", "30"]) |
| 37 |
.spawn() |
| 38 |
.expect("cannot spawn target"), |
| 39 |
killed: false, |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/// Wait for the target to exit, so `Drop` no longer has to kill it. |
| 44 |
fn reap(&mut self) -> ExitStatus { |
| 45 |
let status = self.child.wait().expect("cannot wait on target"); |
| 46 |
self.killed = true; |
| 47 |
status |
| 48 |
} |
| 49 |
|
| 50 |
/// Reap the target and assert it was killed by `signal` (`128 + n` exit |
| 51 |
/// code on windows). |
| 52 |
fn assert_signaled(&mut self, signal: i32) { |
| 53 |
let status = self.reap(); |
| 54 |
#[cfg(unix)] |
| 55 |
assert_eq!( |
| 56 |
status.signal(), |
| 57 |
Some(signal), |
| 58 |
"target exited with {status:?} instead of dying from signal {signal}" |
| 59 |
); |
| 60 |
#[cfg(windows)] |
| 61 |
assert_eq!( |
| 62 |
status.code(), |
| 63 |
Some(128 + signal), |
| 64 |
"target exited with {status:?} instead of dying from signal {signal}" |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
#[cfg(windows)] |
| 69 |
fn assert_alive(&mut self) { |
| 70 |
assert!(self.child.try_wait().expect("cannot poll target").is_none()); |
| 71 |
} |
| 72 |
|
| 73 |
fn pid(&self) -> u32 { |
| 74 |
self.child.id() |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
impl Drop for Target { |
| 79 |
// Terminates this target to avoid littering test boxes with zombi processes |
| 80 |
// when a test fails after creating a target but before killing it. |
| 81 |
fn drop(&mut self) { |
| 82 |
if !self.killed { |
| 83 |
self.child.kill().expect("cannot kill target"); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/// A `cmd.exe` in a freshly created Job object, waiting for a command line on |
| 89 |
/// its stdin pipe. |
| 90 |
/// |
| 91 |
/// The only safe way to run a *terminating* `kill 0` from a test: `kill 0` |
| 92 |
/// signals the killer's immediate job, which here is `job` and nothing else, so |
| 93 |
/// it cannot reach the test runner, another test's children, nextest's per-test |
| 94 |
/// job or a CI agent — the ambient job is `job`'s *parent*, and a parent's |
| 95 |
/// members are not in a child job's list. |
| 96 |
/// |
| 97 |
/// The gate is what makes that true rather than merely likely: `cmd` gets no |
| 98 |
/// command until `assign` has succeeded, so the window between `CreateProcess` |
| 99 |
/// and `AssignProcessToJobObject` contains nothing that could signal. Fail-safe |
| 100 |
/// too — if anything panics before [`JobShell::run`], the pipe closes, `cmd` |
| 101 |
/// sees EOF and exits having executed nothing. |
| 102 |
#[cfg(windows)] |
| 103 |
struct JobShell { |
| 104 |
job: Job, |
| 105 |
shell: Child, |
| 106 |
} |
| 107 |
|
| 108 |
#[cfg(windows)] |
| 109 |
impl JobShell { |
| 110 |
fn new() -> Self { |
| 111 |
let job = Job::new().expect("cannot create job object"); |
| 112 |
let shell = Command::new("cmd") |
| 113 |
// `/d` skips the AutoRun registry command, so a machine that has |
| 114 |
// one cannot run it inside this job or write to the stderr the |
| 115 |
// assertions quote. |
| 116 |
.args(["/d", "/q"]) |
| 117 |
.stdin(Stdio::piped()) |
| 118 |
.stdout(Stdio::null()) |
| 119 |
.stderr(Stdio::piped()) |
| 120 |
.spawn() |
| 121 |
.expect("cannot spawn cmd"); |
| 122 |
job.assign(&shell) |
| 123 |
.expect("cannot assign cmd to the fresh job"); |
| 124 |
Self { job, shell } |
| 125 |
} |
| 126 |
|
| 127 |
/// So a group signal from inside reaches `child`. |
| 128 |
fn adopt(&self, child: &Child) { |
| 129 |
self.job |
| 130 |
.assign(child) |
| 131 |
.expect("cannot assign process to the fresh job"); |
| 132 |
} |
| 133 |
|
| 134 |
/// Closes stdin afterwards so cmd exits at EOF rather than blocking forever |
| 135 |
/// if the command fails to kill it. |
| 136 |
fn run(mut self, command: &str) -> (Option<i32>, String) { |
| 137 |
let mut stdin = self.shell.stdin.take().expect("cmd has no stdin pipe"); |
| 138 |
write!(stdin, "{command}\r\n").expect("cannot write command"); |
| 139 |
drop(stdin); |
| 140 |
let output = self.shell.wait_with_output().expect("cannot wait on cmd"); |
| 141 |
( |
| 142 |
output.status.code(), |
| 143 |
String::from_utf8_lossy(&output.stderr).into_owned(), |
| 144 |
) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn test_invalid_arg() { |
| 150 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 151 |
} |
| 152 |
|
| 153 |
#[test] |
| 154 |
fn test_kill_list_all_signals() { |
| 155 |
// Check for a few signals. Do not try to be comprehensive. |
| 156 |
new_ucmd!() |
| 157 |
.arg("-l") |
| 158 |
.succeeds() |
| 159 |
.stdout_contains("KILL") |
| 160 |
.stdout_contains("TERM") |
| 161 |
.stdout_contains("HUP") |
| 162 |
.stdout_contains("EXIT"); |
| 163 |
} |
| 164 |
|
| 165 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 166 |
#[test] |
| 167 |
fn test_kill_list_contains_realtime_signals() { |
| 168 |
new_ucmd!() |
| 169 |
.arg("-l") |
| 170 |
.succeeds() |
| 171 |
.stdout_contains("RTMIN") |
| 172 |
.stdout_contains("RTMAX"); |
| 173 |
} |
| 174 |
|
| 175 |
#[test] |
| 176 |
fn test_kill_list_final_new_line() { |
| 177 |
let re = Regex::new("\\n$").unwrap(); |
| 178 |
assert!(re.is_match(new_ucmd!().arg("-l").succeeds().stdout_str())); |
| 179 |
} |
| 180 |
|
| 181 |
#[test] |
| 182 |
fn test_kill_list_all_signals_as_table() { |
| 183 |
// Check for a few signals. Do not try to be comprehensive. |
| 184 |
new_ucmd!() |
| 185 |
.arg("-t") |
| 186 |
.succeeds() |
| 187 |
.stdout_contains("KILL") |
| 188 |
.stdout_contains("TERM") |
| 189 |
.stdout_contains("HUP") |
| 190 |
.stdout_contains("EXIT"); |
| 191 |
} |
| 192 |
|
| 193 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 194 |
#[test] |
| 195 |
fn test_kill_table_contains_realtime_signals() { |
| 196 |
new_ucmd!() |
| 197 |
.arg("-t") |
| 198 |
.succeeds() |
| 199 |
.stdout_contains("RTMIN") |
| 200 |
.stdout_contains("RTMAX"); |
| 201 |
} |
| 202 |
|
| 203 |
#[test] |
| 204 |
fn test_kill_table_starts_at_0() { |
| 205 |
new_ucmd!() |
| 206 |
.arg("-t") |
| 207 |
.succeeds() |
| 208 |
.stdout_matches(&Regex::new("^\\s?0\\sEXIT").unwrap()); |
| 209 |
} |
| 210 |
|
| 211 |
#[test] |
| 212 |
fn test_kill_table_lists_all_vertically() { |
| 213 |
// Check for a few signals. Do not try to be comprehensive. |
| 214 |
let command = new_ucmd!().arg("-t").succeeds(); |
| 215 |
let signals = command |
| 216 |
.stdout_str() |
| 217 |
.split('\n') |
| 218 |
.filter_map(|line| line.trim().split(' ').nth(1)) |
| 219 |
.collect::<Vec<&str>>(); |
| 220 |
|
| 221 |
assert!(signals.contains(&"KILL")); |
| 222 |
assert!(signals.contains(&"TERM")); |
| 223 |
assert!(signals.contains(&"HUP")); |
| 224 |
assert!(signals.contains(&"EXIT")); |
| 225 |
} |
| 226 |
|
| 227 |
#[test] |
| 228 |
fn test_kill_list_one_signal_from_number() { |
| 229 |
new_ucmd!() |
| 230 |
.arg("-l") |
| 231 |
.arg("9") |
| 232 |
.succeeds() |
| 233 |
.stdout_only("KILL\n"); |
| 234 |
} |
| 235 |
|
| 236 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 237 |
#[test] |
| 238 |
fn test_kill_list_rtmax_from_name() { |
| 239 |
new_ucmd!() |
| 240 |
.arg("-l") |
| 241 |
.arg("RTMAX") |
| 242 |
.succeeds() |
| 243 |
.stdout_only(format!("{}\n", libc::SIGRTMAX())); |
| 244 |
} |
| 245 |
|
| 246 |
#[test] |
| 247 |
fn test_kill_list_one_signal_from_invalid_number() { |
| 248 |
new_ucmd!() |
| 249 |
.arg("-l") |
| 250 |
.arg("99") |
| 251 |
.fails() |
| 252 |
.stderr_contains("'99': invalid signal"); |
| 253 |
} |
| 254 |
|
| 255 |
#[test] |
| 256 |
fn test_kill_list_one_signal_from_name() { |
| 257 |
// Use SIGKILL because it is 9 on all unixes. |
| 258 |
new_ucmd!() |
| 259 |
.arg("-l") |
| 260 |
.arg("KILL") |
| 261 |
.succeeds() |
| 262 |
.stdout_matches(&Regex::new("\\b9\\b").unwrap()); |
| 263 |
} |
| 264 |
|
| 265 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 266 |
#[test] |
| 267 |
fn test_kill_list_sigio_alias() { |
| 268 |
for signal in ["IO", "SIGIO"] { |
| 269 |
new_ucmd!() |
| 270 |
.arg("-l") |
| 271 |
.arg(signal) |
| 272 |
.succeeds() |
| 273 |
.stdout_only(format!("{}\n", libc::SIGIO)); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
#[test] |
| 278 |
fn test_kill_list_one_signal_ignore_case() { |
| 279 |
// Use SIGKILL because it is 9 on all unixes. |
| 280 |
new_ucmd!() |
| 281 |
.arg("-l") |
| 282 |
.arg("KiLl") |
| 283 |
.succeeds() |
| 284 |
.stdout_matches(&Regex::new("\\b9\\b").unwrap()); |
| 285 |
} |
| 286 |
|
| 287 |
#[test] |
| 288 |
fn test_kill_list_unknown_must_match_input_case() { |
| 289 |
new_ucmd!() |
| 290 |
.arg("-l") |
| 291 |
.arg("IaMnOtAsIgNaL") |
| 292 |
.fails() |
| 293 |
.stderr_contains("IaMnOtAsIgNaL"); |
| 294 |
} |
| 295 |
|
| 296 |
#[test] |
| 297 |
fn test_kill_list_all_vertically() { |
| 298 |
// Check for a few signals. Do not try to be comprehensive. |
| 299 |
let command = new_ucmd!().arg("-l").succeeds(); |
| 300 |
let signals = command.stdout_str().split('\n').collect::<Vec<&str>>(); |
| 301 |
assert!(signals.contains(&"KILL")); |
| 302 |
assert!(signals.contains(&"TERM")); |
| 303 |
assert!(signals.contains(&"HUP")); |
| 304 |
assert!(signals.contains(&"EXIT")); |
| 305 |
} |
| 306 |
|
| 307 |
#[test] |
| 308 |
fn test_kill_list_two_signal_from_name() { |
| 309 |
new_ucmd!() |
| 310 |
.arg("-l") |
| 311 |
.arg("INT") |
| 312 |
.arg("KILL") |
| 313 |
.succeeds() |
| 314 |
.stdout_matches(&Regex::new("\\d\n\\d").unwrap()); |
| 315 |
} |
| 316 |
|
| 317 |
#[test] |
| 318 |
fn test_kill_list_three_signal_first_unknown() { |
| 319 |
new_ucmd!() |
| 320 |
.arg("-l") |
| 321 |
.arg("IAMNOTASIGNAL") |
| 322 |
.arg("INT") |
| 323 |
.arg("KILL") |
| 324 |
.fails() |
| 325 |
.stderr_contains("'IAMNOTASIGNAL': invalid signal") |
| 326 |
.stdout_matches(&Regex::new("\\d\n\\d").unwrap()); |
| 327 |
} |
| 328 |
|
| 329 |
#[test] |
| 330 |
fn test_kill_set_bad_signal_name() { |
| 331 |
new_ucmd!() |
| 332 |
.arg("-s") |
| 333 |
.arg("IAMNOTASIGNAL") |
| 334 |
.fails() |
| 335 |
.stderr_contains("'IAMNOTASIGNAL': invalid signal"); |
| 336 |
} |
| 337 |
|
| 338 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 339 |
#[test] |
| 340 |
fn test_kill_accepts_sigio_alias_for_sending() { |
| 341 |
for args in [["--signal=SIGIO", "999999999"], ["-SIGIO", "999999999"]] { |
| 342 |
new_ucmd!() |
| 343 |
.args(&args) |
| 344 |
.fails_with_code(1) |
| 345 |
.stderr_contains("sending signal") |
| 346 |
.stderr_does_not_contain("invalid signal"); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
#[test] |
| 351 |
fn test_kill_out_of_range_signal_is_rejected_not_sent() { |
| 352 |
// An out-of-range signal number must be rejected up front (like GNU), not |
| 353 |
// fall through to be parsed as a negative PID and signalled with the |
| 354 |
// default SIGTERM. Regression for GHSA-3jmh-xh36-pj6v. |
| 355 |
for bad in ["-65", "-129"] { |
| 356 |
let mut target = Target::new(); |
| 357 |
new_ucmd!() |
| 358 |
.arg(bad) |
| 359 |
.arg(format!("{}", target.pid())) |
| 360 |
.fails_with_code(1) |
| 361 |
.stderr_contains("invalid signal"); |
| 362 |
// The target must have survived: kill it for real and confirm the |
| 363 |
// exit came from std's kill (SIGKILL / code 1), not a stray uu-kill |
| 364 |
// TERM (which would report 143 on windows). |
| 365 |
target.child.kill().expect("cannot kill surviving target"); |
| 366 |
let status = target.reap(); |
| 367 |
#[cfg(unix)] |
| 368 |
assert_eq!(status.signal(), Some(9)); |
| 369 |
#[cfg(windows)] |
| 370 |
assert_eq!(status.code(), Some(1)); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
#[test] |
| 375 |
fn test_kill_with_default_signal() { |
| 376 |
let mut target = Target::new(); |
| 377 |
new_ucmd!().arg(format!("{}", target.pid())).succeeds(); |
| 378 |
target.assert_signaled(15); |
| 379 |
} |
| 380 |
|
| 381 |
#[test] |
| 382 |
fn test_kill_with_signal_number_old_form() { |
| 383 |
let mut target = Target::new(); |
| 384 |
new_ucmd!() |
| 385 |
.arg("-9") |
| 386 |
.arg(format!("{}", target.pid())) |
| 387 |
.succeeds(); |
| 388 |
target.assert_signaled(9); |
| 389 |
} |
| 390 |
|
| 391 |
#[test] |
| 392 |
fn test_kill_with_signal_name_old_form() { |
| 393 |
for arg in ["-Kill", "-KILL"] { |
| 394 |
let mut target = Target::new(); |
| 395 |
new_ucmd!() |
| 396 |
.arg(arg) |
| 397 |
.arg(format!("{}", target.pid())) |
| 398 |
.succeeds(); |
| 399 |
target.assert_signaled(9); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
#[test] |
| 404 |
fn test_kill_with_lower_case_signal_name_old_form() { |
| 405 |
let target = Target::new(); |
| 406 |
new_ucmd!() |
| 407 |
.arg("-kill") |
| 408 |
.arg(format!("{}", target.pid())) |
| 409 |
.fails() |
| 410 |
.stderr_contains("unexpected argument"); |
| 411 |
} |
| 412 |
|
| 413 |
#[test] |
| 414 |
fn test_kill_with_signal_prefixed_name_old_form() { |
| 415 |
let mut target = Target::new(); |
| 416 |
new_ucmd!() |
| 417 |
.arg("-SIGKILL") |
| 418 |
.arg(format!("{}", target.pid())) |
| 419 |
.succeeds(); |
| 420 |
target.assert_signaled(9); |
| 421 |
} |
| 422 |
|
| 423 |
#[test] |
| 424 |
fn test_kill_with_signal_number_new_form() { |
| 425 |
let mut target = Target::new(); |
| 426 |
new_ucmd!() |
| 427 |
.arg("-s") |
| 428 |
.arg("9") |
| 429 |
.arg(format!("{}", target.pid())) |
| 430 |
.succeeds(); |
| 431 |
target.assert_signaled(9); |
| 432 |
} |
| 433 |
|
| 434 |
#[test] |
| 435 |
fn test_kill_with_signal_name_new_form() { |
| 436 |
let mut target = Target::new(); |
| 437 |
new_ucmd!() |
| 438 |
.arg("-s") |
| 439 |
.arg("KILL") |
| 440 |
.arg(format!("{}", target.pid())) |
| 441 |
.succeeds(); |
| 442 |
target.assert_signaled(9); |
| 443 |
} |
| 444 |
|
| 445 |
#[test] |
| 446 |
fn test_kill_with_signal_name_new_form_ignore_case() { |
| 447 |
let mut target = Target::new(); |
| 448 |
new_ucmd!() |
| 449 |
.arg("-s") |
| 450 |
.arg("KiLl") |
| 451 |
.arg(format!("{}", target.pid())) |
| 452 |
.succeeds(); |
| 453 |
target.assert_signaled(9); |
| 454 |
} |
| 455 |
|
| 456 |
#[test] |
| 457 |
fn test_kill_with_signal_prefixed_name_new_form() { |
| 458 |
let mut target = Target::new(); |
| 459 |
new_ucmd!() |
| 460 |
.arg("-s") |
| 461 |
.arg("SIGKILL") |
| 462 |
.arg(format!("{}", target.pid())) |
| 463 |
.succeeds(); |
| 464 |
target.assert_signaled(9); |
| 465 |
} |
| 466 |
|
| 467 |
#[test] |
| 468 |
fn test_kill_with_signal_prefixed_name_new_form_ignore_case() { |
| 469 |
let mut target = Target::new(); |
| 470 |
new_ucmd!() |
| 471 |
.arg("-s") |
| 472 |
.arg("SiGKiLl") |
| 473 |
.arg(format!("{}", target.pid())) |
| 474 |
.succeeds(); |
| 475 |
target.assert_signaled(9); |
| 476 |
} |
| 477 |
|
| 478 |
#[test] |
| 479 |
fn test_kill_with_signal_name_new_form_unknown_must_match_input_case() { |
| 480 |
let target = Target::new(); |
| 481 |
new_ucmd!() |
| 482 |
.arg("-s") |
| 483 |
.arg("IaMnOtAsIgNaL") |
| 484 |
.arg(format!("{}", target.pid())) |
| 485 |
.fails() |
| 486 |
.stderr_contains("'IaMnOtAsIgNaL': invalid signal"); |
| 487 |
} |
| 488 |
|
| 489 |
#[test] |
| 490 |
fn test_kill_no_pid_provided() { |
| 491 |
new_ucmd!() |
| 492 |
.fails() |
| 493 |
.stderr_contains("no process ID specified"); |
| 494 |
} |
| 495 |
|
| 496 |
#[test] |
| 497 |
fn test_kill_with_signal_exit_new_form() { |
| 498 |
let target = Target::new(); |
| 499 |
new_ucmd!() |
| 500 |
.arg("-s") |
| 501 |
.arg("EXIT") |
| 502 |
.arg(format!("{}", target.pid())) |
| 503 |
.succeeds(); |
| 504 |
} |
| 505 |
|
| 506 |
#[test] |
| 507 |
fn test_kill_with_signal_number_hidden_compatibility_option() { |
| 508 |
let mut target = Target::new(); |
| 509 |
new_ucmd!() |
| 510 |
.arg("-n") |
| 511 |
.arg("9") |
| 512 |
.arg(format!("{}", target.pid())) |
| 513 |
.succeeds(); |
| 514 |
target.assert_signaled(9); |
| 515 |
} |
| 516 |
|
| 517 |
#[test] |
| 518 |
fn test_kill_with_signal_and_list() { |
| 519 |
let target = Target::new(); |
| 520 |
new_ucmd!() |
| 521 |
.arg("-s") |
| 522 |
.arg("EXIT") |
| 523 |
.arg(format!("{}", target.pid())) |
| 524 |
.arg("-l") |
| 525 |
.fails(); |
| 526 |
} |
| 527 |
|
| 528 |
#[test] |
| 529 |
fn test_kill_with_list_lower_bits() { |
| 530 |
new_ucmd!() |
| 531 |
.arg("-l") |
| 532 |
.arg("128") |
| 533 |
.succeeds() |
| 534 |
.stdout_contains("EXIT"); |
| 535 |
|
| 536 |
new_ucmd!() |
| 537 |
.arg("-l") |
| 538 |
.arg("143") |
| 539 |
.succeeds() |
| 540 |
.stdout_contains("TERM"); |
| 541 |
|
| 542 |
new_ucmd!() |
| 543 |
.arg("-l") |
| 544 |
.arg("256") |
| 545 |
.succeeds() |
| 546 |
.stdout_contains("EXIT"); |
| 547 |
|
| 548 |
new_ucmd!() |
| 549 |
.arg("-l") |
| 550 |
.arg("2304") |
| 551 |
.succeeds() |
| 552 |
.stdout_contains("EXIT"); |
| 553 |
} |
| 554 |
|
| 555 |
#[test] |
| 556 |
fn test_kill_with_list_lower_bits_unrecognized() { |
| 557 |
new_ucmd!().arg("-l").arg("111").fails(); |
| 558 |
new_ucmd!().arg("-l").arg("384").fails(); |
| 559 |
} |
| 560 |
|
| 561 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 562 |
#[test] |
| 563 |
fn test_kill_with_list_unnamed_signal_numbers() { |
| 564 |
new_ucmd!() |
| 565 |
.arg("-l") |
| 566 |
.arg("32") |
| 567 |
.succeeds() |
| 568 |
.stdout_only("32\n"); |
| 569 |
new_ucmd!() |
| 570 |
.arg("-l") |
| 571 |
.arg("33") |
| 572 |
.succeeds() |
| 573 |
.stdout_only("33\n"); |
| 574 |
} |
| 575 |
|
| 576 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 577 |
#[test] |
| 578 |
fn test_kill_with_list_all_signal_numbers_up_to_last_named_signal() { |
| 579 |
let last_signal_name = new_ucmd!() |
| 580 |
.arg("-l") |
| 581 |
.succeeds() |
| 582 |
.stdout_str() |
| 583 |
.lines() |
| 584 |
.last() |
| 585 |
.unwrap() |
| 586 |
.to_string(); |
| 587 |
|
| 588 |
let last_signal_number: usize = new_ucmd!() |
| 589 |
.arg("-l") |
| 590 |
.arg("--") |
| 591 |
.arg(&last_signal_name) |
| 592 |
.succeeds() |
| 593 |
.stdout_str() |
| 594 |
.trim() |
| 595 |
.parse() |
| 596 |
.unwrap(); |
| 597 |
|
| 598 |
let args = std::iter::once(String::from("--")) |
| 599 |
.chain((0..=last_signal_number).map(|signal| signal.to_string())) |
| 600 |
.collect::<Vec<_>>(); |
| 601 |
|
| 602 |
new_ucmd!().arg("-l").args(&args).succeeds(); |
| 603 |
} |
| 604 |
|
| 605 |
#[test] |
| 606 |
fn test_kill_with_signal_and_table() { |
| 607 |
let target = Target::new(); |
| 608 |
new_ucmd!() |
| 609 |
.arg("-s") |
| 610 |
.arg("EXIT") |
| 611 |
.arg(format!("{}", target.pid())) |
| 612 |
.arg("-t") |
| 613 |
.fails(); |
| 614 |
} |
| 615 |
|
| 616 |
// Listing signals to a full device must report the write error and exit |
| 617 |
// non-zero, not panic/abort. Covers -l, -l <name>, --list <number>, and --table. |
| 618 |
#[cfg(target_os = "linux")] |
| 619 |
#[test] |
| 620 |
fn test_kill_list_signals_write_error_is_reported() { |
| 621 |
for args in [ |
| 622 |
vec!["-l"], |
| 623 |
vec!["-l", "TERM"], |
| 624 |
vec!["--list", "9"], |
| 625 |
vec!["--table"], |
| 626 |
] { |
| 627 |
new_ucmd!() |
| 628 |
.args(&args) |
| 629 |
.set_stdout(std::fs::File::create("/dev/full").unwrap()) |
| 630 |
.fails() |
| 631 |
.stderr_is("kill: write error: No space left on device\n"); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
/// Test that `kill -1` (signal without PID) reports "no process ID" error |
| 636 |
/// instead of being misinterpreted as pid=-1 which would kill all processes. |
| 637 |
/// This matches GNU kill behavior. |
| 638 |
#[test] |
| 639 |
fn test_kill_signal_only_no_pid() { |
| 640 |
// Test with -1 (SIGHUP) |
| 641 |
new_ucmd!() |
| 642 |
.arg("-1") |
| 643 |
.fails() |
| 644 |
.stderr_contains("no process ID specified"); |
| 645 |
|
| 646 |
// Test with -9 (SIGKILL) |
| 647 |
new_ucmd!() |
| 648 |
.arg("-9") |
| 649 |
.fails() |
| 650 |
.stderr_contains("no process ID specified"); |
| 651 |
|
| 652 |
// Test with -TERM |
| 653 |
new_ucmd!() |
| 654 |
.arg("-TERM") |
| 655 |
.fails() |
| 656 |
.stderr_contains("no process ID specified"); |
| 657 |
} |
| 658 |
|
| 659 |
#[test] |
| 660 |
fn test_kill_signal_zero_process() { |
| 661 |
let target = Target::new(); |
| 662 |
// kill -0 should succeed for a running process (signal 0 = existence check) |
| 663 |
new_ucmd!() |
| 664 |
.arg("-0") |
| 665 |
.arg(format!("{}", target.pid())) |
| 666 |
.succeeds(); |
| 667 |
} |
| 668 |
|
| 669 |
#[test] |
| 670 |
fn test_kill_signal_zero_new_form() { |
| 671 |
let target = Target::new(); |
| 672 |
// kill -s 0 should also work |
| 673 |
new_ucmd!() |
| 674 |
.arg("-s") |
| 675 |
.arg("0") |
| 676 |
.arg(format!("{}", target.pid())) |
| 677 |
.succeeds(); |
| 678 |
} |
| 679 |
|
| 680 |
#[test] |
| 681 |
fn test_kill_signal_zero_nonexistent() { |
| 682 |
// kill -0 with a nonexistent PID should fail |
| 683 |
new_ucmd!().arg("-0").arg("999999999").fails(); |
| 684 |
} |
| 685 |
|
| 686 |
#[test] |
| 687 |
fn test_kill_signal_zero_current_process_group() { |
| 688 |
// Signal 0 never terminates anything (windows opens group members with |
| 689 |
// SYNCHRONIZE only), so this is safe un-isolated. It proves only that the |
| 690 |
// enumeration does not error: probing yourself always succeeds, so it would |
| 691 |
// pass on an empty pid list too. |
| 692 |
new_ucmd!().arg("-0").arg("0").succeeds(); |
| 693 |
} |
| 694 |
|
| 695 |
#[cfg(windows)] |
| 696 |
#[test] |
| 697 |
fn test_kill_windows_help_has_platform_notes() { |
| 698 |
// Every asserted phrase sits entirely inside one `kill-after-help-windows` |
| 699 |
// continuation line: clap's `wrap_help` re-wraps lines longer than the |
| 700 |
// terminal width but never joins short ones, so the .ftl line breaks |
| 701 |
// survive verbatim and these assertions cannot straddle a break. |
| 702 |
new_ucmd!() |
| 703 |
.arg("--help") |
| 704 |
.succeeds() |
| 705 |
.stdout_contains("Windows notes") |
| 706 |
.stdout_contains("PID 0 targets the Job object") |
| 707 |
.stdout_contains("Outside a job") |
| 708 |
.stdout_contains("a Job object is usually not yours"); |
| 709 |
} |
| 710 |
|
| 711 |
#[cfg(windows)] |
| 712 |
#[test] |
| 713 |
fn test_kill_windows_int_and_hup_terminate_directly() { |
| 714 |
for (name, sig) in [("INT", 2), ("HUP", 1)] { |
| 715 |
let mut target = Target::new(); |
| 716 |
new_ucmd!() |
| 717 |
.args(&["-s", name, &target.pid().to_string()]) |
| 718 |
.succeeds(); |
| 719 |
target.assert_signaled(sig); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
#[cfg(windows)] |
| 724 |
#[test] |
| 725 |
fn test_kill_windows_ignored_signals_are_noops() { |
| 726 |
let mut target = Target::new(); |
| 727 |
for sig in ["CHLD", "CONT"] { |
| 728 |
new_ucmd!() |
| 729 |
.args(&["-s", sig, &target.pid().to_string()]) |
| 730 |
.succeeds(); |
| 731 |
} |
| 732 |
target.assert_alive(); |
| 733 |
} |
| 734 |
|
| 735 |
#[cfg(windows)] |
| 736 |
#[test] |
| 737 |
fn test_kill_windows_stop_is_rejected() { |
| 738 |
let mut target = Target::new(); |
| 739 |
new_ucmd!() |
| 740 |
.args(&["-s", "STOP", &target.pid().to_string()]) |
| 741 |
.fails_with_code(1) |
| 742 |
.stderr_contains("unsupported signal"); |
| 743 |
target.assert_alive(); |
| 744 |
} |
| 745 |
|
| 746 |
#[cfg(windows)] |
| 747 |
#[test] |
| 748 |
fn test_kill_windows_negative_pid_unsupported() { |
| 749 |
// `-1` is the dangerous one on unix ("every process you may signal"); on |
| 750 |
// windows it must be inert, and `u32::try_from` fails before any Win32 call. |
| 751 |
new_ucmd!() |
| 752 |
.args(&["-9", "-1"]) |
| 753 |
.fails_with_code(1) |
| 754 |
.stderr_contains("a negative PID"); |
| 755 |
|
| 756 |
let mut target = Target::new(); |
| 757 |
new_ucmd!() |
| 758 |
.arg("--") |
| 759 |
.arg(format!("-{}", target.pid())) |
| 760 |
.fails_with_code(1) |
| 761 |
.stderr_contains("a negative PID"); |
| 762 |
target.assert_alive(); |
| 763 |
} |
| 764 |
|
| 765 |
/// `kill 0` must reach *other* members of the job, not just the caller. |
| 766 |
/// |
| 767 |
/// The victim is the proof: it is neither the killer's parent nor its child, so |
| 768 |
/// the only path by which it can die is the job pid list. An implementation |
| 769 |
/// that regressed to "signal only myself" would still make cmd exit 137, so the |
| 770 |
/// exit code alone would not catch it. |
| 771 |
#[cfg(windows)] |
| 772 |
#[test] |
| 773 |
fn test_kill_windows_pid_zero_terminates_the_whole_job() { |
| 774 |
for (args, signal) in [("-9 0", 9), ("0", 15)] { |
| 775 |
let shell = JobShell::new(); |
| 776 |
let mut victim = Target::new(); |
| 777 |
shell.adopt(&victim.child); |
| 778 |
|
| 779 |
// Only now can anything in the job run. |
| 780 |
let (code, stderr) = shell.run(&format!("\"{}\" kill {args}", get_tests_binary())); |
| 781 |
|
| 782 |
// Read cmd's status first, so a broken kill surfaces its own message |
| 783 |
// rather than only a hanging victim wait below. |
| 784 |
assert_eq!( |
| 785 |
code, |
| 786 |
Some(128 + signal), |
| 787 |
"cmd survived `kill {args}`; kill said: {stderr}" |
| 788 |
); |
| 789 |
victim.assert_signaled(signal); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
// The "not in a job" fallback has no integration test on purpose: nothing can |
| 794 |
// guarantee a spawned process is job-less. `CREATE_BREAKAWAY_FROM_JOB` detaches |
| 795 |
// only from the immediate job, so under a nested chain (`cargo nextest` inside |
| 796 |
// `cargo`) the child stays in an ancestor job and `kill -9 0` terminates the |
| 797 |
// test runner — an earlier version of this test did exactly that. Covered by |
| 798 |
// `with_self_last` in uucore instead, whose empty-member case is that case. |
| 799 |
|
| 800 |
#[cfg(windows)] |
| 801 |
#[test] |
| 802 |
fn test_kill_windows_nonexistent_pid_no_such_process() { |
| 803 |
new_ucmd!() |
| 804 |
.arg("999999999") |
| 805 |
.fails_with_code(1) |
| 806 |
.stderr_contains("No such process"); |
| 807 |
new_ucmd!() |
| 808 |
.args(&["-0", "999999999"]) |
| 809 |
.fails_with_code(1) |
| 810 |
.stderr_contains("No such process"); |
| 811 |
} |
| 812 |
|
| 813 |
#[cfg(windows)] |
| 814 |
#[test] |
| 815 |
fn test_kill_windows_exited_target_with_held_handle() { |
| 816 |
// The Child handle pins the exited process object: terminating still |
| 817 |
// succeeds (unix kill-on-unreaped parity) while -0 reports it gone. |
| 818 |
let mut target = Target::new(); |
| 819 |
target.child.kill().expect("cannot kill target"); |
| 820 |
target.reap(); |
| 821 |
let pid = target.pid().to_string(); |
| 822 |
new_ucmd!().arg(&pid).succeeds(); |
| 823 |
new_ucmd!() |
| 824 |
.args(&["-0", &pid]) |
| 825 |
.fails_with_code(1) |
| 826 |
.stderr_contains("No such process"); |
| 827 |
} |
| 828 |
|
| 829 |
/// Some CI containers leave real-time signals ignored; children inherit that, |
| 830 |
/// so `kill` succeeds but the target survives and exits 0 on its own. |
| 831 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 832 |
fn is_ignored(signal: i32) -> bool { |
| 833 |
let mut action = std::mem::MaybeUninit::<libc::sigaction>::uninit(); |
| 834 |
// SAFETY: a null new-action only queries the current disposition. |
| 835 |
unsafe { |
| 836 |
libc::sigaction(signal, std::ptr::null(), action.as_mut_ptr()) == 0 |
| 837 |
&& action.assume_init().sa_sigaction == libc::SIG_IGN |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 842 |
#[test] |
| 843 |
fn test_kill_realtime_signal() { |
| 844 |
if is_ignored(libc::SIGRTMIN()) { |
| 845 |
return; |
| 846 |
} |
| 847 |
let mut target = Target::new(); |
| 848 |
// kill -s RTMIN should send SIGRTMIN and terminate the process |
| 849 |
new_ucmd!() |
| 850 |
.arg("-s") |
| 851 |
.arg("RTMIN") |
| 852 |
.arg(format!("{}", target.pid())) |
| 853 |
.succeeds(); |
| 854 |
target.assert_signaled(libc::SIGRTMIN()); |
| 855 |
} |
| 856 |
|
| 857 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 858 |
#[test] |
| 859 |
fn test_kill_with_rtmax_offset() { |
| 860 |
let (_, rtmax) = realtime_signal_bounds().unwrap(); |
| 861 |
let sig: i32 = (rtmax as i32) - 7; |
| 862 |
if is_ignored(sig) { |
| 863 |
return; |
| 864 |
} |
| 865 |
|
| 866 |
let mut target = Target::new(); |
| 867 |
new_ucmd!() |
| 868 |
.arg("-s") |
| 869 |
.arg("SIGRTMAX-7") |
| 870 |
.arg(format!("{}", target.pid())) |
| 871 |
.succeeds(); |
| 872 |
target.assert_signaled(sig); |
| 873 |
} |
| 874 |
|
| 875 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 876 |
#[test] |
| 877 |
fn test_kill_with_rtmin_offset() { |
| 878 |
let (rtmin, _) = realtime_signal_bounds().unwrap(); |
| 879 |
let sig: i32 = (rtmin as i32) + 7; |
| 880 |
if is_ignored(sig) { |
| 881 |
return; |
| 882 |
} |
| 883 |
|
| 884 |
let mut target = Target::new(); |
| 885 |
new_ucmd!() |
| 886 |
.arg("-s") |
| 887 |
.arg("SIGRTMIN+7") |
| 888 |
.arg(format!("{}", target.pid())) |
| 889 |
.succeeds(); |
| 890 |
target.assert_signaled(sig); |
| 891 |
} |