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;
41 char *temp = path + strlen(path);
43 /* Try to read major/minor string. Note that the kernel puts \n after
44 * the data, so we don't need to worry about null terminating the string
45 * because sscanf() will stop at the first nondigit, which \n is. We
46 * also depend on path having writeable space after it. */
49 fd = open(path, O_RDONLY);
50 len = read(fd, temp + 1, 64);
55 /* Determine device name, type, major and minor */
57 device_name = strrchr(path, '/') + 1;
58 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
59 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
61 /* If we have a config file, look up permissions for this device */
63 if (ENABLE_FEATURE_MDEV_CONF) {
64 char *conf, *pos, *end;
66 /* mmap the config file */
67 if (-1 != (fd=open(MDEV_CONF,O_RDONLY))) {
68 len = lseek(fd, 0, SEEK_END);
69 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
73 /* Loop through lines in mmaped file*/
74 for (pos=conf; pos-conf<len;) {
79 /* find end of this line */
80 for(end=pos; end-conf<len && *end!='\n'; end++)
83 /* Three fields: regex, uid:gid, mode */
84 for (field=3; field; field--) {
86 while (pos<end && isspace(*pos))
88 if (pos==end || *pos=='#')
90 for (end2=pos; end2<end && !isspace(*end2) && *end2!='#'; end2++)
94 /* Regex to match this device */
97 char *regex = strndupa(pos,end2-pos);
103 xregcomp(&match,regex,REG_EXTENDED);
104 result = regexec(&match,device_name,1,&off,0);
107 /* If not this device, skip rest of line */
108 if (result || off.rm_so || off.rm_eo!=strlen(device_name))
119 for(s=pos; s<end2 && *s!=':'; s++)
125 uid = strtoul(pos,&s2,10);
128 pass = getpwnam(strndupa(pos,s-pos));
135 gid = strtoul(s,&s2,10);
138 grp = getgrnam(strndupa(s,end2-s));
148 mode = strtoul(pos,&pos,8);
158 /* Did everything parse happily? */
159 if (field && field!=3)
160 bb_error_msg_and_die("Bad line %d",line);
173 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
174 bb_perror_msg_and_die("mknod %s failed", device_name);
176 if (major==root_major && minor==root_minor)
177 symlink(device_name, "root");
179 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
182 /* Recursive search of /sys/block or /sys/class. path must be a writeable
183 * buffer of size PATH_MAX containing the directory string to start at. */
185 static void find_dev(char *path)
188 size_t len = strlen(path);
189 struct dirent *entry;
191 if ((dir = opendir(path)) == NULL)
194 while ((entry = readdir(dir)) != NULL) {
196 /* Skip "." and ".." (also skips hidden files, which is ok) */
198 if (entry->d_name[0] == '.')
201 if (entry->d_type == DT_DIR) {
202 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
207 /* If there's a dev entry, mknod it */
209 if (!strcmp(entry->d_name, "dev")) make_device(path);
215 int mdev_main(int argc, char *argv[])
219 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 unlink(strrchr(env_path, '/') + 1);
252 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);