blob: c6be1b872772a6ca34ac93add75ca10a9646c0cd [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
2/*
Rob Landley70f7ef72005-12-13 08:21:33 +00003 * mdev - Mini udev for busybox
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00004 *
Rob Landley70f7ef72005-12-13 08:21:33 +00005 * Copyright 2005 Rob Landley <rob@landley.net>
6 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
7 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Rob Landley70f7ef72005-12-13 08:21:33 +00009 */
Pere Orga5bc8c002011-04-11 03:29:49 +020010
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020011//config:config MDEV
12//config: bool "mdev"
13//config: default y
14//config: select PLATFORM_LINUX
15//config: help
16//config: mdev is a mini-udev implementation for dynamically creating device
17//config: nodes in the /dev directory.
18//config:
19//config: For more information, please see docs/mdev.txt
20//config:
21//config:config FEATURE_MDEV_CONF
22//config: bool "Support /etc/mdev.conf"
23//config: default y
24//config: depends on MDEV
25//config: help
26//config: Add support for the mdev config file to control ownership and
27//config: permissions of the device nodes.
28//config:
29//config: For more information, please see docs/mdev.txt
30//config:
31//config:config FEATURE_MDEV_RENAME
32//config: bool "Support subdirs/symlinks"
33//config: default y
34//config: depends on FEATURE_MDEV_CONF
35//config: help
36//config: Add support for renaming devices and creating symlinks.
37//config:
38//config: For more information, please see docs/mdev.txt
39//config:
40//config:config FEATURE_MDEV_RENAME_REGEXP
41//config: bool "Support regular expressions substitutions when renaming device"
42//config: default y
43//config: depends on FEATURE_MDEV_RENAME
44//config: help
45//config: Add support for regular expressions substitutions when renaming
46//config: device.
47//config:
48//config:config FEATURE_MDEV_EXEC
49//config: bool "Support command execution at device addition/removal"
50//config: default y
51//config: depends on FEATURE_MDEV_CONF
52//config: help
53//config: This adds support for an optional field to /etc/mdev.conf for
54//config: executing commands when devices are created/removed.
55//config:
56//config: For more information, please see docs/mdev.txt
57//config:
58//config:config FEATURE_MDEV_LOAD_FIRMWARE
59//config: bool "Support loading of firmwares"
60//config: default y
61//config: depends on MDEV
62//config: help
63//config: Some devices need to load firmware before they can be usable.
64//config:
65//config: These devices will request userspace look up the files in
66//config: /lib/firmware/ and if it exists, send it to the kernel for
67//config: loading into the hardware.
68
69//applet:IF_MDEV(APPLET(mdev, BB_DIR_SBIN, BB_SUID_DROP))
70
71//kbuild:lib-$(CONFIG_MDEV) += mdev.o
72
Pere Orga5bc8c002011-04-11 03:29:49 +020073//usage:#define mdev_trivial_usage
74//usage: "[-s]"
75//usage:#define mdev_full_usage "\n\n"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020076//usage: "mdev -s is to be run during boot to scan /sys and populate /dev.\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020077//usage: "\n"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020078//usage: "Bare mdev is a kernel hotplug helper. To activate it:\n"
79//usage: " echo /sbin/mdev >/proc/sys/kernel/hotplug\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020080//usage: IF_FEATURE_MDEV_CONF(
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020081//usage: "\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020082//usage: "It uses /etc/mdev.conf with lines\n"
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020083//usage: " [-]DEVNAME UID:GID PERM"
84//usage: IF_FEATURE_MDEV_RENAME(" [>|=PATH]|[!]")
Pere Orga5bc8c002011-04-11 03:29:49 +020085//usage: IF_FEATURE_MDEV_EXEC(" [@|$|*PROG]")
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020086//usage: "\n"
87//usage: "where DEVNAME is device name regex, @major,minor[-minor2], or\n"
88//usage: "environment variable regex. A common use of the latter is\n"
89//usage: "to load modules for hotplugged devices:\n"
90//usage: " $MODALIAS=.* 0:0 660 @modprobe \"$MODALIAS\"\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020091//usage: )
Denys Vlasenko5c6ba6c2011-09-19 12:18:11 +020092//usage: "\n"
93//usage: "If /dev/mdev.seq file exists, mdev will wait for its value\n"
94//usage: "to match $SEQNUM variable. This prevents plug/unplug races.\n"
95//usage: "To activate this feature, create empty /dev/mdev.seq at boot."
Pere Orga5bc8c002011-04-11 03:29:49 +020096
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000097#include "libbb.h"
Rob Landleyb56c2852005-12-17 10:52:30 +000098#include "xregex.h"
Rob Landley70f7ef72005-12-13 08:21:33 +000099
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000100/* "mdev -s" scans /sys/class/xxx, looking for directories which have dev
101 * file (it is of the form "M:m\n"). Example: /sys/class/tty/tty0/dev
102 * contains "4:0\n". Directory name is taken as device name, path component
103 * directly after /sys/class/ as subsystem. In this example, "tty0" and "tty".
104 * Then mdev creates the /dev/device_name node.
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000105 * If /sys/class/.../dev file does not exist, mdev still may act
106 * on this device: see "@|$|*command args..." parameter in config file.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000107 *
108 * mdev w/o parameters is called as hotplug helper. It takes device
109 * and subsystem names from $DEVPATH and $SUBSYSTEM, extracts
110 * maj,min from "/sys/$DEVPATH/dev" and also examines
111 * $ACTION ("add"/"delete") and $FIRMWARE.
112 *
113 * If action is "add", mdev creates /dev/device_name similarly to mdev -s.
114 * (todo: explain "delete" and $FIRMWARE)
115 *
116 * If /etc/mdev.conf exists, it may modify /dev/device_name's properties.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000117 *
118 * Leading minus in 1st field means "don't stop on this line", otherwise
119 * search is stopped after the matching line is encountered.
120 *
Denys Vlasenko45f0a112011-09-22 04:55:47 +0200121 * $envvar=regex format is useful for loading modules for hot-plugged devices
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000122 * which do not have driver loaded yet. In this case /sys/class/.../dev
123 * does not exist, but $MODALIAS is set to needed module's name
124 * (actually, an alias to it) by kernel. This rule instructs mdev
125 * to load the module and exit:
126 * $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"
127 * The kernel will generate another hotplug event when /sys/class/.../dev
128 * file appears.
129 *
130 * When line matches, the device node is created, chmod'ed and chown'ed,
131 * moved to path, and if >path, a symlink to moved node is created,
132 * all this if /sys/class/.../dev exists.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000133 * Examples:
134 * =loop/ - moves to /dev/loop
135 * >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000136 *
137 * Then "command args..." is executed (via sh -c 'command args...').
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000138 * @:execute on creation, $:on deletion, *:on both.
Denis Vlasenko11c17f72009-04-19 23:38:08 +0000139 * This happens regardless of /sys/class/.../dev existence.
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000140 */
141
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200142struct rule {
143 bool keep_matching;
144 bool regex_compiled;
145 bool regex_has_slash;
146 mode_t mode;
147 int maj, min0, min1;
148 struct bb_uidgid_t ugid;
149 char *envvar;
150 char *ren_mov;
151 IF_FEATURE_MDEV_EXEC(char *r_cmd;)
152 regex_t match;
153};
154
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000155struct globals {
Rob Landley5cd1ccd2006-05-21 18:33:27 +0000156 int root_major, root_minor;
Denis Vlasenko065c7142009-04-13 22:23:02 +0000157 char *subsystem;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200158#if ENABLE_FEATURE_MDEV_CONF
159 const char *filename;
160 parser_t *parser;
161 struct rule **rule_vec;
162 unsigned rule_idx;
163#endif
164 struct rule cur_rule;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100165} FIX_ALIASING;
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000166#define G (*(struct globals*)&bb_common_bufsiz1)
Denys Vlasenko16714242011-09-21 01:59:15 +0200167#define INIT_G() do { } while (0)
168
Rob Landleya7e3d052006-02-21 06:11:13 +0000169
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000170/* Prevent infinite loops in /sys symlinks */
171#define MAX_SYSFS_DEPTH 3
Mike Frysingerb51fd352007-06-13 09:24:50 +0000172
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000173/* We use additional 64+ bytes in make_device() */
174#define SCRATCH_SIZE 80
175
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200176#if 0
177# define dbg(...) bb_error_msg(__VA_ARGS__)
178#else
179# define dbg(...) ((void)0)
180#endif
181
182
183#if ENABLE_FEATURE_MDEV_CONF
184
185static void make_default_cur_rule(void)
186{
187 memset(&G.cur_rule, 0, sizeof(G.cur_rule));
188 G.cur_rule.maj = -1; /* "not a @major,minor rule" */
189 G.cur_rule.mode = 0660;
190}
191
192static void clean_up_cur_rule(void)
193{
194 free(G.cur_rule.envvar);
195 if (G.cur_rule.regex_compiled)
196 regfree(&G.cur_rule.match);
197 free(G.cur_rule.ren_mov);
198 IF_FEATURE_MDEV_EXEC(free(G.cur_rule.r_cmd);)
199 make_default_cur_rule();
200}
201
202static void parse_next_rule(void)
203{
204 /* Note: on entry, G.cur_rule is set to default */
205 while (1) {
206 char *tokens[4];
207 char *val;
208
209 if (!config_read(G.parser, tokens, 4, 3, "# \t", PARSE_NORMAL))
210 break;
211
212 /* Fields: [-]regex uid:gid mode [alias] [cmd] */
213 dbg("token1:'%s'", tokens[1]);
214
215 /* 1st field */
216 val = tokens[0];
217 G.cur_rule.keep_matching = ('-' == val[0]);
218 val += G.cur_rule.keep_matching; /* swallow leading dash */
219 if (val[0] == '@') {
220 /* @major,minor[-minor2] */
221 /* (useful when name is ambiguous:
222 * "/sys/class/usb/lp0" and
223 * "/sys/class/printer/lp0")
224 */
225 int sc = sscanf(val, "@%u,%u-%u", &G.cur_rule.maj, &G.cur_rule.min0, &G.cur_rule.min1);
226 if (sc < 2 || G.cur_rule.maj < 0) {
227 bb_error_msg("bad @maj,min on line %d", G.parser->lineno);
228 goto next_rule;
229 }
230 if (sc == 2)
231 G.cur_rule.min1 = G.cur_rule.min0;
232 } else {
233 if (val[0] == '$') {
234 char *eq = strchr(++val, '=');
235 if (!eq) {
236 bb_error_msg("bad $envvar=regex on line %d", G.parser->lineno);
237 goto next_rule;
238 }
239 G.cur_rule.envvar = xstrndup(val, eq - val);
240 val = eq + 1;
241 }
242 xregcomp(&G.cur_rule.match, val, REG_EXTENDED);
243 G.cur_rule.regex_compiled = 1;
244 G.cur_rule.regex_has_slash = (strchr(val, '/') != NULL);
245 }
246
247 /* 2nd field: uid:gid - device ownership */
248 if (get_uidgid(&G.cur_rule.ugid, tokens[1], /*allow_numeric:*/ 1) == 0) {
249 bb_error_msg("unknown user/group '%s' on line %d", tokens[1], G.parser->lineno);
250 goto next_rule;
251 }
252
253 /* 3rd field: mode - device permissions */
254 bb_parse_mode(tokens[2], &G.cur_rule.mode);
255
256 /* 4th field (opt): ">|=alias" or "!" to not create the node */
257 val = tokens[3];
258 if (ENABLE_FEATURE_MDEV_RENAME && val && strchr(">=!", val[0])) {
259 char *s = skip_non_whitespace(val);
260 G.cur_rule.ren_mov = xstrndup(val, s - val);
261 val = skip_whitespace(s);
262 }
263
264 if (ENABLE_FEATURE_MDEV_EXEC && val && val[0]) {
265 const char *s = "$@*";
266 const char *s2 = strchr(s, val[0]);
267 if (!s2) {
268 bb_error_msg("bad line %u", G.parser->lineno);
269 goto next_rule;
270 }
271 IF_FEATURE_MDEV_EXEC(G.cur_rule.r_cmd = xstrdup(val);)
272 }
273
274 return;
275 next_rule:
276 clean_up_cur_rule();
277 } /* while (config_read) */
278
279 dbg("config_close(G.parser)");
280 config_close(G.parser);
281 G.parser = NULL;
282
283 return;
284}
285
286/* If mdev -s, we remember rules in G.rule_vec[].
287 * Otherwise, there is no point in doing it, and we just
288 * save only one parsed rule in G.cur_rule.
289 */
290static const struct rule *next_rule(void)
291{
292 struct rule *rule;
293
294 /* Open conf file if we didn't do it yet */
295 if (!G.parser && G.filename) {
296 dbg("config_open('%s')", G.filename);
297 G.parser = config_open2(G.filename, fopen_for_read);
298 G.filename = NULL;
299 }
300
301 if (G.rule_vec) {
302 /* mdev -s */
303 /* Do we have rule parsed already? */
304 if (G.rule_vec[G.rule_idx]) {
305 dbg("< G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]);
306 return G.rule_vec[G.rule_idx++];
307 }
308 make_default_cur_rule();
309 } else {
310 /* not mdev -s */
311 clean_up_cur_rule();
312 }
313
314 /* Parse one more rule if file isn't fully read */
315 rule = &G.cur_rule;
316 if (G.parser) {
317 parse_next_rule();
318 if (G.rule_vec) { /* mdev -s */
319 rule = memcpy(xmalloc(sizeof(G.cur_rule)), &G.cur_rule, sizeof(G.cur_rule));
320 G.rule_vec = xrealloc_vector(G.rule_vec, 4, G.rule_idx);
321 G.rule_vec[G.rule_idx++] = rule;
322 dbg("> G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]);
323 }
324 }
325
326 return rule;
327}
328
329#else
330
331# define next_rule() (&G.cur_rule)
332
333#endif
Denys Vlasenko16714242011-09-21 01:59:15 +0200334
Denis Vlasenkod9860482008-07-12 10:23:16 +0000335/* Builds an alias path.
336 * This function potentionally reallocates the alias parameter.
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000337 * Only used for ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkod9860482008-07-12 10:23:16 +0000338 */
339static char *build_alias(char *alias, const char *device_name)
340{
341 char *dest;
342
343 /* ">bar/": rename to bar/device_name */
344 /* ">bar[/]baz": rename to bar[/]baz */
345 dest = strrchr(alias, '/');
346 if (dest) { /* ">bar/[baz]" ? */
347 *dest = '\0'; /* mkdir bar */
348 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
349 *dest = '/';
350 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
351 dest = alias;
352 alias = concat_path_file(alias, device_name);
353 free(dest);
354 }
355 }
356
357 return alias;
358}
359
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200360/* mknod in /dev based on a path like "/sys/block/hda/hda1"
361 * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes
362 * after NUL, but we promise to not mangle (IOW: to restore if needed)
363 * path string.
364 * NB2: "mdev -s" may call us many times, do not leak memory/fds!
365 */
Rob Landleyef10d522006-06-26 14:11:33 +0000366static void make_device(char *path, int delete)
Rob Landley70f7ef72005-12-13 08:21:33 +0000367{
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100368 char *device_name, *subsystem_slash_devname;
Denis Vlasenkof46be092006-10-16 19:39:37 +0000369 int major, minor, type, len;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200370
371 dbg("%s('%s', delete:%d)", __func__, path, delete);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000372
Rob Landley15fe2e12006-05-08 02:53:23 +0000373 /* Try to read major/minor string. Note that the kernel puts \n after
374 * the data, so we don't need to worry about null terminating the string
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000375 * because sscanf() will stop at the first nondigit, which \n is.
376 * We also depend on path having writeable space after it.
Mike Frysingerc881c732007-11-19 09:04:22 +0000377 */
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000378 major = -1;
Rob Landley10b36f92006-08-10 01:09:37 +0000379 if (!delete) {
Denis Vlasenko3798db52009-04-19 21:37:07 +0000380 char *dev_maj_min = path + strlen(path);
381
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000382 strcpy(dev_maj_min, "/dev");
383 len = open_read_close(path, dev_maj_min + 1, 64);
Denis Vlasenko3798db52009-04-19 21:37:07 +0000384 *dev_maj_min = '\0';
Mike Frysingerae86a332008-02-20 18:31:36 +0000385 if (len < 1) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000386 if (!ENABLE_FEATURE_MDEV_EXEC)
Mike Frysingerae86a332008-02-20 18:31:36 +0000387 return;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000388 /* no "dev" file, but we can still run scripts
389 * based on device name */
390 } else if (sscanf(++dev_maj_min, "%u:%u", &major, &minor) != 2) {
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000391 major = -1;
Mike Frysingerae86a332008-02-20 18:31:36 +0000392 }
Rob Landley10b36f92006-08-10 01:09:37 +0000393 }
Denys Vlasenkof2860bf2010-08-16 14:26:15 +0200394 /* else: for delete, -1 still deletes the node, but < -1 suppresses that */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000395
Rob Landley70f7ef72005-12-13 08:21:33 +0000396 /* Determine device name, type, major and minor */
Denis Vlasenko3798db52009-04-19 21:37:07 +0000397 device_name = (char*) bb_basename(path);
Denis Vlasenkoaa8a6012008-03-29 16:07:46 +0000398 /* http://kernel.org/doc/pending/hotplug.txt says that only
Denis Vlasenkoa87045c2008-07-12 10:28:41 +0000399 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100400 * But since 2.6.25 block devices are also in /sys/class/block.
401 * We use strstr("/block/") to forestall future surprises. */
Denis Vlasenko9504e442008-10-29 01:19:15 +0000402 type = S_IFCHR;
Denys Vlasenko8d4a8d12010-03-16 18:37:19 +0100403 if (strstr(path, "/block/") || (G.subsystem && strncmp(G.subsystem, "block", 5) == 0))
Denis Vlasenko9504e442008-10-29 01:19:15 +0000404 type = S_IFBLK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000405
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000406 /* Make path point to "subsystem/device_name" */
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100407 subsystem_slash_devname = NULL;
408 /* Check for coldplug invocations first */
409 if (strncmp(path, "/sys/block/", 11) == 0) /* legacy case */
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000410 path += sizeof("/sys/") - 1;
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100411 else if (strncmp(path, "/sys/class/", 11) == 0)
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000412 path += sizeof("/sys/class/") - 1;
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100413 else {
414 /* Example of a hotplug invocation:
415 * SUBSYSTEM="block"
416 * DEVPATH="/sys" + "/devices/virtual/mtd/mtd3/mtdblock3"
417 * ("/sys" is added by mdev_main)
418 * - path does not contain subsystem
419 */
420 subsystem_slash_devname = concat_path_file(G.subsystem, device_name);
421 path = subsystem_slash_devname;
422 }
Denis Vlasenko6dda5de2009-04-16 21:42:12 +0000423
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200424#if ENABLE_FEATURE_MDEV_CONF
425 G.rule_idx = 0; /* restart from the beginning (think mdev -s) */
426#endif
427 for (;;) {
428 const char *str_to_match;
429 regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP];
430 char *command;
431 char *alias;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000432 char aliaslink = aliaslink; /* for compiler */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200433 const char *node_name;
434 const struct rule *rule;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000435
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200436 str_to_match = "";
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000437
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200438 rule = next_rule();
Denis Vlasenko3798db52009-04-19 21:37:07 +0000439
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200440#if ENABLE_FEATURE_MDEV_CONF
441 if (rule->maj >= 0) { /* @maj,min rule */
442 if (major != rule->maj)
443 continue;
444 if (minor < rule->min0 || minor > rule->min1)
445 continue;
446 memset(off, 0, sizeof(off));
447 goto rule_matches;
448 }
449 if (rule->envvar) { /* $envvar=regex rule */
450 str_to_match = getenv(rule->envvar);
451 dbg("getenv('%s'):'%s'", rule->envvar, str_to_match);
452 if (!str_to_match)
453 continue;
454 } else {
455 /* regex to match [subsystem/]device_name */
456 str_to_match = (rule->regex_has_slash ? path : device_name);
457 }
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000458
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200459 if (rule->regex_compiled) {
460 int regex_match = regexec(&rule->match, str_to_match, ARRAY_SIZE(off), off, 0);
461 dbg("regex_match for '%s':%d", str_to_match, regex_match);
462 //bb_error_msg("matches:");
463 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
464 // if (off[i].rm_so < 0) continue;
465 // bb_error_msg("match %d: '%.*s'\n", i,
466 // (int)(off[i].rm_eo - off[i].rm_so),
467 // device_name + off[i].rm_so);
468 //}
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000469
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200470 if (regex_match != 0
471 /* regexec returns whole pattern as "range" 0 */
472 || off[0].rm_so != 0
473 || (int)off[0].rm_eo != (int)strlen(str_to_match)
474 ) {
475 continue; /* this rule doesn't match */
Denis Vlasenko3798db52009-04-19 21:37:07 +0000476 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200477 }
478 /* else: it's final implicit "match-all" rule */
479#endif
480
481 rule_matches:
482 dbg("rule matched");
483
484 /* Build alias name */
485 alias = NULL;
486 if (ENABLE_FEATURE_MDEV_RENAME && rule->ren_mov) {
487 aliaslink = rule->ren_mov[0];
488 if (aliaslink == '!') {
489 /* "!": suppress node creation/deletion */
490 major = -2;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000491 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200492 else if (aliaslink == '>' || aliaslink == '=') {
493 if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) {
494 char *s;
495 char *p;
496 unsigned n;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000497
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200498 /* substitute %1..9 with off[1..9], if any */
499 n = 0;
500 s = rule->ren_mov;
501 while (*s)
502 if (*s++ == '%')
503 n++;
Mike Frysingerc881c732007-11-19 09:04:22 +0000504
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200505 p = alias = xzalloc(strlen(rule->ren_mov) + n * strlen(str_to_match));
506 s = rule->ren_mov + 1;
507 while (*s) {
508 *p = *s;
509 if ('%' == *s) {
510 unsigned i = (s[1] - '0');
511 if (i <= 9 && off[i].rm_so >= 0) {
512 n = off[i].rm_eo - off[i].rm_so;
513 strncpy(p, str_to_match + off[i].rm_so, n);
514 p += n - 1;
515 s++;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000516 }
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000517 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200518 p++;
519 s++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000520 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200521 } else {
522 alias = xstrdup(rule->ren_mov + 1);
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200523 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000524 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000525 }
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200526 dbg("alias:'%s'", alias);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000527
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200528 command = NULL;
529 IF_FEATURE_MDEV_EXEC(command = rule->r_cmd;)
530 if (command) {
531 const char *s = "$@*";
532 const char *s2 = strchr(s, command[0]);
533
534 /* Are we running this command now?
535 * Run $cmd on delete, @cmd on create, *cmd on both
536 */
537 if (s2 - s != delete) {
538 /* We are here if: '*',
539 * or: '@' and delete = 0,
540 * or: '$' and delete = 1
541 */
542 command++;
543 } else {
544 command = NULL;
545 }
546 }
547 dbg("command:'%s'", command);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000548
549 /* "Execute" the line we found */
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200550 node_name = device_name;
551 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
552 node_name = alias = build_alias(alias, device_name);
553 dbg("alias2:'%s'", alias);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000554 }
555
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200556 if (!delete && major >= 0) {
557 dbg("mknod('%s',%o,(%d,%d))", node_name, rule->mode | type, major, minor);
558 if (mknod(node_name, rule->mode | type, makedev(major, minor)) && errno != EEXIST)
559 bb_perror_msg("can't create '%s'", node_name);
560 if (major == G.root_major && minor == G.root_minor)
561 symlink(node_name, "root");
562 if (ENABLE_FEATURE_MDEV_CONF) {
563 chmod(node_name, rule->mode);
564 chown(node_name, rule->ugid.uid, rule->ugid.gid);
565 }
566 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
567 if (aliaslink == '>')
568 symlink(node_name, device_name);
569 }
570 }
571
572 if (ENABLE_FEATURE_MDEV_EXEC && command) {
573 /* setenv will leak memory, use putenv/unsetenv/free */
574 char *s = xasprintf("%s=%s", "MDEV", node_name);
575 char *s1 = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem);
576 putenv(s);
577 putenv(s1);
578 if (system(command) == -1)
579 bb_perror_msg("can't run '%s'", command);
580 bb_unsetenv_and_free(s1);
581 bb_unsetenv_and_free(s);
582 }
583
584 if (delete && major >= -1) {
585 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
586 if (aliaslink == '>')
587 unlink(device_name);
588 }
589 unlink(node_name);
590 }
591
592 if (ENABLE_FEATURE_MDEV_RENAME)
593 free(alias);
594
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000595 /* We found matching line.
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200596 * Stop unless it was prefixed with '-'
597 */
598 if (!ENABLE_FEATURE_MDEV_CONF || !rule->keep_matching)
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000599 break;
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200600 } /* for (;;) */
Mike Frysingerc881c732007-11-19 09:04:22 +0000601
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100602 free(subsystem_slash_devname);
Rob Landley70f7ef72005-12-13 08:21:33 +0000603}
604
Mike Frysingerb51fd352007-06-13 09:24:50 +0000605/* File callback for /sys/ traversal */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000606static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000607 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000608 void *userData,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000609 int depth UNUSED_PARAM)
Rob Landley70f7ef72005-12-13 08:21:33 +0000610{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000611 size_t len = strlen(fileName) - 4; /* can't underflow */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000612 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000613
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000614 /* len check is for paranoid reasons */
Denis Vlasenkod9860482008-07-12 10:23:16 +0000615 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000616 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000617
Mike Frysingerb51fd352007-06-13 09:24:50 +0000618 strcpy(scratch, fileName);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000619 scratch[len] = '\0';
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100620 make_device(scratch, /*delete:*/ 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000621
Mike Frysingerb51fd352007-06-13 09:24:50 +0000622 return TRUE;
623}
Rob Landley70f7ef72005-12-13 08:21:33 +0000624
Mike Frysingerb51fd352007-06-13 09:24:50 +0000625/* Directory callback for /sys/ traversal */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000626static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
627 struct stat *statbuf UNUSED_PARAM,
628 void *userData UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000629 int depth)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000630{
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000631 /* Extract device subsystem -- the name of the directory
632 * under /sys/class/ */
Denis Vlasenko065c7142009-04-13 22:23:02 +0000633 if (1 == depth) {
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100634 free(G.subsystem);
635 G.subsystem = strrchr(fileName, '/');
636 if (G.subsystem)
637 G.subsystem = xstrdup(G.subsystem + 1);
Denis Vlasenko065c7142009-04-13 22:23:02 +0000638 }
639
Mike Frysingerb51fd352007-06-13 09:24:50 +0000640 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000641}
642
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000643/* For the full gory details, see linux/Documentation/firmware_class/README
644 *
645 * Firmware loading works like this:
646 * - kernel sets FIRMWARE env var
647 * - userspace checks /lib/firmware/$FIRMWARE
648 * - userspace waits for /sys/$DEVPATH/loading to appear
649 * - userspace writes "1" to /sys/$DEVPATH/loading
650 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
651 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
652 * - kernel loads firmware into device
653 */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000654static void load_firmware(const char *firmware, const char *sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000655{
656 int cnt;
657 int firmware_fd, loading_fd, data_fd;
658
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000659 /* check for /lib/firmware/$FIRMWARE */
660 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000661 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000662
663 /* in case we goto out ... */
664 data_fd = -1;
665
666 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
667 xchdir(sysfs_path);
668 for (cnt = 0; cnt < 30; ++cnt) {
669 loading_fd = open("loading", O_WRONLY);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000670 if (loading_fd != -1)
671 goto loading;
672 sleep(1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000673 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000674 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000675
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000676 loading:
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000677 /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000678 if (full_write(loading_fd, "1", 1) != 1)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000679 goto out;
680
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000681 /* load firmware into /sys/$DEVPATH/data */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000682 data_fd = open("data", O_WRONLY);
683 if (data_fd == -1)
684 goto out;
685 cnt = bb_copyfd_eof(firmware_fd, data_fd);
686
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000687 /* tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading" */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000688 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000689 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000690 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000691 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000692
693 out:
694 if (ENABLE_FEATURE_CLEAN_UP) {
695 close(firmware_fd);
696 close(loading_fd);
697 close(data_fd);
698 }
699}
700
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000701int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkod9860482008-07-12 10:23:16 +0000702int mdev_main(int argc UNUSED_PARAM, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000703{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000704 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +0000705
Denys Vlasenko16714242011-09-21 01:59:15 +0200706 INIT_G();
707
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200708#if ENABLE_FEATURE_MDEV_CONF
709 G.filename = "/etc/mdev.conf";
710#endif
711
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000712 /* We can be called as hotplug helper */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000713 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000714 bb_sanitize_stdio();
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000715
Denis Vlasenko3798db52009-04-19 21:37:07 +0000716 /* Force the configuration file settings exactly */
717 umask(0);
718
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000719 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000720
Denis Vlasenkoaafbae62009-04-13 13:33:02 +0000721 if (argv[1] && strcmp(argv[1], "-s") == 0) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000722 /* Scan:
723 * mdev -s
724 */
Rob Landleya7e3d052006-02-21 06:11:13 +0000725 struct stat st;
726
Denys Vlasenko3eccd7e2011-09-22 03:46:30 +0200727#if ENABLE_FEATURE_MDEV_CONF
728 /* Same as xrealloc_vector(NULL, 4, 0): */
729 G.rule_vec = xzalloc((1 << 4) * sizeof(*G.rule_vec));
730#endif
Denis Vlasenkof46be092006-10-16 19:39:37 +0000731 xstat("/", &st);
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100732 G.root_major = major(st.st_dev);
733 G.root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000734
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000735 /* ACTION_FOLLOWLINKS is needed since in newer kernels
736 * /sys/block/loop* (for example) are symlinks to dirs,
737 * not real directories.
738 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +0000739 * but we can't enforce that on users)
740 */
741 if (access("/sys/class/block", F_OK) != 0) {
742 /* Scan obsolete /sys/block only if /sys/class/block
743 * doesn't exist. Otherwise we'll have dupes.
Denis Vlasenkof1df35c2009-04-12 14:00:12 +0000744 * Also, do not complain if it doesn't exist.
745 * Some people configure kernel to have no blockdevs.
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +0000746 */
747 recursive_action("/sys/block",
Denis Vlasenkof1df35c2009-04-12 14:00:12 +0000748 ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET,
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +0000749 fileAction, dirAction, temp, 0);
750 }
Mike Frysingerb51fd352007-06-13 09:24:50 +0000751 recursive_action("/sys/class",
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000752 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +0000753 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000754 } else {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000755 char *fw;
Denis Vlasenko018bee62008-07-15 22:33:13 +0000756 char *seq;
757 char *action;
758 char *env_path;
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +0100759 static const char keywords[] ALIGN1 = "remove\0add\0";
760 enum { OP_remove = 0, OP_add };
761 smalluint op;
Denis Vlasenko018bee62008-07-15 22:33:13 +0000762
Mike Frysingerc881c732007-11-19 09:04:22 +0000763 /* Hotplug:
Denis Vlasenko065c7142009-04-13 22:23:02 +0000764 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
Mike Frysingerc881c732007-11-19 09:04:22 +0000765 * ACTION can be "add" or "remove"
766 * DEVPATH is like "/block/sda" or "/class/input/mice"
767 */
Rob Landley29e08ff2006-01-12 06:13:50 +0000768 action = getenv("ACTION");
769 env_path = getenv("DEVPATH");
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100770 G.subsystem = getenv("SUBSYSTEM");
771 if (!action || !env_path /*|| !G.subsystem*/)
Mike Frysingera421ba82006-02-03 00:25:37 +0000772 bb_show_usage();
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000773 fw = getenv("FIRMWARE");
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +0100774 op = index_in_strings(keywords, action);
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000775 /* If it exists, does /dev/mdev.seq match $SEQNUM?
776 * If it does not match, earlier mdev is running
777 * in parallel, and we need to wait */
Denis Vlasenko018bee62008-07-15 22:33:13 +0000778 seq = getenv("SEQNUM");
779 if (seq) {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000780 int timeout = 2000 / 32; /* 2000 msec */
Denis Vlasenko018bee62008-07-15 22:33:13 +0000781 do {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000782 int seqlen;
783 char seqbuf[sizeof(int)*3 + 2];
784
Denis Vlasenko018bee62008-07-15 22:33:13 +0000785 seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1));
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000786 if (seqlen < 0) {
787 seq = NULL;
Denis Vlasenko018bee62008-07-15 22:33:13 +0000788 break;
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000789 }
Denis Vlasenko018bee62008-07-15 22:33:13 +0000790 seqbuf[seqlen] = '\0';
791 if (seqbuf[0] == '\n' /* seed file? */
792 || strcmp(seq, seqbuf) == 0 /* correct idx? */
793 ) {
794 break;
795 }
796 usleep(32*1000);
797 } while (--timeout);
798 }
799
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000800 snprintf(temp, PATH_MAX, "/sys%s", env_path);
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +0100801 if (op == OP_remove) {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000802 /* Ignoring "remove firmware". It was reported
803 * to happen and to cause erroneous deletion
804 * of device nodes. */
805 if (!fw)
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100806 make_device(temp, /*delete:*/ 1);
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000807 }
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +0100808 else if (op == OP_add) {
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100809 make_device(temp, /*delete:*/ 0);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000810 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000811 if (fw)
812 load_firmware(fw, temp);
813 }
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000814 }
Denis Vlasenko018bee62008-07-15 22:33:13 +0000815
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000816 if (seq) {
Denis Vlasenko018bee62008-07-15 22:33:13 +0000817 xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1));
818 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000819 }
820
Mike Frysingerc881c732007-11-19 09:04:22 +0000821 if (ENABLE_FEATURE_CLEAN_UP)
822 RELEASE_CONFIG_BUFFER(temp);
823
Denis Vlasenkoaafbae62009-04-13 13:33:02 +0000824 return EXIT_SUCCESS;
Rob Landley70f7ef72005-12-13 08:21:33 +0000825}