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