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
return alias;
}
-/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
-/* NB: "mdev -s" may call us many times, do not leak memory/fds! */
+/* mknod in /dev based on a path like "/sys/block/hda/hda1"
+ * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes
+ * after NUL, but we promise to not mangle (IOW: to restore if needed)
+ * path string.
+ * NB2: "mdev -s" may call us many times, do not leak memory/fds!
+ */
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
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)
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))
) {
/* 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 */
/* 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;
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 */
-
- if (!delete && major >= 0) {
- if (ENABLE_FEATURE_MDEV_RENAME)
- unlink(device_name);
- if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
- bb_perror_msg_and_die("mknod %s", device_name);
- if (major == root_major && minor == root_minor)
- symlink(device_name, "root");
- if (ENABLE_FEATURE_MDEV_CONF) {
- chmod(device_name, mode);
- chown(device_name, ugid.uid, ugid.gid);
- }
- if (ENABLE_FEATURE_MDEV_RENAME && alias) {
- alias = build_alias(alias, device_name);
- /* move the device, and optionally
- * make a symlink to moved device node */
- if (rename(device_name, alias) == 0 && aliaslink == '>')
- symlink(alias, device_name);
- free(alias);
+ {
+ const char *node_name;
+
+ node_name = device_name;
+ if (ENABLE_FEATURE_MDEV_RENAME && 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) {
- unlink(device_name);
- /* At creation time, device might have been moved
- * and a symlink might have been created. Undo that. */
+ if (delete && major >= 0) {
+ if (ENABLE_FEATURE_MDEV_RENAME && alias) {
+ if (aliaslink == '>')
+ unlink(device_name);
+ }
+ unlink(node_name);
+ }
- if (ENABLE_FEATURE_MDEV_RENAME && alias) {
- alias = build_alias(alias, device_name);
- unlink(alias);
+ if (ENABLE_FEATURE_MDEV_RENAME)
free(alias);
- }
}
/* We found matching line.
if (ENABLE_FEATURE_MDEV_CONF)
config_close(parser);
+ free(subsystem_slash_devname);
}
/* File callback for /sys/ traversal */
strcpy(scratch, fileName);
scratch[len] = '\0';
- make_device(scratch, 0);
+ make_device(scratch, /*delete:*/ 0);
return TRUE;
}
/* 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);
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,
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
*/
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 */
}
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 mangles its parameter, use a copy */
- char *s = xstrdup(temp);
- make_device(s, 0);
- free(s);
+ else if (op == OP_add) {
+ make_device(temp, /*delete:*/ 0);
if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
if (fw)
load_firmware(fw, temp);