blob: 8743cdb6b617e77e171daeaad04b84d5f0ead8c0 [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{
Rob Landleyef10d522006-06-26 14:11:33 +000027 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
38 * also depend on path having writeable space after it. */
Rob Landley70f7ef72005-12-13 08:21:33 +000039
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;
Rob Landley10b36f92006-08-10 01:09:37 +000044 if (len < 1) return;
45 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000046
Rob Landley70f7ef72005-12-13 08:21:33 +000047 /* Determine device name, type, major and minor */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000048
Rob Landley70f7ef72005-12-13 08:21:33 +000049 device_name = strrchr(path, '/') + 1;
Rob Landley15fe2e12006-05-08 02:53:23 +000050 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
Rob Landley70f7ef72005-12-13 08:21:33 +000051
Rob Landleyb56c2852005-12-17 10:52:30 +000052 /* If we have a config file, look up permissions for this device */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000053
Rob Landleyb56c2852005-12-17 10:52:30 +000054 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingera421ba82006-02-03 00:25:37 +000055 char *conf, *pos, *end;
Denis Vlasenkof46be092006-10-16 19:39:37 +000056 int line, fd;
Rob Landleyb56c2852005-12-17 10:52:30 +000057
58 /* mmap the config file */
Denis Vlasenkof46be092006-10-16 19:39:37 +000059 fd = open("/etc/mdev.conf", O_RDONLY);
60 if (fd < 0)
61 goto end_parse;
62 len = xlseek(fd, 0, SEEK_END);
63 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
64 close(fd);
65 if (!conf)
66 goto end_parse;
Rob Landleyb56c2852005-12-17 10:52:30 +000067
Denis Vlasenkof46be092006-10-16 19:39:37 +000068 line = 0;
69 /* Loop through lines in mmaped file*/
70 for (pos=conf; pos-conf<len;) {
71 int field;
72 char *end2;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000073
Denis Vlasenkof46be092006-10-16 19:39:37 +000074 line++;
75 /* find end of this line */
Denis Vlasenkocf749bc2006-11-26 15:45:17 +000076 for (end=pos; end-conf<len && *end!='\n'; end++)
Denis Vlasenkof46be092006-10-16 19:39:37 +000077 ;
Rob Landleyb56c2852005-12-17 10:52:30 +000078
Denis Vlasenkof46be092006-10-16 19:39:37 +000079 /* Three fields: regex, uid:gid, mode */
80 for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
81 field++)
82 {
83 /* Skip whitespace */
84 while (pos<end && isspace(*pos)) pos++;
85 if (pos==end || *pos=='#') break;
86 for (end2=pos;
87 end2<end && !isspace(*end2) && *end2!='#'; end2++)
88 ;
Mike Frysingera421ba82006-02-03 00:25:37 +000089
Denis Vlasenkof46be092006-10-16 19:39:37 +000090 if (field == 0) {
91 /* Regex to match this device */
Rob Landleyb56c2852005-12-17 10:52:30 +000092
Denis Vlasenkof46be092006-10-16 19:39:37 +000093 char *regex = strndupa(pos, end2-pos);
94 regex_t match;
95 regmatch_t off;
96 int result;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000097
Denis Vlasenkof46be092006-10-16 19:39:37 +000098 /* Is this it? */
99 xregcomp(&match,regex, REG_EXTENDED);
100 result = regexec(&match, device_name, 1, &off, 0);
101 regfree(&match);
Rob Landleyb56c2852005-12-17 10:52:30 +0000102
Denis Vlasenkof46be092006-10-16 19:39:37 +0000103 /* If not this device, skip rest of line */
104 if (result || off.rm_so
105 || off.rm_eo != strlen(device_name))
106 break;
Rob Landleyb56c2852005-12-17 10:52:30 +0000107 }
Denis Vlasenkof46be092006-10-16 19:39:37 +0000108 if (field == 1) {
109 /* uid:gid */
110
111 char *s, *s2;
112
113 /* Find : */
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000114 for (s=pos; s<end2 && *s!=':'; s++)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000115 ;
116 if (s == end2) break;
117
118 /* Parse UID */
119 uid = strtoul(pos, &s2, 10);
120 if (s != s2) {
121 struct passwd *pass;
122 pass = getpwnam(strndupa(pos, s-pos));
123 if (!pass) break;
124 uid = pass->pw_uid;
125 }
126 s++;
127 /* parse GID */
128 gid = strtoul(s, &s2, 10);
129 if (end2 != s2) {
130 struct group *grp;
131 grp = getgrnam(strndupa(s, end2-s));
132 if (!grp) break;
133 gid = grp->gr_gid;
134 }
135 }
136 if (field == 2) {
137 /* mode */
138
139 mode = strtoul(pos, &pos, 8);
140 if (pos != end2) break;
141 }
142 if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
143 // Command to run
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000144 const char *s = "@$*";
145 const char *s2;
Denis Vlasenkof46be092006-10-16 19:39:37 +0000146 s2 = strchr(s, *pos++);
147 if (!s2) {
148 // Force error
149 field = 1;
150 break;
151 }
152 if ((s2-s+1) & (1<<delete))
153 command = xstrndup(pos, end-pos);
154 }
155
156 pos = end2;
Rob Landleyb56c2852005-12-17 10:52:30 +0000157 }
Denis Vlasenkof46be092006-10-16 19:39:37 +0000158
159 /* Did everything parse happily? */
160
161 if (field > 2) break;
162 if (field) bb_error_msg_and_die("bad line %d",line);
163
164 /* Next line */
165 pos = ++end;
Rob Landleyb56c2852005-12-17 10:52:30 +0000166 }
Denis Vlasenkof46be092006-10-16 19:39:37 +0000167 munmap(conf, len);
168 end_parse: /* nothing */ ;
Rob Landleyb56c2852005-12-17 10:52:30 +0000169 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000170
Rob Landleyb56c2852005-12-17 10:52:30 +0000171 umask(0);
Rob Landleyef10d522006-06-26 14:11:33 +0000172 if (!delete) {
Rob Landley10b36f92006-08-10 01:09:37 +0000173 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
Rob Landleyef10d522006-06-26 14:11:33 +0000174 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
Denis Vlasenkof46be092006-10-16 19:39:37 +0000175 bb_perror_msg_and_die("mknod %s", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000176
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000177 if (major == root_major && minor == root_minor)
Rob Landleyef10d522006-06-26 14:11:33 +0000178 symlink(device_name, "root");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000179
Rob Landleyef10d522006-06-26 14:11:33 +0000180 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
181 }
182 if (command) {
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000183 /* setenv will leak memory, so use putenv */
184 char *s = xasprintf("MDEV=%s", device_name);
Rob Landleyef10d522006-06-26 14:11:33 +0000185 putenv(s);
Denis Vlasenkod6766c72007-06-08 16:18:15 +0000186 if (system(command) == -1)
187 bb_perror_msg_and_die("cannot run %s", command);
188 s[4] = '\0';
189 unsetenv(s);
Rob Landleyef10d522006-06-26 14:11:33 +0000190 free(s);
191 free(command);
Rob Landleyef10d522006-06-26 14:11:33 +0000192 }
193 if (delete) unlink(device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000194}
195
Mike Frysingerb51fd352007-06-13 09:24:50 +0000196/* File callback for /sys/ traversal */
197static int fileAction(const char *fileName, struct stat *statbuf,
198 void *userData, int depth)
Rob Landley70f7ef72005-12-13 08:21:33 +0000199{
Mike Frysingerb51fd352007-06-13 09:24:50 +0000200 size_t len = strlen(fileName) - 4;
201 char *scratch = userData;
Rob Landley70f7ef72005-12-13 08:21:33 +0000202
Mike Frysingerb51fd352007-06-13 09:24:50 +0000203 if (strcmp(fileName + len, "/dev"))
204 return FALSE;
Rob Landley70f7ef72005-12-13 08:21:33 +0000205
Mike Frysingerb51fd352007-06-13 09:24:50 +0000206 strcpy(scratch, fileName);
207 scratch[len] = 0;
208 make_device(scratch, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000209
Mike Frysingerb51fd352007-06-13 09:24:50 +0000210 return TRUE;
211}
Rob Landley70f7ef72005-12-13 08:21:33 +0000212
Mike Frysingerb51fd352007-06-13 09:24:50 +0000213/* Directory callback for /sys/ traversal */
214static int dirAction(const char *fileName, struct stat *statbuf,
215 void *userData, int depth)
216{
217 return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
Rob Landley70f7ef72005-12-13 08:21:33 +0000218}
219
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000220/* For the full gory details, see linux/Documentation/firmware_class/README
221 *
222 * Firmware loading works like this:
223 * - kernel sets FIRMWARE env var
224 * - userspace checks /lib/firmware/$FIRMWARE
225 * - userspace waits for /sys/$DEVPATH/loading to appear
226 * - userspace writes "1" to /sys/$DEVPATH/loading
227 * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
228 * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
229 * - kernel loads firmware into device
230 */
231static inline void load_firmware(const char * const firmware, const char * const sysfs_path)
232{
233 int cnt;
234 int firmware_fd, loading_fd, data_fd;
235
236 /* check for $FIRMWARE from kernel */
237 /* XXX: dont bother: open(NULL) works same as open("no-such-file")
238 * if (!firmware)
239 * return;
240 */
241
242 /* check for /lib/firmware/$FIRMWARE */
243 xchdir("/lib/firmware");
Mike Frysinger0e0639b2007-06-14 09:29:48 +0000244 firmware_fd = xopen(firmware, O_RDONLY);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000245
246 /* in case we goto out ... */
247 data_fd = -1;
248
249 /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
250 xchdir(sysfs_path);
251 for (cnt = 0; cnt < 30; ++cnt) {
252 loading_fd = open("loading", O_WRONLY);
253 if (loading_fd == -1)
254 sleep(1);
255 else
256 break;
257 }
258 if (loading_fd == -1)
259 goto out;
260
261 /* tell kernel we're loading by `echo 1 > /sys/$DEVPATH/loading` */
262 if (write(loading_fd, "1", 1) != 1)
263 goto out;
264
265 /* load firmware by `cat /lib/firmware/$FIRMWARE > /sys/$DEVPATH/data */
266 data_fd = open("data", O_WRONLY);
267 if (data_fd == -1)
268 goto out;
269 cnt = bb_copyfd_eof(firmware_fd, data_fd);
270
271 /* tell kernel result by `echo [0|-1] > /sys/$DEVPATH/loading` */
272 if (cnt > 0)
273 write(loading_fd, "0", 1);
274 else
275 write(loading_fd, "-1", 2);
276
277 out:
278 if (ENABLE_FEATURE_CLEAN_UP) {
279 close(firmware_fd);
280 close(loading_fd);
281 close(data_fd);
282 }
283}
284
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000285int mdev_main(int argc, char **argv);
286int mdev_main(int argc, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000287{
Rob Landley29e08ff2006-01-12 06:13:50 +0000288 char *action;
289 char *env_path;
290 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
291
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000292 xchdir("/dev");
Rob Landley15fe2e12006-05-08 02:53:23 +0000293
Rob Landley29e08ff2006-01-12 06:13:50 +0000294 /* Scan */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000295
Rob Landley29e08ff2006-01-12 06:13:50 +0000296 if (argc == 2 && !strcmp(argv[1],"-s")) {
Rob Landleya7e3d052006-02-21 06:11:13 +0000297 struct stat st;
298
Denis Vlasenkof46be092006-10-16 19:39:37 +0000299 xstat("/", &st);
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +0000300 root_major = major(st.st_dev);
301 root_minor = minor(st.st_dev);
Mike Frysingerb51fd352007-06-13 09:24:50 +0000302
303 recursive_action("/sys/block",
304 ACTION_RECURSE | ACTION_FOLLOWLINKS,
305 fileAction, dirAction, temp, 0);
306
307 recursive_action("/sys/class",
308 ACTION_RECURSE | ACTION_FOLLOWLINKS,
309 fileAction, dirAction, temp, 0);
Rob Landley29e08ff2006-01-12 06:13:50 +0000310
311 /* Hotplug */
312
313 } else {
314 action = getenv("ACTION");
315 env_path = getenv("DEVPATH");
Denis Vlasenko92758142006-10-03 19:56:34 +0000316 if (!action || !env_path)
Mike Frysingera421ba82006-02-03 00:25:37 +0000317 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000318
Rob Landleyef10d522006-06-26 14:11:33 +0000319 sprintf(temp, "/sys%s", env_path);
Mike Frysingera78ef2c2007-06-13 07:34:15 +0000320 if (!strcmp(action, "remove"))
321 make_device(temp, 1);
322 else if (!strcmp(action, "add")) {
323 make_device(temp, 0);
324
325 if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE)
326 load_firmware(getenv("FIRMWARE"), temp);
327 }
Rob Landley29e08ff2006-01-12 06:13:50 +0000328 }
329
Mike Frysingera421ba82006-02-03 00:25:37 +0000330 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000331 return 0;
332}