blob: f6440b19011e9725b76b632edf1468efdfcc910d [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
Mike Frysingera421ba82006-02-03 00:25:37 +000062 uid_t uid = 0;
63 gid_t gid = 0;
Denis Vlasenkodf96df92008-07-26 18:35:10 +000064 parser_t *parser;
65 char *tokens[5];
66#endif
67#if ENABLE_FEATURE_MDEV_EXEC
Rob Landleyef10d522006-06-26 14:11:33 +000068 char *command = NULL;
Denis Vlasenkodf96df92008-07-26 18:35:10 +000069#endif
70#if ENABLE_FEATURE_MDEV_RENAME
Mike Frysingerf0044c42008-02-01 06:53:50 +000071 char *alias = NULL;
Denis Vlasenkoa959a2a2008-05-22 21:35:16 +000072 char aliaslink = aliaslink; /* for compiler */
Denis Vlasenkodf96df92008-07-26 18:35:10 +000073#endif
74 char *dev_maj_min = path + strlen(path);
Mike Frysingerf0044c42008-02-01 06:53:50 +000075
76 /* Force the configuration file settings exactly. */
77 umask(0);
Rob Landley70f7ef72005-12-13 08:21:33 +000078
Rob Landley15fe2e12006-05-08 02:53:23 +000079 /* Try to read major/minor string. Note that the kernel puts \n after
80 * the data, so we don't need to worry about null terminating the string
81 * because sscanf() will stop at the first nondigit, which \n is. We
Mike Frysingerc881c732007-11-19 09:04:22 +000082 * also depend on path having writeable space after it.
83 */
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';
Mike Frysingerae86a332008-02-20 18:31:36 +000093 }
Rob Landley10b36f92006-08-10 01:09:37 +000094 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000095
Rob Landley70f7ef72005-12-13 08:21:33 +000096 /* Determine device name, type, major and minor */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000097 device_name = bb_basename(path);
Denis Vlasenkoaa8a6012008-03-29 16:07:46 +000098 /* http://kernel.org/doc/pending/hotplug.txt says that only
Denis Vlasenkoa87045c2008-07-12 10:28:41 +000099 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
100 * But since 2.6.25 block devices are also in /sys/class/block.
101 * We use strstr("/block/") to forestall future surprises. */
102 type = S_IFCHR;
103 if (strstr(path, "/block/"))
Denis Vlasenko4579e9c2008-05-08 21:49:28 +0000104 type = S_IFBLK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000105
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000106#if ENABLE_FEATURE_MDEV_CONF
107 parser = config_open2("/etc/mdev.conf", fopen_for_read);
Rob Landleyb56c2852005-12-17 10:52:30 +0000108
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000109 /* If we have config file, look up user settings */
110 if (!parser)
111 goto end_parse;
Rob Landleyb56c2852005-12-17 10:52:30 +0000112
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000113 while (config_read(parser, tokens, 4, 3, "# \t", PARSE_LAST_IS_GREEDY)) {
114 regmatch_t off[1 + 9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
115 char *val;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000116
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000117 /* Fields: regex uid:gid mode [alias] [cmd] */
Mike Frysingerc881c732007-11-19 09:04:22 +0000118
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000119 /* 1st field: regex to match this device */
120 {
121 regex_t match;
122 int result;
Mike Frysingera421ba82006-02-03 00:25:37 +0000123
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000124 /* Is this it? */
125 xregcomp(&match, tokens[0], REG_EXTENDED);
126 result = regexec(&match, device_name, ARRAY_SIZE(off), off, 0);
127 regfree(&match);
Rob Landleyb56c2852005-12-17 10:52:30 +0000128
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000129 //bb_error_msg("matches:");
130 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
131 // if (off[i].rm_so < 0) continue;
132 // bb_error_msg("match %d: '%.*s'\n", i,
133 // (int)(off[i].rm_eo - off[i].rm_so),
134 // device_name + off[i].rm_so);
135 //}
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000136
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000137 /* If not this device, skip rest of line */
138 /* (regexec returns whole pattern as "range" 0) */
139 if (result || off[0].rm_so
140 || ((int)off[0].rm_eo != (int)strlen(device_name))
141 ) {
142 continue;
Denis Vlasenko44615642008-03-29 13:10:57 +0000143 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000144 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000145
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000146 /* This line matches: stop parsing the file
147 * after parsing the rest of fields */
Rob Landleyb56c2852005-12-17 10:52:30 +0000148
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000149 /* 2nd field: uid:gid - device ownership */
150 {
151 struct passwd *pass;
152 struct group *grp;
153 char *str_uid = tokens[1];
154 char *str_gid = strchrnul(str_uid, ':');
Denis Vlasenkof46be092006-10-16 19:39:37 +0000155
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000156 if (*str_gid)
157 *str_gid++ = '\0';
158 /* Parse UID */
159 pass = getpwnam(str_uid);
160 if (pass)
161 uid = pass->pw_uid;
162 else
163 uid = strtoul(str_uid, NULL, 10);
164 /* Parse GID */
165 grp = getgrnam(str_gid);
166 if (grp)
167 gid = grp->gr_gid;
168 else
169 gid = strtoul(str_gid, NULL, 10);
170 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000171
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000172 /* 3rd field: mode - device permissions */
173 mode = strtoul(tokens[2], NULL, 8);
Denis Vlasenko44615642008-03-29 13:10:57 +0000174
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000175 val = tokens[3];
176 /* 4th field (opt): >alias */
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000177#if ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000178 if (!val)
179 break;
180 aliaslink = *val;
181 if (aliaslink == '>' || aliaslink == '=') {
182 char *s;
Bernhard Reutner-Fischerb62bd7b2008-07-22 07:33:53 +0000183#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000184 char *p;
185 unsigned i, n;
Bernhard Reutner-Fischerb62bd7b2008-07-22 07:33:53 +0000186#endif
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000187 char *a = val;
188 s = strchr(val, ' ');
189 val = (s && s[1]) ? s+1 : NULL;
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000190#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000191 /* substitute %1..9 with off[1..9], if any */
192 n = 0;
193 s = a;
194 while (*s)
195 if (*s++ == '%')
196 n++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000197
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000198 p = alias = xzalloc(strlen(a) + n * strlen(device_name));
199 s = a + 1;
200 while (*s) {
201 *p = *s;
202 if ('%' == *s) {
203 i = (s[1] - '0');
204 if (i <= 9 && off[i].rm_so >= 0) {
205 n = off[i].rm_eo - off[i].rm_so;
206 strncpy(p, device_name + off[i].rm_so, n);
207 p += n - 1;
208 s++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000209 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000210 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000211 p++;
212 s++;
Denis Vlasenko44615642008-03-29 13:10:57 +0000213 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000214#else
215 alias = xstrdup(a + 1);
216#endif
217 }
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000218#endif /* ENABLE_FEATURE_MDEV_RENAME */
Denis Vlasenko44615642008-03-29 13:10:57 +0000219
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000220#if ENABLE_FEATURE_MDEV_EXEC
221 /* The rest (opt): command to run */
222 if (!val)
223 break;
224 {
225 const char *s = "@$*";
226 const char *s2 = strchr(s, *val);
Denis Vlasenko44615642008-03-29 13:10:57 +0000227
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000228 if (!s2)
229 bb_error_msg_and_die("bad line %u", parser->lineno);
Denis Vlasenko44615642008-03-29 13:10:57 +0000230
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000231 /* Correlate the position in the "@$*" with the delete
232 * step so that we get the proper behavior:
233 * @cmd: run on create
234 * $cmd: run on delete
235 * *cmd: run on both
236 */
237 if ((s2 - s + 1) /*1/2/3*/ & /*1/2*/ (1 + delete)) {
238 command = xstrdup(val + 1);
Denis Vlasenko44615642008-03-29 13:10:57 +0000239 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000240 }
241#endif
242 /* end of field parsing */
243 break; /* we found matching line, stop */
244 } /* end of "while line is read from /etc/mdev.conf" */
Mike Frysingerc881c732007-11-19 09:04:22 +0000245
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000246 config_close(parser);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000247 end_parse:
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000248#endif /* ENABLE_FEATURE_MDEV_CONF */
Rob Landley70f7ef72005-12-13 08:21:33 +0000249
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000250 if (!delete && sscanf(dev_maj_min, "%u:%u", &major, &minor) == 2) {
Mike Frysingerf0044c42008-02-01 06:53:50 +0000251
252 if (ENABLE_FEATURE_MDEV_RENAME)
253 unlink(device_name);
254
Rob Landleyef10d522006-06-26 14:11:33 +0000255 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000256 bb_perror_msg_and_die("mknod %s", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000257
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000258 if (major == root_major && minor == root_minor)
Rob Landleyef10d522006-06-26 14:11:33 +0000259 symlink(device_name, "root");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000260
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000261#if ENABLE_FEATURE_MDEV_CONF
262 chown(device_name, uid, gid);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000263
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000264#if ENABLE_FEATURE_MDEV_RENAME
265 if (alias) {
266 alias = build_alias(alias, device_name);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000267
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000268 /* move the device, and optionally
269 * make a symlink to moved device node */
270 if (rename(device_name, alias) == 0 && aliaslink == '>')
271 symlink(alias, device_name);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000272
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000273 free(alias);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000274 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000275#endif
276#endif
Rob Landleyef10d522006-06-26 14:11:33 +0000277 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000278
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000279#if ENABLE_FEATURE_MDEV_EXEC
280 if (command) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000281 /* setenv will leak memory, use putenv/unsetenv/free */
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000282 char *s = xasprintf("MDEV=%s", device_name);
Rob Landleyef10d522006-06-26 14:11:33 +0000283 putenv(s);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000284 if (system(command) == -1)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000285 bb_perror_msg_and_die("can't run '%s'", command);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000286 s[4] = '\0';
287 unsetenv(s);
Rob Landleyef10d522006-06-26 14:11:33 +0000288 free(s);
289 free(command);
Rob Landleyef10d522006-06-26 14:11:33 +0000290 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000291#endif
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000292
Denis Vlasenkod9860482008-07-12 10:23:16 +0000293 if (delete) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000294 unlink(device_name);
Denis Vlasenkod9860482008-07-12 10:23:16 +0000295 /* At creation time, device might have been moved
296 * and a symlink might have been created. Undo that. */
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000297#if ENABLE_FEATURE_MDEV_RENAME
298 if (alias) {
Denis Vlasenkod9860482008-07-12 10:23:16 +0000299 alias = build_alias(alias, device_name);
300 unlink(alias);
301 free(alias);
302 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000303#endif
Denis Vlasenkod9860482008-07-12 10:23:16 +0000304 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000305}
306
Mike Frysingerb51fd352007-06-13 09:24:50 +0000307/* File callback for /sys/ traversal */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000308static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000309 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000310 void *userData,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000311 int depth UNUSED_PARAM)
Rob Landley70f7ef72005-12-13 08:21:33 +0000312{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000313 size_t len = strlen(fileName) - 4; /* can't underflow */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000314 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000315
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000316 /* len check is for paranoid reasons */
Denis Vlasenkod9860482008-07-12 10:23:16 +0000317 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000318 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000319
Mike Frysingerb51fd352007-06-13 09:24:50 +0000320 strcpy(scratch, fileName);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000321 scratch[len] = '\0';
Mike Frysingerb51fd352007-06-13 09:24:50 +0000322 make_device(scratch, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000323
Mike Frysingerb51fd352007-06-13 09:24:50 +0000324 return TRUE;
325}
Rob Landley70f7ef72005-12-13 08:21:33 +0000326
Mike Frysingerb51fd352007-06-13 09:24:50 +0000327/* Directory callback for /sys/ traversal */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000328static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
329 struct stat *statbuf UNUSED_PARAM,
330 void *userData UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000331 int depth)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000332{
333 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000334}
335
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000336/* For the full gory details, see linux/Documentation/firmware_class/README
337 *
338 * Firmware loading works like this:
339 * - kernel sets FIRMWARE env var
340 * - userspace checks /lib/firmware/$FIRMWARE
341 * - userspace waits for /sys/$DEVPATH/loading to appear
342 * - userspace writes "1" to /sys/$DEVPATH/loading
343 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
344 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
345 * - kernel loads firmware into device
346 */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000347static void load_firmware(const char *const firmware, const char *const sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000348{
349 int cnt;
350 int firmware_fd, loading_fd, data_fd;
351
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000352 /* check for /lib/firmware/$FIRMWARE */
353 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000354 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000355
356 /* in case we goto out ... */
357 data_fd = -1;
358
359 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
360 xchdir(sysfs_path);
361 for (cnt = 0; cnt < 30; ++cnt) {
362 loading_fd = open("loading", O_WRONLY);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000363 if (loading_fd != -1)
364 goto loading;
365 sleep(1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000366 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000367 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000368
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000369 loading:
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000370 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000371 if (full_write(loading_fd, "1", 1) != 1)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000372 goto out;
373
374 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
375 data_fd = open("data", O_WRONLY);
376 if (data_fd == -1)
377 goto out;
378 cnt = bb_copyfd_eof(firmware_fd, data_fd);
379
380 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
381 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000382 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000383 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000384 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000385
386 out:
387 if (ENABLE_FEATURE_CLEAN_UP) {
388 close(firmware_fd);
389 close(loading_fd);
390 close(data_fd);
391 }
392}
393
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000394int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkod9860482008-07-12 10:23:16 +0000395int mdev_main(int argc UNUSED_PARAM, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000396{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000397 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +0000398
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000399 /* We can be called as hotplug helper */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000400 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000401#if 1
402 bb_sanitize_stdio();
403#else
404 /* Debug code */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000405 /* Replace LOGFILE by other file or device name if you need */
406#define LOGFILE "/dev/console"
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000407 /* Just making sure fd 0 is not closed,
408 * we don't really intend to read from it */
409 xmove_fd(xopen("/", O_RDONLY), STDIN_FILENO);
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000410 xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDOUT_FILENO);
411 xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDERR_FILENO);
412#endif
413
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000414 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000415
Denis Vlasenkod9860482008-07-12 10:23:16 +0000416 if (argv[1] && !strcmp(argv[1], "-s")) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000417 /* Scan:
418 * mdev -s
419 */
Rob Landleya7e3d052006-02-21 06:11:13 +0000420 struct stat st;
421
Denis Vlasenkof46be092006-10-16 19:39:37 +0000422 xstat("/", &st);
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000423 root_major = major(st.st_dev);
424 root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000425
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000426 /* ACTION_FOLLOWLINKS is needed since in newer kernels
427 * /sys/block/loop* (for example) are symlinks to dirs,
428 * not real directories.
429 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
430 * but we can't enforce that on users) */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000431 recursive_action("/sys/block",
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000432 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +0000433 fileAction, dirAction, temp, 0);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000434 recursive_action("/sys/class",
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000435 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +0000436 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000437 } else {
Denis Vlasenko018bee62008-07-15 22:33:13 +0000438 char *seq;
439 char *action;
440 char *env_path;
441 char seqbuf[sizeof(int)*3 + 2];
442 int seqlen = seqlen; /* for compiler */
443
Mike Frysingerc881c732007-11-19 09:04:22 +0000444 /* Hotplug:
Denis Vlasenko018bee62008-07-15 22:33:13 +0000445 * env ACTION=... DEVPATH=... [SEQNUM=...] mdev
Mike Frysingerc881c732007-11-19 09:04:22 +0000446 * ACTION can be "add" or "remove"
447 * DEVPATH is like "/block/sda" or "/class/input/mice"
448 */
Rob Landley29e08ff2006-01-12 06:13:50 +0000449 action = getenv("ACTION");
450 env_path = getenv("DEVPATH");
Denis Vlasenko92758142006-10-03 19:56:34 +0000451 if (!action || !env_path)
Mike Frysingera421ba82006-02-03 00:25:37 +0000452 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000453
Denis Vlasenko018bee62008-07-15 22:33:13 +0000454 seq = getenv("SEQNUM");
455 if (seq) {
456 int timeout = 2000 / 32;
457 do {
458 seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1));
459 if (seqlen < 0)
460 break;
461 seqbuf[seqlen] = '\0';
462 if (seqbuf[0] == '\n' /* seed file? */
463 || strcmp(seq, seqbuf) == 0 /* correct idx? */
464 ) {
465 break;
466 }
467 usleep(32*1000);
468 } while (--timeout);
469 }
470
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000471 snprintf(temp, PATH_MAX, "/sys%s", env_path);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000472 if (!strcmp(action, "remove"))
473 make_device(temp, 1);
474 else if (!strcmp(action, "add")) {
475 make_device(temp, 0);
476
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000477 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
478 char *fw = getenv("FIRMWARE");
479 if (fw)
480 load_firmware(fw, temp);
481 }
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000482 }
Denis Vlasenko018bee62008-07-15 22:33:13 +0000483
484 if (seq && seqlen >= 0) {
485 xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1));
486 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000487 }
488
Mike Frysingerc881c732007-11-19 09:04:22 +0000489 if (ENABLE_FEATURE_CLEAN_UP)
490 RELEASE_CONFIG_BUFFER(temp);
491
Rob Landley70f7ef72005-12-13 08:21:33 +0000492 return 0;
493}