4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
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
10 * Harald Hoyer <harald@redhat.com>
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.
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.
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.
38 #include <sys/types.h>
40 #include "libsysfs/sysfs/libsysfs.h"
41 #include "udev_libc_wrapper.h"
42 #include "udev_sysfs.h"
44 #include "udev_version.h"
46 #include "udev_utils.h"
47 #include "udev_rules.h"
50 static const char *udev_run_str;
51 static const char *udev_log_str;
52 static struct udev_rules rules;
55 void log_message(int priority, const char *format, ...)
59 if (priority > udev_log_priority)
62 va_start(args, format);
63 vsyslog(priority, format, args);
69 struct list_head node;
71 char subsys[NAME_SIZE];
74 /* sort files in lexical order */
75 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
77 struct device *loop_device;
78 struct device *new_device;
80 dbg("insert: '%s'\n", path);
82 list_for_each_entry(loop_device, device_list, node) {
83 if (strcmp(loop_device->path, path) > 0) {
88 new_device = malloc(sizeof(struct device));
89 if (new_device == NULL) {
94 strlcpy(new_device->path, path, sizeof(new_device->path));
95 strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys));
96 list_add_tail(&new_device->node, &loop_device->node);
97 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
101 /* list of devices that we should run last due to any one of a number of reasons */
102 static char *last_list[] = {
103 "/block/dm", /* on here because dm wants to have the block devices around before it */
107 /* list of devices that we should run first due to any one of a number of reasons */
108 static char *first_list[] = {
109 "/class/mem", /* people tend to like their memory devices around first... */
113 static int add_device(const char *path, const char *subsystem)
116 struct sysfs_class_device *class_dev;
119 devpath = &path[strlen(sysfs_path)];
121 /* clear and set environment for next event */
123 setenv("ACTION", "add", 1);
124 setenv("DEVPATH", devpath, 1);
125 setenv("SUBSYSTEM", subsystem, 1);
126 setenv("UDEV_START", "1", 1);
128 setenv("UDEV_LOG", udev_log_str, 1);
130 setenv("UDEV_RUN", udev_run_str, 1);
131 dbg("exec: '%s' (%s)\n", devpath, path);
133 class_dev = sysfs_open_class_device_path(path);
134 if (class_dev == NULL) {
135 dbg("sysfs_open_class_device_path failed");
139 udev_init_device(&udev, devpath, subsystem, "add");
140 udev.devt = get_devt(class_dev);
141 if (!udev.devt && udev.type != DEV_NET) {
142 dbg("sysfs_open_class_device_path failed");
145 udev_rules_get_name(&rules, &udev, class_dev);
146 if (udev.ignore_device) {
147 dbg("device event will be ignored");
150 if (udev.name[0] == '\0') {
151 dbg("device node creation supressed");
155 udev_add_device(&udev, class_dev);
156 if (udev.devname[0] != '\0')
157 setenv("DEVNAME", udev.devname, 1);
159 if (udev_run && !list_empty(&udev.run_list)) {
160 struct name_entry *name_loop;
162 dbg("executing run list");
163 list_for_each_entry(name_loop, &udev.run_list, node)
164 execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
167 sysfs_close_class_device(class_dev);
168 udev_cleanup_device(&udev);
173 static void exec_list(struct list_head *device_list)
175 struct device *loop_device;
176 struct device *tmp_device;
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, loop_device->subsys);
184 list_del(&loop_device->node);
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) {
194 for (i = 0; last_list[i] != NULL; i++) {
195 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
203 add_device(loop_device->path, loop_device->subsys);
204 list_del(&loop_device->node);
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, loop_device->subsys);
211 list_del(&loop_device->node);
216 static int has_devt(const char *directory)
218 char filename[PATH_SIZE];
221 snprintf(filename, sizeof(filename), "%s/dev", directory);
222 filename[sizeof(filename)-1] = '\0';
224 if (stat(filename, &statbuf) == 0)
230 static void udev_scan_block(void)
232 char base[PATH_SIZE];
235 LIST_HEAD(device_list);
237 snprintf(base, sizeof(base), "%s/block", sysfs_path);
238 base[sizeof(base)-1] = '\0';
242 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
243 char dirname[PATH_SIZE];
245 struct dirent *dent2;
247 if (dent->d_name[0] == '.')
250 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
251 dirname[sizeof(dirname)-1] = '\0';
252 if (has_devt(dirname))
253 device_list_insert(dirname, "block", &device_list);
257 /* look for partitions */
258 dir2 = opendir(dirname);
260 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
261 char dirname2[PATH_SIZE];
263 if (dent2->d_name[0] == '.')
266 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
267 dirname2[sizeof(dirname2)-1] = '\0';
269 if (has_devt(dirname2))
270 device_list_insert(dirname2, "block", &device_list);
277 exec_list(&device_list);
280 static void udev_scan_class(void)
282 char base[PATH_SIZE];
285 LIST_HEAD(device_list);
287 snprintf(base, sizeof(base), "%s/class", sysfs_path);
288 base[sizeof(base)-1] = '\0';
292 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
293 char dirname[PATH_SIZE];
295 struct dirent *dent2;
297 if (dent->d_name[0] == '.')
300 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
301 dirname[sizeof(dirname)-1] = '\0';
303 dir2 = opendir(dirname);
305 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
306 char dirname2[PATH_SIZE];
308 if (dent2->d_name[0] == '.')
311 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
312 dirname2[sizeof(dirname2)-1] = '\0';
314 /* pass the net class as it is */
315 if (strcmp(dent->d_name, "net") == 0)
316 device_list_insert(dirname2, "net", &device_list);
317 else if (has_devt(dirname2))
318 device_list_insert(dirname2, dent->d_name, &device_list);
325 exec_list(&device_list);
328 static void asmlinkage sig_handler(int signum)
339 int main(int argc, char *argv[], char *envp[])
341 struct sigaction act;
343 logging_init("udevstart");
345 dbg("version %s", UDEV_VERSION);
347 udev_run_str = getenv("UDEV_RUN");
348 udev_log_str = getenv("UDEV_LOG");
350 /* disable all logging if not explicitely requested */
351 if (udev_log_str == NULL)
352 udev_log_priority = 0;
354 /* set signal handlers */
355 memset(&act, 0x00, sizeof(act));
356 act.sa_handler = (void (*) (int))sig_handler;
357 sigemptyset (&act.sa_mask);
359 sigaction(SIGALRM, &act, NULL);
360 sigaction(SIGINT, &act, NULL);
361 sigaction(SIGTERM, &act, NULL);
363 /* trigger timeout to prevent hanging processes */
364 alarm(UDEV_ALARM_TIMEOUT);
366 udev_rules_init(&rules, 1);
371 udev_rules_close(&rules);