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.
34 #include <sys/types.h>
37 #include "libsysfs/sysfs/libsysfs.h"
39 #include "udev_utils.h"
44 #define MAX_PATH_SIZE 512
47 struct list_head list;
48 char path[MAX_PATH_SIZE];
49 char subsys[MAX_PATH_SIZE];
52 /* sort files in lexical order */
53 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
55 struct device *loop_device;
56 struct device *new_device;
58 dbg("insert: '%s'\n", path);
60 list_for_each_entry(loop_device, device_list, list) {
61 if (strcmp(loop_device->path, path) > 0) {
66 new_device = malloc(sizeof(struct device));
67 if (new_device == NULL) {
72 strfieldcpy(new_device->path, path);
73 strfieldcpy(new_device->subsys, subsystem);
74 list_add_tail(&new_device->list, &loop_device->list);
75 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
79 /* list of devices that we should run last due to any one of a number of reasons */
80 static char *last_list[] = {
81 "/block/dm", /* on here because dm wants to have the block devices around before it */
85 /* list of devices that we should run first due to any one of a number of reasons */
86 static char *first_list[] = {
87 "/class/mem", /* people tend to like their memory devices around first... */
91 static int add_device(const char *path, const char *subsystem)
94 struct sysfs_class_device *class_dev;
97 devpath = &path[strlen(sysfs_path)];
99 /* set environment for callouts and dev.d/ */
100 setenv("DEVPATH", devpath, 1);
101 setenv("SUBSYSTEM", subsystem, 1);
103 dbg("exec : '%s' (%s)\n", devpath, path);
105 class_dev = sysfs_open_class_device_path(path);
106 if (class_dev == NULL) {
107 dbg ("sysfs_open_class_device_path failed");
111 udev_init_device(&udev, devpath, subsystem);
112 udev_add_device(&udev, class_dev);
114 /* run dev.d/ scripts if we created a node or changed a netif name */
115 if (udev_dev_d && udev.devname[0] != '\0') {
116 setenv("DEVNAME", udev.devname, 1);
117 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
120 sysfs_close_class_device(class_dev);
125 static void exec_list(struct list_head *device_list)
127 struct device *loop_device;
128 struct device *tmp_device;
131 /* handle the "first" type devices first */
132 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
133 for (i = 0; first_list[i] != NULL; i++) {
134 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
135 add_device(loop_device->path, loop_device->subsys);
136 list_del(&loop_device->list);
143 /* handle the devices we are allowed to, excluding the "last" type devices */
144 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
146 for (i = 0; last_list[i] != NULL; i++) {
147 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
155 add_device(loop_device->path, loop_device->subsys);
156 list_del(&loop_device->list);
160 /* handle the rest of the devices left over, if any */
161 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
162 add_device(loop_device->path, loop_device->subsys);
163 list_del(&loop_device->list);
168 static int has_devt(const char *directory)
170 char filename[MAX_PATH_SIZE];
173 snprintf(filename, MAX_PATH_SIZE, "%s/dev", directory);
174 filename[MAX_PATH_SIZE-1] = '\0';
176 if (stat(filename, &statbuf) == 0)
182 static void udev_scan_block(void)
184 char base[MAX_PATH_SIZE];
187 LIST_HEAD(device_list);
189 snprintf(base, MAX_PATH_SIZE, "%s/block", sysfs_path);
190 base[MAX_PATH_SIZE-1] = '\0';
194 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
195 char dirname[MAX_PATH_SIZE];
197 struct dirent *dent2;
199 if (dent->d_name[0] == '.')
202 snprintf(dirname, MAX_PATH_SIZE, "%s/%s", base, dent->d_name);
203 dirname[MAX_PATH_SIZE-1] = '\0';
204 if (has_devt(dirname))
205 device_list_insert(dirname, "block", &device_list);
209 snprintf(dirname, MAX_PATH_SIZE, "%s/%s", base, dent->d_name);
210 dir2 = opendir(dirname);
212 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
213 char dirname2[MAX_PATH_SIZE];
215 if (dent2->d_name[0] == '.')
218 snprintf(dirname2, MAX_PATH_SIZE, "%s/%s", dirname, dent2->d_name);
219 dirname2[MAX_PATH_SIZE-1] = '\0';
221 if (has_devt(dirname2))
222 device_list_insert(dirname2, "block", &device_list);
230 exec_list(&device_list);
233 static void udev_scan_class(void)
235 char base[MAX_PATH_SIZE];
238 LIST_HEAD(device_list);
240 snprintf(base, MAX_PATH_SIZE, "%s/class", sysfs_path);
241 base[MAX_PATH_SIZE-1] = '\0';
245 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
246 char dirname[MAX_PATH_SIZE];
248 struct dirent *dent2;
250 if (dent->d_name[0] == '.')
253 snprintf(dirname, MAX_PATH_SIZE, "%s/%s", base, dent->d_name);
254 dirname[MAX_PATH_SIZE-1] = '\0';
255 dir2 = opendir(dirname);
257 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
258 char dirname2[MAX_PATH_SIZE];
260 if (dent2->d_name[0] == '.')
263 snprintf(dirname2, MAX_PATH_SIZE, "%s/%s", dirname, dent2->d_name);
264 dirname2[MAX_PATH_SIZE-1] = '\0';
266 /* pass the net class as it is */
267 if (strcmp(dent->d_name, "net") == 0)
268 device_list_insert(dirname2, "net", &device_list);
269 else if (has_devt(dirname2))
270 device_list_insert(dirname2, dent->d_name, &device_list);
278 exec_list(&device_list);
283 /* set environment for callouts and dev.d/ */
284 setenv("ACTION", "add", 1);
285 setenv("UDEV_START", "1", 1);