blob: 14aa593f558c26c6c2b76a0d5c32b0c882288893 [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
Mike Frysingerb51fd352007-06-13 09:24:50 +000022#define MAX_SYSFS_DEPTH 3 /* prevent infinite loops in /sys symlinks */
23
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000024/* We use additional 64+ bytes in make_device() */
25#define SCRATCH_SIZE 80
26
Denis Vlasenko44615642008-03-29 13:10:57 +000027static char *next_field(char *s)
28{
29 char *end = skip_non_whitespace(s);
30 s = skip_whitespace(end);
31 *end = '\0';
32 if (*s == '\0')
33 s = NULL;
34 return s;
35}
36
Rob Landley70f7ef72005-12-13 08:21:33 +000037/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000038/* NB: "mdev -s" may call us many times, do not leak memory/fds! */
Rob Landleyef10d522006-06-26 14:11:33 +000039static void make_device(char *path, int delete)
Rob Landley70f7ef72005-12-13 08:21:33 +000040{
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000041 const char *device_name;
Denis Vlasenkof46be092006-10-16 19:39:37 +000042 int major, minor, type, len;
Mike Frysingera421ba82006-02-03 00:25:37 +000043 int mode = 0660;
44 uid_t uid = 0;
45 gid_t gid = 0;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000046 char *dev_maj_min = path + strlen(path);
Rob Landleyef10d522006-06-26 14:11:33 +000047 char *command = NULL;
Mike Frysingerf0044c42008-02-01 06:53:50 +000048 char *alias = NULL;
49
50 /* Force the configuration file settings exactly. */
51 umask(0);
Rob Landley70f7ef72005-12-13 08:21:33 +000052
Rob Landley15fe2e12006-05-08 02:53:23 +000053 /* Try to read major/minor string. Note that the kernel puts \n after
54 * the data, so we don't need to worry about null terminating the string
55 * because sscanf() will stop at the first nondigit, which \n is. We
Mike Frysingerc881c732007-11-19 09:04:22 +000056 * also depend on path having writeable space after it.
57 */
Rob Landley10b36f92006-08-10 01:09:37 +000058 if (!delete) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000059 strcpy(dev_maj_min, "/dev");
60 len = open_read_close(path, dev_maj_min + 1, 64);
61 *dev_maj_min++ = '\0';
Mike Frysingerae86a332008-02-20 18:31:36 +000062 if (len < 1) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000063 if (!ENABLE_FEATURE_MDEV_EXEC)
Mike Frysingerae86a332008-02-20 18:31:36 +000064 return;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000065 /* no "dev" file, so just try to run script */
66 *dev_maj_min = '\0';
Mike Frysingerae86a332008-02-20 18:31:36 +000067 }
Rob Landley10b36f92006-08-10 01:09:37 +000068 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000069
Rob Landley70f7ef72005-12-13 08:21:33 +000070 /* Determine device name, type, major and minor */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000071 device_name = bb_basename(path);
Denis Vlasenkoaa8a6012008-03-29 16:07:46 +000072 /* http://kernel.org/doc/pending/hotplug.txt says that only
Denis Vlasenko4579e9c2008-05-08 21:49:28 +000073 * "/sys/block/..." is for block devices. "/sys/bus" etc is not!
74 * Since kernel 2.6.25 block devices are also in /sys/class/block. */
75 /* TODO: would it be acceptable to just use strstr(path, "/block/")? */
76 if (strncmp(&path[5], "class/block/"+6, 6) != 0
77 && strncmp(&path[5], "class/block/", 12) != 0)
78 type = S_IFCHR;
79 else
80 type = S_IFBLK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000081
Rob Landleyb56c2852005-12-17 10:52:30 +000082 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingerc881c732007-11-19 09:04:22 +000083 FILE *fp;
Denis Vlasenko44615642008-03-29 13:10:57 +000084 char *line, *val, *next;
Denis Vlasenkoee87ebf2007-12-21 22:18:16 +000085 unsigned lineno = 0;
Rob Landleyb56c2852005-12-17 10:52:30 +000086
Denis Vlasenko44615642008-03-29 13:10:57 +000087 /* If we have config file, look up user settings */
Mike Frysingerc881c732007-11-19 09:04:22 +000088 fp = fopen_or_warn("/etc/mdev.conf", "r");
89 if (!fp)
Denis Vlasenkof46be092006-10-16 19:39:37 +000090 goto end_parse;
Rob Landleyb56c2852005-12-17 10:52:30 +000091
Denis Vlasenko44615642008-03-29 13:10:57 +000092 while ((line = xmalloc_fgetline(fp)) != NULL) {
Denis Vlasenkof2f38682008-03-29 17:26:10 +000093 regmatch_t off[1+9*ENABLE_FEATURE_MDEV_RENAME_REGEXP];
94
Mike Frysingerc881c732007-11-19 09:04:22 +000095 ++lineno;
Denis Vlasenko44615642008-03-29 13:10:57 +000096 trim(line);
97 if (!line[0])
98 goto next_line;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000099
100 /* Fields: regex uid:gid mode [alias] [cmd] */
Mike Frysingerc881c732007-11-19 09:04:22 +0000101
Denis Vlasenko44615642008-03-29 13:10:57 +0000102 /* 1st field: regex to match this device */
103 next = next_field(line);
104 {
105 regex_t match;
Denis Vlasenko44615642008-03-29 13:10:57 +0000106 int result;
Mike Frysingera421ba82006-02-03 00:25:37 +0000107
Denis Vlasenko44615642008-03-29 13:10:57 +0000108 /* Is this it? */
109 xregcomp(&match, line, REG_EXTENDED);
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000110 result = regexec(&match, device_name, ARRAY_SIZE(off), off, 0);
Denis Vlasenko44615642008-03-29 13:10:57 +0000111 regfree(&match);
Rob Landleyb56c2852005-12-17 10:52:30 +0000112
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000113 //bb_error_msg("matches:");
114 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
115 // if (off[i].rm_so < 0) continue;
116 // bb_error_msg("match %d: '%.*s'\n", i,
117 // (int)(off[i].rm_eo - off[i].rm_so),
118 // device_name + off[i].rm_so);
119 //}
120
Denis Vlasenko44615642008-03-29 13:10:57 +0000121 /* If not this device, skip rest of line */
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000122 /* (regexec returns whole pattern as "range" 0) */
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000123 if (result || off[0].rm_so
124 || ((int)off[0].rm_eo != (int)strlen(device_name))
125 ) {
Denis Vlasenko44615642008-03-29 13:10:57 +0000126 goto next_line;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000127 }
Denis Vlasenko44615642008-03-29 13:10:57 +0000128 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000129
Denis Vlasenko44615642008-03-29 13:10:57 +0000130 /* This line matches: stop parsing the file
131 * after parsing the rest of fields */
Rob Landleyb56c2852005-12-17 10:52:30 +0000132
Denis Vlasenko44615642008-03-29 13:10:57 +0000133 /* 2nd field: uid:gid - device ownership */
134 if (!next) /* field must exist */
Denis Vlasenkoee87ebf2007-12-21 22:18:16 +0000135 bb_error_msg_and_die("bad line %u", lineno);
Denis Vlasenko44615642008-03-29 13:10:57 +0000136 val = next;
137 next = next_field(val);
138 {
139 struct passwd *pass;
140 struct group *grp;
141 char *str_uid = val;
142 char *str_gid = strchrnul(val, ':');
Denis Vlasenkof46be092006-10-16 19:39:37 +0000143
Denis Vlasenko44615642008-03-29 13:10:57 +0000144 if (*str_gid)
145 *str_gid++ = '\0';
146 /* Parse UID */
147 pass = getpwnam(str_uid);
148 if (pass)
149 uid = pass->pw_uid;
150 else
151 uid = strtoul(str_uid, NULL, 10);
152 /* Parse GID */
153 grp = getgrnam(str_gid);
154 if (grp)
155 gid = grp->gr_gid;
156 else
157 gid = strtoul(str_gid, NULL, 10);
158 }
159
160 /* 3rd field: mode - device permissions */
161 if (!next) /* field must exist */
162 bb_error_msg_and_die("bad line %u", lineno);
163 val = next;
164 next = next_field(val);
165 mode = strtoul(val, NULL, 8);
166
167 /* 4th field (opt): >alias */
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000168#if ENABLE_FEATURE_MDEV_RENAME
169 if (!next)
170 break;
171 if (*next == '>') {
172#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
173 char *s, *p;
174 unsigned i, n;
175
Denis Vlasenko44615642008-03-29 13:10:57 +0000176 val = next;
177 next = next_field(val);
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000178 /* substitute %1..9 with off[1..9], if any */
179 n = 0;
180 s = val;
181 while (*s && *s++ == '%')
182 n++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000183
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000184 p = alias = xzalloc(strlen(val) + n * strlen(device_name));
185 s = val + 1;
186 while (*s) {
187 *p = *s;
188 if ('%' == *s) {
189 i = (s[1] - '0');
190 if (i <= 9 && off[i].rm_so >= 0) {
191 n = off[i].rm_eo - off[i].rm_so;
192 strncpy(p, device_name + off[i].rm_so, n);
193 p += n - 1;
194 s++;
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000195 }
Denis Vlasenkof2f38682008-03-29 17:26:10 +0000196 }
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000197 p++;
198 s++;
Denis Vlasenko44615642008-03-29 13:10:57 +0000199 }
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000200#else
201 val = next;
202 next = next_field(val);
203 alias = xstrdup(val + 1);
204#endif
Denis Vlasenko44615642008-03-29 13:10:57 +0000205 }
Denis Vlasenko32dcc532008-05-02 13:21:24 +0000206#endif /* ENABLE_FEATURE_MDEV_RENAME */
Denis Vlasenko44615642008-03-29 13:10:57 +0000207
208 /* The rest (opt): command to run */
209 if (!next)
210 break;
211 val = next;
212 if (ENABLE_FEATURE_MDEV_EXEC) {
213 const char *s = "@$*";
214 const char *s2 = strchr(s, *val);
215
216 if (!s2)
217 bb_error_msg_and_die("bad line %u", lineno);
218
219 /* Correlate the position in the "@$*" with the delete
220 * step so that we get the proper behavior:
221 * @cmd: run on create
222 * $cmd: run on delete
223 * *cmd: run on both
224 */
225 if ((s2 - s + 1) /*1/2/3*/ & /*1/2*/ (1 + delete)) {
226 command = xstrdup(val + 1);
227 }
228 }
229 /* end of field parsing */
230 break; /* we found matching line, stop */
Mike Frysingerc881c732007-11-19 09:04:22 +0000231 next_line:
232 free(line);
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
Denis Vlasenko44615642008-03-29 13:10:57 +0000235 free(line); /* in case we used "break" to get here */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000236 fclose(fp);
Rob Landleyb56c2852005-12-17 10:52:30 +0000237 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000238 end_parse:
Rob Landley70f7ef72005-12-13 08:21:33 +0000239
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000240 if (!delete && sscanf(dev_maj_min, "%u:%u", &major, &minor) == 2) {
Mike Frysingerf0044c42008-02-01 06:53:50 +0000241
242 if (ENABLE_FEATURE_MDEV_RENAME)
243 unlink(device_name);
244
Rob Landleyef10d522006-06-26 14:11:33 +0000245 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000246 bb_perror_msg_and_die("mknod %s", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000247
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000248 if (major == root_major && minor == root_minor)
Rob Landleyef10d522006-06-26 14:11:33 +0000249 symlink(device_name, "root");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000250
Mike Frysingerf0044c42008-02-01 06:53:50 +0000251 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000252 chown(device_name, uid, gid);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000253
254 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
255 char *dest;
256
Denis Vlasenkocae11b52008-03-29 15:11:07 +0000257 /* ">bar/": rename to bar/device_name */
258 /* ">bar[/]baz": rename to bar[/]baz */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000259 dest = strrchr(alias, '/');
Denis Vlasenkocae11b52008-03-29 15:11:07 +0000260 if (dest) { /* ">bar/[baz]" ? */
261 *dest = '\0'; /* mkdir bar */
Mike Frysingerf0044c42008-02-01 06:53:50 +0000262 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
Denis Vlasenkocae11b52008-03-29 15:11:07 +0000263 *dest = '/';
264 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
265 dest = alias;
266 alias = concat_path_file(alias, device_name);
267 free(dest);
268 }
269 }
Mike Frysingerf0044c42008-02-01 06:53:50 +0000270
Denis Vlasenkocae11b52008-03-29 15:11:07 +0000271 /* recreate device_name as a symlink to moved device node */
272 if (rename(device_name, alias) == 0) {
273 symlink(alias, device_name);
274 }
Mike Frysingerf0044c42008-02-01 06:53:50 +0000275
Denis Vlasenkocae11b52008-03-29 15:11:07 +0000276 free(alias);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000277 }
278 }
Rob Landleyef10d522006-06-26 14:11:33 +0000279 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000280
Mike Frysingerf0044c42008-02-01 06:53:50 +0000281 if (ENABLE_FEATURE_MDEV_EXEC && command) {
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000282 /* setenv will leak memory, use putenv/unsetenv/free */
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000283 char *s = xasprintf("MDEV=%s", device_name);
Rob Landleyef10d522006-06-26 14:11:33 +0000284 putenv(s);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000285 if (system(command) == -1)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000286 bb_perror_msg_and_die("can't run '%s'", command);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000287 s[4] = '\0';
288 unsetenv(s);
Rob Landleyef10d522006-06-26 14:11:33 +0000289 free(s);
290 free(command);
Rob Landleyef10d522006-06-26 14:11:33 +0000291 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000292
Mike Frysingerc881c732007-11-19 09:04:22 +0000293 if (delete)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000294 unlink(device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000295}
296
Mike Frysingerb51fd352007-06-13 09:24:50 +0000297/* File callback for /sys/ traversal */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000298static int fileAction(const char *fileName,
299 struct stat *statbuf ATTRIBUTE_UNUSED,
300 void *userData,
301 int depth ATTRIBUTE_UNUSED)
Rob Landley70f7ef72005-12-13 08:21:33 +0000302{
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000303 size_t len = strlen(fileName) - 4; /* can't underflow */
Mike Frysingerb51fd352007-06-13 09:24:50 +0000304 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000305
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000306 /* len check is for paranoid reasons */
307 if (strcmp(fileName + len, "/dev") || len >= PATH_MAX)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000308 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000309
Mike Frysingerb51fd352007-06-13 09:24:50 +0000310 strcpy(scratch, fileName);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000311 scratch[len] = '\0';
Mike Frysingerb51fd352007-06-13 09:24:50 +0000312 make_device(scratch, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000313
Mike Frysingerb51fd352007-06-13 09:24:50 +0000314 return TRUE;
315}
Rob Landley70f7ef72005-12-13 08:21:33 +0000316
Mike Frysingerb51fd352007-06-13 09:24:50 +0000317/* Directory callback for /sys/ traversal */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000318static int dirAction(const char *fileName ATTRIBUTE_UNUSED,
319 struct stat *statbuf ATTRIBUTE_UNUSED,
320 void *userData ATTRIBUTE_UNUSED,
321 int depth)
Mike Frysingerb51fd352007-06-13 09:24:50 +0000322{
323 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000324}
325
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000326/* For the full gory details, see linux/Documentation/firmware_class/README
327 *
328 * Firmware loading works like this:
329 * - kernel sets FIRMWARE env var
330 * - userspace checks /lib/firmware/$FIRMWARE
331 * - userspace waits for /sys/$DEVPATH/loading to appear
332 * - userspace writes "1" to /sys/$DEVPATH/loading
333 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
334 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
335 * - kernel loads firmware into device
336 */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000337static void load_firmware(const char *const firmware, const char *const sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000338{
339 int cnt;
340 int firmware_fd, loading_fd, data_fd;
341
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000342 /* check for /lib/firmware/$FIRMWARE */
343 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000344 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000345
346 /* in case we goto out ... */
347 data_fd = -1;
348
349 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
350 xchdir(sysfs_path);
351 for (cnt = 0; cnt < 30; ++cnt) {
352 loading_fd = open("loading", O_WRONLY);
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000353 if (loading_fd != -1)
354 goto loading;
355 sleep(1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000356 }
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000357 goto out;
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000358
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000359 loading:
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000360 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000361 if (full_write(loading_fd, "1", 1) != 1)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000362 goto out;
363
364 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
365 data_fd = open("data", O_WRONLY);
366 if (data_fd == -1)
367 goto out;
368 cnt = bb_copyfd_eof(firmware_fd, data_fd);
369
370 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
371 if (cnt > 0)
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000372 full_write(loading_fd, "0", 1);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000373 else
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000374 full_write(loading_fd, "-1", 2);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000375
376 out:
377 if (ENABLE_FEATURE_CLEAN_UP) {
378 close(firmware_fd);
379 close(loading_fd);
380 close(data_fd);
381 }
382}
383
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000384int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000385int mdev_main(int argc, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000386{
Rob Landley29e08ff2006-01-12 06:13:50 +0000387 char *action;
388 char *env_path;
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000389 RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
Rob Landley29e08ff2006-01-12 06:13:50 +0000390
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000391 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000392
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000393 if (argc == 2 && !strcmp(argv[1], "-s")) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000394 /* Scan:
395 * mdev -s
396 */
Rob Landleya7e3d052006-02-21 06:11:13 +0000397 struct stat st;
398
Denis Vlasenkof46be092006-10-16 19:39:37 +0000399 xstat("/", &st);
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000400 root_major = major(st.st_dev);
401 root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000402
403 recursive_action("/sys/block",
404 ACTION_RECURSE | ACTION_FOLLOWLINKS,
405 fileAction, dirAction, temp, 0);
406
407 recursive_action("/sys/class",
408 ACTION_RECURSE | ACTION_FOLLOWLINKS,
409 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000410
Rob Landley29e08ff2006-01-12 06:13:50 +0000411 } else {
Mike Frysingerc881c732007-11-19 09:04:22 +0000412 /* Hotplug:
413 * env ACTION=... DEVPATH=... mdev
414 * ACTION can be "add" or "remove"
415 * DEVPATH is like "/block/sda" or "/class/input/mice"
416 */
Rob Landley29e08ff2006-01-12 06:13:50 +0000417 action = getenv("ACTION");
418 env_path = getenv("DEVPATH");
Denis Vlasenko92758142006-10-03 19:56:34 +0000419 if (!action || !env_path)
Mike Frysingera421ba82006-02-03 00:25:37 +0000420 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000421
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000422 snprintf(temp, PATH_MAX, "/sys%s", env_path);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000423 if (!strcmp(action, "remove"))
424 make_device(temp, 1);
425 else if (!strcmp(action, "add")) {
426 make_device(temp, 0);
427
Denis Vlasenkocf26ab72008-03-27 22:45:44 +0000428 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
429 char *fw = getenv("FIRMWARE");
430 if (fw)
431 load_firmware(fw, temp);
432 }
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000433 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000434 }
435
Mike Frysingerc881c732007-11-19 09:04:22 +0000436 if (ENABLE_FEATURE_CLEAN_UP)
437 RELEASE_CONFIG_BUFFER(temp);
438
Rob Landley70f7ef72005-12-13 08:21:33 +0000439 return 0;
440}