forgot about avn add... :(
[oweals/busybox.git] / miscutils / devfsd.c
index d8a630c043e7310b45d16d368cf97611d682a6d5..de046e818cc96ae23d4e329bb149cb54c0acc684 100644 (file)
@@ -1,3 +1,8 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
 /*
        devfsd implementation for busybox
 
@@ -57,7 +62,6 @@
 #include <stdarg.h>
 #include <string.h>
 #include <ctype.h>
-#include <sys/time.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -302,7 +306,7 @@ static void msg_logger(int pri, const char * fmt, ... )
        va_start(ap, fmt);
        ret = access ("/dev/log", F_OK);
        if (ret == 0) {
-               openlog(bb_applet_name, 0, LOG_DAEMON);
+               openlog(applet_name, 0, LOG_DAEMON);
                vsyslog( pri , fmt, ap);
                /* Man: A trailing newline is added when needed. */
                closelog();
@@ -471,12 +475,13 @@ int devfsd_main (int argc, char **argv)
        if (chdir (mount_point) != 0)
                devfsd_perror_msg_and_die(mount_point);
 
-       fd = bb_xopen (".devfsd", O_RDONLY);
+       fd = xopen (".devfsd", O_RDONLY);
 
        if (fcntl (fd, F_SETFD, FD_CLOEXEC) != 0)
                devfsd_perror_msg_and_die("FD_CLOEXEC");
 
-       do_ioctl_and_die(fd, DEVFSDIOC_GET_PROTO_REV,(int )&proto_rev);
+       if (ioctl (fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev) == -1)
+               msg_logger_and_die(LOG_ERR, "ioctl");
 
        /*setup initial entries */
     for (curr = initial_symlinks; curr->dest != NULL; ++curr)
@@ -486,9 +491,9 @@ int devfsd_main (int argc, char **argv)
 
        if ( print_version  || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev) )
        {
-               bb_printf( "%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
-                                        bb_applet_name,DEVFSD_VERSION,bb_msg_proto_rev,
-                                        DEVFSD_PROTOCOL_REVISION_DAEMON,bb_msg_proto_rev, 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);
                if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
                        bb_error_msg_and_die( "%s mismatch!",bb_msg_proto_rev);
                exit(EXIT_SUCCESS); /* -v */
@@ -504,7 +509,7 @@ int devfsd_main (int argc, char **argv)
        if (sigaction (SIGHUP, &new_action, NULL) != 0 || sigaction (SIGUSR1, &new_action, NULL) != 0 )
                devfsd_error_msg_and_die( "sigaction");
 
-       bb_printf("%s v%s  started for %s\n",bb_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);
@@ -605,23 +610,24 @@ static void process_config_line (const char *line, unsigned long *event_mask)
        char name[STRING_LENGTH];
        char * msg="";
        char *ptr;
+       int i;
 
        /* !!!! Only Uppercase Keywords in devsfd.conf */
-       const char *options[] = {       "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE", "RESTORE",
-                                                               "PERMISSIONS", "MODLOAD", "EXECUTE", "COPY", "IGNORE",
-                                                               "MKOLDCOMPAT", "MKNEWCOMPAT","RMOLDCOMPAT", "RMNEWCOMPAT", 0 };
-
-       int i;
+       static const char *const options[] = {
+               "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE",
+               "RESTORE", "PERMISSIONS", "MODLOAD", "EXECUTE",
+               "COPY", "IGNORE", "MKOLDCOMPAT", "MKNEWCOMPAT",
+               "RMOLDCOMPAT", "RMNEWCOMPAT", 0
+       };
 
        debug_msg_logger(LOG_INFO, __FUNCTION__);
 
-
        for (count = 0; count < MAX_ARGS; ++count) p[count][0] = '\0';
        num_args = sscanf (line, "%s %s %s %s %s %s %s %s %s %s",
                        when, name, what,
                        p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
 
-       i = compare_string_array(options, when );
+       i = index_in_str_array(options, when );
 
        /*"CLEAR_CONFIG"*/
        if( i == 0)
@@ -667,7 +673,7 @@ static void process_config_line (const char *line, unsigned long *event_mask)
                goto process_config_line_err;
        }
 
-       i = compare_string_array(options, what );
+       i = index_in_str_array(options, what );
 
        switch(i)
        {
@@ -677,7 +683,7 @@ static void process_config_line (const char *line, unsigned long *event_mask)
                        if ( ( ptr = strchr (p[0], '.') ) == NULL )
                        {
                                msg="UID.GID";
-                               goto process_config_line_err; /*"missing '.' in UID.GID */
+                               goto process_config_line_err; /*"missing '.' in UID.GID"*/
                        }
 
                        *ptr++ = '\0';
@@ -698,7 +704,7 @@ static void process_config_line (const char *line, unsigned long *event_mask)
                        num_args -= 3;
 
                        for (count = 0; count < num_args; ++count)
-                               new->u.execute.argv[count] = bb_xstrdup (p[count]);
+                               new->u.execute.argv[count] = xstrdup (p[count]);
 
                        new->u.execute.argv[num_args] = NULL;
                        break;
@@ -708,8 +714,8 @@ static void process_config_line (const char *line, unsigned long *event_mask)
                        if (num_args != 2)
                                goto process_config_line_err; /* missing path and function in line */
 
-                       new->u.copy.source = bb_xstrdup (p[0]);
-                       new->u.copy.destination = bb_xstrdup (p[1]);
+                       new->u.copy.source = xstrdup (p[0]);
+                       new->u.copy.destination = xstrdup (p[1]);
                        break;
                case 8: /* IGNORE */
                /* FALLTROUGH */
@@ -870,7 +876,7 @@ static void action_permissions (const struct devfsd_notify_struct *info,
 }   /*  End Function action_permissions  */
 
 static void action_modload (const struct devfsd_notify_struct *info,
-                           const struct config_entry_struct *entry)
+                           const struct config_entry_struct *entry ATTRIBUTE_UNUSED)
 /*  [SUMMARY] Load a module.
     <info> The devfs change.
     <entry> The config file entry.
@@ -992,17 +998,19 @@ static void action_compat (const struct devfsd_notify_struct *info, unsigned int
        unsigned int i;
        char rewind_;
        /* 1 to 5  "scsi/" , 6 to 9 "ide/host" */
-       const char *fmt[] = {   NULL ,
-                                                       "sg/c%db%dt%du%d",                      /* scsi/generic */
-                                                       "sd/c%db%dt%du%d",                      /* scsi/disc */
-                                                       "sr/c%db%dt%du%d",                      /* scsi/cd */
-                                                       "sd/c%db%dt%du%dp%d",           /* scsi/part */
-                                                       "st/c%db%dt%du%dm%d%c",         /* scsi/mt */
-                                                       "ide/hd/c%db%dt%du%d",          /* ide/host/disc */
-                                                       "ide/cd/c%db%dt%du%d",          /* ide/host/cd */
-                                                       "ide/hd/c%db%dt%du%dp%d",       /* ide/host/part */
-                                                       "ide/mt/c%db%dt%du%d%s",        /* ide/host/mt */
-                                                       NULL };
+       static const char *const fmt[] = {
+               NULL ,
+               "sg/c%db%dt%du%d",              /* scsi/generic */
+               "sd/c%db%dt%du%d",              /* scsi/disc */
+               "sr/c%db%dt%du%d",              /* scsi/cd */
+               "sd/c%db%dt%du%dp%d",           /* scsi/part */
+               "st/c%db%dt%du%dm%d%c",         /* scsi/mt */
+               "ide/hd/c%db%dt%du%d",          /* ide/host/disc */
+               "ide/cd/c%db%dt%du%d",          /* ide/host/cd */
+               "ide/hd/c%db%dt%du%dp%d",       /* ide/host/part */
+               "ide/mt/c%db%dt%du%d%s",        /* ide/host/mt */
+               NULL
+       };
 
        /*  First construct compatibility name  */
        switch (action)
@@ -1183,7 +1191,7 @@ do_chown:
        return (FALSE);
 }   /*  End Function copy_inode  */
 
