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"
33 int root_major, root_minor;
36 #define bbg mdev_globals
38 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
39 static void make_device(char *path)
41 char *device_name, *s;
42 int major, minor, type, len, fd;
46 char *temp = path + strlen(path);
48 /* Try to read major/minor string. Note that the kernel puts \n after
49 * the data, so we don't need to worry about null terminating the string
50 * because sscanf() will stop at the first nondigit, which \n is. We
51 * also depend on path having writeable space after it. */
54 fd = open(path, O_RDONLY);
55 len = read(fd, temp + 1, 64);
60 /* Determine device name, type, major and minor */
62 device_name = strrchr(path, '/') + 1;
63 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
64 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
66 /* If we have a config file, look up permissions for this device */
68 if (ENABLE_FEATURE_MDEV_CONF) {
69 char *conf, *pos, *end;
71 /* mmap the config file */
72 if (-1 != (fd=open(MDEV_CONF,O_RDONLY))) {
73 len = lseek(fd, 0, SEEK_END);
74 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
78 /* Loop through lines in mmaped file*/
79 for (pos=conf; pos-conf<len;) {
84 /* find end of this line */
85 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))
93 if (pos==end || *pos=='#')
95 for (end2=pos; end2<end && !isspace(*end2) && *end2!='#'; end2++)
99 /* Regex to match this device */
102 char *regex = strndupa(pos,end2-pos);
108 xregcomp(&match,regex,REG_EXTENDED);
109 result = regexec(&match,device_name,1,&off,0);
112 /* If not this device, skip rest of line */
113 if (result || off.rm_so || off.rm_eo!=strlen(device_name))
124 for(s=pos; s<end2 && *s!=':'; s++)
130 uid = strtoul(pos,&s2,10);
133 pass = getpwnam(strndupa(pos,s-pos));
140 gid = strtoul(s,&s2,10);
143 grp = getgrnam(strndupa(s,end2-s));
153 mode = strtoul(pos,&pos,8);
163 /* Did everything parse happily? */
164 if (field && field!=3)
165 bb_error_msg_and_die("Bad line %d",line);
178 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
179 bb_perror_msg_and_die("mknod %s failed", device_name);
181 if (major==bbg.root_major && minor==bbg.root_minor)
182 symlink(device_name, "root");
184 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
187 /* Recursive search of /sys/block or /sys/class. path must be a writeable
188 * buffer of size PATH_MAX containing the directory string to start at. */
190 static void find_dev(char *path)
193 size_t len = strlen(path);
194 struct dirent *entry;
196 if ((dir = opendir(path)) == NULL)
199 while ((entry = readdir(dir)) != NULL) {
201 /* Skip "." and ".." (also skips hidden files, which is ok) */
203 if (entry->d_name[0] == '.')
206 if (entry->d_type == DT_DIR) {
207 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
212 /* If there's a dev entry, mknod it */
214 if (!strcmp(entry->d_name, "dev")) make_device(path);
220 int mdev_main(int argc, char *argv[])
224 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
230 if (argc == 2 && !strcmp(argv[1],"-s")) {
233 stat("/", &st); // If this fails, we have bigger problems.
234 bbg.root_major=major(st.st_dev);
235 bbg.root_minor=minor(st.st_dev);
236 strcpy(temp,"/sys/block");
238 strcpy(temp,"/sys/class");
244 action = getenv("ACTION");
245 env_path = getenv("DEVPATH");
246 if (!action || !env_path)
249 if (!strcmp(action, "add")) {
250 sprintf(temp, "/sys%s", env_path);
252 } else if (!strcmp(action, "remove")) {
253 unlink(strrchr(env_path, '/') + 1);
257 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);