]>
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> | |
56a8a624 | 29 | #include <unistd.h> |
82b9a637 GKH |
30 | #include <errno.h> |
31 | #include <ctype.h> | |
82b9a637 | 32 | #include <dirent.h> |
56a8a624 | 33 | #include <signal.h> |
82b9a637 | 34 | #include <sys/wait.h> |
f22e9686 | 35 | #include <sys/stat.h> |
f27125f9 | 36 | #include <sys/types.h> |
82b9a637 | 37 | |
7a947ce5 | 38 | #include "libsysfs/sysfs/libsysfs.h" |
82b9a637 | 39 | #include "logging.h" |
56a8a624 | 40 | #include "namedev.h" |
9af5bb2f | 41 | #include "udev_utils.h" |
0e306d08 | 42 | #include "list.h" |
eb6c7cd0 | 43 | #include "udev.h" |
82b9a637 | 44 | |
56a8a624 KS |
45 | #ifdef USE_LOG |
46 | void log_message(int level, const char *format, ...) | |
47 | { | |
48 | } | |
49 | #endif | |
82b9a637 | 50 | |
0e306d08 GKH |
51 | struct device { |
52 | struct list_head list; | |
56a8a624 KS |
53 | char path[DEVPATH_SIZE]; |
54 | char subsys[SUBSYSTEM_SIZE]; | |
0e306d08 GKH |
55 | }; |
56 | ||
57 | /* sort files in lexical order */ | |
f22e9686 | 58 | static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list) |
0e306d08 GKH |
59 | { |
60 | struct device *loop_device; | |
61 | struct device *new_device; | |
62 | ||
f22e9686 KS |
63 | dbg("insert: '%s'\n", path); |
64 | ||
0e306d08 GKH |
65 | list_for_each_entry(loop_device, device_list, list) { |
66 | if (strcmp(loop_device->path, path) > 0) { | |
67 | break; | |
68 | } | |
69 | } | |
70 | ||
71 | new_device = malloc(sizeof(struct device)); | |
72 | if (new_device == NULL) { | |
73 | dbg("error malloc"); | |
74 | return -ENOMEM; | |
75 | } | |
76 | ||
77 | strfieldcpy(new_device->path, path); | |
78 | strfieldcpy(new_device->subsys, subsystem); | |
79 | list_add_tail(&new_device->list, &loop_device->list); | |
aee380b6 | 80 | dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys); |
0e306d08 GKH |
81 | return 0; |
82 | } | |
83 | ||
0e306d08 GKH |
84 | /* list of devices that we should run last due to any one of a number of reasons */ |
85 | static char *last_list[] = { | |
86 | "/block/dm", /* on here because dm wants to have the block devices around before it */ | |
87 | NULL, | |
88 | }; | |
89 | ||
70f630f6 GKH |
90 | /* list of devices that we should run first due to any one of a number of reasons */ |
91 | static char *first_list[] = { | |
92 | "/class/mem", /* people tend to like their memory devices around first... */ | |
93 | NULL, | |
94 | }; | |
95 | ||
f22e9686 | 96 | static int add_device(const char *path, const char *subsystem) |
f608f8ac | 97 | { |
7a947ce5 | 98 | struct udevice udev; |
7a947ce5 | 99 | struct sysfs_class_device *class_dev; |
f22e9686 KS |
100 | const char *devpath; |
101 | ||
102 | devpath = &path[strlen(sysfs_path)]; | |
f608f8ac | 103 | |
af4b05d4 | 104 | /* set environment for callouts and dev.d/ */ |
7a947ce5 | 105 | setenv("DEVPATH", devpath, 1); |
af4b05d4 | 106 | setenv("SUBSYSTEM", subsystem, 1); |
7a947ce5 | 107 | |
56a8a624 | 108 | dbg("exec: '%s' (%s)\n", devpath, path); |
f22e9686 | 109 | |
7a947ce5 KS |
110 | class_dev = sysfs_open_class_device_path(path); |
111 | if (class_dev == NULL) { | |
112 | dbg ("sysfs_open_class_device_path failed"); | |
113 | return -ENODEV; | |
114 | } | |
115 | ||
45a7b668 | 116 | udev_init_device(&udev, devpath, subsystem); |
5d24c6ca | 117 | udev_add_device(&udev, class_dev); |
7a947ce5 | 118 | |
8673dcb8 | 119 | /* run dev.d/ scripts if we created a node or changed a netif name */ |
7757db1f | 120 | if (udev_dev_d && udev.devname[0] != '\0') { |
8673dcb8 | 121 | setenv("DEVNAME", udev.devname, 1); |
9af5bb2f | 122 | udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX); |
8673dcb8 | 123 | } |
5d24c6ca KS |
124 | |
125 | sysfs_close_class_device(class_dev); | |
56a8a624 | 126 | udev_cleanup_device(&udev); |
5d24c6ca KS |
127 | |
128 | return 0; | |
f608f8ac KS |
129 | } |
130 | ||
0e306d08 GKH |
131 | static void exec_list(struct list_head *device_list) |
132 | { | |
133 | struct device *loop_device; | |
134 | struct device *tmp_device; | |
70f630f6 GKH |
135 | int i; |
136 | ||
137 | /* handle the "first" type devices first */ | |
138 | list_for_each_entry_safe(loop_device, tmp_device, device_list, list) { | |
f22e9686 | 139 | for (i = 0; first_list[i] != NULL; i++) { |
70f630f6 | 140 | if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) { |
7a947ce5 | 141 | add_device(loop_device->path, loop_device->subsys); |
70f630f6 GKH |
142 | list_del(&loop_device->list); |
143 | free(loop_device); | |
144 | break; | |
145 | } | |
146 | } | |
147 | } | |
0e306d08 GKH |
148 | |
149 | /* handle the devices we are allowed to, excluding the "last" type devices */ | |
150 | list_for_each_entry_safe(loop_device, tmp_device, device_list, list) { | |
151 | int found = 0; | |
f22e9686 | 152 | for (i = 0; last_list[i] != NULL; i++) { |
0e306d08 GKH |
153 | if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) { |
154 | found = 1; | |
155 | break; | |
156 | } | |
157 | } | |
158 | if (found) | |
159 | continue; | |
160 | ||
7a947ce5 | 161 | add_device(loop_device->path, loop_device->subsys); |
0e306d08 GKH |
162 | list_del(&loop_device->list); |
163 | free(loop_device); | |
164 | } | |
165 | ||
166 | /* handle the rest of the devices left over, if any */ | |
167 | list_for_each_entry_safe(loop_device, tmp_device, device_list, list) { | |
7a947ce5 | 168 | add_device(loop_device->path, loop_device->subsys); |
0e306d08 GKH |
169 | list_del(&loop_device->list); |
170 | free(loop_device); | |
171 | } | |
172 | } | |
173 | ||
f22e9686 KS |
174 | static int has_devt(const char *directory) |
175 | { | |
56a8a624 | 176 | char filename[NAME_SIZE]; |
f22e9686 KS |
177 | struct stat statbuf; |
178 | ||
56a8a624 KS |
179 | snprintf(filename, NAME_SIZE, "%s/dev", directory); |
180 | filename[NAME_SIZE-1] = '\0'; | |
f22e9686 KS |
181 | |
182 | if (stat(filename, &statbuf) == 0) | |
183 | return 1; | |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
0e306d08 | 188 | static void udev_scan_block(void) |
82b9a637 | 189 | { |
56a8a624 | 190 | char base[NAME_SIZE]; |
e4dc0e11 KS |
191 | DIR *dir; |
192 | struct dirent *dent; | |
0e306d08 | 193 | LIST_HEAD(device_list); |
82b9a637 | 194 | |
56a8a624 KS |
195 | snprintf(base, DEVPATH_SIZE, "%s/block", sysfs_path); |
196 | base[DEVPATH_SIZE-1] = '\0'; | |
f22e9686 KS |
197 | |
198 | dir = opendir(base); | |
e4dc0e11 KS |
199 | if (dir != NULL) { |
200 | for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { | |
56a8a624 | 201 | char dirname[DEVPATH_SIZE]; |
e4dc0e11 KS |
202 | DIR *dir2; |
203 | struct dirent *dent2; | |
204 | ||
f22e9686 | 205 | if (dent->d_name[0] == '.') |
82b9a637 GKH |
206 | continue; |
207 | ||
56a8a624 KS |
208 | snprintf(dirname, NAME_SIZE, "%s/%s", base, dent->d_name); |
209 | dirname[NAME_SIZE-1] = '\0'; | |
f22e9686 KS |
210 | if (has_devt(dirname)) |
211 | device_list_insert(dirname, "block", &device_list); | |
212 | else | |
213 | continue; | |
0e306d08 | 214 | |
56a8a624 | 215 | /* look for partitions */ |
82b9a637 | 216 | dir2 = opendir(dirname); |
e4dc0e11 KS |
217 | if (dir2 != NULL) { |
218 | for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) { | |
56a8a624 | 219 | char dirname2[DEVPATH_SIZE]; |
82b9a637 | 220 | |
f22e9686 | 221 | if (dent2->d_name[0] == '.') |
82b9a637 GKH |
222 | continue; |
223 | ||
56a8a624 KS |
224 | snprintf(dirname2, DEVPATH_SIZE, "%s/%s", dirname, dent2->d_name); |
225 | dirname2[DEVPATH_SIZE-1] = '\0'; | |
f22e9686 KS |
226 | |
227 | if (has_devt(dirname2)) | |
228 | device_list_insert(dirname2, "block", &device_list); | |
82b9a637 | 229 | } |
e13fa599 | 230 | closedir(dir2); |
82b9a637 GKH |
231 | } |
232 | } | |
e13fa599 | 233 | closedir(dir); |
82b9a637 | 234 | } |
0e306d08 GKH |
235 | exec_list(&device_list); |
236 | } | |
237 | ||
238 | static void udev_scan_class(void) | |
239 | { | |
56a8a624 | 240 | char base[DEVPATH_SIZE]; |
0e306d08 GKH |
241 | DIR *dir; |
242 | struct dirent *dent; | |
243 | LIST_HEAD(device_list); | |
244 | ||
56a8a624 KS |
245 | snprintf(base, DEVPATH_SIZE, "%s/class", sysfs_path); |
246 | base[DEVPATH_SIZE-1] = '\0'; | |
f22e9686 KS |
247 | |
248 | dir = opendir(base); | |
e4dc0e11 KS |
249 | if (dir != NULL) { |
250 | for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { | |
56a8a624 | 251 | char dirname[DEVPATH_SIZE]; |
e4dc0e11 KS |
252 | DIR *dir2; |
253 | struct dirent *dent2; | |
254 | ||
f22e9686 | 255 | if (dent->d_name[0] == '.') |
82b9a637 GKH |
256 | continue; |
257 | ||
56a8a624 KS |
258 | snprintf(dirname, DEVPATH_SIZE, "%s/%s", base, dent->d_name); |
259 | dirname[DEVPATH_SIZE-1] = '\0'; | |
260 | ||
82b9a637 | 261 | dir2 = opendir(dirname); |
e4dc0e11 KS |
262 | if (dir2 != NULL) { |
263 | for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) { | |
56a8a624 | 264 | char dirname2[DEVPATH_SIZE]; |
82b9a637 | 265 | |
f22e9686 | 266 | if (dent2->d_name[0] == '.') |
82b9a637 GKH |
267 | continue; |
268 | ||
56a8a624 KS |
269 | snprintf(dirname2, DEVPATH_SIZE, "%s/%s", dirname, dent2->d_name); |
270 | dirname2[DEVPATH_SIZE-1] = '\0'; | |
f22e9686 | 271 | |
aee380b6 | 272 | /* pass the net class as it is */ |
f22e9686 | 273 | if (strcmp(dent->d_name, "net") == 0) |
aee380b6 | 274 | device_list_insert(dirname2, "net", &device_list); |
f22e9686 KS |
275 | else if (has_devt(dirname2)) |
276 | device_list_insert(dirname2, dent->d_name, &device_list); | |
82b9a637 | 277 | } |
e13fa599 | 278 | closedir(dir2); |
82b9a637 GKH |
279 | } |
280 | } | |
e13fa599 | 281 | closedir(dir); |
82b9a637 | 282 | } |
0e306d08 | 283 | exec_list(&device_list); |
82b9a637 GKH |
284 | } |
285 | ||
56a8a624 | 286 | static void asmlinkage sig_handler(int signum) |
82b9a637 | 287 | { |
56a8a624 KS |
288 | switch (signum) { |
289 | case SIGALRM: | |
290 | exit(1); | |
291 | case SIGINT: | |
292 | case SIGTERM: | |
293 | exit(20 + signum); | |
294 | } | |
295 | } | |
296 | ||
297 | int main(int argc, char *argv[], char *envp[]) | |
298 | { | |
299 | struct sigaction act; | |
300 | ||
301 | udev_init_config(); | |
302 | ||
303 | /* set signal handlers */ | |
304 | memset(&act, 0x00, sizeof(act)); | |
305 | act.sa_handler = (void (*) (int))sig_handler; | |
306 | sigemptyset (&act.sa_mask); | |
307 | act.sa_flags = 0; | |
308 | sigaction(SIGALRM, &act, NULL); | |
309 | sigaction(SIGINT, &act, NULL); | |
310 | sigaction(SIGTERM, &act, NULL); | |
311 | ||
312 | /* trigger timeout to prevent hanging processes */ | |
313 | alarm(ALARM_TIMEOUT); | |
314 | ||
315 | /* set environment for executed programs */ | |
af4b05d4 | 316 | setenv("ACTION", "add", 1); |
69d2dbd6 | 317 | setenv("UDEV_START", "1", 1); |
af4b05d4 | 318 | |
56a8a624 KS |
319 | namedev_init(); |
320 | ||
0e306d08 | 321 | udev_scan_block(); |
56a8a624 | 322 | udev_scan_class(); |
af4b05d4 | 323 | |
3f20eac0 | 324 | return 0; |
82b9a637 | 325 | } |