1 /* vi: set sw=4 ts=4: */
3 * mdev - Mini udev for busybox
5 * Copyright 2005 Rob Landley <rob@landley.net>
6 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
8 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
14 int root_major, root_minor;
17 #define G (*(struct globals*)&bb_common_bufsiz1)
18 #define root_major (G.root_major)
19 #define root_minor (G.root_minor)
20 #define subsystem (G.subsystem)
22 /* Prevent infinite loops in /sys symlinks */
23 #define MAX_SYSFS_DEPTH 3
25 /* We use additional 64+ bytes in make_device() */
26 #define SCRATCH_SIZE 80
28 #if ENABLE_FEATURE_MDEV_RENAME
29 /* Builds an alias path.
30 * This function potentionally reallocates the alias parameter.
32 static char *build_alias(char *alias, const char *device_name)
36 /* ">bar/": rename to bar/device_name */
37 /* ">bar[/]baz": rename to bar[/]baz */
38 dest = strrchr(alias, '/');
39 if (dest) { /* ">bar/[baz]" ? */
40 *dest = '\0'; /* mkdir bar */
41 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
43 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
45 alias = concat_path_file(alias, device_name);
54 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
55 /* NB: "mdev -s" may call us many times, do not leak memory/fds! */
56 static void make_device(char *path, int delete)
58 #if ENABLE_FEATURE_MDEV_CONF
61 const char *device_name;
62 int major, minor, type, len;
64 char *dev_maj_min = path + strlen(path);
66 /* Force the configuration file settings exactly. */
69 /* Try to read major/minor string. Note that the kernel puts \n after
70 * the data, so we don't need to worry about null terminating the string
71 * because sscanf() will stop at the first nondigit, which \n is.
72 * We also depend on path having writeable space after it.
76 strcpy(dev_maj_min, "/dev");
77 len = open_read_close(path, dev_maj_min + 1, 64);
78 *dev_maj_min++ = '\0';
80 if (!ENABLE_FEATURE_MDEV_EXEC)
82 /* no "dev" file, so just try to run script */
84 } else if (sscanf(dev_maj_min, "%u:%u", &major, &minor) != 2) {
89 /* Determine device name, type, major and minor */
90 device_name = bb_basename(path);
91 /* http://kernel.org/doc/pending/hotplug.txt says that only
92 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
93 * But since 2.6.25 block devices are also in /sys/class/block.
94 * We use strstr("/block/") to forestall future surprises. */
96 if (strstr(path, "/block/"))
99 #if !ENABLE_FEATURE_MDEV_CONF
102 /* If we have config file, look up user settings */
103 parser = config_open2("/etc/mdev.conf", fopen_for_read);
105 regmatch_t off[1 + 9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
108 struct bb_uidgid_t ugid;
110 # if ENABLE_FEATURE_MDEV_EXEC
111 char *command = NULL;
113 # if ENABLE_FEATURE_MDEV_RENAME
115 char aliaslink = aliaslink; /* for compiler */
117 /* Defaults in case we won't match any line */
118 ugid.uid = ugid.gid = 0;
122 if (!config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL)) {
123 /* End of file, create dev node with default params */
128 keep_matching = ('-' == val[0]);
129 val += keep_matching; /* swallow leading dash */
131 /* Fields: regex uid:gid mode [alias] [cmd] */
133 /* 1st field: @<numeric maj,min>... */
135 /* @major,minor[-last] */
136 /* (useful when name is ambiguous:
137 * "/sys/class/usb/lp0" and
138 * "/sys/class/printer/lp0") */
139 int cmaj, cmin0, cmin1, sc;
141 continue; /* no dev, no match */
142 sc = sscanf(val, "@%u,%u-%u", &cmaj, &cmin0, &cmin1);
143 if (sc < 1 || major != cmaj
144 || (sc == 2 && minor != cmin0)
145 || (sc == 3 && (minor < cmin0 || minor > cmin1))
147 continue; /* this line doesn't match */
149 } else { /* ... or regex to match device name */
152 const char *dev_name_or_subsystem = device_name;
153 if ('/' == val[0] && subsystem) {
154 dev_name_or_subsystem = subsystem;
159 xregcomp(&match, val, REG_EXTENDED);
160 result = regexec(&match, dev_name_or_subsystem, ARRAY_SIZE(off), off, 0);
163 //bb_error_msg("matches:");
164 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
165 // if (off[i].rm_so < 0) continue;
166 // bb_error_msg("match %d: '%.*s'\n", i,
167 // (int)(off[i].rm_eo - off[i].rm_so),
168 // device_name + off[i].rm_so);
171 /* If not this device, skip rest of line */
172 /* (regexec returns whole pattern as "range" 0) */
173 if (result || off[0].rm_so
174 || ((int)off[0].rm_eo != (int)strlen(dev_name_or_subsystem))
176 continue; /* this line doesn't match */
180 /* This line matches: stop parsing the file after parsing
181 * the rest of fields unless keep_matching == 1 */
183 /* 2nd field: uid:gid - device ownership */
184 if (get_uidgid(&ugid, tokens[1], 1) == 0)
185 bb_error_msg("unknown user/group %s", tokens[1]);
187 /* 3rd field: mode - device permissions */
188 mode = strtoul(tokens[2], NULL, 8);
191 /* 4th field (opt): >|=alias */
192 # if ENABLE_FEATURE_MDEV_RENAME
196 if (aliaslink == '>' || aliaslink == '=') {
198 # if ENABLE_FEATURE_MDEV_RENAME_REGEXP
203 s = strchrnul(val, ' ');
204 st = strchrnul(val, '\t');
207 val = (s[0] && s[1]) ? s+1 : NULL;
210 # if ENABLE_FEATURE_MDEV_RENAME_REGEXP
211 /* substitute %1..9 with off[1..9], if any */
218 p = alias = xzalloc(strlen(a) + n * strlen(device_name));
224 if (i <= 9 && off[i].rm_so >= 0) {
225 n = off[i].rm_eo - off[i].rm_so;
226 strncpy(p, device_name + off[i].rm_so, n);
235 alias = xstrdup(a + 1);
238 # endif /* ENABLE_FEATURE_MDEV_RENAME */
240 # if ENABLE_FEATURE_MDEV_EXEC
241 /* The rest (opt): @|$|*command */
245 const char *s = "@$*";
246 const char *s2 = strchr(s, val[0]);
249 bb_error_msg_and_die("bad line %u", parser->lineno);
251 /* Correlate the position in the "@$*" with the delete
252 * step so that we get the proper behavior:
253 * @cmd: run on create
254 * $cmd: run on delete
257 if ((s2 - s + 1) /*1/2/3*/ & /*1/2*/ (1 + delete)) {
258 command = xstrdup(val + 1);
262 /* End of field parsing */
264 #endif /* ENABLE_FEATURE_MDEV_CONF */
266 /* "Execute" the line we found */
268 if (!delete && major >= 0) {
269 if (ENABLE_FEATURE_MDEV_RENAME)
271 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
272 bb_perror_msg_and_die("mknod %s", device_name);
273 if (major == root_major && minor == root_minor)
274 symlink(device_name, "root");
275 #if ENABLE_FEATURE_MDEV_CONF
276 chown(device_name, ugid.uid, ugid.gid);
277 # if ENABLE_FEATURE_MDEV_RENAME
279 alias = build_alias(alias, device_name);
280 /* move the device, and optionally
281 * make a symlink to moved device node */
282 if (rename(device_name, alias) == 0 && aliaslink == '>')
283 symlink(alias, device_name);
289 #if ENABLE_FEATURE_MDEV_EXEC
291 /* setenv will leak memory, use putenv/unsetenv/free */
292 char *s = xasprintf("%s=%s", "MDEV", device_name);
293 char *s1 = xasprintf("%s=%s", "SUBSYSTEM", subsystem);
296 if (system(command) == -1)
297 bb_perror_msg_and_die("can't run '%s'", command);
298 unsetenv("SUBSYSTEM");
307 /* At creation time, device might have been moved
308 * and a symlink might have been created. Undo that. */
309 #if ENABLE_FEATURE_MDEV_RENAME
311 alias = build_alias(alias, device_name);
318 #if ENABLE_FEATURE_MDEV_CONF
319 /* We found matching line.
320 * Stop unless it was prefixed with '-' */
323 } /* end of "while line is read from /etc/mdev.conf" */
325 config_close(parser);
326 #endif /* ENABLE_FEATURE_MDEV_CONF */
329 /* File callback for /sys/ traversal */
330 static int FAST_FUNC fileAction(const char *fileName,
331 struct stat *statbuf UNUSED_PARAM,
333 int depth UNUSED_PARAM)
335 size_t len = strlen(fileName) - 4; /* can't underflow */
336 char *scratch = userData;
338 /* len check is for paranoid reasons */
339 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
342 strcpy(scratch, fileName);
344 make_device(scratch, 0);
349 /* Directory callback for /sys/ traversal */
350 static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
351 struct stat *statbuf UNUSED_PARAM,
352 void *userData UNUSED_PARAM,
355 /* Extract device subsystem -- the name of the directory
356 * under /sys/class/ */
359 subsystem = strrchr(fileName, '/');
361 subsystem = xstrdup(subsystem + 1);
364 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
367 /* For the full gory details, see linux/Documentation/firmware_class/README
369 * Firmware loading works like this:
370 * - kernel sets FIRMWARE env var
371 * - userspace checks /lib/firmware/$FIRMWARE
372 * - userspace waits for /sys/$DEVPATH/loading to appear
373 * - userspace writes "1" to /sys/$DEVPATH/loading
374 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
375 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
376 * - kernel loads firmware into device
378 static void load_firmware(const char *const firmware, const char *const sysfs_path)
381 int firmware_fd, loading_fd, data_fd;
383 /* check for /lib/firmware/$FIRMWARE */
384 xchdir("/lib/firmware");
385 firmware_fd = xopen(firmware, O_RDONLY);
387 /* in case we goto out ... */
390 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
392 for (cnt = 0; cnt < 30; ++cnt) {
393 loading_fd = open("loading", O_WRONLY);
394 if (loading_fd != -1)
401 /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */
402 if (full_write(loading_fd, "1", 1) != 1)
405 /* load firmware into /sys/$DEVPATH/data */
406 data_fd = open("data", O_WRONLY);
409 cnt = bb_copyfd_eof(firmware_fd, data_fd);
411 /* tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading" */
413 full_write(loading_fd, "0", 1);
415 full_write(loading_fd, "-1", 2);
418 if (ENABLE_FEATURE_CLEAN_UP) {
425 int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
426 int mdev_main(int argc UNUSED_PARAM, char **argv)
428 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
430 /* We can be called as hotplug helper */
431 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
436 /* Replace LOGFILE by other file or device name if you need */
437 #define LOGFILE "/dev/console"
438 /* Just making sure fd 0 is not closed,
439 * we don't really intend to read from it */
440 xmove_fd(xopen("/", O_RDONLY), STDIN_FILENO);
441 xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDOUT_FILENO);
442 xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDERR_FILENO);
447 if (argv[1] && strcmp(argv[1], "-s") == 0) {
454 root_major = major(st.st_dev);
455 root_minor = minor(st.st_dev);
457 /* ACTION_FOLLOWLINKS is needed since in newer kernels
458 * /sys/block/loop* (for example) are symlinks to dirs,
459 * not real directories.
460 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
461 * but we can't enforce that on users)
463 if (access("/sys/class/block", F_OK) != 0) {
464 /* Scan obsolete /sys/block only if /sys/class/block
465 * doesn't exist. Otherwise we'll have dupes.
466 * Also, do not complain if it doesn't exist.
467 * Some people configure kernel to have no blockdevs.
469 recursive_action("/sys/block",
470 ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET,
471 fileAction, dirAction, temp, 0);
473 recursive_action("/sys/class",
474 ACTION_RECURSE | ACTION_FOLLOWLINKS,
475 fileAction, dirAction, temp, 0);
483 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
484 * ACTION can be "add" or "remove"
485 * DEVPATH is like "/block/sda" or "/class/input/mice"
487 action = getenv("ACTION");
488 env_path = getenv("DEVPATH");
489 subsystem = getenv("SUBSYSTEM");
490 if (!action || !env_path /*|| !subsystem*/)
492 fw = getenv("FIRMWARE");
494 /* If it exists, does /dev/mdev.seq match $SEQNUM?
495 * If it does not match, earlier mdev is running
496 * in parallel, and we need to wait */
497 seq = getenv("SEQNUM");
499 int timeout = 2000 / 32; /* 2000 msec */
502 char seqbuf[sizeof(int)*3 + 2];
504 seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1));
509 seqbuf[seqlen] = '\0';
510 if (seqbuf[0] == '\n' /* seed file? */
511 || strcmp(seq, seqbuf) == 0 /* correct idx? */
519 snprintf(temp, PATH_MAX, "/sys%s", env_path);
520 if (strcmp(action, "remove") == 0) {
521 /* Ignoring "remove firmware". It was reported
522 * to happen and to cause erroneous deletion
523 * of device nodes. */
525 make_device(temp, 1);
527 else if (strcmp(action, "add") == 0) {
528 make_device(temp, 0);
529 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
531 load_firmware(fw, temp);
536 xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1));
540 if (ENABLE_FEATURE_CLEAN_UP)
541 RELEASE_CONFIG_BUFFER(temp);