Annotation of src/usr.bin/pkill/pkill.c, Revision 1.17
1.17 ! ray 1: /* $OpenBSD: pkill.c,v 1.16 2008/02/07 15:38:07 millert 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: #ifndef lint
1.17 ! ray 34: static const char rcsid[] = "$OpenBSD: pkill.c,v 1.16 2008/02/07 15:38:07 millert Exp $";
1.1 millert 35: #endif /* !lint */
36:
37: #include <sys/types.h>
38: #include <sys/param.h>
39: #include <sys/sysctl.h>
40: #include <sys/proc.h>
41: #include <sys/queue.h>
42: #include <sys/stat.h>
43:
44: #include <stdio.h>
45: #include <stdlib.h>
1.16 millert 46: #include <stdint.h>
1.1 millert 47: #include <limits.h>
48: #include <string.h>
49: #include <unistd.h>
50: #include <signal.h>
51: #include <regex.h>
52: #include <ctype.h>
53: #include <kvm.h>
54: #include <err.h>
55: #include <pwd.h>
56: #include <grp.h>
57: #include <errno.h>
58:
59: #define STATUS_MATCH 0
60: #define STATUS_NOMATCH 1
61: #define STATUS_BADUSAGE 2
62: #define STATUS_ERROR 3
63:
64: enum listtype {
65: LT_GENERIC,
66: LT_USER,
67: LT_GROUP,
68: LT_TTY,
69: LT_PGRP,
70: LT_SID
71: };
72:
73: struct list {
74: SLIST_ENTRY(list) li_chain;
75: long li_number;
76: };
77:
78: SLIST_HEAD(listhead, list);
79:
1.2 millert 80: struct kinfo_proc2 *plist;
1.1 millert 81: char *selected;
82: char *delim = "\n";
83: int nproc;
84: int pgrep;
85: int signum = SIGTERM;
86: int newest;
1.16 millert 87: int oldest;
1.1 millert 88: int inverse;
89: int longfmt;
90: int matchargs;
91: int fullmatch;
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);
102:
103: int main(int, char **);
104: void usage(void);
1.8 millert 105: int killact(struct kinfo_proc2 *, int);
106: int grepact(struct kinfo_proc2 *, int);
1.1 millert 107: void makelist(struct listhead *, enum listtype, char *);
108:
109: extern char *__progname;
110:
111: int
112: main(int argc, char **argv)
113: {
114: extern char *optarg;
115: extern int optind;
1.2 millert 116: char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;
1.1 millert 117: int i, j, ch, bestidx, rv, criteria;
1.8 millert 118: int (*action)(struct kinfo_proc2 *, int);
1.2 millert 119: struct kinfo_proc2 *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.16 millert 156: while ((ch = getopt(argc, argv, "G:P: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;
166: case 'U':
167: makelist(&ruidlist, LT_USER, optarg);
168: criteria = 1;
169: break;
170: case 'd':
171: if (!pgrep)
172: usage();
173: delim = optarg;
174: break;
175: case 'f':
176: matchargs = 1;
177: break;
178: case 'g':
179: makelist(&pgrplist, LT_PGRP, optarg);
180: criteria = 1;
181: break;
182: case 'l':
183: if (!pgrep)
184: usage();
185: longfmt = 1;
186: break;
187: case 'n':
188: newest = 1;
189: criteria = 1;
190: break;
1.16 millert 191: case 'o':
192: oldest = 1;
193: criteria = 1;
194: break;
1.1 millert 195: case 's':
196: makelist(&sidlist, LT_SID, optarg);
197: criteria = 1;
198: break;
199: case 't':
200: makelist(&tdevlist, LT_TTY, optarg);
201: criteria = 1;
202: break;
203: case 'u':
204: makelist(&euidlist, LT_USER, optarg);
205: criteria = 1;
206: break;
207: case 'v':
208: inverse = 1;
209: break;
210: case 'x':
211: fullmatch = 1;
212: break;
213: default:
214: usage();
215: /* NOTREACHED */
216: }
217:
218: argc -= optind;
219: argv += optind;
220: if (argc != 0)
221: criteria = 1;
1.16 millert 222: if (!criteria || (newest && oldest))
1.1 millert 223: usage();
224:
225: mypid = getpid();
226:
227: /*
228: * Retrieve the list of running processes from the kernel.
229: */
230: kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf);
231: if (kd == NULL)
232: errx(STATUS_ERROR, "kvm_openfiles(): %s", buf);
233:
1.2 millert 234: plist = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc);
1.1 millert 235: if (plist == NULL)
1.2 millert 236: errx(STATUS_ERROR, "kvm_getproc2() failed");
1.1 millert 237:
238: /*
239: * Allocate memory which will be used to keep track of the
240: * selection.
241: */
242: if ((selected = malloc(nproc)) == NULL)
243: errx(STATUS_ERROR, "memory allocation failure");
244: memset(selected, 0, nproc);
245:
246: /*
247: * Refine the selection.
248: */
249: for (; *argv != NULL; argv++) {
1.11 robert 250: if ((rv = regcomp(®, *argv, REG_EXTENDED)) != 0) {
1.1 millert 251: regerror(rv, ®, buf, sizeof(buf));
252: errx(STATUS_BADUSAGE, "bad expression: %s", buf);
253: }
254:
255: for (i = 0, kp = plist; i < nproc; i++, kp++) {
1.2 millert 256: if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
1.1 millert 257: continue;
258:
259: if (matchargs) {
1.2 millert 260: if ((pargv = kvm_getargv2(kd, kp, 0)) == NULL)
1.1 millert 261: continue;
262:
263: j = 0;
264: while (j < sizeof(buf) && *pargv != NULL) {
1.7 deraadt 265: int ret;
266:
267: ret = snprintf(buf + j, sizeof(buf) - j,
1.1 millert 268: pargv[1] != NULL ? "%s " : "%s",
269: pargv[0]);
1.12 deraadt 270: if (ret >= sizeof(buf) - j)
271: j += sizeof(buf) - j - 1;
272: else if (ret > 0)
1.7 deraadt 273: j += ret;
1.1 millert 274: pargv++;
275: }
276:
277: mstr = buf;
278: } else
1.2 millert 279: mstr = kp->p_comm;
1.1 millert 280:
281: rv = regexec(®, mstr, 1, ®match, 0);
282: if (rv == 0) {
283: if (fullmatch) {
284: if (regmatch.rm_so == 0 &&
285: regmatch.rm_eo == strlen(mstr))
286: selected[i] = 1;
287: } else
288: selected[i] = 1;
289: } else if (rv != REG_NOMATCH) {
290: regerror(rv, ®, buf, sizeof(buf));
291: errx(STATUS_ERROR, "regexec(): %s", buf);
292: }
293: }
294:
295: regfree(®);
296: }
297:
298: for (i = 0, kp = plist; i < nproc; i++, kp++) {
1.5 mpech 299: if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
1.1 millert 300: continue;
301:
302: SLIST_FOREACH(li, &ruidlist, li_chain)
1.2 millert 303: if (kp->p_ruid == (uid_t)li->li_number)
1.1 millert 304: break;
305: if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {
306: selected[i] = 0;
307: continue;
308: }
1.3 deraadt 309:
1.1 millert 310: SLIST_FOREACH(li, &rgidlist, li_chain)
1.2 millert 311: if (kp->p_rgid == (gid_t)li->li_number)
1.1 millert 312: break;
313: if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {
314: selected[i] = 0;
315: continue;
316: }
317:
318: SLIST_FOREACH(li, &euidlist, li_chain)
1.2 millert 319: if (kp->p_uid == (uid_t)li->li_number)
1.1 millert 320: break;
321: if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
322: selected[i] = 0;
323: continue;
324: }
325:
326: SLIST_FOREACH(li, &ppidlist, li_chain)
1.2 millert 327: if (kp->p_ppid == (uid_t)li->li_number)
1.1 millert 328: break;
329: if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
330: selected[i] = 0;
331: continue;
332: }
333:
334: SLIST_FOREACH(li, &pgrplist, li_chain)
1.2 millert 335: if (kp->p__pgid == (uid_t)li->li_number)
1.1 millert 336: break;
337: if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
338: selected[i] = 0;
339: continue;
340: }
341:
342: SLIST_FOREACH(li, &tdevlist, li_chain) {
343: if (li->li_number == -1 &&
1.2 millert 344: (kp->p_flag & P_CONTROLT) == 0)
1.1 millert 345: break;
1.2 millert 346: if (kp->p_tdev == (uid_t)li->li_number)
1.1 millert 347: break;
348: }
349: if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
350: selected[i] = 0;
351: continue;
352: }
353:
354: SLIST_FOREACH(li, &sidlist, li_chain)
1.2 millert 355: if (kp->p_sid == (uid_t)li->li_number)
1.1 millert 356: break;
357: if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
358: selected[i] = 0;
359: continue;
360: }
361:
362: if (argc == 0)
363: selected[i] = 1;
364: }
365:
1.16 millert 366: if (newest || oldest) {
1.1 millert 367: bestidx = -1;
368:
1.16 millert 369: if (newest)
370: bestsec = bestusec = 0;
371: else
372: bestsec = bestusec = UINT32_MAX;
373:
1.1 millert 374: for (i = 0, kp = plist; i < nproc; i++, kp++) {
375: if (!selected[i])
376: continue;
377:
1.16 millert 378: if ((newest && (kp->p_ustart_sec > bestsec ||
1.2 millert 379: (kp->p_ustart_sec == bestsec
1.16 millert 380: && kp->p_ustart_usec > bestusec)))
381: || (oldest && (kp->p_ustart_sec < bestsec ||
382: (kp->p_ustart_sec == bestsec
383: && kp->p_ustart_usec < bestusec)))) {
384:
1.3 deraadt 385: bestsec = kp->p_ustart_sec;
386: bestusec = kp->p_ustart_usec;
1.1 millert 387: bestidx = i;
388: }
389: }
390:
391: memset(selected, 0, nproc);
392: if (bestidx != -1)
393: selected[bestidx] = 1;
394: }
395:
396: /*
397: * Take the appropriate action for each matched process, if any.
398: */
1.8 millert 399: rv = STATUS_NOMATCH;
400: for (i = 0, j = 0, kp = plist; i < nproc; i++, kp++) {
1.5 mpech 401: if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
1.1 millert 402: continue;
403: if (selected[i]) {
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.16 millert 428: ustr = "[-signal] [-fnovx]";
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.8 millert 437: killact(struct kinfo_proc2 *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.8 millert 448: grepact(struct kinfo_proc2 *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.2 millert 455: if ((argv = kvm_getargv2(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: }