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