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

tac: should fail when stdin is closed but doesn't #2873

Open
jfinkels opened this issue Jan 16, 2022 · 6 comments
Open

tac: should fail when stdin is closed but doesn't #2873

jfinkels opened this issue Jan 16, 2022 · 6 comments
Labels

Comments

@jfinkels
Copy link
Collaborator

jfinkels commented Jan 16, 2022

The uutils version of tac disagrees with GNU tac in the case where stdin is closed on the command line:

GNU prints an error message to stderr and returns exit status code 1:

$ tac <&-
tac: 'standard input': read error: Bad file descriptor
tac: -: Bad file descriptor

<&- means close stdin.

uutils prints nothing and returns exit status code 0:

$ ./target/debug/coreutils tac <&-

Edit: This causes a failure in the GNU test file tests/tac/tac-2-nonseekable.sh.

@bjorn3
Copy link

bjorn3 commented Jan 29, 2022

Libstd opens stdin, stdout and stderr as /dev/null if they aren't open at program start to prevent std::io::{stdin,stdout,stderr} from pointing to unrelated file resources that are opened later. See https://github.com/rust-lang/rust/blob/17dfae79bbc3dabe1427073086acf7f7bd45148c/library/std/src/sys/unix/mod.rs#L54-L57

@tavianator
Copy link

The stdlib could open /dev/null with different permissions in that case so that reads/writes still fail, like this: https://github.com/tavianator/bfs/blob/f4772fe4c13eb8b9b3c199d4e1703c242babcfd7/main.c#L88-L114

@jhscheer
Copy link
Contributor

jhscheer commented May 9, 2022

I came across this because one of the tests from gnu/tests/tail-2/follow-stdin.sh also uses <&- to provoke the bad fd error. (Actually <&- is also used in: gnu/tests/misc/{tac-2-nonseekable, sort-merge-fdlimit, sort-continue, tty}.sh and gnu/tests/cp/cp-mv-backup.sh).

I tried this, but it didn't work and I don't know why:

fn get_errno() -> i32 {
    Error::last_os_error().raw_os_error().unwrap()
}
pub fn stdin_is_bad_fd() -> bool {
    let fd = stdin().as_raw_fd();
    // FIXME: this is never `true`, even with `<&-`
    unsafe { libc::fcntl(fd, libc::F_GETFD) == -1 && get_errno() == libc::EBADF }
}

@tavianator
Copy link

@jhscheer It's because of what @bjorn3 wrote in #2873 (comment)

@jhscheer
Copy link
Contributor

jhscheer commented May 9, 2022

@jhscheer It's because of what @bjorn3 wrote in #2873 (comment)

Thanks.

There's a discussion about why stdlib is reopening the file descriptors here.
@tavianator did you open an issue with your suggestion at the rust-lang repo?

So I guess, unless stdlib implements a better workaround than the one currently used, it's not likely we will ever pass the GNU tests mentioned above.

@tavianator
Copy link

Ah so in the linked PR, rust-lang/rust#75295 (comment) suggests using pipes to accomplish something similar to my suggestion in #2873 (comment). But RFC 1014 explicitly conflicts with that, as pointed out in rust-lang/rust#75295 (comment). So we can't do that unless we're okay with reverting that RFC I guess.

I didn't open an issue, but there's already one: rust-lang/rust#88825

jhscheer added a commit to jhscheer/coreutils that referenced this issue May 16, 2022
This is WIP or even WONT-FIX because there's a workaround in Rust's
stdlib which prevents us from detecting a closed FD.

see also the discussion at:
uutils#2873
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 8, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 9, 2022
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 9, 2022
Optimize tail plateform module using the libc::stdin fd constant.

Commenting out `is_bad_symlink` as uutils#2873 will not be fixed for the time being.
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 9, 2022
Optimize tail plateform module using the libc::stdin fd constant.

Commenting out `is_bad_symlink` as uutils#2873 will not be fixed for the time being.
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 9, 2022
Optimize tail plateform module using the libc::stdin fd constant.

Commenting out `is_bad_symlink` as uutils#2873 will not be fixed for the time being.
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 14, 2022
Optimize tail plateform module using the libc::stdin fd constant.

Commenting out `is_bad_symlink` as uutils#2873 will not be fixed for the time being.
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 18, 2022
Optimize tail plateform module using the libc::stdin fd constant.

Commenting out `is_bad_symlink` as uutils#2873 will not be fixed for the time being.
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 18, 2022
Optimize tail plateform module using the libc::stdin fd constant.

Commenting out `is_bad_symlink` as uutils#2873 will not be fixed for the time being.
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 18, 2022
Optimize tail plateform module using the libc::stdin fd constant.

Commenting out `is_bad_symlink` as uutils#2873 will not be fixed for the time being.
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 19, 2022
Optimize tail plateform module using the libc::stdin fd constant.

Commenting out `is_bad_symlink` as uutils#2873 will not be fixed for the time being.
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 20, 2022
Optimize tail plateform module using the libc::stdin fd constant.

Commenting out `is_bad_symlink` as uutils#2873 will not be fixed for the time being.
anastygnome added a commit to anastygnome/coreutils that referenced this issue Jul 20, 2022
Optimize tail plateform module using the libc::stdin fd constant.

Commenting out `is_bad_symlink` as uutils#2873 will not be fixed for the time being.
sylvestre pushed a commit to anastygnome/coreutils that referenced this issue Aug 14, 2022
Optimize tail plateform module using the libc::stdin fd constant.

Commenting out `is_bad_symlink` as uutils#2873 will not be fixed for the time being.
sylvestre added a commit that referenced this issue Aug 14, 2022
Rework tail plateform module to tackle #2873
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants