]>
git.saurik.com Git - apple/shell_cmds.git/blob - killall/killall.c
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.15 2001/10/10 17:48:44 bde Exp $
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
34 #include <sys/sysctl.h>
54 fprintf(stderr
, "usage: %s [-l] [-v] [-m] [-sig] [-u user] [-t tty] [-c cmd] [cmd]...\n", prog
);
55 fprintf(stderr
, "At least one option or argument to specify processes must be given.\n");
60 upper(const char *str
)
65 strncpy(buf
, str
, sizeof(buf
));
66 buf
[sizeof(buf
) - 1] = '\0';
67 for (s
= buf
; *s
; s
++)
76 const char *const * p
;
80 for (cnt
= NSIG
, p
= sys_signame
+ 1; --cnt
; ++p
) {
81 offset
+= fprintf(fp
, "%s ", upper(*p
));
82 if (offset
>= 75 && cnt
> 1) {
94 warnx("unknown signal %s; valid signals:", name
);
100 main(int ac
, char **av
)
102 struct kinfo_proc
*procs
= NULL
, *newprocs
;
119 char thiscmd
[MAXCOMLEN
+1];
127 const char *const *p
;
142 if (strcmp(*av
, "-l") == 0) {
146 if (strcmp(*av
, "-help") == 0)
186 if (strncasecmp(*av
, "sig", 3) == 0)
188 for (sig
= NSIG
, p
= sys_signame
+ 1;
190 if (strcasecmp(*p
, *av
) == 0) {
191 sig
= p
- sys_signame
;
196 } else if (isdigit(**av
)) {
197 sig
= strtol(*av
, &ep
, 10);
199 errx(1, "illegal signal number: %s", *av
);
200 if (sig
< 0 || sig
> NSIG
)
212 if (user
== NULL
&& tty
== NULL
&& cmd
== NULL
&& ac
== 0)
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
);
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
);
228 printf("ttydev:0x%x\n", tdev
);
231 uid
= strtol(user
, &ep
, 10);
232 if ((ep
- user
) < strlen(user
)) {
235 errx(1, "user %s does not exist", user
);
238 printf("uid:%d\n", uid
);
247 printf("uid:%d\n", uid
);
253 mib
[2] = KERN_PROC_ALL
;
257 if (user
&& mib
[2] == KERN_PROC_ALL
) {
258 mib
[2] = KERN_PROC_RUID
;
262 if (tty
&& mib
[2] == KERN_PROC_ALL
) {
263 mib
[2] = KERN_PROC_TTY
;
268 st
= sysctl(mib
, miblen
, NULL
, &size
, NULL
, 0);
271 newprocs
= realloc(procs
, size
);
275 errx(1, "could not reallocate memory");
278 st
= sysctl(mib
, miblen
, procs
, &size
, NULL
, 0);
279 } while (st
== -1 && errno
== ENOMEM
);
281 err(1, "could not sysctl(KERN_PROC)");
282 if (size
% sizeof(struct kinfo_proc
) != 0) {
283 fprintf(stderr
, "proc size mismatch (%d total, %d chunks)\n",
284 size
, sizeof(struct kinfo_proc
));
285 fprintf(stderr
, "userland out of sync with kernel, recompile libkvm etc\n");
288 nprocs
= size
/ sizeof(struct kinfo_proc
);
290 printf("nprocs %d\n", nprocs
);
292 for (i
= 0; i
< nprocs
; i
++) {
299 mib
[1] = KERN_ARGMAX
;
301 syssize
= sizeof(argmax
);
302 if (sysctl(mib
, 2, &argmax
, &syssize
, NULL
, 0) == -1)
305 procargs
= malloc(argmax
);
306 if (procargs
== NULL
)
310 mib
[1] = KERN_PROCARGS
;
311 mib
[2] = procs
[i
].kp_proc
.p_pid
;
313 syssize
= (size_t)argmax
;
314 if (sysctl(mib
, 3, procargs
, &syssize
, NULL
, 0) == -1) {
319 for (cp
= procargs
; cp
< &procargs
[syssize
]; cp
++) {
324 if (cp
== &procargs
[syssize
]) {
329 for (; cp
< &procargs
[syssize
]; cp
++) {
335 if (cp
== &procargs
[syssize
]) {
340 /* Strip off any path that was specified */
341 for (thiscmd
= cp
; (cp
< &procargs
[syssize
]) && (*cp
!= '\0'); cp
++) {
347 thiscmd
[MAXCOMLEN
] = '\0';
349 thispid
= procs
[i
].kp_proc
.p_pid
;
351 thistdev
= procs
[i
].kp_eproc
.e_tdev
;
352 thisuid
= procs
[i
].kp_eproc
.e_pcred
.p_ruid
; /* real uid */
360 if (thistdev
!= tdev
)
365 if (regcomp(&rgx
, cmd
,
366 REG_EXTENDED
|REG_NOSUB
) != 0) {
368 warnx("%s: illegal regexp", cmd
);
373 pmatch
.rm_eo
= strlen(thiscmd
);
374 if (regexec(&rgx
, thiscmd
, 0, &pmatch
,
379 if (strncmp(thiscmd
, cmd
, MAXCOMLEN
) != 0)
391 for (j
= 0; j
< ac
; j
++) {
393 if (regcomp(&rgx
, av
[j
],
394 REG_EXTENDED
|REG_NOSUB
) != 0) {
396 warnx("%s: illegal regexp", av
[j
]);
401 pmatch
.rm_eo
= strlen(thiscmd
);
402 if (regexec(&rgx
, thiscmd
, 0, &pmatch
,
407 if (strcmp(thiscmd
, av
[j
]) == 0)
420 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig
,
421 thiscmd
, thispid
, thistdev
, thisuid
);
424 printf("kill -%s %d\n", upper(sys_signame
[sig
]),
428 if (!dflag
&& !sflag
) {
429 if (kill(thispid
, sig
) < 0 /* && errno != ESRCH */ ) {
430 warn("kill -%s %d", upper(sys_signame
[sig
]),
437 fprintf(stderr
, "No matching processes %swere found\n",
438 getuid() != 0 ? "belonging to you " : "");