blob: c5c0d613c8a4a08f5be0b6f9c26110931a0b5fa0 [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 Vlasenko40b97fb2013-02-26 00:40:46 +010083//usage: " [-][ENV=regex;]...DEVNAME UID:GID PERM"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020084//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"
Denys Vlasenko8d118542012-05-18 04:47:18 +020095//usage: "To activate this feature, create empty /dev/mdev.seq at boot.\n"
Denys Vlasenko516530c2012-05-18 04:45:35 +020096//usage: "\n"
Denys Vlasenko8d118542012-05-18 04:47:18 +020097//usage: "If /dev/mdev.log file exists, debug log will be appended to it."
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
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100233#define DEBUG_LVL 2
234
235#if DEBUG_LVL >= 1
236# define dbg1(...) do { if (G.verbose) bb_error_msg(__VA_ARGS__); } while(0)
237#else
238# define dbg1(...) ((void)0)
239#endif
240#if DEBUG_LVL >= 2
241# define dbg2(...) do { if (G.verbose >= 2) bb_error_msg(__VA_ARGS__); } while(0)
242#else
243# define dbg2(...) ((void)0)
244#endif
245#if DEBUG_LVL >= 3
246# define dbg3(...) do { if (G.verbose >= 3) bb_error_msg(__VA_ARGS__); } while(0)
247#else
248# define dbg3(...) ((void)0)
249#endif
250
251
252static const char keywords[] ALIGN1 = "add\0remove\0"; // "change\0"
Denys Vlasenko516530c2012-05-18 04:45:35 +0200253enum { OP_add, OP_remove };
254
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100255struct envmatch {
256 struct envmatch *next;
257 char *envname;
258 regex_t match;
259};
260
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200261struct rule {
262 bool keep_matching;
263 bool regex_compiled;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200264 mode_t mode;
265 int maj, min0, min1;
266 struct bb_uidgid_t ugid;
267 char *envvar;
268 char *ren_mov;
269 IF_FEATURE_MDEV_EXEC(char *r_cmd;)
270 regex_t match;
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100271 struct envmatch *envmatch;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200272};
273
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000274struct globals {
Rob Landley5cd1ccd2006-05-21 18:33:27 +0000275 int root_major, root_minor;
Denys Vlasenko516530c2012-05-18 04:45:35 +0200276 smallint verbose;
Denis Vlasenko065c7142009-04-13 22:23:02 +0000277 char *subsystem;
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100278 char *subsys_env; /* for putenv("SUBSYSTEM=subsystem") */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200279#if ENABLE_FEATURE_MDEV_CONF
280 const char *filename;
281 parser_t *parser;
282 struct rule **rule_vec;
283 unsigned rule_idx;
284#endif
285 struct rule cur_rule;
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100286 char timestr[sizeof("60.123456")];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100287} FIX_ALIASING;
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000288#define G (*(struct globals*)&bb_common_bufsiz1)
Denys Vlasenkoc9665632012-04-21 14:35:32 +0200289#define INIT_G() do { \
290 IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.maj = -1;) \
291 IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.mode = 0660;) \
292} while (0)
Denys Vlasenko16714242011-09-21 01:59:15 +0200293
Rob Landleya7e3d052006-02-21 06:11:13 +0000294
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000295/* Prevent infinite loops in /sys symlinks */
296#define MAX_SYSFS_DEPTH 3
Mike Frysingerb51fd352007-06-13 09:24:50 +0000297
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000298/* We use additional 64+ bytes in make_device() */
299#define SCRATCH_SIZE 80
300
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200301#if ENABLE_FEATURE_MDEV_CONF
302
303static void make_default_cur_rule(void)
304{
305 memset(&G.cur_rule, 0, sizeof(G.cur_rule));
306 G.cur_rule.maj = -1; /* "not a @major,minor rule" */
307 G.cur_rule.mode = 0660;
308}
309
310static void clean_up_cur_rule(void)
311{
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100312 struct envmatch *e;
313
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200314 free(G.cur_rule.envvar);
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100315 free(G.cur_rule.ren_mov);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200316 if (G.cur_rule.regex_compiled)
317 regfree(&G.cur_rule.match);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200318 IF_FEATURE_MDEV_EXEC(free(G.cur_rule.r_cmd);)
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100319 e = G.cur_rule.envmatch;
320 while (e) {
321 free(e->envname);
322 regfree(&e->match);
323 e = e->next;
324 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200325 make_default_cur_rule();
326}
327
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100328static char *parse_envmatch_pfx(char *val)
329{
330 struct envmatch **nextp = &G.cur_rule.envmatch;
331
332 for (;;) {
333 struct envmatch *e;
334 char *semicolon;
335 char *eq = strchr(val, '=');
336 if (!eq /* || eq == val? */)
337 return val;
338 if (endofname(val) != eq)
339 return val;
340 semicolon = strchr(eq, ';');
341 if (!semicolon)
342 return val;
343 /* ENVVAR=regex;... */
344 *nextp = e = xzalloc(sizeof(*e));
345 nextp = &e->next;
346 e->envname = xstrndup(val, eq - val);
347 *semicolon = '\0';
348 xregcomp(&e->match, eq + 1, REG_EXTENDED);
349 *semicolon = ';';
350 val = semicolon + 1;
351 }
352}
353
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200354static void parse_next_rule(void)
355{
356 /* Note: on entry, G.cur_rule is set to default */
357 while (1) {
358 char *tokens[4];
359 char *val;
360
Denys Vlasenko499597d2012-01-11 00:37:17 +0100361 /* No PARSE_EOL_COMMENTS, because command may contain '#' chars */
362 if (!config_read(G.parser, tokens, 4, 3, "# \t", PARSE_NORMAL & ~PARSE_EOL_COMMENTS))
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200363 break;
364
365 /* Fields: [-]regex uid:gid mode [alias] [cmd] */
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100366 dbg3("token1:'%s'", tokens[1]);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200367
368 /* 1st field */
369 val = tokens[0];
370 G.cur_rule.keep_matching = ('-' == val[0]);
371 val += G.cur_rule.keep_matching; /* swallow leading dash */
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100372 val = parse_envmatch_pfx(val);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200373 if (val[0] == '@') {
374 /* @major,minor[-minor2] */
375 /* (useful when name is ambiguous:
376 * "/sys/class/usb/lp0" and
377 * "/sys/class/printer/lp0")
378 */
379 int sc = sscanf(val, "@%u,%u-%u", &G.cur_rule.maj, &G.cur_rule.min0, &G.cur_rule.min1);
380 if (sc < 2 || G.cur_rule.maj < 0) {
381 bb_error_msg("bad @maj,min on line %d", G.parser->lineno);
382 goto next_rule;
383 }
384 if (sc == 2)
385 G.cur_rule.min1 = G.cur_rule.min0;
386 } else {
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100387 char *eq = strchr(val, '=');
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200388 if (val[0] == '$') {
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100389 /* $ENVVAR=regex ... */
390 val++;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200391 if (!eq) {
392 bb_error_msg("bad $envvar=regex on line %d", G.parser->lineno);
393 goto next_rule;
394 }
395 G.cur_rule.envvar = xstrndup(val, eq - val);
396 val = eq + 1;
397 }
398 xregcomp(&G.cur_rule.match, val, REG_EXTENDED);
399 G.cur_rule.regex_compiled = 1;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200400 }
401
402 /* 2nd field: uid:gid - device ownership */
403 if (get_uidgid(&G.cur_rule.ugid, tokens[1], /*allow_numeric:*/ 1) == 0) {
404 bb_error_msg("unknown user/group '%s' on line %d", tokens[1], G.parser->lineno);
405 goto next_rule;
406 }
407
408 /* 3rd field: mode - device permissions */
409 bb_parse_mode(tokens[2], &G.cur_rule.mode);
410
411 /* 4th field (opt): ">|=alias" or "!" to not create the node */
412 val = tokens[3];
413 if (ENABLE_FEATURE_MDEV_RENAME && val && strchr(">=!", val[0])) {
414 char *s = skip_non_whitespace(val);
415 G.cur_rule.ren_mov = xstrndup(val, s - val);
416 val = skip_whitespace(s);
417 }
418
419 if (ENABLE_FEATURE_MDEV_EXEC && val && val[0]) {
420 const char *s = "$@*";
421 const char *s2 = strchr(s, val[0]);
422 if (!s2) {
423 bb_error_msg("bad line %u", G.parser->lineno);
424 goto next_rule;
425 }
426 IF_FEATURE_MDEV_EXEC(G.cur_rule.r_cmd = xstrdup(val);)
427 }
428
429 return;
430 next_rule:
431 clean_up_cur_rule();
432 } /* while (config_read) */
433
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100434 dbg3("config_close(G.parser)");
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200435 config_close(G.parser);
436 G.parser = NULL;
437
438 return;
439}
440
441/* If mdev -s, we remember rules in G.rule_vec[].
442 * Otherwise, there is no point in doing it, and we just
443 * save only one parsed rule in G.cur_rule.
444 */
445static const struct rule *next_rule(void)
446{
447 struct rule *rule;
448
449 /* Open conf file if we didn't do it yet */
450 if (!G.parser && G.filename) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100451 dbg3("config_open('%s')", G.filename);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200452 G.parser = config_open2(G.filename, fopen_for_read);
453 G.filename = NULL;
454 }
455
456 if (G.rule_vec) {
457 /* mdev -s */
458 /* Do we have rule parsed already? */
459 if (G.rule_vec[G.rule_idx]) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100460 dbg3("< G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200461 return G.rule_vec[G.rule_idx++];
462 }
463 make_default_cur_rule();
464 } else {
465 /* not mdev -s */
466 clean_up_cur_rule();
467 }
468
469 /* Parse one more rule if file isn't fully read */
470 rule = &G.cur_rule;
471 if (G.parser) {
472 parse_next_rule();
473 if (G.rule_vec) { /* mdev -s */
474 rule = memcpy(xmalloc(sizeof(G.cur_rule)), &G.cur_rule, sizeof(G.cur_rule));
475 G.rule_vec = xrealloc_vector(G.rule_vec, 4, G.rule_idx);
476 G.rule_vec[G.rule_idx++] = rule;
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100477 dbg3("> G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200478 }
479 }
480
481 return rule;
482}
483
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100484static int env_matches(struct envmatch *e)
485{
486 while (e) {
487 int r;
488 char *val = getenv(e->envname);
489 if (!val)
490 return 0;
491 r = regexec(&e->match, val, /*size*/ 0, /*range[]*/ NULL, /*eflags*/ 0);
492 if (r != 0) /* no match */
493 return 0;
494 e = e->next;
495 }
496 return 1;
497}
498
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200499#else
500
501# define next_rule() (&G.cur_rule)
502
503#endif
Denys Vlasenko16714242011-09-21 01:59:15 +0200504
Denys Vlasenko4609f472013-01-21 01:22:12 +0100505static void mkdir_recursive(char *name)
506{
507 /* if name has many levels ("dir1/dir2"),
508 * bb_make_directory() will create dir1 according to umask,
509 * not according to its "mode" parameter.
510 * Since we run with umask=0, need to temporarily switch it.
511 */
512 umask(022); /* "dir1" (if any) will be 0755 too */
513 bb_make_directory(name, 0755, FILEUTILS_RECUR);
514 umask(0);
515}
516
Denis Vlasenkod9860482008-07-12 10:23:16 +0000517/* Builds an alias path.
518 * This function potentionally reallocates the alias parameter.
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000519 * Only used for ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkod9860482008-07-12 10:23:16 +0000520 */
521static char *build_alias(char *alias, const char *device_name)
522{
523 char *dest;
524
525 /* ">bar/": rename to bar/device_name */
526 /* ">bar[/]baz": rename to bar[/]baz */
527 dest = strrchr(alias, '/');
528 if (dest) { /* ">bar/[baz]" ? */
529 *dest = '\0'; /* mkdir bar */
Denys Vlasenko4609f472013-01-21 01:22:12 +0100530 mkdir_recursive(alias);
Denis Vlasenkod9860482008-07-12 10:23:16 +0000531 *dest = '/';
532 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
533 dest = alias;
534 alias = concat_path_file(alias, device_name);
535 free(dest);
536 }
537 }
538
539 return alias;
540}
541
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200542/* mknod in /dev based on a path like "/sys/block/hda/hda1"
543 * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes
544 * after NUL, but we promise to not mangle (IOW: to restore if needed)
545 * path string.
546 * NB2: "mdev -s" may call us many times, do not leak memory/fds!
Denys Vlasenko516530c2012-05-18 04:45:35 +0200547 *
548 * device_name = $DEVNAME (may be NULL)
549 * path = /sys/$DEVPATH
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200550 */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200551static void make_device(char *device_name, char *path, int operation)
Rob Landley70f7ef72005-12-13 08:21:33 +0000552{
Denis Vlasenkof46be092006-10-16 19:39:37 +0000553 int major, minor, type, len;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200554
Rob Landley15fe2e12006-05-08 02:53:23 +0000555 /* Try to read major/minor string. Note that the kernel puts \n after
556 * the data, so we don't need to worry about null terminating the string
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000557 * because sscanf() will stop at the first nondigit, which \n is.
558 * We also depend on path having writeable space after it.
Mike Frysingerc881c732007-11-19 09:04:22 +0000559 */
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000560 major = -1;
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200561 if (operation == OP_add) {
Denis Vlasenko3798db52009-04-19 21:37:07 +0000562 char *dev_maj_min = path + strlen(path);
563
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000564 strcpy(dev_maj_min, "/dev");
565 len = open_read_close(path, dev_maj_min + 1, 64);
Denis Vlasenko3798db52009-04-19 21:37:07 +0000566 *dev_maj_min = '\0';
Mike Frysingerae86a332008-02-20 18:31:36 +0000567 if (len < 1) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000568 if (!ENABLE_FEATURE_MDEV_EXEC)
Mike Frysingerae86a332008-02-20 18:31:36 +0000569 return;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000570 /* no "dev" file, but we can still run scripts
571 * based on device name */
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200572 } else if (sscanf(++dev_maj_min, "%u:%u", &major, &minor) == 2) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100573 dbg1("dev %u,%u", major, minor);
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200574 } else {
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000575 major = -1;
Mike Frysingerae86a332008-02-20 18:31:36 +0000576 }
Rob Landley10b36f92006-08-10 01:09:37 +0000577 }
Denys Vlasenkof2860bf2010-08-16 14:26:15 +0200578 /* else: for delete, -1 still deletes the node, but < -1 suppresses that */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000579
Rob Landley70f7ef72005-12-13 08:21:33 +0000580 /* Determine device name, type, major and minor */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200581 if (!device_name)
582 device_name = (char*) bb_basename(path);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100583 /*
584 * http://kernel.org/doc/pending/hotplug.txt says that only
Denis Vlasenkoa87045c2008-07-12 10:28:41 +0000585 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100586 * But since 2.6.25 block devices are also in /sys/class/block.
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200587 * We use strstr("/block/") to forestall future surprises.
588 */
Denis Vlasenko9504e442008-10-29 01:19:15 +0000589 type = S_IFCHR;
Denys Vlasenko8d4a8d12010-03-16 18:37:19 +0100590 if (strstr(path, "/block/") || (G.subsystem && strncmp(G.subsystem, "block", 5) == 0))
Denis Vlasenko9504e442008-10-29 01:19:15 +0000591 type = S_IFBLK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000592
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200593#if ENABLE_FEATURE_MDEV_CONF
594 G.rule_idx = 0; /* restart from the beginning (think mdev -s) */
595#endif
596 for (;;) {
597 const char *str_to_match;
598 regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP];
599 char *command;
600 char *alias;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000601 char aliaslink = aliaslink; /* for compiler */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200602 char *node_name;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200603 const struct rule *rule;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000604
Denys Vlasenkob6beada2012-05-18 15:11:16 +0200605 str_to_match = device_name;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000606
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200607 rule = next_rule();
Denis Vlasenko3798db52009-04-19 21:37:07 +0000608
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200609#if ENABLE_FEATURE_MDEV_CONF
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100610 if (!env_matches(rule->envmatch))
611 continue;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200612 if (rule->maj >= 0) { /* @maj,min rule */
613 if (major != rule->maj)
614 continue;
615 if (minor < rule->min0 || minor > rule->min1)
616 continue;
617 memset(off, 0, sizeof(off));
618 goto rule_matches;
619 }
620 if (rule->envvar) { /* $envvar=regex rule */
621 str_to_match = getenv(rule->envvar);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100622 dbg3("getenv('%s'):'%s'", rule->envvar, str_to_match);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200623 if (!str_to_match)
624 continue;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200625 }
Denys Vlasenkob6beada2012-05-18 15:11:16 +0200626 /* else: str_to_match = device_name */
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000627
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200628 if (rule->regex_compiled) {
629 int regex_match = regexec(&rule->match, str_to_match, ARRAY_SIZE(off), off, 0);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100630 dbg3("regex_match for '%s':%d", str_to_match, regex_match);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200631 //bb_error_msg("matches:");
632 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
633 // if (off[i].rm_so < 0) continue;
634 // bb_error_msg("match %d: '%.*s'\n", i,
635 // (int)(off[i].rm_eo - off[i].rm_so),
636 // device_name + off[i].rm_so);
637 //}
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000638
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200639 if (regex_match != 0
640 /* regexec returns whole pattern as "range" 0 */
641 || off[0].rm_so != 0
642 || (int)off[0].rm_eo != (int)strlen(str_to_match)
643 ) {
644 continue; /* this rule doesn't match */
Denis Vlasenko3798db52009-04-19 21:37:07 +0000645 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200646 }
647 /* else: it's final implicit "match-all" rule */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200648 rule_matches:
Denys Vlasenkoc9665632012-04-21 14:35:32 +0200649#endif
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100650 dbg2("rule matched, line %d", G.parser ? G.parser->lineno : -1);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200651
652 /* Build alias name */
653 alias = NULL;
654 if (ENABLE_FEATURE_MDEV_RENAME && rule->ren_mov) {
655 aliaslink = rule->ren_mov[0];
656 if (aliaslink == '!') {
657 /* "!": suppress node creation/deletion */
658 major = -2;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000659 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200660 else if (aliaslink == '>' || aliaslink == '=') {
661 if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) {
662 char *s;
663 char *p;
664 unsigned n;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000665
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200666 /* substitute %1..9 with off[1..9], if any */
667 n = 0;
668 s = rule->ren_mov;
669 while (*s)
670 if (*s++ == '%')
671 n++;
Mike Frysingerc881c732007-11-19 09:04:22 +0000672
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200673 p = alias = xzalloc(strlen(rule->ren_mov) + n * strlen(str_to_match));
674 s = rule->ren_mov + 1;
675 while (*s) {
676 *p = *s;
677 if ('%' == *s) {
678 unsigned i = (s[1] - '0');
679 if (i <= 9 && off[i].rm_so >= 0) {
680 n = off[i].rm_eo - off[i].rm_so;
681 strncpy(p, str_to_match + off[i].rm_so, n);
682 p += n - 1;
683 s++;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000684 }
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000685 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200686 p++;
687 s++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000688 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200689 } else {
690 alias = xstrdup(rule->ren_mov + 1);
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200691 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000692 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000693 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100694 dbg3("alias:'%s'", alias);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000695
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200696 command = NULL;
697 IF_FEATURE_MDEV_EXEC(command = rule->r_cmd;)
698 if (command) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200699 /* Are we running this command now?
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100700 * Run @cmd on create, $cmd on delete, *cmd on any
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200701 */
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100702 if ((command[0] == '@' && operation == OP_add)
703 || (command[0] == '$' && operation == OP_remove)
704 || (command[0] == '*')
705 ) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200706 command++;
707 } else {
708 command = NULL;
709 }
710 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100711 dbg3("command:'%s'", command);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000712
713 /* "Execute" the line we found */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200714 node_name = device_name;
715 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
716 node_name = alias = build_alias(alias, device_name);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100717 dbg3("alias2:'%s'", alias);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000718 }
719
Denys Vlasenko516530c2012-05-18 04:45:35 +0200720 if (operation == OP_add && major >= 0) {
721 char *slash = strrchr(node_name, '/');
722 if (slash) {
723 *slash = '\0';
Denys Vlasenko4609f472013-01-21 01:22:12 +0100724 mkdir_recursive(node_name);
Denys Vlasenko516530c2012-05-18 04:45:35 +0200725 *slash = '/';
726 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100727 if (ENABLE_FEATURE_MDEV_CONF) {
728 dbg1("mknod %s (%d,%d) %o"
729 " %u:%u",
730 node_name, major, minor, rule->mode | type,
731 rule->ugid.uid, rule->ugid.gid
732 );
733 } else {
734 dbg1("mknod %s (%d,%d) %o",
735 node_name, major, minor, rule->mode | type
736 );
737 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200738 if (mknod(node_name, rule->mode | type, makedev(major, minor)) && errno != EEXIST)
739 bb_perror_msg("can't create '%s'", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200740 if (ENABLE_FEATURE_MDEV_CONF) {
741 chmod(node_name, rule->mode);
742 chown(node_name, rule->ugid.uid, rule->ugid.gid);
743 }
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200744 if (major == G.root_major && minor == G.root_minor)
745 symlink(node_name, "root");
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200746 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
Denys Vlasenko638dbc32012-02-06 01:00:00 +0100747 if (aliaslink == '>') {
748//TODO: on devtmpfs, device_name already exists and symlink() fails.
749//End result is that instead of symlink, we have two nodes.
750//What should be done?
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100751 dbg1("symlink: %s", device_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200752 symlink(node_name, device_name);
Denys Vlasenko638dbc32012-02-06 01:00:00 +0100753 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200754 }
755 }
756
757 if (ENABLE_FEATURE_MDEV_EXEC && command) {
758 /* setenv will leak memory, use putenv/unsetenv/free */
759 char *s = xasprintf("%s=%s", "MDEV", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200760 putenv(s);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100761 dbg1("running: %s", command);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200762 if (system(command) == -1)
763 bb_perror_msg("can't run '%s'", command);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200764 bb_unsetenv_and_free(s);
765 }
766
Denys Vlasenko516530c2012-05-18 04:45:35 +0200767 if (operation == OP_remove && major >= -1) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200768 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
Denys Vlasenko516530c2012-05-18 04:45:35 +0200769 if (aliaslink == '>') {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100770 dbg1("unlink: %s", device_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200771 unlink(device_name);
Denys Vlasenko516530c2012-05-18 04:45:35 +0200772 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200773 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100774 dbg1("unlink: %s", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200775 unlink(node_name);
776 }
777
778 if (ENABLE_FEATURE_MDEV_RENAME)
779 free(alias);
780
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000781 /* We found matching line.
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200782 * Stop unless it was prefixed with '-'
783 */
784 if (!ENABLE_FEATURE_MDEV_CONF || !rule->keep_matching)
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000785 break;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200786 } /* for (;;) */
Rob Landley70f7ef72005-12-13 08:21:33 +0000787}
788
Mike Frysingerb51fd352007-06-13 09:24:50 +0000789/* File callback for /sys/ traversal */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000790static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000791 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000792 void *userData,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000793 int depth UNUSED_PARAM)
Rob Landley70f7ef72005-12-13 08:21:33 +0000794{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000795 size_t len = strlen(fileName) - 4; /* can't underflow */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000796 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000797
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000798 /* len check is for paranoid reasons */
Denis Vlasenkod9860482008-07-12 10:23:16 +0000799 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000800 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000801
Mike Frysingerb51fd352007-06-13 09:24:50 +0000802 strcpy(scratch, fileName);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000803 scratch[len] = '\0';
Denys Vlasenko516530c2012-05-18 04:45:35 +0200804 make_device(/*DEVNAME:*/ NULL, scratch, OP_add);
Rob Landley70f7ef72005-12-13 08:21:33 +0000805
Mike Frysingerb51fd352007-06-13 09:24:50 +0000806 return TRUE;
807}
Rob Landley70f7ef72005-12-13 08:21:33 +0000808
Mike Frysingerb51fd352007-06-13 09:24:50 +0000809/* Directory callback for /sys/ traversal */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000810static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
811 struct stat *statbuf UNUSED_PARAM,
812 void *userData UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000813 int depth)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000814{
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000815 /* Extract device subsystem -- the name of the directory
816 * under /sys/class/ */
Denis Vlasenko065c7142009-04-13 22:23:02 +0000817 if (1 == depth) {
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100818 free(G.subsystem);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100819 if (G.subsys_env) {
820 bb_unsetenv_and_free(G.subsys_env);
821 G.subsys_env = NULL;
822 }
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100823 G.subsystem = strrchr(fileName, '/');
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100824 if (G.subsystem) {
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100825 G.subsystem = xstrdup(G.subsystem + 1);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100826 G.subsys_env = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem);
827 putenv(G.subsys_env);
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100828 }
Denis Vlasenko065c7142009-04-13 22:23:02 +0000829 }
830
Mike Frysingerb51fd352007-06-13 09:24:50 +0000831 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000832}
833
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000834/* For the full gory details, see linux/Documentation/firmware_class/README
835 *
836 * Firmware loading works like this:
837 * - kernel sets FIRMWARE env var
838 * - userspace checks /lib/firmware/$FIRMWARE
839 * - userspace waits for /sys/$DEVPATH/loading to appear
840 * - userspace writes "1" to /sys/$DEVPATH/loading
841 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
842 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
843 * - kernel loads firmware into device
844 */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000845static void load_firmware(const char *firmware, const char *sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000846{
847 int cnt;
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200848 int firmware_fd, loading_fd;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000849
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000850 /* check for /lib/firmware/$FIRMWARE */
851 xchdir("/lib/firmware");
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200852 firmware_fd = open(firmware, O_RDONLY); /* can fail */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000853
854 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
855 xchdir(sysfs_path);
856 for (cnt = 0; cnt < 30; ++cnt) {
857 loading_fd = open("loading", O_WRONLY);
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200858 if (loading_fd >= 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000859 goto loading;
860 sleep(1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000861 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000862 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000863
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000864 loading:
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200865 cnt = 0;
866 if (firmware_fd >= 0) {
867 int data_fd;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000868
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200869 /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */
870 if (full_write(loading_fd, "1", 1) != 1)
871 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000872
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200873 /* load firmware into /sys/$DEVPATH/data */
874 data_fd = open("data", O_WRONLY);
875 if (data_fd < 0)
876 goto out;
877 cnt = bb_copyfd_eof(firmware_fd, data_fd);
878 if (ENABLE_FEATURE_CLEAN_UP)
879 close(data_fd);
880 }
881
882 /* Tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading"
883 * Note: we emit -1 also if firmware file wasn't found.
884 * There are cases when otherwise kernel would wait for minutes
885 * before timing out.
886 */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000887 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000888 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000889 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000890 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000891
892 out:
Denys Vlasenkoba76b7a2013-02-04 23:35:27 +0100893 xchdir("/dev");
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000894 if (ENABLE_FEATURE_CLEAN_UP) {
895 close(firmware_fd);
896 close(loading_fd);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000897 }
898}
899
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100900static char *curtime(void)
901{
902 struct timeval tv;
903 gettimeofday(&tv, NULL);
904 sprintf(G.timestr, "%u.%06u", (unsigned)tv.tv_sec % 60, (unsigned)tv.tv_usec);
905 return G.timestr;
906}
907
908static void open_mdev_log(const char *seq, unsigned my_pid)
909{
910 int logfd = open("mdev.log", O_WRONLY | O_APPEND);
911 if (logfd >= 0) {
912 xmove_fd(logfd, STDERR_FILENO);
913 G.verbose = 2;
914 applet_name = xasprintf("%s[%s]", applet_name, seq ? seq : utoa(my_pid));
915 }
916}
917
918/* If it exists, does /dev/mdev.seq match $SEQNUM?
919 * If it does not match, earlier mdev is running
920 * in parallel, and we need to wait.
921 * Active mdev pokes us with SIGCHLD to check the new file.
922 */
923static int
924wait_for_seqfile(const char *seq)
925{
926 /* We time out after 2 sec */
927 static const struct timespec ts = { 0, 32*1000*1000 };
928 int timeout = 2000 / 32;
929 int seq_fd = -1;
930 int do_once = 1;
931 sigset_t set_CHLD;
932
933 sigemptyset(&set_CHLD);
934 sigaddset(&set_CHLD, SIGCHLD);
935 sigprocmask(SIG_BLOCK, &set_CHLD, NULL);
936
937 for (;;) {
938 int seqlen;
939 char seqbuf[sizeof(int)*3 + 2];
940
941 if (seq_fd < 0) {
942 seq_fd = open("mdev.seq", O_RDWR);
943 if (seq_fd < 0)
944 break;
945 }
946 seqlen = pread(seq_fd, seqbuf, sizeof(seqbuf) - 1, 0);
947 if (seqlen < 0) {
948 close(seq_fd);
949 seq_fd = -1;
950 break;
951 }
952 seqbuf[seqlen] = '\0';
953 if (seqbuf[0] == '\n') {
954 /* seed file: write out seq ASAP */
955 xwrite_str(seq_fd, seq);
956 xlseek(seq_fd, 0, SEEK_SET);
957 dbg2("first seq written");
958 break;
959 }
960 if (strcmp(seq, seqbuf) == 0) {
961 /* correct idx */
962 break;
963 }
964 if (do_once) {
965 dbg2("%s waiting for '%s'", curtime(), seqbuf);
966 do_once = 0;
967 }
968 if (sigtimedwait(&set_CHLD, NULL, &ts) >= 0) {
969 dbg3("woken up");
970 continue; /* don't decrement timeout! */
971 }
972 if (--timeout == 0) {
973 dbg1("%s waiting for '%s'", "timed out", seqbuf);
974 break;
975 }
976 }
977 sigprocmask(SIG_UNBLOCK, &set_CHLD, NULL);
978 return seq_fd;
979}
980
981static void signal_mdevs(unsigned my_pid)
982{
983 procps_status_t* p = NULL;
984 while ((p = procps_scan(p, PSSCAN_ARGV0)) != NULL) {
985 if (p->pid != my_pid
986 && p->argv0
987 && strcmp(bb_basename(p->argv0), "mdev") == 0
988 ) {
989 kill(p->pid, SIGCHLD);
990 }
991 }
992}
993
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000994int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkod9860482008-07-12 10:23:16 +0000995int mdev_main(int argc UNUSED_PARAM, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000996{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000997 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +0000998
Denys Vlasenko16714242011-09-21 01:59:15 +0200999 INIT_G();
1000
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +02001001#if ENABLE_FEATURE_MDEV_CONF
1002 G.filename = "/etc/mdev.conf";
1003#endif
1004
Denis Vlasenkod48e81f2008-07-06 07:00:11 +00001005 /* We can be called as hotplug helper */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +00001006 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +00001007 bb_sanitize_stdio();
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +00001008
Denis Vlasenko3798db52009-04-19 21:37:07 +00001009 /* Force the configuration file settings exactly */
1010 umask(0);
1011
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +00001012 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +00001013
Denis Vlasenkoaafbae62009-04-13 13:33:02 +00001014 if (argv[1] && strcmp(argv[1], "-s") == 0) {
Denys Vlasenko40b97fb2013-02-26 00:40:46 +01001015 /*
1016 * Scan: mdev -s
Mike Frysingerc881c732007-11-19 09:04:22 +00001017 */
Rob Landleya7e3d052006-02-21 06:11:13 +00001018 struct stat st;
1019
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +02001020#if ENABLE_FEATURE_MDEV_CONF
1021 /* Same as xrealloc_vector(NULL, 4, 0): */
1022 G.rule_vec = xzalloc((1 << 4) * sizeof(*G.rule_vec));
1023#endif
Denis Vlasenkof46be092006-10-16 19:39:37 +00001024 xstat("/", &st);
Denys Vlasenko64bb95a2010-02-24 10:39:47 +01001025 G.root_major = major(st.st_dev);
1026 G.root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +00001027
Denys Vlasenko40b97fb2013-02-26 00:40:46 +01001028 putenv((char*)"ACTION=add");
1029
Denis Vlasenko67075bb2008-07-06 17:00:49 +00001030 /* ACTION_FOLLOWLINKS is needed since in newer kernels
1031 * /sys/block/loop* (for example) are symlinks to dirs,
1032 * not real directories.
1033 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +00001034 * but we can't enforce that on users)
1035 */
1036 if (access("/sys/class/block", F_OK) != 0) {
1037 /* Scan obsolete /sys/block only if /sys/class/block
1038 * doesn't exist. Otherwise we'll have dupes.
Denis Vlasenkof1df35c2009-04-12 14:00:12 +00001039 * Also, do not complain if it doesn't exist.
1040 * Some people configure kernel to have no blockdevs.
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +00001041 */
1042 recursive_action("/sys/block",
Denis Vlasenkof1df35c2009-04-12 14:00:12 +00001043 ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET,
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +00001044 fileAction, dirAction, temp, 0);
1045 }
Mike Frysingerb51fd352007-06-13 09:24:50 +00001046 recursive_action("/sys/class",
Denis Vlasenko67075bb2008-07-06 17:00:49 +00001047 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +00001048 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +00001049 } else {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +00001050 char *fw;
Denis Vlasenko018bee62008-07-15 22:33:13 +00001051 char *seq;
1052 char *action;
Denys Vlasenko516530c2012-05-18 04:45:35 +02001053 char *env_devname;
1054 char *env_devpath;
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001055 unsigned my_pid;
1056 int seq_fd;
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +01001057 smalluint op;
Denis Vlasenko018bee62008-07-15 22:33:13 +00001058
Mike Frysingerc881c732007-11-19 09:04:22 +00001059 /* Hotplug:
Denis Vlasenko065c7142009-04-13 22:23:02 +00001060 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001061 * ACTION can be "add", "remove", "change"
Mike Frysingerc881c732007-11-19 09:04:22 +00001062 * DEVPATH is like "/block/sda" or "/class/input/mice"
1063 */
Rob Landley29e08ff2006-01-12 06:13:50 +00001064 action = getenv("ACTION");
Denys Vlasenkoea57f512012-05-18 11:39:57 +02001065 op = index_in_strings(keywords, action);
Denys Vlasenko516530c2012-05-18 04:45:35 +02001066 env_devname = getenv("DEVNAME"); /* can be NULL */
1067 env_devpath = getenv("DEVPATH");
Denys Vlasenko64bb95a2010-02-24 10:39:47 +01001068 G.subsystem = getenv("SUBSYSTEM");
Denys Vlasenko516530c2012-05-18 04:45:35 +02001069 if (!action || !env_devpath /*|| !G.subsystem*/)
Mike Frysingera421ba82006-02-03 00:25:37 +00001070 bb_show_usage();
Denis Vlasenko1fb26da2009-04-13 02:15:57 +00001071 fw = getenv("FIRMWARE");
Denis Vlasenko018bee62008-07-15 22:33:13 +00001072 seq = getenv("SEQNUM");
Denis Vlasenko1fb26da2009-04-13 02:15:57 +00001073
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001074 my_pid = getpid();
1075 open_mdev_log(seq, my_pid);
Denis Vlasenko018bee62008-07-15 22:33:13 +00001076
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001077 seq_fd = seq ? wait_for_seqfile(seq) : -1;
1078
1079 dbg1("%s "
1080 "ACTION:%s SUBSYSTEM:%s DEVNAME:%s DEVPATH:%s"
1081 "%s%s",
1082 curtime(),
1083 action, G.subsystem, env_devname, env_devpath,
1084 fw ? " FW:" : "", fw ? fw : ""
1085 );
Denys Vlasenko516530c2012-05-18 04:45:35 +02001086
1087 snprintf(temp, PATH_MAX, "/sys%s", env_devpath);
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +01001088 if (op == OP_remove) {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +00001089 /* Ignoring "remove firmware". It was reported
1090 * to happen and to cause erroneous deletion
1091 * of device nodes. */
1092 if (!fw)
Denys Vlasenko516530c2012-05-18 04:45:35 +02001093 make_device(env_devname, temp, op);
Denis Vlasenko1fb26da2009-04-13 02:15:57 +00001094 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001095 else {
Denys Vlasenko516530c2012-05-18 04:45:35 +02001096 make_device(env_devname, temp, op);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +00001097 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001098 if (op == OP_add && fw)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +00001099 load_firmware(fw, temp);
1100 }
Mike Frysingera78ef2c2007-06-13 07:34:15 +00001101 }
Denis Vlasenko018bee62008-07-15 22:33:13 +00001102
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001103 dbg1("%s exiting", curtime());
1104 if (seq_fd >= 0) {
1105 xwrite_str(seq_fd, utoa(xatou(seq) + 1));
1106 signal_mdevs(my_pid);
Denis Vlasenko018bee62008-07-15 22:33:13 +00001107 }
Rob Landley29e08ff2006-01-12 06:13:50 +00001108 }
1109
Mike Frysingerc881c732007-11-19 09:04:22 +00001110 if (ENABLE_FEATURE_CLEAN_UP)
1111 RELEASE_CONFIG_BUFFER(temp);
1112
Denis Vlasenkoaafbae62009-04-13 13:33:02 +00001113 return EXIT_SUCCESS;
Rob Landley70f7ef72005-12-13 08:21:33 +00001114}