This mitigates these issues identified by Anthropic's OSS Scanner:
ANT-2026-GV4Z4T3T, NT-2026-D9DT1IM8, ANT-2026-FW4W9RAN
* NEWS: Mention the new feature.
* doc/coreutils.texi (timeout invocation): Document -q,
and mention generally that --quiet provides more robust operation.
* src/timeout.c: Accept -q and use it to disable diagnostics
after arg parsing (including any triggered by close_stdout).
Also limit core dump and timer diagnostics to --verbose mode.
* tests/timeout/timeout-parameters.sh: Ensure -q operates as expected.
* tests/timeout/timeout.sh: Adjust stall tests to use -q.
---
NEWS | 3 ++
doc/coreutils.texi | 13 ++++++-
src/timeout.c | 59 +++++++++++++++++++++--------
tests/timeout/timeout-parameters.sh | 15 ++++++++
tests/timeout/timeout.sh | 34 +++++++++++------
5 files changed, 95 insertions(+), 29 deletions(-)
diff --git a/NEWS b/NEWS
index 8a5c71124..5c771d33e 100644
--- a/NEWS
+++ b/NEWS
@@ -444,6 +444,9 @@ GNU coreutils NEWS -*-
outline -*-
'env' and 'printenv' now support the --quoting-style option
to control the format of printed environment variables.
+ 'timeout' now accepts -q,--quiet to suppress its diagnostics. This supports
+ more robust operation in edge cases where standard error might stall.
+
'tr' accepts the undocumented -u option to operate in unbuffered mode,
for compatibility with FreeBSD and macOS tr(1).
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 60133214f..bbc4bae44 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -18513,6 +18513,13 @@ Return the exit status of the managed @var{command} on
timeout, rather than
a specific exit status indicating a timeout. This is useful if the
managed @var{command} supports running for an indeterminate amount of time.
+@optItem{timeout,-q,}
+@optItemx{timeout,--quiet,}
+Suppress diagnostics from @command{timeout}. This allows for more resilient
+operation in edge cases where the output to standard error may stall.
+If both @option{--quiet} and @option{--verbose} are given,
+the last one takes effect.
+
@optItem{timeout,-s,@w{ }@var{signal}}
@optItemx{timeout,--signal,=@var{signal}}
Send this @var{signal} to @var{command} on timeout, rather than the
@@ -18521,9 +18528,13 @@ or a number. @xref{Signal specifications}.
@optItem{timeout,-v,}
@optItemx{timeout,--verbose,}
-Diagnose to standard error, any signal sent upon timeout.
+Report signals sent upon timeout, and less important warnings.
@end table
+If writing a diagnostic to standard error blocks,
+@command{timeout} can be delayed in sending a signal or exiting.
+Use @option{--quiet} to avoid such delays after argument checking.
+
@cindex time units
@var{duration} is a floating point number in either the current or the
C locale (@pxref{Floating point}) followed by an optional unit:
diff --git a/src/timeout.c b/src/timeout.c
index c297259f0..23e0b8d09 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -94,7 +94,7 @@ static pid_t monitored_pid;
static double kill_after;
static bool foreground; /* whether to use another program group. */
static bool preserve_status; /* whether to use a timeout status or not. */
-static bool verbose; /* whether to diagnose timeouts or not. */
+static enum { V_quiet, V_normal, V_verbose } verbosity = V_normal;
static char const *command;
static struct option const long_options[] =
@@ -102,6 +102,7 @@ static struct option const long_options[] =
{"foreground", no_argument, NULL, 'f'},
{"kill-after", required_argument, NULL, 'k'},
{"preserve-status", no_argument, NULL, 'p'},
+ {"quiet", no_argument, NULL, 'q'},
{"signal", required_argument, NULL, 's'},
{"verbose", no_argument, NULL, 'v'},
{GETOPT_HELP_OPTION_DECL},
@@ -256,7 +257,7 @@ static void
cleanup (int sig)
{
/* Don't diagnose signals that the diagnostic itself could generate. */
- bool diagnose = verbose && sig != SIGPIPE;
+ bool diagnose = verbosity == V_verbose && sig != SIGPIPE;
#ifdef SIGXFSZ
diagnose &= sig != SIGXFSZ;
#endif
@@ -350,6 +351,11 @@ Start COMMAND, and kill it if still running after
DURATION.\n\
-p, --preserve-status\n\
exit with the same status as COMMAND,\n\
even when the command times out\n\
+"));
+ oputs (_("\
+ -q, --quiet\n\
+ suppress diagnostics, which supports more robust operation\n\
+ in the edge case where output to standard error may stall\n\
"));
oputs (_("\
-s, --signal=SIGNAL\n\
@@ -359,7 +365,7 @@ Start COMMAND, and kill it if still running after
DURATION.\n\
"));
oputs (_("\
-v, --verbose\n\
- diagnose to standard error any signal sent upon timeout\n\
+ report signals sent upon timeout, and less important warnings\n\
"));
oputs (HELP_OPTION_DESCRIPTION);
@@ -455,7 +461,8 @@ unblock_signal (int sig)
sigset_t unblock_set;
sigemptyset (&unblock_set);
sigaddset (&unblock_set, sig);
- if (sigprocmask (SIG_UNBLOCK, &unblock_set, NULL) != 0)
+ if (sigprocmask (SIG_UNBLOCK, &unblock_set, NULL) != 0
+ && verbosity != V_quiet)
error (0, errno, _("warning: sigprocmask"));
}
@@ -536,7 +543,8 @@ block_cleanup_and_chld (int sigterm, sigset_t *old_set)
sigaddset (&block_set, SIGCHLD);
- if (sigprocmask (SIG_BLOCK, &block_set, old_set) != 0)
+ if (sigprocmask (SIG_BLOCK, &block_set, old_set) != 0
+ && verbosity != V_quiet)
error (0, errno, _("warning: sigprocmask"));
}
@@ -559,10 +567,20 @@ disable_core_dumps (void)
return false;
#endif
- error (0, errno, _("warning: disabling core dumps failed"));
+ if (verbosity == V_verbose)
+ error (0, errno, _("warning: disabling core dumps failed"));
return false;
}
+static void
+close_output (void)
+{
+ /* Once the arguments are valid, quiet mode writes to neither stream.
+ Keep checking output from --help, --version, and usage errors. */
+ if (!command || verbosity != V_quiet)
+ close_stdout ();
+}
+
int
main (int argc, char **argv)
{
@@ -576,9 +594,9 @@ main (int argc, char **argv)
textdomain (PACKAGE);
initialize_exit_failure (EXIT_CANCELED);
- atexit (close_stdout);
+ atexit (close_output);
- while ((c = getopt_long (argc, argv, "+fk:ps:v", long_options, NULL))
+ while ((c = getopt_long (argc, argv, "+fk:pqs:v", long_options, NULL))
!= -1)
{
switch (c)
@@ -595,6 +613,10 @@ main (int argc, char **argv)
preserve_status = true;
break;
+ case 'q':
+ verbosity = V_quiet;
+ break;
+
case 's':
term_signal = operand2sig (optarg);
if (term_signal == -1)
@@ -602,7 +624,7 @@ main (int argc, char **argv)
break;
case 'v':
- verbose = true;
+ verbosity = V_verbose;
break;
case_GETOPT_HELP_CHAR;
@@ -659,7 +681,8 @@ main (int argc, char **argv)
monitored_pid = fork ();
if (monitored_pid == -1)
{
- error (0, errno, _("fork system call failed"));
+ if (verbosity != V_quiet)
+ error (0, errno, _("fork system call failed"));
return EXIT_CANCELED;
}
else if (monitored_pid == 0) /* child */
@@ -677,7 +700,8 @@ main (int argc, char **argv)
/* Restore signal mask for child. */
if (sigprocmask (SIG_SETMASK, &orig_set, NULL) != 0)
{
- error (0, errno, _("child failed to reset signal mask"));
+ if (verbosity != V_quiet)
+ error (0, errno, _("child failed to reset signal mask"));
return EXIT_CANCELED;
}
@@ -689,7 +713,8 @@ main (int argc, char **argv)
/* exit like sh, env, nohup, ... */
int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
- error (0, errno, _("failed to run command %s"), quote (command));
+ if (verbosity != V_quiet)
+ error (0, errno, _("failed to run command %s"), quote (command));
return exit_status;
}
else
@@ -697,7 +722,7 @@ main (int argc, char **argv)
pid_t wait_result;
int status;
- settimeout (timeout, true);
+ settimeout (timeout, verbosity == V_verbose);
/* Note signals remain blocked in parent here, to ensure
we don't cleanup() after waitpid() reaps the child,
@@ -717,7 +742,8 @@ main (int argc, char **argv)
if (wait_result < 0)
{
/* shouldn't happen. */
- error (0, errno, _("error waiting for command"));
+ if (verbosity != V_quiet)
+ error (0, errno, _("error waiting for command"));
status = EXIT_CANCELED;
}
else
@@ -727,7 +753,7 @@ main (int argc, char **argv)
else if (WIFSIGNALED (status))
{
int sig = WTERMSIG (status);
- if (WCOREDUMP (status))
+ if (verbosity == V_verbose && WCOREDUMP (status))
error (0, 0, _("the monitored command dumped core"));
if (!timed_out && disable_core_dumps ())
{
@@ -746,7 +772,8 @@ main (int argc, char **argv)
else
{
/* shouldn't happen. */
- error (0, 0, _("unknown status from command (%d)"), status);
+ if (verbosity != V_quiet)
+ error (0, 0, _("unknown status from command (%d)"), status);
status = EXIT_FAILURE;
}
}
diff --git a/tests/timeout/timeout-parameters.sh
b/tests/timeout/timeout-parameters.sh
index 9303fecf0..1f898a1d8 100755
--- a/tests/timeout/timeout-parameters.sh
+++ b/tests/timeout/timeout-parameters.sh
@@ -52,4 +52,19 @@ returns_ 126 env . && { returns_ 126 timeout 10 . || fail=1;
}
# no such command
returns_ 127 timeout 10 no_such || fail=1
+# Quiet mode still diagnoses invalid arguments.
+for args in '--bad 1 true' '-s bad 1 true' '-k bad 1 true' 'bad true' '1'; do
+ timeout -q $args 2>err
+ test $? = 125 && test -s err || fail=1
+done
+
+# Suppress execution errors while preserving the reason for failure.
+timeout -q 10 ./no_such 2>err
+test $? = 127 || fail=1
+compare /dev/null err || fail=1
+touch non-executable || framework_failure_
+timeout --quiet 10 ./non-executable 2>err
+test $? = 126 || fail=1
+compare /dev/null err || fail=1
+
Exit $fail
diff --git a/tests/timeout/timeout.sh b/tests/timeout/timeout.sh
index 45ab9ab60..4deb0bf10 100755
--- a/tests/timeout/timeout.sh
+++ b/tests/timeout/timeout.sh
@@ -80,12 +80,19 @@ cat > exp <<\EOF
timeout: sending signal 0 to command 'sleep'
timeout: sending signal KILL to command 'sleep'
EOF
-for opt in -v --verbose; do
- timeout --foreground $opt -s0 -k .1 .1 sleep 10 2> errt
+for opt in -v '--foreground --quiet --verbose'; do
+ timeout $opt -s0 -k .1 .1 sleep 10 2> errt
sed '/^Killed/d' < errt > err || framework_failure_
compare exp err || fail=1
done
+# A final --quiet suppresses timeout's diagnostics and preserves the
+# command's stderr and exit status.
+echo child > exp || framework_failure_
+timeout -v --quiet -p -s0 .1 $SHELL -c 'echo child >&2; sleep .3; exit 42'
2>err
+test $? = 42 || fail=1
+compare exp err || fail=1
+
# Ensure we propagate all terminating signals.
# Specifically here we're testing that SIGPIPE is handled.
# I.e., that we're not killed by the SIGPIPE (and leave the sleep running).
@@ -109,10 +116,10 @@ if (trap - XFSZ && ulimit -f 0) 2>/dev/null; then
' || fail=1
fi
-# Signal the command and its group before writing to a blocked stderr.
+# Quiet mode must signal the command and its group even with blocked stderr.
file_seen() { sleep "$1"; test -s "$file"; }
file=term.seen
-timeout -v .1 $SHELL -c '
+timeout -q .1 $SHELL -c '
exec 2>/dev/null # ensure no "Terminated" message blocks the trap
trap "echo 1 >term.seen" TERM
yes # Fill the pipe
@@ -121,23 +128,26 @@ timeout -v .1 $SHELL -c '
{ retry_delay_ file_seen .1 6 || touch term.missed; }
test -e term.missed && fail=1
-# A group-wide SIGKILL must not wait for the verbose diagnostic either.
+# A group-wide SIGKILL must also work with blocked stderr.
# Redirect in a subshell so a waiting shell's "Killed" diagnostic
# cannot block on the filled pipe before recording the exit status.
file=kill.status
-{ (exec timeout -v -s KILL .1 yes 2>&1)
+{ (exec timeout -q -s KILL .1 yes 2>&1)
echo $? >kill.status; } |
{ retry_delay_ file_seen .1 6 || touch kill.missed; }
test -e kill.missed && fail=1
echo 137 > kill.exp || framework_failure_
compare kill.exp kill.status || fail=1
-# --kill-after must interrupt a blocked diagnostic for the initial signal.
+# Quiet mode must escalate with blocked stderr in either process-group mode.
file=kill-after.status
-{ (exec timeout -v -k .1 .1 $SHELL -c 'trap "" TERM; exec yes >&2' 2>&1)
- echo $? >kill-after.status; } |
- { retry_delay_ file_seen .1 6 || touch kill-after.missed; }
-test -e kill-after.missed && fail=1
-compare kill.exp kill-after.status || fail=1
+for opt in '' --foreground; do
+ rm -f "$file"
+ { (exec timeout -q $opt -k .1 .1 $SHELL -c 'trap "" TERM; exec yes >&2' 2>&1)
+ echo $? >"$file"; } |
+ { retry_delay_ file_seen .1 6 || touch kill-after.missed; }
+ test -e kill-after.missed && fail=1
+ compare kill.exp "$file" || fail=1
+done
Exit $fail
--
2.56.0