timeout(1): Add -v/--verbose option to show diagnosis info

The -v/--verbose option enables this utility to show diagnosis
info to stderr about any signal sent on timeout.

This implementation refers to GNU coreutils's timeout(1).

Reviewed by:	bapt, Alexander Ziaee (manpages)
Approved by:	bapt (src)
Obtained from:	DragonFlyBSD
Differential Revision:	https://reviews.freebsd.org/D48225

(cherry picked from commit d633a7d121)
This commit is contained in:
Gordon Bergling 2025-01-04 09:52:56 +01:00
parent dc3f24ea8a
commit f31dbc1c2d
2 changed files with 27 additions and 7 deletions

View file

@ -24,7 +24,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd December 28, 2024
.Dd January 4, 2025
.Dt TIMEOUT 1
.Os
.Sh NAME
@ -34,7 +34,7 @@
.Nm
.Op Fl k Ar time | Fl -kill-after Ar time
.Op Fl s Ar sig | Fl -signal Ar sig
.Op Fl -kill-after Ar time | Fl k Ar time
.Op Fl v | Fl -verbose
.Op Fl -foreground
.Op Fl -preserve-status
.Ar duration
@ -76,6 +76,8 @@ Specify the signal to send on timeout.
By default,
.Dv SIGTERM
is sent.
.It Fl v , Fl -verbose
Show information to stderr about any signal sent on timeout.
.It Fl -foreground
Do not propagate timeout to the children of
.Ar command .

View file

@ -49,14 +49,17 @@ static sig_atomic_t sig_chld = 0;
static sig_atomic_t sig_term = 0;
static sig_atomic_t sig_alrm = 0;
static sig_atomic_t sig_ign = 0;
static const char *command = NULL;
static bool verbose = false;
static void
usage(void)
{
fprintf(stderr, "Usage: %s [-k time | --kill-after time]"
" [-s sig | --signal sig] [--foreground] [--preserve-status]"
" <duration> <command> <arg ...>\n", getprogname());
" [-s sig | --signal sig] [-v | --verbose] [--foreground]"
" [--preserve-status] <duration> <command> <arg ...>\n",
getprogname());
exit(EXIT_FAILURE);
}
@ -146,6 +149,16 @@ sig_handler(int signo)
}
}
static void
send_sig(pid_t pid, int signo)
{
if (verbose) {
warnx("sending signal %s(%d) to command '%s'",
sys_signame[signo], signo, command);
}
kill(pid, signo);
}
static void
set_interval(double iv)
{
@ -196,10 +209,11 @@ main(int argc, char **argv)
{ "kill-after", required_argument, NULL, 'k'},
{ "signal", required_argument, NULL, 's'},
{ "help", no_argument, NULL, 'h'},
{ "verbose", no_argument, NULL, 'v'},
{ NULL, 0, NULL, 0 }
};
while ((ch = getopt_long(argc, argv, "+k:s:h", longopts, NULL)) != -1) {
while ((ch = getopt_long(argc, argv, "+k:s:vh", longopts, NULL)) != -1) {
switch (ch) {
case 'k':
do_second_kill = true;
@ -208,6 +222,9 @@ main(int argc, char **argv)
case 's':
killsig = parse_signal(optarg);
break;
case 'v':
verbose = true;
break;
case 0:
break;
case 'h':
@ -226,6 +243,7 @@ main(int argc, char **argv)
first_kill = parse_duration(argv[0]);
argc--;
argv++;
command = argv[0];
if (!foreground) {
/* Acquire a reaper */
@ -316,7 +334,7 @@ main(int argc, char **argv)
procctl(P_PID, getpid(), PROC_REAP_KILL,
&killemall);
} else
kill(pid, killsig);
send_sig(pid, killsig);
if (do_second_kill) {
set_interval(second_kill);
@ -333,7 +351,7 @@ main(int argc, char **argv)
procctl(P_PID, getpid(), PROC_REAP_KILL,
&killemall);
} else
kill(pid, sig_term);
send_sig(pid, sig_term);
if (do_second_kill) {
set_interval(second_kill);