Annotation of src/usr.bin/pkill/pkill.c, Revision 1.10
1.10 ! robert 1: /* $OpenBSD: pkill.c,v 1.9 2005/03/02 21:45:53 otto Exp $ */
1.1 millert 2: /* $NetBSD: pkill.c,v 1.5 2002/10/27 11:49:34 kleink Exp $ */
3:
4: /*-
5: * Copyright (c) 2002 The NetBSD Foundation, Inc.
6: * All rights reserved.
7: *
8: * This code is derived from software contributed to The NetBSD Foundation
9: * by Andrew Doran.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the NetBSD
22: * Foundation, Inc. and its contributors.
23: * 4. Neither the name of The NetBSD Foundation nor the names of its
24: * contributors may be used to endorse or promote products derived
25: * from this software without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37: * POSSIBILITY OF SUCH DAMAGE.
38: */
39:
40: #ifndef lint
1.10 ! robert 41: static const char rcsid[] = "$OpenBSD: pkill.c,v 1.9 2005/03/02 21:45:53 otto Exp $";
1.1 millert 42: #endif /* !lint */
43:
44: #include <sys/types.h>
45: #include <sys/param.h>
46: #include <sys/sysctl.h>
47: #include <sys/proc.h>
48: #include <sys/queue.h>
49: #include <sys/stat.h>
50:
51: #include <stdio.h>
52: #include <stdlib.h>
53: #include <limits.h>
54: #include <string.h>
55: #include <unistd.h>
56: #include <signal.h>
57: #include <regex.h>
58: #include <ctype.h>
59: #include <kvm.h>
60: #include <err.h>
61: #include <pwd.h>
62: #include <grp.h>
63: #include <errno.h>
64:
65: #define STATUS_MATCH 0
66: #define STATUS_NOMATCH 1
67: #define STATUS_BADUSAGE 2
68: #define STATUS_ERROR 3
69:
70: enum listtype {
71: LT_GENERIC,
72: LT_USER,
73: LT_GROUP,
74: LT_TTY,
75: LT_PGRP,
76: LT_SID
77: };
78:
79: struct list {
80: SLIST_ENTRY(list) li_chain;
81: long li_number;
82: };
83:
84: SLIST_HEAD(listhead, list);
85:
1.2 millert 86: struct kinfo_proc2 *plist;
1.1 millert 87: char *selected;
88: char *delim = "\n";
89: int nproc;
90: int pgrep;
91: int signum = SIGTERM;
92: int newest;
93: int inverse;
94: int longfmt;
95: int matchargs;
96: int fullmatch;
1.10 ! robert 97: int cflags = REG_EXTENDED;
1.1 millert 98: kvm_t *kd;
99: pid_t mypid;
100:
101: struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
102: struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
103: struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
104: struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
105: struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
106: struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
107: struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
108:
109: int main(int, char **);
110: void usage(void);
1.8 millert 111: int killact(struct kinfo_proc2 *, int);
112: int grepact(struct kinfo_proc2 *, int);
1.1 millert 113: void makelist(struct listhead *, enum listtype, char *);
114:
115: extern char *__progname;
116:
117: int
118: main(int argc, char **argv)
119: {
120: extern char *optarg;
121: extern int optind;
1.2 millert 122: char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;
1.1 millert 123: int i, j, ch, bestidx, rv, criteria;
1.8 millert 124: int (*action)(struct kinfo_proc2 *, int);
1.2 millert 125: struct kinfo_proc2 *kp;
1.1 millert 126: struct list *li;
1.2 millert 127: u_int32_t bestsec, bestusec;
1.1 millert 128: regex_t reg;
129: regmatch_t regmatch;
130:
131: if (strcmp(__progname, "pgrep") == 0) {
132: action = grepact;
133: pgrep = 1;
134: } else {
135: action = killact;
1.2 millert 136: p = argv[1];
1.1 millert 137:
1.2 millert 138: if (argc > 1 && p[0] == '-') {
139: p++;
140: i = (int)strtol(p, &q, 10);
1.1 millert 141: if (*q == '\0') {
142: signum = i;
143: argv++;
144: argc--;
145: } else {
1.2 millert 146: if (strncasecmp(p, "sig", 3) == 0)
147: p += 3;
1.1 millert 148: for (i = 1; i < NSIG; i++)
1.2 millert 149: if (strcasecmp(sys_signame[i], p) == 0)
1.1 millert 150: break;
151: if (i != NSIG) {
152: signum = i;
153: argv++;
154: argc--;
155: }
156: }
157: }
158: }
159:
160: criteria = 0;
161:
1.10 ! robert 162: while ((ch = getopt(argc, argv, "G:P:U:d:fg:ilns:t:u:vx")) != -1)
1.1 millert 163: switch (ch) {
164: case 'G':
165: makelist(&rgidlist, LT_GROUP, optarg);
166: criteria = 1;
167: break;
168: case 'P':
169: makelist(&ppidlist, LT_GENERIC, optarg);
170: criteria = 1;
171: break;
172: case 'U':
173: makelist(&ruidlist, LT_USER, optarg);
174: criteria = 1;
175: break;
176: case 'd':
177: if (!pgrep)
178: usage();
179: delim = optarg;
180: break;
181: case 'f':
182: matchargs = 1;
183: break;
184: case 'g':
185: makelist(&pgrplist, LT_PGRP, optarg);
186: criteria = 1;
187: break;
1.10 ! robert 188: case 'i':
! 189: cflags |= REG_ICASE;
! 190: break;
1.1 millert 191: case 'l':
192: if (!pgrep)
193: usage();
194: longfmt = 1;
195: break;
196: case 'n':
197: newest = 1;
198: criteria = 1;
199: break;
200: case 's':
201: makelist(&sidlist, LT_SID, optarg);
202: criteria = 1;
203: break;
204: case 't':
205: makelist(&tdevlist, LT_TTY, optarg);
206: criteria = 1;
207: break;
208: case 'u':
209: makelist(&euidlist, LT_USER, optarg);
210: criteria = 1;
211: break;
212: case 'v':
213: inverse = 1;
214: break;
215: case 'x':
216: fullmatch = 1;
217: break;
218: default:
219: usage();
220: /* NOTREACHED */
221: }
222:
223: argc -= optind;
224: argv += optind;
225: if (argc != 0)
226: criteria = 1;
227: if (!criteria)
228: usage();
229:
230: mypid = getpid();
231:
232: /*
233: * Retrieve the list of running processes from the kernel.
234: */
235: kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf);
236: if (kd == NULL)
237: errx(STATUS_ERROR, "kvm_openfiles(): %s", buf);
238:
1.2 millert 239: plist = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc);
1.1 millert 240: if (plist == NULL)
1.2 millert 241: errx(STATUS_ERROR, "kvm_getproc2() failed");
1.1 millert 242:
243: /*
244: * Allocate memory which will be used to keep track of the
245: * selection.
246: */
247: if ((selected = malloc(nproc)) == NULL)
248: errx(STATUS_ERROR, "memory allocation failure");
249: memset(selected, 0, nproc);
250:
251: /*
252: * Refine the selection.
253: */
254: for (; *argv != NULL; argv++) {
1.10 ! robert 255: if ((rv = regcomp(®, *argv, cflags)) != 0) {
1.1 millert 256: regerror(rv, ®, buf, sizeof(buf));
257: errx(STATUS_BADUSAGE, "bad expression: %s", buf);
258: }
259:
260: for (i = 0, kp = plist; i < nproc; i++, kp++) {
1.2 millert 261: if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
1.1 millert 262: continue;
263:
264: if (matchargs) {
1.2 millert 265: if ((pargv = kvm_getargv2(kd, kp, 0)) == NULL)
1.1 millert 266: continue;
267:
268: j = 0;
269: while (j < sizeof(buf) && *pargv != NULL) {
1.7 deraadt 270: int ret;
271:
272: ret = snprintf(buf + j, sizeof(buf) - j,
1.1 millert 273: pargv[1] != NULL ? "%s " : "%s",
274: pargv[0]);
1.7 deraadt 275: if (ret > 0)
276: j += ret;
1.1 millert 277: pargv++;
278: }
279:
280: mstr = buf;
281: } else
1.2 millert 282: mstr = kp->p_comm;
1.1 millert 283:
284: rv = regexec(®, mstr, 1, ®match, 0);
285: if (rv == 0) {
286: if (fullmatch) {
287: if (regmatch.rm_so == 0 &&
288: regmatch.rm_eo == strlen(mstr))
289: selected[i] = 1;
290: } else
291: selected[i] = 1;
292: } else if (rv != REG_NOMATCH) {
293: regerror(rv, ®, buf, sizeof(buf));
294: errx(STATUS_ERROR, "regexec(): %s", buf);
295: }
296: }
297:
298: regfree(®);
299: }
300:
301: for (i = 0, kp = plist; i < nproc; i++, kp++) {
1.5 mpech 302: if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
1.1 millert 303: continue;
304:
305: SLIST_FOREACH(li, &ruidlist, li_chain)
1.2 millert 306: if (kp->p_ruid == (uid_t)li->li_number)
1.1 millert 307: break;
308: if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {
309: selected[i] = 0;
310: continue;
311: }
1.3 deraadt 312:
1.1 millert 313: SLIST_FOREACH(li, &rgidlist, li_chain)
1.2 millert 314: if (kp->p_rgid == (gid_t)li->li_number)
1.1 millert 315: break;
316: if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {
317: selected[i] = 0;
318: continue;
319: }
320:
321: SLIST_FOREACH(li, &euidlist, li_chain)
1.2 millert 322: if (kp->p_uid == (uid_t)li->li_number)
1.1 millert 323: break;
324: if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
325: selected[i] = 0;
326: continue;
327: }
328:
329: SLIST_FOREACH(li, &ppidlist, li_chain)
1.2 millert 330: if (kp->p_ppid == (uid_t)li->li_number)
1.1 millert 331: break;
332: if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
333: selected[i] = 0;
334: continue;
335: }
336:
337: SLIST_FOREACH(li, &pgrplist, li_chain)
1.2 millert 338: if (kp->p__pgid == (uid_t)li->li_number)
1.1 millert 339: break;
340: if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
341: selected[i] = 0;
342: continue;
343: }
344:
345: SLIST_FOREACH(li, &tdevlist, li_chain) {
346: if (li->li_number == -1 &&
1.2 millert 347: (kp->p_flag & P_CONTROLT) == 0)
1.1 millert 348: break;
1.2 millert 349: if (kp->p_tdev == (uid_t)li->li_number)
1.1 millert 350: break;
351: }
352: if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
353: selected[i] = 0;
354: continue;
355: }
356:
357: SLIST_FOREACH(li, &sidlist, li_chain)
1.2 millert 358: if (kp->p_sid == (uid_t)li->li_number)
1.1 millert 359: break;
360: if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
361: selected[i] = 0;
362: continue;
363: }
364:
365: if (argc == 0)
366: selected[i] = 1;
367: }
368:
369: if (newest) {
1.2 millert 370: bestsec = 0;
371: bestusec = 0;
1.1 millert 372: bestidx = -1;
373:
374: for (i = 0, kp = plist; i < nproc; i++, kp++) {
375: if (!selected[i])
376: continue;
377:
1.2 millert 378: if (kp->p_ustart_sec > bestsec ||
379: (kp->p_ustart_sec == bestsec
380: && kp->p_ustart_usec > bestusec)) {
1.3 deraadt 381: bestsec = kp->p_ustart_sec;
382: bestusec = kp->p_ustart_usec;
1.1 millert 383: bestidx = i;
384: }
385: }
386:
387: memset(selected, 0, nproc);
388: if (bestidx != -1)
389: selected[bestidx] = 1;
390: }
391:
392: /*
393: * Take the appropriate action for each matched process, if any.
394: */
1.8 millert 395: rv = STATUS_NOMATCH;
396: for (i = 0, j = 0, kp = plist; i < nproc; i++, kp++) {
1.5 mpech 397: if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
1.1 millert 398: continue;
399: if (selected[i]) {
400: if (inverse)
401: continue;
402: } else if (!inverse)
403: continue;
404:
1.8 millert 405: if ((*action)(kp, j++) == -1)
1.4 millert 406: rv = STATUS_ERROR;
407: else if (rv != STATUS_ERROR)
408: rv = STATUS_MATCH;
1.1 millert 409: }
1.8 millert 410: if (pgrep)
411: putchar('\n');
1.1 millert 412:
1.4 millert 413: exit(rv);
1.1 millert 414: }
415:
416: void
417: usage(void)
418: {
419: const char *ustr;
420:
421: if (pgrep)
1.10 ! robert 422: ustr = "[-filnvx] [-d delim]";
1.1 millert 423: else
1.10 ! robert 424: ustr = "[-signal] [-finvx]";
1.1 millert 425:
426: fprintf(stderr, "usage: %s %s [-G gid] [-P ppid] [-U uid] [-g pgrp] "
427: "[-s sid] [-t tty] [-u euid] pattern ...\n", __progname, ustr);
428:
429: exit(STATUS_ERROR);
430: }
431:
1.4 millert 432: int
1.8 millert 433: killact(struct kinfo_proc2 *kp, int dummy)
1.1 millert 434: {
435:
1.6 millert 436: if (kill(kp->p_pid, signum) == -1 && errno != ESRCH) {
1.4 millert 437: warn("signalling pid %d", (int)kp->p_pid);
438: return (-1);
439: }
440: return (0);
1.1 millert 441: }
442:
1.4 millert 443: int
1.8 millert 444: grepact(struct kinfo_proc2 *kp, int printdelim)
1.1 millert 445: {
446: char **argv;
447:
1.8 millert 448: if (printdelim)
449: fputs(delim, stdout);
1.1 millert 450: if (longfmt && matchargs) {
1.2 millert 451: if ((argv = kvm_getargv2(kd, kp, 0)) == NULL)
1.4 millert 452: return (-1);
1.1 millert 453:
1.2 millert 454: printf("%d ", (int)kp->p_pid);
1.1 millert 455: for (; *argv != NULL; argv++) {
456: printf("%s", *argv);
457: if (argv[1] != NULL)
458: putchar(' ');
459: }
460: } else if (longfmt)
1.2 millert 461: printf("%d %s", (int)kp->p_pid, kp->p_comm);
1.1 millert 462: else
1.2 millert 463: printf("%d", (int)kp->p_pid);
1.1 millert 464:
1.4 millert 465: return (0);
1.1 millert 466: }
467:
468: void
469: makelist(struct listhead *head, enum listtype type, char *src)
470: {
471: struct list *li;
472: struct passwd *pw;
473: struct group *gr;
474: struct stat st;
475: char *sp, *p, buf[MAXPATHLEN];
476: int empty;
477:
478: empty = 1;
479:
480: while ((sp = strsep(&src, ",")) != NULL) {
481: if (*sp == '\0')
482: usage();
483:
484: if ((li = malloc(sizeof(*li))) == NULL)
485: errx(STATUS_ERROR, "memory allocation failure");
486: SLIST_INSERT_HEAD(head, li, li_chain);
487: empty = 0;
488:
489: li->li_number = (uid_t)strtol(sp, &p, 0);
490: if (*p == '\0') {
491: switch (type) {
492: case LT_PGRP:
493: if (li->li_number == 0)
494: li->li_number = getpgrp();
495: break;
496: case LT_SID:
497: if (li->li_number == 0)
498: li->li_number = getsid(mypid);
499: break;
500: case LT_TTY:
501: usage();
502: default:
503: break;
504: }
505: continue;
506: }
507:
508: switch (type) {
509: case LT_USER:
510: if ((pw = getpwnam(sp)) == NULL)
1.9 otto 511: errx(STATUS_BADUSAGE, "unknown user `%s'", sp);
1.1 millert 512: li->li_number = pw->pw_uid;
513: break;
514: case LT_GROUP:
515: if ((gr = getgrnam(sp)) == NULL)
1.9 otto 516: errx(STATUS_BADUSAGE, "unknown group `%s'", sp);
1.1 millert 517: li->li_number = gr->gr_gid;
518: break;
519: case LT_TTY:
520: if (strcmp(sp, "-") == 0) {
521: li->li_number = -1;
522: break;
523: } else if (strcmp(sp, "co") == 0)
524: p = "console";
525: else if (strncmp(sp, "tty", 3) == 0)
526: p = sp;
527: else
528: p = NULL;
529:
530: if (p == NULL)
531: snprintf(buf, sizeof(buf), "/dev/tty%s", sp);
532: else
533: snprintf(buf, sizeof(buf), "/dev/%s", p);
534:
535: if (stat(buf, &st) < 0) {
536: if (errno == ENOENT)
537: errx(STATUS_BADUSAGE,
538: "no such tty: `%s'", sp);
539: err(STATUS_ERROR, "stat(%s)", sp);
540: }
541:
542: if ((st.st_mode & S_IFCHR) == 0)
543: errx(STATUS_BADUSAGE, "not a tty: `%s'", sp);
544:
545: li->li_number = st.st_rdev;
546: break;
547: default:
548: usage();
1.3 deraadt 549: }
1.1 millert 550: }
551:
552: if (empty)
553: usage();
554: }