chiark / gitweb /
remove all built-in wait_for_sysfs logic
[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 <syslog.h>
37 #include <sys/wait.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40
41 #include "libsysfs/sysfs/libsysfs.h"
42 #include "udev_libc_wrapper.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         const char *devpath = &path[strlen(sysfs_path)];
80
81         dbg("insert: '%s'\n", devpath);
82
83         list_for_each_entry(loop_device, device_list, node) {
84                 if (strcmp(loop_device->path, devpath) > 0) {
85                         break;
86                 }
87         }
88
89         new_device = malloc(sizeof(struct device));
90         if (new_device == NULL) {
91                 dbg("error malloc");
92                 return -ENOMEM;
93         }
94
95         strlcpy(new_device->path, devpath, sizeof(new_device->path));
96         strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys));
97         list_add_tail(&new_device->node, &loop_device->node);
98         dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
99         return 0;
100 }
101
102 /* list of devices that we should run last due to any one of a number of reasons */
103 static char *last_list[] = {
104         "/block/dm",    /* on here because dm wants to have the block devices around before it */
105         NULL,
106 };
107
108 /* list of devices that we should run first due to any one of a number of reasons */
109 static char *first_list[] = {
110         "/class/mem",
111         "/class/tty",
112         NULL,
113 };
114
115 static int add_device(const char *devpath, const char *subsystem)
116 {
117         struct udevice udev;
118         struct sysfs_class_device *class_dev;
119         char path[PATH_SIZE];
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("add '%s'", devpath);
132
133         snprintf(path, sizeof(path), "%s%s", sysfs_path, devpath);
134         path[sizeof(path)-1] = '\0';
135         class_dev = sysfs_open_class_device_path(path);
136         if (class_dev == NULL) {
137                 dbg("sysfs_open_class_device_path failed");
138                 return -1;
139         }
140
141         udev_init_device(&udev, &class_dev->path[strlen(sysfs_path)], subsystem, "add");
142         udev.devt = get_devt(class_dev);
143         if (!udev.devt && udev.type != DEV_NET) {
144                 dbg("sysfs_open_class_device_path failed");
145                 return -1;
146         }
147         udev_rules_get_name(&rules, &udev, class_dev);
148         if (udev.ignore_device) {
149                 dbg("device event will be ignored");
150                 goto exit;
151         }
152         if (udev.name[0] == '\0') {
153                 dbg("device node creation supressed");
154                 goto run;
155         }
156
157         udev_add_device(&udev, class_dev);
158         if (udev.devname[0] != '\0')
159                 setenv("DEVNAME", udev.devname, 1);
160 run:
161         if (udev_run && !list_empty(&udev.run_list)) {
162                 struct name_entry *name_loop;
163
164                 dbg("executing run list");
165                 list_for_each_entry(name_loop, &udev.run_list, node) {
166                         if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
167                                 pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, "add");
168                         else
169                                 run_program(name_loop->name, udev.subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
170                 }
171         }
172 exit:
173         sysfs_close_class_device(class_dev);
174         udev_cleanup_device(&udev);
175
176         return 0;
177 }
178
179 static void exec_list(struct list_head *device_list)
180 {
181         struct device *loop_device;
182         struct device *tmp_device;
183         int i;
184
185         /* handle the "first" type devices first */
186         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
187                 for (i = 0; first_list[i] != NULL; i++) {
188                         if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
189                                 add_device(loop_device->path, loop_device->subsys);
190                                 list_del(&loop_device->node);
191                                 free(loop_device);
192                                 break;
193                         }
194                 }
195         }
196
197         /* handle the devices we are allowed to, excluding the "last" type devices */
198         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
199                 int found = 0;
200                 for (i = 0; last_list[i] != NULL; i++) {
201                         if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
202                                 found = 1;
203                                 break;
204                         }
205                 }
206                 if (found)
207                         continue;
208
209                 add_device(loop_device->path, loop_device->subsys);
210                 list_del(&loop_device->node);
211                 free(loop_device);
212         }
213
214         /* handle the rest of the devices left over, if any */
215         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
216                 add_device(loop_device->path, loop_device->subsys);
217                 list_del(&loop_device->node);
218                 free(loop_device);
219         }
220 }
221
222 static int has_devt(const char *path)
223 {
224         char filename[PATH_SIZE];
225         struct stat statbuf;
226
227         snprintf(filename, sizeof(filename), "%s/dev", path);
228         filename[sizeof(filename)-1] = '\0';
229
230         if (stat(filename, &statbuf) == 0)
231                 return 1;
232
233         return 0;
234 }
235
236 static void udev_scan_block(struct list_head *device_list)
237 {
238         char base[PATH_SIZE];
239         DIR *dir;
240         struct dirent *dent;
241
242         snprintf(base, sizeof(base), "%s/block", sysfs_path);
243         base[sizeof(base)-1] = '\0';
244
245         dir = opendir(base);
246         if (dir != NULL) {
247                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
248                         char dirname[PATH_SIZE];
249                         DIR *dir2;
250                         struct dirent *dent2;
251
252                         if (dent->d_name[0] == '.')
253                                 continue;
254
255                         snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
256                         dirname[sizeof(dirname)-1] = '\0';
257                         if (has_devt(dirname))
258                                 device_list_insert(dirname, "block", device_list);
259                         else
260                                 continue;
261
262                         /* look for partitions */
263                         dir2 = opendir(dirname);
264                         if (dir2 != NULL) {
265                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
266                                         char dirname2[PATH_SIZE];
267
268                                         if (dent2->d_name[0] == '.')
269                                                 continue;
270
271                                         snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
272                                         dirname2[sizeof(dirname2)-1] = '\0';
273
274                                         if (has_devt(dirname2))
275                                                 device_list_insert(dirname2, "block", device_list);
276                                 }
277                                 closedir(dir2);
278                         }
279                 }
280                 closedir(dir);
281         }
282 }
283
284 static void udev_scan_class(struct list_head *device_list)
285 {
286         char base[PATH_SIZE];
287         DIR *dir;
288         struct dirent *dent;
289
290         snprintf(base, sizeof(base), "%s/class", sysfs_path);
291         base[sizeof(base)-1] = '\0';
292
293         dir = opendir(base);
294         if (dir != NULL) {
295                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
296                         char dirname[PATH_SIZE];
297                         DIR *dir2;
298                         struct dirent *dent2;
299
300                         if (dent->d_name[0] == '.')
301                                 continue;
302
303                         snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
304                         dirname[sizeof(dirname)-1] = '\0';
305
306                         dir2 = opendir(dirname);
307                         if (dir2 != NULL) {
308                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
309                                         char dirname2[PATH_SIZE];
310
311                                         if (dent2->d_name[0] == '.')
312                                                 continue;
313
314                                         snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
315                                         dirname2[sizeof(dirname2)-1] = '\0';
316
317                                         if (has_devt(dirname2) || strcmp(dent->d_name, "net") == 0)
318                                                 device_list_insert(dirname2, dent->d_name, device_list);
319                                 }
320                                 closedir(dir2);
321                         }
322                 }
323                 closedir(dir);
324         }
325 }
326
327 static void asmlinkage sig_handler(int signum)
328 {
329         switch (signum) {
330                 case SIGALRM:
331                         exit(1);
332                 case SIGINT:
333                 case SIGTERM:
334                         exit(20 + signum);
335         }
336 }
337
338 int main(int argc, char *argv[], char *envp[])
339 {
340         LIST_HEAD(device_list);
341         struct sigaction act;
342
343         logging_init("udevstart");
344         udev_init_config();
345         dbg("version %s", UDEV_VERSION);
346
347         udev_run_str = getenv("UDEV_RUN");
348         udev_log_str = getenv("UDEV_LOG");
349
350         /* disable all logging if not explicitely requested */
351         if (udev_log_str == NULL)
352                 udev_log_priority = 0;
353
354         /* set signal handlers */
355         memset(&act, 0x00, sizeof(act));
356         act.sa_handler = (void (*) (int))sig_handler;
357         sigemptyset (&act.sa_mask);
358         act.sa_flags = 0;
359         sigaction(SIGALRM, &act, NULL);
360         sigaction(SIGINT, &act, NULL);
361         sigaction(SIGTERM, &act, NULL);
362
363         /* trigger timeout to prevent hanging processes */
364         alarm(UDEV_ALARM_TIMEOUT);
365
366         udev_rules_init(&rules, 1);
367
368         udev_scan_class(&device_list);
369         udev_scan_block(&device_list);
370         exec_list(&device_list);
371
372         udev_rules_close(&rules);
373         logging_close();
374         return 0;
375 }