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