coreutils 0.11.0

coreutils ~ GNU coreutils (updated); implemented as universal (cross-platform) utils, written in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore IAMNOTASIGNAL RTMAX RTMIN SIGIO SIGRTMAX GHSA CHLD SIGSTOP taskkill unreaped
use regex::Regex;
#[cfg(windows)]
use std::io::Write;
#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;
#[cfg(windows)]
use std::process::Stdio;
use std::process::{Child, Command, ExitStatus};
#[cfg(windows)]
use uucore::process::Job;
#[cfg(any(target_os = "linux", target_os = "android"))]
use uucore::signals::realtime_signal_bounds;
use uutests::new_ucmd;
use uutests::util::get_tests_binary;

// A child process the tests will try to kill: `<coreutils> sleep 30` via the
// multicall test binary, which exists on every test platform (unlike `sleep`
// from PATH on Windows). The natural death after 30s avoids hanging failing
// tests.
struct Target {
    child: Child,
    killed: bool,
}

impl Target {
    fn new() -> Self {
        Self {
            child: Command::new(get_tests_binary())
                .args(["sleep", "30"])
                .spawn()
                .expect("cannot spawn target"),
            killed: false,
        }
    }

    /// Wait for the target to exit, so `Drop` no longer has to kill it.
    fn reap(&mut self) -> ExitStatus {
        let status = self.child.wait().expect("cannot wait on target");
        self.killed = true;
        status
    }

    /// Reap the target and assert it was killed by `signal` (`128 + n` exit
    /// code on windows).
    fn assert_signaled(&mut self, signal: i32) {
        let status = self.reap();
        #[cfg(unix)]
        assert_eq!(
            status.signal(),
            Some(signal),
            "target exited with {status:?} instead of dying from signal {signal}"
        );
        #[cfg(windows)]
        assert_eq!(
            status.code(),
            Some(128 + signal),
            "target exited with {status:?} instead of dying from signal {signal}"
        );
    }

    #[cfg(windows)]
    fn assert_alive(&mut self) {
        assert!(self.child.try_wait().expect("cannot poll target").is_none());
    }

    fn pid(&self) -> u32 {
        self.child.id()
    }
}

impl Drop for Target {
    // Terminates this target to avoid littering test boxes with zombi processes
    // when a test fails after creating a target but before killing it.
    fn drop(&mut self) {
        if !self.killed {
            self.child.kill().expect("cannot kill target");
        }
    }
}

/// A `cmd.exe` in a freshly created Job object, waiting for a command line on
/// its stdin pipe.
///
/// The only safe way to run a *terminating* `kill 0` from a test: `kill 0`
/// signals the killer's immediate job, which here is `job` and nothing else, so
/// it cannot reach the test runner, another test's children, nextest's per-test
/// job or a CI agent — the ambient job is `job`'s *parent*, and a parent's
/// members are not in a child job's list.
///
/// The gate is what makes that true rather than merely likely: `cmd` gets no
/// command until `assign` has succeeded, so the window between `CreateProcess`
/// and `AssignProcessToJobObject` contains nothing that could signal. Fail-safe
/// too — if anything panics before [`JobShell::run`], the pipe closes, `cmd`
/// sees EOF and exits having executed nothing.
#[cfg(windows)]
struct JobShell {
    job: Job,
    shell: Child,
}

#[cfg(windows)]
impl JobShell {
    fn new() -> Self {
        let job = Job::new().expect("cannot create job object");
        let shell = Command::new("cmd")
            // `/d` skips the AutoRun registry command, so a machine that has
            // one cannot run it inside this job or write to the stderr the
            // assertions quote.
            .args(["/d", "/q"])
            .stdin(Stdio::piped())
            .stdout(Stdio::null())
            .stderr(Stdio::piped())
            .spawn()
            .expect("cannot spawn cmd");
        job.assign(&shell)
            .expect("cannot assign cmd to the fresh job");
        Self { job, shell }
    }

    /// So a group signal from inside reaches `child`.
    fn adopt(&self, child: &Child) {
        self.job
            .assign(child)
            .expect("cannot assign process to the fresh job");
    }

