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