kill: fix segfault in arguments parsing
[platform/upstream/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 GPLv2 or later, see file LICENSE in this source tree.
9  */
10
11 //usage:#define kill_trivial_usage
12 //usage:       "[-l] [-SIG] PID..."
13 //usage:#define kill_full_usage "\n\n"
14 //usage:       "Send a signal (default: TERM) to given PIDs\n"
15 //usage:     "\n        -l      List all signal names and numbers"
16 /* //usage:  "\n        -s SIG  Yet another way of specifying SIG" */
17 //usage:
18 //usage:#define kill_example_usage
19 //usage:       "$ ps | grep apache\n"
20 //usage:       "252 root     root     S [apache]\n"
21 //usage:       "263 www-data www-data S [apache]\n"
22 //usage:       "264 www-data www-data S [apache]\n"
23 //usage:       "265 www-data www-data S [apache]\n"
24 //usage:       "266 www-data www-data S [apache]\n"
25 //usage:       "267 www-data www-data S [apache]\n"
26 //usage:       "$ kill 252\n"
27 //usage:
28 //usage:#define killall_trivial_usage
29 //usage:       "[-l] [-q] [-SIG] PROCESS_NAME..."
30 //usage:#define killall_full_usage "\n\n"
31 //usage:       "Send a signal (default: TERM) to given processes\n"
32 //usage:     "\n        -l      List all signal names and numbers"
33 /* //usage:  "\n        -s SIG  Yet another way of specifying SIG" */
34 //usage:     "\n        -q      Don't complain if no processes were killed"
35 //usage:
36 //usage:#define killall_example_usage
37 //usage:       "$ killall apache\n"
38 //usage:
39 //usage:#define killall5_trivial_usage
40 //usage:       "[-l] [-SIG] [-o PID]..."
41 //usage:#define killall5_full_usage "\n\n"
42 //usage:       "Send a signal (default: TERM) to all processes outside current session\n"
43 //usage:     "\n        -l      List all signal names and numbers"
44 //usage:     "\n        -o PID  Don't signal this PID"
45 /* //usage:  "\n        -s SIG  Yet another way of specifying SIG" */
46
47 #include "libbb.h"
48
49 /* Note: kill_main is directly called from shell in order to implement
50  * kill built-in. Shell substitutes job ids with process groups first.
51  *
52  * This brings some complications:
53  *
54  * + we can't use xfunc here
55  * + we can't use applet_name
56  * + we can't use bb_show_usage
57  * (Above doesn't apply for killall[5] cases)
58  *
59  * kill %n gets translated into kill ' -<process group>' by shell (note space!)
60  * This is needed to avoid collision with kill -9 ... syntax
61  */
62
63 int kill_main(int argc, char **argv)
64 {
65         char *arg;
66         pid_t pid;
67         int signo = SIGTERM, errors = 0, quiet = 0;
68 #if !ENABLE_KILLALL && !ENABLE_KILLALL5
69 #define killall 0
70 #define killall5 0
71 #else
72 /* How to determine who we are? find 3rd char from the end:
73  * kill, killall, killall5
74  *  ^i       ^a        ^l  - it's unique
75  * (checking from the start is complicated by /bin/kill... case) */
76         const char char3 = argv[0][strlen(argv[0]) - 3];
77 #define killall (ENABLE_KILLALL && char3 == 'a')
78 #define killall5 (ENABLE_KILLALL5 && char3 == 'l')
79 #endif
80
81         /* Parse any options */
82         argc--;
83         arg = *++argv;
84
85         if (argc < 1 || arg[0] != '-') {
86                 goto do_it_now;
87         }
88
89         /* The -l option, which prints out signal names.
90          * Intended usage in shell:
91          * echo "Died of SIG`kill -l $?`"
92          * We try to mimic what kill from coreutils-6.8 does */
93         if (arg[1] == 'l' && arg[2] == '\0') {
94                 if (argc == 1) {
95                         /* Print the whole signal list */
96                         print_signames();
97                         return 0;
98                 }
99                 /* -l <sig list> */
100                 while ((arg = *++argv)) {
101                         if (isdigit(arg[0])) {
102                                 signo = bb_strtou(arg, NULL, 10);
103                                 if (errno) {
104                                         bb_error_msg("unknown signal '%s'", arg);
105                                         return EXIT_FAILURE;
106                                 }
107                                 /* Exitcodes >= 0x80 are to be treated
108                                  * as "killed by signal (exitcode & 0x7f)" */
109                                 puts(get_signame(signo & 0x7f));
110                                 /* TODO: 'bad' signal# - coreutils says:
111                                  * kill: 127: invalid signal
112                                  * we just print "127" instead */
113                         } else {
114                                 signo = get_signum(arg);
115                                 if (signo < 0) {
116                                         bb_error_msg("unknown signal '%s'", arg);
117                                         return EXIT_FAILURE;
118                                 }
119                                 printf("%d\n", signo);
120                         }
121                 }
122                 /* If they specified -l, we are all done */
123                 return EXIT_SUCCESS;
124         }
125
126         /* The -q quiet option */
127         if (killall && arg[1] == 'q' && arg[2] == '\0') {
128                 quiet = 1;
129                 arg = *++argv;
130                 argc--;
131                 if (argc < 1)
132                         bb_show_usage();
133                 if (arg[0] != '-')
134                         goto do_it_now;
135         }
136
137         arg++; /* skip '-' */
138
139         /* -o PID? (if present, it always is at the end of command line) */
140         if (killall5 && arg[0] == 'o')
141                 goto do_it_now;
142
143         if (argc > 1 && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */
144                 argc--;
145                 arg = *++argv;
146         } /* else it must be -SIG */
147         signo = get_signum(arg);
148         if (signo < 0) { /* || signo > MAX_SIGNUM ? */
149                 bb_error_msg("bad signal name '%s'", arg);
150                 return EXIT_FAILURE;
151         }
152         arg = *++argv;
153         argc--;
154
155  do_it_now:
156         pid = getpid();
157
158         if (killall5) {
159                 pid_t sid;
160                 procps_status_t* p = NULL;
161                 int ret = 0;
162
163                 /* Find out our session id */
164                 sid = getsid(pid);
165                 /* Stop all processes */
166                 kill(-1, SIGSTOP);
167                 /* Signal all processes except those in our session */
168                 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID)) != NULL) {
169                         int i;
170
171                         if (p->sid == (unsigned)sid
172                          || p->pid == (unsigned)pid
173                          || p->pid == 1
174                         ) {
175                                 continue;
176                         }
177
178                         /* All remaining args must be -o PID options.
179                          * Check p->pid against them. */
180                         for (i = 0; i < argc; i++) {
181                                 pid_t omit;
182
183                                 arg = argv[i];
184                                 if (arg[0] != '-' || arg[1] != 'o') {
185                                         bb_error_msg("bad option '%s'", arg);
186                                         ret = 1;
187                                         goto resume;
188                                 }
189                                 arg += 2;
190                                 if (!arg[0] && argv[++i])
191                                         arg = argv[i];
192                                 omit = bb_strtoi(arg, NULL, 10);
193                                 if (errno) {
194                                         bb_error_msg("invalid number '%s'", arg);
195                                         ret = 1;
196                                         goto resume;
197                                 }
198                                 if (p->pid == omit)
199                                         goto dont_kill;
200                         }
201                         kill(p->pid, signo);
202  dont_kill: ;
203                 }
204  resume:
205                 /* And let them continue */
206                 kill(-1, SIGCONT);
207                 return ret;
208         }
209
210         /* Pid or name is required for kill/killall */
211         if (argc < 1) {
212                 bb_error_msg("you need to specify whom to kill");
213                 return EXIT_FAILURE;
214         }
215
216         if (killall) {
217                 /* Looks like they want to do a killall.  Do that */
218                 while (arg) {
219                         pid_t* pidList;
220
221                         pidList = find_pid_by_name(arg);
222                         if (*pidList == 0) {
223                                 errors++;
224                                 if (!quiet)
225                                         bb_error_msg("%s: no process killed", arg);
226                         } else {
227                                 pid_t *pl;
228
229                                 for (pl = pidList; *pl; pl++) {
230                                         if (*pl == pid)
231                                                 continue;
232                                         if (kill(*pl, signo) == 0)
233                                                 continue;
234                                         errors++;
235                                         if (!quiet)
236                                                 bb_perror_msg("can't kill pid %d", (int)*pl);
237                                 }
238                         }
239                         free(pidList);
240                         arg = *++argv;
241                 }
242                 return errors;
243         }
244
245         /* Looks like they want to do a kill. Do that */
246         while (arg) {
247 #if ENABLE_ASH || ENABLE_HUSH
248                 /*
249                  * We need to support shell's "hack formats" of
250                  * " -PRGP_ID" (yes, with a leading space)
251                  * and " PID1 PID2 PID3" (with degenerate case "")
252                  */
253                 while (*arg != '\0') {
254                         char *end;
255                         if (*arg == ' ')
256                                 arg++;
257                         pid = bb_strtoi(arg, &end, 10);
258                         if (errno && (errno != EINVAL || *end != ' ')) {
259                                 bb_error_msg("invalid number '%s'", arg);
260                                 errors++;
261                                 break;
262                         }
263                         if (kill(pid, signo) != 0) {
264                                 bb_perror_msg("can't kill pid %d", (int)pid);
265                                 errors++;
266                         }
267                         arg = end; /* can only point to ' ' or '\0' now */
268                 }
269 #else
270                 pid = bb_strtoi(arg, NULL, 10);
271                 if (errno) {
272                         bb_error_msg("invalid number '%s'", arg);
273                         errors++;
274                 } else if (kill(pid, signo) != 0) {
275                         bb_perror_msg("can't kill pid %d", (int)pid);
276                         errors++;
277                 }
278 #endif
279                 arg = *++argv;
280         }
281         return errors;
282 }