blob: c77c122b545d862abea43303e68326b6ee4019fd [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 *
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 */
11
Rob Landleyb56c2852005-12-17 10:52:30 +000012#include "busybox.h"
Rob Landleyef10d522006-06-26 14:11:33 +000013#include <ctype.h>
14#include <errno.h>
15#include <sys/mman.h>
16#include <sys/sysmacros.h>
Rob Landleyb56c2852005-12-17 10:52:30 +000017#include "xregex.h"
Rob Landley70f7ef72005-12-13 08:21:33 +000018
Rob Landley9bdd7422005-12-17 10:54:17 +000019#define DEV_PATH "/dev"
Rob Landley70f7ef72005-12-13 08:21:33 +000020
Rob Landley5cd1ccd2006-05-21 18:33:27 +000021struct mdev_globals
22{
23 int root_major, root_minor;
24} mdev_globals;
25
26#define bbg mdev_globals
Rob Landleya7e3d052006-02-21 06:11:13 +000027
Rob Landley70f7ef72005-12-13 08:21:33 +000028/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
Rob Landleyef10d522006-06-26 14:11:33 +000029static void make_device(char *path, int delete)
Rob Landley70f7ef72005-12-13 08:21:33 +000030{
Rob Landleyef10d522006-06-26 14:11:33 +000031 char *device_name;
Mike Frysingera421ba82006-02-03 00:25:37 +000032 int major, minor, type, len, fd;
33 int mode = 0660;
34 uid_t uid = 0;
35 gid_t gid = 0;
Rob Landley15fe2e12006-05-08 02:53:23 +000036 char *temp = path + strlen(path);
Rob Landleyef10d522006-06-26 14:11:33 +000037 char *command = NULL;
Rob Landley70f7ef72005-12-13 08:21:33 +000038
Rob Landley15fe2e12006-05-08 02:53:23 +000039 /* Try to read major/minor string. Note that the kernel puts \n after
40 * the data, so we don't need to worry about null terminating the string
41 * because sscanf() will stop at the first nondigit, which \n is. We
42 * also depend on path having writeable space after it. */
Rob Landley70f7ef72005-12-13 08:21:33 +000043
Rob Landley15fe2e12006-05-08 02:53:23 +000044 strcat(path, "/dev");
45 fd = open(path, O_RDONLY);
46 len = read(fd, temp + 1, 64);
47 *temp++ = 0;
Rob Landley70f7ef72005-12-13 08:21:33 +000048 close(fd);
Rob Landley15fe2e12006-05-08 02:53:23 +000049 if (len < 1) return;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000050
Rob Landley70f7ef72005-12-13 08:21:33 +000051 /* Determine device name, type, major and minor */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000052
Rob Landley70f7ef72005-12-13 08:21:33 +000053 device_name = strrchr(path, '/') + 1;
Rob Landley15fe2e12006-05-08 02:53:23 +000054 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
55 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
Rob Landley70f7ef72005-12-13 08:21:33 +000056
Rob Landleyb56c2852005-12-17 10:52:30 +000057 /* If we have a config file, look up permissions for this device */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000058
Rob Landleyb56c2852005-12-17 10:52:30 +000059 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingera421ba82006-02-03 00:25:37 +000060 char *conf, *pos, *end;
Rob Landleyb56c2852005-12-17 10:52:30 +000061
62 /* mmap the config file */
Rob Landleyef10d522006-06-26 14:11:33 +000063 if (-1 != (fd=open("/etc/mdev.conf",O_RDONLY))) {
Mike Frysingera421ba82006-02-03 00:25:37 +000064 len = lseek(fd, 0, SEEK_END);
65 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
Rob Landleyb56c2852005-12-17 10:52:30 +000066 if (conf) {
Mike Frysingera421ba82006-02-03 00:25:37 +000067 int line = 0;
Rob Landleyb56c2852005-12-17 10:52:30 +000068
69 /* Loop through lines in mmaped file*/
Mike Frysingera421ba82006-02-03 00:25:37 +000070 for (pos=conf; pos-conf<len;) {
Rob Landleyb56c2852005-12-17 10:52:30 +000071 int field;
72 char *end2;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000073
Rob Landleyb56c2852005-12-17 10:52:30 +000074 line++;
75 /* find end of this line */
Mike Frysingera421ba82006-02-03 00:25:37 +000076 for(end=pos; end-conf<len && *end!='\n'; end++)
77 ;
Rob Landleyb56c2852005-12-17 10:52:30 +000078
79 /* Three fields: regex, uid:gid, mode */
Rob Landleyef10d522006-06-26 14:11:33 +000080 for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
81 field++)
82 {
Rob Landleyb56c2852005-12-17 10:52:30 +000083 /* Skip whitespace */
Rob Landleyef10d522006-06-26 14:11:33 +000084 while (pos<end && isspace(*pos)) pos++;
85 if (pos==end || *pos=='#') break;
86 for (end2=pos;
87 end2<end && !isspace(*end2) && *end2!='#'; end2++)
Mike Frysingera421ba82006-02-03 00:25:37 +000088 ;
89
Rob Landleyef10d522006-06-26 14:11:33 +000090 if (!field) {
Rob Landleyb56c2852005-12-17 10:52:30 +000091 /* Regex to match this device */
Rob Landleyb56c2852005-12-17 10:52:30 +000092
Rob Landleyef10d522006-06-26 14:11:33 +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
Rob Landleyef10d522006-06-26 14:11:33 +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
Rob Landleyef10d522006-06-26 14:11:33 +0000103 /* If not this device, skip rest of line */
104 if (result || off.rm_so
105 || off.rm_eo != strlen(device_name))
Rob Landleyb56c2852005-12-17 10:52:30 +0000106 break;
Rob Landleyef10d522006-06-26 14:11:33 +0000107
108 } else if (field == 1) {
Rob Landleyb56c2852005-12-17 10:52:30 +0000109 /* uid:gid */
Rob Landleyb56c2852005-12-17 10:52:30 +0000110
Rob Landleyef10d522006-06-26 14:11:33 +0000111 char *s, *s2;
Rob Landleyb56c2852005-12-17 10:52:30 +0000112
Rob Landleyef10d522006-06-26 14:11:33 +0000113 /* Find : */
114 for(s=pos; s<end2 && *s!=':'; s++)
115 ;
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 } else if (field == 2) {
136 /* mode */
137
138 mode = strtoul(pos, &pos, 8);
139 if (pos != end2) break;
140 } else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
141 // Command to run
142 char *s = "@$*", *s2;
143 if (!(s2 = strchr(s, *pos++))) {
144 // Force error
145 field = 1;
Rob Landleyb56c2852005-12-17 10:52:30 +0000146 break;
147 }
Rob Landleyef10d522006-06-26 14:11:33 +0000148 if ((s2-s+1) & (1<<delete))
149 command = bb_xstrndup(pos, end-pos);
Rob Landleyb56c2852005-12-17 10:52:30 +0000150 }
Rob Landleyef10d522006-06-26 14:11:33 +0000151
Mike Frysingera421ba82006-02-03 00:25:37 +0000152 pos = end2;
Rob Landleyb56c2852005-12-17 10:52:30 +0000153 }
Rob Landleyef10d522006-06-26 14:11:33 +0000154
Rob Landleyb56c2852005-12-17 10:52:30 +0000155 /* Did everything parse happily? */
Rob Landleyef10d522006-06-26 14:11:33 +0000156
157 if (field > 2) break;
158 if (field) 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 }
Mike Frysingera421ba82006-02-03 00:25:37 +0000163 munmap(conf, len);
Rob Landleyb56c2852005-12-17 10:52:30 +0000164 }
165 close(fd);
166 }
167 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000168
Rob Landleyb56c2852005-12-17 10:52:30 +0000169 umask(0);
Rob Landleyef10d522006-06-26 14:11:33 +0000170 if (!delete) {
171 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
172 bb_perror_msg_and_die("mknod %s failed", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000173
Rob Landleyef10d522006-06-26 14:11:33 +0000174 if (major == bbg.root_major && minor == bbg.root_minor)
175 symlink(device_name, "root");
Rob Landleya7e3d052006-02-21 06:11:13 +0000176
Rob Landleyef10d522006-06-26 14:11:33 +0000177 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
178 }
179 if (command) {
180 int rc;
181 char *s;
182
183 s=bb_xasprintf("MDEV=%s",device_name);
184 putenv(s);
185 rc = system(command);
186 s[4]=0;
187 putenv(s);
188 free(s);
189 free(command);
190 if (rc == -1) bb_perror_msg_and_die("Couldn't run %s", command);
191 }
192 if (delete) unlink(device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000193}
194
195/* Recursive search of /sys/block or /sys/class. path must be a writeable
196 * buffer of size PATH_MAX containing the directory string to start at. */
197
Rob Landleyb56c2852005-12-17 10:52:30 +0000198static void find_dev(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +0000199{
200 DIR *dir;
Mike Frysingera421ba82006-02-03 00:25:37 +0000201 size_t len = strlen(path);
Mike Frysinger248d2222006-02-03 00:19:42 +0000202 struct dirent *entry;
Rob Landley70f7ef72005-12-13 08:21:33 +0000203
Mike Frysinger248d2222006-02-03 00:19:42 +0000204 if ((dir = opendir(path)) == NULL)
205 return;
Rob Landley70f7ef72005-12-13 08:21:33 +0000206
Mike Frysinger248d2222006-02-03 00:19:42 +0000207 while ((entry = readdir(dir)) != NULL) {
Rob Landley0960ca72006-06-13 18:27:16 +0000208 struct stat st;
Rob Landley70f7ef72005-12-13 08:21:33 +0000209
210 /* Skip "." and ".." (also skips hidden files, which is ok) */
211
Mike Frysingera421ba82006-02-03 00:25:37 +0000212 if (entry->d_name[0] == '.')
213 continue;
Rob Landley70f7ef72005-12-13 08:21:33 +0000214
Rob Landley0960ca72006-06-13 18:27:16 +0000215 // uClibc doesn't fill out entry->d_type reliably. so we use lstat().
216
217 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
218 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
219 path[len] = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000220
Rob Landley70f7ef72005-12-13 08:21:33 +0000221 /* If there's a dev entry, mknod it */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000222
Rob Landleyef10d522006-06-26 14:11:33 +0000223 if (!strcmp(entry->d_name, "dev")) make_device(path, 0);
Rob Landley70f7ef72005-12-13 08:21:33 +0000224 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000225
Rob Landley70f7ef72005-12-13 08:21:33 +0000226 closedir(dir);
227}
228
229int mdev_main(int argc, char *argv[])
230{
Rob Landley29e08ff2006-01-12 06:13:50 +0000231 char *action;
232 char *env_path;
233 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
234
Rob Landley15fe2e12006-05-08 02:53:23 +0000235 bb_xchdir(DEV_PATH);
236
Rob Landley29e08ff2006-01-12 06:13:50 +0000237 /* Scan */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000238
Rob Landley29e08ff2006-01-12 06:13:50 +0000239 if (argc == 2 && !strcmp(argv[1],"-s")) {
Rob Landleya7e3d052006-02-21 06:11:13 +0000240 struct stat st;
241
242 stat("/", &st); // If this fails, we have bigger problems.
Rob Landley5cd1ccd2006-05-21 18:33:27 +0000243 bbg.root_major=major(st.st_dev);
244 bbg.root_minor=minor(st.st_dev);
Rob Landley29e08ff2006-01-12 06:13:50 +0000245 strcpy(temp,"/sys/block");
246 find_dev(temp);
247 strcpy(temp,"/sys/class");
248 find_dev(temp);
249
250 /* Hotplug */
251
252 } else {
253 action = getenv("ACTION");
254 env_path = getenv("DEVPATH");
Mike Frysingera421ba82006-02-03 00:25:37 +0000255 if (!action || !env_path)
256 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000257
Rob Landleyef10d522006-06-26 14:11:33 +0000258 sprintf(temp, "/sys%s", env_path);
259 if (!strcmp(action, "add")) make_device(temp,0);
260 else if (!strcmp(action, "remove")) make_device(temp,1);
Rob Landley29e08ff2006-01-12 06:13:50 +0000261 }
262
Mike Frysingera421ba82006-02-03 00:25:37 +0000263 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000264 return 0;
265}