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"
48 static const char *udev_run_str;
49 static const char *udev_log_str;
50 static struct udev_rules rules;
53 void log_message(int priority, const char *format, ...)
57 if (priority > udev_log_priority)
60 va_start(args, format);
61 vsyslog(priority, format, args);
67 struct list_head node;
69 char subsys[NAME_SIZE];
72 /* sort files in lexical order */
73 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
75 struct device *loop_device;
76 struct device *new_device;
78 dbg("insert: '%s'\n", path);
80 list_for_each_entry(loop_device, device_list, node) {
81 if (strcmp(loop_device->path, path) > 0) {
86 new_device = malloc(sizeof(struct device));
87 if (new_device == NULL) {
92 strlcpy(new_device->path, path, sizeof(new_device->path));
93 strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys));
94 list_add_tail(&new_device->node, &loop_device->node);
95 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
99 /* list of devices that we should run last due to any one of a number of reasons */
100 static char *last_list[] = {
101 "/block/dm", /* on here because dm wants to have the block devices around before it */
105 /* list of devices that we should run first due to any one of a number of reasons */
106 static char *first_list[] = {
107 "/class/mem", /* people tend to like their memory devices around first... */
111 static int add_device(const char *path, const char *subsystem)
114 struct sysfs_class_device *class_dev;
117 devpath = &path[strlen(sysfs_path)];
119 /* clear and set environment for next event */
121 setenv("ACTION", "add", 1);
122 setenv("DEVPATH", devpath, 1);
123 setenv("SUBSYSTEM", subsystem, 1);
124 setenv("UDEV_START", "1", 1);
126 setenv("UDEV_LOG", udev_log_str, 1);
128 setenv("UDEV_RUN", udev_run_str, 1);
129 dbg("exec: '%s' (%s)\n", devpath, path);
131 class_dev = sysfs_open_class_device_path(path);
132 if (class_dev == NULL) {
133 dbg("sysfs_open_class_device_path failed");
137 udev_init_device(&udev, devpath, subsystem, "add");
138 udev.devt = get_devt(class_dev);
139 if (!udev.devt && udev.type != DEV_NET) {
140 dbg("sysfs_open_class_device_path failed");
143 udev_rules_get_name(&rules, &udev, class_dev);
144 if (udev.ignore_device) {
145 dbg("device event will be ignored");
148 if (udev.name[0] == '\0') {
149 dbg("device node creation supressed");
153 udev_add_device(&udev, class_dev);
154 if (udev.devname[0] != '\0')
155 setenv("DEVNAME", udev.devname, 1);
157 if (udev_run && !list_empty(&udev.run_list)) {
158 struct name_entry *name_loop;
160 dbg("executing run list");
161 list_for_each_entry(name_loop, &udev.run_list, node)
162 execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
165 sysfs_close_class_device(class_dev);
166 udev_cleanup_device(&udev);
171 static void exec_list(struct list_head *device_list)
173 struct device *loop_device;
174 struct device *tmp_device;
177 /* handle the "first" type devices first */
178 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
179 for (i = 0; first_list[i] != NULL; i++) {
180 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
181 add_device(loop_device->path, loop_device->subsys);
182 list_del(&loop_device->node);
189 /* handle the devices we are allowed to, excluding the "last" type devices */
190 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
192 for (i = 0; last_list[i] != NULL; i++) {
193 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
201 add_device(loop_device->path, loop_device->subsys);
202 list_del(&loop_device->node);
206 /* handle the rest of the devices left over, if any */
207 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
208 add_device(loop_device->path, loop_device->subsys);
209 list_del(&loop_device->node);
214 static int has_devt(const char *directory)
216 char filename[PATH_SIZE];
219 snprintf(filename, sizeof(filename), "%s/dev", directory);
220 filename[sizeof(filename)-1] = '\0';
222 if (stat(filename, &statbuf) == 0)
228 static void udev_scan_block(void)
230 char base[PATH_SIZE];
233 LIST_HEAD(device_list);
235 snprintf(base, sizeof(base), "%s/block", sysfs_path);
236 base[sizeof(base)-1] = '\0';
240 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
241 char dirname[PATH_SIZE];
243 struct dirent *dent2;
245 if (dent->d_name[0] == '.')
248 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
249 dirname[sizeof(dirname)-1] = '\0';
250 if (has_devt(dirname))
251 device_list_insert(dirname, "block", &device_list);
255 /* look for partitions */
256 dir2 = opendir(dirname);
258 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
259 char dirname2[PATH_SIZE];
261 if (dent2->d_name[0] == '.')
264 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
265 dirname2[sizeof(dirname2)-1] = '\0';
267 if (has_devt(dirname2))
268 device_list_insert(dirname2, "block", &device_list);
275 exec_list(&device_list);
278 static void udev_scan_class(void)
280 char base[PATH_SIZE];
283 LIST_HEAD(device_list);
285 snprintf(base, sizeof(base), "%s/class", sysfs_path);
286 base[sizeof(base)-1] = '\0';
290 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
291 char dirname[PATH_SIZE];
293 struct dirent *dent2;
295 if (dent->d_name[0] == '.')
298 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
299 dirname[sizeof(dirname)-1] = '\0';
301 dir2 = opendir(dirname);
303 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
304 char dirname2[PATH_SIZE];
306 if (dent2->d_name[0] == '.')
309 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
310 dirname2[sizeof(dirname2)-1] = '\0';
312 /* pass the net class as it is */
313 if (strcmp(dent->d_name, "net") == 0)
314 device_list_insert(dirname2, "net", &device_list);
315 else if (has_devt(dirname2))
316 device_list_insert(dirname2, dent->d_name, &device_list);
323 exec_list(&device_list);
326 static void asmlinkage sig_handler(int signum)
337 int main(int argc, char *argv[], char *envp[])
339 struct sigaction act;
341 logging_init("udevstart");
343 dbg("version %s", UDEV_VERSION);
345 udev_run_str = getenv("UDEV_RUN");
346 udev_log_str = getenv("UDEV_LOG");
348 /* disable all logging if not explicitely requested */
349 if (udev_log_str == NULL)
350 udev_log_priority = 0;
352 /* set signal handlers */
353 memset(&act, 0x00, sizeof(act));
354 act.sa_handler = (void (*) (int))sig_handler;
355 sigemptyset (&act.sa_mask);
357 sigaction(SIGALRM, &act, NULL);
358 sigaction(SIGINT, &act, NULL);
359 sigaction(SIGTERM, &act, NULL);
361 /* trigger timeout to prevent hanging processes */
362 alarm(UDEV_ALARM_TIMEOUT);
364 udev_rules_init(&rules, 1);
369 udev_rules_close(&rules);