|
2 | 2 | // |
3 | 3 | // For the full copyright and license information, please view the LICENSE |
4 | 4 | // file that was distributed with this source code. |
5 | | -// spell-checker:ignore dyld dylib setvbuf |
| 5 | +// spell-checker:ignore cmdline dyld dylib PDEATHSIG setvbuf |
6 | 6 | #[cfg(target_os = "linux")] |
7 | 7 | use uutests::at_and_ucmd; |
8 | 8 | use uutests::new_ucmd; |
@@ -276,3 +276,72 @@ fn test_stdbuf_non_utf8_paths() { |
276 | 276 | .succeeds() |
277 | 277 | .stdout_is("test content for stdbuf\n"); |
278 | 278 | } |
| 279 | + |
| 280 | +#[test] |
| 281 | +#[cfg(target_os = "linux")] |
| 282 | +fn test_stdbuf_no_fork_regression() { |
| 283 | + // Regression test for issue #9066: https://github.com/uutils/coreutils/issues/9066 |
| 284 | + // The original stdbuf implementation used fork+spawn which broke signal handling |
| 285 | + // and PR_SET_PDEATHSIG. This test verifies that stdbuf uses exec() instead. |
| 286 | + // With fork: stdbuf process would remain visible in process list |
| 287 | + // With exec: stdbuf process is replaced by target command (GNU compatible) |
| 288 | + |
| 289 | + use std::process::{Command, Stdio}; |
| 290 | + use std::thread; |
| 291 | + use std::time::Duration; |
| 292 | + |
| 293 | + let scene = TestScenario::new(util_name!()); |
| 294 | + |
| 295 | + // Start stdbuf with a long-running command |
| 296 | + let mut child = Command::new(&scene.bin_path) |
| 297 | + .args(["stdbuf", "-o0", "sleep", "3"]) |
| 298 | + .stdout(Stdio::null()) |
| 299 | + .stderr(Stdio::null()) |
| 300 | + .spawn() |
| 301 | + .expect("Failed to start stdbuf"); |
| 302 | + |
| 303 | + let child_pid = child.id(); |
| 304 | + |
| 305 | + // Poll until exec happens or timeout |
| 306 | + let cmdline_path = format!("/proc/{child_pid}/cmdline"); |
| 307 | + let timeout = Duration::from_secs(2); |
| 308 | + let poll_interval = Duration::from_millis(10); |
| 309 | + let start_time = std::time::Instant::now(); |
| 310 | + |
| 311 | + let command_name = loop { |
| 312 | + if start_time.elapsed() > timeout { |
| 313 | + child.kill().ok(); |
| 314 | + panic!("TIMEOUT: Process {child_pid} did not respond within {timeout:?}"); |
| 315 | + } |
| 316 | + |
| 317 | + if let Ok(cmdline) = std::fs::read_to_string(&cmdline_path) { |
| 318 | + let cmd_parts: Vec<&str> = cmdline.split('\0').collect(); |
| 319 | + let name = cmd_parts.first().map_or("", |v| v); |
| 320 | + |
| 321 | + // Wait for exec to complete (process name changes from original binary to target) |
| 322 | + // Handle both multicall binary (coreutils) and individual utilities (stdbuf) |
| 323 | + if !name.contains("coreutils") && !name.contains("stdbuf") && !name.is_empty() { |
| 324 | + break name.to_string(); |
| 325 | + } |
| 326 | + } |
| 327 | + |
| 328 | + thread::sleep(poll_interval); |
| 329 | + }; |
| 330 | + |
| 331 | + // The loop already waited for exec (no longer original binary), so this should always pass |
| 332 | + // But keep the assertion as a safety check and clear documentation |
| 333 | + assert!( |
| 334 | + !command_name.contains("coreutils") && !command_name.contains("stdbuf"), |
| 335 | + "REGRESSION: Process {child_pid} is still original binary (coreutils or stdbuf) - fork() used instead of exec()" |
| 336 | + ); |
| 337 | + |
| 338 | + // Ensure we're running the expected target command |
| 339 | + assert!( |
| 340 | + command_name.contains("sleep"), |
| 341 | + "Expected 'sleep' command at PID {child_pid}, got: {command_name}" |
| 342 | + ); |
| 343 | + |
| 344 | + // Cleanup |
| 345 | + child.kill().ok(); |
| 346 | + child.wait().ok(); |
| 347 | +} |
0 commit comments