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;
52 void log_message(int priority, const char *format, ...)
56 if (priority > udev_log_priority)
59 va_start(args, format);
60 vsyslog(priority, format, args);
66 struct list_head node;
68 char subsys[NAME_SIZE];
71 /* sort files in lexical order */
72 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
74 struct device *loop_device;
75 struct device *new_device;
77 dbg("insert: '%s'\n", path);
79 list_for_each_entry(loop_device, device_list, node) {
80 if (strcmp(loop_device->path, path) > 0) {
85 new_device = malloc(sizeof(struct device));
86 if (new_device == NULL) {
91 strlcpy(new_device->path, path, sizeof(new_device->path));
92 strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys));
93 list_add_tail(&new_device->node, &loop_device->node);
94 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
98 /* list of devices that we should run last due to any one of a number of reasons */
99 static char *last_list[] = {
100 "/block/dm", /* on here because dm wants to have the block devices around before it */
104 /* list of devices that we should run first due to any one of a number of reasons */
105 static char *first_list[] = {
106 "/class/mem", /* people tend to like their memory devices around first... */
110 static int add_device(const char *path, const char *subsystem)
113 struct sysfs_class_device *class_dev;
116 devpath = &path[strlen(sysfs_path)];
118 /* clear and set environment for next event */
120 setenv("ACTION", "add", 1);
121 setenv("DEVPATH", devpath, 1);
122 setenv("SUBSYSTEM", subsystem, 1);
123 setenv("UDEV_START", "1", 1);
125 setenv("UDEV_LOG", udev_log_str, 1);
127 setenv("UDEV_RUN", udev_run_str, 1);
128 dbg("exec: '%s' (%s)\n", devpath, path);
130 class_dev = sysfs_open_class_device_path(path);
131 if (class_dev == NULL) {
132 dbg("sysfs_open_class_device_path failed");
136 udev_init_device(&udev, devpath, subsystem, "add");
137 udev.devt = get_devt(class_dev);
139 dbg("sysfs_open_class_device_path failed");
142 udev_rules_get_name(&udev, class_dev);
143 if (udev.ignore_device) {
144 dbg("device event will be ignored");
147 if (udev.name[0] == '\0') {
148 dbg("device node creation supressed");
152 udev_add_device(&udev, class_dev);
153 if (udev.devname[0] != '\0')
154 setenv("DEVNAME", udev.devname, 1);
156 if (udev_run && !list_empty(&udev.run_list)) {
157 struct name_entry *name_loop;
159 dbg("executing run list");
160 list_for_each_entry(name_loop, &udev.run_list, node)
161 execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
164 sysfs_close_class_device(class_dev);
165 udev_cleanup_device(&udev);
170 static void exec_list(struct list_head *device_list)
172 struct device *loop_device;
173 struct device *tmp_device;
176 /* handle the "first" type devices first */
177 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
178 for (i = 0; first_list[i] != NULL; i++) {
179 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
180 add_device(loop_device->path, loop_device->subsys);
181 list_del(&loop_device->node);
188 /* handle the devices we are allowed to, excluding the "last" type devices */
189 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
191 for (i = 0; last_list[i] != NULL; i++) {
192 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
200 add_device(loop_device->path, loop_device->subsys);
201 list_del(&loop_device->node);
205 /* handle the rest of the devices left over, if any */
206 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
207 add_device(loop_device->path, loop_device->subsys);
208 list_del(&loop_device->node);
213 static int has_devt(const char *directory)
215 char filename[PATH_SIZE];
218 snprintf(filename, sizeof(filename), "%s/dev", directory);
219 filename[sizeof(filename)-1] = '\0';
221 if (stat(filename, &statbuf) == 0)
227 static void udev_scan_block(void)
229 char base[PATH_SIZE];
232 LIST_HEAD(device_list);
234 snprintf(base, sizeof(base), "%s/block", sysfs_path);
235 base[sizeof(base)-1] = '\0';
239 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
240 char dirname[PATH_SIZE];
242 struct dirent *dent2;
244 if (dent->d_name[0] == '.')
247 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
248 dirname[sizeof(dirname)-1] = '\0';
249 if (has_devt(dirname))
250 device_list_insert(dirname, "block", &device_list);
254 /* look for partitions */
255 dir2 = opendir(dirname);
257 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
258 char dirname2[PATH_SIZE];
260 if (dent2->d_name[0] == '.')
263 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
264 dirname2[sizeof(dirname2)-1] = '\0';
266 if (has_devt(dirname2))
267 device_list_insert(dirname2, "block", &device_list);
274 exec_list(&device_list);
277 static void udev_scan_class(void)
279 char base[PATH_SIZE];
282 LIST_HEAD(device_list);
284 snprintf(base, sizeof(base), "%s/class", sysfs_path);
285 base[sizeof(base)-1] = '\0';
289 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
290 char dirname[PATH_SIZE];
292 struct dirent *dent2;
294 if (dent->d_name[0] == '.')
297 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
298 dirname[sizeof(dirname)-1] = '\0';
300 dir2 = opendir(dirname);
302 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
303 char dirname2[PATH_SIZE];
305 if (dent2->d_name[0] == '.')
308 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
309 dirname2[sizeof(dirname2)-1] = '\0';
311 /* pass the net class as it is */
312 if (strcmp(dent->d_name, "net") == 0)
313 device_list_insert(dirname2, "net", &device_list);
314 else if (has_devt(dirname2))
315 device_list_insert(dirname2, dent->d_name, &device_list);
322 exec_list(&device_list);
325 static void asmlinkage sig_handler(int signum)
336 int main(int argc, char *argv[], char *envp[])
338 struct sigaction act;
340 logging_init("udev");
342 dbg("version %s", UDEV_VERSION);
344 udev_run_str = getenv("UDEV_RUN");
345 udev_log_str = getenv("UDEV_LOG");
347 /* disable all logging if not explicitely requested */
348 if (udev_log_str == NULL)
349 udev_log_priority = 0;
351 /* set signal handlers */
352 memset(&act, 0x00, sizeof(act));
353 act.sa_handler = (void (*) (int))sig_handler;
354 sigemptyset (&act.sa_mask);
356 sigaction(SIGALRM, &act, NULL);
357 sigaction(SIGINT, &act, NULL);
358 sigaction(SIGTERM, &act, NULL);
360 /* trigger timeout to prevent hanging processes */
361 alarm(ALARM_TIMEOUT);