2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org>
5 * Copyright (c) 2000 Paul Saab <ps@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
38 #include <sys/sysctl.h>
59 fprintf(stderr, "usage: killall [-delmsqvz] [-help] [-I] [-j jail]\n");
61 " [-u user] [-t tty] [-c cmd] [-SIGNAL] [cmd]...\n");
62 fprintf(stderr, "At least one option or argument to specify processes must be given.\n");
70 const char *const * p;
74 for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
75 offset += fprintf(fp, "%s ", *p);
76 if (offset >= 75 && cnt > 1) {
88 warnx("unknown signal %s; valid signals:", name);
94 main(int ac, char **av)
97 struct kinfo_proc *procs, *newprocs;
120 char thiscmd[MAXCOMLEN + 1];
125 const char *const *p;
136 setlocale(LC_ALL, "");
142 if (strcmp(*av, "-l") == 0) {
146 if (strcmp(*av, "-help") == 0)
159 errx(1, "must specify jail");
160 jid = jail_getid(*av);
162 errx(1, "%s", jail_errmsg);
163 if (jail_attach(jid) == -1)
164 err(1, "jail_attach(%d)", jid);
173 errx(1, "must specify user");
183 errx(1, "must specify tty");
193 errx(1, "must specify procname");
219 if (isalpha((unsigned char)**av)) {
220 if (strncasecmp(*av, "SIG", 3) == 0)
222 for (sig = NSIG, p = sys_signame + 1;
224 if (strcasecmp(*p, *av) == 0) {
225 sig = p - sys_signame;
229 if (**saved_av == 'I') {
236 } else if (isdigit((unsigned char)**av)) {
237 sig = strtol(*av, &ep, 10);
239 errx(1, "illegal signal number: %s", *av);
240 if (sig < 0 || sig >= NSIG)
252 if (user == NULL && tty == NULL && cmd == NULL && !jflag && ac == 0)
256 if (strncmp(tty, "/dev/", 5) == 0)
257 snprintf(buf, sizeof(buf), "%s", tty);
258 else if (strncmp(tty, "tty", 3) == 0)
259 snprintf(buf, sizeof(buf), "/dev/%s", tty);
261 snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
262 if (stat(buf, &sb) < 0)
263 err(1, "stat(%s)", buf);
264 if (!S_ISCHR(sb.st_mode))
265 errx(1, "%s: not a character device", buf);
268 printf("ttydev:0x%jx\n", (uintmax_t)tdev);
271 uid = strtol(user, &ep, 10);
272 if (*user == '\0' || *ep != '\0') { /* was it a number? */
275 errx(1, "user %s does not exist", user);
278 printf("uid:%d\n", uid);
287 printf("uid:%d\n", uid);
295 mib[2] = eflag ? KERN_PROC_UID : KERN_PROC_RUID;
299 mib[2] = KERN_PROC_TTY;
303 mib[2] = KERN_PROC_PROC;
309 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
312 newprocs = realloc(procs, size);
313 if (newprocs == NULL) {
315 err(1, "could not reallocate memory");
318 st = sysctl(mib, miblen, procs, &size, NULL, 0);
319 } while (st == -1 && errno == ENOMEM);
321 err(1, "could not sysctl(KERN_PROC)");
322 if (size % sizeof(struct kinfo_proc) != 0) {
323 fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
324 size, sizeof(struct kinfo_proc));
325 fprintf(stderr, "userland out of sync with kernel\n");
328 nprocs = size / sizeof(struct kinfo_proc);
330 printf("nprocs %d\n", nprocs);
333 for (i = 0; i < nprocs; i++) {
334 if (procs[i].ki_stat == SZOMB && !zflag)
336 thispid = procs[i].ki_pid;
337 strlcpy(thiscmd, procs[i].ki_comm, sizeof(thiscmd));
338 thistdev = procs[i].ki_tdev;
340 thisuid = procs[i].ki_uid; /* effective uid */
342 thisuid = procs[i].ki_ruid; /* real uid */
344 if (thispid == mypid)
352 if (thistdev != tdev)
357 if (regcomp(&rgx, cmd,
358 REG_EXTENDED|REG_NOSUB) != 0) {
360 warnx("%s: illegal regexp", cmd);
365 pmatch.rm_eo = strlen(thiscmd);
366 if (regexec(&rgx, thiscmd, 0, &pmatch,
371 if (strncmp(thiscmd, cmd, MAXCOMLEN) != 0)
375 if (jflag && thispid == getpid())
381 for (j = 0; j < ac; j++) {
383 if (regcomp(&rgx, av[j],
384 REG_EXTENDED|REG_NOSUB) != 0) {
386 warnx("%s: illegal regexp", av[j]);
391 pmatch.rm_eo = strlen(thiscmd);
392 if (regexec(&rgx, thiscmd, 0, &pmatch,
397 if (strcmp(thiscmd, av[j]) == 0)
403 if (matched != 0 && Iflag) {
404 printf("Send signal %d to %s (pid %d uid %d)? ",
405 sig, thiscmd, thispid, thisuid);
407 first = ch = getchar();
408 while (ch != '\n' && ch != EOF)
410 if (first != 'y' && first != 'Y')
416 printf("sig:%d, cmd:%s, pid:%d, dev:0x%jx uid:%d\n",
417 sig, thiscmd, thispid, (uintmax_t)thistdev,
421 printf("kill -%s %d\n", sys_signame[sig], thispid);
424 if (!dflag && !sflag) {
425 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
426 warn("warning: kill -%s %d",
427 sys_signame[sig], thispid);
434 fprintf(stderr, "No matching processes %swere found\n",
435 getuid() != 0 ? "belonging to you " : "");