add open_read_close() and similar stuff
[oweals/busybox.git] / util-linux / mdev.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *
4  * mdev - Mini udev for busybox
5  *
6  * Copyright 2005 Rob Landley <rob@landley.net>
7  * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
8  *
9  * Licensed under GPL version 2, see file LICENSE in this tarball for details.
10  */
11
12 #include "busybox.h"
13 #include "xregex.h"
14
15 #define DEV_PATH        "/dev"
16
17 struct mdev_globals
18 {
19         int root_major, root_minor;
20 } mdev_globals;
21
22 #define bbg mdev_globals
23
24 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
25 static void make_device(char *path, int delete)
26 {
27         char *device_name;
28         int major, minor, type, len, fd;
29         int mode = 0660;
30         uid_t uid = 0;
31         gid_t gid = 0;
32         char *temp = path + strlen(path);
33         char *command = NULL;
34
35         /* Try to read major/minor string.  Note that the kernel puts \n after
36          * the data, so we don't need to worry about null terminating the string
37          * because sscanf() will stop at the first nondigit, which \n is.  We
38          * also depend on path having writeable space after it. */
39
40         if (!delete) {
41                 strcat(path, "/dev");
42                 len = open_read_close(path, temp + 1, 64);
43                 *temp++ = 0;
44                 if (len < 1) return;
45         }
46
47         /* Determine device name, type, major and minor */
48
49         device_name = strrchr(path, '/') + 1;
50         type = path[5]=='c' ? S_IFCHR : S_IFBLK;
51
52         /* If we have a config file, look up permissions for this device */
53
54         if (ENABLE_FEATURE_MDEV_CONF) {
55                 char *conf, *pos, *end;
56
57                 /* mmap the config file */
58                 if (-1 != (fd=open("/etc/mdev.conf",O_RDONLY))) {
59                         len = lseek(fd, 0, SEEK_END);
60                         conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
61                         if (conf) {
62                                 int line = 0;
63
64                                 /* Loop through lines in mmaped file*/
65                                 for (pos=conf; pos-conf<len;) {
66                                         int field;
67                                         char *end2;
68
69                                         line++;
70                                         /* find end of this line */
71                                         for(end=pos; end-conf<len && *end!='\n'; end++)
72                                                 ;
73
74                                         /* Three fields: regex, uid:gid, mode */
75                                         for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
76                                                         field++)
77                                         {
78                                                 /* Skip whitespace */
79                                                 while (pos<end && isspace(*pos)) pos++;
80                                                 if (pos==end || *pos=='#') break;
81                                                 for (end2=pos;
82                                                         end2<end && !isspace(*end2) && *end2!='#'; end2++)
83                                                         ;
84
85                                                 if (!field) {
86                                                         /* Regex to match this device */
87
88                                                         char *regex = strndupa(pos, end2-pos);
89                                                         regex_t match;
90                                                         regmatch_t off;
91                                                         int result;
92
93                                                         /* Is this it? */
94                                                         xregcomp(&match,regex, REG_EXTENDED);
95                                                         result = regexec(&match, device_name, 1, &off, 0);
96                                                         regfree(&match);
97
98                                                         /* If not this device, skip rest of line */
99                                                         if (result || off.rm_so
100                                                                         || off.rm_eo != strlen(device_name))
101                                                                 break;
102
103                                                 } else if (field == 1) {
104                                                         /* uid:gid */
105
106                                                         char *s, *s2;
107
108                                                         /* Find : */
109                                                         for(s=pos; s<end2 && *s!=':'; s++)
110                                                                 ;
111                                                         if (s == end2) break;
112
113                                                         /* Parse UID */
114                                                         uid = strtoul(pos,&s2,10);
115                                                         if (s != s2) {
116                                                                 struct passwd *pass;
117                                                                 pass = getpwnam(strndupa(pos, s-pos));
118                                                                 if (!pass) break;
119                                                                 uid = pass->pw_uid;
120                                                         }
121                                                         s++;
122                                                         /* parse GID */
123                                                         gid = strtoul(s, &s2, 10);
124                                                         if (end2 != s2) {
125                                                                 struct group *grp;
126                                                                 grp = getgrnam(strndupa(s, end2-s));
127                                                                 if (!grp) break;
128                                                                 gid = grp->gr_gid;
129                                                         }
130                                                 } else if (field == 2) {
131                                                         /* mode */
132
133                                                         mode = strtoul(pos, &pos, 8);
134                                                         if (pos != end2) break;
135                                                 } else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
136                                                         // Command to run
137                                                         char *s = "@$*", *s2;
138                                                         if (!(s2 = strchr(s, *pos++))) {
139                                                                 // Force error
140                                                                 field = 1;
141                                                                 break;
142                                                         }
143                                                         if ((s2-s+1) & (1<<delete))
144                                                                 command = xstrndup(pos, end-pos);
145                                                 }
146
147                                                 pos = end2;
148                                         }
149
150                                         /* Did everything parse happily? */
151
152                                         if (field > 2) break;
153                                         if (field) bb_error_msg_and_die("Bad line %d",line);
154
155                                         /* Next line */
156                                         pos = ++end;
157                                 }
158                                 munmap(conf, len);
159                         }
160                         close(fd);
161                 }
162         }
163
164         umask(0);
165         if (!delete) {
166                 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
167                 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
168                         bb_perror_msg_and_die("mknod %s failed", device_name);
169
170                 if (major == bbg.root_major && minor == bbg.root_minor)
171                         symlink(device_name, "root");
172
173                 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
174         }
175         if (command) {
176                 int rc;
177                 char *s;
178
179                 s=xasprintf("MDEV=%s",device_name);
180                 putenv(s);
181                 rc = system(command);
182                 s[4]=0;
183                 putenv(s);
184                 free(s);
185                 free(command);
186                 if (rc == -1) bb_perror_msg_and_die("cannot run %s", command);
187         }
188         if (delete) unlink(device_name);
189 }
190
191 /* Recursive search of /sys/block or /sys/class.  path must be a writeable
192  * buffer of size PATH_MAX containing the directory string to start at. */
193
194 static void find_dev(char *path)
195 {
196         DIR *dir;
197         size_t len = strlen(path);
198         struct dirent *entry;
199
200         if ((dir = opendir(path)) == NULL)
201                 return;
202
203         while ((entry = readdir(dir)) != NULL) {
204                 struct stat st;
205
206                 /* Skip "." and ".." (also skips hidden files, which is ok) */
207
208                 if (entry->d_name[0] == '.')
209                         continue;
210
211                 // uClibc doesn't fill out entry->d_type reliably. so we use lstat().
212
213                 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
214                 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
215                 path[len] = 0;
216
217                 /* If there's a dev entry, mknod it */
218
219                 if (!strcmp(entry->d_name, "dev")) make_device(path, 0);
220         }
221
222         closedir(dir);
223 }
224
225 int mdev_main(int argc, char *argv[])
226 {
227         char *action;
228         char *env_path;
229         RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
230
231         xchdir(DEV_PATH);
232
233         /* Scan */
234
235         if (argc == 2 && !strcmp(argv[1],"-s")) {
236                 struct stat st;
237
238                 stat("/", &st);  // If this fails, we have bigger problems.
239                 bbg.root_major=major(st.st_dev);
240                 bbg.root_minor=minor(st.st_dev);
241                 strcpy(temp,"/sys/block");
242                 find_dev(temp);
243                 strcpy(temp,"/sys/class");
244                 find_dev(temp);
245
246         /* Hotplug */
247
248         } else {
249                 action = getenv("ACTION");
250                 env_path = getenv("DEVPATH");
251                 if (!action || !env_path)
252                         bb_show_usage();
253
254                 sprintf(temp, "/sys%s", env_path);
255                 if (!strcmp(action, "add")) make_device(temp,0);
256                 else if (!strcmp(action, "remove")) make_device(temp,1);
257         }
258
259         if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
260         return 0;
261 }