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
27 * $FreeBSD: src/usr.bin/killall/killall.c,v 1.5.2.4 2001/05/19 19:22:49 phk Exp $
28 * $DragonFly: src/usr.bin/killall/killall.c,v 1.9 2007/02/01 10:33:26 corecode Exp $
32 #include <sys/param.h>
34 #include <sys/sysctl.h>
54 fprintf(stderr, "usage: %s [-l] [-v] [-m] [-sig] "
55 "[-u user] [-j jail] [-t tty] "
56 "[-c cmd] [cmd]...\n", prog);
57 fprintf(stderr, "At least one option or argument to specify "
58 "processes must be given.\n");
63 upper(const char *str)
68 strlcpy(buf, str, sizeof(buf));
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;
117 int jflag = 0, jailid = 0;
123 char thiscmd[MAXCOMLEN + 1];
128 const char *const *p;
143 if (strcmp(*av, "-l") == 0) {
147 if (strcmp(*av, "-help") == 0)
180 jailid = strtonum(*av, 1, INT_MAX, &errstr);
183 errx(1, "jail id is %s: %s", errstr, *av);
203 if (isalpha((unsigned char)**av)) {
204 if (strncasecmp(*av, "sig", 3) == 0)
206 for (sig = NSIG, p = sys_signame + 1;
208 if (strcasecmp(*p, *av) == 0) {
209 sig = p - sys_signame;
214 } else if (isdigit((unsigned char)**av)) {
215 sig = strtol(*av, &ep, 10);
217 errx(1, "illegal signal number: %s", *av);
218 if (sig < 0 || sig >= NSIG)
230 if (user == NULL && tty == NULL && cmd == NULL && jflag == 0 && ac == 0)
234 if (strncmp(tty, "/dev/", 5) == 0)
235 snprintf(buf, sizeof(buf), "%s", tty);
236 else if (strncmp(tty, "tty", 3) == 0)
237 snprintf(buf, sizeof(buf), "/dev/%s", tty);
238 else if (isdigit(tty[0]))
239 snprintf(buf, sizeof(buf), "/dev/pts/%s", tty);
241 snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
242 if (stat(buf, &sb) < 0)
243 err(1, "stat(%s)", buf);
244 if (!S_ISCHR(sb.st_mode))
245 errx(1, "%s: not a character device", buf);
248 printf("ttydev:0x%x\n", tdev);
253 errx(1, "user %s does not exist", user);
256 printf("uid:%d\n", uid);
264 printf("uid:%d\n", uid);
270 mib[2] = KERN_PROC_ALL;
274 if (user && mib[2] == KERN_PROC_ALL) {
275 mib[2] = KERN_PROC_RUID;
279 if (tty && mib[2] == KERN_PROC_ALL) {
280 mib[2] = KERN_PROC_TTY;
285 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
288 newprocs = realloc(procs, size);
292 errx(1, "could not reallocate memory");
295 st = sysctl(mib, miblen, procs, &size, NULL, 0);
296 } while (st == -1 && errno == ENOMEM);
298 err(1, "could not sysctl(KERN_PROC)");
299 if (size % sizeof(struct kinfo_proc) != 0) {
300 fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
301 size, sizeof(struct kinfo_proc));
302 fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n");
305 nprocs = size / sizeof(struct kinfo_proc);
307 printf("nprocs %d\n", nprocs);
310 for (i = 0; i < nprocs; i++) {
311 thispid = procs[i].kp_pid;
312 strncpy(thiscmd, procs[i].kp_comm, MAXCOMLEN);
313 thiscmd[MAXCOMLEN] = '\0';
314 thistdev = procs[i].kp_tdev;
315 thisuid = procs[i].kp_ruid; /* real uid */
317 if (thispid == mypid)
320 if ((int)procs[i].kp_pid < 0)
327 if (thistdev != tdev)
331 if (procs[i].kp_jailid != jailid)
336 if (regcomp(&rgx, cmd,
337 REG_EXTENDED|REG_NOSUB) != 0) {
339 warnx("%s: illegal regexp", cmd);
344 pmatch.rm_eo = strlen(thiscmd);
345 if (regexec(&rgx, thiscmd, 0, &pmatch,
350 if (strcmp(thiscmd, cmd) != 0)
358 for (j = 0; j < ac; j++) {
360 if (regcomp(&rgx, av[j],
361 REG_EXTENDED|REG_NOSUB) != 0) {
363 warnx("%s: illegal regexp", av[j]);
368 pmatch.rm_eo = strlen(thiscmd);
369 if (regexec(&rgx, thiscmd, 0, &pmatch,
374 if (strcmp(thiscmd, av[j]) == 0)
384 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
385 thiscmd, thispid, thistdev, thisuid);
388 printf("kill -%s %d\n", upper(sys_signame[sig]),
392 if (!dflag && !sflag) {
393 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
394 warn("kill -%s %d", upper(sys_signame[sig]),
400 if (!qflag && killed == 0) {
401 fprintf(stderr, "No matching processes %swere found\n",
402 getuid() != 0 ? "belonging to you " : "");