blob: 84d5e1c0ed34b5092455a6ae8e468e9991f16f9c [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;
Rob Landley70f7ef72005-12-13 08:21:33 +000034
Rob Landley15fe2e12006-05-08 02:53:23 +000035 /* Try to read major/minor string. Note that the kernel puts \n after
36 * the data, so we don't need to worry about null terminating the string
37 * because sscanf() will stop at the first nondigit, which \n is. We
Mike Frysingerc881c732007-11-19 09:04:22 +000038 * also depend on path having writeable space after it.
39 */
Rob Landley10b36f92006-08-10 01:09:37 +000040 if (!delete) {
41 strcat(path, "/dev");
Denis Vlasenkoea620772006-10-14 02:23:43 +000042 len = open_read_close(path, temp + 1, 64);
Rob Landley10b36f92006-08-10 01:09:37 +000043 *temp++ = 0;
Mike Frysingerc881c732007-11-19 09:04:22 +000044 if (len < 1)
45 return;
Rob Landley10b36f92006-08-10 01:09:37 +000046 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000047
Rob Landley70f7ef72005-12-13 08:21:33 +000048 /* Determine device name, type, major and minor */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +000049 device_name = bb_basename(path);
Mike Frysingerc881c732007-11-19 09:04:22 +000050 type = (path[5] == 'c' ? S_IFCHR : S_IFBLK);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000051
Rob Landleyb56c2852005-12-17 10:52:30 +000052 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingerc881c732007-11-19 09:04:22 +000053 FILE *fp;
54 char *line, *vline;
Denis Vlasenkoee87ebf2007-12-21 22:18:16 +000055 unsigned lineno = 0;
Rob Landleyb56c2852005-12-17 10:52:30 +000056
Mike Frysingerc881c732007-11-19 09:04:22 +000057 /* If we have a config file, look up the user settings */
58 fp = fopen_or_warn("/etc/mdev.conf", "r");
59 if (!fp)
Denis Vlasenkof46be092006-10-16 19:39:37 +000060 goto end_parse;
Rob Landleyb56c2852005-12-17 10:52:30 +000061
Mike Frysingerc881c732007-11-19 09:04:22 +000062 while ((vline = line = xmalloc_getline(fp)) != NULL) {
Denis Vlasenkof46be092006-10-16 19:39:37 +000063 int field;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000064
Mike Frysingerc881c732007-11-19 09:04:22 +000065 /* A pristine copy for command execution. */
66 char *orig_line;
67 if (ENABLE_FEATURE_MDEV_EXEC)
68 orig_line = xstrdup(line);
69
70 ++lineno;
Rob Landleyb56c2852005-12-17 10:52:30 +000071
Denis Vlasenkof46be092006-10-16 19:39:37 +000072 /* Three fields: regex, uid:gid, mode */
Mike Frysingerc881c732007-11-19 09:04:22 +000073 for (field = 0; field < (3 + ENABLE_FEATURE_MDEV_EXEC); ++field) {
74
75 /* Find a non-empty field */
76 char *val;
77 do {
78 val = strtok(vline, " \t");
79 vline = NULL;
80 } while (val && !*val);
81 if (!val)
82 break;
Mike Frysingera421ba82006-02-03 00:25:37 +000083
Denis Vlasenkof46be092006-10-16 19:39:37 +000084 if (field == 0) {
Rob Landleyb56c2852005-12-17 10:52:30 +000085
Mike Frysingerc881c732007-11-19 09:04:22 +000086 /* Regex to match this device */
Denis Vlasenkof46be092006-10-16 19:39:37 +000087 regex_t match;
88 regmatch_t off;
89 int result;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000090
Denis Vlasenkof46be092006-10-16 19:39:37 +000091 /* Is this it? */
Mike Frysingerc881c732007-11-19 09:04:22 +000092 xregcomp(&match, val, REG_EXTENDED);
Denis Vlasenkof46be092006-10-16 19:39:37 +000093 result = regexec(&match, device_name, 1, &off, 0);
94 regfree(&match);
Rob Landleyb56c2852005-12-17 10:52:30 +000095
Denis Vlasenkof46be092006-10-16 19:39:37 +000096 /* If not this device, skip rest of line */
Mike Frysingerc881c732007-11-19 09:04:22 +000097 if (result || off.rm_so || off.rm_eo != strlen(device_name))
98 goto next_line;
Denis Vlasenkof46be092006-10-16 19:39:37 +000099
Mike Frysingerc881c732007-11-19 09:04:22 +0000100 } else if (field == 1) {
Denis Vlasenkof46be092006-10-16 19:39:37 +0000101
Mike Frysingerc881c732007-11-19 09:04:22 +0000102 /* uid:gid device ownership */
103 struct passwd *pass;
104 struct group *grp;
105
106 char *str_uid = val;
107 char *str_gid = strchr(val, ':');
108 if (str_gid)
109 *str_gid = '\0', ++str_gid;
Denis Vlasenkof46be092006-10-16 19:39:37 +0000110
111 /* Parse UID */
Mike Frysingerc881c732007-11-19 09:04:22 +0000112 pass = getpwnam(str_uid);
113 if (pass)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000114 uid = pass->pw_uid;
Mike Frysingerc881c732007-11-19 09:04:22 +0000115 else
116 uid = strtoul(str_uid, NULL, 10);
Denis Vlasenkof46be092006-10-16 19:39:37 +0000117
Mike Frysingerc881c732007-11-19 09:04:22 +0000118 /* parse GID */
119 grp = getgrnam(str_gid);
120 if (grp)
121 gid = grp->gr_gid;
122 else
123 gid = strtoul(str_gid, NULL, 10);
124
125 } else if (field == 2) {
126
127 /* Mode device permissions */
128 mode = strtoul(val, NULL, 8);
129
130 } else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
131
132 /* Optional command to run */
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000133 const char *s = "@$*";
Mike Frysingerc881c732007-11-19 09:04:22 +0000134 const char *s2 = strchr(s, *val);
135
Denis Vlasenkof46be092006-10-16 19:39:37 +0000136 if (!s2) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000137 /* Force error */
Denis Vlasenkof46be092006-10-16 19:39:37 +0000138 field = 1;
139 break;
140 }
Denis Vlasenkof46be092006-10-16 19:39:37 +0000141
Mike Frysingerc881c732007-11-19 09:04:22 +0000142 /* Correlate the position in the "@$*" with the delete
143 * step so that we get the proper behavior.
144 */
145 if ((s2 - s + 1) & (1 << delete))
146 command = xstrdup(orig_line + (val + 1 - line));
147 }
Rob Landleyb56c2852005-12-17 10:52:30 +0000148 }
Denis Vlasenkof46be092006-10-16 19:39:37 +0000149
150 /* Did everything parse happily? */
Mike Frysingerc881c732007-11-19 09:04:22 +0000151 if (field <= 2)
Denis Vlasenkoee87ebf2007-12-21 22:18:16 +0000152 bb_error_msg_and_die("bad line %u", lineno);
Denis Vlasenkof46be092006-10-16 19:39:37 +0000153
Mike Frysingerc881c732007-11-19 09:04:22 +0000154 next_line:
155 free(line);
156 if (ENABLE_FEATURE_MDEV_EXEC)
157 free(orig_line);
Rob Landleyb56c2852005-12-17 10:52:30 +0000158 }
Mike Frysingerc881c732007-11-19 09:04:22 +0000159
160 if (ENABLE_FEATURE_CLEAN_UP)
161 fclose(fp);
162
Denis Vlasenkof46be092006-10-16 19:39:37 +0000163 end_parse: /* nothing */ ;
Rob Landleyb56c2852005-12-17 10:52:30 +0000164 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000165
Rob Landleyb56c2852005-12-17 10:52:30 +0000166 umask(0);
Rob Landleyef10d522006-06-26 14:11:33 +0000167 if (!delete) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000168 if (sscanf(temp, "%d:%d", &major, &minor) != 2)
169 return;
Rob Landleyef10d522006-06-26 14:11:33 +0000170 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000171 bb_perror_msg_and_die("mknod %s", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000172
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000173 if (major == root_major && minor == root_minor)
Rob Landleyef10d522006-06-26 14:11:33 +0000174 symlink(device_name, "root");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000175
Mike Frysingerc881c732007-11-19 09:04:22 +0000176 if (ENABLE_FEATURE_MDEV_CONF)
177 chown(device_name, uid, gid);
Rob Landleyef10d522006-06-26 14:11:33 +0000178 }
179 if (command) {
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000180 /* setenv will leak memory, so use putenv */
181 char *s = xasprintf("MDEV=%s", device_name);
Rob Landleyef10d522006-06-26 14:11:33 +0000182 putenv(s);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000183 if (system(command) == -1)
184 bb_perror_msg_and_die("cannot run %s", command);
185 s[4] = '\0';
186 unsetenv(s);
Rob Landleyef10d522006-06-26 14:11:33 +0000187 free(s);
188 free(command);
Rob Landleyef10d522006-06-26 14:11:33 +0000189 }
Mike Frysingerc881c732007-11-19 09:04:22 +0000190 if (delete)
191 remove_file(device_name, FILEUTILS_FORCE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000192}
193
Mike Frysingerb51fd352007-06-13 09:24:50 +0000194/* File callback for /sys/ traversal */
195static int fileAction(const char *fileName, struct stat *statbuf,
196 void *userData, int depth)
Rob Landley70f7ef72005-12-13 08:21:33 +0000197{
Mike Frysingerb51fd352007-06-13 09:24:50 +0000198 size_t len = strlen(fileName) - 4;
199 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000200
Mike Frysingerb51fd352007-06-13 09:24:50 +0000201 if (strcmp(fileName + len, "/dev"))
202 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000203
Mike Frysingerb51fd352007-06-13 09:24:50 +0000204 strcpy(scratch, fileName);
205 scratch[len] = 0;
206 make_device(scratch, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000207
Mike Frysingerb51fd352007-06-13 09:24:50 +0000208 return TRUE;
209}
Rob Landley70f7ef72005-12-13 08:21:33 +0000210
Mike Frysingerb51fd352007-06-13 09:24:50 +0000211/* Directory callback for /sys/ traversal */
212static int dirAction(const char *fileName, struct stat *statbuf,
213 void *userData, int depth)
214{
215 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000216}
217
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000218/* For the full gory details, see linux/Documentation/firmware_class/README
219 *
220 * Firmware loading works like this:
221 * - kernel sets FIRMWARE env var
222 * - userspace checks /lib/firmware/$FIRMWARE
223 * - userspace waits for /sys/$DEVPATH/loading to appear
224 * - userspace writes "1" to /sys/$DEVPATH/loading
225 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
226 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
227 * - kernel loads firmware into device
228 */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000229static void load_firmware(const char *const firmware, const char *const sysfs_path)
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000230{
231 int cnt;
232 int firmware_fd, loading_fd, data_fd;
233
234 /* check for $FIRMWARE from kernel */
235 /* XXX: dont bother: open(NULL) works same as open("no-such-file")
236 * if (!firmware)
237 * return;
238 */
239
240 /* check for /lib/firmware/$FIRMWARE */
241 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000242 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000243
244 /* in case we goto out ... */
245 data_fd = -1;
246
247 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
248 xchdir(sysfs_path);
249 for (cnt = 0; cnt < 30; ++cnt) {
250 loading_fd = open("loading", O_WRONLY);
251 if (loading_fd == -1)
252 sleep(1);
253 else
254 break;
255 }
256 if (loading_fd == -1)
257 goto out;
258
259 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
260 if (write(loading_fd, "1", 1) != 1)
261 goto out;
262
263 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
264 data_fd = open("data", O_WRONLY);
265 if (data_fd == -1)
266 goto out;
267 cnt = bb_copyfd_eof(firmware_fd, data_fd);
268
269 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
270 if (cnt > 0)
271 write(loading_fd, "0", 1);
272 else
273 write(loading_fd, "-1", 2);
274
275 out:
276 if (ENABLE_FEATURE_CLEAN_UP) {
277 close(firmware_fd);
278 close(loading_fd);
279 close(data_fd);
280 }
281}
282
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000283int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000284int mdev_main(int argc, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000285{
Rob Landley29e08ff2006-01-12 06:13:50 +0000286 char *action;
287 char *env_path;
288 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
289
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000290 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000291
Rob Landley29e08ff2006-01-12 06:13:50 +0000292 if (argc == 2 && !strcmp(argv[1],"-s")) {
Mike Frysingerc881c732007-11-19 09:04:22 +0000293
294 /* Scan:
295 * mdev -s
296 */
297
Rob Landleya7e3d052006-02-21 06:11:13 +0000298 struct stat st;
299
Denis Vlasenkof46be092006-10-16 19:39:37 +0000300 xstat("/", &st);
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000301 root_major = major(st.st_dev);
302 root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000303
304 recursive_action("/sys/block",
305 ACTION_RECURSE | ACTION_FOLLOWLINKS,
306 fileAction, dirAction, temp, 0);
307
308 recursive_action("/sys/class",
309 ACTION_RECURSE | ACTION_FOLLOWLINKS,
310 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000311
Rob Landley29e08ff2006-01-12 06:13:50 +0000312 } else {
Mike Frysingerc881c732007-11-19 09:04:22 +0000313
314 /* Hotplug:
315 * env ACTION=... DEVPATH=... mdev
316 * ACTION can be "add" or "remove"
317 * DEVPATH is like "/block/sda" or "/class/input/mice"
318 */
319
Rob Landley29e08ff2006-01-12 06:13:50 +0000320 action = getenv("ACTION");
321 env_path = getenv("DEVPATH");
Denis Vlasenko92758142006-10-03 19:56:34 +0000322 if (!action || !env_path)
Mike Frysingera421ba82006-02-03 00:25:37 +0000323 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000324
Rob Landleyef10d522006-06-26 14:11:33 +0000325 sprintf(temp, "/sys%s", env_path);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000326 if (!strcmp(action, "remove"))
327 make_device(temp, 1);
328 else if (!strcmp(action, "add")) {
329 make_device(temp, 0);
330
331 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE)
332 load_firmware(getenv("FIRMWARE"), temp);
333 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000334 }
335
Mike Frysingerc881c732007-11-19 09:04:22 +0000336 if (ENABLE_FEATURE_CLEAN_UP)
337 RELEASE_CONFIG_BUFFER(temp);
338
Rob Landley70f7ef72005-12-13 08:21:33 +0000339 return 0;
340}