3 * mdev - Mini udev for busybox
5 * Copyright 2005 Rob Landley <rob@landley.net>
6 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17 #include <sys/types.h>
20 #define DEV_PATH "/dev"
25 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
26 void make_device(char *path)
28 char *device_name, *s;
29 int major,minor,type,len,fd;
31 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
33 /* Try to read major/minor string */
35 snprintf(temp, PATH_MAX, "%s/dev", path);
36 fd = open(temp, O_RDONLY);
37 len = read(fd, temp, PATH_MAX-1);
42 /* Determine device name, type, major and minor */
44 device_name = strrchr(path, '/') + 1;
45 type = strncmp(path+5, "block/" ,6) ? S_IFCHR : S_IFBLK;
47 for(s = temp; *s; s++) {
57 /* Open config file here, look up permissions */
59 sprintf(temp, "%s/%s", DEV_PATH, device_name);
60 if(mknod(temp, DEV_MODE | type, makedev(major, minor)) && errno != EEXIST)
61 bb_perror_msg_and_die("mknod %s failed", temp);
63 /* Perform shellout here */
66 RELEASE_CONFIG_BUFFER(temp);
69 /* Recursive search of /sys/block or /sys/class. path must be a writeable
70 * buffer of size PATH_MAX containing the directory string to start at. */
72 void find_dev(char *path)
77 if(!(dir = opendir(path)))
78 bb_perror_msg_and_die("No %s",path);
81 struct dirent *entry = readdir(dir);
85 /* Skip "." and ".." (also skips hidden files, which is ok) */
87 if (entry->d_name[0]=='.') continue;
89 if (entry->d_type == DT_DIR) {
90 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
95 /* If there's a dev entry, mknod it */
97 if (strcmp(entry->d_name, "dev")) make_device(path);
103 int mdev_main(int argc, char *argv[])
106 if(argc == 2 && !strcmp(argv[1],"-s")) {
107 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
108 strcpy(temp,"/sys/block");
110 strcpy(temp,"/sys/class");
112 if(ENABLE_FEATURE_CLEAN_UP)
113 RELEASE_CONFIG_BUFFER(temp);
115 } else bb_show_usage();
118 /* hotplug support goes here */