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.
19 #include <sys/sysmacros.h>
20 #include <sys/types.h>
26 #define DEV_PATH "/dev"
27 #define MDEV_CONF "/etc/mdev.conf"
31 int root_major, root_minor;
33 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
34 static void make_device(char *path)
36 char *device_name, *s;
37 int major, minor, type, len, fd;
42 RESERVE_CONFIG_BUFFER(temp, PATH_MAX);
44 /* Try to read major/minor string */
46 snprintf(temp, PATH_MAX, "%s/dev", path);
47 fd = open(temp, O_RDONLY);
48 len = read(fd, temp, PATH_MAX-1);
50 if (len < 1) goto end;
52 /* Determine device name, type, major and minor */
54 device_name = strrchr(path, '/') + 1;
55 type = strncmp(path+5, "block/", 6) ? S_IFCHR : S_IFBLK;
56 if (sscanf(temp, "%d:%d", &major, &minor) != 2)
59 /* If we have a config file, look up permissions for this device */
61 if (ENABLE_FEATURE_MDEV_CONF) {
62 char *conf, *pos, *end;
64 /* mmap the config file */
65 if (-1 != (fd=open(MDEV_CONF,O_RDONLY))) {
66 len = lseek(fd, 0, SEEK_END);
67 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
71 /* Loop through lines in mmaped file*/
72 for (pos=conf; pos-conf<len;) {
77 /* find end of this line */
78 for(end=pos; end-conf<len && *end!='\n'; end++)
81 /* Three fields: regex, uid:gid, mode */
82 for (field=3; field; field--) {
84 while (pos<end && isspace(*pos))
86 if (pos==end || *pos=='#')
88 for (end2=pos; end2<end && !isspace(*end2) && *end2!='#'; end2++)
92 /* Regex to match this device */
95 char *regex = strndupa(pos,end2-pos);
101 xregcomp(&match,regex,REG_EXTENDED);
102 result = regexec(&match,device_name,1,&off,0);
105 /* If not this device, skip rest of line */
106 if (result || off.rm_so || off.rm_eo!=strlen(device_name))
117 for(s=pos; s<end2 && *s!=':'; s++)
123 uid = strtoul(pos,&s2,10);
126 pass = getpwnam(strndupa(pos,s-pos));
133 gid = strtoul(s,&s2,10);
136 grp = getgrnam(strndupa(s,end2-s));
146 mode = strtoul(pos,&pos,8);
156 /* Did everything parse happily? */
157 if (field && field!=3)
158 bb_error_msg_and_die("Bad line %d",line);
170 sprintf(temp, "%s/%s", DEV_PATH, device_name);
172 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
173 bb_perror_msg_and_die("mknod %s failed", temp);
175 if (major==root_major && minor==root_minor)
176 symlink(temp,DEV_PATH "/root");
178 if (ENABLE_FEATURE_MDEV_CONF) chown(temp,uid,gid);
181 RELEASE_CONFIG_BUFFER(temp);
184 /* Recursive search of /sys/block or /sys/class. path must be a writeable
185 * buffer of size PATH_MAX containing the directory string to start at. */
187 static void find_dev(char *path)
190 size_t len = strlen(path);
191 struct dirent *entry;
193 if ((dir = opendir(path)) == NULL)
196 while ((entry = readdir(dir)) != NULL) {
198 /* Skip "." and ".." (also skips hidden files, which is ok) */
200 if (entry->d_name[0] == '.')
203 if (entry->d_type == DT_DIR) {
204 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
209 /* If there's a dev entry, mknod it */
211 if (!strcmp(entry->d_name, "dev")) make_device(path);
217 int mdev_main(int argc, char *argv[])
221 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
225 if (argc == 2 && !strcmp(argv[1],"-s")) {
228 stat("/", &st); // If this fails, we have bigger problems.
229 root_major=major(st.st_dev);
230 root_minor=minor(st.st_dev);
231 strcpy(temp,"/sys/block");
233 strcpy(temp,"/sys/class");
239 action = getenv("ACTION");
240 env_path = getenv("DEVPATH");
241 if (!action || !env_path)
244 if (!strcmp(action, "add")) {
245 sprintf(temp, "/sys%s", env_path);
247 } else if (!strcmp(action, "remove")) {
248 sprintf(temp, "%s/%s", DEV_PATH, strrchr(env_path, '/') + 1);
253 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);