Subject: bug#50129: -f - option doesn't respond to single EOF from TTY.

From: "Paul Jackson" <[email-masked]>
Date: Thu, 19 Aug 2021 21:10:32 -0500

My fading recollection from long, long ago is that this is a difficult to
avoid artifact of using stdio.

If no patterns are fed into grep, just a single EOF, then grep exits immediately.

But some non-empty input (just a single "pat1" is sufficient) is fed to grep,
then it takes two EOF's to get grep to exit.

These two cases can be seen in the output of the following two commands:

# With an input of "pat1", it takes two reads returning 0 bytes (EOF's) to exit.
(sleep 2; echo pat1) | strace -tt -T  grep -f - 2>&1 | tail -15

# With no "pat1" input, it only takes one read of 0 bytes to exit.
sleep 2 | strace -tt -T  grep -f - 2>&1 | tail -15

=== ===

Here's a copy-paste of a terminal session in which I invoke the above two commands.
Notice the read() system calls that return 0 bytes.  That's how the kernel presents an
EOF to a user process on a read.

$ (sleep 2; echo pat1) | strace -tt -T  grep -f - 2>&1 | tail -15
21:07:12.887450 openat(AT_FDCWD, "/usr/share/locale-langpack/en.utf8/LC_MESSAGES/grep.mo", O_RDONLY) = -1 ENOENT (No such file or directory) <0.000013>
21:07:12.887490 openat(AT_FDCWD, "/usr/share/locale-langpack/en/LC_MESSAGES/grep.mo", O_RDONLY) = -1 ENOENT (No such file or directory) <0.000013>
21:07:12.887535 rt_sigaction(SIGSEGV, {sa_handler=0x55e0d5c49570, sa_mask=[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_NODEFER|SA_RESETHAND|SA_SIGINFO, sa_restorer=0x7f9b26cd3210}, NULL, 8) = 0 <0.000011>
21:07:12.887579 fstat(0, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0 <0.000012>
21:07:12.887623 read(0, "pat1\n", 4096) = 5 <1.993486>
21:07:14.881203 read(0, "", 4096)       = 0 <0.000020>
21:07:14.881269 fstat(1, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0 <0.000019>
21:07:14.881420 brk(0x55e0d7954000)     = 0x55e0d7954000 <0.000023>
21:07:14.881477 fstat(0, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0 <0.000012>
21:07:14.881522 lseek(0, 0, SEEK_CUR)   = -1 ESPIPE (Illegal seek) <0.000012>
21:07:14.881570 read(0, "", 98304)      = 0 <0.000012>
21:07:14.881616 close(1)                = 0 <0.000012>
21:07:14.881655 close(2)                = 0 <0.000011>
21:07:14.881706 exit_group(1)           = ?
21:07:14.881936 +++ exited with 1 +++

$ sleep 2 | strace -tt -T  grep -f - 2>&1 | tail -15
21:07:25.407841 openat(AT_FDCWD, "/usr/share/locale/en/LC_MESSAGES/grep.mo", O_RDONLY) = -1 ENOENT (No such file or directory) <0.000015>
21:07:25.407888 openat(AT_FDCWD, "/usr/share/locale-langpack/en_US.UTF-8/LC_MESSAGES/grep.mo", O_RDONLY) = -1 ENOENT (No such file or directory) <0.000014>
21:07:25.407933 openat(AT_FDCWD, "/usr/share/locale-langpack/en_US.utf8/LC_MESSAGES/grep.mo", O_RDONLY) = -1 ENOENT (No such file or directory) <0.000014>
21:07:25.407980 openat(AT_FDCWD, "/usr/share/locale-langpack/en_US/LC_MESSAGES/grep.mo", O_RDONLY) = -1 ENOENT (No such file or directory) <0.000014>
21:07:25.408026 openat(AT_FDCWD, "/usr/share/locale-langpack/en.UTF-8/LC_MESSAGES/grep.mo", O_RDONLY) = -1 ENOENT (No such file or directory) <0.000014>
21:07:25.408071 openat(AT_FDCWD, "/usr/share/locale-langpack/en.utf8/LC_MESSAGES/grep.mo", O_RDONLY) = -1 ENOENT (No such file or directory) <0.000014>
21:07:25.408116 openat(AT_FDCWD, "/usr/share/locale-langpack/en/LC_MESSAGES/grep.mo", O_RDONLY) = -1 ENOENT (No such file or directory) <0.000015>
21:07:25.408165 rt_sigaction(SIGSEGV, {sa_handler=0x55ac5b515570, sa_mask=[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_NODEFER|SA_RESETHAND|SA_SIGINFO, sa_restorer=0x7f84afc54210}, NULL, 8) = 0 <0.000012>
21:07:25.408213 fstat(0, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0 <0.000012>
21:07:25.408260 read(0, "", 4096)       = 0 <1.992492>
21:07:27.400830 fstat(1, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0 <0.000019>
21:07:27.400921 close(1)                = 0 <0.000018>
21:07:27.400979 close(2)                = 0 <0.000019>
21:07:27.401046 exit_group(1)           = ?
21:07:27.401259 +++ exited with 1 +++

-- 
                Paul Jackson
                [email-masked]