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"
13 //config: Look for processes by name.
16 //config: bool "pkill"
19 //config: Send signals to processes by name.
21 //applet:IF_PGREP(APPLET(pgrep, BB_DIR_USR_BIN, BB_SUID_DROP))
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))
25 //kbuild:lib-$(CONFIG_PGREP) += pgrep.o
26 //kbuild:lib-$(CONFIG_PKILL) += pgrep.o
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"
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"
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"
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"
57 /* Idea taken from kill.c */
58 #define pgrep (ENABLE_PGREP && applet_name[1] == 'g')
59 #define pkill (ENABLE_PKILL && applet_name[1] == 'k')
63 OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */
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))
82 static void act(unsigned pid, char *cmd, int signo)
85 if (option_mask32 & (1 << OPTBIT_L)) /* OPT_LIST */
86 printf("%u %s\n", pid, cmd);
93 int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
94 int pgrep_main(int argc UNUSED_PARAM, char **argv)
101 int sid2match, ppid2match;
103 procps_status_t *proc;
104 /* These are initialized to 0 */
107 regmatch_t re_match[1];
109 #define re_buffer (Z.re_buffer)
110 #define re_match (Z.re_match )
112 memset(&Z, 0, sizeof(Z));
114 /* Parse -SIGNAL for pkill. Must be first option, if present */
116 if (pkill && argv[1] && argv[1][0] == '-') {
117 int temp = get_signum(argv[1]+1);
124 /* Parse remaining options */
127 opt = getopt32(argv, "vlfxons:+P:+", &sid2match, &ppid2match);
130 if (pkill && OPT_LIST) { /* -l: print the whole signal list */
137 sid2match = getsid(pid);
139 scan_mask = PSSCAN_COMM | PSSCAN_ARGV0;
141 scan_mask |= PSSCAN_ARGVN;
143 /* One pattern is required, if no -s and no -P */
144 if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1]))
148 xregcomp(&re_buffer, argv[0], OPT_ANCHOR ? REG_EXTENDED : (REG_EXTENDED|REG_NOSUB));
153 while ((proc = procps_scan(proc, scan_mask)) != NULL) {
156 if (proc->pid == pid)
159 if (ppid2match >= 0 && ppid2match != proc->ppid)
161 if (sid2match >= 0 && sid2match != proc->sid)
168 int i = proc->argv_len;
170 * "sleep 11" looks like "sleep""\0""11""\0" in argv0.
171 * Make sure last "\0" does not get converted to " ":
173 if (i && cmd[i-1] == '\0')
176 if ((unsigned char)cmd[i] < ' ')
181 /* NB: OPT_INVERT is always 0 or 1 */
183 || (regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
184 && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))
187 matched_pid = proc->pid;
190 cmd_last = xstrdup(cmd);
193 act(proc->pid, cmd, signo);
200 act(matched_pid, cmd_last, signo);
201 if (ENABLE_FEATURE_CLEAN_UP)
204 return matched_pid == 0; /* return 1 if no processes listed/signaled */