]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevstart.c
[PATCH] add permission rule for jogdial device.
[thirdparty/systemd.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 <stddef.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <ctype.h>
31 #include <dirent.h>
32 #include <sys/wait.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include "logging.h"
37 #include "udev_lib.h"
38 #include "list.h"
39
40
41 #ifdef LOG
42 unsigned char logname[LOGNAME_SIZE];
43 void log_message(int level, const char *format, ...)
44 {
45 va_list args;
46
47 va_start(args, format);
48 vsyslog(level, format, args);
49 va_end(args);
50 }
51 #endif
52
53
54 #define MAX_PATHLEN 1024
55 #define SYSBLOCK "/sys/block"
56 #define SYSCLASS "/sys/class"
57 #define UDEV_BIN "/sbin/udev"
58
59 struct device {
60 struct list_head list;
61 char path[MAX_PATHLEN];
62 char subsys[MAX_PATHLEN];
63 };
64
65 /* sort files in lexical order */
66 static int device_list_insert(char *path, char *subsystem, struct list_head *device_list)
67 {
68 struct device *loop_device;
69 struct device *new_device;
70
71 list_for_each_entry(loop_device, device_list, list) {
72 if (strcmp(loop_device->path, path) > 0) {
73 break;
74 }
75 }
76
77 new_device = malloc(sizeof(struct device));
78 if (new_device == NULL) {
79 dbg("error malloc");
80 return -ENOMEM;
81 }
82
83 strfieldcpy(new_device->path, path);
84 strfieldcpy(new_device->subsys, subsystem);
85 list_add_tail(&new_device->list, &loop_device->list);
86 return 0;
87 }
88
89 static void udev_exec(const char *path, const char* subsystem)
90 {
91 pid_t pid;
92 char action[] = "ACTION=add";
93 char devpath[MAX_PATHLEN];
94 char nosleep[] = "UDEV_NO_SLEEP=1";
95 char *env[] = { action, devpath, nosleep, NULL };
96
97 strcpy(devpath, "DEVPATH=");
98 strfieldcat(devpath, path);
99
100 pid = fork();
101 switch (pid) {
102 case 0:
103 /* child */
104 execle(UDEV_BIN, "udev", subsystem, NULL, env);
105 dbg("exec of child failed");
106 exit(1);
107 break;
108 case -1:
109 dbg("fork of child failed");
110 break;
111 default:
112 wait(NULL);
113 }
114 }
115
116 /* list of devices that we should run last due to any one of a number of reasons */
117 static char *last_list[] = {
118 "/block/dm", /* on here because dm wants to have the block devices around before it */
119 NULL,
120 };
121
122 static void exec_list(struct list_head *device_list)
123 {
124 struct device *loop_device;
125 struct device *tmp_device;
126
127 /* handle the devices we are allowed to, excluding the "last" type devices */
128 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
129 int found = 0;
130 int i;
131 for (i=0; last_list[i] != NULL; i++) {
132 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
133 found = 1;
134 break;
135 }
136 }
137 if (found)
138 continue;
139
140 udev_exec(loop_device->path, loop_device->subsys);
141 list_del(&loop_device->list);
142 free(loop_device);
143 }
144
145 /* handle the rest of the devices left over, if any */
146 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
147 udev_exec(loop_device->path, loop_device->subsys);
148 list_del(&loop_device->list);
149 free(loop_device);
150 }
151 }
152
153 static void udev_scan_block(void)
154 {
155 DIR *dir;
156 struct dirent *dent;
157 LIST_HEAD(device_list);
158
159 dir = opendir(SYSBLOCK);
160 if (dir != NULL) {
161 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
162 char dirname[MAX_PATHLEN];
163 DIR *dir2;
164 struct dirent *dent2;
165
166 if ((strcmp(dent->d_name, ".") == 0) ||
167 (strcmp(dent->d_name, "..") == 0))
168 continue;
169
170 snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name);
171 dirname[MAX_PATHLEN-1] = '\0';
172 device_list_insert(dirname, "block", &device_list);
173
174 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name);
175 dir2 = opendir(dirname);
176 if (dir2 != NULL) {
177 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
178 char dirname2[MAX_PATHLEN];
179 DIR *dir3;
180 struct dirent *dent3;
181
182 if ((strcmp(dent2->d_name, ".") == 0) ||
183 (strcmp(dent2->d_name, "..") == 0))
184 continue;
185
186 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
187 dirname2[MAX_PATHLEN-1] = '\0';
188
189 dir3 = opendir(dirname2);
190 if (dir3 != NULL) {
191 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
192 char filename[MAX_PATHLEN];
193
194 if (strcmp(dent3->d_name, "dev") == 0) {
195 snprintf(filename, MAX_PATHLEN, "/block/%s/%s",
196 dent->d_name, dent2->d_name);
197 filename[MAX_PATHLEN-1] = '\0';
198 device_list_insert(filename, "block", &device_list);
199 }
200 }
201 closedir(dir3);
202 }
203 }
204 closedir(dir2);
205 }
206 }
207 closedir(dir);
208 }
209
210 exec_list(&device_list);
211 }
212
213 static void udev_scan_class(void)
214 {
215 DIR *dir;
216 struct dirent *dent;
217 LIST_HEAD(device_list);
218
219 dir = opendir(SYSCLASS);
220 if (dir != NULL) {
221 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
222 char dirname[MAX_PATHLEN];
223 DIR *dir2;
224 struct dirent *dent2;
225
226 if ((strcmp(dent->d_name, ".") == 0) ||
227 (strcmp(dent->d_name, "..") == 0))
228 continue;
229
230 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name);
231 dirname[MAX_PATHLEN] = '\0';
232 dir2 = opendir(dirname);
233 if (dir2 != NULL) {
234 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
235 char dirname2[MAX_PATHLEN-1];
236 DIR *dir3;
237 struct dirent *dent3;
238
239 if ((strcmp(dent2->d_name, ".") == 0) ||
240 (strcmp(dent2->d_name, "..") == 0))
241 continue;
242
243 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
244 dirname2[MAX_PATHLEN-1] = '\0';
245
246 dir3 = opendir(dirname2);
247 if (dir3 != NULL) {
248 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
249 char filename[MAX_PATHLEN];
250
251 if (strcmp(dent3->d_name, "dev") == 0) {
252 snprintf(filename, MAX_PATHLEN, "/class/%s/%s",
253 dent->d_name, dent2->d_name);
254 filename[MAX_PATHLEN-1] = '\0';
255 device_list_insert(filename, dent->d_name, &device_list);
256 }
257 }
258 closedir(dir3);
259 }
260 }
261 closedir(dir2);
262 }
263 }
264 closedir(dir);
265 }
266
267 exec_list(&device_list);
268 }
269
270 int main(int argc, char *argv[], char *envp[])
271 {
272 init_logging("udevstart");
273
274 udev_scan_class();
275 udev_scan_block();
276
277 return 0;
278 }