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