source: MondoRescue/branches/stable/mindi-busybox/util-linux/mdev.c@ 821

Last change on this file since 821 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 6.1 KB
RevLine 
[821]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 "busybox.h"
12#include <ctype.h>
13#include <errno.h>
14#include <sys/mman.h>
15#include <sys/sysmacros.h>
16#include "xregex.h"
17
18#define DEV_PATH "/dev"
19
20struct mdev_globals
21{
22 int root_major, root_minor;
23} mdev_globals;
24
25#define bbg mdev_globals
26
27/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
28static void make_device(char *path, int delete)
29{
30 char *device_name;
31 int major, minor, type, len, fd;
32 int mode = 0660;
33 uid_t uid = 0;
34 gid_t gid = 0;
35 char *temp = path + strlen(path);
36 char *command = NULL;
37
38 /* Try to read major/minor string. Note that the kernel puts \n after
39 * the data, so we don't need to worry about null terminating the string
40 * because sscanf() will stop at the first nondigit, which \n is. We
41 * also depend on path having writeable space after it. */
42
43 strcat(path, "/dev");
44 fd = open(path, O_RDONLY);
45 len = read(fd, temp + 1, 64);
46 *temp++ = 0;
47 close(fd);
48 if (len < 1) return;
49
50 /* Determine device name, type, major and minor */
51
52 device_name = strrchr(path, '/') + 1;
53 type = path[5]=='c' ? S_IFCHR : S_IFBLK;
54 if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
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("/etc/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=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC);
80 field++)
81 {
82 /* Skip whitespace */
83 while (pos<end && isspace(*pos)) pos++;
84 if (pos==end || *pos=='#') break;
85 for (end2=pos;
86 end2<end && !isspace(*end2) && *end2!='#'; end2++)
87 ;
88
89 if (!field) {
90 /* Regex to match this device */
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
104 || off.rm_eo != strlen(device_name))
105 break;
106
107 } else if (field == 1) {
108 /* uid:gid */
109
110 char *s, *s2;
111
112 /* Find : */
113 for(s=pos; s<end2 && *s!=':'; s++)
114 ;
115 if (s == end2) break;
116
117 /* Parse UID */
118 uid = strtoul(pos,&s2,10);
119 if (s != s2) {
120 struct passwd *pass;
121 pass = getpwnam(strndupa(pos, s-pos));
122 if (!pass) break;
123 uid = pass->pw_uid;
124 }
125 s++;
126 /* parse GID */
127 gid = strtoul(s, &s2, 10);
128 if (end2 != s2) {
129 struct group *grp;
130 grp = getgrnam(strndupa(s, end2-s));
131 if (!grp) break;
132 gid = grp->gr_gid;
133 }
134 } else if (field == 2) {
135 /* mode */
136
137 mode = strtoul(pos, &pos, 8);
138 if (pos != end2) break;
139 } else if (ENABLE_FEATURE_MDEV_EXEC && field == 3) {
140 // Command to run
141 char *s = "@$*", *s2;
142 if (!(s2 = strchr(s, *pos++))) {
143 // Force error
144 field = 1;
145 break;
146 }
147 if ((s2-s+1) & (1<<delete))
148 command = bb_xstrndup(pos, end-pos);
149 }
150
151 pos = end2;
152 }
153
154 /* Did everything parse happily? */
155
156 if (field > 2) break;
157 if (field) bb_error_msg_and_die("Bad line %d",line);
158
159 /* Next line */
160 pos = ++end;
161 }
162 munmap(conf, len);
163 }
164 close(fd);
165 }
166 }
167
168 umask(0);
169 if (!delete) {
170 if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
171 bb_perror_msg_and_die("mknod %s failed", device_name);
172
173 if (major == bbg.root_major && minor == bbg.root_minor)
174 symlink(device_name, "root");
175
176 if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
177 }
178 if (command) {
179 int rc;
180 char *s;
181
182 s=bb_xasprintf("MDEV=%s",device_name);
183 putenv(s);
184 rc = system(command);
185 s[4]=0;
186 putenv(s);
187 free(s);
188 free(command);
189 if (rc == -1) bb_perror_msg_and_die("Couldn't run %s", 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
197static void find_dev(char *path)
198{
199 DIR *dir;
200 size_t len = strlen(path);
201 struct dirent *entry;
202
203 if ((dir = opendir(path)) == NULL)
204 return;
205
206 while ((entry = readdir(dir)) != NULL) {
207 struct stat st;
208
209 /* Skip "." and ".." (also skips hidden files, which is ok) */
210
211 if (entry->d_name[0] == '.')
212 continue;
213
214 // uClibc doesn't fill out entry->d_type reliably. so we use lstat().
215
216 snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name);
217 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path);
218 path[len] = 0;
219
220 /* If there's a dev entry, mknod it */
221
222 if (!strcmp(entry->d_name, "dev")) make_device(path, 0);
223 }
224
225 closedir(dir);
226}
227
228int mdev_main(int argc, char *argv[])
229{
230 char *action;
231 char *env_path;
232 RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
233
234 bb_xchdir(DEV_PATH);
235
236 /* Scan */
237
238 if (argc == 2 && !strcmp(argv[1],"-s")) {
239 struct stat st;
240
241 stat("/", &st); // If this fails, we have bigger problems.
242 bbg.root_major=major(st.st_dev);
243 bbg.root_minor=minor(st.st_dev);
244 strcpy(temp,"/sys/block");
245 find_dev(temp);
246 strcpy(temp,"/sys/class");
247 find_dev(temp);
248
249 /* Hotplug */
250
251 } else {
252 action = getenv("ACTION");
253 env_path = getenv("DEVPATH");
254 if (!action || !env_path)
255 bb_show_usage();
256
257 sprintf(temp, "/sys%s", env_path);
258 if (!strcmp(action, "add")) make_device(temp,0);
259 else if (!strcmp(action, "remove")) make_device(temp,1);
260 }
261
262 if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp);
263 return 0;
264}
Note: See TracBrowser for help on using the repository browser.