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 $
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
35 #include <sys/sysctl.h>
55 fprintf(stderr, "usage: %s [-l] [-v] [-m] [-sig] [-u user] [-j jail] [-t tty] [-c cmd] [cmd]...\n", prog);
56 fprintf(stderr, "At least one option or argument to specify processes must be given.\n");
61 upper(const char *str)
66 strlcpy(buf, str, sizeof(buf));
67 for (s = buf; *s; s++)
68 *s = toupper((unsigned char)*s);
76 const char *const * p;
80 for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
81 offset += fprintf(fp, "%s ", upper(*p));
82 if (offset >= 75 && cnt > 1) {
94 warnx("unknown signal %s; valid signals:", name);
100 main(int ac, char **av)
102 struct kinfo_proc *procs = NULL, *newprocs;
115 int jflag = 0, jailid = 0;
121 char thiscmd[MAXCOMLEN + 1];
126 const char *const *p;
141 if (strcmp(*av, "-l") == 0) {
145 if (strcmp(*av, "-help") == 0)
178 jailid = strtonum(*av, 1, INT_MAX, &errstr);
181 errx(1, "jail id is %s: %s", errstr, *av);
201 if (isalpha((unsigned char)**av)) {
202 if (strncasecmp(*av, "sig", 3) == 0)
204 for (sig = NSIG, p = sys_signame + 1;
206 if (strcasecmp(*p, *av) == 0) {
207 sig = p - sys_signame;
212 } else if (isdigit((unsigned char)**av)) {
213 sig = strtol(*av, &ep, 10);
215 errx(1, "illegal signal number: %s", *av);
216 if (sig < 0 || sig >= NSIG)
228 if (user == NULL && tty == NULL && cmd == NULL && jflag == 0 && ac == 0)
232 if (strncmp(tty, "/dev/", 5) == 0)
233 snprintf(buf, sizeof(buf), "%s", tty);
234 else if (strncmp(tty, "tty", 3) == 0)
235 snprintf(buf, sizeof(buf), "/dev/%s", tty);
237 snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
238 if (stat(buf, &sb) < 0)
239 err(1, "stat(%s)", buf);
240 if (!S_ISCHR(sb.st_mode))
241 errx(1, "%s: not a character device", buf);
244 printf("ttydev:0x%x\n", tdev);
249 errx(1, "user %s does not exist", user);
252 printf("uid:%d\n", uid);
260 printf("uid:%d\n", uid);
266 mib[2] = KERN_PROC_ALL;
270 if (user && mib[2] == KERN_PROC_ALL) {
271 mib[2] = KERN_PROC_RUID;
275 if (tty && mib[2] == KERN_PROC_ALL) {
276 mib[2] = KERN_PROC_TTY;
281 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
284 newprocs = realloc(procs, size);
288 errx(1, "could not reallocate memory");
291 st = sysctl(mib, miblen, procs, &size, NULL, 0);
292 } while (st == -1 && errno == ENOMEM);
294 err(1, "could not sysctl(KERN_PROC)");
295 if (size % sizeof(struct kinfo_proc) != 0) {
296 fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
297 size, sizeof(struct kinfo_proc));
298 fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n");
301 nprocs = size / sizeof(struct kinfo_proc);
303 printf("nprocs %d\n", nprocs);
306 for (i = 0; i < nprocs; i++) {
307 thispid = procs[i].kp_pid;
308 strncpy(thiscmd, procs[i].kp_comm, MAXCOMLEN);
309 thiscmd[MAXCOMLEN] = '\0';
310 thistdev = procs[i].kp_tdev;
311 thisuid = procs[i].kp_ruid; /* real uid */
313 if (thispid == mypid)
316 if ((int)procs[i].kp_pid < 0)
323 if (thistdev != tdev)
327 if (procs[i].kp_jailid != jailid)
332 if (regcomp(&rgx, cmd,
333 REG_EXTENDED|REG_NOSUB) != 0) {
335 warnx("%s: illegal regexp", cmd);
340 pmatch.rm_eo = strlen(thiscmd);
341 if (regexec(&rgx, thiscmd, 0, &pmatch,
346 if (strcmp(thiscmd, cmd) != 0)
354 for (j = 0; j < ac; j++) {
356 if (regcomp(&rgx, av[j],
357 REG_EXTENDED|REG_NOSUB) != 0) {
359 warnx("%s: illegal regexp", av[j]);
364 pmatch.rm_eo = strlen(thiscmd);
365 if (regexec(&rgx, thiscmd, 0, &pmatch,
370 if (strcmp(thiscmd, av[j]) == 0)
380 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
381 thiscmd, thispid, thistdev, thisuid);
384 printf("kill -%s %d\n", upper(sys_signame[sig]),
388 if (!dflag && !sflag) {
389 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
390 warn("kill -%s %d", upper(sys_signame[sig]),
396 if (!qflag && killed == 0) {
397 fprintf(stderr, "No matching processes %swere found\n",
398 getuid() != 0 ? "belonging to you " : "");