blob: a4716e2da936e085a8a6eed62baab12472f78c7d [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
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;
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
Rob Landleyef10d522006-06-26 14:11:33 +0000177 if (major == bbg.root_major && minor == bbg.root_minor)
178 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) {
183 int rc;
184 char *s;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000185
Denis Vlasenkof46be092006-10-16 19:39:37 +0000186 s = xasprintf("MDEV=%s", device_name);
Rob Landleyef10d522006-06-26 14:11:33 +0000187 putenv(s);
188 rc = system(command);
Denis Vlasenkof46be092006-10-16 19:39:37 +0000189 s[4] = 0;
Rob Landleyef10d522006-06-26 14:11:33 +0000190 putenv(s);
191 free(s);
192 free(command);
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000193 if (rc == -1) bb_perror_msg_and_die("cannot run %s", command);
Rob Landleyef10d522006-06-26 14:11:33 +0000194 }
195 if (delete) unlink(device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000196}
197
198/* Recursive search of /sys/block or /sys/class. path must be a writeable
199 * buffer of size PATH_MAX containing the directory string to start at. */
200
Rob Landleyb56c2852005-12-17 10:52:30 +0000201static void find_dev(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +0000202{
203 DIR *dir;
Mike Frysingera421ba82006-02-03 00:25:37 +0000204 size_t len = strlen(path);
Mike Frysinger248d2222006-02-03 00:19:42 +0000205 struct dirent *entry;
Rob Landley70f7ef72005-12-13 08:21:33 +0000206
Denis Vlasenkof46be092006-10-16 19:39:37 +0000207 dir = opendir(path);
208 if (dir == NULL)
Mike Frysinger248d2222006-02-03 00:19:42 +0000209 return;
Rob Landley70f7ef72005-12-13 08:21:33 +0000210
Mike Frysinger248d2222006-02-03 00:19:42 +0000211 while ((entry = readdir(dir)) != NULL) {
Rob Landley0960ca72006-06-13 18:27:16 +0000212 struct stat st;
Rob Landley70f7ef72005-12-13 08:21:33 +0000213
214 /* Skip "." and ".." (also skips hidden files, which is ok) */
215
Mike Frysingera421ba82006-02-03 00:25:37 +0000216 if (entry->d_name[0] == '.')
217 continue;
Rob Landley70f7ef72005-12-13 08:21:33 +0000218
Rob Landley0960ca72006-06-13 18:27:16 +0000219 // uClibc doesn't fill out entry->d_type reliably. so we use lstat().
220
221 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
222 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
223 path[len] = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000224
Rob Landley70f7ef72005-12-13 08:21:33 +0000225 /* If there's a dev entry, mknod it */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000226
Rob Landleyef10d522006-06-26 14:11:33 +0000227 if (!strcmp(entry->d_name, "dev")) make_device(path, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000228 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000229
Rob Landley70f7ef72005-12-13 08:21:33 +0000230 closedir(dir);
231}
232
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000233int mdev_main(int argc, char **argv);
234int mdev_main(int argc, char **argv)
Rob Landley70f7ef72005-12-13 08:21:33 +0000235{
Rob Landley29e08ff2006-01-12 06:13:50 +0000236 char *action;
237 char *env_path;
238 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
239
Rob Landleyd921b2e2006-08-03 15:41:12 +0000240 xchdir(DEV_PATH);
Rob Landley15fe2e12006-05-08 02:53:23 +0000241
Rob Landley29e08ff2006-01-12 06:13:50 +0000242 /* Scan */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000243
Rob Landley29e08ff2006-01-12 06:13:50 +0000244 if (argc == 2 && !strcmp(argv[1],"-s")) {
Rob Landleya7e3d052006-02-21 06:11:13 +0000245 struct stat st;
246
Denis Vlasenkof46be092006-10-16 19:39:37 +0000247 xstat("/", &st);
248 bbg.root_major = major(st.st_dev);
249 bbg.root_minor = minor(st.st_dev);
Rob Landley29e08ff2006-01-12 06:13:50 +0000250 strcpy(temp,"/sys/block");
251 find_dev(temp);
252 strcpy(temp,"/sys/class");
253 find_dev(temp);
254
255 /* Hotplug */
256
257 } else {
258 action = getenv("ACTION");
259 env_path = getenv("DEVPATH");
Denis Vlasenko92758142006-10-03 19:56:34 +0000260 if (!action || !env_path)
Mike Frysingera421ba82006-02-03 00:25:37 +0000261 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000262
Rob Landleyef10d522006-06-26 14:11:33 +0000263 sprintf(temp, "/sys%s", env_path);
264 if (!strcmp(action, "add")) make_device(temp,0);
265 else if (!strcmp(action, "remove")) make_device(temp,1);
Rob Landley29e08ff2006-01-12 06:13:50 +0000266 }
267
Mike Frysingera421ba82006-02-03 00:25:37 +0000268 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000269 return 0;
270}