961f8cb14bcce01f3c9f27c5e3c5d23208fd646e
[oweals/busybox.git] / procps / kill.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini kill/killall[5] implementation for busybox
4  *
5  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7  *
8  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
9  */
10
11 #include "libbb.h"
12
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.
15  *
16  * This brings some complications:
17  *
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)
22  *
23  * kill %n gets translated into kill ' -<process group>' by shell (note space!)
24  * This is needed to avoid collision with kill -9 ... syntax
25  */
26
27 int kill_main(int argc, char **argv);
28 int kill_main(int argc, char **argv)
29 {
30         char *arg;
31         pid_t pid;
32         int signo = SIGTERM, errors = 0, quiet = 0;
33 #if !ENABLE_KILLALL && !ENABLE_KILLALL5
34 #define killall 0
35 #define killall5 0
36 #else
37 /* How to determine who we are? find 3rd char from the end:
38  * kill, killall, killall5
39  *  ^i       ^a        ^l  - it's unique
40  * (checking from the start is complicated by /bin/kill... case) */
41         const char char3 = argv[0][strlen(argv[0]) - 3];
42 #define killall (ENABLE_KILLALL && char3 == 'a')
43 #define killall5 (ENABLE_KILLALL5 && char3 == 'l')
44 #endif
45
46         /* Parse any options */
47         argc--;
48         arg = *++argv;
49
50         if (argc < 1 || arg[0] != '-') {
51                 goto do_it_now;
52         }
53
54         /* The -l option, which prints out signal names.
55          * Intended usage in shell:
56          * echo "Died of SIG`kill -l $?`"
57          * We try to mimic what kill from coreutils-6.8 does */
58         if (arg[1] == 'l' && arg[2] == '\0') {
59                 if (argc == 1) {
60                         /* Print the whole signal list */
61                         print_signames_and_exit();
62                 }
63                 /* -l <sig list> */
64                 while ((arg = *++argv)) {
65                         if (isdigit(arg[0])) {
66                                 signo = bb_strtou(arg, NULL, 10);
67                                 if (errno) {
68                                         bb_error_msg("unknown signal '%s'", arg);
69                                         return EXIT_FAILURE;
70                                 }
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 */
77                         } else {
78                                 signo = get_signum(arg);
79                                 if (signo < 0) {
80                                         bb_error_msg("unknown signal '%s'", arg);
81                                         return EXIT_FAILURE;
82                                 }
83                                 printf("%d\n", signo);
84                         }
85                 }
86                 /* If they specified -l, we are all done */
87                 return EXIT_SUCCESS;
88         }
89
90         /* The -q quiet option */
91         if (killall && arg[1] == 'q' && arg[2] == '\0') {
92                 quiet = 1;
93                 arg = *++argv;
94                 argc--;
95                 if (argc < 1) bb_show_usage();
96                 if (arg[0] != '-') goto do_it_now;
97         }
98
99         /* -SIG */
100         signo = get_signum(&arg[1]);
101         if (signo < 0) { /* || signo > MAX_SIGNUM ? */
102                 bb_error_msg("bad signal name '%s'", &arg[1]);
103                 return EXIT_FAILURE;
104         }
105         arg = *++argv;
106         argc--;
107
108 do_it_now:
109
110         if (killall5) {
111                 pid_t sid;
112                 procps_status_t* p = NULL;
113
114                 /* Now stop all processes */
115                 kill(-1, SIGSTOP);
116                 /* Find out our own session id */
117                 pid = getpid();
118                 sid = getsid(pid);
119                 /* Now kill all processes except our session */
120                 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) {
121                         if (p->sid != sid && p->pid != pid && p->pid != 1)
122                                 kill(p->pid, signo);
123                 }
124                 /* And let them continue */
125                 kill(-1, SIGCONT);
126                 return 0;
127         }
128
129         /* Pid or name is required for kill/killall */
130         if (argc < 1) {
131                 puts("You need to specify whom to kill");
132                 return EXIT_FAILURE;
133         }
134
135         if (killall) {
136                 /* Looks like they want to do a killall.  Do that */
137                 pid = getpid();
138                 while (arg) {
139                         pid_t* pidList;
140
141                         pidList = find_pid_by_name(arg);
142                         if (*pidList == 0) {
143                                 errors++;
144                                 if (!quiet)
145                                         bb_error_msg("%s: no process killed", arg);
146                         } else {
147                                 pid_t *pl;
148
149                                 for (pl = pidList; *pl; pl++) {
150                                         if (*pl == pid)
151                                                 continue;
152                                         if (kill(*pl, signo) == 0)
153                                                 continue;
154                                         errors++;
155                                         if (!quiet)
156                                                 bb_perror_msg("cannot kill pid %u", (unsigned)*pl);
157                                 }
158                         }
159                         free(pidList);
160                         arg = *++argv;
161                 }
162                 return errors;
163         }
164
165         /* Looks like they want to do a kill. Do that */
166         while (arg) {
167                 /* Support shell 'space' trick */
168                 if (arg[0] == ' ')
169                         arg++;
170                 pid = bb_strtoi(arg, NULL, 10);
171                 if (errno) {
172                         bb_error_msg("bad pid '%s'", arg);
173                         errors++;
174                 } else if (kill(pid, signo) != 0) {
175                         bb_perror_msg("cannot kill pid %d", (int)pid);
176                         errors++;
177                 }
178                 arg = *++argv;
179         }
180         return errors;
181 }