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#include "libbb.h"
54#include "xregex.h"
55
56
57#define pgrep (ENABLE_PGREP && applet_name[1] == 'g')
58#define pkill (ENABLE_PKILL && applet_name[1] == 'k')
59
60enum {
61
62 OPTBIT_V = 0,
63 OPTBIT_L,
64 OPTBIT_F,
65 OPTBIT_X,
66 OPTBIT_O,
67 OPTBIT_N,
68 OPTBIT_S,
69 OPTBIT_P,
70};
71
72#define OPT_INVERT (opt & (1 << OPTBIT_V))
73#define OPT_LIST (opt & (1 << OPTBIT_L))
74#define OPT_FULL (opt & (1 << OPTBIT_F))
75#define OPT_ANCHOR (opt & (1 << OPTBIT_X))
76#define OPT_FIRST (opt & (1 << OPTBIT_O))
77#define OPT_LAST (opt & (1 << OPTBIT_N))
78#define OPT_SID (opt & (1 << OPTBIT_S))
79#define OPT_PPID (opt & (1 << OPTBIT_P))
80
81static void act(unsigned pid, char *cmd, int signo)
82{
83 if (pgrep) {
84 if (option_mask32 & (1 << OPTBIT_L))
85 printf("%u %s\n", pid, cmd);
86 else
87 printf("%u\n", pid);
88 } else
89 kill(pid, signo);
90}
91
92int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
93int pgrep_main(int argc UNUSED_PARAM, char **argv)
94{
95 unsigned pid;
96 int signo;
97 unsigned opt;
98 int scan_mask;
99 int matched_pid;
100 int sid2match, ppid2match;
101 char *cmd_last;
102 procps_status_t *proc;
103
104 struct {
105 regex_t re_buffer;
106 regmatch_t re_match[1];
107 } Z;
108#define re_buffer (Z.re_buffer)
109#define re_match (Z.re_match )
110
111 memset(&Z, 0, sizeof(Z));
112
113
114 signo = SIGTERM;
115 if (pkill && argv[1] && argv[1][0] == '-') {
116 int temp = get_signum(argv[1]+1);
117 if (temp != -1) {
118 signo = temp;
119 argv++;
120 }
121 }
122
123
124 ppid2match = -1;
125 sid2match = -1;
126 opt = getopt32(argv, "vlfxons:+P:+", &sid2match, &ppid2match);
127 argv += optind;
128
129 if (pkill && OPT_LIST) {
130 print_signames();
131 return 0;
132 }
133
134 pid = getpid();
135 if (sid2match == 0)
136 sid2match = getsid(pid);
137
138 scan_mask = PSSCAN_COMM | PSSCAN_ARGV0;
139 if (OPT_FULL)
140 scan_mask |= PSSCAN_ARGVN;
141
142
143 if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1]))
144 bb_show_usage();
145
146 if (argv[0])
147 xregcomp(&re_buffer, argv[0], OPT_ANCHOR ? REG_EXTENDED : (REG_EXTENDED|REG_NOSUB));
148
149 matched_pid = 0;
150 cmd_last = NULL;
151 proc = NULL;
152 while ((proc = procps_scan(proc, scan_mask)) != NULL) {
153 char *cmd;
154
155 if (proc->pid == pid)
156 continue;
157
158 cmd = proc->argv0;
159 if (!cmd) {
160 cmd = proc->comm;
161 } else {
162 int i = proc->argv_len;
163 while (--i >= 0) {
164 if ((unsigned char)cmd[i] < ' ')
165 cmd[i] = ' ';
166 }
167 }
168
169 if (ppid2match >= 0 && ppid2match != proc->ppid)
170 continue;
171 if (sid2match >= 0 && sid2match != proc->sid)
172 continue;
173
174
175 if (!argv[0]
176 || (regexec(&re_buffer, cmd, 1, re_match, 0) == 0
177 && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))
178 ) ^ OPT_INVERT
179 ) {
180 matched_pid = proc->pid;
181 if (OPT_LAST) {
182 free(cmd_last);
183 cmd_last = xstrdup(cmd);
184 continue;
185 }
186 act(proc->pid, cmd, signo);
187 if (OPT_FIRST)
188 break;
189 }
190 }
191
192 if (cmd_last) {
193 act(matched_pid, cmd_last, signo);
194 if (ENABLE_FEATURE_CLEAN_UP)
195 free(cmd_last);
196 }
197 return matched_pid == 0;
198}
199