blob: a18fc0916dd7fa8c83d2863d90b6b3cba4ea94ab [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 Frysingerc881c732007-11-19 09:04:22 +000048 if (len < 1)
49 return;
Rob Landley10b36f92006-08-10 01:09:37 +000050 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000051
Rob Landley70f7ef72005-12-13 08:21:33 +000052 /* Determine device name, type, major and minor */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000053 device_name = bb_basename(path);
Mike Frysingerc881c732007-11-19 09:04:22 +000054 type = (path[5] == 'c' ? S_IFCHR : S_IFBLK);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000055
Rob Landleyb56c2852005-12-17 10:52:30 +000056 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingerc881c732007-11-19 09:04:22 +000057 FILE *fp;
58 char *line, *vline;
Denis Vlasenkoee87ebf2007-12-21 22:18:16 +000059 unsigned lineno = 0;
Rob Landleyb56c2852005-12-17 10:52:30 +000060
Mike Frysingerc881c732007-11-19 09:04:22 +000061 /* If we have a config file, look up the user settings */
62 fp = fopen_or_warn("/etc/mdev.conf", "r");
63 if (!fp)
Denis Vlasenkof46be092006-10-16 19:39:37 +000064 goto end_parse;
Rob Landleyb56c2852005-12-17 10:52:30 +000065
Mike Frysingerc881c732007-11-19 09:04:22 +000066 while ((vline = line = xmalloc_getline(fp)) != NULL) {
Denis Vlasenkof46be092006-10-16 19:39:37 +000067 int field;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000068
Mike Frysingerc881c732007-11-19 09:04:22 +000069 /* A pristine copy for command execution. */
70 char *orig_line;
71 if (ENABLE_FEATURE_MDEV_EXEC)
72 orig_line = xstrdup(line);
73
74 ++lineno;
Rob Landleyb56c2852005-12-17 10:52:30 +000075
Denis Vlasenkof46be092006-10-16 19:39:37 +000076 /* Three fields: regex, uid:gid, mode */
Mike Frysingerf0044c42008-02-01 06:53:50 +000077 for (field = 0; field < (3 + ENABLE_FEATURE_MDEV_RENAME + ENABLE_FEATURE_MDEV_EXEC); ++field) {
Mike Frysingerc881c732007-11-19 09:04:22 +000078
79 /* Find a non-empty field */
80 char *val;
81 do {
82 val = strtok(vline, " \t");
83 vline = NULL;
84 } while (val && !*val);
Mike Frysinger46ef46a2008-01-23 18:48:26 +000085 if (!val) {
86 if (field)
87 break;
88 else
89 goto next_line;
90 }
Mike Frysingera421ba82006-02-03 00:25:37 +000091
Denis Vlasenkof46be092006-10-16 19:39:37 +000092 if (field == 0) {
Rob Landleyb56c2852005-12-17 10:52:30 +000093
Mike Frysingerc881c732007-11-19 09:04:22 +000094 /* Regex to match this device */
Denis Vlasenkof46be092006-10-16 19:39:37 +000095 regex_t match;
96 regmatch_t off;
97 int result;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000098
Denis Vlasenkof46be092006-10-16 19:39:37 +000099 /* Is this it? */
Mike Frysingerc881c732007-11-19 09:04:22 +0000100 xregcomp(&match, val, REG_EXTENDED);
Denis Vlasenkof46be092006-10-16 19:39:37 +0000101 result = regexec(&match, device_name, 1, &off, 0);
102 regfree(&match);
Rob Landleyb56c2852005-12-17 10:52:30 +0000103
Denis Vlasenkof46be092006-10-16 19:39:37 +0000104 /* If not this device, skip rest of line */
Mike Frysingerc881c732007-11-19 09:04:22 +0000105 if (result || off.rm_so || off.rm_eo != strlen(device_name))
106 goto next_line;
Denis Vlasenkof46be092006-10-16 19:39:37 +0000107
Mike Frysingerc881c732007-11-19 09:04:22 +0000108 } else if (field == 1) {
Denis Vlasenkof46be092006-10-16 19:39:37 +0000109
Mike Frysingerc881c732007-11-19 09:04:22 +0000110 /* uid:gid device ownership */
111 struct passwd *pass;
112 struct group *grp;
113
114 char *str_uid = val;
115 char *str_gid = strchr(val, ':');
116 if (str_gid)
117 *str_gid = '\0', ++str_gid;
Denis Vlasenkof46be092006-10-16 19:39:37 +0000118
119 /* Parse UID */
Mike Frysingerc881c732007-11-19 09:04:22 +0000120 pass = getpwnam(str_uid);
121 if (pass)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000122 uid = pass->pw_uid;
Mike Frysingerc881c732007-11-19 09:04:22 +0000123 else
124 uid = strtoul(str_uid, NULL, 10);
Denis Vlasenkof46be092006-10-16 19:39:37 +0000125
Mike Frysingerc881c732007-11-19 09:04:22 +0000126 /* parse GID */
127 grp = getgrnam(str_gid);
128 if (grp)
129 gid = grp->gr_gid;
130 else
131 gid = strtoul(str_gid, NULL, 10);
132
133 } else if (field == 2) {
134
135 /* Mode device permissions */
136 mode = strtoul(val, NULL, 8);
137
Mike Frysingerf0044c42008-02-01 06:53:50 +0000138 } else if (ENABLE_FEATURE_MDEV_RENAME && field == 3) {
139
140 if (*val != '>')
141 ++field;
142 else
143 alias = xstrdup(val + 1);
144
145 }
146
147 if (ENABLE_FEATURE_MDEV_EXEC && field == 3 + ENABLE_FEATURE_MDEV_RENAME) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000148
149 /* Optional command to run */
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000150 const char *s = "@$*";
Mike Frysingerc881c732007-11-19 09:04:22 +0000151 const char *s2 = strchr(s, *val);
152
Denis Vlasenkof46be092006-10-16 19:39:37 +0000153 if (!s2) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000154 /* Force error */
Denis Vlasenkof46be092006-10-16 19:39:37 +0000155 field = 1;
156 break;
157 }
Denis Vlasenkof46be092006-10-16 19:39:37 +0000158
Mike Frysingerc881c732007-11-19 09:04:22 +0000159 /* Correlate the position in the "@$*" with the delete
160 * step so that we get the proper behavior.
161 */
162 if ((s2 - s + 1) & (1 << delete))
163 command = xstrdup(orig_line + (val + 1 - line));
164 }
Rob Landleyb56c2852005-12-17 10:52:30 +0000165 }
Denis Vlasenkof46be092006-10-16 19:39:37 +0000166
167 /* Did everything parse happily? */
Mike Frysingerc881c732007-11-19 09:04:22 +0000168 if (field <= 2)
Denis Vlasenkoee87ebf2007-12-21 22:18:16 +0000169 bb_error_msg_and_die("bad line %u", lineno);
Denis Vlasenkof46be092006-10-16 19:39:37 +0000170
Mike Frysingerc881c732007-11-19 09:04:22 +0000171 next_line:
172 free(line);
173 if (ENABLE_FEATURE_MDEV_EXEC)
174 free(orig_line);
Rob Landleyb56c2852005-12-17 10:52:30 +0000175 }
Mike Frysingerc881c732007-11-19 09:04:22 +0000176
177 if (ENABLE_FEATURE_CLEAN_UP)
178 fclose(fp);
179
Denis Vlasenkof46be092006-10-16 19:39:37 +0000180 end_parse: /* nothing */ ;
Rob Landleyb56c2852005-12-17 10:52:30 +0000181 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000182
Rob Landleyef10d522006-06-26 14:11:33 +0000183 if (!delete) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000184 if (sscanf(temp, "%d:%d", &major, &minor) != 2)
185 return;
Mike Frysingerf0044c42008-02-01 06:53:50 +0000186
187 if (ENABLE_FEATURE_MDEV_RENAME)
188 unlink(device_name);
189
Rob Landleyef10d522006-06-26 14:11:33 +0000190 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000191 bb_perror_msg_and_die("mknod %s", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000192
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000193 if (major == root_major && minor == root_minor)
Rob Landleyef10d522006-06-26 14:11:33 +0000194 symlink(device_name, "root");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000195
Mike Frysingerf0044c42008-02-01 06:53:50 +0000196 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000197 chown(device_name, uid, gid);
Mike Frysingerf0044c42008-02-01 06:53:50 +0000198
199 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
200 char *dest;
201
202 temp = strrchr(alias, '/');
203 if (temp) {
204 if (temp[1] != '\0')
205 /* given a file name, so rename it */
206 *temp = '\0';
207 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
208 dest = concat_path_file(alias, device_name);
209 } else
210 dest = alias;
211
212 rename(device_name, dest);
213 symlink(dest, device_name);
214
215 if (alias != dest)
216 free(alias);
217 free(dest);
218 }
219 }
Rob Landleyef10d522006-06-26 14:11:33 +0000220 }
Mike Frysingerf0044c42008-02-01 06:53:50 +0000221 if (ENABLE_FEATURE_MDEV_EXEC && command) {
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000222 /* setenv will leak memory, so use putenv */
223 char *s = xasprintf("MDEV=%s", device_name);
Rob Landleyef10d522006-06-26 14:11:33 +0000224 putenv(s);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000225 if (system(command) == -1)
226 bb_perror_msg_and_die("cannot run %s", command);
227 s[4] = '\0';
228 unsetenv(s);
Rob Landleyef10d522006-06-26 14:11:33 +0000229 free(s);
230 free(command);
Rob Landleyef10d522006-06-26 14:11:33 +0000231 }
Mike Frysingerc881c732007-11-19 09:04:22 +0000232 if (delete)
233 remove_file(device_name, FILEUTILS_FORCE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000234}
235
Mike Frysingerb51fd352007-06-13 09:24:50 +0000236/* File callback for /sys/ traversal */
237static int fileAction(const char *fileName, struct stat *statbuf,
238 void *userData, int depth)
Rob Landley70f7ef72005-12-13 08:21:33 +0000239{
Mike Frysingerb51fd352007-06-13 09:24:50 +0000240 size_t len = strlen(fileName) - 4;
241 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000242
Mike Frysingerb51fd352007-06-13 09:24:50 +0000243 if (strcmp(fileName + len, "/dev"))
244 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000245
Mike Frysingerb51fd352007-06-13 09:24:50 +0000246 strcpy(scratch, fileName);
247 scratch[len] = 0;
248 make_device(scratch, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000249
Mike Frysingerb51fd352007-06-13 09:24:50 +0000250 return TRUE;
251}
Rob Landley70f7ef72005-12-13 08:21:33 +0000252
Mike Frysingerb51fd352007-06-13 09:24:50 +0000253/* Directory callback for /sys/ traversal */
254static int dirAction(const char *fileName, struct stat *statbuf,
255 void *userData, int depth)
256{
257 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000258}
259
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000260/* For the full gory details, see linux/Documentation/firmware_class/README
261 *
262 * Firmware loading works like this:
263 * - kernel sets FIRMWARE env var
264 * - userspace checks /lib/firmware/$FIRMWARE
265 * - userspace waits for /sys/$DEVPATH/loading to appear
266 * - userspace writes "1" to /sys/$DEVPATH/loading
267 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
268 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
269 * - kernel loads firmware into device
270 */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000271static void load_firmware(const char *const firmware, const char *const sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000272{
273 int cnt;
274 int firmware_fd, loading_fd, data_fd;
275
276 /* check for $FIRMWARE from kernel */
277 /* XXX: dont bother: open(NULL) works same as open("no-such-file")
278 * if (!firmware)
279 * return;
280 */
281
282 /* check for /lib/firmware/$FIRMWARE */
283 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000284 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000285
286 /* in case we goto out ... */
287 data_fd = -1;
288
289 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
290 xchdir(sysfs_path);
291 for (cnt = 0; cnt < 30; ++cnt) {
292 loading_fd = open("loading", O_WRONLY);
293 if (loading_fd == -1)
294 sleep(1);
295 else
296 break;
297 }
298 if (loading_fd == -1)
299 goto out;
300
301 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
302 if (write(loading_fd, "1", 1) != 1)
303 goto out;
304
305 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
306 data_fd = open("data", O_WRONLY);
307 if (data_fd == -1)
308 goto out;
309 cnt = bb_copyfd_eof(firmware_fd, data_fd);
310
311 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
312 if (cnt > 0)
313 write(loading_fd, "0", 1);
314 else
315 write(loading_fd, "-1", 2);
316
317 out:
318 if (ENABLE_FEATURE_CLEAN_UP) {
319 close(firmware_fd);
320 close(loading_fd);
321 close(data_fd);
322 }
323}
324
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000325int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000326int mdev_main(int argc, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000327{
Rob Landley29e08ff2006-01-12 06:13:50 +0000328 char *action;
329 char *env_path;
330 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
331
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000332 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000333
Rob Landley29e08ff2006-01-12 06:13:50 +0000334 if (argc == 2 && !strcmp(argv[1],"-s")) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000335
336 /* Scan:
337 * mdev -s
338 */
339
Rob Landleya7e3d052006-02-21 06:11:13 +0000340 struct stat st;
341
Denis Vlasenkof46be092006-10-16 19:39:37 +0000342 xstat("/", &st);
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000343 root_major = major(st.st_dev);
344 root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000345
346 recursive_action("/sys/block",
347 ACTION_RECURSE | ACTION_FOLLOWLINKS,
348 fileAction, dirAction, temp, 0);
349
350 recursive_action("/sys/class",
351 ACTION_RECURSE | ACTION_FOLLOWLINKS,
352 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000353
Rob Landley29e08ff2006-01-12 06:13:50 +0000354 } else {
Mike Frysingerc881c732007-11-19 09:04:22 +0000355
356 /* Hotplug:
357 * env ACTION=... DEVPATH=... mdev
358 * ACTION can be "add" or "remove"
359 * DEVPATH is like "/block/sda" or "/class/input/mice"
360 */
361
Rob Landley29e08ff2006-01-12 06:13:50 +0000362 action = getenv("ACTION");
363 env_path = getenv("DEVPATH");
Denis Vlasenko92758142006-10-03 19:56:34 +0000364 if (!action || !env_path)
Mike Frysingera421ba82006-02-03 00:25:37 +0000365 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000366
Rob Landleyef10d522006-06-26 14:11:33 +0000367 sprintf(temp, "/sys%s", env_path);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000368 if (!strcmp(action, "remove"))
369 make_device(temp, 1);
370 else if (!strcmp(action, "add")) {
371 make_device(temp, 0);
372
373 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE)
374 load_firmware(getenv("FIRMWARE"), temp);
375 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000376 }
377
Mike Frysingerc881c732007-11-19 09:04:22 +0000378 if (ENABLE_FEATURE_CLEAN_UP)
379 RELEASE_CONFIG_BUFFER(temp);
380
Rob Landley70f7ef72005-12-13 08:21:33 +0000381 return 0;
382}