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