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