blob: c100e0fa6b0da72985049a64691d1729d321c9c4 [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);
Rob Landley70f7ef72005-12-13 08:21:33 +000045 close(fd);
Mike Frysinger53d57db2006-02-03 00:16:53 +000046 if (len<1) goto end;
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;
Mike Frysinger248d2222006-02-03 00:19:42 +0000173 size_t len=strlen(path);
174 struct dirent *entry;
Rob Landley70f7ef72005-12-13 08:21:33 +0000175
Mike Frysinger248d2222006-02-03 00:19:42 +0000176 if ((dir = opendir(path)) == NULL)
177 return;
Rob Landley70f7ef72005-12-13 08:21:33 +0000178
Mike Frysinger248d2222006-02-03 00:19:42 +0000179 while ((entry = readdir(dir)) != NULL) {
Rob Landley70f7ef72005-12-13 08:21:33 +0000180
181 /* Skip "." and ".." (also skips hidden files, which is ok) */
182
183 if (entry->d_name[0]=='.') continue;
184
185 if (entry->d_type == DT_DIR) {
186 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
187 find_dev(path);
Rob Landley70f7ef72005-12-13 08:21:33 +0000188 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000189
Rob Landley70f7ef72005-12-13 08:21:33 +0000190 /* If there's a dev entry, mknod it */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000191
Rob Landleye9277432006-01-22 23:14:16 +0000192 if (!strcmp(entry->d_name, "dev")) make_device(path);
Rob Landley70f7ef72005-12-13 08:21:33 +0000193 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000194
Rob Landley70f7ef72005-12-13 08:21:33 +0000195 closedir(dir);
196}
197
198int mdev_main(int argc, char *argv[])
199{
Rob Landley29e08ff2006-01-12 06:13:50 +0000200 char *action;
201 char *env_path;
202 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
203
204 /* Scan */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000205
Rob Landley29e08ff2006-01-12 06:13:50 +0000206 if (argc == 2 && !strcmp(argv[1],"-s")) {
207 strcpy(temp,"/sys/block");
208 find_dev(temp);
209 strcpy(temp,"/sys/class");
210 find_dev(temp);
211
212 /* Hotplug */
213
214 } else {
215 action = getenv("ACTION");
216 env_path = getenv("DEVPATH");
217 if (!action || !env_path) bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000218
Rob Landley29e08ff2006-01-12 06:13:50 +0000219 if (!strcmp(action, "add")) {
220 sprintf(temp, "/sys%s", env_path);
221 make_device(temp);
222 } else if (!strcmp(action, "remove")) {
223 sprintf(temp, "%s/%s", DEV_PATH, strrchr(env_path, '/') + 1);
224 unlink(temp);
225 }
226 }
227
228 if(ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
Rob Landley70f7ef72005-12-13 08:21:33 +0000229 return 0;
230}