xref: /netbsd-src/usr.bin/pkill/pkill.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: pkill.c,v 1.27 2010/12/07 07:39:15 mrg Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: pkill.c,v 1.27 2010/12/07 07:39:15 mrg Exp $");
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>
46 #include <limits.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <signal.h>
50 #include <regex.h>
51 #include <ctype.h>
52 #include <kvm.h>
53 #include <err.h>
54 #include <pwd.h>
55 #include <grp.h>
56 #include <errno.h>
57 #include <paths.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 
80 static struct kinfo_proc2	*plist;
81 static char	*selected;
82 static const char *delim = "\n";
83 static int	nproc;
84 static int	pgrep;
85 static int	prenice;
86 static int	signum = SIGTERM;
87 static int	nicenum;
88 static int	newest;
89 static int	inverse;
90 static int	longfmt;
91 static int	matchargs;
92 static int	fullmatch;
93 static int	cflags = REG_EXTENDED;
94 static kvm_t	*kd;
95 static pid_t	mypid;
96 
97 static struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
98 static struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
99 static struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
100 static struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
101 static struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
102 static struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
103 static struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
104 
105 int	main(int, char **);
106 static void	usage(void) __dead;
107 static int	killact(const struct kinfo_proc2 *);
108 static int	reniceact(const struct kinfo_proc2 *);
109 static int	grepact(const struct kinfo_proc2 *);
110 static void	makelist(struct listhead *, enum listtype, char *);
111 
112 int
113 main(int argc, char **argv)
114 {
115 	char buf[_POSIX2_LINE_MAX], **pargv, *q;
116 	int i, j, ch, bestidx, rv, criteria;
117 	int (*action)(const struct kinfo_proc2 *);
118 	const struct kinfo_proc2 *kp;
119 	struct list *li;
120 	const char *mstr, *p;
121 	u_int32_t bestsec, bestusec;
122 	regex_t reg;
123 	regmatch_t regmatch;
124 
125 	setprogname(argv[0]);
126 
127 	if (strcmp(getprogname(), "pgrep") == 0) {
128 		action = grepact;
129 		pgrep = 1;
130 	} else if (strcmp(getprogname(), "prenice") == 0) {
131 		prenice = 1;
132 
133 	} else {
134 		action = killact;
135 		p = argv[1];
136 
137 		if (argc > 1 && p[0] == '-') {
138 			p++;
139 			i = (int)strtol(p, &q, 10);
140 			if (*q == '\0') {
141 				signum = i;
142 				argv++;
143 				argc--;
144 			} else {
145 				if (strncasecmp(p, "sig", 3) == 0)
146 					p += 3;
147 				for (i = 1; i < NSIG; i++)
148 					if (strcasecmp(sys_signame[i], p) == 0)
149 						break;
150 				if (i != NSIG) {
151 					signum = i;
152 					argv++;
153 					argc--;
154 				}
155 			}
156 		}
157 	}
158 
159 	criteria = 0;
160 
161 	if (prenice) {
162 		if (argc < 2)
163 			usage();
164 
165 		if (strcmp(argv[1], "-l") == 0) {
166 			longfmt = 1;
167 			argv++;
168 			argc--;
169 		}
170 
171 		if (argc < 2)
172 			usage();
173 
174 		action = reniceact;
175 		p = argv[1];
176 
177 		i = (int)strtol(p, &q, 10);
178 		if (*q == '\0') {
179 			nicenum = i;
180 			argv++;
181 			argc--;
182 		} else
183 			usage();
184 	} else {
185 		while ((ch = getopt(argc, argv, "G:P:U:d:fg:ilns:t:u:vx")) != -1)
186 			switch (ch) {
187 			case 'G':
188 				makelist(&rgidlist, LT_GROUP, optarg);
189 				criteria = 1;
190 				break;
191 			case 'P':
192 				makelist(&ppidlist, LT_GENERIC, optarg);
193 				criteria = 1;
194 				break;
195 			case 'U':
196 				makelist(&ruidlist, LT_USER, optarg);
197 				criteria = 1;
198 				break;
199 			case 'd':
200 				if (!pgrep)
201 					usage();
202 				delim = optarg;
203 				break;
204 			case 'f':
205 				matchargs = 1;
206 				break;
207 			case 'g':
208 				makelist(&pgrplist, LT_PGRP, optarg);
209 				criteria = 1;
210 				break;
211 			case 'i':
212 				cflags |= REG_ICASE;
213 				break;
214 			case 'l':
215 				longfmt = 1;
216 				break;
217 			case 'n':
218 				newest = 1;
219 				criteria = 1;
220 				break;
221 			case 's':
222 				makelist(&sidlist, LT_SID, optarg);
223 				criteria = 1;
224 				break;
225 			case 't':
226 				makelist(&tdevlist, LT_TTY, optarg);
227 				criteria = 1;
228 				break;
229 			case 'u':
230 				makelist(&euidlist, LT_USER, optarg);
231 				criteria = 1;
232 				break;
233 			case 'v':
234 				inverse = 1;
235 				break;
236 			case 'x':
237 				fullmatch = 1;
238 				break;
239 			default:
240 				usage();
241 				/* NOTREACHED */
242 			}
243 		argc -= optind;
244 		argv += optind;
245 	}
246 
247 	if (argc != 0)
248 		criteria = 1;
249 	if (!criteria)
250 		usage();
251 
252 	mypid = getpid();
253 
254 	/*
255 	 * Retrieve the list of running processes from the kernel.
256 	 */
257 	kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf);
258 	if (kd == NULL)
259 		errx(STATUS_ERROR, "Cannot open kernel files (%s)", buf);
260 
261 	plist = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc);
262 	if (plist == NULL)
263 		errx(STATUS_ERROR, "Cannot get process list (%s)",
264 		    kvm_geterr(kd));
265 
266 	/*
267 	 * Allocate memory which will be used to keep track of the
268 	 * selection.
269 	 */
270 	if ((selected = calloc((size_t)1, (size_t)nproc)) == NULL)
271 		err(STATUS_ERROR, "Cannot allocate memory for %d processes",
272 		    nproc);
273 
274 	/*
275 	 * Refine the selection.
276 	 */
277 	for (; *argv != NULL; argv++) {
278 		if ((rv = regcomp(&reg, *argv, cflags)) != 0) {
279 			(void)regerror(rv, &reg, buf, sizeof(buf));
280 			errx(STATUS_BADUSAGE,
281 			    "Cannot compile regular expression `%s' (%s)",
282 			    *argv, buf);
283 		}
284 
285 		for (i = 0, kp = plist; i < nproc; i++, kp++) {
286 			if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
287 				continue;
288 
289 			if (matchargs) {
290 				if ((pargv = kvm_getargv2(kd, kp, 0)) == NULL)
291 					continue;
292 
293 				j = 0;
294 				while (j < (int)sizeof(buf) && *pargv != NULL) {
295 					j += snprintf(buf + j, sizeof(buf) - j,
296 					    pargv[1] != NULL ? "%s " : "%s",
297 					    pargv[0]);
298 					pargv++;
299 				}
300 
301 				mstr = buf;
302 			} else
303 				mstr = kp->p_comm;
304 
305 			rv = regexec(&reg, mstr, 1, &regmatch, 0);
306 			if (rv == 0) {
307 				if (fullmatch) {
308 					if (regmatch.rm_so == 0 &&
309 					    regmatch.rm_eo == (regoff_t)strlen(mstr))
310 						selected[i] = 1;
311 				} else
312 					selected[i] = 1;
313 			} else if (rv != REG_NOMATCH) {
314 				(void)regerror(rv, &reg, buf, sizeof(buf));
315 				errx(STATUS_ERROR,
316 				    "Regular expression evaluation error (%s)",
317 				    buf);
318 			}
319 		}
320 
321 		regfree(&reg);
322 	}
323 
324 	for (i = 0, kp = plist; i < nproc; i++, kp++) {
325 		if ((kp->p_flag & P_SYSTEM) != 0)
326 			continue;
327 
328 		SLIST_FOREACH(li, &ruidlist, li_chain)
329 			if (kp->p_ruid == (uid_t)li->li_number)
330 				break;
331 		if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {
332 			selected[i] = 0;
333 			continue;
334 		}
335 
336 		SLIST_FOREACH(li, &rgidlist, li_chain)
337 			if (kp->p_rgid == (gid_t)li->li_number)
338 				break;
339 		if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {
340 			selected[i] = 0;
341 			continue;
342 		}
343 
344 		SLIST_FOREACH(li, &euidlist, li_chain)
345 			if (kp->p_uid == (uid_t)li->li_number)
346 				break;
347 		if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
348 			selected[i] = 0;
349 			continue;
350 		}
351 
352 		SLIST_FOREACH(li, &ppidlist, li_chain)
353 			if ((uid_t)kp->p_ppid == (uid_t)li->li_number)
354 				break;
355 		if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
356 			selected[i] = 0;
357 			continue;
358 		}
359 
360 		SLIST_FOREACH(li, &pgrplist, li_chain)
361 			if (kp->p__pgid == (pid_t)li->li_number)
362 				break;
363 		if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
364 			selected[i] = 0;
365 			continue;
366 		}
367 
368 		SLIST_FOREACH(li, &tdevlist, li_chain) {
369 			if (li->li_number == -1 &&
370 			    (kp->p_flag & P_CONTROLT) == 0)
371 				break;
372 			if (kp->p_tdev == (uid_t)li->li_number)
373 				break;
374 		}
375 		if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
376 			selected[i] = 0;
377 			continue;
378 		}
379 
380 		SLIST_FOREACH(li, &sidlist, li_chain)
381 			if (kp->p_sid == (pid_t)li->li_number)
382 				break;
383 		if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
384 			selected[i] = 0;
385 			continue;
386 		}
387 
388 		if (argc == 0)
389 			selected[i] = 1;
390 	}
391 
392 	if (newest) {
393 		bestsec = 0;
394 		bestusec = 0;
395 		bestidx = -1;
396 
397 		for (i = 0, kp = plist; i < nproc; i++, kp++) {
398 			if (!selected[i])
399 				continue;
400 
401 			if (kp->p_ustart_sec > bestsec ||
402 			    (kp->p_ustart_sec == bestsec
403 			    && kp->p_ustart_usec > bestusec)) {
404 			    	bestsec = kp->p_ustart_sec;
405 			    	bestusec = kp->p_ustart_usec;
406 				bestidx = i;
407 			}
408 		}
409 
410 		(void)memset(selected, 0, (size_t)nproc);
411 		if (bestidx != -1)
412 			selected[bestidx] = 1;
413 	}
414 
415 	/*
416 	 * Take the appropriate action for each matched process, if any.
417 	 */
418 	for (i = 0, rv = 0, kp = plist; i < nproc; i++, kp++) {
419 		if (kp->p_pid == mypid)
420 			continue;
421 		if (selected[i]) {
422 			if (inverse)
423 				continue;
424 		} else if (!inverse)
425 			continue;
426 
427 		if ((kp->p_flag & P_SYSTEM) != 0)
428 			continue;
429 
430 		rv |= (*action)(kp);
431 	}
432 
433 	return rv ? STATUS_MATCH : STATUS_NOMATCH;
434 }
435 
436 static void
437 usage(void)
438 {
439 	const char *ustr;
440 
441 	if (prenice)
442 		fprintf(stderr, "Usage: %s [-l] priority pattern ...\n",
443 		    getprogname());
444 	else {
445 		if (pgrep)
446 			ustr = "[-filnvx] [-d delim]";
447 		else
448 			ustr = "[-signal] [-filnvx]";
449 
450 		(void)fprintf(stderr,
451 		    "Usage: %s %s [-G gid] [-g pgrp] [-P ppid] [-s sid] "
452 			   "[-t tty]\n"
453 		    "             [-U uid] [-u euid] pattern ...\n",
454 			      getprogname(), ustr);
455 	}
456 
457 	exit(STATUS_BADUSAGE);
458 }
459 
460 static int
461 killact(const struct kinfo_proc2 *kp)
462 {
463 	if (longfmt)
464 		grepact(kp);
465 	if (kill(kp->p_pid, signum) == -1) {
466 
467 		/*
468 		 * Check for ESRCH, which indicates that the process
469 		 * disappeared between us matching it and us
470 		 * signalling it; don't issue a warning about it.
471 		 */
472 		if (errno != ESRCH)
473 			warn("signalling pid %d", (int)kp->p_pid);
474 
475 		/*
476 		 * Return 0 to indicate that the process should not be
477 		 * considered a match, since we didn't actually get to
478 		 * signal it.
479 		 */
480 		return 0;
481 	}
482 
483 	return 1;
484 }
485 
486 static int
487 reniceact(const struct kinfo_proc2 *kp)
488 {
489 	int oldprio;
490 
491 	if (longfmt)
492 		grepact(kp);
493 
494 	errno = 0;
495 	if ((oldprio = getpriority(PRIO_PROCESS, kp->p_pid)) == -1 &&
496 	    errno != 0) {
497 		warn("%d: getpriority", kp->p_pid);
498 		return 0;
499 	}
500 
501 	if (setpriority(PRIO_PROCESS, kp->p_pid, nicenum) == -1) {
502 		warn("%d: setpriority", kp->p_pid);
503 		return 0;
504 	}
505 
506 	(void)printf("%d: old priority %d, new priority %d\n",
507 	    kp->p_pid, oldprio, nicenum);
508 
509 	return 1;
510 }
511 
512 static int
513 grepact(const struct kinfo_proc2 *kp)
514 {
515 	char **argv;
516 
517 	if (longfmt && matchargs) {
518 
519 		/*
520 		 * If kvm_getargv2() failed the process has probably
521 		 * disappeared.  Return 0 to indicate that the process
522 		 * should not be considered a match, since we are no
523 		 * longer in a position to output it as a match.
524 		 */
525 		if ((argv = kvm_getargv2(kd, kp, 0)) == NULL)
526 			return 0;
527 
528 		(void)printf("%d ", (int)kp->p_pid);
529 		for (; *argv != NULL; argv++) {
530 			(void)printf("%s", *argv);
531 			if (argv[1] != NULL)
532 				(void)putchar(' ');
533 		}
534 	} else if (longfmt)
535 		(void)printf("%d %s", (int)kp->p_pid, kp->p_comm);
536 	else
537 		(void)printf("%d", (int)kp->p_pid);
538 
539 	(void)printf("%s", delim);
540 
541 	return 1;
542 }
543 
544 static void
545 makelist(struct listhead *head, enum listtype type, char *src)
546 {
547 	struct list *li;
548 	struct passwd *pw;
549 	struct group *gr;
550 	struct stat st;
551 	char *sp, *ep, buf[MAXPATHLEN];
552 	const char *p;
553 	int empty;
554 	const char *prefix = _PATH_DEV;
555 
556 	empty = 1;
557 
558 	while ((sp = strsep(&src, ",")) != NULL) {
559 		if (*sp == '\0')
560 			usage();
561 
562 		if ((li = malloc(sizeof(*li))) == NULL)
563 			err(STATUS_ERROR, "Cannot allocate %zd bytes",
564 			    sizeof(*li));
565 		SLIST_INSERT_HEAD(head, li, li_chain);
566 		empty = 0;
567 
568 		li->li_number = (uid_t)strtol(sp, &ep, 0);
569 		if (*ep == '\0' && type != LT_TTY) {
570 			switch (type) {
571 			case LT_PGRP:
572 				if (li->li_number == 0)
573 					li->li_number = getpgrp();
574 				break;
575 			case LT_SID:
576 				if (li->li_number == 0)
577 					li->li_number = getsid(mypid);
578 				break;
579 			default:
580 				break;
581 			}
582 			continue;
583 		}
584 
585 		switch (type) {
586 		case LT_USER:
587 			if ((pw = getpwnam(sp)) == NULL)
588 				errx(STATUS_BADUSAGE, "Unknown user `%s'",
589 				    sp);
590 			li->li_number = pw->pw_uid;
591 			break;
592 		case LT_GROUP:
593 			if ((gr = getgrnam(sp)) == NULL)
594 				errx(STATUS_BADUSAGE, "Unknown group `%s'",
595 				    sp);
596 			li->li_number = gr->gr_gid;
597 			break;
598 		case LT_TTY:
599 			p = sp;
600 			if (*sp == '/')
601 				prefix = "";
602 			else if (strcmp(sp, "-") == 0) {
603 				li->li_number = -1;
604 				break;
605 			} else if (strcmp(sp, "co") == 0)
606 				p = "console";
607 			else if (strncmp(sp, "tty", 3) == 0)
608 				/* all set */;
609 			else if (strncmp(sp, "pts/", 4) == 0)
610 				/* all set */;
611 			else if (*ep != '\0' || (strlen(sp) == 2 && *sp == '0'))
612 				prefix = _PATH_TTY;
613 			else
614 				prefix = _PATH_DEV_PTS;
615 
616 			(void)snprintf(buf, sizeof(buf), "%s%s", prefix, p);
617 
618 			if (stat(buf, &st) == -1) {
619 				if (errno == ENOENT)
620 					errx(STATUS_BADUSAGE,
621 					    "No such tty: `%s'", buf);
622 				err(STATUS_ERROR, "Cannot access `%s'", buf);
623 			}
624 
625 			if ((st.st_mode & S_IFCHR) == 0)
626 				errx(STATUS_BADUSAGE, "Not a tty: `%s'", buf);
627 
628 			li->li_number = st.st_rdev;
629 			break;
630 		default:
631 			usage();
632 		}
633 	}
634 
635 	if (empty)
636 		usage();
637 }
638