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);
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);
48 /* Determine device name, type, major and minor */
49 device_name = bb_basename(path);
50 type = (path[5] == 'c' ? S_IFCHR : S_IFBLK);
52 if (ENABLE_FEATURE_MDEV_CONF) {
57 /* If we have a config file, look up the user settings */
58 fp = fopen_or_warn("/etc/mdev.conf", "r");
62 while ((vline = line = xmalloc_getline(fp)) != NULL) {
65 /* A pristine copy for command execution. */
67 if (ENABLE_FEATURE_MDEV_EXEC)
68 orig_line = xstrdup(line);
72 /* Three fields: regex, uid:gid, mode */
73 for (field = 0; field < (3 + ENABLE_FEATURE_MDEV_EXEC); ++field) {
75 /* Find a non-empty field */
78 val = strtok(vline, " \t");
80 } while (val && !*val);
86 /* Regex to match this device */
92 xregcomp(&match, val, 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 || off.rm_eo != strlen(device_name))
100 } else if (field == 1) {
102 /* uid:gid device ownership */
107 char *str_gid = strchr(val, ':');
109 *str_gid = '\0', ++str_gid;
112 pass = getpwnam(str_uid);
116 uid = strtoul(str_uid, NULL, 10);
119 grp = getgrnam(str_gid);
123 gid = strtoul(str_gid, NULL, 10);
125 } else if (field == 2) {
127 /* Mode device permissions */
128 mode = strtoul(val, NULL, 8);
130 } else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
132 /* Optional command to run */
133 const char *s = "@$*";
134 const char *s2 = strchr(s, *val);
142 /* Correlate the position in the "@$*" with the delete
143 * step so that we get the proper behavior.
145 if ((s2 - s + 1) & (1 << delete))
146 command = xstrdup(orig_line + (val + 1 - line));
150 /* Did everything parse happily? */
152 bb_error_msg_and_die("bad line %u", lineno);
156 if (ENABLE_FEATURE_MDEV_EXEC)
160 if (ENABLE_FEATURE_CLEAN_UP)
163 end_parse: /* nothing */ ;
168 if (sscanf(temp, "%d:%d", &major, &minor) != 2)
170 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
171 bb_perror_msg_and_die("mknod %s", device_name);
173 if (major == root_major && minor == root_minor)
174 symlink(device_name, "root");
176 if (ENABLE_FEATURE_MDEV_CONF)
177 chown(device_name, uid, gid);
180 /* setenv will leak memory, so use putenv */
181 char *s = xasprintf("MDEV=%s", device_name);
183 if (system(command) == -1)
184 bb_perror_msg_and_die("cannot run %s", command);
191 remove_file(device_name, FILEUTILS_FORCE);
194 /* File callback for /sys/ traversal */
195 static int fileAction(const char *fileName, struct stat *statbuf,
196 void *userData, int depth)
198 size_t len = strlen(fileName) - 4;
199 char *scratch = userData;
201 if (strcmp(fileName + len, "/dev"))
204 strcpy(scratch, fileName);
206 make_device(scratch, 0);
211 /* Directory callback for /sys/ traversal */
212 static int dirAction(const char *fileName, struct stat *statbuf,
213 void *userData, int depth)
215 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
218 /* For the full gory details, see linux/Documentation/firmware_class/README
220 * Firmware loading works like this:
221 * - kernel sets FIRMWARE env var
222 * - userspace checks /lib/firmware/$FIRMWARE
223 * - userspace waits for /sys/$DEVPATH/loading to appear
224 * - userspace writes "1" to /sys/$DEVPATH/loading
225 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
226 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
227 * - kernel loads firmware into device
229 static void load_firmware(const char *const firmware, const char *const sysfs_path)
232 int firmware_fd, loading_fd, data_fd;
234 /* check for $FIRMWARE from kernel */
235 /* XXX: dont bother: open(NULL) works same as open("no-such-file")
240 /* check for /lib/firmware/$FIRMWARE */
241 xchdir("/lib/firmware");
242 firmware_fd = xopen(firmware, O_RDONLY);
244 /* in case we goto out ... */
247 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
249 for (cnt = 0; cnt < 30; ++cnt) {
250 loading_fd = open("loading", O_WRONLY);
251 if (loading_fd == -1)
256 if (loading_fd == -1)
259 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
260 if (write(loading_fd, "1", 1) != 1)
263 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
264 data_fd = open("data", O_WRONLY);
267 cnt = bb_copyfd_eof(firmware_fd, data_fd);
269 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
271 write(loading_fd, "0", 1);
273 write(loading_fd, "-1", 2);
276 if (ENABLE_FEATURE_CLEAN_UP) {
283 int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
284 int mdev_main(int argc, char **argv)
288 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
292 if (argc == 2 && !strcmp(argv[1],"-s")) {
301 root_major = major(st.st_dev);
302 root_minor = minor(st.st_dev);
304 recursive_action("/sys/block",
305 ACTION_RECURSE | ACTION_FOLLOWLINKS,
306 fileAction, dirAction, temp, 0);
308 recursive_action("/sys/class",
309 ACTION_RECURSE | ACTION_FOLLOWLINKS,
310 fileAction, dirAction, temp, 0);
315 * env ACTION=... DEVPATH=... mdev
316 * ACTION can be "add" or "remove"
317 * DEVPATH is like "/block/sda" or "/class/input/mice"
320 action = getenv("ACTION");
321 env_path = getenv("DEVPATH");
322 if (!action || !env_path)
325 sprintf(temp, "/sys%s", env_path);
326 if (!strcmp(action, "remove"))
327 make_device(temp, 1);
328 else if (!strcmp(action, "add")) {
329 make_device(temp, 0);
331 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE)
332 load_firmware(getenv("FIRMWARE"), temp);
336 if (ENABLE_FEATURE_CLEAN_UP)
337 RELEASE_CONFIG_BUFFER(temp);