chiark / gitweb /
[PATCH] split udev and udevstart
[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 <unistd.h>
30 #include <errno.h>
31 #include <ctype.h>
32 #include <dirent.h>
33 #include <signal.h>
34 #include <sys/wait.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37
38 #include "libsysfs/sysfs/libsysfs.h"
39 #include "logging.h"
40 #include "namedev.h"
41 #include "udev_utils.h"
42 #include "list.h"
43 #include "udev.h"
44
45 #ifdef USE_LOG
46 void log_message(int level, const char *format, ...)
47 {
48 }
49 #endif
50
51 struct device {
52         struct list_head list;
53         char path[DEVPATH_SIZE];
54         char subsys[SUBSYSTEM_SIZE];
55 };
56
57 /* sort files in lexical order */
58 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
59 {
60         struct device *loop_device;
61         struct device *new_device;
62
63         dbg("insert: '%s'\n", path);
64
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);
80         dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
81         return 0;
82 }
83
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
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
96 static int add_device(const char *path, const char *subsystem)
97 {
98         struct udevice udev;
99         struct sysfs_class_device *class_dev;
100         const char *devpath;
101
102         devpath = &path[strlen(sysfs_path)];
103
104         /* set environment for callouts and dev.d/ */
105         setenv("DEVPATH", devpath, 1);
106         setenv("SUBSYSTEM", subsystem, 1);
107
108         dbg("exec: '%s' (%s)\n", devpath, path);
109
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
116         udev_init_device(&udev, devpath, subsystem);
117         udev_add_device(&udev, class_dev);
118
119         /* run dev.d/ scripts if we created a node or changed a netif name */
120         if (udev_dev_d && udev.devname[0] != '\0') {
121                 setenv("DEVNAME", udev.devname, 1);
122                 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
123         }
124
125         sysfs_close_class_device(class_dev);
126         udev_cleanup_device(&udev);
127
128         return 0;
129 }
130
131 static void exec_list(struct list_head *device_list)
132 {
133         struct device *loop_device;
134         struct device *tmp_device;
135         int i;
136
137         /* handle the "first" type devices first */
138         list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
139                 for (i = 0; first_list[i] != NULL; i++) {
140                         if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
141                                 add_device(loop_device->path, loop_device->subsys);
142                                 list_del(&loop_device->list);
143                                 free(loop_device);
144                                 break;
145                         }
146                 }
147         }
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;
152                 for (i = 0; last_list[i] != NULL; i++) {
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
161                 add_device(loop_device->path, loop_device->subsys);
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) {
168                 add_device(loop_device->path, loop_device->subsys);
169                 list_del(&loop_device->list);
170                 free(loop_device);
171         }
172 }
173
174 static int has_devt(const char *directory)
175 {
176         char filename[NAME_SIZE];
177         struct stat statbuf;
178
179         snprintf(filename, NAME_SIZE, "%s/dev", directory);
180         filename[NAME_SIZE-1] = '\0';
181
182         if (stat(filename, &statbuf) == 0)
183                 return 1;
184
185         return 0;
186 }
187
188 static void udev_scan_block(void)
189 {
190         char base[NAME_SIZE];
191         DIR *dir;
192         struct dirent *dent;
193         LIST_HEAD(device_list);
194
195         snprintf(base, DEVPATH_SIZE, "%s/block", sysfs_path);
196         base[DEVPATH_SIZE-1] = '\0';
197
198         dir = opendir(base);
199         if (dir != NULL) {
200                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
201                         char dirname[DEVPATH_SIZE];
202                         DIR *dir2;
203                         struct dirent *dent2;
204
205                         if (dent->d_name[0] == '.')
206                                 continue;
207
208                         snprintf(dirname, NAME_SIZE, "%s/%s", base, dent->d_name);
209                         dirname[NAME_SIZE-1] = '\0';
210                         if (has_devt(dirname))
211                                 device_list_insert(dirname, "block", &device_list);
212                         else
213                                 continue;
214
215                         /* look for partitions */
216                         dir2 = opendir(dirname);
217                         if (dir2 != NULL) {
218                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
219                                         char dirname2[DEVPATH_SIZE];
220
221                                         if (dent2->d_name[0] == '.')
222                                                 continue;
223
224                                         snprintf(dirname2, DEVPATH_SIZE, "%s/%s", dirname, dent2->d_name);
225                                         dirname2[DEVPATH_SIZE-1] = '\0';
226
227                                         if (has_devt(dirname2))
228                                                 device_list_insert(dirname2, "block", &device_list);
229                                 }
230                                 closedir(dir2);
231                         }
232                 }
233                 closedir(dir);
234         }
235         exec_list(&device_list);
236 }
237
238 static void udev_scan_class(void)
239 {
240         char base[DEVPATH_SIZE];
241         DIR *dir;
242         struct dirent *dent;
243         LIST_HEAD(device_list);
244
245         snprintf(base, DEVPATH_SIZE, "%s/class", sysfs_path);
246         base[DEVPATH_SIZE-1] = '\0';
247
248         dir = opendir(base);
249         if (dir != NULL) {
250                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
251                         char dirname[DEVPATH_SIZE];
252                         DIR *dir2;
253                         struct dirent *dent2;
254
255                         if (dent->d_name[0] == '.')
256                                 continue;
257
258                         snprintf(dirname, DEVPATH_SIZE, "%s/%s", base, dent->d_name);
259                         dirname[DEVPATH_SIZE-1] = '\0';
260
261                         dir2 = opendir(dirname);
262                         if (dir2 != NULL) {
263                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
264                                         char dirname2[DEVPATH_SIZE];
265
266                                         if (dent2->d_name[0] == '.')
267                                                 continue;
268
269                                         snprintf(dirname2, DEVPATH_SIZE, "%s/%s", dirname, dent2->d_name);
270                                         dirname2[DEVPATH_SIZE-1] = '\0';
271
272                                         /* pass the net class as it is */
273                                         if (strcmp(dent->d_name, "net") == 0)
274                                                 device_list_insert(dirname2, "net", &device_list);
275                                         else if (has_devt(dirname2))
276                                                 device_list_insert(dirname2, dent->d_name, &device_list);
277                                 }
278                                 closedir(dir2);
279                         }
280                 }
281                 closedir(dir);
282         }
283         exec_list(&device_list);
284 }
285
286 static void asmlinkage sig_handler(int signum)
287 {
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 */
316         setenv("ACTION", "add", 1);
317         setenv("UDEV_START", "1", 1);
318
319         namedev_init();
320
321         udev_scan_block();
322         udev_scan_class();
323
324         return 0;
325 }