chiark / gitweb /
[PATCH] Make udev/udevstart be one binary
[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         /* Setup env variables. */
79         setenv("UDEV_NO_SLEEP", "1", 1);
80
81         /* Now call __udev_hotplug(). */
82         if (__udev_hotplug("add", path, subsystem)) {
83                 dbg("Calling of udev_hotplug failed");
84                 exit(1);
85         }
86 }
87
88 /* list of devices that we should run last due to any one of a number of reasons */
89 static char *last_list[] = {
90         "/block/dm",    /* on here because dm wants to have the block devices around before it */
91         NULL,
92 };
93
94 static void exec_list(struct list_head *device_list)
95 {
96         struct device *loop_device;
97         struct device *tmp_device;
98
99         /* handle the devices we are allowed to, excluding the "last" type devices */
100         list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
101                 int found = 0;
102                 int i;
103                 for (i=0; last_list[i] != NULL; i++) {
104                         if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
105                                 found = 1;
106                                 break;
107                         }
108                 }
109                 if (found)
110                         continue;
111
112                 udev_exec(loop_device->path, loop_device->subsys);
113                 list_del(&loop_device->list);
114                 free(loop_device);
115         }
116
117         /* handle the rest of the devices left over, if any */
118         list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
119                 udev_exec(loop_device->path, loop_device->subsys);
120                 list_del(&loop_device->list);
121                 free(loop_device);
122         }
123 }
124
125 static void udev_scan_block(void)
126 {
127         DIR *dir;
128         struct dirent *dent;
129         LIST_HEAD(device_list);
130
131         dir = opendir(SYSBLOCK);
132         if (dir != NULL) {
133                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
134                         char dirname[MAX_PATHLEN];
135                         DIR *dir2;
136                         struct dirent *dent2;
137
138                         if ((strcmp(dent->d_name, ".") == 0) ||
139                             (strcmp(dent->d_name, "..") == 0))
140                                 continue;
141
142                         snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name);
143                         dirname[MAX_PATHLEN-1] = '\0';
144                         device_list_insert(dirname, "block", &device_list);
145
146                         snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name);
147                         dir2 = opendir(dirname);
148                         if (dir2 != NULL) {
149                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
150                                         char dirname2[MAX_PATHLEN];
151                                         DIR *dir3;
152                                         struct dirent *dent3;
153
154                                         if ((strcmp(dent2->d_name, ".") == 0) ||
155                                             (strcmp(dent2->d_name, "..") == 0))
156                                                 continue;
157
158                                         snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
159                                         dirname2[MAX_PATHLEN-1] = '\0';
160
161                                         dir3 = opendir(dirname2);
162                                         if (dir3 != NULL) {
163                                                 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
164                                                         char filename[MAX_PATHLEN];
165
166                                                         if (strcmp(dent3->d_name, "dev") == 0) {
167                                                                 snprintf(filename, MAX_PATHLEN, "/block/%s/%s",
168                                                                          dent->d_name, dent2->d_name);
169                                                                 filename[MAX_PATHLEN-1] = '\0';
170                                                                 device_list_insert(filename, "block", &device_list);
171                                                         }
172                                                 }
173                                                 closedir(dir3);
174                                         }
175                                 }
176                                 closedir(dir2);
177                         }
178                 }
179                 closedir(dir);
180         }
181
182         exec_list(&device_list);
183 }
184
185 static void udev_scan_class(void)
186 {
187         DIR *dir;
188         struct dirent *dent;
189         LIST_HEAD(device_list);
190
191         dir = opendir(SYSCLASS);
192         if (dir != NULL) {
193                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
194                         char dirname[MAX_PATHLEN];
195                         DIR *dir2;
196                         struct dirent *dent2;
197
198                         if ((strcmp(dent->d_name, ".") == 0) ||
199                             (strcmp(dent->d_name, "..") == 0))
200                                 continue;
201
202                         snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name);
203                         dirname[MAX_PATHLEN-1] = '\0';
204                         dir2 = opendir(dirname);
205                         if (dir2 != NULL) {
206                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
207                                         char dirname2[MAX_PATHLEN-1];
208                                         DIR *dir3;
209                                         struct dirent *dent3;
210
211                                         if ((strcmp(dent2->d_name, ".") == 0) ||
212                                             (strcmp(dent2->d_name, "..") == 0))
213                                                 continue;
214
215                                         snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
216                                         dirname2[MAX_PATHLEN-1] = '\0';
217
218                                         dir3 = opendir(dirname2);
219                                         if (dir3 != NULL) {
220                                                 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
221                                                         char filename[MAX_PATHLEN];
222
223                                                         if (strcmp(dent3->d_name, "dev") == 0) {
224                                                                 snprintf(filename, MAX_PATHLEN, "/class/%s/%s",
225                                                                          dent->d_name, dent2->d_name);
226                                                                 filename[MAX_PATHLEN-1] = '\0';
227                                                                 device_list_insert(filename, dent->d_name, &device_list);
228                                                         }
229                                                 }
230                                                 closedir(dir3);
231                                         }
232                                 }
233                                 closedir(dir2);
234                         }
235                 }
236                 closedir(dir);
237         }
238
239         exec_list(&device_list);
240 }
241
242 int udev_start(void)
243 {
244         udev_scan_class();
245         udev_scan_block();
246         return 0;
247 }