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 dbg("device event will be ignored");
135 if (udev.name[0] == '\0') {
136 dbg("device node creation supressed");
140 udev_add_device(&udev, class_dev);
141 if (udev.devname[0] != '\0')
142 setenv("DEVNAME", udev.devname, 1);
144 if (udev_run && !list_empty(&udev.run_list)) {
145 struct name_entry *name_loop;
147 dbg("executing run list");
148 list_for_each_entry(name_loop, &udev.run_list, node)
149 execute_command(name_loop->name, udev.subsystem);
152 sysfs_close_class_device(class_dev);
153 udev_cleanup_device(&udev);
158 static void exec_list(struct list_head *device_list)
160 struct device *loop_device;
161 struct device *tmp_device;
164 /* handle the "first" type devices first */
165 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
166 for (i = 0; first_list[i] != NULL; i++) {
167 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
168 add_device(loop_device->path, loop_device->subsys);
169 list_del(&loop_device->node);
176 /* handle the devices we are allowed to, excluding the "last" type devices */
177 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
179 for (i = 0; last_list[i] != NULL; i++) {
180 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
188 add_device(loop_device->path, loop_device->subsys);
189 list_del(&loop_device->node);
193 /* handle the rest of the devices left over, if any */
194 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
195 add_device(loop_device->path, loop_device->subsys);
196 list_del(&loop_device->node);
201 static int has_devt(const char *directory)
203 char filename[PATH_SIZE];
206 snprintf(filename, sizeof(filename), "%s/dev", directory);
207 filename[sizeof(filename)-1] = '\0';
209 if (stat(filename, &statbuf) == 0)
215 static void udev_scan_block(void)
217 char base[PATH_SIZE];
220 LIST_HEAD(device_list);
222 snprintf(base, sizeof(base), "%s/block", sysfs_path);
223 base[sizeof(base)-1] = '\0';
227 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
228 char dirname[PATH_SIZE];
230 struct dirent *dent2;
232 if (dent->d_name[0] == '.')
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, "block", &device_list);
242 /* look for partitions */
243 dir2 = opendir(dirname);
245 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
246 char dirname2[PATH_SIZE];
248 if (dent2->d_name[0] == '.')
251 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
252 dirname2[sizeof(dirname2)-1] = '\0';
254 if (has_devt(dirname2))
255 device_list_insert(dirname2, "block", &device_list);
262 exec_list(&device_list);
265 static void udev_scan_class(void)
267 char base[PATH_SIZE];
270 LIST_HEAD(device_list);
272 snprintf(base, sizeof(base), "%s/class", sysfs_path);
273 base[sizeof(base)-1] = '\0';
277 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
278 char dirname[PATH_SIZE];
280 struct dirent *dent2;
282 if (dent->d_name[0] == '.')
285 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
286 dirname[sizeof(dirname)-1] = '\0';
288 dir2 = opendir(dirname);
290 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
291 char dirname2[PATH_SIZE];
293 if (dent2->d_name[0] == '.')
296 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
297 dirname2[sizeof(dirname2)-1] = '\0';
299 /* pass the net class as it is */
300 if (strcmp(dent->d_name, "net") == 0)
301 device_list_insert(dirname2, "net", &device_list);
302 else if (has_devt(dirname2))
303 device_list_insert(dirname2, dent->d_name, &device_list);
310 exec_list(&device_list);
313 static void asmlinkage sig_handler(int signum)
324 int main(int argc, char *argv[], char *envp[])
326 struct sigaction act;
328 logging_init("udev");
330 /* disable all logging if not explicitely requested */
331 if (getenv("UDEV_LOG") == NULL)
332 udev_log_priority = 0;
333 dbg("version %s", UDEV_VERSION);
335 /* set signal handlers */
336 memset(&act, 0x00, sizeof(act));
337 act.sa_handler = (void (*) (int))sig_handler;
338 sigemptyset (&act.sa_mask);
340 sigaction(SIGALRM, &act, NULL);
341 sigaction(SIGINT, &act, NULL);
342 sigaction(SIGTERM, &act, NULL);
344 /* trigger timeout to prevent hanging processes */
345 alarm(ALARM_TIMEOUT);
347 /* set environment for executed programs */
348 setenv("ACTION", "add", 1);
349 setenv("UDEV_START", "1", 1);