    /// Closes stdin afterwards so cmd exits at EOF rather than blocking forever
    /// if the command fails to kill it.
    fn run(mut self, command: &str) -> (Option<i32>, String) {
        let mut stdin = self.shell.stdin.take().expect("cmd has no stdin pipe");
        write!(stdin, "{command}\r\n").expect("cannot write command");
        drop(stdin);
        let output = self.shell.wait_with_output().expect("cannot wait on cmd");
        (
            output.status.code(),
            String::from_utf8_lossy(&output.stderr).into_owned(),
        )
    }
}

#[test]
fn test_invalid_arg() {
    new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
}

#[test]
fn test_kill_list_all_signals() {
    // Check for a few signals.  Do not try to be comprehensive.
    new_ucmd!()
        .arg("-l")
        .succeeds()
        .stdout_contains("KILL")
        .stdout_contains("TERM")
        .stdout_contains("HUP")
        .stdout_contains("EXIT");
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_kill_list_contains_realtime_signals() {
    new_ucmd!()
        .arg("-l")
        .succeeds()
        .stdout_contains("RTMIN")
        .stdout_contains("RTMAX");
}

#[test]
fn test_kill_list_final_new_line() {
    let re = Regex::new("\\n$").unwrap();
    assert!(re.is_match(new_ucmd!().arg("-l").succeeds().stdout_str()));
}

#[test]
fn test_kill_list_all_signals_as_table() {
    // Check for a few signals.  Do not try to be comprehensive.
    new_ucmd!()
        .arg("-t")
        .succeeds()
        .stdout_contains("KILL")
        .stdout_contains("TERM")
        .stdout_contains("HUP")
        .stdout_contains("EXIT");
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_kill_table_contains_realtime_signals() {
    new_ucmd!()
        .arg("-t")
        .succeeds()
        .stdout_contains("RTMIN")
        .stdout_contains("RTMAX");
}

#[test]
fn test_kill_table_starts_at_0() {
    new_ucmd!()
        .arg("-t")
        .succeeds()
        .stdout_matches(&Regex::new("^\\s?0\\sEXIT").unwrap());
}

#[test]
fn test_kill_table_lists_all_vertically() {
    // Check for a few signals.  Do not try to be comprehensive.
    let command = new_ucmd!().arg("-t").succeeds();
    let signals = command
        .stdout_str()
        .split('\n')
        .filter_map(|line| line.trim().split(' ').nth(1))
        .collect::<Vec<&str>>();

    assert!(signals.contains(&"KILL"));
    assert!(signals.contains(&"TERM"));
    assert!(signals.contains(&"HUP"));
    assert!(signals.contains(&"EXIT"));
}

#[test]
fn test_kill_list_one_signal_from_number() {
    new_ucmd!()
        .arg("-l")
        .arg("9")
        .succeeds()
        .stdout_only("KILL\n");
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_kill_list_rtmax_from_name() {
    new_ucmd!()
        .arg("-l")
        .arg("RTMAX")
        .succeeds()
        .stdout_only(format!("{}\n", libc::SIGRTMAX()));
}

#[test]
fn test_kill_list_one_signal_from_invalid_number() {
    new_ucmd!()
        .arg("-l")
        .arg("99")
        .fails()
        .stderr_contains("'99': invalid signal");
}

#[test]
fn test_kill_list_one_signal_from_name() {
    // Use SIGKILL because it is 9 on all unixes.
    new_ucmd!()
        .arg("-l")
        .arg("KILL")
        .succeeds()
        .stdout_matches(&Regex::new("\\b9\\b").unwrap());
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_kill_list_sigio_alias() {
    for signal in ["IO", "SIGIO"] {
        new_ucmd!()
            .arg("-l")
            .arg(signal)
            .succeeds()
            .stdout_only(format!("{}\n", libc::SIGIO));
    }
}

#[test]
fn test_kill_list_one_signal_ignore_case() {
    // Use SIGKILL because it is 9 on all unixes.
    new_ucmd!()
        .arg("-l")
        .arg("KiLl")
        .succeeds()
        .stdout_matches(&Regex::new("\\b9\\b").unwrap());
}

#[test]
fn test_kill_list_unknown_must_match_input_case() {
    new_ucmd!()
        .arg("-l")
        .arg("IaMnOtAsIgNaL")
        .fails()
        .stderr_contains("IaMnOtAsIgNaL");
}

#[test]
fn test_kill_list_all_vertically() {
    // Check for a few signals.  Do not try to be comprehensive.
    let command = new_ucmd!().arg("-l").succeeds();
    let signals = command.stdout_str().split('\n').collect::<Vec<&str>>();
    assert!(signals.contains(&"KILL"));
    assert!(signals.contains(&"TERM"));
    assert!(signals.contains(&"HUP"));
    assert!(signals.contains(&"EXIT"));
}

#[test]
fn test_kill_list_two_signal_from_name() {
    new_ucmd!()
        .arg("-l")
        .arg("INT")
        .arg("KILL")
        .succeeds()
        .stdout_matches(&Regex::new("\\d\n\\d").unwrap());
}

#[test]
fn test_kill_list_three_signal_first_unknown() {
    new_ucmd!()
        .arg("-l")
        .arg("IAMNOTASIGNAL")
        .arg("INT")
        .arg("KILL")
        .fails()
        .stderr_contains("'IAMNOTASIGNAL': invalid signal")
        .stdout_matches(&Regex::new("\\d\n\\d").unwrap());
}

#[test]
fn test_kill_set_bad_signal_name() {
    new_ucmd!()
        .arg("-s")
        .arg("IAMNOTASIGNAL")
        .fails()
        .stderr_contains("'IAMNOTASIGNAL': invalid signal");
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_kill_accepts_sigio_alias_for_sending() {
    for args in [["--signal=SIGIO", "999999999"], ["-SIGIO", "999999999"]] {
        new_ucmd!()
            .args(&args)
            .fails_with_code(1)
            .stderr_contains("sending signal")
            .stderr_does_not_contain("invalid signal");
    }
}

#[test]
fn test_kill_out_of_range_signal_is_rejected_not_sent() {
    // An out-of-range signal number must be rejected up front (like GNU), not
    // fall through to be parsed as a negative PID and signalled with the
    // default SIGTERM. Regression for GHSA-3jmh-xh36-pj6v.
    for bad in ["-65", "-129"] {
        let mut target = Target::new();
        new_ucmd!()
            .arg(bad)
            .arg(format!("{}", target.pid()))
            .fails_with_code(1)
            .stderr_contains("invalid signal");
        // The target must have survived: kill it for real and confirm the
        // exit came from std's kill (SIGKILL / code 1), not a stray uu-kill
        // TERM (which would report 143 on windows).
        target.child.kill().expect("cannot kill surviving target");
        let status = target.reap();
        #[cfg(unix)]
        assert_eq!(status.signal(), Some(9));
        #[cfg(windows)]
        assert_eq!(status.code(), Some(1));
    }
}

#[test]
fn test_kill_with_default_signal() {
    let mut target = Target::new();
    new_ucmd!().arg(format!("{}", target.pid())).succeeds();
    target.assert_signaled(15);
}

#[test]
fn test_kill_with_signal_number_old_form() {
    let mut target = Target::new();
    new_ucmd!()
        .arg("-9")
        .arg(format!("{}", target.pid()))
        .succeeds();
    target.assert_signaled(9);
}

#[test]
fn test_kill_with_signal_name_old_form() {
    for arg in ["-Kill", "-KILL"] {
        let mut target = Target::new();
        new_ucmd!()
            .arg(arg)
            .arg(format!("{}", target.pid()))
            .succeeds();
        target.assert_signaled(9);
    }
}

#[test]
fn test_kill_with_lower_case_signal_name_old_form() {
    let target = Target::new();
    new_ucmd!()
        .arg("-kill")
        .arg(format!("{}", target.pid()))
        .fails()
        .stderr_contains("unexpected argument");
}

#[test]
fn test_kill_with_signal_prefixed_name_old_form() {
    let mut target = Target::new();
    new_ucmd!()
        .arg("-SIGKILL")
        .arg(format!("{}", target.pid()))
        .succeeds();
    target.assert_signaled(9);
}

#[test]
fn test_kill_with_signal_number_new_form() {
    let mut target = Target::new();
    new_ucmd!()
        .arg("-s")
        .arg("9")
        .arg(format!("{}", target.pid()))
        .succeeds();
    target.assert_signaled(9);
}

#[test]
fn test_kill_with_signal_name_new_form() {
    let mut target = Target::new();
    new_ucmd!()
        .arg("-s")
        .arg("KILL")
        .arg(format!("{}", target.pid()))
        .succeeds();
    target.assert_signaled(9);
}

#[test]
fn test_kill_with_signal_name_new_form_ignore_case() {
    let mut target = Target::new();
    new_ucmd!()
        .arg("-s")
        .arg("KiLl")
        .arg(format!("{}", target.pid()))
        .succeeds();
    target.assert_signaled(9);
}

#[test]
fn test_kill_with_signal_prefixed_name_new_form() {
    let mut target = Target::new();
    new_ucmd!()
        .arg("-s")
        .arg("SIGKILL")
        .arg(format!("{}", target.pid()))
        .succeeds();
    target.assert_signaled(9);
}

#[test]
fn test_kill_with_signal_prefixed_name_new_form_ignore_case() {
    let mut target = Target::new();
    new_ucmd!()
        .arg("-s")
        .arg("SiGKiLl")
        .arg(format!("{}", target.pid()))
        .succeeds();
    target.assert_signaled(9);
}

#[test]
fn test_kill_with_signal_name_new_form_unknown_must_match_input_case() {
    let target = Target::new();
    new_ucmd!()
        .arg("-s")
        .arg("IaMnOtAsIgNaL")
        .arg(format!("{}", target.pid()))
        .fails()
        .stderr_contains("'IaMnOtAsIgNaL': invalid signal");
}

#[test]
fn test_kill_no_pid_provided() {
    new_ucmd!()
        .fails()
        .stderr_contains("no process ID specified");
}

#[test]
fn test_kill_with_signal_exit_new_form() {
    let target = Target::new();
    new_ucmd!()
        .arg("-s")
        .arg("EXIT")
        .arg(format!("{}", target.pid()))
        .succeeds();
}

#[test]
fn test_kill_with_signal_number_hidden_compatibility_option() {
    let mut target = Target::new();
    new_ucmd!()
        .arg("-n")
        .arg("9")
        .arg(format!("{}", target.pid()))
        .succeeds();
    target.assert_signaled(9);
}

#[test]
fn test_kill_with_signal_and_list() {
    let target = Target::new();
    new_ucmd!()
        .arg("-s")
        .arg("EXIT")
        .arg(format!("{}", target.pid()))
        .arg("-l")
        .fails();
}

#[test]
fn test_kill_with_list_lower_bits() {
    new_ucmd!()
        .arg("-l")
        .arg("128")
        .succeeds()
        .stdout_contains("EXIT");

    new_ucmd!()
        .arg("-l")
        .arg("143")
        .succeeds()
        .stdout_contains("TERM");

    new_ucmd!()
        .arg("-l")
        .arg("256")
        .succeeds()
        .stdout_contains("EXIT");

    new_ucmd!()
        .arg("-l")
        .arg("2304")
        .succeeds()
        .stdout_contains("EXIT");
}

#[test]
fn test_kill_with_list_lower_bits_unrecognized() {
    new_ucmd!().arg("-l").arg("111").fails();
    new_ucmd!().arg("-l").arg("384").fails();
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_kill_with_list_unnamed_signal_numbers() {
    new_ucmd!()
        .arg("-l")
        .arg("32")
        .succeeds()
        .stdout_only("32\n");
    new_ucmd!()
        .arg("-l")
        .arg("33")
        .succeeds()
        .stdout_only("33\n");
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_kill_with_list_all_signal_numbers_up_to_last_named_signal() {
    let last_signal_name = new_ucmd!()
        .arg("-l")
        .succeeds()
        .stdout_str()
        .lines()
        .last()
        .unwrap()
        .to_string();

    let last_signal_number: usize = new_ucmd!()
        .arg("-l")
        .arg("--")
        .arg(&last_signal_name)
        .succeeds()
        .stdout_str()
        .trim()
        .parse()
        .unwrap();

    let args = std::iter::once(String::from("--"))
        .chain((0..=last_signal_number).map(|signal| signal.to_string()))
        .collect::<Vec<_>>();

    new_ucmd!().arg("-l").args(&args).succeeds();
}

#[test]
fn test_kill_with_signal_and_table() {
    let target = Target::new();
    new_ucmd!()
        .arg("-s")
        .arg("EXIT")
        .arg(format!("{}", target.pid()))
        .arg("-t")
        .fails();
}

// Listing signals to a full device must report the write error and exit
// non-zero, not panic/abort. Covers -l, -l <name>, --list <number>, and --table.
#[cfg(target_os = "linux")]
#[test]
fn test_kill_list_signals_write_error_is_reported() {
    for args in [
        vec!["-l"],
        vec!["-l", "TERM"],
        vec!["--list", "9"],
        vec!["--table"],
    ] {
        new_ucmd!()
            .args(&args)
            .set_stdout(std::fs::File::create("/dev/full").unwrap())
            .fails()
            .stderr_is("kill: write error: No space left on device\n");
    }
}

/// Test that `kill -1` (signal without PID) reports "no process ID" error
/// instead of being misinterpreted as pid=-1 which would kill all processes.
/// This matches GNU kill behavior.
#[test]
fn test_kill_signal_only_no_pid() {
    // Test with -1 (SIGHUP)
    new_ucmd!()
        .arg("-1")
        .fails()
        .stderr_contains("no process ID specified");

    // Test with -9 (SIGKILL)
    new_ucmd!()
        .arg("-9")
        .fails()
        .stderr_contains("no process ID specified");

    // Test with -TERM
    new_ucmd!()
        .arg("-TERM")
        .fails()
        .stderr_contains("no process ID specified");
}

#[test]
fn test_kill_signal_zero_process() {
    let target = Target::new();
    // kill -0 should succeed for a running process (signal 0 = existence check)
    new_ucmd!()
        .arg("-0")
        .arg(format!("{}", target.pid()))
        .succeeds();
}

#[test]
fn test_kill_signal_zero_new_form() {
    let target = Target::new();
    // kill -s 0 should also work
    new_ucmd!()
        .arg("-s")
        .arg("0")
        .arg(format!("{}", target.pid()))
        .succeeds();
}

#[test]
fn test_kill_signal_zero_nonexistent() {
    // kill -0 with a nonexistent PID should fail
    new_ucmd!().arg("-0").arg("999999999").fails();
}

#[test]
fn test_kill_signal_zero_current_process_group() {
    // Signal 0 never terminates anything (windows opens group members with
    // SYNCHRONIZE only), so this is safe un-isolated. It proves only that the
    // enumeration does not error: probing yourself always succeeds, so it would
    // pass on an empty pid list too.
    new_ucmd!().arg("-0").arg("0").succeeds();
}

#[cfg(windows)]
#[test]
fn test_kill_windows_help_has_platform_notes() {
    // Every asserted phrase sits entirely inside one `kill-after-help-windows`
    // continuation line: clap's `wrap_help` re-wraps lines longer than the
    // terminal width but never joins short ones, so the .ftl line breaks
    // survive verbatim and these assertions cannot straddle a break.
    new_ucmd!()
        .arg("--help")
        .succeeds()
        .stdout_contains("Windows notes")
        .stdout_contains("PID 0 targets the Job object")
        .stdout_contains("Outside a job")
        .stdout_contains("a Job object is usually not yours");
}

#[cfg(windows)]
#[test]
fn test_kill_windows_int_and_hup_terminate_directly() {
    for (name, sig) in [("INT", 2), ("HUP", 1)] {
        let mut target = Target::new();
        new_ucmd!()
            .args(&["-s", name, &target.pid().to_string()])
            .succeeds();
        target.assert_signaled(sig);
    }
}

#[cfg(windows)]
#[test]
fn test_kill_windows_ignored_signals_are_noops() {
    let mut target = Target::new();
    for sig in ["CHLD", "CONT"] {
        new_ucmd!()
            .args(&["-s", sig, &target.pid().to_string()])
            .succeeds();
    }
    target.assert_alive();
}

#[cfg(windows)]
#[test]
fn test_kill_windows_stop_is_rejected() {
    let mut target = Target::new();
    new_ucmd!()
        .args(&["-s", "STOP", &target.pid().to_string()])
        .fails_with_code(1)
        .stderr_contains("unsupported signal");
    target.assert_alive();
}

#[cfg(windows)]
#[test]
fn test_kill_windows_negative_pid_unsupported() {
    // `-1` is the dangerous one on unix ("every process you may signal"); on
    // windows it must be inert, and `u32::try_from` fails before any Win32 call.
    new_ucmd!()
        .args(&["-9", "-1"])
        .fails_with_code(1)
        .stderr_contains("a negative PID");

    let mut target = Target::new();
    new_ucmd!()
        .arg("--")
        .arg(format!("-{}", target.pid()))
        .fails_with_code(1)
        .stderr_contains("a negative PID");
    target.assert_alive();
}

/// `kill 0` must reach *other* members of the job, not just the caller.
///
/// The victim is the proof: it is neither the killer's parent nor its child, so
/// the only path by which it can die is the job pid list. An implementation
/// that regressed to "signal only myself" would still make cmd exit 137, so the
/// exit code alone would not catch it.
#[cfg(windows)]
#[test]
fn test_kill_windows_pid_zero_terminates_the_whole_job() {
    for (args, signal) in [("-9 0", 9), ("0", 15)] {
        let shell = JobShell::new();
        let mut victim = Target::new();
        shell.adopt(&victim.child);

        // Only now can anything in the job run.
        let (code, stderr) = shell.run(&format!("\"{}\" kill {args}", get_tests_binary()));

        // Read cmd's status first, so a broken kill surfaces its own message
        // rather than only a hanging victim wait below.
        assert_eq!(
            code,
            Some(128 + signal),
            "cmd survived `kill {args}`; kill said: {stderr}"
        );
        victim.assert_signaled(signal);
    }
}

// The "not in a job" fallback has no integration test on purpose: nothing can
// guarantee a spawned process is job-less. `CREATE_BREAKAWAY_FROM_JOB` detaches
// only from the immediate job, so under a nested chain (`cargo nextest` inside
// `cargo`) the child stays in an ancestor job and `kill -9 0` terminates the
// test runner — an earlier version of this test did exactly that. Covered by
// `with_self_last` in uucore instead, whose empty-member case is that case.

#[cfg(windows)]
#[test]
fn test_kill_windows_nonexistent_pid_no_such_process() {
    new_ucmd!()
        .arg("999999999")
        .fails_with_code(1)
        .stderr_contains("No such process");
    new_ucmd!()
        .args(&["-0", "999999999"])
        .fails_with_code(1)
        .stderr_contains("No such process");
}

#[cfg(windows)]
#[test]
fn test_kill_windows_exited_target_with_held_handle() {
    // The Child handle pins the exited process object: terminating still
    // succeeds (unix kill-on-unreaped parity) while -0 reports it gone.
    let mut target = Target::new();
    target.child.kill().expect("cannot kill target");
    target.reap();
    let pid = target.pid().to_string();
    new_ucmd!().arg(&pid).succeeds();
    new_ucmd!()
        .args(&["-0", &pid])
        .fails_with_code(1)
        .stderr_contains("No such process");
}

/// Some CI containers leave real-time signals ignored; children inherit that,
/// so `kill` succeeds but the target survives and exits 0 on its own.
#[cfg(any(target_os = "linux", target_os = "android"))]
fn is_ignored(signal: i32) -> bool {
    let mut action = std::mem::MaybeUninit::<libc::sigaction>::uninit();
    // SAFETY: a null new-action only queries the current disposition.
    unsafe {
        libc::sigaction(signal, std::ptr::null(), action.as_mut_ptr()) == 0
            && action.assume_init().sa_sigaction == libc::SIG_IGN
    }
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_kill_realtime_signal() {
    if is_ignored(libc::SIGRTMIN()) {
        return;
    }
    let mut target = Target::new();
    // kill -s RTMIN should send SIGRTMIN and terminate the process
    new_ucmd!()
        .arg("-s")
        .arg("RTMIN")
        .arg(format!("{}", target.pid()))
        .succeeds();
    target.assert_signaled(libc::SIGRTMIN());
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_kill_with_rtmax_offset() {
    let (_, rtmax) = realtime_signal_bounds().unwrap();
    let sig: i32 = (rtmax as i32) - 7;
    if is_ignored(sig) {
        return;
    }

    let mut target = Target::new();
    new_ucmd!()
        .arg("-s")
        .arg("SIGRTMAX-7")
        .arg(format!("{}", target.pid()))
        .succeeds();
    target.assert_signaled(sig);
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_kill_with_rtmin_offset() {
    let (rtmin, _) = realtime_signal_bounds().unwrap();
    let sig: i32 = (rtmin as i32) + 7;
    if is_ignored(sig) {
        return;
    }

    let mut target = Target::new();
    new_ucmd!()
        .arg("-s")
        .arg("SIGRTMIN+7")
        .arg(format!("{}", target.pid()))
        .succeeds();
    target.assert_signaled(sig);
}