1 /* vi: set sw=4 ts=4: */
3 * Mini kill/killall 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 int kill_main(int argc, char **argv)
17 int signo = SIGTERM, errors = 0, quiet = 0;
18 const int killall = (ENABLE_KILLALL && applet_name[4] == 'a'
19 && (!ENABLE_KILLALL5 || applet_name[7] != '5'));
20 const int killall5 = (ENABLE_KILLALL5 && applet_name[4] == 'a'
21 && (!ENABLE_KILLALL || applet_name[7] == '5'));
23 /* Parse any options */
27 if (argc < 1 || arg[0] != '-') {
31 /* The -l option, which prints out signal names. */
32 if (arg[1] == 'l' && arg[2] == '\0') {
35 /* Print the whole signal list */
37 for (signo = 1; signo < 32; signo++) {
38 name = get_signame(signo);
39 if (isdigit(name[0])) continue;
44 col += printf("%2d) %-6s", signo, name);
47 } else { /* -l <sig list> */
48 while ((arg = *++argv)) {
49 if (isdigit(arg[0])) {
51 name = get_signame(signo);
53 signo = get_signum(arg);
55 bb_error_msg_and_die("unknown signal '%s'", arg);
56 name = get_signame(signo);
58 printf("%2d) %s\n", signo, name);
61 /* If they specified -l, we are all done */
65 /* The -q quiet option */
66 if (killall && arg[1] == 'q' && arg[2] == '\0') {
70 if (argc < 1) bb_show_usage();
71 if (arg[0] != '-') goto do_it_now;
75 signo = get_signum(&arg[1]);
77 bb_error_msg_and_die("bad signal name '%s'", &arg[1]);
85 procps_status_t* p = NULL;
87 // Cannot happen anyway? We don't TERM ourself, we STOP
88 // /* kill(-1, sig) on Linux (at least 2.1.x)
89 // * might send signal to the calling process too */
90 // signal(SIGTERM, SIG_IGN);
91 /* Now stop all processes */
93 /* Find out our own session id */
96 /* Now kill all processes except our session */
97 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) {
98 if (p->sid != sid && p->pid != pid && p->pid != 1)
101 /* And let them continue */
106 /* Pid or name required for kill/killall */
111 /* Looks like they want to do a killall. Do that */
116 pidList = find_pid_by_name(arg);
120 bb_error_msg("%s: no process killed", arg);
124 for (pl = pidList; *pl; pl++) {
127 if (kill(*pl, signo) == 0)
131 bb_perror_msg("cannot kill pid %u", (unsigned)*pl);
140 /* Looks like they want to do a kill. Do that */
143 if (!isdigit(arg[0]) && arg[0] != '-')
144 bb_error_msg_and_die("bad pid '%s'", arg);
147 /* FIXME: better overflow check? */
148 if (kill(pid, signo) != 0) {
149 bb_perror_msg("cannot kill pid %u", (unsigned)pid);