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 GPLv2, see file LICENSE in this source tree.
14 //config: select PLATFORM_LINUX
16 //config: mdev is a mini-udev implementation for dynamically creating device
17 //config: nodes in the /dev directory.
19 //config: For more information, please see docs/mdev.txt
21 //config:config FEATURE_MDEV_CONF
22 //config: bool "Support /etc/mdev.conf"
24 //config: depends on MDEV
26 //config: Add support for the mdev config file to control ownership and
27 //config: permissions of the device nodes.
29 //config: For more information, please see docs/mdev.txt
31 //config:config FEATURE_MDEV_RENAME
32 //config: bool "Support subdirs/symlinks"
34 //config: depends on FEATURE_MDEV_CONF
36 //config: Add support for renaming devices and creating symlinks.
38 //config: For more information, please see docs/mdev.txt
40 //config:config FEATURE_MDEV_RENAME_REGEXP
41 //config: bool "Support regular expressions substitutions when renaming device"
43 //config: depends on FEATURE_MDEV_RENAME
45 //config: Add support for regular expressions substitutions when renaming
48 //config:config FEATURE_MDEV_EXEC
49 //config: bool "Support command execution at device addition/removal"
51 //config: depends on FEATURE_MDEV_CONF
53 //config: This adds support for an optional field to /etc/mdev.conf for
54 //config: executing commands when devices are created/removed.
56 //config: For more information, please see docs/mdev.txt
58 //config:config FEATURE_MDEV_LOAD_FIRMWARE
59 //config: bool "Support loading of firmwares"
61 //config: depends on MDEV
63 //config: Some devices need to load firmware before they can be usable.
65 //config: These devices will request userspace look up the files in
66 //config: /lib/firmware/ and if it exists, send it to the kernel for
67 //config: loading into the hardware.
69 //applet:IF_MDEV(APPLET(mdev, BB_DIR_SBIN, BB_SUID_DROP))
71 //kbuild:lib-$(CONFIG_MDEV) += mdev.o
73 //usage:#define mdev_trivial_usage
75 //usage:#define mdev_full_usage "\n\n"
76 //usage: "mdev -s is to be run during boot to scan /sys and populate /dev.\n"
78 //usage: "Bare mdev is a kernel hotplug helper. To activate it:\n"
79 //usage: " echo /sbin/mdev >/proc/sys/kernel/hotplug\n"
80 //usage: IF_FEATURE_MDEV_CONF(
82 //usage: "It uses /etc/mdev.conf with lines\n"
83 //usage: " [-]DEVNAME UID:GID PERM"
84 //usage: IF_FEATURE_MDEV_RENAME(" [>|=PATH]|[!]")
85 //usage: IF_FEATURE_MDEV_EXEC(" [@|$|*PROG]")
87 //usage: "where DEVNAME is device name regex, @major,minor[-minor2], or\n"
88 //usage: "environment variable regex. A common use of the latter is\n"
89 //usage: "to load modules for hotplugged devices:\n"
90 //usage: " $MODALIAS=.* 0:0 660 @modprobe \"$MODALIAS\"\n"
93 //usage: "If /dev/mdev.seq file exists, mdev will wait for its value\n"
94 //usage: "to match $SEQNUM variable. This prevents plug/unplug races.\n"
95 //usage: "To activate this feature, create empty /dev/mdev.seq at boot.\n"
97 //usage: "If /dev/mdev.log file exists, debug log will be appended to it."
102 /* "mdev -s" scans /sys/class/xxx, looking for directories which have dev
103 * file (it is of the form "M:m\n"). Example: /sys/class/tty/tty0/dev
104 * contains "4:0\n". Directory name is taken as device name, path component
105 * directly after /sys/class/ as subsystem. In this example, "tty0" and "tty".
106 * Then mdev creates the /dev/device_name node.
107 * If /sys/class/.../dev file does not exist, mdev still may act
108 * on this device: see "@|$|*command args..." parameter in config file.
110 * mdev w/o parameters is called as hotplug helper. It takes device
111 * and subsystem names from $DEVPATH and $SUBSYSTEM, extracts
112 * maj,min from "/sys/$DEVPATH/dev" and also examines
113 * $ACTION ("add"/"delete") and $FIRMWARE.
115 * If action is "add", mdev creates /dev/device_name similarly to mdev -s.
116 * (todo: explain "delete" and $FIRMWARE)
118 * If /etc/mdev.conf exists, it may modify /dev/device_name's properties.
120 * Leading minus in 1st field means "don't stop on this line", otherwise
121 * search is stopped after the matching line is encountered.
123 * $envvar=regex format is useful for loading modules for hot-plugged devices
124 * which do not have driver loaded yet. In this case /sys/class/.../dev
125 * does not exist, but $MODALIAS is set to needed module's name
126 * (actually, an alias to it) by kernel. This rule instructs mdev
127 * to load the module and exit:
128 * $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"
129 * The kernel will generate another hotplug event when /sys/class/.../dev
132 * When line matches, the device node is created, chmod'ed and chown'ed,
133 * moved to path, and if >path, a symlink to moved node is created,
134 * all this if /sys/class/.../dev exists.
136 * =loop/ - moves to /dev/loop
137 * >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink
139 * Then "command args..." is executed (via sh -c 'command args...').
140 * @:execute on creation, $:on deletion, *:on both.
141 * This happens regardless of /sys/class/.../dev existence.
144 /* Kernel's hotplug environment constantly changes.
145 * Here are new cases I observed on 3.1.0:
147 * Case with $DEVNAME and $DEVICE, not just $DEVPATH:
150 * DEVICE=/proc/bus/usb/001/003
151 * DEVNAME=bus/usb/001/003
153 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5
157 * PRODUCT=18d1/4e12/227
161 * Case with $DEVICE, but no $DEVNAME - apparenty, usb iface notification?
162 * "Please load me a module" thing?
164 * DEVICE=/proc/bus/usb/001/003
165 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0
166 * DEVTYPE=usb_interface
168 * MODALIAS=usb:v18D1p4E12d0227dc00dsc00dp00ic08isc06ip50
169 * PRODUCT=18d1/4e12/227
174 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5
179 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/scsi_host/host5
180 * SUBSYSTEM=scsi_host
183 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0
184 * DEVTYPE=scsi_target
187 * Case with strange $MODALIAS:
189 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0/5:0:0:0
190 * DEVTYPE=scsi_device
191 * MODALIAS=scsi:t-0x00
195 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0/5:0:0:0/scsi_disk/5:0:0:0
196 * SUBSYSTEM=scsi_disk
199 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0/5:0:0:0/scsi_device/5:0:0:0
200 * SUBSYSTEM=scsi_device
202 * Case with explicit $MAJOR/$MINOR (no need to read /sys/$DEVPATH/dev?):
204 * DEVNAME=bsg/5:0:0:0
205 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0/5:0:0:0/bsg/5:0:0:0
211 * DEVPATH=/devices/virtual/bdi/8:16
222 * Case with ACTION=change:
227 * DISK_MEDIA_CHANGE=1
233 static const char keywords[] ALIGN1 = "add\0remove\0change\0";
234 enum { OP_add, OP_remove };
241 struct bb_uidgid_t ugid;
244 IF_FEATURE_MDEV_EXEC(char *r_cmd;)
249 int root_major, root_minor;
252 #if ENABLE_FEATURE_MDEV_CONF
253 const char *filename;
255 struct rule **rule_vec;
258 struct rule cur_rule;
260 #define G (*(struct globals*)&bb_common_bufsiz1)
261 #define INIT_G() do { \
262 IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.maj = -1;) \
263 IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.mode = 0660;) \
267 /* Prevent infinite loops in /sys symlinks */
268 #define MAX_SYSFS_DEPTH 3
270 /* We use additional 64+ bytes in make_device() */
271 #define SCRATCH_SIZE 80
274 # define dbg(...) bb_error_msg(__VA_ARGS__)
276 # define dbg(...) ((void)0)
280 #if ENABLE_FEATURE_MDEV_CONF
282 static void make_default_cur_rule(void)
284 memset(&G.cur_rule, 0, sizeof(G.cur_rule));
285 G.cur_rule.maj = -1; /* "not a @major,minor rule" */
286 G.cur_rule.mode = 0660;
289 static void clean_up_cur_rule(void)
291 free(G.cur_rule.envvar);
292 if (G.cur_rule.regex_compiled)
293 regfree(&G.cur_rule.match);
294 free(G.cur_rule.ren_mov);
295 IF_FEATURE_MDEV_EXEC(free(G.cur_rule.r_cmd);)
296 make_default_cur_rule();
299 static void parse_next_rule(void)
301 /* Note: on entry, G.cur_rule is set to default */
306 /* No PARSE_EOL_COMMENTS, because command may contain '#' chars */
307 if (!config_read(G.parser, tokens, 4, 3, "# \t", PARSE_NORMAL & ~PARSE_EOL_COMMENTS))
310 /* Fields: [-]regex uid:gid mode [alias] [cmd] */
311 dbg("token1:'%s'", tokens[1]);
315 G.cur_rule.keep_matching = ('-' == val[0]);
316 val += G.cur_rule.keep_matching; /* swallow leading dash */
318 /* @major,minor[-minor2] */
319 /* (useful when name is ambiguous:
320 * "/sys/class/usb/lp0" and
321 * "/sys/class/printer/lp0")
323 int sc = sscanf(val, "@%u,%u-%u", &G.cur_rule.maj, &G.cur_rule.min0, &G.cur_rule.min1);
324 if (sc < 2 || G.cur_rule.maj < 0) {
325 bb_error_msg("bad @maj,min on line %d", G.parser->lineno);
329 G.cur_rule.min1 = G.cur_rule.min0;
332 char *eq = strchr(++val, '=');
334 bb_error_msg("bad $envvar=regex on line %d", G.parser->lineno);
337 G.cur_rule.envvar = xstrndup(val, eq - val);
340 xregcomp(&G.cur_rule.match, val, REG_EXTENDED);
341 G.cur_rule.regex_compiled = 1;
344 /* 2nd field: uid:gid - device ownership */
345 if (get_uidgid(&G.cur_rule.ugid, tokens[1], /*allow_numeric:*/ 1) == 0) {
346 bb_error_msg("unknown user/group '%s' on line %d", tokens[1], G.parser->lineno);
350 /* 3rd field: mode - device permissions */
351 bb_parse_mode(tokens[2], &G.cur_rule.mode);
353 /* 4th field (opt): ">|=alias" or "!" to not create the node */
355 if (ENABLE_FEATURE_MDEV_RENAME && val && strchr(">=!", val[0])) {
356 char *s = skip_non_whitespace(val);
357 G.cur_rule.ren_mov = xstrndup(val, s - val);
358 val = skip_whitespace(s);
361 if (ENABLE_FEATURE_MDEV_EXEC && val && val[0]) {
362 const char *s = "$@*";
363 const char *s2 = strchr(s, val[0]);
365 bb_error_msg("bad line %u", G.parser->lineno);
368 IF_FEATURE_MDEV_EXEC(G.cur_rule.r_cmd = xstrdup(val);)
374 } /* while (config_read) */
376 dbg("config_close(G.parser)");
377 config_close(G.parser);
383 /* If mdev -s, we remember rules in G.rule_vec[].
384 * Otherwise, there is no point in doing it, and we just
385 * save only one parsed rule in G.cur_rule.
387 static const struct rule *next_rule(void)
391 /* Open conf file if we didn't do it yet */
392 if (!G.parser && G.filename) {
393 dbg("config_open('%s')", G.filename);
394 G.parser = config_open2(G.filename, fopen_for_read);
400 /* Do we have rule parsed already? */
401 if (G.rule_vec[G.rule_idx]) {
402 dbg("< G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]);
403 return G.rule_vec[G.rule_idx++];
405 make_default_cur_rule();
411 /* Parse one more rule if file isn't fully read */
415 if (G.rule_vec) { /* mdev -s */
416 rule = memcpy(xmalloc(sizeof(G.cur_rule)), &G.cur_rule, sizeof(G.cur_rule));
417 G.rule_vec = xrealloc_vector(G.rule_vec, 4, G.rule_idx);
418 G.rule_vec[G.rule_idx++] = rule;
419 dbg("> G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]);
428 # define next_rule() (&G.cur_rule)
432 /* Builds an alias path.
433 * This function potentionally reallocates the alias parameter.
434 * Only used for ENABLE_FEATURE_MDEV_RENAME
436 static char *build_alias(char *alias, const char *device_name)
440 /* ">bar/": rename to bar/device_name */
441 /* ">bar[/]baz": rename to bar[/]baz */
442 dest = strrchr(alias, '/');
443 if (dest) { /* ">bar/[baz]" ? */
444 *dest = '\0'; /* mkdir bar */
445 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
447 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
449 alias = concat_path_file(alias, device_name);
457 /* mknod in /dev based on a path like "/sys/block/hda/hda1"
458 * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes
459 * after NUL, but we promise to not mangle (IOW: to restore if needed)
461 * NB2: "mdev -s" may call us many times, do not leak memory/fds!
463 * device_name = $DEVNAME (may be NULL)
464 * path = /sys/$DEVPATH
466 static void make_device(char *device_name, char *path, int operation)
468 int major, minor, type, len;
471 bb_error_msg("device: %s, %s", device_name, path);
473 /* Try to read major/minor string. Note that the kernel puts \n after
474 * the data, so we don't need to worry about null terminating the string
475 * because sscanf() will stop at the first nondigit, which \n is.
476 * We also depend on path having writeable space after it.
479 if (operation == OP_add) {
480 char *dev_maj_min = path + strlen(path);
482 strcpy(dev_maj_min, "/dev");
483 len = open_read_close(path, dev_maj_min + 1, 64);
486 if (!ENABLE_FEATURE_MDEV_EXEC)
488 /* no "dev" file, but we can still run scripts
489 * based on device name */
490 } else if (sscanf(++dev_maj_min, "%u:%u", &major, &minor) == 2) {
492 bb_error_msg("maj,min: %u,%u", major, minor);
497 /* else: for delete, -1 still deletes the node, but < -1 suppresses that */
499 /* Determine device name, type, major and minor */
501 device_name = (char*) bb_basename(path);
502 /* http://kernel.org/doc/pending/hotplug.txt says that only
503 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
504 * But since 2.6.25 block devices are also in /sys/class/block.
505 * We use strstr("/block/") to forestall future surprises.
508 if (strstr(path, "/block/") || (G.subsystem && strncmp(G.subsystem, "block", 5) == 0))
511 #if ENABLE_FEATURE_MDEV_CONF
512 G.rule_idx = 0; /* restart from the beginning (think mdev -s) */
515 const char *str_to_match;
516 regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP];
519 char aliaslink = aliaslink; /* for compiler */
521 const struct rule *rule;
523 str_to_match = device_name;
527 #if ENABLE_FEATURE_MDEV_CONF
528 if (rule->maj >= 0) { /* @maj,min rule */
529 if (major != rule->maj)
531 if (minor < rule->min0 || minor > rule->min1)
533 memset(off, 0, sizeof(off));
536 if (rule->envvar) { /* $envvar=regex rule */
537 str_to_match = getenv(rule->envvar);
538 dbg("getenv('%s'):'%s'", rule->envvar, str_to_match);
542 /* else: str_to_match = device_name */
544 if (rule->regex_compiled) {
545 int regex_match = regexec(&rule->match, str_to_match, ARRAY_SIZE(off), off, 0);
546 dbg("regex_match for '%s':%d", str_to_match, regex_match);
547 //bb_error_msg("matches:");
548 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
549 // if (off[i].rm_so < 0) continue;
550 // bb_error_msg("match %d: '%.*s'\n", i,
551 // (int)(off[i].rm_eo - off[i].rm_so),
552 // device_name + off[i].rm_so);
556 /* regexec returns whole pattern as "range" 0 */
558 || (int)off[0].rm_eo != (int)strlen(str_to_match)
560 continue; /* this rule doesn't match */
563 /* else: it's final implicit "match-all" rule */
568 /* Build alias name */
570 if (ENABLE_FEATURE_MDEV_RENAME && rule->ren_mov) {
571 aliaslink = rule->ren_mov[0];
572 if (aliaslink == '!') {
573 /* "!": suppress node creation/deletion */
576 else if (aliaslink == '>' || aliaslink == '=') {
577 if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) {
582 /* substitute %1..9 with off[1..9], if any */
589 p = alias = xzalloc(strlen(rule->ren_mov) + n * strlen(str_to_match));
590 s = rule->ren_mov + 1;
594 unsigned i = (s[1] - '0');
595 if (i <= 9 && off[i].rm_so >= 0) {
596 n = off[i].rm_eo - off[i].rm_so;
597 strncpy(p, str_to_match + off[i].rm_so, n);
606 alias = xstrdup(rule->ren_mov + 1);
610 dbg("alias:'%s'", alias);
613 IF_FEATURE_MDEV_EXEC(command = rule->r_cmd;)
615 const char *s = "$@*";
616 const char *s2 = strchr(s, command[0]);
618 /* Are we running this command now?
619 * Run $cmd on delete, @cmd on create, *cmd on both
621 if (s2 - s != (operation == OP_remove) || *s2 == '*') {
622 /* We are here if: '*',
623 * or: '@' and delete = 0,
624 * or: '$' and delete = 1
631 dbg("command:'%s'", command);
633 /* "Execute" the line we found */
634 node_name = device_name;
635 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
636 node_name = alias = build_alias(alias, device_name);
637 dbg("alias2:'%s'", alias);
640 if (operation == OP_add && major >= 0) {
641 char *slash = strrchr(node_name, '/');
644 bb_make_directory(node_name, 0755, FILEUTILS_RECUR);
648 bb_error_msg("mknod: %s (%d,%d) %o", node_name, major, minor, rule->mode | type);
649 if (mknod(node_name, rule->mode | type, makedev(major, minor)) && errno != EEXIST)
650 bb_perror_msg("can't create '%s'", node_name);
651 if (ENABLE_FEATURE_MDEV_CONF) {
652 chmod(node_name, rule->mode);
653 chown(node_name, rule->ugid.uid, rule->ugid.gid);
655 if (major == G.root_major && minor == G.root_minor)
656 symlink(node_name, "root");
657 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
658 if (aliaslink == '>') {
659 //TODO: on devtmpfs, device_name already exists and symlink() fails.
660 //End result is that instead of symlink, we have two nodes.
661 //What should be done?
663 bb_error_msg("symlink: %s", device_name);
664 symlink(node_name, device_name);
669 if (ENABLE_FEATURE_MDEV_EXEC && command) {
670 /* setenv will leak memory, use putenv/unsetenv/free */
671 char *s = xasprintf("%s=%s", "MDEV", node_name);
672 char *s1 = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem);
676 bb_error_msg("running: %s", command);
677 if (system(command) == -1)
678 bb_perror_msg("can't run '%s'", command);
679 bb_unsetenv_and_free(s1);
680 bb_unsetenv_and_free(s);
683 if (operation == OP_remove && major >= -1) {
684 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
685 if (aliaslink == '>') {
687 bb_error_msg("unlink: %s", device_name);
692 bb_error_msg("unlink: %s", node_name);
696 if (ENABLE_FEATURE_MDEV_RENAME)
699 /* We found matching line.
700 * Stop unless it was prefixed with '-'
702 if (!ENABLE_FEATURE_MDEV_CONF || !rule->keep_matching)
707 /* File callback for /sys/ traversal */
708 static int FAST_FUNC fileAction(const char *fileName,
709 struct stat *statbuf UNUSED_PARAM,
711 int depth UNUSED_PARAM)
713 size_t len = strlen(fileName) - 4; /* can't underflow */
714 char *scratch = userData;
716 /* len check is for paranoid reasons */
717 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
720 strcpy(scratch, fileName);
722 make_device(/*DEVNAME:*/ NULL, scratch, OP_add);
727 /* Directory callback for /sys/ traversal */
728 static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
729 struct stat *statbuf UNUSED_PARAM,
730 void *userData UNUSED_PARAM,
733 /* Extract device subsystem -- the name of the directory
734 * under /sys/class/ */
737 G.subsystem = strrchr(fileName, '/');
739 G.subsystem = xstrdup(G.subsystem + 1);
742 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
745 /* For the full gory details, see linux/Documentation/firmware_class/README
747 * Firmware loading works like this:
748 * - kernel sets FIRMWARE env var
749 * - userspace checks /lib/firmware/$FIRMWARE
750 * - userspace waits for /sys/$DEVPATH/loading to appear
751 * - userspace writes "1" to /sys/$DEVPATH/loading
752 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
753 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
754 * - kernel loads firmware into device
756 static void load_firmware(const char *firmware, const char *sysfs_path)
759 int firmware_fd, loading_fd;
761 /* check for /lib/firmware/$FIRMWARE */
762 xchdir("/lib/firmware");
763 firmware_fd = open(firmware, O_RDONLY); /* can fail */
765 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
767 for (cnt = 0; cnt < 30; ++cnt) {
768 loading_fd = open("loading", O_WRONLY);
777 if (firmware_fd >= 0) {
780 /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */
781 if (full_write(loading_fd, "1", 1) != 1)
784 /* load firmware into /sys/$DEVPATH/data */
785 data_fd = open("data", O_WRONLY);
788 cnt = bb_copyfd_eof(firmware_fd, data_fd);
789 if (ENABLE_FEATURE_CLEAN_UP)
793 /* Tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading"
794 * Note: we emit -1 also if firmware file wasn't found.
795 * There are cases when otherwise kernel would wait for minutes
799 full_write(loading_fd, "0", 1);
801 full_write(loading_fd, "-1", 2);
804 if (ENABLE_FEATURE_CLEAN_UP) {
810 int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
811 int mdev_main(int argc UNUSED_PARAM, char **argv)
813 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
817 #if ENABLE_FEATURE_MDEV_CONF
818 G.filename = "/etc/mdev.conf";
821 /* We can be called as hotplug helper */
822 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
825 /* Force the configuration file settings exactly */
830 if (argv[1] && strcmp(argv[1], "-s") == 0) {
836 #if ENABLE_FEATURE_MDEV_CONF
837 /* Same as xrealloc_vector(NULL, 4, 0): */
838 G.rule_vec = xzalloc((1 << 4) * sizeof(*G.rule_vec));
841 G.root_major = major(st.st_dev);
842 G.root_minor = minor(st.st_dev);
844 /* ACTION_FOLLOWLINKS is needed since in newer kernels
845 * /sys/block/loop* (for example) are symlinks to dirs,
846 * not real directories.
847 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
848 * but we can't enforce that on users)
850 if (access("/sys/class/block", F_OK) != 0) {
851 /* Scan obsolete /sys/block only if /sys/class/block
852 * doesn't exist. Otherwise we'll have dupes.
853 * Also, do not complain if it doesn't exist.
854 * Some people configure kernel to have no blockdevs.
856 recursive_action("/sys/block",
857 ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET,
858 fileAction, dirAction, temp, 0);
860 recursive_action("/sys/class",
861 ACTION_RECURSE | ACTION_FOLLOWLINKS,
862 fileAction, dirAction, temp, 0);
872 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
873 * ACTION can be "add" or "remove"
874 * DEVPATH is like "/block/sda" or "/class/input/mice"
876 action = getenv("ACTION");
877 op = index_in_strings(keywords, action);
878 env_devname = getenv("DEVNAME"); /* can be NULL */
879 env_devpath = getenv("DEVPATH");
880 G.subsystem = getenv("SUBSYSTEM");
881 if (!action || !env_devpath /*|| !G.subsystem*/)
883 fw = getenv("FIRMWARE");
884 /* If it exists, does /dev/mdev.seq match $SEQNUM?
885 * If it does not match, earlier mdev is running
886 * in parallel, and we need to wait */
887 seq = getenv("SEQNUM");
889 int timeout = 2000 / 32; /* 2000 msec */
892 char seqbuf[sizeof(int)*3 + 2];
894 seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf) - 1);
899 seqbuf[seqlen] = '\0';
900 if (seqbuf[0] == '\n' /* seed file? */
901 || strcmp(seq, seqbuf) == 0 /* correct idx? */
910 int logfd = open("/dev/mdev.log", O_WRONLY | O_APPEND);
912 xmove_fd(logfd, STDERR_FILENO);
914 bb_error_msg("seq: %s action: %s", seq, action);
918 snprintf(temp, PATH_MAX, "/sys%s", env_devpath);
919 if (op == OP_remove) {
920 /* Ignoring "remove firmware". It was reported
921 * to happen and to cause erroneous deletion
922 * of device nodes. */
924 make_device(env_devname, temp, op);
926 else if (op == OP_add) {
927 make_device(env_devname, temp, op);
928 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
930 load_firmware(fw, temp);
935 xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1));
939 if (ENABLE_FEATURE_CLEAN_UP)
940 RELEASE_CONFIG_BUFFER(temp);