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