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>
42 #define MAX_PATHLEN 1024
43 #define SYSBLOCK "/sys/block"
44 #define SYSCLASS "/sys/class"
47 struct list_head list;
48 char path[MAX_PATHLEN];
49 char subsys[MAX_PATHLEN];
52 /* sort files in lexical order */
53 static int device_list_insert(char *path, char *subsystem, struct list_head *device_list)
55 struct device *loop_device;
56 struct device *new_device;
58 list_for_each_entry(loop_device, device_list, list) {
59 if (strcmp(loop_device->path, path) > 0) {
64 new_device = malloc(sizeof(struct device));
65 if (new_device == NULL) {
70 strfieldcpy(new_device->path, path);
71 strfieldcpy(new_device->subsys, subsystem);
72 list_add_tail(&new_device->list, &loop_device->list);
76 static void udev_exec(const char *path, const char* subsystem)
78 /* Now call __udev_hotplug(). */
79 if (__udev_hotplug("add", path, subsystem)) {
80 dbg("Calling of udev_hotplug failed");
85 /* list of devices that we should run last due to any one of a number of reasons */
86 static char *last_list[] = {
87 "/block/dm", /* on here because dm wants to have the block devices around before it */
91 static void exec_list(struct list_head *device_list)
93 struct device *loop_device;
94 struct device *tmp_device;
96 /* handle the devices we are allowed to, excluding the "last" type devices */
97 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
100 for (i=0; last_list[i] != NULL; i++) {
101 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
109 udev_exec(loop_device->path, loop_device->subsys);
110 list_del(&loop_device->list);
114 /* handle the rest of the devices left over, if any */
115 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
116 udev_exec(loop_device->path, loop_device->subsys);
117 list_del(&loop_device->list);
122 static void udev_scan_block(void)
126 LIST_HEAD(device_list);
128 dir = opendir(SYSBLOCK);
130 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
131 char dirname[MAX_PATHLEN];
133 struct dirent *dent2;
135 if ((strcmp(dent->d_name, ".") == 0) ||
136 (strcmp(dent->d_name, "..") == 0))
139 snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name);
140 dirname[MAX_PATHLEN-1] = '\0';
141 device_list_insert(dirname, "block", &device_list);
143 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name);
144 dir2 = opendir(dirname);
146 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
147 char dirname2[MAX_PATHLEN];
149 struct dirent *dent3;
151 if ((strcmp(dent2->d_name, ".") == 0) ||
152 (strcmp(dent2->d_name, "..") == 0))
155 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
156 dirname2[MAX_PATHLEN-1] = '\0';
158 dir3 = opendir(dirname2);
160 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
161 char filename[MAX_PATHLEN];
163 if (strcmp(dent3->d_name, "dev") == 0) {
164 snprintf(filename, MAX_PATHLEN, "/block/%s/%s",
165 dent->d_name, dent2->d_name);
166 filename[MAX_PATHLEN-1] = '\0';
167 device_list_insert(filename, "block", &device_list);
179 exec_list(&device_list);
182 static void udev_scan_class(void)
186 LIST_HEAD(device_list);
188 dir = opendir(SYSCLASS);
190 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
191 char dirname[MAX_PATHLEN];
193 struct dirent *dent2;
195 if ((strcmp(dent->d_name, ".") == 0) ||
196 (strcmp(dent->d_name, "..") == 0))
199 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name);
200 dirname[MAX_PATHLEN-1] = '\0';
201 dir2 = opendir(dirname);
203 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
204 char dirname2[MAX_PATHLEN-1];
206 struct dirent *dent3;
208 if ((strcmp(dent2->d_name, ".") == 0) ||
209 (strcmp(dent2->d_name, "..") == 0))
212 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
213 dirname2[MAX_PATHLEN-1] = '\0';
215 dir3 = opendir(dirname2);
217 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
218 char filename[MAX_PATHLEN];
220 if (strcmp(dent3->d_name, "dev") == 0) {
221 snprintf(filename, MAX_PATHLEN, "/class/%s/%s",
222 dent->d_name, dent2->d_name);
223 filename[MAX_PATHLEN-1] = '\0';
224 device_list_insert(filename, dent->d_name, &device_list);
236 exec_list(&device_list);