aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>1999-10-28 00:24:35 +0000
committerEric Andersen <andersen@codepoet.org>1999-10-28 00:24:35 +0000
commitc7c41d306b8e172a2fba432d3c4b9f97b9963816 (patch)
treee603e6812ac8032d84da87b4c427b375b7445268
parenta745606df39d10664080c857dea6fd3583a8743a (diff)
downloadbusybox-w32-c7c41d306b8e172a2fba432d3c4b9f97b9963816.tar.gz
busybox-w32-c7c41d306b8e172a2fba432d3c4b9f97b9963816.tar.bz2
busybox-w32-c7c41d306b8e172a2fba432d3c4b9f97b9963816.zip
Cleanup of init
-rw-r--r--init.c169
-rw-r--r--init/init.c169
2 files changed, 126 insertions, 212 deletions
diff --git a/init.c b/init.c
index 83fc9519a..d950e2bef 100644
--- a/init.c
+++ b/init.c
@@ -52,7 +52,7 @@
52#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" 52#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
53 53
54static char *console = CONSOLE; 54static char *console = CONSOLE;
55static char *first_terminal = "/dev/tty1"; 55//static char *first_terminal = "/dev/tty1";
56static char *second_terminal = "/dev/tty2"; 56static char *second_terminal = "/dev/tty2";
57static char *log = "/dev/tty3"; 57static char *log = "/dev/tty3";
58 58
@@ -83,7 +83,7 @@ void message(char *device, char *fmt, ...)
83 int fd; 83 int fd;
84 va_list arguments; 84 va_list arguments;
85 85
86 if ((fd = device_open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0) { 86 if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
87 va_start(arguments, fmt); 87 va_start(arguments, fmt);
88 vdprintf(fd, fmt, arguments); 88 vdprintf(fd, fmt, arguments);
89 va_end(arguments); 89 va_end(arguments);
@@ -98,18 +98,13 @@ void message(char *device, char *fmt, ...)
98} 98}
99 99
100/* Set terminal settings to reasonable defaults */ 100/* Set terminal settings to reasonable defaults */
101void set_term() 101void set_term( int fd)
102{ 102{
103 int fd;
104 struct termios tty; 103 struct termios tty;
105 104
106 if ((fd = device_open(console, O_RDWR | O_NOCTTY)) < 0) { 105 tcgetattr(fd, &tty);
107 message(log, "can't open %s\n", console); 106 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
108 return; 107 tty.c_cflag |= HUPCL|CLOCAL;
109 }
110 ioctl(fd, TCGETS, &tty);
111 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
112 tty.c_cflag |= HUPCL | CLOCAL;
113 108
114 tty.c_cc[VINTR] = 3; 109 tty.c_cc[VINTR] = 3;
115 tty.c_cc[VQUIT] = 28; 110 tty.c_cc[VQUIT] = 28;
@@ -122,14 +117,11 @@ void set_term()
122 tty.c_cc[VSTOP] = 19; 117 tty.c_cc[VSTOP] = 19;
123 tty.c_cc[VSUSP] = 26; 118 tty.c_cc[VSUSP] = 26;
124 119
125 /* Set pre and post processing */ 120 tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY;
126 tty.c_iflag = IGNPAR | ICRNL | IXON | IXANY; 121 tty.c_oflag = OPOST|ONLCR;
127 tty.c_oflag = OPOST | ONLCR; 122 tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE;
128 tty.c_lflag = ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE;
129 123
130 /* Now set the terminal line. */ 124 tcsetattr(fd, TCSANOW, &tty);
131 ioctl(fd, TCSETS, &tty);
132 close(fd);
133} 125}
134 126
135static int mem_total() 127static int mem_total()
@@ -175,6 +167,7 @@ static void console_init()
175 int tried_vtmaster = 0; 167 int tried_vtmaster = 0;
176 char *s; 168 char *s;
177 169
170 fprintf(stderr, "entering console_init()\n");
178 if ((s = getenv("CONSOLE")) != NULL) 171 if ((s = getenv("CONSOLE")) != NULL)
179 console = s; 172 console = s;
180 else { 173 else {
@@ -183,7 +176,7 @@ static void console_init()
183 } 176 }
184 177
185 if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) { 178 if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) {
186 fprintf(stderr, "/dev/console does not exist, or is a symlink.\n"); 179 fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n");
187 } 180 }
188 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { 181 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
189 if (!tried_devcons) { 182 if (!tried_devcons) {
@@ -218,42 +211,14 @@ static int waitfor(int pid)
218} 211}
219 212
220 213
221static int run(const char *command, char *terminal, int get_enter) 214static pid_t run(const char * const* command,
215 char *terminal, int get_enter)
222{ 216{
223 int f, pid; 217 int i;
224 char *args[16]; 218 pid_t pid;
225 char buf[256];
226 char *ptr;
227 static const char press_enter[] = 219 static const char press_enter[] =
228 "\nPlease press Enter to activate this console. "; 220 "\nPlease press Enter to activate this console. ";
229 221
230
231 /* Make a proper command from the command string */
232 strcpy(buf, command);
233 ptr = buf;
234 for (f = 1; f < 15; f++) {
235 /* Skip white space */
236 while (*ptr == ' ' || *ptr == '\t')
237 ptr++;
238 args[f] = ptr;
239 /* May be trailing space.. */
240 if (*ptr == 0)
241 break;
242 /* Skip this `word' */
243 while (*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '#')
244 ptr++;
245 /* If end-of-line, break */
246 if (*ptr == '#' || *ptr == 0) {
247 f++;
248 *ptr = 0;
249 break;
250 }
251 /* End word with \0 and continue */
252 args[f] = NULL;
253 }
254 args[0] = args[1];
255
256
257 if ((pid = fork()) == 0) { 222 if ((pid = fork()) == 0) {
258 /* Clean up */ 223 /* Clean up */
259 close(0); 224 close(0);
@@ -261,22 +226,11 @@ static int run(const char *command, char *terminal, int get_enter)
261 close(2); 226 close(2);
262 setsid(); 227 setsid();
263 228
264#if 1
265 //if ((f = device_open(terminal, O_RDWR | O_NOCTTY)) < 0) {
266 if ((f = device_open(terminal, O_RDWR )) < 0) {
267 message(log, "open(%s) failed: %s\n",
268 terminal, strerror(errno));
269 return -1;
270 }
271 dup(f);
272 dup(f);
273#else
274 open(terminal, O_RDWR); 229 open(terminal, O_RDWR);
275 dup(0); 230 dup(0);
276 dup(0); 231 dup(0);
277 //tcsetpgrp(0, getpgrp()); 232 tcsetpgrp(0, getpgrp());
278#endif 233 set_term(0);
279 set_term();
280 234
281 if (get_enter) { 235 if (get_enter) {
282 /* 236 /*
@@ -288,32 +242,30 @@ static int run(const char *command, char *terminal, int get_enter)
288 * specifies. 242 * specifies.
289 */ 243 */
290 char c; 244 char c;
291 write(1, press_enter, sizeof(press_enter) - 1); 245 write(0, press_enter, sizeof(press_enter) - 1);
292 read(0, &c, 1); 246 read(0, &c, 1);
293 message(console, "Got an enter\r\n");
294 } 247 }
295 248
296 /* Log the process name and args */ 249 /* Log the process name and args */
297 message(console, "Executing '%s'\r\n", command);
298 message(log, "Executing '%s'\r\n", command);
299 250
300 /* Now run it. This program should take over this PID, 251 /* Now run it. The new program will take over this PID,
301 * so nothing further in init.c should be run. */ 252 * so nothing further in init.c should be run. */
302 execvp(args[1], args + 1); 253 message(log, "Executing '%s', pid(%d)\r\n", *command, getpid());
254 execvp(*command, (char**)command+1);
303 255
304 message(console, "Hmm. Trying as a script.\r\n"); 256 message(log, "Hmm. Trying as a script.\r\n");
305 /* If shell scripts are not executed, force the issue */ 257 /* If shell scripts are not executed, force the issue */
306 if (errno == ENOEXEC) { 258 if (errno == ENOEXEC) {
307 char buf[256]; 259 char * args[16];
308 args[1] = SHELL; 260 args[0] = SHELL;
309 args[2] = "-c"; 261 args[1] = "-c";
310 strcpy(buf, "exec "); 262 args[2] = "exec";
311 strcat(buf, command); 263 for( i=0 ; i<16 && command[i]; i++)
312 args[3] = buf; 264 args[3+i] = (char*)command[i];
313 args[4] = NULL; 265 args[i] = NULL;
314 execvp(args[1], args + 1); 266 execvp(*args, (char**)args+1);
315 } 267 }
316 message(console, "Could not execute '%s'\n", command); 268 message(log, "Could not execute '%s'\n", command);
317 exit(-1); 269 exit(-1);
318 } 270 }
319 return pid; 271 return pid;
@@ -323,6 +275,8 @@ static int run(const char *command, char *terminal, int get_enter)
323#ifndef DEBUG_INIT 275#ifndef DEBUG_INIT
324static void shutdown_system(void) 276static void shutdown_system(void)
325{ 277{
278 const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0};
279 const char* const umount_cmd[] = { "/bin/umount", "-a", "-n", 0};
326 280
327 message(console, "The system is going down NOW !!\r\n"); 281 message(console, "The system is going down NOW !!\r\n");
328 sync(); 282 sync();
@@ -337,8 +291,8 @@ static void shutdown_system(void)
337 message(console, "Sending SIGKILL to all processes.\r\n"); 291 message(console, "Sending SIGKILL to all processes.\r\n");
338 kill(-1, SIGKILL); 292 kill(-1, SIGKILL);
339 sleep(1); 293 sleep(1);
340 waitfor(run("/bin/swapoff -a", console, 0)); 294 waitfor(run( swap_off_cmd, console, 0));
341 waitfor(run("/bin/umount -a -n", console, 0)); 295 waitfor(run( umount_cmd, console, 0));
342 sync(); 296 sync();
343 if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) { 297 if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) {
344 /* Removed bdflush call, kupdate in kernels >2.2.11 */ 298 /* Removed bdflush call, kupdate in kernels >2.2.11 */
@@ -369,18 +323,21 @@ static void reboot_signal(int sig)
369extern int init_main(int argc, char **argv) 323extern int init_main(int argc, char **argv)
370{ 324{
371 int run_rc = TRUE; 325 int run_rc = TRUE;
372 int pid1 = 0; 326 pid_t pid = 0;
373 int pid2 = 0; 327 pid_t pid1 = 0;
328 pid_t pid2 = 0;
374 struct stat statbuf; 329 struct stat statbuf;
375 const char *init_commands = SHELL " -c exec " INITSCRIPT; 330 const char* const swap_on_cmd[] = { "/bin/swapon", " -a ", 0};
376 const char *shell_commands = SHELL " -"; 331 const char* const init_commands[] = { SHELL, " -c", " exec ", INITSCRIPT, 0};
377 const char *tty0_commands = init_commands; 332 const char* const shell_commands[] = { SHELL, " -", 0};
378 const char *tty1_commands = shell_commands; 333 const char* const* tty0_commands = init_commands;
334 const char* const* tty1_commands = shell_commands;
379 char *hello_msg_format = 335 char *hello_msg_format =
380 "init started: BusyBox v%s (%s) multi-call binary\r\n"; 336 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
381 const char *no_memory = 337 const char *no_memory =
382 "Sorry, your computer does not have enough memory.\r\n"; 338 "Sorry, your computer does not have enough memory.\r\n";
383 339
340 pid = getpid();
384 341
385#ifndef DEBUG_INIT 342#ifndef DEBUG_INIT
386 /* Set up sig handlers */ 343 /* Set up sig handlers */
@@ -392,21 +349,20 @@ extern int init_main(int argc, char **argv)
392 signal(SIGUSR2, reboot_signal); 349 signal(SIGUSR2, reboot_signal);
393 signal(SIGINT, reboot_signal); 350 signal(SIGINT, reboot_signal);
394 signal(SIGTERM, reboot_signal); 351 signal(SIGTERM, reboot_signal);
395#endif
396 /* Figure out where the default console should be */
397 console_init();
398 352
399 /* Turn off rebooting via CTL-ALT-DEL -- we get a 353 /* Turn off rebooting via CTL-ALT-DEL -- we get a
400 * SIGINT on CAD so we can shut things down gracefully... */ 354 * SIGINT on CAD so we can shut things down gracefully... */
401#ifndef DEBUG_INIT
402 reboot(RB_DISABLE_CAD); 355 reboot(RB_DISABLE_CAD);
403#endif 356#endif
357
358 /* Figure out where the default console should be */
359 console_init();
404 360
405 /* Close whatever files are open, and reset the console. */ 361 /* Close whatever files are open, and reset the console. */
406 close(0); 362 close(0);
407 close(1); 363 close(1);
408 close(2); 364 close(2);
409 set_term(); 365 //set_term(0);
410 setsid(); 366 setsid();
411 367
412 /* Make sure PATH is set to something sane */ 368 /* Make sure PATH is set to something sane */
@@ -414,16 +370,17 @@ extern int init_main(int argc, char **argv)
414 putenv(PATH_DEFAULT); 370 putenv(PATH_DEFAULT);
415 371
416 /* Hello world */ 372 /* Hello world */
417 message(log, hello_msg_format, BB_VER, BB_BT); 373 message(log, hello_msg_format, pid, BB_VER, BB_BT);
418 message(console, hello_msg_format, BB_VER, BB_BT); 374 fprintf(stderr, hello_msg_format, pid, BB_VER, BB_BT);
419 375
376
420 /* Mount /proc */ 377 /* Mount /proc */
421 if (mount("/proc", "/proc", "proc", 0, 0)) { 378 if (mount("/proc", "/proc", "proc", 0, 0) == 0) {
422 message(console, "Mounting /proc: failed!\r\n"); 379 fprintf(stderr, "Mounting /proc: done.\n");
423 message(log, "Mounting /proc: failed!\n");
424 } else {
425 message(console, "Mounting /proc: done.\r\n");
426 message(log, "Mounting /proc: done.\n"); 380 message(log, "Mounting /proc: done.\n");
381 } else {
382 fprintf(stderr, "Mounting /proc: failed!\n");
383 message(log, "Mounting /proc: failed!\n");
427 } 384 }
428 385
429 /* Make sure there is enough memory to do something useful */ 386 /* Make sure there is enough memory to do something useful */
@@ -432,15 +389,15 @@ extern int init_main(int argc, char **argv)
432 int retval; 389 int retval;
433 retval = stat("/etc/fstab", &statbuf); 390 retval = stat("/etc/fstab", &statbuf);
434 if (retval) { 391 if (retval) {
435 message(console, "%s", no_memory); 392 fprintf(stderr, "%s", no_memory);
436 while (1) { 393 while (1) {
437 sleep(1); 394 sleep(1);
438 } 395 }
439 } else { 396 } else {
440 /* Try to turn on swap */ 397 /* Try to turn on swap */
441 waitfor(run("/bin/swapon -a", console, 0)); 398 waitfor(run(swap_on_cmd, console, 0));
442 if (mem_total() < 2000) { 399 if (mem_total() < 2000) {
443 message(console, "%s", no_memory); 400 fprintf(stderr, "%s", no_memory);
444 while (1) { 401 while (1) {
445 sleep(1); 402 sleep(1);
446 } 403 }
@@ -466,11 +423,11 @@ extern int init_main(int argc, char **argv)
466 * start up some VTs if somebody hits enter... 423 * start up some VTs if somebody hits enter...
467 */ 424 */
468 for (;;) { 425 for (;;) {
469 int wpid; 426 pid_t wpid;
470 int status; 427 int status;
471 428
472 if (pid1 == 0 && tty0_commands) { 429 if (pid1 == 0 && tty0_commands) {
473 pid1 = run(tty0_commands, first_terminal, 1); 430 pid1 = run(tty0_commands, console, 1);
474 } 431 }
475 if (pid2 == 0 && tty1_commands) { 432 if (pid2 == 0 && tty1_commands) {
476 pid2 = run(tty1_commands, second_terminal, 1); 433 pid2 = run(tty1_commands, second_terminal, 1);
diff --git a/init/init.c b/init/init.c
index 83fc9519a..d950e2bef 100644
--- a/init/init.c
+++ b/init/init.c
@@ -52,7 +52,7 @@
52#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" 52#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
53 53
54static char *console = CONSOLE; 54static char *console = CONSOLE;
55static char *first_terminal = "/dev/tty1"; 55//static char *first_terminal = "/dev/tty1";
56static char *second_terminal = "/dev/tty2"; 56static char *second_terminal = "/dev/tty2";
57static char *log = "/dev/tty3"; 57static char *log = "/dev/tty3";
58 58
@@ -83,7 +83,7 @@ void message(char *device, char *fmt, ...)
83 int fd; 83 int fd;
84 va_list arguments; 84 va_list arguments;
85 85
86 if ((fd = device_open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0) { 86 if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
87 va_start(arguments, fmt); 87 va_start(arguments, fmt);
88 vdprintf(fd, fmt, arguments); 88 vdprintf(fd, fmt, arguments);
89 va_end(arguments); 89 va_end(arguments);
@@ -98,18 +98,13 @@ void message(char *device, char *fmt, ...)
98} 98}
99 99
100/* Set terminal settings to reasonable defaults */ 100/* Set terminal settings to reasonable defaults */
101void set_term() 101void set_term( int fd)
102{ 102{
103 int fd;
104 struct termios tty; 103 struct termios tty;
105 104
106 if ((fd = device_open(console, O_RDWR | O_NOCTTY)) < 0) { 105 tcgetattr(fd, &tty);
107 message(log, "can't open %s\n", console); 106 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
108 return; 107 tty.c_cflag |= HUPCL|CLOCAL;
109 }
110 ioctl(fd, TCGETS, &tty);
111 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
112 tty.c_cflag |= HUPCL | CLOCAL;
113 108
114 tty.c_cc[VINTR] = 3; 109 tty.c_cc[VINTR] = 3;
115 tty.c_cc[VQUIT] = 28; 110 tty.c_cc[VQUIT] = 28;
@@ -122,14 +117,11 @@ void set_term()
122 tty.c_cc[VSTOP] = 19; 117 tty.c_cc[VSTOP] = 19;
123 tty.c_cc[VSUSP] = 26; 118 tty.c_cc[VSUSP] = 26;
124 119
125 /* Set pre and post processing */ 120 tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY;
126 tty.c_iflag = IGNPAR | ICRNL | IXON | IXANY; 121 tty.c_oflag = OPOST|ONLCR;
127 tty.c_oflag = OPOST | ONLCR; 122 tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE;
128 tty.c_lflag = ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE;
129 123
130 /* Now set the terminal line. */ 124 tcsetattr(fd, TCSANOW, &tty);
131 ioctl(fd, TCSETS, &tty);
132 close(fd);
133} 125}
134 126
135static int mem_total() 127static int mem_total()
@@ -175,6 +167,7 @@ static void console_init()
175 int tried_vtmaster = 0; 167 int tried_vtmaster = 0;
176 char *s; 168 char *s;
177 169
170 fprintf(stderr, "entering console_init()\n");
178 if ((s = getenv("CONSOLE")) != NULL) 171 if ((s = getenv("CONSOLE")) != NULL)
179 console = s; 172 console = s;
180 else { 173 else {
@@ -183,7 +176,7 @@ static void console_init()
183 } 176 }
184 177
185 if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) { 178 if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) {
186 fprintf(stderr, "/dev/console does not exist, or is a symlink.\n"); 179 fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n");
187 } 180 }
188 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { 181 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
189 if (!tried_devcons) { 182 if (!tried_devcons) {
@@ -218,42 +211,14 @@ static int waitfor(int pid)
218} 211}
219 212
220 213
221static int run(const char *command, char *terminal, int get_enter) 214static pid_t run(const char * const* command,
215 char *terminal, int get_enter)
222{ 216{
223 int f, pid; 217 int i;
224 char *args[16]; 218 pid_t pid;
225 char buf[256];
226 char *ptr;
227 static const char press_enter[] = 219 static const char press_enter[] =
228 "\nPlease press Enter to activate this console. "; 220 "\nPlease press Enter to activate this console. ";
229 221
230
231 /* Make a proper command from the command string */
232 strcpy(buf, command);
233 ptr = buf;
234 for (f = 1; f < 15; f++) {
235 /* Skip white space */
236 while (*ptr == ' ' || *ptr == '\t')
237 ptr++;
238 args[f] = ptr;
239 /* May be trailing space.. */
240 if (*ptr == 0)
241 break;
242 /* Skip this `word' */
243 while (*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '#')
244 ptr++;
245 /* If end-of-line, break */
246 if (*ptr == '#' || *ptr == 0) {
247 f++;
248 *ptr = 0;
249 break;
250 }
251 /* End word with \0 and continue */
252 args[f] = NULL;
253 }
254 args[0] = args[1];
255
256
257 if ((pid = fork()) == 0) { 222 if ((pid = fork()) == 0) {
258 /* Clean up */ 223 /* Clean up */
259 close(0); 224 close(0);
@@ -261,22 +226,11 @@ static int run(const char *command, char *terminal, int get_enter)
261 close(2); 226 close(2);
262 setsid(); 227 setsid();
263 228
264#if 1
265 //if ((f = device_open(terminal, O_RDWR | O_NOCTTY)) < 0) {
266 if ((f = device_open(terminal, O_RDWR )) < 0) {
267 message(log, "open(%s) failed: %s\n",
268 terminal, strerror(errno));
269 return -1;
270 }
271 dup(f);
272 dup(f);
273#else
274 open(terminal, O_RDWR); 229 open(terminal, O_RDWR);
275 dup(0); 230 dup(0);
276 dup(0); 231 dup(0);
277 //tcsetpgrp(0, getpgrp()); 232 tcsetpgrp(0, getpgrp());
278#endif 233 set_term(0);
279 set_term();
280 234
281 if (get_enter) { 235 if (get_enter) {
282 /* 236 /*
@@ -288,32 +242,30 @@ static int run(const char *command, char *terminal, int get_enter)
288 * specifies. 242 * specifies.
289 */ 243 */
290 char c; 244 char c;
291 write(1, press_enter, sizeof(press_enter) - 1); 245 write(0, press_enter, sizeof(press_enter) - 1);
292 read(0, &c, 1); 246 read(0, &c, 1);
293 message(console, "Got an enter\r\n");
294 } 247 }
295 248
296 /* Log the process name and args */ 249 /* Log the process name and args */
297 message(console, "Executing '%s'\r\n", command);
298 message(log, "Executing '%s'\r\n", command);
299 250
300 /* Now run it. This program should take over this PID, 251 /* Now run it. The new program will take over this PID,
301 * so nothing further in init.c should be run. */ 252 * so nothing further in init.c should be run. */
302 execvp(args[1], args + 1); 253 message(log, "Executing '%s', pid(%d)\r\n", *command, getpid());
254 execvp(*command, (char**)command+1);
303 255
304 message(console, "Hmm. Trying as a script.\r\n"); 256 message(log, "Hmm. Trying as a script.\r\n");
305 /* If shell scripts are not executed, force the issue */ 257 /* If shell scripts are not executed, force the issue */
306 if (errno == ENOEXEC) { 258 if (errno == ENOEXEC) {
307 char buf[256]; 259 char * args[16];
308 args[1] = SHELL; 260 args[0] = SHELL;
309 args[2] = "-c"; 261 args[1] = "-c";
310 strcpy(buf, "exec "); 262 args[2] = "exec";
311 strcat(buf, command); 263 for( i=0 ; i<16 && command[i]; i++)
312 args[3] = buf; 264 args[3+i] = (char*)command[i];
313 args[4] = NULL; 265 args[i] = NULL;
314 execvp(args[1], args + 1); 266 execvp(*args, (char**)args+1);
315 } 267 }
316 message(console, "Could not execute '%s'\n", command); 268 message(log, "Could not execute '%s'\n", command);
317 exit(-1); 269 exit(-1);
318 } 270 }
319 return pid; 271 return pid;
@@ -323,6 +275,8 @@ static int run(const char *command, char *terminal, int get_enter)
323#ifndef DEBUG_INIT 275#ifndef DEBUG_INIT
324static void shutdown_system(void) 276static void shutdown_system(void)
325{ 277{
278 const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0};
279 const char* const umount_cmd[] = { "/bin/umount", "-a", "-n", 0};
326 280
327 message(console, "The system is going down NOW !!\r\n"); 281 message(console, "The system is going down NOW !!\r\n");
328 sync(); 282 sync();
@@ -337,8 +291,8 @@ static void shutdown_system(void)
337 message(console, "Sending SIGKILL to all processes.\r\n"); 291 message(console, "Sending SIGKILL to all processes.\r\n");
338 kill(-1, SIGKILL); 292 kill(-1, SIGKILL);
339 sleep(1); 293 sleep(1);
340 waitfor(run("/bin/swapoff -a", console, 0)); 294 waitfor(run( swap_off_cmd, console, 0));
341 waitfor(run("/bin/umount -a -n", console, 0)); 295 waitfor(run( umount_cmd, console, 0));
342 sync(); 296 sync();
343 if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) { 297 if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) {
344 /* Removed bdflush call, kupdate in kernels >2.2.11 */ 298 /* Removed bdflush call, kupdate in kernels >2.2.11 */
@@ -369,18 +323,21 @@ static void reboot_signal(int sig)
369extern int init_main(int argc, char **argv) 323extern int init_main(int argc, char **argv)
370{ 324{
371 int run_rc = TRUE; 325 int run_rc = TRUE;
372 int pid1 = 0; 326 pid_t pid = 0;
373 int pid2 = 0; 327 pid_t pid1 = 0;
328 pid_t pid2 = 0;
374 struct stat statbuf; 329 struct stat statbuf;
375 const char *init_commands = SHELL " -c exec " INITSCRIPT; 330 const char* const swap_on_cmd[] = { "/bin/swapon", " -a ", 0};
376 const char *shell_commands = SHELL " -"; 331 const char* const init_commands[] = { SHELL, " -c", " exec ", INITSCRIPT, 0};
377 const char *tty0_commands = init_commands; 332 const char* const shell_commands[] = { SHELL, " -", 0};
378 const char *tty1_commands = shell_commands; 333 const char* const* tty0_commands = init_commands;
334 const char* const* tty1_commands = shell_commands;
379 char *hello_msg_format = 335 char *hello_msg_format =
380 "init started: BusyBox v%s (%s) multi-call binary\r\n"; 336 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
381 const char *no_memory = 337 const char *no_memory =
382 "Sorry, your computer does not have enough memory.\r\n"; 338 "Sorry, your computer does not have enough memory.\r\n";
383 339
340 pid = getpid();
384 341
385#ifndef DEBUG_INIT 342#ifndef DEBUG_INIT
386 /* Set up sig handlers */ 343 /* Set up sig handlers */
@@ -392,21 +349,20 @@ extern int init_main(int argc, char **argv)
392 signal(SIGUSR2, reboot_signal); 349 signal(SIGUSR2, reboot_signal);
393 signal(SIGINT, reboot_signal); 350 signal(SIGINT, reboot_signal);
394 signal(SIGTERM, reboot_signal); 351 signal(SIGTERM, reboot_signal);
395#endif
396 /* Figure out where the default console should be */
397 console_init();
398 352
399 /* Turn off rebooting via CTL-ALT-DEL -- we get a 353 /* Turn off rebooting via CTL-ALT-DEL -- we get a
400 * SIGINT on CAD so we can shut things down gracefully... */ 354 * SIGINT on CAD so we can shut things down gracefully... */
401#ifndef DEBUG_INIT
402 reboot(RB_DISABLE_CAD); 355 reboot(RB_DISABLE_CAD);
403#endif 356#endif
357
358 /* Figure out where the default console should be */
359 console_init();
404 360
405 /* Close whatever files are open, and reset the console. */ 361 /* Close whatever files are open, and reset the console. */
406 close(0); 362 close(0);
407 close(1); 363 close(1);
408 close(2); 364 close(2);
409 set_term(); 365 //set_term(0);
410 setsid(); 366 setsid();
411 367
412 /* Make sure PATH is set to something sane */ 368 /* Make sure PATH is set to something sane */
@@ -414,16 +370,17 @@ extern int init_main(int argc, char **argv)
414 putenv(PATH_DEFAULT); 370 putenv(PATH_DEFAULT);
415 371
416 /* Hello world */ 372 /* Hello world */
417 message(log, hello_msg_format, BB_VER, BB_BT); 373 message(log, hello_msg_format, pid, BB_VER, BB_BT);
418 message(console, hello_msg_format, BB_VER, BB_BT); 374 fprintf(stderr, hello_msg_format, pid, BB_VER, BB_BT);
419 375
376
420 /* Mount /proc */ 377 /* Mount /proc */
421 if (mount("/proc", "/proc", "proc", 0, 0)) { 378 if (mount("/proc", "/proc", "proc", 0, 0) == 0) {
422 message(console, "Mounting /proc: failed!\r\n"); 379 fprintf(stderr, "Mounting /proc: done.\n");
423 message(log, "Mounting /proc: failed!\n");
424 } else {
425 message(console, "Mounting /proc: done.\r\n");
426 message(log, "Mounting /proc: done.\n"); 380 message(log, "Mounting /proc: done.\n");
381 } else {
382 fprintf(stderr, "Mounting /proc: failed!\n");
383 message(log, "Mounting /proc: failed!\n");
427 } 384 }
428 385
429 /* Make sure there is enough memory to do something useful */ 386 /* Make sure there is enough memory to do something useful */
@@ -432,15 +389,15 @@ extern int init_main(int argc, char **argv)
432 int retval; 389 int retval;
433 retval = stat("/etc/fstab", &statbuf); 390 retval = stat("/etc/fstab", &statbuf);
434 if (retval) { 391 if (retval) {
435 message(console, "%s", no_memory); 392 fprintf(stderr, "%s", no_memory);
436 while (1) { 393 while (1) {
437 sleep(1); 394 sleep(1);
438 } 395 }
439 } else { 396 } else {
440 /* Try to turn on swap */ 397 /* Try to turn on swap */
441 waitfor(run("/bin/swapon -a", console, 0)); 398 waitfor(run(swap_on_cmd, console, 0));
442 if (mem_total() < 2000) { 399 if (mem_total() < 2000) {
443 message(console, "%s", no_memory); 400 fprintf(stderr, "%s", no_memory);
444 while (1) { 401 while (1) {
445 sleep(1); 402 sleep(1);
446 } 403 }
@@ -466,11 +423,11 @@ extern int init_main(int argc, char **argv)
466 * start up some VTs if somebody hits enter... 423 * start up some VTs if somebody hits enter...
467 */ 424 */
468 for (;;) { 425 for (;;) {
469 int wpid; 426 pid_t wpid;
470 int status; 427 int status;
471 428
472 if (pid1 == 0 && tty0_commands) { 429 if (pid1 == 0 && tty0_commands) {
473 pid1 = run(tty0_commands, first_terminal, 1); 430 pid1 = run(tty0_commands, console, 1);
474 } 431 }
475 if (pid2 == 0 && tty1_commands) { 432 if (pid2 == 0 && tty1_commands) {
476 pid2 = run(tty1_commands, second_terminal, 1); 433 pid2 = run(tty1_commands, second_terminal, 1);