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 $
31 #include <sys/param.h>
33 #include <sys/sysctl.h>
53 fprintf(stderr, "usage: %s [-l] [-v] [-m] [-sig] "
54 "[-u user] [-j jail] [-t tty] "
55 "[-c cmd] [cmd]...\n", prog);
56 fprintf(stderr, "At least one option or argument to specify "
57 "processes must be given.\n");
62 upper(const char *str)
67 strlcpy(buf, str, sizeof(buf));
68 for (s = buf; *s; s++)
69 *s = toupper((unsigned char)*s);
77 const char *const * p;
81 for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
82 offset += fprintf(fp, "%s ", upper(*p));
83 if (offset >= 75 && cnt > 1) {
95 warnx("unknown signal %s; valid signals:", name);
101 main(int ac, char **av)
103 struct kinfo_proc *procs = NULL, *newprocs;
116 int jflag = 0, jailid = 0;
122 char thiscmd[MAXCOMLEN + 1];
127 const char *const *p;
142 if (strcmp(*av, "-l") == 0) {
146 if (strcmp(*av, "-help") == 0)
179 jailid = strtonum(*av, 1, INT_MAX, &errstr);
182 errx(1, "jail id is %s: %s", errstr, *av);
202 if (isalpha((unsigned char)**av)) {
203 if (strncasecmp(*av, "sig", 3) == 0)
205 for (sig = NSIG, p = sys_signame + 1;
207 if (strcasecmp(*p, *av) == 0) {
208 sig = p - sys_signame;
213 } else if (isdigit((unsigned char)**av)) {
214 sig = strtol(*av, &ep, 10);
216 errx(1, "illegal signal number: %s", *av);
217 if (sig < 0 || sig >= NSIG)
229 if (user == NULL && tty == NULL && cmd == NULL && jflag == 0 && ac == 0)
233 if (strncmp(tty, "/dev/", 5) == 0)
234 snprintf(buf, sizeof(buf), "%s", tty);
235 else if (strncmp(tty, "tty", 3) == 0)
236 snprintf(buf, sizeof(buf), "/dev/%s", tty);
237 else if (isdigit(tty[0]))
238 snprintf(buf, sizeof(buf), "/dev/pts/%s", tty);
240 snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
241 if (stat(buf, &sb) < 0)
242 err(1, "stat(%s)", buf);
243 if (!S_ISCHR(sb.st_mode))
244 errx(1, "%s: not a character device", buf);
247 printf("ttydev:0x%x\n", tdev);
252 errx(1, "user %s does not exist", user);
255 printf("uid:%d\n", uid);
263 printf("uid:%d\n", uid);
269 mib[2] = KERN_PROC_ALL;
273 if (user && mib[2] == KERN_PROC_ALL) {
274 mib[2] = KERN_PROC_RUID;
278 if (tty && mib[2] == KERN_PROC_ALL) {
279 mib[2] = KERN_PROC_TTY;
284 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
287 newprocs = realloc(procs, size);
288 if (newprocs == NULL) {
291 errx(1, "could not reallocate memory");
294 st = sysctl(mib, miblen, procs, &size, NULL, 0);
295 } while (st == -1 && errno == ENOMEM);
297 err(1, "could not sysctl(KERN_PROC)");
298 if (size % sizeof(struct kinfo_proc) != 0) {
299 fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
300 size, sizeof(struct kinfo_proc));
301 fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n");
304 nprocs = size / sizeof(struct kinfo_proc);
306 printf("nprocs %d\n", nprocs);
309 for (i = 0; i < nprocs; i++) {
310 thispid = procs[i].kp_pid;
311 strncpy(thiscmd, procs[i].kp_comm, MAXCOMLEN);
312 thiscmd[MAXCOMLEN] = '\0';
313 thistdev = procs[i].kp_tdev;
314 thisuid = procs[i].kp_ruid; /* real uid */
316 if (thispid == mypid)
319 if ((int)procs[i].kp_pid < 0)
326 if (thistdev != tdev)
330 if (procs[i].kp_jailid != jailid)
335 if (regcomp(&rgx, cmd,
336 REG_EXTENDED|REG_NOSUB) != 0) {
338 warnx("%s: illegal regexp", cmd);
343 pmatch.rm_eo = strlen(thiscmd);
344 if (regexec(&rgx, thiscmd, 0, &pmatch,
349 if (strcmp(thiscmd, cmd) != 0)
357 for (j = 0; j < ac; j++) {
359 if (regcomp(&rgx, av[j],
360 REG_EXTENDED|REG_NOSUB) != 0) {
362 warnx("%s: illegal regexp", av[j]);
367 pmatch.rm_eo = strlen(thiscmd);
368 if (regexec(&rgx, thiscmd, 0, &pmatch,
373 if (strcmp(thiscmd, av[j]) == 0)
383 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
384 thiscmd, thispid, thistdev, thisuid);
387 printf("kill -%s %d\n", upper(sys_signame[sig]),
391 if (!dflag && !sflag) {
392 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
393 warn("kill -%s %d", upper(sys_signame[sig]),
399 if (!qflag && killed == 0) {
400 fprintf(stderr, "No matching processes %swere found\n",
401 getuid() != 0 ? "belonging to you " : "");