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);
52 /* Determine device name, type, major and minor */
53 device_name = bb_basename(path);
54 type = (path[5] == 'c' ? S_IFCHR : S_IFBLK);
56 if (ENABLE_FEATURE_MDEV_CONF) {
61 /* If we have a config file, look up the user settings */
62 fp = fopen_or_warn("/etc/mdev.conf", "r");
66 while ((vline = line = xmalloc_getline(fp)) != NULL) {
69 /* A pristine copy for command execution. */
71 if (ENABLE_FEATURE_MDEV_EXEC)
72 orig_line = xstrdup(line);
76 /* Three fields: regex, uid:gid, mode */
77 for (field = 0; field < (3 + ENABLE_FEATURE_MDEV_RENAME + ENABLE_FEATURE_MDEV_EXEC); ++field) {
79 /* Find a non-empty field */
82 val = strtok(vline, " \t");
84 } while (val && !*val);
94 /* Regex to match this device */
100 xregcomp(&match, val, REG_EXTENDED);
101 result = regexec(&match, device_name, 1, &off, 0);
104 /* If not this device, skip rest of line */
105 if (result || off.rm_so || off.rm_eo != strlen(device_name))
108 } else if (field == 1) {
110 /* uid:gid device ownership */
115 char *str_gid = strchr(val, ':');
117 *str_gid = '\0', ++str_gid;
120 pass = getpwnam(str_uid);
124 uid = strtoul(str_uid, NULL, 10);
127 grp = getgrnam(str_gid);
131 gid = strtoul(str_gid, NULL, 10);
133 } else if (field == 2) {
135 /* Mode device permissions */
136 mode = strtoul(val, NULL, 8);
138 } else if (ENABLE_FEATURE_MDEV_RENAME && field == 3) {
143 alias = xstrdup(val + 1);
147 if (ENABLE_FEATURE_MDEV_EXEC && field == 3 + ENABLE_FEATURE_MDEV_RENAME) {
149 /* Optional command to run */
150 const char *s = "@$*";
151 const char *s2 = strchr(s, *val);
159 /* Correlate the position in the "@$*" with the delete
160 * step so that we get the proper behavior.
162 if ((s2 - s + 1) & (1 << delete))
163 command = xstrdup(orig_line + (val + 1 - line));
167 /* Did everything parse happily? */
169 bb_error_msg_and_die("bad line %u", lineno);
173 if (ENABLE_FEATURE_MDEV_EXEC)
177 if (ENABLE_FEATURE_CLEAN_UP)
180 end_parse: /* nothing */ ;
184 if (sscanf(temp, "%d:%d", &major, &minor) != 2)
187 if (ENABLE_FEATURE_MDEV_RENAME)
190 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
191 bb_perror_msg_and_die("mknod %s", device_name);
193 if (major == root_major && minor == root_minor)
194 symlink(device_name, "root");
196 if (ENABLE_FEATURE_MDEV_CONF) {
197 chown(device_name, uid, gid);
199 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
202 temp = strrchr(alias, '/');
205 /* given a file name, so rename it */
207 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
208 dest = concat_path_file(alias, device_name);
212 rename(device_name, dest); // TODO: xrename?
213 symlink(dest, device_name);
221 if (ENABLE_FEATURE_MDEV_EXEC && command) {
222 /* setenv will leak memory, so use putenv */
223 char *s = xasprintf("MDEV=%s", device_name);
225 if (system(command) == -1)
226 bb_perror_msg_and_die("cannot run %s", command);
233 remove_file(device_name, FILEUTILS_FORCE);
236 /* File callback for /sys/ traversal */
237 static int fileAction(const char *fileName, struct stat *statbuf,
238 void *userData, int depth)
240 size_t len = strlen(fileName) - 4;
241 char *scratch = userData;
243 if (strcmp(fileName + len, "/dev"))
246 strcpy(scratch, fileName);
248 make_device(scratch, 0);
253 /* Directory callback for /sys/ traversal */
254 static int dirAction(const char *fileName, struct stat *statbuf,
255 void *userData, int depth)
257 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
260 /* For the full gory details, see linux/Documentation/firmware_class/README
262 * Firmware loading works like this:
263 * - kernel sets FIRMWARE env var
264 * - userspace checks /lib/firmware/$FIRMWARE
265 * - userspace waits for /sys/$DEVPATH/loading to appear
266 * - userspace writes "1" to /sys/$DEVPATH/loading
267 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
268 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
269 * - kernel loads firmware into device
271 static void load_firmware(const char *const firmware, const char *const sysfs_path)
274 int firmware_fd, loading_fd, data_fd;
276 /* check for $FIRMWARE from kernel */
277 /* XXX: dont bother: open(NULL) works same as open("no-such-file")
282 /* check for /lib/firmware/$FIRMWARE */
283 xchdir("/lib/firmware");
284 firmware_fd = xopen(firmware, O_RDONLY);
286 /* in case we goto out ... */
289 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
291 for (cnt = 0; cnt < 30; ++cnt) {
292 loading_fd = open("loading", O_WRONLY);
293 if (loading_fd == -1)
298 if (loading_fd == -1)
301 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
302 if (write(loading_fd, "1", 1) != 1)
305 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
306 data_fd = open("data", O_WRONLY);
309 cnt = bb_copyfd_eof(firmware_fd, data_fd);
311 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
313 write(loading_fd, "0", 1);
315 write(loading_fd, "-1", 2);
318 if (ENABLE_FEATURE_CLEAN_UP) {
325 int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
326 int mdev_main(int argc, char **argv)
330 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
334 if (argc == 2 && !strcmp(argv[1],"-s")) {
343 root_major = major(st.st_dev);
344 root_minor = minor(st.st_dev);
346 recursive_action("/sys/block",
347 ACTION_RECURSE | ACTION_FOLLOWLINKS,
348 fileAction, dirAction, temp, 0);
350 recursive_action("/sys/class",
351 ACTION_RECURSE | ACTION_FOLLOWLINKS,
352 fileAction, dirAction, temp, 0);
357 * env ACTION=... DEVPATH=... mdev
358 * ACTION can be "add" or "remove"
359 * DEVPATH is like "/block/sda" or "/class/input/mice"
362 action = getenv("ACTION");
363 env_path = getenv("DEVPATH");
364 if (!action || !env_path)
367 sprintf(temp, "/sys%s", env_path);
368 if (!strcmp(action, "remove"))
369 make_device(temp, 1);
370 else if (!strcmp(action, "add")) {
371 make_device(temp, 0);
373 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE)
374 load_firmware(getenv("FIRMWARE"), temp);
378 if (ENABLE_FEATURE_CLEAN_UP)
379 RELEASE_CONFIG_BUFFER(temp);