-static void free_config ()
+static void free_config (void)
 /*  [SUMMARY] Free the configuration information.
     [RETURNS] Nothing.
 */
@@ -1305,12 +1313,12 @@ static const char *get_variable (const char *variable, void *info)
                /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
                hostname[STRING_LENGTH - 1] = '\0';
 
-       /* compare_string_array returns i>=0  */
-       i=compare_string_array(field_names, variable);
+       /* index_in_str_array returns i>=0  */
+       i=index_in_str_array(field_names, variable);
 
-       if ( i > 6 && (i > 1 && gv_info == NULL))
+       if ( i > 6 || i < 0 || (i > 1 && gv_info == NULL))
                        return (NULL);
-       if( i >= 0 || i <= 3)
+       if( i >= 0 && i <= 3)
        {
                debug_msg_logger(LOG_INFO, "%s: i=%d %s", __FUNCTION__, i ,field_names[i+7]);
                return(field_names[i+7]);
@@ -1595,20 +1603,22 @@ const char *get_old_name (const char *devname, unsigned int namelen,
        const char *pty2;
        size_t len;
        /* 1 to 5  "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
-       const char *fmt[] = {   NULL ,
-                                                       "sg%u",                 /* scsi/generic */
-                                                       NULL,                   /* scsi/disc */
-                                                       "sr%u",                 /* scsi/cd */
-                                                       NULL,                   /* scsi/part */
-                                                       "nst%u%c",              /* scsi/mt */
-                                                       "hd%c"  ,               /* ide/host/disc */
-                                                       "hd%c"  ,               /* ide/host/cd */
-                                                       "hd%c%s",               /* ide/host/part */
-                                                       "%sht%d",               /* ide/host/mt */
-                                                       "sbpcd%u",              /* sbp/ */
-                                                       "vcs%s",                /* vcc/ */
-                                                       "%cty%c%c",             /* pty/ */
-                                                       NULL };
+       static const char *const fmt[] = {
+               NULL ,
+               "sg%u",                 /* scsi/generic */
+               NULL,                   /* scsi/disc */
+               "sr%u",                 /* scsi/cd */
+               NULL,                   /* scsi/part */
+               "nst%u%c",              /* scsi/mt */
+               "hd%c"  ,               /* ide/host/disc */
+               "hd%c"  ,               /* ide/host/cd */
+               "hd%c%s",               /* ide/host/part */
+               "%sht%d",               /* ide/host/mt */
+               "sbpcd%u",              /* sbp/ */
+               "vcs%s",                /* vcc/ */
+               "%cty%c%c",             /* pty/ */
+               NULL
+       };
 
        debug_msg_logger(LOG_INFO, __FUNCTION__);
 
@@ -1704,7 +1714,7 @@ static char get_old_ide_name (unsigned int major, unsigned int minor)
                        i==IDE6_MAJOR || i==IDE7_MAJOR || i==IDE8_MAJOR ||
                        i==IDE9_MAJOR )
                {
-                       if(i==major)
+                       if((unsigned int)i==major)
                        {
                                letter=c;
                                break;
@@ -1900,7 +1910,7 @@ static const char *expand_variable (char *buffer, unsigned int length,
                for (ptr = input; isalnum (ch) || (ch == '_') || (ch == ':');ch = *++ptr)
                        /* VOID */ ;
                len = ptr - input;
-               if (len >= sizeof tmp)
+               if ((size_t)len >= sizeof tmp)
                        goto expand_variable_out;
 
                safe_memcpy (tmp, input, len);
@@ -1926,7 +1936,7 @@ static const char *expand_variable (char *buffer, unsigned int length,
        {
                /*  Must be simple variable expansion with "${var}"  */
                len = ptr - input;
-               if (len >= sizeof tmp)
+               if ((size_t)len >= sizeof tmp)
                        goto expand_variable_out;
 
                safe_memcpy (tmp, input, len);
@@ -1942,7 +1952,7 @@ static const char *expand_variable (char *buffer, unsigned int length,
        }
        /*  It's that handy "${var:-word}" expression. Check if var is defined  */
        len = ptr - input;
-       if (len >= sizeof tmp)
+       if ((size_t)len >= sizeof tmp)
                goto expand_variable_out;
 
        safe_memcpy (tmp, input, len);
@@ -1987,7 +1997,7 @@ static const char *expand_variable (char *buffer, unsigned int length,
        pointer to start of word in "${var:-word}"  */
        input += 2;
        len = ptr - input;
-       if (len >= sizeof tmp)
+       if ((size_t)len >= sizeof tmp)
                goto expand_variable_out;
 
        safe_memcpy (tmp, input, len);