1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

tail: fix issue #6543 (--pid when reading from stdin) (#6582)

---------

Co-authored-by: just-an-engineer <Julian.Beltz@zetier.com>
Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
This commit is contained in:
Julian 2024-12-02 13:04:36 -05:00 committed by GitHub
parent 59a712da75
commit 2799b288e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 67 additions and 6 deletions

View file

@ -65,13 +65,15 @@ fn uu_tail(settings: &Settings) -> UResult<()> {
// Add `path` and `reader` to `files` map if `--follow` is selected. // Add `path` and `reader` to `files` map if `--follow` is selected.
for input in &settings.inputs.clone() { for input in &settings.inputs.clone() {
match input.kind() { match input.kind() {
InputKind::File(path) if cfg!(not(unix)) || path != &PathBuf::from(text::DEV_STDIN) => { InputKind::Stdin => {
tail_file(settings, &mut printer, input, path, &mut observer, 0)?;
}
// File points to /dev/stdin here
InputKind::File(_) | InputKind::Stdin => {
tail_stdin(settings, &mut printer, input, &mut observer)?; tail_stdin(settings, &mut printer, input, &mut observer)?;
} }
InputKind::File(path) if cfg!(unix) && path == &PathBuf::from(text::DEV_STDIN) => {
tail_stdin(settings, &mut printer, input, &mut observer)?;
}
InputKind::File(path) => {
tail_file(settings, &mut printer, input, path, &mut observer, 0)?;
}
} }
} }
@ -85,7 +87,7 @@ fn uu_tail(settings: &Settings) -> UResult<()> {
the input file is not a FIFO, pipe, or regular file, it is unspecified whether or the input file is not a FIFO, pipe, or regular file, it is unspecified whether or
not the -f option shall be ignored. not the -f option shall be ignored.
*/ */
if !settings.has_only_stdin() { if !settings.has_only_stdin() || settings.pid != 0 {
follow::follow(observer, settings)?; follow::follow(observer, settings)?;
} }
} }

View file

@ -6,6 +6,7 @@
// spell-checker:ignore (ToDO) abcdefghijklmnopqrstuvwxyz efghijklmnopqrstuvwxyz vwxyz emptyfile file siette ocho nueve diez MULT // spell-checker:ignore (ToDO) abcdefghijklmnopqrstuvwxyz efghijklmnopqrstuvwxyz vwxyz emptyfile file siette ocho nueve diez MULT
// spell-checker:ignore (libs) kqueue // spell-checker:ignore (libs) kqueue
// spell-checker:ignore (jargon) tailable untailable datasame runneradmin tmpi // spell-checker:ignore (jargon) tailable untailable datasame runneradmin tmpi
// spell-checker:ignore (cmd) taskkill
#![allow( #![allow(
clippy::unicode_not_nfc, clippy::unicode_not_nfc,
clippy::cast_lossless, clippy::cast_lossless,
@ -4822,3 +4823,61 @@ fn test_obsolete_encoding_windows() {
.stderr_is("tail: bad argument encoding: '-<2D>b'\n") .stderr_is("tail: bad argument encoding: '-<2D>b'\n")
.code_is(1); .code_is(1);
} }
#[test]
#[cfg(not(target_vendor = "apple"))] // FIXME: for currently not working platforms
fn test_following_with_pid() {
use std::process::Command;
let ts = TestScenario::new(util_name!());
#[cfg(not(windows))]
let mut sleep_command = Command::new("sleep")
.arg("999d")
.spawn()
.expect("failed to start sleep command");
#[cfg(windows)]
let mut sleep_command = Command::new("powershell")
.arg("-Command")
.arg("Start-Sleep -Seconds 999")
.spawn()
.expect("failed to start sleep command");
let sleep_pid = sleep_command.id();
let at = &ts.fixtures;
at.touch("f");
// when -f is specified, tail should die after
// the pid from --pid also dies
let mut child = ts
.ucmd()
.args(&[
"--pid",
&sleep_pid.to_string(),
"-f",
at.plus("f").to_str().unwrap(),
])
.stderr_to_stdout()
.run_no_wait();
child.make_assertion_with_delay(2000).is_alive();
#[cfg(not(windows))]
Command::new("kill")
.arg("-9")
.arg(sleep_pid.to_string())
.output()
.expect("failed to kill sleep command");
#[cfg(windows)]
Command::new("taskkill")
.arg("/PID")
.arg(sleep_pid.to_string())
.arg("/F")
.output()
.expect("failed to kill sleep command");
let _ = sleep_command.wait();
child.make_assertion_with_delay(2000).is_not_alive();
child.kill();
}