1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59#include "libbb.h"
60#include "xregex.h"
61
62
63#define pgrep (ENABLE_PGREP && (!ENABLE_PKILL || applet_name[1] == 'g'))
64#define pkill (ENABLE_PKILL && (!ENABLE_PGREP || applet_name[1] == 'k'))
65
66enum {
67
68 OPTBIT_V = 0,
69 OPTBIT_L,
70 OPTBIT_A,
71 OPTBIT_F,
72 OPTBIT_X,
73 OPTBIT_O,
74 OPTBIT_N,
75 OPTBIT_S,
76 OPTBIT_P,
77};
78
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))
88
89static void act(unsigned pid, char *cmd, int signo)
90{
91 if (pgrep) {
92 if (option_mask32 & ((1 << OPTBIT_L)|(1 << OPTBIT_A)))
93 printf("%u %s\n", pid, cmd);
94 else
95 printf("%u\n", pid);
96 } else
97 kill(pid, signo);
98}
99
100int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
101int pgrep_main(int argc UNUSED_PARAM, char **argv)
102{
103 unsigned pid;
104 int signo;
105 unsigned opt;
106 int scan_mask;
107 int matched_pid;
108 int sid2match, ppid2match;
109 char *cmd_last;
110 procps_status_t *proc;
111
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
121
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;
127 argv++;
128 }
129 }
130
131
132 ppid2match = -1;
133 sid2match = -1;
134 opt = getopt32(argv, "vlafxons:+P:+", &sid2match, &ppid2match);
135 argv += optind;
136
137 if (pkill && OPT_LIST) {
138 print_signames();
139 return 0;
140 }
141
142 pid = getpid();
143 if (sid2match == 0)
144 sid2match = getsid(pid);
145
146 scan_mask = PSSCAN_COMM | PSSCAN_ARGV0;
147 if (OPT_FULL)
148 scan_mask |= PSSCAN_ARGVN;
149
150
151 if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1]))
152 bb_show_usage();
153
154 if (argv[0])
155 xregcomp(&re_buffer, argv[0], OPT_ANCHOR ? REG_EXTENDED : (REG_EXTENDED|REG_NOSUB));
156
157 matched_pid = 0;
158 cmd_last = NULL;
159 proc = NULL;
160 while ((proc = procps_scan(proc, scan_mask)) != NULL) {
161 char *cmd;
162 int cmdlen, match;
163
164 if (proc->pid == pid)
165 continue;
166
167 if (!OPT_INVERT) {
168
169 if (ppid2match >= 0 && ppid2match != proc->ppid)
170 continue;
171 if (sid2match >= 0 && sid2match != proc->sid)
172 continue;
173 }
174
175 cmdlen = -1;
176 cmd = proc->argv0;
177 if (!cmd) {
178 cmd = proc->comm;
179 } else {
180 int i = proc->argv_len;
181
182 if (!OPT_LISTFULL)
183 cmdlen = strlen(cmd);
184
185
186
187
188 if (i && cmd[i-1] == '\0')
189 i--;
190 while (--i >= 0) {
191 if ((unsigned char)cmd[i] < ' ')
192 cmd[i] = ' ';
193 }
194 }
195
196 if (OPT_INVERT) {
197
198
199
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];
208 if (!match) {
209 again:
210 match = (regexec(&re_buffer, cmd, 1, re_match, 0) == 0);
211 if (!match && cmd != proc->comm) {
212
213 cmdlen = -1;
214 cmd = proc->comm;
215 goto again;
216 }
217 if (match && OPT_ANCHOR) {
218
219 match = (re_match[0].rm_so == 0 && cmd[re_match[0].rm_eo] == '\0');
220 }
221 }
222
223
224 if (match ^ OPT_INVERT) {
225 got_it:
226 matched_pid = proc->pid;
227 if (OPT_LAST) {
228 free(cmd_last);
229 cmd_last = xstrdup(cmd);
230 continue;
231 }
232 if (cmdlen >= 0)
233 cmd[cmdlen] = '\0';
234 act(proc->pid, cmd, signo);
235 if (OPT_FIRST)
236 break;
237 }
238 }
239
240 if (cmd_last) {
241 act(matched_pid, cmd_last, signo);
242 if (ENABLE_FEATURE_CLEAN_UP)
243 free(cmd_last);
244 }
245 return matched_pid == 0;
246}
247