1 /* vi: set sw=4 ts=4: */
3 * Mini pgrep/pkill implementation for busybox
5 * Copyright (C) 2007 Loic Grenie <loic.grenie@gmail.com>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config: bool "pgrep (6.8 kb)"
13 //config: Look for processes by name.
16 //config: bool "pkill (7.6 kb)"
19 //config: Send signals to processes by name.
21 //applet:IF_PGREP(APPLET_ODDNAME(pgrep, pgrep, BB_DIR_USR_BIN, BB_SUID_DROP, pgrep))
22 // APPLET_ODDNAME:name main location suid_type help
23 //applet:IF_PKILL(APPLET_ODDNAME(pkill, pgrep, BB_DIR_USR_BIN, BB_SUID_DROP, pkill))
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)),
29 //kbuild:lib-$(CONFIG_PGREP) += pgrep.o
30 //kbuild:lib-$(CONFIG_PKILL) += pgrep.o
32 //usage:#define pgrep_trivial_usage
33 //usage: "[-flanovx] [-s SID|-P PPID|PATTERN]"
34 //usage:#define pgrep_full_usage "\n\n"
35 //usage: "Display process(es) selected by regex PATTERN\n"
36 //usage: "\n -l Show command name too"
37 //usage: "\n -a Show command line too"
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"
46 //usage:#define pkill_trivial_usage
47 //usage: "[-l|-SIGNAL] [-fnovx] [-s SID|-P PPID|PATTERN]"
48 //usage:#define pkill_full_usage "\n\n"
49 //usage: "Send a signal to process(es) selected by regex PATTERN\n"
50 //usage: "\n -l List all signals"
51 //usage: "\n -f Match against entire command line"
52 //usage: "\n -n Signal the newest process only"
53 //usage: "\n -o Signal the oldest process only"
54 //usage: "\n -v Negate the match"
55 //usage: "\n -x Match whole name (not substring)"
56 //usage: "\n -s Match session ID (0 for current)"
57 //usage: "\n -P Match parent process ID"
62 /* Idea taken from kill.c */
63 #define pgrep (ENABLE_PGREP && (!ENABLE_PKILL || applet_name[1] == 'g'))
64 #define pkill (ENABLE_PKILL && (!ENABLE_PGREP || applet_name[1] == 'k'))
68 OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */
79 #define OPT_INVERT (opt & (1 << OPTBIT_V))
80 #define OPT_LIST (opt & (1 << OPTBIT_L))
81 #define OPT_LISTFULL (opt & (1 << OPTBIT_A))
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))
89 static void act(unsigned pid, char *cmd, int signo)
92 if (option_mask32 & ((1 << OPTBIT_L)|(1 << OPTBIT_A))) /* -l or -a */
93 printf("%u %s\n", pid, cmd);
100 int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
101 int pgrep_main(int argc UNUSED_PARAM, char **argv)
108 int sid2match, ppid2match;
110 procps_status_t *proc;
111 /* These are initialized to 0 */
114 regmatch_t re_match[1];
116 #define re_buffer (Z.re_buffer)
117 #define re_match (Z.re_match )
119 memset(&Z, 0, sizeof(Z));
121 /* Parse -SIGNAL for pkill. Must be first option, if present */
123 if (pkill && argv[1] && argv[1][0] == '-') {
124 int temp = get_signum(argv[1]+1);
131 /* Parse remaining options */
134 opt = getopt32(argv, "vlafxons:+P:+", &sid2match, &ppid2match);
137 if (pkill && OPT_LIST) { /* -l: print the whole signal list */
144 sid2match = getsid(pid);
146 scan_mask = PSSCAN_COMM | PSSCAN_ARGV0;
148 scan_mask |= PSSCAN_ARGVN;
150 /* One pattern is required, if no -s and no -P */
151 if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1]))
155 xregcomp(&re_buffer, argv[0], OPT_ANCHOR ? REG_EXTENDED : (REG_EXTENDED|REG_NOSUB));
160 while ((proc = procps_scan(proc, scan_mask)) != NULL) {
164 if (proc->pid == pid)
168 /* Quickly reject -sN -PN mismatches... unless -v */
169 if (ppid2match >= 0 && ppid2match != proc->ppid)
171 if (sid2match >= 0 && sid2match != proc->sid)
180 int i = proc->argv_len;
183 cmdlen = strlen(cmd); /* not -a: find first NUL */
185 * "sleep 11" looks like "sleep""\0""11""\0" in argv0.
186 * Make sure last "\0" does not get converted to " ":
188 if (i && cmd[i-1] == '\0')
191 if ((unsigned char)cmd[i] < ' ')
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.
201 if (ppid2match >= 0 && ppid2match != proc->ppid)
203 if (sid2match >= 0 && sid2match != proc->sid)
207 match = !argv[0]; /* if no PATTERN, then it's a match, else... */
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 */
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');
223 /* NB: OPT_INVERT is always 0 or 1 */
224 if (match ^ OPT_INVERT) {
226 matched_pid = proc->pid;
229 cmd_last = xstrdup(cmd);
234 act(proc->pid, cmd, signo);
241 act(matched_pid, cmd_last, signo);
242 if (ENABLE_FEATURE_CLEAN_UP)
245 return matched_pid == 0; /* return 1 if no processes listed/signaled */