]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevstart.c
[PATCH] udev-test.pl: add test for DEVNAME export to RUN environment
[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 <unistd.h>
30 #include <errno.h>
31 #include <ctype.h>
32 #include <dirent.h>
33 #include <signal.h>
34 #include <sys/wait.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37
38 #include "libsysfs/sysfs/libsysfs.h"
39 #include "udev_libc_wrapper.h"
40 #include "udev.h"
41 #include "udev_version.h"
42 #include "logging.h"
43 #include "udev_rules.h"
44 #include "udev_utils.h"
45 #include "list.h"
46
47 #ifdef USE_LOG
48 void log_message(int priority, const char *format, ...)
49 {
50 va_list args;
51
52 if (priority > udev_log_priority)
53 return;
54
55 va_start(args, format);
56 vsyslog(priority, format, args);
57 va_end(args);
58 }
59 #endif
60
61 struct device {
62 struct list_head node;
63 char path[PATH_SIZE];
64 char subsys[NAME_SIZE];
65 };
66
67 /* sort files in lexical order */
68 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
69 {
70 struct device *loop_device;
71 struct device *new_device;
72
73 dbg("insert: '%s'\n", path);
74
75 list_for_each_entry(loop_device, device_list, node) {
76 if (strcmp(loop_device->path, path) > 0) {
77 break;
78 }
79 }
80
81 new_device = malloc(sizeof(struct device));
82 if (new_device == NULL) {
83 dbg("error malloc");
84 return -ENOMEM;
85 }
86
87 strlcpy(new_device->path, path, sizeof(new_device->path));
88 strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys));
89 list_add_tail(&new_device->node, &loop_device->node);
90 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
91 return 0;
92 }
93
94 /* list of devices that we should run last due to any one of a number of reasons */
95 static char *last_list[] = {
96 "/block/dm", /* on here because dm wants to have the block devices around before it */
97 NULL,
98 };
99
100 /* list of devices that we should run first due to any one of a number of reasons */
101 static char *first_list[] = {
102 "/class/mem", /* people tend to like their memory devices around first... */
103 NULL,
104 };
105
106 static int add_device(const char *path, const char *subsystem)
107 {
108 struct udevice udev;
109 struct sysfs_class_device *class_dev;
110 const char *devpath;
111
112 devpath = &path[strlen(sysfs_path)];
113
114 /* set environment for callouts and dev.d/ */
115 setenv("DEVPATH", devpath, 1);
116 setenv("SUBSYSTEM", subsystem, 1);
117
118 dbg("exec: '%s' (%s)\n", devpath, path);
119
120 class_dev = sysfs_open_class_device_path(path);
121 if (class_dev == NULL) {
122 dbg ("sysfs_open_class_device_path failed");
123 return -ENODEV;
124 }
125
126 udev_init_device(&udev, devpath, subsystem, "add");
127 udev_add_device(&udev, class_dev);
128
129 if (udev.devname[0] != '\0')
130 setenv("DEVNAME", udev.devname, 1);
131
132 if (udev_run && !list_empty(&udev.run_list)) {
133 struct name_entry *name_loop;
134
135 dbg("executing run list");
136 list_for_each_entry(name_loop, &udev.run_list, node)
137 execute_command(name_loop->name, udev.subsystem);
138 }
139
140 /* run dev.d/ scripts if we created a node or changed a netif name */
141 if (udev_dev_d && udev.devname[0] != '\0')
142 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
143
144 sysfs_close_class_device(class_dev);
145 udev_cleanup_device(&udev);
146
147 return 0;
148 }
149
150 static void exec_list(struct list_head *device_list)
151 {
152 struct device *loop_device;
153 struct device *tmp_device;
154 int i;
155
156 /* handle the "first" type devices first */
157 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
158 for (i = 0; first_list[i] != NULL; i++) {
159 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
160 add_device(loop_device->path, loop_device->subsys);
161 list_del(&loop_device->node);
162 free(loop_device);
163 break;
164 }
165 }
166 }
167
168 /* handle the devices we are allowed to, excluding the "last" type devices */
169 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
170 int found = 0;
171 for (i = 0; last_list[i] != NULL; i++) {
172 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
173 found = 1;
174 break;
175 }
176 }
177 if (found)
178 continue;
179
180 add_device(loop_device->path, loop_device->subsys);
181 list_del(&loop_device->node);
182 free(loop_device);
183 }
184
185 /* handle the rest of the devices left over, if any */
186 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
187 add_device(loop_device->path, loop_device->subsys);
188 list_del(&loop_device->node);
189 free(loop_device);
190 }
191 }
192
193 static int has_devt(const char *directory)
194 {
195 char filename[PATH_SIZE];
196 struct stat statbuf;
197
198 snprintf(filename, sizeof(filename), "%s/dev", directory);
199 filename[sizeof(filename)-1] = '\0';
200
201 if (stat(filename, &statbuf) == 0)
202 return 1;
203
204 return 0;
205 }
206
207 static void udev_scan_block(void)
208 {
209 char base[PATH_SIZE];
210 DIR *dir;
211 struct dirent *dent;
212 LIST_HEAD(device_list);
213
214 snprintf(base, sizeof(base), "%s/block", sysfs_path);
215 base[sizeof(base)-1] = '\0';
216
217 dir = opendir(base);
218 if (dir != NULL) {
219 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
220 char dirname[PATH_SIZE];
221 DIR *dir2;
222 struct dirent *dent2;
223
224 if (dent->d_name[0] == '.')
225 continue;
226
227 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
228 dirname[sizeof(dirname)-1] = '\0';
229 if (has_devt(dirname))
230 device_list_insert(dirname, "block", &device_list);
231 else
232 continue;
233
234 /* look for partitions */
235 dir2 = opendir(dirname);
236 if (dir2 != NULL) {
237 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
238 char dirname2[PATH_SIZE];
239
240 if (dent2->d_name[0] == '.')
241 continue;
242
243 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
244 dirname2[sizeof(dirname2)-1] = '\0';
245
246 if (has_devt(dirname2))
247 device_list_insert(dirname2, "block", &device_list);
248 }
249 closedir(dir2);
250 }
251 }
252 closedir(dir);
253 }
254 exec_list(&device_list);
255 }
256
257 static void udev_scan_class(void)
258 {
259 char base[PATH_SIZE];
260 DIR *dir;
261 struct dirent *dent;
262 LIST_HEAD(device_list);
263
264 snprintf(base, sizeof(base), "%s/class", sysfs_path);
265 base[sizeof(base)-1] = '\0';
266
267 dir = opendir(base);
268 if (dir != NULL) {
269 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
270 char dirname[PATH_SIZE];
271 DIR *dir2;
272 struct dirent *dent2;
273
274 if (dent->d_name[0] == '.')
275 continue;
276
277 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
278 dirname[sizeof(dirname)-1] = '\0';
279
280 dir2 = opendir(dirname);
281 if (dir2 != NULL) {
282 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
283 char dirname2[PATH_SIZE];
284
285 if (dent2->d_name[0] == '.')
286 continue;
287
288 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
289 dirname2[sizeof(dirname2)-1] = '\0';
290
291 /* pass the net class as it is */
292 if (strcmp(dent->d_name, "net") == 0)
293 device_list_insert(dirname2, "net", &device_list);
294 else if (has_devt(dirname2))
295 device_list_insert(dirname2, dent->d_name, &device_list);
296 }
297 closedir(dir2);
298 }
299 }
300 closedir(dir);
301 }
302 exec_list(&device_list);
303 }
304
305 static void asmlinkage sig_handler(int signum)
306 {
307 switch (signum) {
308 case SIGALRM:
309 exit(1);
310 case SIGINT:
311 case SIGTERM:
312 exit(20 + signum);
313 }
314 }
315
316 int main(int argc, char *argv[], char *envp[])
317 {
318 struct sigaction act;
319
320 logging_init("udev");
321 udev_init_config();
322 /* disable all logging if not explicitely requested */
323 if (getenv("UDEV_LOG") == NULL)
324 udev_log_priority = 0;
325 dbg("version %s", UDEV_VERSION);
326
327 /* set signal handlers */
328 memset(&act, 0x00, sizeof(act));
329 act.sa_handler = (void (*) (int))sig_handler;
330 sigemptyset (&act.sa_mask);
331 act.sa_flags = 0;
332 sigaction(SIGALRM, &act, NULL);
333 sigaction(SIGINT, &act, NULL);
334 sigaction(SIGTERM, &act, NULL);
335
336 /* trigger timeout to prevent hanging processes */
337 alarm(ALARM_TIMEOUT);
338
339 /* set environment for executed programs */
340 setenv("ACTION", "add", 1);
341 setenv("UDEV_START", "1", 1);
342
343 udev_rules_init();
344
345 udev_scan_block();
346 udev_scan_class();
347
348 logging_close();
349 return 0;
350 }