blob: 21707566031a2c91ea80308d7a6e64cd64f30ea5 [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 *
Rob Landleye9a7a622006-09-22 02:52:41 +00008 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
Rob Landley70f7ef72005-12-13 08:21:33 +00009 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Rob Landleyb56c2852005-12-17 10:52:30 +000011#include "xregex.h"
Rob Landley70f7ef72005-12-13 08:21:33 +000012
Denis Vlasenko5ff96292009-04-17 00:01:04 +000013/* "mdev -s" scans /sys/class/xxx, looking for directories which have dev
14 * file (it is of the form "M:m\n"). Example: /sys/class/tty/tty0/dev
15 * contains "4:0\n". Directory name is taken as device name, path component
16 * directly after /sys/class/ as subsystem. In this example, "tty0" and "tty".
17 * Then mdev creates the /dev/device_name node.
Denis Vlasenko11c17f72009-04-19 23:38:08 +000018 * If /sys/class/.../dev file does not exist, mdev still may act
19 * on this device: see "@|$|*command args..." parameter in config file.
Denis Vlasenko5ff96292009-04-17 00:01:04 +000020 *
21 * mdev w/o parameters is called as hotplug helper. It takes device
22 * and subsystem names from $DEVPATH and $SUBSYSTEM, extracts
23 * maj,min from "/sys/$DEVPATH/dev" and also examines
24 * $ACTION ("add"/"delete") and $FIRMWARE.
25 *
26 * If action is "add", mdev creates /dev/device_name similarly to mdev -s.
27 * (todo: explain "delete" and $FIRMWARE)
28 *
29 * If /etc/mdev.conf exists, it may modify /dev/device_name's properties.
30 * /etc/mdev.conf file format:
31 *
32 * [-][subsystem/]device user:grp mode [>|=path] [@|$|*command args...]
33 * [-]@maj,min[-min2] user:grp mode [>|=path] [@|$|*command args...]
Denis Vlasenko11c17f72009-04-19 23:38:08 +000034 * [-]$envvar=val user:grp mode [>|=path] [@|$|*command args...]
Denis Vlasenko5ff96292009-04-17 00:01:04 +000035 *
36 * Leading minus in 1st field means "don't stop on this line", otherwise
37 * search is stopped after the matching line is encountered.
38 *
Denis Vlasenko11c17f72009-04-19 23:38:08 +000039 * The device name or "subsystem/device" combo is matched against 1st field
40 * (which is a regex), or maj,min is matched against 1st field,
41 * or specified environment variable (as regex) is matched against 1st field.
42 *
43 * $envvar=val format is useful for loading modules for hot-plugged devices
44 * which do not have driver loaded yet. In this case /sys/class/.../dev
45 * does not exist, but $MODALIAS is set to needed module's name
46 * (actually, an alias to it) by kernel. This rule instructs mdev
47 * to load the module and exit:
48 * $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"
49 * The kernel will generate another hotplug event when /sys/class/.../dev
50 * file appears.
51 *
52 * When line matches, the device node is created, chmod'ed and chown'ed,
53 * moved to path, and if >path, a symlink to moved node is created,
54 * all this if /sys/class/.../dev exists.
Denis Vlasenko5ff96292009-04-17 00:01:04 +000055 * Examples:
56 * =loop/ - moves to /dev/loop
57 * >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink
Denis Vlasenko11c17f72009-04-19 23:38:08 +000058 *
59 * Then "command args..." is executed (via sh -c 'command args...').
Denis Vlasenko5ff96292009-04-17 00:01:04 +000060 * @:execute on creation, $:on deletion, *:on both.
Denis Vlasenko11c17f72009-04-19 23:38:08 +000061 * This happens regardless of /sys/class/.../dev existence.
Denis Vlasenko5ff96292009-04-17 00:01:04 +000062 */
63
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +000064struct globals {
Rob Landley5cd1ccd2006-05-21 18:33:27 +000065 int root_major, root_minor;
Denis Vlasenko065c7142009-04-13 22:23:02 +000066 char *subsystem;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010067} FIX_ALIASING;
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +000068#define G (*(struct globals*)&bb_common_bufsiz1)
Rob Landleya7e3d052006-02-21 06:11:13 +000069
Denis Vlasenkod48e81f2008-07-06 07:00:11 +000070/* Prevent infinite loops in /sys symlinks */
71#define MAX_SYSFS_DEPTH 3
Mike Frysingerb51fd352007-06-13 09:24:50 +000072
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000073/* We use additional 64+ bytes in make_device() */
74#define SCRATCH_SIZE 80
75
Denis Vlasenkod9860482008-07-12 10:23:16 +000076/* Builds an alias path.
77 * This function potentionally reallocates the alias parameter.
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +000078 * Only used for ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkod9860482008-07-12 10:23:16 +000079 */
80static char *build_alias(char *alias, const char *device_name)
81{
82 char *dest;
83
84 /* ">bar/": rename to bar/device_name */
85 /* ">bar[/]baz": rename to bar[/]baz */
86 dest = strrchr(alias, '/');
87 if (dest) { /* ">bar/[baz]" ? */
88 *dest = '\0'; /* mkdir bar */
89 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
90 *dest = '/';
91 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
92 dest = alias;
93 alias = concat_path_file(alias, device_name);
94 free(dest);
95 }
96 }
97
98 return alias;
99}
100
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200101/* mknod in /dev based on a path like "/sys/block/hda/hda1"
102 * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes
103 * after NUL, but we promise to not mangle (IOW: to restore if needed)
104 * path string.
105 * NB2: "mdev -s" may call us many times, do not leak memory/fds!
106 */
Rob Landleyef10d522006-06-26 14:11:33 +0000107static void make_device(char *path, int delete)
Rob Landley70f7ef72005-12-13 08:21:33 +0000108{
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100109 char *device_name, *subsystem_slash_devname;
Denis Vlasenkof46be092006-10-16 19:39:37 +0000110 int major, minor, type, len;
Bernhard Reutner-Fischer933993a2009-10-26 23:27:05 +0100111 mode_t mode;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000112 parser_t *parser;
Mike Frysingerf0044c42008-02-01 06:53:50 +0000113
Rob Landley15fe2e12006-05-08 02:53:23 +0000114 /* Try to read major/minor string. Note that the kernel puts \n after
115 * the data, so we don't need to worry about null terminating the string
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000116 * because sscanf() will stop at the first nondigit, which \n is.
117 * We also depend on path having writeable space after it.
Mike Frysingerc881c732007-11-19 09:04:22 +0000118 */
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000119 major = -1;
Rob Landley10b36f92006-08-10 01:09:37 +0000120 if (!delete) {
Denis Vlasenko3798db52009-04-19 21:37:07 +0000121 char *dev_maj_min = path + strlen(path);
122
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000123 strcpy(dev_maj_min, "/dev");
124 len = open_read_close(path, dev_maj_min + 1, 64);
Denis Vlasenko3798db52009-04-19 21:37:07 +0000125 *dev_maj_min = '\0';
Mike Frysingerae86a332008-02-20 18:31:36 +0000126 if (len < 1) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000127 if (!ENABLE_FEATURE_MDEV_EXEC)
Mike Frysingerae86a332008-02-20 18:31:36 +0000128 return;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000129 /* no "dev" file, but we can still run scripts
130 * based on device name */
131 } else if (sscanf(++dev_maj_min, "%u:%u", &major, &minor) != 2) {
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000132 major = -1;
Mike Frysingerae86a332008-02-20 18:31:36 +0000133 }
Rob Landley10b36f92006-08-10 01:09:37 +0000134 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000135
Rob Landley70f7ef72005-12-13 08:21:33 +0000136 /* Determine device name, type, major and minor */
Denis Vlasenko3798db52009-04-19 21:37:07 +0000137 device_name = (char*) bb_basename(path);
Denis Vlasenkoaa8a6012008-03-29 16:07:46 +0000138 /* http://kernel.org/doc/pending/hotplug.txt says that only
Denis Vlasenkoa87045c2008-07-12 10:28:41 +0000139 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100140 * But since 2.6.25 block devices are also in /sys/class/block.
141 * We use strstr("/block/") to forestall future surprises. */
Denis Vlasenko9504e442008-10-29 01:19:15 +0000142 type = S_IFCHR;
Denys Vlasenko8d4a8d12010-03-16 18:37:19 +0100143 if (strstr(path, "/block/") || (G.subsystem && strncmp(G.subsystem, "block", 5) == 0))
Denis Vlasenko9504e442008-10-29 01:19:15 +0000144 type = S_IFBLK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000145
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000146 /* Make path point to "subsystem/device_name" */
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100147 subsystem_slash_devname = NULL;
148 /* Check for coldplug invocations first */
149 if (strncmp(path, "/sys/block/", 11) == 0) /* legacy case */
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000150 path += sizeof("/sys/") - 1;
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100151 else if (strncmp(path, "/sys/class/", 11) == 0)
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000152 path += sizeof("/sys/class/") - 1;
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100153 else {
154 /* Example of a hotplug invocation:
155 * SUBSYSTEM="block"
156 * DEVPATH="/sys" + "/devices/virtual/mtd/mtd3/mtdblock3"
157 * ("/sys" is added by mdev_main)
158 * - path does not contain subsystem
159 */
160 subsystem_slash_devname = concat_path_file(G.subsystem, device_name);
161 path = subsystem_slash_devname;
162 }
Denis Vlasenko6dda5de2009-04-16 21:42:12 +0000163
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000164 /* If we have config file, look up user settings */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000165 if (ENABLE_FEATURE_MDEV_CONF)
166 parser = config_open2("/etc/mdev.conf", fopen_for_read);
167
168 do {
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000169 int keep_matching;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000170 struct bb_uidgid_t ugid;
171 char *tokens[4];
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000172 char *command = NULL;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000173 char *alias = NULL;
174 char aliaslink = aliaslink; /* for compiler */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000175
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000176 /* Defaults in case we won't match any line */
177 ugid.uid = ugid.gid = 0;
178 keep_matching = 0;
179 mode = 0660;
180
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000181 if (ENABLE_FEATURE_MDEV_CONF
182 && config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL)
183 ) {
Denis Vlasenko3798db52009-04-19 21:37:07 +0000184 char *val;
185 char *str_to_match;
186 regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP];
187
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000188 val = tokens[0];
189 keep_matching = ('-' == val[0]);
190 val += keep_matching; /* swallow leading dash */
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000191
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000192 /* Match against either "subsystem/device_name"
193 * or "device_name" alone */
Denis Vlasenko3798db52009-04-19 21:37:07 +0000194 str_to_match = strchr(val, '/') ? path : device_name;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000195
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000196 /* Fields: regex uid:gid mode [alias] [cmd] */
Denis Vlasenko6dda5de2009-04-16 21:42:12 +0000197
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000198 if (val[0] == '@') {
Denis Vlasenko3798db52009-04-19 21:37:07 +0000199 /* @major,minor[-minor2] */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000200 /* (useful when name is ambiguous:
201 * "/sys/class/usb/lp0" and
202 * "/sys/class/printer/lp0") */
203 int cmaj, cmin0, cmin1, sc;
204 if (major < 0)
205 continue; /* no dev, no match */
206 sc = sscanf(val, "@%u,%u-%u", &cmaj, &cmin0, &cmin1);
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200207 if (sc < 1
208 || major != cmaj
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000209 || (sc == 2 && minor != cmin0)
210 || (sc == 3 && (minor < cmin0 || minor > cmin1))
211 ) {
212 continue; /* this line doesn't match */
213 }
Denis Vlasenko3798db52009-04-19 21:37:07 +0000214 goto line_matches;
215 }
216 if (val[0] == '$') {
217 /* regex to match an environment variable */
218 char *eq = strchr(++val, '=');
219 if (!eq)
220 continue;
221 *eq = '\0';
222 str_to_match = getenv(val);
223 if (!str_to_match)
224 continue;
225 str_to_match -= strlen(val) + 1;
226 *eq = '=';
227 }
228 /* else: regex to match [subsystem/]device_name */
229
230 {
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000231 regex_t match;
232 int result;
Mike Frysingerc881c732007-11-19 09:04:22 +0000233
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000234 xregcomp(&match, val, REG_EXTENDED);
Denis Vlasenko3798db52009-04-19 21:37:07 +0000235 result = regexec(&match, str_to_match, ARRAY_SIZE(off), off, 0);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000236 regfree(&match);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000237 //bb_error_msg("matches:");
238 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
239 // if (off[i].rm_so < 0) continue;
240 // bb_error_msg("match %d: '%.*s'\n", i,
241 // (int)(off[i].rm_eo - off[i].rm_so),
242 // device_name + off[i].rm_so);
243 //}
244
Denis Vlasenko3798db52009-04-19 21:37:07 +0000245 /* If no match, skip rest of line */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000246 /* (regexec returns whole pattern as "range" 0) */
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200247 if (result
248 || off[0].rm_so
Denis Vlasenko3798db52009-04-19 21:37:07 +0000249 || ((int)off[0].rm_eo != (int)strlen(str_to_match))
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000250 ) {
251 continue; /* this line doesn't match */
252 }
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000253 }
Denis Vlasenko3798db52009-04-19 21:37:07 +0000254 line_matches:
255 /* This line matches. Stop parsing after parsing
256 * the rest the line unless keep_matching == 1 */
Rob Landleyb56c2852005-12-17 10:52:30 +0000257
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000258 /* 2nd field: uid:gid - device ownership */
Denys Vlasenkoaa0a12d2009-05-02 16:33:10 +0200259 if (get_uidgid(&ugid, tokens[1], 1) == 0)
Denys Vlasenko1612c752009-11-07 03:33:05 +0100260 bb_error_msg("unknown user/group %s on line %d", tokens[1], parser->lineno);
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000261
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000262 /* 3rd field: mode - device permissions */
Bernhard Reutner-Fischer933993a2009-10-26 23:27:05 +0100263 bb_parse_mode(tokens[2], &mode);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000264
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000265 val = tokens[3];
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200266 /* 4th field (opt): ">|=alias" or "!" to not create the node */
Rob Landleyb56c2852005-12-17 10:52:30 +0000267
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000268 if (ENABLE_FEATURE_MDEV_RENAME && val) {
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200269 char *a, *s, *st;
Denis Vlasenko44615642008-03-29 13:10:57 +0000270
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200271 a = val;
272 s = strchrnul(val, ' ');
273 st = strchrnul(val, '\t');
274 if (st < s)
275 s = st;
276 st = (s[0] && s[1]) ? s+1 : NULL;
277
278 aliaslink = a[0];
279 if (aliaslink == '!' && s == a+1) {
280 val = st;
281 /* "!": suppress node creation/deletion */
282 major = -1;
283 }
284 else if (aliaslink == '>' || aliaslink == '=') {
285 val = st;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000286 s[0] = '\0';
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000287 if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) {
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200288 char *p;
289 unsigned i, n;
290
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000291 /* substitute %1..9 with off[1..9], if any */
292 n = 0;
293 s = a;
294 while (*s)
295 if (*s++ == '%')
296 n++;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000297
Denis Vlasenko3798db52009-04-19 21:37:07 +0000298 p = alias = xzalloc(strlen(a) + n * strlen(str_to_match));
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000299 s = a + 1;
300 while (*s) {
301 *p = *s;
302 if ('%' == *s) {
303 i = (s[1] - '0');
304 if (i <= 9 && off[i].rm_so >= 0) {
305 n = off[i].rm_eo - off[i].rm_so;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000306 strncpy(p, str_to_match + off[i].rm_so, n);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000307 p += n - 1;
308 s++;
309 }
310 }
311 p++;
312 s++;
313 }
314 } else {
315 alias = xstrdup(a + 1);
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000316 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000317 }
318 }
319
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000320 if (ENABLE_FEATURE_MDEV_EXEC && val) {
321 const char *s = "$@*";
322 const char *s2 = strchr(s, val[0]);
Denis Vlasenko44615642008-03-29 13:10:57 +0000323
Denys Vlasenko5478aaf2009-07-02 13:17:10 +0200324 if (!s2) {
325 bb_error_msg("bad line %u", parser->lineno);
326 if (ENABLE_FEATURE_MDEV_RENAME)
327 free(alias);
328 continue;
329 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000330
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000331 /* Are we running this command now?
332 * Run $cmd on delete, @cmd on create, *cmd on both
333 */
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200334 if (s2 - s != delete) {
335 /* We are here if: '*',
336 * or: '@' and delete = 0,
337 * or: '$' and delete = 1
338 */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000339 command = xstrdup(val + 1);
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200340 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000341 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000342 }
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000343
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000344 /* End of field parsing */
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000345
346 /* "Execute" the line we found */
Denys Vlasenko90db3592009-07-02 13:07:11 +0200347 {
348 const char *node_name;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000349
Denys Vlasenko90db3592009-07-02 13:07:11 +0200350 node_name = device_name;
Denys Vlasenko2f86ca12009-06-22 01:31:12 +0200351 if (ENABLE_FEATURE_MDEV_RENAME && alias)
Denys Vlasenko90db3592009-07-02 13:07:11 +0200352 node_name = alias = build_alias(alias, device_name);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000353
Denys Vlasenko90db3592009-07-02 13:07:11 +0200354 if (!delete && major >= 0) {
355 if (mknod(node_name, mode | type, makedev(major, minor)) && errno != EEXIST)
Denys Vlasenko651a2692010-03-23 16:25:17 +0100356 bb_perror_msg("can't create '%s'", node_name);
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100357 if (major == G.root_major && minor == G.root_minor)
Denys Vlasenko90db3592009-07-02 13:07:11 +0200358 symlink(node_name, "root");
359 if (ENABLE_FEATURE_MDEV_CONF) {
360 chmod(node_name, mode);
361 chown(node_name, ugid.uid, ugid.gid);
362 }
363 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
364 if (aliaslink == '>')
365 symlink(node_name, device_name);
366 }
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000367 }
Denys Vlasenko90db3592009-07-02 13:07:11 +0200368
369 if (ENABLE_FEATURE_MDEV_EXEC && command) {
370 /* setenv will leak memory, use putenv/unsetenv/free */
371 char *s = xasprintf("%s=%s", "MDEV", node_name);
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100372 char *s1 = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem);
Denys Vlasenko90db3592009-07-02 13:07:11 +0200373 putenv(s);
374 putenv(s1);
375 if (system(command) == -1)
Denys Vlasenko5478aaf2009-07-02 13:17:10 +0200376 bb_perror_msg("can't run '%s'", command);
Denys Vlasenko90db3592009-07-02 13:07:11 +0200377 unsetenv("SUBSYSTEM");
378 free(s1);
379 unsetenv("MDEV");
380 free(s);
381 free(command);
382 }
383
Denys Vlasenkob38af7b2010-04-02 06:47:14 +0200384 if (delete && major >= 0) {
Denys Vlasenko90db3592009-07-02 13:07:11 +0200385 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
386 if (aliaslink == '>')
387 unlink(device_name);
388 }
389 unlink(node_name);
390 }
391
Denys Vlasenko2f86ca12009-06-22 01:31:12 +0200392 if (ENABLE_FEATURE_MDEV_RENAME)
393 free(alias);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000394 }
395
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000396 /* We found matching line.
397 * Stop unless it was prefixed with '-' */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000398 if (ENABLE_FEATURE_MDEV_CONF && !keep_matching)
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000399 break;
Mike Frysingerc881c732007-11-19 09:04:22 +0000400
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000401 /* end of "while line is read from /etc/mdev.conf" */
402 } while (ENABLE_FEATURE_MDEV_CONF);
403
404 if (ENABLE_FEATURE_MDEV_CONF)
405 config_close(parser);
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100406 free(subsystem_slash_devname);
Rob Landley70f7ef72005-12-13 08:21:33 +0000407}
408
Mike Frysingerb51fd352007-06-13 09:24:50 +0000409/* File callback for /sys/ traversal */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000410static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000411 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000412 void *userData,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000413 int depth UNUSED_PARAM)
Rob Landley70f7ef72005-12-13 08:21:33 +0000414{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000415 size_t len = strlen(fileName) - 4; /* can't underflow */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000416 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000417
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000418 /* len check is for paranoid reasons */
Denis Vlasenkod9860482008-07-12 10:23:16 +0000419 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000420 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000421
Mike Frysingerb51fd352007-06-13 09:24:50 +0000422 strcpy(scratch, fileName);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000423 scratch[len] = '\0';
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100424 make_device(scratch, /*delete:*/ 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000425
Mike Frysingerb51fd352007-06-13 09:24:50 +0000426 return TRUE;
427}
Rob Landley70f7ef72005-12-13 08:21:33 +0000428
Mike Frysingerb51fd352007-06-13 09:24:50 +0000429/* Directory callback for /sys/ traversal */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000430static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
431 struct stat *statbuf UNUSED_PARAM,
432 void *userData UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000433 int depth)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000434{
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000435 /* Extract device subsystem -- the name of the directory
436 * under /sys/class/ */
Denis Vlasenko065c7142009-04-13 22:23:02 +0000437 if (1 == depth) {
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100438 free(G.subsystem);
439 G.subsystem = strrchr(fileName, '/');
440 if (G.subsystem)
441 G.subsystem = xstrdup(G.subsystem + 1);
Denis Vlasenko065c7142009-04-13 22:23:02 +0000442 }
443
Mike Frysingerb51fd352007-06-13 09:24:50 +0000444 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000445}
446
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000447/* For the full gory details, see linux/Documentation/firmware_class/README
448 *
449 * Firmware loading works like this:
450 * - kernel sets FIRMWARE env var
451 * - userspace checks /lib/firmware/$FIRMWARE
452 * - userspace waits for /sys/$DEVPATH/loading to appear
453 * - userspace writes "1" to /sys/$DEVPATH/loading
454 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
455 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
456 * - kernel loads firmware into device
457 */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000458static void load_firmware(const char *firmware, const char *sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000459{
460 int cnt;
461 int firmware_fd, loading_fd, data_fd;
462
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000463 /* check for /lib/firmware/$FIRMWARE */
464 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000465 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000466
467 /* in case we goto out ... */
468 data_fd = -1;
469
470 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
471 xchdir(sysfs_path);
472 for (cnt = 0; cnt < 30; ++cnt) {
473 loading_fd = open("loading", O_WRONLY);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000474 if (loading_fd != -1)
475 goto loading;
476 sleep(1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000477 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000478 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000479
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000480 loading:
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000481 /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000482 if (full_write(loading_fd, "1", 1) != 1)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000483 goto out;
484
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000485 /* load firmware into /sys/$DEVPATH/data */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000486 data_fd = open("data", O_WRONLY);
487 if (data_fd == -1)
488 goto out;
489 cnt = bb_copyfd_eof(firmware_fd, data_fd);
490
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000491 /* tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading" */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000492 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000493 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000494 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000495 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000496
497 out:
498 if (ENABLE_FEATURE_CLEAN_UP) {
499 close(firmware_fd);
500 close(loading_fd);
501 close(data_fd);
502 }
503}
504
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000505int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkod9860482008-07-12 10:23:16 +0000506int mdev_main(int argc UNUSED_PARAM, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000507{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000508 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +0000509
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000510 /* We can be called as hotplug helper */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000511 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000512 bb_sanitize_stdio();
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000513
Denis Vlasenko3798db52009-04-19 21:37:07 +0000514 /* Force the configuration file settings exactly */
515 umask(0);
516
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000517 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000518
Denis Vlasenkoaafbae62009-04-13 13:33:02 +0000519 if (argv[1] && strcmp(argv[1], "-s") == 0) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000520 /* Scan:
521 * mdev -s
522 */
Rob Landleya7e3d052006-02-21 06:11:13 +0000523 struct stat st;
524
Denis Vlasenkof46be092006-10-16 19:39:37 +0000525 xstat("/", &st);
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100526 G.root_major = major(st.st_dev);
527 G.root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000528
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000529 /* ACTION_FOLLOWLINKS is needed since in newer kernels
530 * /sys/block/loop* (for example) are symlinks to dirs,
531 * not real directories.
532 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +0000533 * but we can't enforce that on users)
534 */
535 if (access("/sys/class/block", F_OK) != 0) {
536 /* Scan obsolete /sys/block only if /sys/class/block
537 * doesn't exist. Otherwise we'll have dupes.
Denis Vlasenkof1df35c2009-04-12 14:00:12 +0000538 * Also, do not complain if it doesn't exist.
539 * Some people configure kernel to have no blockdevs.
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +0000540 */
541 recursive_action("/sys/block",
Denis Vlasenkof1df35c2009-04-12 14:00:12 +0000542 ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET,
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +0000543 fileAction, dirAction, temp, 0);
544 }
Mike Frysingerb51fd352007-06-13 09:24:50 +0000545 recursive_action("/sys/class",
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000546 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +0000547 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000548 } else {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000549 char *fw;
Denis Vlasenko018bee62008-07-15 22:33:13 +0000550 char *seq;
551 char *action;
552 char *env_path;
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +0100553 static const char keywords[] ALIGN1 = "remove\0add\0";
554 enum { OP_remove = 0, OP_add };
555 smalluint op;
Denis Vlasenko018bee62008-07-15 22:33:13 +0000556
Mike Frysingerc881c732007-11-19 09:04:22 +0000557 /* Hotplug:
Denis Vlasenko065c7142009-04-13 22:23:02 +0000558 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
Mike Frysingerc881c732007-11-19 09:04:22 +0000559 * ACTION can be "add" or "remove"
560 * DEVPATH is like "/block/sda" or "/class/input/mice"
561 */
Rob Landley29e08ff2006-01-12 06:13:50 +0000562 action = getenv("ACTION");
563 env_path = getenv("DEVPATH");
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100564 G.subsystem = getenv("SUBSYSTEM");
565 if (!action || !env_path /*|| !G.subsystem*/)
Mike Frysingera421ba82006-02-03 00:25:37 +0000566 bb_show_usage();
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000567 fw = getenv("FIRMWARE");
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +0100568 op = index_in_strings(keywords, action);
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000569 /* If it exists, does /dev/mdev.seq match $SEQNUM?
570 * If it does not match, earlier mdev is running
571 * in parallel, and we need to wait */
Denis Vlasenko018bee62008-07-15 22:33:13 +0000572 seq = getenv("SEQNUM");
573 if (seq) {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000574 int timeout = 2000 / 32; /* 2000 msec */
Denis Vlasenko018bee62008-07-15 22:33:13 +0000575 do {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000576 int seqlen;
577 char seqbuf[sizeof(int)*3 + 2];
578
Denis Vlasenko018bee62008-07-15 22:33:13 +0000579 seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1));
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000580 if (seqlen < 0) {
581 seq = NULL;
Denis Vlasenko018bee62008-07-15 22:33:13 +0000582 break;
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000583 }
Denis Vlasenko018bee62008-07-15 22:33:13 +0000584 seqbuf[seqlen] = '\0';
585 if (seqbuf[0] == '\n' /* seed file? */
586 || strcmp(seq, seqbuf) == 0 /* correct idx? */
587 ) {
588 break;
589 }
590 usleep(32*1000);
591 } while (--timeout);
592 }
593
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000594 snprintf(temp, PATH_MAX, "/sys%s", env_path);
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +0100595 if (op == OP_remove) {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000596 /* Ignoring "remove firmware". It was reported
597 * to happen and to cause erroneous deletion
598 * of device nodes. */
599 if (!fw)
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100600 make_device(temp, /*delete:*/ 1);
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000601 }
Bernhard Reutner-Fischer6531f092009-10-26 23:27:04 +0100602 else if (op == OP_add) {
Denys Vlasenko64bb95a2010-02-24 10:39:47 +0100603 make_device(temp, /*delete:*/ 0);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000604 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000605 if (fw)
606 load_firmware(fw, temp);
607 }
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000608 }
Denis Vlasenko018bee62008-07-15 22:33:13 +0000609
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000610 if (seq) {
Denis Vlasenko018bee62008-07-15 22:33:13 +0000611 xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1));
612 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000613 }
614
Mike Frysingerc881c732007-11-19 09:04:22 +0000615 if (ENABLE_FEATURE_CLEAN_UP)
616 RELEASE_CONFIG_BUFFER(temp);
617
Denis Vlasenkoaafbae62009-04-13 13:33:02 +0000618 return EXIT_SUCCESS;
Rob Landley70f7ef72005-12-13 08:21:33 +0000619}