xref: /freebsd/usr.bin/killall/killall.c (revision de555084834b10902e8333fdd76c93fa6f71be03)
11198f21cSPeter Wemm /*-
21198f21cSPeter Wemm  * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org>
31198f21cSPeter Wemm  * Copyright (c) 2000 Paul Saab <ps@FreeBSD.org>
41198f21cSPeter Wemm  * All rights reserved.
51198f21cSPeter Wemm  *
61198f21cSPeter Wemm  * Redistribution and use in source and binary forms, with or without
71198f21cSPeter Wemm  * modification, are permitted provided that the following conditions
81198f21cSPeter Wemm  * are met:
91198f21cSPeter Wemm  * 1. Redistributions of source code must retain the above copyright
101198f21cSPeter Wemm  *    notice, this list of conditions and the following disclaimer.
111198f21cSPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
121198f21cSPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
131198f21cSPeter Wemm  *    documentation and/or other materials provided with the distribution.
141198f21cSPeter Wemm  *
151198f21cSPeter Wemm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161198f21cSPeter Wemm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171198f21cSPeter Wemm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181198f21cSPeter Wemm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191198f21cSPeter Wemm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201198f21cSPeter Wemm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211198f21cSPeter Wemm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221198f21cSPeter Wemm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231198f21cSPeter Wemm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241198f21cSPeter Wemm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251198f21cSPeter Wemm  * SUCH DAMAGE.
261198f21cSPeter Wemm  */
271198f21cSPeter Wemm 
288750ed5fSPeter Wemm #include <sys/cdefs.h>
29e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
30e026a48cSDavid E. O'Brien 
311198f21cSPeter Wemm #include <sys/param.h>
32ebf5d9bcSMike Barcroft #include <sys/jail.h>
331198f21cSPeter Wemm #include <sys/stat.h>
3473d0971bSJamie Gritton #include <sys/uio.h>
351198f21cSPeter Wemm #include <sys/user.h>
361198f21cSPeter Wemm #include <sys/sysctl.h>
37821df508SXin LI #include <fcntl.h>
38821df508SXin LI #include <dirent.h>
39de6f3704SJamie Gritton #include <jail.h>
401198f21cSPeter Wemm #include <stdio.h>
411198f21cSPeter Wemm #include <stdlib.h>
421198f21cSPeter Wemm #include <string.h>
431198f21cSPeter Wemm #include <pwd.h>
441198f21cSPeter Wemm #include <signal.h>
451198f21cSPeter Wemm #include <regex.h>
461198f21cSPeter Wemm #include <ctype.h>
471198f21cSPeter Wemm #include <err.h>
481198f21cSPeter Wemm #include <errno.h>
491198f21cSPeter Wemm #include <unistd.h>
50c5453cb4STim J. Robbins #include <locale.h>
511198f21cSPeter Wemm 
528750ed5fSPeter Wemm static void __dead2
531198f21cSPeter Wemm usage(void)
541198f21cSPeter Wemm {
551198f21cSPeter Wemm 
5673d0971bSJamie Gritton 	fprintf(stderr, "usage: killall [-delmsvz] [-help] [-j jail]\n");
57ebf5d9bcSMike Barcroft 	fprintf(stderr,
586dcfd347SMaxim Konovalov 	    "               [-u user] [-t tty] [-c cmd] [-SIGNAL] [cmd]...\n");
591198f21cSPeter Wemm 	fprintf(stderr, "At least one option or argument to specify processes must be given.\n");
601198f21cSPeter Wemm 	exit(1);
611198f21cSPeter Wemm }
621198f21cSPeter Wemm 
631198f21cSPeter Wemm 
641198f21cSPeter Wemm static void
651198f21cSPeter Wemm printsig(FILE *fp)
661198f21cSPeter Wemm {
671198f21cSPeter Wemm 	const char	*const * p;
681198f21cSPeter Wemm 	int		cnt;
691198f21cSPeter Wemm 	int		offset = 0;
701198f21cSPeter Wemm 
711198f21cSPeter Wemm 	for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
72*de555084SJilles Tjoelker 		offset += fprintf(fp, "%s ", *p);
731198f21cSPeter Wemm 		if (offset >= 75 && cnt > 1) {
741198f21cSPeter Wemm 			offset = 0;
751198f21cSPeter Wemm 			fprintf(fp, "\n");
761198f21cSPeter Wemm 		}
771198f21cSPeter Wemm 	}
781198f21cSPeter Wemm 	fprintf(fp, "\n");
791198f21cSPeter Wemm }
801198f21cSPeter Wemm 
811198f21cSPeter Wemm static void
821198f21cSPeter Wemm nosig(char *name)
831198f21cSPeter Wemm {
841198f21cSPeter Wemm 
851198f21cSPeter Wemm 	warnx("unknown signal %s; valid signals:", name);
861198f21cSPeter Wemm 	printsig(stderr);
871198f21cSPeter Wemm 	exit(1);
881198f21cSPeter Wemm }
891198f21cSPeter Wemm 
901198f21cSPeter Wemm int
911198f21cSPeter Wemm main(int ac, char **av)
921198f21cSPeter Wemm {
931198f21cSPeter Wemm 	struct kinfo_proc *procs = NULL, *newprocs;
941198f21cSPeter Wemm 	struct stat	sb;
951198f21cSPeter Wemm 	struct passwd	*pw;
961198f21cSPeter Wemm 	regex_t		rgx;
971198f21cSPeter Wemm 	regmatch_t	pmatch;
981198f21cSPeter Wemm 	int		i, j;
991198f21cSPeter Wemm 	char		buf[256];
1001198f21cSPeter Wemm 	char		*user = NULL;
1011198f21cSPeter Wemm 	char		*tty = NULL;
1021198f21cSPeter Wemm 	char		*cmd = NULL;
1031198f21cSPeter Wemm 	int		vflag = 0;
1041198f21cSPeter Wemm 	int		sflag = 0;
1051198f21cSPeter Wemm 	int		dflag = 0;
1065aa41214SDiomidis Spinellis 	int		eflag = 0;
107ebf5d9bcSMike Barcroft 	int		jflag = 0;
1081198f21cSPeter Wemm 	int		mflag = 0;
109a2fefc8dSDag-Erling Smørgrav 	int		zflag = 0;
1101198f21cSPeter Wemm 	uid_t		uid = 0;
1111198f21cSPeter Wemm 	dev_t		tdev = 0;
112b4e148e9SColin Percival 	pid_t		mypid;
1131198f21cSPeter Wemm 	char		thiscmd[MAXCOMLEN + 1];
1141198f21cSPeter Wemm 	pid_t		thispid;
1151198f21cSPeter Wemm 	uid_t		thisuid;
1161198f21cSPeter Wemm 	dev_t		thistdev;
1171198f21cSPeter Wemm 	int		sig = SIGTERM;
1181198f21cSPeter Wemm 	const char *const *p;
1191198f21cSPeter Wemm 	char		*ep;
1201198f21cSPeter Wemm 	int		errors = 0;
121ebf5d9bcSMike Barcroft 	int		jid;
12249a4598bSPoul-Henning Kamp 	int		mib[4];
1231198f21cSPeter Wemm 	size_t		miblen;
1241198f21cSPeter Wemm 	int		st, nprocs;
1251198f21cSPeter Wemm 	size_t		size;
1261198f21cSPeter Wemm 	int		matched;
1271198f21cSPeter Wemm 	int		killed = 0;
1281198f21cSPeter Wemm 
129c5453cb4STim J. Robbins 	setlocale(LC_ALL, "");
130c5453cb4STim J. Robbins 
1311198f21cSPeter Wemm 	av++;
1321198f21cSPeter Wemm 	ac--;
1331198f21cSPeter Wemm 
1341198f21cSPeter Wemm 	while (ac > 0) {
1351198f21cSPeter Wemm 		if (strcmp(*av, "-l") == 0) {
1361198f21cSPeter Wemm 			printsig(stdout);
1371198f21cSPeter Wemm 			exit(0);
1381198f21cSPeter Wemm 		}
1398750ed5fSPeter Wemm 		if (strcmp(*av, "-help") == 0)
1408750ed5fSPeter Wemm 			usage();
1411198f21cSPeter Wemm 		if (**av == '-') {
1421198f21cSPeter Wemm 			++*av;
1431198f21cSPeter Wemm 			switch (**av) {
144ebf5d9bcSMike Barcroft 			case 'j':
145ebf5d9bcSMike Barcroft 				++*av;
146b8272c91SRuslan Ermilov 				if (**av == '\0') {
147ebf5d9bcSMike Barcroft 					++av;
148ebf5d9bcSMike Barcroft 					--ac;
149b8272c91SRuslan Ermilov 				}
150ebf5d9bcSMike Barcroft 				jflag++;
151b8272c91SRuslan Ermilov 				if (*av == NULL)
15273d0971bSJamie Gritton 				    	errx(1, "must specify jail");
153de6f3704SJamie Gritton 				jid = jail_getid(*av);
15473d0971bSJamie Gritton 				if (jid < 0)
155de6f3704SJamie Gritton 					errx(1, "%s", jail_errmsg);
156ebf5d9bcSMike Barcroft 				if (jail_attach(jid) == -1)
15773d0971bSJamie Gritton 					err(1, "jail_attach(%d)", jid);
158ebf5d9bcSMike Barcroft 				break;
1591198f21cSPeter Wemm 			case 'u':
1601198f21cSPeter Wemm 				++*av;
161b8272c91SRuslan Ermilov 				if (**av == '\0') {
1621198f21cSPeter Wemm 					++av;
1631198f21cSPeter Wemm 					--ac;
164b8272c91SRuslan Ermilov 				}
165b8272c91SRuslan Ermilov 				if (*av == NULL)
166b8272c91SRuslan Ermilov 				    	errx(1, "must specify user");
1671198f21cSPeter Wemm 				user = *av;
1681198f21cSPeter Wemm 				break;
1691198f21cSPeter Wemm 			case 't':
1701198f21cSPeter Wemm 				++*av;
171b8272c91SRuslan Ermilov 				if (**av == '\0') {
1721198f21cSPeter Wemm 					++av;
1731198f21cSPeter Wemm 					--ac;
174b8272c91SRuslan Ermilov 				}
175b8272c91SRuslan Ermilov 				if (*av == NULL)
176b8272c91SRuslan Ermilov 				    	errx(1, "must specify tty");
1771198f21cSPeter Wemm 				tty = *av;
1781198f21cSPeter Wemm 				break;
1791198f21cSPeter Wemm 			case 'c':
1801198f21cSPeter Wemm 				++*av;
181b8272c91SRuslan Ermilov 				if (**av == '\0') {
1821198f21cSPeter Wemm 					++av;
1831198f21cSPeter Wemm 					--ac;
184b8272c91SRuslan Ermilov 				}
185b8272c91SRuslan Ermilov 				if (*av == NULL)
186b8272c91SRuslan Ermilov 				    	errx(1, "must specify procname");
1871198f21cSPeter Wemm 				cmd = *av;
1881198f21cSPeter Wemm 				break;
1891198f21cSPeter Wemm 			case 'v':
1901198f21cSPeter Wemm 				vflag++;
1911198f21cSPeter Wemm 				break;
1921198f21cSPeter Wemm 			case 's':
1931198f21cSPeter Wemm 				sflag++;
1941198f21cSPeter Wemm 				break;
1951198f21cSPeter Wemm 			case 'd':
1961198f21cSPeter Wemm 				dflag++;
1971198f21cSPeter Wemm 				break;
1985aa41214SDiomidis Spinellis 			case 'e':
1995aa41214SDiomidis Spinellis 				eflag++;
2005aa41214SDiomidis Spinellis 				break;
2011198f21cSPeter Wemm 			case 'm':
2021198f21cSPeter Wemm 				mflag++;
2031198f21cSPeter Wemm 				break;
204a2fefc8dSDag-Erling Smørgrav 			case 'z':
205a2fefc8dSDag-Erling Smørgrav 				zflag++;
206a2fefc8dSDag-Erling Smørgrav 				break;
2071198f21cSPeter Wemm 			default:
208fe1ba53cSTim J. Robbins 				if (isalpha((unsigned char)**av)) {
20912dacf62SJilles Tjoelker 					if (strncasecmp(*av, "SIG", 3) == 0)
2101198f21cSPeter Wemm 						*av += 3;
2111198f21cSPeter Wemm 					for (sig = NSIG, p = sys_signame + 1;
2121198f21cSPeter Wemm 					     --sig; ++p)
2131198f21cSPeter Wemm 						if (strcasecmp(*p, *av) == 0) {
2141198f21cSPeter Wemm 							sig = p - sys_signame;
2151198f21cSPeter Wemm 							break;
2161198f21cSPeter Wemm 						}
2171198f21cSPeter Wemm 					if (!sig)
2181198f21cSPeter Wemm 						nosig(*av);
219fe1ba53cSTim J. Robbins 				} else if (isdigit((unsigned char)**av)) {
2201198f21cSPeter Wemm 					sig = strtol(*av, &ep, 10);
2211198f21cSPeter Wemm 					if (!*av || *ep)
2221198f21cSPeter Wemm 						errx(1, "illegal signal number: %s", *av);
22379d0f70fSMaxim Konovalov 					if (sig < 0 || sig >= NSIG)
2241198f21cSPeter Wemm 						nosig(*av);
2251198f21cSPeter Wemm 				} else
2261198f21cSPeter Wemm 					nosig(*av);
2271198f21cSPeter Wemm 			}
2281198f21cSPeter Wemm 			++av;
2291198f21cSPeter Wemm 			--ac;
2301198f21cSPeter Wemm 		} else {
2311198f21cSPeter Wemm 			break;
2321198f21cSPeter Wemm 		}
2331198f21cSPeter Wemm 	}
2341198f21cSPeter Wemm 
235ebf5d9bcSMike Barcroft 	if (user == NULL && tty == NULL && cmd == NULL && !jflag && ac == 0)
2361198f21cSPeter Wemm 		usage();
2371198f21cSPeter Wemm 
2381198f21cSPeter Wemm 	if (tty) {
2391198f21cSPeter Wemm 		if (strncmp(tty, "/dev/", 5) == 0)
2401198f21cSPeter Wemm 			snprintf(buf, sizeof(buf), "%s", tty);
2411198f21cSPeter Wemm 		else if (strncmp(tty, "tty", 3) == 0)
2421198f21cSPeter Wemm 			snprintf(buf, sizeof(buf), "/dev/%s", tty);
2431198f21cSPeter Wemm 		else
2441198f21cSPeter Wemm 			snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
2451198f21cSPeter Wemm 		if (stat(buf, &sb) < 0)
2461198f21cSPeter Wemm 			err(1, "stat(%s)", buf);
2471198f21cSPeter Wemm 		if (!S_ISCHR(sb.st_mode))
2481198f21cSPeter Wemm 			errx(1, "%s: not a character device", buf);
2491198f21cSPeter Wemm 		tdev = sb.st_rdev;
2501198f21cSPeter Wemm 		if (dflag)
2511198f21cSPeter Wemm 			printf("ttydev:0x%x\n", tdev);
2521198f21cSPeter Wemm 	}
2531198f21cSPeter Wemm 	if (user) {
2540d1b691eSDavid E. O'Brien 		uid = strtol(user, &ep, 10);
255fb757ee2SDavid Malone 		if (*user == '\0' || *ep != '\0') { /* was it a number? */
2561198f21cSPeter Wemm 			pw = getpwnam(user);
2571198f21cSPeter Wemm 			if (pw == NULL)
2581198f21cSPeter Wemm 				errx(1, "user %s does not exist", user);
2591198f21cSPeter Wemm 			uid = pw->pw_uid;
2601198f21cSPeter Wemm 			if (dflag)
2611198f21cSPeter Wemm 				printf("uid:%d\n", uid);
2620d1b691eSDavid E. O'Brien 		}
2631198f21cSPeter Wemm 	} else {
2641198f21cSPeter Wemm 		uid = getuid();
2651198f21cSPeter Wemm 		if (uid != 0) {
2661198f21cSPeter Wemm 			pw = getpwuid(uid);
2671198f21cSPeter Wemm 			if (pw)
2681198f21cSPeter Wemm 				user = pw->pw_name;
2691198f21cSPeter Wemm 			if (dflag)
2701198f21cSPeter Wemm 				printf("uid:%d\n", uid);
2711198f21cSPeter Wemm 		}
2721198f21cSPeter Wemm 	}
2731198f21cSPeter Wemm 	size = 0;
2741198f21cSPeter Wemm 	mib[0] = CTL_KERN;
2751198f21cSPeter Wemm 	mib[1] = KERN_PROC;
2762c801d3dSTim J. Robbins 	mib[2] = KERN_PROC_PROC;
2771198f21cSPeter Wemm 	mib[3] = 0;
278f2dd06abSTim J. Robbins 	miblen = 3;
2791198f21cSPeter Wemm 
2802c801d3dSTim J. Robbins 	if (user) {
2815aa41214SDiomidis Spinellis 		mib[2] = eflag ? KERN_PROC_UID : KERN_PROC_RUID;
2821198f21cSPeter Wemm 		mib[3] = uid;
283f2dd06abSTim J. Robbins 		miblen = 4;
2842c801d3dSTim J. Robbins 	} else if (tty) {
2851198f21cSPeter Wemm 		mib[2] = KERN_PROC_TTY;
2861198f21cSPeter Wemm 		mib[3] = tdev;
287f2dd06abSTim J. Robbins 		miblen = 4;
2881198f21cSPeter Wemm 	}
2891198f21cSPeter Wemm 
2901198f21cSPeter Wemm 	st = sysctl(mib, miblen, NULL, &size, NULL, 0);
2911198f21cSPeter Wemm 	do {
2921198f21cSPeter Wemm 		size += size / 10;
2931198f21cSPeter Wemm 		newprocs = realloc(procs, size);
2941198f21cSPeter Wemm 		if (newprocs == 0) {
2951198f21cSPeter Wemm 			if (procs)
2961198f21cSPeter Wemm 				free(procs);
2971198f21cSPeter Wemm 			errx(1, "could not reallocate memory");
2981198f21cSPeter Wemm 		}
2991198f21cSPeter Wemm 		procs = newprocs;
3001198f21cSPeter Wemm 		st = sysctl(mib, miblen, procs, &size, NULL, 0);
3011198f21cSPeter Wemm 	} while (st == -1 && errno == ENOMEM);
3021198f21cSPeter Wemm 	if (st == -1)
3031198f21cSPeter Wemm 		err(1, "could not sysctl(KERN_PROC)");
3041198f21cSPeter Wemm 	if (size % sizeof(struct kinfo_proc) != 0) {
305ee6b783cSTim J. Robbins 		fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
3061198f21cSPeter Wemm 			size, sizeof(struct kinfo_proc));
3071198f21cSPeter Wemm 		fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n");
3081198f21cSPeter Wemm 		exit(1);
3091198f21cSPeter Wemm 	}
3101198f21cSPeter Wemm 	nprocs = size / sizeof(struct kinfo_proc);
3111198f21cSPeter Wemm 	if (dflag)
3121198f21cSPeter Wemm 		printf("nprocs %d\n", nprocs);
313b4e148e9SColin Percival 	mypid = getpid();
3141198f21cSPeter Wemm 
3151198f21cSPeter Wemm 	for (i = 0; i < nprocs; i++) {
316a2fefc8dSDag-Erling Smørgrav 		if ((procs[i].ki_stat & SZOMB) == SZOMB && !zflag)
317a2fefc8dSDag-Erling Smørgrav 			continue;
31893480bceSJohn Baldwin 		thispid = procs[i].ki_pid;
3195c13cfe0SXin LI 		strlcpy(thiscmd, procs[i].ki_comm, sizeof(thiscmd));
32093480bceSJohn Baldwin 		thistdev = procs[i].ki_tdev;
3215aa41214SDiomidis Spinellis 		if (eflag)
3225aa41214SDiomidis Spinellis 			thisuid = procs[i].ki_uid;	/* effective uid */
3235aa41214SDiomidis Spinellis 		else
32493480bceSJohn Baldwin 			thisuid = procs[i].ki_ruid;	/* real uid */
3251198f21cSPeter Wemm 
326b4e148e9SColin Percival 		if (thispid == mypid)
327b4e148e9SColin Percival 			continue;
3281198f21cSPeter Wemm 		matched = 1;
3291198f21cSPeter Wemm 		if (user) {
3301198f21cSPeter Wemm 			if (thisuid != uid)
3311198f21cSPeter Wemm 				matched = 0;
3321198f21cSPeter Wemm 		}
3331198f21cSPeter Wemm 		if (tty) {
3341198f21cSPeter Wemm 			if (thistdev != tdev)
3351198f21cSPeter Wemm 				matched = 0;
3361198f21cSPeter Wemm 		}
3371198f21cSPeter Wemm 		if (cmd) {
3381198f21cSPeter Wemm 			if (mflag) {
3391198f21cSPeter Wemm 				if (regcomp(&rgx, cmd,
3401198f21cSPeter Wemm 				    REG_EXTENDED|REG_NOSUB) != 0) {
3411198f21cSPeter Wemm 					mflag = 0;
3421198f21cSPeter Wemm 					warnx("%s: illegal regexp", cmd);
3431198f21cSPeter Wemm 				}
3441198f21cSPeter Wemm 			}
3451198f21cSPeter Wemm 			if (mflag) {
3461198f21cSPeter Wemm 				pmatch.rm_so = 0;
3471198f21cSPeter Wemm 				pmatch.rm_eo = strlen(thiscmd);
3481198f21cSPeter Wemm 				if (regexec(&rgx, thiscmd, 0, &pmatch,
3491198f21cSPeter Wemm 				    REG_STARTEND) != 0)
3501198f21cSPeter Wemm 					matched = 0;
3511198f21cSPeter Wemm 				regfree(&rgx);
3521198f21cSPeter Wemm 			} else {
35369179c14SPeter Wemm 				if (strncmp(thiscmd, cmd, MAXCOMLEN) != 0)
3541198f21cSPeter Wemm 					matched = 0;
3551198f21cSPeter Wemm 			}
3561198f21cSPeter Wemm 		}
357ebf5d9bcSMike Barcroft 		if (jflag && thispid == getpid())
358ebf5d9bcSMike Barcroft 			matched = 0;
3591198f21cSPeter Wemm 		if (matched == 0)
3601198f21cSPeter Wemm 			continue;
3616e1dea06SAndrey A. Chernov 		if (ac > 0)
3621198f21cSPeter Wemm 			matched = 0;
3631198f21cSPeter Wemm 		for (j = 0; j < ac; j++) {
3641198f21cSPeter Wemm 			if (mflag) {
3651198f21cSPeter Wemm 				if (regcomp(&rgx, av[j],
3661198f21cSPeter Wemm 				    REG_EXTENDED|REG_NOSUB) != 0) {
3671198f21cSPeter Wemm 					mflag = 0;
3681198f21cSPeter Wemm 					warnx("%s: illegal regexp", av[j]);
3691198f21cSPeter Wemm 				}
3701198f21cSPeter Wemm 			}
3711198f21cSPeter Wemm 			if (mflag) {
3721198f21cSPeter Wemm 				pmatch.rm_so = 0;
3731198f21cSPeter Wemm 				pmatch.rm_eo = strlen(thiscmd);
3741198f21cSPeter Wemm 				if (regexec(&rgx, thiscmd, 0, &pmatch,
3751198f21cSPeter Wemm 				    REG_STARTEND) == 0)
3761198f21cSPeter Wemm 					matched = 1;
3771198f21cSPeter Wemm 				regfree(&rgx);
3781198f21cSPeter Wemm 			} else {
3791198f21cSPeter Wemm 				if (strcmp(thiscmd, av[j]) == 0)
3801198f21cSPeter Wemm 					matched = 1;
3811198f21cSPeter Wemm 			}
3821198f21cSPeter Wemm 			if (matched)
3831198f21cSPeter Wemm 				break;
3841198f21cSPeter Wemm 		}
3851198f21cSPeter Wemm 		if (matched == 0)
3861198f21cSPeter Wemm 			continue;
3871198f21cSPeter Wemm 		if (dflag)
3881198f21cSPeter Wemm 			printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
3891198f21cSPeter Wemm 			    thiscmd, thispid, thistdev, thisuid);
3901198f21cSPeter Wemm 
3911198f21cSPeter Wemm 		if (vflag || sflag)
392*de555084SJilles Tjoelker 			printf("kill -%s %d\n", sys_signame[sig], thispid);
3931198f21cSPeter Wemm 
3941198f21cSPeter Wemm 		killed++;
3951198f21cSPeter Wemm 		if (!dflag && !sflag) {
3961198f21cSPeter Wemm 			if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
397a79a73abSDima Dorfman 				warn("warning: kill -%s %d",
398*de555084SJilles Tjoelker 				    sys_signame[sig], thispid);
3991198f21cSPeter Wemm 				errors = 1;
4001198f21cSPeter Wemm 			}
4011198f21cSPeter Wemm 		}
4021198f21cSPeter Wemm 	}
4031198f21cSPeter Wemm 	if (killed == 0) {
4041198f21cSPeter Wemm 		fprintf(stderr, "No matching processes %swere found\n",
4051198f21cSPeter Wemm 		    getuid() != 0 ? "belonging to you " : "");
4061198f21cSPeter Wemm 		errors = 1;
4071198f21cSPeter Wemm 	}
4081198f21cSPeter Wemm 	exit(errors);
4091198f21cSPeter Wemm }
410