blob: 3c7cf7bd1331d5822ae3f6e14692f5ede80127c4 [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 "busybox.h"
Rob Landleyef10d522006-06-26 14:11:33 +000012#include <ctype.h>
13#include <errno.h>
14#include <sys/mman.h>
15#include <sys/sysmacros.h>
Rob Landleyb56c2852005-12-17 10:52:30 +000016#include "xregex.h"
Rob Landley70f7ef72005-12-13 08:21:33 +000017
Rob Landley9bdd7422005-12-17 10:54:17 +000018#define DEV_PATH "/dev"
Rob Landley70f7ef72005-12-13 08:21:33 +000019
Rob Landley5cd1ccd2006-05-21 18:33:27 +000020struct mdev_globals
21{
22 int root_major, root_minor;
23} mdev_globals;
24
25#define bbg mdev_globals
Rob Landleya7e3d052006-02-21 06:11:13 +000026
Rob Landley70f7ef72005-12-13 08:21:33 +000027/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
Rob Landleyef10d522006-06-26 14:11:33 +000028static void make_device(char *path, int delete)
Rob Landley70f7ef72005-12-13 08:21:33 +000029{
Rob Landleyef10d522006-06-26 14:11:33 +000030 char *device_name;
Mike Frysingera421ba82006-02-03 00:25:37 +000031 int major, minor, type, len, fd;
32 int mode = 0660;
33 uid_t uid = 0;
34 gid_t gid = 0;
Rob Landley15fe2e12006-05-08 02:53:23 +000035 char *temp = path + strlen(path);
Rob Landleyef10d522006-06-26 14:11:33 +000036 char *command = NULL;
Rob Landley70f7ef72005-12-13 08:21:33 +000037
Rob Landley15fe2e12006-05-08 02:53:23 +000038 /* Try to read major/minor string. Note that the kernel puts \n after
39 * the data, so we don't need to worry about null terminating the string
40 * because sscanf() will stop at the first nondigit, which \n is. We
41 * also depend on path having writeable space after it. */
Rob Landley70f7ef72005-12-13 08:21:33 +000042
Rob Landley15fe2e12006-05-08 02:53:23 +000043 strcat(path, "/dev");
44 fd = open(path, O_RDONLY);
45 len = read(fd, temp + 1, 64);
46 *temp++ = 0;
Rob Landley70f7ef72005-12-13 08:21:33 +000047 close(fd);
Rob Landley15fe2e12006-05-08 02:53:23 +000048 if (len < 1) return;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000049
Rob Landley70f7ef72005-12-13 08:21:33 +000050 /* Determine device name, type, major and minor */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000051
Rob Landley70f7ef72005-12-13 08:21:33 +000052 device_name = strrchr(path, '/') + 1;
Rob Landley15fe2e12006-05-08 02:53:23 +000053 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
54 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
Rob Landley70f7ef72005-12-13 08:21:33 +000055
Rob Landleyb56c2852005-12-17 10:52:30 +000056 /* If we have a config file, look up permissions for this device */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000057
Rob Landleyb56c2852005-12-17 10:52:30 +000058 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingera421ba82006-02-03 00:25:37 +000059 char *conf, *pos, *end;
Rob Landleyb56c2852005-12-17 10:52:30 +000060
61 /* mmap the config file */
Rob Landleyef10d522006-06-26 14:11:33 +000062 if (-1 != (fd=open("/etc/mdev.conf",O_RDONLY))) {
Mike Frysingera421ba82006-02-03 00:25:37 +000063 len = lseek(fd, 0, SEEK_END);
64 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
Rob Landleyb56c2852005-12-17 10:52:30 +000065 if (conf) {
Mike Frysingera421ba82006-02-03 00:25:37 +000066 int line = 0;
Rob Landleyb56c2852005-12-17 10:52:30 +000067
68 /* Loop through lines in mmaped file*/
Mike Frysingera421ba82006-02-03 00:25:37 +000069 for (pos=conf; pos-conf<len;) {
Rob Landleyb56c2852005-12-17 10:52:30 +000070 int field;
71 char *end2;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000072
Rob Landleyb56c2852005-12-17 10:52:30 +000073 line++;
74 /* find end of this line */
Mike Frysingera421ba82006-02-03 00:25:37 +000075 for(end=pos; end-conf<len && *end!='\n'; end++)
76 ;
Rob Landleyb56c2852005-12-17 10:52:30 +000077
78 /* Three fields: regex, uid:gid, mode */
Rob Landleyef10d522006-06-26 14:11:33 +000079 for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
80 field++)
81 {
Rob Landleyb56c2852005-12-17 10:52:30 +000082 /* Skip whitespace */
Rob Landleyef10d522006-06-26 14:11:33 +000083 while (pos<end && isspace(*pos)) pos++;
84 if (pos==end || *pos=='#') break;
85 for (end2=pos;
86 end2<end && !isspace(*end2) && *end2!='#'; end2++)
Mike Frysingera421ba82006-02-03 00:25:37 +000087 ;
88
Rob Landleyef10d522006-06-26 14:11:33 +000089 if (!field) {
Rob Landleyb56c2852005-12-17 10:52:30 +000090 /* Regex to match this device */
Rob Landleyb56c2852005-12-17 10:52:30 +000091
Rob Landleyef10d522006-06-26 14:11:33 +000092 char *regex = strndupa(pos, end2-pos);
93 regex_t match;
94 regmatch_t off;
95 int result;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000096
Rob Landleyef10d522006-06-26 14:11:33 +000097 /* Is this it? */
98 xregcomp(&match,regex, REG_EXTENDED);
99 result = regexec(&match, device_name, 1, &off, 0);
100 regfree(&match);
Rob Landleyb56c2852005-12-17 10:52:30 +0000101
Rob Landleyef10d522006-06-26 14:11:33 +0000102 /* If not this device, skip rest of line */
103 if (result || off.rm_so
104 || off.rm_eo != strlen(device_name))
Rob Landleyb56c2852005-12-17 10:52:30 +0000105 break;
Rob Landleyef10d522006-06-26 14:11:33 +0000106
107 } else if (field == 1) {
Rob Landleyb56c2852005-12-17 10:52:30 +0000108 /* uid:gid */
Rob Landleyb56c2852005-12-17 10:52:30 +0000109
Rob Landleyef10d522006-06-26 14:11:33 +0000110 char *s, *s2;
Rob Landleyb56c2852005-12-17 10:52:30 +0000111
Rob Landleyef10d522006-06-26 14:11:33 +0000112 /* Find : */
113 for(s=pos; s<end2 && *s!=':'; s++)
114 ;
115 if (s == end2) break;
116
117 /* Parse UID */
118 uid = strtoul(pos,&s2,10);
119 if (s != s2) {
120 struct passwd *pass;
121 pass = getpwnam(strndupa(pos, s-pos));
122 if (!pass) break;
123 uid = pass->pw_uid;
124 }
125 s++;
126 /* parse GID */
127 gid = strtoul(s, &s2, 10);
128 if (end2 != s2) {
129 struct group *grp;
130 grp = getgrnam(strndupa(s, end2-s));
131 if (!grp) break;
132 gid = grp->gr_gid;
133 }
134 } else if (field == 2) {
135 /* mode */
136
137 mode = strtoul(pos, &pos, 8);
138 if (pos != end2) break;
139 } else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
140 // Command to run
141 char *s = "@$*", *s2;
142 if (!(s2 = strchr(s, *pos++))) {
143 // Force error
144 field = 1;
Rob Landleyb56c2852005-12-17 10:52:30 +0000145 break;
146 }
Rob Landleyef10d522006-06-26 14:11:33 +0000147 if ((s2-s+1) & (1<<delete))
148 command = bb_xstrndup(pos, end-pos);
Rob Landleyb56c2852005-12-17 10:52:30 +0000149 }
Rob Landleyef10d522006-06-26 14:11:33 +0000150
Mike Frysingera421ba82006-02-03 00:25:37 +0000151 pos = end2;
Rob Landleyb56c2852005-12-17 10:52:30 +0000152 }
Rob Landleyef10d522006-06-26 14:11:33 +0000153
Rob Landleyb56c2852005-12-17 10:52:30 +0000154 /* Did everything parse happily? */
Rob Landleyef10d522006-06-26 14:11:33 +0000155
156 if (field > 2) break;
157 if (field) bb_error_msg_and_die("Bad line %d",line);
Rob Landleyb56c2852005-12-17 10:52:30 +0000158
159 /* Next line */
Mike Frysingera421ba82006-02-03 00:25:37 +0000160 pos = ++end;
Rob Landleyb56c2852005-12-17 10:52:30 +0000161 }
Mike Frysingera421ba82006-02-03 00:25:37 +0000162 munmap(conf, len);
Rob Landleyb56c2852005-12-17 10:52:30 +0000163 }
164 close(fd);
165 }
166 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000167
Rob Landleyb56c2852005-12-17 10:52:30 +0000168 umask(0);
Rob Landleyef10d522006-06-26 14:11:33 +0000169 if (!delete) {
170 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
171 bb_perror_msg_and_die("mknod %s failed", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000172
Rob Landleyef10d522006-06-26 14:11:33 +0000173 if (major == bbg.root_major && minor == bbg.root_minor)
174 symlink(device_name, "root");
Rob Landleya7e3d052006-02-21 06:11:13 +0000175
Rob Landleyef10d522006-06-26 14:11:33 +0000176 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
177 }
178 if (command) {
179 int rc;
180 char *s;
181
182 s=bb_xasprintf("MDEV=%s",device_name);
183 putenv(s);
184 rc = system(command);
185 s[4]=0;
186 putenv(s);
187 free(s);
188 free(command);
189 if (rc == -1) bb_perror_msg_and_die("Couldn't run %s", command);
190 }
191 if (delete) unlink(device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000192}
193
194/* Recursive search of /sys/block or /sys/class. path must be a writeable
195 * buffer of size PATH_MAX containing the directory string to start at. */
196
Rob Landleyb56c2852005-12-17 10:52:30 +0000197static void find_dev(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +0000198{
199 DIR *dir;
Mike Frysingera421ba82006-02-03 00:25:37 +0000200 size_t len = strlen(path);
Mike Frysinger248d2222006-02-03 00:19:42 +0000201 struct dirent *entry;
Rob Landley70f7ef72005-12-13 08:21:33 +0000202
Mike Frysinger248d2222006-02-03 00:19:42 +0000203 if ((dir = opendir(path)) == NULL)
204 return;
Rob Landley70f7ef72005-12-13 08:21:33 +0000205
Mike Frysinger248d2222006-02-03 00:19:42 +0000206 while ((entry = readdir(dir)) != NULL) {
Rob Landley0960ca72006-06-13 18:27:16 +0000207 struct stat st;
Rob Landley70f7ef72005-12-13 08:21:33 +0000208
209 /* Skip "." and ".." (also skips hidden files, which is ok) */
210
Mike Frysingera421ba82006-02-03 00:25:37 +0000211 if (entry->d_name[0] == '.')
212 continue;
Rob Landley70f7ef72005-12-13 08:21:33 +0000213
Rob Landley0960ca72006-06-13 18:27:16 +0000214 // uClibc doesn't fill out entry->d_type reliably. so we use lstat().
215
216 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
217 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
218 path[len] = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000219
Rob Landley70f7ef72005-12-13 08:21:33 +0000220 /* If there's a dev entry, mknod it */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000221
Rob Landleyef10d522006-06-26 14:11:33 +0000222 if (!strcmp(entry->d_name, "dev")) make_device(path, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000223 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000224
Rob Landley70f7ef72005-12-13 08:21:33 +0000225 closedir(dir);
226}
227
228int mdev_main(int argc, char *argv[])
229{
Rob Landley29e08ff2006-01-12 06:13:50 +0000230 char *action;
231 char *env_path;
232 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
233
Rob Landley15fe2e12006-05-08 02:53:23 +0000234 bb_xchdir(DEV_PATH);
235
Rob Landley29e08ff2006-01-12 06:13:50 +0000236 /* Scan */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000237
Rob Landley29e08ff2006-01-12 06:13:50 +0000238 if (argc == 2 && !strcmp(argv[1],"-s")) {
Rob Landleya7e3d052006-02-21 06:11:13 +0000239 struct stat st;
240
241 stat("/", &st); // If this fails, we have bigger problems.
Rob Landley5cd1ccd2006-05-21 18:33:27 +0000242 bbg.root_major=major(st.st_dev);
243 bbg.root_minor=minor(st.st_dev);
Rob Landley29e08ff2006-01-12 06:13:50 +0000244 strcpy(temp,"/sys/block");
245 find_dev(temp);
246 strcpy(temp,"/sys/class");
247 find_dev(temp);
248
249 /* Hotplug */
250
251 } else {
252 action = getenv("ACTION");
253 env_path = getenv("DEVPATH");
Mike Frysingera421ba82006-02-03 00:25:37 +0000254 if (!action || !env_path)
255 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000256
Rob Landleyef10d522006-06-26 14:11:33 +0000257 sprintf(temp, "/sys%s", env_path);
258 if (!strcmp(action, "add")) make_device(temp,0);
259 else if (!strcmp(action, "remove")) make_device(temp,1);
Rob Landley29e08ff2006-01-12 06:13:50 +0000260 }
261
Mike Frysingera421ba82006-02-03 00:25:37 +0000262 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000263 return 0;
264}