ifconfig: code shrink
[oweals/busybox.git] / miscutils / devfsd.c
index d8730459b318c798b7d23063e7179af3c3e08ab0..cd94869ae63f888b86ccb5690cfb4c5a21acbafb 100644 (file)
@@ -245,12 +245,10 @@ static struct config_entry_struct *last_config = 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"},
@@ -258,12 +256,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"},
@@ -277,10 +273,10 @@ 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";
+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 ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
@@ -340,7 +336,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;
@@ -375,10 +371,7 @@ int devfsd_main(int argc, char **argv)
        xchdir(mount_point);
 
        fd = xopen(".devfsd", O_RDONLY);
-
-       if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
-               bb_perror_msg_and_die("FD_CLOEXEC");
-
+       close_on_exec_on(fd);
        xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
 
        /*setup initial entries */
@@ -387,10 +380,10 @@ 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 */
@@ -509,12 +502,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';
@@ -522,9 +514,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;
@@ -562,7 +554,7 @@ 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" */
@@ -636,7 +628,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   */
 
@@ -649,11 +642,9 @@ 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;
-       xioctl(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);
@@ -1140,27 +1131,30 @@ static void signal_handler(int sig)
 
 static const char *get_variable(const char *variable, void *info)
 {
+       static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */
+
+       char hostname[STRING_LENGTH];
        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)
+               /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
                error_logger_and_die(LOG_ERR, "gethostname");
 
-               /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
-               hostname[STRING_LENGTH - 1] = '\0';
+       hostname[STRING_LENGTH - 1] = '\0';
 
        /* 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);
@@ -1600,7 +1594,7 @@ 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;
@@ -1679,7 +1673,7 @@ 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;