blob: 6603a7d7949dfaef5f322617d14af48d3b9cd04c [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
2/*
Rob Landley70f7ef72005-12-13 08:21:33 +00003 * mdev - Mini udev for busybox
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00004 *
Rob Landley70f7ef72005-12-13 08:21:33 +00005 * Copyright 2005 Rob Landley <rob@landley.net>
6 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
7 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Rob Landley70f7ef72005-12-13 08:21:33 +00009 */
Pere Orga5bc8c002011-04-11 03:29:49 +020010
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020011//config:config MDEV
12//config: bool "mdev"
13//config: default y
14//config: select PLATFORM_LINUX
15//config: help
16//config: mdev is a mini-udev implementation for dynamically creating device
17//config: nodes in the /dev directory.
18//config:
19//config: For more information, please see docs/mdev.txt
20//config:
21//config:config FEATURE_MDEV_CONF
22//config: bool "Support /etc/mdev.conf"
23//config: default y
24//config: depends on MDEV
25//config: help
26//config: Add support for the mdev config file to control ownership and
27//config: permissions of the device nodes.
28//config:
29//config: For more information, please see docs/mdev.txt
30//config:
31//config:config FEATURE_MDEV_RENAME
32//config: bool "Support subdirs/symlinks"
33//config: default y
34//config: depends on FEATURE_MDEV_CONF
35//config: help
36//config: Add support for renaming devices and creating symlinks.
37//config:
38//config: For more information, please see docs/mdev.txt
39//config:
40//config:config FEATURE_MDEV_RENAME_REGEXP
41//config: bool "Support regular expressions substitutions when renaming device"
42//config: default y
43//config: depends on FEATURE_MDEV_RENAME
44//config: help
45//config: Add support for regular expressions substitutions when renaming
46//config: device.
47//config:
48//config:config FEATURE_MDEV_EXEC
49//config: bool "Support command execution at device addition/removal"
50//config: default y
51//config: depends on FEATURE_MDEV_CONF
52//config: help
53//config: This adds support for an optional field to /etc/mdev.conf for
54//config: executing commands when devices are created/removed.
55//config:
56//config: For more information, please see docs/mdev.txt
57//config:
58//config:config FEATURE_MDEV_LOAD_FIRMWARE
59//config: bool "Support loading of firmwares"
60//config: default y
61//config: depends on MDEV
62//config: help
63//config: Some devices need to load firmware before they can be usable.
64//config:
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.
68
69//applet:IF_MDEV(APPLET(mdev, BB_DIR_SBIN, BB_SUID_DROP))
70
71//kbuild:lib-$(CONFIG_MDEV) += mdev.o
72
Pere Orga5bc8c002011-04-11 03:29:49 +020073//usage:#define mdev_trivial_usage
74//usage: "[-s]"
75//usage:#define mdev_full_usage "\n\n"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020076//usage: "mdev -s is to be run during boot to scan /sys and populate /dev.\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020077//usage: "\n"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020078//usage: "Bare mdev is a kernel hotplug helper. To activate it:\n"
79//usage: " echo /sbin/mdev >/proc/sys/kernel/hotplug\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020080//usage: IF_FEATURE_MDEV_CONF(
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020081//usage: "\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020082//usage: "It uses /etc/mdev.conf with lines\n"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020083//usage: " [-]DEVNAME UID:GID PERM"
84//usage: IF_FEATURE_MDEV_RENAME(" [>|=PATH]|[!]")
Pere Orga5bc8c002011-04-11 03:29:49 +020085//usage: IF_FEATURE_MDEV_EXEC(" [@|$|*PROG]")
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020086//usage: "\n"
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"
Pere Orga5bc8c002011-04-11 03:29:49 +020091//usage: )
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020092//usage: "\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."
Denys Vlasenko516530c2012-05-18 04:45:35 +020096//usage: "\n"
97//usage: "If /dev/mdev.log file exists, debug log will be appended to it.\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020098
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000099#include "libbb.h"
Rob Landleyb56c2852005-12-17 10:52:30 +0000100#include "xregex.h"
Rob Landley70f7ef72005-12-13 08:21:33 +0000101
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000102/* "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.
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000107 * If /sys/class/.../dev file does not exist, mdev still may act
108 * on this device: see "@|$|*command args..." parameter in config file.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000109 *
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.
114 *
115 * If action is "add", mdev creates /dev/device_name similarly to mdev -s.
116 * (todo: explain "delete" and $FIRMWARE)
117 *
118 * If /etc/mdev.conf exists, it may modify /dev/device_name's properties.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000119 *
120 * Leading minus in 1st field means "don't stop on this line", otherwise
121 * search is stopped after the matching line is encountered.
122 *
Denys Vlasenko45f0a112011-09-22 04:55:47 +0200123 * $envvar=regex format is useful for loading modules for hot-plugged devices
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000124 * 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
130 * file appears.
131 *
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.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000135 * Examples:
136 * =loop/ - moves to /dev/loop
137 * >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000138 *
139 * Then "command args..." is executed (via sh -c 'command args...').
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000140 * @:execute on creation, $:on deletion, *:on both.
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000141 * This happens regardless of /sys/class/.../dev existence.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000142 */
143
Denys Vlasenko516530c2012-05-18 04:45:35 +0200144/* Kernel's hotplug environment constantly changes.
145 * Here are new cases I observed on 3.1.0:
146 *
147 * Case with $DEVNAME and $DEVICE, not just $DEVPATH:
148 * ACTION=add
149 * BUSNUM=001
150 * DEVICE=/proc/bus/usb/001/003
151 * DEVNAME=bus/usb/001/003
152 * DEVNUM=003
153 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5
154 * DEVTYPE=usb_device
155 * MAJOR=189
156 * MINOR=2
157 * PRODUCT=18d1/4e12/227
158 * SUBSYSTEM=usb
159 * TYPE=0/0/0
160 *
161 * Case with $DEVICE, but no $DEVNAME - apparenty, usb iface notification?
162 * "Please load me a module" thing?
163 * ACTION=add
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
167 * INTERFACE=8/6/80
168 * MODALIAS=usb:v18D1p4E12d0227dc00dsc00dp00ic08isc06ip50
169 * PRODUCT=18d1/4e12/227
170 * SUBSYSTEM=usb
171 * TYPE=0/0/0
172 *
173 * ACTION=add
174 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5
175 * DEVTYPE=scsi_host
176 * SUBSYSTEM=scsi
177 *
178 * ACTION=add
179 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/scsi_host/host5
180 * SUBSYSTEM=scsi_host
181 *
182 * ACTION=add
183 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0
184 * DEVTYPE=scsi_target
185 * SUBSYSTEM=scsi
186 *
187 * Case with strange $MODALIAS:
188 * ACTION=add
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
192 * SUBSYSTEM=scsi
193 *
194 * ACTION=add
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
197 *
198 * ACTION=add
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
201 *
202 * Case with explicit $MAJOR/$MINOR (no need to read /sys/$DEVPATH/dev?):
203 * ACTION=add
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
206 * MAJOR=253
207 * MINOR=1
208 * SUBSYSTEM=bsg
209 *
210 * ACTION=add
211 * DEVPATH=/devices/virtual/bdi/8:16
212 * SUBSYSTEM=bdi
213 *
214 * ACTION=add
215 * DEVNAME=sdb
216 * DEVPATH=/block/sdb
217 * DEVTYPE=disk
218 * MAJOR=8
219 * MINOR=16
220 * SUBSYSTEM=block
221 *
222 * Case with ACTION=change:
223 * ACTION=change
224 * DEVNAME=sdb
225 * DEVPATH=/block/sdb
226 * DEVTYPE=disk
227 * DISK_MEDIA_CHANGE=1
228 * MAJOR=8
229 * MINOR=16
230 * SUBSYSTEM=block
231 */
232
233static const char keywords[] ALIGN1 = "add\0remove\0change\0";
234enum { OP_add, OP_remove };
235
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200236struct rule {
237 bool keep_matching;
238 bool regex_compiled;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200239 mode_t mode;
240 int maj, min0, min1;
241 struct bb_uidgid_t ugid;
242 char *envvar;
243 char *ren_mov;
244 IF_FEATURE_MDEV_EXEC(char *r_cmd;)
245 regex_t match;
246};
247
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000248struct globals {
Rob Landley5cd1ccd2006-05-21 18:33:27 +0000249 int root_major, root_minor;
Denys Vlasenko516530c2012-05-18 04:45:35 +0200250 smallint verbose;
Denis Vlasenko065c7142009-04-13 22:23:02 +0000251 char *subsystem;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200252#if ENABLE_FEATURE_MDEV_CONF
253 const char *filename;
254 parser_t *parser;
255 struct rule **rule_vec;
256 unsigned rule_idx;
257#endif
258 struct rule cur_rule;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100259} FIX_ALIASING;
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000260#define G (*(struct globals*)&bb_common_bufsiz1)
Denys Vlasenkoc9665632012-04-21 14:35:32 +0200261#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;) \
264} while (0)
Denys Vlasenko16714242011-09-21 01:59:15 +0200265
Rob Landleya7e3d052006-02-21 06:11:13 +0000266
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000267/* Prevent infinite loops in /sys symlinks */
268#define MAX_SYSFS_DEPTH 3
Mike Frysingerb51fd352007-06-13 09:24:50 +0000269
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000270/* We use additional 64+ bytes in make_device() */
271#define SCRATCH_SIZE 80
272
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200273#if 0
274# define dbg(...) bb_error_msg(__VA_ARGS__)
275#else
276# define dbg(...) ((void)0)
277#endif
278
279
280#if ENABLE_FEATURE_MDEV_CONF
281
282static void make_default_cur_rule(void)
283{
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;
287}
288
289static void clean_up_cur_rule(void)
290{
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();
297}
298
299static void parse_next_rule(void)
300{
301 /* Note: on entry, G.cur_rule is set to default */
302 while (1) {
303 char *tokens[4];
304 char *val;
305
Denys Vlasenko499597d2012-01-11 00:37:17 +0100306 /* No PARSE_EOL_COMMENTS, because command may contain '#' chars */
307 if (!config_read(G.parser, tokens, 4, 3, "# \t", PARSE_NORMAL & ~PARSE_EOL_COMMENTS))
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200308 break;
309
310 /* Fields: [-]regex uid:gid mode [alias] [cmd] */
311 dbg("token1:'%s'", tokens[1]);
312
313 /* 1st field */
314 val = tokens[0];
315 G.cur_rule.keep_matching = ('-' == val[0]);
316 val += G.cur_rule.keep_matching; /* swallow leading dash */
317 if (val[0] == '@') {
318 /* @major,minor[-minor2] */
319 /* (useful when name is ambiguous:
320 * "/sys/class/usb/lp0" and
321 * "/sys/class/printer/lp0")
322 */
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);
326 goto next_rule;
327 }
328 if (sc == 2)
329 G.cur_rule.min1 = G.cur_rule.min0;
330 } else {
331 if (val[0] == '$') {
332 char *eq = strchr(++val, '=');
333 if (!eq) {
334 bb_error_msg("bad $envvar=regex on line %d", G.parser->lineno);
335 goto next_rule;
336 }
337 G.cur_rule.envvar = xstrndup(val, eq - val);
338 val = eq + 1;
339 }
340 xregcomp(&G.cur_rule.match, val, REG_EXTENDED);
341 G.cur_rule.regex_compiled = 1;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200342 }
343
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);
347 goto next_rule;
348 }
349
350 /* 3rd field: mode - device permissions */
351 bb_parse_mode(tokens[2], &G.cur_rule.mode);
352
353 /* 4th field (opt): ">|=alias" or "!" to not create the node */
354 val = tokens[3];
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);
359 }
360
361 if (ENABLE_FEATURE_MDEV_EXEC && val && val[0]) {
362 const char *s = "$@*";
363 const char *s2 = strchr(s, val[0]);
364 if (!s2) {
365 bb_error_msg("bad line %u", G.parser->lineno);
366 goto next_rule;
367 }
368 IF_FEATURE_MDEV_EXEC(G.cur_rule.r_cmd = xstrdup(val);)
369 }
370
371 return;
372 next_rule:
373 clean_up_cur_rule();
374 } /* while (config_read) */
375
376 dbg("config_close(G.parser)");
377 config_close(G.parser);
378 G.parser = NULL;
379
380 return;
381}
382
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.
386 */
387static const struct rule *next_rule(void)
388{
389 struct rule *rule;
390
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);
395 G.filename = NULL;
396 }
397
398 if (G.rule_vec) {
399 /* mdev -s */
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++];
404 }
405 make_default_cur_rule();
406 } else {
407 /* not mdev -s */
408 clean_up_cur_rule();
409 }
410
411 /* Parse one more rule if file isn't fully read */
412 rule = &G.cur_rule;
413 if (G.parser) {
414 parse_next_rule();
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]);
420 }
421 }
422
423 return rule;
424}
425
426#else
427
428# define next_rule() (&G.cur_rule)
429
430#endif
Denys Vlasenko16714242011-09-21 01:59:15 +0200431
Denis Vlasenkod9860482008-07-12 10:23:16 +0000432/* Builds an alias path.
433 * This function potentionally reallocates the alias parameter.
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000434 * Only used for ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkod9860482008-07-12 10:23:16 +0000435 */
436static char *build_alias(char *alias, const char *device_name)
437{
438 char *dest;
439
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);
446 *dest = '/';
447 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
448 dest = alias;
449 alias = concat_path_file(alias, device_name);
450 free(dest);
451 }
452 }
453
454 return alias;
455}
456
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200457/* 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)
460 * path string.
461 * NB2: "mdev -s" may call us many times, do not leak memory/fds!
Denys Vlasenko516530c2012-05-18 04:45:35 +0200462 *
463 * device_name = $DEVNAME (may be NULL)
464 * path = /sys/$DEVPATH
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200465 */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200466static void make_device(char *device_name, char *path, int operation)
Rob Landley70f7ef72005-12-13 08:21:33 +0000467{
Denis Vlasenkof46be092006-10-16 19:39:37 +0000468 int major, minor, type, len;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200469
Denys Vlasenko516530c2012-05-18 04:45:35 +0200470 if (G.verbose)
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200471 bb_error_msg("device: %s, %s", device_name, path);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000472
Rob Landley15fe2e12006-05-08 02:53:23 +0000473 /* 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
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000475 * because sscanf() will stop at the first nondigit, which \n is.
476 * We also depend on path having writeable space after it.
Mike Frysingerc881c732007-11-19 09:04:22 +0000477 */
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000478 major = -1;
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200479 if (operation == OP_add) {
Denis Vlasenko3798db52009-04-19 21:37:07 +0000480 char *dev_maj_min = path + strlen(path);
481
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000482 strcpy(dev_maj_min, "/dev");
483 len = open_read_close(path, dev_maj_min + 1, 64);
Denis Vlasenko3798db52009-04-19 21:37:07 +0000484 *dev_maj_min = '\0';
Mike Frysingerae86a332008-02-20 18:31:36 +0000485 if (len < 1) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000486 if (!ENABLE_FEATURE_MDEV_EXEC)
Mike Frysingerae86a332008-02-20 18:31:36 +0000487 return;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000488 /* no "dev" file, but we can still run scripts
489 * based on device name */
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200490 } else if (sscanf(++dev_maj_min, "%u:%u", &major, &minor) == 2) {
491 if (G.verbose)
492 bb_error_msg("maj,min: %u,%u", major, minor);
493 } else {
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000494 major = -1;
Mike Frysingerae86a332008-02-20 18:31:36 +0000495 }
Rob Landley10b36f92006-08-10 01:09:37 +0000496 }
Denys Vlasenkof2860bf2010-08-16 14:26:15 +0200497 /* else: for delete, -1 still deletes the node, but < -1 suppresses that */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000498
Rob Landley70f7ef72005-12-13 08:21:33 +0000499 /* Determine device name, type, major and minor */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200500 if (!device_name)
501 device_name = (char*) bb_basename(path);
Denis Vlasenkoaa8a6012008-03-29 16:07:46 +0000502 /* http://kernel.org/doc/pending/hotplug.txt says that only
Denis Vlasenkoa87045c2008-07-12 10:28:41 +0000503 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100504 * But since 2.6.25 block devices are also in /sys/class/block.
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200505 * We use strstr("/block/") to forestall future surprises.
506 */
Denis Vlasenko9504e442008-10-29 01:19:15 +0000507 type = S_IFCHR;
Denys Vlasenko8d4a8d12010-03-16 18:37:19 +0100508 if (strstr(path, "/block/") || (G.subsystem && strncmp(G.subsystem, "block", 5) == 0))
Denis Vlasenko9504e442008-10-29 01:19:15 +0000509 type = S_IFBLK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000510
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200511#if ENABLE_FEATURE_MDEV_CONF
512 G.rule_idx = 0; /* restart from the beginning (think mdev -s) */
513#endif
514 for (;;) {
515 const char *str_to_match;
516 regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP];
517 char *command;
518 char *alias;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000519 char aliaslink = aliaslink; /* for compiler */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200520 char *node_name;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200521 const struct rule *rule;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000522
Denys Vlasenkob6beada2012-05-18 15:11:16 +0200523 str_to_match = device_name;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000524
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200525 rule = next_rule();
Denis Vlasenko3798db52009-04-19 21:37:07 +0000526
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200527#if ENABLE_FEATURE_MDEV_CONF
528 if (rule->maj >= 0) { /* @maj,min rule */
529 if (major != rule->maj)
530 continue;
531 if (minor < rule->min0 || minor > rule->min1)
532 continue;
533 memset(off, 0, sizeof(off));
534 goto rule_matches;
535 }
536 if (rule->envvar) { /* $envvar=regex rule */
537 str_to_match = getenv(rule->envvar);
538 dbg("getenv('%s'):'%s'", rule->envvar, str_to_match);
539 if (!str_to_match)
540 continue;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200541 }
Denys Vlasenkob6beada2012-05-18 15:11:16 +0200542 /* else: str_to_match = device_name */
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000543
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200544 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);
553 //}
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000554
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200555 if (regex_match != 0
556 /* regexec returns whole pattern as "range" 0 */
557 || off[0].rm_so != 0
558 || (int)off[0].rm_eo != (int)strlen(str_to_match)
559 ) {
560 continue; /* this rule doesn't match */
Denis Vlasenko3798db52009-04-19 21:37:07 +0000561 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200562 }
563 /* else: it's final implicit "match-all" rule */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200564 rule_matches:
Denys Vlasenkoc9665632012-04-21 14:35:32 +0200565#endif
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200566 dbg("rule matched");
567
568 /* Build alias name */
569 alias = NULL;
570 if (ENABLE_FEATURE_MDEV_RENAME && rule->ren_mov) {
571 aliaslink = rule->ren_mov[0];
572 if (aliaslink == '!') {
573 /* "!": suppress node creation/deletion */
574 major = -2;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000575 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200576 else if (aliaslink == '>' || aliaslink == '=') {
577 if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) {
578 char *s;
579 char *p;
580 unsigned n;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000581
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200582 /* substitute %1..9 with off[1..9], if any */
583 n = 0;
584 s = rule->ren_mov;
585 while (*s)
586 if (*s++ == '%')
587 n++;
Mike Frysingerc881c732007-11-19 09:04:22 +0000588
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200589 p = alias = xzalloc(strlen(rule->ren_mov) + n * strlen(str_to_match));
590 s = rule->ren_mov + 1;
591 while (*s) {
592 *p = *s;
593 if ('%' == *s) {
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);
598 p += n - 1;
599 s++;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000600 }
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000601 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200602 p++;
603 s++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000604 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200605 } else {
606 alias = xstrdup(rule->ren_mov + 1);
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200607 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000608 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000609 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200610 dbg("alias:'%s'", alias);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000611
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200612 command = NULL;
613 IF_FEATURE_MDEV_EXEC(command = rule->r_cmd;)
614 if (command) {
615 const char *s = "$@*";
616 const char *s2 = strchr(s, command[0]);
617
618 /* Are we running this command now?
619 * Run $cmd on delete, @cmd on create, *cmd on both
620 */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200621 if (s2 - s != (operation == OP_remove) || *s2 == '*') {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200622 /* We are here if: '*',
623 * or: '@' and delete = 0,
624 * or: '$' and delete = 1
625 */
626 command++;
627 } else {
628 command = NULL;
629 }
630 }
631 dbg("command:'%s'", command);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000632
633 /* "Execute" the line we found */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200634 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);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000638 }
639
Denys Vlasenko516530c2012-05-18 04:45:35 +0200640 if (operation == OP_add && major >= 0) {
641 char *slash = strrchr(node_name, '/');
642 if (slash) {
643 *slash = '\0';
644 bb_make_directory(node_name, 0755, FILEUTILS_RECUR);
645 *slash = '/';
646 }
647 if (G.verbose)
648 bb_error_msg("mknod: %s (%d,%d) %o", node_name, major, minor, rule->mode | type);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200649 if (mknod(node_name, rule->mode | type, makedev(major, minor)) && errno != EEXIST)
650 bb_perror_msg("can't create '%s'", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200651 if (ENABLE_FEATURE_MDEV_CONF) {
652 chmod(node_name, rule->mode);
653 chown(node_name, rule->ugid.uid, rule->ugid.gid);
654 }
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200655 if (major == G.root_major && minor == G.root_minor)
656 symlink(node_name, "root");
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200657 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
Denys Vlasenko638dbc32012-02-06 01:00:00 +0100658 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?
Denys Vlasenko516530c2012-05-18 04:45:35 +0200662 if (G.verbose)
663 bb_error_msg("symlink: %s", device_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200664 symlink(node_name, device_name);
Denys Vlasenko638dbc32012-02-06 01:00:00 +0100665 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200666 }
667 }
668
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);
673 putenv(s);
674 putenv(s1);
Denys Vlasenko516530c2012-05-18 04:45:35 +0200675 if (G.verbose)
676 bb_error_msg("running: %s", command);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200677 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);
681 }
682
Denys Vlasenko516530c2012-05-18 04:45:35 +0200683 if (operation == OP_remove && major >= -1) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200684 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
Denys Vlasenko516530c2012-05-18 04:45:35 +0200685 if (aliaslink == '>') {
686 if (G.verbose)
687 bb_error_msg("unlink: %s", device_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200688 unlink(device_name);
Denys Vlasenko516530c2012-05-18 04:45:35 +0200689 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200690 }
Denys Vlasenko516530c2012-05-18 04:45:35 +0200691 if (G.verbose)
692 bb_error_msg("unlink: %s", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200693 unlink(node_name);
694 }
695
696 if (ENABLE_FEATURE_MDEV_RENAME)
697 free(alias);
698
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000699 /* We found matching line.
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200700 * Stop unless it was prefixed with '-'
701 */
702 if (!ENABLE_FEATURE_MDEV_CONF || !rule->keep_matching)
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000703 break;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200704 } /* for (;;) */
Rob Landley70f7ef72005-12-13 08:21:33 +0000705}
706
Mike Frysingerb51fd352007-06-13 09:24:50 +0000707/* File callback for /sys/ traversal */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000708static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000709 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000710 void *userData,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000711 int depth UNUSED_PARAM)
Rob Landley70f7ef72005-12-13 08:21:33 +0000712{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000713 size_t len = strlen(fileName) - 4; /* can't underflow */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000714 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000715
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000716 /* len check is for paranoid reasons */
Denis Vlasenkod9860482008-07-12 10:23:16 +0000717 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000718 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000719
Mike Frysingerb51fd352007-06-13 09:24:50 +0000720 strcpy(scratch, fileName);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000721 scratch[len] = '\0';
Denys Vlasenko516530c2012-05-18 04:45:35 +0200722 make_device(/*DEVNAME:*/ NULL, scratch, OP_add);
Rob Landley70f7ef72005-12-13 08:21:33 +0000723
Mike Frysingerb51fd352007-06-13 09:24:50 +0000724 return TRUE;
725}
Rob Landley70f7ef72005-12-13 08:21:33 +0000726
Mike Frysingerb51fd352007-06-13 09:24:50 +0000727/* Directory callback for /sys/ traversal */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000728static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
729 struct stat *statbuf UNUSED_PARAM,
730 void *userData UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000731 int depth)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000732{
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000733 /* Extract device subsystem -- the name of the directory
734 * under /sys/class/ */
Denis Vlasenko065c7142009-04-13 22:23:02 +0000735 if (1 == depth) {
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100736 free(G.subsystem);
737 G.subsystem = strrchr(fileName, '/');
738 if (G.subsystem)
739 G.subsystem = xstrdup(G.subsystem + 1);
Denis Vlasenko065c7142009-04-13 22:23:02 +0000740 }
741
Mike Frysingerb51fd352007-06-13 09:24:50 +0000742 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000743}
744
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000745/* For the full gory details, see linux/Documentation/firmware_class/README
746 *
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
755 */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000756static void load_firmware(const char *firmware, const char *sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000757{
758 int cnt;
759 int firmware_fd, loading_fd, data_fd;
760
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000761 /* check for /lib/firmware/$FIRMWARE */
762 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000763 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000764
765 /* in case we goto out ... */
766 data_fd = -1;
767
768 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
769 xchdir(sysfs_path);
770 for (cnt = 0; cnt < 30; ++cnt) {
771 loading_fd = open("loading", O_WRONLY);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000772 if (loading_fd != -1)
773 goto loading;
774 sleep(1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000775 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000776 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000777
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000778 loading:
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000779 /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000780 if (full_write(loading_fd, "1", 1) != 1)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000781 goto out;
782
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000783 /* load firmware into /sys/$DEVPATH/data */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000784 data_fd = open("data", O_WRONLY);
785 if (data_fd == -1)
786 goto out;
787 cnt = bb_copyfd_eof(firmware_fd, data_fd);
788
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000789 /* tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading" */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000790 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000791 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000792 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000793 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000794
795 out:
796 if (ENABLE_FEATURE_CLEAN_UP) {
797 close(firmware_fd);
798 close(loading_fd);
799 close(data_fd);
800 }
801}
802
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000803int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkod9860482008-07-12 10:23:16 +0000804int mdev_main(int argc UNUSED_PARAM, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000805{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000806 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +0000807
Denys Vlasenko16714242011-09-21 01:59:15 +0200808 INIT_G();
809
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200810#if ENABLE_FEATURE_MDEV_CONF
811 G.filename = "/etc/mdev.conf";
812#endif
813
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000814 /* We can be called as hotplug helper */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000815 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000816 bb_sanitize_stdio();
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000817
Denis Vlasenko3798db52009-04-19 21:37:07 +0000818 /* Force the configuration file settings exactly */
819 umask(0);
820
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000821 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000822
Denis Vlasenkoaafbae62009-04-13 13:33:02 +0000823 if (argv[1] && strcmp(argv[1], "-s") == 0) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000824 /* Scan:
825 * mdev -s
826 */
Rob Landleya7e3d052006-02-21 06:11:13 +0000827 struct stat st;
828
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200829#if ENABLE_FEATURE_MDEV_CONF
830 /* Same as xrealloc_vector(NULL, 4, 0): */
831 G.rule_vec = xzalloc((1 << 4) * sizeof(*G.rule_vec));
832#endif
Denis Vlasenkof46be092006-10-16 19:39:37 +0000833 xstat("/", &st);
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100834 G.root_major = major(st.st_dev);
835 G.root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000836
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000837 /* ACTION_FOLLOWLINKS is needed since in newer kernels
838 * /sys/block/loop* (for example) are symlinks to dirs,
839 * not real directories.
840 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +0000841 * but we can't enforce that on users)
842 */
843 if (access("/sys/class/block", F_OK) != 0) {
844 /* Scan obsolete /sys/block only if /sys/class/block
845 * doesn't exist. Otherwise we'll have dupes.
Denis Vlasenkof1df35c2009-04-12 14:00:12 +0000846 * Also, do not complain if it doesn't exist.
847 * Some people configure kernel to have no blockdevs.
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +0000848 */
849 recursive_action("/sys/block",
Denis Vlasenkof1df35c2009-04-12 14:00:12 +0000850 ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET,
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +0000851 fileAction, dirAction, temp, 0);
852 }
Mike Frysingerb51fd352007-06-13 09:24:50 +0000853 recursive_action("/sys/class",
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000854 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +0000855 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000856 } else {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000857 char *fw;
Denis Vlasenko018bee62008-07-15 22:33:13 +0000858 char *seq;
859 char *action;
Denys Vlasenko516530c2012-05-18 04:45:35 +0200860 char *env_devname;
861 char *env_devpath;
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +0100862 smalluint op;
Denis Vlasenko018bee62008-07-15 22:33:13 +0000863
Mike Frysingerc881c732007-11-19 09:04:22 +0000864 /* Hotplug:
Denis Vlasenko065c7142009-04-13 22:23:02 +0000865 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
Mike Frysingerc881c732007-11-19 09:04:22 +0000866 * ACTION can be "add" or "remove"
867 * DEVPATH is like "/block/sda" or "/class/input/mice"
868 */
Rob Landley29e08ff2006-01-12 06:13:50 +0000869 action = getenv("ACTION");
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200870 op = index_in_strings(keywords, action);
Denys Vlasenko516530c2012-05-18 04:45:35 +0200871 env_devname = getenv("DEVNAME"); /* can be NULL */
872 env_devpath = getenv("DEVPATH");
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100873 G.subsystem = getenv("SUBSYSTEM");
Denys Vlasenko516530c2012-05-18 04:45:35 +0200874 if (!action || !env_devpath /*|| !G.subsystem*/)
Mike Frysingera421ba82006-02-03 00:25:37 +0000875 bb_show_usage();
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000876 fw = getenv("FIRMWARE");
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000877 /* If it exists, does /dev/mdev.seq match $SEQNUM?
878 * If it does not match, earlier mdev is running
879 * in parallel, and we need to wait */
Denis Vlasenko018bee62008-07-15 22:33:13 +0000880 seq = getenv("SEQNUM");
881 if (seq) {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000882 int timeout = 2000 / 32; /* 2000 msec */
Denis Vlasenko018bee62008-07-15 22:33:13 +0000883 do {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000884 int seqlen;
885 char seqbuf[sizeof(int)*3 + 2];
886
Javier Viguerae8758ea2012-01-27 18:30:20 +0100887 seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf) - 1);
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000888 if (seqlen < 0) {
889 seq = NULL;
Denis Vlasenko018bee62008-07-15 22:33:13 +0000890 break;
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000891 }
Denis Vlasenko018bee62008-07-15 22:33:13 +0000892 seqbuf[seqlen] = '\0';
893 if (seqbuf[0] == '\n' /* seed file? */
894 || strcmp(seq, seqbuf) == 0 /* correct idx? */
895 ) {
896 break;
897 }
898 usleep(32*1000);
899 } while (--timeout);
900 }
901
Denys Vlasenko516530c2012-05-18 04:45:35 +0200902 {
903 int logfd = open("/dev/mdev.log", O_WRONLY | O_APPEND);
904 if (logfd >= 0) {
905 xmove_fd(logfd, STDERR_FILENO);
906 G.verbose = 1;
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200907 bb_error_msg("seq: %s action: %s", seq, action);
Denys Vlasenko516530c2012-05-18 04:45:35 +0200908 }
909 }
910
911 snprintf(temp, PATH_MAX, "/sys%s", env_devpath);
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +0100912 if (op == OP_remove) {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000913 /* Ignoring "remove firmware". It was reported
914 * to happen and to cause erroneous deletion
915 * of device nodes. */
916 if (!fw)
Denys Vlasenko516530c2012-05-18 04:45:35 +0200917 make_device(env_devname, temp, op);
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000918 }
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +0100919 else if (op == OP_add) {
Denys Vlasenko516530c2012-05-18 04:45:35 +0200920 make_device(env_devname, temp, op);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000921 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000922 if (fw)
923 load_firmware(fw, temp);
924 }
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000925 }
Denis Vlasenko018bee62008-07-15 22:33:13 +0000926
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000927 if (seq) {
Denis Vlasenko018bee62008-07-15 22:33:13 +0000928 xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1));
929 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000930 }
931
Mike Frysingerc881c732007-11-19 09:04:22 +0000932 if (ENABLE_FEATURE_CLEAN_UP)
933 RELEASE_CONFIG_BUFFER(temp);
934
Denis Vlasenkoaafbae62009-04-13 13:33:02 +0000935 return EXIT_SUCCESS;
Rob Landley70f7ef72005-12-13 08:21:33 +0000936}