7c1223dcba73f9a263b25cd3511b33e61e055058
[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 GPLv2 or later, 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         strcat(path, "/dev");
41         fd = open(path, O_RDONLY);
42         len = read(fd, temp + 1, 64);
43         *temp++ = 0;
44         close(fd);
45         if (len < 1) return;
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         if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
52
53         /* If we have a config file, look up permissions for this device */
54
55         if (ENABLE_FEATURE_MDEV_CONF) {
56                 char *conf, *pos, *end;
57
58                 /* mmap the config file */
59                 if (-1 != (fd=open("/etc/mdev.conf",O_RDONLY))) {
60                         len = lseek(fd, 0, SEEK_END);
61                         conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
62                         if (conf) {
63                                 int line = 0;
64
65                                 /* Loop through lines in mmaped file*/
66                                 for (pos=conf; pos-conf<len;) {
67                                         int field;
68                                         char *end2;
69
70                                         line++;
71                                         /* find end of this line */
72                                         for(end=pos; end-conf<len && *end!='\n'; end++)
73                                                 ;
74
75                                         /* Three fields: regex, uid:gid, mode */
76                                         for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
77                                                         field++)
78                                         {
79                                                 /* Skip whitespace */
80                                                 while (pos<end && isspace(*pos)) pos++;
81                                                 if (pos==end || *pos=='#') break;
82                                                 for (end2=pos;
83                                                         end2<end && !isspace(*end2) && *end2!='#'; end2++)
84                                                         ;
85
86                                                 if (!field) {
87                                                         /* Regex to match this device */
88
89                                                         char *regex = strndupa(pos, end2-pos);
90                                                         regex_t match;
91                                                         regmatch_t off;
92                                                         int result;
93
94                                                         /* Is this it? */
95                                                         xregcomp(&match,regex, REG_EXTENDED);
96                                                         result = regexec(&match, device_name, 1, &off, 0);
97                                                         regfree(&match);
98
99                                                         /* If not this device, skip rest of line */
100                                                         if (result || off.rm_so
101                                                                         || off.rm_eo != strlen(device_name))
102                                                                 break;
103
104                                                 } else if (field == 1) {
105                                                         /* uid:gid */
106
107                                                         char *s, *s2;
108
109                                                         /* Find : */
110                                                         for(s=pos; s<end2 && *s!=':'; s++)
111                                                                 ;
112                                                         if (s == end2) break;
113
114                                                         /* Parse UID */
115                                                         uid = strtoul(pos,&s2,10);
116                                                         if (s != s2) {
117                                                                 struct passwd *pass;
118                                                                 pass = getpwnam(strndupa(pos, s-pos));
119                                                                 if (!pass) break;
120                                                                 uid = pass->pw_uid;
121                                                         }
122                                                         s++;
123                                                         /* parse GID */
124                                                         gid = strtoul(s, &s2, 10);
125                                                         if (end2 != s2) {
126                                                                 struct group *grp;
127                                                                 grp = getgrnam(strndupa(s, end2-s));
128                                                                 if (!grp) break;
129                                                                 gid = grp->gr_gid;
130                                                         }
131                                                 } else if (field == 2) {
132                                                         /* mode */
133
134                                                         mode = strtoul(pos, &pos, 8);
135                                                         if (pos != end2) break;
136                                                 } else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
137                                                         // Command to run
138                                                         char *s = "@$*", *s2;
139                                                         if (!(s2 = strchr(s, *pos++))) {
140                                                                 // Force error
141                                                                 field = 1;
142                                                                 break;
143                                                         }
144                                                         if ((s2-s+1) & (1<<delete))
145                                                                 command = xstrndup(pos, end-pos);
146                                                 }
147
148                                                 pos = end2;
149                                         }
150
151                                         /* Did everything parse happily? */
152
153                                         if (field > 2) break;
154                                         if (field) bb_error_msg_and_die("Bad line %d",line);
155
156                                         /* Next line */
157                                         pos = ++end;
158                                 }
159                                 munmap(conf, len);
160                         }
161                         close(fd);
162                 }
163         }
164
165         umask(0);
166         if (!delete) {
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("Couldn't 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 }