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"
41 #include "udev_version.h"
43 #include "udev_rules.h"
44 #include "udev_utils.h"
48 void log_message(int priority, const char *format, ...)
52 if (priority > udev_log_priority)
55 va_start(args, format);
56 vsyslog(priority, format, args);
62 struct list_head node;
64 char subsys[NAME_SIZE];
67 /* sort files in lexical order */
68 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
70 struct device *loop_device;
71 struct device *new_device;
73 dbg("insert: '%s'\n", path);
75 list_for_each_entry(loop_device, device_list, node) {
76 if (strcmp(loop_device->path, path) > 0) {
81 new_device = malloc(sizeof(struct device));
82 if (new_device == NULL) {
87 strlcpy(new_device->path, path, sizeof(new_device->path));
88 strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys));
89 list_add_tail(&new_device->node, &loop_device->node);
90 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
94 /* list of devices that we should run last due to any one of a number of reasons */
95 static char *last_list[] = {
96 "/block/dm", /* on here because dm wants to have the block devices around before it */
100 /* list of devices that we should run first due to any one of a number of reasons */
101 static char *first_list[] = {
102 "/class/mem", /* people tend to like their memory devices around first... */
106 static int add_device(const char *path, const char *subsystem)
109 struct sysfs_class_device *class_dev;
112 devpath = &path[strlen(sysfs_path)];
114 /* set environment for callouts and dev.d/ */
115 setenv("DEVPATH", devpath, 1);
116 setenv("SUBSYSTEM", subsystem, 1);
118 dbg("exec: '%s' (%s)\n", devpath, path);
120 class_dev = sysfs_open_class_device_path(path);
121 if (class_dev == NULL) {
122 dbg ("sysfs_open_class_device_path failed");
126 udev_init_device(&udev, devpath, subsystem);
127 udev_add_device(&udev, class_dev);
129 /* run dev.d/ scripts if we created a node or changed a netif name */
130 if (udev_dev_d && udev.devname[0] != '\0') {
131 setenv("DEVNAME", udev.devname, 1);
132 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
135 sysfs_close_class_device(class_dev);
136 udev_cleanup_device(&udev);
141 static void exec_list(struct list_head *device_list)
143 struct device *loop_device;
144 struct device *tmp_device;
147 /* handle the "first" type devices first */
148 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
149 for (i = 0; first_list[i] != NULL; i++) {
150 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
151 add_device(loop_device->path, loop_device->subsys);
152 list_del(&loop_device->node);
159 /* handle the devices we are allowed to, excluding the "last" type devices */
160 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
162 for (i = 0; last_list[i] != NULL; i++) {
163 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
171 add_device(loop_device->path, loop_device->subsys);
172 list_del(&loop_device->node);
176 /* handle the rest of the devices left over, if any */
177 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
178 add_device(loop_device->path, loop_device->subsys);
179 list_del(&loop_device->node);
184 static int has_devt(const char *directory)
186 char filename[PATH_SIZE];
189 snprintf(filename, sizeof(filename), "%s/dev", directory);
190 filename[sizeof(filename)-1] = '\0';
192 if (stat(filename, &statbuf) == 0)
198 static void udev_scan_block(void)
200 char base[PATH_SIZE];
203 LIST_HEAD(device_list);
205 snprintf(base, sizeof(base), "%s/block", sysfs_path);
206 base[sizeof(base)-1] = '\0';
210 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
211 char dirname[PATH_SIZE];
213 struct dirent *dent2;
215 if (dent->d_name[0] == '.')
218 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
219 dirname[sizeof(dirname)-1] = '\0';
220 if (has_devt(dirname))
221 device_list_insert(dirname, "block", &device_list);
225 /* look for partitions */
226 dir2 = opendir(dirname);
228 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
229 char dirname2[PATH_SIZE];
231 if (dent2->d_name[0] == '.')
234 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
235 dirname2[sizeof(dirname2)-1] = '\0';
237 if (has_devt(dirname2))
238 device_list_insert(dirname2, "block", &device_list);
245 exec_list(&device_list);
248 static void udev_scan_class(void)
250 char base[PATH_SIZE];
253 LIST_HEAD(device_list);
255 snprintf(base, sizeof(base), "%s/class", sysfs_path);
256 base[sizeof(base)-1] = '\0';
260 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
261 char dirname[PATH_SIZE];
263 struct dirent *dent2;
265 if (dent->d_name[0] == '.')
268 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
269 dirname[sizeof(dirname)-1] = '\0';
271 dir2 = opendir(dirname);
273 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
274 char dirname2[PATH_SIZE];
276 if (dent2->d_name[0] == '.')
279 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
280 dirname2[sizeof(dirname2)-1] = '\0';
282 /* pass the net class as it is */
283 if (strcmp(dent->d_name, "net") == 0)
284 device_list_insert(dirname2, "net", &device_list);
285 else if (has_devt(dirname2))
286 device_list_insert(dirname2, dent->d_name, &device_list);
293 exec_list(&device_list);
296 static void asmlinkage sig_handler(int signum)
307 int main(int argc, char *argv[], char *envp[])
309 struct sigaction act;
311 logging_init("udev");
313 /* disable all logging if not explicitely requested */
314 if (getenv("UDEV_LOG") == NULL)
315 udev_log_priority = 0;
316 dbg("version %s", UDEV_VERSION);
318 /* set signal handlers */
319 memset(&act, 0x00, sizeof(act));
320 act.sa_handler = (void (*) (int))sig_handler;
321 sigemptyset (&act.sa_mask);
323 sigaction(SIGALRM, &act, NULL);
324 sigaction(SIGINT, &act, NULL);
325 sigaction(SIGTERM, &act, NULL);
327 /* trigger timeout to prevent hanging processes */
328 alarm(ALARM_TIMEOUT);
330 /* set environment for executed programs */
331 setenv("ACTION", "add", 1);
332 setenv("UDEV_START", "1", 1);