init: stop using static data
[oweals/busybox.git] / init / init.c
index 6f3374eaca658aedb41c0f0b861451e35e181084..cac165fc776bb2d9fafcba49835e2c936a4f91a4 100644 (file)
 #define DEBUG_SEGV_HANDLER 0
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include <syslog.h>
 #include <sys/resource.h>
 #ifdef __linux__
  */
 #define RESTART     0x80
 
-
 /* A linked list of init_actions, to be read from inittab */
 struct init_action {
        struct init_action *next;
@@ -213,11 +213,17 @@ struct init_action {
        char command[1];
 };
 
-static struct init_action *init_action_list = NULL;
-
+struct globals {
+       struct init_action *init_action_list;
 #if !ENABLE_FEATURE_INIT_SYSLOG
-static const char *log_console = VC_5;
+       const char *log_console;
 #endif
+} FIX_ALIASING;
+#define G (*(struct globals*)bb_common_bufsiz1)
+#define INIT_G() do { \
+       setup_common_bufsiz(); \
+       IF_NOT_FEATURE_INIT_SYSLOG(G.log_console = VC_5;) \
+} while (0)
 
 enum {
        L_LOG = 0x1,
@@ -265,10 +271,10 @@ static void message(int where, const char *fmt, ...)
 
                if (log_fd < 0) {
                        log_fd = STDERR_FILENO;
-                       if (log_console) {
-                               log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
+                       if (G.log_console) {
+                               log_fd = device_open(G.log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
                                if (log_fd < 0) {
-                                       bb_error_msg("can't log to %s", log_console);
+                                       bb_error_msg("can't log to %s", G.log_console);
                                        where = L_CONSOLE;
                                } else {
                                        close_on_exec_on(log_fd);
@@ -328,7 +334,7 @@ static void console_init(void)
                if (!s || strcmp(s, "linux") == 0)
                        putenv((char*)"TERM=vt102");
 # if !ENABLE_FEATURE_INIT_SYSLOG
-               log_console = NULL;
+               G.log_console = NULL;
 # endif
        } else
 #endif
@@ -562,7 +568,7 @@ static struct init_action *mark_terminated(pid_t pid)
 
        if (pid > 0) {
                update_utmp_DEAD_PROCESS(pid);
-               for (a = init_action_list; a; a = a->next) {
+               for (a = G.init_action_list; a; a = a->next) {
                        if (a->pid == pid) {
                                a->pid = 0;
                                return a;
@@ -596,7 +602,7 @@ static void run_actions(int action_type)
 {
        struct init_action *a;
 
-       for (a = init_action_list; a; a = a->next) {
+       for (a = G.init_action_list; a; a = a->next) {
                if (!(a->action_type & action_type))
                        continue;
 
@@ -630,7 +636,7 @@ static void new_init_action(uint8_t action_type, const char *command, const char
         * To achieve that, if we find a matching entry, we move it
         * to the end.
         */
-       nextp = &init_action_list;
+       nextp = &G.init_action_list;
        while ((a = *nextp) != NULL) {
                /* Don't enter action if it's already in the list.
                 * This prevents losing running RESPAWNs.
@@ -845,7 +851,7 @@ static void exec_restart_action(void)
 {
        struct init_action *a;
 
-       for (a = init_action_list; a; a = a->next) {
+       for (a = G.init_action_list; a; a = a->next) {
                if (!(a->action_type & RESTART))
                        continue;
 
@@ -923,7 +929,7 @@ static void reload_inittab(void)
        message(L_LOG, "reloading /etc/inittab");
 
        /* Disable old entries */
-       for (a = init_action_list; a; a = a->next)
+       for (a = G.init_action_list; a; a = a->next)
                a->action_type = 0;
 
        /* Append new entries, or modify existing entries
@@ -936,14 +942,14 @@ static void reload_inittab(void)
 #if ENABLE_FEATURE_KILL_REMOVED
        /* Kill stale entries */
        /* Be nice and send SIGTERM first */
-       for (a = init_action_list; a; a = a->next)
+       for (a = G.init_action_list; a; a = a->next)
                if (a->action_type == 0 && a->pid != 0)
                        kill(a->pid, SIGTERM);
        if (CONFIG_FEATURE_KILL_DELAY) {
                /* NB: parent will wait in NOMMU case */
                if ((BB_MMU ? fork() : vfork()) == 0) { /* child */
                        sleep(CONFIG_FEATURE_KILL_DELAY);
-                       for (a = init_action_list; a; a = a->next)
+                       for (a = G.init_action_list; a; a = a->next)
                                if (a->action_type == 0 && a->pid != 0)
                                        kill(a->pid, SIGKILL);
                        _exit(EXIT_SUCCESS);
@@ -955,7 +961,7 @@ static void reload_inittab(void)
         * We never rerun SYSINIT entries anyway,
         * removing them too saves a few bytes
         */
-       nextp = &init_action_list;
+       nextp = &G.init_action_list;
        while ((a = *nextp) != NULL) {
                /*
                 * Why pid == 0 check?
@@ -1046,6 +1052,8 @@ static void sleep_much(void)
 int init_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int init_main(int argc UNUSED_PARAM, char **argv)
 {
+       INIT_G();
+
        if (argv[1] && strcmp(argv[1], "-q") == 0) {
                return kill(1, SIGHUP);
        }