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