4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
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>
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.
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.
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.
36 #include <sys/types.h>
38 #include "libsysfs/sysfs/libsysfs.h"
39 #include "udev_libc_wrapper.h"
40 #include "udev_sysfs.h"
42 #include "udev_version.h"
44 #include "udev_rules.h"
45 #include "udev_utils.h"
49 void log_message(int priority, const char *format, ...)
53 if (priority > udev_log_priority)
56 va_start(args, format);
57 vsyslog(priority, format, args);
63 struct list_head node;
65 char subsys[NAME_SIZE];
68 /* sort files in lexical order */
69 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
71 struct device *loop_device;
72 struct device *new_device;
74 dbg("insert: '%s'\n", path);
76 list_for_each_entry(loop_device, device_list, node) {
77 if (strcmp(loop_device->path, path) > 0) {
82 new_device = malloc(sizeof(struct device));
83 if (new_device == NULL) {
88 strlcpy(new_device->path, path, sizeof(new_device->path));
89 strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys));
90 list_add_tail(&new_device->node, &loop_device->node);
91 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
95 /* list of devices that we should run last due to any one of a number of reasons */
96 static char *last_list[] = {
97 "/block/dm", /* on here because dm wants to have the block devices around before it */
101 /* list of devices that we should run first due to any one of a number of reasons */
102 static char *first_list[] = {
103 "/class/mem", /* people tend to like their memory devices around first... */
107 static int add_device(const char *path, const char *subsystem)
110 struct sysfs_class_device *class_dev;
113 devpath = &path[strlen(sysfs_path)];
114 setenv("DEVPATH", devpath, 1);
115 setenv("SUBSYSTEM", subsystem, 1);
116 dbg("exec: '%s' (%s)\n", devpath, path);
118 class_dev = sysfs_open_class_device_path(path);
119 if (class_dev == NULL) {
120 dbg ("sysfs_open_class_device_path failed");
124 udev_init_device(&udev, devpath, subsystem, "add");
125 udev.devt = get_devt(class_dev);
127 dbg ("sysfs_open_class_device_path failed");
130 udev_rules_get_name(&udev, class_dev);
131 if (udev.ignore_device) {
132 info("device event will be ignored");
135 if (udev.name[0] == '\0') {
136 info("device node creation supressed");
140 udev_add_device(&udev, class_dev);
141 if (udev.devname[0] != '\0')
142 setenv("DEVNAME", udev.devname, 1);
145 if (udev_run && !list_empty(&udev.run_list)) {
146 struct name_entry *name_loop;
148 dbg("executing run list");
149 list_for_each_entry(name_loop, &udev.run_list, node)
150 execute_command(name_loop->name, udev.subsystem);
154 sysfs_close_class_device(class_dev);
155 udev_cleanup_device(&udev);
160 static void exec_list(struct list_head *device_list)
162 struct device *loop_device;
163 struct device *tmp_device;
166 /* handle the "first" type devices first */
167 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
168 for (i = 0; first_list[i] != NULL; i++) {
169 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
170 add_device(loop_device->path, loop_device->subsys);
171 list_del(&loop_device->node);
178 /* handle the devices we are allowed to, excluding the "last" type devices */
179 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
181 for (i = 0; last_list[i] != NULL; i++) {
182 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
190 add_device(loop_device->path, loop_device->subsys);
191 list_del(&loop_device->node);
195 /* handle the rest of the devices left over, if any */
196 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
197 add_device(loop_device->path, loop_device->subsys);
198 list_del(&loop_device->node);
203 static int has_devt(const char *directory)
205 char filename[PATH_SIZE];
208 snprintf(filename, sizeof(filename), "%s/dev", directory);
209 filename[sizeof(filename)-1] = '\0';
211 if (stat(filename, &statbuf) == 0)
217 static void udev_scan_block(void)
219 char base[PATH_SIZE];
222 LIST_HEAD(device_list);
224 snprintf(base, sizeof(base), "%s/block", sysfs_path);
225 base[sizeof(base)-1] = '\0';
229 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
230 char dirname[PATH_SIZE];
232 struct dirent *dent2;
234 if (dent->d_name[0] == '.')
237 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
238 dirname[sizeof(dirname)-1] = '\0';
239 if (has_devt(dirname))
240 device_list_insert(dirname, "block", &device_list);
244 /* look for partitions */
245 dir2 = opendir(dirname);
247 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
248 char dirname2[PATH_SIZE];
250 if (dent2->d_name[0] == '.')
253 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
254 dirname2[sizeof(dirname2)-1] = '\0';
256 if (has_devt(dirname2))
257 device_list_insert(dirname2, "block", &device_list);
264 exec_list(&device_list);
267 static void udev_scan_class(void)
269 char base[PATH_SIZE];
272 LIST_HEAD(device_list);
274 snprintf(base, sizeof(base), "%s/class", sysfs_path);
275 base[sizeof(base)-1] = '\0';
279 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
280 char dirname[PATH_SIZE];
282 struct dirent *dent2;
284 if (dent->d_name[0] == '.')
287 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
288 dirname[sizeof(dirname)-1] = '\0';
290 dir2 = opendir(dirname);
292 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
293 char dirname2[PATH_SIZE];
295 if (dent2->d_name[0] == '.')
298 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
299 dirname2[sizeof(dirname2)-1] = '\0';
301 /* pass the net class as it is */
302 if (strcmp(dent->d_name, "net") == 0)
303 device_list_insert(dirname2, "net", &device_list);
304 else if (has_devt(dirname2))
305 device_list_insert(dirname2, dent->d_name, &device_list);
312 exec_list(&device_list);
315 static void asmlinkage sig_handler(int signum)
326 int main(int argc, char *argv[], char *envp[])
328 struct sigaction act;
330 logging_init("udev");
332 /* disable all logging if not explicitely requested */
333 if (getenv("UDEV_LOG") == NULL)
334 udev_log_priority = 0;
335 dbg("version %s", UDEV_VERSION);
337 /* set signal handlers */
338 memset(&act, 0x00, sizeof(act));
339 act.sa_handler = (void (*) (int))sig_handler;
340 sigemptyset (&act.sa_mask);
342 sigaction(SIGALRM, &act, NULL);
343 sigaction(SIGINT, &act, NULL);
344 sigaction(SIGTERM, &act, NULL);
346 /* trigger timeout to prevent hanging processes */
347 alarm(ALARM_TIMEOUT);
349 /* set environment for executed programs */
350 setenv("ACTION", "add", 1);
351 setenv("UDEV_START", "1", 1);