diff options
Diffstat (limited to 'init.c')
-rw-r--r-- | init.c | 84 |
1 files changed, 51 insertions, 33 deletions
@@ -41,6 +41,9 @@ | |||
41 | #include <sys/vt.h> /* for vt_stat */ | 41 | #include <sys/vt.h> /* for vt_stat */ |
42 | #include <sys/ioctl.h> | 42 | #include <sys/ioctl.h> |
43 | 43 | ||
44 | /* Turn this on to debug init so it won't reboot when killed */ | ||
45 | #define DEBUG_INIT | ||
46 | |||
44 | #define CONSOLE "/dev/console" /* Logical system console */ | 47 | #define CONSOLE "/dev/console" /* Logical system console */ |
45 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ | 48 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ |
46 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ | 49 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ |
@@ -61,7 +64,7 @@ int device_open(char *device, int mode) | |||
61 | { | 64 | { |
62 | int m, f, fd = -1; | 65 | int m, f, fd = -1; |
63 | 66 | ||
64 | mode = m | O_NONBLOCK; | 67 | m = mode | O_NONBLOCK; |
65 | 68 | ||
66 | /* Retry up to 5 times */ | 69 | /* Retry up to 5 times */ |
67 | for (f = 0; f < 5; f++) | 70 | for (f = 0; f < 5; f++) |
@@ -99,25 +102,28 @@ void message(char *device, char *fmt, ...) | |||
99 | void set_term( int fd) | 102 | void set_term( int fd) |
100 | { | 103 | { |
101 | struct termios tty; | 104 | struct termios tty; |
105 | static const char control_characters[] = { | ||
106 | '\003', '\034', '\177', '\025', '\004', '\0', | ||
107 | '\1', '\0', '\021', '\023', '\032', '\0', '\022', | ||
108 | '\017', '\027', '\026', '\0' | ||
109 | }; | ||
102 | 110 | ||
103 | tcgetattr(fd, &tty); | 111 | tcgetattr(fd, &tty); |
104 | tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD; | 112 | |
105 | tty.c_cflag |= HUPCL|CLOCAL; | 113 | /* input modes */ |
106 | 114 | tty.c_iflag = IGNPAR|ICRNL|IXON|IXOFF|IXANY; | |
107 | tty.c_cc[VINTR] = 3; | 115 | |
108 | tty.c_cc[VQUIT] = 28; | 116 | /* use lineo dicipline 0 */ |
109 | tty.c_cc[VERASE] = 127; | 117 | tty.c_line = 0; |
110 | tty.c_cc[VKILL] = 24; | 118 | |
111 | tty.c_cc[VEOF] = 4; | 119 | /* output modes */ |
112 | tty.c_cc[VTIME] = 0; | ||
113 | tty.c_cc[VMIN] = 1; | ||
114 | tty.c_cc[VSTART] = 17; | ||
115 | tty.c_cc[VSTOP] = 19; | ||
116 | tty.c_cc[VSUSP] = 26; | ||
117 | |||
118 | tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY; | ||
119 | tty.c_oflag = OPOST|ONLCR; | 120 | tty.c_oflag = OPOST|ONLCR; |
120 | tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE; | 121 | |
122 | /* local modes */ | ||
123 | tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOPRT|ECHOKE|IEXTEN; | ||
124 | |||
125 | /* control chars */ | ||
126 | memcpy(tty.c_cc, control_characters, sizeof(control_characters)); | ||
121 | 127 | ||
122 | tcsetattr(fd, TCSANOW, &tty); | 128 | tcsetattr(fd, TCSANOW, &tty); |
123 | } | 129 | } |
@@ -151,7 +157,7 @@ static void set_free_pages() | |||
151 | fclose(f); | 157 | fclose(f); |
152 | f = fopen("/proc/sys/vm/freepages", "w"); | 158 | f = fopen("/proc/sys/vm/freepages", "w"); |
153 | fprintf(f, "30\t40\t50\n"); | 159 | fprintf(f, "30\t40\t50\n"); |
154 | printf("\nIncreased /proc/sys/vm/freepages values to 30/40/50\n"); | 160 | message(log, "\nIncreased /proc/sys/vm/freepages values to 30/40/50\n"); |
155 | } | 161 | } |
156 | fclose(f); | 162 | fclose(f); |
157 | } | 163 | } |
@@ -165,7 +171,6 @@ static void console_init() | |||
165 | int tried_vtmaster = 0; | 171 | int tried_vtmaster = 0; |
166 | char *s; | 172 | char *s; |
167 | 173 | ||
168 | fprintf(stderr, "entering console_init()\n"); | ||
169 | if ((s = getenv("CONSOLE")) != NULL) | 174 | if ((s = getenv("CONSOLE")) != NULL) |
170 | console = s; | 175 | console = s; |
171 | else { | 176 | else { |
@@ -175,6 +180,7 @@ static void console_init() | |||
175 | 180 | ||
176 | if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) { | 181 | if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) { |
177 | fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n"); | 182 | fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n"); |
183 | message(log, "Yikes! /dev/console does not exist or is a symlink.\n"); | ||
178 | } | 184 | } |
179 | while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { | 185 | while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { |
180 | if (!tried_devcons) { | 186 | if (!tried_devcons) { |
@@ -193,7 +199,7 @@ static void console_init() | |||
193 | console = "/dev/null"; | 199 | console = "/dev/null"; |
194 | else | 200 | else |
195 | close(fd); | 201 | close(fd); |
196 | fprintf(stderr, "console=%s\n", console); | 202 | message(log, "console=%s\n", console); |
197 | } | 203 | } |
198 | 204 | ||
199 | static int waitfor(int pid) | 205 | static int waitfor(int pid) |
@@ -212,7 +218,7 @@ static int waitfor(int pid) | |||
212 | static pid_t run(const char * const* command, | 218 | static pid_t run(const char * const* command, |
213 | char *terminal, int get_enter) | 219 | char *terminal, int get_enter) |
214 | { | 220 | { |
215 | int i; | 221 | int i, f; |
216 | pid_t pid; | 222 | pid_t pid; |
217 | static const char press_enter[] = | 223 | static const char press_enter[] = |
218 | "\nPlease press Enter to activate this console. "; | 224 | "\nPlease press Enter to activate this console. "; |
@@ -224,9 +230,9 @@ static pid_t run(const char * const* command, | |||
224 | close(2); | 230 | close(2); |
225 | setsid(); | 231 | setsid(); |
226 | 232 | ||
227 | open(terminal, O_RDWR); | 233 | f=open(terminal, O_RDWR); |
228 | dup(0); | 234 | dup(f); |
229 | dup(0); | 235 | dup(f); |
230 | tcsetpgrp(0, getpgrp()); | 236 | tcsetpgrp(0, getpgrp()); |
231 | set_term(0); | 237 | set_term(0); |
232 | 238 | ||
@@ -240,7 +246,7 @@ static pid_t run(const char * const* command, | |||
240 | * specifies. | 246 | * specifies. |
241 | */ | 247 | */ |
242 | char c; | 248 | char c; |
243 | write(0, press_enter, sizeof(press_enter) - 1); | 249 | write(1, press_enter, sizeof(press_enter) - 1); |
244 | read(0, &c, 1); | 250 | read(0, &c, 1); |
245 | } | 251 | } |
246 | 252 | ||
@@ -269,7 +275,7 @@ static pid_t run(const char * const* command, | |||
269 | return pid; | 275 | return pid; |
270 | } | 276 | } |
271 | 277 | ||
272 | 278 | #ifndef DEBUG_INIT | |
273 | static void shutdown_system(void) | 279 | static void shutdown_system(void) |
274 | { | 280 | { |
275 | const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0}; | 281 | const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0}; |
@@ -314,6 +320,7 @@ static void reboot_signal(int sig) | |||
314 | reboot(RB_AUTOBOOT); | 320 | reboot(RB_AUTOBOOT); |
315 | exit(0); | 321 | exit(0); |
316 | } | 322 | } |
323 | #endif | ||
317 | 324 | ||
318 | extern int init_main(int argc, char **argv) | 325 | extern int init_main(int argc, char **argv) |
319 | { | 326 | { |
@@ -326,11 +333,17 @@ extern int init_main(int argc, char **argv) | |||
326 | const char* const shell_commands[] = { SHELL, " -", 0}; | 333 | const char* const shell_commands[] = { SHELL, " -", 0}; |
327 | const char* const* tty0_commands = init_commands; | 334 | const char* const* tty0_commands = init_commands; |
328 | const char* const* tty1_commands = shell_commands; | 335 | const char* const* tty1_commands = shell_commands; |
336 | #ifndef DEBUG_INIT | ||
337 | char *hello_msg_format = | ||
338 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n"; | ||
339 | #else | ||
329 | char *hello_msg_format = | 340 | char *hello_msg_format = |
330 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; | 341 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; |
342 | #endif | ||
331 | const char *no_memory = | 343 | const char *no_memory = |
332 | "Sorry, your computer does not have enough memory.\r\n"; | 344 | "Sorry, your computer does not have enough memory.\r\n"; |
333 | 345 | ||
346 | #ifndef DEBUG_INIT | ||
334 | /* Set up sig handlers */ | 347 | /* Set up sig handlers */ |
335 | signal(SIGUSR1, halt_signal); | 348 | signal(SIGUSR1, halt_signal); |
336 | signal(SIGSEGV, halt_signal); | 349 | signal(SIGSEGV, halt_signal); |
@@ -344,7 +357,7 @@ extern int init_main(int argc, char **argv) | |||
344 | /* Turn off rebooting via CTL-ALT-DEL -- we get a | 357 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
345 | * SIGINT on CAD so we can shut things down gracefully... */ | 358 | * SIGINT on CAD so we can shut things down gracefully... */ |
346 | reboot(RB_DISABLE_CAD); | 359 | reboot(RB_DISABLE_CAD); |
347 | 360 | #endif | |
348 | /* Figure out where the default console should be */ | 361 | /* Figure out where the default console should be */ |
349 | console_init(); | 362 | console_init(); |
350 | 363 | ||
@@ -352,7 +365,7 @@ extern int init_main(int argc, char **argv) | |||
352 | close(0); | 365 | close(0); |
353 | close(1); | 366 | close(1); |
354 | close(2); | 367 | close(2); |
355 | //set_term(0); | 368 | set_term(0); |
356 | setsid(); | 369 | setsid(); |
357 | 370 | ||
358 | /* Make sure PATH is set to something sane */ | 371 | /* Make sure PATH is set to something sane */ |
@@ -360,17 +373,22 @@ extern int init_main(int argc, char **argv) | |||
360 | putenv(PATH_DEFAULT); | 373 | putenv(PATH_DEFAULT); |
361 | 374 | ||
362 | /* Hello world */ | 375 | /* Hello world */ |
376 | #ifndef DEBUG_INIT | ||
363 | message(log, hello_msg_format, BB_VER, BB_BT); | 377 | message(log, hello_msg_format, BB_VER, BB_BT); |
364 | fprintf(stderr, hello_msg_format, BB_VER, BB_BT); | 378 | message(console, hello_msg_format, BB_VER, BB_BT); |
379 | #else | ||
380 | message(log, hello_msg_format, getpid(), BB_VER, BB_BT); | ||
381 | message(console, hello_msg_format, getpid(), BB_VER, BB_BT); | ||
382 | #endif | ||
365 | 383 | ||
366 | 384 | ||
367 | /* Mount /proc */ | 385 | /* Mount /proc */ |
368 | if (mount("/proc", "/proc", "proc", 0, 0) == 0) { | 386 | if (mount("/proc", "/proc", "proc", 0, 0) == 0) { |
369 | fprintf(stderr, "Mounting /proc: done.\n"); | ||
370 | message(log, "Mounting /proc: done.\n"); | 387 | message(log, "Mounting /proc: done.\n"); |
388 | message(console, "Mounting /proc: done.\n"); | ||
371 | } else { | 389 | } else { |
372 | fprintf(stderr, "Mounting /proc: failed!\n"); | ||
373 | message(log, "Mounting /proc: failed!\n"); | 390 | message(log, "Mounting /proc: failed!\n"); |
391 | message(console, "Mounting /proc: failed!\n"); | ||
374 | } | 392 | } |
375 | 393 | ||
376 | /* Make sure there is enough memory to do something useful */ | 394 | /* Make sure there is enough memory to do something useful */ |
@@ -379,7 +397,7 @@ extern int init_main(int argc, char **argv) | |||
379 | int retval; | 397 | int retval; |
380 | retval = stat("/etc/fstab", &statbuf); | 398 | retval = stat("/etc/fstab", &statbuf); |
381 | if (retval) { | 399 | if (retval) { |
382 | fprintf(stderr, "%s", no_memory); | 400 | message(console, "%s", no_memory); |
383 | while (1) { | 401 | while (1) { |
384 | sleep(1); | 402 | sleep(1); |
385 | } | 403 | } |
@@ -387,7 +405,7 @@ extern int init_main(int argc, char **argv) | |||
387 | /* Try to turn on swap */ | 405 | /* Try to turn on swap */ |
388 | waitfor(run(swap_on_cmd, console, 0)); | 406 | waitfor(run(swap_on_cmd, console, 0)); |
389 | if (mem_total() < 2000) { | 407 | if (mem_total() < 2000) { |
390 | fprintf(stderr, "%s", no_memory); | 408 | message(console, "%s", no_memory); |
391 | while (1) { | 409 | while (1) { |
392 | sleep(1); | 410 | sleep(1); |
393 | } | 411 | } |