blob: b8c0090fae72d34d21713933c48db010e7d1e240 [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"
Rob Landley70f7ef72005-12-13 08:21:33 +000026
27#include <busybox.h>
28
29/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
Rob Landleyb56c2852005-12-17 10:52:30 +000030static void make_device(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +000031{
Rob Landleyb56c2852005-12-17 10:52:30 +000032 char *device_name,*s;
Rob Landley70f7ef72005-12-13 08:21:33 +000033 int major,minor,type,len,fd;
Rob Landleyb56c2852005-12-17 10:52:30 +000034 int mode=0660;
35 uid_t uid=0;
36 gid_t gid=0;
Rob Landley70f7ef72005-12-13 08:21:33 +000037
38 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
39
40 /* Try to read major/minor string */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000041
Rob Landley70f7ef72005-12-13 08:21:33 +000042 snprintf(temp, PATH_MAX, "%s/dev", path);
43 fd = open(temp, O_RDONLY);
44 len = read(fd, temp, PATH_MAX-1);
45 if (len<1) goto end;
Rob Landley70f7ef72005-12-13 08:21:33 +000046 close(fd);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000047
Rob Landley70f7ef72005-12-13 08:21:33 +000048 /* Determine device name, type, major and minor */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000049
Rob Landley70f7ef72005-12-13 08:21:33 +000050 device_name = strrchr(path, '/') + 1;
51 type = strncmp(path+5, "block/" ,6) ? S_IFCHR : S_IFBLK;
Rob Landleye9277432006-01-22 23:14:16 +000052 if(sscanf(temp, "%d:%d", &major, &minor) != 2) goto end;
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) {
57 char *conf,*pos,*end;
58
59 /* mmap the config file */
60 if (-1!=(fd=open("/etc/mdev.conf",O_RDONLY))) {
61 len=lseek(fd,0,SEEK_END);
Mike Frysinger5990efb2006-01-04 07:31:19 +000062 conf=mmap(NULL,len,PROT_READ,MAP_PRIVATE,fd,0);
Rob Landleyb56c2852005-12-17 10:52:30 +000063 if (conf) {
64 int line=0;
65
66 /* Loop through lines in mmaped file*/
67 for (pos=conf;pos-conf<len;) {
68 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 */
73 for(end=pos;end-conf<len && *end!='\n';end++);
74
75 /* Three fields: regex, uid:gid, mode */
76 for (field=3;field;field--) {
77 /* Skip whitespace */
78 while (pos<end && isspace(*pos)) pos++;
79 if (pos==end || *pos=='#') break;
80 for (end2=pos;
81 end2<end && !isspace(*end2) && *end2!='#'; end2++);
82 switch(field) {
83 /* Regex to match this device */
84 case 3:
85 {
86 char *regex=strndupa(pos,end2-pos);
87 regex_t match;
88 regmatch_t off;
89 int result;
90
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000091 /* Is this it? */
Rob Landleyb56c2852005-12-17 10:52:30 +000092 xregcomp(&match,regex,REG_EXTENDED);
93 result=regexec(&match,device_name,1,&off,0);
94 regfree(&match);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000095
Rob Landleyb56c2852005-12-17 10:52:30 +000096 /* If not this device, skip rest of line */
97 if(result || off.rm_so
98 || off.rm_eo!=strlen(device_name))
99 goto end_line;
100
101 break;
102 }
103 /* uid:gid */
104 case 2:
105 {
106 char *s2;
107
108 /* Find : */
109 for(s=pos;s<end2 && *s!=':';s++);
110 if(s==end2) goto end_line;
111
112 /* Parse UID */
113 uid=strtoul(pos,&s2,10);
114 if(s!=s2) {
115 struct passwd *pass;
116 pass=getpwnam(strndupa(pos,s-pos));
117 if(!pass) goto end_line;
118 uid=pass->pw_uid;
119 }
120 s++;
121 /* parse GID */
122 gid=strtoul(s,&s2,10);
123 if(end2!=s2) {
124 struct group *grp;
125 grp=getgrnam(strndupa(s,end2-s));
126 if(!grp) goto end_line;
127 gid=grp->gr_gid;
128 }
129 break;
130 }
131 /* mode */
132 case 1:
133 {
134 mode=strtoul(pos,&pos,8);
135 if(pos!=end2) goto end_line;
136 goto found_device;
137 }
138 }
139 pos=end2;
140 }
141end_line:
142 /* Did everything parse happily? */
143 if (field && field!=3)
144 bb_error_msg_and_die("Bad line %d",line);
145
146 /* Next line */
147 pos=++end;
148 }
149found_device:
150 munmap(conf,len);
151 }
152 close(fd);
153 }
154 }
Rob Landley70f7ef72005-12-13 08:21:33 +0000155
156 sprintf(temp, "%s/%s", DEV_PATH, device_name);
Rob Landleyb56c2852005-12-17 10:52:30 +0000157 umask(0);
158 if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
Rob Landley70f7ef72005-12-13 08:21:33 +0000159 bb_perror_msg_and_die("mknod %s failed", temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000160
Rob Landleyb56c2852005-12-17 10:52:30 +0000161 if (ENABLE_FEATURE_MDEV_CONF) chown(temp,uid,gid);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000162
Rob Landley70f7ef72005-12-13 08:21:33 +0000163end:
164 RELEASE_CONFIG_BUFFER(temp);
165}
166
167/* Recursive search of /sys/block or /sys/class. path must be a writeable
168 * buffer of size PATH_MAX containing the directory string to start at. */
169
Rob Landleyb56c2852005-12-17 10:52:30 +0000170static void find_dev(char *path)
Rob Landley70f7ef72005-12-13 08:21:33 +0000171{
172 DIR *dir;
173 int len=strlen(path);
174
Rob Landleyb56c2852005-12-17 10:52:30 +0000175 if (!(dir = opendir(path)))
Rob Landley70f7ef72005-12-13 08:21:33 +0000176 bb_perror_msg_and_die("No %s",path);
177
Rob Landleyb56c2852005-12-17 10:52:30 +0000178 for (;;) {
Rob Landley70f7ef72005-12-13 08:21:33 +0000179 struct dirent *entry = readdir(dir);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000180
Rob Landleyb56c2852005-12-17 10:52:30 +0000181 if (!entry) break;
Rob Landley70f7ef72005-12-13 08:21:33 +0000182
183 /* Skip "." and ".." (also skips hidden files, which is ok) */
184
185 if (entry->d_name[0]=='.') continue;
186
187 if (entry->d_type == DT_DIR) {
188 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
189 find_dev(path);
190 path[len] = 0;
191 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000192
Rob Landley70f7ef72005-12-13 08:21:33 +0000193 /* If there's a dev entry, mknod it */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000194
Rob Landleye9277432006-01-22 23:14:16 +0000195 if (!strcmp(entry->d_name, "dev")) make_device(path);
Rob Landley70f7ef72005-12-13 08:21:33 +0000196 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000197
Rob Landley70f7ef72005-12-13 08:21:33 +0000198 closedir(dir);
199}
200
201int mdev_main(int argc, char *argv[])
202{
Rob Landley29e08ff2006-01-12 06:13:50 +0000203 char *action;
204 char *env_path;
205 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
206
207 /* Scan */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000208
Rob Landley29e08ff2006-01-12 06:13:50 +0000209 if (argc == 2 && !strcmp(argv[1],"-s")) {
210 strcpy(temp,"/sys/block");
211 find_dev(temp);
212 strcpy(temp,"/sys/class");
213 find_dev(temp);
214
215 /* Hotplug */
216
217 } else {
218 action = getenv("ACTION");
219 env_path = getenv("DEVPATH");
220 if (!action || !env_path) bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000221
Rob Landley29e08ff2006-01-12 06:13:50 +0000222 if (!strcmp(action, "add")) {
223 sprintf(temp, "/sys%s", env_path);
224 make_device(temp);
225 } else if (!strcmp(action, "remove")) {
226 sprintf(temp, "%s/%s", DEV_PATH, strrchr(env_path, '/') + 1);
227 unlink(temp);
228 }
229 }
230
231 if(ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000232 return 0;
233}