2 * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org>
3 * Copyright (c) 2000 Paul Saab <ps@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/usr.bin/killall/killall.c,v 1.5.2.4 2001/05/19 19:22:49 phk Exp $
31 #include <sys/param.h>
33 #include <sys/sysctl.h>
47 #define PHASH_SIZE 1024
51 struct kinfo_proc
*proc
;
52 struct pchain
*parent
;
56 static struct pchain
*phash
[PHASH_SIZE
];
58 static struct pchain
*saveparents(struct kinfo_proc
*procs
, int nprocs
,
60 static int checkparent(struct pchain
*mychain
, pid_t pid
);
66 fprintf(stderr
, "usage: %s [-l] [-v] [-m] [-sig] "
67 "[-u user] [-j jail] [-t tty] [-T] "
68 "[-c cmd] [cmd]...\n", prog
);
69 fprintf(stderr
, "At least one option or argument to specify "
70 "processes must be given.\n");
75 upper(const char *str
)
80 strlcpy(buf
, str
, sizeof(buf
));
81 for (s
= buf
; *s
; s
++)
82 *s
= toupper((unsigned char)*s
);
90 const char *const * p
;
94 for (cnt
= NSIG
, p
= sys_signame
+ 1; --cnt
; ++p
) {
95 offset
+= fprintf(fp
, "%s ", upper(*p
));
96 if (offset
>= 75 && cnt
> 1) {
108 warnx("unknown signal %s; valid signals:", name
);
114 main(int ac
, char **av
)
116 struct kinfo_proc
*procs
= NULL
, *newprocs
;
117 struct pchain
*mychain
;
130 int jflag
= 0, jailid
= 0;
137 char thiscmd
[MAXCOMLEN
+ 1];
142 const char *const *p
;
157 if (strcmp(*av
, "-l") == 0) {
161 if (strcmp(*av
, "-help") == 0)
194 jailid
= strtonum(*av
, 1, INT_MAX
, &errstr
);
197 errx(1, "jail id is %s: %s", errstr
, *av
);
220 if (isalpha((unsigned char)**av
)) {
221 if (strncasecmp(*av
, "sig", 3) == 0)
223 for (sig
= NSIG
, p
= sys_signame
+ 1;
225 if (strcasecmp(*p
, *av
) == 0) {
226 sig
= p
- sys_signame
;
231 } else if (isdigit((unsigned char)**av
)) {
232 sig
= strtol(*av
, &ep
, 10);
234 errx(1, "illegal signal number: %s", *av
);
235 if (sig
< 0 || sig
>= NSIG
)
247 if (user
== NULL
&& tty
== NULL
&& cmd
== NULL
&&
248 jflag
== 0 && Tflag
== 0 && ac
== 0) {
259 if (strncmp(tty
, "/dev/", 5) == 0)
260 snprintf(buf
, sizeof(buf
), "%s", tty
);
261 else if (strncmp(tty
, "tty", 3) == 0)
262 snprintf(buf
, sizeof(buf
), "/dev/%s", tty
);
263 else if (isdigit(tty
[0]))
264 snprintf(buf
, sizeof(buf
), "/dev/pts/%s", tty
);
266 snprintf(buf
, sizeof(buf
), "/dev/tty%s", tty
);
267 if (stat(buf
, &sb
) < 0)
268 err(1, "stat(%s)", buf
);
269 if (!S_ISCHR(sb
.st_mode
))
270 errx(1, "%s: not a character device", buf
);
273 printf("ttydev:0x%x\n", tdev
);
278 errx(1, "user %s does not exist", user
);
281 printf("uid:%d\n", uid
);
289 printf("uid:%d\n", uid
);
295 mib
[2] = KERN_PROC_ALL
;
299 if (user
&& mib
[2] == KERN_PROC_ALL
) {
300 mib
[2] = KERN_PROC_RUID
;
304 if (tty
&& mib
[2] == KERN_PROC_ALL
) {
305 mib
[2] = KERN_PROC_TTY
;
310 st
= sysctl(mib
, miblen
, NULL
, &size
, NULL
, 0);
313 newprocs
= realloc(procs
, size
);
314 if (newprocs
== NULL
) {
317 errx(1, "could not reallocate memory");
320 st
= sysctl(mib
, miblen
, procs
, &size
, NULL
, 0);
321 } while (st
== -1 && errno
== ENOMEM
);
323 err(1, "could not sysctl(KERN_PROC)");
324 if (size
% sizeof(struct kinfo_proc
) != 0) {
325 fprintf(stderr
, "proc size mismatch (%zu total, %zu chunks)\n",
326 size
, sizeof(struct kinfo_proc
));
327 fprintf(stderr
, "userland out of sync with kernel, recompile libkvm etc\n");
330 nprocs
= size
/ sizeof(struct kinfo_proc
);
332 printf("nprocs %d\n", nprocs
);
336 * Record parent chain if Tflag
339 mychain
= saveparents(procs
, nprocs
, mypid
);
346 for (i
= 0; i
< nprocs
; i
++) {
347 thispid
= procs
[i
].kp_pid
;
348 strncpy(thiscmd
, procs
[i
].kp_comm
, MAXCOMLEN
);
349 thiscmd
[MAXCOMLEN
] = '\0';
350 thistdev
= procs
[i
].kp_tdev
;
351 thisuid
= procs
[i
].kp_ruid
; /* real uid */
353 if (thispid
== mypid
)
356 if ((int)procs
[i
].kp_pid
< 0)
363 if (thistdev
!= tdev
)
365 if (Tflag
&& checkparent(mychain
, procs
[i
].kp_pid
))
369 if (procs
[i
].kp_jailid
!= jailid
)
374 if (regcomp(&rgx
, cmd
,
375 REG_EXTENDED
|REG_NOSUB
) != 0) {
377 warnx("%s: illegal regexp", cmd
);
382 pmatch
.rm_eo
= strlen(thiscmd
);
383 if (regexec(&rgx
, thiscmd
, 0, &pmatch
,
388 if (strcmp(thiscmd
, cmd
) != 0)
396 for (j
= 0; j
< ac
; j
++) {
398 if (regcomp(&rgx
, av
[j
],
399 REG_EXTENDED
|REG_NOSUB
) != 0) {
401 warnx("%s: illegal regexp", av
[j
]);
406 pmatch
.rm_eo
= strlen(thiscmd
);
407 if (regexec(&rgx
, thiscmd
, 0, &pmatch
,
412 if (strcmp(thiscmd
, av
[j
]) == 0)
422 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig
,
423 thiscmd
, thispid
, thistdev
, thisuid
);
426 printf("kill -%s %d\n", upper(sys_signame
[sig
]),
430 if (!dflag
&& !sflag
) {
431 if (kill(thispid
, sig
) < 0 /* && errno != ESRCH */ ) {
432 warn("kill -%s %d", upper(sys_signame
[sig
]),
438 if (!qflag
&& killed
== 0) {
439 fprintf(stderr
, "No matching processes %swere found\n",
440 getuid() != 0 ? "belonging to you " : "");
449 saveparents(struct kinfo_proc
*procs
, int nprocs
, pid_t mypid
)
451 struct pchain
*rchain
= NULL
;
452 struct pchain
*chain
;
453 struct pchain
*pchain
;
457 for (i
= 0; i
< nprocs
; ++i
) {
458 if ((int)procs
[i
].kp_pid
< 0)
460 hv
= (int)procs
[i
].kp_pid
& 1023;
461 chain
= malloc(sizeof(*chain
));
462 chain
->proc
= &procs
[i
];
463 chain
->parent
= NULL
;
464 chain
->next
= phash
[hv
];
466 if (mypid
== procs
[i
].kp_pid
)
469 for (i
= 0; i
< nprocs
; ++i
) {
470 if ((int)procs
[i
].kp_pid
< 0)
472 if ((int)procs
[i
].kp_ppid
< 0)
474 hv
= (int)procs
[i
].kp_pid
& 1023;
475 for (chain
= phash
[hv
]; chain
; chain
= chain
->next
) {
476 if (chain
->proc
->kp_pid
== procs
[i
].kp_pid
)
479 hv
= (int)procs
[i
].kp_ppid
& 1023;
480 for (pchain
= phash
[hv
]; pchain
; pchain
= pchain
->next
) {
481 if (pchain
->proc
->kp_pid
== procs
[i
].kp_ppid
) {
483 chain
->parent
= pchain
;
493 checkparent(struct pchain
*chain
, pid_t pid
)
496 if (chain
->proc
->kp_pid
== pid
)
498 chain
= chain
->parent
;