Annotation of src/usr.bin/pkill/pkill.c, Revision 1.24
1.24 ! lum 1: /* $OpenBSD: pkill.c,v 1.23 2012/02/16 21:25:35 jmc 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.1 millert 86: int inverse;
87: int longfmt;
88: int matchargs;
89: int fullmatch;
90: kvm_t *kd;
91: pid_t mypid;
92:
93: struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
94: struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
95: struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
96: struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
97: struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
98: struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
99: struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
1.21 markus 100: struct listhead rtablist = SLIST_HEAD_INITIALIZER(list);
1.1 millert 101:
102: int main(int, char **);
103: void usage(void);
1.19 guenther 104: int killact(struct kinfo_proc *, int);
105: int grepact(struct kinfo_proc *, int);
1.1 millert 106: void makelist(struct listhead *, enum listtype, char *);
107:
108: extern char *__progname;
109:
110: int
111: main(int argc, char **argv)
112: {
113: extern char *optarg;
114: extern int optind;
1.2 millert 115: char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;
1.1 millert 116: int i, j, ch, bestidx, rv, criteria;
1.19 guenther 117: int (*action)(struct kinfo_proc *, int);
118: struct kinfo_proc *kp;
1.1 millert 119: struct list *li;
1.2 millert 120: u_int32_t bestsec, bestusec;
1.1 millert 121: regex_t reg;
122: regmatch_t regmatch;
123:
124: if (strcmp(__progname, "pgrep") == 0) {
125: action = grepact;
126: pgrep = 1;
127: } else {
128: action = killact;
1.2 millert 129: p = argv[1];
1.1 millert 130:
1.2 millert 131: if (argc > 1 && p[0] == '-') {
132: p++;
133: i = (int)strtol(p, &q, 10);
1.1 millert 134: if (*q == '\0') {
135: signum = i;
136: argv++;
137: argc--;
138: } else {
1.2 millert 139: if (strncasecmp(p, "sig", 3) == 0)
140: p += 3;
1.1 millert 141: for (i = 1; i < NSIG; i++)
1.2 millert 142: if (strcasecmp(sys_signame[i], p) == 0)
1.1 millert 143: break;
144: if (i != NSIG) {
145: signum = i;
146: argv++;
147: argc--;
148: }
149: }
150: }
151: }
152:
153: criteria = 0;
154:
1.21 markus 155: while ((ch = getopt(argc, argv, "G:P:T:U:d:fg:lnos:t:u:vx")) != -1)
1.1 millert 156: switch (ch) {
157: case 'G':
158: makelist(&rgidlist, LT_GROUP, optarg);
159: criteria = 1;
160: break;
161: case 'P':
162: makelist(&ppidlist, LT_GENERIC, optarg);
163: criteria = 1;
164: break;
1.21 markus 165: case 'T':
166: makelist(&rtablist, LT_RTABLE, optarg);
167: criteria = 1;
168: break;
1.1 millert 169: case 'U':
170: makelist(&ruidlist, LT_USER, optarg);
171: criteria = 1;
172: break;
173: case 'd':
174: if (!pgrep)
175: usage();
176: delim = optarg;
177: break;
178: case 'f':
179: matchargs = 1;
180: break;
181: case 'g':
182: makelist(&pgrplist, LT_PGRP, optarg);
183: criteria = 1;
184: break;
185: case 'l':
186: longfmt = 1;
187: break;
188: case 'n':
189: newest = 1;
190: criteria = 1;
191: break;
1.16 millert 192: case 'o':
193: oldest = 1;
194: criteria = 1;
195: break;
1.1 millert 196: case 's':
197: makelist(&sidlist, LT_SID, optarg);
198: criteria = 1;
199: break;
200: case 't':
201: makelist(&tdevlist, LT_TTY, optarg);
202: criteria = 1;
203: break;
204: case 'u':
205: makelist(&euidlist, LT_USER, optarg);
206: criteria = 1;
207: break;
208: case 'v':
209: inverse = 1;
210: break;
211: case 'x':
212: fullmatch = 1;
213: break;
214: default:
215: usage();
216: /* NOTREACHED */
217: }
218:
219: argc -= optind;
220: argv += optind;
221: if (argc != 0)
222: criteria = 1;
1.16 millert 223: if (!criteria || (newest && oldest))
1.1 millert 224: usage();
225:
226: mypid = getpid();
227:
228: /*
229: * Retrieve the list of running processes from the kernel.
230: */
231: kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf);
232: if (kd == NULL)
233: errx(STATUS_ERROR, "kvm_openfiles(): %s", buf);
234:
1.19 guenther 235: plist = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc);
1.1 millert 236: if (plist == NULL)
1.19 guenther 237: errx(STATUS_ERROR, "kvm_getprocs() failed");
1.1 millert 238:
239: /*
240: * Allocate memory which will be used to keep track of the
241: * selection.
242: */
243: if ((selected = malloc(nproc)) == NULL)
244: errx(STATUS_ERROR, "memory allocation failure");
245: memset(selected, 0, nproc);
246:
247: /*
248: * Refine the selection.
249: */
250: for (; *argv != NULL; argv++) {
1.11 robert 251: if ((rv = regcomp(®, *argv, REG_EXTENDED)) != 0) {
1.1 millert 252: regerror(rv, ®, buf, sizeof(buf));
253: errx(STATUS_BADUSAGE, "bad expression: %s", buf);
254: }
255:
256: for (i = 0, kp = plist; i < nproc; i++, kp++) {
1.2 millert 257: if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
1.1 millert 258: continue;
259:
260: if (matchargs) {
1.19 guenther 261: if ((pargv = kvm_getargv(kd, kp, 0)) == NULL)
1.1 millert 262: continue;
263:
264: j = 0;
265: while (j < sizeof(buf) && *pargv != NULL) {
1.7 deraadt 266: int ret;
267:
268: ret = snprintf(buf + j, sizeof(buf) - j,
1.1 millert 269: pargv[1] != NULL ? "%s " : "%s",
270: pargv[0]);
1.12 deraadt 271: if (ret >= sizeof(buf) - j)
272: j += sizeof(buf) - j - 1;
273: else if (ret > 0)
1.7 deraadt 274: j += ret;
1.1 millert 275: pargv++;
276: }
277:
278: mstr = buf;
279: } else
1.2 millert 280: mstr = kp->p_comm;
1.1 millert 281:
282: rv = regexec(®, mstr, 1, ®match, 0);
283: if (rv == 0) {
284: if (fullmatch) {
285: if (regmatch.rm_so == 0 &&
286: regmatch.rm_eo == strlen(mstr))
287: selected[i] = 1;
288: } else
289: selected[i] = 1;
290: } else if (rv != REG_NOMATCH) {
291: regerror(rv, ®, buf, sizeof(buf));
292: errx(STATUS_ERROR, "regexec(): %s", buf);
293: }
294: }
295:
296: regfree(®);
297: }
298:
299: for (i = 0, kp = plist; i < nproc; i++, kp++) {
1.5 mpech 300: if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
1.1 millert 301: continue;
302:
303: SLIST_FOREACH(li, &ruidlist, li_chain)
1.2 millert 304: if (kp->p_ruid == (uid_t)li->li_number)
1.1 millert 305: break;
306: if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {
307: selected[i] = 0;
308: continue;
309: }
1.3 deraadt 310:
1.1 millert 311: SLIST_FOREACH(li, &rgidlist, li_chain)
1.2 millert 312: if (kp->p_rgid == (gid_t)li->li_number)
1.1 millert 313: break;
314: if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {
315: selected[i] = 0;
316: continue;
317: }
318:
319: SLIST_FOREACH(li, &euidlist, li_chain)
1.2 millert 320: if (kp->p_uid == (uid_t)li->li_number)
1.1 millert 321: break;
322: if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
323: selected[i] = 0;
324: continue;
325: }
326:
327: SLIST_FOREACH(li, &ppidlist, li_chain)
1.2 millert 328: if (kp->p_ppid == (uid_t)li->li_number)
1.1 millert 329: break;
330: if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
331: selected[i] = 0;
332: continue;
333: }
334:
335: SLIST_FOREACH(li, &pgrplist, li_chain)
1.2 millert 336: if (kp->p__pgid == (uid_t)li->li_number)
1.1 millert 337: break;
338: if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
339: selected[i] = 0;
340: continue;
341: }
342:
343: SLIST_FOREACH(li, &tdevlist, li_chain) {
344: if (li->li_number == -1 &&
1.2 millert 345: (kp->p_flag & P_CONTROLT) == 0)
1.1 millert 346: break;
1.2 millert 347: if (kp->p_tdev == (uid_t)li->li_number)
1.1 millert 348: break;
349: }
350: if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
351: selected[i] = 0;
352: continue;
353: }
354:
355: SLIST_FOREACH(li, &sidlist, li_chain)
1.2 millert 356: if (kp->p_sid == (uid_t)li->li_number)
1.1 millert 357: break;
358: if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
359: selected[i] = 0;
360: continue;
361: }
362:
1.21 markus 363: SLIST_FOREACH(li, &rtablist, li_chain)
364: if (kp->p_rtableid == (u_int32_t)li->li_number)
365: break;
366: if (SLIST_FIRST(&rtablist) != NULL && li == NULL) {
367: selected[i] = 0;
368: continue;
369: }
370:
1.1 millert 371: if (argc == 0)
372: selected[i] = 1;
373: }
374:
1.16 millert 375: if (newest || oldest) {
1.1 millert 376: bestidx = -1;
377:
1.16 millert 378: if (newest)
379: bestsec = bestusec = 0;
380: else
381: bestsec = bestusec = UINT32_MAX;
382:
1.1 millert 383: for (i = 0, kp = plist; i < nproc; i++, kp++) {
384: if (!selected[i])
385: continue;
386:
1.16 millert 387: if ((newest && (kp->p_ustart_sec > bestsec ||
1.2 millert 388: (kp->p_ustart_sec == bestsec
1.16 millert 389: && kp->p_ustart_usec > bestusec)))
390: || (oldest && (kp->p_ustart_sec < bestsec ||
391: (kp->p_ustart_sec == bestsec
392: && kp->p_ustart_usec < bestusec)))) {
393:
1.3 deraadt 394: bestsec = kp->p_ustart_sec;
395: bestusec = kp->p_ustart_usec;
1.1 millert 396: bestidx = i;
397: }
398: }
399:
400: memset(selected, 0, nproc);
401: if (bestidx != -1)
402: selected[bestidx] = 1;
403: }
404:
405: /*
406: * Take the appropriate action for each matched process, if any.
407: */
1.8 millert 408: rv = STATUS_NOMATCH;
409: for (i = 0, j = 0, kp = plist; i < nproc; i++, kp++) {
1.5 mpech 410: if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
1.1 millert 411: continue;
412: if (selected[i]) {
1.24 ! lum 413: if (longfmt && !pgrep)
1.20 lum 414: printf("%d %s\n", (int)kp->p_pid, kp->p_comm);
1.1 millert 415: if (inverse)
416: continue;
417: } else if (!inverse)
418: continue;
419:
1.8 millert 420: if ((*action)(kp, j++) == -1)
1.4 millert 421: rv = STATUS_ERROR;
422: else if (rv != STATUS_ERROR)
423: rv = STATUS_MATCH;
1.1 millert 424: }
1.13 otto 425: if (pgrep && j)
1.8 millert 426: putchar('\n');
1.1 millert 427:
1.4 millert 428: exit(rv);
1.1 millert 429: }
430:
431: void
432: usage(void)
433: {
434: const char *ustr;
435:
436: if (pgrep)
1.16 millert 437: ustr = "[-flnovx] [-d delim]";
1.1 millert 438: else
1.20 lum 439: ustr = "[-signal] [-flnovx]";
1.1 millert 440:
1.22 jmc 441: fprintf(stderr, "usage: %s %s [-G gid] [-g pgrp] [-P ppid] [-s sid]"
442: "\n\t[-T rtable] [-t tty] [-U uid] [-u euid] [pattern ...]\n",
1.21 markus 443: __progname, ustr);
1.1 millert 444:
1.23 jmc 445: exit(STATUS_BADUSAGE);
1.1 millert 446: }
447:
1.4 millert 448: int
1.19 guenther 449: killact(struct kinfo_proc *kp, int dummy)
1.1 millert 450: {
451:
1.6 millert 452: if (kill(kp->p_pid, signum) == -1 && errno != ESRCH) {
1.4 millert 453: warn("signalling pid %d", (int)kp->p_pid);
454: return (-1);
455: }
456: return (0);
1.1 millert 457: }
458:
1.4 millert 459: int
1.19 guenther 460: grepact(struct kinfo_proc *kp, int printdelim)
1.1 millert 461: {
462: char **argv;
463:
1.8 millert 464: if (printdelim)
465: fputs(delim, stdout);
1.1 millert 466: if (longfmt && matchargs) {
1.19 guenther 467: if ((argv = kvm_getargv(kd, kp, 0)) == NULL)
1.4 millert 468: return (-1);
1.1 millert 469:
1.2 millert 470: printf("%d ", (int)kp->p_pid);
1.1 millert 471: for (; *argv != NULL; argv++) {
472: printf("%s", *argv);
473: if (argv[1] != NULL)
474: putchar(' ');
475: }
476: } else if (longfmt)
1.2 millert 477: printf("%d %s", (int)kp->p_pid, kp->p_comm);
1.1 millert 478: else
1.2 millert 479: printf("%d", (int)kp->p_pid);
1.1 millert 480:
1.4 millert 481: return (0);
1.1 millert 482: }
483:
484: void
485: makelist(struct listhead *head, enum listtype type, char *src)
486: {
487: struct list *li;
488: struct passwd *pw;
489: struct group *gr;
490: struct stat st;
491: char *sp, *p, buf[MAXPATHLEN];
492: int empty;
493:
494: empty = 1;
495:
496: while ((sp = strsep(&src, ",")) != NULL) {
497: if (*sp == '\0')
498: usage();
499:
500: if ((li = malloc(sizeof(*li))) == NULL)
501: errx(STATUS_ERROR, "memory allocation failure");
502: SLIST_INSERT_HEAD(head, li, li_chain);
503: empty = 0;
504:
1.21 markus 505: li->li_number = strtol(sp, &p, 0);
1.1 millert 506: if (*p == '\0') {
507: switch (type) {
508: case LT_PGRP:
509: if (li->li_number == 0)
510: li->li_number = getpgrp();
511: break;
512: case LT_SID:
513: if (li->li_number == 0)
514: li->li_number = getsid(mypid);
1.21 markus 515: break;
516: case LT_RTABLE:
517: if (li->li_number < 0 ||
518: li->li_number > RT_TABLEID_MAX)
519: errx(STATUS_BADUSAGE,
520: "rtable out of range");
1.1 millert 521: break;
522: case LT_TTY:
523: usage();
524: default:
525: break;
526: }
527: continue;
528: }
529:
530: switch (type) {
531: case LT_USER:
532: if ((pw = getpwnam(sp)) == NULL)
1.9 otto 533: errx(STATUS_BADUSAGE, "unknown user `%s'", sp);
1.1 millert 534: li->li_number = pw->pw_uid;
535: break;
536: case LT_GROUP:
537: if ((gr = getgrnam(sp)) == NULL)
1.9 otto 538: errx(STATUS_BADUSAGE, "unknown group `%s'", sp);
1.1 millert 539: li->li_number = gr->gr_gid;
540: break;
541: case LT_TTY:
542: if (strcmp(sp, "-") == 0) {
543: li->li_number = -1;
544: break;
545: } else if (strcmp(sp, "co") == 0)
546: p = "console";
547: else if (strncmp(sp, "tty", 3) == 0)
548: p = sp;
549: else
550: p = NULL;
551:
552: if (p == NULL)
553: snprintf(buf, sizeof(buf), "/dev/tty%s", sp);
554: else
555: snprintf(buf, sizeof(buf), "/dev/%s", p);
556:
557: if (stat(buf, &st) < 0) {
558: if (errno == ENOENT)
559: errx(STATUS_BADUSAGE,
560: "no such tty: `%s'", sp);
561: err(STATUS_ERROR, "stat(%s)", sp);
562: }
563:
1.15 otto 564: if (!S_ISCHR(st.st_mode))
1.1 millert 565: errx(STATUS_BADUSAGE, "not a tty: `%s'", sp);
566:
567: li->li_number = st.st_rdev;
568: break;
569: default:
570: usage();
1.3 deraadt 571: }
1.1 millert 572: }
573:
574: if (empty)
575: usage();
576: }