blob: f6d4d8df00c103ef4ec7af8bf699b75a9f6352c6 [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>
19#include <sys/types.h>
20#include <unistd.h>
Rob Landley29e08ff2006-01-12 06:13:50 +000021#include <stdlib.h>
Rob Landleyb56c2852005-12-17 10:52:30 +000022#include "busybox.h"
23#include "xregex.h"
Rob Landley70f7ef72005-12-13 08:21:33 +000024
Rob Landley9bdd7422005-12-17 10:54:17 +000025#define DEV_PATH "/dev"
Mike Frysingera421ba82006-02-03 00:25:37 +000026#define MDEV_CONF "/etc/mdev.conf"
Rob Landley70f7ef72005-12-13 08:21:33 +000027
28#include <busybox.h>
29
30/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
Rob Landleyb56c2852005-12-17 10:52:30 +000031static void make_device(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +000032{
Mike Frysingera421ba82006-02-03 00:25:37 +000033 char *device_name, *s;
34 int major, minor, type, len, fd;
35 int mode = 0660;
36 uid_t uid = 0;
37 gid_t gid = 0;
Rob Landley70f7ef72005-12-13 08:21:33 +000038
Mike Frysingera421ba82006-02-03 00:25:37 +000039 RESERVE_CONFIG_BUFFER(temp, PATH_MAX);
Rob Landley70f7ef72005-12-13 08:21:33 +000040
41 /* Try to read major/minor string */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000042
Rob Landley70f7ef72005-12-13 08:21:33 +000043 snprintf(temp, PATH_MAX, "%s/dev", path);
44 fd = open(temp, O_RDONLY);
45 len = read(fd, temp, PATH_MAX-1);
Rob Landley70f7ef72005-12-13 08:21:33 +000046 close(fd);
Mike Frysingera421ba82006-02-03 00:25:37 +000047 if (len < 1) goto end;
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;
Mike Frysingera421ba82006-02-03 00:25:37 +000052 type = strncmp(path+5, "block/", 6) ? S_IFCHR : S_IFBLK;
53 if (sscanf(temp, "%d:%d", &major, &minor) != 2)
54 goto end;
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 */
Mike Frysingera421ba82006-02-03 00:25:37 +000062 if (-1 != (fd=open(MDEV_CONF,O_RDONLY))) {
63 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 */
Mike Frysingera421ba82006-02-03 00:25:37 +000079 for (field=3; field; field--) {
Rob Landleyb56c2852005-12-17 10:52:30 +000080 /* Skip whitespace */
Mike Frysingera421ba82006-02-03 00:25:37 +000081 while (pos<end && isspace(*pos))
82 pos++;
83 if (pos==end || *pos=='#')
84 break;
85 for (end2=pos; end2<end && !isspace(*end2) && *end2!='#'; end2++)
86 ;
87
88 switch (field) {
Rob Landleyb56c2852005-12-17 10:52:30 +000089 /* Regex to match this device */
90 case 3:
91 {
Mike Frysingera421ba82006-02-03 00:25:37 +000092 char *regex = strndupa(pos,end2-pos);
Rob Landleyb56c2852005-12-17 10:52:30 +000093 regex_t match;
94 regmatch_t off;
95 int result;
96
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000097 /* Is this it? */
Rob Landleyb56c2852005-12-17 10:52:30 +000098 xregcomp(&match,regex,REG_EXTENDED);
Mike Frysingera421ba82006-02-03 00:25:37 +000099 result = regexec(&match,device_name,1,&off,0);
Rob Landleyb56c2852005-12-17 10:52:30 +0000100 regfree(&match);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000101
Rob Landleyb56c2852005-12-17 10:52:30 +0000102 /* If not this device, skip rest of line */
Mike Frysingera421ba82006-02-03 00:25:37 +0000103 if (result || off.rm_so || off.rm_eo!=strlen(device_name))
104 goto end_line;
Rob Landleyb56c2852005-12-17 10:52:30 +0000105
106 break;
107 }
108 /* uid:gid */
109 case 2:
110 {
111 char *s2;
112
113 /* Find : */
Mike Frysingera421ba82006-02-03 00:25:37 +0000114 for(s=pos; s<end2 && *s!=':'; s++)
115 ;
116 if (s == end2)
117 goto end_line;
Rob Landleyb56c2852005-12-17 10:52:30 +0000118
119 /* Parse UID */
Mike Frysingera421ba82006-02-03 00:25:37 +0000120 uid = strtoul(pos,&s2,10);
121 if (s != s2) {
Rob Landleyb56c2852005-12-17 10:52:30 +0000122 struct passwd *pass;
Mike Frysingera421ba82006-02-03 00:25:37 +0000123 pass = getpwnam(strndupa(pos,s-pos));
124 if (!pass)
125 goto end_line;
126 uid = pass->pw_uid;
Rob Landleyb56c2852005-12-17 10:52:30 +0000127 }
128 s++;
129 /* parse GID */
Mike Frysingera421ba82006-02-03 00:25:37 +0000130 gid = strtoul(s,&s2,10);
131 if (end2 != s2) {
Rob Landleyb56c2852005-12-17 10:52:30 +0000132 struct group *grp;
Mike Frysingera421ba82006-02-03 00:25:37 +0000133 grp = getgrnam(strndupa(s,end2-s));
134 if (!grp)
135 goto end_line;
136 gid = grp->gr_gid;
Rob Landleyb56c2852005-12-17 10:52:30 +0000137 }
138 break;
139 }
140 /* mode */
141 case 1:
142 {
Mike Frysingera421ba82006-02-03 00:25:37 +0000143 mode = strtoul(pos,&pos,8);
144 if (pos != end2)
145 goto end_line;
146 else
147 goto found_device;
Rob Landleyb56c2852005-12-17 10:52:30 +0000148 }
149 }
Mike Frysingera421ba82006-02-03 00:25:37 +0000150 pos = end2;
Rob Landleyb56c2852005-12-17 10:52:30 +0000151 }
152end_line:
153 /* Did everything parse happily? */
154 if (field && field!=3)
Mike Frysingera421ba82006-02-03 00:25:37 +0000155 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 }
160found_device:
Mike Frysingera421ba82006-02-03 00:25:37 +0000161 munmap(conf, len);
Rob Landleyb56c2852005-12-17 10:52:30 +0000162 }
163 close(fd);
164 }
165 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000166
167 sprintf(temp, "%s/%s", DEV_PATH, device_name);
Rob Landleyb56c2852005-12-17 10:52:30 +0000168 umask(0);
169 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
Rob Landley70f7ef72005-12-13 08:21:33 +0000170 bb_perror_msg_and_die("mknod %s failed", temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000171
Rob Landleyb56c2852005-12-17 10:52:30 +0000172 if (ENABLE_FEATURE_MDEV_CONF) chown(temp,uid,gid);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000173
Rob Landley70f7ef72005-12-13 08:21:33 +0000174end:
175 RELEASE_CONFIG_BUFFER(temp);
176}
177
178/* Recursive search of /sys/block or /sys/class. path must be a writeable
179 * buffer of size PATH_MAX containing the directory string to start at. */
180
Rob Landleyb56c2852005-12-17 10:52:30 +0000181static void find_dev(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +0000182{
183 DIR *dir;
Mike Frysingera421ba82006-02-03 00:25:37 +0000184 size_t len = strlen(path);
Mike Frysinger248d2222006-02-03 00:19:42 +0000185 struct dirent *entry;
Rob Landley70f7ef72005-12-13 08:21:33 +0000186
Mike Frysinger248d2222006-02-03 00:19:42 +0000187 if ((dir = opendir(path)) == NULL)
188 return;
Rob Landley70f7ef72005-12-13 08:21:33 +0000189
Mike Frysinger248d2222006-02-03 00:19:42 +0000190 while ((entry = readdir(dir)) != NULL) {
Rob Landley70f7ef72005-12-13 08:21:33 +0000191
192 /* Skip "." and ".." (also skips hidden files, which is ok) */
193
Mike Frysingera421ba82006-02-03 00:25:37 +0000194 if (entry->d_name[0] == '.')
195 continue;
Rob Landley70f7ef72005-12-13 08:21:33 +0000196
197 if (entry->d_type == DT_DIR) {
198 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
199 find_dev(path);
Mike Frysinger271aa402006-02-07 06:10:45 +0000200 path[len] = 0;
Rob Landley70f7ef72005-12-13 08:21:33 +0000201 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000202
Rob Landley70f7ef72005-12-13 08:21:33 +0000203 /* If there's a dev entry, mknod it */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000204
Rob Landleye9277432006-01-22 23:14:16 +0000205 if (!strcmp(entry->d_name, "dev")) make_device(path);
Rob Landley70f7ef72005-12-13 08:21:33 +0000206 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000207
Rob Landley70f7ef72005-12-13 08:21:33 +0000208 closedir(dir);
209}
210
211int mdev_main(int argc, char *argv[])
212{
Rob Landley29e08ff2006-01-12 06:13:50 +0000213 char *action;
214 char *env_path;
215 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
216
217 /* Scan */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000218
Rob Landley29e08ff2006-01-12 06:13:50 +0000219 if (argc == 2 && !strcmp(argv[1],"-s")) {
220 strcpy(temp,"/sys/block");
221 find_dev(temp);
222 strcpy(temp,"/sys/class");
223 find_dev(temp);
224
225 /* Hotplug */
226
227 } else {
228 action = getenv("ACTION");
229 env_path = getenv("DEVPATH");
Mike Frysingera421ba82006-02-03 00:25:37 +0000230 if (!action || !env_path)
231 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000232
Rob Landley29e08ff2006-01-12 06:13:50 +0000233 if (!strcmp(action, "add")) {
234 sprintf(temp, "/sys%s", env_path);
235 make_device(temp);
236 } else if (!strcmp(action, "remove")) {
237 sprintf(temp, "%s/%s", DEV_PATH, strrchr(env_path, '/') + 1);
238 unlink(temp);
239 }
240 }
241
Mike Frysingera421ba82006-02-03 00:25:37 +0000242 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000243 return 0;
244}