]>
Commit | Line | Data |
---|---|---|
82b9a637 GKH |
1 | /* |
2 | * udevstart.c | |
3 | * | |
4 | * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> | |
5 | * | |
6 | * Quick and dirty way to populate a /dev with udev if your system | |
7 | * does not have access to a shell. Based originally on a patch to udev | |
8 | * from Harald Hoyer <harald@redhat.com> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify it | |
11 | * under the terms of the GNU General Public License as published by the | |
12 | * Free Software Foundation version 2 of the License. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, but | |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License along | |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | * | |
23 | */ | |
24 | ||
25 | #include <stdlib.h> | |
0e306d08 | 26 | #include <stddef.h> |
82b9a637 GKH |
27 | #include <string.h> |
28 | #include <stdio.h> | |
29 | #include <errno.h> | |
30 | #include <ctype.h> | |
82b9a637 GKH |
31 | #include <dirent.h> |
32 | #include <sys/wait.h> | |
f27125f9 | 33 | #include <sys/types.h> |
34 | #include <unistd.h> | |
82b9a637 GKH |
35 | |
36 | #include "logging.h" | |
9b28a52a | 37 | #include "udev_lib.h" |
0e306d08 | 38 | #include "list.h" |
82b9a637 GKH |
39 | |
40 | ||
41 | #ifdef LOG | |
d00bd172 | 42 | unsigned char logname[LOGNAME_SIZE]; |
82b9a637 GKH |
43 | void log_message(int level, const char *format, ...) |
44 | { | |
45 | va_list args; | |
46 | ||
47 | va_start(args, format); | |
48 | vsyslog(level, format, args); | |
49 | va_end(args); | |
50 | } | |
51 | #endif | |
52 | ||
53 | ||
e4dc0e11 KS |
54 | #define MAX_PATHLEN 1024 |
55 | #define SYSBLOCK "/sys/block" | |
56 | #define SYSCLASS "/sys/class" | |
57 | #define UDEV_BIN "/sbin/udev" | |
82b9a637 | 58 | |
0e306d08 GKH |
59 | struct device { |
60 | struct list_head list; | |
61 | char path[MAX_PATHLEN]; | |
62 | char subsys[MAX_PATHLEN]; | |
63 | }; | |
64 | ||
65 | /* sort files in lexical order */ | |
66 | static int device_list_insert(char *path, char *subsystem, struct list_head *device_list) | |
67 | { | |
68 | struct device *loop_device; | |
69 | struct device *new_device; | |
70 | ||
71 | list_for_each_entry(loop_device, device_list, list) { | |
72 | if (strcmp(loop_device->path, path) > 0) { | |
73 | break; | |
74 | } | |
75 | } | |
76 | ||
77 | new_device = malloc(sizeof(struct device)); | |
78 | if (new_device == NULL) { | |
79 | dbg("error malloc"); | |
80 | return -ENOMEM; | |
81 | } | |
82 | ||
83 | strfieldcpy(new_device->path, path); | |
84 | strfieldcpy(new_device->subsys, subsystem); | |
85 | list_add_tail(&new_device->list, &loop_device->list); | |
86 | return 0; | |
87 | } | |
88 | ||
e4dc0e11 | 89 | static void udev_exec(const char *path, const char* subsystem) |
82b9a637 | 90 | { |
82b9a637 | 91 | pid_t pid; |
e4dc0e11 KS |
92 | char action[] = "ACTION=add"; |
93 | char devpath[MAX_PATHLEN]; | |
94 | char nosleep[] = "UDEV_NO_SLEEP=1"; | |
95 | char *env[] = { action, devpath, nosleep, NULL }; | |
96 | ||
0e306d08 | 97 | return; |
5df33ccd KS |
98 | strcpy(devpath, "DEVPATH="); |
99 | strfieldcat(devpath, path); | |
e4dc0e11 | 100 | |
82b9a637 | 101 | pid = fork(); |
e4dc0e11 | 102 | switch (pid) { |
82b9a637 GKH |
103 | case 0: |
104 | /* child */ | |
e4dc0e11 KS |
105 | execle(UDEV_BIN, "udev", subsystem, NULL, env); |
106 | dbg("exec of child failed"); | |
82b9a637 | 107 | exit(1); |
e4dc0e11 | 108 | break; |
82b9a637 | 109 | case -1: |
e4dc0e11 KS |
110 | dbg("fork of child failed"); |
111 | break; | |
82b9a637 | 112 | default: |
e4dc0e11 | 113 | wait(NULL); |
82b9a637 | 114 | } |
82b9a637 GKH |
115 | } |
116 | ||
0e306d08 GKH |
117 | /* list of devices that we should run last due to any one of a number of reasons */ |
118 | static char *last_list[] = { | |
119 | "/block/dm", /* on here because dm wants to have the block devices around before it */ | |
120 | NULL, | |
121 | }; | |
122 | ||
123 | static void exec_list(struct list_head *device_list) | |
124 | { | |
125 | struct device *loop_device; | |
126 | struct device *tmp_device; | |
127 | ||
128 | /* handle the devices we are allowed to, excluding the "last" type devices */ | |
129 | list_for_each_entry_safe(loop_device, tmp_device, device_list, list) { | |
130 | int found = 0; | |
131 | int i; | |
132 | for (i=0; last_list[i] != NULL; i++) { | |
133 | if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) { | |
134 | found = 1; | |
135 | break; | |
136 | } | |
137 | } | |
138 | if (found) | |
139 | continue; | |
140 | ||
141 | udev_exec(loop_device->path, loop_device->subsys); | |
142 | list_del(&loop_device->list); | |
143 | free(loop_device); | |
144 | } | |
145 | ||
146 | /* handle the rest of the devices left over, if any */ | |
147 | list_for_each_entry_safe(loop_device, tmp_device, device_list, list) { | |
148 | udev_exec(loop_device->path, loop_device->subsys); | |
149 | list_del(&loop_device->list); | |
150 | free(loop_device); | |
151 | } | |
152 | } | |
153 | ||
154 | static void udev_scan_block(void) | |
82b9a637 | 155 | { |
e4dc0e11 KS |
156 | DIR *dir; |
157 | struct dirent *dent; | |
0e306d08 | 158 | LIST_HEAD(device_list); |
82b9a637 | 159 | |
0e306d08 | 160 | dir = opendir(SYSBLOCK); |
e4dc0e11 KS |
161 | if (dir != NULL) { |
162 | for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { | |
163 | char dirname[MAX_PATHLEN]; | |
164 | DIR *dir2; | |
165 | struct dirent *dent2; | |
166 | ||
167 | if ((strcmp(dent->d_name, ".") == 0) || | |
168 | (strcmp(dent->d_name, "..") == 0)) | |
82b9a637 GKH |
169 | continue; |
170 | ||
0e306d08 GKH |
171 | snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name); |
172 | dirname[MAX_PATHLEN-1] = '\0'; | |
173 | device_list_insert(dirname, "block", &device_list); | |
174 | ||
175 | snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name); | |
82b9a637 | 176 | dir2 = opendir(dirname); |
e4dc0e11 KS |
177 | if (dir2 != NULL) { |
178 | for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) { | |
0e306d08 | 179 | char dirname2[MAX_PATHLEN]; |
e4dc0e11 KS |
180 | DIR *dir3; |
181 | struct dirent *dent3; | |
82b9a637 GKH |
182 | |
183 | if ((strcmp(dent2->d_name, ".") == 0) || | |
184 | (strcmp(dent2->d_name, "..") == 0)) | |
185 | continue; | |
186 | ||
187 | snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name); | |
e4dc0e11 | 188 | dirname2[MAX_PATHLEN-1] = '\0'; |
82b9a637 GKH |
189 | |
190 | dir3 = opendir(dirname2); | |
e4dc0e11 KS |
191 | if (dir3 != NULL) { |
192 | for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) { | |
82b9a637 GKH |
193 | char filename[MAX_PATHLEN]; |
194 | ||
195 | if (strcmp(dent3->d_name, "dev") == 0) { | |
0e306d08 | 196 | snprintf(filename, MAX_PATHLEN, "/block/%s/%s", |
e4dc0e11 KS |
197 | dent->d_name, dent2->d_name); |
198 | filename[MAX_PATHLEN-1] = '\0'; | |
0e306d08 | 199 | device_list_insert(filename, "block", &device_list); |
82b9a637 GKH |
200 | } |
201 | } | |
e13fa599 | 202 | closedir(dir3); |
82b9a637 GKH |
203 | } |
204 | } | |
e13fa599 | 205 | closedir(dir2); |
82b9a637 GKH |
206 | } |
207 | } | |
e13fa599 | 208 | closedir(dir); |
82b9a637 GKH |
209 | } |
210 | ||
0e306d08 GKH |
211 | exec_list(&device_list); |
212 | } | |
213 | ||
214 | static void udev_scan_class(void) | |
215 | { | |
216 | DIR *dir; | |
217 | struct dirent *dent; | |
218 | LIST_HEAD(device_list); | |
219 | ||
220 | dir = opendir(SYSCLASS); | |
e4dc0e11 KS |
221 | if (dir != NULL) { |
222 | for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { | |
223 | char dirname[MAX_PATHLEN]; | |
224 | DIR *dir2; | |
225 | struct dirent *dent2; | |
226 | ||
227 | if ((strcmp(dent->d_name, ".") == 0) || | |
228 | (strcmp(dent->d_name, "..") == 0)) | |
82b9a637 GKH |
229 | continue; |
230 | ||
0e306d08 GKH |
231 | snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name); |
232 | dirname[MAX_PATHLEN] = '\0'; | |
82b9a637 | 233 | dir2 = opendir(dirname); |
e4dc0e11 KS |
234 | if (dir2 != NULL) { |
235 | for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) { | |
0e306d08 | 236 | char dirname2[MAX_PATHLEN-1]; |
e4dc0e11 KS |
237 | DIR *dir3; |
238 | struct dirent *dent3; | |
82b9a637 | 239 | |
e4dc0e11 KS |
240 | if ((strcmp(dent2->d_name, ".") == 0) || |
241 | (strcmp(dent2->d_name, "..") == 0)) | |
82b9a637 GKH |
242 | continue; |
243 | ||
244 | snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name); | |
e4dc0e11 | 245 | dirname2[MAX_PATHLEN-1] = '\0'; |
82b9a637 GKH |
246 | |
247 | dir3 = opendir(dirname2); | |
e4dc0e11 KS |
248 | if (dir3 != NULL) { |
249 | for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) { | |
250 | char filename[MAX_PATHLEN]; | |
82b9a637 GKH |
251 | |
252 | if (strcmp(dent3->d_name, "dev") == 0) { | |
0e306d08 | 253 | snprintf(filename, MAX_PATHLEN, "/class/%s/%s", |
e4dc0e11 KS |
254 | dent->d_name, dent2->d_name); |
255 | filename[MAX_PATHLEN-1] = '\0'; | |
0e306d08 | 256 | device_list_insert(filename, dent->d_name, &device_list); |
82b9a637 GKH |
257 | } |
258 | } | |
e13fa599 | 259 | closedir(dir3); |
82b9a637 GKH |
260 | } |
261 | } | |
e13fa599 | 262 | closedir(dir2); |
82b9a637 GKH |
263 | } |
264 | } | |
e13fa599 | 265 | closedir(dir); |
82b9a637 | 266 | } |
0e306d08 GKH |
267 | |
268 | exec_list(&device_list); | |
82b9a637 GKH |
269 | } |
270 | ||
d00bd172 | 271 | int main(int argc, char *argv[], char *envp[]) |
82b9a637 GKH |
272 | { |
273 | init_logging("udevstart"); | |
274 | ||
0e306d08 GKH |
275 | udev_scan_class(); |
276 | udev_scan_block(); | |
3f20eac0 MI |
277 | |
278 | return 0; | |
82b9a637 | 279 | } |