2 * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org>
3 * Copyright (c) 2000 Paul Saab <ps@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/usr.bin/killall/killall.c,v 1.5.2.4 2001/05/19 19:22:49 phk Exp $
31 #include <sys/param.h>
33 #include <sys/sysctl.h>
47 #define PHASH_SIZE 1024
51 struct kinfo_proc *proc;
52 struct pchain *parent;
56 static struct pchain *phash[PHASH_SIZE];
58 static struct pchain *saveparents(struct kinfo_proc *procs, int nprocs,
60 static int checkparent(struct pchain *mychain, pid_t pid);
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);
69 fprintf(stderr, "At least one option or argument to specify "
70 "processes must be given.\n");
75 upper(const char *str)
80 strlcpy(buf, str, sizeof(buf));
81 for (s = buf; *s; s++)
82 *s = toupper((unsigned char)*s);
90 const char *const * p;
94 for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
95 offset += fprintf(fp, "%s ", upper(*p));
96 if (offset >= 75 && cnt > 1) {
108 warnx("unknown signal %s; valid signals:", name);
114 main(int ac, char **av)
116 struct kinfo_proc *procs = NULL, *newprocs;
117 struct pchain *mychain;
130 int jflag = 0, jailid = 0;
137 char thiscmd[MAXCOMLEN + 1];
142 const char *const *p;
157 if (strcmp(*av, "-l") == 0) {
161 if (strcmp(*av, "-help") == 0)
198 jailid = strtonum(*av, 1, INT_MAX, &errstr);
201 errx(1, "jail id is %s: %s", errstr, *av);
224 if (isalpha((unsigned char)**av)) {
225 if (strncasecmp(*av, "sig", 3) == 0)
227 for (sig = NSIG, p = sys_signame + 1;
229 if (strcasecmp(*p, *av) == 0) {
230 sig = p - sys_signame;
235 } else if (isdigit((unsigned char)**av)) {
236 sig = strtol(*av, &ep, 10);
238 errx(1, "illegal signal number: %s", *av);
239 if (sig < 0 || sig >= NSIG)
251 if (user == NULL && tty == NULL && cmd == NULL &&
252 jflag == 0 && Tflag == 0 && ac == 0) {
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);
267 else if (isdigit(tty[0]))
268 snprintf(buf, sizeof(buf), "/dev/pts/%s", tty);
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);
277 printf("ttydev:0x%x\n", tdev);
282 errx(1, "user %s does not exist", user);
285 printf("uid:%d\n", uid);
293 printf("uid:%d\n", uid);
299 mib[2] = KERN_PROC_ALL;
303 if (user && mib[2] == KERN_PROC_ALL) {
304 mib[2] = KERN_PROC_RUID;
308 if (tty && mib[2] == KERN_PROC_ALL) {
309 mib[2] = KERN_PROC_TTY;
314 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
317 newprocs = realloc(procs, size);
318 if (newprocs == NULL) {
321 errx(1, "could not reallocate memory");
324 st = sysctl(mib, miblen, procs, &size, NULL, 0);
325 } while (st == -1 && errno == ENOMEM);
327 err(1, "could not sysctl(KERN_PROC)");
328 if (size % sizeof(struct kinfo_proc) != 0) {
329 fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
330 size, sizeof(struct kinfo_proc));
331 fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n");
334 nprocs = size / sizeof(struct kinfo_proc);
336 printf("nprocs %d\n", nprocs);
340 * Record parent chain if Tflag
343 mychain = saveparents(procs, nprocs, mypid);
350 for (i = 0; i < nprocs; i++) {
351 thispid = procs[i].kp_pid;
352 strncpy(thiscmd, procs[i].kp_comm, MAXCOMLEN);
353 thiscmd[MAXCOMLEN] = '\0';
354 thistdev = procs[i].kp_tdev;
355 thisuid = procs[i].kp_ruid; /* real uid */
357 if (thispid == mypid)
360 if ((int)procs[i].kp_pid < 0)
367 if (thistdev != tdev)
369 if (Tflag && checkparent(mychain, procs[i].kp_pid))
373 if (procs[i].kp_jailid != jailid)
378 if (regcomp(&rgx, cmd,
379 REG_EXTENDED|REG_NOSUB) != 0) {
381 warnx("%s: illegal regexp", cmd);
386 pmatch.rm_eo = strlen(thiscmd);
387 if (regexec(&rgx, thiscmd, 0, &pmatch,
392 if (strcmp(thiscmd, cmd) != 0)
400 for (j = 0; j < ac; j++) {
402 if (regcomp(&rgx, av[j],
403 REG_EXTENDED|REG_NOSUB) != 0) {
405 warnx("%s: illegal regexp", av[j]);
410 pmatch.rm_eo = strlen(thiscmd);
411 if (regexec(&rgx, thiscmd, 0, &pmatch,
416 if (strcmp(thiscmd, av[j]) == 0)
426 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
427 thiscmd, thispid, thistdev, thisuid);
430 printf("kill -%s %d\n", upper(sys_signame[sig]),
434 if (!dflag && !sflag) {
435 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
436 warn("kill -%s %d", upper(sys_signame[sig]),
442 if (!qflag && killed == 0) {
443 fprintf(stderr, "No matching processes %swere found\n",
444 getuid() != 0 ? "belonging to you " : "");
453 saveparents(struct kinfo_proc *procs, int nprocs, pid_t mypid)
455 struct pchain *rchain = NULL;
456 struct pchain *chain;
457 struct pchain *pchain;
461 for (i = 0; i < nprocs; ++i) {
462 if ((int)procs[i].kp_pid < 0)
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];
470 if (mypid == procs[i].kp_pid)
473 for (i = 0; i < nprocs; ++i) {
474 if ((int)procs[i].kp_pid < 0)
476 if ((int)procs[i].kp_ppid < 0)
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)
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) {
487 chain->parent = pchain;
497 checkparent(struct pchain *chain, pid_t pid)
500 if (chain->proc->kp_pid == pid)
502 chain = chain->parent;