chiark / gitweb /
usb_id: don't fail on other subsytems than "scsi"
[elogind.git] / udevstart.c
1 /*
2  * udevstart.c
3  *
4  * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
6  *
7  * Quick and dirty way to populate a /dev with udev if your system
8  * does not have access to a shell.  Based originally on a patch
9  * from:
10  *      Harald Hoyer <harald@redhat.com>
11  *
12  *      This program is free software; you can redistribute it and/or modify it
13  *      under the terms of the GNU General Public License as published by the
14  *      Free Software Foundation version 2 of the License.
15  * 
16  *      This program is distributed in the hope that it will be useful, but
17  *      WITHOUT ANY WARRANTY; without even the implied warranty of
18  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *      General Public License for more details.
20  * 
21  *      You should have received a copy of the GNU General Public License along
22  *      with this program; if not, write to the Free Software Foundation, Inc.,
23  *      675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  */
26
27 #include <stdlib.h>
28 #include <stddef.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <ctype.h>
34 #include <dirent.h>
35 #include <signal.h>
36 #include <syslog.h>
37 #include <sys/wait.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40
41 #include "udev.h"
42 #include "udev_rules.h"
43
44 static const char *udev_run_str;
45 static const char *udev_log_str;
46 static struct udev_rules rules;
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 };
66
67 /* sort files in lexical order */
68 static int device_list_insert(const char *path, struct list_head *device_list)
69 {
70         struct device *loop_device;
71         struct device *new_device;
72         const char *devpath = &path[strlen(sysfs_path)];
73
74         dbg("insert: '%s'\n", devpath);
75
76         list_for_each_entry(loop_device, device_list, node) {
77                 if (strcmp(loop_device->path, devpath) > 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, devpath, sizeof(new_device->path));
89         list_add_tail(&new_device->node, &loop_device->node);
90         dbg("add '%s'" , new_device->path);
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",
103         "/class/tty",
104         NULL,
105 };
106
107 static int add_device(const char *devpath)
108 {
109         struct sysfs_device *dev;
110         struct udevice *udev;
111
112         /* clear and set environment for next event */
113         clearenv();
114         setenv("ACTION", "add", 1);
115         setenv("UDEV_START", "1", 1);
116         if (udev_log_str)
117                 setenv("UDEV_LOG", udev_log_str, 1);
118         if (udev_run_str)
119                 setenv("UDEV_RUN", udev_run_str, 1);
120
121         dev = sysfs_device_get(devpath);
122         if (dev == NULL)
123                 return -1;
124
125         udev = udev_device_init();
126         if (udev == NULL)
127                 return -1;
128
129         /* override built-in sysfs device */
130         udev->dev = dev;
131         strcpy(udev->action, "add");
132         udev->devt = udev_device_get_devt(udev);
133
134         if (strcmp(udev->dev->subsystem, "net") != 0) {
135                 udev->devt = udev_device_get_devt(udev);
136                 if (major(udev->devt) == 0)
137                         return -1;
138         }
139
140         dbg("add '%s'", udev->dev->devpath);
141         setenv("DEVPATH", udev->dev->devpath, 1);
142         setenv("SUBSYSTEM", udev->dev->subsystem, 1);
143
144         udev_rules_get_name(&rules, udev);
145         if (udev->ignore_device) {
146                 dbg("device event will be ignored");
147                 goto exit;
148         }
149         if (udev->name[0] == '\0') {
150                 dbg("device node creation supressed");
151                 goto run;
152         }
153
154         udev_add_device(udev);
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                         if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
162                                 pass_env_to_socket(&name_loop->name[strlen("socket:")], udev->dev->devpath, "add");
163                         else
164                                 run_program(name_loop->name, udev->dev->subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
165                 }
166         }
167 exit:
168         udev_device_cleanup(udev);
169
170         return 0;
171 }
172
173 static void exec_list(struct list_head *device_list)
174 {
175         struct device *loop_device;
176         struct device *tmp_device;
177         int i;
178
179         /* handle the "first" type devices first */
180         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
181                 for (i = 0; first_list[i] != NULL; i++) {
182                         if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
183                                 add_device(loop_device->path);
184                                 list_del(&loop_device->node);
185                                 free(loop_device);
186                                 break;
187                         }
188                 }
189         }
190
191         /* handle the devices we are allowed to, excluding the "last" type devices */
192         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
193                 int found = 0;
194                 for (i = 0; last_list[i] != NULL; i++) {
195                         if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
196                                 found = 1;
197                                 break;
198                         }
199                 }
200                 if (found)
201                         continue;
202
203                 add_device(loop_device->path);
204                 list_del(&loop_device->node);
205                 free(loop_device);
206         }
207
208         /* handle the rest of the devices left over, if any */
209         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
210                 add_device(loop_device->path);
211                 list_del(&loop_device->node);
212                 free(loop_device);
213         }
214 }
215
216 static int has_devt(const char *path)
217 {
218         char filename[PATH_SIZE];
219         struct stat statbuf;
220
221         snprintf(filename, sizeof(filename), "%s/dev", path);
222         filename[sizeof(filename)-1] = '\0';
223
224         if (stat(filename, &statbuf) == 0)
225                 return 1;
226
227         return 0;
228 }
229
230 static void udev_scan_block(struct list_head *device_list)
231 {
232         char base[PATH_SIZE];
233         DIR *dir;
234         struct dirent *dent;
235
236         snprintf(base, sizeof(base), "%s/block", sysfs_path);
237         base[sizeof(base)-1] = '\0';
238
239         dir = opendir(base);
240         if (dir != NULL) {
241                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
242                         char dirname[PATH_SIZE];
243                         DIR *dir2;
244                         struct dirent *dent2;
245
246                         if (dent->d_name[0] == '.')
247                                 continue;
248
249                         snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
250                         dirname[sizeof(dirname)-1] = '\0';
251                         if (has_devt(dirname))
252                                 device_list_insert(dirname, device_list);
253                         else
254                                 continue;
255
256                         /* look for partitions */
257                         dir2 = opendir(dirname);
258                         if (dir2 != NULL) {
259                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
260                                         char dirname2[PATH_SIZE];
261
262                                         if (dent2->d_name[0] == '.')
263                                                 continue;
264
265                                         snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
266                                         dirname2[sizeof(dirname2)-1] = '\0';
267
268                                         if (has_devt(dirname2))
269                                                 device_list_insert(dirname2, device_list);
270                                 }
271                                 closedir(dir2);
272                         }
273                 }
274                 closedir(dir);
275         }
276 }
277
278 static void udev_scan_class(struct list_head *device_list)
279 {
280         char base[PATH_SIZE];
281         DIR *dir;
282         struct dirent *dent;
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                                         if (has_devt(dirname2) || strcmp(dent->d_name, "net") == 0)
312                                                 device_list_insert(dirname2, device_list);
313                                 }
314                                 closedir(dir2);
315                         }
316                 }
317                 closedir(dir);
318         }
319 }
320
321 static void asmlinkage sig_handler(int signum)
322 {
323         switch (signum) {
324                 case SIGALRM:
325                         exit(1);
326                 case SIGINT:
327                 case SIGTERM:
328                         exit(20 + signum);
329         }
330 }
331
332 int main(int argc, char *argv[], char *envp[])
333 {
334         LIST_HEAD(device_list);
335         struct sigaction act;
336
337         logging_init("udevstart");
338         udev_config_init();
339         dbg("version %s", UDEV_VERSION);
340
341         udev_run_str = getenv("UDEV_RUN");
342         udev_log_str = getenv("UDEV_LOG");
343
344         /* disable all logging if not explicitely requested */
345         if (udev_log_str == NULL)
346                 udev_log_priority = 0;
347
348         /* set signal handlers */
349         memset(&act, 0x00, sizeof(act));
350         act.sa_handler = (void (*) (int))sig_handler;
351         sigemptyset (&act.sa_mask);
352         act.sa_flags = 0;
353         sigaction(SIGALRM, &act, NULL);
354         sigaction(SIGINT, &act, NULL);
355         sigaction(SIGTERM, &act, NULL);
356
357         /* trigger timeout to prevent hanging processes */
358         alarm(UDEV_ALARM_TIMEOUT);
359
360         sysfs_init();
361         udev_rules_init(&rules, 1);
362
363         udev_scan_class(&device_list);
364         udev_scan_block(&device_list);
365         exec_list(&device_list);
366
367         udev_rules_cleanup(&rules);
368         sysfs_cleanup();
369         logging_close();
370         return 0;
371 }