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