chiark / gitweb /
fix GGC signed pointer warnings and switch volume_id to stdint
[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                         execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
165         }
166 exit:
167         sysfs_close_class_device(class_dev);
168         udev_cleanup_device(&udev);
169
170         return 0;
171 }
172
173 static void exec_list(struct list_head *device_list)
174 {
175         struct device *loop_device;
176         struct device *tmp_device;
177         int i;
178
179         /* handle the "first" type devices first */
180         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
181                 for (i = 0; first_list[i] != NULL; i++) {
182                         if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
183                                 add_device(loop_device->path, loop_device->subsys);
184                                 list_del(&loop_device->node);
185                                 free(loop_device);
186                                 break;
187                         }
188                 }
189         }
190
191         /* handle the devices we are allowed to, excluding the "last" type devices */
192         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
193                 int found = 0;
194                 for (i = 0; last_list[i] != NULL; i++) {
195                         if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
196                                 found = 1;
197                                 break;
198                         }
199                 }
200                 if (found)
201                         continue;
202
203                 add_device(loop_device->path, loop_device->subsys);
204                 list_del(&loop_device->node);
205                 free(loop_device);
206         }
207
208         /* handle the rest of the devices left over, if any */
209         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
210                 add_device(loop_device->path, loop_device->subsys);
211                 list_del(&loop_device->node);
212                 free(loop_device);
213         }
214 }
215
216 static int has_devt(const char *directory)
217 {
218         char filename[PATH_SIZE];
219         struct stat statbuf;
220
221         snprintf(filename, sizeof(filename), "%s/dev", directory);
222         filename[sizeof(filename)-1] = '\0';
223
224         if (stat(filename, &statbuf) == 0)
225                 return 1;
226
227         return 0;
228 }
229
230 static void udev_scan_block(void)
231 {
232         char base[PATH_SIZE];
233         DIR *dir;
234         struct dirent *dent;
235         LIST_HEAD(device_list);
236
237         snprintf(base, sizeof(base), "%s/block", sysfs_path);
238         base[sizeof(base)-1] = '\0';
239
240         dir = opendir(base);
241         if (dir != NULL) {
242                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
243                         char dirname[PATH_SIZE];
244                         DIR *dir2;
245                         struct dirent *dent2;
246
247                         if (dent->d_name[0] == '.')
248                                 continue;
249
250                         snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
251                         dirname[sizeof(dirname)-1] = '\0';
252                         if (has_devt(dirname))
253                                 device_list_insert(dirname, "block", &device_list);
254                         else
255                                 continue;
256
257                         /* look for partitions */
258                         dir2 = opendir(dirname);
259                         if (dir2 != NULL) {
260                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
261                                         char dirname2[PATH_SIZE];
262
263                                         if (dent2->d_name[0] == '.')
264                                                 continue;
265
266                                         snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
267                                         dirname2[sizeof(dirname2)-1] = '\0';
268
269                                         if (has_devt(dirname2))
270                                                 device_list_insert(dirname2, "block", &device_list);
271                                 }
272                                 closedir(dir2);
273                         }
274                 }
275                 closedir(dir);
276         }
277         exec_list(&device_list);
278 }
279
280 static void udev_scan_class(void)
281 {
282         char base[PATH_SIZE];
283         DIR *dir;
284         struct dirent *dent;
285         LIST_HEAD(device_list);
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                                         /* pass the net class as it is */
315                                         if (strcmp(dent->d_name, "net") == 0)
316                                                 device_list_insert(dirname2, "net", &device_list);
317                                         else if (has_devt(dirname2))
318                                                 device_list_insert(dirname2, dent->d_name, &device_list);
319                                 }
320                                 closedir(dir2);
321                         }
322                 }
323                 closedir(dir);
324         }
325         exec_list(&device_list);
326 }
327
328 static void asmlinkage sig_handler(int signum)
329 {
330         switch (signum) {
331                 case SIGALRM:
332                         exit(1);
333                 case SIGINT:
334                 case SIGTERM:
335                         exit(20 + signum);
336         }
337 }
338
339 int main(int argc, char *argv[], char *envp[])
340 {
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_block();
369         udev_scan_class();
370
371         udev_rules_close(&rules);
372         logging_close();
373         return 0;
374 }