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 | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame^] | 10 | //config: bool "pgrep (6.8 kb)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
| 13 | //config: Look for processes by name. |
| 14 | //config: |
| 15 | //config:config PKILL |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame^] | 16 | //config: bool "pkill (7.6 kb)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 17 | //config: default y |
| 18 | //config: help |
| 19 | //config: Send signals to processes by name. |
| 20 | |
| 21 | //applet:IF_PGREP(APPLET(pgrep, BB_DIR_USR_BIN, BB_SUID_DROP)) |
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)) |
| 24 | |
| 25 | //kbuild:lib-$(CONFIG_PGREP) += pgrep.o |
| 26 | //kbuild:lib-$(CONFIG_PKILL) += pgrep.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 27 | |
| 28 | //usage:#define pgrep_trivial_usage |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 29 | //usage: "[-flanovx] [-s SID|-P PPID|PATTERN]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 30 | //usage:#define pgrep_full_usage "\n\n" |
| 31 | //usage: "Display process(es) selected by regex PATTERN\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 32 | //usage: "\n -l Show command name too" |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 33 | //usage: "\n -a Show command line too" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 34 | //usage: "\n -f Match against entire command line" |
| 35 | //usage: "\n -n Show the newest process only" |
| 36 | //usage: "\n -o Show the oldest process only" |
| 37 | //usage: "\n -v Negate the match" |
| 38 | //usage: "\n -x Match whole name (not substring)" |
| 39 | //usage: "\n -s Match session ID (0 for current)" |
| 40 | //usage: "\n -P Match parent process ID" |
| 41 | //usage: |
| 42 | //usage:#define pkill_trivial_usage |
| 43 | //usage: "[-l|-SIGNAL] [-fnovx] [-s SID|-P PPID|PATTERN]" |
| 44 | //usage:#define pkill_full_usage "\n\n" |
| 45 | //usage: "Send a signal to process(es) selected by regex PATTERN\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 46 | //usage: "\n -l List all signals" |
| 47 | //usage: "\n -f Match against entire command line" |
| 48 | //usage: "\n -n Signal the newest process only" |
| 49 | //usage: "\n -o Signal the oldest process only" |
| 50 | //usage: "\n -v Negate the match" |
| 51 | //usage: "\n -x Match whole name (not substring)" |
| 52 | //usage: "\n -s Match session ID (0 for current)" |
| 53 | //usage: "\n -P Match parent process ID" |
| 54 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 55 | #include "libbb.h" |
| 56 | #include "xregex.h" |
| 57 | |
| 58 | /* Idea taken from kill.c */ |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 59 | #define pgrep (ENABLE_PGREP && (!ENABLE_PKILL || applet_name[1] == 'g')) |
| 60 | #define pkill (ENABLE_PKILL && (!ENABLE_PGREP || applet_name[1] == 'k')) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 61 | |
| 62 | enum { |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 63 | /* "vlafxons:+P:+" */ |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 64 | OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */ |
| 65 | OPTBIT_L, |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 66 | OPTBIT_A, |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 67 | OPTBIT_F, |
| 68 | OPTBIT_X, |
| 69 | OPTBIT_O, |
| 70 | OPTBIT_N, |
| 71 | OPTBIT_S, |
| 72 | OPTBIT_P, |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 75 | #define OPT_INVERT (opt & (1 << OPTBIT_V)) |
| 76 | #define OPT_LIST (opt & (1 << OPTBIT_L)) |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 77 | #define OPT_LISTFULL (opt & (1 << OPTBIT_A)) |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 78 | #define OPT_FULL (opt & (1 << OPTBIT_F)) |
| 79 | #define OPT_ANCHOR (opt & (1 << OPTBIT_X)) |
| 80 | #define OPT_FIRST (opt & (1 << OPTBIT_O)) |
| 81 | #define OPT_LAST (opt & (1 << OPTBIT_N)) |
| 82 | #define OPT_SID (opt & (1 << OPTBIT_S)) |
| 83 | #define OPT_PPID (opt & (1 << OPTBIT_P)) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 84 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 85 | static void act(unsigned pid, char *cmd, int signo) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 86 | { |
| 87 | if (pgrep) { |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 88 | if (option_mask32 & ((1 << OPTBIT_L)|(1 << OPTBIT_A))) /* -l or -a */ |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 89 | printf("%u %s\n", pid, cmd); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 90 | else |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 91 | printf("%u\n", pid); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 92 | } else |
| 93 | kill(pid, signo); |
| 94 | } |
| 95 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 96 | int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 97 | int pgrep_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 98 | { |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 99 | unsigned pid; |
| 100 | int signo; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 101 | unsigned opt; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 102 | int scan_mask; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 103 | int matched_pid; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 104 | int sid2match, ppid2match; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 105 | char *cmd_last; |
| 106 | procps_status_t *proc; |
| 107 | /* These are initialized to 0 */ |
| 108 | struct { |
| 109 | regex_t re_buffer; |
| 110 | regmatch_t re_match[1]; |
| 111 | } Z; |
| 112 | #define re_buffer (Z.re_buffer) |
| 113 | #define re_match (Z.re_match ) |
| 114 | |
| 115 | memset(&Z, 0, sizeof(Z)); |
| 116 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 117 | /* Parse -SIGNAL for pkill. Must be first option, if present */ |
| 118 | signo = SIGTERM; |
| 119 | if (pkill && argv[1] && argv[1][0] == '-') { |
| 120 | int temp = get_signum(argv[1]+1); |
| 121 | if (temp != -1) { |
| 122 | signo = temp; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 123 | argv++; |
| 124 | } |
| 125 | } |
| 126 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 127 | /* Parse remaining options */ |
| 128 | ppid2match = -1; |
| 129 | sid2match = -1; |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 130 | opt = getopt32(argv, "vlafxons:+P:+", &sid2match, &ppid2match); |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 131 | argv += optind; |
| 132 | |
| 133 | if (pkill && OPT_LIST) { /* -l: print the whole signal list */ |
| 134 | print_signames(); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | pid = getpid(); |
| 139 | if (sid2match == 0) |
| 140 | sid2match = getsid(pid); |
| 141 | |
Denys Vlasenko | 16d1e3c | 2009-08-14 22:33:10 +0200 | [diff] [blame] | 142 | scan_mask = PSSCAN_COMM | PSSCAN_ARGV0; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 143 | if (OPT_FULL) |
| 144 | scan_mask |= PSSCAN_ARGVN; |
| 145 | |
| 146 | /* One pattern is required, if no -s and no -P */ |
| 147 | if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1])) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 148 | bb_show_usage(); |
| 149 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 150 | if (argv[0]) |
Denys Vlasenko | 7794c21 | 2013-02-28 15:51:55 +0100 | [diff] [blame] | 151 | 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] | 152 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 153 | matched_pid = 0; |
| 154 | cmd_last = NULL; |
| 155 | proc = NULL; |
| 156 | while ((proc = procps_scan(proc, scan_mask)) != NULL) { |
| 157 | char *cmd; |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 158 | int cmdlen; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 159 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 160 | if (proc->pid == pid) |
| 161 | continue; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 162 | |
Denys Vlasenko | 4add757 | 2017-06-26 14:41:53 +0200 | [diff] [blame] | 163 | if (ppid2match >= 0 && ppid2match != proc->ppid) |
| 164 | continue; |
| 165 | if (sid2match >= 0 && sid2match != proc->sid) |
| 166 | continue; |
| 167 | |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 168 | cmdlen = -1; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 169 | cmd = proc->argv0; |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 170 | if (!cmd) { |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 171 | cmd = proc->comm; |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 172 | } else { |
| 173 | int i = proc->argv_len; |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 174 | |
| 175 | if (!OPT_LISTFULL) |
| 176 | cmdlen = strlen(cmd); /* not -a: find first NUL */ |
Denys Vlasenko | 4add757 | 2017-06-26 14:41:53 +0200 | [diff] [blame] | 177 | /* |
| 178 | * "sleep 11" looks like "sleep""\0""11""\0" in argv0. |
| 179 | * Make sure last "\0" does not get converted to " ": |
| 180 | */ |
| 181 | if (i && cmd[i-1] == '\0') |
| 182 | i--; |
Denys Vlasenko | 16d1e3c | 2009-08-14 22:33:10 +0200 | [diff] [blame] | 183 | while (--i >= 0) { |
| 184 | if ((unsigned char)cmd[i] < ' ') |
| 185 | cmd[i] = ' '; |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 188 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 189 | /* NB: OPT_INVERT is always 0 or 1 */ |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 190 | if (!argv[0] |
| 191 | || (regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */ |
| 192 | && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd))) |
| 193 | ) ^ OPT_INVERT |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 194 | ) { |
| 195 | matched_pid = proc->pid; |
| 196 | if (OPT_LAST) { |
| 197 | free(cmd_last); |
Denis Vlasenko | 5fb0965 | 2007-09-30 16:36:02 +0000 | [diff] [blame] | 198 | cmd_last = xstrdup(cmd); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 199 | continue; |
| 200 | } |
Denys Vlasenko | 9f4b422 | 2017-06-26 21:10:47 +0200 | [diff] [blame] | 201 | if (cmdlen >= 0) |
| 202 | cmd[cmdlen] = '\0'; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 203 | act(proc->pid, cmd, signo); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 204 | if (OPT_FIRST) |
| 205 | break; |
| 206 | } |
| 207 | } |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 208 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 209 | if (cmd_last) { |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 210 | act(matched_pid, cmd_last, signo); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 211 | if (ENABLE_FEATURE_CLEAN_UP) |
| 212 | free(cmd_last); |
| 213 | } |
| 214 | return matched_pid == 0; /* return 1 if no processes listed/signaled */ |
| 215 | } |