* contains "4:0\n". Directory name is taken as device name, path component
* directly after /sys/class/ as subsystem. In this example, "tty0" and "tty".
* Then mdev creates the /dev/device_name node.
+ * If /sys/class/.../dev file does not exist, mdev still may act
+ * on this device: see "@|$|*command args..." parameter in config file.
*
* mdev w/o parameters is called as hotplug helper. It takes device
* and subsystem names from $DEVPATH and $SUBSYSTEM, extracts
*
* [-][subsystem/]device user:grp mode [>|=path] [@|$|*command args...]
* [-]@maj,min[-min2] user:grp mode [>|=path] [@|$|*command args...]
- *
- * The device name or "subsystem/device" combo is matched against 1st field
- * (which is a regex), or maj,min is matched against 1st field.
+ * [-]$envvar=val user:grp mode [>|=path] [@|$|*command args...]
*
* Leading minus in 1st field means "don't stop on this line", otherwise
* search is stopped after the matching line is encountered.
*
- * When line matches, the device node is created, chmod'ed and chown'ed.
- * Then it moved to path, and if >path, a symlink to moved node is created
+ * The device name or "subsystem/device" combo is matched against 1st field
+ * (which is a regex), or maj,min is matched against 1st field,
+ * or specified environment variable (as regex) is matched against 1st field.
+ *
+ * $envvar=val format is useful for loading modules for hot-plugged devices
+ * which do not have driver loaded yet. In this case /sys/class/.../dev
+ * does not exist, but $MODALIAS is set to needed module's name
+ * (actually, an alias to it) by kernel. This rule instructs mdev
+ * to load the module and exit:
+ * $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"
+ * The kernel will generate another hotplug event when /sys/class/.../dev
+ * file appears.
+ *
+ * When line matches, the device node is created, chmod'ed and chown'ed,
+ * moved to path, and if >path, a symlink to moved node is created,
+ * all this if /sys/class/.../dev exists.
* Examples:
* =loop/ - moves to /dev/loop
* >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink
- * Then "command args" is executed (via sh -c 'command args').
+ *
+ * Then "command args..." is executed (via sh -c 'command args...').
* @:execute on creation, $:on deletion, *:on both.
+ * This happens regardless of /sys/class/.../dev existence.
*/
struct globals {
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;
* the rest the line unless keep_matching == 1 */
/* 2nd field: uid:gid - device ownership */
- parse_chown_usergroup_or_die(&ugid, tokens[1]);
+ if (get_uidgid(&ugid, tokens[1], 1) == 0)
+ bb_error_msg("unknown user/group %s", tokens[1]);
/* 3rd field: mode - device permissions */
mode = strtoul(tokens[2], NULL, 8);
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
/* 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_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);
+ }
}
- }
- 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", subsystem);
+ putenv(s);
+ putenv(s1);
+ if (system(command) == -1)
+ bb_perror_msg("can't run '%s'", command);
+ unsetenv("SUBSYSTEM");
+ free(s1);
+ unsetenv("MDEV");
+ 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) {
+ 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.