chiark / gitweb /
udev: move dev.d/ handling to external helper
[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, "add");
127         udev_add_device(&udev, class_dev);
128
129         if (udev.devname[0] != '\0')
130                 setenv("DEVNAME", udev.devname, 1);
131
132         if (udev_run && !list_empty(&udev.run_list)) {
133                 struct name_entry *name_loop;
134
135                 dbg("executing run list");
136                 list_for_each_entry(name_loop, &udev.run_list, node)
137                         execute_command(name_loop->name, udev.subsystem);
138         }
139
140         sysfs_close_class_device(class_dev);
141         udev_cleanup_device(&udev);
142
143         return 0;
144 }
145
146 static void exec_list(struct list_head *device_list)
147 {
148         struct device *loop_device;
149         struct device *tmp_device;
150         int i;
151
152         /* handle the "first" type devices first */
153         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
154                 for (i = 0; first_list[i] != NULL; i++) {
155                         if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
156                                 add_device(loop_device->path, loop_device->subsys);
157                                 list_del(&loop_device->node);
158                                 free(loop_device);
159                                 break;
160                         }
161                 }
162         }
163
164         /* handle the devices we are allowed to, excluding the "last" type devices */
165         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
166                 int found = 0;
167                 for (i = 0; last_list[i] != NULL; i++) {
168                         if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
169                                 found = 1;
170                                 break;
171                         }
172                 }
173                 if (found)
174                         continue;
175
176                 add_device(loop_device->path, loop_device->subsys);
177                 list_del(&loop_device->node);
178                 free(loop_device);
179         }
180
181         /* handle the rest of the devices left over, if any */
182         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
183                 add_device(loop_device->path, loop_device->subsys);
184                 list_del(&loop_device->node);
185                 free(loop_device);
186         }
187 }
188
189 static int has_devt(const char *directory)
190 {
191         char filename[PATH_SIZE];
192         struct stat statbuf;
193
194         snprintf(filename, sizeof(filename), "%s/dev", directory);
195         filename[sizeof(filename)-1] = '\0';
196
197         if (stat(filename, &statbuf) == 0)
198                 return 1;
199
200         return 0;
201 }
202
203 static void udev_scan_block(void)
204 {
205         char base[PATH_SIZE];
206         DIR *dir;
207         struct dirent *dent;
208         LIST_HEAD(device_list);
209
210         snprintf(base, sizeof(base), "%s/block", sysfs_path);
211         base[sizeof(base)-1] = '\0';
212
213         dir = opendir(base);
214         if (dir != NULL) {
215                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
216                         char dirname[PATH_SIZE];
217                         DIR *dir2;
218                         struct dirent *dent2;
219
220                         if (dent->d_name[0] == '.')
221                                 continue;
222
223                         snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
224                         dirname[sizeof(dirname)-1] = '\0';
225                         if (has_devt(dirname))
226                                 device_list_insert(dirname, "block", &device_list);
227                         else
228                                 continue;
229
230                         /* look for partitions */
231                         dir2 = opendir(dirname);
232                         if (dir2 != NULL) {
233                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
234                                         char dirname2[PATH_SIZE];
235
236                                         if (dent2->d_name[0] == '.')
237                                                 continue;
238
239                                         snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
240                                         dirname2[sizeof(dirname2)-1] = '\0';
241
242                                         if (has_devt(dirname2))
243                                                 device_list_insert(dirname2, "block", &device_list);
244                                 }
245                                 closedir(dir2);
246                         }
247                 }
248                 closedir(dir);
249         }
250         exec_list(&device_list);
251 }
252
253 static void udev_scan_class(void)
254 {
255         char base[PATH_SIZE];
256         DIR *dir;
257         struct dirent *dent;
258         LIST_HEAD(device_list);
259
260         snprintf(base, sizeof(base), "%s/class", sysfs_path);
261         base[sizeof(base)-1] = '\0';
262
263         dir = opendir(base);
264         if (dir != NULL) {
265                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
266                         char dirname[PATH_SIZE];
267                         DIR *dir2;
268                         struct dirent *dent2;
269
270                         if (dent->d_name[0] == '.')
271                                 continue;
272
273                         snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
274                         dirname[sizeof(dirname)-1] = '\0';
275
276                         dir2 = opendir(dirname);
277                         if (dir2 != NULL) {
278                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
279                                         char dirname2[PATH_SIZE];
280
281                                         if (dent2->d_name[0] == '.')
282                                                 continue;
283
284                                         snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
285                                         dirname2[sizeof(dirname2)-1] = '\0';
286
287                                         /* pass the net class as it is */
288                                         if (strcmp(dent->d_name, "net") == 0)
289                                                 device_list_insert(dirname2, "net", &device_list);
290                                         else if (has_devt(dirname2))
291                                                 device_list_insert(dirname2, dent->d_name, &device_list);
292                                 }
293                                 closedir(dir2);
294                         }
295                 }
296                 closedir(dir);
297         }
298         exec_list(&device_list);
299 }
300
301 static void asmlinkage sig_handler(int signum)
302 {
303         switch (signum) {
304                 case SIGALRM:
305                         exit(1);
306                 case SIGINT:
307                 case SIGTERM:
308                         exit(20 + signum);
309         }
310 }
311
312 int main(int argc, char *argv[], char *envp[])
313 {
314         struct sigaction act;
315
316         logging_init("udev");
317         udev_init_config();
318         /* disable all logging if not explicitely requested */
319         if (getenv("UDEV_LOG") == NULL)
320                 udev_log_priority = 0;
321         dbg("version %s", UDEV_VERSION);
322
323         /* set signal handlers */
324         memset(&act, 0x00, sizeof(act));
325         act.sa_handler = (void (*) (int))sig_handler;
326         sigemptyset (&act.sa_mask);
327         act.sa_flags = 0;
328         sigaction(SIGALRM, &act, NULL);
329         sigaction(SIGINT, &act, NULL);
330         sigaction(SIGTERM, &act, NULL);
331
332         /* trigger timeout to prevent hanging processes */
333         alarm(ALARM_TIMEOUT);
334
335         /* set environment for executed programs */
336         setenv("ACTION", "add", 1);
337         setenv("UDEV_START", "1", 1);
338
339         udev_rules_init();
340
341         udev_scan_block();
342         udev_scan_class();
343
344         logging_close();
345         return 0;
346 }