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.
33 #include <sys/types.h>
36 #include "libsysfs/sysfs/libsysfs.h"
38 #include "udev_utils.h"
43 #define MAX_PATHLEN 1024
44 #define SYSBLOCK "/sys/block"
45 #define SYSCLASS "/sys/class"
48 struct list_head list;
49 char path[MAX_PATHLEN];
50 char subsys[MAX_PATHLEN];
53 /* sort files in lexical order */
54 static int device_list_insert(char *path, char *subsystem, struct list_head *device_list)
56 struct device *loop_device;
57 struct device *new_device;
59 list_for_each_entry(loop_device, device_list, list) {
60 if (strcmp(loop_device->path, path) > 0) {
65 new_device = malloc(sizeof(struct device));
66 if (new_device == NULL) {
71 strfieldcpy(new_device->path, path);
72 strfieldcpy(new_device->subsys, subsystem);
73 list_add_tail(&new_device->list, &loop_device->list);
74 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
78 /* list of devices that we should run last due to any one of a number of reasons */
79 static char *last_list[] = {
80 "/block/dm", /* on here because dm wants to have the block devices around before it */
84 /* list of devices that we should run first due to any one of a number of reasons */
85 static char *first_list[] = {
86 "/class/mem", /* people tend to like their memory devices around first... */
90 static int add_device(char *devpath, char *subsystem)
93 char path[SYSFS_PATH_MAX];
94 struct sysfs_class_device *class_dev;
96 /* set environment for callouts and dev.d/ */
97 setenv("DEVPATH", devpath, 1);
98 setenv("SUBSYSTEM", subsystem, 1);
100 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath);
101 path[SYSFS_PATH_MAX-1] = '\0';
102 class_dev = sysfs_open_class_device_path(path);
103 if (class_dev == NULL) {
104 dbg ("sysfs_open_class_device_path failed");
108 udev_init_device(&udev, devpath, subsystem);
109 udev_add_device(&udev, class_dev);
111 /* run dev.d/ scripts if we created a node or changed a netif name */
112 if (udev_dev_d && udev.devname[0] != '\0') {
113 setenv("DEVNAME", udev.devname, 1);
114 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
117 sysfs_close_class_device(class_dev);
122 static void exec_list(struct list_head *device_list)
124 struct device *loop_device;
125 struct device *tmp_device;
128 /* handle the "first" type devices first */
129 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
130 for (i=0; first_list[i] != NULL; i++) {
131 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
132 add_device(loop_device->path, loop_device->subsys);
133 list_del(&loop_device->list);
140 /* handle the devices we are allowed to, excluding the "last" type devices */
141 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
143 for (i=0; last_list[i] != NULL; i++) {
144 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
152 add_device(loop_device->path, loop_device->subsys);
153 list_del(&loop_device->list);
157 /* handle the rest of the devices left over, if any */
158 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
159 add_device(loop_device->path, loop_device->subsys);
160 list_del(&loop_device->list);
165 static void udev_scan_block(void)
169 LIST_HEAD(device_list);
171 dir = opendir(SYSBLOCK);
173 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
174 char dirname[MAX_PATHLEN];
176 struct dirent *dent2;
178 if ((strcmp(dent->d_name, ".") == 0) ||
179 (strcmp(dent->d_name, "..") == 0))
182 snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name);
183 dirname[MAX_PATHLEN-1] = '\0';
184 device_list_insert(dirname, "block", &device_list);
186 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name);
187 dir2 = opendir(dirname);
189 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
190 char dirname2[MAX_PATHLEN];
192 struct dirent *dent3;
194 if ((strcmp(dent2->d_name, ".") == 0) ||
195 (strcmp(dent2->d_name, "..") == 0))
198 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
199 dirname2[MAX_PATHLEN-1] = '\0';
201 dir3 = opendir(dirname2);
203 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
204 char filename[MAX_PATHLEN];
206 if (strcmp(dent3->d_name, "dev") == 0) {
207 snprintf(filename, MAX_PATHLEN, "/block/%s/%s",
208 dent->d_name, dent2->d_name);
209 filename[MAX_PATHLEN-1] = '\0';
210 device_list_insert(filename, "block", &device_list);
222 exec_list(&device_list);
225 static void udev_scan_class(void)
229 LIST_HEAD(device_list);
231 dir = opendir(SYSCLASS);
233 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
234 char dirname[MAX_PATHLEN];
236 struct dirent *dent2;
238 if ((strcmp(dent->d_name, ".") == 0) ||
239 (strcmp(dent->d_name, "..") == 0))
242 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name);
243 dirname[MAX_PATHLEN-1] = '\0';
244 dir2 = opendir(dirname);
246 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
247 char dirname2[MAX_PATHLEN];
249 struct dirent *dent3;
251 if ((strcmp(dent2->d_name, ".") == 0) ||
252 (strcmp(dent2->d_name, "..") == 0))
255 /* pass the net class as it is */
256 if (strcmp(dent->d_name, "net") == 0) {
257 snprintf(dirname2, MAX_PATHLEN, "/class/net/%s", dent2->d_name);
258 device_list_insert(dirname2, "net", &device_list);
262 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
263 dirname2[MAX_PATHLEN-1] = '\0';
264 dir3 = opendir(dirname2);
266 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
267 char filename[MAX_PATHLEN];
269 /* pass devices with a "dev" file */
270 if (strcmp(dent3->d_name, "dev") == 0) {
271 snprintf(filename, MAX_PATHLEN, "/class/%s/%s",
272 dent->d_name, dent2->d_name);
273 filename[MAX_PATHLEN-1] = '\0';
274 device_list_insert(filename, dent->d_name, &device_list);
287 exec_list(&device_list);
292 /* set environment for callouts and dev.d/ */
293 setenv("ACTION", "add", 1);
294 setenv("UDEV_START", "1", 1);