chiark / gitweb /
add flag for reading of precompiled rules
[elogind.git] / udevstart.c
1 /*
2  * udevstart.c
3  *
4  * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
6  *
7  * Quick and dirty way to populate a /dev with udev if your system
8  * does not have access to a shell.  Based originally on a patch
9  * from:
10  *      Harald Hoyer <harald@redhat.com>
11  *
12  *      This program is free software; you can redistribute it and/or modify it
13  *      under the terms of the GNU General Public License as published by the
14  *      Free Software Foundation version 2 of the License.
15  * 
16  *      This program is distributed in the hope that it will be useful, but
17  *      WITHOUT ANY WARRANTY; without even the implied warranty of
18  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *      General Public License for more details.
20  * 
21  *      You should have received a copy of the GNU General Public License along
22  *      with this program; if not, write to the Free Software Foundation, Inc.,
23  *      675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  */
26
27 #include <stdlib.h>
28 #include <stddef.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <ctype.h>
34 #include <dirent.h>
35 #include <signal.h>
36 #include <sys/wait.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39
40 #include "libsysfs/sysfs/libsysfs.h"
41 #include "udev_libc_wrapper.h"
42 #include "udev_sysfs.h"
43 #include "udev.h"
44 #include "udev_version.h"
45 #include "logging.h"
46 #include "udev_utils.h"
47 #include "udev_rules.h"
48 #include "list.h"
49
50 static const char *udev_run_str;
51 static const char *udev_log_str;
52 static struct udev_rules rules;
53
54 #ifdef USE_LOG
55 void log_message(int priority, const char *format, ...)
56 {
57         va_list args;
58
59         if (priority > udev_log_priority)
60                 return;
61
62         va_start(args, format);
63         vsyslog(priority, format, args);
64         va_end(args);
65 }
66 #endif
67
68 struct device {
69         struct list_head node;
70         char path[PATH_SIZE];
71         char subsys[NAME_SIZE];
72 };
73
74 /* sort files in lexical order */
75 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
76 {
77         struct device *loop_device;
78         struct device *new_device;
79
80         dbg("insert: '%s'\n", path);
81
82         list_for_each_entry(loop_device, device_list, node) {
83                 if (strcmp(loop_device->path, path) > 0) {
84                         break;
85                 }
86         }
87
88         new_device = malloc(sizeof(struct device));
89         if (new_device == NULL) {
90                 dbg("error malloc");
91                 return -ENOMEM;
92         }
93
94         strlcpy(new_device->path, path, sizeof(new_device->path));
95         strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys));
96         list_add_tail(&new_device->node, &loop_device->node);
97         dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
98         return 0;
99 }
100
101 /* list of devices that we should run last due to any one of a number of reasons */
102 static char *last_list[] = {
103         "/block/dm",    /* on here because dm wants to have the block devices around before it */
104         NULL,
105 };
106
107 /* list of devices that we should run first due to any one of a number of reasons */
108 static char *first_list[] = {
109         "/class/mem",   /* people tend to like their memory devices around first... */
110         NULL,
111 };
112
113 static int add_device(const char *path, const char *subsystem)
114 {
115         struct udevice udev;
116         struct sysfs_class_device *class_dev;
117         const char *devpath;
118
119         devpath = &path[strlen(sysfs_path)];
120
121         /* clear and set environment for next event */
122         clearenv();
123         setenv("ACTION", "add", 1);
124         setenv("DEVPATH", devpath, 1);
125         setenv("SUBSYSTEM", subsystem, 1);
126         setenv("UDEV_START", "1", 1);
127         if (udev_log_str)
128                 setenv("UDEV_LOG", udev_log_str, 1);
129         if (udev_run_str)
130                 setenv("UDEV_RUN", udev_run_str, 1);
131         dbg("exec: '%s' (%s)\n", devpath, path);
132
133         class_dev = sysfs_open_class_device_path(path);
134         if (class_dev == NULL) {
135                 dbg("sysfs_open_class_device_path failed");
136                 return -1;
137         }
138
139         udev_init_device(&udev, devpath, subsystem, "add");
140         udev.devt = get_devt(class_dev);
141         if (!udev.devt && udev.type != DEV_NET) {
142                 dbg("sysfs_open_class_device_path failed");
143                 return -1;
144         }
145         udev_rules_get_name(&rules, &udev, class_dev);
146         if (udev.ignore_device) {
147                 dbg("device event will be ignored");
148                 goto exit;
149         }
150         if (udev.name[0] == '\0') {
151                 dbg("device node creation supressed");
152                 goto run;
153         }
154
155         udev_add_device(&udev, class_dev);
156         if (udev.devname[0] != '\0')
157                 setenv("DEVNAME", udev.devname, 1);
158 run:
159         if (udev_run && !list_empty(&udev.run_list)) {
160                 struct name_entry *name_loop;
161
162                 dbg("executing run list");
163                 list_for_each_entry(name_loop, &udev.run_list, node) {
164                         if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
165                                 pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, "add");
166                         else
167                                 execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
168                 }
169         }
170 exit:
171         sysfs_close_class_device(class_dev);
172         udev_cleanup_device(&udev);
173
174         return 0;
175 }
176
177 static void exec_list(struct list_head *device_list)
178 {
179         struct device *loop_device;
180         struct device *tmp_device;
181         int i;
182
183         /* handle the "first" type devices first */
184         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
185                 for (i = 0; first_list[i] != NULL; i++) {
186                         if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
187                                 add_device(loop_device->path, loop_device->subsys);
188                                 list_del(&loop_device->node);
189                                 free(loop_device);
190                                 break;
191                         }
192                 }
193         }
194
195         /* handle the devices we are allowed to, excluding the "last" type devices */
196         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
197                 int found = 0;
198                 for (i = 0; last_list[i] != NULL; i++) {
199                         if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
200                                 found = 1;
201                                 break;
202                         }
203                 }
204                 if (found)
205                         continue;
206
207                 add_device(loop_device->path, loop_device->subsys);
208                 list_del(&loop_device->node);
209                 free(loop_device);
210         }
211
212         /* handle the rest of the devices left over, if any */
213         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
214                 add_device(loop_device->path, loop_device->subsys);
215                 list_del(&loop_device->node);
216                 free(loop_device);
217         }
218 }
219
220 static int has_devt(const char *directory)
221 {
222         char filename[PATH_SIZE];
223         struct stat statbuf;
224
225         snprintf(filename, sizeof(filename), "%s/dev", directory);
226         filename[sizeof(filename)-1] = '\0';
227
228         if (stat(filename, &statbuf) == 0)
229                 return 1;
230
231         return 0;
232 }
233
234 static void udev_scan_block(void)
235 {
236         char base[PATH_SIZE];
237         DIR *dir;
238         struct dirent *dent;
239         LIST_HEAD(device_list);
240
241         snprintf(base, sizeof(base), "%s/block", sysfs_path);
242         base[sizeof(base)-1] = '\0';
243
244         dir = opendir(base);
245         if (dir != NULL) {
246                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
247                         char dirname[PATH_SIZE];
248                         DIR *dir2;
249                         struct dirent *dent2;
250
251                         if (dent->d_name[0] == '.')
252                                 continue;
253
254                         snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
255                         dirname[sizeof(dirname)-1] = '\0';
256                         if (has_devt(dirname))
257                                 device_list_insert(dirname, "block", &device_list);
258                         else
259                                 continue;
260
261                         /* look for partitions */
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                                         if (has_devt(dirname2))
274                                                 device_list_insert(dirname2, "block", &device_list);
275                                 }
276                                 closedir(dir2);
277                         }
278                 }
279                 closedir(dir);
280         }
281         exec_list(&device_list);
282 }
283
284 static void udev_scan_class(void)
285 {
286         char base[PATH_SIZE];
287         DIR *dir;
288         struct dirent *dent;
289         LIST_HEAD(device_list);
290
291         snprintf(base, sizeof(base), "%s/class", sysfs_path);
292         base[sizeof(base)-1] = '\0';
293
294         dir = opendir(base);
295         if (dir != NULL) {
296                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
297                         char dirname[PATH_SIZE];
298                         DIR *dir2;
299                         struct dirent *dent2;
300
301                         if (dent->d_name[0] == '.')
302                                 continue;
303
304                         snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
305                         dirname[sizeof(dirname)-1] = '\0';
306
307                         dir2 = opendir(dirname);
308                         if (dir2 != NULL) {
309                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
310                                         char dirname2[PATH_SIZE];
311
312                                         if (dent2->d_name[0] == '.')
313                                                 continue;
314
315                                         snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
316                                         dirname2[sizeof(dirname2)-1] = '\0';
317
318                                         /* pass the net class as it is */
319                                         if (strcmp(dent->d_name, "net") == 0)
320                                                 device_list_insert(dirname2, "net", &device_list);
321                                         else if (has_devt(dirname2))
322                                                 device_list_insert(dirname2, dent->d_name, &device_list);
323                                 }
324                                 closedir(dir2);
325                         }
326                 }
327                 closedir(dir);
328         }
329         exec_list(&device_list);
330 }
331
332 static void asmlinkage sig_handler(int signum)
333 {
334         switch (signum) {
335                 case SIGALRM:
336                         exit(1);
337                 case SIGINT:
338                 case SIGTERM:
339                         exit(20 + signum);
340         }
341 }
342
343 int main(int argc, char *argv[], char *envp[])
344 {
345         struct sigaction act;
346
347         logging_init("udevstart");
348         udev_init_config();
349         dbg("version %s", UDEV_VERSION);
350
351         udev_run_str = getenv("UDEV_RUN");
352         udev_log_str = getenv("UDEV_LOG");
353
354         /* disable all logging if not explicitely requested */
355         if (udev_log_str == NULL)
356                 udev_log_priority = 0;
357
358         /* set signal handlers */
359         memset(&act, 0x00, sizeof(act));
360         act.sa_handler = (void (*) (int))sig_handler;
361         sigemptyset (&act.sa_mask);
362         act.sa_flags = 0;
363         sigaction(SIGALRM, &act, NULL);
364         sigaction(SIGINT, &act, NULL);
365         sigaction(SIGTERM, &act, NULL);
366
367         /* trigger timeout to prevent hanging processes */
368         alarm(UDEV_ALARM_TIMEOUT);
369
370         udev_rules_init(&rules, 0, 1);
371
372         udev_scan_block();
373         udev_scan_class();
374
375         udev_rules_close(&rules);
376         logging_close();
377         return 0;
378 }