Annotation of src/usr.bin/pkill/pkill.c, Revision 1.31
1.31 ! halex 1: /* $OpenBSD: pkill.c,v 1.30 2012/08/21 10:32:38 ajacoutot 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: *
20: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30: * POSSIBILITY OF SUCH DAMAGE.
31: */
32:
33: #include <sys/types.h>
34: #include <sys/param.h>
35: #include <sys/sysctl.h>
36: #include <sys/proc.h>
37: #include <sys/queue.h>
38: #include <sys/stat.h>
1.21 markus 39: #include <sys/socket.h>
1.1 millert 40:
41: #include <stdio.h>
42: #include <stdlib.h>
1.16 millert 43: #include <stdint.h>
1.1 millert 44: #include <limits.h>
45: #include <string.h>
46: #include <unistd.h>
47: #include <signal.h>
48: #include <regex.h>
49: #include <ctype.h>
50: #include <kvm.h>
51: #include <err.h>
52: #include <pwd.h>
53: #include <grp.h>
54: #include <errno.h>
55:
56: #define STATUS_MATCH 0
57: #define STATUS_NOMATCH 1
58: #define STATUS_BADUSAGE 2
59: #define STATUS_ERROR 3
60:
61: enum listtype {
62: LT_GENERIC,
63: LT_USER,
64: LT_GROUP,
65: LT_TTY,
66: LT_PGRP,
1.21 markus 67: LT_SID,
68: LT_RTABLE
1.1 millert 69: };
70:
71: struct list {
72: SLIST_ENTRY(list) li_chain;
73: long li_number;
74: };
75:
76: SLIST_HEAD(listhead, list);
77:
1.19 guenther 78: struct kinfo_proc *plist;
1.1 millert 79: char *selected;
80: char *delim = "\n";
81: int nproc;
82: int pgrep;
83: int signum = SIGTERM;
84: int newest;
1.16 millert 85: int oldest;
1.29 espie 86: int quiet;
1.1 millert 87: int inverse;
88: int longfmt;
89: int matchargs;
90: int fullmatch;
91: kvm_t *kd;
92: pid_t mypid;
93:
94: struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
95: struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
96: struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
97: struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
98: struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
99: struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
100: struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
1.21 markus 101: struct listhead rtablist = SLIST_HEAD_INITIALIZER(list);
1.1 millert 102:
103: int main(int, char **);
104: void usage(void);
1.19 guenther 105: int killact(struct kinfo_proc *, int);
106: int grepact(struct kinfo_proc *, int);
1.1 millert 107: void makelist(struct listhead *, enum listtype, char *);
108:
109: extern char *__progname;
110:
111: int
112: main(int argc, char **argv)
113: {
114: extern char *optarg;
115: extern int optind;
1.2 millert 116: char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;
1.1 millert 117: int i, j, ch, bestidx, rv, criteria;
1.19 guenther 118: int (*action)(struct kinfo_proc *, int);
119: struct kinfo_proc *kp;
1.1 millert 120: struct list *li;
1.2 millert 121: u_int32_t bestsec, bestusec;
1.1 millert 122: regex_t reg;
123: regmatch_t regmatch;
124:
125: if (strcmp(__progname, "pgrep") == 0) {
126: action = grepact;
127: pgrep = 1;
128: } else {
129: action = killact;
1.2 millert 130: p = argv[1];
1.1 millert 131:
1.2 millert 132: if (argc > 1 && p[0] == '-') {
133: p++;
134: i = (int)strtol(p, &q, 10);
1.1 millert 135: if (*q == '\0') {
136: signum = i;
137: argv++;
138: argc--;
139: } else {
1.2 millert 140: if (strncasecmp(p, "sig", 3) == 0)
141: p += 3;
1.1 millert 142: for (i = 1; i < NSIG; i++)
1.2 millert 143: if (strcasecmp(sys_signame[i], p) == 0)
1.1 millert 144: break;
145: if (i != NSIG) {
146: signum = i;
147: argv++;
148: argc--;
149: }
150: }
151: }
152: }
153:
154: criteria = 0;
155:
1.29 espie 156: while ((ch = getopt(argc, argv, "G:P:T:U:d:fg:lnoqs:t:u:vx")) != -1)
1.1 millert 157: switch (ch) {
158: case 'G':
159: makelist(&rgidlist, LT_GROUP, optarg);
160: criteria = 1;
161: break;
162: case 'P':
163: makelist(&ppidlist, LT_GENERIC, optarg);
164: criteria = 1;
165: break;
1.21 markus 166: case 'T':
167: makelist(&rtablist, LT_RTABLE, optarg);
168: criteria = 1;
169: break;
1.1 millert 170: case 'U':
171: makelist(&ruidlist, LT_USER, optarg);
172: criteria = 1;
173: break;
174: case 'd':
175: if (!pgrep)
176: usage();
177: delim = optarg;
178: break;
179: case 'f':
180: matchargs = 1;
181: break;
182: case 'g':
183: makelist(&pgrplist, LT_PGRP, optarg);
184: criteria = 1;
185: break;
186: case 'l':
187: longfmt = 1;
188: break;
189: case 'n':
190: newest = 1;
191: criteria = 1;
192: break;
1.16 millert 193: case 'o':
194: oldest = 1;
195: criteria = 1;
196: break;
1.29 espie 197: case 'q':
198: quiet = 1;
199: break;
1.1 millert 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;
1.16 millert 227: if (!criteria || (newest && oldest))
1.1 millert 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.19 guenther 239: plist = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc);
1.1 millert 240: if (plist == NULL)
1.19 guenther 241: errx(STATUS_ERROR, "kvm_getprocs() 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.11 robert 255: if ((rv = regcomp(®, *argv, REG_EXTENDED)) != 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.25 sthen 261: if ((kp->p_flag & (P_SYSTEM | P_THREAD)) != 0 ||
262: kp->p_pid == mypid)
1.1 millert 263: continue;
264:
265: if (matchargs) {
1.19 guenther 266: if ((pargv = kvm_getargv(kd, kp, 0)) == NULL)
1.1 millert 267: continue;
268:
269: j = 0;
270: while (j < sizeof(buf) && *pargv != NULL) {
1.7 deraadt 271: int ret;
272:
273: ret = snprintf(buf + j, sizeof(buf) - j,
1.1 millert 274: pargv[1] != NULL ? "%s " : "%s",
275: pargv[0]);
1.12 deraadt 276: if (ret >= sizeof(buf) - j)
277: j += sizeof(buf) - j - 1;
278: else if (ret > 0)
1.7 deraadt 279: j += ret;
1.1 millert 280: pargv++;
281: }
282:
283: mstr = buf;
284: } else
1.2 millert 285: mstr = kp->p_comm;
1.1 millert 286:
287: rv = regexec(®, mstr, 1, ®match, 0);
288: if (rv == 0) {
289: if (fullmatch) {
290: if (regmatch.rm_so == 0 &&
291: regmatch.rm_eo == strlen(mstr))
292: selected[i] = 1;
293: } else
294: selected[i] = 1;
295: } else if (rv != REG_NOMATCH) {
296: regerror(rv, ®, buf, sizeof(buf));
297: errx(STATUS_ERROR, "regexec(): %s", buf);
298: }
299: }
300:
301: regfree(®);
302: }
303:
304: for (i = 0, kp = plist; i < nproc; i++, kp++) {
1.25 sthen 305: if ((kp->p_flag & (P_SYSTEM | P_THREAD)) != 0 ||
306: kp->p_pid == mypid)
1.1 millert 307: continue;
308:
309: SLIST_FOREACH(li, &ruidlist, li_chain)
1.2 millert 310: if (kp->p_ruid == (uid_t)li->li_number)
1.1 millert 311: break;
312: if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {
313: selected[i] = 0;
314: continue;
315: }
1.3 deraadt 316:
1.1 millert 317: SLIST_FOREACH(li, &rgidlist, li_chain)
1.2 millert 318: if (kp->p_rgid == (gid_t)li->li_number)
1.1 millert 319: break;
320: if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {
321: selected[i] = 0;
322: continue;
323: }
324:
325: SLIST_FOREACH(li, &euidlist, li_chain)
1.2 millert 326: if (kp->p_uid == (uid_t)li->li_number)
1.1 millert 327: break;
328: if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
329: selected[i] = 0;
330: continue;
331: }
332:
333: SLIST_FOREACH(li, &ppidlist, li_chain)
1.2 millert 334: if (kp->p_ppid == (uid_t)li->li_number)
1.1 millert 335: break;
336: if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
337: selected[i] = 0;
338: continue;
339: }
340:
341: SLIST_FOREACH(li, &pgrplist, li_chain)
1.2 millert 342: if (kp->p__pgid == (uid_t)li->li_number)
1.1 millert 343: break;
344: if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
345: selected[i] = 0;
346: continue;
347: }
348:
349: SLIST_FOREACH(li, &tdevlist, li_chain) {
350: if (li->li_number == -1 &&
1.2 millert 351: (kp->p_flag & P_CONTROLT) == 0)
1.1 millert 352: break;
1.2 millert 353: if (kp->p_tdev == (uid_t)li->li_number)
1.1 millert 354: break;
355: }
356: if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
357: selected[i] = 0;
358: continue;
359: }
360:
361: SLIST_FOREACH(li, &sidlist, li_chain)
1.2 millert 362: if (kp->p_sid == (uid_t)li->li_number)
1.1 millert 363: break;
364: if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
365: selected[i] = 0;
366: continue;
367: }
368:
1.21 markus 369: SLIST_FOREACH(li, &rtablist, li_chain)
370: if (kp->p_rtableid == (u_int32_t)li->li_number)
371: break;
372: if (SLIST_FIRST(&rtablist) != NULL && li == NULL) {
373: selected[i] = 0;
374: continue;
375: }
376:
1.1 millert 377: if (argc == 0)
378: selected[i] = 1;
379: }
380:
1.16 millert 381: if (newest || oldest) {
1.1 millert 382: bestidx = -1;
383:
1.16 millert 384: if (newest)
385: bestsec = bestusec = 0;
386: else
387: bestsec = bestusec = UINT32_MAX;
388:
1.1 millert 389: for (i = 0, kp = plist; i < nproc; i++, kp++) {
390: if (!selected[i])
391: continue;
392:
1.16 millert 393: if ((newest && (kp->p_ustart_sec > bestsec ||
1.2 millert 394: (kp->p_ustart_sec == bestsec
1.16 millert 395: && kp->p_ustart_usec > bestusec)))
396: || (oldest && (kp->p_ustart_sec < bestsec ||
397: (kp->p_ustart_sec == bestsec
398: && kp->p_ustart_usec < bestusec)))) {
399:
1.3 deraadt 400: bestsec = kp->p_ustart_sec;
401: bestusec = kp->p_ustart_usec;
1.1 millert 402: bestidx = i;
403: }
404: }
405:
406: memset(selected, 0, nproc);
407: if (bestidx != -1)
408: selected[bestidx] = 1;
409: }
410:
411: /*
412: * Take the appropriate action for each matched process, if any.
413: */
1.8 millert 414: rv = STATUS_NOMATCH;
415: for (i = 0, j = 0, kp = plist; i < nproc; i++, kp++) {
1.25 sthen 416: if ((kp->p_flag & (P_SYSTEM | P_THREAD)) != 0 ||
417: kp->p_pid == mypid)
1.1 millert 418: continue;
1.28 halex 419: if (selected[i] == inverse)
1.1 millert 420: continue;
421:
1.31 ! halex 422: switch ((*action)(kp, j++)) {
! 423: case STATUS_MATCH:
! 424: if (rv != STATUS_ERROR)
! 425: rv = STATUS_MATCH;
! 426: break;
! 427: case STATUS_NOMATCH:
! 428: j--;
! 429: break;
! 430: case STATUS_ERROR:
1.4 millert 431: rv = STATUS_ERROR;
1.31 ! halex 432: break;
! 433: }
1.1 millert 434: }
1.29 espie 435: if (pgrep && j && !quiet)
1.8 millert 436: putchar('\n');
1.1 millert 437:
1.4 millert 438: exit(rv);
1.1 millert 439: }
440:
441: void
442: usage(void)
443: {
444: const char *ustr;
445:
446: if (pgrep)
1.30 ajacouto 447: ustr = "[-flnoqvx] [-d delim]";
1.1 millert 448: else
1.30 ajacouto 449: ustr = "[-signal] [-flnoqvx]";
1.1 millert 450:
1.22 jmc 451: fprintf(stderr, "usage: %s %s [-G gid] [-g pgrp] [-P ppid] [-s sid]"
452: "\n\t[-T rtable] [-t tty] [-U uid] [-u euid] [pattern ...]\n",
1.21 markus 453: __progname, ustr);
1.1 millert 454:
1.23 jmc 455: exit(STATUS_BADUSAGE);
1.1 millert 456: }
457:
1.4 millert 458: int
1.19 guenther 459: killact(struct kinfo_proc *kp, int dummy)
1.1 millert 460: {
1.29 espie 461: if (longfmt && !quiet)
1.28 halex 462: printf("%d %s\n", (int)kp->p_pid, kp->p_comm);
1.1 millert 463:
1.31 ! halex 464: if (kill(kp->p_pid, signum) == -1) {
! 465: if (errno == ESRCH)
! 466: return (STATUS_NOMATCH);
1.4 millert 467: warn("signalling pid %d", (int)kp->p_pid);
1.31 ! halex 468: return (STATUS_ERROR);
1.4 millert 469: }
1.31 ! halex 470: return (STATUS_MATCH);
1.1 millert 471: }
472:
1.4 millert 473: int
1.19 guenther 474: grepact(struct kinfo_proc *kp, int printdelim)
1.1 millert 475: {
476: char **argv;
477:
1.29 espie 478: if (quiet)
1.31 ! halex 479: return (STATUS_MATCH);
! 480: if (longfmt && matchargs)
! 481: if ((argv = kvm_getargv(kd, kp, 0)) == NULL)
! 482: return (errno == ESRCH ? STATUS_NOMATCH : STATUS_ERROR);
1.8 millert 483: if (printdelim)
484: fputs(delim, stdout);
1.1 millert 485: if (longfmt && matchargs) {
1.2 millert 486: printf("%d ", (int)kp->p_pid);
1.1 millert 487: for (; *argv != NULL; argv++) {
488: printf("%s", *argv);
489: if (argv[1] != NULL)
490: putchar(' ');
491: }
492: } else if (longfmt)
1.2 millert 493: printf("%d %s", (int)kp->p_pid, kp->p_comm);
1.1 millert 494: else
1.2 millert 495: printf("%d", (int)kp->p_pid);
1.1 millert 496:
1.31 ! halex 497: return (STATUS_MATCH);
1.1 millert 498: }
499:
500: void
501: makelist(struct listhead *head, enum listtype type, char *src)
502: {
503: struct list *li;
504: struct passwd *pw;
505: struct group *gr;
506: struct stat st;
507: char *sp, *p, buf[MAXPATHLEN];
508: int empty;
509:
510: empty = 1;
511:
512: while ((sp = strsep(&src, ",")) != NULL) {
513: if (*sp == '\0')
514: usage();
515:
516: if ((li = malloc(sizeof(*li))) == NULL)
517: errx(STATUS_ERROR, "memory allocation failure");
518: SLIST_INSERT_HEAD(head, li, li_chain);
519: empty = 0;
520:
1.21 markus 521: li->li_number = strtol(sp, &p, 0);
1.1 millert 522: if (*p == '\0') {
523: switch (type) {
524: case LT_PGRP:
525: if (li->li_number == 0)
526: li->li_number = getpgrp();
527: break;
528: case LT_SID:
529: if (li->li_number == 0)
530: li->li_number = getsid(mypid);
1.21 markus 531: break;
532: case LT_RTABLE:
533: if (li->li_number < 0 ||
534: li->li_number > RT_TABLEID_MAX)
535: errx(STATUS_BADUSAGE,
536: "rtable out of range");
1.1 millert 537: break;
538: case LT_TTY:
539: usage();
540: default:
541: break;
542: }
543: continue;
544: }
545:
546: switch (type) {
547: case LT_USER:
548: if ((pw = getpwnam(sp)) == NULL)
1.9 otto 549: errx(STATUS_BADUSAGE, "unknown user `%s'", sp);
1.1 millert 550: li->li_number = pw->pw_uid;
551: break;
552: case LT_GROUP:
553: if ((gr = getgrnam(sp)) == NULL)
1.9 otto 554: errx(STATUS_BADUSAGE, "unknown group `%s'", sp);
1.1 millert 555: li->li_number = gr->gr_gid;
556: break;
557: case LT_TTY:
558: if (strcmp(sp, "-") == 0) {
559: li->li_number = -1;
560: break;
561: } else if (strcmp(sp, "co") == 0)
562: p = "console";
563: else if (strncmp(sp, "tty", 3) == 0)
564: p = sp;
565: else
566: p = NULL;
567:
568: if (p == NULL)
569: snprintf(buf, sizeof(buf), "/dev/tty%s", sp);
570: else
571: snprintf(buf, sizeof(buf), "/dev/%s", p);
572:
573: if (stat(buf, &st) < 0) {
574: if (errno == ENOENT)
575: errx(STATUS_BADUSAGE,
576: "no such tty: `%s'", sp);
577: err(STATUS_ERROR, "stat(%s)", sp);
578: }
579:
1.15 otto 580: if (!S_ISCHR(st.st_mode))
1.1 millert 581: errx(STATUS_BADUSAGE, "not a tty: `%s'", sp);
582:
583: li->li_number = st.st_rdev;
584: break;
585: default:
586: usage();
1.3 deraadt 587: }
1.1 millert 588: }
589:
590: if (empty)
591: usage();
592: }