]> git.saurik.com Git - apple/shell_cmds.git/blame - killall/killall.c
shell_cmds-149.tar.gz
[apple/shell_cmds.git] / killall / killall.c
CommitLineData
c0fcf4e1
A
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.
c0fcf4e1
A
26 */
27
28#include <sys/cdefs.h>
e1a085ba
A
29__FBSDID("$FreeBSD: src/usr.bin/killall/killall.c,v 1.31 2004/07/29 18:36:35 maxim Exp $");
30
c0fcf4e1 31#include <sys/param.h>
e1a085ba
A
32#ifndef __APPLE__
33#include <sys/jail.h>
34#endif /* !__APPLE__ */
c0fcf4e1
A
35#include <sys/stat.h>
36#include <sys/user.h>
37#include <sys/sysctl.h>
38#include <fcntl.h>
39#include <dirent.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <pwd.h>
44#include <signal.h>
45#include <regex.h>
46#include <ctype.h>
47#include <err.h>
48#include <errno.h>
49#include <unistd.h>
e1a085ba 50#include <locale.h>
c0fcf4e1 51
ddb4a88b
A
52#include <getopt.h>
53#define OPTIONS ("c:dej:lmst:u:vz")
54
e1a085ba 55static void __dead2
c0fcf4e1
A
56usage(void)
57{
58
e1a085ba
A
59#ifdef __APPLE__
60 fprintf(stderr, "usage: killall [-delmsvz] [-help]\n");
61#else /* !__APPLE__ */
62 fprintf(stderr, "usage: killall [-delmsvz] [-help] [-j jid]\n");
63#endif /* __APPLE__ */
64 fprintf(stderr,
65 " [-u user] [-t tty] [-c cmd] [-SIGNAL] [cmd]...\n");
c0fcf4e1
A
66 fprintf(stderr, "At least one option or argument to specify processes must be given.\n");
67 exit(1);
68}
69
70static char *
71upper(const char *str)
72{
73 static char buf[80];
74 char *s;
75
76 strncpy(buf, str, sizeof(buf));
77 buf[sizeof(buf) - 1] = '\0';
78 for (s = buf; *s; s++)
e1a085ba 79 *s = toupper((unsigned char)*s);
c0fcf4e1
A
80 return buf;
81}
82
83
84static void
85printsig(FILE *fp)
86{
87 const char *const * p;
88 int cnt;
89 int offset = 0;
90
91 for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
92 offset += fprintf(fp, "%s ", upper(*p));
93 if (offset >= 75 && cnt > 1) {
94 offset = 0;
95 fprintf(fp, "\n");
96 }
97 }
98 fprintf(fp, "\n");
99}
100
101static void
102nosig(char *name)
103{
104
105 warnx("unknown signal %s; valid signals:", name);
106 printsig(stderr);
107 exit(1);
108}
109
ddb4a88b
A
110/*
111 * kludge_signal_args - remove any signal option (-SIGXXX, -##) from the argv array.
112 */
113void
114kludge_signal_args(int *argc, char **argv, int *sig)
115{
116 int i;
117 int shift = 0;
118 int kludge = 1;
119 char *ptr;
120 const char *const *p;
121 char *ep;
122
123 /* i = 1, skip program name */
124 for (i = 1; i < *argc; i++) {
125 /* Stop if we encounter either a non-option or -- */
126 if (*argv[i] != '-' || strcmp(argv[i], "--") == 0)
127 kludge = 0;
128 ptr = argv[i] + 1;
129 if (kludge && strchr(OPTIONS, *ptr) == NULL) {
130 if (isalpha(*ptr)) {
131 if (strcmp(ptr, "help") == 0)
132 usage();
133 if (strncasecmp(ptr, "sig", 3) == 0)
134 ptr += 3;
135 for (*sig = NSIG, p = sys_signame + 1; --*sig; ++p)
136 if (strcasecmp(*p, ptr) == 0) {
137 *sig = p - sys_signame;
138 break;
139 }
140 if (!*sig)
141 nosig(ptr);
142 } else if (isdigit(*ptr)) {
143 *sig = strtol(ptr, &ep, 10);
144 if (*ep)
145 errx(1, "illegal signal number: %s", ptr);
146 if (*sig < 0 || *sig >= NSIG)
147 nosig(ptr);
148 } else
149 nosig(ptr);
150
151 shift++;
152 continue;
153 }
154
155 argv[i - shift] = argv[i];
156 }
157
158 for (i = *argc - shift; i < *argc; i++) {
159 argv[i] = NULL;
160 }
161
162 *argc -= shift;
163}
164
c0fcf4e1
A
165int
166main(int ac, char **av)
167{
168 struct kinfo_proc *procs = NULL, *newprocs;
169 struct stat sb;
170 struct passwd *pw;
171 regex_t rgx;
172 regmatch_t pmatch;
173 int i, j;
174 char buf[256];
175 char *user = NULL;
176 char *tty = NULL;
177 char *cmd = NULL;
178 int vflag = 0;
179 int sflag = 0;
180 int dflag = 0;
e1a085ba
A
181 int eflag = 0;
182#ifndef __APPLE__
183 int jflag = 0;
184#endif /* !__APPLE__*/
c0fcf4e1 185 int mflag = 0;
e1a085ba 186 int zflag = 0;
c0fcf4e1
A
187 uid_t uid = 0;
188 dev_t tdev = 0;
e1a085ba
A
189 pid_t mypid;
190#ifdef __APPLE__
9bafe280 191 char *thiscmd;
e1a085ba
A
192#else /* !__APPLE__ */
193 char thiscmd[MAXCOMLEN + 1];
194#endif /* __APPLE__ */
c0fcf4e1 195 pid_t thispid;
e1a085ba 196#ifndef __APPLE__
c0fcf4e1 197 uid_t thisuid;
e1a085ba 198#endif /* !__APPLE__ */
c0fcf4e1
A
199 dev_t thistdev;
200 int sig = SIGTERM;
c0fcf4e1
A
201 char *ep;
202 int errors = 0;
e1a085ba
A
203#ifndef __APPLE__
204 int jid;
205#endif /* !__APPLE__ */
c0fcf4e1
A
206 int mib[4];
207 size_t miblen;
208 int st, nprocs;
209 size_t size;
210 int matched;
211 int killed = 0;
ddb4a88b 212 int ch;
c0fcf4e1 213
e1a085ba
A
214 setlocale(LC_ALL, "");
215
ddb4a88b 216 kludge_signal_args(&ac, av, &sig);
c0fcf4e1 217
ddb4a88b
A
218 while ((ch = getopt(ac, av, OPTIONS)) != -1) {
219 switch (ch) {
220 case 'c':
221 cmd = optarg;
222 break;
223 case 'd':
224 dflag++;
225 break;
226 case 'e':
227 eflag++;
228 break;
229#ifndef __APPLE__
230 case 'j':
231 jflag++;
232 jid = strtol(optarg, &ep, 10);
233 if (*ep)
234 errx(1, "illegal jid: %s", optarg);
235 if (jail_attach(jid) == -1)
236 err(1, "jail_attach(): %d", jid);
237 break;
238#endif /* __APPLE__ */
239 case 'l':
c0fcf4e1
A
240 printsig(stdout);
241 exit(0);
ddb4a88b
A
242 case 'm':
243 mflag++;
244 break;
245 case 's':
246 sflag++;
247 break;
248 case 't':
249 tty = optarg;
250 break;
251 case 'u':
252 user = optarg;
c0fcf4e1 253 break;
ddb4a88b
A
254 case 'v':
255 vflag++;
256 break;
257 case 'z':
258 zflag++;
259 break;
260 default:
261 usage();
c0fcf4e1
A
262 }
263 }
264
ddb4a88b
A
265 ac -= optind;
266 av += optind;
267
e1a085ba 268#ifdef __APPLE__
c0fcf4e1 269 if (user == NULL && tty == NULL && cmd == NULL && ac == 0)
e1a085ba
A
270#else /* !__APPLE__*/
271 if (user == NULL && tty == NULL && cmd == NULL && !jflag && ac == 0)
272#endif /* __APPLE__ */
c0fcf4e1
A
273 usage();
274
275 if (tty) {
276 if (strncmp(tty, "/dev/", 5) == 0)
277 snprintf(buf, sizeof(buf), "%s", tty);
278 else if (strncmp(tty, "tty", 3) == 0)
279 snprintf(buf, sizeof(buf), "/dev/%s", tty);
280 else
281 snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
282 if (stat(buf, &sb) < 0)
283 err(1, "stat(%s)", buf);
284 if (!S_ISCHR(sb.st_mode))
285 errx(1, "%s: not a character device", buf);
286 tdev = sb.st_rdev;
287 if (dflag)
288 printf("ttydev:0x%x\n", tdev);
289 }
290 if (user) {
291 uid = strtol(user, &ep, 10);
e1a085ba 292 if (*user == '\0' || *ep != '\0') { /* was it a number? */
c0fcf4e1
A
293 pw = getpwnam(user);
294 if (pw == NULL)
295 errx(1, "user %s does not exist", user);
296 uid = pw->pw_uid;
297 if (dflag)
298 printf("uid:%d\n", uid);
299 }
300 } else {
301 uid = getuid();
302 if (uid != 0) {
303 pw = getpwuid(uid);
304 if (pw)
305 user = pw->pw_name;
306 if (dflag)
307 printf("uid:%d\n", uid);
308 }
309 }
310 size = 0;
311 mib[0] = CTL_KERN;
312 mib[1] = KERN_PROC;
e1a085ba 313#ifdef __APPLE__
c0fcf4e1 314 mib[2] = KERN_PROC_ALL;
e1a085ba
A
315#else /* !__APPLE__ */
316 mib[2] = KERN_PROC_PROC;
317#endif /* __APPLE__ */
c0fcf4e1
A
318 mib[3] = 0;
319 miblen = 3;
320
e1a085ba
A
321 if (user) {
322 mib[2] = eflag ? KERN_PROC_UID : KERN_PROC_RUID;
c0fcf4e1
A
323 mib[3] = uid;
324 miblen = 4;
e1a085ba 325 } else if (tty) {
c0fcf4e1
A
326 mib[2] = KERN_PROC_TTY;
327 mib[3] = tdev;
328 miblen = 4;
329 }
330
331 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
332 do {
333 size += size / 10;
334 newprocs = realloc(procs, size);
335 if (newprocs == 0) {
336 if (procs)
337 free(procs);
338 errx(1, "could not reallocate memory");
339 }
340 procs = newprocs;
341 st = sysctl(mib, miblen, procs, &size, NULL, 0);
342 } while (st == -1 && errno == ENOMEM);
343 if (st == -1)
344 err(1, "could not sysctl(KERN_PROC)");
345 if (size % sizeof(struct kinfo_proc) != 0) {
e1a085ba 346 fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
c0fcf4e1
A
347 size, sizeof(struct kinfo_proc));
348 fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n");
349 exit(1);
350 }
351 nprocs = size / sizeof(struct kinfo_proc);
352 if (dflag)
353 printf("nprocs %d\n", nprocs);
e1a085ba 354 mypid = getpid();
c0fcf4e1
A
355
356 for (i = 0; i < nprocs; i++) {
e1a085ba
A
357#ifdef __APPLE__
358 if ((procs[i].kp_proc.p_stat & SZOMB) == SZOMB && !zflag)
359 continue;
360 thispid = procs[i].kp_proc.p_pid;
361
9bafe280
A
362 int mib[3], argmax;
363 size_t syssize;
364 char *procargs, *cp;
365
366 mib[0] = CTL_KERN;
367 mib[1] = KERN_ARGMAX;
368
369 syssize = sizeof(argmax);
e1a085ba 370 if (sysctl(mib, 2, &argmax, &syssize, NULL, 0) == -1)
9bafe280
A
371 continue;
372
373 procargs = malloc(argmax);
374 if (procargs == NULL)
375 continue;
376
377 mib[0] = CTL_KERN;
378 mib[1] = KERN_PROCARGS;
e1a085ba
A
379 mib[2] = thispid;
380
9bafe280
A
381 syssize = (size_t)argmax;
382 if (sysctl(mib, 3, procargs, &syssize, NULL, 0) == -1) {
383 free(procargs);
384 continue;
385 }
386
387 for (cp = procargs; cp < &procargs[syssize]; cp++) {
388 if (*cp == '\0') {
389 break;
390 }
391 }
e1a085ba 392
9bafe280
A
393 if (cp == &procargs[syssize]) {
394 free(procargs);
395 continue;
396 }
397
398 for (; cp < &procargs[syssize]; cp++) {
399 if (*cp != '\0') {
400 break;
401 }
402 }
403
404 if (cp == &procargs[syssize]) {
405 free(procargs);
406 continue;
407 }
408
409 /* Strip off any path that was specified */
410 for (thiscmd = cp; (cp < &procargs[syssize]) && (*cp != '\0'); cp++) {
e1a085ba
A
411 if (*cp == '/') {
412 thiscmd = cp + 1;
9bafe280
A
413 }
414 }
9bafe280 415
c0fcf4e1 416 thistdev = procs[i].kp_eproc.e_tdev;
e1a085ba
A
417#else /* !__APPLE__ */
418 if ((procs[i].ki_stat & SZOMB) == SZOMB && !zflag)
419 continue;
420 thispid = procs[i].ki_pid;
421 strncpy(thiscmd, procs[i].ki_comm, MAXCOMLEN);
422 thiscmd[MAXCOMLEN] = '\0';
423 thistdev = procs[i].ki_tdev;
424#endif /* __APPLE__ */
425#ifndef __APPLE__
426 if (eflag)
427 thisuid = procs[i].ki_uid; /* effective uid */
428 else
429 thisuid = procs[i].ki_ruid; /* real uid */
430#endif /* !__APPLE__ */
c0fcf4e1 431
e1a085ba
A
432 if (thispid == mypid) {
433#ifdef __APPLE__
434 free(procargs);
435#endif /* __APPLE__ */
436 continue;
437 }
c0fcf4e1 438 matched = 1;
e1a085ba 439#ifndef __APPLE__
c0fcf4e1
A
440 if (user) {
441 if (thisuid != uid)
442 matched = 0;
443 }
e1a085ba 444#endif /* !__APPLE__ */
c0fcf4e1
A
445 if (tty) {
446 if (thistdev != tdev)
447 matched = 0;
448 }
449 if (cmd) {
450 if (mflag) {
451 if (regcomp(&rgx, cmd,
452 REG_EXTENDED|REG_NOSUB) != 0) {
453 mflag = 0;
454 warnx("%s: illegal regexp", cmd);
455 }
456 }
457 if (mflag) {
458 pmatch.rm_so = 0;
459 pmatch.rm_eo = strlen(thiscmd);
460 if (regexec(&rgx, thiscmd, 0, &pmatch,
461 REG_STARTEND) != 0)
462 matched = 0;
463 regfree(&rgx);
464 } else {
465 if (strncmp(thiscmd, cmd, MAXCOMLEN) != 0)
466 matched = 0;
467 }
468 }
e1a085ba
A
469#ifndef __APPLE__
470 if (jflag && thispid == getpid())
471 matched = 0;
472#endif /* !__APPLE__ */
9bafe280 473 if (matched == 0) {
e1a085ba 474#ifdef __APPLE__
9bafe280 475 free(procargs);
e1a085ba 476#endif /* !__APPLE__ */
c0fcf4e1 477 continue;
9bafe280 478 }
c0fcf4e1
A
479 if (ac > 0)
480 matched = 0;
481 for (j = 0; j < ac; j++) {
482 if (mflag) {
483 if (regcomp(&rgx, av[j],
484 REG_EXTENDED|REG_NOSUB) != 0) {
485 mflag = 0;
486 warnx("%s: illegal regexp", av[j]);
487 }
488 }
489 if (mflag) {
490 pmatch.rm_so = 0;
491 pmatch.rm_eo = strlen(thiscmd);
492 if (regexec(&rgx, thiscmd, 0, &pmatch,
493 REG_STARTEND) == 0)
494 matched = 1;
495 regfree(&rgx);
496 } else {
497 if (strcmp(thiscmd, av[j]) == 0)
498 matched = 1;
499 }
500 if (matched)
501 break;
502 }
9bafe280 503 if (matched == 0) {
e1a085ba 504#ifdef __APPLE__
9bafe280 505 free(procargs);
e1a085ba 506#endif /* __APPLE__ */
c0fcf4e1 507 continue;
9bafe280 508 }
c0fcf4e1 509 if (dflag)
e1a085ba
A
510#ifdef __APPLE__
511 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x\n", sig,
512 thiscmd, thispid, thistdev);
513#else /* !__APPLE__ */
c0fcf4e1
A
514 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
515 thiscmd, thispid, thistdev, thisuid);
e1a085ba 516#endif /* __APPLE__ */
c0fcf4e1
A
517
518 if (vflag || sflag)
519 printf("kill -%s %d\n", upper(sys_signame[sig]),
520 thispid);
521
522 killed++;
523 if (!dflag && !sflag) {
524 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
e1a085ba
A
525 warn("warning: kill -%s %d",
526 upper(sys_signame[sig]), thispid);
c0fcf4e1
A
527 errors = 1;
528 }
529 }
e1a085ba
A
530#ifdef __APPLE__
531 free(procargs);
532#endif /* __APPLE__ */
c0fcf4e1
A
533 }
534 if (killed == 0) {
535 fprintf(stderr, "No matching processes %swere found\n",
536 getuid() != 0 ? "belonging to you " : "");
537 errors = 1;
538 }
539 exit(errors);
540}