chiark / gitweb /
2d5553e95529c8870aa21c622d361e69c5a5abaa
[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 "logging.h"
37 #include "udev_lib.h"
38 #include "list.h"
39 #include "udev.h"
40
41
42 #define MAX_PATHLEN             1024
43 #define SYSBLOCK                "/sys/block"
44 #define SYSCLASS                "/sys/class"
45
46 struct device {
47         struct list_head list;
48         char path[MAX_PATHLEN];
49         char subsys[MAX_PATHLEN];
50 };
51
52 /* sort files in lexical order */
53 static int device_list_insert(char *path, char *subsystem, struct list_head *device_list)
54 {
55         struct device *loop_device;
56         struct device *new_device;
57
58         list_for_each_entry(loop_device, device_list, list) {
59                 if (strcmp(loop_device->path, path) > 0) {
60                         break;
61                 }
62         }
63
64         new_device = malloc(sizeof(struct device));
65         if (new_device == NULL) {
66                 dbg("error malloc");
67                 return -ENOMEM;
68         }
69
70         strfieldcpy(new_device->path, path);
71         strfieldcpy(new_device->subsys, subsystem);
72         list_add_tail(&new_device->list, &loop_device->list);
73         return 0;
74 }
75
76 static void udev_exec(const char *path, const char* subsystem)
77 {
78         /* Now call __udev_hotplug(). */
79         if (__udev_hotplug("add", path, subsystem)) {
80                 dbg("Calling of udev_hotplug failed");
81                 exit(1);
82         }
83 }
84
85 /* list of devices that we should run last due to any one of a number of reasons */
86 static char *last_list[] = {
87         "/block/dm",    /* on here because dm wants to have the block devices around before it */
88         NULL,
89 };
90
91 static void exec_list(struct list_head *device_list)
92 {
93         struct device *loop_device;
94         struct device *tmp_device;
95
96         /* handle the devices we are allowed to, excluding the "last" type devices */
97         list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
98                 int found = 0;
99                 int i;
100                 for (i=0; last_list[i] != NULL; i++) {
101                         if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
102                                 found = 1;
103                                 break;
104                         }
105                 }
106                 if (found)
107                         continue;
108
109                 udev_exec(loop_device->path, loop_device->subsys);
110                 list_del(&loop_device->list);
111                 free(loop_device);
112         }
113
114         /* handle the rest of the devices left over, if any */
115         list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
116                 udev_exec(loop_device->path, loop_device->subsys);
117                 list_del(&loop_device->list);
118                 free(loop_device);
119         }
120 }
121
122 static void udev_scan_block(void)
123 {
124         DIR *dir;
125         struct dirent *dent;
126         LIST_HEAD(device_list);
127
128         dir = opendir(SYSBLOCK);
129         if (dir != NULL) {
130                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
131                         char dirname[MAX_PATHLEN];
132                         DIR *dir2;
133                         struct dirent *dent2;
134
135                         if ((strcmp(dent->d_name, ".") == 0) ||
136                             (strcmp(dent->d_name, "..") == 0))
137                                 continue;
138
139                         snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name);
140                         dirname[MAX_PATHLEN-1] = '\0';
141                         device_list_insert(dirname, "block", &device_list);
142
143                         snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name);
144                         dir2 = opendir(dirname);
145                         if (dir2 != NULL) {
146                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
147                                         char dirname2[MAX_PATHLEN];
148                                         DIR *dir3;
149                                         struct dirent *dent3;
150
151                                         if ((strcmp(dent2->d_name, ".") == 0) ||
152                                             (strcmp(dent2->d_name, "..") == 0))
153                                                 continue;
154
155                                         snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
156                                         dirname2[MAX_PATHLEN-1] = '\0';
157
158                                         dir3 = opendir(dirname2);
159                                         if (dir3 != NULL) {
160                                                 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
161                                                         char filename[MAX_PATHLEN];
162
163                                                         if (strcmp(dent3->d_name, "dev") == 0) {
164                                                                 snprintf(filename, MAX_PATHLEN, "/block/%s/%s",
165                                                                          dent->d_name, dent2->d_name);
166                                                                 filename[MAX_PATHLEN-1] = '\0';
167                                                                 device_list_insert(filename, "block", &device_list);
168                                                         }
169                                                 }
170                                                 closedir(dir3);
171                                         }
172                                 }
173                                 closedir(dir2);
174                         }
175                 }
176                 closedir(dir);
177         }
178
179         exec_list(&device_list);
180 }
181
182 static void udev_scan_class(void)
183 {
184         DIR *dir;
185         struct dirent *dent;
186         LIST_HEAD(device_list);
187
188         dir = opendir(SYSCLASS);
189         if (dir != NULL) {
190                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
191                         char dirname[MAX_PATHLEN];
192                         DIR *dir2;
193                         struct dirent *dent2;
194
195                         if ((strcmp(dent->d_name, ".") == 0) ||
196                             (strcmp(dent->d_name, "..") == 0))
197                                 continue;
198
199                         snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name);
200                         dirname[MAX_PATHLEN-1] = '\0';
201                         dir2 = opendir(dirname);
202                         if (dir2 != NULL) {
203                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
204                                         char dirname2[MAX_PATHLEN-1];
205                                         DIR *dir3;
206                                         struct dirent *dent3;
207
208                                         if ((strcmp(dent2->d_name, ".") == 0) ||
209                                             (strcmp(dent2->d_name, "..") == 0))
210                                                 continue;
211
212                                         snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
213                                         dirname2[MAX_PATHLEN-1] = '\0';
214
215                                         dir3 = opendir(dirname2);
216                                         if (dir3 != NULL) {
217                                                 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
218                                                         char filename[MAX_PATHLEN];
219
220                                                         if (strcmp(dent3->d_name, "dev") == 0) {
221                                                                 snprintf(filename, MAX_PATHLEN, "/class/%s/%s",
222                                                                          dent->d_name, dent2->d_name);
223                                                                 filename[MAX_PATHLEN-1] = '\0';
224                                                                 device_list_insert(filename, dent->d_name, &device_list);
225                                                         }
226                                                 }
227                                                 closedir(dir3);
228                                         }
229                                 }
230                                 closedir(dir2);
231                         }
232                 }
233                 closedir(dir);
234         }
235
236         exec_list(&device_list);
237 }
238
239 int udev_start(void)
240 {
241         udev_scan_class();
242         udev_scan_block();
243         return 0;
244 }