]>
git.saurik.com Git - apple/shell_cmds.git/blob - killall/killall.c
795df0424a3b1e9f281340a2aafcfa3d030a68a1
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: src/usr.bin/killall/killall.c,v 1.31 2004/07/29 18:36:35 maxim Exp $");
31 #include <sys/param.h>
34 #endif /* !__APPLE__ */
37 #include <sys/sysctl.h>
57 fprintf(stderr
, "usage: killall [-delmsvz] [-help]\n");
58 #else /* !__APPLE__ */
59 fprintf(stderr
, "usage: killall [-delmsvz] [-help] [-j jid]\n");
60 #endif /* __APPLE__ */
62 " [-u user] [-t tty] [-c cmd] [-SIGNAL] [cmd]...\n");
63 fprintf(stderr
, "At least one option or argument to specify processes must be given.\n");
68 upper(const char *str
)
73 strncpy(buf
, str
, sizeof(buf
));
74 buf
[sizeof(buf
) - 1] = '\0';
75 for (s
= buf
; *s
; s
++)
76 *s
= toupper((unsigned char)*s
);
84 const char *const * p
;
88 for (cnt
= NSIG
, p
= sys_signame
+ 1; --cnt
; ++p
) {
89 offset
+= fprintf(fp
, "%s ", upper(*p
));
90 if (offset
>= 75 && cnt
> 1) {
102 warnx("unknown signal %s; valid signals:", name
);
108 main(int ac
, char **av
)
110 struct kinfo_proc
*procs
= NULL
, *newprocs
;
126 #endif /* !__APPLE__*/
134 #else /* !__APPLE__ */
135 char thiscmd
[MAXCOMLEN
+ 1];
136 #endif /* __APPLE__ */
140 #endif /* !__APPLE__ */
143 const char *const *p
;
148 #endif /* !__APPLE__ */
156 setlocale(LC_ALL
, "");
162 if (strcmp(*av
, "-l") == 0) {
166 if (strcmp(*av
, "-help") == 0)
179 errx(1, "must specify jid");
180 jid
= strtol(*av
, &ep
, 10);
182 errx(1, "illegal jid: %s", *av
);
183 if (jail_attach(jid
) == -1)
184 err(1, "jail_attach(): %d", jid
);
186 #endif /* !__APPLE__ */
227 if (isalpha((unsigned char)**av
)) {
228 if (strncasecmp(*av
, "sig", 3) == 0)
230 for (sig
= NSIG
, p
= sys_signame
+ 1;
232 if (strcasecmp(*p
, *av
) == 0) {
233 sig
= p
- sys_signame
;
238 } else if (isdigit((unsigned char)**av
)) {
239 sig
= strtol(*av
, &ep
, 10);
241 errx(1, "illegal signal number: %s", *av
);
242 if (sig
< 0 || sig
>= NSIG
)
255 if (user
== NULL
&& tty
== NULL
&& cmd
== NULL
&& ac
== 0)
256 #else /* !__APPLE__*/
257 if (user
== NULL
&& tty
== NULL
&& cmd
== NULL
&& !jflag
&& ac
== 0)
258 #endif /* __APPLE__ */
262 if (strncmp(tty
, "/dev/", 5) == 0)
263 snprintf(buf
, sizeof(buf
), "%s", tty
);
264 else if (strncmp(tty
, "tty", 3) == 0)
265 snprintf(buf
, sizeof(buf
), "/dev/%s", tty
);
267 snprintf(buf
, sizeof(buf
), "/dev/tty%s", tty
);
268 if (stat(buf
, &sb
) < 0)
269 err(1, "stat(%s)", buf
);
270 if (!S_ISCHR(sb
.st_mode
))
271 errx(1, "%s: not a character device", buf
);
274 printf("ttydev:0x%x\n", tdev
);
277 uid
= strtol(user
, &ep
, 10);
278 if (*user
== '\0' || *ep
!= '\0') { /* was it a number? */
281 errx(1, "user %s does not exist", user
);
284 printf("uid:%d\n", uid
);
293 printf("uid:%d\n", uid
);
300 mib
[2] = KERN_PROC_ALL
;
301 #else /* !__APPLE__ */
302 mib
[2] = KERN_PROC_PROC
;
303 #endif /* __APPLE__ */
308 mib
[2] = eflag
? KERN_PROC_UID
: KERN_PROC_RUID
;
312 mib
[2] = KERN_PROC_TTY
;
317 st
= sysctl(mib
, miblen
, NULL
, &size
, NULL
, 0);
320 newprocs
= realloc(procs
, size
);
324 errx(1, "could not reallocate memory");
327 st
= sysctl(mib
, miblen
, procs
, &size
, NULL
, 0);
328 } while (st
== -1 && errno
== ENOMEM
);
330 err(1, "could not sysctl(KERN_PROC)");
331 if (size
% sizeof(struct kinfo_proc
) != 0) {
332 fprintf(stderr
, "proc size mismatch (%zu total, %zu chunks)\n",
333 size
, sizeof(struct kinfo_proc
));
334 fprintf(stderr
, "userland out of sync with kernel, recompile libkvm etc\n");
337 nprocs
= size
/ sizeof(struct kinfo_proc
);
339 printf("nprocs %d\n", nprocs
);
342 for (i
= 0; i
< nprocs
; i
++) {
344 if ((procs
[i
].kp_proc
.p_stat
& SZOMB
) == SZOMB
&& !zflag
)
346 thispid
= procs
[i
].kp_proc
.p_pid
;
353 mib
[1] = KERN_ARGMAX
;
355 syssize
= sizeof(argmax
);
356 if (sysctl(mib
, 2, &argmax
, &syssize
, NULL
, 0) == -1)
359 procargs
= malloc(argmax
);
360 if (procargs
== NULL
)
364 mib
[1] = KERN_PROCARGS
;
367 syssize
= (size_t)argmax
;
368 if (sysctl(mib
, 3, procargs
, &syssize
, NULL
, 0) == -1) {
373 for (cp
= procargs
; cp
< &procargs
[syssize
]; cp
++) {
379 if (cp
== &procargs
[syssize
]) {
384 for (; cp
< &procargs
[syssize
]; cp
++) {
390 if (cp
== &procargs
[syssize
]) {
395 /* Strip off any path that was specified */
396 for (thiscmd
= cp
; (cp
< &procargs
[syssize
]) && (*cp
!= '\0'); cp
++) {
402 thistdev
= procs
[i
].kp_eproc
.e_tdev
;
403 #else /* !__APPLE__ */
404 if ((procs
[i
].ki_stat
& SZOMB
) == SZOMB
&& !zflag
)
406 thispid
= procs
[i
].ki_pid
;
407 strncpy(thiscmd
, procs
[i
].ki_comm
, MAXCOMLEN
);
408 thiscmd
[MAXCOMLEN
] = '\0';
409 thistdev
= procs
[i
].ki_tdev
;
410 #endif /* __APPLE__ */
413 thisuid
= procs
[i
].ki_uid
; /* effective uid */
415 thisuid
= procs
[i
].ki_ruid
; /* real uid */
416 #endif /* !__APPLE__ */
418 if (thispid
== mypid
) {
421 #endif /* __APPLE__ */
430 #endif /* !__APPLE__ */
432 if (thistdev
!= tdev
)
437 if (regcomp(&rgx
, cmd
,
438 REG_EXTENDED
|REG_NOSUB
) != 0) {
440 warnx("%s: illegal regexp", cmd
);
445 pmatch
.rm_eo
= strlen(thiscmd
);
446 if (regexec(&rgx
, thiscmd
, 0, &pmatch
,
451 if (strncmp(thiscmd
, cmd
, MAXCOMLEN
) != 0)
456 if (jflag
&& thispid
== getpid())
458 #endif /* !__APPLE__ */
462 #endif /* !__APPLE__ */
467 for (j
= 0; j
< ac
; j
++) {
469 if (regcomp(&rgx
, av
[j
],
470 REG_EXTENDED
|REG_NOSUB
) != 0) {
472 warnx("%s: illegal regexp", av
[j
]);
477 pmatch
.rm_eo
= strlen(thiscmd
);
478 if (regexec(&rgx
, thiscmd
, 0, &pmatch
,
483 if (strcmp(thiscmd
, av
[j
]) == 0)
492 #endif /* __APPLE__ */
497 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x\n", sig
,
498 thiscmd
, thispid
, thistdev
);
499 #else /* !__APPLE__ */
500 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig
,
501 thiscmd
, thispid
, thistdev
, thisuid
);
502 #endif /* __APPLE__ */
505 printf("kill -%s %d\n", upper(sys_signame
[sig
]),
509 if (!dflag
&& !sflag
) {
510 if (kill(thispid
, sig
) < 0 /* && errno != ESRCH */ ) {
511 warn("warning: kill -%s %d",
512 upper(sys_signame
[sig
]), thispid
);
518 #endif /* __APPLE__ */
521 fprintf(stderr
, "No matching processes %swere found\n",
522 getuid() != 0 ? "belonging to you " : "");