]>
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
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>
53 #define OPTIONS ("c:dej:lmst:u:vz")
60 fprintf(stderr
, "usage: killall [-delmsvz] [-help]\n");
61 #else /* !__APPLE__ */
62 fprintf(stderr
, "usage: killall [-delmsvz] [-help] [-j jid]\n");
63 #endif /* __APPLE__ */
65 " [-u user] [-t tty] [-c cmd] [-SIGNAL] [cmd]...\n");
66 fprintf(stderr
, "At least one option or argument to specify processes must be given.\n");
71 upper(const char *str
)
76 strncpy(buf
, str
, sizeof(buf
));
77 buf
[sizeof(buf
) - 1] = '\0';
78 for (s
= buf
; *s
; s
++)
79 *s
= toupper((unsigned char)*s
);
87 const char *const * p
;
91 for (cnt
= NSIG
, p
= sys_signame
+ 1; --cnt
; ++p
) {
92 offset
+= fprintf(fp
, "%s ", upper(*p
));
93 if (offset
>= 75 && cnt
> 1) {
105 warnx("unknown signal %s; valid signals:", name
);
111 * kludge_signal_args - remove any signal option (-SIGXXX, -##) from the argv array.
114 kludge_signal_args(int *argc
, char **argv
, int *sig
)
120 const char *const *p
;
123 /* i = 1, skip program name */
124 for (i
= 1; i
< *argc
; i
++) {
125 /* Stop if we encounter either a non-option or -- */
126 if (*argv
[i
] != '-' || strcmp(argv
[i
], "--") == 0)
129 if (kludge
&& strchr(OPTIONS
, *ptr
) == NULL
) {
131 if (strcmp(ptr
, "help") == 0)
133 if (strncasecmp(ptr
, "sig", 3) == 0)
135 for (*sig
= NSIG
, p
= sys_signame
+ 1; --*sig
; ++p
)
136 if (strcasecmp(*p
, ptr
) == 0) {
137 *sig
= p
- sys_signame
;
142 } else if (isdigit(*ptr
)) {
143 *sig
= strtol(ptr
, &ep
, 10);
145 errx(1, "illegal signal number: %s", ptr
);
146 if (*sig
< 0 || *sig
>= NSIG
)
155 argv
[i
- shift
] = argv
[i
];
158 for (i
= *argc
- shift
; i
< *argc
; i
++) {
166 main(int ac
, char **av
)
168 struct kinfo_proc
*procs
= NULL
, *newprocs
;
184 #endif /* !__APPLE__*/
192 #else /* !__APPLE__ */
193 char thiscmd
[MAXCOMLEN
+ 1];
194 #endif /* __APPLE__ */
198 #endif /* !__APPLE__ */
205 #endif /* !__APPLE__ */
214 setlocale(LC_ALL
, "");
216 kludge_signal_args(&ac
, av
, &sig
);
218 while ((ch
= getopt(ac
, av
, OPTIONS
)) != -1) {
232 jid
= strtol(optarg
, &ep
, 10);
234 errx(1, "illegal jid: %s", optarg
);
235 if (jail_attach(jid
) == -1)
236 err(1, "jail_attach(): %d", jid
);
238 #endif /* __APPLE__ */
269 if (user
== NULL
&& tty
== NULL
&& cmd
== NULL
&& ac
== 0)
270 #else /* !__APPLE__*/
271 if (user
== NULL
&& tty
== NULL
&& cmd
== NULL
&& !jflag
&& ac
== 0)
272 #endif /* __APPLE__ */
276 if (strncmp(tty
, "/dev/", 5) == 0)
277 snprintf(buf
, sizeof(buf
), "%s", tty
);
278 else if (strncmp(tty
, "tty", 3) == 0)
279 snprintf(buf
, sizeof(buf
), "/dev/%s", tty
);
281 snprintf(buf
, sizeof(buf
), "/dev/tty%s", tty
);
282 if (stat(buf
, &sb
) < 0)
283 err(1, "stat(%s)", buf
);
284 if (!S_ISCHR(sb
.st_mode
))
285 errx(1, "%s: not a character device", buf
);
288 printf("ttydev:0x%x\n", tdev
);
291 uid
= strtol(user
, &ep
, 10);
292 if (*user
== '\0' || *ep
!= '\0') { /* was it a number? */
295 errx(1, "user %s does not exist", user
);
298 printf("uid:%d\n", uid
);
307 printf("uid:%d\n", uid
);
314 mib
[2] = KERN_PROC_ALL
;
315 #else /* !__APPLE__ */
316 mib
[2] = KERN_PROC_PROC
;
317 #endif /* __APPLE__ */
322 mib
[2] = eflag
? KERN_PROC_UID
: KERN_PROC_RUID
;
326 mib
[2] = KERN_PROC_TTY
;
331 st
= sysctl(mib
, miblen
, NULL
, &size
, NULL
, 0);
334 newprocs
= realloc(procs
, size
);
338 errx(1, "could not reallocate memory");
341 st
= sysctl(mib
, miblen
, procs
, &size
, NULL
, 0);
342 } while (st
== -1 && errno
== ENOMEM
);
344 err(1, "could not sysctl(KERN_PROC)");
345 if (size
% sizeof(struct kinfo_proc
) != 0) {
346 fprintf(stderr
, "proc size mismatch (%zu total, %zu chunks)\n",
347 size
, sizeof(struct kinfo_proc
));
348 fprintf(stderr
, "userland out of sync with kernel, recompile libkvm etc\n");
351 nprocs
= size
/ sizeof(struct kinfo_proc
);
353 printf("nprocs %d\n", nprocs
);
356 for (i
= 0; i
< nprocs
; i
++) {
358 if ((procs
[i
].kp_proc
.p_stat
& SZOMB
) == SZOMB
&& !zflag
)
360 thispid
= procs
[i
].kp_proc
.p_pid
;
367 mib
[1] = KERN_ARGMAX
;
369 syssize
= sizeof(argmax
);
370 if (sysctl(mib
, 2, &argmax
, &syssize
, NULL
, 0) == -1)
373 procargs
= malloc(argmax
);
374 if (procargs
== NULL
)
378 mib
[1] = KERN_PROCARGS
;
381 syssize
= (size_t)argmax
;
382 if (sysctl(mib
, 3, procargs
, &syssize
, NULL
, 0) == -1) {
387 for (cp
= procargs
; cp
< &procargs
[syssize
]; cp
++) {
393 if (cp
== &procargs
[syssize
]) {
398 for (; cp
< &procargs
[syssize
]; cp
++) {
404 if (cp
== &procargs
[syssize
]) {
409 /* Strip off any path that was specified */
410 for (thiscmd
= cp
; (cp
< &procargs
[syssize
]) && (*cp
!= '\0'); cp
++) {
416 thistdev
= procs
[i
].kp_eproc
.e_tdev
;
417 #else /* !__APPLE__ */
418 if ((procs
[i
].ki_stat
& SZOMB
) == SZOMB
&& !zflag
)
420 thispid
= procs
[i
].ki_pid
;
421 strncpy(thiscmd
, procs
[i
].ki_comm
, MAXCOMLEN
);
422 thiscmd
[MAXCOMLEN
] = '\0';
423 thistdev
= procs
[i
].ki_tdev
;
424 #endif /* __APPLE__ */
427 thisuid
= procs
[i
].ki_uid
; /* effective uid */
429 thisuid
= procs
[i
].ki_ruid
; /* real uid */
430 #endif /* !__APPLE__ */
432 if (thispid
== mypid
) {
435 #endif /* __APPLE__ */
444 #endif /* !__APPLE__ */
446 if (thistdev
!= tdev
)
451 if (regcomp(&rgx
, cmd
,
452 REG_EXTENDED
|REG_NOSUB
) != 0) {
454 warnx("%s: illegal regexp", cmd
);
459 pmatch
.rm_eo
= strlen(thiscmd
);
460 if (regexec(&rgx
, thiscmd
, 0, &pmatch
,
465 if (strncmp(thiscmd
, cmd
, MAXCOMLEN
) != 0)
470 if (jflag
&& thispid
== getpid())
472 #endif /* !__APPLE__ */
476 #endif /* !__APPLE__ */
481 for (j
= 0; j
< ac
; j
++) {
483 if (regcomp(&rgx
, av
[j
],
484 REG_EXTENDED
|REG_NOSUB
) != 0) {
486 warnx("%s: illegal regexp", av
[j
]);
491 pmatch
.rm_eo
= strlen(thiscmd
);
492 if (regexec(&rgx
, thiscmd
, 0, &pmatch
,
497 if (strcmp(thiscmd
, av
[j
]) == 0)
506 #endif /* __APPLE__ */
511 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x\n", sig
,
512 thiscmd
, thispid
, thistdev
);
513 #else /* !__APPLE__ */
514 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig
,
515 thiscmd
, thispid
, thistdev
, thisuid
);
516 #endif /* __APPLE__ */
519 printf("kill -%s %d\n", upper(sys_signame
[sig
]),
523 if (!dflag
&& !sflag
) {
524 if (kill(thispid
, sig
) < 0 /* && errno != ESRCH */ ) {
525 warn("warning: kill -%s %d",
526 upper(sys_signame
[sig
]), thispid
);
532 #endif /* __APPLE__ */
535 fprintf(stderr
, "No matching processes %swere found\n",
536 getuid() != 0 ? "belonging to you " : "");