Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
addf7b5
Added in logic in tail to be able to follow stdin when a PID is speci…
Jul 18, 2024
03b3bf1
Add test to follow file until specified PID dies. Remove comment
Jul 19, 2024
05262b6
Merge branch 'uutils:main' into main
just-an-engineer Jul 19, 2024
aa8f2af
Improve pid test for tail
Jul 19, 2024
bbefeba
cargo fmt
Jul 20, 2024
cbbef09
Increase assert is alive in test_following_with_pid in test_tail to 1…
Jul 23, 2024
520e8b8
Added in logic in tail to be able to follow stdin when a PID is speci…
Jul 18, 2024
b242461
Add test to follow file until specified PID dies. Remove comment
Jul 19, 2024
465ef45
Improve pid test for tail
Jul 19, 2024
731e4e1
cargo fmt
Jul 20, 2024
fed2705
Increase assert is alive in test_following_with_pid in test_tail to 1…
Jul 23, 2024
a4cc3a7
Changed test_following_with_pid to read from a file instead of stdio:…
Jul 28, 2024
e7d1c0b
Merge branch 'main' of https://github.com/just-an-engineer/coreutils …
Jul 28, 2024
1ade9ac
Merge branch 'main' into main
just-an-engineer Jul 28, 2024
0811fea
Apple doesn't currently work for passing in files to stdin
Jul 29, 2024
73bbd9a
Merge branch 'main' of https://github.com/just-an-engineer/coreutils …
Jul 29, 2024
bcc144b
Got test_following_with_pid to work on Windows
just-an-engineer Jul 29, 2024
b8c5bc8
is_not_alive -> is_alive in test_following_with_pid
Jul 29, 2024
b120a56
Fixed `is_not_alive` issue on linux by reaping child sleep process, o…
Jul 29, 2024
8c5097a
Add missed `mut` to windows sleep_command in test_tail for test_follo…
just-an-engineer Jul 29, 2024
1f0d9d5
Fixed formatting, spelling, and made clippy happy
Jul 30, 2024
98c546f
Merge branch 'main' into main
sylvestre Jul 30, 2024
09e2610
Merge branch 'main' into main
just-an-engineer Aug 7, 2024
59aea7e
Merge branch 'main' into main
sylvestre Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/uu/tail/src/tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ fn uu_tail(settings: &Settings) -> UResult<()> {
// Add `path` and `reader` to `files` map if `--follow` is selected.
for input in &settings.inputs.clone() {
match input.kind() {
InputKind::File(path) if cfg!(not(unix)) || path != &PathBuf::from(text::DEV_STDIN) => {
tail_file(settings, &mut printer, input, path, &mut observer, 0)?;
InputKind::Stdin => {
tail_stdin(settings, &mut printer, input, &mut observer)?;
}
// File points to /dev/stdin here
InputKind::File(_) | InputKind::Stdin => {
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)?;
}
}
}

Expand All @@ -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
not the -f option shall be ignored.
*/
if !settings.has_only_stdin() {
if !settings.has_only_stdin() || settings.pid != 0 {
follow::follow(observer, settings)?;
}
}
Expand Down
59 changes: 59 additions & 0 deletions tests/by-util/test_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// spell-checker:ignore (ToDO) abcdefghijklmnopqrstuvwxyz efghijklmnopqrstuvwxyz vwxyz emptyfile file siette ocho nueve diez MULT
// spell-checker:ignore (libs) kqueue
// spell-checker:ignore (jargon) tailable untailable datasame runneradmin tmpi
// spell-checker:ignore (cmd) taskkill
#![allow(
clippy::unicode_not_nfc,
clippy::cast_lossless,
Expand Down Expand Up @@ -4824,3 +4825,61 @@ fn test_obsolete_encoding_windows() {
.stderr_is("tail: bad argument encoding: '-�b'\n")
.code_is(1);
}

#[test]
#[cfg(not(target_vendor = "apple"))] // FIXME: for currently not working platforms
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add details why it isn't working on other os

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I'm sorry I didn't answer for over a month. I remember seeing your message shortly after you wrote it, but I don't remember after that, if I got busy or what. I had thought I had written something for it, but I guess not.
I think I had known at one point, but I don't have an Apple device to test on, and so currently I don't actually know.
I'll reach out to a friend with one and see if they can figure it out or not, and try to update this thread or the code accordingly.

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();
}
Loading