chiark / gitweb /
fix klibc build issue in cdrom_id.c
[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_sysfs.h"
44 #include "udev.h"
45 #include "udev_version.h"
46 #include "logging.h"
47 #include "udev_utils.h"
48 #include "udev_rules.h"
49 #include "list.h"
50
51 static const char *udev_run_str;
52 static const char *udev_log_str;
53 static struct udev_rules rules;
54
55 #ifdef USE_LOG
56 void log_message(int priority, const char *format, ...)
57 {
58         va_list args;
59
60         if (priority > udev_log_priority)
61                 return;
62
63         va_start(args, format);
64         vsyslog(priority, format, args);
65         va_end(args);
66 }
67 #endif
68
69 struct device {
70         struct list_head node;
71         char path[PATH_SIZE];
72         char subsys[NAME_SIZE];
73 };
74
75 /* sort files in lexical order */
76 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
77 {
78         struct device *loop_device;
79         struct device *new_device;
80
81         dbg("insert: '%s'\n", path);
82
83         list_for_each_entry(loop_device, device_list, node) {
84                 if (strcmp(loop_device->path, path) > 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, path, 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",   /* people tend to like their memory devices around first... */
111         NULL,
112 };
113
114 static int add_device(const char *path, const char *subsystem)
115 {
116         struct udevice udev;
117         struct sysfs_class_device *class_dev;
118         const char *devpath;
119
120         devpath = &path[strlen(sysfs_path)];
121
122         /* clear and set environment for next event */
123         clearenv();
124         setenv("ACTION", "add", 1);
125         setenv("DEVPATH", devpath, 1);
126         setenv("SUBSYSTEM", subsystem, 1);
127         setenv("UDEV_START", "1", 1);
128         if (udev_log_str)
129                 setenv("UDEV_LOG", udev_log_str, 1);
130         if (udev_run_str)
131                 setenv("UDEV_RUN", udev_run_str, 1);
132         dbg("exec: '%s' (%s)\n", devpath, path);
133
134         class_dev = sysfs_open_class_device_path(path);
135         if (class_dev == NULL) {
136                 dbg("sysfs_open_class_device_path failed");
137                 return -1;
138         }
139
140         udev_init_device(&udev, devpath, subsystem, "add");
141         udev.devt = get_devt(class_dev);
142         if (!udev.devt && udev.type != DEV_NET) {
143                 dbg("sysfs_open_class_device_path failed");
144                 return -1;
145         }
146         udev_rules_get_name(&rules, &udev, class_dev);
147         if (udev.ignore_device) {
148                 dbg("device event will be ignored");
149                 goto exit;
150         }
151         if (udev.name[0] == '\0') {
152                 dbg("device node creation supressed");
153                 goto run;
154         }
155
156         udev_add_device(&udev, class_dev);
157         if (udev.devname[0] != '\0')
158                 setenv("DEVNAME", udev.devname, 1);
159 run:
160         if (udev_run && !list_empty(&udev.run_list)) {
161                 struct name_entry *name_loop;
162
163                 dbg("executing run list");
164                 list_for_each_entry(name_loop, &udev.run_list, node) {
165                         if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
166                                 pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, "add");
167                         else
168                                 run_program(name_loop->name, udev.subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_DEBUG));
169                 }
170         }
171 exit:
172         sysfs_close_class_device(class_dev);
173         udev_cleanup_device(&udev);
174
175         return 0;
176 }
177
178 static void exec_list(struct list_head *device_list)
179 {
180         struct device *loop_device;
181         struct device *tmp_device;
182         int i;
183
184         /* handle the "first" type devices first */
185         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
186                 for (i = 0; first_list[i] != NULL; i++) {
187                         if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
188                                 add_device(loop_device->path, loop_device->subsys);
189                                 list_del(&loop_device->node);
190                                 free(loop_device);
191                                 break;
192                         }
193                 }
194         }
195
196         /* handle the devices we are allowed to, excluding the "last" type devices */
197         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
198                 int found = 0;
199                 for (i = 0; last_list[i] != NULL; i++) {
200                         if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
201                                 found = 1;
202                                 break;
203                         }
204                 }
205                 if (found)
206                         continue;
207
208                 add_device(loop_device->path, loop_device->subsys);
209                 list_del(&loop_device->node);
210                 free(loop_device);
211         }
212
213         /* handle the rest of the devices left over, if any */
214         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
215                 add_device(loop_device->path, loop_device->subsys);
216                 list_del(&loop_device->node);
217                 free(loop_device);
218         }
219 }
220
221 static int has_devt(const char *directory)
222 {
223         char filename[PATH_SIZE];
224         struct stat statbuf;
225
226         snprintf(filename, sizeof(filename), "%s/dev", directory);
227         filename[sizeof(filename)-1] = '\0';
228
229         if (stat(filename, &statbuf) == 0)
230                 return 1;
231
232         return 0;
233 }
234
235 static void udev_scan_block(void)
236 {
237         char base[PATH_SIZE];
238         DIR *dir;
239         struct dirent *dent;
240         LIST_HEAD(device_list);
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         exec_list(&device_list);
283 }
284
285 static void udev_scan_class(void)
286 {
287         char base[PATH_SIZE];
288         DIR *dir;
289         struct dirent *dent;
290         LIST_HEAD(device_list);
291
292         snprintf(base, sizeof(base), "%s/class", sysfs_path);
293         base[sizeof(base)-1] = '\0';
294
295         dir = opendir(base);
296         if (dir != NULL) {
297                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
298                         char dirname[PATH_SIZE];
299                         DIR *dir2;
300                         struct dirent *dent2;
301
302                         if (dent->d_name[0] == '.')
303                                 continue;
304
305                         snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
306                         dirname[sizeof(dirname)-1] = '\0';
307
308                         dir2 = opendir(dirname);
309                         if (dir2 != NULL) {
310                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
311                                         char dirname2[PATH_SIZE];
312
313                                         if (dent2->d_name[0] == '.')
314                                                 continue;
315
316                                         snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
317                                         dirname2[sizeof(dirname2)-1] = '\0';
318
319                                         if (has_devt(dirname2) || strcmp(dent->d_name, "net") == 0)
320                                                 device_list_insert(dirname2, dent->d_name, &device_list);
321                                 }
322                                 closedir(dir2);
323                         }
324                 }
325                 closedir(dir);
326         }
327         exec_list(&device_list);
328 }
329
330 static void asmlinkage sig_handler(int signum)
331 {
332         switch (signum) {
333                 case SIGALRM:
334                         exit(1);
335                 case SIGINT:
336                 case SIGTERM:
337                         exit(20 + signum);
338         }
339 }
340
341 int main(int argc, char *argv[], char *envp[])
342 {
343         struct sigaction act;
344
345         logging_init("udevstart");
346         udev_init_config();
347         dbg("version %s", UDEV_VERSION);
348
349         udev_run_str = getenv("UDEV_RUN");
350         udev_log_str = getenv("UDEV_LOG");
351
352         /* disable all logging if not explicitely requested */
353         if (udev_log_str == NULL)
354                 udev_log_priority = 0;
355
356         /* set signal handlers */
357         memset(&act, 0x00, sizeof(act));
358         act.sa_handler = (void (*) (int))sig_handler;
359         sigemptyset (&act.sa_mask);
360         act.sa_flags = 0;
361         sigaction(SIGALRM, &act, NULL);
362         sigaction(SIGINT, &act, NULL);
363         sigaction(SIGTERM, &act, NULL);
364
365         /* trigger timeout to prevent hanging processes */
366         alarm(UDEV_ALARM_TIMEOUT);
367
368         udev_rules_init(&rules, 0, 1);
369
370         udev_scan_block();
371         udev_scan_class();
372
373         udev_rules_close(&rules);
374         logging_close();
375         return 0;
376 }