return s;
}
+/* Builds an alias path.
+ * This function potentionally reallocates the alias parameter.
+ */
+static char *build_alias(char *alias, const char *device_name)
+{
+ char *dest;
+
+ /* ">bar/": rename to bar/device_name */
+ /* ">bar[/]baz": rename to bar[/]baz */
+ dest = strrchr(alias, '/');
+ if (dest) { /* ">bar/[baz]" ? */
+ *dest = '\0'; /* mkdir bar */
+ bb_make_directory(alias, 0755, FILEUTILS_RECUR);
+ *dest = '/';
+ if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
+ dest = alias;
+ alias = concat_path_file(alias, device_name);
+ free(dest);
+ }
+ }
+
+ 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! */
static void make_device(char *path, int delete)
chown(device_name, uid, gid);
if (ENABLE_FEATURE_MDEV_RENAME && alias) {
- char *dest;
-
- /* ">bar/": rename to bar/device_name */
- /* ">bar[/]baz": rename to bar[/]baz */
- dest = strrchr(alias, '/');
- if (dest) { /* ">bar/[baz]" ? */
- *dest = '\0'; /* mkdir bar */
- bb_make_directory(alias, 0755, FILEUTILS_RECUR);
- *dest = '/';
- if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
- dest = alias;
- alias = concat_path_file(alias, device_name);
- free(dest);
- }
- }
+ alias = build_alias(alias, device_name);
/* move the device, and optionally
* make a symlink to moved device node */
free(command);
}
- if (delete)
+ if (delete) {
unlink(device_name);
+ /* At creation time, device might have been moved
+ * and a symlink might have been created. Undo that. */
+ if (ENABLE_FEATURE_MDEV_RENAME && alias) {
+ alias = build_alias(alias, device_name);
+ unlink(alias);
+ free(alias);
+ }
+ }
}
/* File callback for /sys/ traversal */
char *scratch = userData;
/* len check is for paranoid reasons */
- if (strcmp(fileName + len, "/dev") || len >= PATH_MAX)
+ if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
return FALSE;
strcpy(scratch, fileName);
}
int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int mdev_main(int argc, char **argv)
+int mdev_main(int argc UNUSED_PARAM, char **argv)
{
char *action;
char *env_path;
xchdir("/dev");
- if (argc == 2 && !strcmp(argv[1], "-s")) {
+ if (argv[1] && !strcmp(argv[1], "-s")) {
/* Scan:
* mdev -s
*/