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);
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;
52 if(sscanf(temp, "%d:%d", &major, &minor) != 2) goto end;
54 /* If we have a config file, look up permissions for this device */
56 if (ENABLE_FEATURE_MDEV_CONF) {
59 /* mmap the config file */
60 if (-1!=(fd=open("/etc/mdev.conf",O_RDONLY))) {
61 len=lseek(fd,0,SEEK_END);
62 conf=mmap(NULL,len,PROT_READ,MAP_PRIVATE,fd,0);
66 /* Loop through lines in mmaped file*/
67 for (pos=conf;pos-conf<len;) {
72 /* find end of this line */
73 for(end=pos;end-conf<len && *end!='\n';end++);
75 /* Three fields: regex, uid:gid, mode */
76 for (field=3;field;field--) {
78 while (pos<end && isspace(*pos)) pos++;
79 if (pos==end || *pos=='#') break;
81 end2<end && !isspace(*end2) && *end2!='#'; end2++);
83 /* Regex to match this device */
86 char *regex=strndupa(pos,end2-pos);
92 xregcomp(&match,regex,REG_EXTENDED);
93 result=regexec(&match,device_name,1,&off,0);
96 /* If not this device, skip rest of line */
97 if(result || off.rm_so
98 || off.rm_eo!=strlen(device_name))
109 for(s=pos;s<end2 && *s!=':';s++);
110 if(s==end2) goto end_line;
113 uid=strtoul(pos,&s2,10);
116 pass=getpwnam(strndupa(pos,s-pos));
117 if(!pass) goto end_line;
122 gid=strtoul(s,&s2,10);
125 grp=getgrnam(strndupa(s,end2-s));
126 if(!grp) goto end_line;
134 mode=strtoul(pos,&pos,8);
135 if(pos!=end2) goto end_line;
142 /* Did everything parse happily? */
143 if (field && field!=3)
144 bb_error_msg_and_die("Bad line %d",line);
156 sprintf(temp, "%s/%s", DEV_PATH, device_name);
158 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
159 bb_perror_msg_and_die("mknod %s failed", temp);
161 if (ENABLE_FEATURE_MDEV_CONF) chown(temp,uid,gid);
164 RELEASE_CONFIG_BUFFER(temp);
167 /* Recursive search of /sys/block or /sys/class. path must be a writeable
168 * buffer of size PATH_MAX containing the directory string to start at. */
170 static void find_dev(char *path)
173 size_t len=strlen(path);
174 struct dirent *entry;
176 if ((dir = opendir(path)) == NULL)
179 while ((entry = readdir(dir)) != NULL) {
181 /* Skip "." and ".." (also skips hidden files, which is ok) */
183 if (entry->d_name[0]=='.') continue;
185 if (entry->d_type == DT_DIR) {
186 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
190 /* If there's a dev entry, mknod it */
192 if (!strcmp(entry->d_name, "dev")) make_device(path);
198 int mdev_main(int argc, char *argv[])
202 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
206 if (argc == 2 && !strcmp(argv[1],"-s")) {
207 strcpy(temp,"/sys/block");
209 strcpy(temp,"/sys/class");
215 action = getenv("ACTION");
216 env_path = getenv("DEVPATH");
217 if (!action || !env_path) bb_show_usage();
219 if (!strcmp(action, "add")) {
220 sprintf(temp, "/sys%s", env_path);
222 } else if (!strcmp(action, "remove")) {
223 sprintf(temp, "%s/%s", DEV_PATH, strrchr(env_path, '/') + 1);
228 if(ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);