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.
38 unsigned char logname[42];
39 void log_message(int level, const char *format, ...)
43 va_start(args, format);
44 vsyslog(level, format, args);
50 #define MAX_PATHLEN 1024
51 #define SYSBLOCK "/sys/block"
52 #define SYSCLASS "/sys/class"
54 static int execute_udev(char *path, char *value, int len)
76 /* dup write side of pipe to STDOUT */
79 dbg("executing /sbin/udev '%s'", path);
80 retval = execl("/sbin/udev", "/sbin/udev", path, NULL);
82 info("execution of '%s' failed", path);
88 /* parent reads from fds[0] */
92 res = read(fds[0], buffer, sizeof(buffer) - 1);
97 dbg("result len %d too short", len);
101 dbg("result value already set");
105 strncpy(value, buffer, len);
106 pos = value + strlen(value)-1;
109 dbg("result is '%s'", value);
115 dbg("wait failed result %d", res);
119 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
120 dbg("exec program status 0x%x", status);
127 static int udev_scan(void)
132 int retval = -EINVAL;
136 dir = opendir(SYSBLOCK);
138 for (dent = readdir(dir); dent; dent = readdir(dir)) {
139 char dirname[MAX_PATHLEN];
141 struct dirent *dent2;
142 if ((strcmp(dent->d_name, ".") == 0)
143 || (strcmp(dent->d_name, "..") == 0))
146 snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name);
148 setenv("DEVPATH", dirname, 1);
149 dbg("udev block, 'DEVPATH' = '%s'", dirname);
150 execute_udev("block", scratch, sizeof(scratch));
152 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name);
154 dir2 = opendir(dirname);
156 for (dent2 = readdir(dir2); dent2; dent2 = readdir(dir2)) {
157 char dirname2[MAX_PATHLEN];
159 struct dirent *dent3;
161 if ((strcmp(dent2->d_name, ".") == 0) ||
162 (strcmp(dent2->d_name, "..") == 0))
165 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
167 dir3 = opendir(dirname2);
169 for (dent3 = readdir(dir3); dent3; dent3 = readdir(dir3)) {
170 char filename[MAX_PATHLEN];
172 if (strcmp(dent3->d_name, "dev") == 0) {
173 snprintf(filename, MAX_PATHLEN, "/block/%s/%s", dent->d_name, dent2->d_name);
174 setenv("DEVPATH", filename, 1);
175 dbg("udev block, 'DEVPATH' = '%s'", filename);
176 execute_udev("block", scratch, sizeof(scratch));
186 dir = opendir(SYSCLASS);
188 for (dent = readdir(dir); dent; dent = readdir(dir)) {
189 char dirname[MAX_PATHLEN];
191 struct dirent *dent2;
192 if ((strcmp(dent->d_name, ".") == 0)
193 || (strcmp(dent->d_name, "..") == 0))
196 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name);
198 dir2 = opendir(dirname);
200 for (dent2 = readdir(dir2); dent2; dent2 = readdir(dir2)) {
201 char dirname2[MAX_PATHLEN];
203 struct dirent *dent3;
205 if ((strcmp(dent2->d_name, ".") == 0) || (strcmp(dent2->d_name, "..") == 0))
208 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
210 dir3 = opendir(dirname2);
212 for (dent3 = readdir(dir3); dent3; dent3 = readdir(dir3)) {
214 filename[MAX_PATHLEN];
216 if (strcmp(dent3->d_name, "dev") == 0) {
220 "/class/%s/%s", dent->d_name, dent2->d_name);
221 setenv("DEVPATH", filename, 1);
222 dbg("udev '%s', 'DEVPATH' = '%s'", dent->d_name, filename);
223 execute_udev(dent->d_name, scratch, sizeof(scratch));
239 int main(int argc, char **argv, char **envp)
241 init_logging("udevstart");
243 setenv("ACTION", "add", 1);
244 setenv("UDEV_NO_SLEEP", "1", 1);