Thanks, I didn't know about that lseek glitch with the proc file system.
I installed the attached patch to the grep master.
The use case seems unusual. Did a user discover this in practice?
From f614a1e2384ca86f27921c19fcdc5538899f592a Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Mon, 12 Dec 2016 08:31:32 -0800
Subject: [PATCH] grep: work around proc lseek glitch
Problem reported by Andreas Schwab (Bug#25180).
* NEWS: Document this.
* src/grep.c (finalize_input): Ignore EINVAL lseek failures.
* tests/Makefile.am (TESTS): Add proc.
* tests/proc: New file.
---
NEWS | 6 ++++++
src/grep.c | 9 ++++++---
tests/Makefile.am | 1 +
tests/proc | 18 ++++++++++++++++++
4 files changed, 31 insertions(+), 3 deletions(-)
create mode 100755 tests/proc
diff --git a/NEWS b/NEWS
index f33001d..aa0483b 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,12 @@ GNU grep NEWS -*- outline -*-
* Noteworthy changes in release ?.? (????-??-??) [?]
+** Bug fixes
+
+ grep no longer fails when standard input is a file in the Linux
+ /proc file system and standard output is /dev/null.
+ [bug introduced in grep-2.27]
+
* Noteworthy changes in release 2.27 (2016-12-06) [stable]
diff --git a/src/grep.c b/src/grep.c
index 1e910cb..30e0f54 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1760,9 +1760,12 @@ finalize_input (int fd, struct stat const *st, bool ineof)
{
if (fd == STDIN_FILENO
&& (outleft
- ? (!ineof && (seek_failed
- ? ! drain_input (fd, st)
- : lseek (fd, 0, SEEK_END) < 0))
+ ? (!ineof
+ && (seek_failed
+ || (lseek (fd, 0, SEEK_END) < 0
+ /* Linux proc file system has EINVAL (Bug#25180). */
+ && errno != EINVAL))
+ && ! drain_input (fd, st))
: (bufoffset != after_last_match && !seek_failed
&& lseek (fd, after_last_match, SEEK_SET) < 0)))
suppressible_error (errno);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3ded7a7..52e2b9e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -141,6 +141,7 @@ TESTS = \
pcre-z \
posix-bracket \
prefix-of-multibyte \
+ proc \
r-dot \
repetition-overflow \
reversed-range-endpoints \
diff --git a/tests/proc b/tests/proc
new file mode 100755
index 0000000..aeb4a50
--- /dev/null
+++ b/tests/proc
@@ -0,0 +1,18 @@
+#! /bin/sh
+# Test the /proc file system if available.
+
+# Copyright 2016 Free Software Foundation, Inc.
+#
+# Copying and distribution of this file, with or without modification,
+# are permitted in any medium without royalty provided the copyright
+# notice and this notice are preserved.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
+
+fail=0
+
+test -r /proc/self/status || skip_ 'No /proc/self/status on this platform.'
+
+grep '^' </proc/self/status >/dev/null || fail=1
+
+Exit $fail
--
2.7.4