Contents of /trunk/mkinitrd-magellan/busybox/util-linux/mdev.c
Parent Directory
|
Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 6008 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 6008 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
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; |
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 | int line, fd; |
57 | |
58 | /* mmap the config file */ |
59 | fd = open("/etc/mdev.conf", O_RDONLY); |
60 | if (fd < 0) |
61 | goto end_parse; |
62 | len = xlseek(fd, 0, SEEK_END); |
63 | conf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); |
64 | close(fd); |
65 | if (!conf) |
66 | goto end_parse; |
67 | |
68 | line = 0; |
69 | /* Loop through lines in mmaped file*/ |
70 | for (pos=conf; pos-conf<len;) { |
71 | int field; |
72 | char *end2; |
73 | |
74 | line++; |
75 | /* find end of this line */ |
76 | for (end=pos; end-conf<len && *end!='\n'; end++) |
77 | ; |
78 | |
79 | /* Three fields: regex, uid:gid, mode */ |
80 | for (field=0; field < (3 + ENABLE_FEATURE_MDEV_EXEC); |
81 | field++) |
82 | { |
83 | /* Skip whitespace */ |
84 | while (pos<end && isspace(*pos)) pos++; |
85 | if (pos==end || *pos=='#') break; |
86 | for (end2=pos; |
87 | end2<end && !isspace(*end2) && *end2!='#'; end2++) |
88 | ; |
89 | |
90 | if (field == 0) { |
91 | /* Regex to match this device */ |
92 | |
93 | char *regex = strndupa(pos, end2-pos); |
94 | regex_t match; |
95 | regmatch_t off; |
96 | int result; |
97 | |
98 | /* Is this it? */ |
99 | xregcomp(&match,regex, REG_EXTENDED); |
100 | result = regexec(&match, device_name, 1, &off, 0); |
101 | regfree(&match); |
102 | |
103 | /* If not this device, skip rest of line */ |
104 | if (result || off.rm_so |
105 | || off.rm_eo != strlen(device_name)) |
106 | break; |
107 | } |
108 | if (field == 1) { |
109 | /* uid:gid */ |
110 | |
111 | char *s, *s2; |
112 | |
113 | /* Find : */ |
114 | for (s=pos; s<end2 && *s!=':'; s++) |
115 | ; |
116 | if (s == end2) break; |
117 | |
118 | /* Parse UID */ |
119 | uid = strtoul(pos, &s2, 10); |
120 | if (s != s2) { |
121 | struct passwd *pass; |
122 | pass = getpwnam(strndupa(pos, s-pos)); |
123 | if (!pass) break; |
124 | uid = pass->pw_uid; |
125 | } |
126 | s++; |
127 | /* parse GID */ |
128 | gid = strtoul(s, &s2, 10); |
129 | if (end2 != s2) { |
130 | struct group *grp; |
131 | grp = getgrnam(strndupa(s, end2-s)); |
132 | if (!grp) break; |
133 | gid = grp->gr_gid; |
134 | } |
135 | } |
136 | if (field == 2) { |
137 | /* mode */ |
138 | |
139 | mode = strtoul(pos, &pos, 8); |
140 | if (pos != end2) break; |
141 | } |
142 | if (ENABLE_FEATURE_MDEV_EXEC && field == 3) { |
143 | // Command to run |
144 | char *s = "@$*", *s2; |
145 | s2 = strchr(s, *pos++); |
146 | if (!s2) { |
147 | // Force error |
148 | field = 1; |
149 | break; |
150 | } |
151 | if ((s2-s+1) & (1<<delete)) |
152 | command = xstrndup(pos, end-pos); |
153 | } |
154 | |
155 | pos = end2; |
156 | } |
157 | |
158 | /* Did everything parse happily? */ |
159 | |
160 | if (field > 2) break; |
161 | if (field) bb_error_msg_and_die("bad line %d",line); |
162 | |
163 | /* Next line */ |
164 | pos = ++end; |
165 | } |
166 | munmap(conf, len); |
167 | end_parse: /* nothing */ ; |
168 | } |
169 | |
170 | umask(0); |
171 | if (!delete) { |
172 | if (sscanf(temp, "%d:%d", &major, &minor) != 2) return; |
173 | if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST) |
174 | bb_perror_msg_and_die("mknod %s", device_name); |
175 | |
176 | if (major == bbg.root_major && minor == bbg.root_minor) |
177 | symlink(device_name, "root"); |
178 | |
179 | if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid); |
180 | } |
181 | if (command) { |
182 | int rc; |
183 | char *s; |
184 | |
185 | s = xasprintf("MDEV=%s", device_name); |
186 | putenv(s); |
187 | rc = system(command); |
188 | s[4] = 0; |
189 | putenv(s); |
190 | free(s); |
191 | free(command); |
192 | if (rc == -1) bb_perror_msg_and_die("cannot run %s", command); |
193 | } |
194 | if (delete) unlink(device_name); |
195 | } |
196 | |
197 | /* Recursive search of /sys/block or /sys/class. path must be a writeable |
198 | * buffer of size PATH_MAX containing the directory string to start at. */ |
199 | |
200 | static void find_dev(char *path) |
201 | { |
202 | DIR *dir; |
203 | size_t len = strlen(path); |
204 | struct dirent *entry; |
205 | |
206 | dir = opendir(path); |
207 | if (dir == NULL) |
208 | return; |
209 | |
210 | while ((entry = readdir(dir)) != NULL) { |
211 | struct stat st; |
212 | |
213 | /* Skip "." and ".." (also skips hidden files, which is ok) */ |
214 | |
215 | if (entry->d_name[0] == '.') |
216 | continue; |
217 | |
218 | // uClibc doesn't fill out entry->d_type reliably. so we use lstat(). |
219 | |
220 | snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name); |
221 | if (!lstat(path, &st) && S_ISDIR(st.st_mode)) find_dev(path); |
222 | path[len] = 0; |
223 | |
224 | /* If there's a dev entry, mknod it */ |
225 | |
226 | if (!strcmp(entry->d_name, "dev")) make_device(path, 0); |
227 | } |
228 | |
229 | closedir(dir); |
230 | } |
231 | |
232 | int mdev_main(int argc, char *argv[]) |
233 | { |
234 | char *action; |
235 | char *env_path; |
236 | RESERVE_CONFIG_BUFFER(temp,PATH_MAX); |
237 | |
238 | xchdir(DEV_PATH); |
239 | |
240 | /* Scan */ |
241 | |
242 | if (argc == 2 && !strcmp(argv[1],"-s")) { |
243 | struct stat st; |
244 | |
245 | xstat("/", &st); |
246 | bbg.root_major = major(st.st_dev); |
247 | bbg.root_minor = minor(st.st_dev); |
248 | strcpy(temp,"/sys/block"); |
249 | find_dev(temp); |
250 | strcpy(temp,"/sys/class"); |
251 | find_dev(temp); |
252 | |
253 | /* Hotplug */ |
254 | |
255 | } else { |
256 | action = getenv("ACTION"); |
257 | env_path = getenv("DEVPATH"); |
258 | if (!action || !env_path) |
259 | bb_show_usage(); |
260 | |
261 | sprintf(temp, "/sys%s", env_path); |
262 | if (!strcmp(action, "add")) make_device(temp,0); |
263 | else if (!strcmp(action, "remove")) make_device(temp,1); |
264 | } |
265 | |
266 | if (ENABLE_FEATURE_CLEAN_UP) RELEASE_CONFIG_BUFFER(temp); |
267 | return 0; |
268 | } |