chiark / gitweb /
rules_generator: add S/390 persistent network support
[elogind.git] / udevstart.c
1 /*
2  * quick and dirty way to populate a /dev directory
3  *
4  * Copyright (C) 2004 Harald Hoyer <harald@redhat.com>
5  * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
6  * Copyright (C) 2004-2006 Kay Sievers <kay@vrfy.org>
7  *
8  *      This program is free software; you can redistribute it and/or modify it
9  *      under the terms of the GNU General Public License as published by the
10  *      Free Software Foundation version 2 of the License.
11  * 
12  *      This program is distributed in the hope that it will be useful, but
13  *      WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *      General Public License for more details.
16  * 
17  *      You should have received a copy of the GNU General Public License along
18  *      with this program; if not, write to the Free Software Foundation, Inc.,
19  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <dirent.h>
31 #include <signal.h>
32 #include <syslog.h>
33 #include <sys/wait.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36
37 #include "udev.h"
38 #include "udev_rules.h"
39 #include "udev_selinux.h"
40
41 static const char *udev_run_str;
42 static const char *udev_log_str;
43 static struct udev_rules rules;
44
45 #ifdef USE_LOG
46 void log_message(int priority, const char *format, ...)
47 {
48         va_list args;
49
50         if (priority > udev_log_priority)
51                 return;
52
53         va_start(args, format);
54         vsyslog(priority, format, args);
55         va_end(args);
56 }
57 #endif
58
59 struct device {
60         struct list_head node;
61         char path[PATH_SIZE];
62 };
63
64 /* sort files in lexical order */
65 static int device_list_insert(const char *path, struct list_head *device_list)
66 {
67         struct device *loop_device;
68         struct device *new_device;
69         const char *devpath = &path[strlen(sysfs_path)];
70
71         dbg("insert: '%s'", devpath);
72
73         list_for_each_entry(loop_device, device_list, node) {
74                 if (strcmp(loop_device->path, devpath) > 0) {
75                         break;
76                 }
77         }
78
79         new_device = malloc(sizeof(struct device));
80         if (new_device == NULL) {
81                 dbg("error malloc");
82                 return -ENOMEM;
83         }
84
85         strlcpy(new_device->path, devpath, sizeof(new_device->path));
86         list_add_tail(&new_device->node, &loop_device->node);
87         dbg("add '%s'" , new_device->path);
88         return 0;
89 }
90
91 /* list of devices that we should run last due to any one of a number of reasons */
92 static char *last_list[] = {
93         "/block/dm",    /* on here because dm wants to have the block devices around before it */
94         NULL,
95 };
96
97 /* list of devices that we should run first due to any one of a number of reasons */
98 static char *first_list[] = {
99         "/class/mem",
100         "/class/tty",
101         NULL,
102 };
103
104 static int add_device(const char *devpath)
105 {
106         struct sysfs_device *dev;
107         struct udevice *udev;
108         int retval = 0;
109
110         /* clear and set environment for next event */
111         clearenv();
112         setenv("ACTION", "add", 1);
113         setenv("UDEV_START", "1", 1);
114         if (udev_log_str)
115                 setenv("UDEV_LOG", udev_log_str, 1);
116         if (udev_run_str)
117                 setenv("UDEV_RUN", udev_run_str, 1);
118
119         dev = sysfs_device_get(devpath);
120         if (dev == NULL)
121                 return -1;
122
123         udev = udev_device_init(NULL);
124         if (udev == NULL)
125                 return -1;
126
127         /* override built-in sysfs device */
128         udev->dev = dev;
129         strcpy(udev->action, "add");
130
131         if (strcmp(udev->dev->subsystem, "net") != 0) {
132                 udev->devt = udev_device_get_devt(udev);
133                 if (major(udev->devt) == 0)
134                         return -1;
135         }
136
137         dbg("add '%s'", udev->dev->devpath);
138         setenv("DEVPATH", udev->dev->devpath, 1);
139         setenv("SUBSYSTEM", udev->dev->subsystem, 1);
140
141         udev_rules_get_name(&rules, udev);
142         if (udev->ignore_device) {
143                 dbg("device event will be ignored");
144                 goto exit;
145         }
146         if (udev->name[0] != '\0')
147                 retval = udev_device_event(&rules, udev);
148         else
149                 info("device node creation supressed");
150
151         if (retval == 0 && udev_run)
152                 udev_rules_run(udev);
153
154 exit:
155         udev_device_cleanup(udev);
156         return 0;
157 }
158
159 static void exec_list(struct list_head *device_list)
160 {
161         struct device *loop_device;
162         struct device *tmp_device;
163         int i;
164
165         /* handle the "first" type devices first */
166         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
167                 for (i = 0; first_list[i] != NULL; i++) {
168                         if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
169                                 add_device(loop_device->path);
170                                 list_del(&loop_device->node);
171                                 free(loop_device);
172                                 break;
173                         }
174                 }
175         }
176
177         /* handle the devices we are allowed to, excluding the "last" type devices */
178         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
179                 int found = 0;
180                 for (i = 0; last_list[i] != NULL; i++) {
181                         if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
182                                 found = 1;
183                                 break;
184                         }
185                 }
186                 if (found)
187                         continue;
188
189                 add_device(loop_device->path);
190                 list_del(&loop_device->node);
191                 free(loop_device);
192         }
193
194         /* handle the rest of the devices left over, if any */
195         list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
196                 add_device(loop_device->path);
197                 list_del(&loop_device->node);
198                 free(loop_device);
199         }
200 }
201
202 static int has_devt(const char *path)
203 {
204         char filename[PATH_SIZE];
205         struct stat statbuf;
206
207         snprintf(filename, sizeof(filename), "%s/dev", path);
208         filename[sizeof(filename)-1] = '\0';
209
210         if (stat(filename, &statbuf) == 0)
211                 return 1;
212
213         return 0;
214 }
215
216 static void udev_scan_block(struct list_head *device_list)
217 {
218         char base[PATH_SIZE];
219         DIR *dir;
220         struct dirent *dent;
221
222         snprintf(base, sizeof(base), "%s/block", sysfs_path);
223         base[sizeof(base)-1] = '\0';
224
225         dir = opendir(base);
226         if (dir != NULL) {
227                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
228                         char dirname[PATH_SIZE];
229                         DIR *dir2;
230                         struct dirent *dent2;
231
232                         if (dent->d_name[0] == '.')
233                                 continue;
234
235                         snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
236                         dirname[sizeof(dirname)-1] = '\0';
237                         if (has_devt(dirname))
238                                 device_list_insert(dirname, device_list);
239                         else
240                                 continue;
241
242                         /* look for partitions */
243                         dir2 = opendir(dirname);
244                         if (dir2 != NULL) {
245                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
246                                         char dirname2[PATH_SIZE];
247
248                                         if (dent2->d_name[0] == '.')
249                                                 continue;
250
251                                         snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
252                                         dirname2[sizeof(dirname2)-1] = '\0';
253
254                                         if (has_devt(dirname2))
255                                                 device_list_insert(dirname2, device_list);
256                                 }
257                                 closedir(dir2);
258                         }
259                 }
260                 closedir(dir);
261         }
262 }
263
264 static void udev_scan_class(struct list_head *device_list)
265 {
266         char base[PATH_SIZE];
267         DIR *dir;
268         struct dirent *dent;
269
270         snprintf(base, sizeof(base), "%s/class", sysfs_path);
271         base[sizeof(base)-1] = '\0';
272
273         dir = opendir(base);
274         if (dir != NULL) {
275                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
276                         char dirname[PATH_SIZE];
277                         DIR *dir2;
278                         struct dirent *dent2;
279
280                         if (dent->d_name[0] == '.')
281                                 continue;
282
283                         snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
284                         dirname[sizeof(dirname)-1] = '\0';
285
286                         dir2 = opendir(dirname);
287                         if (dir2 != NULL) {
288                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
289                                         char dirname2[PATH_SIZE];
290
291                                         if (dent2->d_name[0] == '.')
292                                                 continue;
293
294                                         snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
295                                         dirname2[sizeof(dirname2)-1] = '\0';
296
297                                         if (has_devt(dirname2) || strcmp(dent->d_name, "net") == 0)
298                                                 device_list_insert(dirname2, device_list);
299                                 }
300                                 closedir(dir2);
301                         }
302                 }
303                 closedir(dir);
304         }
305 }
306
307 static void asmlinkage sig_handler(int signum)
308 {
309         switch (signum) {
310                 case SIGALRM:
311                         exit(1);
312                 case SIGINT:
313                 case SIGTERM:
314                         exit(20 + signum);
315         }
316 }
317
318 int main(int argc, char *argv[], char *envp[])
319 {
320         LIST_HEAD(device_list);
321         struct sigaction act;
322
323         logging_init("udevstart");
324         udev_config_init();
325         selinux_init();
326         dbg("version %s", UDEV_VERSION);
327
328         udev_run_str = getenv("UDEV_RUN");
329         udev_log_str = getenv("UDEV_LOG");
330
331         /* disable all logging if not explicitely requested */
332         if (udev_log_str == NULL)
333                 udev_log_priority = 0;
334
335         /* set signal handlers */
336         memset(&act, 0x00, sizeof(act));
337         act.sa_handler = (void (*) (int))sig_handler;
338         sigemptyset (&act.sa_mask);
339         act.sa_flags = 0;
340         sigaction(SIGALRM, &act, NULL);
341         sigaction(SIGINT, &act, NULL);
342         sigaction(SIGTERM, &act, NULL);
343
344         /* trigger timeout to prevent hanging processes */
345         alarm(UDEV_ALARM_TIMEOUT);
346
347         sysfs_init();
348         udev_rules_init(&rules, 1);
349
350         udev_scan_class(&device_list);
351         udev_scan_block(&device_list);
352         exec_list(&device_list);
353
354         udev_rules_cleanup(&rules);
355         sysfs_cleanup();
356         selinux_exit();
357         logging_close();
358         return 0;
359 }