blob: 5fea1b8e28cc53ae23d3fefde0f310fc43156821 [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 Landley70f7ef72005-12-13 08:21:33 +000041
Mike Frysingera421ba82006-02-03 00:25:37 +000042 RESERVE_CONFIG_BUFFER(temp, PATH_MAX);
Rob Landley70f7ef72005-12-13 08:21:33 +000043
44 /* Try to read major/minor string */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000045
Rob Landley70f7ef72005-12-13 08:21:33 +000046 snprintf(temp, PATH_MAX, "%s/dev", path);
47 fd = open(temp, O_RDONLY);
48 len = read(fd, temp, PATH_MAX-1);
Rob Landley70f7ef72005-12-13 08:21:33 +000049 close(fd);
Mike Frysingera421ba82006-02-03 00:25:37 +000050 if (len < 1) goto end;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000051
Rob Landley70f7ef72005-12-13 08:21:33 +000052 /* Determine device name, type, major and minor */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000053
Rob Landley70f7ef72005-12-13 08:21:33 +000054 device_name = strrchr(path, '/') + 1;
Mike Frysingera421ba82006-02-03 00:25:37 +000055 type = strncmp(path+5, "block/", 6) ? S_IFCHR : S_IFBLK;
56 if (sscanf(temp, "%d:%d", &major, &minor) != 2)
57 goto end;
Rob Landley70f7ef72005-12-13 08:21:33 +000058
Rob Landleyb56c2852005-12-17 10:52:30 +000059 /* If we have a config file, look up permissions for this device */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000060
Rob Landleyb56c2852005-12-17 10:52:30 +000061 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingera421ba82006-02-03 00:25:37 +000062 char *conf, *pos, *end;
Rob Landleyb56c2852005-12-17 10:52:30 +000063
64 /* mmap the config file */
Mike Frysingera421ba82006-02-03 00:25:37 +000065 if (-1 != (fd=open(MDEV_CONF,O_RDONLY))) {
66 len = lseek(fd, 0, SEEK_END);
67 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
Rob Landleyb56c2852005-12-17 10:52:30 +000068 if (conf) {
Mike Frysingera421ba82006-02-03 00:25:37 +000069 int line = 0;
Rob Landleyb56c2852005-12-17 10:52:30 +000070
71 /* Loop through lines in mmaped file*/
Mike Frysingera421ba82006-02-03 00:25:37 +000072 for (pos=conf; pos-conf<len;) {
Rob Landleyb56c2852005-12-17 10:52:30 +000073 int field;
74 char *end2;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000075
Rob Landleyb56c2852005-12-17 10:52:30 +000076 line++;
77 /* find end of this line */
Mike Frysingera421ba82006-02-03 00:25:37 +000078 for(end=pos; end-conf<len && *end!='\n'; end++)
79 ;
Rob Landleyb56c2852005-12-17 10:52:30 +000080
81 /* Three fields: regex, uid:gid, mode */
Mike Frysingera421ba82006-02-03 00:25:37 +000082 for (field=3; field; field--) {
Rob Landleyb56c2852005-12-17 10:52:30 +000083 /* Skip whitespace */
Mike Frysingera421ba82006-02-03 00:25:37 +000084 while (pos<end && isspace(*pos))
85 pos++;
86 if (pos==end || *pos=='#')
87 break;
88 for (end2=pos; end2<end && !isspace(*end2) && *end2!='#'; end2++)
89 ;
90
91 switch (field) {
Rob Landleyb56c2852005-12-17 10:52:30 +000092 /* Regex to match this device */
93 case 3:
94 {
Mike Frysingera421ba82006-02-03 00:25:37 +000095 char *regex = strndupa(pos,end2-pos);
Rob Landleyb56c2852005-12-17 10:52:30 +000096 regex_t match;
97 regmatch_t off;
98 int result;
99
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000100 /* Is this it? */
Rob Landleyb56c2852005-12-17 10:52:30 +0000101 xregcomp(&match,regex,REG_EXTENDED);
Mike Frysingera421ba82006-02-03 00:25:37 +0000102 result = regexec(&match,device_name,1,&off,0);
Rob Landleyb56c2852005-12-17 10:52:30 +0000103 regfree(&match);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000104
Rob Landleyb56c2852005-12-17 10:52:30 +0000105 /* If not this device, skip rest of line */
Mike Frysingera421ba82006-02-03 00:25:37 +0000106 if (result || off.rm_so || off.rm_eo!=strlen(device_name))
107 goto end_line;
Rob Landleyb56c2852005-12-17 10:52:30 +0000108
109 break;
110 }
111 /* uid:gid */
112 case 2:
113 {
114 char *s2;
115
116 /* Find : */
Mike Frysingera421ba82006-02-03 00:25:37 +0000117 for(s=pos; s<end2 && *s!=':'; s++)
118 ;
119 if (s == end2)
120 goto end_line;
Rob Landleyb56c2852005-12-17 10:52:30 +0000121
122 /* Parse UID */
Mike Frysingera421ba82006-02-03 00:25:37 +0000123 uid = strtoul(pos,&s2,10);
124 if (s != s2) {
Rob Landleyb56c2852005-12-17 10:52:30 +0000125 struct passwd *pass;
Mike Frysingera421ba82006-02-03 00:25:37 +0000126 pass = getpwnam(strndupa(pos,s-pos));
127 if (!pass)
128 goto end_line;
129 uid = pass->pw_uid;
Rob Landleyb56c2852005-12-17 10:52:30 +0000130 }
131 s++;
132 /* parse GID */
Mike Frysingera421ba82006-02-03 00:25:37 +0000133 gid = strtoul(s,&s2,10);
134 if (end2 != s2) {
Rob Landleyb56c2852005-12-17 10:52:30 +0000135 struct group *grp;
Mike Frysingera421ba82006-02-03 00:25:37 +0000136 grp = getgrnam(strndupa(s,end2-s));
137 if (!grp)
138 goto end_line;
139 gid = grp->gr_gid;
Rob Landleyb56c2852005-12-17 10:52:30 +0000140 }
141 break;
142 }
143 /* mode */
144 case 1:
145 {
Mike Frysingera421ba82006-02-03 00:25:37 +0000146 mode = strtoul(pos,&pos,8);
147 if (pos != end2)
148 goto end_line;
149 else
150 goto found_device;
Rob Landleyb56c2852005-12-17 10:52:30 +0000151 }
152 }
Mike Frysingera421ba82006-02-03 00:25:37 +0000153 pos = end2;
Rob Landleyb56c2852005-12-17 10:52:30 +0000154 }
155end_line:
156 /* Did everything parse happily? */
157 if (field && field!=3)
Mike Frysingera421ba82006-02-03 00:25:37 +0000158 bb_error_msg_and_die("Bad line %d",line);
Rob Landleyb56c2852005-12-17 10:52:30 +0000159
160 /* Next line */
Mike Frysingera421ba82006-02-03 00:25:37 +0000161 pos = ++end;
Rob Landleyb56c2852005-12-17 10:52:30 +0000162 }
163found_device:
Mike Frysingera421ba82006-02-03 00:25:37 +0000164 munmap(conf, len);
Rob Landleyb56c2852005-12-17 10:52:30 +0000165 }
166 close(fd);
167 }
168 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000169
170 sprintf(temp, "%s/%s", DEV_PATH, device_name);
Rob Landleyb56c2852005-12-17 10:52:30 +0000171 umask(0);
172 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
Rob Landley70f7ef72005-12-13 08:21:33 +0000173 bb_perror_msg_and_die("mknod %s failed", temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000174
Rob Landleya7e3d052006-02-21 06:11:13 +0000175 if (major==root_major && minor==root_minor)
176 symlink(temp,DEV_PATH "/root");
177
Rob Landleyb56c2852005-12-17 10:52:30 +0000178 if (ENABLE_FEATURE_MDEV_CONF) chown(temp,uid,gid);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000179
Rob Landley70f7ef72005-12-13 08:21:33 +0000180end:
181 RELEASE_CONFIG_BUFFER(temp);
182}
183
184/* Recursive search of /sys/block or /sys/class. path must be a writeable
185 * buffer of size PATH_MAX containing the directory string to start at. */
186
Rob Landleyb56c2852005-12-17 10:52:30 +0000187static void find_dev(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +0000188{
189 DIR *dir;
Mike Frysingera421ba82006-02-03 00:25:37 +0000190 size_t len = strlen(path);
Mike Frysinger248d2222006-02-03 00:19:42 +0000191 struct dirent *entry;
Rob Landley70f7ef72005-12-13 08:21:33 +0000192
Mike Frysinger248d2222006-02-03 00:19:42 +0000193 if ((dir = opendir(path)) == NULL)
194 return;
Rob Landley70f7ef72005-12-13 08:21:33 +0000195
Mike Frysinger248d2222006-02-03 00:19:42 +0000196 while ((entry = readdir(dir)) != NULL) {
Rob Landley70f7ef72005-12-13 08:21:33 +0000197
198 /* Skip "." and ".." (also skips hidden files, which is ok) */
199
Mike Frysingera421ba82006-02-03 00:25:37 +0000200 if (entry->d_name[0] == '.')
201 continue;
Rob Landley70f7ef72005-12-13 08:21:33 +0000202
203 if (entry->d_type == DT_DIR) {
204 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
205 find_dev(path);
Mike Frysinger271aa402006-02-07 06:10:45 +0000206 path[len] = 0;
Rob Landley70f7ef72005-12-13 08:21:33 +0000207 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000208
Rob Landley70f7ef72005-12-13 08:21:33 +0000209 /* If there's a dev entry, mknod it */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000210
Rob Landleye9277432006-01-22 23:14:16 +0000211 if (!strcmp(entry->d_name, "dev")) make_device(path);
Rob Landley70f7ef72005-12-13 08:21:33 +0000212 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000213
Rob Landley70f7ef72005-12-13 08:21:33 +0000214 closedir(dir);
215}
216
217int mdev_main(int argc, char *argv[])
218{
Rob Landley29e08ff2006-01-12 06:13:50 +0000219 char *action;
220 char *env_path;
221 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
222
223 /* 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")) {
248 sprintf(temp, "%s/%s", DEV_PATH, strrchr(env_path, '/') + 1);
249 unlink(temp);
250 }
251 }
252
Mike Frysingera421ba82006-02-03 00:25:37 +0000253 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000254 return 0;
255}