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
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
35 #include <sys/sysctl.h>
55 fprintf(stderr, "usage: %s [-l] [-v] [-m] [-sig] [-u user] [-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 strncpy(buf, str, sizeof(buf));
67 buf[sizeof(buf) - 1] = '\0';
68 for (s = buf; *s; 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;
119 char thiscmd[MAXCOMLEN + 1];
124 const char *const *p;
139 if (strcmp(*av, "-l") == 0) {
143 if (strcmp(*av, "-help") == 0)
183 if (strncasecmp(*av, "sig", 3) == 0)
185 for (sig = NSIG, p = sys_signame + 1;
187 if (strcasecmp(*p, *av) == 0) {
188 sig = p - sys_signame;
193 } else if (isdigit(**av)) {
194 sig = strtol(*av, &ep, 10);
196 errx(1, "illegal signal number: %s", *av);
197 if (sig < 0 || sig > NSIG)
209 if (user == NULL && tty == NULL && cmd == NULL && ac == 0)
213 if (strncmp(tty, "/dev/", 5) == 0)
214 snprintf(buf, sizeof(buf), "%s", tty);
215 else if (strncmp(tty, "tty", 3) == 0)
216 snprintf(buf, sizeof(buf), "/dev/%s", tty);
218 snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
219 if (stat(buf, &sb) < 0)
220 err(1, "stat(%s)", buf);
221 if (!S_ISCHR(sb.st_mode))
222 errx(1, "%s: not a character device", buf);
225 printf("ttydev:0x%x\n", tdev);
228 uid = strtol(user, &ep, 10);
229 if ((ep - user) < strlen(user)) {
232 errx(1, "user %s does not exist", user);
235 printf("uid:%d\n", uid);
244 printf("uid:%d\n", uid);
250 mib[2] = KERN_PROC_ALL;
254 if (user && mib[2] == KERN_PROC_ALL) {
255 mib[2] = KERN_PROC_RUID;
259 if (tty && mib[2] == KERN_PROC_ALL) {
260 mib[2] = KERN_PROC_TTY;
265 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
268 newprocs = realloc(procs, size);
272 errx(1, "could not reallocate memory");
275 st = sysctl(mib, miblen, procs, &size, NULL, 0);
276 } while (st == -1 && errno == ENOMEM);
278 err(1, "could not sysctl(KERN_PROC)");
279 if (size % sizeof(struct kinfo_proc) != 0) {
280 fprintf(stderr, "proc size mismatch (%d total, %d chunks)\n",
281 size, sizeof(struct kinfo_proc));
282 fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n");
285 nprocs = size / sizeof(struct kinfo_proc);
287 printf("nprocs %d\n", nprocs);
289 for (i = 0; i < nprocs; i++) {
290 thispid = procs[i].ki_pid;
291 strncpy(thiscmd, procs[i].ki_comm, MAXCOMLEN);
292 thiscmd[MAXCOMLEN] = '\0';
293 thistdev = procs[i].ki_tdev;
294 thisuid = procs[i].ki_ruid; /* real uid */
302 if (thistdev != tdev)
307 if (regcomp(&rgx, cmd,
308 REG_EXTENDED|REG_NOSUB) != 0) {
310 warnx("%s: illegal regexp", cmd);
315 pmatch.rm_eo = strlen(thiscmd);
316 if (regexec(&rgx, thiscmd, 0, &pmatch,
321 if (strncmp(thiscmd, cmd, MAXCOMLEN) != 0)
329 for (j = 0; j < ac; j++) {
331 if (regcomp(&rgx, av[j],
332 REG_EXTENDED|REG_NOSUB) != 0) {
334 warnx("%s: illegal regexp", av[j]);
339 pmatch.rm_eo = strlen(thiscmd);
340 if (regexec(&rgx, thiscmd, 0, &pmatch,
345 if (strcmp(thiscmd, av[j]) == 0)
354 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
355 thiscmd, thispid, thistdev, thisuid);
358 printf("kill -%s %d\n", upper(sys_signame[sig]),
362 if (!dflag && !sflag) {
363 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
364 warn("kill -%s %d", upper(sys_signame[sig]),
371 fprintf(stderr, "No matching processes %swere found\n",
372 getuid() != 0 ? "belonging to you " : "");