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