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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
35 #include <sys/sysctl.h>
54 fprintf(stderr, "usage: killall [-delmsvz] [-help] [-j jid]\n");
56 " [-u user] [-t tty] [-c cmd] [-SIGNAL] [cmd]...\n");
57 fprintf(stderr, "At least one option or argument to specify processes must be given.\n");
62 upper(const char *str)
67 strlcpy(buf, str, sizeof(buf));
68 for (s = buf; *s; s++)
69 *s = toupper((unsigned char)*s);
77 const char *const * p;
81 for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
82 offset += fprintf(fp, "%s ", upper(*p));
83 if (offset >= 75 && cnt > 1) {
95 warnx("unknown signal %s; valid signals:", name);
101 main(int ac, char **av)
103 struct kinfo_proc *procs = NULL, *newprocs;
123 char thiscmd[MAXCOMLEN + 1];
128 const char *const *p;
139 setlocale(LC_ALL, "");
145 if (strcmp(*av, "-l") == 0) {
149 if (strcmp(*av, "-help") == 0)
162 errx(1, "must specify jid");
163 jid = strtol(*av, &ep, 10);
165 errx(1, "illegal jid: %s", *av);
166 if (jail_attach(jid) == -1)
167 err(1, "jail_attach(): %d", jid);
176 errx(1, "must specify user");
186 errx(1, "must specify tty");
196 errx(1, "must specify procname");
218 if (isalpha((unsigned char)**av)) {
219 if (strncasecmp(*av, "sig", 3) == 0)
221 for (sig = NSIG, p = sys_signame + 1;
223 if (strcasecmp(*p, *av) == 0) {
224 sig = p - sys_signame;
229 } else if (isdigit((unsigned char)**av)) {
230 sig = strtol(*av, &ep, 10);
232 errx(1, "illegal signal number: %s", *av);
233 if (sig < 0 || sig >= NSIG)
245 if (user == NULL && tty == NULL && cmd == NULL && !jflag && ac == 0)
249 if (strncmp(tty, "/dev/", 5) == 0)
250 snprintf(buf, sizeof(buf), "%s", tty);
251 else if (strncmp(tty, "tty", 3) == 0)
252 snprintf(buf, sizeof(buf), "/dev/%s", tty);
254 snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
255 if (stat(buf, &sb) < 0)
256 err(1, "stat(%s)", buf);
257 if (!S_ISCHR(sb.st_mode))
258 errx(1, "%s: not a character device", buf);
261 printf("ttydev:0x%x\n", tdev);
264 uid = strtol(user, &ep, 10);
265 if (*user == '\0' || *ep != '\0') { /* was it a number? */
268 errx(1, "user %s does not exist", user);
271 printf("uid:%d\n", uid);
280 printf("uid:%d\n", uid);
286 mib[2] = KERN_PROC_PROC;
291 mib[2] = eflag ? KERN_PROC_UID : KERN_PROC_RUID;
295 mib[2] = KERN_PROC_TTY;
300 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
303 newprocs = realloc(procs, size);
307 errx(1, "could not reallocate memory");
310 st = sysctl(mib, miblen, procs, &size, NULL, 0);
311 } while (st == -1 && errno == ENOMEM);
313 err(1, "could not sysctl(KERN_PROC)");
314 if (size % sizeof(struct kinfo_proc) != 0) {
315 fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
316 size, sizeof(struct kinfo_proc));
317 fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n");
320 nprocs = size / sizeof(struct kinfo_proc);
322 printf("nprocs %d\n", nprocs);
325 for (i = 0; i < nprocs; i++) {
326 if ((procs[i].ki_stat & SZOMB) == SZOMB && !zflag)
328 thispid = procs[i].ki_pid;
329 strlcpy(thiscmd, procs[i].ki_comm, sizeof(thiscmd));
330 thistdev = procs[i].ki_tdev;
332 thisuid = procs[i].ki_uid; /* effective uid */
334 thisuid = procs[i].ki_ruid; /* real uid */
336 if (thispid == mypid)
344 if (thistdev != tdev)
349 if (regcomp(&rgx, cmd,
350 REG_EXTENDED|REG_NOSUB) != 0) {
352 warnx("%s: illegal regexp", cmd);
357 pmatch.rm_eo = strlen(thiscmd);
358 if (regexec(&rgx, thiscmd, 0, &pmatch,
363 if (strncmp(thiscmd, cmd, MAXCOMLEN) != 0)
367 if (jflag && thispid == getpid())
373 for (j = 0; j < ac; j++) {
375 if (regcomp(&rgx, av[j],
376 REG_EXTENDED|REG_NOSUB) != 0) {
378 warnx("%s: illegal regexp", av[j]);
383 pmatch.rm_eo = strlen(thiscmd);
384 if (regexec(&rgx, thiscmd, 0, &pmatch,
389 if (strcmp(thiscmd, av[j]) == 0)
398 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
399 thiscmd, thispid, thistdev, thisuid);
402 printf("kill -%s %d\n", upper(sys_signame[sig]),
406 if (!dflag && !sflag) {
407 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
408 warn("warning: kill -%s %d",
409 upper(sys_signame[sig]), thispid);
415 fprintf(stderr, "No matching processes %swere found\n",
416 getuid() != 0 ? "belonging to you " : "");