losetup: use LOOP_foo defines
[oweals/busybox.git] / util-linux / mdev.c
index c68c0a5e70569a7fdd019bc0188b39e61543db6d..b4042c07ecaeda7bbabc2a9ad5c4707d36a13caf 100644 (file)
 struct globals {
        int root_major, root_minor;
        char *subsystem;
-};
+} FIX_ALIASING;
 #define G (*(struct globals*)&bb_common_bufsiz1)
-#define root_major (G.root_major)
-#define root_minor (G.root_minor)
-#define subsystem  (G.subsystem )
 
 /* Prevent infinite loops in /sys symlinks */
 #define MAX_SYSFS_DEPTH 3
@@ -109,9 +106,9 @@ static char *build_alias(char *alias, const char *device_name)
  */
 static void make_device(char *path, int delete)
 {
-       char *device_name;
+       char *device_name, *subsystem_slash_devname;
        int major, minor, type, len;
-       int mode;
+       mode_t mode;
        parser_t *parser;
 
        /* Try to read major/minor string.  Note that the kernel puts \n after
@@ -140,17 +137,29 @@ static void make_device(char *path, int delete)
        device_name = (char*) bb_basename(path);
        /* http://kernel.org/doc/pending/hotplug.txt says that only
         * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
-        * But since 2.6.25 block devices are also in /sys/class/block,
-        * we use strstr("/block/") to forestall future surprises. */
+        * But since 2.6.25 block devices are also in /sys/class/block.
+        * We use strstr("/block/") to forestall future surprises. */
        type = S_IFCHR;
-       if (strstr(path, "/block/"))
+       if (strstr(path, "/block/") || (G.subsystem && strncmp(G.subsystem, "block", 5) == 0))
                type = S_IFBLK;
 
        /* Make path point to "subsystem/device_name" */
-       if (path[5] == 'b') /* legacy /sys/block? */
+       subsystem_slash_devname = NULL;
+       /* Check for coldplug invocations first */
+       if (strncmp(path, "/sys/block/", 11) == 0) /* legacy case */
                path += sizeof("/sys/") - 1;
-       else
+       else if (strncmp(path, "/sys/class/", 11) == 0)
                path += sizeof("/sys/class/") - 1;
+       else {
+               /* Example of a hotplug invocation:
+                * SUBSYSTEM="block"
+                * DEVPATH="/sys" + "/devices/virtual/mtd/mtd3/mtdblock3"
+                * ("/sys" is added by mdev_main)
+                * - path does not contain subsystem
+                */
+               subsystem_slash_devname = concat_path_file(G.subsystem, device_name);
+               path = subsystem_slash_devname;
+       }
 
        /* If we have config file, look up user settings */
        if (ENABLE_FEATURE_MDEV_CONF)
@@ -195,7 +204,8 @@ static void make_device(char *path, int delete)
                                if (major < 0)
                                        continue; /* no dev, no match */
                                sc = sscanf(val, "@%u,%u-%u", &cmaj, &cmin0, &cmin1);
-                               if (sc < 1 || major != cmaj
+                               if (sc < 1
+                                || major != cmaj
                                 || (sc == 2 && minor != cmin0)
                                 || (sc == 3 && (minor < cmin0 || minor > cmin1))
                                ) {
@@ -234,7 +244,8 @@ static void make_device(char *path, int delete)
 
                                /* If no match, skip rest of line */
                                /* (regexec returns whole pattern as "range" 0) */
-                               if (result || off[0].rm_so
+                               if (result
+                                || off[0].rm_so
                                 || ((int)off[0].rm_eo != (int)strlen(str_to_match))
                                ) {
                                        continue; /* this line doesn't match */
@@ -246,30 +257,37 @@ static void make_device(char *path, int delete)
 
                        /* 2nd field: uid:gid - device ownership */
                        if (get_uidgid(&ugid, tokens[1], 1) == 0)
-                               bb_error_msg("unknown user/group %s", tokens[1]);
+                               bb_error_msg("unknown user/group %s on line %d", tokens[1], parser->lineno);
 
                        /* 3rd field: mode - device permissions */
-                       mode = strtoul(tokens[2], NULL, 8);
+                       bb_parse_mode(tokens[2], &mode);
 
                        val = tokens[3];
-                       /* 4th field (opt): >|=alias */
+                       /* 4th field (opt): ">|=alias" or "!" to not create the node */
 
                        if (ENABLE_FEATURE_MDEV_RENAME && val) {
-                               aliaslink = val[0];
-                               if (aliaslink == '>' || aliaslink == '=') {
-                                       char *a, *s, *st;
-                                       char *p;
-                                       unsigned i, n;
-
-                                       a = val;
-                                       s = strchrnul(val, ' ');
-                                       st = strchrnul(val, '\t');
-                                       if (st < s)
-                                               s = st;
-                                       val = (s[0] && s[1]) ? s+1 : NULL;
+                               char *a, *s, *st;
+
+                               a = val;
+                               s = strchrnul(val, ' ');
+                               st = strchrnul(val, '\t');
+                               if (st < s)
+                                       s = st;
+                               st = (s[0] && s[1]) ? s+1 : NULL;
+
+                               aliaslink = a[0];
+                               if (aliaslink == '!' && s == a+1) {
+                                       val = st;
+                                       /* "!": suppress node creation/deletion */
+                                       major = -1;
+                               }
+                               else if (aliaslink == '>' || aliaslink == '=') {
+                                       val = st;
                                        s[0] = '\0';
-
                                        if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) {
+                                               char *p;
+                                               unsigned i, n;
+
                                                /* substitute %1..9 with off[1..9], if any */
                                                n = 0;
                                                s = a;
@@ -303,63 +321,72 @@ static void make_device(char *path, int delete)
                                const char *s = "$@*";
                                const char *s2 = strchr(s, val[0]);
 
-                               if (!s2)
-                                       bb_error_msg_and_die("bad line %u", parser->lineno);
+                               if (!s2) {
+                                       bb_error_msg("bad line %u", parser->lineno);
+                                       if (ENABLE_FEATURE_MDEV_RENAME)
+                                               free(alias);
+                                       continue;
+                               }
 
                                /* Are we running this command now?
                                 * Run $cmd on delete, @cmd on create, *cmd on both
                                 */
-                               if (s2-s != delete)
+                               if (s2 - s != delete) {
+                                       /* We are here if: '*',
+                                        * or: '@' and delete = 0,
+                                        * or: '$' and delete = 1
+                                        */
                                        command = xstrdup(val + 1);
+                               }
                        }
                }
 
                /* End of field parsing */
 
                /* "Execute" the line we found */
+               {
+                       const char *node_name;
 
-               if (!delete && major >= 0) {
-                       char *node_name = (char *)device_name;
+                       node_name = device_name;
                        if (ENABLE_FEATURE_MDEV_RENAME && alias)
-                               alias = node_name = build_alias(alias, device_name);
-                       if (mknod(node_name, mode | type, makedev(major, minor)) && errno != EEXIST)
-                               bb_perror_msg_and_die("mknod %s", node_name);
-                       if (major == root_major && minor == root_minor)
-                               symlink(node_name, "root");
-                       if (ENABLE_FEATURE_MDEV_CONF) {
-                               chmod(node_name, mode);
-                               chown(node_name, ugid.uid, ugid.gid);
-                       }
-                       if (ENABLE_FEATURE_MDEV_RENAME && alias) {
-                               if (aliaslink == '>')
-                                       symlink(node_name, device_name);
-                               free(alias);
+                               node_name = alias = build_alias(alias, device_name);
+
+                       if (!delete && major >= 0) {
+                               if (mknod(node_name, mode | type, makedev(major, minor)) && errno != EEXIST)
+                                       bb_perror_msg("can't create '%s'", node_name);
+                               if (major == G.root_major && minor == G.root_minor)
+                                       symlink(node_name, "root");
+                               if (ENABLE_FEATURE_MDEV_CONF) {
+                                       chmod(node_name, mode);
+                                       chown(node_name, ugid.uid, ugid.gid);
+                               }
+                               if (ENABLE_FEATURE_MDEV_RENAME && alias) {
+                                       if (aliaslink == '>')
+                                               symlink(node_name, device_name);
+                               }
                        }
-               }
 
-               if (ENABLE_FEATURE_MDEV_EXEC && command) {
-                       /* setenv will leak memory, use putenv/unsetenv/free */
-                       char *s = xasprintf("%s=%s", "MDEV", device_name);
-                       char *s1 = xasprintf("%s=%s", "SUBSYSTEM", subsystem);
-                       putenv(s);
-                       putenv(s1);
-                       if (system(command) == -1)
-                               bb_perror_msg_and_die("can't run '%s'", command);
-                       unsetenv("SUBSYSTEM");
-                       free(s1);
-                       unsetenv("MDEV");
-                       free(s);
-                       free(command);
-               }
+                       if (ENABLE_FEATURE_MDEV_EXEC && command) {
+                               /* setenv will leak memory, use putenv/unsetenv/free */
+                               char *s = xasprintf("%s=%s", "MDEV", node_name);
+                               char *s1 = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem);
+                               putenv(s);
+                               putenv(s1);
+                               if (system(command) == -1)
+                                       bb_perror_msg("can't run '%s'", command);
+                               bb_unsetenv_and_free(s1);
+                               bb_unsetenv_and_free(s);
+                               free(command);
+                       }
 
-               if (delete) {
-                       char *node_name = (char *)device_name;
-                       if (ENABLE_FEATURE_MDEV_RENAME && alias) {
-                               alias = node_name = build_alias(alias, device_name);
-                               if (aliaslink == '>')
-                                       unlink(device_name);
+                       if (delete && major >= 0) {
+                               if (ENABLE_FEATURE_MDEV_RENAME && alias) {
+                                       if (aliaslink == '>')
+                                               unlink(device_name);
+                               }
+                               unlink(node_name);
                        }
-                       unlink(node_name);
+
                        if (ENABLE_FEATURE_MDEV_RENAME)
                                free(alias);
                }
@@ -374,6 +401,7 @@ static void make_device(char *path, int delete)
 
        if (ENABLE_FEATURE_MDEV_CONF)
                config_close(parser);
+       free(subsystem_slash_devname);
 }
 
 /* File callback for /sys/ traversal */
@@ -391,7 +419,7 @@ static int FAST_FUNC fileAction(const char *fileName,
 
        strcpy(scratch, fileName);
        scratch[len] = '\0';
-       make_device(scratch, 0);
+       make_device(scratch, /*delete:*/ 0);
 
        return TRUE;
 }
@@ -405,10 +433,10 @@ static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
        /* Extract device subsystem -- the name of the directory
         * under /sys/class/ */
        if (1 == depth) {
-               free(subsystem);
-               subsystem = strrchr(fileName, '/');
-               if (subsystem)
-                       subsystem = xstrdup(subsystem + 1);
+               free(G.subsystem);
+               G.subsystem = strrchr(fileName, '/');
+               if (G.subsystem)
+                       G.subsystem = xstrdup(G.subsystem + 1);
        }
 
        return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
@@ -493,8 +521,8 @@ int mdev_main(int argc UNUSED_PARAM, char **argv)
                struct stat st;
 
                xstat("/", &st);
-               root_major = major(st.st_dev);
-               root_minor = minor(st.st_dev);
+               G.root_major = major(st.st_dev);
+               G.root_minor = minor(st.st_dev);
 
                /* ACTION_FOLLOWLINKS is needed since in newer kernels
                 * /sys/block/loop* (for example) are symlinks to dirs,
@@ -520,6 +548,9 @@ int mdev_main(int argc UNUSED_PARAM, char **argv)
                char *seq;
                char *action;
                char *env_path;
+               static const char keywords[] ALIGN1 = "remove\0add\0";
+               enum { OP_remove = 0, OP_add };
+               smalluint op;
 
                /* Hotplug:
                 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
@@ -528,11 +559,11 @@ int mdev_main(int argc UNUSED_PARAM, char **argv)
                 */
                action = getenv("ACTION");
                env_path = getenv("DEVPATH");
-               subsystem = getenv("SUBSYSTEM");
-               if (!action || !env_path /*|| !subsystem*/)
+               G.subsystem = getenv("SUBSYSTEM");
+               if (!action || !env_path /*|| !G.subsystem*/)
                        bb_show_usage();
                fw = getenv("FIRMWARE");
-
+               op = index_in_strings(keywords, action);
                /* If it exists, does /dev/mdev.seq match $SEQNUM?
                 * If it does not match, earlier mdev is running
                 * in parallel, and we need to wait */
@@ -559,15 +590,15 @@ int mdev_main(int argc UNUSED_PARAM, char **argv)
                }
 
                snprintf(temp, PATH_MAX, "/sys%s", env_path);
-               if (strcmp(action, "remove") == 0) {
+               if (op == OP_remove) {
                        /* Ignoring "remove firmware". It was reported
                         * to happen and to cause erroneous deletion
                         * of device nodes. */
                        if (!fw)
-                               make_device(temp, 1);
+                               make_device(temp, /*delete:*/ 1);
                }
-               else if (strcmp(action, "add") == 0) {
-                       make_device(temp, 0);
+               else if (op == OP_add) {
+                       make_device(temp, /*delete:*/ 0);
                        if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
                                if (fw)
                                        load_firmware(fw, temp);