blob: 5ac8015551c6ebb4a299065487d7428cf84be7a7 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
2/*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003 *
Rob Landley70f7ef72005-12-13 08:21:33 +00004 * mdev - Mini udev for busybox
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00005 *
Rob Landley70f7ef72005-12-13 08:21:33 +00006 * Copyright 2005 Rob Landley <rob@landley.net>
7 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
8 *
Rob Landleye9a7a622006-09-22 02:52:41 +00009 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
Rob Landley70f7ef72005-12-13 08:21:33 +000010 */
11
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Rob Landleyb56c2852005-12-17 10:52:30 +000013#include "xregex.h"
Rob Landley70f7ef72005-12-13 08:21:33 +000014
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +000015struct globals {
Rob Landley5cd1ccd2006-05-21 18:33:27 +000016 int root_major, root_minor;
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +000017};
18#define G (*(struct globals*)&bb_common_bufsiz1)
19#define root_major (G.root_major)
20#define root_minor (G.root_minor)
Rob Landleya7e3d052006-02-21 06:11:13 +000021
Denis Vlasenkod48e81f2008-07-06 07:00:11 +000022/* Prevent infinite loops in /sys symlinks */
23#define MAX_SYSFS_DEPTH 3
Mike Frysingerb51fd352007-06-13 09:24:50 +000024
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000025/* We use additional 64+ bytes in make_device() */
26#define SCRATCH_SIZE 80
27
Denis Vlasenkodf96df92008-07-26 18:35:10 +000028#if ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkod9860482008-07-12 10:23:16 +000029/* Builds an alias path.
30 * This function potentionally reallocates the alias parameter.
31 */
32static char *build_alias(char *alias, const char *device_name)
33{
34 char *dest;
35
36 /* ">bar/": rename to bar/device_name */
37 /* ">bar[/]baz": rename to bar[/]baz */
38 dest = strrchr(alias, '/');
39 if (dest) { /* ">bar/[baz]" ? */
40 *dest = '\0'; /* mkdir bar */
41 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
42 *dest = '/';
43 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
44 dest = alias;
45 alias = concat_path_file(alias, device_name);
46 free(dest);
47 }
48 }
49
50 return alias;
51}
Denis Vlasenkodf96df92008-07-26 18:35:10 +000052#endif
Denis Vlasenkod9860482008-07-12 10:23:16 +000053
Rob Landley70f7ef72005-12-13 08:21:33 +000054/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000055/* NB: "mdev -s" may call us many times, do not leak memory/fds! */
Rob Landleyef10d522006-06-26 14:11:33 +000056static void make_device(char *path, int delete)
Rob Landley70f7ef72005-12-13 08:21:33 +000057{
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000058 const char *device_name;
Denis Vlasenkof46be092006-10-16 19:39:37 +000059 int major, minor, type, len;
Mike Frysingera421ba82006-02-03 00:25:37 +000060 int mode = 0660;
Denis Vlasenkodf96df92008-07-26 18:35:10 +000061#if ENABLE_FEATURE_MDEV_CONF
Denis Vlasenko8d89bed2008-09-07 23:22:08 +000062 struct bb_uidgid_t ugid = { 0, 0 };
Denis Vlasenkodf96df92008-07-26 18:35:10 +000063 parser_t *parser;
64 char *tokens[5];
65#endif
66#if ENABLE_FEATURE_MDEV_EXEC
Rob Landleyef10d522006-06-26 14:11:33 +000067 char *command = NULL;
Denis Vlasenkodf96df92008-07-26 18:35:10 +000068#endif
69#if ENABLE_FEATURE_MDEV_RENAME
Mike Frysingerf0044c42008-02-01 06:53:50 +000070 char *alias = NULL;
Denis Vlasenkoa959a2a2008-05-22 21:35:16 +000071 char aliaslink = aliaslink; /* for compiler */
Denis Vlasenkodf96df92008-07-26 18:35:10 +000072#endif
73 char *dev_maj_min = path + strlen(path);
Mike Frysingerf0044c42008-02-01 06:53:50 +000074
75 /* Force the configuration file settings exactly. */
76 umask(0);
Rob Landley70f7ef72005-12-13 08:21:33 +000077
Rob Landley15fe2e12006-05-08 02:53:23 +000078 /* Try to read major/minor string. Note that the kernel puts \n after
79 * the data, so we don't need to worry about null terminating the string
Denis Vlasenko8d89bed2008-09-07 23:22:08 +000080 * because sscanf() will stop at the first nondigit, which \n is.
81 * We also depend on path having writeable space after it.
Mike Frysingerc881c732007-11-19 09:04:22 +000082 */
Denis Vlasenko8d89bed2008-09-07 23:22:08 +000083 major = -1;
Rob Landley10b36f92006-08-10 01:09:37 +000084 if (!delete) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000085 strcpy(dev_maj_min, "/dev");
86 len = open_read_close(path, dev_maj_min + 1, 64);
87 *dev_maj_min++ = '\0';
Mike Frysingerae86a332008-02-20 18:31:36 +000088 if (len < 1) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000089 if (!ENABLE_FEATURE_MDEV_EXEC)
Mike Frysingerae86a332008-02-20 18:31:36 +000090 return;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000091 /* no "dev" file, so just try to run script */
92 *dev_maj_min = '\0';
Denis Vlasenko8d89bed2008-09-07 23:22:08 +000093 } else if (sscanf(dev_maj_min, "%u:%u", &major, &minor) != 2) {
94 major = -1;
Mike Frysingerae86a332008-02-20 18:31:36 +000095 }
Rob Landley10b36f92006-08-10 01:09:37 +000096 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000097
Rob Landley70f7ef72005-12-13 08:21:33 +000098 /* Determine device name, type, major and minor */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000099 device_name = bb_basename(path);
Denis Vlasenkoaa8a6012008-03-29 16:07:46 +0000100 /* http://kernel.org/doc/pending/hotplug.txt says that only
Denis Vlasenkoa87045c2008-07-12 10:28:41 +0000101 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
102 * But since 2.6.25 block devices are also in /sys/class/block.
103 * We use strstr("/block/") to forestall future surprises. */
104 type = S_IFCHR;
105 if (strstr(path, "/block/"))
Denis Vlasenko4579e9c2008-05-08 21:49:28 +0000106 type = S_IFBLK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000107
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000108#if ENABLE_FEATURE_MDEV_CONF
109 parser = config_open2("/etc/mdev.conf", fopen_for_read);
Rob Landleyb56c2852005-12-17 10:52:30 +0000110
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000111 /* If we have config file, look up user settings */
Denis Vlasenko084266e2008-07-26 23:08:31 +0000112 while (config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL)) {
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000113 regmatch_t off[1 + 9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
114 char *val;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000115
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000116 /* Fields: regex uid:gid mode [alias] [cmd] */
Mike Frysingerc881c732007-11-19 09:04:22 +0000117
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000118 /* 1st field: @<numeric maj,min>... */
119 if (tokens[0][0] == '@') {
120 /* @major,minor[-last] */
121 /* (useful when name is ambiguous:
122 * "/sys/class/usb/lp0" and
123 * "/sys/class/printer/lp0") */
124 int cmaj, cmin0, cmin1, sc;
125 if (major < 0)
126 continue; /* no dev, no match */
127 sc = sscanf(tokens[0], "@%u,%u-%u", &cmaj, &cmin0, &cmin1);
128 if (sc < 1 || major != cmaj
129 || (sc == 2 && minor != cmin0)
130 || (sc == 3 && (minor < cmin0 || minor > cmin1))
131 ) {
132 continue; /* no match */
133 }
134 } else { /* ... or regex to match device name */
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000135 regex_t match;
136 int result;
Mike Frysingera421ba82006-02-03 00:25:37 +0000137
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000138 /* Is this it? */
139 xregcomp(&match, tokens[0], REG_EXTENDED);
140 result = regexec(&match, device_name, ARRAY_SIZE(off), off, 0);
141 regfree(&match);
Rob Landleyb56c2852005-12-17 10:52:30 +0000142
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000143 //bb_error_msg("matches:");
144 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
145 // if (off[i].rm_so < 0) continue;
146 // bb_error_msg("match %d: '%.*s'\n", i,
147 // (int)(off[i].rm_eo - off[i].rm_so),
148 // device_name + off[i].rm_so);
149 //}
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000150
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000151 /* If not this device, skip rest of line */
152 /* (regexec returns whole pattern as "range" 0) */
153 if (result || off[0].rm_so
154 || ((int)off[0].rm_eo != (int)strlen(device_name))
155 ) {
156 continue;
Denis Vlasenko44615642008-03-29 13:10:57 +0000157 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000158 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000159
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000160 /* This line matches: stop parsing the file
161 * after parsing the rest of fields */
Rob Landleyb56c2852005-12-17 10:52:30 +0000162
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000163 /* 2nd field: uid:gid - device ownership */
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000164 parse_chown_usergroup_or_die(&ugid, tokens[1]);
Denis Vlasenko44615642008-03-29 13:10:57 +0000165
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000166 /* 3rd field: mode - device permissions */
167 mode = strtoul(tokens[2], NULL, 8);
Denis Vlasenko44615642008-03-29 13:10:57 +0000168
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000169 val = tokens[3];
170 /* 4th field (opt): >alias */
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000171#if ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000172 if (!val)
173 break;
174 aliaslink = *val;
175 if (aliaslink == '>' || aliaslink == '=') {
176 char *s;
Bernhard Reutner-Fischerb62bd7b2008-07-22 07:33:53 +0000177#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000178 char *p;
179 unsigned i, n;
Bernhard Reutner-Fischerb62bd7b2008-07-22 07:33:53 +0000180#endif
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000181 char *a = val;
182 s = strchr(val, ' ');
183 val = (s && s[1]) ? s+1 : NULL;
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000184#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000185 /* substitute %1..9 with off[1..9], if any */
186 n = 0;
187 s = a;
188 while (*s)
189 if (*s++ == '%')
190 n++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000191
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000192 p = alias = xzalloc(strlen(a) + n * strlen(device_name));
193 s = a + 1;
194 while (*s) {
195 *p = *s;
196 if ('%' == *s) {
197 i = (s[1] - '0');
198 if (i <= 9 && off[i].rm_so >= 0) {
199 n = off[i].rm_eo - off[i].rm_so;
200 strncpy(p, device_name + off[i].rm_so, n);
201 p += n - 1;
202 s++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000203 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000204 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000205 p++;
206 s++;
Denis Vlasenko44615642008-03-29 13:10:57 +0000207 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000208#else
209 alias = xstrdup(a + 1);
210#endif
211 }
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000212#endif /* ENABLE_FEATURE_MDEV_RENAME */
Denis Vlasenko44615642008-03-29 13:10:57 +0000213
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000214#if ENABLE_FEATURE_MDEV_EXEC
215 /* The rest (opt): command to run */
216 if (!val)
217 break;
218 {
219 const char *s = "@$*";
220 const char *s2 = strchr(s, *val);
Denis Vlasenko44615642008-03-29 13:10:57 +0000221
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000222 if (!s2)
223 bb_error_msg_and_die("bad line %u", parser->lineno);
Denis Vlasenko44615642008-03-29 13:10:57 +0000224
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000225 /* Correlate the position in the "@$*" with the delete
226 * step so that we get the proper behavior:
227 * @cmd: run on create
228 * $cmd: run on delete
229 * *cmd: run on both
230 */
231 if ((s2 - s + 1) /*1/2/3*/ & /*1/2*/ (1 + delete)) {
232 command = xstrdup(val + 1);
Denis Vlasenko44615642008-03-29 13:10:57 +0000233 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000234 }
235#endif
236 /* end of field parsing */
237 break; /* we found matching line, stop */
238 } /* end of "while line is read from /etc/mdev.conf" */
Mike Frysingerc881c732007-11-19 09:04:22 +0000239
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000240 config_close(parser);
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000241#endif /* ENABLE_FEATURE_MDEV_CONF */
Rob Landley70f7ef72005-12-13 08:21:33 +0000242
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000243 if (!delete && major >= 0) {
Mike Frysingerf0044c42008-02-01 06:53:50 +0000244
245 if (ENABLE_FEATURE_MDEV_RENAME)
246 unlink(device_name);
247
Rob Landleyef10d522006-06-26 14:11:33 +0000248 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000249 bb_perror_msg_and_die("mknod %s", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000250
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000251 if (major == root_major && minor == root_minor)
Rob Landleyef10d522006-06-26 14:11:33 +0000252 symlink(device_name, "root");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000253
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000254#if ENABLE_FEATURE_MDEV_CONF
Denis Vlasenko8d89bed2008-09-07 23:22:08 +0000255 chown(device_name, ugid.uid, ugid.gid);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000256
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000257#if ENABLE_FEATURE_MDEV_RENAME
258 if (alias) {
259 alias = build_alias(alias, device_name);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000260
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000261 /* move the device, and optionally
262 * make a symlink to moved device node */
263 if (rename(device_name, alias) == 0 && aliaslink == '>')
264 symlink(alias, device_name);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000265
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000266 free(alias);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000267 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000268#endif
269#endif
Rob Landleyef10d522006-06-26 14:11:33 +0000270 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000271
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000272#if ENABLE_FEATURE_MDEV_EXEC
273 if (command) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000274 /* setenv will leak memory, use putenv/unsetenv/free */
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000275 char *s = xasprintf("MDEV=%s", device_name);
Rob Landleyef10d522006-06-26 14:11:33 +0000276 putenv(s);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000277 if (system(command) == -1)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000278 bb_perror_msg_and_die("can't run '%s'", command);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000279 s[4] = '\0';
280 unsetenv(s);
Rob Landleyef10d522006-06-26 14:11:33 +0000281 free(s);
282 free(command);
Rob Landleyef10d522006-06-26 14:11:33 +0000283 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000284#endif
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000285
Denis Vlasenkod9860482008-07-12 10:23:16 +0000286 if (delete) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000287 unlink(device_name);
Denis Vlasenkod9860482008-07-12 10:23:16 +0000288 /* At creation time, device might have been moved
289 * and a symlink might have been created. Undo that. */
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000290#if ENABLE_FEATURE_MDEV_RENAME
291 if (alias) {
Denis Vlasenkod9860482008-07-12 10:23:16 +0000292 alias = build_alias(alias, device_name);
293 unlink(alias);
294 free(alias);
295 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000296#endif
Denis Vlasenkod9860482008-07-12 10:23:16 +0000297 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000298}
299
Mike Frysingerb51fd352007-06-13 09:24:50 +0000300/* File callback for /sys/ traversal */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000301static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000302 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000303 void *userData,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000304 int depth UNUSED_PARAM)
Rob Landley70f7ef72005-12-13 08:21:33 +0000305{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000306 size_t len = strlen(fileName) - 4; /* can't underflow */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000307 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000308
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000309 /* len check is for paranoid reasons */
Denis Vlasenkod9860482008-07-12 10:23:16 +0000310 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000311 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000312
Mike Frysingerb51fd352007-06-13 09:24:50 +0000313 strcpy(scratch, fileName);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000314 scratch[len] = '\0';
Mike Frysingerb51fd352007-06-13 09:24:50 +0000315 make_device(scratch, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000316
Mike Frysingerb51fd352007-06-13 09:24:50 +0000317 return TRUE;
318}
Rob Landley70f7ef72005-12-13 08:21:33 +0000319
Mike Frysingerb51fd352007-06-13 09:24:50 +0000320/* Directory callback for /sys/ traversal */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000321static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
322 struct stat *statbuf UNUSED_PARAM,
323 void *userData UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000324 int depth)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000325{
326 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000327}
328
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000329/* For the full gory details, see linux/Documentation/firmware_class/README
330 *
331 * Firmware loading works like this:
332 * - kernel sets FIRMWARE env var
333 * - userspace checks /lib/firmware/$FIRMWARE
334 * - userspace waits for /sys/$DEVPATH/loading to appear
335 * - userspace writes "1" to /sys/$DEVPATH/loading
336 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
337 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
338 * - kernel loads firmware into device
339 */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000340static void load_firmware(const char *const firmware, const char *const sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000341{
342 int cnt;
343 int firmware_fd, loading_fd, data_fd;
344
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000345 /* check for /lib/firmware/$FIRMWARE */
346 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000347 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000348
349 /* in case we goto out ... */
350 data_fd = -1;
351
352 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
353 xchdir(sysfs_path);
354 for (cnt = 0; cnt < 30; ++cnt) {
355 loading_fd = open("loading", O_WRONLY);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000356 if (loading_fd != -1)
357 goto loading;
358 sleep(1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000359 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000360 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000361
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000362 loading:
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000363 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000364 if (full_write(loading_fd, "1", 1) != 1)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000365 goto out;
366
367 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
368 data_fd = open("data", O_WRONLY);
369 if (data_fd == -1)
370 goto out;
371 cnt = bb_copyfd_eof(firmware_fd, data_fd);
372
373 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
374 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000375 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000376 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000377 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000378
379 out:
380 if (ENABLE_FEATURE_CLEAN_UP) {
381 close(firmware_fd);
382 close(loading_fd);
383 close(data_fd);
384 }
385}
386
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000387int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkod9860482008-07-12 10:23:16 +0000388int mdev_main(int argc UNUSED_PARAM, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000389{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000390 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +0000391
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000392 /* We can be called as hotplug helper */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000393 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000394#if 1
395 bb_sanitize_stdio();
396#else
397 /* Debug code */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000398 /* Replace LOGFILE by other file or device name if you need */
399#define LOGFILE "/dev/console"
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000400 /* Just making sure fd 0 is not closed,
401 * we don't really intend to read from it */
402 xmove_fd(xopen("/", O_RDONLY), STDIN_FILENO);
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000403 xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDOUT_FILENO);
404 xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDERR_FILENO);
405#endif
406
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000407 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000408
Denis Vlasenkod9860482008-07-12 10:23:16 +0000409 if (argv[1] && !strcmp(argv[1], "-s")) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000410 /* Scan:
411 * mdev -s
412 */
Rob Landleya7e3d052006-02-21 06:11:13 +0000413 struct stat st;
414
Denis Vlasenkof46be092006-10-16 19:39:37 +0000415 xstat("/", &st);
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000416 root_major = major(st.st_dev);
417 root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000418
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000419 /* ACTION_FOLLOWLINKS is needed since in newer kernels
420 * /sys/block/loop* (for example) are symlinks to dirs,
421 * not real directories.
422 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
423 * but we can't enforce that on users) */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000424 recursive_action("/sys/block",
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000425 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +0000426 fileAction, dirAction, temp, 0);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000427 recursive_action("/sys/class",
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000428 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +0000429 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000430 } else {
Denis Vlasenko018bee62008-07-15 22:33:13 +0000431 char *seq;
432 char *action;
433 char *env_path;
434 char seqbuf[sizeof(int)*3 + 2];
435 int seqlen = seqlen; /* for compiler */
436
Mike Frysingerc881c732007-11-19 09:04:22 +0000437 /* Hotplug:
Denis Vlasenko018bee62008-07-15 22:33:13 +0000438 * env ACTION=... DEVPATH=... [SEQNUM=...] mdev
Mike Frysingerc881c732007-11-19 09:04:22 +0000439 * ACTION can be "add" or "remove"
440 * DEVPATH is like "/block/sda" or "/class/input/mice"
441 */
Rob Landley29e08ff2006-01-12 06:13:50 +0000442 action = getenv("ACTION");
443 env_path = getenv("DEVPATH");
Denis Vlasenko92758142006-10-03 19:56:34 +0000444 if (!action || !env_path)
Mike Frysingera421ba82006-02-03 00:25:37 +0000445 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000446
Denis Vlasenko018bee62008-07-15 22:33:13 +0000447 seq = getenv("SEQNUM");
448 if (seq) {
449 int timeout = 2000 / 32;
450 do {
451 seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1));
452 if (seqlen < 0)
453 break;
454 seqbuf[seqlen] = '\0';
455 if (seqbuf[0] == '\n' /* seed file? */
456 || strcmp(seq, seqbuf) == 0 /* correct idx? */
457 ) {
458 break;
459 }
460 usleep(32*1000);
461 } while (--timeout);
462 }
463
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000464 snprintf(temp, PATH_MAX, "/sys%s", env_path);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000465 if (!strcmp(action, "remove"))
466 make_device(temp, 1);
467 else if (!strcmp(action, "add")) {
468 make_device(temp, 0);
469
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000470 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
471 char *fw = getenv("FIRMWARE");
472 if (fw)
473 load_firmware(fw, temp);
474 }
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000475 }
Denis Vlasenko018bee62008-07-15 22:33:13 +0000476
477 if (seq && seqlen >= 0) {
478 xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1));
479 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000480 }
481
Mike Frysingerc881c732007-11-19 09:04:22 +0000482 if (ENABLE_FEATURE_CLEAN_UP)
483 RELEASE_CONFIG_BUFFER(temp);
484
Rob Landley70f7ef72005-12-13 08:21:33 +0000485 return 0;
486}