make it possible to have include/applets.h-esque entries in .c files
[oweals/busybox.git] / miscutils / devfsd.c
index 3ab36cc3c9535ad440b93e020b47e29471e0b28c..4ccb76d9544f621c4397069e34d1ea9f4d394af2 100644 (file)
     The postal address is:
       Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
 */
-
-//#include <sys/wait.h>
-//#include <sys/ioctl.h>
-//#include <sys/socket.h>
-#include <sys/un.h>
-#include <dirent.h>
-#include <syslog.h>
-#include <sys/sysmacros.h>
 #include "libbb.h"
 #include "xregex.h"
+#include <syslog.h>
 
+#include <sys/un.h>
+#include <sys/sysmacros.h>
 
 /* Various defines taken from linux/major.h */
 #define IDE0_MAJOR     3
 #define DEVFS_PATHLEN               1024
 /*  Never change this otherwise the binary interface will change   */
 
-struct devfsd_notify_struct
-{   /*  Use native C types to ensure same types in kernel and user space     */
-    unsigned int type;           /*  DEVFSD_NOTIFY_* value                   */
-    unsigned int mode;           /*  Mode of the inode or device entry       */
-    unsigned int major;          /*  Major number of device entry            */
-    unsigned int minor;          /*  Minor number of device entry            */
-    unsigned int uid;            /*  Uid of process, inode or device entry   */
-    unsigned int gid;            /*  Gid of process, inode or device entry   */
-    unsigned int overrun_count;  /*  Number of lost events                   */
-    unsigned int namelen;        /*  Number of characters not including '\0' */
-    /*  The device name MUST come last                                       */
-    char devname[DEVFS_PATHLEN]; /*  This will be '\0' terminated            */
+struct devfsd_notify_struct {
+       /*  Use native C types to ensure same types in kernel and user space     */
+       unsigned int type;           /*  DEVFSD_NOTIFY_* value                   */
+       unsigned int mode;           /*  Mode of the inode or device entry       */
+       unsigned int major;          /*  Major number of device entry            */
+       unsigned int minor;          /*  Minor number of device entry            */
+       unsigned int uid;            /*  Uid of process, inode or device entry   */
+       unsigned int gid;            /*  Gid of process, inode or device entry   */
+       unsigned int overrun_count;  /*  Number of lost events                   */
+       unsigned int namelen;        /*  Number of characters not including '\0' */
+       /*  The device name MUST come last                                       */
+       char devname[DEVFS_PATHLEN]; /*  This will be '\0' terminated            */
 };
 
 #define BUFFER_SIZE 16384
@@ -156,32 +151,27 @@ struct devfsd_notify_struct
 #define AC_RMNEWCOMPAT                         10
 #define AC_RESTORE                                     11
 
-struct permissions_type
-{
+struct permissions_type {
        mode_t mode;
        uid_t uid;
        gid_t gid;
 };
 
-struct execute_type
-{
+struct execute_type {
        char *argv[MAX_ARGS + 1];  /*  argv[0] must always be the programme  */
 };
 
-struct copy_type
-{
+struct copy_type {
        const char *source;
        const char *destination;
 };
 
-struct action_type
-{
+struct action_type {
        unsigned int what;
        unsigned int when;
 };
 
-struct config_entry_struct
-{
+struct config_entry_struct {
        struct action_type action;
        regex_t preg;
        union
@@ -194,8 +184,7 @@ struct config_entry_struct
        struct config_entry_struct *next;
 };
 
-struct get_variable_info
-{
+struct get_variable_info {
        const struct devfsd_notify_struct *info;
        const char *devname;
        char devpath[STRING_LENGTH];
@@ -234,11 +223,7 @@ static char get_old_ide_name(unsigned , unsigned);
 static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
 
 /* busybox functions */
-static void msg_logger(int pri, const char * fmt, ...)__attribute__((format(printf, 2, 3)));
-static void msg_logger_and_die(int pri, const char * fmt, ...)__attribute__((noreturn, format(printf, 2, 3)));
-static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag);
-static void fork_and_execute(int die, char *arg0, char **arg);
-static int get_uid_gid(int, const char *);
+static int get_uid_gid(int flag, const char *string);
 static void safe_memcpy(char * dest, const char * src, int len);
 static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
 static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
@@ -246,15 +231,13 @@ static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr
 /* Structs and vars */
 static struct config_entry_struct *first_config = NULL;
 static struct config_entry_struct *last_config = NULL;
-static const char *mount_point = NULL;
+static char *mount_point = NULL;
 static volatile int caught_signal = FALSE;
 static volatile int caught_sighup = FALSE;
-static struct initial_symlink_struct
-{
+static struct initial_symlink_struct {
        const char *dest;
        const char *name;
-} initial_symlinks[] =
-{
+} initial_symlinks[] = {
        {"/proc/self/fd", "fd"},
        {"fd/0", "stdin"},
        {"fd/1", "stdout"},
@@ -262,12 +245,10 @@ static struct initial_symlink_struct
        {NULL, NULL},
 };
 
-static struct event_type
-{
+static struct event_type {
        unsigned int type;        /*  The DEVFSD_NOTIFY_* value                  */
        const char *config_name;  /*  The name used in the config file           */
-} event_types[] =
-{
+} event_types[] = {
        {DEVFSD_NOTIFY_REGISTERED,   "REGISTER"},
        {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
        {DEVFSD_NOTIFY_ASYNC_OPEN,   "ASYNC_OPEN"},
@@ -281,88 +262,26 @@ static struct event_type
 
 /* Busybox messages */
 
-static const char * const bb_msg_proto_rev                     = "protocol revision";
-static const char * const bb_msg_bad_config            = "bad %s config file: %s";
-static const char * const bb_msg_small_buffer          = "buffer too small";
-static const char * const bb_msg_variable_not_found = "variable: %s not found";
-
-/* Busybox functions  */
-static void msg_logger(int pri, const char * fmt, ...)
-{
-       va_list ap;
-       int ret;
-
-       va_start(ap, fmt);
-       ret = access("/dev/log", F_OK);
-       if (ret == 0) {
-               openlog(applet_name, 0, LOG_DAEMON);
-               vsyslog(pri , fmt, ap);
-               /* Man: A trailing newline is added when needed. */
-               closelog();
-       }
-       /* ENABLE_DEVFSD_VERBOSE is always enabled if msg_logger is used */
-       if ((ENABLE_DEVFSD_VERBOSE && ret) || ENABLE_DEBUG) {
-               bb_error_msg(fmt, ap);
-       }
-       va_end(ap);
-}
-
-static void msg_logger_and_die(int pri, const char* fmt, ...)
-{
-       va_list ap;
-
-       va_start(ap, fmt);
-       msg_logger(pri, fmt, ap);
-       va_end(ap);
-       exit(EXIT_FAILURE);
-}
+static const char bb_msg_proto_rev[] ALIGN1          = "protocol revision";
+static const char bb_msg_bad_config[] ALIGN1         = "bad %s config file: %s";
+static const char bb_msg_small_buffer[] ALIGN1       = "buffer too small";
+static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found";
 
 /* Busybox stuff */
-#if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG)
-#define devfsd_error_msg(fmt, args...)                bb_error_msg(fmt, ## args)
-#define devfsd_perror_msg_and_die(fmt, args...)       bb_perror_msg_and_die(fmt, ## args)
-#define devfsd_error_msg_and_die(fmt, args...)        bb_error_msg_and_die(fmt, ## args)
+#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
+#define info_logger(p, fmt, args...)                 bb_info_msg(fmt, ## args)
+#define msg_logger(p, fmt, args...)                  bb_error_msg(fmt, ## args)
+#define msg_logger_and_die(p, fmt, args...)          bb_error_msg_and_die(fmt, ## args)
+#define error_logger(p, fmt, args...)                bb_perror_msg(fmt, ## args)
+#define error_logger_and_die(p, fmt, args...)        bb_perror_msg_and_die(fmt, ## args)
 #else
+#define info_logger(p, fmt, args...)
 #define msg_logger(p, fmt, args...)
-#define msg_logger_and_die(p, fmt, args...)           exit(1)
-#define devfsd_perror_msg_and_die(fmt, args...)       exit(1)
-#define devfsd_error_msg_and_die(fmt, args...)        exit(1)
-#define devfsd_error_msg(fmt, args...)
+#define msg_logger_and_die(p, fmt, args...)           exit(EXIT_FAILURE)
+#define error_logger(p, fmt, args...)
+#define error_logger_and_die(p, fmt, args...)         exit(EXIT_FAILURE)
 #endif
 
-static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag)
-{
-       if (ioctl(fd, request, event_mask_flag) == -1)
-               msg_logger_and_die(LOG_ERR, "ioctl");
-}
-
-static void fork_and_execute(int die, char *arg0, char **arg)
-{
-       switch (fork()) {
-               case 0:
-                       /*  Child  */
-                       break;
-               case -1:
-                       /*  Parent: Error  : die or return */
-                       msg_logger(LOG_ERR,(char *)bb_msg_memory_exhausted);
-                       if (die)
-                               exit(EXIT_FAILURE);
-                       return;
-               default:
-                       /*  Parent : ok : return or exit  */
-                       if (arg0 != NULL) {
-                               wait(NULL);
-                               return;
-                       }
-                       exit(EXIT_SUCCESS);
-       }
-        /* Child : if arg0 != NULL do execvp */
-       if (arg0 != NULL) {
-               BB_EXECVP(arg0, arg);
-               msg_logger_and_die(LOG_ERR, "execvp");
-       }
-}
-
 static void safe_memcpy(char *dest, const char *src, int len)
 {
        memcpy(dest , src, len);
@@ -406,7 +325,7 @@ static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr
 
 /*  Public functions follow  */
 
-int devfsd_main(int argc, char **argv);
+int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int devfsd_main(int argc, char **argv)
 {
        int print_version = FALSE;
@@ -436,21 +355,13 @@ int devfsd_main(int argc, char **argv)
                }
        }
 
-       /* strip last / from mount point, so we don't need to check for it later */
-       while (argv[1][1] != '\0' && argv[1][strlen(argv[1]) - 1] == '/')
-               argv[1][strlen(argv[1]) - 1] = '\0';
-
-       mount_point = argv[1];
+       mount_point = bb_simplify_path(argv[1]);
 
        xchdir(mount_point);
 
        fd = xopen(".devfsd", O_RDONLY);
-
-       if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
-               devfsd_perror_msg_and_die("FD_CLOEXEC");
-
-       if (ioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev) == -1)
-               msg_logger_and_die(LOG_ERR, "ioctl");
+       close_on_exec_on(fd);
+       xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
 
        /*setup initial entries */
        for (curr = initial_symlinks; curr->dest != NULL; ++curr)
@@ -458,26 +369,25 @@ int devfsd_main(int argc, char **argv)
 
        /* NB: The check for CONFIG_FILE is done in read_config_file() */
 
-       if (print_version ||(DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
+       if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
                printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
                                applet_name, DEVFSD_VERSION, bb_msg_proto_rev,
-                               DEVFSD_PROTOCOL_REVISION_DAEMON,bb_msg_proto_rev, proto_rev);
+                               DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev);
                if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
                        bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev);
                exit(EXIT_SUCCESS); /* -v */
        }
        /*  Tell kernel we are special(i.e. we get to see hidden entries)  */
-       do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
+       xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
 
+       /*  Set up SIGHUP and SIGUSR1 handlers  */
        sigemptyset(&new_action.sa_mask);
        new_action.sa_flags = 0;
-
-       /*  Set up SIGHUP and SIGUSR1 handlers  */
        new_action.sa_handler = signal_handler;
-       if (sigaction(SIGHUP, &new_action, NULL) != 0 || sigaction(SIGUSR1, &new_action, NULL) != 0)
-               devfsd_error_msg_and_die("sigaction");
+       sigaction_set(SIGHUP, &new_action);
+       sigaction_set(SIGUSR1, &new_action);
 
-       printf("%s v%s  started for %s\n",applet_name, DEVFSD_VERSION, mount_point);
+       printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point);
 
        /*  Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions  */
        umask(0);
@@ -486,12 +396,20 @@ int devfsd_main(int argc, char **argv)
        dir_operation(SERVICE, mount_point, 0, NULL);
 
        if (ENABLE_DEVFSD_FG_NP && no_polling)
-               exit(0);
+               exit(EXIT_SUCCESS);
+
+       if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
+               logmode = LOGMODE_BOTH;
+       else if (do_daemon == TRUE)
+               logmode = LOGMODE_SYSLOG;
+       /* This is the default */
+       /*else
+               logmode = LOGMODE_STDIO; */
+
        if (do_daemon) {
                /*  Release so that the child can grab it  */
-               do_ioctl_and_die(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
-               fork_and_execute(DIE, NULL, NULL);
-               setsid();        /*  Prevent hangups and become pgrp leader         */
+               xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
+               bb_daemonize_or_rexec(0, argv);
        } else if (ENABLE_DEVFSD_FG_NP) {
                setpgid(0, 0);  /*  Become process group leader                    */
        }
@@ -504,6 +422,7 @@ int devfsd_main(int argc, char **argv)
                if (do_scan)
                        dir_operation(SERVICE, mount_point, 0, NULL);
        }
+       if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
 }   /*  End Function main  */
 
 
@@ -522,24 +441,24 @@ static void read_config_file(char *path, int optional, unsigned long *event_mask
        FILE *fp;
        char buf[STRING_LENGTH];
        char *line = NULL;
+       char *p;
 
        if (stat(path, &statbuf) == 0) {
                /* Don't read 0 length files: ignored */
                /*if (statbuf.st_size == 0)
                                return;*/
                if (S_ISDIR(statbuf.st_mode)) {
-                       /* strip last / from dirname so we don't need to check for it later */
-                       while (path && path[1] != '\0' && path[strlen(path) - 1] == '/')
-                               path[strlen(path) - 1] = '\0';
-
-                       dir_operation(READ_CONFIG, path, 0, event_mask);
+                       p = bb_simplify_path(path);
+                       dir_operation(READ_CONFIG, p, 0, event_mask);
+                       free(p);
                        return;
                }
-               if ((fp = fopen(path, "r")) != NULL) {
+               fp = fopen_for_read(path);
+               if (fp != NULL) {
                        while (fgets(buf, STRING_LENGTH, fp) != NULL) {
                                /*  Skip whitespace  */
-                               for (line = buf; isspace(*line); ++line)
-                                       /*VOID*/;
+                               line = buf;
+                               line = skip_whitespace(line);
                                if (line[0] == '\0' || line[0] == '#')
                                        continue;
                                process_config_line(line, event_mask);
@@ -550,8 +469,8 @@ static void read_config_file(char *path, int optional, unsigned long *event_mask
                }
        } else {
 read_config_file_err:
-       if (optional ==  0  && errno == ENOENT)
-               msg_logger_and_die(LOG_ERR, "read config file: %s: %m", path);
+               if (optional == 0 && errno == ENOENT)
+                       error_logger_and_die(LOG_ERR, "read config file: %s", path);
        }
 }   /*  End Function read_config_file   */
 
@@ -572,12 +491,11 @@ static void process_config_line(const char *line, unsigned long *event_mask)
        int i;
 
        /* !!!! Only Uppercase Keywords in devsfd.conf */
-       static const char *const options[] = {
-               "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE",
-               "RESTORE", "PERMISSIONS", "MODLOAD", "EXECUTE",
-               "COPY", "IGNORE", "MKOLDCOMPAT", "MKNEWCOMPAT",
-               "RMOLDCOMPAT", "RMNEWCOMPAT", 0
-       };
+       static const char options[] ALIGN1 =
+               "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
+               "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
+               "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
+               "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
 
        for (count = 0; count < MAX_ARGS; ++count)
                p[count][0] = '\0';
@@ -585,9 +503,9 @@ static void process_config_line(const char *line, unsigned long *event_mask)
                        when, name, what,
                        p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
 
-       i = index_in_str_array(options, when);
+       i = index_in_strings(options, when);
 
-       /*"CLEAR_CONFIG"*/
+       /* "CLEAR_CONFIG" */
        if (i == 0) {
                free_config();
                *event_mask = 0;
@@ -600,7 +518,7 @@ static void process_config_line(const char *line, unsigned long *event_mask)
        /* "INCLUDE" & "OPTIONAL_INCLUDE" */
        if (i == 1 || i == 2) {
                st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
-               msg_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
+               info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
                read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
                return;
        }
@@ -625,13 +543,14 @@ static void process_config_line(const char *line, unsigned long *event_mask)
                goto process_config_line_err;
        }
 
-       i = index_in_str_array(options, what);
+       i = index_in_strings(options, what);
 
        switch (i) {
                case 4: /* "PERMISSIONS" */
                        new->action.what = AC_PERMISSIONS;
                        /*  Get user and group  */
-                       if ((ptr = strchr(p[0], '.')) == NULL) {
+                       ptr = strchr(p[0], '.');
+                       if (ptr == NULL) {
                                msg = "UID.GID";
                                goto process_config_line_err; /*"missing '.' in UID.GID"*/
                        }
@@ -699,7 +618,8 @@ static void process_config_line(const char *line, unsigned long *event_mask)
                last_config->next = new;
        last_config = new;
        return;
-process_config_line_err:
+
+ process_config_line_err:
        msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
 }  /*  End Function process_config_line   */
 
@@ -712,14 +632,12 @@ static int do_servicing(int fd, unsigned long event_mask)
 {
        ssize_t bytes;
        struct devfsd_notify_struct info;
-       unsigned long tmp_event_mask;
 
-       /*  Tell devfs what events we care about  */
-       tmp_event_mask = event_mask;
-       do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, tmp_event_mask);
+       /* (void*) cast is only in order to match prototype */
+       xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
        while (!caught_signal) {
                errno = 0;
-               bytes = read(fd,(char *) &info, sizeof info);
+               bytes = read(fd, (char *) &info, sizeof info);
                if (caught_signal)
                        break;      /*  Must test for this first     */
                if (errno == EINTR)
@@ -752,9 +670,9 @@ static void service_name(const struct devfsd_notify_struct *info)
                msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
 
        /*  Discard lookups on "/dev/log" and "/dev/initctl"  */
-       if (info->type == DEVFSD_NOTIFY_LOOKUP 
+       if (info->type == DEVFSD_NOTIFY_LOOKUP
                && ((info->devname[0] == 'l' && info->devname[1] == 'o'
-               && info->devname[2] == 'g' && !info->devname[3]) 
+               && info->devname[2] == 'g' && !info->devname[3])
                || (info->devname[0] == 'i' && info->devname[1] == 'n'
                && info->devname[2] == 'i' && info->devname[3] == 't'
                && info->devname[4] == 'c' && info->devname[5] == 't'
@@ -764,7 +682,7 @@ static void service_name(const struct devfsd_notify_struct *info)
 
        for (entry = first_config; entry != NULL; entry = entry->next) {
                /*  First check if action matches the type, then check if name matches */
-               if (info->type != entry->action.when 
+               if (info->type != entry->action.when
                || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
                        continue;
                for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
@@ -809,15 +727,15 @@ static void action_permissions(const struct devfsd_notify_struct *info,
 {
        struct stat statbuf;
 
-       if (stat(info->devname, &statbuf) != 0  ||
-                chmod(info->devname,(statbuf.st_mode & S_IFMT) |(entry->u.permissions.mode & ~S_IFMT)) != 0 ||
-               chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
+       if (stat(info->devname, &statbuf) != 0
+        || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
+        || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
        )
-               msg_logger(LOG_ERR, "Can't chmod or chown: %s: %m", info->devname);
+               error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
 }   /*  End Function action_permissions  */
 
 static void action_modload(const struct devfsd_notify_struct *info,
-                           const struct config_entry_struct *entry ATTRIBUTE_UNUSED)
+                           const struct config_entry_struct *entry UNUSED_PARAM)
 /*  [SUMMARY] Load a module.
     <info> The devfs change.
     <entry> The config file entry.
@@ -826,14 +744,14 @@ static void action_modload(const struct devfsd_notify_struct *info,
 {
        char *argv[6];
 
-       argv[0] =(char*)MODPROBE;
-       argv[1] =(char*)MODPROBE_SWITCH_1; /* "-k" */
-       argv[2] =(char*)MODPROBE_SWITCH_2; /* "-C" */
-       argv[3] =(char*)CONFIG_MODULES_DEVFS;
+       argv[0] = (char*)MODPROBE;
+       argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
+       argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
+       argv[3] = (char*)CONFIG_MODULES_DEVFS;
        argv[4] = concat_path_file("/dev", info->devname); /* device */
        argv[5] = NULL;
 
-       fork_and_execute(DIE, argv[0], argv);
+       spawn_and_wait(argv);
        free(argv[4]);
 }  /*  End Function action_modload  */
 
@@ -865,7 +783,7 @@ static void action_execute(const struct devfsd_notify_struct *info,
                argv[count] = largv[count];
        }
        argv[count] = NULL;
-       fork_and_execute(NO_DIE, argv[0], argv);
+       spawn_and_wait(argv);
 }   /*  End Function action_execute  */
 
 
@@ -913,7 +831,7 @@ static void action_copy(const struct devfsd_notify_struct *info,
                new_mode |= S_ISVTX;
        ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
        if (ENABLE_DEBUG && ret && (errno != EEXIST))
-               msg_logger(LOG_ERR, "copy_inode: %s to %s: %m", source, destination);
+               error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
 }   /*  End Function action_copy  */
 
 static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
@@ -1007,7 +925,7 @@ static void action_compat(const struct devfsd_notify_struct *info, unsigned int
                case AC_RMNEWCOMPAT:
                        ret = unlink(compat_name);
                        if (ENABLE_DEBUG && ret)
-                               msg_logger(LOG_ERR, "unlink: %s: %m", compat_name);
+                               error_logger(LOG_ERR, "unlink: %s", compat_name);
                        break;
                /*esac*/
        } /* switch (action) */
@@ -1022,11 +940,11 @@ static void restore(char *spath, struct stat source_stat, int rootlen)
        dpath = concat_path_file(mount_point, spath + rootlen);
        lstat(dpath, &dest_stat);
        free(dpath);
-       if (S_ISLNK(source_stat.st_mode) ||(source_stat.st_mode & S_ISVTX))
-               copy_inode(dpath, &dest_stat,(source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
+       if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
+               copy_inode(dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX), spath, &source_stat);
 
        if (S_ISDIR(source_stat.st_mode))
-               dir_operation(RESTORE, spath, rootlen,NULL);
+               dir_operation(RESTORE, spath, rootlen, NULL);
 }
 
 
@@ -1051,8 +969,9 @@ static int copy_inode(const char *destpath, const struct stat *dest_stat,
        if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
                /*  Same type  */
                if (S_ISLNK(source_stat->st_mode)) {
-                       if ((source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1)) < 0
-                               || (dest_len   = readlink(destpath  , dest_link  , STRING_LENGTH - 1)) < 0
+                       source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1);
+                       if ((source_len < 0)
+                        || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0
                        )
                                return FALSE;
                        source_link[source_len] = '\0';
@@ -1071,24 +990,27 @@ static int copy_inode(const char *destpath, const struct stat *dest_stat,
        unlink(destpath);
        switch (source_stat->st_mode & S_IFMT) {
                case S_IFSOCK:
-                       if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
+                       fd = socket(AF_UNIX, SOCK_STREAM, 0);
+                       if (fd < 0)
                                break;
                        un_addr.sun_family = AF_UNIX;
                        snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);
-                       val = bind(fd,(struct sockaddr *) &un_addr,(int) sizeof un_addr);
+                       val = bind(fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
                        close(fd);
                        if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0)
                                break;
                        goto do_chown;
                case S_IFLNK:
-                       if ((val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1)) < 0)
+                       val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1);
+                       if (val < 0)
                                break;
                        symlink_val[val] = '\0';
                        if (symlink(symlink_val, destpath) == 0)
                                return TRUE;
                        break;
                case S_IFREG:
-                       if ((fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT)) < 0)
+                       fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT);
+                       if (fd < 0)
                                break;
                        close(fd);
                        if (chmod(destpath, new_mode & ~S_IFMT) != 0)
@@ -1154,7 +1076,7 @@ static int get_uid_gid(int flag, const char *string)
        if (isdigit(string[0]) ||((string[0] == '-') && isdigit(string[1])))
                return atoi(string);
 
-       if (flag == UID && (pw_ent  = getpwnam(string)) != NULL)
+       if (flag == UID && (pw_ent = getpwnam(string)) != NULL)
                return pw_ent->pw_uid;
 
        if (flag == GID && (grp_ent = getgrnam(string)) != NULL)
@@ -1163,7 +1085,7 @@ static int get_uid_gid(int flag, const char *string)
                msg = "group";
 
        if (ENABLE_DEVFSD_VERBOSE)
-               msg_logger(LOG_ERR,"unknown %s: %s, defaulting to %cid=0",  msg, string, msg[0]);
+               msg_logger(LOG_ERR, "unknown %s: %s, defaulting to %cid=0",  msg, string, msg[0]);
        return 0;
 }/*  End Function get_uid_gid  */
 
@@ -1198,32 +1120,31 @@ static void signal_handler(int sig)
        if (sig == SIGHUP)
                caught_sighup = TRUE;
 
-       msg_logger(LOG_INFO, "Caught signal %d", sig);
+       info_logger(LOG_INFO, "Caught signal %d", sig);
 }   /*  End Function signal_handler  */
 
 static const char *get_variable(const char *variable, void *info)
 {
+       static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */
+       static char *hostname;
+
        struct get_variable_info *gv_info = info;
-       static char hostname[STRING_LENGTH], sbuf[STRING_LENGTH];
-       const char *field_names[] = { "hostname", "mntpt", "devpath", "devname",
-                                                                  "uid", "gid", "mode", hostname, mount_point,
-                                                                  gv_info->devpath, gv_info->devname, 0 };
+       const char *field_names[] = {
+                       "hostname", "mntpt", "devpath", "devname",
+                       "uid", "gid", "mode", hostname, mount_point,
+                       gv_info->devpath, gv_info->devname, NULL
+       };
        int i;
 
-       if (gethostname(hostname, STRING_LENGTH - 1) != 0)
-               msg_logger_and_die(LOG_ERR, "gethostname: %m");
-
-               /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
-               hostname[STRING_LENGTH - 1] = '\0';
-
+       if (!hostname)
+               hostname = safe_gethostname();
        /* index_in_str_array returns i>=0  */
        i = index_in_str_array(field_names, variable);
 
        if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
-                       return NULL;
-       if (i >= 0 && i <= 3) {
+               return NULL;
+       if (i >= 0 && i <= 3)
                return field_names[i + 7];
-       }
 
        if (i == 4)
                sprintf(sbuf, "%u", gv_info->info->uid);
@@ -1266,7 +1187,8 @@ static void dir_operation(int type, const char * dir_name, int var, unsigned lon
        struct dirent *de;
        char *path;
 
-       if ((dp = warn_opendir(dir_name)) == NULL)
+       dp = warn_opendir(dir_name);
+       if (dp == NULL)
                return;
 
        while ((de = readdir(dp)) != NULL) {
@@ -1408,8 +1330,7 @@ static void expand_regexp(char *output, size_t outsize, const char *input,
 
 /* from compat_name.c */
 
-struct translate_struct
-{
+struct translate_struct {
        const char *match;    /*  The string to match to(up to length)                */
        const char *format;   /*  Format of output, "%s" takes data past match string,
                        NULL is effectively "%s"(just more efficient)       */
@@ -1511,7 +1432,7 @@ const char *get_old_name(const char *devname, unsigned int namelen,
 
        /* 2 ==scsi/disc, 4 == scsi/part */
        if (i == 2 || i == 4)
-               compat_name = write_old_sd_name(buffer, major, minor,((i == 2) ? "" : (ptr + 4)));
+               compat_name = write_old_sd_name(buffer, major, minor, ((i == 2) ? "" : (ptr + 4)));
 
        /* 5 == scsi/mt */
        if (i == 5) {
@@ -1650,8 +1571,9 @@ int st_expr_expand(char *output, unsigned int length, const char *input,
                                ch = input[1];
                                if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
                                        /* User's own home directory: leave separator for next time */
-                                       if ((env = getenv("HOME")) == NULL) {
-                                               msg_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
+                                       env = getenv("HOME");
+                                       if (env == NULL) {
+                                               info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
                                                return FALSE;
                                        }
                                        len = strlen(env);
@@ -1663,14 +1585,15 @@ int st_expr_expand(char *output, unsigned int length, const char *input,
                                }
                                /*  Someone else's home directory  */
                                for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
-                                       /* VOID */ ;
+                                       /* VOID */;
                                len = ptr - input;
                                if (len >= sizeof tmp)
                                        goto st_expr_expand_out;
                                safe_memcpy(tmp, input, len);
                                input = ptr - 1;
-                               if ((pwent = getpwnam(tmp)) == NULL) {
-                                       msg_logger(LOG_INFO, "no pwent for: %s", tmp);
+                               pwent = getpwnam(tmp);
+                               if (pwent == NULL) {
+                                       info_logger(LOG_INFO, "no pwent for: %s", tmp);
                                        return FALSE;
                                }
                                len = strlen(pwent->pw_dir);
@@ -1695,7 +1618,7 @@ int st_expr_expand(char *output, unsigned int length, const char *input,
        }
        return FALSE;
 st_expr_expand_out:
-       msg_logger(LOG_INFO, bb_msg_small_buffer);
+       info_logger(LOG_INFO, bb_msg_small_buffer);
        return FALSE;
 }   /*  End Function st_expr_expand  */
 
@@ -1729,7 +1652,7 @@ static const char *expand_variable(char *buffer, unsigned int length,
        ch = input[0];
        if (ch == '$') {
                /*  Special case for "$$": PID  */
-               sprintf(tmp, "%d",(int) getpid());
+               sprintf(tmp, "%d", (int) getpid());
                len = strlen(tmp);
                if (len + *out_pos >= length)
                        goto expand_variable_out;
@@ -1742,15 +1665,16 @@ static const char *expand_variable(char *buffer, unsigned int length,
        if (ch != '{') {
                /*  Simple variable expansion  */
                for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
-                       /* VOID */ ;
+                       /* VOID */;
                len = ptr - input;
                if ((size_t)len >= sizeof tmp)
                        goto expand_variable_out;
 
                safe_memcpy(tmp, input, len);
                input = ptr - 1;
-               if ((env = get_variable_v2(tmp, func, info)) == NULL) {
-                       msg_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
+               env = get_variable_v2(tmp, func, info);
+               if (env == NULL) {
+                       info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
                        return NULL;
                }
                len = strlen(env);
@@ -1778,7 +1702,7 @@ static const char *expand_variable(char *buffer, unsigned int length,
                return input + len;
        }
        if (ch != ':' || ptr[1] != '-') {
-               msg_logger(LOG_INFO, "illegal char in var name");
+               info_logger(LOG_INFO, "illegal char in var name");
                return NULL;
        }
        /*  It's that handy "${var:-word}" expression. Check if var is defined  */
@@ -1801,7 +1725,7 @@ static const char *expand_variable(char *buffer, unsigned int length,
                                --open_braces;
                                break;
                        case '\0':
-                               msg_logger(LOG_INFO,"\"}\" not found in: %s", input);
+                               info_logger(LOG_INFO, "\"}\" not found in: %s", input);
                                return NULL;
                        default:
                                break;
@@ -1809,7 +1733,8 @@ static const char *expand_variable(char *buffer, unsigned int length,
        }
        --ptr;
        /*  At this point ptr should point to closing brace of "${var:-word}"  */
-       if ((env = get_variable_v2(tmp, func, info)) != NULL) {
+       env = get_variable_v2(tmp, func, info);
+       if (env != NULL) {
                /*  Found environment variable, so skip the input to the closing brace
                        and return the variable  */
                input = ptr;
@@ -1840,7 +1765,7 @@ static const char *expand_variable(char *buffer, unsigned int length,
        *out_pos += len;
        return input;
 expand_variable_out:
-       msg_logger(LOG_INFO, bb_msg_small_buffer);
+       info_logger(LOG_INFO, bb_msg_small_buffer);
        return NULL;
 }   /*  End Function expand_variable  */