aboutsummaryrefslogtreecommitdiff
path: root/init/init.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-02-17 15:52:02 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-02-17 15:52:02 +0000
commitec27feb04589d46233802f686559fb5f532fb2df (patch)
tree3c6f8dd683c840fd4855f86e6956db38ad9320bb /init/init.c
parentc84520d73dbe100449d84241ec0df9d02ee0fc4d (diff)
downloadbusybox-w32-ec27feb04589d46233802f686559fb5f532fb2df.tar.gz
busybox-w32-ec27feb04589d46233802f686559fb5f532fb2df.tar.bz2
busybox-w32-ec27feb04589d46233802f686559fb5f532fb2df.zip
init: code readability enhancements; very small code changes
Diffstat (limited to 'init/init.c')
-rw-r--r--init/init.c104
1 files changed, 52 insertions, 52 deletions
diff --git a/init/init.c b/init/init.c
index 76368fb2e..b488f649f 100644
--- a/init/init.c
+++ b/init/init.c
@@ -10,7 +10,6 @@
10 */ 10 */
11 11
12#include "busybox.h" 12#include "busybox.h"
13#include <errno.h>
14#include <paths.h> 13#include <paths.h>
15#include <signal.h> 14#include <signal.h>
16#include <sys/ioctl.h> 15#include <sys/ioctl.h>
@@ -119,7 +118,7 @@ struct init_action {
119 118
120/* Static variables */ 119/* Static variables */
121static struct init_action *init_action_list = NULL; 120static struct init_action *init_action_list = NULL;
122static char console[CONSOLE_BUFF_SIZE] = CONSOLE_DEV; 121static char console_name[CONSOLE_BUFF_SIZE] = DEV_CONSOLE;
123 122
124#if !ENABLE_SYSLOGD 123#if !ENABLE_SYSLOGD
125static const char *log_console = VC_5; 124static const char *log_console = VC_5;
@@ -129,13 +128,13 @@ static sig_atomic_t got_cont = 0;
129#endif 128#endif
130 129
131enum { 130enum {
132 LOG = 0x1, 131 L_LOG = 0x1,
133 CONSOLE = 0x2, 132 L_CONSOLE = 0x2,
134 133
135#if ENABLE_FEATURE_EXTRA_QUIET 134#if ENABLE_FEATURE_EXTRA_QUIET
136 MAYBE_CONSOLE = 0x0, 135 MAYBE_CONSOLE = 0x0,
137#else 136#else
138 MAYBE_CONSOLE = CONSOLE, 137 MAYBE_CONSOLE = L_CONSOLE,
139#endif 138#endif
140 139
141#ifndef RB_HALT_SYSTEM 140#ifndef RB_HALT_SYSTEM
@@ -171,7 +170,7 @@ static void loop_forever(void)
171#endif 170#endif
172 171
173/* Print a message to the specified device. 172/* Print a message to the specified device.
174 * Device may be bitwise-or'd from LOG | CONSOLE */ 173 * Device may be bitwise-or'd from L_LOG | L_CONSOLE */
175#if ENABLE_DEBUG_INIT 174#if ENABLE_DEBUG_INIT
176#define messageD message 175#define messageD message
177#else 176#else
@@ -191,11 +190,11 @@ static void message(int device, const char *fmt, ...)
191 msg[0] = '\r'; 190 msg[0] = '\r';
192 va_start(arguments, fmt); 191 va_start(arguments, fmt);
193 l = vsnprintf(msg + 1, 1024 - 2, fmt, arguments) + 1; 192 l = vsnprintf(msg + 1, 1024 - 2, fmt, arguments) + 1;
194 va_end(arguments); 193 va_end(arguments);
195 194
196#if ENABLE_SYSLOGD 195#if ENABLE_SYSLOGD
197 /* Log the message to syslogd */ 196 /* Log the message to syslogd */
198 if (device & LOG) { 197 if (device & L_LOG) {
199 /* don`t out "\r\n" */ 198 /* don`t out "\r\n" */
200 openlog(applet_name, 0, LOG_DAEMON); 199 openlog(applet_name, 0, LOG_DAEMON);
201 syslog(LOG_INFO, "%s", msg + 1); 200 syslog(LOG_INFO, "%s", msg + 1);
@@ -211,21 +210,22 @@ static void message(int device, const char *fmt, ...)
211 /* Take full control of the log tty, and never close it. 210 /* Take full control of the log tty, and never close it.
212 * It's mine, all mine! Muhahahaha! */ 211 * It's mine, all mine! Muhahahaha! */
213 if (log_fd < 0) { 212 if (log_fd < 0) {
214 if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) { 213 log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
214 if (log_fd < 0) {
215 log_fd = -2; 215 log_fd = -2;
216 bb_error_msg("bummer, can't write to log on %s!", log_console); 216 bb_error_msg("bummer, can't log to %s!", log_console);
217 device = CONSOLE; 217 device = L_CONSOLE;
218 } else { 218 } else {
219 fcntl(log_fd, F_SETFD, FD_CLOEXEC); 219 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
220 } 220 }
221 } 221 }
222 if ((device & LOG) && (log_fd >= 0)) { 222 if (device & L_LOG) {
223 full_write(log_fd, msg, l); 223 full_write(log_fd, msg, l);
224 } 224 }
225#endif 225#endif
226 226
227 if (device & CONSOLE) { 227 if (device & L_CONSOLE) {
228 int fd = device_open(CONSOLE_DEV, 228 int fd = device_open(DEV_CONSOLE,
229 O_WRONLY | O_NOCTTY | O_NONBLOCK); 229 O_WRONLY | O_NOCTTY | O_NONBLOCK);
230 /* Always send console messages to /dev/console so people will see them. */ 230 /* Always send console messages to /dev/console so people will see them. */
231 if (fd >= 0) { 231 if (fd >= 0) {
@@ -291,26 +291,25 @@ static void console_init(void)
291 char *s; 291 char *s;
292 292
293 if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) { 293 if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
294 safe_strncpy(console, s, sizeof(console)); 294 safe_strncpy(console_name, s, sizeof(console_name));
295 } else { 295 } else {
296 /* 2.2 kernels: identify the real console backend and try to use it */ 296 /* 2.2 kernels: identify the real console backend and try to use it */
297 if (ioctl(0, TIOCGSERIAL, &sr) == 0) { 297 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
298 /* this is a serial console */ 298 /* this is a serial console */
299 snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line); 299 snprintf(console_name, sizeof(console_name) - 1, SC_FORMAT, sr.line);
300 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) { 300 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
301 /* this is linux virtual tty */ 301 /* this is linux virtual tty */
302 snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active); 302 snprintf(console_name, sizeof(console_name) - 1, VC_FORMAT, vt.v_active);
303 } else { 303 } else {
304 safe_strncpy(console, CONSOLE_DEV, sizeof(console)); 304 strcpy(console_name, DEV_CONSOLE);
305 tried++; 305 tried++;
306 } 306 }
307 } 307 }
308 308
309 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) { 309 while ((fd = open(console_name, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
310 /* Can't open selected console -- try 310 /* Can't open selected console -- try
311 logical system console and VT_MASTER */ 311 logical system console and VT_MASTER */
312 safe_strncpy(console, (tried == 0 ? CONSOLE_DEV : CURRENT_VC), 312 strcpy(console_name, (tried == 0 ? DEV_CONSOLE : CURRENT_VC));
313 sizeof(console));
314 tried++; 313 tried++;
315 } 314 }
316 if (fd < 0) { 315 if (fd < 0) {
@@ -318,7 +317,7 @@ static void console_init(void)
318#if !ENABLE_SYSLOGD 317#if !ENABLE_SYSLOGD
319 log_console = 318 log_console =
320#endif 319#endif
321 safe_strncpy(console, bb_dev_null, sizeof(console)); 320 strcpy(console_name, bb_dev_null);
322 } else { 321 } else {
323 s = getenv("TERM"); 322 s = getenv("TERM");
324 /* check for serial console */ 323 /* check for serial console */
@@ -328,7 +327,7 @@ static void console_init(void)
328 if (s == NULL || strcmp(s, "linux") == 0) 327 if (s == NULL || strcmp(s, "linux") == 0)
329 putenv((char*)"TERM=vt102"); 328 putenv((char*)"TERM=vt102");
330#if !ENABLE_SYSLOGD 329#if !ENABLE_SYSLOGD
331 log_console = console; 330 log_console = console_name;
332#endif 331#endif
333 } else { 332 } else {
334 if (s == NULL) 333 if (s == NULL)
@@ -336,7 +335,7 @@ static void console_init(void)
336 } 335 }
337 close(fd); 336 close(fd);
338 } 337 }
339 messageD(LOG, "console=%s", console); 338 messageD(L_LOG, "console=%s", console_name);
340} 339}
341 340
342static void fixup_argv(int argc, char **argv, const char *new_argv0) 341static void fixup_argv(int argc, char **argv, const char *new_argv0)
@@ -356,14 +355,14 @@ static void fixup_argv(int argc, char **argv, const char *new_argv0)
356} 355}
357 356
358/* Open the new terminal device */ 357/* Open the new terminal device */
359static void open_new_terminal(const char * const device, const int fail) { 358static void open_new_terminal(const char* device, int fail) {
360 struct stat sb; 359 struct stat sb;
361 360
362 if ((device_open(device, O_RDWR)) < 0) { 361 if ((device_open(device, O_RDWR)) < 0) {
363 if (stat(device, &sb) != 0) { 362 if (stat(device, &sb) != 0) {
364 message(LOG | CONSOLE, "device '%s' does not exist.", device); 363 message(L_LOG | L_CONSOLE, "device '%s' does not exist", device);
365 } else { 364 } else {
366 message(LOG | CONSOLE, "Bummer, can't open %s", device); 365 message(L_LOG | L_CONSOLE, "Bummer, can't open %s", device);
367 } 366 }
368 if (fail) 367 if (fail)
369 _exit(1); 368 _exit(1);
@@ -434,7 +433,7 @@ static pid_t run(const struct init_action *a)
434 433
435 /* Now fork off another process to just hang around */ 434 /* Now fork off another process to just hang around */
436 if ((pid = fork()) < 0) { 435 if ((pid = fork()) < 0) {
437 message(LOG | CONSOLE, "Can't fork!"); 436 message(L_LOG | L_CONSOLE, "Can't fork!");
438 _exit(1); 437 _exit(1);
439 } 438 }
440 439
@@ -453,7 +452,7 @@ static pid_t run(const struct init_action *a)
453 452
454 /* Use a temporary process to steal the controlling tty. */ 453 /* Use a temporary process to steal the controlling tty. */
455 if ((pid = fork()) < 0) { 454 if ((pid = fork()) < 0) {
456 message(LOG | CONSOLE, "Can't fork!"); 455 message(L_LOG | L_CONSOLE, "Can't fork!");
457 _exit(1); 456 _exit(1);
458 } 457 }
459 if (pid == 0) { 458 if (pid == 0) {
@@ -506,7 +505,7 @@ static pid_t run(const struct init_action *a)
506 505
507 /* make a new argv[0] */ 506 /* make a new argv[0] */
508 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) { 507 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
509 message(LOG | CONSOLE, bb_msg_memory_exhausted); 508 message(L_LOG | L_CONSOLE, bb_msg_memory_exhausted);
510 cmd[0] = cmdpath; 509 cmd[0] = cmdpath;
511 } else { 510 } else {
512 cmd[0][0] = '-'; 511 cmd[0][0] = '-';
@@ -532,7 +531,7 @@ static pid_t run(const struct init_action *a)
532 * be allowed to start a shell or whatever an init script 531 * be allowed to start a shell or whatever an init script
533 * specifies. 532 * specifies.
534 */ 533 */
535 messageD(LOG, "Waiting for enter to start '%s'" 534 messageD(L_LOG, "Waiting for enter to start '%s'"
536 "(pid %d, terminal %s)\n", 535 "(pid %d, terminal %s)\n",
537 cmdpath, getpid(), a->terminal); 536 cmdpath, getpid(), a->terminal);
538 full_write(1, press_enter, sizeof(press_enter) - 1); 537 full_write(1, press_enter, sizeof(press_enter) - 1);
@@ -542,7 +541,7 @@ static pid_t run(const struct init_action *a)
542#endif 541#endif
543 542
544 /* Log the process name and args */ 543 /* Log the process name and args */
545 message(LOG, "Starting pid %d, console %s: '%s'", 544 message(L_LOG, "Starting pid %d, console %s: '%s'",
546 getpid(), a->terminal, cmdpath); 545 getpid(), a->terminal, cmdpath);
547 546
548#if ENABLE_FEATURE_INIT_COREDUMPS 547#if ENABLE_FEATURE_INIT_COREDUMPS
@@ -563,7 +562,7 @@ static pid_t run(const struct init_action *a)
563 BB_EXECVP(cmdpath, cmd); 562 BB_EXECVP(cmdpath, cmd);
564 563
565 /* We're still here? Some error happened. */ 564 /* We're still here? Some error happened. */
566 message(LOG | CONSOLE, "Bummer, cannot run '%s': %m", cmdpath); 565 message(L_LOG | L_CONSOLE, "Bummer, cannot run '%s': %m", cmdpath);
567 _exit(-1); 566 _exit(-1);
568 } 567 }
569 sigprocmask(SIG_SETMASK, &omask, NULL); 568 sigprocmask(SIG_SETMASK, &omask, NULL);
@@ -658,16 +657,16 @@ static void shutdown_system(void)
658 /* Allow Ctrl-Alt-Del to reboot system. */ 657 /* Allow Ctrl-Alt-Del to reboot system. */
659 init_reboot(RB_ENABLE_CAD); 658 init_reboot(RB_ENABLE_CAD);
660 659
661 message(CONSOLE | LOG, "The system is going down NOW !!"); 660 message(L_CONSOLE | L_LOG, "The system is going down NOW !!");
662 sync(); 661 sync();
663 662
664 /* Send signals to every process _except_ pid 1 */ 663 /* Send signals to every process _except_ pid 1 */
665 message(CONSOLE | LOG, init_sending_format, "TERM"); 664 message(L_CONSOLE | L_LOG, init_sending_format, "TERM");
666 kill(-1, SIGTERM); 665 kill(-1, SIGTERM);
667 sleep(1); 666 sleep(1);
668 sync(); 667 sync();
669 668
670 message(CONSOLE | LOG, init_sending_format, "KILL"); 669 message(L_CONSOLE | L_LOG, init_sending_format, "KILL");
671 kill(-1, SIGKILL); 670 kill(-1, SIGKILL);
672 sleep(1); 671 sleep(1);
673 672
@@ -712,10 +711,10 @@ static void exec_signal(int sig ATTRIBUTE_UNUSED)
712 dup(0); 711 dup(0);
713 dup(0); 712 dup(0);
714 713
715 messageD(CONSOLE | LOG, "Trying to re-exec %s", a->command); 714 messageD(L_CONSOLE | L_LOG, "Trying to re-exec %s", a->command);
716 BB_EXECLP(a->command, a->command, NULL); 715 BB_EXECLP(a->command, a->command, NULL);
717 716
718 message(CONSOLE | LOG, "exec of '%s' failed: %m", 717 message(L_CONSOLE | L_LOG, "exec of '%s' failed: %m",
719 a->command); 718 a->command);
720 sync(); 719 sync();
721 sleep(2); 720 sleep(2);
@@ -741,7 +740,7 @@ static void shutdown_signal(int sig)
741 m = "poweroff"; 740 m = "poweroff";
742 rb = RB_POWER_OFF; 741 rb = RB_POWER_OFF;
743 } 742 }
744 message(CONSOLE | LOG, "Requesting system %s.", m); 743 message(L_CONSOLE | L_LOG, "Requesting system %s", m);
745 sync(); 744 sync();
746 745
747 /* allow time for last message to reach serial console */ 746 /* allow time for last message to reach serial console */
@@ -781,7 +780,7 @@ static void new_init_action(int action, const char *command, const char *cons)
781 struct init_action *new_action, *a, *last; 780 struct init_action *new_action, *a, *last;
782 781
783 if (*cons == '\0') 782 if (*cons == '\0')
784 cons = console; 783 cons = console_name;
785 784
786 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST)) 785 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
787 return; 786 return;
@@ -792,8 +791,9 @@ static void new_init_action(int action, const char *command, const char *cons)
792 for (a = last = init_action_list; a; a = a->next) { 791 for (a = last = init_action_list; a; a = a->next) {
793 /* don't enter action if it's already in the list, 792 /* don't enter action if it's already in the list,
794 * but do overwrite existing actions */ 793 * but do overwrite existing actions */
795 if ((strcmp(a->command, command) == 0) && 794 if ((strcmp(a->command, command) == 0)
796 (strcmp(a->terminal, cons) == 0)) { 795 && (strcmp(a->terminal, cons) == 0)
796 ) {
797 a->action = action; 797 a->action = action;
798 free(new_action); 798 free(new_action);
799 return; 799 return;
@@ -808,7 +808,7 @@ static void new_init_action(int action, const char *command, const char *cons)
808 strcpy(new_action->command, command); 808 strcpy(new_action->command, command);
809 new_action->action = action; 809 new_action->action = action;
810 strcpy(new_action->terminal, cons); 810 strcpy(new_action->terminal, cons);
811 messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n", 811 messageD(L_LOG | L_CONSOLE, "command='%s' action='%d' terminal='%s'\n",
812 new_action->command, new_action->action, new_action->terminal); 812 new_action->command, new_action->action, new_action->terminal);
813} 813}
814 814
@@ -890,7 +890,7 @@ static void parse_inittab(void)
890 /* Separate the ID field from the runlevels */ 890 /* Separate the ID field from the runlevels */
891 runlev = strchr(id, ':'); 891 runlev = strchr(id, ':');
892 if (runlev == NULL || *(runlev + 1) == '\0') { 892 if (runlev == NULL || *(runlev + 1) == '\0') {
893 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead); 893 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
894 continue; 894 continue;
895 } else { 895 } else {
896 *runlev = '\0'; 896 *runlev = '\0';
@@ -900,7 +900,7 @@ static void parse_inittab(void)
900 /* Separate the runlevels from the action */ 900 /* Separate the runlevels from the action */
901 action = strchr(runlev, ':'); 901 action = strchr(runlev, ':');
902 if (action == NULL || *(action + 1) == '\0') { 902 if (action == NULL || *(action + 1) == '\0') {
903 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead); 903 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
904 continue; 904 continue;
905 } else { 905 } else {
906 *action = '\0'; 906 *action = '\0';
@@ -910,7 +910,7 @@ static void parse_inittab(void)
910 /* Separate the action from the command */ 910 /* Separate the action from the command */
911 command = strchr(action, ':'); 911 command = strchr(action, ':');
912 if (command == NULL || *(command + 1) == '\0') { 912 if (command == NULL || *(command + 1) == '\0') {
913 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead); 913 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
914 continue; 914 continue;
915 } else { 915 } else {
916 *command = '\0'; 916 *command = '\0';
@@ -934,7 +934,7 @@ static void parse_inittab(void)
934 } 934 }
935 if (a->name == 0) { 935 if (a->name == 0) {
936 /* Choke on an unknown action */ 936 /* Choke on an unknown action */
937 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead); 937 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
938 } 938 }
939 } 939 }
940 fclose(file); 940 fclose(file);
@@ -947,7 +947,7 @@ static void reload_signal(int sig ATTRIBUTE_UNUSED)
947{ 947{
948 struct init_action *a, *tmp; 948 struct init_action *a, *tmp;
949 949
950 message(LOG, "Reloading /etc/inittab"); 950 message(L_LOG, "Reloading /etc/inittab");
951 951
952 /* disable old entrys */ 952 /* disable old entrys */
953 for (a = init_action_list; a; a = a->next ) { 953 for (a = init_action_list; a; a = a->next ) {
@@ -1012,7 +1012,7 @@ int init_main(int argc, char **argv)
1012 close(1); 1012 close(1);
1013 close(2); 1013 close(2);
1014 1014
1015 if (device_open(console, O_RDWR | O_NOCTTY) == 0) { 1015 if (device_open(console_name, O_RDWR | O_NOCTTY) == 0) {
1016 set_term(); 1016 set_term();
1017 close(0); 1017 close(0);
1018 } 1018 }
@@ -1029,7 +1029,7 @@ int init_main(int argc, char **argv)
1029 if (argc > 1) setenv("RUNLEVEL", argv[1], 1); 1029 if (argc > 1) setenv("RUNLEVEL", argv[1], 1);
1030 1030
1031 /* Hello world */ 1031 /* Hello world */
1032 message(MAYBE_CONSOLE | LOG, "init started: %s", bb_msg_full_version); 1032 message(MAYBE_CONSOLE | L_LOG, "init started: %s", bb_msg_full_version);
1033 1033
1034 /* Make sure there is enough memory to do something useful. */ 1034 /* Make sure there is enough memory to do something useful. */
1035 if (ENABLE_SWAPONOFF) { 1035 if (ENABLE_SWAPONOFF) {
@@ -1038,7 +1038,7 @@ int init_main(int argc, char **argv)
1038 if (!sysinfo(&info) && 1038 if (!sysinfo(&info) &&
1039 (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024) 1039 (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024)
1040 { 1040 {
1041 message(CONSOLE,"Low memory: forcing swapon."); 1041 message(L_CONSOLE, "Low memory: forcing swapon.");
1042 /* swapon -a requires /proc typically */ 1042 /* swapon -a requires /proc typically */
1043 new_init_action(SYSINIT, "mount -t proc proc /proc", ""); 1043 new_init_action(SYSINIT, "mount -t proc proc /proc", "");
1044 /* Try to turn on swap */ 1044 /* Try to turn on swap */
@@ -1121,7 +1121,7 @@ int init_main(int argc, char **argv)
1121 /* Set the pid to 0 so that the process gets 1121 /* Set the pid to 0 so that the process gets
1122 * restarted by run_actions() */ 1122 * restarted by run_actions() */
1123 a->pid = 0; 1123 a->pid = 0;
1124 message(LOG, "Process '%s' (pid %d) exited. " 1124 message(L_LOG, "Process '%s' (pid %d) exited. "
1125 "Scheduling it for restart.", 1125 "Scheduling it for restart.",
1126 a->command, wpid); 1126 a->command, wpid);
1127 } 1127 }