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.
37 unsigned char logname[LOGNAME_SIZE];
38 void log_message(int level, const char *format, ...)
42 va_start(args, format);
43 vsyslog(level, format, args);
49 #define MAX_PATHLEN 1024
50 #define SYSBLOCK "/sys/block"
51 #define SYSCLASS "/sys/class"
52 #define UDEV_BIN "/sbin/udev"
54 static void udev_exec(const char *path, const char* subsystem)
57 char action[] = "ACTION=add";
58 char devpath[MAX_PATHLEN];
59 char nosleep[] = "UDEV_NO_SLEEP=1";
60 char *env[] = { action, devpath, nosleep, NULL };
62 snprintf(devpath, MAX_PATHLEN, "DEVPATH=%s", path);
63 devpath[MAX_PATHLEN-1] = '\0';
69 execle(UDEV_BIN, "udev", subsystem, NULL, env);
70 dbg("exec of child failed");
74 dbg("fork of child failed");
81 static void udev_scan(void)
88 dir = opendir(SYSBLOCK);
90 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
91 char dirname[MAX_PATHLEN];
95 if ((strcmp(dent->d_name, ".") == 0) ||
96 (strcmp(dent->d_name, "..") == 0))
99 snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name);
100 dirname[MAX_PATHLEN-1] = '\0';
101 udev_exec(dirname, "block");
103 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name);
104 dir2 = opendir(dirname);
106 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
107 char dirname2[MAX_PATHLEN];
109 struct dirent *dent3;
111 if ((strcmp(dent2->d_name, ".") == 0) ||
112 (strcmp(dent2->d_name, "..") == 0))
115 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
116 dirname2[MAX_PATHLEN-1] = '\0';
118 dir3 = opendir(dirname2);
120 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
121 char filename[MAX_PATHLEN];
123 if (strcmp(dent3->d_name, "dev") == 0) {
124 snprintf(filename, MAX_PATHLEN, "/block/%s/%s",
125 dent->d_name, dent2->d_name);
126 filename[MAX_PATHLEN-1] = '\0';
127 udev_exec(filename, "block");
137 dir = opendir(SYSCLASS);
139 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
140 char dirname[MAX_PATHLEN];
142 struct dirent *dent2;
144 if ((strcmp(dent->d_name, ".") == 0) ||
145 (strcmp(dent->d_name, "..") == 0))
148 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name);
149 dirname[MAX_PATHLEN] = '\0';
150 dir2 = opendir(dirname);
152 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
153 char dirname2[MAX_PATHLEN-1];
155 struct dirent *dent3;
157 if ((strcmp(dent2->d_name, ".") == 0) ||
158 (strcmp(dent2->d_name, "..") == 0))
161 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
162 dirname2[MAX_PATHLEN-1] = '\0';
164 dir3 = opendir(dirname2);
166 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
167 char filename[MAX_PATHLEN];
169 if (strcmp(dent3->d_name, "dev") == 0) {
170 snprintf(filename, MAX_PATHLEN, "/class/%s/%s",
171 dent->d_name, dent2->d_name);
172 filename[MAX_PATHLEN-1] = '\0';
173 udev_exec(filename, dent->d_name);
184 int main(int argc, char *argv[], char *envp[])
186 init_logging("udevstart");