]>
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")
56 #include <TargetConditionals.h>
64 fprintf(stderr
, "usage: killall [-delmsvz] [-help]\n");
65 #else /* !__APPLE__ */
66 fprintf(stderr
, "usage: killall [-delmsvz] [-help] [-j jid]\n");
67 #endif /* __APPLE__ */
69 " [-u user] [-t tty] [-c cmd] [-SIGNAL] [cmd]...\n");
70 fprintf(stderr
, "At least one option or argument to specify processes must be given.\n");
75 upper(const char *str
)
80 strncpy(buf
, str
, sizeof(buf
));
81 buf
[sizeof(buf
) - 1] = '\0';
82 for (s
= buf
; *s
; s
++)
83 *s
= toupper((unsigned char)*s
);
91 const char *const * p
;
95 for (cnt
= NSIG
, p
= sys_signame
+ 1; --cnt
; ++p
) {
96 offset
+= fprintf(fp
, "%s ", upper(*p
));
97 if (offset
>= 75 && cnt
> 1) {
109 warnx("unknown signal %s; valid signals:", name
);
115 * kludge_signal_args - remove any signal option (-SIGXXX, -##) from the argv array.
118 kludge_signal_args(int *argc
, char **argv
, int *sig
)
124 const char *const *p
;
127 /* i = 1, skip program name */
128 for (i
= 1; i
< *argc
; i
++) {
129 /* Stop kludging if we encounter -- */
130 if (strcmp(argv
[i
], "--") == 0)
133 /* Only process arguments that start with - and do not look like an existing option. */
134 if (kludge
&& *argv
[i
] == '-' && *ptr
&& strchr(OPTIONS
, *ptr
) == NULL
) {
136 if (strcmp(ptr
, "help") == 0)
138 if (strncasecmp(ptr
, "sig", 3) == 0)
140 for (*sig
= NSIG
, p
= sys_signame
+ 1; --*sig
; ++p
)
141 if (strcasecmp(*p
, ptr
) == 0) {
142 *sig
= p
- sys_signame
;
147 } else if (isdigit(*ptr
)) {
148 *sig
= strtol(ptr
, &ep
, 10);
150 errx(1, "illegal signal number: %s", ptr
);
151 if (*sig
< 0 || *sig
>= NSIG
)
160 argv
[i
- shift
] = argv
[i
];
163 for (i
= *argc
- shift
; i
< *argc
; i
++) {
171 main(int ac
, char **av
)
173 struct kinfo_proc
*procs
= NULL
, *newprocs
;
189 #endif /* !__APPLE__*/
197 #else /* !__APPLE__ */
198 char thiscmd
[MAXCOMLEN
+ 1];
199 #endif /* __APPLE__ */
203 #endif /* !__APPLE__ */
210 #endif /* !__APPLE__ */
219 setlocale(LC_ALL
, "");
221 kludge_signal_args(&ac
, av
, &sig
);
223 while ((ch
= getopt(ac
, av
, OPTIONS
)) != -1) {
237 jid
= strtol(optarg
, &ep
, 10);
239 errx(1, "illegal jid: %s", optarg
);
240 if (jail_attach(jid
) == -1)
241 err(1, "jail_attach(): %d", jid
);
243 #endif /* __APPLE__ */
274 if (user
== NULL
&& tty
== NULL
&& cmd
== NULL
&& ac
== 0)
275 #else /* !__APPLE__*/
276 if (user
== NULL
&& tty
== NULL
&& cmd
== NULL
&& !jflag
&& ac
== 0)
277 #endif /* __APPLE__ */
281 if (strncmp(tty
, "/dev/", 5) == 0)
282 snprintf(buf
, sizeof(buf
), "%s", tty
);
283 else if (strncmp(tty
, "tty", 3) == 0)
284 snprintf(buf
, sizeof(buf
), "/dev/%s", tty
);
286 snprintf(buf
, sizeof(buf
), "/dev/tty%s", tty
);
287 if (stat(buf
, &sb
) < 0)
288 err(1, "stat(%s)", buf
);
289 if (!S_ISCHR(sb
.st_mode
))
290 errx(1, "%s: not a character device", buf
);
293 printf("ttydev:0x%x\n", tdev
);
296 uid
= strtol(user
, &ep
, 10);
297 if (*user
== '\0' || *ep
!= '\0') { /* was it a number? */
300 errx(1, "user %s does not exist", user
);
303 printf("uid:%d\n", uid
);
312 printf("uid:%d\n", uid
);
319 mib
[2] = KERN_PROC_ALL
;
320 #else /* !__APPLE__ */
321 mib
[2] = KERN_PROC_PROC
;
322 #endif /* __APPLE__ */
327 mib
[2] = eflag
? KERN_PROC_UID
: KERN_PROC_RUID
;
331 mib
[2] = KERN_PROC_TTY
;
336 st
= sysctl(mib
, miblen
, NULL
, &size
, NULL
, 0);
339 newprocs
= realloc(procs
, size
);
343 errx(1, "could not reallocate memory");
346 st
= sysctl(mib
, miblen
, procs
, &size
, NULL
, 0);
347 } while (st
== -1 && errno
== ENOMEM
);
349 err(1, "could not sysctl(KERN_PROC)");
350 if (size
% sizeof(struct kinfo_proc
) != 0) {
351 fprintf(stderr
, "proc size mismatch (%zu total, %zu chunks)\n",
352 size
, sizeof(struct kinfo_proc
));
353 fprintf(stderr
, "userland out of sync with kernel, recompile libkvm etc\n");
356 nprocs
= size
/ sizeof(struct kinfo_proc
);
358 printf("nprocs %d\n", nprocs
);
361 for (i
= 0; i
< nprocs
; i
++) {
363 if (procs
[i
].kp_proc
.p_stat
== SZOMB
&& !zflag
)
365 thispid
= procs
[i
].kp_proc
.p_pid
;
372 mib
[1] = KERN_ARGMAX
;
374 syssize
= sizeof(argmax
);
375 if (sysctl(mib
, 2, &argmax
, &syssize
, NULL
, 0) == -1)
378 procargs
= malloc(argmax
);
379 if (procargs
== NULL
)
383 #if defined(__APPLE__) && TARGET_OS_EMBEDDED
384 mib
[1] = KERN_PROCARGS2
;
386 mib
[1] = KERN_PROCARGS
;
390 syssize
= (size_t)argmax
;
391 if (sysctl(mib
, 3, procargs
, &syssize
, NULL
, 0) == -1) {
396 for (cp
= procargs
; cp
< &procargs
[syssize
]; cp
++) {
402 if (cp
== &procargs
[syssize
]) {
407 for (; cp
< &procargs
[syssize
]; cp
++) {
413 if (cp
== &procargs
[syssize
]) {
418 /* Strip off any path that was specified */
419 for (thiscmd
= cp
; (cp
< &procargs
[syssize
]) && (*cp
!= '\0'); cp
++) {
425 thistdev
= procs
[i
].kp_eproc
.e_tdev
;
426 #else /* !__APPLE__ */
427 if (procs
[i
].ki_stat
== SZOMB
&& !zflag
)
429 thispid
= procs
[i
].ki_pid
;
430 strncpy(thiscmd
, procs
[i
].ki_comm
, MAXCOMLEN
);
431 thiscmd
[MAXCOMLEN
] = '\0';
432 thistdev
= procs
[i
].ki_tdev
;
433 #endif /* __APPLE__ */
436 thisuid
= procs
[i
].ki_uid
; /* effective uid */
438 thisuid
= procs
[i
].ki_ruid
; /* real uid */
439 #endif /* !__APPLE__ */
441 if (thispid
== mypid
) {
444 #endif /* __APPLE__ */
453 #endif /* !__APPLE__ */
455 if (thistdev
!= tdev
)
460 if (regcomp(&rgx
, cmd
,
461 REG_EXTENDED
|REG_NOSUB
) != 0) {
463 warnx("%s: illegal regexp", cmd
);
468 pmatch
.rm_eo
= strlen(thiscmd
);
469 if (regexec(&rgx
, thiscmd
, 0, &pmatch
,
474 if (strncmp(thiscmd
, cmd
, MAXCOMLEN
) != 0)
479 if (jflag
&& thispid
== getpid())
481 #endif /* !__APPLE__ */
485 #endif /* !__APPLE__ */
490 for (j
= 0; j
< ac
; j
++) {
492 if (regcomp(&rgx
, av
[j
],
493 REG_EXTENDED
|REG_NOSUB
) != 0) {
495 warnx("%s: illegal regexp", av
[j
]);
500 pmatch
.rm_eo
= strlen(thiscmd
);
501 if (regexec(&rgx
, thiscmd
, 0, &pmatch
,
506 if (strcmp(thiscmd
, av
[j
]) == 0)
515 #endif /* __APPLE__ */
520 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x\n", sig
,
521 thiscmd
, thispid
, thistdev
);
522 #else /* !__APPLE__ */
523 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig
,
524 thiscmd
, thispid
, thistdev
, thisuid
);
525 #endif /* __APPLE__ */
528 printf("kill -%s %d\n", upper(sys_signame
[sig
]),
532 if (!dflag
&& !sflag
) {
533 if (kill(thispid
, sig
) < 0 /* && errno != ESRCH */ ) {
534 warn("warning: kill -%s %d",
535 upper(sys_signame
[sig
]), thispid
);
541 #endif /* __APPLE__ */
544 fprintf(stderr
, "No matching processes %swere found\n",
545 getuid() != 0 ? "belonging to you " : "");