Shrink the code about 50 bytes, allocate less run-time memory, and add a
[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         char *temp = path + strlen(path);
42
43         /* Try to read major/minor string.  Note that the kernel puts \n after
44          * the data, so we don't need to worry about null terminating the string
45          * because sscanf() will stop at the first nondigit, which \n is.  We
46          * also depend on path having writeable space after it. */
47
48         strcat(path, "/dev");
49         fd = open(path, O_RDONLY);
50         len = read(fd, temp + 1, 64);
51         *temp++ = 0;
52         close(fd);
53         if (len < 1) return;
54
55         /* Determine device name, type, major and minor */
56
57         device_name = strrchr(path, '/') + 1;
58         type = path[5]=='c' ? S_IFCHR : S_IFBLK;
59         if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
60
61         /* If we have a config file, look up permissions for this device */
62
63         if (ENABLE_FEATURE_MDEV_CONF) {
64                 char *conf, *pos, *end;
65
66                 /* mmap the config file */
67                 if (-1 != (fd=open(MDEV_CONF,O_RDONLY))) {
68                         len = lseek(fd, 0, SEEK_END);
69                         conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
70                         if (conf) {
71                                 int line = 0;
72
73                                 /* Loop through lines in mmaped file*/
74                                 for (pos=conf; pos-conf<len;) {
75                                         int field;
76                                         char *end2;
77
78                                         line++;
79                                         /* find end of this line */
80                                         for(end=pos; end-conf<len && *end!='\n'; end++)
81                                                 ;
82
83                                         /* Three fields: regex, uid:gid, mode */
84                                         for (field=3; field; field--) {
85                                                 /* Skip whitespace */
86                                                 while (pos<end && isspace(*pos))
87                                                         pos++;
88                                                 if (pos==end || *pos=='#')
89                                                         break;
90                                                 for (end2=pos; end2<end && !isspace(*end2) && *end2!='#'; end2++)
91                                                         ;
92
93                                                 switch (field) {
94                                                         /* Regex to match this device */
95                                                         case 3:
96                                                         {
97                                                                 char *regex = strndupa(pos,end2-pos);
98                                                                 regex_t match;
99                                                                 regmatch_t off;
100                                                                 int result;
101
102                                                                 /* Is this it? */
103                                                                 xregcomp(&match,regex,REG_EXTENDED);
104                                                                 result = regexec(&match,device_name,1,&off,0);
105                                                                 regfree(&match);
106
107                                                                 /* If not this device, skip rest of line */
108                                                                 if (result || off.rm_so || 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                                                                         ;
121                                                                 if (s == end2)
122                                                                         goto end_line;
123
124                                                                 /* Parse UID */
125                                                                 uid = strtoul(pos,&s2,10);
126                                                                 if (s != s2) {
127                                                                         struct passwd *pass;
128                                                                         pass = getpwnam(strndupa(pos,s-pos));
129                                                                         if (!pass)
130                                                                                 goto end_line;
131                                                                         uid = pass->pw_uid;
132                                                                 }
133                                                                 s++;
134                                                                 /* parse GID */
135                                                                 gid = strtoul(s,&s2,10);
136                                                                 if (end2 != s2) {
137                                                                         struct group *grp;
138                                                                         grp = getgrnam(strndupa(s,end2-s));
139                                                                         if (!grp)
140                                                                                 goto end_line;
141                                                                         gid = grp->gr_gid;
142                                                                 }
143                                                                 break;
144                                                         }
145                                                         /* mode */
146                                                         case 1:
147                                                         {
148                                                                 mode = strtoul(pos,&pos,8);
149                                                                 if (pos != end2)
150                                                                         goto end_line;
151                                                                 else
152                                                                         goto found_device;
153                                                         }
154                                                 }
155                                                 pos = end2;
156                                         }
157 end_line:
158                                         /* Did everything parse happily? */
159                                         if (field && field!=3)
160                                                 bb_error_msg_and_die("Bad line %d",line);
161
162                                         /* Next line */
163                                         pos = ++end;
164                                 }
165 found_device:
166                                 munmap(conf, len);
167                         }
168                         close(fd);
169                 }
170         }
171
172         umask(0);
173         if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
174                 bb_perror_msg_and_die("mknod %s failed", device_name);
175
176         if (major==root_major && minor==root_minor)
177                 symlink(device_name, "root");
178         
179         if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
180 }
181
182 /* Recursive search of /sys/block or /sys/class.  path must be a writeable
183  * buffer of size PATH_MAX containing the directory string to start at. */
184
185 static void find_dev(char *path)
186 {
187         DIR *dir;
188         size_t len = strlen(path);
189         struct dirent *entry;
190
191         if ((dir = opendir(path)) == NULL)
192                 return;
193
194         while ((entry = readdir(dir)) != NULL) {
195
196                 /* Skip "." and ".." (also skips hidden files, which is ok) */
197
198                 if (entry->d_name[0] == '.')
199                         continue;
200
201                 if (entry->d_type == DT_DIR) {
202                         snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
203                         find_dev(path);
204                         path[len] = 0;
205                 }
206
207                 /* If there's a dev entry, mknod it */
208
209                 if (!strcmp(entry->d_name, "dev")) make_device(path);
210         }
211
212         closedir(dir);
213 }
214
215 int mdev_main(int argc, char *argv[])
216 {
217         char *action;
218         char *env_path;
219         RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
220
221         bb_xchdir(DEV_PATH);
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                         unlink(strrchr(env_path, '/') + 1);
249                 }
250         }
251
252         if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
253         return 0;
254 }