xref: /dragonfly/usr.bin/killall/killall.c (revision c89a6c1b)
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.9 2007/02/01 10:33:26 corecode 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] [-j jail] [-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((unsigned char)*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		jflag = 0, jailid = 0;
116 	int		dflag = 0;
117 	int		mflag = 0;
118 	uid_t		uid = 0;
119 	dev_t		tdev = 0;
120 	pid_t		mypid;
121 	char		thiscmd[MAXCOMLEN + 1];
122 	pid_t		thispid;
123 	uid_t		thisuid;
124 	dev_t		thistdev;
125 	int		sig = SIGTERM;
126 	const char *const *p;
127 	char		*ep;
128 	int		errors = 0;
129 	int		mib[4];
130 	size_t		miblen;
131 	int		st, nprocs;
132 	size_t		size;
133 	int		matched;
134 	int		killed = 0;
135 
136 	prog = av[0];
137 	av++;
138 	ac--;
139 
140 	while (ac > 0) {
141 		if (strcmp(*av, "-l") == 0) {
142 			printsig(stdout);
143 			exit(0);
144 		}
145 		if (strcmp(*av, "-help") == 0)
146 			usage();
147 		if (**av == '-') {
148 			++*av;
149 			switch (**av) {
150 			case 'u':
151 				++*av;
152 				if (**av == '\0')
153 					++av;
154 				--ac;
155 				user = *av;
156 				break;
157 			case 't':
158 				++*av;
159 				if (**av == '\0')
160 					++av;
161 				--ac;
162 				tty = *av;
163 				break;
164 			case 'c':
165 				++*av;
166 				if (**av == '\0')
167 					++av;
168 				--ac;
169 				cmd = *av;
170 				break;
171 			case 'j':
172 			{
173 				const char *errstr;
174 				++*av;
175 				if (**av == '\0')
176 					++av;
177 				--ac;
178 				jailid = strtonum(*av, 1, INT_MAX, &errstr);
179 
180 				if (errstr)
181 					errx(1, "jail id is %s: %s", errstr, *av);
182 				jflag++;
183 				break;
184 			}
185 			case 'q':
186 				qflag++;
187 				break;
188 			case 'v':
189 				vflag++;
190 				break;
191 			case 's':
192 				sflag++;
193 				break;
194 			case 'd':
195 				dflag++;
196 				break;
197 			case 'm':
198 				mflag++;
199 				break;
200 			default:
201 				if (isalpha((unsigned char)**av)) {
202 					if (strncasecmp(*av, "sig", 3) == 0)
203 						*av += 3;
204 					for (sig = NSIG, p = sys_signame + 1;
205 					     --sig; ++p)
206 						if (strcasecmp(*p, *av) == 0) {
207 							sig = p - sys_signame;
208 							break;
209 						}
210 					if (!sig)
211 						nosig(*av);
212 				} else if (isdigit((unsigned char)**av)) {
213 					sig = strtol(*av, &ep, 10);
214 					if (!*av || *ep)
215 						errx(1, "illegal signal number: %s", *av);
216 					if (sig < 0 || sig >= NSIG)
217 						nosig(*av);
218 				} else
219 					nosig(*av);
220 			}
221 			++av;
222 			--ac;
223 		} else {
224 			break;
225 		}
226 	}
227 
228 	if (user == NULL && tty == NULL && cmd == NULL && jflag == 0 && ac == 0)
229 		usage();
230 
231 	if (tty) {
232 		if (strncmp(tty, "/dev/", 5) == 0)
233 			snprintf(buf, sizeof(buf), "%s", tty);
234 		else if (strncmp(tty, "tty", 3) == 0)
235 			snprintf(buf, sizeof(buf), "/dev/%s", tty);
236 		else
237 			snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
238 		if (stat(buf, &sb) < 0)
239 			err(1, "stat(%s)", buf);
240 		if (!S_ISCHR(sb.st_mode))
241 			errx(1, "%s: not a character device", buf);
242 		tdev = sb.st_rdev;
243 		if (dflag)
244 			printf("ttydev:0x%x\n", tdev);
245 	}
246 	if (user) {
247 		pw = getpwnam(user);
248 		if (pw == NULL)
249 			errx(1, "user %s does not exist", user);
250 		uid = pw->pw_uid;
251 		if (dflag)
252 			printf("uid:%d\n", uid);
253 	} else {
254 		uid = getuid();
255 		if (uid != 0) {
256 			pw = getpwuid(uid);
257 			if (pw)
258 				user = pw->pw_name;
259 			if (dflag)
260 				printf("uid:%d\n", uid);
261 		}
262 	}
263 	size = 0;
264 	mib[0] = CTL_KERN;
265 	mib[1] = KERN_PROC;
266 	mib[2] = KERN_PROC_ALL;
267 	mib[3] = 0;
268 	miblen = 3;
269 
270 	if (user && mib[2] == KERN_PROC_ALL) {
271 		mib[2] = KERN_PROC_RUID;
272 		mib[3] = uid;
273 		miblen = 4;
274 	}
275 	if (tty && mib[2] == KERN_PROC_ALL) {
276 		mib[2] = KERN_PROC_TTY;
277 		mib[3] = tdev;
278 		miblen = 4;
279 	}
280 
281 	st = sysctl(mib, miblen, NULL, &size, NULL, 0);
282 	do {
283 		size += size / 10;
284 		newprocs = realloc(procs, size);
285 		if (newprocs == 0) {
286 			if (procs)
287 				free(procs);
288 			errx(1, "could not reallocate memory");
289 		}
290 		procs = newprocs;
291 		st = sysctl(mib, miblen, procs, &size, NULL, 0);
292 	} while (st == -1 && errno == ENOMEM);
293 	if (st == -1)
294 		err(1, "could not sysctl(KERN_PROC)");
295 	if (size % sizeof(struct kinfo_proc) != 0) {
296 		fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
297 			size, sizeof(struct kinfo_proc));
298 		fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n");
299 		exit(1);
300 	}
301 	nprocs = size / sizeof(struct kinfo_proc);
302 	if (dflag)
303 		printf("nprocs %d\n", nprocs);
304 	mypid = getpid();
305 
306 	for (i = 0; i < nprocs; i++) {
307 		thispid = procs[i].kp_pid;
308 		strncpy(thiscmd, procs[i].kp_comm, MAXCOMLEN);
309 		thiscmd[MAXCOMLEN] = '\0';
310 		thistdev = procs[i].kp_tdev;
311 		thisuid = procs[i].kp_ruid;	/* real uid */
312 
313 		if (thispid == mypid)
314 			continue;
315 		matched = 1;
316 		if ((int)procs[i].kp_pid < 0)
317 			matched = 0;
318 		if (user) {
319 			if (thisuid != uid)
320 				matched = 0;
321 		}
322 		if (tty) {
323 			if (thistdev != tdev)
324 				matched = 0;
325 		}
326 		if (jflag) {
327 			if (procs[i].kp_jailid != jailid)
328 				matched = 0;
329 		}
330 		if (cmd) {
331 			if (mflag) {
332 				if (regcomp(&rgx, cmd,
333 				    REG_EXTENDED|REG_NOSUB) != 0) {
334 					mflag = 0;
335 					warnx("%s: illegal regexp", cmd);
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 = 0;
344 				regfree(&rgx);
345 			} else {
346 				if (strcmp(thiscmd, cmd) != 0)
347 					matched = 0;
348 			}
349 		}
350 		if (matched == 0)
351 			continue;
352 		if (ac > 0) {
353 			matched = 0;
354 			for (j = 0; j < ac; j++) {
355 				if (mflag) {
356 					if (regcomp(&rgx, av[j],
357 					    REG_EXTENDED|REG_NOSUB) != 0) {
358 						mflag = 0;
359 						warnx("%s: illegal regexp", av[j]);
360 					}
361 				}
362 				if (mflag) {
363 					pmatch.rm_so = 0;
364 					pmatch.rm_eo = strlen(thiscmd);
365 					if (regexec(&rgx, thiscmd, 0, &pmatch,
366 					    REG_STARTEND) == 0)
367 						matched = 1;
368 					regfree(&rgx);
369 				} else {
370 					if (strcmp(thiscmd, av[j]) == 0)
371 						matched = 1;
372 				}
373 				if (matched)
374 					break;
375 			}
376 			if (matched == 0)
377 				continue;
378 		}
379 		if (dflag)
380 			printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
381 			    thiscmd, thispid, thistdev, thisuid);
382 
383 		if (vflag || sflag)
384 			printf("kill -%s %d\n", upper(sys_signame[sig]),
385 			    thispid);
386 
387 		killed++;
388 		if (!dflag && !sflag) {
389 			if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
390 				warn("kill -%s %d", upper(sys_signame[sig]),
391 				    thispid);
392 				errors = 1;
393 			}
394 		}
395 	}
396 	if (!qflag && killed == 0) {
397 		fprintf(stderr, "No matching processes %swere found\n",
398 		    getuid() != 0 ? "belonging to you " : "");
399 		errors = 1;
400 	}
401 	exit(errors);
402 }
403