blob: 73938f8deee6b6e8c7018ab5a4676a85eafddce6 [file] [log] [blame]
Rob Landley70f7ef72005-12-13 08:21:33 +00001/* vi:set ts=4:
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002 *
Rob Landley70f7ef72005-12-13 08:21:33 +00003 * mdev - Mini udev for busybox
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00004 *
Rob Landley70f7ef72005-12-13 08:21:33 +00005 * Copyright 2005 Rob Landley <rob@landley.net>
6 * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
7 *
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 */
10
Rob Landleyb56c2852005-12-17 10:52:30 +000011#include <ctype.h>
Rob Landley70f7ef72005-12-13 08:21:33 +000012#include <dirent.h>
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <string.h>
Rob Landleyb56c2852005-12-17 10:52:30 +000017#include <sys/mman.h>
Rob Landley70f7ef72005-12-13 08:21:33 +000018#include <sys/stat.h>
Rob Landleya7e3d052006-02-21 06:11:13 +000019#include <sys/sysmacros.h>
Rob Landley70f7ef72005-12-13 08:21:33 +000020#include <sys/types.h>
21#include <unistd.h>
Rob Landley29e08ff2006-01-12 06:13:50 +000022#include <stdlib.h>
Rob Landleyb56c2852005-12-17 10:52:30 +000023#include "busybox.h"
24#include "xregex.h"
Rob Landley70f7ef72005-12-13 08:21:33 +000025
Rob Landley9bdd7422005-12-17 10:54:17 +000026#define DEV_PATH "/dev"
Mike Frysingera421ba82006-02-03 00:25:37 +000027#define MDEV_CONF "/etc/mdev.conf"
Rob Landley70f7ef72005-12-13 08:21:33 +000028
29#include <busybox.h>
30
Rob Landleya7e3d052006-02-21 06:11:13 +000031int root_major, root_minor;
32
Rob Landley70f7ef72005-12-13 08:21:33 +000033/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
Rob Landleyb56c2852005-12-17 10:52:30 +000034static void make_device(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +000035{
Mike Frysingera421ba82006-02-03 00:25:37 +000036 char *device_name, *s;
37 int major, minor, type, len, fd;
38 int mode = 0660;
39 uid_t uid = 0;
40 gid_t gid = 0;
Rob Landley15fe2e12006-05-08 02:53:23 +000041 char *temp = path + strlen(path);
Rob Landley70f7ef72005-12-13 08:21:33 +000042
Rob Landley15fe2e12006-05-08 02:53:23 +000043 /* Try to read major/minor string. Note that the kernel puts \n after
44 * the data, so we don't need to worry about null terminating the string
45 * because sscanf() will stop at the first nondigit, which \n is. We
46 * also depend on path having writeable space after it. */
Rob Landley70f7ef72005-12-13 08:21:33 +000047
Rob Landley15fe2e12006-05-08 02:53:23 +000048 strcat(path, "/dev");
49 fd = open(path, O_RDONLY);
50 len = read(fd, temp + 1, 64);
51 *temp++ = 0;
Rob Landley70f7ef72005-12-13 08:21:33 +000052 close(fd);
Rob Landley15fe2e12006-05-08 02:53:23 +000053 if (len < 1) return;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000054
Rob Landley70f7ef72005-12-13 08:21:33 +000055 /* Determine device name, type, major and minor */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000056
Rob Landley70f7ef72005-12-13 08:21:33 +000057 device_name = strrchr(path, '/') + 1;
Rob Landley15fe2e12006-05-08 02:53:23 +000058 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
59 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
Rob Landley70f7ef72005-12-13 08:21:33 +000060
Rob Landleyb56c2852005-12-17 10:52:30 +000061 /* If we have a config file, look up permissions for this device */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000062
Rob Landleyb56c2852005-12-17 10:52:30 +000063 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingera421ba82006-02-03 00:25:37 +000064 char *conf, *pos, *end;
Rob Landleyb56c2852005-12-17 10:52:30 +000065
66 /* mmap the config file */
Mike Frysingera421ba82006-02-03 00:25:37 +000067 if (-1 != (fd=open(MDEV_CONF,O_RDONLY))) {
68 len = lseek(fd, 0, SEEK_END);
69 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
Rob Landleyb56c2852005-12-17 10:52:30 +000070 if (conf) {
Mike Frysingera421ba82006-02-03 00:25:37 +000071 int line = 0;
Rob Landleyb56c2852005-12-17 10:52:30 +000072
73 /* Loop through lines in mmaped file*/
Mike Frysingera421ba82006-02-03 00:25:37 +000074 for (pos=conf; pos-conf<len;) {
Rob Landleyb56c2852005-12-17 10:52:30 +000075 int field;
76 char *end2;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000077
Rob Landleyb56c2852005-12-17 10:52:30 +000078 line++;
79 /* find end of this line */
Mike Frysingera421ba82006-02-03 00:25:37 +000080 for(end=pos; end-conf<len && *end!='\n'; end++)
81 ;
Rob Landleyb56c2852005-12-17 10:52:30 +000082
83 /* Three fields: regex, uid:gid, mode */
Mike Frysingera421ba82006-02-03 00:25:37 +000084 for (field=3; field; field--) {
Rob Landleyb56c2852005-12-17 10:52:30 +000085 /* Skip whitespace */
Mike Frysingera421ba82006-02-03 00:25:37 +000086 while (pos<end && isspace(*pos))
87 pos++;
88 if (pos==end || *pos=='#')
89 break;
90 for (end2=pos; end2<end && !isspace(*end2) && *end2!='#'; end2++)
91 ;
92
93 switch (field) {
Rob Landleyb56c2852005-12-17 10:52:30 +000094 /* Regex to match this device */
95 case 3:
96 {
Mike Frysingera421ba82006-02-03 00:25:37 +000097 char *regex = strndupa(pos,end2-pos);
Rob Landleyb56c2852005-12-17 10:52:30 +000098 regex_t match;
99 regmatch_t off;
100 int result;
101
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000102 /* Is this it? */
Rob Landleyb56c2852005-12-17 10:52:30 +0000103 xregcomp(&match,regex,REG_EXTENDED);
Mike Frysingera421ba82006-02-03 00:25:37 +0000104 result = regexec(&match,device_name,1,&off,0);
Rob Landleyb56c2852005-12-17 10:52:30 +0000105 regfree(&match);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000106
Rob Landleyb56c2852005-12-17 10:52:30 +0000107 /* If not this device, skip rest of line */
Mike Frysingera421ba82006-02-03 00:25:37 +0000108 if (result || off.rm_so || off.rm_eo!=strlen(device_name))
109 goto end_line;
Rob Landleyb56c2852005-12-17 10:52:30 +0000110
111 break;
112 }
113 /* uid:gid */
114 case 2:
115 {
116 char *s2;
117
118 /* Find : */
Mike Frysingera421ba82006-02-03 00:25:37 +0000119 for(s=pos; s<end2 && *s!=':'; s++)
120 ;
121 if (s == end2)
122 goto end_line;
Rob Landleyb56c2852005-12-17 10:52:30 +0000123
124 /* Parse UID */
Mike Frysingera421ba82006-02-03 00:25:37 +0000125 uid = strtoul(pos,&s2,10);
126 if (s != s2) {
Rob Landleyb56c2852005-12-17 10:52:30 +0000127 struct passwd *pass;
Mike Frysingera421ba82006-02-03 00:25:37 +0000128 pass = getpwnam(strndupa(pos,s-pos));
129 if (!pass)
130 goto end_line;
131 uid = pass->pw_uid;
Rob Landleyb56c2852005-12-17 10:52:30 +0000132 }
133 s++;
134 /* parse GID */
Mike Frysingera421ba82006-02-03 00:25:37 +0000135 gid = strtoul(s,&s2,10);
136 if (end2 != s2) {
Rob Landleyb56c2852005-12-17 10:52:30 +0000137 struct group *grp;
Mike Frysingera421ba82006-02-03 00:25:37 +0000138 grp = getgrnam(strndupa(s,end2-s));
139 if (!grp)
140 goto end_line;
141 gid = grp->gr_gid;
Rob Landleyb56c2852005-12-17 10:52:30 +0000142 }
143 break;
144 }
145 /* mode */
146 case 1:
147 {
Mike Frysingera421ba82006-02-03 00:25:37 +0000148 mode = strtoul(pos,&pos,8);
149 if (pos != end2)
150 goto end_line;
151 else
152 goto found_device;
Rob Landleyb56c2852005-12-17 10:52:30 +0000153 }
154 }
Mike Frysingera421ba82006-02-03 00:25:37 +0000155 pos = end2;
Rob Landleyb56c2852005-12-17 10:52:30 +0000156 }
157end_line:
158 /* Did everything parse happily? */
159 if (field && field!=3)
Mike Frysingera421ba82006-02-03 00:25:37 +0000160 bb_error_msg_and_die("Bad line %d",line);
Rob Landleyb56c2852005-12-17 10:52:30 +0000161
162 /* Next line */
Mike Frysingera421ba82006-02-03 00:25:37 +0000163 pos = ++end;
Rob Landleyb56c2852005-12-17 10:52:30 +0000164 }
165found_device:
Mike Frysingera421ba82006-02-03 00:25:37 +0000166 munmap(conf, len);
Rob Landleyb56c2852005-12-17 10:52:30 +0000167 }
168 close(fd);
169 }
170 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000171
Rob Landleyb56c2852005-12-17 10:52:30 +0000172 umask(0);
Rob Landley15fe2e12006-05-08 02:53:23 +0000173 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
174 bb_perror_msg_and_die("mknod %s failed", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000175
Rob Landleya7e3d052006-02-21 06:11:13 +0000176 if (major==root_major && minor==root_minor)
Rob Landley15fe2e12006-05-08 02:53:23 +0000177 symlink(device_name, "root");
Rob Landleya7e3d052006-02-21 06:11:13 +0000178
Rob Landley15fe2e12006-05-08 02:53:23 +0000179 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
Rob Landley70f7ef72005-12-13 08:21:33 +0000180}
181
182/* Recursive search of /sys/block or /sys/class. path must be a writeable
183 * buffer of size PATH_MAX containing the directory string to start at. */
184
Rob Landleyb56c2852005-12-17 10:52:30 +0000185static void find_dev(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +0000186{
187 DIR *dir;
Mike Frysingera421ba82006-02-03 00:25:37 +0000188 size_t len = strlen(path);
Mike Frysinger248d2222006-02-03 00:19:42 +0000189 struct dirent *entry;
Rob Landley70f7ef72005-12-13 08:21:33 +0000190
Mike Frysinger248d2222006-02-03 00:19:42 +0000191 if ((dir = opendir(path)) == NULL)
192 return;
Rob Landley70f7ef72005-12-13 08:21:33 +0000193
Mike Frysinger248d2222006-02-03 00:19:42 +0000194 while ((entry = readdir(dir)) != NULL) {
Rob Landley70f7ef72005-12-13 08:21:33 +0000195
196 /* Skip "." and ".." (also skips hidden files, which is ok) */
197
Mike Frysingera421ba82006-02-03 00:25:37 +0000198 if (entry->d_name[0] == '.')
199 continue;
Rob Landley70f7ef72005-12-13 08:21:33 +0000200
201 if (entry->d_type == DT_DIR) {
202 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
203 find_dev(path);
Mike Frysinger271aa402006-02-07 06:10:45 +0000204 path[len] = 0;
Rob Landley70f7ef72005-12-13 08:21:33 +0000205 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000206
Rob Landley70f7ef72005-12-13 08:21:33 +0000207 /* If there's a dev entry, mknod it */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000208
Rob Landleye9277432006-01-22 23:14:16 +0000209 if (!strcmp(entry->d_name, "dev")) make_device(path);
Rob Landley70f7ef72005-12-13 08:21:33 +0000210 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000211
Rob Landley70f7ef72005-12-13 08:21:33 +0000212 closedir(dir);
213}
214
215int mdev_main(int argc, char *argv[])
216{
Rob Landley29e08ff2006-01-12 06:13:50 +0000217 char *action;
218 char *env_path;
219 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
220
Rob Landley15fe2e12006-05-08 02:53:23 +0000221 bb_xchdir(DEV_PATH);
222
Rob Landley29e08ff2006-01-12 06:13:50 +0000223 /* Scan */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000224
Rob Landley29e08ff2006-01-12 06:13:50 +0000225 if (argc == 2 && !strcmp(argv[1],"-s")) {
Rob Landleya7e3d052006-02-21 06:11:13 +0000226 struct stat st;
227
228 stat("/", &st); // If this fails, we have bigger problems.
229 root_major=major(st.st_dev);
230 root_minor=minor(st.st_dev);
Rob Landley29e08ff2006-01-12 06:13:50 +0000231 strcpy(temp,"/sys/block");
232 find_dev(temp);
233 strcpy(temp,"/sys/class");
234 find_dev(temp);
235
236 /* Hotplug */
237
238 } else {
239 action = getenv("ACTION");
240 env_path = getenv("DEVPATH");
Mike Frysingera421ba82006-02-03 00:25:37 +0000241 if (!action || !env_path)
242 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000243
Rob Landley29e08ff2006-01-12 06:13:50 +0000244 if (!strcmp(action, "add")) {
245 sprintf(temp, "/sys%s", env_path);
246 make_device(temp);
247 } else if (!strcmp(action, "remove")) {
Rob Landley15fe2e12006-05-08 02:53:23 +0000248 unlink(strrchr(env_path, '/') + 1);
Rob Landley29e08ff2006-01-12 06:13:50 +0000249 }
250 }
251
Mike Frysingera421ba82006-02-03 00:25:37 +0000252 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000253 return 0;
254}