d32efbee0280c224986a666f607d1d0f5147ab2b
[dragonfly.git] / usr.bin / killall / killall.c
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/user.h>
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <sys/sysctl.h>
35 #include <fcntl.h>
36 #include <dirent.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <pwd.h>
41 #include <signal.h>
42 #include <regex.h>
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <unistd.h>
47
48 static char     *prog;
49
50 static void __dead2
51 usage(void)
52 {
53
54         fprintf(stderr, "usage: %s [-l] [-v] [-m] [-sig] [-u user] [-j jail] [-t tty] [-c cmd] [cmd]...\n", prog);
55         fprintf(stderr, "At least one option or argument to specify processes must be given.\n");
56         exit(1);
57 }
58
59 static char *
60 upper(const char *str)
61 {
62         static char buf[80];
63         char *s;
64
65         strlcpy(buf, str, sizeof(buf));
66         for (s = buf; *s; s++)
67                 *s = toupper((unsigned char)*s);
68         return buf;
69 }
70
71
72 static void
73 printsig(FILE *fp)
74 {
75         const char      *const * p;
76         int             cnt;
77         int             offset = 0;
78
79         for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) {
80                 offset += fprintf(fp, "%s ", upper(*p));
81                 if (offset >= 75 && cnt > 1) {
82                         offset = 0;
83                         fprintf(fp, "\n");
84                 }
85         }
86         fprintf(fp, "\n");
87 }
88
89 static void
90 nosig(char *name)
91 {
92
93         warnx("unknown signal %s; valid signals:", name);
94         printsig(stderr);
95         exit(1);
96 }
97
98 int
99 main(int ac, char **av)
100 {
101         struct kinfo_proc *procs = NULL, *newprocs;
102         struct stat     sb;
103         struct passwd   *pw;
104         regex_t         rgx;
105         regmatch_t      pmatch;
106         int             i, j;
107         char            buf[256];
108         char            *user = NULL;
109         char            *tty = NULL;
110         char            *cmd = NULL;
111         int             qflag = 0;
112         int             vflag = 0;
113         int             sflag = 0;
114         int             jflag = 0, jailid = 0;
115         int             dflag = 0;
116         int             mflag = 0;
117         uid_t           uid = 0;
118         dev_t           tdev = 0;
119         pid_t           mypid;
120         char            thiscmd[MAXCOMLEN + 1];
121         pid_t           thispid;
122         uid_t           thisuid;
123         dev_t           thistdev;
124         int             sig = SIGTERM;
125         const char *const *p;
126         char            *ep;
127         int             errors = 0;
128         int             mib[4];
129         size_t          miblen;
130         int             st, nprocs;
131         size_t          size;
132         int             matched;
133         int             killed = 0;
134
135         prog = av[0];
136         av++;
137         ac--;
138
139         while (ac > 0) {
140                 if (strcmp(*av, "-l") == 0) {
141                         printsig(stdout);
142                         exit(0);
143                 }
144                 if (strcmp(*av, "-help") == 0)
145                         usage();
146                 if (**av == '-') {
147                         ++*av;
148                         switch (**av) {
149                         case 'u':
150                                 ++*av;
151                                 if (**av == '\0')
152                                         ++av;
153                                 --ac;
154                                 user = *av;
155                                 break;
156                         case 't':
157                                 ++*av;
158                                 if (**av == '\0')
159                                         ++av;
160                                 --ac;
161                                 tty = *av;
162                                 break;
163                         case 'c':
164                                 ++*av;
165                                 if (**av == '\0')
166                                         ++av;
167                                 --ac;
168                                 cmd = *av;
169                                 break;
170                         case 'j':
171                         {
172                                 const char *errstr;
173                                 ++*av;
174                                 if (**av == '\0')
175                                         ++av;
176                                 --ac;
177                                 jailid = strtonum(*av, 1, INT_MAX, &errstr);
178
179                                 if (errstr)
180                                         errx(1, "jail id is %s: %s", errstr, *av);
181                                 jflag++;
182                                 break;
183                         }
184                         case 'q':
185                                 qflag++;
186                                 break;
187                         case 'v':
188                                 vflag++;
189                                 break;
190                         case 's':
191                                 sflag++;
192                                 break;
193                         case 'd':
194                                 dflag++;
195                                 break;
196                         case 'm':
197                                 mflag++;
198                                 break;
199                         default:
200                                 if (isalpha((unsigned char)**av)) {
201                                         if (strncasecmp(*av, "sig", 3) == 0)
202                                                 *av += 3;
203                                         for (sig = NSIG, p = sys_signame + 1;
204                                              --sig; ++p)
205                                                 if (strcasecmp(*p, *av) == 0) {
206                                                         sig = p - sys_signame;
207                                                         break;
208                                                 }
209                                         if (!sig)
210                                                 nosig(*av);
211                                 } else if (isdigit((unsigned char)**av)) {
212                                         sig = strtol(*av, &ep, 10);
213                                         if (!*av || *ep)
214                                                 errx(1, "illegal signal number: %s", *av);
215                                         if (sig < 0 || sig >= NSIG)
216                                                 nosig(*av);
217                                 } else
218                                         nosig(*av);
219                         }
220                         ++av;
221                         --ac;
222                 } else {
223                         break;
224                 }
225         }
226
227         if (user == NULL && tty == NULL && cmd == NULL && jflag == 0 && ac == 0)
228                 usage();
229
230         if (tty) {
231                 if (strncmp(tty, "/dev/", 5) == 0)
232                         snprintf(buf, sizeof(buf), "%s", tty);
233                 else if (strncmp(tty, "tty", 3) == 0)
234                         snprintf(buf, sizeof(buf), "/dev/%s", tty);
235                 else
236                         snprintf(buf, sizeof(buf), "/dev/tty%s", tty);
237                 if (stat(buf, &sb) < 0)
238                         err(1, "stat(%s)", buf);
239                 if (!S_ISCHR(sb.st_mode))
240                         errx(1, "%s: not a character device", buf);
241                 tdev = sb.st_rdev;
242                 if (dflag)
243                         printf("ttydev:0x%x\n", tdev);
244         }
245         if (user) {
246                 pw = getpwnam(user);
247                 if (pw == NULL)
248                         errx(1, "user %s does not exist", user);
249                 uid = pw->pw_uid;
250                 if (dflag)
251                         printf("uid:%d\n", uid);
252         } else {
253                 uid = getuid();
254                 if (uid != 0) {
255                         pw = getpwuid(uid);
256                         if (pw)
257                                 user = pw->pw_name;
258                         if (dflag)
259                                 printf("uid:%d\n", uid);
260                 }
261         }
262         size = 0;
263         mib[0] = CTL_KERN;
264         mib[1] = KERN_PROC;
265         mib[2] = KERN_PROC_ALL;
266         mib[3] = 0;
267         miblen = 3;
268
269         if (user && mib[2] == KERN_PROC_ALL) {
270                 mib[2] = KERN_PROC_RUID;
271                 mib[3] = uid;
272                 miblen = 4;
273         }
274         if (tty && mib[2] == KERN_PROC_ALL) {
275                 mib[2] = KERN_PROC_TTY;
276                 mib[3] = tdev;
277                 miblen = 4;
278         }
279
280         st = sysctl(mib, miblen, NULL, &size, NULL, 0);
281         do {
282                 size += size / 10;
283                 newprocs = realloc(procs, size);
284                 if (newprocs == 0) {
285                         if (procs)
286                                 free(procs);
287                         errx(1, "could not reallocate memory");
288                 }
289                 procs = newprocs;
290                 st = sysctl(mib, miblen, procs, &size, NULL, 0);
291         } while (st == -1 && errno == ENOMEM);
292         if (st == -1)
293                 err(1, "could not sysctl(KERN_PROC)");
294         if (size % sizeof(struct kinfo_proc) != 0) {
295                 fprintf(stderr, "proc size mismatch (%zu total, %zu chunks)\n",
296                         size, sizeof(struct kinfo_proc));
297                 fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n");
298                 exit(1);
299         }
300         nprocs = size / sizeof(struct kinfo_proc);
301         if (dflag)
302                 printf("nprocs %d\n", nprocs);
303         mypid = getpid();
304
305         for (i = 0; i < nprocs; i++) {
306                 thispid = procs[i].kp_pid;
307                 strncpy(thiscmd, procs[i].kp_comm, MAXCOMLEN);
308                 thiscmd[MAXCOMLEN] = '\0';
309                 thistdev = procs[i].kp_tdev;
310                 thisuid = procs[i].kp_ruid;     /* real uid */
311
312                 if (thispid == mypid)
313                         continue;
314                 matched = 1;
315                 if ((int)procs[i].kp_pid < 0)
316                         matched = 0;
317                 if (user) {
318                         if (thisuid != uid)
319                                 matched = 0;
320                 }
321                 if (tty) {
322                         if (thistdev != tdev)
323                                 matched = 0;
324                 }
325                 if (jflag) {
326                         if (procs[i].kp_jailid != jailid)
327                                 matched = 0;
328                 }
329                 if (cmd) {
330                         if (mflag) {
331                                 if (regcomp(&rgx, cmd,
332                                     REG_EXTENDED|REG_NOSUB) != 0) {
333                                         mflag = 0;
334                                         warnx("%s: illegal regexp", cmd);
335                                 }
336                         }
337                         if (mflag) {
338                                 pmatch.rm_so = 0;
339                                 pmatch.rm_eo = strlen(thiscmd);
340                                 if (regexec(&rgx, thiscmd, 0, &pmatch,
341                                     REG_STARTEND) != 0)
342                                         matched = 0;
343                                 regfree(&rgx);
344                         } else {
345                                 if (strcmp(thiscmd, cmd) != 0)
346                                         matched = 0;
347                         }
348                 }
349                 if (matched == 0)
350                         continue;
351                 if (ac > 0) {
352                         matched = 0;
353                         for (j = 0; j < ac; j++) {
354                                 if (mflag) {
355                                         if (regcomp(&rgx, av[j],
356                                             REG_EXTENDED|REG_NOSUB) != 0) {
357                                                 mflag = 0;
358                                                 warnx("%s: illegal regexp", av[j]);
359                                         }
360                                 }
361                                 if (mflag) {
362                                         pmatch.rm_so = 0;
363                                         pmatch.rm_eo = strlen(thiscmd);
364                                         if (regexec(&rgx, thiscmd, 0, &pmatch,
365                                             REG_STARTEND) == 0)
366                                                 matched = 1;
367                                         regfree(&rgx);
368                                 } else {
369                                         if (strcmp(thiscmd, av[j]) == 0)
370                                                 matched = 1;
371                                 }
372                                 if (matched)
373                                         break;
374                         }
375                         if (matched == 0)
376                                 continue;
377                 }
378                 if (dflag)
379                         printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig,
380                             thiscmd, thispid, thistdev, thisuid);
381
382                 if (vflag || sflag)
383                         printf("kill -%s %d\n", upper(sys_signame[sig]),
384                             thispid);
385
386                 killed++;
387                 if (!dflag && !sflag) {
388                         if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) {
389                                 warn("kill -%s %d", upper(sys_signame[sig]),
390                                     thispid);
391                                 errors = 1;
392                         }
393                 }
394         }
395         if (!qflag && killed == 0) {
396                 fprintf(stderr, "No matching processes %swere found\n",
397                     getuid() != 0 ? "belonging to you " : "");
398                 errors = 1;
399         }
400         exit(errors);
401 }