X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=util-linux%2Fmdev.c;h=80549d1b7c98d0193f6697506ce04130177ec92b;hb=f5914992f316f8a628505067e108e7ba5a9590ba;hp=99e487532f20df41a758e5b2a832fe30fdc76587;hpb=aa0a12d5495a9cc8612e026ea5787eee4b019095;p=oweals%2Fbusybox.git diff --git a/util-linux/mdev.c b/util-linux/mdev.c index 99e487532..80549d1b7 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c @@ -101,13 +101,17 @@ static char *build_alias(char *alias, const char *device_name) 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; 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 @@ -242,10 +246,11 @@ 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); + /* mode = strtoul(tokens[2], NULL, 8); */ + bb_parse_mode(tokens[2], &mode); val = tokens[3]; /* 4th field (opt): >|=alias */ @@ -299,8 +304,12 @@ 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 @@ -313,53 +322,53 @@ static void make_device(char *path, int delete) /* 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 == 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. @@ -518,6 +527,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 @@ -530,7 +542,7 @@ int mdev_main(int argc UNUSED_PARAM, char **argv) if (!action || !env_path /*|| !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 */ @@ -557,18 +569,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); } - 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, 0); if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) { if (fw) load_firmware(fw, temp);