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