1 /* vi: set sw=4 ts=4: */
3 * Mini kill/killall[5] implementation for busybox
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
13 /* Note: kill_main is directly called from shell in order to implement
14 * kill built-in. Shell substitutes job ids with process groups first.
16 * This brings some complications:
18 * + we can't use xfunc here
19 * + we can't use applet_name
20 * + we can't use bb_show_usage
21 * (Above doesn't apply for killall[5] cases)
23 * kill %n gets translated into kill ' -<process group>' by shell (note space!)
24 * This is needed to avoid collision with kill -9 ... syntax
27 int kill_main(int argc
, char **argv
)
31 int signo
= SIGTERM
, errors
= 0, quiet
= 0;
32 #if !ENABLE_KILLALL && !ENABLE_KILLALL5
36 /* How to determine who we are? find 3rd char from the end:
37 * kill, killall, killall5
38 * ^i ^a ^l - it's unique
39 * (checking from the start is complicated by /bin/kill... case) */
40 const char char3
= argv
[0][strlen(argv
[0]) - 3];
41 #define killall (ENABLE_KILLALL && char3 == 'a')
42 #define killall5 (ENABLE_KILLALL5 && char3 == 'l')
45 /* Parse any options */
49 if (argc
< 1 || arg
[0] != '-') {
53 /* The -l option, which prints out signal names.
54 * Intended usage in shell:
55 * echo "Died of SIG`kill -l $?`"
56 * We try to mimic what kill from coreutils-6.8 does */
57 if (arg
[1] == 'l' && arg
[2] == '\0') {
59 /* Print the whole signal list */
64 while ((arg
= *++argv
)) {
65 if (isdigit(arg
[0])) {
66 signo
= bb_strtou(arg
, NULL
, 10);
68 bb_error_msg("unknown signal '%s'", arg
);
71 /* Exitcodes >= 0x80 are to be treated
72 * as "killed by signal (exitcode & 0x7f)" */
73 puts(get_signame(signo
& 0x7f));
74 /* TODO: 'bad' signal# - coreutils says:
75 * kill: 127: invalid signal
76 * we just print "127" instead */
78 signo
= get_signum(arg
);
80 bb_error_msg("unknown signal '%s'", arg
);
83 printf("%d\n", signo
);
86 /* If they specified -l, we are all done */
90 /* The -q quiet option */
91 if (killall
&& arg
[1] == 'q' && arg
[2] == '\0') {
101 arg
++; /* skip '-' */
103 /* -o PID? (if present, it always is at the end of command line) */
104 if (killall5
&& arg
[0] == 'o')
107 if (argc
> 1 && arg
[0] == 's' && arg
[1] == '\0') { /* -s SIG? */
110 } /* else it must be -SIG */
111 signo
= get_signum(arg
);
112 if (signo
< 0) { /* || signo > MAX_SIGNUM ? */
113 bb_error_msg("bad signal name '%s'", arg
);
124 procps_status_t
* p
= NULL
;
127 /* Find out our session id */
129 /* Stop all processes */
131 /* Signal all processes except those in our session */
132 while ((p
= procps_scan(p
, PSSCAN_PID
|PSSCAN_SID
))) {
135 if (p
->sid
== (unsigned)sid
136 || p
->pid
== (unsigned)pid
140 /* All remaining args must be -o PID options.
141 * Check p->pid against them. */
142 for (i
= 0; i
< argc
; i
++) {
146 if (arg
[0] != '-' || arg
[1] != 'o') {
147 bb_error_msg("bad option '%s'", arg
);
152 if (!arg
[0] && argv
[++i
])
154 omit
= bb_strtoi(arg
, NULL
, 10);
156 bb_error_msg("bad pid '%s'", arg
);
167 /* And let them continue */
172 /* Pid or name is required for kill/killall */
174 bb_error_msg("you need to specify whom to kill");
179 /* Looks like they want to do a killall. Do that */
183 pidList
= find_pid_by_name(arg
);
187 bb_error_msg("%s: no process killed", arg
);
191 for (pl
= pidList
; *pl
; pl
++) {
194 if (kill(*pl
, signo
) == 0)
198 bb_perror_msg("cannot kill pid %u", (unsigned)*pl
);
207 /* Looks like they want to do a kill. Do that */
209 /* Support shell 'space' trick */
212 pid
= bb_strtoi(arg
, NULL
, 10);
214 bb_error_msg("bad pid '%s'", arg
);
216 } else if (kill(pid
, signo
) != 0) {
217 bb_perror_msg("cannot kill pid %d", (int)pid
);