1 /* vi: set sw=4 ts=4: */
4 * mdev - Mini udev for busybox
6 * Copyright 2005 Rob Landley <rob@landley.net>
7 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
15 #define DEV_PATH "/dev"
19 int root_major, root_minor;
22 #define bbg mdev_globals
24 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
25 static void make_device(char *path, int delete)
28 int major, minor, type, len, fd;
32 char *temp = path + strlen(path);
35 /* Try to read major/minor string. Note that the kernel puts \n after
36 * the data, so we don't need to worry about null terminating the string
37 * because sscanf() will stop at the first nondigit, which \n is. We
38 * also depend on path having writeable space after it. */
42 fd = open(path, O_RDONLY);
43 len = read(fd, temp + 1, 64);
49 /* Determine device name, type, major and minor */
51 device_name = strrchr(path, '/') + 1;
52 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
54 /* If we have a config file, look up permissions for this device */
56 if (ENABLE_FEATURE_MDEV_CONF) {
57 char *conf, *pos, *end;
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++)
76 /* Three fields: regex, uid:gid, mode */
77 for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
81 while (pos<end && isspace(*pos)) pos++;
82 if (pos==end || *pos=='#') break;
84 end2<end && !isspace(*end2) && *end2!='#'; end2++)
88 /* Regex to match this device */
90 char *regex = strndupa(pos, end2-pos);
96 xregcomp(&match,regex, REG_EXTENDED);
97 result = regexec(&match, device_name, 1, &off, 0);
100 /* If not this device, skip rest of line */
101 if (result || off.rm_so
102 || off.rm_eo != strlen(device_name))
105 } else if (field == 1) {
111 for(s=pos; s<end2 && *s!=':'; s++)
113 if (s == end2) break;
116 uid = strtoul(pos,&s2,10);
119 pass = getpwnam(strndupa(pos, s-pos));
125 gid = strtoul(s, &s2, 10);
128 grp = getgrnam(strndupa(s, end2-s));
132 } else if (field == 2) {
135 mode = strtoul(pos, &pos, 8);
136 if (pos != end2) break;
137 } else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
139 char *s = "@$*", *s2;
140 if (!(s2 = strchr(s, *pos++))) {
145 if ((s2-s+1) & (1<<delete))
146 command = xstrndup(pos, end-pos);
152 /* Did everything parse happily? */
154 if (field > 2) break;
155 if (field) bb_error_msg_and_die("Bad line %d",line);
168 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
169 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
170 bb_perror_msg_and_die("mknod %s failed", device_name);
172 if (major == bbg.root_major && minor == bbg.root_minor)
173 symlink(device_name, "root");
175 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
181 s=xasprintf("MDEV=%s",device_name);
183 rc = system(command);
188 if (rc == -1) bb_perror_msg_and_die("Couldn't run %s", command);
190 if (delete) unlink(device_name);
193 /* Recursive search of /sys/block or /sys/class. path must be a writeable
194 * buffer of size PATH_MAX containing the directory string to start at. */
196 static void find_dev(char *path)
199 size_t len = strlen(path);
200 struct dirent *entry;
202 if ((dir = opendir(path)) == NULL)
205 while ((entry = readdir(dir)) != NULL) {
208 /* Skip "." and ".." (also skips hidden files, which is ok) */
210 if (entry->d_name[0] == '.')
213 // uClibc doesn't fill out entry->d_type reliably. so we use lstat().
215 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
216 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
219 /* If there's a dev entry, mknod it */
221 if (!strcmp(entry->d_name, "dev")) make_device(path, 0);
227 int mdev_main(int argc, char *argv[])
231 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
237 if (argc == 2 && !strcmp(argv[1],"-s")) {
240 stat("/", &st); // If this fails, we have bigger problems.
241 bbg.root_major=major(st.st_dev);
242 bbg.root_minor=minor(st.st_dev);
243 strcpy(temp,"/sys/block");
245 strcpy(temp,"/sys/class");
251 action = getenv("ACTION");
252 env_path = getenv("DEVPATH");
253 if (!action || !env_path)
256 sprintf(temp, "/sys%s", env_path);
257 if (!strcmp(action, "add")) make_device(temp,0);
258 else if (!strcmp(action, "remove")) make_device(temp,1);
261 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);