Commit | Line | Data |
---|---|---|
984263bc MD |
1 | /*- |
2 | * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org> | |
3 | * Copyright (c) 2000 Paul Saab <ps@FreeBSD.org> | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * | |
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
25 | * SUCH DAMAGE. | |
26 | * | |
27 | * $FreeBSD: src/usr.bin/killall/killall.c,v 1.5.2.4 2001/05/19 19:22:49 phk Exp $ | |
28 | */ | |
29 | ||
e0ecab34 | 30 | #include <sys/user.h> |
984263bc MD |
31 | #include <sys/param.h> |
32 | #include <sys/stat.h> | |
984263bc MD |
33 | #include <sys/sysctl.h> |
34 | #include <fcntl.h> | |
35 | #include <dirent.h> | |
36 | #include <stdio.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | #include <pwd.h> | |
40 | #include <signal.h> | |
41 | #include <regex.h> | |
42 | #include <ctype.h> | |
43 | #include <err.h> | |
44 | #include <errno.h> | |
45 | #include <unistd.h> | |
46 | ||
b031767f MD |
47 | #define PHASH_SIZE 1024 |
48 | ||
49 | struct pchain { | |
50 | struct pchain *next; | |
51 | struct kinfo_proc *proc; | |
52 | struct pchain *parent; | |
53 | }; | |
54 | ||
984263bc | 55 | static char *prog; |
b031767f MD |
56 | static struct pchain *phash[PHASH_SIZE]; |
57 | ||
58 | static struct pchain *saveparents(struct kinfo_proc *procs, int nprocs, | |
59 | pid_t mypid); | |
60 | static int checkparent(struct pchain *mychain, pid_t pid); | |
984263bc MD |
61 | |
62 | static void __dead2 | |
63 | usage(void) | |
64 | { | |
65 | ||
2faa2b55 | 66 | fprintf(stderr, "usage: %s [-c cmd] [-d|-v] [-h|-?|-help] [-j jail] " |
67 | "[-l] [-m] [-q] [-s] [-t tty] [-T] [-u user] " | |
68 | "[-sig] [cmd]...\n", prog); | |
c5017cd9 MD |
69 | fprintf(stderr, "At least one option or argument to specify " |
70 | "processes must be given.\n"); | |
984263bc MD |
71 | exit(1); |
72 | } | |
73 | ||
74 | static char * | |
75 | upper(const char *str) | |
76 | { | |
77 | static char buf[80]; | |
78 | char *s; | |
79 | ||
0ec73fda | 80 | strlcpy(buf, str, sizeof(buf)); |
984263bc | 81 | for (s = buf; *s; s++) |
a38bc7f3 | 82 | *s = toupper((unsigned char)*s); |
984263bc MD |
83 | return buf; |
84 | } | |
85 | ||
86 | ||
87 | static void | |
88 | printsig(FILE *fp) | |
89 | { | |
90 | const char *const * p; | |
91 | int cnt; | |
92 | int offset = 0; | |
93 | ||
94 | for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) { | |
95 | offset += fprintf(fp, "%s ", upper(*p)); | |
96 | if (offset >= 75 && cnt > 1) { | |
97 | offset = 0; | |
98 | fprintf(fp, "\n"); | |
99 | } | |
100 | } | |
101 | fprintf(fp, "\n"); | |
102 | } | |
103 | ||
104 | static void | |
105 | nosig(char *name) | |
106 | { | |
107 | ||
108 | warnx("unknown signal %s; valid signals:", name); | |
109 | printsig(stderr); | |
110 | exit(1); | |
111 | } | |
112 | ||
113 | int | |
114 | main(int ac, char **av) | |
115 | { | |
116 | struct kinfo_proc *procs = NULL, *newprocs; | |
b031767f | 117 | struct pchain *mychain; |
984263bc MD |
118 | struct stat sb; |
119 | struct passwd *pw; | |
120 | regex_t rgx; | |
121 | regmatch_t pmatch; | |
122 | int i, j; | |
123 | char buf[256]; | |
124 | char *user = NULL; | |
125 | char *tty = NULL; | |
126 | char *cmd = NULL; | |
0ec73fda | 127 | int qflag = 0; |
984263bc MD |
128 | int vflag = 0; |
129 | int sflag = 0; | |
35048f4d | 130 | int jflag = 0, jailid = 0; |
984263bc MD |
131 | int dflag = 0; |
132 | int mflag = 0; | |
b031767f | 133 | int Tflag = 0; |
984263bc MD |
134 | uid_t uid = 0; |
135 | dev_t tdev = 0; | |
1991012b | 136 | pid_t mypid; |
984263bc MD |
137 | char thiscmd[MAXCOMLEN + 1]; |
138 | pid_t thispid; | |
139 | uid_t thisuid; | |
140 | dev_t thistdev; | |
141 | int sig = SIGTERM; | |
142 | const char *const *p; | |
143 | char *ep; | |
144 | int errors = 0; | |
145 | int mib[4]; | |
146 | size_t miblen; | |
147 | int st, nprocs; | |
148 | size_t size; | |
149 | int matched; | |
150 | int killed = 0; | |
151 | ||
152 | prog = av[0]; | |
153 | av++; | |
154 | ac--; | |
155 | ||
156 | while (ac > 0) { | |
157 | if (strcmp(*av, "-l") == 0) { | |
158 | printsig(stdout); | |
159 | exit(0); | |
160 | } | |
161 | if (strcmp(*av, "-help") == 0) | |
162 | usage(); | |
163 | if (**av == '-') { | |
164 | ++*av; | |
165 | switch (**av) { | |
2faa2b55 | 166 | case 'h': |
167 | case '?': | |
168 | usage(); | |
169 | /* NOTREACHED */ | |
984263bc MD |
170 | case 'u': |
171 | ++*av; | |
172 | if (**av == '\0') | |
173 | ++av; | |
174 | --ac; | |
175 | user = *av; | |
176 | break; | |
177 | case 't': | |
178 | ++*av; | |
179 | if (**av == '\0') | |
180 | ++av; | |
181 | --ac; | |
182 | tty = *av; | |
183 | break; | |
184 | case 'c': | |
185 | ++*av; | |
186 | if (**av == '\0') | |
187 | ++av; | |
188 | --ac; | |
189 | cmd = *av; | |
190 | break; | |
35048f4d JS |
191 | case 'j': |
192 | { | |
193 | const char *errstr; | |
194 | ++*av; | |
195 | if (**av == '\0') | |
196 | ++av; | |
197 | --ac; | |
198 | jailid = strtonum(*av, 1, INT_MAX, &errstr); | |
199 | ||
200 | if (errstr) | |
201 | errx(1, "jail id is %s: %s", errstr, *av); | |
202 | jflag++; | |
203 | break; | |
204 | } | |
0ec73fda HP |
205 | case 'q': |
206 | qflag++; | |
207 | break; | |
984263bc MD |
208 | case 'v': |
209 | vflag++; | |
210 | break; | |
211 | case 's': | |
212 | sflag++; | |
213 | break; | |
214 | case 'd': | |
215 | dflag++; | |
216 | break; | |
217 | case 'm': | |
218 | mflag++; | |
219 | break; | |
b031767f MD |
220 | case 'T': |
221 | Tflag++; | |
222 | break; | |
984263bc | 223 | default: |
a38bc7f3 | 224 | if (isalpha((unsigned char)**av)) { |
984263bc MD |
225 | if (strncasecmp(*av, "sig", 3) == 0) |
226 | *av += 3; | |
227 | for (sig = NSIG, p = sys_signame + 1; | |
228 | --sig; ++p) | |
229 | if (strcasecmp(*p, *av) == 0) { | |
230 | sig = p - sys_signame; | |
231 | break; | |
232 | } | |
233 | if (!sig) | |
234 | nosig(*av); | |
a38bc7f3 | 235 | } else if (isdigit((unsigned char)**av)) { |
984263bc MD |
236 | sig = strtol(*av, &ep, 10); |
237 | if (!*av || *ep) | |
238 | errx(1, "illegal signal number: %s", *av); | |
c24db2df | 239 | if (sig < 0 || sig >= NSIG) |
984263bc MD |
240 | nosig(*av); |
241 | } else | |
242 | nosig(*av); | |
243 | } | |
244 | ++av; | |
245 | --ac; | |
246 | } else { | |
247 | break; | |
248 | } | |
249 | } | |
250 | ||
b031767f MD |
251 | if (user == NULL && tty == NULL && cmd == NULL && |
252 | jflag == 0 && Tflag == 0 && ac == 0) { | |
984263bc | 253 | usage(); |
b031767f MD |
254 | } |
255 | ||
256 | if (Tflag) { | |
257 | tty = ttyname(0); | |
258 | if (tty) | |
259 | tty = strdup(tty); | |
260 | } | |
984263bc MD |
261 | |
262 | if (tty) { | |
263 | if (strncmp(tty, "/dev/", 5) == 0) | |
264 | snprintf(buf, sizeof(buf), "%s", tty); | |
265 | else if (strncmp(tty, "tty", 3) == 0) | |
266 | snprintf(buf, sizeof(buf), "/dev/%s", tty); | |
c5017cd9 MD |
267 | else if (isdigit(tty[0])) |
268 | snprintf(buf, sizeof(buf), "/dev/pts/%s", tty); | |
984263bc MD |
269 | else |
270 | snprintf(buf, sizeof(buf), "/dev/tty%s", tty); | |
271 | if (stat(buf, &sb) < 0) | |
272 | err(1, "stat(%s)", buf); | |
273 | if (!S_ISCHR(sb.st_mode)) | |
274 | errx(1, "%s: not a character device", buf); | |
275 | tdev = sb.st_rdev; | |
276 | if (dflag) | |
277 | printf("ttydev:0x%x\n", tdev); | |
278 | } | |
279 | if (user) { | |
280 | pw = getpwnam(user); | |
281 | if (pw == NULL) | |
282 | errx(1, "user %s does not exist", user); | |
283 | uid = pw->pw_uid; | |
284 | if (dflag) | |
285 | printf("uid:%d\n", uid); | |
286 | } else { | |
287 | uid = getuid(); | |
288 | if (uid != 0) { | |
289 | pw = getpwuid(uid); | |
290 | if (pw) | |
291 | user = pw->pw_name; | |
292 | if (dflag) | |
293 | printf("uid:%d\n", uid); | |
294 | } | |
295 | } | |
296 | size = 0; | |
297 | mib[0] = CTL_KERN; | |
298 | mib[1] = KERN_PROC; | |
299 | mib[2] = KERN_PROC_ALL; | |
300 | mib[3] = 0; | |
301 | miblen = 3; | |
302 | ||
303 | if (user && mib[2] == KERN_PROC_ALL) { | |
304 | mib[2] = KERN_PROC_RUID; | |
305 | mib[3] = uid; | |
306 | miblen = 4; | |
307 | } | |
308 | if (tty && mib[2] == KERN_PROC_ALL) { | |
309 | mib[2] = KERN_PROC_TTY; | |
310 | mib[3] = tdev; | |
311 | miblen = 4; | |
312 | } | |
313 | ||
314 | st = sysctl(mib, miblen, NULL, &size, NULL, 0); | |
315 | do { | |
316 | size += size / 10; | |
317 | newprocs = realloc(procs, size); | |
678e8cc6 | 318 | if (newprocs == NULL) { |
984263bc MD |
319 | if (procs) |
320 | free(procs); | |
321 | errx(1, "could not reallocate memory"); | |
322 | } | |
323 | procs = newprocs; | |
324 | st = sysctl(mib, miblen, procs, &size, NULL, 0); | |
325 | } while (st == -1 && errno == ENOMEM); | |
326 | if (st == -1) | |
327 | err(1, "could not sysctl(KERN_PROC)"); | |
328 | if (size % sizeof(struct kinfo_proc) != 0) { | |
a38bc7f3 | 329 | fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n", |
984263bc MD |
330 | size, sizeof(struct kinfo_proc)); |
331 | fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n"); | |
332 | exit(1); | |
333 | } | |
334 | nprocs = size / sizeof(struct kinfo_proc); | |
335 | if (dflag) | |
336 | printf("nprocs %d\n", nprocs); | |
1991012b | 337 | mypid = getpid(); |
984263bc | 338 | |
b031767f MD |
339 | /* |
340 | * Record parent chain if Tflag | |
341 | */ | |
342 | if (Tflag && tty) | |
343 | mychain = saveparents(procs, nprocs, mypid); | |
344 | else | |
345 | mychain = NULL; | |
346 | ||
347 | /* | |
348 | * Final scan | |
349 | */ | |
984263bc | 350 | for (i = 0; i < nprocs; i++) { |
5dfd06ac SS |
351 | thispid = procs[i].kp_pid; |
352 | strncpy(thiscmd, procs[i].kp_comm, MAXCOMLEN); | |
984263bc | 353 | thiscmd[MAXCOMLEN] = '\0'; |
5dfd06ac SS |
354 | thistdev = procs[i].kp_tdev; |
355 | thisuid = procs[i].kp_ruid; /* real uid */ | |
984263bc | 356 | |
1991012b MD |
357 | if (thispid == mypid) |
358 | continue; | |
984263bc | 359 | matched = 1; |
5dfd06ac | 360 | if ((int)procs[i].kp_pid < 0) |
17c88c3a | 361 | matched = 0; |
984263bc MD |
362 | if (user) { |
363 | if (thisuid != uid) | |
364 | matched = 0; | |
365 | } | |
366 | if (tty) { | |
367 | if (thistdev != tdev) | |
368 | matched = 0; | |
b031767f MD |
369 | if (Tflag && checkparent(mychain, procs[i].kp_pid)) |
370 | matched = 0; | |
984263bc | 371 | } |
35048f4d | 372 | if (jflag) { |
5dfd06ac | 373 | if (procs[i].kp_jailid != jailid) |
35048f4d JS |
374 | matched = 0; |
375 | } | |
984263bc MD |
376 | if (cmd) { |
377 | if (mflag) { | |
378 | if (regcomp(&rgx, cmd, | |
379 | REG_EXTENDED|REG_NOSUB) != 0) { | |
380 | mflag = 0; | |
381 | warnx("%s: illegal regexp", cmd); | |
382 | } | |
383 | } | |
384 | if (mflag) { | |
385 | pmatch.rm_so = 0; | |
386 | pmatch.rm_eo = strlen(thiscmd); | |
387 | if (regexec(&rgx, thiscmd, 0, &pmatch, | |
388 | REG_STARTEND) != 0) | |
389 | matched = 0; | |
390 | regfree(&rgx); | |
391 | } else { | |
392 | if (strcmp(thiscmd, cmd) != 0) | |
393 | matched = 0; | |
394 | } | |
395 | } | |
396 | if (matched == 0) | |
397 | continue; | |
35048f4d JS |
398 | if (ac > 0) { |
399 | matched = 0; | |
400 | for (j = 0; j < ac; j++) { | |
401 | if (mflag) { | |
402 | if (regcomp(&rgx, av[j], | |
403 | REG_EXTENDED|REG_NOSUB) != 0) { | |
404 | mflag = 0; | |
405 | warnx("%s: illegal regexp", av[j]); | |
406 | } | |
984263bc | 407 | } |
35048f4d JS |
408 | if (mflag) { |
409 | pmatch.rm_so = 0; | |
410 | pmatch.rm_eo = strlen(thiscmd); | |
411 | if (regexec(&rgx, thiscmd, 0, &pmatch, | |
412 | REG_STARTEND) == 0) | |
413 | matched = 1; | |
414 | regfree(&rgx); | |
415 | } else { | |
416 | if (strcmp(thiscmd, av[j]) == 0) | |
417 | matched = 1; | |
418 | } | |
419 | if (matched) | |
420 | break; | |
984263bc | 421 | } |
35048f4d JS |
422 | if (matched == 0) |
423 | continue; | |
984263bc | 424 | } |
984263bc MD |
425 | if (dflag) |
426 | printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig, | |
427 | thiscmd, thispid, thistdev, thisuid); | |
428 | ||
429 | if (vflag || sflag) | |
430 | printf("kill -%s %d\n", upper(sys_signame[sig]), | |
431 | thispid); | |
432 | ||
433 | killed++; | |
434 | if (!dflag && !sflag) { | |
435 | if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) { | |
436 | warn("kill -%s %d", upper(sys_signame[sig]), | |
437 | thispid); | |
438 | errors = 1; | |
439 | } | |
440 | } | |
441 | } | |
0ec73fda | 442 | if (!qflag && killed == 0) { |
984263bc MD |
443 | fprintf(stderr, "No matching processes %swere found\n", |
444 | getuid() != 0 ? "belonging to you " : ""); | |
445 | errors = 1; | |
446 | } | |
447 | exit(errors); | |
448 | } | |
b031767f MD |
449 | |
450 | ||
451 | static | |
452 | struct pchain * | |
453 | saveparents(struct kinfo_proc *procs, int nprocs, pid_t mypid) | |
454 | { | |
455 | struct pchain *rchain = NULL; | |
456 | struct pchain *chain; | |
457 | struct pchain *pchain; | |
458 | int i; | |
459 | int hv; | |
460 | ||
461 | for (i = 0; i < nprocs; ++i) { | |
462 | if ((int)procs[i].kp_pid < 0) | |
463 | continue; | |
464 | hv = (int)procs[i].kp_pid & 1023; | |
465 | chain = malloc(sizeof(*chain)); | |
466 | chain->proc = &procs[i]; | |
467 | chain->parent = NULL; | |
468 | chain->next = phash[hv]; | |
469 | phash[hv] = chain; | |
470 | if (mypid == procs[i].kp_pid) | |
471 | rchain = chain; | |
472 | } | |
473 | for (i = 0; i < nprocs; ++i) { | |
474 | if ((int)procs[i].kp_pid < 0) | |
475 | continue; | |
476 | if ((int)procs[i].kp_ppid < 0) | |
477 | continue; | |
478 | hv = (int)procs[i].kp_pid & 1023; | |
479 | for (chain = phash[hv]; chain; chain = chain->next) { | |
480 | if (chain->proc->kp_pid == procs[i].kp_pid) | |
481 | break; | |
482 | } | |
483 | hv = (int)procs[i].kp_ppid & 1023; | |
484 | for (pchain = phash[hv]; pchain; pchain = pchain->next) { | |
485 | if (pchain->proc->kp_pid == procs[i].kp_ppid) { | |
486 | if (chain) | |
487 | chain->parent = pchain; | |
488 | break; | |
489 | } | |
490 | } | |
491 | } | |
492 | return (rchain); | |
493 | } | |
494 | ||
495 | static | |
496 | int | |
497 | checkparent(struct pchain *chain, pid_t pid) | |
498 | { | |
499 | while (chain) { | |
500 | if (chain->proc->kp_pid == pid) | |
501 | return(1); | |
502 | chain = chain->parent; | |
503 | } | |
504 | return(0); | |
505 | } |