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