blob: 73a82314c3cf3ef572ffcdf7b697898c83c2c06f [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 Landley5cd1ccd2006-05-21 18:33:27 +000031struct mdev_globals
32{
33 int root_major, root_minor;
34} mdev_globals;
35
36#define bbg mdev_globals
Rob Landleya7e3d052006-02-21 06:11:13 +000037
Rob Landley70f7ef72005-12-13 08:21:33 +000038/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
Rob Landleyb56c2852005-12-17 10:52:30 +000039static void make_device(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +000040{
Mike Frysingera421ba82006-02-03 00:25:37 +000041 char *device_name, *s;
42 int major, minor, type, len, fd;
43 int mode = 0660;
44 uid_t uid = 0;
45 gid_t gid = 0;
Rob Landley15fe2e12006-05-08 02:53:23 +000046 char *temp = path + strlen(path);
Rob Landley70f7ef72005-12-13 08:21:33 +000047
Rob Landley15fe2e12006-05-08 02:53:23 +000048 /* Try to read major/minor string. Note that the kernel puts \n after
49 * the data, so we don't need to worry about null terminating the string
50 * because sscanf() will stop at the first nondigit, which \n is. We
51 * also depend on path having writeable space after it. */
Rob Landley70f7ef72005-12-13 08:21:33 +000052
Rob Landley15fe2e12006-05-08 02:53:23 +000053 strcat(path, "/dev");
54 fd = open(path, O_RDONLY);
55 len = read(fd, temp + 1, 64);
56 *temp++ = 0;
Rob Landley70f7ef72005-12-13 08:21:33 +000057 close(fd);
Rob Landley15fe2e12006-05-08 02:53:23 +000058 if (len < 1) return;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000059
Rob Landley70f7ef72005-12-13 08:21:33 +000060 /* Determine device name, type, major and minor */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000061
Rob Landley70f7ef72005-12-13 08:21:33 +000062 device_name = strrchr(path, '/') + 1;
Rob Landley15fe2e12006-05-08 02:53:23 +000063 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
64 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
Rob Landley70f7ef72005-12-13 08:21:33 +000065
Rob Landleyb56c2852005-12-17 10:52:30 +000066 /* If we have a config file, look up permissions for this device */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000067
Rob Landleyb56c2852005-12-17 10:52:30 +000068 if (ENABLE_FEATURE_MDEV_CONF) {
Mike Frysingera421ba82006-02-03 00:25:37 +000069 char *conf, *pos, *end;
Rob Landleyb56c2852005-12-17 10:52:30 +000070
71 /* mmap the config file */
Mike Frysingera421ba82006-02-03 00:25:37 +000072 if (-1 != (fd=open(MDEV_CONF,O_RDONLY))) {
73 len = lseek(fd, 0, SEEK_END);
74 conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
Rob Landleyb56c2852005-12-17 10:52:30 +000075 if (conf) {
Mike Frysingera421ba82006-02-03 00:25:37 +000076 int line = 0;
Rob Landleyb56c2852005-12-17 10:52:30 +000077
78 /* Loop through lines in mmaped file*/
Mike Frysingera421ba82006-02-03 00:25:37 +000079 for (pos=conf; pos-conf<len;) {
Rob Landleyb56c2852005-12-17 10:52:30 +000080 int field;
81 char *end2;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000082
Rob Landleyb56c2852005-12-17 10:52:30 +000083 line++;
84 /* find end of this line */
Mike Frysingera421ba82006-02-03 00:25:37 +000085 for(end=pos; end-conf<len && *end!='\n'; end++)
86 ;
Rob Landleyb56c2852005-12-17 10:52:30 +000087
88 /* Three fields: regex, uid:gid, mode */
Mike Frysingera421ba82006-02-03 00:25:37 +000089 for (field=3; field; field--) {
Rob Landleyb56c2852005-12-17 10:52:30 +000090 /* Skip whitespace */
Mike Frysingera421ba82006-02-03 00:25:37 +000091 while (pos<end && isspace(*pos))
92 pos++;
93 if (pos==end || *pos=='#')
94 break;
95 for (end2=pos; end2<end && !isspace(*end2) && *end2!='#'; end2++)
96 ;
97
98 switch (field) {
Rob Landleyb56c2852005-12-17 10:52:30 +000099 /* Regex to match this device */
100 case 3:
101 {
Mike Frysingera421ba82006-02-03 00:25:37 +0000102 char *regex = strndupa(pos,end2-pos);
Rob Landleyb56c2852005-12-17 10:52:30 +0000103 regex_t match;
104 regmatch_t off;
105 int result;
106
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000107 /* Is this it? */
Rob Landleyb56c2852005-12-17 10:52:30 +0000108 xregcomp(&match,regex,REG_EXTENDED);
Mike Frysingera421ba82006-02-03 00:25:37 +0000109 result = regexec(&match,device_name,1,&off,0);
Rob Landleyb56c2852005-12-17 10:52:30 +0000110 regfree(&match);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000111
Rob Landleyb56c2852005-12-17 10:52:30 +0000112 /* If not this device, skip rest of line */
Mike Frysingera421ba82006-02-03 00:25:37 +0000113 if (result || off.rm_so || off.rm_eo!=strlen(device_name))
114 goto end_line;
Rob Landleyb56c2852005-12-17 10:52:30 +0000115
116 break;
117 }
118 /* uid:gid */
119 case 2:
120 {
121 char *s2;
122
123 /* Find : */
Mike Frysingera421ba82006-02-03 00:25:37 +0000124 for(s=pos; s<end2 && *s!=':'; s++)
125 ;
126 if (s == end2)
127 goto end_line;
Rob Landleyb56c2852005-12-17 10:52:30 +0000128
129 /* Parse UID */
Mike Frysingera421ba82006-02-03 00:25:37 +0000130 uid = strtoul(pos,&s2,10);
131 if (s != s2) {
Rob Landleyb56c2852005-12-17 10:52:30 +0000132 struct passwd *pass;
Mike Frysingera421ba82006-02-03 00:25:37 +0000133 pass = getpwnam(strndupa(pos,s-pos));
134 if (!pass)
135 goto end_line;
136 uid = pass->pw_uid;
Rob Landleyb56c2852005-12-17 10:52:30 +0000137 }
138 s++;
139 /* parse GID */
Mike Frysingera421ba82006-02-03 00:25:37 +0000140 gid = strtoul(s,&s2,10);
141 if (end2 != s2) {
Rob Landleyb56c2852005-12-17 10:52:30 +0000142 struct group *grp;
Mike Frysingera421ba82006-02-03 00:25:37 +0000143 grp = getgrnam(strndupa(s,end2-s));
144 if (!grp)
145 goto end_line;
146 gid = grp->gr_gid;
Rob Landleyb56c2852005-12-17 10:52:30 +0000147 }
148 break;
149 }
150 /* mode */
151 case 1:
152 {
Mike Frysingera421ba82006-02-03 00:25:37 +0000153 mode = strtoul(pos,&pos,8);
154 if (pos != end2)
155 goto end_line;
156 else
157 goto found_device;
Rob Landleyb56c2852005-12-17 10:52:30 +0000158 }
159 }
Mike Frysingera421ba82006-02-03 00:25:37 +0000160 pos = end2;
Rob Landleyb56c2852005-12-17 10:52:30 +0000161 }
162end_line:
163 /* Did everything parse happily? */
164 if (field && field!=3)
Mike Frysingera421ba82006-02-03 00:25:37 +0000165 bb_error_msg_and_die("Bad line %d",line);
Rob Landleyb56c2852005-12-17 10:52:30 +0000166
167 /* Next line */
Mike Frysingera421ba82006-02-03 00:25:37 +0000168 pos = ++end;
Rob Landleyb56c2852005-12-17 10:52:30 +0000169 }
170found_device:
Mike Frysingera421ba82006-02-03 00:25:37 +0000171 munmap(conf, len);
Rob Landleyb56c2852005-12-17 10:52:30 +0000172 }
173 close(fd);
174 }
175 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000176
Rob Landleyb56c2852005-12-17 10:52:30 +0000177 umask(0);
Rob Landley15fe2e12006-05-08 02:53:23 +0000178 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
179 bb_perror_msg_and_die("mknod %s failed", device_name);
Rob Landley70f7ef72005-12-13 08:21:33 +0000180
Rob Landley5cd1ccd2006-05-21 18:33:27 +0000181 if (major==bbg.root_major && minor==bbg.root_minor)
Rob Landley15fe2e12006-05-08 02:53:23 +0000182 symlink(device_name, "root");
Rob Landleya7e3d052006-02-21 06:11:13 +0000183
Rob Landley15fe2e12006-05-08 02:53:23 +0000184 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
Rob Landley70f7ef72005-12-13 08:21:33 +0000185}
186
187/* Recursive search of /sys/block or /sys/class. path must be a writeable
188 * buffer of size PATH_MAX containing the directory string to start at. */
189
Rob Landleyb56c2852005-12-17 10:52:30 +0000190static void find_dev(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +0000191{
192 DIR *dir;
Mike Frysingera421ba82006-02-03 00:25:37 +0000193 size_t len = strlen(path);
Mike Frysinger248d2222006-02-03 00:19:42 +0000194 struct dirent *entry;
Rob Landley70f7ef72005-12-13 08:21:33 +0000195
Mike Frysinger248d2222006-02-03 00:19:42 +0000196 if ((dir = opendir(path)) == NULL)
197 return;
Rob Landley70f7ef72005-12-13 08:21:33 +0000198
Mike Frysinger248d2222006-02-03 00:19:42 +0000199 while ((entry = readdir(dir)) != NULL) {
Rob Landley0960ca72006-06-13 18:27:16 +0000200 struct stat st;
Rob Landley70f7ef72005-12-13 08:21:33 +0000201
202 /* Skip "." and ".." (also skips hidden files, which is ok) */
203
Mike Frysingera421ba82006-02-03 00:25:37 +0000204 if (entry->d_name[0] == '.')
205 continue;
Rob Landley70f7ef72005-12-13 08:21:33 +0000206
Rob Landley0960ca72006-06-13 18:27:16 +0000207 // uClibc doesn't fill out entry->d_type reliably. so we use lstat().
208
209 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
210 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
211 path[len] = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000212
Rob Landley70f7ef72005-12-13 08:21:33 +0000213 /* If there's a dev entry, mknod it */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000214
Rob Landleye9277432006-01-22 23:14:16 +0000215 if (!strcmp(entry->d_name, "dev")) make_device(path);
Rob Landley70f7ef72005-12-13 08:21:33 +0000216 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000217
Rob Landley70f7ef72005-12-13 08:21:33 +0000218 closedir(dir);
219}
220
221int mdev_main(int argc, char *argv[])
222{
Rob Landley29e08ff2006-01-12 06:13:50 +0000223 char *action;
224 char *env_path;
225 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
226
Rob Landley15fe2e12006-05-08 02:53:23 +0000227 bb_xchdir(DEV_PATH);
228
Rob Landley29e08ff2006-01-12 06:13:50 +0000229 /* Scan */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000230
Rob Landley29e08ff2006-01-12 06:13:50 +0000231 if (argc == 2 && !strcmp(argv[1],"-s")) {
Rob Landleya7e3d052006-02-21 06:11:13 +0000232 struct stat st;
233
234 stat("/", &st); // If this fails, we have bigger problems.
Rob Landley5cd1ccd2006-05-21 18:33:27 +0000235 bbg.root_major=major(st.st_dev);
236 bbg.root_minor=minor(st.st_dev);
Rob Landley29e08ff2006-01-12 06:13:50 +0000237 strcpy(temp,"/sys/block");
238 find_dev(temp);
239 strcpy(temp,"/sys/class");
240 find_dev(temp);
241
242 /* Hotplug */
243
244 } else {
245 action = getenv("ACTION");
246 env_path = getenv("DEVPATH");
Mike Frysingera421ba82006-02-03 00:25:37 +0000247 if (!action || !env_path)
248 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000249
Rob Landley29e08ff2006-01-12 06:13:50 +0000250 if (!strcmp(action, "add")) {
251 sprintf(temp, "/sys%s", env_path);
252 make_device(temp);
253 } else if (!strcmp(action, "remove")) {
Rob Landley15fe2e12006-05-08 02:53:23 +0000254 unlink(strrchr(env_path, '/') + 1);
Rob Landley29e08ff2006-01-12 06:13:50 +0000255 }
256 }
257
Mike Frysingera421ba82006-02-03 00:25:37 +0000258 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000259 return 0;
260}