The major:minor read from dev ends with \n, need to trim that.
[oweals/busybox.git] / util-linux / mdev.c
1 /* vi:set ts=4:
2  * 
3  * mdev - Mini udev for busybox
4  * 
5  * 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
11 #include <ctype.h>
12 #include <dirent.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/mman.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include "busybox.h"
22 #include "xregex.h"
23
24 #define DEV_PATH        "/dev"
25
26 #include <busybox.h>
27
28 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
29 static void make_device(char *path)
30 {
31         char *device_name,*s;
32         int major,minor,type,len,fd;
33         int mode=0660;
34         uid_t uid=0;
35         gid_t gid=0;
36
37         RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
38
39         /* Try to read major/minor string */
40         
41         snprintf(temp, PATH_MAX, "%s/dev", path);
42         fd = open(temp, O_RDONLY);
43         len = read(fd, temp, PATH_MAX-1);
44         if (len<1) goto end;
45         temp[--len] = 0; // remove trailing \n
46         close(fd);
47         
48         /* Determine device name, type, major and minor */
49         
50         device_name = strrchr(path, '/') + 1;
51         type = strncmp(path+5, "block/" ,6) ? S_IFCHR : S_IFBLK;
52         major = minor = 0;
53         for (s = temp; *s; s++) {
54                 if (*s == ':') {
55                         major = minor;
56                         minor = 0;
57                 } else {
58                         minor *= 10;
59                         minor += (*s) - '0';
60                 }
61         }
62
63         /* If we have a config file, look up permissions for this device */
64         
65         if (ENABLE_FEATURE_MDEV_CONF) {
66                 char *conf,*pos,*end;
67
68                 /* mmap the config file */
69                 if (-1!=(fd=open("/etc/mdev.conf",O_RDONLY))) {
70                         len=lseek(fd,0,SEEK_END);
71                         conf=mmap(NULL,len,PROT_READ,MAP_PRIVATE,fd,0);
72                         if (conf) {
73                                 int line=0;
74
75                                 /* Loop through lines in mmaped file*/
76                                 for (pos=conf;pos-conf<len;) {
77                                         int field;
78                                         char *end2;
79                                         
80                                         line++;
81                                         /* find end of this line */
82                                         for(end=pos;end-conf<len && *end!='\n';end++);
83
84                                         /* Three fields: regex, uid:gid, mode */
85                                         for (field=3;field;field--) {
86                                                 /* Skip whitespace */
87                                                 while (pos<end && isspace(*pos)) pos++;
88                                                 if (pos==end || *pos=='#') break;
89                                                 for (end2=pos;
90                                                         end2<end && !isspace(*end2) && *end2!='#'; end2++);
91                                                 switch(field) {
92                                                         /* Regex to match this device */
93                                                         case 3:
94                                                         {
95                                                                 char *regex=strndupa(pos,end2-pos);
96                                                                 regex_t match;
97                                                                 regmatch_t off;
98                                                                 int result;
99
100                                                                 /* Is this it? */       
101                                                                 xregcomp(&match,regex,REG_EXTENDED);
102                                                                 result=regexec(&match,device_name,1,&off,0);
103                                                                 regfree(&match);
104                                                                 
105                                                                 /* If not this device, skip rest of line */
106                                                                 if(result || off.rm_so
107                                                                         || off.rm_eo!=strlen(device_name))
108                                                                                 goto end_line;
109
110                                                                 break;
111                                                         }
112                                                         /* uid:gid */
113                                                         case 2:
114                                                         {
115                                                                 char *s2;
116
117                                                                 /* Find : */
118                                                                 for(s=pos;s<end2 && *s!=':';s++);
119                                                                 if(s==end2) goto end_line;
120
121                                                                 /* Parse UID */
122                                                                 uid=strtoul(pos,&s2,10);
123                                                                 if(s!=s2) {
124                                                                         struct passwd *pass;
125                                                                         pass=getpwnam(strndupa(pos,s-pos));
126                                                                         if(!pass) goto end_line;
127                                                                         uid=pass->pw_uid;
128                                                                 }
129                                                                 s++;
130                                                                 /* parse GID */
131                                                                 gid=strtoul(s,&s2,10);
132                                                                 if(end2!=s2) {
133                                                                         struct group *grp;
134                                                                         grp=getgrnam(strndupa(s,end2-s));
135                                                                         if(!grp) goto end_line;
136                                                                         gid=grp->gr_gid;
137                                                                 }
138                                                                 break;
139                                                         }
140                                                         /* mode */
141                                                         case 1:
142                                                         {
143                                                                 mode=strtoul(pos,&pos,8);
144                                                                 if(pos!=end2) goto end_line;
145                                                                 goto found_device;
146                                                         }
147                                                 }
148                                                 pos=end2;
149                                         }
150 end_line:
151                                         /* Did everything parse happily? */
152                                         if (field && field!=3)
153                                                         bb_error_msg_and_die("Bad line %d",line);
154
155                                         /* Next line */
156                                         pos=++end;
157                                 }
158 found_device:
159                                 munmap(conf,len);
160                         }
161                         close(fd);
162                 }
163         }
164
165         sprintf(temp, "%s/%s", DEV_PATH, device_name);
166         umask(0);
167         if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
168                 bb_perror_msg_and_die("mknod %s failed", temp);
169
170         if (ENABLE_FEATURE_MDEV_CONF) chown(temp,uid,gid);
171         
172 end:
173         RELEASE_CONFIG_BUFFER(temp);
174 }
175
176 /* Recursive search of /sys/block or /sys/class.  path must be a writeable
177  * buffer of size PATH_MAX containing the directory string to start at. */
178
179 static void find_dev(char *path)
180 {
181         DIR *dir;
182         int len=strlen(path);
183
184         if (!(dir = opendir(path)))
185                 bb_perror_msg_and_die("No %s",path);
186
187         for (;;) {
188                 struct dirent *entry = readdir(dir);
189                 
190                 if (!entry) break;
191
192                 /* Skip "." and ".." (also skips hidden files, which is ok) */
193
194                 if (entry->d_name[0]=='.') continue;
195
196                 if (entry->d_type == DT_DIR) {
197                         snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
198                         find_dev(path);
199                         path[len] = 0;
200                 }
201                 
202                 /* If there's a dev entry, mknod it */
203                 
204                 if (strcmp(entry->d_name, "dev")) make_device(path);
205         }
206         
207         closedir(dir);
208 }
209
210 int mdev_main(int argc, char *argv[])
211 {
212         if (argc > 1) {
213                 if (argc == 2 && !strcmp(argv[1],"-s")) {
214                         RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
215                         strcpy(temp,"/sys/block");
216                         find_dev(temp);
217                         strcpy(temp,"/sys/class");
218                         find_dev(temp);
219                         if(ENABLE_FEATURE_CLEAN_UP)
220                                 RELEASE_CONFIG_BUFFER(temp);
221                         return 0;
222                 } else bb_show_usage();
223         } 
224         
225 /* hotplug support goes here */
226         
227         return 0;
228 }