aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2007-12-25 04:30:14 +0000
committerMike Frysinger <vapier@gentoo.org>2007-12-25 04:30:14 +0000
commitbb50fdfe07072f9e1f25eb6014176ace3f738d08 (patch)
tree44820381a3d1cbbf1d8c879cf05503c2aaef4c37
parentec5631b6d697895ff4f1f750840326f9f035df96 (diff)
downloadbusybox-w32-bb50fdfe07072f9e1f25eb6014176ace3f738d08.tar.gz
busybox-w32-bb50fdfe07072f9e1f25eb6014176ace3f738d08.tar.bz2
busybox-w32-bb50fdfe07072f9e1f25eb6014176ace3f738d08.zip
convert #if to if -- let gcc optimize away dead code for us
-rw-r--r--init/Config.in3
-rw-r--r--init/init.c239
2 files changed, 109 insertions, 133 deletions
diff --git a/init/Config.in b/init/Config.in
index 1084de905..efc8beaf9 100644
--- a/init/Config.in
+++ b/init/Config.in
@@ -37,10 +37,9 @@ config FEATURE_KILL_REMOVED
37 been removed. 37 been removed.
38 38
39config FEATURE_KILL_DELAY 39config FEATURE_KILL_DELAY
40 int "How long to wait between TERM and KILL (0 - send TERM only)" 40 int "How long to wait between TERM and KILL (0 - send TERM only)" if FEATURE_KILL_REMOVED
41 range 0 1024 41 range 0 1024
42 default 0 42 default 0
43 depends on FEATURE_KILL_REMOVED
44 help 43 help
45 With nonzero setting, init sends TERM, forks, child waits N 44 With nonzero setting, init sends TERM, forks, child waits N
46 seconds, sends KILL and exits. Setting it too high is unwise 45 seconds, sends KILL and exits. Setting it too high is unwise
diff --git a/init/init.c b/init/init.c
index e6581880c..f2e5d9d43 100644
--- a/init/init.c
+++ b/init/init.c
@@ -12,16 +12,12 @@
12#include "libbb.h" 12#include "libbb.h"
13#include <paths.h> 13#include <paths.h>
14#include <sys/reboot.h> 14#include <sys/reboot.h>
15 15#include <sys/syslog.h>
16#if ENABLE_FEATURE_INIT_SYSLOG
17# include <sys/syslog.h>
18#endif
19 16
20#define INIT_BUFFS_SIZE 256 17#define INIT_BUFFS_SIZE 256
21#define CONSOLE_NAME_SIZE 32 18#define CONSOLE_NAME_SIZE 32
22#define MAXENV 16 /* Number of env. vars */ 19#define MAXENV 16 /* Number of env. vars */
23 20
24#if ENABLE_FEATURE_INIT_COREDUMPS
25/* 21/*
26 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called 22 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
27 * before processes are spawned to set core file size as unlimited. 23 * before processes are spawned to set core file size as unlimited.
@@ -30,7 +26,6 @@
30 */ 26 */
31#define CORE_ENABLE_FLAG_FILE "/.init_enable_core" 27#define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
32#include <sys/resource.h> 28#include <sys/resource.h>
33#endif
34 29
35#define INITTAB "/etc/inittab" /* inittab file location */ 30#define INITTAB "/etc/inittab" /* inittab file location */
36#ifndef INIT_SCRIPT 31#ifndef INIT_SCRIPT
@@ -68,12 +63,8 @@ struct init_action {
68/* Static variables */ 63/* Static variables */
69static struct init_action *init_action_list = NULL; 64static struct init_action *init_action_list = NULL;
70 65
71#if !ENABLE_FEATURE_INIT_SYSLOG
72static const char *log_console = VC_5; 66static const char *log_console = VC_5;
73#endif
74#if !ENABLE_DEBUG_INIT
75static sig_atomic_t got_cont = 0; 67static sig_atomic_t got_cont = 0;
76#endif
77 68
78enum { 69enum {
79 L_LOG = 0x1, 70 L_LOG = 0x1,
@@ -105,33 +96,22 @@ static const char *const environment[] = {
105/* Function prototypes */ 96/* Function prototypes */
106static void delete_init_action(struct init_action *a); 97static void delete_init_action(struct init_action *a);
107static int waitfor(pid_t pid); 98static int waitfor(pid_t pid);
108#if !ENABLE_DEBUG_INIT
109static void shutdown_signal(int sig); 99static void shutdown_signal(int sig);
110#endif
111 100
112#if !ENABLE_DEBUG_INIT
113static void loop_forever(void) 101static void loop_forever(void)
114{ 102{
115 while (1) 103 while (1)
116 sleep(1); 104 sleep(1);
117} 105}
118#endif
119 106
120/* Print a message to the specified device. 107/* Print a message to the specified device.
121 * Device may be bitwise-or'd from L_LOG | L_CONSOLE */ 108 * Device may be bitwise-or'd from L_LOG | L_CONSOLE */
122#if ENABLE_DEBUG_INIT 109#define messageD(...) do { if (ENABLE_DEBUG_INIT) message(__VA_ARGS__); } while (0)
123#define messageD message
124#else
125#define messageD(...) do {} while (0)
126#endif
127static void message(int device, const char *fmt, ...) 110static void message(int device, const char *fmt, ...)
128 __attribute__ ((format(printf, 2, 3))); 111 __attribute__ ((format(printf, 2, 3)));
129static void message(int device, const char *fmt, ...) 112static void message(int device, const char *fmt, ...)
130{ 113{
131#if !ENABLE_FEATURE_INIT_SYSLOG
132 static int log_fd = -1; 114 static int log_fd = -1;
133#endif
134
135 va_list arguments; 115 va_list arguments;
136 int l; 116 int l;
137 char msg[128]; 117 char msg[128];
@@ -143,40 +123,40 @@ static void message(int device, const char *fmt, ...)
143 msg[sizeof(msg) - 2] = '\0'; 123 msg[sizeof(msg) - 2] = '\0';
144 l = strlen(msg); 124 l = strlen(msg);
145 125
146#if ENABLE_FEATURE_INIT_SYSLOG 126 if (ENABLE_FEATURE_INIT_SYSLOG) {
147 /* Log the message to syslogd */ 127 /* Log the message to syslogd */
148 if (device & L_LOG) { 128 if (device & L_LOG) {
149 /* don't out "\r" */ 129 /* don't out "\r" */
150 openlog(applet_name, 0, LOG_DAEMON); 130 openlog(applet_name, 0, LOG_DAEMON);
151 syslog(LOG_INFO, "init: %s", msg + 1); 131 syslog(LOG_INFO, "init: %s", msg + 1);
152 closelog(); 132 closelog();
153 } 133 }
154 msg[l++] = '\n'; 134 msg[l++] = '\n';
155 msg[l] = '\0'; 135 msg[l] = '\0';
156#else 136 } else {
157 msg[l++] = '\n'; 137 msg[l++] = '\n';
158 msg[l] = '\0'; 138 msg[l] = '\0';
159 /* Take full control of the log tty, and never close it. 139 /* Take full control of the log tty, and never close it.
160 * It's mine, all mine! Muhahahaha! */ 140 * It's mine, all mine! Muhahahaha! */
161 if (log_fd < 0) { 141 if (log_fd < 0) {
162 if (!log_console) { 142 if (!log_console) {
163 log_fd = 2; 143 log_fd = 2;
164 } else {
165 log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
166 if (log_fd < 0) {
167 bb_error_msg("can't log to %s", log_console);
168 device = L_CONSOLE;
169 } else { 144 } else {
170 close_on_exec_on(log_fd); 145 log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
146 if (log_fd < 0) {
147 bb_error_msg("can't log to %s", log_console);
148 device = L_CONSOLE;
149 } else {
150 close_on_exec_on(log_fd);
151 }
171 } 152 }
172 } 153 }
154 if (device & L_LOG) {
155 full_write(log_fd, msg, l);
156 if (log_fd == 2)
157 return; /* don't print dup messages */
158 }
173 } 159 }
174 if (device & L_LOG) {
175 full_write(log_fd, msg, l);
176 if (log_fd == 2)
177 return; /* don't print dup messages */
178 }
179#endif
180 160
181 if (device & L_CONSOLE) { 161 if (device & L_CONSOLE) {
182 /* Send console messages to console so people will see them. */ 162 /* Send console messages to console so people will see them. */
@@ -235,9 +215,8 @@ static void console_init(void)
235 * if TERM is set to linux (the default) */ 215 * if TERM is set to linux (the default) */
236 if (!s || strcmp(s, "linux") == 0) 216 if (!s || strcmp(s, "linux") == 0)
237 putenv((char*)"TERM=vt102"); 217 putenv((char*)"TERM=vt102");
238#if !ENABLE_FEATURE_INIT_SYSLOG 218 if (!ENABLE_FEATURE_INIT_SYSLOG)
239 log_console = NULL; 219 log_console = NULL;
240#endif
241 } else if (!s) 220 } else if (!s)
242 putenv((char*)"TERM=linux"); 221 putenv((char*)"TERM=linux");
243} 222}
@@ -290,11 +269,10 @@ static void open_stdio_to_tty(const char* tty_name, int fail)
290 tty_name, strerror(errno)); 269 tty_name, strerror(errno));
291 if (fail) 270 if (fail)
292 _exit(1); 271 _exit(1);
293#if !ENABLE_DEBUG_INIT 272 if (!ENABLE_DEBUG_INIT)
294 shutdown_signal(SIGUSR1); 273 shutdown_signal(SIGUSR1);
295#else 274 else
296 _exit(2); 275 _exit(2);
297#endif
298 } else { 276 } else {
299 dup2(fd, 0); 277 dup2(fd, 0);
300 dup2(fd, 1); 278 dup2(fd, 1);
@@ -348,9 +326,13 @@ static pid_t run(const struct init_action *a)
348 open_stdio_to_tty(a->terminal, 1 /* - exit if open fails*/); 326 open_stdio_to_tty(a->terminal, 1 /* - exit if open fails*/);
349 327
350#ifdef BUT_RUN_ACTIONS_ALREADY_DOES_WAITING 328#ifdef BUT_RUN_ACTIONS_ALREADY_DOES_WAITING
329# define BRAADS 1
330#else
331# define BRAADS 0
332#endif
351 /* If the init Action requires us to wait, then force the 333 /* If the init Action requires us to wait, then force the
352 * supplied terminal to be the controlling tty. */ 334 * supplied terminal to be the controlling tty. */
353 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) { 335 if (BRAADS && a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
354 336
355 /* Now fork off another process to just hang around */ 337 /* Now fork off another process to just hang around */
356 pid = fork(); 338 pid = fork();
@@ -388,7 +370,6 @@ static pid_t run(const struct init_action *a)
388 370
389 /* Child - fall though to actually execute things */ 371 /* Child - fall though to actually execute things */
390 } 372 }
391#endif
392 373
393 /* See if any special /bin/sh requiring characters are present */ 374 /* See if any special /bin/sh requiring characters are present */
394 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) { 375 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
@@ -422,25 +403,29 @@ static pid_t run(const struct init_action *a)
422 ++cmdpath; 403 ++cmdpath;
423 404
424#ifdef WHY_WE_DO_THIS_SHELL_MUST_HANDLE_THIS_ITSELF 405#ifdef WHY_WE_DO_THIS_SHELL_MUST_HANDLE_THIS_ITSELF
425 /* find the last component in the command pathname */ 406# define WWDTSMHTI 1
426 s = bb_get_last_path_component_nostrip(cmdpath); 407#else
427 /* make a new argv[0] */ 408# define WWDTSMHTI 0
428 cmd[0] = malloc(strlen(s) + 2);
429 if (cmd[0] == NULL) {
430 message(L_LOG | L_CONSOLE, bb_msg_memory_exhausted);
431 cmd[0] = cmdpath;
432 } else {
433 cmd[0][0] = '-';
434 strcpy(cmd[0] + 1, s);
435 }
436#endif 409#endif
410 if (WWDTSMHTI) {
411 /* find the last component in the command pathname */
412 s = bb_get_last_path_component_nostrip(cmdpath);
413 /* make a new argv[0] */
414 cmd[0] = malloc(strlen(s) + 2);
415 if (cmd[0] == NULL) {
416 message(L_LOG | L_CONSOLE, bb_msg_memory_exhausted);
417 cmd[0] = cmdpath;
418 } else {
419 cmd[0][0] = '-';
420 strcpy(cmd[0] + 1, s);
421 }
422 }
437 423
438#if ENABLE_FEATURE_INIT_SCTTY
439 /* Establish this process as session leader and 424 /* Establish this process as session leader and
440 * _attempt_ to make stdin a controlling tty. 425 * _attempt_ to make stdin a controlling tty.
441 */ 426 */
442 ioctl(0, TIOCSCTTY, 0 /*only try, don't steal*/); 427 if (ENABLE_FEATURE_INIT_SCTTY)
443#endif 428 ioctl(0, TIOCSCTTY, 0 /*only try, don't steal*/);
444 } 429 }
445 430
446 if (a->action & ASKFIRST) { 431 if (a->action & ASKFIRST) {
@@ -470,8 +455,7 @@ static pid_t run(const struct init_action *a)
470 message(L_LOG, "starting pid %d, tty '%s': '%s'", 455 message(L_LOG, "starting pid %d, tty '%s': '%s'",
471 getpid(), a->terminal, cmdpath); 456 getpid(), a->terminal, cmdpath);
472 457
473#if ENABLE_FEATURE_INIT_COREDUMPS 458 if (ENABLE_FEATURE_INIT_COREDUMPS) {
474 {
475 struct stat sb; 459 struct stat sb;
476 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) { 460 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
477 struct rlimit limit; 461 struct rlimit limit;
@@ -481,7 +465,7 @@ static pid_t run(const struct init_action *a)
481 setrlimit(RLIMIT_CORE, &limit); 465 setrlimit(RLIMIT_CORE, &limit);
482 } 466 }
483 } 467 }
484#endif 468
485 /* Now run it. The new program will take over this PID, 469 /* Now run it. The new program will take over this PID,
486 * so nothing further in init.c should be run. */ 470 * so nothing further in init.c should be run. */
487 BB_EXECVP(cmdpath, cmd); 471 BB_EXECVP(cmdpath, cmd);
@@ -536,7 +520,6 @@ static void run_actions(int action)
536 } 520 }
537} 521}
538 522
539#if !ENABLE_DEBUG_INIT
540static void init_reboot(unsigned long magic) 523static void init_reboot(unsigned long magic)
541{ 524{
542 pid_t pid; 525 pid_t pid;
@@ -676,8 +659,6 @@ static void cont_handler(int sig ATTRIBUTE_UNUSED)
676 got_cont = 1; 659 got_cont = 1;
677} 660}
678 661
679#endif /* !ENABLE_DEBUG_INIT */
680
681static void new_init_action(uint8_t action, const char *command, const char *cons) 662static void new_init_action(uint8_t action, const char *command, const char *cons)
682{ 663{
683 struct init_action *new_action, *a, *last; 664 struct init_action *new_action, *a, *last;
@@ -737,14 +718,16 @@ static void delete_init_action(struct init_action *action)
737 */ 718 */
738static void parse_inittab(void) 719static void parse_inittab(void)
739{ 720{
740#if ENABLE_FEATURE_USE_INITTAB
741 FILE *file; 721 FILE *file;
742 char buf[INIT_BUFFS_SIZE]; 722 char buf[INIT_BUFFS_SIZE];
743 723
744 file = fopen(INITTAB, "r"); 724 if (ENABLE_FEATURE_USE_INITTAB)
725 file = fopen(INITTAB, "r");
726 else
727 file = NULL;
728
729 /* No inittab file -- set up some default behavior */
745 if (file == NULL) { 730 if (file == NULL) {
746 /* No inittab file -- set up some default behavior */
747#endif
748 /* Reboot on Ctrl-Alt-Del */ 731 /* Reboot on Ctrl-Alt-Del */
749 new_init_action(CTRLALTDEL, "reboot", ""); 732 new_init_action(CTRLALTDEL, "reboot", "");
750 /* Umount all filesystems on halt/reboot */ 733 /* Umount all filesystems on halt/reboot */
@@ -763,7 +746,6 @@ static void parse_inittab(void)
763 new_init_action(SYSINIT, INIT_SCRIPT, ""); 746 new_init_action(SYSINIT, INIT_SCRIPT, "");
764 747
765 return; 748 return;
766#if ENABLE_FEATURE_USE_INITTAB
767 } 749 }
768 750
769 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) { 751 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
@@ -823,10 +805,8 @@ static void parse_inittab(void)
823 next_line: ; 805 next_line: ;
824 } 806 }
825 fclose(file); 807 fclose(file);
826#endif /* FEATURE_USE_INITTAB */
827} 808}
828 809
829#if ENABLE_FEATURE_USE_INITTAB
830static void reload_signal(int sig ATTRIBUTE_UNUSED) 810static void reload_signal(int sig ATTRIBUTE_UNUSED)
831{ 811{
832 struct init_action *a, *tmp; 812 struct init_action *a, *tmp;
@@ -840,22 +820,21 @@ static void reload_signal(int sig ATTRIBUTE_UNUSED)
840 820
841 parse_inittab(); 821 parse_inittab();
842 822
843#if ENABLE_FEATURE_KILL_REMOVED 823 if (ENABLE_FEATURE_KILL_REMOVED) {
844 for (a = init_action_list; a; a = a->next) { 824 for (a = init_action_list; a; a = a->next) {
845 pid_t pid = a->pid; 825 pid_t pid = a->pid;
846 if ((a->action & ONCE) && pid != 0) { 826 if ((a->action & ONCE) && pid != 0) {
847 /* Be nice and send SIGTERM first */ 827 /* Be nice and send SIGTERM first */
848 kill(pid, SIGTERM); 828 kill(pid, SIGTERM);
849#if CONFIG_FEATURE_KILL_DELAY 829 if (CONFIG_FEATURE_KILL_DELAY)
850 if (fork() == 0) { /* child */ 830 if (fork() == 0) { /* child */
851 sleep(CONFIG_FEATURE_KILL_DELAY); 831 sleep(CONFIG_FEATURE_KILL_DELAY);
852 kill(pid, SIGKILL); 832 kill(pid, SIGKILL);
853 _exit(0); 833 _exit(0);
834 }
854 } 835 }
855#endif
856 } 836 }
857 } 837 }
858#endif /* FEATURE_KILL_REMOVED */
859 838
860 /* remove unused entrys */ 839 /* remove unused entrys */
861 for (a = init_action_list; a; a = tmp) { 840 for (a = init_action_list; a; a = tmp) {
@@ -866,7 +845,6 @@ static void reload_signal(int sig ATTRIBUTE_UNUSED)
866 } 845 }
867 run_actions(RESPAWN); 846 run_actions(RESPAWN);
868} 847}
869#endif /* FEATURE_USE_INITTAB */
870 848
871int init_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 849int init_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
872int init_main(int argc, char **argv) 850int init_main(int argc, char **argv)
@@ -879,30 +857,30 @@ int init_main(int argc, char **argv)
879 if (argc > 1 && !strcmp(argv[1], "-q")) { 857 if (argc > 1 && !strcmp(argv[1], "-q")) {
880 return kill(1, SIGHUP); 858 return kill(1, SIGHUP);
881 } 859 }
882#if !ENABLE_DEBUG_INIT
883 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
884 if (getpid() != 1
885 && (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc"))
886 ) {
887 bb_show_usage();
888 }
889 /* Set up sig handlers -- be sure to
890 * clear all of these in run() */
891 signal(SIGHUP, exec_signal);
892 signal(SIGQUIT, exec_signal);
893 signal(SIGUSR1, shutdown_signal);
894 signal(SIGUSR2, shutdown_signal);
895 signal(SIGINT, ctrlaltdel_signal);
896 signal(SIGTERM, shutdown_signal);
897 signal(SIGCONT, cont_handler);
898 signal(SIGSTOP, stop_handler);
899 signal(SIGTSTP, stop_handler);
900
901 /* Turn off rebooting via CTL-ALT-DEL -- we get a
902 * SIGINT on CAD so we can shut things down gracefully... */
903 init_reboot(RB_DISABLE_CAD);
904#endif
905 860
861 if (!ENABLE_DEBUG_INIT) {
862 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
863 if (getpid() != 1
864 && (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc"))
865 ) {
866 bb_show_usage();
867 }
868 /* Set up sig handlers -- be sure to
869 * clear all of these in run() */
870 signal(SIGHUP, exec_signal);
871 signal(SIGQUIT, exec_signal);
872 signal(SIGUSR1, shutdown_signal);
873 signal(SIGUSR2, shutdown_signal);
874 signal(SIGINT, ctrlaltdel_signal);
875 signal(SIGTERM, shutdown_signal);
876 signal(SIGCONT, cont_handler);
877 signal(SIGSTOP, stop_handler);
878 signal(SIGTSTP, stop_handler);
879
880 /* Turn off rebooting via CTL-ALT-DEL -- we get a
881 * SIGINT on CAD so we can shut things down gracefully... */
882 init_reboot(RB_DISABLE_CAD);
883 }
906 884
907 /* Figure out where the default console should be */ 885 /* Figure out where the default console should be */
908 console_init(); 886 console_init();
@@ -986,12 +964,11 @@ int init_main(int argc, char **argv)
986 /* Next run anything to be run only once */ 964 /* Next run anything to be run only once */
987 run_actions(ONCE); 965 run_actions(ONCE);
988 966
989#if ENABLE_FEATURE_USE_INITTAB
990 /* Redefine SIGHUP to reread /etc/inittab */ 967 /* Redefine SIGHUP to reread /etc/inittab */
991 signal(SIGHUP, reload_signal); 968 if (ENABLE_FEATURE_USE_INITTAB)
992#else 969 signal(SIGHUP, reload_signal);
993 signal(SIGHUP, SIG_IGN); 970 else
994#endif /* FEATURE_USE_INITTAB */ 971 signal(SIGHUP, SIG_IGN);
995 972
996 /* Now run the looping stuff for the rest of forever */ 973 /* Now run the looping stuff for the rest of forever */
997 while (1) { 974 while (1) {