Annotation of src/usr.bin/pkill/pkill.c, Revision 1.30
1.30 ! ajacouto 1: /* $OpenBSD: pkill.c,v 1.29 2012/08/21 06:28:36 espie 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.8 millert 422: if ((*action)(kp, j++) == -1)
1.4 millert 423: rv = STATUS_ERROR;
424: else if (rv != STATUS_ERROR)
425: rv = STATUS_MATCH;
1.1 millert 426: }
1.29 espie 427: if (pgrep && j && !quiet)
1.8 millert 428: putchar('\n');
1.1 millert 429:
1.4 millert 430: exit(rv);
1.1 millert 431: }
432:
433: void
434: usage(void)
435: {
436: const char *ustr;
437:
438: if (pgrep)
1.30 ! ajacouto 439: ustr = "[-flnoqvx] [-d delim]";
1.1 millert 440: else
1.30 ! ajacouto 441: ustr = "[-signal] [-flnoqvx]";
1.1 millert 442:
1.22 jmc 443: fprintf(stderr, "usage: %s %s [-G gid] [-g pgrp] [-P ppid] [-s sid]"
444: "\n\t[-T rtable] [-t tty] [-U uid] [-u euid] [pattern ...]\n",
1.21 markus 445: __progname, ustr);
1.1 millert 446:
1.23 jmc 447: exit(STATUS_BADUSAGE);
1.1 millert 448: }
449:
1.4 millert 450: int
1.19 guenther 451: killact(struct kinfo_proc *kp, int dummy)
1.1 millert 452: {
1.29 espie 453: if (longfmt && !quiet)
1.28 halex 454: printf("%d %s\n", (int)kp->p_pid, kp->p_comm);
1.1 millert 455:
1.6 millert 456: if (kill(kp->p_pid, signum) == -1 && errno != ESRCH) {
1.4 millert 457: warn("signalling pid %d", (int)kp->p_pid);
458: return (-1);
459: }
460: return (0);
1.1 millert 461: }
462:
1.4 millert 463: int
1.19 guenther 464: grepact(struct kinfo_proc *kp, int printdelim)
1.1 millert 465: {
466: char **argv;
467:
1.29 espie 468: if (quiet)
469: return (0);
1.8 millert 470: if (printdelim)
471: fputs(delim, stdout);
1.1 millert 472: if (longfmt && matchargs) {
1.19 guenther 473: if ((argv = kvm_getargv(kd, kp, 0)) == NULL)
1.4 millert 474: return (-1);
1.1 millert 475:
1.2 millert 476: printf("%d ", (int)kp->p_pid);
1.1 millert 477: for (; *argv != NULL; argv++) {
478: printf("%s", *argv);
479: if (argv[1] != NULL)
480: putchar(' ');
481: }
482: } else if (longfmt)
1.2 millert 483: printf("%d %s", (int)kp->p_pid, kp->p_comm);
1.1 millert 484: else
1.2 millert 485: printf("%d", (int)kp->p_pid);
1.1 millert 486:
1.4 millert 487: return (0);
1.1 millert 488: }
489:
490: void
491: makelist(struct listhead *head, enum listtype type, char *src)
492: {
493: struct list *li;
494: struct passwd *pw;
495: struct group *gr;
496: struct stat st;
497: char *sp, *p, buf[MAXPATHLEN];
498: int empty;
499:
500: empty = 1;
501:
502: while ((sp = strsep(&src, ",")) != NULL) {
503: if (*sp == '\0')
504: usage();
505:
506: if ((li = malloc(sizeof(*li))) == NULL)
507: errx(STATUS_ERROR, "memory allocation failure");
508: SLIST_INSERT_HEAD(head, li, li_chain);
509: empty = 0;
510:
1.21 markus 511: li->li_number = strtol(sp, &p, 0);
1.1 millert 512: if (*p == '\0') {
513: switch (type) {
514: case LT_PGRP:
515: if (li->li_number == 0)
516: li->li_number = getpgrp();
517: break;
518: case LT_SID:
519: if (li->li_number == 0)
520: li->li_number = getsid(mypid);
1.21 markus 521: break;
522: case LT_RTABLE:
523: if (li->li_number < 0 ||
524: li->li_number > RT_TABLEID_MAX)
525: errx(STATUS_BADUSAGE,
526: "rtable out of range");
1.1 millert 527: break;
528: case LT_TTY:
529: usage();
530: default:
531: break;
532: }
533: continue;
534: }
535:
536: switch (type) {
537: case LT_USER:
538: if ((pw = getpwnam(sp)) == NULL)
1.9 otto 539: errx(STATUS_BADUSAGE, "unknown user `%s'", sp);
1.1 millert 540: li->li_number = pw->pw_uid;
541: break;
542: case LT_GROUP:
543: if ((gr = getgrnam(sp)) == NULL)
1.9 otto 544: errx(STATUS_BADUSAGE, "unknown group `%s'", sp);
1.1 millert 545: li->li_number = gr->gr_gid;
546: break;
547: case LT_TTY:
548: if (strcmp(sp, "-") == 0) {
549: li->li_number = -1;
550: break;
551: } else if (strcmp(sp, "co") == 0)
552: p = "console";
553: else if (strncmp(sp, "tty", 3) == 0)
554: p = sp;
555: else
556: p = NULL;
557:
558: if (p == NULL)
559: snprintf(buf, sizeof(buf), "/dev/tty%s", sp);
560: else
561: snprintf(buf, sizeof(buf), "/dev/%s", p);
562:
563: if (stat(buf, &st) < 0) {
564: if (errno == ENOENT)
565: errx(STATUS_BADUSAGE,
566: "no such tty: `%s'", sp);
567: err(STATUS_ERROR, "stat(%s)", sp);
568: }
569:
1.15 otto 570: if (!S_ISCHR(st.st_mode))
1.1 millert 571: errx(STATUS_BADUSAGE, "not a tty: `%s'", sp);
572:
573: li->li_number = st.st_rdev;
574: break;
575: default:
576: usage();
1.3 deraadt 577: }
1.1 millert 578: }
579:
580: if (empty)
581: usage();
582: }