blob: d0d0103821794c8c44b738c6a5d4fbca9dbb8fae [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 */
Denis Vlasenko084266e2008-07-26 23:08:31 +0000110 while (config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL)) {
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000111 regmatch_t off[1 + 9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
112 char *val;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000113
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000114 /* Fields: regex uid:gid mode [alias] [cmd] */
Mike Frysingerc881c732007-11-19 09:04:22 +0000115
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000116 /* 1st field: regex to match this device */
117 {
118 regex_t match;
119 int result;
Mike Frysingera421ba82006-02-03 00:25:37 +0000120
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000121 /* Is this it? */
122 xregcomp(&match, tokens[0], REG_EXTENDED);
123 result = regexec(&match, device_name, ARRAY_SIZE(off), off, 0);
124 regfree(&match);
Rob Landleyb56c2852005-12-17 10:52:30 +0000125
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000126 //bb_error_msg("matches:");
127 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
128 // if (off[i].rm_so < 0) continue;
129 // bb_error_msg("match %d: '%.*s'\n", i,
130 // (int)(off[i].rm_eo - off[i].rm_so),
131 // device_name + off[i].rm_so);
132 //}
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000133
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000134 /* If not this device, skip rest of line */
135 /* (regexec returns whole pattern as "range" 0) */
136 if (result || off[0].rm_so
137 || ((int)off[0].rm_eo != (int)strlen(device_name))
138 ) {
139 continue;
Denis Vlasenko44615642008-03-29 13:10:57 +0000140 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000141 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000142
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000143 /* This line matches: stop parsing the file
144 * after parsing the rest of fields */
Rob Landleyb56c2852005-12-17 10:52:30 +0000145
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000146 /* 2nd field: uid:gid - device ownership */
147 {
148 struct passwd *pass;
149 struct group *grp;
150 char *str_uid = tokens[1];
151 char *str_gid = strchrnul(str_uid, ':');
Denis Vlasenkof46be092006-10-16 19:39:37 +0000152
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000153 if (*str_gid)
154 *str_gid++ = '\0';
155 /* Parse UID */
156 pass = getpwnam(str_uid);
157 if (pass)
158 uid = pass->pw_uid;
159 else
160 uid = strtoul(str_uid, NULL, 10);
161 /* Parse GID */
162 grp = getgrnam(str_gid);
163 if (grp)
164 gid = grp->gr_gid;
165 else
166 gid = strtoul(str_gid, NULL, 10);
167 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000168
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000169 /* 3rd field: mode - device permissions */
170 mode = strtoul(tokens[2], NULL, 8);
Denis Vlasenko44615642008-03-29 13:10:57 +0000171
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000172 val = tokens[3];
173 /* 4th field (opt): >alias */
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000174#if ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000175 if (!val)
176 break;
177 aliaslink = *val;
178 if (aliaslink == '>' || aliaslink == '=') {
179 char *s;
Bernhard Reutner-Fischerb62bd7b2008-07-22 07:33:53 +0000180#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000181 char *p;
182 unsigned i, n;
Bernhard Reutner-Fischerb62bd7b2008-07-22 07:33:53 +0000183#endif
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000184 char *a = val;
185 s = strchr(val, ' ');
186 val = (s && s[1]) ? s+1 : NULL;
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000187#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000188 /* substitute %1..9 with off[1..9], if any */
189 n = 0;
190 s = a;
191 while (*s)
192 if (*s++ == '%')
193 n++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000194
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000195 p = alias = xzalloc(strlen(a) + n * strlen(device_name));
196 s = a + 1;
197 while (*s) {
198 *p = *s;
199 if ('%' == *s) {
200 i = (s[1] - '0');
201 if (i <= 9 && off[i].rm_so >= 0) {
202 n = off[i].rm_eo - off[i].rm_so;
203 strncpy(p, device_name + off[i].rm_so, n);
204 p += n - 1;
205 s++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000206 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000207 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000208 p++;
209 s++;
Denis Vlasenko44615642008-03-29 13:10:57 +0000210 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000211#else
212 alias = xstrdup(a + 1);
213#endif
214 }
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000215#endif /* ENABLE_FEATURE_MDEV_RENAME */
Denis Vlasenko44615642008-03-29 13:10:57 +0000216
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000217#if ENABLE_FEATURE_MDEV_EXEC
218 /* The rest (opt): command to run */
219 if (!val)
220 break;
221 {
222 const char *s = "@$*";
223 const char *s2 = strchr(s, *val);
Denis Vlasenko44615642008-03-29 13:10:57 +0000224
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000225 if (!s2)
226 bb_error_msg_and_die("bad line %u", parser->lineno);
Denis Vlasenko44615642008-03-29 13:10:57 +0000227
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000228 /* Correlate the position in the "@$*" with the delete
229 * step so that we get the proper behavior:
230 * @cmd: run on create
231 * $cmd: run on delete
232 * *cmd: run on both
233 */
234 if ((s2 - s + 1) /*1/2/3*/ & /*1/2*/ (1 + delete)) {
235 command = xstrdup(val + 1);
Denis Vlasenko44615642008-03-29 13:10:57 +0000236 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000237 }
238#endif
239 /* end of field parsing */
240 break; /* we found matching line, stop */
241 } /* end of "while line is read from /etc/mdev.conf" */
Mike Frysingerc881c732007-11-19 09:04:22 +0000242
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000243 config_close(parser);
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000244#endif /* ENABLE_FEATURE_MDEV_CONF */
Rob Landley70f7ef72005-12-13 08:21:33 +0000245
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000246 if (!delete && sscanf(dev_maj_min, "%u:%u", &major, &minor) == 2) {
Mike Frysingerf0044c42008-02-01 06:53:50 +0000247
248 if (ENABLE_FEATURE_MDEV_RENAME)
249 unlink(device_name);
250
Rob Landleyef10d522006-06-26 14:11:33 +0000251 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000252 bb_perror_msg_and_die("mknod %s", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000253
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000254 if (major == root_major && minor == root_minor)
Rob Landleyef10d522006-06-26 14:11:33 +0000255 symlink(device_name, "root");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000256
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000257#if ENABLE_FEATURE_MDEV_CONF
258 chown(device_name, uid, gid);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000259
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000260#if ENABLE_FEATURE_MDEV_RENAME
261 if (alias) {
262 alias = build_alias(alias, device_name);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000263
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000264 /* move the device, and optionally
265 * make a symlink to moved device node */
266 if (rename(device_name, alias) == 0 && aliaslink == '>')
267 symlink(alias, device_name);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000268
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000269 free(alias);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000270 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000271#endif
272#endif
Rob Landleyef10d522006-06-26 14:11:33 +0000273 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000274
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000275#if ENABLE_FEATURE_MDEV_EXEC
276 if (command) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000277 /* setenv will leak memory, use putenv/unsetenv/free */
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000278 char *s = xasprintf("MDEV=%s", device_name);
Rob Landleyef10d522006-06-26 14:11:33 +0000279 putenv(s);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000280 if (system(command) == -1)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000281 bb_perror_msg_and_die("can't run '%s'", command);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000282 s[4] = '\0';
283 unsetenv(s);
Rob Landleyef10d522006-06-26 14:11:33 +0000284 free(s);
285 free(command);
Rob Landleyef10d522006-06-26 14:11:33 +0000286 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000287#endif
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000288
Denis Vlasenkod9860482008-07-12 10:23:16 +0000289 if (delete) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000290 unlink(device_name);
Denis Vlasenkod9860482008-07-12 10:23:16 +0000291 /* At creation time, device might have been moved
292 * and a symlink might have been created. Undo that. */
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000293#if ENABLE_FEATURE_MDEV_RENAME
294 if (alias) {
Denis Vlasenkod9860482008-07-12 10:23:16 +0000295 alias = build_alias(alias, device_name);
296 unlink(alias);
297 free(alias);
298 }
Denis Vlasenkodf96df92008-07-26 18:35:10 +0000299#endif
Denis Vlasenkod9860482008-07-12 10:23:16 +0000300 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000301}
302
Mike Frysingerb51fd352007-06-13 09:24:50 +0000303/* File callback for /sys/ traversal */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000304static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000305 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000306 void *userData,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000307 int depth UNUSED_PARAM)
Rob Landley70f7ef72005-12-13 08:21:33 +0000308{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000309 size_t len = strlen(fileName) - 4; /* can't underflow */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000310 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000311
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000312 /* len check is for paranoid reasons */
Denis Vlasenkod9860482008-07-12 10:23:16 +0000313 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000314 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000315
Mike Frysingerb51fd352007-06-13 09:24:50 +0000316 strcpy(scratch, fileName);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000317 scratch[len] = '\0';
Mike Frysingerb51fd352007-06-13 09:24:50 +0000318 make_device(scratch, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000319
Mike Frysingerb51fd352007-06-13 09:24:50 +0000320 return TRUE;
321}
Rob Landley70f7ef72005-12-13 08:21:33 +0000322
Mike Frysingerb51fd352007-06-13 09:24:50 +0000323/* Directory callback for /sys/ traversal */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000324static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
325 struct stat *statbuf UNUSED_PARAM,
326 void *userData UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000327 int depth)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000328{
329 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000330}
331
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000332/* For the full gory details, see linux/Documentation/firmware_class/README
333 *
334 * Firmware loading works like this:
335 * - kernel sets FIRMWARE env var
336 * - userspace checks /lib/firmware/$FIRMWARE
337 * - userspace waits for /sys/$DEVPATH/loading to appear
338 * - userspace writes "1" to /sys/$DEVPATH/loading
339 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
340 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
341 * - kernel loads firmware into device
342 */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000343static void load_firmware(const char *const firmware, const char *const sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000344{
345 int cnt;
346 int firmware_fd, loading_fd, data_fd;
347
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000348 /* check for /lib/firmware/$FIRMWARE */
349 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000350 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000351
352 /* in case we goto out ... */
353 data_fd = -1;
354
355 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
356 xchdir(sysfs_path);
357 for (cnt = 0; cnt < 30; ++cnt) {
358 loading_fd = open("loading", O_WRONLY);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000359 if (loading_fd != -1)
360 goto loading;
361 sleep(1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000362 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000363 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000364
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000365 loading:
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000366 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000367 if (full_write(loading_fd, "1", 1) != 1)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000368 goto out;
369
370 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
371 data_fd = open("data", O_WRONLY);
372 if (data_fd == -1)
373 goto out;
374 cnt = bb_copyfd_eof(firmware_fd, data_fd);
375
376 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
377 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000378 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000379 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000380 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000381
382 out:
383 if (ENABLE_FEATURE_CLEAN_UP) {
384 close(firmware_fd);
385 close(loading_fd);
386 close(data_fd);
387 }
388}
389
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000390int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkod9860482008-07-12 10:23:16 +0000391int mdev_main(int argc UNUSED_PARAM, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000392{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000393 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +0000394
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000395 /* We can be called as hotplug helper */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000396 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000397#if 1
398 bb_sanitize_stdio();
399#else
400 /* Debug code */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000401 /* Replace LOGFILE by other file or device name if you need */
402#define LOGFILE "/dev/console"
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000403 /* Just making sure fd 0 is not closed,
404 * we don't really intend to read from it */
405 xmove_fd(xopen("/", O_RDONLY), STDIN_FILENO);
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000406 xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDOUT_FILENO);
407 xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDERR_FILENO);
408#endif
409
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000410 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000411
Denis Vlasenkod9860482008-07-12 10:23:16 +0000412 if (argv[1] && !strcmp(argv[1], "-s")) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000413 /* Scan:
414 * mdev -s
415 */
Rob Landleya7e3d052006-02-21 06:11:13 +0000416 struct stat st;
417
Denis Vlasenkof46be092006-10-16 19:39:37 +0000418 xstat("/", &st);
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000419 root_major = major(st.st_dev);
420 root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000421
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000422 /* ACTION_FOLLOWLINKS is needed since in newer kernels
423 * /sys/block/loop* (for example) are symlinks to dirs,
424 * not real directories.
425 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
426 * but we can't enforce that on users) */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000427 recursive_action("/sys/block",
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000428 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +0000429 fileAction, dirAction, temp, 0);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000430 recursive_action("/sys/class",
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000431 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +0000432 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000433 } else {
Denis Vlasenko018bee62008-07-15 22:33:13 +0000434 char *seq;
435 char *action;
436 char *env_path;
437 char seqbuf[sizeof(int)*3 + 2];
438 int seqlen = seqlen; /* for compiler */
439
Mike Frysingerc881c732007-11-19 09:04:22 +0000440 /* Hotplug:
Denis Vlasenko018bee62008-07-15 22:33:13 +0000441 * env ACTION=... DEVPATH=... [SEQNUM=...] mdev
Mike Frysingerc881c732007-11-19 09:04:22 +0000442 * ACTION can be "add" or "remove"
443 * DEVPATH is like "/block/sda" or "/class/input/mice"
444 */
Rob Landley29e08ff2006-01-12 06:13:50 +0000445 action = getenv("ACTION");
446 env_path = getenv("DEVPATH");
Denis Vlasenko92758142006-10-03 19:56:34 +0000447 if (!action || !env_path)
Mike Frysingera421ba82006-02-03 00:25:37 +0000448 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000449
Denis Vlasenko018bee62008-07-15 22:33:13 +0000450 seq = getenv("SEQNUM");
451 if (seq) {
452 int timeout = 2000 / 32;
453 do {
454 seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1));
455 if (seqlen < 0)
456 break;
457 seqbuf[seqlen] = '\0';
458 if (seqbuf[0] == '\n' /* seed file? */
459 || strcmp(seq, seqbuf) == 0 /* correct idx? */
460 ) {
461 break;
462 }
463 usleep(32*1000);
464 } while (--timeout);
465 }
466
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000467 snprintf(temp, PATH_MAX, "/sys%s", env_path);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000468 if (!strcmp(action, "remove"))
469 make_device(temp, 1);
470 else if (!strcmp(action, "add")) {
471 make_device(temp, 0);
472
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000473 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
474 char *fw = getenv("FIRMWARE");
475 if (fw)
476 load_firmware(fw, temp);
477 }
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000478 }
Denis Vlasenko018bee62008-07-15 22:33:13 +0000479
480 if (seq && seqlen >= 0) {
481 xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1));
482 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000483 }
484
Mike Frysingerc881c732007-11-19 09:04:22 +0000485 if (ENABLE_FEATURE_CLEAN_UP)
486 RELEASE_CONFIG_BUFFER(temp);
487
Rob Landley70f7ef72005-12-13 08:21:33 +0000488 return 0;
489}