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 GPL version 2, 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;
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 len = open_read_close(path, temp + 1, 64);
47 /* Determine device name, type, major and minor */
49 device_name = strrchr(path, '/') + 1;
50 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
52 /* If we have a config file, look up permissions for this device */
54 if (ENABLE_FEATURE_MDEV_CONF) {
55 char *conf, *pos, *end;
58 /* mmap the config file */
59 fd = open("/etc/mdev.conf", O_RDONLY);
62 len = xlseek(fd, 0, SEEK_END);
63 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
69 /* Loop through lines in mmaped file*/
70 for (pos=conf; pos-conf<len;) {
75 /* find end of this line */
76 for(end=pos; end-conf<len && *end!='\n'; end++)
79 /* Three fields: regex, uid:gid, mode */
80 for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
84 while (pos<end && isspace(*pos)) pos++;
85 if (pos==end || *pos=='#') break;
87 end2<end && !isspace(*end2) && *end2!='#'; end2++)
91 /* Regex to match this device */
93 char *regex = strndupa(pos, end2-pos);
99 xregcomp(&match,regex, REG_EXTENDED);
100 result = regexec(&match, device_name, 1, &off, 0);
103 /* If not this device, skip rest of line */
104 if (result || off.rm_so
105 || off.rm_eo != strlen(device_name))
114 for(s=pos; s<end2 && *s!=':'; s++)
116 if (s == end2) break;
119 uid = strtoul(pos, &s2, 10);
122 pass = getpwnam(strndupa(pos, s-pos));
128 gid = strtoul(s, &s2, 10);
131 grp = getgrnam(strndupa(s, end2-s));
139 mode = strtoul(pos, &pos, 8);
140 if (pos != end2) break;
142 if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
144 char *s = "@$*", *s2;
145 s2 = strchr(s, *pos++);
151 if ((s2-s+1) & (1<<delete))
152 command = xstrndup(pos, end-pos);
158 /* Did everything parse happily? */
160 if (field > 2) break;
161 if (field) bb_error_msg_and_die("bad line %d",line);
167 end_parse: /* nothing */ ;
172 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
173 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
174 bb_perror_msg_and_die("mknod %s", device_name);
176 if (major == bbg.root_major && minor == bbg.root_minor)
177 symlink(device_name, "root");
179 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
185 s = xasprintf("MDEV=%s", device_name);
187 rc = system(command);
192 if (rc == -1) bb_perror_msg_and_die("cannot run %s", command);
194 if (delete) unlink(device_name);
197 /* Recursive search of /sys/block or /sys/class. path must be a writeable
198 * buffer of size PATH_MAX containing the directory string to start at. */
200 static void find_dev(char *path)
203 size_t len = strlen(path);
204 struct dirent *entry;
210 while ((entry = readdir(dir)) != NULL) {
213 /* Skip "." and ".." (also skips hidden files, which is ok) */
215 if (entry->d_name[0] == '.')
218 // uClibc doesn't fill out entry->d_type reliably. so we use lstat().
220 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
221 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
224 /* If there's a dev entry, mknod it */
226 if (!strcmp(entry->d_name, "dev")) make_device(path, 0);
232 int mdev_main(int argc, char *argv[])
236 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
242 if (argc == 2 && !strcmp(argv[1],"-s")) {
246 bbg.root_major = major(st.st_dev);
247 bbg.root_minor = minor(st.st_dev);
248 strcpy(temp,"/sys/block");
250 strcpy(temp,"/sys/class");
256 action = getenv("ACTION");
257 env_path = getenv("DEVPATH");
258 if (!action || !env_path)
261 sprintf(temp, "/sys%s", env_path);
262 if (!strcmp(action, "add")) make_device(temp,0);
263 else if (!strcmp(action, "remove")) make_device(temp,1);
266 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);