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