3 * mdev - Mini udev for busybox
5 * Copyright 2005, 2007 Rob Landley <rob@landley.net>
6 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
10USE_MDEV(NEWTOY(mdev, "s", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_UMASK))
18 Create devices in /dev using information from /sys.
20 -s Scan all entries in /sys to populate /dev.
23 bool "Configuration file for mdev"
27 The mdev config file (/etc/mdev.conf) contains lines that look like:
30 Each line must contain three whitespace separated fields. The first
31 field is a regular expression matching one or more device names, and
32 the second and third fields are uid:gid and file permissions for
37#include "lib/xregcomp.h"
39// mknod in /dev based on a path like "/sys/block/hda/hda1"
40static void make_device(char *path)
42 char *device_name, *s, *temp;
43 int major, minor, type, len, fd;
48 // Try to read major/minor string
50 temp = path+strlen(path);
52 fd = open(path, O_RDONLY);
55 len = read(fd, temp, 64);
60 // Determine device name, type, major and minor
62 device_name = strrchr(path, '/') + 1;
63 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
65 sscanf(temp, "%u:%u", &major, &minor);
67 // If we have a config file, look up permissions for this device
70 char *conf, *pos, *end;
72 // mmap the config file
73 if (-1!=(fd = open("/etc/mdev.conf", O_RDONLY))) {
74 len = lseek(fd, 0, SEEK_END);
75 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
79 // Loop through lines in mmaped file
80 for (pos = conf; pos-conf<len;) {
85 // find end of this line
86 for(end = pos; end-conf<len && *end!='\n'; end++);
88 // Three fields: regex, uid:gid, mode
89 for (field = 3; field; field--) {
91 while (pos<end && isspace(*pos)) pos++;
92 if (pos==end || *pos=='#') break;
94 end2<end && !isspace(*end2) && *end2!='#'; end2++);
96 // Regex to match this device
99 char *regex = strndupa(pos, end2-pos);
105 xregcomp(&match, regex, REG_EXTENDED);
106 result=regexec(&match, device_name, 1, &off, 0);
109 // If not this device, skip rest of line
110 if (result || off.rm_so
111 || off.rm_eo!=strlen(device_name))
122 for(s = pos; s<end2 && *s!=':'; s++);
123 if (s==end2) goto end_line;
126 uid = strtoul(pos,&s2,10);
129 pass = getpwnam(strndupa(pos, s-pos));
130 if (!pass) goto end_line;
135 gid = strtoul(s,&s2,10);
138 grp = getgrnam(strndupa(s, end2-s));
139 if (!grp) goto end_line;
147 mode = strtoul(pos, &pos, 8);
148 if (pos!=end2) goto end_line;
155 // Did everything parse happily?
156 if (field && field!=3) error_exit("Bad line %d", line);
168 sprintf(temp, "/dev/%s", device_name);
169 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
170 perror_exit("mknod %s failed", temp);
172 if (CFG_MDEV_CONF) chown(temp, uid, gid);
175// Recursive search of /sys/block or /sys/class. path must be a writeable
176// buffer of size PATH_MAX containing the directory string to start at.
178static void find_dev(char *path)
181 int len=strlen(path);
183 if (!(dir = opendir(path)))
184 perror_exit("No %s",path);
187 struct dirent *entry = readdir(dir);
191 // Skip "." and ".." (also skips hidden files, which is ok)
193 if (entry->d_name[0]=='.') continue;
195 if (entry->d_type == DT_DIR) {
196 snprintf(path+len, sizeof(toybuf)-len, "/%s", entry->d_name);
201 // If there's a dev entry, mknod it
203 if (strcmp(entry->d_name, "dev")) make_device(path);
212 strcpy(toybuf, "/sys/block");
214 strcpy(toybuf, "/sys/class");
219 // hotplug support goes here