xref: /dragonfly/usr.bin/killall/killall.c (revision 1d1731fa)
1 /*-
2  * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org>
3  * Copyright (c) 2000 Paul Saab <ps@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/usr.bin/killall/killall.c,v 1.5.2.4 2001/05/19 19:22:49 phk Exp $
28  * $DragonFly: src/usr.bin/killall/killall.c,v 1.4 2003/08/28 02:35:54 hmp Exp $
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <sys/user.h>
35 #include <sys/sysctl.h>
36 #include <fcntl.h>
37 #include <dirent.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <pwd.h>
42 #include <signal.h>
43 #include <regex.h>
44 #include <ctype.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <unistd.h>
48 
49 static char	*prog;
50 
51 static void __dead2
52 usage(void)
53 {
54 
55 	fprintf(stderr, "usage: %s [-l] [-v] [-m] [-sig] [-u user] [-t tty] [-c cmd] [cmd]...\n", prog);
56 	fprintf(stderr, "At least one option or argument to specify processes must be given.\n");
57 	exit(1);
58 }
59 
60 static char *
61 upper(const char *str)
62 {
63 	static char buf[80];
64 	char *s;
65 
66 	strlcpy(buf, str, sizeof(buf));
67 	for (s = buf; *s; s++)
68 		*s = toupper(*s);
69 	return buf;
70 }
71 
72 
73 static void
74 printsig(FILE *fp)
75 {
76 	const char	*const * p;
77 	int		cnt;
78 	int		offset = 0;
79 
80 	for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
81 		offset += fprintf(fp, "%s ", upper(*p));
82 		if (offset >= 75 && cnt > 1) {
83 			offset = 0;
84 			fprintf(fp, "\n");
85 		}
86 	}
87 	fprintf(fp, "\n");
88 }
89 
90 static void
91 nosig(char *name)
92 {
93 
94 	warnx("unknown signal %s; valid signals:", name);
95 	printsig(stderr);
96 	exit(1);
97 }
98 
99 int
100 main(int ac, char **av)
101 {
102 	struct kinfo_proc *procs = NULL, *newprocs;
103 	struct stat	sb;
104 	struct passwd	*pw;
105 	regex_t		rgx;
106 	regmatch_t	pmatch;
107 	int		i, j;
108 	char		buf[256];
109 	char		*user = NULL;
110 	char		*tty = NULL;
111 	char		*cmd = NULL;
112 	int		qflag = 0;
113 	int		vflag = 0;
114 	int		sflag = 0;
115 	int		dflag = 0;
116 	int		mflag = 0;
117 	uid_t		uid = 0;
118 	dev_t		tdev = 0;
119 	char		thiscmd[MAXCOMLEN + 1];
120 	pid_t		thispid;
121 	uid_t		thisuid;
122 	dev_t		thistdev;
123 	int		sig = SIGTERM;
124 	const char *const *p;
125 	char		*ep;
126 	int		errors = 0;
127 	int		mib[4];
128 	size_t		miblen;
129 	int		st, nprocs;
130 	size_t		size;
131 	int		matched;
132 	int		killed = 0;
133 
134 	prog = av[0];
135 	av++;
136 	ac--;
137 
138 	while (ac > 0) {
139 		if (strcmp(*av, "-l") == 0) {
140 			printsig(stdout);
141 			exit(0);
142 		}
143 		if (strcmp(*av, "-help") == 0)
144 			usage();
145 		if (**av == '-') {
146 			++*av;
147 			switch (**av) {
148 			case 'u':
149 				++*av;
150 				if (**av == '\0')
151 					++av;
152 				--ac;
153 				user = *av;
154 				break;
155 			case 't':
156 				++*av;
157 				if (**av == '\0')
158 					++av;
159 				--ac;
160 				tty = *av;
161 				break;
162 			case 'c':
163 				++*av;
164 				if (**av == '\0')
165 					++av;
166 				--ac;
167 				cmd = *av;
168 				break;
169 			case 'q':
170 				qflag++;
171 				break;
172 			case 'v':
173 				vflag++;
174 				break;
175 			case 's':
176 				sflag++;
177 				break;
178 			case 'd':
179 				dflag++;
180 				break;
181 			case 'm':
182 				mflag++;
183 				break;
184 			default:
185 				if (isalpha(**av)) {
186 					if (strncasecmp(*av, "sig", 3) == 0)
187 						*av += 3;
188 					for (sig = NSIG, p = sys_signame + 1;
189 					     --sig; ++p)
190 						if (strcasecmp(*p, *av) == 0) {
191 							sig = p - sys_signame;
192 							break;
193 						}
194 					if (!sig)
195 						nosig(*av);
196 				} else if (isdigit(**av)) {
197 					sig = strtol(*av, &ep, 10);
198 					if (!*av || *ep)
199 						errx(1, "illegal signal number: %s", *av);
200 					if (sig < 0 || sig > NSIG)
201 						nosig(*av);
202 				} else
203 					nosig(*av);
204 			}
205 			++av;
206 			--ac;
207 		} else {
208 			break;
209 		}
210 	}
211 
212 	if (user == NULL && tty == NULL && cmd == NULL && ac == 0)
213 		usage();
214 
215 	if (tty) {
216 		if (strncmp(tty, "/dev/", 5) == 0)
217 			snprintf(buf, sizeof(buf), "%s", tty);
218 		else if (strncmp(tty, "tty", 3) == 0)
219 			snprintf(buf, sizeof(buf), "/dev/%s", tty);
220 		else
221 			snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
222 		if (stat(buf, &sb) < 0)
223 			err(1, "stat(%s)", buf);
224 		if (!S_ISCHR(sb.st_mode))
225 			errx(1, "%s: not a character device", buf);
226 		tdev = sb.st_rdev;
227 		if (dflag)
228 			printf("ttydev:0x%x\n", tdev);
229 	}
230 	if (user) {
231 		pw = getpwnam(user);
232 		if (pw == NULL)
233 			errx(1, "user %s does not exist", user);
234 		uid = pw->pw_uid;
235 		if (dflag)
236 			printf("uid:%d\n", uid);
237 	} else {
238 		uid = getuid();
239 		if (uid != 0) {
240 			pw = getpwuid(uid);
241 			if (pw)
242 				user = pw->pw_name;
243 			if (dflag)
244 				printf("uid:%d\n", uid);
245 		}
246 	}
247 	size = 0;
248 	mib[0] = CTL_KERN;
249 	mib[1] = KERN_PROC;
250 	mib[2] = KERN_PROC_ALL;
251 	mib[3] = 0;
252 	miblen = 3;
253 
254 	if (user && mib[2] == KERN_PROC_ALL) {
255 		mib[2] = KERN_PROC_RUID;
256 		mib[3] = uid;
257 		miblen = 4;
258 	}
259 	if (tty && mib[2] == KERN_PROC_ALL) {
260 		mib[2] = KERN_PROC_TTY;
261 		mib[3] = tdev;
262 		miblen = 4;
263 	}
264 
265 	st = sysctl(mib, miblen, NULL, &size, NULL, 0);
266 	do {
267 		size += size / 10;
268 		newprocs = realloc(procs, size);
269 		if (newprocs == 0) {
270 			if (procs)
271 				free(procs);
272 			errx(1, "could not reallocate memory");
273 		}
274 		procs = newprocs;
275 		st = sysctl(mib, miblen, procs, &size, NULL, 0);
276 	} while (st == -1 && errno == ENOMEM);
277 	if (st == -1)
278 		err(1, "could not sysctl(KERN_PROC)");
279 	if (size % sizeof(struct kinfo_proc) != 0) {
280 		fprintf(stderr, "proc size mismatch (%d total, %d chunks)\n",
281 			size, sizeof(struct kinfo_proc));
282 		fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n");
283 		exit(1);
284 	}
285 	nprocs = size / sizeof(struct kinfo_proc);
286 	if (dflag)
287 		printf("nprocs %d\n", nprocs);
288 
289 	for (i = 0; i < nprocs; i++) {
290 		thispid = procs[i].kp_proc.p_pid;
291 		strncpy(thiscmd, procs[i].kp_thread.td_comm, MAXCOMLEN);
292 		thiscmd[MAXCOMLEN] = '\0';
293 		thistdev = procs[i].kp_eproc.e_tdev;
294 		thisuid = procs[i].kp_eproc.e_ucred.cr_ruid;	/* real uid */
295 
296 		matched = 1;
297 		if ((int)procs[i].kp_proc.p_pid < 0)
298 			matched = 0;
299 		if (user) {
300 			if (thisuid != uid)
301 				matched = 0;
302 		}
303 		if (tty) {
304 			if (thistdev != tdev)
305 				matched = 0;
306 		}
307 		if (cmd) {
308 			if (mflag) {
309 				if (regcomp(&rgx, cmd,
310 				    REG_EXTENDED|REG_NOSUB) != 0) {
311 					mflag = 0;
312 					warnx("%s: illegal regexp", cmd);
313 				}
314 			}
315 			if (mflag) {
316 				pmatch.rm_so = 0;
317 				pmatch.rm_eo = strlen(thiscmd);
318 				if (regexec(&rgx, thiscmd, 0, &pmatch,
319 				    REG_STARTEND) != 0)
320 					matched = 0;
321 				regfree(&rgx);
322 			} else {
323 				if (strcmp(thiscmd, cmd) != 0)
324 					matched = 0;
325 			}
326 		}
327 		if (matched == 0)
328 			continue;
329 		matched = 0;
330 		for (j = 0; j < ac; j++) {
331 			if (mflag) {
332 				if (regcomp(&rgx, av[j],
333 				    REG_EXTENDED|REG_NOSUB) != 0) {
334 					mflag = 0;
335 					warnx("%s: illegal regexp", av[j]);
336 				}
337 			}
338 			if (mflag) {
339 				pmatch.rm_so = 0;
340 				pmatch.rm_eo = strlen(thiscmd);
341 				if (regexec(&rgx, thiscmd, 0, &pmatch,
342 				    REG_STARTEND) == 0)
343 					matched = 1;
344 				regfree(&rgx);
345 			} else {
346 				if (strcmp(thiscmd, av[j]) == 0)
347 					matched = 1;
348 			}
349 			if (matched)
350 				break;
351 		}
352 		if (matched == 0)
353 			continue;
354 		if (dflag)
355 			printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
356 			    thiscmd, thispid, thistdev, thisuid);
357 
358 		if (vflag || sflag)
359 			printf("kill -%s %d\n", upper(sys_signame[sig]),
360 			    thispid);
361 
362 		killed++;
363 		if (!dflag && !sflag) {
364 			if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
365 				warn("kill -%s %d", upper(sys_signame[sig]),
366 				    thispid);
367 				errors = 1;
368 			}
369 		}
370 	}
371 	if (!qflag && killed == 0) {
372 		fprintf(stderr, "No matching processes %swere found\n",
373 		    getuid() != 0 ? "belonging to you " : "");
374 		errors = 1;
375 	}
376 	exit(errors);
377 }
378