chiark / gitweb /
[PATCH] make the udev object available to more processing stages
[elogind.git] / udevstart.c
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>
26 #include <stddef.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <ctype.h>
31 #include <dirent.h>
32 #include <sys/wait.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include "libsysfs/sysfs/libsysfs.h"
37 #include "logging.h"
38 #include "udev_lib.h"
39 #include "list.h"
40 #include "udev.h"
41
42
43 #define MAX_PATHLEN             1024
44 #define SYSBLOCK                "/sys/block"
45 #define SYSCLASS                "/sys/class"
46
47 struct device {
48         struct list_head list;
49         char path[MAX_PATHLEN];
50         char subsys[MAX_PATHLEN];
51 };
52
53 /* sort files in lexical order */
54 static int device_list_insert(char *path, char *subsystem, struct list_head *device_list)
55 {
56         struct device *loop_device;
57         struct device *new_device;
58
59         list_for_each_entry(loop_device, device_list, list) {
60                 if (strcmp(loop_device->path, path) > 0) {
61                         break;
62                 }
63         }
64
65         new_device = malloc(sizeof(struct device));
66         if (new_device == NULL) {
67                 dbg("error malloc");
68                 return -ENOMEM;
69         }
70
71         strfieldcpy(new_device->path, path);
72         strfieldcpy(new_device->subsys, subsystem);
73         list_add_tail(&new_device->list, &loop_device->list);
74         dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
75         return 0;
76 }
77
78 /* list of devices that we should run last due to any one of a number of reasons */
79 static char *last_list[] = {
80         "/block/dm",    /* on here because dm wants to have the block devices around before it */
81         NULL,
82 };
83
84 /* list of devices that we should run first due to any one of a number of reasons */
85 static char *first_list[] = {
86         "/class/mem",   /* people tend to like their memory devices around first... */
87         NULL,
88 };
89
90 static int add_device(char *devpath, char *subsystem)
91 {
92         struct udevice udev;
93         char path[SYSFS_PATH_MAX];
94         struct sysfs_class_device *class_dev;
95
96         /* set environment for callouts and dev.d/ */
97         setenv("DEVPATH", devpath, 1);
98         setenv("SUBSYSTEM", subsystem, 1);
99
100         snprintf(path, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
101         class_dev = sysfs_open_class_device_path(path);
102         if (class_dev == NULL) {
103                 dbg ("sysfs_open_class_device_path failed");
104                 return -ENODEV;
105         }
106
107         udev_set_values(&udev, devpath, subsystem, "add");
108         udev_add_device(&udev, class_dev);
109
110         /* run scripts */
111         dev_d_execute(&udev);
112
113         sysfs_close_class_device(class_dev);
114
115         return 0;
116 }
117
118 static void exec_list(struct list_head *device_list)
119 {
120         struct device *loop_device;
121         struct device *tmp_device;
122         int i;
123
124         /* handle the "first" type devices first */
125         list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
126                 for (i=0; first_list[i] != NULL; i++) {
127                         if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
128                                 add_device(loop_device->path, loop_device->subsys);
129                                 list_del(&loop_device->list);
130                                 free(loop_device);
131                                 break;
132                         }
133                 }
134         }
135
136         /* handle the devices we are allowed to, excluding the "last" type devices */
137         list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
138                 int found = 0;
139                 for (i=0; last_list[i] != NULL; i++) {
140                         if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
141                                 found = 1;
142                                 break;
143                         }
144                 }
145                 if (found)
146                         continue;
147
148                 add_device(loop_device->path, loop_device->subsys);
149                 list_del(&loop_device->list);
150                 free(loop_device);
151         }
152
153         /* handle the rest of the devices left over, if any */
154         list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
155                 add_device(loop_device->path, loop_device->subsys);
156                 list_del(&loop_device->list);
157                 free(loop_device);
158         }
159 }
160
161 static void udev_scan_block(void)
162 {
163         DIR *dir;
164         struct dirent *dent;
165         LIST_HEAD(device_list);
166
167         dir = opendir(SYSBLOCK);
168         if (dir != NULL) {
169                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
170                         char dirname[MAX_PATHLEN];
171                         DIR *dir2;
172                         struct dirent *dent2;
173
174                         if ((strcmp(dent->d_name, ".") == 0) ||
175                             (strcmp(dent->d_name, "..") == 0))
176                                 continue;
177
178                         snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name);
179                         dirname[MAX_PATHLEN-1] = '\0';
180                         device_list_insert(dirname, "block", &device_list);
181
182                         snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name);
183                         dir2 = opendir(dirname);
184                         if (dir2 != NULL) {
185                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
186                                         char dirname2[MAX_PATHLEN];
187                                         DIR *dir3;
188                                         struct dirent *dent3;
189
190                                         if ((strcmp(dent2->d_name, ".") == 0) ||
191                                             (strcmp(dent2->d_name, "..") == 0))
192                                                 continue;
193
194                                         snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
195                                         dirname2[MAX_PATHLEN-1] = '\0';
196
197                                         dir3 = opendir(dirname2);
198                                         if (dir3 != NULL) {
199                                                 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
200                                                         char filename[MAX_PATHLEN];
201
202                                                         if (strcmp(dent3->d_name, "dev") == 0) {
203                                                                 snprintf(filename, MAX_PATHLEN, "/block/%s/%s",
204                                                                          dent->d_name, dent2->d_name);
205                                                                 filename[MAX_PATHLEN-1] = '\0';
206                                                                 device_list_insert(filename, "block", &device_list);
207                                                         }
208                                                 }
209                                                 closedir(dir3);
210                                         }
211                                 }
212                                 closedir(dir2);
213                         }
214                 }
215                 closedir(dir);
216         }
217
218         exec_list(&device_list);
219 }
220
221 static void udev_scan_class(void)
222 {
223         DIR *dir;
224         struct dirent *dent;
225         LIST_HEAD(device_list);
226
227         dir = opendir(SYSCLASS);
228         if (dir != NULL) {
229                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
230                         char dirname[MAX_PATHLEN];
231                         DIR *dir2;
232                         struct dirent *dent2;
233
234                         if ((strcmp(dent->d_name, ".") == 0) ||
235                             (strcmp(dent->d_name, "..") == 0))
236                                 continue;
237
238                         snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name);
239                         dirname[MAX_PATHLEN-1] = '\0';
240                         dir2 = opendir(dirname);
241                         if (dir2 != NULL) {
242                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
243                                         char dirname2[MAX_PATHLEN];
244                                         DIR *dir3;
245                                         struct dirent *dent3;
246
247                                         if ((strcmp(dent2->d_name, ".") == 0) ||
248                                             (strcmp(dent2->d_name, "..") == 0))
249                                                 continue;
250
251                                         /* pass the net class as it is */
252                                         if (strcmp(dent->d_name, "net") == 0) {
253                                                 snprintf(dirname2, MAX_PATHLEN, "/class/net/%s", dent2->d_name);
254                                                 device_list_insert(dirname2, "net", &device_list);
255                                                 continue;
256                                         }
257
258                                         snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
259                                         dirname2[MAX_PATHLEN-1] = '\0';
260                                         dir3 = opendir(dirname2);
261                                         if (dir3 != NULL) {
262                                                 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
263                                                         char filename[MAX_PATHLEN];
264
265                                                         /* pass devices with a "dev" file */
266                                                         if (strcmp(dent3->d_name, "dev") == 0) {
267                                                                 snprintf(filename, MAX_PATHLEN, "/class/%s/%s",
268                                                                          dent->d_name, dent2->d_name);
269                                                                 filename[MAX_PATHLEN-1] = '\0';
270                                                                 device_list_insert(filename, dent->d_name, &device_list);
271                                                                 break;
272                                                         }
273                                                 }
274                                                 closedir(dir3);
275                                         }
276                                 }
277                                 closedir(dir2);
278                         }
279                 }
280                 closedir(dir);
281         }
282
283         exec_list(&device_list);
284 }
285
286 int udev_start(void)
287 {
288         /* set environment for callouts and dev.d/ */
289         setenv("ACTION", "add", 1);
290         setenv("UDEVSTART", "1", 1);
291
292         udev_scan_class();
293         udev_scan_block();
294
295         return 0;
296 }