xref: /freebsd/usr.bin/killall/killall.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
11198f21cSPeter Wemm /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
41198f21cSPeter Wemm  * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org>
51198f21cSPeter Wemm  * Copyright (c) 2000 Paul Saab <ps@FreeBSD.org>
61198f21cSPeter Wemm  * All rights reserved.
71198f21cSPeter Wemm  *
81198f21cSPeter Wemm  * Redistribution and use in source and binary forms, with or without
91198f21cSPeter Wemm  * modification, are permitted provided that the following conditions
101198f21cSPeter Wemm  * are met:
111198f21cSPeter Wemm  * 1. Redistributions of source code must retain the above copyright
121198f21cSPeter Wemm  *    notice, this list of conditions and the following disclaimer.
131198f21cSPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
141198f21cSPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
151198f21cSPeter Wemm  *    documentation and/or other materials provided with the distribution.
161198f21cSPeter Wemm  *
171198f21cSPeter Wemm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
181198f21cSPeter Wemm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191198f21cSPeter Wemm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201198f21cSPeter Wemm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
211198f21cSPeter Wemm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221198f21cSPeter Wemm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231198f21cSPeter Wemm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241198f21cSPeter Wemm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251198f21cSPeter Wemm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261198f21cSPeter Wemm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271198f21cSPeter Wemm  * SUCH DAMAGE.
281198f21cSPeter Wemm  */
291198f21cSPeter Wemm 
301198f21cSPeter Wemm #include <sys/param.h>
31ebf5d9bcSMike Barcroft #include <sys/jail.h>
321198f21cSPeter Wemm #include <sys/stat.h>
3373d0971bSJamie Gritton #include <sys/uio.h>
341198f21cSPeter Wemm #include <sys/user.h>
351198f21cSPeter Wemm #include <sys/sysctl.h>
36821df508SXin LI #include <fcntl.h>
37821df508SXin LI #include <dirent.h>
38de6f3704SJamie Gritton #include <jail.h>
399b337054SGleb Kurtsou #include <stdint.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
usage(void)531198f21cSPeter Wemm usage(void)
541198f21cSPeter Wemm {
551198f21cSPeter Wemm 
56214e0878SMateusz Guzik 	fprintf(stderr, "usage: killall [-delmsqvz] [-help] [-I] [-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
printsig(FILE * fp)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) {
72de555084SJilles 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
nosig(char * name)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
main(int ac,char ** av)911198f21cSPeter Wemm main(int ac, char **av)
921198f21cSPeter Wemm {
936f17dec9SChristian S.J. Peron 	char		**saved_av;
9440bcb503SKonstantin Belousov 	struct kinfo_proc *procs, *newprocs;
951198f21cSPeter Wemm 	struct stat	sb;
961198f21cSPeter Wemm 	struct passwd	*pw;
971198f21cSPeter Wemm 	regex_t		rgx;
98bcd34af4SAlexander Kabaev 	regmatch_t	pmatch;
998d5ec393SXin LI 	int		i, j, ch;
1001198f21cSPeter Wemm 	char		buf[256];
1018d5ec393SXin LI 	char		first;
1021198f21cSPeter Wemm 	char		*user = NULL;
1031198f21cSPeter Wemm 	char		*tty = NULL;
1041198f21cSPeter Wemm 	char		*cmd = NULL;
105214e0878SMateusz Guzik 	int		qflag = 0;
1061198f21cSPeter Wemm 	int		vflag = 0;
1071198f21cSPeter Wemm 	int		sflag = 0;
1081198f21cSPeter Wemm 	int		dflag = 0;
1095aa41214SDiomidis Spinellis 	int		eflag = 0;
1108d5ec393SXin LI 	int		Iflag = 0;
111ebf5d9bcSMike Barcroft 	int		jflag = 0;
1121198f21cSPeter Wemm 	int		mflag = 0;
113a2fefc8dSDag-Erling Smørgrav 	int		zflag = 0;
1141198f21cSPeter Wemm 	uid_t		uid = 0;
1151198f21cSPeter Wemm 	dev_t		tdev = 0;
116b4e148e9SColin Percival 	pid_t		mypid;
1171198f21cSPeter Wemm 	char		thiscmd[MAXCOMLEN + 1];
1181198f21cSPeter Wemm 	pid_t		thispid;
1191198f21cSPeter Wemm 	uid_t		thisuid;
1201198f21cSPeter Wemm 	dev_t		thistdev;
1211198f21cSPeter Wemm 	int		sig = SIGTERM;
1221198f21cSPeter Wemm 	const char *const *p;
1231198f21cSPeter Wemm 	char		*ep;
1241198f21cSPeter Wemm 	int		errors = 0;
125ebf5d9bcSMike Barcroft 	int		jid;
12649a4598bSPoul-Henning Kamp 	int		mib[4];
1271198f21cSPeter Wemm 	size_t		miblen;
1281198f21cSPeter Wemm 	int		st, nprocs;
1291198f21cSPeter Wemm 	size_t		size;
1301198f21cSPeter Wemm 	int		matched;
1311198f21cSPeter Wemm 	int		killed = 0;
1321198f21cSPeter Wemm 
133c5453cb4STim J. Robbins 	setlocale(LC_ALL, "");
134c5453cb4STim J. Robbins 
1351198f21cSPeter Wemm 	av++;
1361198f21cSPeter Wemm 	ac--;
1371198f21cSPeter Wemm 
1381198f21cSPeter Wemm 	while (ac > 0) {
1391198f21cSPeter Wemm 		if (strcmp(*av, "-l") == 0) {
1401198f21cSPeter Wemm 			printsig(stdout);
1411198f21cSPeter Wemm 			exit(0);
1421198f21cSPeter Wemm 		}
1438750ed5fSPeter Wemm 		if (strcmp(*av, "-help") == 0)
1448750ed5fSPeter Wemm 			usage();
1451198f21cSPeter Wemm 		if (**av == '-') {
1461198f21cSPeter Wemm 			++*av;
1471198f21cSPeter Wemm 			switch (**av) {
148ebf5d9bcSMike Barcroft 			case 'j':
149ebf5d9bcSMike Barcroft 				++*av;
150b8272c91SRuslan Ermilov 				if (**av == '\0') {
151ebf5d9bcSMike Barcroft 					++av;
152ebf5d9bcSMike Barcroft 					--ac;
153b8272c91SRuslan Ermilov 				}
154ebf5d9bcSMike Barcroft 				jflag++;
155b8272c91SRuslan Ermilov 				if (*av == NULL)
15673d0971bSJamie Gritton 				    	errx(1, "must specify jail");
157de6f3704SJamie Gritton 				jid = jail_getid(*av);
15873d0971bSJamie Gritton 				if (jid < 0)
159de6f3704SJamie Gritton 					errx(1, "%s", jail_errmsg);
160ebf5d9bcSMike Barcroft 				if (jail_attach(jid) == -1)
16173d0971bSJamie Gritton 					err(1, "jail_attach(%d)", jid);
162ebf5d9bcSMike Barcroft 				break;
1631198f21cSPeter Wemm 			case 'u':
1641198f21cSPeter Wemm 				++*av;
165b8272c91SRuslan Ermilov 				if (**av == '\0') {
1661198f21cSPeter Wemm 					++av;
1671198f21cSPeter Wemm 					--ac;
168b8272c91SRuslan Ermilov 				}
169b8272c91SRuslan Ermilov 				if (*av == NULL)
170b8272c91SRuslan Ermilov 				    	errx(1, "must specify user");
1711198f21cSPeter Wemm 				user = *av;
1721198f21cSPeter Wemm 				break;
1731198f21cSPeter Wemm 			case 't':
1741198f21cSPeter Wemm 				++*av;
175b8272c91SRuslan Ermilov 				if (**av == '\0') {
1761198f21cSPeter Wemm 					++av;
1771198f21cSPeter Wemm 					--ac;
178b8272c91SRuslan Ermilov 				}
179b8272c91SRuslan Ermilov 				if (*av == NULL)
180b8272c91SRuslan Ermilov 				    	errx(1, "must specify tty");
1811198f21cSPeter Wemm 				tty = *av;
1821198f21cSPeter Wemm 				break;
1831198f21cSPeter Wemm 			case 'c':
1841198f21cSPeter Wemm 				++*av;
185b8272c91SRuslan Ermilov 				if (**av == '\0') {
1861198f21cSPeter Wemm 					++av;
1871198f21cSPeter Wemm 					--ac;
188b8272c91SRuslan Ermilov 				}
189b8272c91SRuslan Ermilov 				if (*av == NULL)
190b8272c91SRuslan Ermilov 				    	errx(1, "must specify procname");
1911198f21cSPeter Wemm 				cmd = *av;
1921198f21cSPeter Wemm 				break;
193214e0878SMateusz Guzik 			case 'q':
194214e0878SMateusz Guzik 				qflag++;
195214e0878SMateusz Guzik 				break;
1961198f21cSPeter Wemm 			case 'v':
1971198f21cSPeter Wemm 				vflag++;
1981198f21cSPeter Wemm 				break;
1991198f21cSPeter Wemm 			case 's':
2001198f21cSPeter Wemm 				sflag++;
2011198f21cSPeter Wemm 				break;
2021198f21cSPeter Wemm 			case 'd':
2031198f21cSPeter Wemm 				dflag++;
2041198f21cSPeter Wemm 				break;
2055aa41214SDiomidis Spinellis 			case 'e':
2065aa41214SDiomidis Spinellis 				eflag++;
2075aa41214SDiomidis Spinellis 				break;
2081198f21cSPeter Wemm 			case 'm':
2091198f21cSPeter Wemm 				mflag++;
2101198f21cSPeter Wemm 				break;
211a2fefc8dSDag-Erling Smørgrav 			case 'z':
212a2fefc8dSDag-Erling Smørgrav 				zflag++;
213a2fefc8dSDag-Erling Smørgrav 				break;
2141198f21cSPeter Wemm 			default:
2156f17dec9SChristian S.J. Peron 				saved_av = av;
216fe1ba53cSTim J. Robbins 				if (isalpha((unsigned char)**av)) {
21712dacf62SJilles Tjoelker 					if (strncasecmp(*av, "SIG", 3) == 0)
2181198f21cSPeter Wemm 						*av += 3;
2191198f21cSPeter Wemm 					for (sig = NSIG, p = sys_signame + 1;
2201198f21cSPeter Wemm 					     --sig; ++p)
2211198f21cSPeter Wemm 						if (strcasecmp(*p, *av) == 0) {
2221198f21cSPeter Wemm 							sig = p - sys_signame;
2231198f21cSPeter Wemm 							break;
2241198f21cSPeter Wemm 						}
2256f17dec9SChristian S.J. Peron 					if (!sig) {
2266f17dec9SChristian S.J. Peron 						if (**saved_av == 'I') {
2276f17dec9SChristian S.J. Peron 							av = saved_av;
2286f17dec9SChristian S.J. Peron 							Iflag = 1;
2296f17dec9SChristian S.J. Peron 							break;
2306f17dec9SChristian S.J. Peron 						} else
2311198f21cSPeter Wemm 							nosig(*av);
2326f17dec9SChristian S.J. Peron 					}
233fe1ba53cSTim J. Robbins 				} else if (isdigit((unsigned char)**av)) {
2341198f21cSPeter Wemm 					sig = strtol(*av, &ep, 10);
2351198f21cSPeter Wemm 					if (!*av || *ep)
2361198f21cSPeter Wemm 						errx(1, "illegal signal number: %s", *av);
23779d0f70fSMaxim Konovalov 					if (sig < 0 || sig >= NSIG)
2381198f21cSPeter Wemm 						nosig(*av);
2391198f21cSPeter Wemm 				} else
2401198f21cSPeter Wemm 					nosig(*av);
2411198f21cSPeter Wemm 			}
2421198f21cSPeter Wemm 			++av;
2431198f21cSPeter Wemm 			--ac;
2441198f21cSPeter Wemm 		} else {
2451198f21cSPeter Wemm 			break;
2461198f21cSPeter Wemm 		}
2471198f21cSPeter Wemm 	}
2481198f21cSPeter Wemm 
249ebf5d9bcSMike Barcroft 	if (user == NULL && tty == NULL && cmd == NULL && !jflag && ac == 0)
2501198f21cSPeter Wemm 		usage();
2511198f21cSPeter Wemm 
2521198f21cSPeter Wemm 	if (tty) {
2531198f21cSPeter Wemm 		if (strncmp(tty, "/dev/", 5) == 0)
2541198f21cSPeter Wemm 			snprintf(buf, sizeof(buf), "%s", tty);
255e442917eSDaniel Dowse 		else if (strncmp(tty, "tty", 3) == 0 ||
256e442917eSDaniel Dowse 		    strncmp(tty, "pts/", 4) == 0)
2571198f21cSPeter Wemm 			snprintf(buf, sizeof(buf), "/dev/%s", tty);
2581198f21cSPeter Wemm 		else
2591198f21cSPeter Wemm 			snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
2601198f21cSPeter Wemm 		if (stat(buf, &sb) < 0)
2611198f21cSPeter Wemm 			err(1, "stat(%s)", buf);
2621198f21cSPeter Wemm 		if (!S_ISCHR(sb.st_mode))
2631198f21cSPeter Wemm 			errx(1, "%s: not a character device", buf);
2641198f21cSPeter Wemm 		tdev = sb.st_rdev;
2651198f21cSPeter Wemm 		if (dflag)
2669b337054SGleb Kurtsou 			printf("ttydev:0x%jx\n", (uintmax_t)tdev);
2671198f21cSPeter Wemm 	}
2681198f21cSPeter Wemm 	if (user) {
2690d1b691eSDavid E. O'Brien 		uid = strtol(user, &ep, 10);
270fb757ee2SDavid Malone 		if (*user == '\0' || *ep != '\0') { /* was it a number? */
2711198f21cSPeter Wemm 			pw = getpwnam(user);
2721198f21cSPeter Wemm 			if (pw == NULL)
2731198f21cSPeter Wemm 				errx(1, "user %s does not exist", user);
2741198f21cSPeter Wemm 			uid = pw->pw_uid;
2751198f21cSPeter Wemm 			if (dflag)
2761198f21cSPeter Wemm 				printf("uid:%d\n", uid);
2770d1b691eSDavid E. O'Brien 		}
2781198f21cSPeter Wemm 	} else {
2791198f21cSPeter Wemm 		uid = getuid();
2801198f21cSPeter Wemm 		if (uid != 0) {
2811198f21cSPeter Wemm 			pw = getpwuid(uid);
2821198f21cSPeter Wemm 			if (pw)
2831198f21cSPeter Wemm 				user = pw->pw_name;
2841198f21cSPeter Wemm 			if (dflag)
2851198f21cSPeter Wemm 				printf("uid:%d\n", uid);
2861198f21cSPeter Wemm 		}
2871198f21cSPeter Wemm 	}
2881198f21cSPeter Wemm 	size = 0;
2891198f21cSPeter Wemm 	mib[0] = CTL_KERN;
2901198f21cSPeter Wemm 	mib[1] = KERN_PROC;
2911198f21cSPeter Wemm 
2922c801d3dSTim J. Robbins 	if (user) {
2935aa41214SDiomidis Spinellis 		mib[2] = eflag ? KERN_PROC_UID : KERN_PROC_RUID;
2941198f21cSPeter Wemm 		mib[3] = uid;
295f2dd06abSTim J. Robbins 		miblen = 4;
2962c801d3dSTim J. Robbins 	} else if (tty) {
2971198f21cSPeter Wemm 		mib[2] = KERN_PROC_TTY;
2981198f21cSPeter Wemm 		mib[3] = tdev;
299f2dd06abSTim J. Robbins 		miblen = 4;
300a488a0f1SKonstantin Belousov 	} else {
301a488a0f1SKonstantin Belousov 		mib[2] = KERN_PROC_PROC;
302a488a0f1SKonstantin Belousov 		mib[3] = 0;
303a488a0f1SKonstantin Belousov 		miblen = 3;
3041198f21cSPeter Wemm 	}
3051198f21cSPeter Wemm 
30640bcb503SKonstantin Belousov 	procs = NULL;
3071198f21cSPeter Wemm 	st = sysctl(mib, miblen, NULL, &size, NULL, 0);
3081198f21cSPeter Wemm 	do {
3091198f21cSPeter Wemm 		size += size / 10;
3101198f21cSPeter Wemm 		newprocs = realloc(procs, size);
31140bcb503SKonstantin Belousov 		if (newprocs == NULL) {
3121198f21cSPeter Wemm 			free(procs);
31340bcb503SKonstantin Belousov 			err(1, "could not reallocate memory");
3141198f21cSPeter Wemm 		}
3151198f21cSPeter Wemm 		procs = newprocs;
3161198f21cSPeter Wemm 		st = sysctl(mib, miblen, procs, &size, NULL, 0);
3171198f21cSPeter Wemm 	} while (st == -1 && errno == ENOMEM);
3181198f21cSPeter Wemm 	if (st == -1)
3191198f21cSPeter Wemm 		err(1, "could not sysctl(KERN_PROC)");
3201198f21cSPeter Wemm 	if (size % sizeof(struct kinfo_proc) != 0) {
321ee6b783cSTim J. Robbins 		fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
3221198f21cSPeter Wemm 			size, sizeof(struct kinfo_proc));
323407e615cSKonstantin Belousov 		fprintf(stderr, "userland out of sync with kernel\n");
3241198f21cSPeter Wemm 		exit(1);
3251198f21cSPeter Wemm 	}
3261198f21cSPeter Wemm 	nprocs = size / sizeof(struct kinfo_proc);
3271198f21cSPeter Wemm 	if (dflag)
3281198f21cSPeter Wemm 		printf("nprocs %d\n", nprocs);
329b4e148e9SColin Percival 	mypid = getpid();
3301198f21cSPeter Wemm 
3311198f21cSPeter Wemm 	for (i = 0; i < nprocs; i++) {
332c517bc15SKonstantin Belousov 		if (procs[i].ki_stat == SZOMB && !zflag)
333a2fefc8dSDag-Erling Smørgrav 			continue;
33493480bceSJohn Baldwin 		thispid = procs[i].ki_pid;
3355c13cfe0SXin LI 		strlcpy(thiscmd, procs[i].ki_comm, sizeof(thiscmd));
33693480bceSJohn Baldwin 		thistdev = procs[i].ki_tdev;
3375aa41214SDiomidis Spinellis 		if (eflag)
3385aa41214SDiomidis Spinellis 			thisuid = procs[i].ki_uid;	/* effective uid */
3395aa41214SDiomidis Spinellis 		else
34093480bceSJohn Baldwin 			thisuid = procs[i].ki_ruid;	/* real uid */
3411198f21cSPeter Wemm 
342b4e148e9SColin Percival 		if (thispid == mypid)
343b4e148e9SColin Percival 			continue;
3441198f21cSPeter Wemm 		matched = 1;
3451198f21cSPeter Wemm 		if (user) {
3461198f21cSPeter Wemm 			if (thisuid != uid)
3471198f21cSPeter Wemm 				matched = 0;
3481198f21cSPeter Wemm 		}
3491198f21cSPeter Wemm 		if (tty) {
3501198f21cSPeter Wemm 			if (thistdev != tdev)
3511198f21cSPeter Wemm 				matched = 0;
3521198f21cSPeter Wemm 		}
3531198f21cSPeter Wemm 		if (cmd) {
3541198f21cSPeter Wemm 			if (mflag) {
3551198f21cSPeter Wemm 				if (regcomp(&rgx, cmd,
3561198f21cSPeter Wemm 				    REG_EXTENDED|REG_NOSUB) != 0) {
3571198f21cSPeter Wemm 					mflag = 0;
3581198f21cSPeter Wemm 					warnx("%s: illegal regexp", cmd);
3591198f21cSPeter Wemm 				}
3601198f21cSPeter Wemm 			}
3611198f21cSPeter Wemm 			if (mflag) {
362bcd34af4SAlexander Kabaev 				pmatch.rm_so = 0;
363bcd34af4SAlexander Kabaev 				pmatch.rm_eo = strlen(thiscmd);
364bcd34af4SAlexander Kabaev 				if (regexec(&rgx, thiscmd, 0, &pmatch,
3651198f21cSPeter Wemm 				    REG_STARTEND) != 0)
3661198f21cSPeter Wemm 					matched = 0;
3671198f21cSPeter Wemm 				regfree(&rgx);
3681198f21cSPeter Wemm 			} else {
36969179c14SPeter Wemm 				if (strncmp(thiscmd, cmd, MAXCOMLEN) != 0)
3701198f21cSPeter Wemm 					matched = 0;
3711198f21cSPeter Wemm 			}
3721198f21cSPeter Wemm 		}
373ebf5d9bcSMike Barcroft 		if (jflag && thispid == getpid())
374ebf5d9bcSMike Barcroft 			matched = 0;
3751198f21cSPeter Wemm 		if (matched == 0)
3761198f21cSPeter Wemm 			continue;
3776e1dea06SAndrey A. Chernov 		if (ac > 0)
3781198f21cSPeter Wemm 			matched = 0;
3791198f21cSPeter Wemm 		for (j = 0; j < ac; j++) {
3801198f21cSPeter Wemm 			if (mflag) {
3811198f21cSPeter Wemm 				if (regcomp(&rgx, av[j],
3821198f21cSPeter Wemm 				    REG_EXTENDED|REG_NOSUB) != 0) {
3831198f21cSPeter Wemm 					mflag = 0;
3841198f21cSPeter Wemm 					warnx("%s: illegal regexp", av[j]);
3851198f21cSPeter Wemm 				}
3861198f21cSPeter Wemm 			}
3871198f21cSPeter Wemm 			if (mflag) {
388bcd34af4SAlexander Kabaev 				pmatch.rm_so = 0;
389bcd34af4SAlexander Kabaev 				pmatch.rm_eo = strlen(thiscmd);
390bcd34af4SAlexander Kabaev 				if (regexec(&rgx, thiscmd, 0, &pmatch,
3911198f21cSPeter Wemm 				    REG_STARTEND) == 0)
3921198f21cSPeter Wemm 					matched = 1;
3931198f21cSPeter Wemm 				regfree(&rgx);
3941198f21cSPeter Wemm 			} else {
3951198f21cSPeter Wemm 				if (strcmp(thiscmd, av[j]) == 0)
3961198f21cSPeter Wemm 					matched = 1;
3971198f21cSPeter Wemm 			}
3981198f21cSPeter Wemm 			if (matched)
3991198f21cSPeter Wemm 				break;
4001198f21cSPeter Wemm 		}
4018d5ec393SXin LI 		if (matched != 0 && Iflag) {
4028d5ec393SXin LI 			printf("Send signal %d to %s (pid %d uid %d)? ",
4038d5ec393SXin LI 				sig, thiscmd, thispid, thisuid);
4048d5ec393SXin LI 			fflush(stdout);
4058d5ec393SXin LI 			first = ch = getchar();
4068d5ec393SXin LI 			while (ch != '\n' && ch != EOF)
4078d5ec393SXin LI 				ch = getchar();
4088d5ec393SXin LI 			if (first != 'y' && first != 'Y')
4098d5ec393SXin LI 				matched = 0;
4108d5ec393SXin LI 		}
4111198f21cSPeter Wemm 		if (matched == 0)
4121198f21cSPeter Wemm 			continue;
4131198f21cSPeter Wemm 		if (dflag)
4149b337054SGleb Kurtsou 			printf("sig:%d, cmd:%s, pid:%d, dev:0x%jx uid:%d\n",
4159b337054SGleb Kurtsou 			    sig, thiscmd, thispid, (uintmax_t)thistdev,
4169b337054SGleb Kurtsou 			    thisuid);
4171198f21cSPeter Wemm 
4181198f21cSPeter Wemm 		if (vflag || sflag)
419de555084SJilles Tjoelker 			printf("kill -%s %d\n", sys_signame[sig], thispid);
4201198f21cSPeter Wemm 
4211198f21cSPeter Wemm 		killed++;
4221198f21cSPeter Wemm 		if (!dflag && !sflag) {
4231198f21cSPeter Wemm 			if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
424a79a73abSDima Dorfman 				warn("warning: kill -%s %d",
425de555084SJilles Tjoelker 				    sys_signame[sig], thispid);
4261198f21cSPeter Wemm 				errors = 1;
4271198f21cSPeter Wemm 			}
4281198f21cSPeter Wemm 		}
4291198f21cSPeter Wemm 	}
4301198f21cSPeter Wemm 	if (killed == 0) {
431214e0878SMateusz Guzik 		if (!qflag)
4321198f21cSPeter Wemm 			fprintf(stderr, "No matching processes %swere found\n",
4331198f21cSPeter Wemm 			    getuid() != 0 ? "belonging to you " : "");
4341198f21cSPeter Wemm 		errors = 1;
4351198f21cSPeter Wemm 	}
4361198f21cSPeter Wemm 	exit(errors);
4371198f21cSPeter Wemm }
438