1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-06-01 23:36:29 +02:00

shred: don't block when opening FIFOs with no readers

* 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.
This commit is contained in:
Collin Funk
2026-05-09 15:43:39 -07:00
parent ab18e90e8b
commit f77f365ef5
4 changed files with 66 additions and 7 deletions
+3
View File
@@ -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]
+19 -7
View 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);
+1
View 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 \
+43
View File
@@ -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