blob: b292e14b741b4e264565eab4175d87dcb69c0dd4 [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
Rob Landleyb56c2852005-12-17 10:52:30 +000012#include "busybox.h"
13#include "xregex.h"
Rob Landley70f7ef72005-12-13 08:21:33 +000014
Rob Landley9bdd7422005-12-17 10:54:17 +000015#define DEV_PATH "/dev"
Rob Landley70f7ef72005-12-13 08:21:33 +000016
Rob Landley5cd1ccd2006-05-21 18:33:27 +000017struct mdev_globals
18{
19 int root_major, root_minor;
20} mdev_globals;
21
22#define bbg mdev_globals
Rob Landleya7e3d052006-02-21 06:11:13 +000023
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;
Mike Frysingera421ba82006-02-03 00:25:37 +000028 int major, minor, type, len, fd;
29 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");
42 fd = open(path, O_RDONLY);
43 len = read(fd, temp + 1, 64);
44 *temp++ = 0;
45 close(fd);
46 if (len < 1) return;
47 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000048
Rob Landley70f7ef72005-12-13 08:21:33 +000049 /* Determine device name, type, major and minor */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000050
Rob Landley70f7ef72005-12-13 08:21:33 +000051 device_name = strrchr(path, '/') + 1;
Rob Landley15fe2e12006-05-08 02:53:23 +000052 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
Rob Landley70f7ef72005-12-13 08:21:33 +000053
Rob Landleyb56c2852005-12-17 10:52:30 +000054 /* If we have a config file, look up permissions for this device */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000055
Rob Landleyb56c2852005-12-17 10:52:30 +000056 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingera421ba82006-02-03 00:25:37 +000057 char *conf, *pos, *end;
Rob Landleyb56c2852005-12-17 10:52:30 +000058
59 /* mmap the config file */
Rob Landleyef10d522006-06-26 14:11:33 +000060 if (-1 != (fd=open("/etc/mdev.conf",O_RDONLY))) {
Mike Frysingera421ba82006-02-03 00:25:37 +000061 len = lseek(fd, 0, SEEK_END);
62 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
Rob Landleyb56c2852005-12-17 10:52:30 +000063 if (conf) {
Mike Frysingera421ba82006-02-03 00:25:37 +000064 int line = 0;
Rob Landleyb56c2852005-12-17 10:52:30 +000065
66 /* Loop through lines in mmaped file*/
Mike Frysingera421ba82006-02-03 00:25:37 +000067 for (pos=conf; pos-conf<len;) {
Rob Landleyb56c2852005-12-17 10:52:30 +000068 int field;
69 char *end2;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000070
Rob Landleyb56c2852005-12-17 10:52:30 +000071 line++;
72 /* find end of this line */
Mike Frysingera421ba82006-02-03 00:25:37 +000073 for(end=pos; end-conf<len && *end!='\n'; end++)
74 ;
Rob Landleyb56c2852005-12-17 10:52:30 +000075
76 /* Three fields: regex, uid:gid, mode */
Rob Landleyef10d522006-06-26 14:11:33 +000077 for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
78 field++)
79 {
Rob Landleyb56c2852005-12-17 10:52:30 +000080 /* Skip whitespace */
Rob Landleyef10d522006-06-26 14:11:33 +000081 while (pos<end && isspace(*pos)) pos++;
82 if (pos==end || *pos=='#') break;
83 for (end2=pos;
84 end2<end && !isspace(*end2) && *end2!='#'; end2++)
Mike Frysingera421ba82006-02-03 00:25:37 +000085 ;
86
Rob Landleyef10d522006-06-26 14:11:33 +000087 if (!field) {
Rob Landleyb56c2852005-12-17 10:52:30 +000088 /* Regex to match this device */
Rob Landleyb56c2852005-12-17 10:52:30 +000089
Rob Landleyef10d522006-06-26 14:11:33 +000090 char *regex = strndupa(pos, end2-pos);
91 regex_t match;
92 regmatch_t off;
93 int result;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000094
Rob Landleyef10d522006-06-26 14:11:33 +000095 /* Is this it? */
96 xregcomp(&match,regex, REG_EXTENDED);
97 result = regexec(&match, device_name, 1, &off, 0);
98 regfree(&match);
Rob Landleyb56c2852005-12-17 10:52:30 +000099
Rob Landleyef10d522006-06-26 14:11:33 +0000100 /* If not this device, skip rest of line */
101 if (result || off.rm_so
102 || off.rm_eo != strlen(device_name))
Rob Landleyb56c2852005-12-17 10:52:30 +0000103 break;
Rob Landleyef10d522006-06-26 14:11:33 +0000104
105 } else if (field == 1) {
Rob Landleyb56c2852005-12-17 10:52:30 +0000106 /* uid:gid */
Rob Landleyb56c2852005-12-17 10:52:30 +0000107
Rob Landleyef10d522006-06-26 14:11:33 +0000108 char *s, *s2;
Rob Landleyb56c2852005-12-17 10:52:30 +0000109
Rob Landleyef10d522006-06-26 14:11:33 +0000110 /* Find : */
111 for(s=pos; s<end2 && *s!=':'; s++)
112 ;
113 if (s == end2) break;
114
115 /* Parse UID */
116 uid = strtoul(pos,&s2,10);
117 if (s != s2) {
118 struct passwd *pass;
119 pass = getpwnam(strndupa(pos, s-pos));
120 if (!pass) break;
121 uid = pass->pw_uid;
122 }
123 s++;
124 /* parse GID */
125 gid = strtoul(s, &s2, 10);
126 if (end2 != s2) {
127 struct group *grp;
128 grp = getgrnam(strndupa(s, end2-s));
129 if (!grp) break;
130 gid = grp->gr_gid;
131 }
132 } else if (field == 2) {
133 /* mode */
134
135 mode = strtoul(pos, &pos, 8);
136 if (pos != end2) break;
137 } else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
138 // Command to run
139 char *s = "@$*", *s2;
140 if (!(s2 = strchr(s, *pos++))) {
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000141 // Force error
Rob Landleyef10d522006-06-26 14:11:33 +0000142 field = 1;
Rob Landleyb56c2852005-12-17 10:52:30 +0000143 break;
144 }
Rob Landleyef10d522006-06-26 14:11:33 +0000145 if ((s2-s+1) & (1<<delete))
Rob Landleyd921b2e2006-08-03 15:41:12 +0000146 command = xstrndup(pos, end-pos);
Rob Landleyb56c2852005-12-17 10:52:30 +0000147 }
Rob Landleyef10d522006-06-26 14:11:33 +0000148
Mike Frysingera421ba82006-02-03 00:25:37 +0000149 pos = end2;
Rob Landleyb56c2852005-12-17 10:52:30 +0000150 }
Rob Landleyef10d522006-06-26 14:11:33 +0000151
Rob Landleyb56c2852005-12-17 10:52:30 +0000152 /* Did everything parse happily? */
Rob Landleyef10d522006-06-26 14:11:33 +0000153
154 if (field > 2) break;
155 if (field) bb_error_msg_and_die("Bad line %d",line);
Rob Landleyb56c2852005-12-17 10:52:30 +0000156
157 /* Next line */
Mike Frysingera421ba82006-02-03 00:25:37 +0000158 pos = ++end;
Rob Landleyb56c2852005-12-17 10:52:30 +0000159 }
Mike Frysingera421ba82006-02-03 00:25:37 +0000160 munmap(conf, len);
Rob Landleyb56c2852005-12-17 10:52:30 +0000161 }
162 close(fd);
163 }
164 }
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) {
Rob Landley10b36f92006-08-10 01:09:37 +0000168 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
Rob Landleyef10d522006-06-26 14:11:33 +0000169 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
170 bb_perror_msg_and_die("mknod %s failed", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000171
Rob Landleyef10d522006-06-26 14:11:33 +0000172 if (major == bbg.root_major && minor == bbg.root_minor)
173 symlink(device_name, "root");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000174
Rob Landleyef10d522006-06-26 14:11:33 +0000175 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
176 }
177 if (command) {
178 int rc;
179 char *s;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000180
Rob Landleyd921b2e2006-08-03 15:41:12 +0000181 s=xasprintf("MDEV=%s",device_name);
Rob Landleyef10d522006-06-26 14:11:33 +0000182 putenv(s);
183 rc = system(command);
184 s[4]=0;
185 putenv(s);
186 free(s);
187 free(command);
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000188 if (rc == -1) bb_perror_msg_and_die("cannot run %s", command);
Rob Landleyef10d522006-06-26 14:11:33 +0000189 }
190 if (delete) unlink(device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000191}
192
193/* Recursive search of /sys/block or /sys/class. path must be a writeable
194 * buffer of size PATH_MAX containing the directory string to start at. */
195
Rob Landleyb56c2852005-12-17 10:52:30 +0000196static void find_dev(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +0000197{
198 DIR *dir;
Mike Frysingera421ba82006-02-03 00:25:37 +0000199 size_t len = strlen(path);
Mike Frysinger248d2222006-02-03 00:19:42 +0000200 struct dirent *entry;
Rob Landley70f7ef72005-12-13 08:21:33 +0000201
Mike Frysinger248d2222006-02-03 00:19:42 +0000202 if ((dir = opendir(path)) == NULL)
203 return;
Rob Landley70f7ef72005-12-13 08:21:33 +0000204
Mike Frysinger248d2222006-02-03 00:19:42 +0000205 while ((entry = readdir(dir)) != NULL) {
Rob Landley0960ca72006-06-13 18:27:16 +0000206 struct stat st;
Rob Landley70f7ef72005-12-13 08:21:33 +0000207
208 /* Skip "." and ".." (also skips hidden files, which is ok) */
209
Mike Frysingera421ba82006-02-03 00:25:37 +0000210 if (entry->d_name[0] == '.')
211 continue;
Rob Landley70f7ef72005-12-13 08:21:33 +0000212
Rob Landley0960ca72006-06-13 18:27:16 +0000213 // uClibc doesn't fill out entry->d_type reliably. so we use lstat().
214
215 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
216 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
217 path[len] = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000218
Rob Landley70f7ef72005-12-13 08:21:33 +0000219 /* If there's a dev entry, mknod it */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000220
Rob Landleyef10d522006-06-26 14:11:33 +0000221 if (!strcmp(entry->d_name, "dev")) make_device(path, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000222 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000223
Rob Landley70f7ef72005-12-13 08:21:33 +0000224 closedir(dir);
225}
226
227int mdev_main(int argc, char *argv[])
228{
Rob Landley29e08ff2006-01-12 06:13:50 +0000229 char *action;
230 char *env_path;
231 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
232
Rob Landleyd921b2e2006-08-03 15:41:12 +0000233 xchdir(DEV_PATH);
Rob Landley15fe2e12006-05-08 02:53:23 +0000234
Rob Landley29e08ff2006-01-12 06:13:50 +0000235 /* Scan */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000236
Rob Landley29e08ff2006-01-12 06:13:50 +0000237 if (argc == 2 && !strcmp(argv[1],"-s")) {
Rob Landleya7e3d052006-02-21 06:11:13 +0000238 struct stat st;
239
240 stat("/", &st); // If this fails, we have bigger problems.
Rob Landley5cd1ccd2006-05-21 18:33:27 +0000241 bbg.root_major=major(st.st_dev);
242 bbg.root_minor=minor(st.st_dev);
Rob Landley29e08ff2006-01-12 06:13:50 +0000243 strcpy(temp,"/sys/block");
244 find_dev(temp);
245 strcpy(temp,"/sys/class");
246 find_dev(temp);
247
248 /* Hotplug */
249
250 } else {
251 action = getenv("ACTION");
252 env_path = getenv("DEVPATH");
Mike Frysingera421ba82006-02-03 00:25:37 +0000253 if (!action || !env_path)
254 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000255
Rob Landleyef10d522006-06-26 14:11:33 +0000256 sprintf(temp, "/sys%s", env_path);
257 if (!strcmp(action, "add")) make_device(temp,0);
258 else if (!strcmp(action, "remove")) make_device(temp,1);
Rob Landley29e08ff2006-01-12 06:13:50 +0000259 }
260
Mike Frysingera421ba82006-02-03 00:25:37 +0000261 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000262 return 0;
263}