blob: 2ac15e762f23282b4e26aa483f38aff97a450611 [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;
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +000067};
68#define G (*(struct globals*)&bb_common_bufsiz1)
69#define root_major (G.root_major)
70#define root_minor (G.root_minor)
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +000071#define subsystem (G.subsystem )
Rob Landleya7e3d052006-02-21 06:11:13 +000072
Denis Vlasenkod48e81f2008-07-06 07:00:11 +000073/* Prevent infinite loops in /sys symlinks */
74#define MAX_SYSFS_DEPTH 3
Mike Frysingerb51fd352007-06-13 09:24:50 +000075
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000076/* We use additional 64+ bytes in make_device() */
77#define SCRATCH_SIZE 80
78
Denis Vlasenkod9860482008-07-12 10:23:16 +000079/* Builds an alias path.
80 * This function potentionally reallocates the alias parameter.
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +000081 * Only used for ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkod9860482008-07-12 10:23:16 +000082 */
83static char *build_alias(char *alias, const char *device_name)
84{
85 char *dest;
86
87 /* ">bar/": rename to bar/device_name */
88 /* ">bar[/]baz": rename to bar[/]baz */
89 dest = strrchr(alias, '/');
90 if (dest) { /* ">bar/[baz]" ? */
91 *dest = '\0'; /* mkdir bar */
92 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
93 *dest = '/';
94 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
95 dest = alias;
96 alias = concat_path_file(alias, device_name);
97 free(dest);
98 }
99 }
100
101 return alias;
102}
103
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200104/* mknod in /dev based on a path like "/sys/block/hda/hda1"
105 * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes
106 * after NUL, but we promise to not mangle (IOW: to restore if needed)
107 * path string.
108 * NB2: "mdev -s" may call us many times, do not leak memory/fds!
109 */
Rob Landleyef10d522006-06-26 14:11:33 +0000110static void make_device(char *path, int delete)
Rob Landley70f7ef72005-12-13 08:21:33 +0000111{
Denis Vlasenko3798db52009-04-19 21:37:07 +0000112 char *device_name;
Denis Vlasenkof46be092006-10-16 19:39:37 +0000113 int major, minor, type, len;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000114 int mode;
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000115 parser_t *parser;
Mike Frysingerf0044c42008-02-01 06:53:50 +0000116
Rob Landley15fe2e12006-05-08 02:53:23 +0000117 /* Try to read major/minor string. Note that the kernel puts \n after
118 * the data, so we don't need to worry about null terminating the string
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000119 * because sscanf() will stop at the first nondigit, which \n is.
120 * We also depend on path having writeable space after it.
Mike Frysingerc881c732007-11-19 09:04:22 +0000121 */
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000122 major = -1;
Rob Landley10b36f92006-08-10 01:09:37 +0000123 if (!delete) {
Denis Vlasenko3798db52009-04-19 21:37:07 +0000124 char *dev_maj_min = path + strlen(path);
125
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000126 strcpy(dev_maj_min, "/dev");
127 len = open_read_close(path, dev_maj_min + 1, 64);
Denis Vlasenko3798db52009-04-19 21:37:07 +0000128 *dev_maj_min = '\0';
Mike Frysingerae86a332008-02-20 18:31:36 +0000129 if (len < 1) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000130 if (!ENABLE_FEATURE_MDEV_EXEC)
Mike Frysingerae86a332008-02-20 18:31:36 +0000131 return;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000132 /* no "dev" file, but we can still run scripts
133 * based on device name */
134 } else if (sscanf(++dev_maj_min, "%u:%u", &major, &minor) != 2) {
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000135 major = -1;
Mike Frysingerae86a332008-02-20 18:31:36 +0000136 }
Rob Landley10b36f92006-08-10 01:09:37 +0000137 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000138
Rob Landley70f7ef72005-12-13 08:21:33 +0000139 /* Determine device name, type, major and minor */
Denis Vlasenko3798db52009-04-19 21:37:07 +0000140 device_name = (char*) bb_basename(path);
Denis Vlasenkoaa8a6012008-03-29 16:07:46 +0000141 /* http://kernel.org/doc/pending/hotplug.txt says that only
Denis Vlasenkoa87045c2008-07-12 10:28:41 +0000142 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
Denis Vlasenko6dda5de2009-04-16 21:42:12 +0000143 * But since 2.6.25 block devices are also in /sys/class/block,
144 * we use strstr("/block/") to forestall future surprises. */
Denis Vlasenko9504e442008-10-29 01:19:15 +0000145 type = S_IFCHR;
Denis Vlasenkoa87045c2008-07-12 10:28:41 +0000146 if (strstr(path, "/block/"))
Denis Vlasenko9504e442008-10-29 01:19:15 +0000147 type = S_IFBLK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000148
Denis Vlasenko5ff96292009-04-17 00:01:04 +0000149 /* Make path point to "subsystem/device_name" */
150 if (path[5] == 'b') /* legacy /sys/block? */
151 path += sizeof("/sys/") - 1;
152 else
153 path += sizeof("/sys/class/") - 1;
Denis Vlasenko6dda5de2009-04-16 21:42:12 +0000154
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000155 /* If we have config file, look up user settings */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000156 if (ENABLE_FEATURE_MDEV_CONF)
157 parser = config_open2("/etc/mdev.conf", fopen_for_read);
158
159 do {
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000160 int keep_matching;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000161 struct bb_uidgid_t ugid;
162 char *tokens[4];
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000163 char *command = NULL;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000164 char *alias = NULL;
165 char aliaslink = aliaslink; /* for compiler */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000166
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000167 /* Defaults in case we won't match any line */
168 ugid.uid = ugid.gid = 0;
169 keep_matching = 0;
170 mode = 0660;
171
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000172 if (ENABLE_FEATURE_MDEV_CONF
173 && config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL)
174 ) {
Denis Vlasenko3798db52009-04-19 21:37:07 +0000175 char *val;
176 char *str_to_match;
177 regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP];
178
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000179 val = tokens[0];
180 keep_matching = ('-' == val[0]);
181 val += keep_matching; /* swallow leading dash */
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000182
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000183 /* Match against either "subsystem/device_name"
184 * or "device_name" alone */
Denis Vlasenko3798db52009-04-19 21:37:07 +0000185 str_to_match = strchr(val, '/') ? path : device_name;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000186
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000187 /* Fields: regex uid:gid mode [alias] [cmd] */
Denis Vlasenko6dda5de2009-04-16 21:42:12 +0000188
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000189 if (val[0] == '@') {
Denis Vlasenko3798db52009-04-19 21:37:07 +0000190 /* @major,minor[-minor2] */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000191 /* (useful when name is ambiguous:
192 * "/sys/class/usb/lp0" and
193 * "/sys/class/printer/lp0") */
194 int cmaj, cmin0, cmin1, sc;
195 if (major < 0)
196 continue; /* no dev, no match */
197 sc = sscanf(val, "@%u,%u-%u", &cmaj, &cmin0, &cmin1);
198 if (sc < 1 || major != cmaj
199 || (sc == 2 && minor != cmin0)
200 || (sc == 3 && (minor < cmin0 || minor > cmin1))
201 ) {
202 continue; /* this line doesn't match */
203 }
Denis Vlasenko3798db52009-04-19 21:37:07 +0000204 goto line_matches;
205 }
206 if (val[0] == '$') {
207 /* regex to match an environment variable */
208 char *eq = strchr(++val, '=');
209 if (!eq)
210 continue;
211 *eq = '\0';
212 str_to_match = getenv(val);
213 if (!str_to_match)
214 continue;
215 str_to_match -= strlen(val) + 1;
216 *eq = '=';
217 }
218 /* else: regex to match [subsystem/]device_name */
219
220 {
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000221 regex_t match;
222 int result;
Mike Frysingerc881c732007-11-19 09:04:22 +0000223
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000224 xregcomp(&match, val, REG_EXTENDED);
Denis Vlasenko3798db52009-04-19 21:37:07 +0000225 result = regexec(&match, str_to_match, ARRAY_SIZE(off), off, 0);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000226 regfree(&match);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000227 //bb_error_msg("matches:");
228 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
229 // if (off[i].rm_so < 0) continue;
230 // bb_error_msg("match %d: '%.*s'\n", i,
231 // (int)(off[i].rm_eo - off[i].rm_so),
232 // device_name + off[i].rm_so);
233 //}
234
Denis Vlasenko3798db52009-04-19 21:37:07 +0000235 /* If no match, skip rest of line */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000236 /* (regexec returns whole pattern as "range" 0) */
237 if (result || off[0].rm_so
Denis Vlasenko3798db52009-04-19 21:37:07 +0000238 || ((int)off[0].rm_eo != (int)strlen(str_to_match))
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000239 ) {
240 continue; /* this line doesn't match */
241 }
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000242 }
Denis Vlasenko3798db52009-04-19 21:37:07 +0000243 line_matches:
244 /* This line matches. Stop parsing after parsing
245 * the rest the line unless keep_matching == 1 */
Rob Landleyb56c2852005-12-17 10:52:30 +0000246
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000247 /* 2nd field: uid:gid - device ownership */
Denys Vlasenkoaa0a12d2009-05-02 16:33:10 +0200248 if (get_uidgid(&ugid, tokens[1], 1) == 0)
249 bb_error_msg("unknown user/group %s", tokens[1]);
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000250
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000251 /* 3rd field: mode - device permissions */
252 mode = strtoul(tokens[2], NULL, 8);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000253
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000254 val = tokens[3];
255 /* 4th field (opt): >|=alias */
Rob Landleyb56c2852005-12-17 10:52:30 +0000256
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000257 if (ENABLE_FEATURE_MDEV_RENAME && val) {
258 aliaslink = val[0];
259 if (aliaslink == '>' || aliaslink == '=') {
260 char *a, *s, *st;
261 char *p;
262 unsigned i, n;
Denis Vlasenko44615642008-03-29 13:10:57 +0000263
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000264 a = val;
265 s = strchrnul(val, ' ');
266 st = strchrnul(val, '\t');
267 if (st < s)
268 s = st;
269 val = (s[0] && s[1]) ? s+1 : NULL;
270 s[0] = '\0';
Denis Vlasenko44615642008-03-29 13:10:57 +0000271
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000272 if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) {
273 /* substitute %1..9 with off[1..9], if any */
274 n = 0;
275 s = a;
276 while (*s)
277 if (*s++ == '%')
278 n++;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000279
Denis Vlasenko3798db52009-04-19 21:37:07 +0000280 p = alias = xzalloc(strlen(a) + n * strlen(str_to_match));
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000281 s = a + 1;
282 while (*s) {
283 *p = *s;
284 if ('%' == *s) {
285 i = (s[1] - '0');
286 if (i <= 9 && off[i].rm_so >= 0) {
287 n = off[i].rm_eo - off[i].rm_so;
Denis Vlasenko3798db52009-04-19 21:37:07 +0000288 strncpy(p, str_to_match + off[i].rm_so, n);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000289 p += n - 1;
290 s++;
291 }
292 }
293 p++;
294 s++;
295 }
296 } else {
297 alias = xstrdup(a + 1);
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000298 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000299 }
300 }
301
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000302 if (ENABLE_FEATURE_MDEV_EXEC && val) {
303 const char *s = "$@*";
304 const char *s2 = strchr(s, val[0]);
Denis Vlasenko44615642008-03-29 13:10:57 +0000305
Denys Vlasenko5478aaf2009-07-02 13:17:10 +0200306 if (!s2) {
307 bb_error_msg("bad line %u", parser->lineno);
308 if (ENABLE_FEATURE_MDEV_RENAME)
309 free(alias);
310 continue;
311 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000312
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000313 /* Are we running this command now?
314 * Run $cmd on delete, @cmd on create, *cmd on both
315 */
316 if (s2-s != delete)
317 command = xstrdup(val + 1);
Denis Vlasenko44615642008-03-29 13:10:57 +0000318 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000319 }
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000320
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000321 /* End of field parsing */
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000322
323 /* "Execute" the line we found */
Denys Vlasenko90db3592009-07-02 13:07:11 +0200324 {
325 const char *node_name;
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000326
Denys Vlasenko90db3592009-07-02 13:07:11 +0200327 node_name = device_name;
Denys Vlasenko2f86ca12009-06-22 01:31:12 +0200328 if (ENABLE_FEATURE_MDEV_RENAME && alias)
Denys Vlasenko90db3592009-07-02 13:07:11 +0200329 node_name = alias = build_alias(alias, device_name);
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000330
Denys Vlasenko90db3592009-07-02 13:07:11 +0200331 if (!delete && major >= 0) {
332 if (mknod(node_name, mode | type, makedev(major, minor)) && errno != EEXIST)
333 bb_perror_msg_and_die("mknod %s", node_name);
334 if (major == root_major && minor == root_minor)
335 symlink(node_name, "root");
336 if (ENABLE_FEATURE_MDEV_CONF) {
337 chmod(node_name, mode);
338 chown(node_name, ugid.uid, ugid.gid);
339 }
340 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
341 if (aliaslink == '>')
342 symlink(node_name, device_name);
343 }
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000344 }
Denys Vlasenko90db3592009-07-02 13:07:11 +0200345
346 if (ENABLE_FEATURE_MDEV_EXEC && command) {
347 /* setenv will leak memory, use putenv/unsetenv/free */
348 char *s = xasprintf("%s=%s", "MDEV", node_name);
349 char *s1 = xasprintf("%s=%s", "SUBSYSTEM", subsystem);
350 putenv(s);
351 putenv(s1);
352 if (system(command) == -1)
Denys Vlasenko5478aaf2009-07-02 13:17:10 +0200353 bb_perror_msg("can't run '%s'", command);
Denys Vlasenko90db3592009-07-02 13:07:11 +0200354 unsetenv("SUBSYSTEM");
355 free(s1);
356 unsetenv("MDEV");
357 free(s);
358 free(command);
359 }
360
361 if (delete) {
362 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
363 if (aliaslink == '>')
364 unlink(device_name);
365 }
366 unlink(node_name);
367 }
368
Denys Vlasenko2f86ca12009-06-22 01:31:12 +0200369 if (ENABLE_FEATURE_MDEV_RENAME)
370 free(alias);
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000371 }
372
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000373 /* We found matching line.
374 * Stop unless it was prefixed with '-' */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000375 if (ENABLE_FEATURE_MDEV_CONF && !keep_matching)
Denis Vlasenkof2b39e02009-04-13 23:18:52 +0000376 break;
Mike Frysingerc881c732007-11-19 09:04:22 +0000377
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000378 /* end of "while line is read from /etc/mdev.conf" */
379 } while (ENABLE_FEATURE_MDEV_CONF);
380
381 if (ENABLE_FEATURE_MDEV_CONF)
382 config_close(parser);
Rob Landley70f7ef72005-12-13 08:21:33 +0000383}
384
Mike Frysingerb51fd352007-06-13 09:24:50 +0000385/* File callback for /sys/ traversal */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000386static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000387 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000388 void *userData,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000389 int depth UNUSED_PARAM)
Rob Landley70f7ef72005-12-13 08:21:33 +0000390{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000391 size_t len = strlen(fileName) - 4; /* can't underflow */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000392 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000393
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000394 /* len check is for paranoid reasons */
Denis Vlasenkod9860482008-07-12 10:23:16 +0000395 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000396 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000397
Mike Frysingerb51fd352007-06-13 09:24:50 +0000398 strcpy(scratch, fileName);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000399 scratch[len] = '\0';
Mike Frysingerb51fd352007-06-13 09:24:50 +0000400 make_device(scratch, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000401
Mike Frysingerb51fd352007-06-13 09:24:50 +0000402 return TRUE;
403}
Rob Landley70f7ef72005-12-13 08:21:33 +0000404
Mike Frysingerb51fd352007-06-13 09:24:50 +0000405/* Directory callback for /sys/ traversal */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000406static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
407 struct stat *statbuf UNUSED_PARAM,
408 void *userData UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000409 int depth)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000410{
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000411 /* Extract device subsystem -- the name of the directory
412 * under /sys/class/ */
Denis Vlasenko065c7142009-04-13 22:23:02 +0000413 if (1 == depth) {
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000414 free(subsystem);
Denis Vlasenko065c7142009-04-13 22:23:02 +0000415 subsystem = strrchr(fileName, '/');
416 if (subsystem)
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000417 subsystem = xstrdup(subsystem + 1);
Denis Vlasenko065c7142009-04-13 22:23:02 +0000418 }
419
Mike Frysingerb51fd352007-06-13 09:24:50 +0000420 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000421}
422
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000423/* For the full gory details, see linux/Documentation/firmware_class/README
424 *
425 * Firmware loading works like this:
426 * - kernel sets FIRMWARE env var
427 * - userspace checks /lib/firmware/$FIRMWARE
428 * - userspace waits for /sys/$DEVPATH/loading to appear
429 * - userspace writes "1" to /sys/$DEVPATH/loading
430 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
431 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
432 * - kernel loads firmware into device
433 */
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000434static void load_firmware(const char *firmware, const char *sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000435{
436 int cnt;
437 int firmware_fd, loading_fd, data_fd;
438
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000439 /* check for /lib/firmware/$FIRMWARE */
440 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000441 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000442
443 /* in case we goto out ... */
444 data_fd = -1;
445
446 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
447 xchdir(sysfs_path);
448 for (cnt = 0; cnt < 30; ++cnt) {
449 loading_fd = open("loading", O_WRONLY);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000450 if (loading_fd != -1)
451 goto loading;
452 sleep(1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000453 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000454 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000455
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000456 loading:
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000457 /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000458 if (full_write(loading_fd, "1", 1) != 1)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000459 goto out;
460
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000461 /* load firmware into /sys/$DEVPATH/data */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000462 data_fd = open("data", O_WRONLY);
463 if (data_fd == -1)
464 goto out;
465 cnt = bb_copyfd_eof(firmware_fd, data_fd);
466
Denis Vlasenko32a3d082009-04-14 21:23:33 +0000467 /* tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading" */
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000468 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000469 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000470 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000471 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000472
473 out:
474 if (ENABLE_FEATURE_CLEAN_UP) {
475 close(firmware_fd);
476 close(loading_fd);
477 close(data_fd);
478 }
479}
480
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000481int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkod9860482008-07-12 10:23:16 +0000482int mdev_main(int argc UNUSED_PARAM, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000483{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000484 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +0000485
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000486 /* We can be called as hotplug helper */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000487 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000488 bb_sanitize_stdio();
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000489
Denis Vlasenko3798db52009-04-19 21:37:07 +0000490 /* Force the configuration file settings exactly */
491 umask(0);
492
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000493 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000494
Denis Vlasenkoaafbae62009-04-13 13:33:02 +0000495 if (argv[1] && strcmp(argv[1], "-s") == 0) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000496 /* Scan:
497 * mdev -s
498 */
Rob Landleya7e3d052006-02-21 06:11:13 +0000499 struct stat st;
500
Denis Vlasenkof46be092006-10-16 19:39:37 +0000501 xstat("/", &st);
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000502 root_major = major(st.st_dev);
503 root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000504
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000505 /* ACTION_FOLLOWLINKS is needed since in newer kernels
506 * /sys/block/loop* (for example) are symlinks to dirs,
507 * not real directories.
508 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +0000509 * but we can't enforce that on users)
510 */
511 if (access("/sys/class/block", F_OK) != 0) {
512 /* Scan obsolete /sys/block only if /sys/class/block
513 * doesn't exist. Otherwise we'll have dupes.
Denis Vlasenkof1df35c2009-04-12 14:00:12 +0000514 * Also, do not complain if it doesn't exist.
515 * Some people configure kernel to have no blockdevs.
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +0000516 */
517 recursive_action("/sys/block",
Denis Vlasenkof1df35c2009-04-12 14:00:12 +0000518 ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET,
Denis Vlasenkod63cd1b2009-02-13 00:02:54 +0000519 fileAction, dirAction, temp, 0);
520 }
Mike Frysingerb51fd352007-06-13 09:24:50 +0000521 recursive_action("/sys/class",
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000522 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +0000523 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000524 } else {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000525 char *fw;
Denis Vlasenko018bee62008-07-15 22:33:13 +0000526 char *seq;
527 char *action;
528 char *env_path;
Denis Vlasenko018bee62008-07-15 22:33:13 +0000529
Mike Frysingerc881c732007-11-19 09:04:22 +0000530 /* Hotplug:
Denis Vlasenko065c7142009-04-13 22:23:02 +0000531 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
Mike Frysingerc881c732007-11-19 09:04:22 +0000532 * ACTION can be "add" or "remove"
533 * DEVPATH is like "/block/sda" or "/class/input/mice"
534 */
Rob Landley29e08ff2006-01-12 06:13:50 +0000535 action = getenv("ACTION");
536 env_path = getenv("DEVPATH");
Denis Vlasenko065c7142009-04-13 22:23:02 +0000537 subsystem = getenv("SUBSYSTEM");
538 if (!action || !env_path /*|| !subsystem*/)
Mike Frysingera421ba82006-02-03 00:25:37 +0000539 bb_show_usage();
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000540 fw = getenv("FIRMWARE");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000541
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000542 /* If it exists, does /dev/mdev.seq match $SEQNUM?
543 * If it does not match, earlier mdev is running
544 * in parallel, and we need to wait */
Denis Vlasenko018bee62008-07-15 22:33:13 +0000545 seq = getenv("SEQNUM");
546 if (seq) {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000547 int timeout = 2000 / 32; /* 2000 msec */
Denis Vlasenko018bee62008-07-15 22:33:13 +0000548 do {
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000549 int seqlen;
550 char seqbuf[sizeof(int)*3 + 2];
551
Denis Vlasenko018bee62008-07-15 22:33:13 +0000552 seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1));
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000553 if (seqlen < 0) {
554 seq = NULL;
Denis Vlasenko018bee62008-07-15 22:33:13 +0000555 break;
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000556 }
Denis Vlasenko018bee62008-07-15 22:33:13 +0000557 seqbuf[seqlen] = '\0';
558 if (seqbuf[0] == '\n' /* seed file? */
559 || strcmp(seq, seqbuf) == 0 /* correct idx? */
560 ) {
561 break;
562 }
563 usleep(32*1000);
564 } while (--timeout);
565 }
566
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000567 snprintf(temp, PATH_MAX, "/sys%s", env_path);
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000568 if (strcmp(action, "remove") == 0) {
569 /* Ignoring "remove firmware". It was reported
570 * to happen and to cause erroneous deletion
571 * of device nodes. */
572 if (!fw)
573 make_device(temp, 1);
574 }
575 else if (strcmp(action, "add") == 0) {
Denys Vlasenko76f5e382009-05-04 21:59:05 +0200576 make_device(temp, 0);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000577 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000578 if (fw)
579 load_firmware(fw, temp);
580 }
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000581 }
Denis Vlasenko018bee62008-07-15 22:33:13 +0000582
Denis Vlasenko1fb26da2009-04-13 02:15:57 +0000583 if (seq) {
Denis Vlasenko018bee62008-07-15 22:33:13 +0000584 xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1));
585 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000586 }
587
Mike Frysingerc881c732007-11-19 09:04:22 +0000588 if (ENABLE_FEATURE_CLEAN_UP)
589 RELEASE_CONFIG_BUFFER(temp);
590
Denis Vlasenkoaafbae62009-04-13 13:33:02 +0000591 return EXIT_SUCCESS;
Rob Landley70f7ef72005-12-13 08:21:33 +0000592}