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