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