Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini pgrep/pkill implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2007 Loic Grenie <loic.grenie@gmail.com> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 9 | //config:config PGREP |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "pgrep (6.5 kb)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: Look for processes by name. |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 14 | //config: |
| 15 | //config:config PKILL |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 16 | //config: bool "pkill (7.5 kb)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 17 | //config: default y |
| 18 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 19 | //config: Send signals to processes by name. |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 20 | |
Denys Vlasenko | 248a67f | 2017-08-07 18:18:09 +0200 | [diff] [blame] | 21 | //applet:IF_PGREP(APPLET_ODDNAME(pgrep, pgrep, BB_DIR_USR_BIN, BB_SUID_DROP, pgrep)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 22 | // APPLET_ODDNAME:name main location suid_type help |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 23 | //applet:IF_PKILL(APPLET_ODDNAME(pkill, pgrep, BB_DIR_USR_BIN, BB_SUID_DROP, pkill)) |
Denys Vlasenko | 248a67f | 2017-08-07 18:18:09 +0200 | [diff] [blame] | 24 | /* can't be noexec: can find _itself_ under wrong name, since after fork only, |
| 25 | * /proc/PID/cmdline and comm are wrong! Can fix comm (prctl(PR_SET_NAME)), |
| 26 | * but cmdline? |
| 27 | */ |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 28 | |
| 29 | //kbuild:lib-$(CONFIG_PGREP) += pgrep.o |
| 30 | //kbuild:lib-$(CONFIG_PKILL) += pgrep.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 31 | |
| 32 | //usage:#define pgrep_trivial_usage |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 33 | //usage: "[-flanovx] [-s SID|-P PPID|PATTERN]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 34 | //usage:#define pgrep_full_usage "\n\n" |
| 35 | //usage: "Display process(es) selected by regex PATTERN\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 36 | //usage: "\n -l Show command name too" |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 37 | //usage: "\n -a Show command line too" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 38 | //usage: "\n -f Match against entire command line" |
| 39 | //usage: "\n -n Show the newest process only" |
| 40 | //usage: "\n -o Show the oldest process only" |
| 41 | //usage: "\n -v Negate the match" |
| 42 | //usage: "\n -x Match whole name (not substring)" |
| 43 | //usage: "\n -s Match session ID (0 for current)" |
| 44 | //usage: "\n -P Match parent process ID" |
| 45 | //usage: |
| 46 | //usage:#define pkill_trivial_usage |
Denys Vlasenko | 6f7b10c | 2021-06-13 03:51:55 +0200 | [diff] [blame] | 47 | //usage: "[-l|-SIGNAL] [-xfvno] [-s SID|-P PPID|PATTERN]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 48 | //usage:#define pkill_full_usage "\n\n" |
Denys Vlasenko | 6f7b10c | 2021-06-13 03:51:55 +0200 | [diff] [blame] | 49 | //usage: "Send signal to processes selected by regex PATTERN\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 50 | //usage: "\n -l List all signals" |
Denys Vlasenko | 6f7b10c | 2021-06-13 03:51:55 +0200 | [diff] [blame] | 51 | //usage: "\n -x Match whole name (not substring)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 52 | //usage: "\n -f Match against entire command line" |
Denys Vlasenko | 6f7b10c | 2021-06-13 03:51:55 +0200 | [diff] [blame] | 53 | //usage: "\n -s SID Match session ID (0 for current)" |
| 54 | //usage: "\n -P PPID Match parent process ID" |
| 55 | //usage: "\n -v Negate the match" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 56 | //usage: "\n -n Signal the newest process only" |
| 57 | //usage: "\n -o Signal the oldest process only" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 58 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 59 | #include "libbb.h" |
| 60 | #include "xregex.h" |
| 61 | |
| 62 | /* Idea taken from kill.c */ |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 63 | #define pgrep (ENABLE_PGREP && (!ENABLE_PKILL || applet_name[1] == 'g')) |
| 64 | #define pkill (ENABLE_PKILL && (!ENABLE_PGREP || applet_name[1] == 'k')) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 65 | |
| 66 | enum { |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 67 | /* "vlafxons:+P:+" */ |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 68 | OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */ |
| 69 | OPTBIT_L, |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 70 | OPTBIT_A, |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 71 | OPTBIT_F, |
| 72 | OPTBIT_X, |
| 73 | OPTBIT_O, |
| 74 | OPTBIT_N, |
| 75 | OPTBIT_S, |
| 76 | OPTBIT_P, |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 79 | #define OPT_INVERT (opt & (1 << OPTBIT_V)) |
| 80 | #define OPT_LIST (opt & (1 << OPTBIT_L)) |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 81 | #define OPT_LISTFULL (opt & (1 << OPTBIT_A)) |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 82 | #define OPT_FULL (opt & (1 << OPTBIT_F)) |
| 83 | #define OPT_ANCHOR (opt & (1 << OPTBIT_X)) |
| 84 | #define OPT_FIRST (opt & (1 << OPTBIT_O)) |
| 85 | #define OPT_LAST (opt & (1 << OPTBIT_N)) |
| 86 | #define OPT_SID (opt & (1 << OPTBIT_S)) |
| 87 | #define OPT_PPID (opt & (1 << OPTBIT_P)) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 88 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 89 | static void act(unsigned pid, char *cmd, int signo) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 90 | { |
| 91 | if (pgrep) { |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 92 | if (option_mask32 & ((1 << OPTBIT_L)|(1 << OPTBIT_A))) /* -l or -a */ |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 93 | printf("%u %s\n", pid, cmd); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 94 | else |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 95 | printf("%u\n", pid); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 96 | } else |
| 97 | kill(pid, signo); |
| 98 | } |
| 99 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 100 | int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 101 | int pgrep_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 102 | { |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 103 | unsigned pid; |
| 104 | int signo; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 105 | unsigned opt; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 106 | int scan_mask; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 107 | int matched_pid; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 108 | int sid2match, ppid2match; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 109 | char *cmd_last; |
| 110 | procps_status_t *proc; |
| 111 | /* These are initialized to 0 */ |
| 112 | struct { |
| 113 | regex_t re_buffer; |
| 114 | regmatch_t re_match[1]; |
| 115 | } Z; |
| 116 | #define re_buffer (Z.re_buffer) |
| 117 | #define re_match (Z.re_match ) |
| 118 | |
| 119 | memset(&Z, 0, sizeof(Z)); |
| 120 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 121 | /* Parse -SIGNAL for pkill. Must be first option, if present */ |
| 122 | signo = SIGTERM; |
| 123 | if (pkill && argv[1] && argv[1][0] == '-') { |
| 124 | int temp = get_signum(argv[1]+1); |
| 125 | if (temp != -1) { |
| 126 | signo = temp; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 127 | argv++; |
| 128 | } |
| 129 | } |
| 130 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 131 | /* Parse remaining options */ |
| 132 | ppid2match = -1; |
| 133 | sid2match = -1; |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 134 | opt = getopt32(argv, "vlafxons:+P:+", &sid2match, &ppid2match); |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 135 | argv += optind; |
| 136 | |
| 137 | if (pkill && OPT_LIST) { /* -l: print the whole signal list */ |
| 138 | print_signames(); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | pid = getpid(); |
| 143 | if (sid2match == 0) |
| 144 | sid2match = getsid(pid); |
| 145 | |
Denys Vlasenko | 16d1e3c | 2009-08-14 22:33:10 +0200 | [diff] [blame] | 146 | scan_mask = PSSCAN_COMM | PSSCAN_ARGV0; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 147 | if (OPT_FULL) |
| 148 | scan_mask |= PSSCAN_ARGVN; |
| 149 | |
| 150 | /* One pattern is required, if no -s and no -P */ |
| 151 | if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1])) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 152 | bb_show_usage(); |
| 153 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 154 | if (argv[0]) |
Denys Vlasenko | 7794c21 | 2013-02-28 15:51:55 +0100 | [diff] [blame] | 155 | xregcomp(&re_buffer, argv[0], OPT_ANCHOR ? REG_EXTENDED : (REG_EXTENDED|REG_NOSUB)); |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 156 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 157 | matched_pid = 0; |
| 158 | cmd_last = NULL; |
| 159 | proc = NULL; |
| 160 | while ((proc = procps_scan(proc, scan_mask)) != NULL) { |
| 161 | char *cmd; |
Denys Vlasenko | 578008a | 2017-07-21 17:43:14 +0200 | [diff] [blame] | 162 | int cmdlen, match; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 163 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 164 | if (proc->pid == pid) |
| 165 | continue; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 166 | |
Denys Vlasenko | 578008a | 2017-07-21 17:43:14 +0200 | [diff] [blame] | 167 | if (!OPT_INVERT) { |
| 168 | /* Quickly reject -sN -PN mismatches... unless -v */ |
| 169 | if (ppid2match >= 0 && ppid2match != proc->ppid) |
| 170 | continue; |
| 171 | if (sid2match >= 0 && sid2match != proc->sid) |
| 172 | continue; |
| 173 | } |
Denys Vlasenko | 4add757 | 2017-06-26 14:41:53 +0200 | [diff] [blame] | 174 | |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 175 | cmdlen = -1; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 176 | cmd = proc->argv0; |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 177 | if (!cmd) { |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 178 | cmd = proc->comm; |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 179 | } else { |
| 180 | int i = proc->argv_len; |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 181 | |
| 182 | if (!OPT_LISTFULL) |
| 183 | cmdlen = strlen(cmd); /* not -a: find first NUL */ |
Denys Vlasenko | 4add757 | 2017-06-26 14:41:53 +0200 | [diff] [blame] | 184 | /* |
| 185 | * "sleep 11" looks like "sleep""\0""11""\0" in argv0. |
| 186 | * Make sure last "\0" does not get converted to " ": |
| 187 | */ |
| 188 | if (i && cmd[i-1] == '\0') |
| 189 | i--; |
Denys Vlasenko | 16d1e3c | 2009-08-14 22:33:10 +0200 | [diff] [blame] | 190 | while (--i >= 0) { |
| 191 | if ((unsigned char)cmd[i] < ' ') |
| 192 | cmd[i] = ' '; |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 195 | |
Denys Vlasenko | 578008a | 2017-07-21 17:43:14 +0200 | [diff] [blame] | 196 | if (OPT_INVERT) { |
| 197 | /* "pgrep -v -P1 firefox" means "not (ppid=1 AND name=firefox)" |
| 198 | * or equivalently "ppid!=1 OR name!=firefox". |
| 199 | * Check the first condition and if true, skip matching. |
| 200 | */ |
| 201 | if (ppid2match >= 0 && ppid2match != proc->ppid) |
| 202 | goto got_it; |
| 203 | if (sid2match >= 0 && sid2match != proc->sid) |
| 204 | goto got_it; |
| 205 | } |
| 206 | |
| 207 | match = !argv[0]; /* if no PATTERN, then it's a match, else... */ |
| 208 | if (!match) { |
| 209 | again: |
| 210 | match = (regexec(&re_buffer, cmd, 1, re_match, 0) == 0); |
| 211 | if (!match && cmd != proc->comm) { |
| 212 | /* if argv[] did not match, try comm */ |
| 213 | cmdlen = -1; |
| 214 | cmd = proc->comm; |
| 215 | goto again; |
| 216 | } |
| 217 | if (match && OPT_ANCHOR) { |
| 218 | /* -x requires full string match */ |
| 219 | match = (re_match[0].rm_so == 0 && cmd[re_match[0].rm_eo] == '\0'); |
| 220 | } |
| 221 | } |
| 222 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 223 | /* NB: OPT_INVERT is always 0 or 1 */ |
Denys Vlasenko | 578008a | 2017-07-21 17:43:14 +0200 | [diff] [blame] | 224 | if (match ^ OPT_INVERT) { |
| 225 | got_it: |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 226 | matched_pid = proc->pid; |
| 227 | if (OPT_LAST) { |
| 228 | free(cmd_last); |
Denis Vlasenko | 5fb0965 | 2007-09-30 16:36:02 +0000 | [diff] [blame] | 229 | cmd_last = xstrdup(cmd); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 230 | continue; |
| 231 | } |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 232 | if (cmdlen >= 0) |
| 233 | cmd[cmdlen] = '\0'; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 234 | act(proc->pid, cmd, signo); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 235 | if (OPT_FIRST) |
| 236 | break; |
| 237 | } |
| 238 | } |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 239 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 240 | if (cmd_last) { |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 241 | act(matched_pid, cmd_last, signo); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 242 | if (ENABLE_FEATURE_CLEAN_UP) |
| 243 | free(cmd_last); |
| 244 | } |
| 245 | return matched_pid == 0; /* return 1 if no processes listed/signaled */ |
| 246 | } |