From 8c81808de09b034c5dc2221aacc85d25c3218676 Mon Sep 17 00:00:00 2001 From: Craig Small Date: Wed, 1 Mar 2023 17:35:14 +1100 Subject: [PATCH] pgrep: Use only --signal option for signal When pgrep was used to match on signal, it makes sense to use the same signal parsing code as pkill. Unfortunately the "find the signal" part is a little too enthusaistic about what a signal is, meaning pgrep -u -42 fails because the signal becomes "42" and then there is no UID. This is a bit sad for pkill but has been that way for a long time. For pgrep this is new so now only the long form pgrep --signal will work. In addition, when using --signal if pgrep/pkill couldn't work out what the signal was it just silently ignored it. It now complains and aborts. References: https://bugs.debian.org/1031765 commit 866abacf8805a74fb7c59cae1f64963e0a540b14 --- NEWS | 1 + man/pgrep.1 | 2 +- src/pgrep.c | 21 ++++++++++++++------- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index bc293a3f..85bf845e 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,6 @@ procps-ng-NEXT --------------- + * pgrep: Use only --signal option for signal Debian #1031765 * tests: dont compare floats with == issue #271 procps-ng-4.0.3 diff --git a/man/pgrep.1 b/man/pgrep.1 index a68633c5..41d9ac04 100644 --- a/man/pgrep.1 +++ b/man/pgrep.1 @@ -60,7 +60,7 @@ the symbolic signal name can be used. In .B pgrep or .B pidwait -mode this has no effect unless used in conjunction with +mode only the long option can be used and has no effect unless used in conjunction with \fB\-\-require\-handler\fR to filter to processes with a userspace signal handler present for a particular signal. diff --git a/src/pgrep.c b/src/pgrep.c index 94607a3f..442dbfcc 100644 --- a/src/pgrep.c +++ b/src/pgrep.c @@ -163,6 +163,7 @@ static int __attribute__ ((__noreturn__)) usage(int opt) fputs(_(" -w, --lightweight list all TID\n"), fp); break; case PKILL: + fputs(_(" - signal to send (either number or name)\n"), fp); fputs(_(" -H, --require-handler match only if signal handler is present\n"), fp); fputs(_(" -q, --queue integer value to be sent with the signal\n"), fp); fputs(_(" -e, --echo display what is killed\n"), fp); @@ -173,7 +174,6 @@ static int __attribute__ ((__noreturn__)) usage(int opt) break; #endif } - fputs(_(" -, --signal signal to send (either number or name)\n"), fp); fputs(_(" -c, --count count of matching processes\n"), fp); fputs(_(" -f, --full use full process name to match\n"), fp); fputs(_(" -g, --pgroup match listed process group IDs\n"), fp); @@ -184,6 +184,7 @@ static int __attribute__ ((__noreturn__)) usage(int opt) fputs(_(" -O, --older select where older than seconds\n"), fp); fputs(_(" -P, --parent match only child processes of the given parent\n"), fp); fputs(_(" -s, --session match session IDs\n"), fp); + fputs(_(" --signal signal to send (either number or name)\n"), fp); fputs(_(" -t, --terminal match by controlling terminal\n"), fp); fputs(_(" -u, --euid match by effective IDs\n"), fp); fputs(_(" -U, --uid match by real IDs\n"), fp); @@ -823,7 +824,6 @@ static int pidfd_open (pid_t pid, unsigned int flags) static void parse_opts (int argc, char **argv) { char opts[64] = ""; - int sig; int opt; int criteria_count = 0; @@ -869,9 +869,6 @@ static void parse_opts (int argc, char **argv) {NULL, 0, NULL, 0} }; - sig = signal_option(&argc, argv); - if (-1 < sig) - opt_signal = sig; #ifdef ENABLE_PIDWAIT if (strcmp (program_invocation_short_name, "pidwait") == 0 || @@ -882,7 +879,11 @@ static void parse_opts (int argc, char **argv) #endif if (strcmp (program_invocation_short_name, "pkill") == 0 || strcmp (program_invocation_short_name, "lt-pkill") == 0) { + int sig; prog_mode = PKILL; + sig = signal_option(&argc, argv); + if (-1 < sig) + opt_signal = sig; strcat (opts, "eq:"); } else { strcat (opts, "lad:vw"); @@ -895,8 +896,14 @@ static void parse_opts (int argc, char **argv) switch (opt) { case SIGNAL_OPTION: opt_signal = signal_name_to_number (optarg); - if (opt_signal == -1 && isdigit (optarg[0])) - opt_signal = atoi (optarg); + if (opt_signal == -1) { + if (isdigit (optarg[0])) + opt_signal = atoi (optarg); + else { + fprintf(stderr, _("Unknown signal \"%s\"."), optarg); + usage('?'); + } + } break; case 'e': opt_echo = 1;