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