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."
100 /* "mdev -s" scans /sys/class/xxx, looking for directories which have dev
101 * file (it is of the form "M:m\n"). Example: /sys/class/tty/tty0/dev
102 * contains "4:0\n". Directory name is taken as device name, path component
103 * directly after /sys/class/ as subsystem. In this example, "tty0" and "tty".
104 * Then mdev creates the /dev/device_name node.
105 * If /sys/class/.../dev file does not exist, mdev still may act
106 * on this device: see "@|$|*command args..." parameter in config file.
108 * mdev w/o parameters is called as hotplug helper. It takes device
109 * and subsystem names from $DEVPATH and $SUBSYSTEM, extracts
110 * maj,min from "/sys/$DEVPATH/dev" and also examines
111 * $ACTION ("add"/"delete") and $FIRMWARE.
113 * If action is "add", mdev creates /dev/device_name similarly to mdev -s.
114 * (todo: explain "delete" and $FIRMWARE)
116 * If /etc/mdev.conf exists, it may modify /dev/device_name's properties.
118 * Leading minus in 1st field means "don't stop on this line", otherwise
119 * search is stopped after the matching line is encountered.
121 * $envvar=regex format is useful for loading modules for hot-plugged devices
122 * which do not have driver loaded yet. In this case /sys/class/.../dev
123 * does not exist, but $MODALIAS is set to needed module's name
124 * (actually, an alias to it) by kernel. This rule instructs mdev
125 * to load the module and exit:
126 * $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"
127 * The kernel will generate another hotplug event when /sys/class/.../dev
130 * When line matches, the device node is created, chmod'ed and chown'ed,
131 * moved to path, and if >path, a symlink to moved node is created,
132 * all this if /sys/class/.../dev exists.
134 * =loop/ - moves to /dev/loop
135 * >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink
137 * Then "command args..." is executed (via sh -c 'command args...').
138 * @:execute on creation, $:on deletion, *:on both.
139 * This happens regardless of /sys/class/.../dev existence.
145 bool regex_has_slash
;
148 struct bb_uidgid_t ugid
;
151 IF_FEATURE_MDEV_EXEC(char *r_cmd
;)
156 int root_major
, root_minor
;
158 #if ENABLE_FEATURE_MDEV_CONF
159 const char *filename
;
161 struct rule
**rule_vec
;
164 struct rule cur_rule
;
166 #define G (*(struct globals*)&bb_common_bufsiz1)
167 #define INIT_G() do { \
168 IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.maj = -1;) \
169 IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.mode = 0660;) \
173 /* Prevent infinite loops in /sys symlinks */
174 #define MAX_SYSFS_DEPTH 3
176 /* We use additional 64+ bytes in make_device() */
177 #define SCRATCH_SIZE 80
180 # define dbg(...) bb_error_msg(__VA_ARGS__)
182 # define dbg(...) ((void)0)
186 #if ENABLE_FEATURE_MDEV_CONF
188 static void make_default_cur_rule(void)
190 memset(&G
.cur_rule
, 0, sizeof(G
.cur_rule
));
191 G
.cur_rule
.maj
= -1; /* "not a @major,minor rule" */
192 G
.cur_rule
.mode
= 0660;
195 static void clean_up_cur_rule(void)
197 free(G
.cur_rule
.envvar
);
198 if (G
.cur_rule
.regex_compiled
)
199 regfree(&G
.cur_rule
.match
);
200 free(G
.cur_rule
.ren_mov
);
201 IF_FEATURE_MDEV_EXEC(free(G
.cur_rule
.r_cmd
);)
202 make_default_cur_rule();
205 static void parse_next_rule(void)
207 /* Note: on entry, G.cur_rule is set to default */
212 /* No PARSE_EOL_COMMENTS, because command may contain '#' chars */
213 if (!config_read(G
.parser
, tokens
, 4, 3, "# \t", PARSE_NORMAL
& ~PARSE_EOL_COMMENTS
))
216 /* Fields: [-]regex uid:gid mode [alias] [cmd] */
217 dbg("token1:'%s'", tokens
[1]);
221 G
.cur_rule
.keep_matching
= ('-' == val
[0]);
222 val
+= G
.cur_rule
.keep_matching
; /* swallow leading dash */
224 /* @major,minor[-minor2] */
225 /* (useful when name is ambiguous:
226 * "/sys/class/usb/lp0" and
227 * "/sys/class/printer/lp0")
229 int sc
= sscanf(val
, "@%u,%u-%u", &G
.cur_rule
.maj
, &G
.cur_rule
.min0
, &G
.cur_rule
.min1
);
230 if (sc
< 2 || G
.cur_rule
.maj
< 0) {
231 bb_error_msg("bad @maj,min on line %d", G
.parser
->lineno
);
235 G
.cur_rule
.min1
= G
.cur_rule
.min0
;
238 char *eq
= strchr(++val
, '=');
240 bb_error_msg("bad $envvar=regex on line %d", G
.parser
->lineno
);
243 G
.cur_rule
.envvar
= xstrndup(val
, eq
- val
);
246 xregcomp(&G
.cur_rule
.match
, val
, REG_EXTENDED
);
247 G
.cur_rule
.regex_compiled
= 1;
248 G
.cur_rule
.regex_has_slash
= (strchr(val
, '/') != NULL
);
251 /* 2nd field: uid:gid - device ownership */
252 if (get_uidgid(&G
.cur_rule
.ugid
, tokens
[1], /*allow_numeric:*/ 1) == 0) {
253 bb_error_msg("unknown user/group '%s' on line %d", tokens
[1], G
.parser
->lineno
);
257 /* 3rd field: mode - device permissions */
258 bb_parse_mode(tokens
[2], &G
.cur_rule
.mode
);
260 /* 4th field (opt): ">|=alias" or "!" to not create the node */
262 if (ENABLE_FEATURE_MDEV_RENAME
&& val
&& strchr(">=!", val
[0])) {
263 char *s
= skip_non_whitespace(val
);
264 G
.cur_rule
.ren_mov
= xstrndup(val
, s
- val
);
265 val
= skip_whitespace(s
);
268 if (ENABLE_FEATURE_MDEV_EXEC
&& val
&& val
[0]) {
269 const char *s
= "$@*";
270 const char *s2
= strchr(s
, val
[0]);
272 bb_error_msg("bad line %u", G
.parser
->lineno
);
275 IF_FEATURE_MDEV_EXEC(G
.cur_rule
.r_cmd
= xstrdup(val
);)
281 } /* while (config_read) */
283 dbg("config_close(G.parser)");
284 config_close(G
.parser
);
290 /* If mdev -s, we remember rules in G.rule_vec[].
291 * Otherwise, there is no point in doing it, and we just
292 * save only one parsed rule in G.cur_rule.
294 static const struct rule
*next_rule(void)
298 /* Open conf file if we didn't do it yet */
299 if (!G
.parser
&& G
.filename
) {
300 dbg("config_open('%s')", G
.filename
);
301 G
.parser
= config_open2(G
.filename
, fopen_for_read
);
307 /* Do we have rule parsed already? */
308 if (G
.rule_vec
[G
.rule_idx
]) {
309 dbg("< G.rule_vec[G.rule_idx:%d]=%p", G
.rule_idx
, G
.rule_vec
[G
.rule_idx
]);
310 return G
.rule_vec
[G
.rule_idx
++];
312 make_default_cur_rule();
318 /* Parse one more rule if file isn't fully read */
322 if (G
.rule_vec
) { /* mdev -s */
323 rule
= memcpy(xmalloc(sizeof(G
.cur_rule
)), &G
.cur_rule
, sizeof(G
.cur_rule
));
324 G
.rule_vec
= xrealloc_vector(G
.rule_vec
, 4, G
.rule_idx
);
325 G
.rule_vec
[G
.rule_idx
++] = rule
;
326 dbg("> G.rule_vec[G.rule_idx:%d]=%p", G
.rule_idx
, G
.rule_vec
[G
.rule_idx
]);
335 # define next_rule() (&G.cur_rule)
339 /* Builds an alias path.
340 * This function potentionally reallocates the alias parameter.
341 * Only used for ENABLE_FEATURE_MDEV_RENAME
343 static char *build_alias(char *alias
, const char *device_name
)
347 /* ">bar/": rename to bar/device_name */
348 /* ">bar[/]baz": rename to bar[/]baz */
349 dest
= strrchr(alias
, '/');
350 if (dest
) { /* ">bar/[baz]" ? */
351 *dest
= '\0'; /* mkdir bar */
352 bb_make_directory(alias
, 0755, FILEUTILS_RECUR
);
354 if (dest
[1] == '\0') { /* ">bar/" => ">bar/device_name" */
356 alias
= concat_path_file(alias
, device_name
);
364 /* mknod in /dev based on a path like "/sys/block/hda/hda1"
365 * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes
366 * after NUL, but we promise to not mangle (IOW: to restore if needed)
368 * NB2: "mdev -s" may call us many times, do not leak memory/fds!
370 static void make_device(char *path
, int delete)
372 char *device_name
, *subsystem_slash_devname
;
373 int major
, minor
, type
, len
;
375 dbg("%s('%s', delete:%d)", __func__
, path
, delete);
377 /* Try to read major/minor string. Note that the kernel puts \n after
378 * the data, so we don't need to worry about null terminating the string
379 * because sscanf() will stop at the first nondigit, which \n is.
380 * We also depend on path having writeable space after it.
384 char *dev_maj_min
= path
+ strlen(path
);
386 strcpy(dev_maj_min
, "/dev");
387 len
= open_read_close(path
, dev_maj_min
+ 1, 64);
390 if (!ENABLE_FEATURE_MDEV_EXEC
)
392 /* no "dev" file, but we can still run scripts
393 * based on device name */
394 } else if (sscanf(++dev_maj_min
, "%u:%u", &major
, &minor
) != 2) {
398 /* else: for delete, -1 still deletes the node, but < -1 suppresses that */
400 /* Determine device name, type, major and minor */
401 device_name
= (char*) bb_basename(path
);
402 /* http://kernel.org/doc/pending/hotplug.txt says that only
403 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
404 * But since 2.6.25 block devices are also in /sys/class/block.
405 * We use strstr("/block/") to forestall future surprises. */
407 if (strstr(path
, "/block/") || (G
.subsystem
&& strncmp(G
.subsystem
, "block", 5) == 0))
410 /* Make path point to "subsystem/device_name" */
411 subsystem_slash_devname
= NULL
;
412 /* Check for coldplug invocations first */
413 if (strncmp(path
, "/sys/block/", 11) == 0) /* legacy case */
414 path
+= sizeof("/sys/") - 1;
415 else if (strncmp(path
, "/sys/class/", 11) == 0)
416 path
+= sizeof("/sys/class/") - 1;
418 /* Example of a hotplug invocation:
420 * DEVPATH="/sys" + "/devices/virtual/mtd/mtd3/mtdblock3"
421 * ("/sys" is added by mdev_main)
422 * - path does not contain subsystem
424 subsystem_slash_devname
= concat_path_file(G
.subsystem
, device_name
);
425 path
= subsystem_slash_devname
;
428 #if ENABLE_FEATURE_MDEV_CONF
429 G
.rule_idx
= 0; /* restart from the beginning (think mdev -s) */
432 const char *str_to_match
;
433 regmatch_t off
[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP
];
436 char aliaslink
= aliaslink
; /* for compiler */
437 const char *node_name
;
438 const struct rule
*rule
;
444 #if ENABLE_FEATURE_MDEV_CONF
445 if (rule
->maj
>= 0) { /* @maj,min rule */
446 if (major
!= rule
->maj
)
448 if (minor
< rule
->min0
|| minor
> rule
->min1
)
450 memset(off
, 0, sizeof(off
));
453 if (rule
->envvar
) { /* $envvar=regex rule */
454 str_to_match
= getenv(rule
->envvar
);
455 dbg("getenv('%s'):'%s'", rule
->envvar
, str_to_match
);
459 /* regex to match [subsystem/]device_name */
460 str_to_match
= (rule
->regex_has_slash
? path
: device_name
);
463 if (rule
->regex_compiled
) {
464 int regex_match
= regexec(&rule
->match
, str_to_match
, ARRAY_SIZE(off
), off
, 0);
465 dbg("regex_match for '%s':%d", str_to_match
, regex_match
);
466 //bb_error_msg("matches:");
467 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
468 // if (off[i].rm_so < 0) continue;
469 // bb_error_msg("match %d: '%.*s'\n", i,
470 // (int)(off[i].rm_eo - off[i].rm_so),
471 // device_name + off[i].rm_so);
475 /* regexec returns whole pattern as "range" 0 */
477 || (int)off
[0].rm_eo
!= (int)strlen(str_to_match
)
479 continue; /* this rule doesn't match */
482 /* else: it's final implicit "match-all" rule */
487 /* Build alias name */
489 if (ENABLE_FEATURE_MDEV_RENAME
&& rule
->ren_mov
) {
490 aliaslink
= rule
->ren_mov
[0];
491 if (aliaslink
== '!') {
492 /* "!": suppress node creation/deletion */
495 else if (aliaslink
== '>' || aliaslink
== '=') {
496 if (ENABLE_FEATURE_MDEV_RENAME_REGEXP
) {
501 /* substitute %1..9 with off[1..9], if any */
508 p
= alias
= xzalloc(strlen(rule
->ren_mov
) + n
* strlen(str_to_match
));
509 s
= rule
->ren_mov
+ 1;
513 unsigned i
= (s
[1] - '0');
514 if (i
<= 9 && off
[i
].rm_so
>= 0) {
515 n
= off
[i
].rm_eo
- off
[i
].rm_so
;
516 strncpy(p
, str_to_match
+ off
[i
].rm_so
, n
);
525 alias
= xstrdup(rule
->ren_mov
+ 1);
529 dbg("alias:'%s'", alias
);
532 IF_FEATURE_MDEV_EXEC(command
= rule
->r_cmd
;)
534 const char *s
= "$@*";
535 const char *s2
= strchr(s
, command
[0]);
537 /* Are we running this command now?
538 * Run $cmd on delete, @cmd on create, *cmd on both
540 if (s2
- s
!= delete) {
541 /* We are here if: '*',
542 * or: '@' and delete = 0,
543 * or: '$' and delete = 1
550 dbg("command:'%s'", command
);
552 /* "Execute" the line we found */
553 node_name
= device_name
;
554 if (ENABLE_FEATURE_MDEV_RENAME
&& alias
) {
555 node_name
= alias
= build_alias(alias
, device_name
);
556 dbg("alias2:'%s'", alias
);
559 if (!delete && major
>= 0) {
560 dbg("mknod('%s',%o,(%d,%d))", node_name
, rule
->mode
| type
, major
, minor
);
561 if (mknod(node_name
, rule
->mode
| type
, makedev(major
, minor
)) && errno
!= EEXIST
)
562 bb_perror_msg("can't create '%s'", node_name
);
563 if (major
== G
.root_major
&& minor
== G
.root_minor
)
564 symlink(node_name
, "root");
565 if (ENABLE_FEATURE_MDEV_CONF
) {
566 chmod(node_name
, rule
->mode
);
567 chown(node_name
, rule
->ugid
.uid
, rule
->ugid
.gid
);
569 if (ENABLE_FEATURE_MDEV_RENAME
&& alias
) {
570 if (aliaslink
== '>') {
571 //TODO: on devtmpfs, device_name already exists and symlink() fails.
572 //End result is that instead of symlink, we have two nodes.
573 //What should be done?
574 symlink(node_name
, device_name
);
579 if (ENABLE_FEATURE_MDEV_EXEC
&& command
) {
580 /* setenv will leak memory, use putenv/unsetenv/free */
581 char *s
= xasprintf("%s=%s", "MDEV", node_name
);
582 char *s1
= xasprintf("%s=%s", "SUBSYSTEM", G
.subsystem
);
585 if (system(command
) == -1)
586 bb_perror_msg("can't run '%s'", command
);
587 bb_unsetenv_and_free(s1
);
588 bb_unsetenv_and_free(s
);
591 if (delete && major
>= -1) {
592 if (ENABLE_FEATURE_MDEV_RENAME
&& alias
) {
593 if (aliaslink
== '>')
599 if (ENABLE_FEATURE_MDEV_RENAME
)
602 /* We found matching line.
603 * Stop unless it was prefixed with '-'
605 if (!ENABLE_FEATURE_MDEV_CONF
|| !rule
->keep_matching
)
609 free(subsystem_slash_devname
);
612 /* File callback for /sys/ traversal */
613 static int FAST_FUNC
fileAction(const char *fileName
,
614 struct stat
*statbuf UNUSED_PARAM
,
616 int depth UNUSED_PARAM
)
618 size_t len
= strlen(fileName
) - 4; /* can't underflow */
619 char *scratch
= userData
;
621 /* len check is for paranoid reasons */
622 if (strcmp(fileName
+ len
, "/dev") != 0 || len
>= PATH_MAX
)
625 strcpy(scratch
, fileName
);
627 make_device(scratch
, /*delete:*/ 0);
632 /* Directory callback for /sys/ traversal */
633 static int FAST_FUNC
dirAction(const char *fileName UNUSED_PARAM
,
634 struct stat
*statbuf UNUSED_PARAM
,
635 void *userData UNUSED_PARAM
,
638 /* Extract device subsystem -- the name of the directory
639 * under /sys/class/ */
642 G
.subsystem
= strrchr(fileName
, '/');
644 G
.subsystem
= xstrdup(G
.subsystem
+ 1);
647 return (depth
>= MAX_SYSFS_DEPTH
? SKIP
: TRUE
);
650 /* For the full gory details, see linux/Documentation/firmware_class/README
652 * Firmware loading works like this:
653 * - kernel sets FIRMWARE env var
654 * - userspace checks /lib/firmware/$FIRMWARE
655 * - userspace waits for /sys/$DEVPATH/loading to appear
656 * - userspace writes "1" to /sys/$DEVPATH/loading
657 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
658 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
659 * - kernel loads firmware into device
661 static void load_firmware(const char *firmware
, const char *sysfs_path
)
664 int firmware_fd
, loading_fd
;
666 /* check for /lib/firmware/$FIRMWARE */
667 xchdir("/lib/firmware");
668 firmware_fd
= open(firmware
, O_RDONLY
); /* can fail */
670 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
672 for (cnt
= 0; cnt
< 30; ++cnt
) {
673 loading_fd
= open("loading", O_WRONLY
);
682 if (firmware_fd
>= 0) {
685 /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */
686 if (full_write(loading_fd
, "1", 1) != 1)
689 /* load firmware into /sys/$DEVPATH/data */
690 data_fd
= open("data", O_WRONLY
);
693 cnt
= bb_copyfd_eof(firmware_fd
, data_fd
);
694 if (ENABLE_FEATURE_CLEAN_UP
)
698 /* Tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading"
699 * Note: we emit -1 if firmware file wasn't found.
700 * There are cases when otherwise kernel would wait for minutes
704 full_write(loading_fd
, "0", 1);
706 full_write(loading_fd
, "-1", 2);
709 if (ENABLE_FEATURE_CLEAN_UP
) {
715 int mdev_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
716 int mdev_main(int argc UNUSED_PARAM
, char **argv
)
718 RESERVE_CONFIG_BUFFER(temp
, PATH_MAX
+ SCRATCH_SIZE
);
722 #if ENABLE_FEATURE_MDEV_CONF
723 G
.filename
= "/etc/mdev.conf";
726 /* We can be called as hotplug helper */
727 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
730 /* Force the configuration file settings exactly */
735 if (argv
[1] && strcmp(argv
[1], "-s") == 0) {
741 #if ENABLE_FEATURE_MDEV_CONF
742 /* Same as xrealloc_vector(NULL, 4, 0): */
743 G
.rule_vec
= xzalloc((1 << 4) * sizeof(*G
.rule_vec
));
746 G
.root_major
= major(st
.st_dev
);
747 G
.root_minor
= minor(st
.st_dev
);
749 /* ACTION_FOLLOWLINKS is needed since in newer kernels
750 * /sys/block/loop* (for example) are symlinks to dirs,
751 * not real directories.
752 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
753 * but we can't enforce that on users)
755 if (access("/sys/class/block", F_OK
) != 0) {
756 /* Scan obsolete /sys/block only if /sys/class/block
757 * doesn't exist. Otherwise we'll have dupes.
758 * Also, do not complain if it doesn't exist.
759 * Some people configure kernel to have no blockdevs.
761 recursive_action("/sys/block",
762 ACTION_RECURSE
| ACTION_FOLLOWLINKS
| ACTION_QUIET
,
763 fileAction
, dirAction
, temp
, 0);
765 recursive_action("/sys/class",
766 ACTION_RECURSE
| ACTION_FOLLOWLINKS
,
767 fileAction
, dirAction
, temp
, 0);
773 static const char keywords
[] ALIGN1
= "remove\0add\0";
774 enum { OP_remove
= 0, OP_add
};
778 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
779 * ACTION can be "add" or "remove"
780 * DEVPATH is like "/block/sda" or "/class/input/mice"
782 action
= getenv("ACTION");
783 env_path
= getenv("DEVPATH");
784 G
.subsystem
= getenv("SUBSYSTEM");
785 if (!action
|| !env_path
/*|| !G.subsystem*/)
787 fw
= getenv("FIRMWARE");
788 op
= index_in_strings(keywords
, action
);
789 /* If it exists, does /dev/mdev.seq match $SEQNUM?
790 * If it does not match, earlier mdev is running
791 * in parallel, and we need to wait */
792 seq
= getenv("SEQNUM");
794 int timeout
= 2000 / 32; /* 2000 msec */
797 char seqbuf
[sizeof(int)*3 + 2];
799 seqlen
= open_read_close("mdev.seq", seqbuf
, sizeof(seqbuf
) - 1);
804 seqbuf
[seqlen
] = '\0';
805 if (seqbuf
[0] == '\n' /* seed file? */
806 || strcmp(seq
, seqbuf
) == 0 /* correct idx? */
814 snprintf(temp
, PATH_MAX
, "/sys%s", env_path
);
815 if (op
== OP_remove
) {
816 /* Ignoring "remove firmware". It was reported
817 * to happen and to cause erroneous deletion
818 * of device nodes. */
820 make_device(temp
, /*delete:*/ 1);
822 else if (op
== OP_add
) {
823 make_device(temp
, /*delete:*/ 0);
824 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE
) {
826 load_firmware(fw
, temp
);
831 xopen_xwrite_close("mdev.seq", utoa(xatou(seq
) + 1));
835 if (ENABLE_FEATURE_CLEAN_UP
)
836 RELEASE_CONFIG_BUFFER(temp
);