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.
16 int root_major, root_minor;
18 #define G (*(struct globals*)&bb_common_bufsiz1)
19 #define root_major (G.root_major)
20 #define root_minor (G.root_minor)
22 #define MAX_SYSFS_DEPTH 3 /* prevent infinite loops in /sys symlinks */
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 const char *s = "@$*";
146 s2 = strchr(s, *pos++);
152 if ((s2-s+1) & (1<<delete))
153 command = xstrndup(pos, end-pos);
159 /* Did everything parse happily? */
161 if (field > 2) break;
162 if (field) bb_error_msg_and_die("bad line %d",line);
168 end_parse: /* nothing */ ;
173 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
174 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
175 bb_perror_msg_and_die("mknod %s", device_name);
177 if (major == root_major && minor == root_minor)
178 symlink(device_name, "root");
180 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
183 /* setenv will leak memory, so use putenv */
184 char *s = xasprintf("MDEV=%s", device_name);
186 if (system(command) == -1)
187 bb_perror_msg_and_die("cannot run %s", command);
193 if (delete) unlink(device_name);
196 /* File callback for /sys/ traversal */
197 static int fileAction(const char *fileName, struct stat *statbuf,
198 void *userData, int depth)
200 size_t len = strlen(fileName) - 4;
201 char *scratch = userData;
203 if (strcmp(fileName + len, "/dev"))
206 strcpy(scratch, fileName);
208 make_device(scratch, 0);
213 /* Directory callback for /sys/ traversal */
214 static int dirAction(const char *fileName, struct stat *statbuf,
215 void *userData, int depth)
217 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
220 /* For the full gory details, see linux/Documentation/firmware_class/README
222 * Firmware loading works like this:
223 * - kernel sets FIRMWARE env var
224 * - userspace checks /lib/firmware/$FIRMWARE
225 * - userspace waits for /sys/$DEVPATH/loading to appear
226 * - userspace writes "1" to /sys/$DEVPATH/loading
227 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
228 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
229 * - kernel loads firmware into device
231 static inline void load_firmware(const char * const firmware, const char * const sysfs_path)
234 int firmware_fd, loading_fd, data_fd;
236 /* check for $FIRMWARE from kernel */
237 /* XXX: dont bother: open(NULL) works same as open("no-such-file")
242 /* check for /lib/firmware/$FIRMWARE */
243 xchdir("/lib/firmware");
244 firmware_fd = xopen(firmware, O_RDONLY);
246 /* in case we goto out ... */
249 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
251 for (cnt = 0; cnt < 30; ++cnt) {
252 loading_fd = open("loading", O_WRONLY);
253 if (loading_fd == -1)
258 if (loading_fd == -1)
261 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
262 if (write(loading_fd, "1", 1) != 1)
265 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
266 data_fd = open("data", O_WRONLY);
269 cnt = bb_copyfd_eof(firmware_fd, data_fd);
271 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
273 write(loading_fd, "0", 1);
275 write(loading_fd, "-1", 2);
278 if (ENABLE_FEATURE_CLEAN_UP) {
285 int mdev_main(int argc, char **argv);
286 int mdev_main(int argc, char **argv)
290 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
296 if (argc == 2 && !strcmp(argv[1],"-s")) {
300 root_major = major(st.st_dev);
301 root_minor = minor(st.st_dev);
303 recursive_action("/sys/block",
304 ACTION_RECURSE | ACTION_FOLLOWLINKS,
305 fileAction, dirAction, temp, 0);
307 recursive_action("/sys/class",
308 ACTION_RECURSE | ACTION_FOLLOWLINKS,
309 fileAction, dirAction, temp, 0);
314 action = getenv("ACTION");
315 env_path = getenv("DEVPATH");
316 if (!action || !env_path)
319 sprintf(temp, "/sys%s", env_path);
320 if (!strcmp(action, "remove"))
321 make_device(temp, 1);
322 else if (!strcmp(action, "add")) {
323 make_device(temp, 0);
325 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE)
326 load_firmware(getenv("FIRMWARE"), temp);
330 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);