blob: 461d3c8c148e5c4d56e5135fe19fd15ff6975c61 [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 Vlasenkod9860482008-07-12 10:23:16 +000028/* Builds an alias path.
29 * This function potentionally reallocates the alias parameter.
30 */
31static char *build_alias(char *alias, const char *device_name)
32{
33 char *dest;
34
35 /* ">bar/": rename to bar/device_name */
36 /* ">bar[/]baz": rename to bar[/]baz */
37 dest = strrchr(alias, '/');
38 if (dest) { /* ">bar/[baz]" ? */
39 *dest = '\0'; /* mkdir bar */
40 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
41 *dest = '/';
42 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
43 dest = alias;
44 alias = concat_path_file(alias, device_name);
45 free(dest);
46 }
47 }
48
49 return alias;
50}
51
Rob Landley70f7ef72005-12-13 08:21:33 +000052/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000053/* NB: "mdev -s" may call us many times, do not leak memory/fds! */
Rob Landleyef10d522006-06-26 14:11:33 +000054static void make_device(char *path, int delete)
Rob Landley70f7ef72005-12-13 08:21:33 +000055{
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000056 const char *device_name;
Denis Vlasenkof46be092006-10-16 19:39:37 +000057 int major, minor, type, len;
Mike Frysingera421ba82006-02-03 00:25:37 +000058 int mode = 0660;
59 uid_t uid = 0;
60 gid_t gid = 0;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000061 char *dev_maj_min = path + strlen(path);
Rob Landleyef10d522006-06-26 14:11:33 +000062 char *command = NULL;
Mike Frysingerf0044c42008-02-01 06:53:50 +000063 char *alias = NULL;
Denis Vlasenkoa959a2a2008-05-22 21:35:16 +000064 char aliaslink = aliaslink; /* for compiler */
Mike Frysingerf0044c42008-02-01 06:53:50 +000065
66 /* Force the configuration file settings exactly. */
67 umask(0);
Rob Landley70f7ef72005-12-13 08:21:33 +000068
Rob Landley15fe2e12006-05-08 02:53:23 +000069 /* Try to read major/minor string. Note that the kernel puts \n after
70 * the data, so we don't need to worry about null terminating the string
71 * because sscanf() will stop at the first nondigit, which \n is. We
Mike Frysingerc881c732007-11-19 09:04:22 +000072 * also depend on path having writeable space after it.
73 */
Rob Landley10b36f92006-08-10 01:09:37 +000074 if (!delete) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000075 strcpy(dev_maj_min, "/dev");
76 len = open_read_close(path, dev_maj_min + 1, 64);
77 *dev_maj_min++ = '\0';
Mike Frysingerae86a332008-02-20 18:31:36 +000078 if (len < 1) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000079 if (!ENABLE_FEATURE_MDEV_EXEC)
Mike Frysingerae86a332008-02-20 18:31:36 +000080 return;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000081 /* no "dev" file, so just try to run script */
82 *dev_maj_min = '\0';
Mike Frysingerae86a332008-02-20 18:31:36 +000083 }
Rob Landley10b36f92006-08-10 01:09:37 +000084 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000085
Rob Landley70f7ef72005-12-13 08:21:33 +000086 /* Determine device name, type, major and minor */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000087 device_name = bb_basename(path);
Denis Vlasenkoaa8a6012008-03-29 16:07:46 +000088 /* http://kernel.org/doc/pending/hotplug.txt says that only
Denis Vlasenkoa87045c2008-07-12 10:28:41 +000089 * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
90 * But since 2.6.25 block devices are also in /sys/class/block.
91 * We use strstr("/block/") to forestall future surprises. */
92 type = S_IFCHR;
93 if (strstr(path, "/block/"))
Denis Vlasenko4579e9c2008-05-08 21:49:28 +000094 type = S_IFBLK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000095
Rob Landleyb56c2852005-12-17 10:52:30 +000096 if (ENABLE_FEATURE_MDEV_CONF) {
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +000097 parser_t *parser = config_open("/etc/mdev.conf");
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +000098 char *tokens[5];
Rob Landleyb56c2852005-12-17 10:52:30 +000099
Denis Vlasenko44615642008-03-29 13:10:57 +0000100 /* If we have config file, look up user settings */
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000101 if (!parser)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000102 goto end_parse;
Rob Landleyb56c2852005-12-17 10:52:30 +0000103
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000104 while (config_read(parser, tokens, 4, 3, "# \t", PARSE_LAST_IS_GREEDY)) {
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000105 regmatch_t off[1+9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000106 char *val;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000107
108 /* Fields: regex uid:gid mode [alias] [cmd] */
Mike Frysingerc881c732007-11-19 09:04:22 +0000109
Denis Vlasenko44615642008-03-29 13:10:57 +0000110 /* 1st field: regex to match this device */
Denis Vlasenko44615642008-03-29 13:10:57 +0000111 {
112 regex_t match;
Denis Vlasenko44615642008-03-29 13:10:57 +0000113 int result;
Mike Frysingera421ba82006-02-03 00:25:37 +0000114
Denis Vlasenko44615642008-03-29 13:10:57 +0000115 /* Is this it? */
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000116 xregcomp(&match, tokens[0], REG_EXTENDED);
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000117 result = regexec(&match, device_name, ARRAY_SIZE(off), off, 0);
Denis Vlasenko44615642008-03-29 13:10:57 +0000118 regfree(&match);
Rob Landleyb56c2852005-12-17 10:52:30 +0000119
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000120 //bb_error_msg("matches:");
121 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
122 // if (off[i].rm_so < 0) continue;
123 // bb_error_msg("match %d: '%.*s'\n", i,
124 // (int)(off[i].rm_eo - off[i].rm_so),
125 // device_name + off[i].rm_so);
126 //}
127
Denis Vlasenko44615642008-03-29 13:10:57 +0000128 /* If not this device, skip rest of line */
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000129 /* (regexec returns whole pattern as "range" 0) */
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000130 if (result || off[0].rm_so
131 || ((int)off[0].rm_eo != (int)strlen(device_name))
132 ) {
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000133 continue;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000134 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000135 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000136
Denis Vlasenko44615642008-03-29 13:10:57 +0000137 /* This line matches: stop parsing the file
138 * after parsing the rest of fields */
Rob Landleyb56c2852005-12-17 10:52:30 +0000139
Denis Vlasenko44615642008-03-29 13:10:57 +0000140 /* 2nd field: uid:gid - device ownership */
Denis Vlasenko44615642008-03-29 13:10:57 +0000141 {
142 struct passwd *pass;
143 struct group *grp;
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000144 char *str_uid = tokens[1];
145 char *str_gid = strchrnul(str_uid, ':');
Denis Vlasenkof46be092006-10-16 19:39:37 +0000146
Denis Vlasenko44615642008-03-29 13:10:57 +0000147 if (*str_gid)
148 *str_gid++ = '\0';
149 /* Parse UID */
150 pass = getpwnam(str_uid);
151 if (pass)
152 uid = pass->pw_uid;
153 else
154 uid = strtoul(str_uid, NULL, 10);
155 /* Parse GID */
156 grp = getgrnam(str_gid);
157 if (grp)
158 gid = grp->gr_gid;
159 else
160 gid = strtoul(str_gid, NULL, 10);
161 }
162
163 /* 3rd field: mode - device permissions */
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000164 mode = strtoul(tokens[2], NULL, 8);
Denis Vlasenko44615642008-03-29 13:10:57 +0000165
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000166 val = tokens[3];
Denis Vlasenko44615642008-03-29 13:10:57 +0000167 /* 4th field (opt): >alias */
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000168#if ENABLE_FEATURE_MDEV_RENAME
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000169 if (!val)
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000170 break;
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000171 aliaslink = *val;
172 if (aliaslink == '>' || aliaslink == '=') {
Bernhard Reutner-Fischerb62bd7b2008-07-22 07:33:53 +0000173 char *s;
174#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
175 char *p;
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000176 unsigned i, n;
Bernhard Reutner-Fischerb62bd7b2008-07-22 07:33:53 +0000177#endif
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000178 char *a = val;
179 s = strchr(val, ' ');
180 val = (s && s[1]) ? s+1 : NULL;
181#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000182 /* substitute %1..9 with off[1..9], if any */
183 n = 0;
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000184 s = a;
Denis Vlasenkoe94a8712008-05-24 16:46:13 +0000185 while (*s)
186 if (*s++ == '%')
187 n++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000188
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000189 p = alias = xzalloc(strlen(a) + n * strlen(device_name));
190 s = a + 1;
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000191 while (*s) {
192 *p = *s;
193 if ('%' == *s) {
194 i = (s[1] - '0');
195 if (i <= 9 && off[i].rm_so >= 0) {
196 n = off[i].rm_eo - off[i].rm_so;
197 strncpy(p, device_name + off[i].rm_so, n);
198 p += n - 1;
199 s++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000200 }
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000201 }
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000202 p++;
203 s++;
Denis Vlasenko44615642008-03-29 13:10:57 +0000204 }
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000205#else
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000206 alias = xstrdup(a + 1);
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000207#endif
Denis Vlasenko44615642008-03-29 13:10:57 +0000208 }
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000209#endif /* ENABLE_FEATURE_MDEV_RENAME */
Denis Vlasenko44615642008-03-29 13:10:57 +0000210
211 /* The rest (opt): command to run */
Denis Vlasenkoe559e0a2008-07-15 21:09:30 +0000212 if (!val)
Denis Vlasenko44615642008-03-29 13:10:57 +0000213 break;
Denis Vlasenko44615642008-03-29 13:10:57 +0000214 if (ENABLE_FEATURE_MDEV_EXEC) {
215 const char *s = "@$*";
216 const char *s2 = strchr(s, *val);
217
218 if (!s2)
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000219 bb_error_msg_and_die("bad line %u", parser->lineno);
Denis Vlasenko44615642008-03-29 13:10:57 +0000220
221 /* Correlate the position in the "@$*" with the delete
222 * step so that we get the proper behavior:
223 * @cmd: run on create
224 * $cmd: run on delete
225 * *cmd: run on both
226 */
227 if ((s2 - s + 1) /*1/2/3*/ & /*1/2*/ (1 + delete)) {
228 command = xstrdup(val + 1);
229 }
230 }
231 /* end of field parsing */
232 break; /* we found matching line, stop */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000233 } /* end of "while line is read from /etc/mdev.conf" */
Mike Frysingerc881c732007-11-19 09:04:22 +0000234
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000235 config_close(parser);
Rob Landleyb56c2852005-12-17 10:52:30 +0000236 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000237 end_parse:
Rob Landley70f7ef72005-12-13 08:21:33 +0000238
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000239 if (!delete && sscanf(dev_maj_min, "%u:%u", &major, &minor) == 2) {
Mike Frysingerf0044c42008-02-01 06:53:50 +0000240
241 if (ENABLE_FEATURE_MDEV_RENAME)
242 unlink(device_name);
243
Rob Landleyef10d522006-06-26 14:11:33 +0000244 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000245 bb_perror_msg_and_die("mknod %s", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000246
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000247 if (major == root_major && minor == root_minor)
Rob Landleyef10d522006-06-26 14:11:33 +0000248 symlink(device_name, "root");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000249
Mike Frysingerf0044c42008-02-01 06:53:50 +0000250 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000251 chown(device_name, uid, gid);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000252
253 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
Denis Vlasenkod9860482008-07-12 10:23:16 +0000254 alias = build_alias(alias, device_name);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000255
Denis Vlasenkoa959a2a2008-05-22 21:35:16 +0000256 /* move the device, and optionally
257 * make a symlink to moved device node */
Denis Vlasenkoff923492008-05-23 20:36:33 +0000258 if (rename(device_name, alias) == 0 && aliaslink == '>')
Denis Vlasenkocae11b52008-03-29 15:11:07 +0000259 symlink(alias, device_name);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000260
Denis Vlasenkocae11b52008-03-29 15:11:07 +0000261 free(alias);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000262 }
263 }
Rob Landleyef10d522006-06-26 14:11:33 +0000264 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000265
Mike Frysingerf0044c42008-02-01 06:53:50 +0000266 if (ENABLE_FEATURE_MDEV_EXEC && command) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000267 /* setenv will leak memory, use putenv/unsetenv/free */
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000268 char *s = xasprintf("MDEV=%s", device_name);
Rob Landleyef10d522006-06-26 14:11:33 +0000269 putenv(s);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000270 if (system(command) == -1)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000271 bb_perror_msg_and_die("can't run '%s'", command);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000272 s[4] = '\0';
273 unsetenv(s);
Rob Landleyef10d522006-06-26 14:11:33 +0000274 free(s);
275 free(command);
Rob Landleyef10d522006-06-26 14:11:33 +0000276 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000277
Denis Vlasenkod9860482008-07-12 10:23:16 +0000278 if (delete) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000279 unlink(device_name);
Denis Vlasenkod9860482008-07-12 10:23:16 +0000280 /* At creation time, device might have been moved
281 * and a symlink might have been created. Undo that. */
282 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
283 alias = build_alias(alias, device_name);
284 unlink(alias);
285 free(alias);
286 }
287 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000288}
289
Mike Frysingerb51fd352007-06-13 09:24:50 +0000290/* File callback for /sys/ traversal */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000291static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000292 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000293 void *userData,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000294 int depth UNUSED_PARAM)
Rob Landley70f7ef72005-12-13 08:21:33 +0000295{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000296 size_t len = strlen(fileName) - 4; /* can't underflow */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000297 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000298
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000299 /* len check is for paranoid reasons */
Denis Vlasenkod9860482008-07-12 10:23:16 +0000300 if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000301 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000302
Mike Frysingerb51fd352007-06-13 09:24:50 +0000303 strcpy(scratch, fileName);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000304 scratch[len] = '\0';
Mike Frysingerb51fd352007-06-13 09:24:50 +0000305 make_device(scratch, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000306
Mike Frysingerb51fd352007-06-13 09:24:50 +0000307 return TRUE;
308}
Rob Landley70f7ef72005-12-13 08:21:33 +0000309
Mike Frysingerb51fd352007-06-13 09:24:50 +0000310/* Directory callback for /sys/ traversal */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000311static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
312 struct stat *statbuf UNUSED_PARAM,
313 void *userData UNUSED_PARAM,
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000314 int depth)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000315{
316 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000317}
318
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000319/* For the full gory details, see linux/Documentation/firmware_class/README
320 *
321 * Firmware loading works like this:
322 * - kernel sets FIRMWARE env var
323 * - userspace checks /lib/firmware/$FIRMWARE
324 * - userspace waits for /sys/$DEVPATH/loading to appear
325 * - userspace writes "1" to /sys/$DEVPATH/loading
326 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
327 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
328 * - kernel loads firmware into device
329 */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000330static void load_firmware(const char *const firmware, const char *const sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000331{
332 int cnt;
333 int firmware_fd, loading_fd, data_fd;
334
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000335 /* check for /lib/firmware/$FIRMWARE */
336 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000337 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000338
339 /* in case we goto out ... */
340 data_fd = -1;
341
342 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
343 xchdir(sysfs_path);
344 for (cnt = 0; cnt < 30; ++cnt) {
345 loading_fd = open("loading", O_WRONLY);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000346 if (loading_fd != -1)
347 goto loading;
348 sleep(1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000349 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000350 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000351
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000352 loading:
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000353 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000354 if (full_write(loading_fd, "1", 1) != 1)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000355 goto out;
356
357 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
358 data_fd = open("data", O_WRONLY);
359 if (data_fd == -1)
360 goto out;
361 cnt = bb_copyfd_eof(firmware_fd, data_fd);
362
363 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
364 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000365 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000366 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000367 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000368
369 out:
370 if (ENABLE_FEATURE_CLEAN_UP) {
371 close(firmware_fd);
372 close(loading_fd);
373 close(data_fd);
374 }
375}
376
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000377int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkod9860482008-07-12 10:23:16 +0000378int mdev_main(int argc UNUSED_PARAM, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000379{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000380 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +0000381
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000382 /* We can be called as hotplug helper */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000383 /* Kernel cannot provide suitable stdio fds for us, do it ourself */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000384#if 1
385 bb_sanitize_stdio();
386#else
387 /* Debug code */
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000388 /* Replace LOGFILE by other file or device name if you need */
389#define LOGFILE "/dev/console"
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000390 /* Just making sure fd 0 is not closed,
391 * we don't really intend to read from it */
392 xmove_fd(xopen("/", O_RDONLY), STDIN_FILENO);
Denis Vlasenkof4e6bd02008-05-31 18:27:58 +0000393 xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDOUT_FILENO);
394 xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDERR_FILENO);
395#endif
396
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000397 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000398
Denis Vlasenkod9860482008-07-12 10:23:16 +0000399 if (argv[1] && !strcmp(argv[1], "-s")) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000400 /* Scan:
401 * mdev -s
402 */
Rob Landleya7e3d052006-02-21 06:11:13 +0000403 struct stat st;
404
Denis Vlasenkof46be092006-10-16 19:39:37 +0000405 xstat("/", &st);
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000406 root_major = major(st.st_dev);
407 root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000408
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000409 /* ACTION_FOLLOWLINKS is needed since in newer kernels
410 * /sys/block/loop* (for example) are symlinks to dirs,
411 * not real directories.
412 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
413 * but we can't enforce that on users) */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000414 recursive_action("/sys/block",
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000415 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +0000416 fileAction, dirAction, temp, 0);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000417 recursive_action("/sys/class",
Denis Vlasenko67075bb2008-07-06 17:00:49 +0000418 ACTION_RECURSE | ACTION_FOLLOWLINKS,
Mike Frysingerb51fd352007-06-13 09:24:50 +0000419 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000420 } else {
Denis Vlasenko018bee62008-07-15 22:33:13 +0000421 char *seq;
422 char *action;
423 char *env_path;
424 char seqbuf[sizeof(int)*3 + 2];
425 int seqlen = seqlen; /* for compiler */
426
Mike Frysingerc881c732007-11-19 09:04:22 +0000427 /* Hotplug:
Denis Vlasenko018bee62008-07-15 22:33:13 +0000428 * env ACTION=... DEVPATH=... [SEQNUM=...] mdev
Mike Frysingerc881c732007-11-19 09:04:22 +0000429 * ACTION can be "add" or "remove"
430 * DEVPATH is like "/block/sda" or "/class/input/mice"
431 */
Rob Landley29e08ff2006-01-12 06:13:50 +0000432 action = getenv("ACTION");
433 env_path = getenv("DEVPATH");
Denis Vlasenko92758142006-10-03 19:56:34 +0000434 if (!action || !env_path)
Mike Frysingera421ba82006-02-03 00:25:37 +0000435 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000436
Denis Vlasenko018bee62008-07-15 22:33:13 +0000437 seq = getenv("SEQNUM");
438 if (seq) {
439 int timeout = 2000 / 32;
440 do {
441 seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1));
442 if (seqlen < 0)
443 break;
444 seqbuf[seqlen] = '\0';
445 if (seqbuf[0] == '\n' /* seed file? */
446 || strcmp(seq, seqbuf) == 0 /* correct idx? */
447 ) {
448 break;
449 }
450 usleep(32*1000);
451 } while (--timeout);
452 }
453
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000454 snprintf(temp, PATH_MAX, "/sys%s", env_path);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000455 if (!strcmp(action, "remove"))
456 make_device(temp, 1);
457 else if (!strcmp(action, "add")) {
458 make_device(temp, 0);
459
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000460 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
461 char *fw = getenv("FIRMWARE");
462 if (fw)
463 load_firmware(fw, temp);
464 }
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000465 }
Denis Vlasenko018bee62008-07-15 22:33:13 +0000466
467 if (seq && seqlen >= 0) {
468 xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1));
469 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000470 }
471
Mike Frysingerc881c732007-11-19 09:04:22 +0000472 if (ENABLE_FEATURE_CLEAN_UP)
473 RELEASE_CONFIG_BUFFER(temp);
474
Rob Landley70f7ef72005-12-13 08:21:33 +0000475 return 0;
476}