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

From: Gordon Bergling <gbe_at_FreeBSD.org>
Date: Sat, 04 Jan 2025 08:53:31 UTC
The branch main has been updated by gbe:

URL: https://cgit.FreeBSD.org/src/commit/?id=d633a7d12105a54551622882f4eee80a13a1445a

commit d633a7d12105a54551622882f4eee80a13a1445a
Author:     Gordon Bergling <gbe@FreeBSD.org>
AuthorDate: 2025-01-04 08:52:56 +0000
Commit:     Gordon Bergling <gbe@FreeBSD.org>
CommitDate: 2025-01-04 08:52:56 +0000

    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
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D48225
---
 bin/timeout/timeout.1 |  6 ++++--
 bin/timeout/timeout.c | 28 +++++++++++++++++++++++-----
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/bin/timeout/timeout.1 b/bin/timeout/timeout.1
index b8ec3030b271..1a5fd95a6256 100644
--- a/bin/timeout/timeout.1
+++ b/bin/timeout/timeout.1
@@ -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 .
diff --git a/bin/timeout/timeout.c b/bin/timeout/timeout.c
index 76e51eaa965e..429ca64349e3 100644
--- a/bin/timeout/timeout.c
+++ b/bin/timeout/timeout.c
@@ -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':
@@ -225,6 +242,7 @@ main(int argc, char **argv)
 	first_kill = parse_duration(argv[0]);
 	argc--;
 	argv++;
+	command = argv[0];
 
 	if (!foreground) {
 		/* Acquire a reaper */
@@ -315,7 +333,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);
@@ -332,7 +350,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);