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>
36 #include <sys/sysctl.h>
57 fprintf(stderr, "usage: killall [-delmsqvz] [-help] [-I] [-j jail]\n");
59 " [-u user] [-t tty] [-c cmd] [-SIGNAL] [cmd]...\n");
60 fprintf(stderr, "At least one option or argument to specify processes must be given.\n");
68 const char *const * p;
72 for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
73 offset += fprintf(fp, "%s ", *p);
74 if (offset >= 75 && cnt > 1) {
86 warnx("unknown signal %s; valid signals:", name);
92 main(int ac, char **av)
95 struct kinfo_proc *procs, *newprocs;
118 char thiscmd[MAXCOMLEN + 1];
123 const char *const *p;
134 setlocale(LC_ALL, "");
140 if (strcmp(*av, "-l") == 0) {
144 if (strcmp(*av, "-help") == 0)
157 errx(1, "must specify jail");
158 jid = jail_getid(*av);
160 errx(1, "%s", jail_errmsg);
161 if (jail_attach(jid) == -1)
162 err(1, "jail_attach(%d)", jid);
171 errx(1, "must specify user");
181 errx(1, "must specify tty");
191 errx(1, "must specify procname");
217 if (isalpha((unsigned char)**av)) {
218 if (strncasecmp(*av, "SIG", 3) == 0)
220 for (sig = NSIG, p = sys_signame + 1;
222 if (strcasecmp(*p, *av) == 0) {
223 sig = p - sys_signame;
227 if (**saved_av == 'I') {
234 } else if (isdigit((unsigned char)**av)) {
235 sig = strtol(*av, &ep, 10);
237 errx(1, "illegal signal number: %s", *av);
238 if (sig < 0 || sig >= NSIG)
250 if (user == NULL && tty == NULL && cmd == NULL && !jflag && ac == 0)
254 if (strncmp(tty, "/dev/", 5) == 0)
255 snprintf(buf, sizeof(buf), "%s", tty);
256 else if (strncmp(tty, "tty", 3) == 0)
257 snprintf(buf, sizeof(buf), "/dev/%s", tty);
259 snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
260 if (stat(buf, &sb) < 0)
261 err(1, "stat(%s)", buf);
262 if (!S_ISCHR(sb.st_mode))
263 errx(1, "%s: not a character device", buf);
266 printf("ttydev:0x%jx\n", (uintmax_t)tdev);
269 uid = strtol(user, &ep, 10);
270 if (*user == '\0' || *ep != '\0') { /* was it a number? */
273 errx(1, "user %s does not exist", user);
276 printf("uid:%d\n", uid);
285 printf("uid:%d\n", uid);
293 mib[2] = eflag ? KERN_PROC_UID : KERN_PROC_RUID;
297 mib[2] = KERN_PROC_TTY;
301 mib[2] = KERN_PROC_PROC;
307 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
310 newprocs = realloc(procs, size);
311 if (newprocs == NULL) {
313 err(1, "could not reallocate memory");
316 st = sysctl(mib, miblen, procs, &size, NULL, 0);
317 } while (st == -1 && errno == ENOMEM);
319 err(1, "could not sysctl(KERN_PROC)");
320 if (size % sizeof(struct kinfo_proc) != 0) {
321 fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
322 size, sizeof(struct kinfo_proc));
323 fprintf(stderr, "userland out of sync with kernel\n");
326 nprocs = size / sizeof(struct kinfo_proc);
328 printf("nprocs %d\n", nprocs);
331 for (i = 0; i < nprocs; i++) {
332 if (procs[i].ki_stat == SZOMB && !zflag)
334 thispid = procs[i].ki_pid;
335 strlcpy(thiscmd, procs[i].ki_comm, sizeof(thiscmd));
336 thistdev = procs[i].ki_tdev;
338 thisuid = procs[i].ki_uid; /* effective uid */
340 thisuid = procs[i].ki_ruid; /* real uid */
342 if (thispid == mypid)
350 if (thistdev != tdev)
355 if (regcomp(&rgx, cmd,
356 REG_EXTENDED|REG_NOSUB) != 0) {
358 warnx("%s: illegal regexp", cmd);
363 pmatch.rm_eo = strlen(thiscmd);
364 if (regexec(&rgx, thiscmd, 0, &pmatch,
369 if (strncmp(thiscmd, cmd, MAXCOMLEN) != 0)
373 if (jflag && thispid == getpid())
379 for (j = 0; j < ac; j++) {
381 if (regcomp(&rgx, av[j],
382 REG_EXTENDED|REG_NOSUB) != 0) {
384 warnx("%s: illegal regexp", av[j]);
389 pmatch.rm_eo = strlen(thiscmd);
390 if (regexec(&rgx, thiscmd, 0, &pmatch,
395 if (strcmp(thiscmd, av[j]) == 0)
401 if (matched != 0 && Iflag) {
402 printf("Send signal %d to %s (pid %d uid %d)? ",
403 sig, thiscmd, thispid, thisuid);
405 first = ch = getchar();
406 while (ch != '\n' && ch != EOF)
408 if (first != 'y' && first != 'Y')
414 printf("sig:%d, cmd:%s, pid:%d, dev:0x%jx uid:%d\n",
415 sig, thiscmd, thispid, (uintmax_t)thistdev,
419 printf("kill -%s %d\n", sys_signame[sig], thispid);
422 if (!dflag && !sflag) {
423 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
424 warn("warning: kill -%s %d",
425 sys_signame[sig], thispid);
432 fprintf(stderr, "No matching processes %swere found\n",
433 getuid() != 0 ? "belonging to you " : "");