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