blob: a59115dd45729654aa08ac2fb31072396c5a02ac [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"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200100#include "common_bufsiz.h"
Rob Landleyb56c2852005-12-17 10:52:30 +0000101#include "xregex.h"
Rob Landley70f7ef72005-12-13 08:21:33 +0000102
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000103/* "mdev -s" scans /sys/class/xxx, looking for directories which have dev
104 * file (it is of the form "M:m\n"). Example: /sys/class/tty/tty0/dev
105 * contains "4:0\n". Directory name is taken as device name, path component
106 * directly after /sys/class/ as subsystem. In this example, "tty0" and "tty".
107 * Then mdev creates the /dev/device_name node.
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000108 * If /sys/class/.../dev file does not exist, mdev still may act
109 * on this device: see "@|$|*command args..." parameter in config file.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000110 *
111 * mdev w/o parameters is called as hotplug helper. It takes device
112 * and subsystem names from $DEVPATH and $SUBSYSTEM, extracts
113 * maj,min from "/sys/$DEVPATH/dev" and also examines
114 * $ACTION ("add"/"delete") and $FIRMWARE.
115 *
116 * If action is "add", mdev creates /dev/device_name similarly to mdev -s.
117 * (todo: explain "delete" and $FIRMWARE)
118 *
119 * If /etc/mdev.conf exists, it may modify /dev/device_name's properties.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000120 *
121 * Leading minus in 1st field means "don't stop on this line", otherwise
122 * search is stopped after the matching line is encountered.
123 *
Denys Vlasenko45f0a112011-09-22 04:55:47 +0200124 * $envvar=regex format is useful for loading modules for hot-plugged devices
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000125 * which do not have driver loaded yet. In this case /sys/class/.../dev
126 * does not exist, but $MODALIAS is set to needed module's name
127 * (actually, an alias to it) by kernel. This rule instructs mdev
128 * to load the module and exit:
129 * $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"
130 * The kernel will generate another hotplug event when /sys/class/.../dev
131 * file appears.
132 *
133 * When line matches, the device node is created, chmod'ed and chown'ed,
134 * moved to path, and if >path, a symlink to moved node is created,
135 * all this if /sys/class/.../dev exists.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000136 * Examples:
137 * =loop/ - moves to /dev/loop
138 * >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000139 *
140 * Then "command args..." is executed (via sh -c 'command args...').
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000141 * @:execute on creation, $:on deletion, *:on both.
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000142 * This happens regardless of /sys/class/.../dev existence.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000143 */
144
Denys Vlasenko516530c2012-05-18 04:45:35 +0200145/* Kernel's hotplug environment constantly changes.
146 * Here are new cases I observed on 3.1.0:
147 *
148 * Case with $DEVNAME and $DEVICE, not just $DEVPATH:
149 * ACTION=add
150 * BUSNUM=001
151 * DEVICE=/proc/bus/usb/001/003
152 * DEVNAME=bus/usb/001/003
153 * DEVNUM=003
154 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5
155 * DEVTYPE=usb_device
156 * MAJOR=189
157 * MINOR=2
158 * PRODUCT=18d1/4e12/227
159 * SUBSYSTEM=usb
160 * TYPE=0/0/0
161 *
162 * Case with $DEVICE, but no $DEVNAME - apparenty, usb iface notification?
163 * "Please load me a module" thing?
164 * ACTION=add
165 * DEVICE=/proc/bus/usb/001/003
166 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0
167 * DEVTYPE=usb_interface
168 * INTERFACE=8/6/80
169 * MODALIAS=usb:v18D1p4E12d0227dc00dsc00dp00ic08isc06ip50
170 * PRODUCT=18d1/4e12/227
171 * SUBSYSTEM=usb
172 * TYPE=0/0/0
173 *
174 * ACTION=add
175 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5
176 * DEVTYPE=scsi_host
177 * SUBSYSTEM=scsi
178 *
179 * ACTION=add
180 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/scsi_host/host5
181 * SUBSYSTEM=scsi_host
182 *
183 * ACTION=add
184 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0
185 * DEVTYPE=scsi_target
186 * SUBSYSTEM=scsi
187 *
188 * Case with strange $MODALIAS:
189 * ACTION=add
190 * DEVPATH=/devices/pci0000:00/0000:00:02.1/usb1/1-5/1-5:1.0/host5/target5:0:0/5:0:0:0
191 * DEVTYPE=scsi_device
192 * MODALIAS=scsi:t-0x00
193 * SUBSYSTEM=scsi
194 *
195 * ACTION=add
196 * 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
197 * SUBSYSTEM=scsi_disk
198 *
199 * ACTION=add
200 * 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
201 * SUBSYSTEM=scsi_device
202 *
203 * Case with explicit $MAJOR/$MINOR (no need to read /sys/$DEVPATH/dev?):
204 * ACTION=add
205 * DEVNAME=bsg/5:0:0:0
206 * 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
207 * MAJOR=253
208 * MINOR=1
209 * SUBSYSTEM=bsg
210 *
211 * ACTION=add
212 * DEVPATH=/devices/virtual/bdi/8:16
213 * SUBSYSTEM=bdi
214 *
215 * ACTION=add
216 * DEVNAME=sdb
217 * DEVPATH=/block/sdb
218 * DEVTYPE=disk
219 * MAJOR=8
220 * MINOR=16
221 * SUBSYSTEM=block
222 *
223 * Case with ACTION=change:
224 * ACTION=change
225 * DEVNAME=sdb
226 * DEVPATH=/block/sdb
227 * DEVTYPE=disk
228 * DISK_MEDIA_CHANGE=1
229 * MAJOR=8
230 * MINOR=16
231 * SUBSYSTEM=block
232 */
233
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100234#define DEBUG_LVL 2
235
236#if DEBUG_LVL >= 1
237# define dbg1(...) do { if (G.verbose) bb_error_msg(__VA_ARGS__); } while(0)
238#else
239# define dbg1(...) ((void)0)
240#endif
241#if DEBUG_LVL >= 2
242# define dbg2(...) do { if (G.verbose >= 2) bb_error_msg(__VA_ARGS__); } while(0)
243#else
244# define dbg2(...) ((void)0)
245#endif
246#if DEBUG_LVL >= 3
247# define dbg3(...) do { if (G.verbose >= 3) bb_error_msg(__VA_ARGS__); } while(0)
248#else
249# define dbg3(...) ((void)0)
250#endif
251
252
253static const char keywords[] ALIGN1 = "add\0remove\0"; // "change\0"
Denys Vlasenko516530c2012-05-18 04:45:35 +0200254enum { OP_add, OP_remove };
255
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100256struct envmatch {
257 struct envmatch *next;
258 char *envname;
259 regex_t match;
260};
261
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200262struct rule {
263 bool keep_matching;
264 bool regex_compiled;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200265 mode_t mode;
266 int maj, min0, min1;
267 struct bb_uidgid_t ugid;
268 char *envvar;
269 char *ren_mov;
270 IF_FEATURE_MDEV_EXEC(char *r_cmd;)
271 regex_t match;
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100272 struct envmatch *envmatch;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200273};
274
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000275struct globals {
Rob Landley5cd1ccd2006-05-21 18:33:27 +0000276 int root_major, root_minor;
Denys Vlasenko516530c2012-05-18 04:45:35 +0200277 smallint verbose;
Denis Vlasenko065c7142009-04-13 22:23:02 +0000278 char *subsystem;
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100279 char *subsys_env; /* for putenv("SUBSYSTEM=subsystem") */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200280#if ENABLE_FEATURE_MDEV_CONF
281 const char *filename;
282 parser_t *parser;
283 struct rule **rule_vec;
284 unsigned rule_idx;
285#endif
286 struct rule cur_rule;
Denys Vlasenkobd77e9d2015-04-19 18:54:49 +0200287 char timestr[sizeof("HH:MM:SS.123456")];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100288} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200289#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenkoc9665632012-04-21 14:35:32 +0200290#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200291 setup_common_bufsiz(); \
Denys Vlasenkoc9665632012-04-21 14:35:32 +0200292 IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.maj = -1;) \
293 IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.mode = 0660;) \
294} while (0)
Denys Vlasenko16714242011-09-21 01:59:15 +0200295
Rob Landleya7e3d052006-02-21 06:11:13 +0000296
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000297/* Prevent infinite loops in /sys symlinks */
298#define MAX_SYSFS_DEPTH 3
Mike Frysingerb51fd352007-06-13 09:24:50 +0000299
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200300/* We use additional bytes in make_device() */
301#define SCRATCH_SIZE 128
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000302
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200303#if ENABLE_FEATURE_MDEV_CONF
304
305static void make_default_cur_rule(void)
306{
307 memset(&G.cur_rule, 0, sizeof(G.cur_rule));
308 G.cur_rule.maj = -1; /* "not a @major,minor rule" */
309 G.cur_rule.mode = 0660;
310}
311
312static void clean_up_cur_rule(void)
313{
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100314 struct envmatch *e;
315
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200316 free(G.cur_rule.envvar);
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100317 free(G.cur_rule.ren_mov);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200318 if (G.cur_rule.regex_compiled)
319 regfree(&G.cur_rule.match);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200320 IF_FEATURE_MDEV_EXEC(free(G.cur_rule.r_cmd);)
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100321 e = G.cur_rule.envmatch;
322 while (e) {
323 free(e->envname);
324 regfree(&e->match);
325 e = e->next;
326 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200327 make_default_cur_rule();
328}
329
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100330static char *parse_envmatch_pfx(char *val)
331{
332 struct envmatch **nextp = &G.cur_rule.envmatch;
333
334 for (;;) {
335 struct envmatch *e;
336 char *semicolon;
337 char *eq = strchr(val, '=');
338 if (!eq /* || eq == val? */)
339 return val;
340 if (endofname(val) != eq)
341 return val;
342 semicolon = strchr(eq, ';');
343 if (!semicolon)
344 return val;
345 /* ENVVAR=regex;... */
346 *nextp = e = xzalloc(sizeof(*e));
347 nextp = &e->next;
348 e->envname = xstrndup(val, eq - val);
349 *semicolon = '\0';
350 xregcomp(&e->match, eq + 1, REG_EXTENDED);
351 *semicolon = ';';
352 val = semicolon + 1;
353 }
354}
355
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200356static void parse_next_rule(void)
357{
358 /* Note: on entry, G.cur_rule is set to default */
359 while (1) {
360 char *tokens[4];
361 char *val;
362
Denys Vlasenko499597d2012-01-11 00:37:17 +0100363 /* No PARSE_EOL_COMMENTS, because command may contain '#' chars */
364 if (!config_read(G.parser, tokens, 4, 3, "# \t", PARSE_NORMAL & ~PARSE_EOL_COMMENTS))
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200365 break;
366
367 /* Fields: [-]regex uid:gid mode [alias] [cmd] */
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100368 dbg3("token1:'%s'", tokens[1]);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200369
370 /* 1st field */
371 val = tokens[0];
372 G.cur_rule.keep_matching = ('-' == val[0]);
373 val += G.cur_rule.keep_matching; /* swallow leading dash */
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100374 val = parse_envmatch_pfx(val);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200375 if (val[0] == '@') {
376 /* @major,minor[-minor2] */
377 /* (useful when name is ambiguous:
378 * "/sys/class/usb/lp0" and
379 * "/sys/class/printer/lp0")
380 */
381 int sc = sscanf(val, "@%u,%u-%u", &G.cur_rule.maj, &G.cur_rule.min0, &G.cur_rule.min1);
382 if (sc < 2 || G.cur_rule.maj < 0) {
383 bb_error_msg("bad @maj,min on line %d", G.parser->lineno);
384 goto next_rule;
385 }
386 if (sc == 2)
387 G.cur_rule.min1 = G.cur_rule.min0;
388 } else {
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100389 char *eq = strchr(val, '=');
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200390 if (val[0] == '$') {
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100391 /* $ENVVAR=regex ... */
392 val++;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200393 if (!eq) {
394 bb_error_msg("bad $envvar=regex on line %d", G.parser->lineno);
395 goto next_rule;
396 }
397 G.cur_rule.envvar = xstrndup(val, eq - val);
398 val = eq + 1;
399 }
400 xregcomp(&G.cur_rule.match, val, REG_EXTENDED);
401 G.cur_rule.regex_compiled = 1;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200402 }
403
404 /* 2nd field: uid:gid - device ownership */
Denys Vlasenko526d8582015-10-19 04:27:17 +0200405 if (get_uidgid(&G.cur_rule.ugid, tokens[1]) == 0) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200406 bb_error_msg("unknown user/group '%s' on line %d", tokens[1], G.parser->lineno);
407 goto next_rule;
408 }
409
410 /* 3rd field: mode - device permissions */
Denys Vlasenko5711a2a2015-10-07 17:55:33 +0200411 G.cur_rule.mode = bb_parse_mode(tokens[2], G.cur_rule.mode);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200412
413 /* 4th field (opt): ">|=alias" or "!" to not create the node */
414 val = tokens[3];
415 if (ENABLE_FEATURE_MDEV_RENAME && val && strchr(">=!", val[0])) {
416 char *s = skip_non_whitespace(val);
417 G.cur_rule.ren_mov = xstrndup(val, s - val);
418 val = skip_whitespace(s);
419 }
420
421 if (ENABLE_FEATURE_MDEV_EXEC && val && val[0]) {
422 const char *s = "$@*";
423 const char *s2 = strchr(s, val[0]);
424 if (!s2) {
425 bb_error_msg("bad line %u", G.parser->lineno);
426 goto next_rule;
427 }
428 IF_FEATURE_MDEV_EXEC(G.cur_rule.r_cmd = xstrdup(val);)
429 }
430
431 return;
432 next_rule:
433 clean_up_cur_rule();
434 } /* while (config_read) */
435
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100436 dbg3("config_close(G.parser)");
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200437 config_close(G.parser);
438 G.parser = NULL;
439
440 return;
441}
442
443/* If mdev -s, we remember rules in G.rule_vec[].
444 * Otherwise, there is no point in doing it, and we just
445 * save only one parsed rule in G.cur_rule.
446 */
447static const struct rule *next_rule(void)
448{
449 struct rule *rule;
450
451 /* Open conf file if we didn't do it yet */
452 if (!G.parser && G.filename) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100453 dbg3("config_open('%s')", G.filename);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200454 G.parser = config_open2(G.filename, fopen_for_read);
455 G.filename = NULL;
456 }
457
458 if (G.rule_vec) {
459 /* mdev -s */
460 /* Do we have rule parsed already? */
461 if (G.rule_vec[G.rule_idx]) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100462 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 +0200463 return G.rule_vec[G.rule_idx++];
464 }
465 make_default_cur_rule();
466 } else {
467 /* not mdev -s */
468 clean_up_cur_rule();
469 }
470
471 /* Parse one more rule if file isn't fully read */
472 rule = &G.cur_rule;
473 if (G.parser) {
474 parse_next_rule();
475 if (G.rule_vec) { /* mdev -s */
Ron Yorstond840c5d2015-07-19 23:05:20 +0200476 rule = xmemdup(&G.cur_rule, sizeof(G.cur_rule));
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200477 G.rule_vec = xrealloc_vector(G.rule_vec, 4, G.rule_idx);
478 G.rule_vec[G.rule_idx++] = rule;
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100479 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 +0200480 }
481 }
482
483 return rule;
484}
485
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100486static int env_matches(struct envmatch *e)
487{
488 while (e) {
489 int r;
490 char *val = getenv(e->envname);
491 if (!val)
492 return 0;
493 r = regexec(&e->match, val, /*size*/ 0, /*range[]*/ NULL, /*eflags*/ 0);
494 if (r != 0) /* no match */
495 return 0;
496 e = e->next;
497 }
498 return 1;
499}
500
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200501#else
502
503# define next_rule() (&G.cur_rule)
504
505#endif
Denys Vlasenko16714242011-09-21 01:59:15 +0200506
Denys Vlasenko4609f472013-01-21 01:22:12 +0100507static void mkdir_recursive(char *name)
508{
509 /* if name has many levels ("dir1/dir2"),
510 * bb_make_directory() will create dir1 according to umask,
511 * not according to its "mode" parameter.
512 * Since we run with umask=0, need to temporarily switch it.
513 */
514 umask(022); /* "dir1" (if any) will be 0755 too */
515 bb_make_directory(name, 0755, FILEUTILS_RECUR);
516 umask(0);
517}
518
Denis Vlasenkod9860482008-07-12 10:23:16 +0000519/* Builds an alias path.
520 * This function potentionally reallocates the alias parameter.
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000521 * Only used for ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkod9860482008-07-12 10:23:16 +0000522 */
523static char *build_alias(char *alias, const char *device_name)
524{
525 char *dest;
526
527 /* ">bar/": rename to bar/device_name */
528 /* ">bar[/]baz": rename to bar[/]baz */
529 dest = strrchr(alias, '/');
530 if (dest) { /* ">bar/[baz]" ? */
531 *dest = '\0'; /* mkdir bar */
Denys Vlasenko4609f472013-01-21 01:22:12 +0100532 mkdir_recursive(alias);
Denis Vlasenkod9860482008-07-12 10:23:16 +0000533 *dest = '/';
534 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
535 dest = alias;
536 alias = concat_path_file(alias, device_name);
537 free(dest);
538 }
539 }
540
541 return alias;
542}
543
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200544/* mknod in /dev based on a path like "/sys/block/hda/hda1"
545 * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes
Denys Vlasenko20a32622016-09-07 14:09:01 +0200546 * after NUL, but we promise to not mangle it (IOW: to restore NUL if needed).
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200547 * NB2: "mdev -s" may call us many times, do not leak memory/fds!
Denys Vlasenko516530c2012-05-18 04:45:35 +0200548 *
549 * device_name = $DEVNAME (may be NULL)
550 * path = /sys/$DEVPATH
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200551 */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200552static void make_device(char *device_name, char *path, int operation)
Rob Landley70f7ef72005-12-13 08:21:33 +0000553{
Denis Vlasenkof46be092006-10-16 19:39:37 +0000554 int major, minor, type, len;
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200555 char *path_end = path + strlen(path);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200556
Rob Landley15fe2e12006-05-08 02:53:23 +0000557 /* Try to read major/minor string. Note that the kernel puts \n after
558 * the data, so we don't need to worry about null terminating the string
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000559 * because sscanf() will stop at the first nondigit, which \n is.
560 * We also depend on path having writeable space after it.
Mike Frysingerc881c732007-11-19 09:04:22 +0000561 */
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000562 major = -1;
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200563 if (operation == OP_add) {
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200564 strcpy(path_end, "/dev");
565 len = open_read_close(path, path_end + 1, SCRATCH_SIZE - 1);
566 *path_end = '\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 */
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200572 } else if (sscanf(path_end + 1, "%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
Nikolaus Vossc3cf1e32013-05-13 03:55:11 +0200580 /* Determine device name */
581 if (!device_name) {
582 /*
583 * There was no $DEVNAME envvar (for example, mdev -s never has).
584 * But it is very useful: it contains the *path*, not only basename,
585 * Thankfully, uevent file has it.
586 * Example of .../sound/card0/controlC0/uevent file on Linux-3.7.7:
587 * MAJOR=116
588 * MINOR=7
589 * DEVNAME=snd/controlC0
590 */
591 strcpy(path_end, "/uevent");
592 len = open_read_close(path, path_end + 1, SCRATCH_SIZE - 1);
593 if (len < 0)
594 len = 0;
595 *path_end = '\0';
596 path_end[1 + len] = '\0';
597 device_name = strstr(path_end + 1, "\nDEVNAME=");
598 if (device_name) {
599 device_name += sizeof("\nDEVNAME=")-1;
600 strchrnul(device_name, '\n')[0] = '\0';
601 } else {
602 /* Fall back to just basename */
603 device_name = (char*) bb_basename(path);
604 }
605 }
606 /* Determine device type */
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100607 /*
608 * http://kernel.org/doc/pending/hotplug.txt says that only
Denis Vlasenkoa87045c2008-07-12 10:28:41 +0000609 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100610 * But since 2.6.25 block devices are also in /sys/class/block.
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200611 * We use strstr("/block/") to forestall future surprises.
612 */
Denis Vlasenko9504e442008-10-29 01:19:15 +0000613 type = S_IFCHR;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100614 if (strstr(path, "/block/") || (G.subsystem && is_prefixed_with(G.subsystem, "block")))
Denis Vlasenko9504e442008-10-29 01:19:15 +0000615 type = S_IFBLK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000616
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200617#if ENABLE_FEATURE_MDEV_CONF
618 G.rule_idx = 0; /* restart from the beginning (think mdev -s) */
619#endif
620 for (;;) {
621 const char *str_to_match;
622 regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP];
623 char *command;
624 char *alias;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000625 char aliaslink = aliaslink; /* for compiler */
Denys Vlasenko516530c2012-05-18 04:45:35 +0200626 char *node_name;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200627 const struct rule *rule;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000628
Denys Vlasenkob6beada2012-05-18 15:11:16 +0200629 str_to_match = device_name;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000630
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200631 rule = next_rule();
Denis Vlasenko3798db52009-04-19 21:37:07 +0000632
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200633#if ENABLE_FEATURE_MDEV_CONF
Denys Vlasenko40b97fb2013-02-26 00:40:46 +0100634 if (!env_matches(rule->envmatch))
635 continue;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200636 if (rule->maj >= 0) { /* @maj,min rule */
637 if (major != rule->maj)
638 continue;
639 if (minor < rule->min0 || minor > rule->min1)
640 continue;
641 memset(off, 0, sizeof(off));
642 goto rule_matches;
643 }
644 if (rule->envvar) { /* $envvar=regex rule */
645 str_to_match = getenv(rule->envvar);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100646 dbg3("getenv('%s'):'%s'", rule->envvar, str_to_match);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200647 if (!str_to_match)
648 continue;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200649 }
Denys Vlasenkob6beada2012-05-18 15:11:16 +0200650 /* else: str_to_match = device_name */
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000651
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200652 if (rule->regex_compiled) {
653 int regex_match = regexec(&rule->match, str_to_match, ARRAY_SIZE(off), off, 0);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100654 dbg3("regex_match for '%s':%d", str_to_match, regex_match);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200655 //bb_error_msg("matches:");
656 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
657 // if (off[i].rm_so < 0) continue;
658 // bb_error_msg("match %d: '%.*s'\n", i,
659 // (int)(off[i].rm_eo - off[i].rm_so),
660 // device_name + off[i].rm_so);
661 //}
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000662
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200663 if (regex_match != 0
664 /* regexec returns whole pattern as "range" 0 */
665 || off[0].rm_so != 0
666 || (int)off[0].rm_eo != (int)strlen(str_to_match)
667 ) {
668 continue; /* this rule doesn't match */
Denis Vlasenko3798db52009-04-19 21:37:07 +0000669 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200670 }
671 /* else: it's final implicit "match-all" rule */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200672 rule_matches:
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100673 dbg2("rule matched, line %d", G.parser ? G.parser->lineno : -1);
Denys Vlasenko4424dfd2013-03-17 23:01:17 +0100674#endif
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200675 /* Build alias name */
676 alias = NULL;
677 if (ENABLE_FEATURE_MDEV_RENAME && rule->ren_mov) {
678 aliaslink = rule->ren_mov[0];
679 if (aliaslink == '!') {
680 /* "!": suppress node creation/deletion */
681 major = -2;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000682 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200683 else if (aliaslink == '>' || aliaslink == '=') {
684 if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) {
685 char *s;
686 char *p;
687 unsigned n;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000688
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200689 /* substitute %1..9 with off[1..9], if any */
690 n = 0;
691 s = rule->ren_mov;
692 while (*s)
693 if (*s++ == '%')
694 n++;
Mike Frysingerc881c732007-11-19 09:04:22 +0000695
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200696 p = alias = xzalloc(strlen(rule->ren_mov) + n * strlen(str_to_match));
697 s = rule->ren_mov + 1;
698 while (*s) {
699 *p = *s;
700 if ('%' == *s) {
701 unsigned i = (s[1] - '0');
702 if (i <= 9 && off[i].rm_so >= 0) {
703 n = off[i].rm_eo - off[i].rm_so;
704 strncpy(p, str_to_match + off[i].rm_so, n);
705 p += n - 1;
706 s++;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000707 }
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000708 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200709 p++;
710 s++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000711 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200712 } else {
713 alias = xstrdup(rule->ren_mov + 1);
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200714 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000715 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000716 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100717 dbg3("alias:'%s'", alias);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000718
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200719 command = NULL;
720 IF_FEATURE_MDEV_EXEC(command = rule->r_cmd;)
721 if (command) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200722 /* Are we running this command now?
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100723 * Run @cmd on create, $cmd on delete, *cmd on any
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200724 */
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100725 if ((command[0] == '@' && operation == OP_add)
726 || (command[0] == '$' && operation == OP_remove)
727 || (command[0] == '*')
728 ) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200729 command++;
730 } else {
731 command = NULL;
732 }
733 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100734 dbg3("command:'%s'", command);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000735
736 /* "Execute" the line we found */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200737 node_name = device_name;
738 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
739 node_name = alias = build_alias(alias, device_name);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100740 dbg3("alias2:'%s'", alias);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000741 }
742
Denys Vlasenko516530c2012-05-18 04:45:35 +0200743 if (operation == OP_add && major >= 0) {
744 char *slash = strrchr(node_name, '/');
745 if (slash) {
746 *slash = '\0';
Denys Vlasenko4609f472013-01-21 01:22:12 +0100747 mkdir_recursive(node_name);
Denys Vlasenko516530c2012-05-18 04:45:35 +0200748 *slash = '/';
749 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100750 if (ENABLE_FEATURE_MDEV_CONF) {
751 dbg1("mknod %s (%d,%d) %o"
752 " %u:%u",
753 node_name, major, minor, rule->mode | type,
754 rule->ugid.uid, rule->ugid.gid
755 );
756 } else {
757 dbg1("mknod %s (%d,%d) %o",
758 node_name, major, minor, rule->mode | type
759 );
760 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200761 if (mknod(node_name, rule->mode | type, makedev(major, minor)) && errno != EEXIST)
762 bb_perror_msg("can't create '%s'", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200763 if (ENABLE_FEATURE_MDEV_CONF) {
764 chmod(node_name, rule->mode);
765 chown(node_name, rule->ugid.uid, rule->ugid.gid);
766 }
Denys Vlasenkoea57f512012-05-18 11:39:57 +0200767 if (major == G.root_major && minor == G.root_minor)
768 symlink(node_name, "root");
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200769 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
Denys Vlasenko638dbc32012-02-06 01:00:00 +0100770 if (aliaslink == '>') {
771//TODO: on devtmpfs, device_name already exists and symlink() fails.
772//End result is that instead of symlink, we have two nodes.
773//What should be done?
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100774 dbg1("symlink: %s", device_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200775 symlink(node_name, device_name);
Denys Vlasenko638dbc32012-02-06 01:00:00 +0100776 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200777 }
778 }
779
780 if (ENABLE_FEATURE_MDEV_EXEC && command) {
781 /* setenv will leak memory, use putenv/unsetenv/free */
782 char *s = xasprintf("%s=%s", "MDEV", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200783 putenv(s);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100784 dbg1("running: %s", command);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200785 if (system(command) == -1)
786 bb_perror_msg("can't run '%s'", command);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200787 bb_unsetenv_and_free(s);
788 }
789
Denys Vlasenko516530c2012-05-18 04:45:35 +0200790 if (operation == OP_remove && major >= -1) {
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200791 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
Denys Vlasenko516530c2012-05-18 04:45:35 +0200792 if (aliaslink == '>') {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100793 dbg1("unlink: %s", device_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200794 unlink(device_name);
Denys Vlasenko516530c2012-05-18 04:45:35 +0200795 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200796 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100797 dbg1("unlink: %s", node_name);
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200798 unlink(node_name);
799 }
800
801 if (ENABLE_FEATURE_MDEV_RENAME)
802 free(alias);
803
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000804 /* We found matching line.
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200805 * Stop unless it was prefixed with '-'
806 */
807 if (!ENABLE_FEATURE_MDEV_CONF || !rule->keep_matching)
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000808 break;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200809 } /* for (;;) */
Rob Landley70f7ef72005-12-13 08:21:33 +0000810}
811
Denys Vlasenko20a32622016-09-07 14:09:01 +0200812/* File callback for /sys/ traversal.
813 * We act only on "/sys/.../dev" (pseudo)file
814 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000815static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000816 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000817 void *userData,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000818 int depth UNUSED_PARAM)
Rob Landley70f7ef72005-12-13 08:21:33 +0000819{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000820 size_t len = strlen(fileName) - 4; /* can't underflow */
Denys Vlasenko20a32622016-09-07 14:09:01 +0200821 char *path = userData; /* char array[PATH_MAX + SCRATCH_SIZE] */
822 char subsys[PATH_MAX];
823 int res;
Rob Landley70f7ef72005-12-13 08:21:33 +0000824
Denys Vlasenko20a32622016-09-07 14:09:01 +0200825 /* Is it a ".../dev" file? (len check is for paranoid reasons) */
826 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX - 32)
827 return FALSE; /* not .../dev */
Rob Landley70f7ef72005-12-13 08:21:33 +0000828
Denys Vlasenko20a32622016-09-07 14:09:01 +0200829 strcpy(path, fileName);
830 path[len] = '\0';
831
832 /* Read ".../subsystem" symlink in the same directory where ".../dev" is */
833 strcpy(subsys, path);
834 strcpy(subsys + len, "/subsystem");
835 res = readlink(subsys, subsys, sizeof(subsys)-1);
836 if (res > 0) {
837 subsys[res] = '\0';
838 free(G.subsystem);
839 if (G.subsys_env) {
840 bb_unsetenv_and_free(G.subsys_env);
841 G.subsys_env = NULL;
842 }
843 /* Set G.subsystem and $SUBSYSTEM from symlink's last component */
844 G.subsystem = strrchr(subsys, '/');
845 if (G.subsystem) {
846 G.subsystem = xstrdup(G.subsystem + 1);
847 G.subsys_env = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem);
848 putenv(G.subsys_env);
849 }
850 }
851
852 make_device(/*DEVNAME:*/ NULL, path, OP_add);
Rob Landley70f7ef72005-12-13 08:21:33 +0000853
Mike Frysingerb51fd352007-06-13 09:24:50 +0000854 return TRUE;
855}
Rob Landley70f7ef72005-12-13 08:21:33 +0000856
Mike Frysingerb51fd352007-06-13 09:24:50 +0000857/* Directory callback for /sys/ traversal */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000858static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
859 struct stat *statbuf UNUSED_PARAM,
860 void *userData UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000861 int depth)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000862{
863 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000864}
865
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000866/* For the full gory details, see linux/Documentation/firmware_class/README
867 *
868 * Firmware loading works like this:
869 * - kernel sets FIRMWARE env var
870 * - userspace checks /lib/firmware/$FIRMWARE
871 * - userspace waits for /sys/$DEVPATH/loading to appear
872 * - userspace writes "1" to /sys/$DEVPATH/loading
873 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
874 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
875 * - kernel loads firmware into device
876 */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000877static void load_firmware(const char *firmware, const char *sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000878{
879 int cnt;
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200880 int firmware_fd, loading_fd;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000881
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000882 /* check for /lib/firmware/$FIRMWARE */
Denys Vlasenko20a32622016-09-07 14:09:01 +0200883 firmware_fd = -1;
884 if (chdir("/lib/firmware") == 0)
885 firmware_fd = open(firmware, O_RDONLY); /* can fail */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000886
887 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
888 xchdir(sysfs_path);
889 for (cnt = 0; cnt < 30; ++cnt) {
890 loading_fd = open("loading", O_WRONLY);
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200891 if (loading_fd >= 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000892 goto loading;
893 sleep(1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000894 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000895 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000896
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000897 loading:
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200898 cnt = 0;
899 if (firmware_fd >= 0) {
900 int data_fd;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000901
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200902 /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */
903 if (full_write(loading_fd, "1", 1) != 1)
904 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000905
Christoph Lohmann25cb5aa2012-05-19 23:39:45 +0200906 /* load firmware into /sys/$DEVPATH/data */
907 data_fd = open("data", O_WRONLY);
908 if (data_fd < 0)
909 goto out;
910 cnt = bb_copyfd_eof(firmware_fd, data_fd);
911 if (ENABLE_FEATURE_CLEAN_UP)
912 close(data_fd);
913 }
914
915 /* Tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading"
916 * Note: we emit -1 also if firmware file wasn't found.
917 * There are cases when otherwise kernel would wait for minutes
918 * before timing out.
919 */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000920 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000921 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000922 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000923 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000924
925 out:
Denys Vlasenkoba76b7a2013-02-04 23:35:27 +0100926 xchdir("/dev");
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000927 if (ENABLE_FEATURE_CLEAN_UP) {
928 close(firmware_fd);
929 close(loading_fd);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000930 }
931}
932
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100933static char *curtime(void)
934{
935 struct timeval tv;
936 gettimeofday(&tv, NULL);
Denys Vlasenkobd77e9d2015-04-19 18:54:49 +0200937 sprintf(
938 strftime_HHMMSS(G.timestr, sizeof(G.timestr), &tv.tv_sec),
939 ".%06u",
940 (unsigned)tv.tv_usec
941 );
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100942 return G.timestr;
943}
944
945static void open_mdev_log(const char *seq, unsigned my_pid)
946{
947 int logfd = open("mdev.log", O_WRONLY | O_APPEND);
948 if (logfd >= 0) {
949 xmove_fd(logfd, STDERR_FILENO);
950 G.verbose = 2;
951 applet_name = xasprintf("%s[%s]", applet_name, seq ? seq : utoa(my_pid));
952 }
953}
954
955/* If it exists, does /dev/mdev.seq match $SEQNUM?
956 * If it does not match, earlier mdev is running
957 * in parallel, and we need to wait.
958 * Active mdev pokes us with SIGCHLD to check the new file.
959 */
960static int
Denys Vlasenkoad795512015-04-19 18:55:12 +0200961wait_for_seqfile(unsigned expected_seq)
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100962{
963 /* We time out after 2 sec */
964 static const struct timespec ts = { 0, 32*1000*1000 };
965 int timeout = 2000 / 32;
966 int seq_fd = -1;
967 int do_once = 1;
968 sigset_t set_CHLD;
969
970 sigemptyset(&set_CHLD);
971 sigaddset(&set_CHLD, SIGCHLD);
972 sigprocmask(SIG_BLOCK, &set_CHLD, NULL);
973
974 for (;;) {
975 int seqlen;
Denys Vlasenkoad795512015-04-19 18:55:12 +0200976 char seqbuf[sizeof(long)*3 + 2];
977 unsigned seqbufnum;
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100978
979 if (seq_fd < 0) {
980 seq_fd = open("mdev.seq", O_RDWR);
981 if (seq_fd < 0)
982 break;
Denys Vlasenkoad795512015-04-19 18:55:12 +0200983 close_on_exec_on(seq_fd);
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100984 }
985 seqlen = pread(seq_fd, seqbuf, sizeof(seqbuf) - 1, 0);
986 if (seqlen < 0) {
987 close(seq_fd);
988 seq_fd = -1;
989 break;
990 }
991 seqbuf[seqlen] = '\0';
Denys Vlasenko26a8b9f2014-09-08 18:19:15 +0200992 if (seqbuf[0] == '\n' || seqbuf[0] == '\0') {
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100993 /* seed file: write out seq ASAP */
Denys Vlasenkoad795512015-04-19 18:55:12 +0200994 xwrite_str(seq_fd, utoa(expected_seq));
Denys Vlasenko5bce1352013-02-27 10:51:41 +0100995 xlseek(seq_fd, 0, SEEK_SET);
996 dbg2("first seq written");
997 break;
998 }
Denys Vlasenkoad795512015-04-19 18:55:12 +0200999 seqbufnum = atoll(seqbuf);
1000 if (seqbufnum == expected_seq) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001001 /* correct idx */
1002 break;
1003 }
Denys Vlasenkoad795512015-04-19 18:55:12 +02001004 if (seqbufnum > expected_seq) {
1005 /* a later mdev runs already (this was seen by users to happen) */
1006 /* do not overwrite seqfile on exit */
1007 close(seq_fd);
1008 seq_fd = -1;
1009 break;
1010 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001011 if (do_once) {
Denys Vlasenkoad795512015-04-19 18:55:12 +02001012 dbg2("%s mdev.seq='%s', need '%u'", curtime(), seqbuf, expected_seq);
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001013 do_once = 0;
1014 }
1015 if (sigtimedwait(&set_CHLD, NULL, &ts) >= 0) {
1016 dbg3("woken up");
1017 continue; /* don't decrement timeout! */
1018 }
1019 if (--timeout == 0) {
Denys Vlasenkobd77e9d2015-04-19 18:54:49 +02001020 dbg1("%s mdev.seq='%s'", "timed out", seqbuf);
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001021 break;
1022 }
1023 }
1024 sigprocmask(SIG_UNBLOCK, &set_CHLD, NULL);
1025 return seq_fd;
1026}
1027
1028static void signal_mdevs(unsigned my_pid)
1029{
1030 procps_status_t* p = NULL;
1031 while ((p = procps_scan(p, PSSCAN_ARGV0)) != NULL) {
1032 if (p->pid != my_pid
1033 && p->argv0
1034 && strcmp(bb_basename(p->argv0), "mdev") == 0
1035 ) {
1036 kill(p->pid, SIGCHLD);
1037 }
1038 }
1039}
1040
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001041int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkod9860482008-07-12 10:23:16 +00001042int mdev_main(int argc UNUSED_PARAM, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +00001043{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +00001044 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +00001045
Denys Vlasenko16714242011-09-21 01:59:15 +02001046 INIT_G();
1047
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +02001048#if ENABLE_FEATURE_MDEV_CONF
1049 G.filename = "/etc/mdev.conf";
1050#endif
1051
Denis Vlasenkod48e81f2008-07-06 07:00:11 +00001052 /* We can be called as hotplug helper */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +00001053 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +00001054 bb_sanitize_stdio();
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +00001055
Denis Vlasenko3798db52009-04-19 21:37:07 +00001056 /* Force the configuration file settings exactly */
1057 umask(0);
1058
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +00001059 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +00001060
Denis Vlasenkoaafbae62009-04-13 13:33:02 +00001061 if (argv[1] && strcmp(argv[1], "-s") == 0) {
Denys Vlasenko40b97fb2013-02-26 00:40:46 +01001062 /*
1063 * Scan: mdev -s
Mike Frysingerc881c732007-11-19 09:04:22 +00001064 */
Rob Landleya7e3d052006-02-21 06:11:13 +00001065 struct stat st;
1066
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +02001067#if ENABLE_FEATURE_MDEV_CONF
1068 /* Same as xrealloc_vector(NULL, 4, 0): */
1069 G.rule_vec = xzalloc((1 << 4) * sizeof(*G.rule_vec));
1070#endif
Denis Vlasenkof46be092006-10-16 19:39:37 +00001071 xstat("/", &st);
Denys Vlasenko64bb95a2010-02-24 10:39:47 +01001072 G.root_major = major(st.st_dev);
1073 G.root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +00001074
Denys Vlasenko40b97fb2013-02-26 00:40:46 +01001075 putenv((char*)"ACTION=add");
1076
Denys Vlasenko20a32622016-09-07 14:09:01 +02001077 /* Create all devices from /sys/dev hierarchy */
1078 recursive_action("/sys/dev",
1079 ACTION_RECURSE | ACTION_FOLLOWLINKS,
1080 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +00001081 } else {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +00001082 char *fw;
Denis Vlasenko018bee62008-07-15 22:33:13 +00001083 char *seq;
1084 char *action;
Denys Vlasenko516530c2012-05-18 04:45:35 +02001085 char *env_devname;
1086 char *env_devpath;
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001087 unsigned my_pid;
Denys Vlasenkoad795512015-04-19 18:55:12 +02001088 unsigned seqnum = seqnum; /* for compiler */
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001089 int seq_fd;
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +01001090 smalluint op;
Denis Vlasenko018bee62008-07-15 22:33:13 +00001091
Mike Frysingerc881c732007-11-19 09:04:22 +00001092 /* Hotplug:
Denis Vlasenko065c7142009-04-13 22:23:02 +00001093 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001094 * ACTION can be "add", "remove", "change"
Mike Frysingerc881c732007-11-19 09:04:22 +00001095 * DEVPATH is like "/block/sda" or "/class/input/mice"
1096 */
Denys Vlasenko516530c2012-05-18 04:45:35 +02001097 env_devname = getenv("DEVNAME"); /* can be NULL */
Denys Vlasenko64bb95a2010-02-24 10:39:47 +01001098 G.subsystem = getenv("SUBSYSTEM");
Denys Vlasenkod35cbad2013-04-02 14:37:06 +02001099 action = getenv("ACTION");
1100 env_devpath = getenv("DEVPATH");
Denys Vlasenko516530c2012-05-18 04:45:35 +02001101 if (!action || !env_devpath /*|| !G.subsystem*/)
Mike Frysingera421ba82006-02-03 00:25:37 +00001102 bb_show_usage();
Denis Vlasenko1fb26da2009-04-13 02:15:57 +00001103 fw = getenv("FIRMWARE");
Denis Vlasenko018bee62008-07-15 22:33:13 +00001104 seq = getenv("SEQNUM");
Denys Vlasenkod35cbad2013-04-02 14:37:06 +02001105 op = index_in_strings(keywords, action);
Denis Vlasenko1fb26da2009-04-13 02:15:57 +00001106
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001107 my_pid = getpid();
1108 open_mdev_log(seq, my_pid);
Denis Vlasenko018bee62008-07-15 22:33:13 +00001109
Denys Vlasenkoad795512015-04-19 18:55:12 +02001110 seq_fd = -1;
1111 if (seq) {
1112 seqnum = atoll(seq);
1113 seq_fd = wait_for_seqfile(seqnum);
1114 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001115
1116 dbg1("%s "
1117 "ACTION:%s SUBSYSTEM:%s DEVNAME:%s DEVPATH:%s"
1118 "%s%s",
1119 curtime(),
1120 action, G.subsystem, env_devname, env_devpath,
1121 fw ? " FW:" : "", fw ? fw : ""
1122 );
Denys Vlasenko516530c2012-05-18 04:45:35 +02001123
1124 snprintf(temp, PATH_MAX, "/sys%s", env_devpath);
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +01001125 if (op == OP_remove) {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +00001126 /* Ignoring "remove firmware". It was reported
1127 * to happen and to cause erroneous deletion
1128 * of device nodes. */
1129 if (!fw)
Denys Vlasenko516530c2012-05-18 04:45:35 +02001130 make_device(env_devname, temp, op);
Denis Vlasenko1fb26da2009-04-13 02:15:57 +00001131 }
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001132 else {
Denys Vlasenko516530c2012-05-18 04:45:35 +02001133 make_device(env_devname, temp, op);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +00001134 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001135 if (op == OP_add && fw)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +00001136 load_firmware(fw, temp);
1137 }
Mike Frysingera78ef2c2007-06-13 07:34:15 +00001138 }
Denis Vlasenko018bee62008-07-15 22:33:13 +00001139
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001140 dbg1("%s exiting", curtime());
1141 if (seq_fd >= 0) {
Denys Vlasenkoad795512015-04-19 18:55:12 +02001142 xwrite_str(seq_fd, utoa(seqnum + 1));
Denys Vlasenko5bce1352013-02-27 10:51:41 +01001143 signal_mdevs(my_pid);
Denis Vlasenko018bee62008-07-15 22:33:13 +00001144 }
Rob Landley29e08ff2006-01-12 06:13:50 +00001145 }
1146
Mike Frysingerc881c732007-11-19 09:04:22 +00001147 if (ENABLE_FEATURE_CLEAN_UP)
1148 RELEASE_CONFIG_BUFFER(temp);
1149
Denis Vlasenkoaafbae62009-04-13 13:33:02 +00001150 return EXIT_SUCCESS;
Rob Landley70f7ef72005-12-13 08:21:33 +00001151}