blob: 9c33feb3590df32fc21fe0b251f6a3351a8053da [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
Rob Landley70f7ef72005-12-13 08:21:33 +000024/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
Rob Landleyef10d522006-06-26 14:11:33 +000025static void make_device(char *path, int delete)
Rob Landley70f7ef72005-12-13 08:21:33 +000026{
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000027 const char *device_name;
Denis Vlasenkof46be092006-10-16 19:39:37 +000028 int major, minor, type, len;
Mike Frysingera421ba82006-02-03 00:25:37 +000029 int mode = 0660;
30 uid_t uid = 0;
31 gid_t gid = 0;
Rob Landley15fe2e12006-05-08 02:53:23 +000032 char *temp = path + strlen(path);
Rob Landleyef10d522006-06-26 14:11:33 +000033 char *command = NULL;
Mike Frysingerf0044c42008-02-01 06:53:50 +000034 char *alias = NULL;
35
36 /* Force the configuration file settings exactly. */
37 umask(0);
Rob Landley70f7ef72005-12-13 08:21:33 +000038
Rob Landley15fe2e12006-05-08 02:53:23 +000039 /* Try to read major/minor string. Note that the kernel puts \n after
40 * the data, so we don't need to worry about null terminating the string
41 * because sscanf() will stop at the first nondigit, which \n is. We
Mike Frysingerc881c732007-11-19 09:04:22 +000042 * also depend on path having writeable space after it.
43 */
Rob Landley10b36f92006-08-10 01:09:37 +000044 if (!delete) {
45 strcat(path, "/dev");
Denis Vlasenkoea620772006-10-14 02:23:43 +000046 len = open_read_close(path, temp + 1, 64);
Rob Landley10b36f92006-08-10 01:09:37 +000047 *temp++ = 0;
Mike Frysingerae86a332008-02-20 18:31:36 +000048 if (len < 1) {
49 if (ENABLE_FEATURE_MDEV_EXEC)
50 /* no "dev" file, so just try to run script */
51 *temp = 0;
52 else
53 return;
54 }
Rob Landley10b36f92006-08-10 01:09:37 +000055 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000056
Rob Landley70f7ef72005-12-13 08:21:33 +000057 /* Determine device name, type, major and minor */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000058 device_name = bb_basename(path);
Mike Frysingerc881c732007-11-19 09:04:22 +000059 type = (path[5] == 'c' ? S_IFCHR : S_IFBLK);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000060
Rob Landleyb56c2852005-12-17 10:52:30 +000061 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingerc881c732007-11-19 09:04:22 +000062 FILE *fp;
63 char *line, *vline;
Denis Vlasenkoee87ebf2007-12-21 22:18:16 +000064 unsigned lineno = 0;
Rob Landleyb56c2852005-12-17 10:52:30 +000065
Mike Frysingerc881c732007-11-19 09:04:22 +000066 /* If we have a config file, look up the user settings */
67 fp = fopen_or_warn("/etc/mdev.conf", "r");
68 if (!fp)
Denis Vlasenkof46be092006-10-16 19:39:37 +000069 goto end_parse;
Rob Landleyb56c2852005-12-17 10:52:30 +000070
Mike Frysingerc881c732007-11-19 09:04:22 +000071 while ((vline = line = xmalloc_getline(fp)) != NULL) {
Denis Vlasenkof46be092006-10-16 19:39:37 +000072 int field;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000073
Mike Frysingerc881c732007-11-19 09:04:22 +000074 /* A pristine copy for command execution. */
75 char *orig_line;
76 if (ENABLE_FEATURE_MDEV_EXEC)
77 orig_line = xstrdup(line);
78
79 ++lineno;
Rob Landleyb56c2852005-12-17 10:52:30 +000080
Denis Vlasenkof46be092006-10-16 19:39:37 +000081 /* Three fields: regex, uid:gid, mode */
Mike Frysingerf0044c42008-02-01 06:53:50 +000082 for (field = 0; field < (3 + ENABLE_FEATURE_MDEV_RENAME + ENABLE_FEATURE_MDEV_EXEC); ++field) {
Mike Frysingerc881c732007-11-19 09:04:22 +000083
84 /* Find a non-empty field */
85 char *val;
86 do {
87 val = strtok(vline, " \t");
88 vline = NULL;
89 } while (val && !*val);
Mike Frysinger46ef46a2008-01-23 18:48:26 +000090 if (!val) {
91 if (field)
92 break;
93 else
94 goto next_line;
95 }
Mike Frysingera421ba82006-02-03 00:25:37 +000096
Denis Vlasenkof46be092006-10-16 19:39:37 +000097 if (field == 0) {
Rob Landleyb56c2852005-12-17 10:52:30 +000098
Mike Frysingerc881c732007-11-19 09:04:22 +000099 /* Regex to match this device */
Denis Vlasenkof46be092006-10-16 19:39:37 +0000100 regex_t match;
101 regmatch_t off;
102 int result;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000103
Denis Vlasenkof46be092006-10-16 19:39:37 +0000104 /* Is this it? */
Mike Frysingerc881c732007-11-19 09:04:22 +0000105 xregcomp(&match, val, REG_EXTENDED);
Denis Vlasenkof46be092006-10-16 19:39:37 +0000106 result = regexec(&match, device_name, 1, &off, 0);
107 regfree(&match);
Rob Landleyb56c2852005-12-17 10:52:30 +0000108
Denis Vlasenkof46be092006-10-16 19:39:37 +0000109 /* If not this device, skip rest of line */
Mike Frysingerc881c732007-11-19 09:04:22 +0000110 if (result || off.rm_so || off.rm_eo != strlen(device_name))
111 goto next_line;
Denis Vlasenkof46be092006-10-16 19:39:37 +0000112
Mike Frysingerc881c732007-11-19 09:04:22 +0000113 } else if (field == 1) {
Denis Vlasenkof46be092006-10-16 19:39:37 +0000114
Mike Frysingerc881c732007-11-19 09:04:22 +0000115 /* uid:gid device ownership */
116 struct passwd *pass;
117 struct group *grp;
118
119 char *str_uid = val;
120 char *str_gid = strchr(val, ':');
121 if (str_gid)
122 *str_gid = '\0', ++str_gid;
Denis Vlasenkof46be092006-10-16 19:39:37 +0000123
124 /* Parse UID */
Mike Frysingerc881c732007-11-19 09:04:22 +0000125 pass = getpwnam(str_uid);
126 if (pass)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000127 uid = pass->pw_uid;
Mike Frysingerc881c732007-11-19 09:04:22 +0000128 else
129 uid = strtoul(str_uid, NULL, 10);
Denis Vlasenkof46be092006-10-16 19:39:37 +0000130
Mike Frysingerc881c732007-11-19 09:04:22 +0000131 /* parse GID */
132 grp = getgrnam(str_gid);
133 if (grp)
134 gid = grp->gr_gid;
135 else
136 gid = strtoul(str_gid, NULL, 10);
137
138 } else if (field == 2) {
139
140 /* Mode device permissions */
141 mode = strtoul(val, NULL, 8);
142
Mike Frysingerf0044c42008-02-01 06:53:50 +0000143 } else if (ENABLE_FEATURE_MDEV_RENAME && field == 3) {
144
145 if (*val != '>')
146 ++field;
147 else
148 alias = xstrdup(val + 1);
149
150 }
151
152 if (ENABLE_FEATURE_MDEV_EXEC && field == 3 + ENABLE_FEATURE_MDEV_RENAME) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000153
154 /* Optional command to run */
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000155 const char *s = "@$*";
Mike Frysingerc881c732007-11-19 09:04:22 +0000156 const char *s2 = strchr(s, *val);
157
Denis Vlasenkof46be092006-10-16 19:39:37 +0000158 if (!s2) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000159 /* Force error */
Denis Vlasenkof46be092006-10-16 19:39:37 +0000160 field = 1;
161 break;
162 }
Denis Vlasenkof46be092006-10-16 19:39:37 +0000163
Mike Frysingerc881c732007-11-19 09:04:22 +0000164 /* Correlate the position in the "@$*" with the delete
165 * step so that we get the proper behavior.
166 */
167 if ((s2 - s + 1) & (1 << delete))
168 command = xstrdup(orig_line + (val + 1 - line));
169 }
Rob Landleyb56c2852005-12-17 10:52:30 +0000170 }
Denis Vlasenkof46be092006-10-16 19:39:37 +0000171
172 /* Did everything parse happily? */
Mike Frysingerc881c732007-11-19 09:04:22 +0000173 if (field <= 2)
Denis Vlasenkoee87ebf2007-12-21 22:18:16 +0000174 bb_error_msg_and_die("bad line %u", lineno);
Denis Vlasenkof46be092006-10-16 19:39:37 +0000175
Mike Frysingerc881c732007-11-19 09:04:22 +0000176 next_line:
177 free(line);
178 if (ENABLE_FEATURE_MDEV_EXEC)
179 free(orig_line);
Rob Landleyb56c2852005-12-17 10:52:30 +0000180 }
Mike Frysingerc881c732007-11-19 09:04:22 +0000181
182 if (ENABLE_FEATURE_CLEAN_UP)
183 fclose(fp);
184
Denis Vlasenkof46be092006-10-16 19:39:37 +0000185 end_parse: /* nothing */ ;
Rob Landleyb56c2852005-12-17 10:52:30 +0000186 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000187
Rob Landleyef10d522006-06-26 14:11:33 +0000188 if (!delete) {
Mike Frysingerae86a332008-02-20 18:31:36 +0000189 if (sscanf(temp, "%d:%d", &major, &minor) != 2) {
190 if (ENABLE_FEATURE_MDEV_EXEC)
191 goto skip_creation;
192 else
193 return;
194 }
Mike Frysingerf0044c42008-02-01 06:53:50 +0000195
196 if (ENABLE_FEATURE_MDEV_RENAME)
197 unlink(device_name);
198
Rob Landleyef10d522006-06-26 14:11:33 +0000199 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000200 bb_perror_msg_and_die("mknod %s", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000201
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000202 if (major == root_major && minor == root_minor)
Rob Landleyef10d522006-06-26 14:11:33 +0000203 symlink(device_name, "root");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000204
Mike Frysingerf0044c42008-02-01 06:53:50 +0000205 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000206 chown(device_name, uid, gid);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000207
208 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
209 char *dest;
210
211 temp = strrchr(alias, '/');
212 if (temp) {
213 if (temp[1] != '\0')
214 /* given a file name, so rename it */
215 *temp = '\0';
216 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
217 dest = concat_path_file(alias, device_name);
218 } else
219 dest = alias;
220
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000221 rename(device_name, dest); // TODO: xrename?
Mike Frysingerf0044c42008-02-01 06:53:50 +0000222 symlink(dest, device_name);
223
224 if (alias != dest)
225 free(alias);
226 free(dest);
227 }
228 }
Mike Frysingerae86a332008-02-20 18:31:36 +0000229 skip_creation: /* nothing */ ;
Rob Landleyef10d522006-06-26 14:11:33 +0000230 }
Mike Frysingerf0044c42008-02-01 06:53:50 +0000231 if (ENABLE_FEATURE_MDEV_EXEC && command) {
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000232 /* setenv will leak memory, so use putenv */
233 char *s = xasprintf("MDEV=%s", device_name);
Rob Landleyef10d522006-06-26 14:11:33 +0000234 putenv(s);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000235 if (system(command) == -1)
236 bb_perror_msg_and_die("cannot run %s", command);
237 s[4] = '\0';
238 unsetenv(s);
Rob Landleyef10d522006-06-26 14:11:33 +0000239 free(s);
240 free(command);
Rob Landleyef10d522006-06-26 14:11:33 +0000241 }
Mike Frysingerc881c732007-11-19 09:04:22 +0000242 if (delete)
243 remove_file(device_name, FILEUTILS_FORCE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000244}
245
Mike Frysingerb51fd352007-06-13 09:24:50 +0000246/* File callback for /sys/ traversal */
247static int fileAction(const char *fileName, struct stat *statbuf,
248 void *userData, int depth)
Rob Landley70f7ef72005-12-13 08:21:33 +0000249{
Mike Frysingerb51fd352007-06-13 09:24:50 +0000250 size_t len = strlen(fileName) - 4;
251 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000252
Mike Frysingerb51fd352007-06-13 09:24:50 +0000253 if (strcmp(fileName + len, "/dev"))
254 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000255
Mike Frysingerb51fd352007-06-13 09:24:50 +0000256 strcpy(scratch, fileName);
257 scratch[len] = 0;
258 make_device(scratch, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000259
Mike Frysingerb51fd352007-06-13 09:24:50 +0000260 return TRUE;
261}
Rob Landley70f7ef72005-12-13 08:21:33 +0000262
Mike Frysingerb51fd352007-06-13 09:24:50 +0000263/* Directory callback for /sys/ traversal */
264static int dirAction(const char *fileName, struct stat *statbuf,
265 void *userData, int depth)
266{
267 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000268}
269
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000270/* For the full gory details, see linux/Documentation/firmware_class/README
271 *
272 * Firmware loading works like this:
273 * - kernel sets FIRMWARE env var
274 * - userspace checks /lib/firmware/$FIRMWARE
275 * - userspace waits for /sys/$DEVPATH/loading to appear
276 * - userspace writes "1" to /sys/$DEVPATH/loading
277 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
278 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
279 * - kernel loads firmware into device
280 */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000281static void load_firmware(const char *const firmware, const char *const sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000282{
283 int cnt;
284 int firmware_fd, loading_fd, data_fd;
285
286 /* check for $FIRMWARE from kernel */
287 /* XXX: dont bother: open(NULL) works same as open("no-such-file")
288 * if (!firmware)
289 * return;
290 */
291
292 /* check for /lib/firmware/$FIRMWARE */
293 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000294 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000295
296 /* in case we goto out ... */
297 data_fd = -1;
298
299 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
300 xchdir(sysfs_path);
301 for (cnt = 0; cnt < 30; ++cnt) {
302 loading_fd = open("loading", O_WRONLY);
303 if (loading_fd == -1)
304 sleep(1);
305 else
306 break;
307 }
308 if (loading_fd == -1)
309 goto out;
310
311 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
312 if (write(loading_fd, "1", 1) != 1)
313 goto out;
314
315 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
316 data_fd = open("data", O_WRONLY);
317 if (data_fd == -1)
318 goto out;
319 cnt = bb_copyfd_eof(firmware_fd, data_fd);
320
321 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
322 if (cnt > 0)
323 write(loading_fd, "0", 1);
324 else
325 write(loading_fd, "-1", 2);
326
327 out:
328 if (ENABLE_FEATURE_CLEAN_UP) {
329 close(firmware_fd);
330 close(loading_fd);
331 close(data_fd);
332 }
333}
334
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000335int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000336int mdev_main(int argc, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000337{
Rob Landley29e08ff2006-01-12 06:13:50 +0000338 char *action;
339 char *env_path;
340 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
341
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000342 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000343
Rob Landley29e08ff2006-01-12 06:13:50 +0000344 if (argc == 2 && !strcmp(argv[1],"-s")) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000345
346 /* Scan:
347 * mdev -s
348 */
349
Rob Landleya7e3d052006-02-21 06:11:13 +0000350 struct stat st;
351
Denis Vlasenkof46be092006-10-16 19:39:37 +0000352 xstat("/", &st);
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000353 root_major = major(st.st_dev);
354 root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000355
356 recursive_action("/sys/block",
357 ACTION_RECURSE | ACTION_FOLLOWLINKS,
358 fileAction, dirAction, temp, 0);
359
360 recursive_action("/sys/class",
361 ACTION_RECURSE | ACTION_FOLLOWLINKS,
362 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000363
Rob Landley29e08ff2006-01-12 06:13:50 +0000364 } else {
Mike Frysingerc881c732007-11-19 09:04:22 +0000365
366 /* Hotplug:
367 * env ACTION=... DEVPATH=... mdev
368 * ACTION can be "add" or "remove"
369 * DEVPATH is like "/block/sda" or "/class/input/mice"
370 */
371
Rob Landley29e08ff2006-01-12 06:13:50 +0000372 action = getenv("ACTION");
373 env_path = getenv("DEVPATH");
Denis Vlasenko92758142006-10-03 19:56:34 +0000374 if (!action || !env_path)
Mike Frysingera421ba82006-02-03 00:25:37 +0000375 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000376
Rob Landleyef10d522006-06-26 14:11:33 +0000377 sprintf(temp, "/sys%s", env_path);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000378 if (!strcmp(action, "remove"))
379 make_device(temp, 1);
380 else if (!strcmp(action, "add")) {
381 make_device(temp, 0);
382
383 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE)
384 load_firmware(getenv("FIRMWARE"), temp);
385 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000386 }
387
Mike Frysingerc881c732007-11-19 09:04:22 +0000388 if (ENABLE_FEATURE_CLEAN_UP)
389 RELEASE_CONFIG_BUFFER(temp);
390
Rob Landley70f7ef72005-12-13 08:21:33 +0000391 return 0;
392}