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.
32 #include <sys/types.h>
40 unsigned char logname[LOGNAME_SIZE];
41 void log_message(int level, const char *format, ...)
45 va_start(args, format);
46 vsyslog(level, format, args);
52 #define MAX_PATHLEN 1024
53 #define SYSBLOCK "/sys/block"
54 #define SYSCLASS "/sys/class"
55 #define UDEV_BIN "/sbin/udev"
57 static void udev_exec(const char *path, const char* subsystem)
60 char action[] = "ACTION=add";
61 char devpath[MAX_PATHLEN];
62 char nosleep[] = "UDEV_NO_SLEEP=1";
63 char *env[] = { action, devpath, nosleep, NULL };
65 strcpy(devpath, "DEVPATH=");
66 strfieldcat(devpath, path);
72 execle(UDEV_BIN, "udev", subsystem, NULL, env);
73 dbg("exec of child failed");
77 dbg("fork of child failed");
84 static void udev_scan(void)
91 * We want to scan the class tree first as lvm and dm needs to be able
92 * to have access to the char control device when probing the block
96 dir = opendir(SYSCLASS);
98 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
99 char dirname[MAX_PATHLEN];
101 struct dirent *dent2;
103 if ((strcmp(dent->d_name, ".") == 0) ||
104 (strcmp(dent->d_name, "..") == 0))
107 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name);
108 dirname[MAX_PATHLEN] = '\0';
109 dir2 = opendir(dirname);
111 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
112 char dirname2[MAX_PATHLEN-1];
114 struct dirent *dent3;
116 if ((strcmp(dent2->d_name, ".") == 0) ||
117 (strcmp(dent2->d_name, "..") == 0))
120 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
121 dirname2[MAX_PATHLEN-1] = '\0';
123 dir3 = opendir(dirname2);
125 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
126 char filename[MAX_PATHLEN];
128 if (strcmp(dent3->d_name, "dev") == 0) {
129 snprintf(filename, MAX_PATHLEN, "/class/%s/%s",
130 dent->d_name, dent2->d_name);
131 filename[MAX_PATHLEN-1] = '\0';
132 udev_exec(filename, dent->d_name);
145 dir = opendir(SYSBLOCK);
147 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
148 char dirname[MAX_PATHLEN];
150 struct dirent *dent2;
152 if ((strcmp(dent->d_name, ".") == 0) ||
153 (strcmp(dent->d_name, "..") == 0))
156 snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name);
157 dirname[MAX_PATHLEN-1] = '\0';
158 udev_exec(dirname, "block");
160 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name);
161 dir2 = opendir(dirname);
163 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
164 char dirname2[MAX_PATHLEN];
166 struct dirent *dent3;
168 if ((strcmp(dent2->d_name, ".") == 0) ||
169 (strcmp(dent2->d_name, "..") == 0))
172 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
173 dirname2[MAX_PATHLEN-1] = '\0';
175 dir3 = opendir(dirname2);
177 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
178 char filename[MAX_PATHLEN];
180 if (strcmp(dent3->d_name, "dev") == 0) {
181 snprintf(filename, MAX_PATHLEN, "/block/%s/%s",
182 dent->d_name, dent2->d_name);
183 filename[MAX_PATHLEN-1] = '\0';
184 udev_exec(filename, "block");
197 int main(int argc, char *argv[], char *envp[])
199 init_logging("udevstart");