init: code readability enhancements; very small code changes

This commit is contained in:
Denis Vlasenko 2007-02-17 15:52:02 +00:00
parent c84520d73d
commit ec27feb045
5 changed files with 62 additions and 65 deletions

View File

@ -38,7 +38,7 @@ int setconsole_main(int argc, char **argv)
device = argv[optind]; device = argv[optind];
} else { } else {
if (flags & OPT_SETCONS_RESET) if (flags & OPT_SETCONS_RESET)
device = CONSOLE_DEV; device = DEV_CONSOLE;
} }
if (-1 == ioctl(xopen(device, O_RDONLY), TIOCCONS)) { if (-1 == ioctl(xopen(device, O_RDONLY), TIOCCONS)) {

View File

@ -846,7 +846,7 @@ extern const char bb_default_login_shell[];
/* The following devices are the same on devfs and non-devfs systems. */ /* The following devices are the same on devfs and non-devfs systems. */
#define CURRENT_TTY "/dev/tty" #define CURRENT_TTY "/dev/tty"
#define CONSOLE_DEV "/dev/console" #define DEV_CONSOLE "/dev/console"
#ifndef RB_POWER_OFF #ifndef RB_POWER_OFF

View File

@ -10,7 +10,6 @@
*/ */
#include "busybox.h" #include "busybox.h"
#include <errno.h>
#include <paths.h> #include <paths.h>
#include <signal.h> #include <signal.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
@ -119,7 +118,7 @@ struct init_action {
/* Static variables */ /* Static variables */
static struct init_action *init_action_list = NULL; static struct init_action *init_action_list = NULL;
static char console[CONSOLE_BUFF_SIZE] = CONSOLE_DEV; static char console_name[CONSOLE_BUFF_SIZE] = DEV_CONSOLE;
#if !ENABLE_SYSLOGD #if !ENABLE_SYSLOGD
static const char *log_console = VC_5; static const char *log_console = VC_5;
@ -129,13 +128,13 @@ static sig_atomic_t got_cont = 0;
#endif #endif
enum { enum {
LOG = 0x1, L_LOG = 0x1,
CONSOLE = 0x2, L_CONSOLE = 0x2,
#if ENABLE_FEATURE_EXTRA_QUIET #if ENABLE_FEATURE_EXTRA_QUIET
MAYBE_CONSOLE = 0x0, MAYBE_CONSOLE = 0x0,
#else #else
MAYBE_CONSOLE = CONSOLE, MAYBE_CONSOLE = L_CONSOLE,
#endif #endif
#ifndef RB_HALT_SYSTEM #ifndef RB_HALT_SYSTEM
@ -171,7 +170,7 @@ static void loop_forever(void)
#endif #endif
/* Print a message to the specified device. /* Print a message to the specified device.
* Device may be bitwise-or'd from LOG | CONSOLE */ * Device may be bitwise-or'd from L_LOG | L_CONSOLE */
#if ENABLE_DEBUG_INIT #if ENABLE_DEBUG_INIT
#define messageD message #define messageD message
#else #else
@ -195,7 +194,7 @@ static void message(int device, const char *fmt, ...)
#if ENABLE_SYSLOGD #if ENABLE_SYSLOGD
/* Log the message to syslogd */ /* Log the message to syslogd */
if (device & LOG) { if (device & L_LOG) {
/* don`t out "\r\n" */ /* don`t out "\r\n" */
openlog(applet_name, 0, LOG_DAEMON); openlog(applet_name, 0, LOG_DAEMON);
syslog(LOG_INFO, "%s", msg + 1); syslog(LOG_INFO, "%s", msg + 1);
@ -211,21 +210,22 @@ static void message(int device, const char *fmt, ...)
/* Take full control of the log tty, and never close it. /* Take full control of the log tty, and never close it.
* It's mine, all mine! Muhahahaha! */ * It's mine, all mine! Muhahahaha! */
if (log_fd < 0) { if (log_fd < 0) {
if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) { log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
if (log_fd < 0) {
log_fd = -2; log_fd = -2;
bb_error_msg("bummer, can't write to log on %s!", log_console); bb_error_msg("bummer, can't log to %s!", log_console);
device = CONSOLE; device = L_CONSOLE;
} else { } else {
fcntl(log_fd, F_SETFD, FD_CLOEXEC); fcntl(log_fd, F_SETFD, FD_CLOEXEC);
} }
} }
if ((device & LOG) && (log_fd >= 0)) { if (device & L_LOG) {
full_write(log_fd, msg, l); full_write(log_fd, msg, l);
} }
#endif #endif
if (device & CONSOLE) { if (device & L_CONSOLE) {
int fd = device_open(CONSOLE_DEV, int fd = device_open(DEV_CONSOLE,
O_WRONLY | O_NOCTTY | O_NONBLOCK); O_WRONLY | O_NOCTTY | O_NONBLOCK);
/* Always send console messages to /dev/console so people will see them. */ /* Always send console messages to /dev/console so people will see them. */
if (fd >= 0) { if (fd >= 0) {
@ -291,26 +291,25 @@ static void console_init(void)
char *s; char *s;
if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) { if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
safe_strncpy(console, s, sizeof(console)); safe_strncpy(console_name, s, sizeof(console_name));
} else { } else {
/* 2.2 kernels: identify the real console backend and try to use it */ /* 2.2 kernels: identify the real console backend and try to use it */
if (ioctl(0, TIOCGSERIAL, &sr) == 0) { if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
/* this is a serial console */ /* this is a serial console */
snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line); snprintf(console_name, sizeof(console_name) - 1, SC_FORMAT, sr.line);
} else if (ioctl(0, VT_GETSTATE, &vt) == 0) { } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
/* this is linux virtual tty */ /* this is linux virtual tty */
snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active); snprintf(console_name, sizeof(console_name) - 1, VC_FORMAT, vt.v_active);
} else { } else {
safe_strncpy(console, CONSOLE_DEV, sizeof(console)); strcpy(console_name, DEV_CONSOLE);
tried++; tried++;
} }
} }
while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) { while ((fd = open(console_name, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
/* Can't open selected console -- try /* Can't open selected console -- try
logical system console and VT_MASTER */ logical system console and VT_MASTER */
safe_strncpy(console, (tried == 0 ? CONSOLE_DEV : CURRENT_VC), strcpy(console_name, (tried == 0 ? DEV_CONSOLE : CURRENT_VC));
sizeof(console));
tried++; tried++;
} }
if (fd < 0) { if (fd < 0) {
@ -318,7 +317,7 @@ static void console_init(void)
#if !ENABLE_SYSLOGD #if !ENABLE_SYSLOGD
log_console = log_console =
#endif #endif
safe_strncpy(console, bb_dev_null, sizeof(console)); strcpy(console_name, bb_dev_null);
} else { } else {
s = getenv("TERM"); s = getenv("TERM");
/* check for serial console */ /* check for serial console */
@ -328,7 +327,7 @@ static void console_init(void)
if (s == NULL || strcmp(s, "linux") == 0) if (s == NULL || strcmp(s, "linux") == 0)
putenv((char*)"TERM=vt102"); putenv((char*)"TERM=vt102");
#if !ENABLE_SYSLOGD #if !ENABLE_SYSLOGD
log_console = console; log_console = console_name;
#endif #endif
} else { } else {
if (s == NULL) if (s == NULL)
@ -336,7 +335,7 @@ static void console_init(void)
} }
close(fd); close(fd);
} }
messageD(LOG, "console=%s", console); messageD(L_LOG, "console=%s", console_name);
} }
static void fixup_argv(int argc, char **argv, const char *new_argv0) static 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)
} }
/* Open the new terminal device */ /* Open the new terminal device */
static void open_new_terminal(const char * const device, const int fail) { static void open_new_terminal(const char* device, int fail) {
struct stat sb; struct stat sb;
if ((device_open(device, O_RDWR)) < 0) { if ((device_open(device, O_RDWR)) < 0) {
if (stat(device, &sb) != 0) { if (stat(device, &sb) != 0) {
message(LOG | CONSOLE, "device '%s' does not exist.", device); message(L_LOG | L_CONSOLE, "device '%s' does not exist", device);
} else { } else {
message(LOG | CONSOLE, "Bummer, can't open %s", device); message(L_LOG | L_CONSOLE, "Bummer, can't open %s", device);
} }
if (fail) if (fail)
_exit(1); _exit(1);
@ -434,7 +433,7 @@ static pid_t run(const struct init_action *a)
/* Now fork off another process to just hang around */ /* Now fork off another process to just hang around */
if ((pid = fork()) < 0) { if ((pid = fork()) < 0) {
message(LOG | CONSOLE, "Can't fork!"); message(L_LOG | L_CONSOLE, "Can't fork!");
_exit(1); _exit(1);
} }
@ -453,7 +452,7 @@ static pid_t run(const struct init_action *a)
/* Use a temporary process to steal the controlling tty. */ /* Use a temporary process to steal the controlling tty. */
if ((pid = fork()) < 0) { if ((pid = fork()) < 0) {
message(LOG | CONSOLE, "Can't fork!"); message(L_LOG | L_CONSOLE, "Can't fork!");
_exit(1); _exit(1);
} }
if (pid == 0) { if (pid == 0) {
@ -506,7 +505,7 @@ static pid_t run(const struct init_action *a)
/* make a new argv[0] */ /* make a new argv[0] */
if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) { if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
message(LOG | CONSOLE, bb_msg_memory_exhausted); message(L_LOG | L_CONSOLE, bb_msg_memory_exhausted);
cmd[0] = cmdpath; cmd[0] = cmdpath;
} else { } else {
cmd[0][0] = '-'; cmd[0][0] = '-';
@ -532,7 +531,7 @@ static pid_t run(const struct init_action *a)
* be allowed to start a shell or whatever an init script * be allowed to start a shell or whatever an init script
* specifies. * specifies.
*/ */
messageD(LOG, "Waiting for enter to start '%s'" messageD(L_LOG, "Waiting for enter to start '%s'"
"(pid %d, terminal %s)\n", "(pid %d, terminal %s)\n",
cmdpath, getpid(), a->terminal); cmdpath, getpid(), a->terminal);
full_write(1, press_enter, sizeof(press_enter) - 1); full_write(1, press_enter, sizeof(press_enter) - 1);
@ -542,7 +541,7 @@ static pid_t run(const struct init_action *a)
#endif #endif
/* Log the process name and args */ /* Log the process name and args */
message(LOG, "Starting pid %d, console %s: '%s'", message(L_LOG, "Starting pid %d, console %s: '%s'",
getpid(), a->terminal, cmdpath); getpid(), a->terminal, cmdpath);
#if ENABLE_FEATURE_INIT_COREDUMPS #if ENABLE_FEATURE_INIT_COREDUMPS
@ -563,7 +562,7 @@ static pid_t run(const struct init_action *a)
BB_EXECVP(cmdpath, cmd); BB_EXECVP(cmdpath, cmd);
/* We're still here? Some error happened. */ /* We're still here? Some error happened. */
message(LOG | CONSOLE, "Bummer, cannot run '%s': %m", cmdpath); message(L_LOG | L_CONSOLE, "Bummer, cannot run '%s': %m", cmdpath);
_exit(-1); _exit(-1);
} }
sigprocmask(SIG_SETMASK, &omask, NULL); sigprocmask(SIG_SETMASK, &omask, NULL);
@ -658,16 +657,16 @@ static void shutdown_system(void)
/* Allow Ctrl-Alt-Del to reboot system. */ /* Allow Ctrl-Alt-Del to reboot system. */
init_reboot(RB_ENABLE_CAD); init_reboot(RB_ENABLE_CAD);
message(CONSOLE | LOG, "The system is going down NOW !!"); message(L_CONSOLE | L_LOG, "The system is going down NOW !!");
sync(); sync();
/* Send signals to every process _except_ pid 1 */ /* Send signals to every process _except_ pid 1 */
message(CONSOLE | LOG, init_sending_format, "TERM"); message(L_CONSOLE | L_LOG, init_sending_format, "TERM");
kill(-1, SIGTERM); kill(-1, SIGTERM);
sleep(1); sleep(1);
sync(); sync();
message(CONSOLE | LOG, init_sending_format, "KILL"); message(L_CONSOLE | L_LOG, init_sending_format, "KILL");
kill(-1, SIGKILL); kill(-1, SIGKILL);
sleep(1); sleep(1);
@ -712,10 +711,10 @@ static void exec_signal(int sig ATTRIBUTE_UNUSED)
dup(0); dup(0);
dup(0); dup(0);
messageD(CONSOLE | LOG, "Trying to re-exec %s", a->command); messageD(L_CONSOLE | L_LOG, "Trying to re-exec %s", a->command);
BB_EXECLP(a->command, a->command, NULL); BB_EXECLP(a->command, a->command, NULL);
message(CONSOLE | LOG, "exec of '%s' failed: %m", message(L_CONSOLE | L_LOG, "exec of '%s' failed: %m",
a->command); a->command);
sync(); sync();
sleep(2); sleep(2);
@ -741,7 +740,7 @@ static void shutdown_signal(int sig)
m = "poweroff"; m = "poweroff";
rb = RB_POWER_OFF; rb = RB_POWER_OFF;
} }
message(CONSOLE | LOG, "Requesting system %s.", m); message(L_CONSOLE | L_LOG, "Requesting system %s", m);
sync(); sync();
/* allow time for last message to reach serial console */ /* 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)
struct init_action *new_action, *a, *last; struct init_action *new_action, *a, *last;
if (*cons == '\0') if (*cons == '\0')
cons = console; cons = console_name;
if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST)) if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
return; return;
@ -792,8 +791,9 @@ static void new_init_action(int action, const char *command, const char *cons)
for (a = last = init_action_list; a; a = a->next) { for (a = last = init_action_list; a; a = a->next) {
/* don't enter action if it's already in the list, /* don't enter action if it's already in the list,
* but do overwrite existing actions */ * but do overwrite existing actions */
if ((strcmp(a->command, command) == 0) && if ((strcmp(a->command, command) == 0)
(strcmp(a->terminal, cons) == 0)) { && (strcmp(a->terminal, cons) == 0)
) {
a->action = action; a->action = action;
free(new_action); free(new_action);
return; return;
@ -808,7 +808,7 @@ static void new_init_action(int action, const char *command, const char *cons)
strcpy(new_action->command, command); strcpy(new_action->command, command);
new_action->action = action; new_action->action = action;
strcpy(new_action->terminal, cons); strcpy(new_action->terminal, cons);
messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n", messageD(L_LOG | L_CONSOLE, "command='%s' action='%d' terminal='%s'\n",
new_action->command, new_action->action, new_action->terminal); new_action->command, new_action->action, new_action->terminal);
} }
@ -890,7 +890,7 @@ static void parse_inittab(void)
/* Separate the ID field from the runlevels */ /* Separate the ID field from the runlevels */
runlev = strchr(id, ':'); runlev = strchr(id, ':');
if (runlev == NULL || *(runlev + 1) == '\0') { if (runlev == NULL || *(runlev + 1) == '\0') {
message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead); message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
continue; continue;
} else { } else {
*runlev = '\0'; *runlev = '\0';
@ -900,7 +900,7 @@ static void parse_inittab(void)
/* Separate the runlevels from the action */ /* Separate the runlevels from the action */
action = strchr(runlev, ':'); action = strchr(runlev, ':');
if (action == NULL || *(action + 1) == '\0') { if (action == NULL || *(action + 1) == '\0') {
message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead); message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
continue; continue;
} else { } else {
*action = '\0'; *action = '\0';
@ -910,7 +910,7 @@ static void parse_inittab(void)
/* Separate the action from the command */ /* Separate the action from the command */
command = strchr(action, ':'); command = strchr(action, ':');
if (command == NULL || *(command + 1) == '\0') { if (command == NULL || *(command + 1) == '\0') {
message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead); message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
continue; continue;
} else { } else {
*command = '\0'; *command = '\0';
@ -934,7 +934,7 @@ static void parse_inittab(void)
} }
if (a->name == 0) { if (a->name == 0) {
/* Choke on an unknown action */ /* Choke on an unknown action */
message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead); message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
} }
} }
fclose(file); fclose(file);
@ -947,7 +947,7 @@ static void reload_signal(int sig ATTRIBUTE_UNUSED)
{ {
struct init_action *a, *tmp; struct init_action *a, *tmp;
message(LOG, "Reloading /etc/inittab"); message(L_LOG, "Reloading /etc/inittab");
/* disable old entrys */ /* disable old entrys */
for (a = init_action_list; a; a = a->next ) { for (a = init_action_list; a; a = a->next ) {
@ -1012,7 +1012,7 @@ int init_main(int argc, char **argv)
close(1); close(1);
close(2); close(2);
if (device_open(console, O_RDWR | O_NOCTTY) == 0) { if (device_open(console_name, O_RDWR | O_NOCTTY) == 0) {
set_term(); set_term();
close(0); close(0);
} }
@ -1029,7 +1029,7 @@ int init_main(int argc, char **argv)
if (argc > 1) setenv("RUNLEVEL", argv[1], 1); if (argc > 1) setenv("RUNLEVEL", argv[1], 1);
/* Hello world */ /* Hello world */
message(MAYBE_CONSOLE | LOG, "init started: %s", bb_msg_full_version); message(MAYBE_CONSOLE | L_LOG, "init started: %s", bb_msg_full_version);
/* Make sure there is enough memory to do something useful. */ /* Make sure there is enough memory to do something useful. */
if (ENABLE_SWAPONOFF) { if (ENABLE_SWAPONOFF) {
@ -1038,7 +1038,7 @@ int init_main(int argc, char **argv)
if (!sysinfo(&info) && if (!sysinfo(&info) &&
(info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024) (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024)
{ {
message(CONSOLE,"Low memory: forcing swapon."); message(L_CONSOLE, "Low memory: forcing swapon.");
/* swapon -a requires /proc typically */ /* swapon -a requires /proc typically */
new_init_action(SYSINIT, "mount -t proc proc /proc", ""); new_init_action(SYSINIT, "mount -t proc proc /proc", "");
/* Try to turn on swap */ /* Try to turn on swap */
@ -1121,7 +1121,7 @@ int init_main(int argc, char **argv)
/* Set the pid to 0 so that the process gets /* Set the pid to 0 so that the process gets
* restarted by run_actions() */ * restarted by run_actions() */
a->pid = 0; a->pid = 0;
message(LOG, "Process '%s' (pid %d) exited. " message(L_LOG, "Process '%s' (pid %d) exited. "
"Scheduling it for restart.", "Scheduling it for restart.",
a->command, wpid); a->command, wpid);
} }

View File

@ -8,15 +8,10 @@
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/ */
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include "libbb.h" #include "libbb.h"
/* From <linux/kd.h> */ /* From <linux/kd.h> */
enum { KDGKBTYPE = 0x4B33 }; /* get keyboard type */ enum { KDGKBTYPE = 0x4B33 }; /* get keyboard type */
@ -47,18 +42,18 @@ static int open_a_console(const char *fnam)
int get_console_fd(void) int get_console_fd(void)
{ {
int fd; static const char *const console_names[] = {
DEV_CONSOLE, CURRENT_VC, CURRENT_TTY
static const char * const choise_console_names[] = {
CONSOLE_DEV, CURRENT_VC, CURRENT_TTY
}; };
int fd;
for (fd = 2; fd >= 0; fd--) { for (fd = 2; fd >= 0; fd--) {
int fd4name; int fd4name;
int choise_fd; int choise_fd;
char arg; char arg;
fd4name = open_a_console(choise_console_names[fd]); fd4name = open_a_console(console_names[fd]);
chk_std: chk_std:
choise_fd = (fd4name >= 0 ? fd4name : fd); choise_fd = (fd4name >= 0 ? fd4name : fd);

View File

@ -57,8 +57,10 @@ int more_main(int argc, char **argv)
/* use input from terminal unless we do "more >outfile" */ /* use input from terminal unless we do "more >outfile" */
if (isatty(STDOUT_FILENO)) { if (isatty(STDOUT_FILENO)) {
cin = fopen(CURRENT_TTY, "r"); cin = fopen(CURRENT_TTY, "r");
/* Huh? why not just fail if "/dev/tty" isn't available?
* If user has no ctty, it's his own problem */
if (!cin) if (!cin)
cin = xfopen(CONSOLE_DEV, "r"); cin = xfopen(DEV_CONSOLE, "r");
please_display_more_prompt = 2; please_display_more_prompt = 2;
#if ENABLE_FEATURE_USE_TERMIOS #if ENABLE_FEATURE_USE_TERMIOS
cin_fileno = fileno(cin); cin_fileno = fileno(cin);