mirror of
https://git.savannah.gnu.org/git/coreutils.git
synced 2026-11-17 18:02:09 +03: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:
1 parent
ab18e90e8b
commit
f77f365ef5
4 files changed
+66
-7
No files matched your search
@@ -12,6 +12,9 @@ GNU coreutils NEWS -*- outline -*-
|
|||||||
standard output is fully buffered, e.g., when redirected to a file.
|
standard output is fully buffered, e.g., when redirected to a file.
|
||||||
[bug introduced in coreutils-9.10]
|
[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.
|
'unexpand -t' no longer overflows a heap buffer, for tab values > SIZE_MAX/16.
|
||||||
[bug introduced in coreutils-9.11]
|
[bug introduced in coreutils-9.11]
|
||||||
|
|
||||||
|
|||||||
+19
-7
@@ -1139,21 +1139,33 @@ static bool
|
|||||||
wipefile (char *name, char const *qname,
|
wipefile (char *name, char const *qname,
|
||||||
struct randint_source *s, struct Options const *flags)
|
struct randint_source *s, struct Options const *flags)
|
||||||
{
|
{
|
||||||
bool ok;
|
int fd = open (name, O_WRONLY | O_NOCTTY | O_NONBLOCK | O_BINARY);
|
||||||
int fd;
|
|
||||||
|
|
||||||
fd = open (name, O_WRONLY | O_NOCTTY | O_BINARY);
|
|
||||||
if (fd < 0
|
if (fd < 0
|
||||||
&& (errno == EACCES && flags->force)
|
&& (errno == EACCES && flags->force)
|
||||||
&& chmod (name, S_IWUSR) == 0)
|
&& 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)
|
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;
|
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)
|
if (close (fd) != 0)
|
||||||
{
|
{
|
||||||
error (0, errno, _("%s: failed to close"), qname);
|
error (0, errno, _("%s: failed to close"), qname);
|
||||||
|
|||||||
@@ -418,6 +418,7 @@ all_tests = \
|
|||||||
tests/cksum/sha384sum.pl \
|
tests/cksum/sha384sum.pl \
|
||||||
tests/cksum/sha512sum.pl \
|
tests/cksum/sha512sum.pl \
|
||||||
tests/cksum/cksum-sha3.sh \
|
tests/cksum/cksum-sha3.sh \
|
||||||
|
tests/shred/fifo.sh \
|
||||||
tests/shred/shred-exact.sh \
|
tests/shred/shred-exact.sh \
|
||||||
tests/shred/shred-passes.sh \
|
tests/shred/shred-passes.sh \
|
||||||
tests/shred/shred-remove.sh \
|
tests/shred/shred-remove.sh \
|
||||||
|
|||||||
Executable
+43
@@ -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
|
||||||
Reference in new issue
Block a user