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 strncmp(tty, "pts/", 4) == 0)
260 snprintf(buf, sizeof(buf), "/dev/%s", tty);
262 snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
263 if (stat(buf, &sb) < 0)
264 err(1, "stat(%s)", buf);
265 if (!S_ISCHR(sb.st_mode))
266 errx(1, "%s: not a character device", buf);
269 printf("ttydev:0x%jx\n", (uintmax_t)tdev);
272 uid = strtol(user, &ep, 10);
273 if (*user == '\0' || *ep != '\0') { /* was it a number? */
276 errx(1, "user %s does not exist", user);
279 printf("uid:%d\n", uid);
288 printf("uid:%d\n", uid);
296 mib[2] = eflag ? KERN_PROC_UID : KERN_PROC_RUID;
300 mib[2] = KERN_PROC_TTY;
304 mib[2] = KERN_PROC_PROC;
310 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
313 newprocs = realloc(procs, size);
314 if (newprocs == NULL) {
316 err(1, "could not reallocate memory");
319 st = sysctl(mib, miblen, procs, &size, NULL, 0);
320 } while (st == -1 && errno == ENOMEM);
322 err(1, "could not sysctl(KERN_PROC)");
323 if (size % sizeof(struct kinfo_proc) != 0) {
324 fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
325 size, sizeof(struct kinfo_proc));
326 fprintf(stderr, "userland out of sync with kernel\n");
329 nprocs = size / sizeof(struct kinfo_proc);
331 printf("nprocs %d\n", nprocs);
334 for (i = 0; i < nprocs; i++) {
335 if (procs[i].ki_stat == SZOMB && !zflag)
337 thispid = procs[i].ki_pid;
338 strlcpy(thiscmd, procs[i].ki_comm, sizeof(thiscmd));
339 thistdev = procs[i].ki_tdev;
341 thisuid = procs[i].ki_uid; /* effective uid */
343 thisuid = procs[i].ki_ruid; /* real uid */
345 if (thispid == mypid)
353 if (thistdev != tdev)
358 if (regcomp(&rgx, cmd,
359 REG_EXTENDED|REG_NOSUB) != 0) {
361 warnx("%s: illegal regexp", cmd);
366 pmatch.rm_eo = strlen(thiscmd);
367 if (regexec(&rgx, thiscmd, 0, &pmatch,
372 if (strncmp(thiscmd, cmd, MAXCOMLEN) != 0)
376 if (jflag && thispid == getpid())
382 for (j = 0; j < ac; j++) {
384 if (regcomp(&rgx, av[j],
385 REG_EXTENDED|REG_NOSUB) != 0) {
387 warnx("%s: illegal regexp", av[j]);
392 pmatch.rm_eo = strlen(thiscmd);
393 if (regexec(&rgx, thiscmd, 0, &pmatch,
398 if (strcmp(thiscmd, av[j]) == 0)
404 if (matched != 0 && Iflag) {
405 printf("Send signal %d to %s (pid %d uid %d)? ",
406 sig, thiscmd, thispid, thisuid);
408 first = ch = getchar();
409 while (ch != '\n' && ch != EOF)
411 if (first != 'y' && first != 'Y')
417 printf("sig:%d, cmd:%s, pid:%d, dev:0x%jx uid:%d\n",
418 sig, thiscmd, thispid, (uintmax_t)thistdev,
422 printf("kill -%s %d\n", sys_signame[sig], thispid);
425 if (!dflag && !sflag) {
426 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
427 warn("warning: kill -%s %d",
428 sys_signame[sig], thispid);
435 fprintf(stderr, "No matching processes %swere found\n",
436 getuid() != 0 ? "belonging to you " : "");