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