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/types.h>
24 #define DEV_PATH "/dev"
28 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
29 static void make_device(char *path)
32 int major,minor,type,len,fd;
37 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
39 /* Try to read major/minor string */
41 snprintf(temp, PATH_MAX, "%s/dev", path);
42 fd = open(temp, O_RDONLY);
43 len = read(fd, temp, PATH_MAX-1);
48 /* Determine device name, type, major and minor */
50 device_name = strrchr(path, '/') + 1;
51 type = strncmp(path+5, "block/" ,6) ? S_IFCHR : S_IFBLK;
53 for (s = temp; *s; s++) {
63 /* If we have a config file, look up permissions for this device */
65 if (ENABLE_FEATURE_MDEV_CONF) {
68 /* mmap the config file */
69 if (-1!=(fd=open("/etc/mdev.conf",O_RDONLY))) {
70 len=lseek(fd,0,SEEK_END);
71 conf=mmap(NULL,len,PROT_READ,MAP_SHARED,fd,0);
75 /* Loop through lines in mmaped file*/
76 for (pos=conf;pos-conf<len;) {
81 /* find end of this line */
82 for(end=pos;end-conf<len && *end!='\n';end++);
84 /* Three fields: regex, uid:gid, mode */
85 for (field=3;field;field--) {
87 while (pos<end && isspace(*pos)) pos++;
88 if (pos==end || *pos=='#') break;
90 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
107 || off.rm_eo!=strlen(device_name))
118 for(s=pos;s<end2 && *s!=':';s++);
119 if(s==end2) goto end_line;
122 uid=strtoul(pos,&s2,10);
125 pass=getpwnam(strndupa(pos,s-pos));
126 if(!pass) goto end_line;
131 gid=strtoul(s,&s2,10);
134 grp=getgrnam(strndupa(s,end2-s));
135 if(!grp) goto end_line;
143 mode=strtoul(pos,&pos,8);
144 if(pos!=end2) goto end_line;
151 /* Did everything parse happily? */
152 if (field && field!=3)
153 bb_error_msg_and_die("Bad line %d",line);
165 sprintf(temp, "%s/%s", DEV_PATH, device_name);
167 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
168 bb_perror_msg_and_die("mknod %s failed", temp);
170 if (ENABLE_FEATURE_MDEV_CONF) chown(temp,uid,gid);
173 RELEASE_CONFIG_BUFFER(temp);
176 /* Recursive search of /sys/block or /sys/class. path must be a writeable
177 * buffer of size PATH_MAX containing the directory string to start at. */
179 static void find_dev(char *path)
182 int len=strlen(path);
184 if (!(dir = opendir(path)))
185 bb_perror_msg_and_die("No %s",path);
188 struct dirent *entry = readdir(dir);
192 /* Skip "." and ".." (also skips hidden files, which is ok) */
194 if (entry->d_name[0]=='.') continue;
196 if (entry->d_type == DT_DIR) {
197 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
202 /* If there's a dev entry, mknod it */
204 if (strcmp(entry->d_name, "dev")) make_device(path);
210 int mdev_main(int argc, char *argv[])
213 if (argc == 2 && !strcmp(argv[1],"-s")) {
214 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
215 strcpy(temp,"/sys/block");
217 strcpy(temp,"/sys/class");
219 if(ENABLE_FEATURE_CLEAN_UP)
220 RELEASE_CONFIG_BUFFER(temp);
222 } else bb_show_usage();
225 /* hotplug support goes here */