| 1 | When writing to a closed pipe, which happens with simple |
| 2 | |
| 3 | seq 99999 | grep -v xxx | head -1 |
| 4 | |
| 5 | the write() function will return an EPIPE error. The kernel also sends |
| 6 | SIGPIPE to the process, which by default kills it. |
| 7 | |
| 8 | As the signal delivery is not immediate, grep may see EPIPE and report |
| 9 | it as an error, which is annoying, especially on Android where this |
| 10 | timing issue is encountered a lot more. |
| 11 | |
| 12 | See http://debbugs.gnu.org/cgi/bugreport.cgi?bug=23267 where it has |
| 13 | been suggested to silently ignore EPIPE. That was in the context of |
| 14 | SIGPIPE being ignored, but this should probably also been done to |
| 15 | avoid timing issues. Feedback has been sent to the above issue and |
| 16 | is awaiting mailing list approval. |
| 17 | |
| 18 | diff -u -r ../grep-2.25/src/grep.c ./src/grep.c |
| 19 | --- ../grep-2.25/src/grep.c 2016-04-02 20:45:51.000000000 -0400 |
| 20 | +++ ./src/grep.c 2016-04-23 06:41:54.419351897 -0400 |
| 21 | @@ -1234,8 +1234,13 @@ |
| 22 | if (line_buffered) |
| 23 | fflush_errno (); |
| 24 | |
| 25 | - if (stdout_errno) |
| 26 | - error (EXIT_TROUBLE, stdout_errno, _("write error")); |
| 27 | + if (stdout_errno) { |
| 28 | + if (stdout_errno == EPIPE) { |
| 29 | + exit (EXIT_SUCCESS); |
| 30 | + } else { |
| 31 | + error (EXIT_TROUBLE, stdout_errno, _("write error")); |
| 32 | + } |
| 33 | + } |
| 34 | |
| 35 | lastout = lim; |
| 36 | } |