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