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>
25 #define DEV_PATH "/dev"
29 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
30 static void make_device(char *path)
33 int major,minor,type,len,fd;
38 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
40 /* Try to read major/minor string */
42 snprintf(temp, PATH_MAX, "%s/dev", path);
43 fd = open(temp, O_RDONLY);
44 len = read(fd, temp, PATH_MAX-1);
46 temp[--len] = 0; // remove trailing \n
49 /* Determine device name, type, major and minor */
51 device_name = strrchr(path, '/') + 1;
52 type = strncmp(path+5, "block/" ,6) ? S_IFCHR : S_IFBLK;
54 for (s = temp; *s; s++) {
64 /* If we have a config file, look up permissions for this device */
66 if (ENABLE_FEATURE_MDEV_CONF) {
69 /* mmap the config file */
70 if (-1!=(fd=open("/etc/mdev.conf",O_RDONLY))) {
71 len=lseek(fd,0,SEEK_END);
72 conf=mmap(NULL,len,PROT_READ,MAP_PRIVATE,fd,0);
76 /* Loop through lines in mmaped file*/
77 for (pos=conf;pos-conf<len;) {
82 /* find end of this line */
83 for(end=pos;end-conf<len && *end!='\n';end++);
85 /* Three fields: regex, uid:gid, mode */
86 for (field=3;field;field--) {
88 while (pos<end && isspace(*pos)) pos++;
89 if (pos==end || *pos=='#') break;
91 end2<end && !isspace(*end2) && *end2!='#'; end2++);
93 /* Regex to match this device */
96 char *regex=strndupa(pos,end2-pos);
102 xregcomp(&match,regex,REG_EXTENDED);
103 result=regexec(&match,device_name,1,&off,0);
106 /* If not this device, skip rest of line */
107 if(result || off.rm_so
108 || off.rm_eo!=strlen(device_name))
119 for(s=pos;s<end2 && *s!=':';s++);
120 if(s==end2) goto end_line;
123 uid=strtoul(pos,&s2,10);
126 pass=getpwnam(strndupa(pos,s-pos));
127 if(!pass) goto end_line;
132 gid=strtoul(s,&s2,10);
135 grp=getgrnam(strndupa(s,end2-s));
136 if(!grp) goto end_line;
144 mode=strtoul(pos,&pos,8);
145 if(pos!=end2) goto end_line;
152 /* Did everything parse happily? */
153 if (field && field!=3)
154 bb_error_msg_and_die("Bad line %d",line);
166 sprintf(temp, "%s/%s", DEV_PATH, device_name);
168 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
169 bb_perror_msg_and_die("mknod %s failed", temp);
171 if (ENABLE_FEATURE_MDEV_CONF) chown(temp,uid,gid);
174 RELEASE_CONFIG_BUFFER(temp);
177 /* Recursive search of /sys/block or /sys/class. path must be a writeable
178 * buffer of size PATH_MAX containing the directory string to start at. */
180 static void find_dev(char *path)
183 int len=strlen(path);
185 if (!(dir = opendir(path)))
186 bb_perror_msg_and_die("No %s",path);
189 struct dirent *entry = readdir(dir);
193 /* Skip "." and ".." (also skips hidden files, which is ok) */
195 if (entry->d_name[0]=='.') continue;
197 if (entry->d_type == DT_DIR) {
198 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
203 /* If there's a dev entry, mknod it */
205 if (strcmp(entry->d_name, "dev")) make_device(path);
211 int mdev_main(int argc, char *argv[])
215 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
219 if (argc == 2 && !strcmp(argv[1],"-s")) {
220 strcpy(temp,"/sys/block");
222 strcpy(temp,"/sys/class");
228 action = getenv("ACTION");
229 env_path = getenv("DEVPATH");
230 if (!action || !env_path) bb_show_usage();
232 if (!strcmp(action, "add")) {
233 sprintf(temp, "/sys%s", env_path);
235 } else if (!strcmp(action, "remove")) {
236 sprintf(temp, "%s/%s", DEV_PATH, strrchr(env_path, '/') + 1);
241 if(ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);