shrink the code a bit
[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         close(fd);
46         if (len<1) goto end;
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         if(sscanf(temp, "%d:%d", &major, &minor) != 2) goto end;
53
54         /* If we have a config file, look up permissions for this device */
55
56         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);
62                         conf=mmap(NULL,len,PROT_READ,MAP_PRIVATE,fd,0);
63                         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;
70
71                                         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
91                                                                 /* Is this it? */
92                                                                 xregcomp(&match,regex,REG_EXTENDED);
93                                                                 result=regexec(&match,device_name,1,&off,0);
94                                                                 regfree(&match);
95
96                                                                 /* 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                                         }
141 end_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                                 }
149 found_device:
150                                 munmap(conf,len);
151                         }
152                         close(fd);
153                 }
154         }
155
156         sprintf(temp, "%s/%s", DEV_PATH, device_name);
157         umask(0);
158         if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
159                 bb_perror_msg_and_die("mknod %s failed", temp);
160
161         if (ENABLE_FEATURE_MDEV_CONF) chown(temp,uid,gid);
162
163 end:
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
170 static void find_dev(char *path)
171 {
172         DIR *dir;
173         size_t len=strlen(path);
174         struct dirent *entry;
175
176         if ((dir = opendir(path)) == NULL)
177                 return;
178
179         while ((entry = readdir(dir)) != NULL) {
180
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);
188                 }
189
190                 /* If there's a dev entry, mknod it */
191
192                 if (!strcmp(entry->d_name, "dev")) make_device(path);
193         }
194
195         closedir(dir);
196 }
197
198 int mdev_main(int argc, char *argv[])
199 {
200         char *action;
201         char *env_path;
202         RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
203
204         /* Scan */
205
206         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();
218
219                 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);
229         return 0;
230 }