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>
56 fprintf(stderr, "usage: killall [-delmsvz] [-help] [-j jail]\n");
58 " [-u user] [-t tty] [-c cmd] [-SIGNAL] [cmd]...\n");
59 fprintf(stderr, "At least one option or argument to specify processes must be given.\n");
64 upper(const char *str)
69 strlcpy(buf, str, sizeof(buf));
70 for (s = buf; *s; s++)
71 *s = toupper((unsigned char)*s);
79 const char *const * p;
83 for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
84 offset += fprintf(fp, "%s ", upper(*p));
85 if (offset >= 75 && cnt > 1) {
97 warnx("unknown signal %s; valid signals:", name);
103 main(int ac, char **av)
105 struct kinfo_proc *procs, *newprocs;
125 char thiscmd[MAXCOMLEN + 1];
130 const char *const *p;
141 setlocale(LC_ALL, "");
147 if (strcmp(*av, "-l") == 0) {
151 if (strcmp(*av, "-help") == 0)
164 errx(1, "must specify jail");
165 jid = jail_getid(*av);
167 errx(1, "%s", jail_errmsg);
168 if (jail_attach(jid) == -1)
169 err(1, "jail_attach(%d)", jid);
178 errx(1, "must specify user");
188 errx(1, "must specify tty");
198 errx(1, "must specify procname");
220 if (isalpha((unsigned char)**av)) {
221 if (strncasecmp(*av, "sig", 3) == 0)
223 for (sig = NSIG, p = sys_signame + 1;
225 if (strcasecmp(*p, *av) == 0) {
226 sig = p - sys_signame;
231 } else if (isdigit((unsigned char)**av)) {
232 sig = strtol(*av, &ep, 10);
234 errx(1, "illegal signal number: %s", *av);
235 if (sig < 0 || sig >= NSIG)
247 if (user == NULL && tty == NULL && cmd == NULL && !jflag && ac == 0)
251 if (strncmp(tty, "/dev/", 5) == 0)
252 snprintf(buf, sizeof(buf), "%s", tty);
253 else if (strncmp(tty, "tty", 3) == 0)
254 snprintf(buf, sizeof(buf), "/dev/%s", tty);
256 snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
257 if (stat(buf, &sb) < 0)
258 err(1, "stat(%s)", buf);
259 if (!S_ISCHR(sb.st_mode))
260 errx(1, "%s: not a character device", buf);
263 printf("ttydev:0x%x\n", tdev);
266 uid = strtol(user, &ep, 10);
267 if (*user == '\0' || *ep != '\0') { /* was it a number? */
270 errx(1, "user %s does not exist", user);
273 printf("uid:%d\n", uid);
282 printf("uid:%d\n", uid);
290 mib[2] = eflag ? KERN_PROC_UID : KERN_PROC_RUID;
294 mib[2] = KERN_PROC_TTY;
298 mib[2] = KERN_PROC_PROC;
304 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
307 newprocs = realloc(procs, size);
308 if (newprocs == NULL) {
310 err(1, "could not reallocate memory");
313 st = sysctl(mib, miblen, procs, &size, NULL, 0);
314 } while (st == -1 && errno == ENOMEM);
316 err(1, "could not sysctl(KERN_PROC)");
317 if (size % sizeof(struct kinfo_proc) != 0) {
318 fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
319 size, sizeof(struct kinfo_proc));
320 fprintf(stderr, "userland out of sync with kernel\n");
323 nprocs = size / sizeof(struct kinfo_proc);
325 printf("nprocs %d\n", nprocs);
328 for (i = 0; i < nprocs; i++) {
329 if (procs[i].ki_stat == SZOMB && !zflag)
331 thispid = procs[i].ki_pid;
332 strlcpy(thiscmd, procs[i].ki_comm, sizeof(thiscmd));
333 thistdev = procs[i].ki_tdev;
335 thisuid = procs[i].ki_uid; /* effective uid */
337 thisuid = procs[i].ki_ruid; /* real uid */
339 if (thispid == mypid)
347 if (thistdev != tdev)
352 if (regcomp(&rgx, cmd,
353 REG_EXTENDED|REG_NOSUB) != 0) {
355 warnx("%s: illegal regexp", cmd);
360 pmatch.rm_eo = strlen(thiscmd);
361 if (regexec(&rgx, thiscmd, 0, &pmatch,
366 if (strncmp(thiscmd, cmd, MAXCOMLEN) != 0)
370 if (jflag && thispid == getpid())
376 for (j = 0; j < ac; j++) {
378 if (regcomp(&rgx, av[j],
379 REG_EXTENDED|REG_NOSUB) != 0) {
381 warnx("%s: illegal regexp", av[j]);
386 pmatch.rm_eo = strlen(thiscmd);
387 if (regexec(&rgx, thiscmd, 0, &pmatch,
392 if (strcmp(thiscmd, av[j]) == 0)
401 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
402 thiscmd, thispid, thistdev, thisuid);
405 printf("kill -%s %d\n", upper(sys_signame[sig]),
409 if (!dflag && !sflag) {
410 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
411 warn("warning: kill -%s %d",
412 upper(sys_signame[sig]), thispid);
418 fprintf(stderr, "No matching processes %swere found\n",
419 getuid() != 0 ? "belonging to you " : "");