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)
27 const char *device_name;
28 int major, minor, type, len;
32 char *temp = path + strlen(path);
36 /* Force the configuration file settings exactly. */
39 /* Try to read major/minor string. Note that the kernel puts \n after
40 * the data, so we don't need to worry about null terminating the string
41 * because sscanf() will stop at the first nondigit, which \n is. We
42 * also depend on path having writeable space after it.
46 len = open_read_close(path, temp + 1, 64);
49 if (ENABLE_FEATURE_MDEV_EXEC)
50 /* no "dev" file, so just try to run script */
57 /* Determine device name, type, major and minor */
58 device_name = bb_basename(path);
59 type = (path[5] == 'c' ? S_IFCHR : S_IFBLK);
61 if (ENABLE_FEATURE_MDEV_CONF) {
66 /* If we have a config file, look up the user settings */
67 fp = fopen_or_warn("/etc/mdev.conf", "r");
71 while ((vline = line = xmalloc_getline(fp)) != NULL) {
74 /* A pristine copy for command execution. */
76 if (ENABLE_FEATURE_MDEV_EXEC)
77 orig_line = xstrdup(line);
81 /* Three fields: regex, uid:gid, mode */
82 for (field = 0; field < (3 + ENABLE_FEATURE_MDEV_RENAME + ENABLE_FEATURE_MDEV_EXEC); ++field) {
84 /* Find a non-empty field */
87 val = strtok(vline, " \t");
89 } while (val && !*val);
99 /* Regex to match this device */
105 xregcomp(&match, val, REG_EXTENDED);
106 result = regexec(&match, device_name, 1, &off, 0);
109 /* If not this device, skip rest of line */
110 if (result || off.rm_so || off.rm_eo != strlen(device_name))
113 } else if (field == 1) {
115 /* uid:gid device ownership */
120 char *str_gid = strchr(val, ':');
122 *str_gid = '\0', ++str_gid;
125 pass = getpwnam(str_uid);
129 uid = strtoul(str_uid, NULL, 10);
132 grp = getgrnam(str_gid);
136 gid = strtoul(str_gid, NULL, 10);
138 } else if (field == 2) {
140 /* Mode device permissions */
141 mode = strtoul(val, NULL, 8);
143 } else if (ENABLE_FEATURE_MDEV_RENAME && field == 3) {
148 alias = xstrdup(val + 1);
152 if (ENABLE_FEATURE_MDEV_EXEC && field == 3 + ENABLE_FEATURE_MDEV_RENAME) {
154 /* Optional command to run */
155 const char *s = "@$*";
156 const char *s2 = strchr(s, *val);
164 /* Correlate the position in the "@$*" with the delete
165 * step so that we get the proper behavior.
167 if ((s2 - s + 1) & (1 << delete))
168 command = xstrdup(orig_line + (val + 1 - line));
172 /* Did everything parse happily? */
174 bb_error_msg_and_die("bad line %u", lineno);
178 if (ENABLE_FEATURE_MDEV_EXEC)
182 if (ENABLE_FEATURE_CLEAN_UP)
185 end_parse: /* nothing */ ;
189 if (sscanf(temp, "%d:%d", &major, &minor) != 2) {
190 if (ENABLE_FEATURE_MDEV_EXEC)
196 if (ENABLE_FEATURE_MDEV_RENAME)
199 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
200 bb_perror_msg_and_die("mknod %s", device_name);
202 if (major == root_major && minor == root_minor)
203 symlink(device_name, "root");
205 if (ENABLE_FEATURE_MDEV_CONF) {
206 chown(device_name, uid, gid);
208 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
211 temp = strrchr(alias, '/');
214 /* given a file name, so rename it */
216 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
217 dest = concat_path_file(alias, device_name);
221 rename(device_name, dest); // TODO: xrename?
222 symlink(dest, device_name);
229 skip_creation: /* nothing */ ;
231 if (ENABLE_FEATURE_MDEV_EXEC && command) {
232 /* setenv will leak memory, so use putenv */
233 char *s = xasprintf("MDEV=%s", device_name);
235 if (system(command) == -1)
236 bb_perror_msg_and_die("cannot run %s", command);
243 remove_file(device_name, FILEUTILS_FORCE);
246 /* File callback for /sys/ traversal */
247 static int fileAction(const char *fileName, struct stat *statbuf,
248 void *userData, int depth)
250 size_t len = strlen(fileName) - 4;
251 char *scratch = userData;
253 if (strcmp(fileName + len, "/dev"))
256 strcpy(scratch, fileName);
258 make_device(scratch, 0);
263 /* Directory callback for /sys/ traversal */
264 static int dirAction(const char *fileName, struct stat *statbuf,
265 void *userData, int depth)
267 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
270 /* For the full gory details, see linux/Documentation/firmware_class/README
272 * Firmware loading works like this:
273 * - kernel sets FIRMWARE env var
274 * - userspace checks /lib/firmware/$FIRMWARE
275 * - userspace waits for /sys/$DEVPATH/loading to appear
276 * - userspace writes "1" to /sys/$DEVPATH/loading
277 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
278 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
279 * - kernel loads firmware into device
281 static void load_firmware(const char *const firmware, const char *const sysfs_path)
284 int firmware_fd, loading_fd, data_fd;
286 /* check for $FIRMWARE from kernel */
287 /* XXX: dont bother: open(NULL) works same as open("no-such-file")
292 /* check for /lib/firmware/$FIRMWARE */
293 xchdir("/lib/firmware");
294 firmware_fd = xopen(firmware, O_RDONLY);
296 /* in case we goto out ... */
299 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
301 for (cnt = 0; cnt < 30; ++cnt) {
302 loading_fd = open("loading", O_WRONLY);
303 if (loading_fd == -1)
308 if (loading_fd == -1)
311 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
312 if (write(loading_fd, "1", 1) != 1)
315 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
316 data_fd = open("data", O_WRONLY);
319 cnt = bb_copyfd_eof(firmware_fd, data_fd);
321 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
323 write(loading_fd, "0", 1);
325 write(loading_fd, "-1", 2);
328 if (ENABLE_FEATURE_CLEAN_UP) {
335 int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
336 int mdev_main(int argc, char **argv)
340 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
344 if (argc == 2 && !strcmp(argv[1],"-s")) {
353 root_major = major(st.st_dev);
354 root_minor = minor(st.st_dev);
356 recursive_action("/sys/block",
357 ACTION_RECURSE | ACTION_FOLLOWLINKS,
358 fileAction, dirAction, temp, 0);
360 recursive_action("/sys/class",
361 ACTION_RECURSE | ACTION_FOLLOWLINKS,
362 fileAction, dirAction, temp, 0);
367 * env ACTION=... DEVPATH=... mdev
368 * ACTION can be "add" or "remove"
369 * DEVPATH is like "/block/sda" or "/class/input/mice"
372 action = getenv("ACTION");
373 env_path = getenv("DEVPATH");
374 if (!action || !env_path)
377 sprintf(temp, "/sys%s", env_path);
378 if (!strcmp(action, "remove"))
379 make_device(temp, 1);
380 else if (!strcmp(action, "add")) {
381 make_device(temp, 0);
383 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE)
384 load_firmware(getenv("FIRMWARE"), temp);
388 if (ENABLE_FEATURE_CLEAN_UP)
389 RELEASE_CONFIG_BUFFER(temp);