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 strncpy(buf, str, sizeof(buf));
68 buf[sizeof(buf) - 1] = '\0';
69 for (s = buf; *s; s++)
70 *s = toupper((unsigned char)*s);
78 const char *const * p;
82 for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
83 offset += fprintf(fp, "%s ", upper(*p));
84 if (offset >= 75 && cnt > 1) {
96 warnx("unknown signal %s; valid signals:", name);
102 main(int ac, char **av)
104 struct kinfo_proc *procs = NULL, *newprocs;
124 char thiscmd[MAXCOMLEN + 1];
129 const char *const *p;
140 setlocale(LC_ALL, "");
146 if (strcmp(*av, "-l") == 0) {
150 if (strcmp(*av, "-help") == 0)
163 errx(1, "must specify jid");
164 jid = strtol(*av, &ep, 10);
166 errx(1, "illegal jid: %s", *av);
167 if (jail_attach(jid) == -1)
168 err(1, "jail_attach(): %d", jid);
177 errx(1, "must specify user");
187 errx(1, "must specify tty");
197 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;
230 } else if (isdigit((unsigned char)**av)) {
231 sig = strtol(*av, &ep, 10);
233 errx(1, "illegal signal number: %s", *av);
234 if (sig < 0 || sig >= NSIG)
246 if (user == NULL && tty == NULL && cmd == NULL && !jflag && ac == 0)
250 if (strncmp(tty, "/dev/", 5) == 0)
251 snprintf(buf, sizeof(buf), "%s", tty);
252 else if (strncmp(tty, "tty", 3) == 0)
253 snprintf(buf, sizeof(buf), "/dev/%s", tty);
255 snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
256 if (stat(buf, &sb) < 0)
257 err(1, "stat(%s)", buf);
258 if (!S_ISCHR(sb.st_mode))
259 errx(1, "%s: not a character device", buf);
262 printf("ttydev:0x%x\n", tdev);
265 uid = strtol(user, &ep, 10);
266 if (*user == '\0' || *ep != '\0') { /* was it a number? */
269 errx(1, "user %s does not exist", user);
272 printf("uid:%d\n", uid);
281 printf("uid:%d\n", uid);
287 mib[2] = KERN_PROC_PROC;
292 mib[2] = eflag ? KERN_PROC_UID : KERN_PROC_RUID;
296 mib[2] = KERN_PROC_TTY;
301 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
304 newprocs = realloc(procs, size);
308 errx(1, "could not reallocate memory");
311 st = sysctl(mib, miblen, procs, &size, NULL, 0);
312 } while (st == -1 && errno == ENOMEM);
314 err(1, "could not sysctl(KERN_PROC)");
315 if (size % sizeof(struct kinfo_proc) != 0) {
316 fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
317 size, sizeof(struct kinfo_proc));
318 fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n");
321 nprocs = size / sizeof(struct kinfo_proc);
323 printf("nprocs %d\n", nprocs);
326 for (i = 0; i < nprocs; i++) {
327 if ((procs[i].ki_stat & SZOMB) == SZOMB && !zflag)
329 thispid = procs[i].ki_pid;
330 strncpy(thiscmd, procs[i].ki_comm, MAXCOMLEN);
331 thiscmd[MAXCOMLEN] = '\0';
332 thistdev = procs[i].ki_tdev;
334 thisuid = procs[i].ki_uid; /* effective uid */
336 thisuid = procs[i].ki_ruid; /* real uid */
338 if (thispid == mypid)
346 if (thistdev != tdev)
351 if (regcomp(&rgx, cmd,
352 REG_EXTENDED|REG_NOSUB) != 0) {
354 warnx("%s: illegal regexp", cmd);
359 pmatch.rm_eo = strlen(thiscmd);
360 if (regexec(&rgx, thiscmd, 0, &pmatch,
365 if (strncmp(thiscmd, cmd, MAXCOMLEN) != 0)
369 if (jflag && thispid == getpid())
375 for (j = 0; j < ac; j++) {
377 if (regcomp(&rgx, av[j],
378 REG_EXTENDED|REG_NOSUB) != 0) {
380 warnx("%s: illegal regexp", av[j]);
385 pmatch.rm_eo = strlen(thiscmd);
386 if (regexec(&rgx, thiscmd, 0, &pmatch,
391 if (strcmp(thiscmd, av[j]) == 0)
400 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
401 thiscmd, thispid, thistdev, thisuid);
404 printf("kill -%s %d\n", upper(sys_signame[sig]),
408 if (!dflag && !sflag) {
409 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
410 warn("warning: kill -%s %d",
411 upper(sys_signame[sig]), thispid);
417 fprintf(stderr, "No matching processes %swere found\n",
418 getuid() != 0 ? "belonging to you " : "");