aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKang-Che Sung <explorer09@gmail.com>2017-01-09 18:46:58 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2017-01-09 18:46:58 +0100
commit61a91af63dbc91f85058efda5c542dfc859ab1be (patch)
tree1a6949fa3f5cf55a9ea1989ff3763a326d1821b4
parent1cc6804f6980d2732df97f15933c93d34041dd83 (diff)
downloadbusybox-w32-61a91af63dbc91f85058efda5c542dfc859ab1be.tar.gz
busybox-w32-61a91af63dbc91f85058efda5c542dfc859ab1be.tar.bz2
busybox-w32-61a91af63dbc91f85058efda5c542dfc859ab1be.zip
kill: optimizations for single-applet build
* Fix a bug with a configuration in which the shell's kill builtin would be mistreated as a killall command (i.e. '-q' works, and 'kill process_name' succeeds when it shouldn't): CONFIG_ASH_JOB_CONTROL=y CONFIG_HUSH_KILL=y # CONFIG_KILL is not set CONFIG_KILLALL=y # CONFIG_KILLALL5 is not set * Optimize out unneeded code when the relevant applets are not selected. * Move kbuild lines about shells' kill builtins from Kbuild.src to kill.c, to accompany the new HAVE_SH_KILL macro. I hope this would make maintanence a little bit easier. Signed-off-by: Kang-Che Sung <explorer09@gmail.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--procps/Kbuild.src3
-rw-r--r--procps/kill.c46
2 files changed, 28 insertions, 21 deletions
diff --git a/procps/Kbuild.src b/procps/Kbuild.src
index dedef8881..6b4fb7470 100644
--- a/procps/Kbuild.src
+++ b/procps/Kbuild.src
@@ -7,6 +7,3 @@
7lib-y:= 7lib-y:=
8 8
9INSERT 9INSERT
10
11lib-$(CONFIG_ASH_JOB_CONTROL) += kill.o # used for built-in kill by ash
12lib-$(CONFIG_HUSH_KILL) += kill.o # used for built-in kill by hush
diff --git a/procps/kill.c b/procps/kill.c
index 7c0822542..0ef1716a4 100644
--- a/procps/kill.c
+++ b/procps/kill.c
@@ -92,28 +92,34 @@
92 * This is needed to avoid collision with kill -9 ... syntax 92 * This is needed to avoid collision with kill -9 ... syntax
93 */ 93 */
94 94
95//kbuild:lib-$(CONFIG_ASH_JOB_CONTROL) += kill.o
96//kbuild:lib-$(CONFIG_HUSH_KILL) += kill.o
97
98#define SH_KILL (ENABLE_ASH_JOB_CONTROL || ENABLE_HUSH_KILL)
99/* If shells want to have "kill", for ifdefs it's like ENABLE_KILL=1 */
100#if SH_KILL
101# undef ENABLE_KILL
102# define ENABLE_KILL 1
103#endif
104#define KILL_APPLET_CNT (ENABLE_KILL + ENABLE_KILLALL + ENABLE_KILLALL5)
105
95int kill_main(int argc UNUSED_PARAM, char **argv) 106int kill_main(int argc UNUSED_PARAM, char **argv)
96{ 107{
97 char *arg; 108 char *arg;
98 pid_t pid; 109 pid_t pid;
99 int signo = SIGTERM, errors = 0, quiet = 0; 110 int signo = SIGTERM, errors = 0, quiet = 0;
100#if ENABLE_KILL && !ENABLE_KILLALL && !ENABLE_KILLALL5 111
101# define killall 0 112#if KILL_APPLET_CNT == 1
102# define killall5 0 113# define is_killall ENABLE_KILLALL
103#elif !ENABLE_KILL && ENABLE_KILLALL && !ENABLE_KILLALL5 114# define is_killall5 ENABLE_KILLALL5
104# define killall 1
105# define killall5 0
106#elif !ENABLE_KILL && !ENABLE_KILLALL && ENABLE_KILLALL5
107# define killall 0
108# define killall5 1
109#else 115#else
110/* How to determine who we are? find 3rd char from the end: 116/* How to determine who we are? find 3rd char from the end:
111 * kill, killall, killall5 117 * kill, killall, killall5
112 * ^i ^a ^l - it's unique 118 * ^i ^a ^l - it's unique
113 * (checking from the start is complicated by /bin/kill... case) */ 119 * (checking from the start is complicated by /bin/kill... case) */
114 const char char3 = argv[0][strlen(argv[0]) - 3]; 120 const char char3 = argv[0][strlen(argv[0]) - 3];
115# define killall (ENABLE_KILLALL && char3 == 'a') 121# define is_killall (ENABLE_KILLALL && char3 == 'a')
116# define killall5 (ENABLE_KILLALL5 && char3 == 'l') 122# define is_killall5 (ENABLE_KILLALL5 && char3 == 'l')
117#endif 123#endif
118 124
119 /* Parse any options */ 125 /* Parse any options */
@@ -162,7 +168,7 @@ int kill_main(int argc UNUSED_PARAM, char **argv)
162 } 168 }
163 169
164 /* The -q quiet option */ 170 /* The -q quiet option */
165 if (killall && arg[1] == 'q' && arg[2] == '\0') { 171 if (is_killall && arg[1] == 'q' && arg[2] == '\0') {
166 quiet = 1; 172 quiet = 1;
167 arg = *++argv; 173 arg = *++argv;
168 if (!arg) 174 if (!arg)
@@ -174,7 +180,7 @@ int kill_main(int argc UNUSED_PARAM, char **argv)
174 arg++; /* skip '-' */ 180 arg++; /* skip '-' */
175 181
176 /* -o PID? (if present, it always is at the end of command line) */ 182 /* -o PID? (if present, it always is at the end of command line) */
177 if (killall5 && arg[0] == 'o') 183 if (is_killall5 && arg[0] == 'o')
178 goto do_it_now; 184 goto do_it_now;
179 185
180 if (argv[1] && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */ 186 if (argv[1] && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */
@@ -190,7 +196,7 @@ int kill_main(int argc UNUSED_PARAM, char **argv)
190 do_it_now: 196 do_it_now:
191 pid = getpid(); 197 pid = getpid();
192 198
193 if (killall5) { 199 if (is_killall5) {
194 pid_t sid; 200 pid_t sid;
195 procps_status_t* p = NULL; 201 procps_status_t* p = NULL;
196 /* compat: exitcode 2 is "no one was signaled" */ 202 /* compat: exitcode 2 is "no one was signaled" */
@@ -248,13 +254,14 @@ int kill_main(int argc UNUSED_PARAM, char **argv)
248 return ret; 254 return ret;
249 } 255 }
250 256
257#if ENABLE_KILL || ENABLE_KILLALL
251 /* Pid or name is required for kill/killall */ 258 /* Pid or name is required for kill/killall */
252 if (!arg) { 259 if (!arg) {
253 bb_error_msg("you need to specify whom to kill"); 260 bb_error_msg("you need to specify whom to kill");
254 return EXIT_FAILURE; 261 return EXIT_FAILURE;
255 } 262 }
256 263
257 if (killall) { 264 if (!ENABLE_KILL || is_killall) {
258 /* Looks like they want to do a killall. Do that */ 265 /* Looks like they want to do a killall. Do that */
259 do { 266 do {
260 pid_t* pidList; 267 pid_t* pidList;
@@ -282,10 +289,12 @@ int kill_main(int argc UNUSED_PARAM, char **argv)
282 } while (arg); 289 } while (arg);
283 return errors; 290 return errors;
284 } 291 }
292#endif
285 293
294#if ENABLE_KILL
286 /* Looks like they want to do a kill. Do that */ 295 /* Looks like they want to do a kill. Do that */
287 while (arg) { 296 while (arg) {
288#if ENABLE_ASH_JOB_CONTROL || ENABLE_HUSH_KILL 297# if SH_KILL
289 /* 298 /*
290 * We need to support shell's "hack formats" of 299 * We need to support shell's "hack formats" of
291 * " -PRGP_ID" (yes, with a leading space) 300 * " -PRGP_ID" (yes, with a leading space)
@@ -307,7 +316,7 @@ int kill_main(int argc UNUSED_PARAM, char **argv)
307 } 316 }
308 arg = end; /* can only point to ' ' or '\0' now */ 317 arg = end; /* can only point to ' ' or '\0' now */
309 } 318 }
310#else 319# else /* ENABLE_KILL but !SH_KILL */
311 pid = bb_strtoi(arg, NULL, 10); 320 pid = bb_strtoi(arg, NULL, 10);
312 if (errno) { 321 if (errno) {
313 bb_error_msg("invalid number '%s'", arg); 322 bb_error_msg("invalid number '%s'", arg);
@@ -316,8 +325,9 @@ int kill_main(int argc UNUSED_PARAM, char **argv)
316 bb_perror_msg("can't kill pid %d", (int)pid); 325 bb_perror_msg("can't kill pid %d", (int)pid);
317 errors++; 326 errors++;
318 } 327 }
319#endif 328# endif
320 arg = *++argv; 329 arg = *++argv;
321 } 330 }
322 return errors; 331 return errors;
332#endif
323} 333}