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