]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
shred: don't block when opening FIFOs with no readers
authorCollin Funk <collin.funk1@gmail.com>
Sat, 9 May 2026 22:43:39 +0000 (15:43 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Sun, 10 May 2026 20:45:44 +0000 (13:45 -0700)
* NEWS: Mention the bug fix.
* src/shred.c (wipefile): Open the file with O_NONBLOCK.
* tests/shred/fifo.sh: New file.
* tests/local.mk (all_tests): Add the new test.

NEWS
src/shred.c
tests/local.mk
tests/shred/fifo.sh [new file with mode: 0755]

diff --git a/NEWS b/NEWS
index a105c48843801158dd8127d2819e43df31908a2b..f8c43d5ed6696ac56eb5bd94cac609aa8abe5c60 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   standard output is fully buffered, e.g., when redirected to a file.
   [bug introduced in coreutils-9.10]
 
+  'shred' no longer blocks when opening a FIFO that has no readers.
+  [This bug was present in "the beginning".]
+
   'unexpand -t' no longer overflows a heap buffer, for tab values > SIZE_MAX/16.
   [bug introduced in coreutils-9.11]
 
index b5bf6f3f0fe06abbb21bfef96938f2bbc45b3784..316268691f83fef3ae4bd2923edb6c9f5d83dd7e 100644 (file)
@@ -1139,21 +1139,33 @@ static bool
 wipefile (char *name, char const *qname,
           struct randint_source *s, struct Options const *flags)
 {
-  bool ok;
-  int fd;
-
-  fd = open (name, O_WRONLY | O_NOCTTY | O_BINARY);
+  int fd = open (name, O_WRONLY | O_NOCTTY | O_NONBLOCK | O_BINARY);
   if (fd < 0
       && (errno == EACCES && flags->force)
       && chmod (name, S_IWUSR) == 0)
-    fd = open (name, O_WRONLY | O_NOCTTY | O_BINARY);
+    fd = open (name, O_WRONLY | O_NOCTTY | O_NONBLOCK | O_BINARY);
   if (fd < 0)
     {
-      error (0, errno, _("%s: failed to open for writing"), qname);
+      struct stat st;
+
+      /* Try to give a more meaningful error message if we attempt to
+         open a FIFO with no readers.  */
+      if (errno == ENXIO && 0 <= stat (name, &st) && S_ISFIFO (st.st_mode))
+        error (0, 0, _("%s: invalid file type"), qname);
+      else
+        error (0, errno, _("%s: failed to open for writing"), qname);
       return false;
     }
 
-  ok = do_wipefd (fd, qname, s, flags);
+  /* We used O_NONBLOCK to prevent blocking when opening a FIFO.
+     Reset that here.  */
+  int fd_flags = fcntl (fd, F_GETFL);
+  bool ok = 0 <= fd_flags && 0 <= fcntl (fd, F_SETFL, fd_flags & ~O_NONBLOCK);
+  if (ok)
+    ok = do_wipefd (fd, qname, s, flags);
+  else
+    error (0, errno, _("couldn't reset non-blocking mode %s"), qname);
+
   if (close (fd) != 0)
     {
       error (0, errno, _("%s: failed to close"), qname);
index 727cf8b58b84d1062cb056d186399e53f9ca949d..fd96d7f3ad42f4d1823703196ef9a1fbed747105 100644 (file)
@@ -418,6 +418,7 @@ all_tests =                                 \
   tests/cksum/sha384sum.pl                     \
   tests/cksum/sha512sum.pl                     \
   tests/cksum/cksum-sha3.sh                    \
+  tests/shred/fifo.sh                          \
   tests/shred/shred-exact.sh                   \
   tests/shred/shred-passes.sh                  \
   tests/shred/shred-remove.sh                  \
diff --git a/tests/shred/fifo.sh b/tests/shred/fifo.sh
new file mode 100755 (executable)
index 0000000..f182d3c
--- /dev/null
@@ -0,0 +1,43 @@
+#!/bin/sh
+# Test that 'shred' behaves correctly on FIFOs.
+
+# Copyright (C) 2026 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ shred
+
+mkfifo_or_skip_ fifo
+
+# Test 'shred' on a FIFO with no readers.
+cat <<\EOF >exp || framework_failure_
+shred: fifo: invalid file type
+EOF
+returns_ 1 timeout 10 shred fifo >out 2>err || fail=1
+compare /dev/null out || fail=1
+compare exp err || fail=1
+
+# Terminate any background processes.
+cleanup_() { kill $pid 2>/dev/null && wait $pid; }
+
+# Test 'shred' on a FIFO with a reader.  After the file descriptor is
+# opened, 'cat' will be unblocked.
+timeout 10 cat pipe > /dev/null & pid=$!
+returns_ 1 timeout 10 shred fifo >out 2>err || fail=1
+compare /dev/null out || fail=1
+compare exp err || fail=1
+kill -0 $pid && fail=1
+
+Exit $fail