chiark / gitweb /
[PATCH] add a test for a minor over 255
[elogind.git] / udevstart.c
1 /*
2  * udevstart.c
3  *
4  * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5  * 
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>
9  *
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.
13  * 
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.
18  * 
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.
22  *
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <dirent.h>
31 #include <sys/wait.h>
32
33 #include "logging.h"
34
35
36 #ifdef LOG
37 unsigned char logname[LOGNAME_SIZE];
38 void log_message(int level, const char *format, ...)
39 {
40         va_list args;
41
42         va_start(args, format);
43         vsyslog(level, format, args);
44         va_end(args);
45 }
46 #endif
47
48
49 #define MAX_PATHLEN             1024
50 #define SYSBLOCK                "/sys/block"
51 #define SYSCLASS                "/sys/class"
52 #define UDEV_BIN                "/sbin/udev"
53
54 static void udev_exec(const char *path, const char* subsystem)
55 {
56         pid_t pid;
57         char action[] = "ACTION=add";
58         char devpath[MAX_PATHLEN];
59         char nosleep[] = "UDEV_NO_SLEEP=1";
60         char *env[] = { action, devpath, nosleep, NULL };
61
62         snprintf(devpath, MAX_PATHLEN, "DEVPATH=%s", path);
63         devpath[MAX_PATHLEN-1] = '\0';
64
65         pid = fork();
66         switch (pid) {
67         case 0:
68                 /* child */
69                 execle(UDEV_BIN, "udev", subsystem, NULL, env);
70                 dbg("exec of child failed");
71                 exit(1);
72                 break;
73         case -1:
74                 dbg("fork of child failed");
75                 break;
76         default:
77                 wait(NULL);
78         }
79 }
80
81 static void udev_scan(void)
82 {
83         char *devpath;
84         DIR *dir;
85         struct dirent *dent;
86
87         devpath = "block";
88         dir = opendir(SYSBLOCK);
89         if (dir != NULL) {
90                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
91                         char dirname[MAX_PATHLEN];
92                         DIR *dir2;
93                         struct dirent *dent2;
94
95                         if ((strcmp(dent->d_name, ".") == 0) ||
96                             (strcmp(dent->d_name, "..") == 0))
97                                 continue;
98
99                         snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name);
100                         dirname[MAX_PATHLEN-1] = '\0';
101                         udev_exec(dirname, "block");
102
103                         snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name);
104                         dir2 = opendir(dirname);
105                         if (dir2 != NULL) {
106                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
107                                         char dirname2[MAX_PATHLEN];
108                                         DIR *dir3;
109                                         struct dirent *dent3;
110
111                                         if ((strcmp(dent2->d_name, ".") == 0) ||
112                                             (strcmp(dent2->d_name, "..") == 0))
113                                                 continue;
114
115                                         snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
116                                         dirname2[MAX_PATHLEN-1] = '\0';
117
118                                         dir3 = opendir(dirname2);
119                                         if (dir3 != NULL) {
120                                                 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
121                                                         char filename[MAX_PATHLEN];
122
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");
128                                                         }
129                                                 }
130                                         }
131                                 }
132                         }
133                 }
134         }
135
136         devpath = "class";
137         dir = opendir(SYSCLASS);
138         if (dir != NULL) {
139                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
140                         char dirname[MAX_PATHLEN];
141                         DIR *dir2;
142                         struct dirent *dent2;
143
144                         if ((strcmp(dent->d_name, ".") == 0) ||
145                             (strcmp(dent->d_name, "..") == 0))
146                                 continue;
147
148                         snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name);
149                         dirname[MAX_PATHLEN] = '\0';
150                         dir2 = opendir(dirname);
151                         if (dir2 != NULL) {
152                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
153                                         char dirname2[MAX_PATHLEN-1];
154                                         DIR *dir3;
155                                         struct dirent *dent3;
156
157                                         if ((strcmp(dent2->d_name, ".") == 0) ||
158                                             (strcmp(dent2->d_name, "..") == 0))
159                                                 continue;
160
161                                         snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
162                                         dirname2[MAX_PATHLEN-1] = '\0';
163
164                                         dir3 = opendir(dirname2);
165                                         if (dir3 != NULL) {
166                                                 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
167                                                         char filename[MAX_PATHLEN];
168
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);
174                                                         }
175                                                 }
176                                         }
177                                 }
178                         }
179                 }
180         }
181 }
182
183
184 int main(int argc, char *argv[], char *envp[])
185 {
186         init_logging("udevstart");
187
188         udev_scan();
189
190         return 0;
191 }