4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
7 * Quick and dirty way to populate a /dev with udev if your system
8 * does not have access to a shell. Based originally on a patch
10 * Harald Hoyer <harald@redhat.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation version 2 of the License.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include <sys/types.h>
41 #include "libsysfs/sysfs/libsysfs.h"
42 #include "udev_libc_wrapper.h"
44 #include "udev_version.h"
46 #include "udev_utils.h"
47 #include "udev_rules.h"
50 static const char *udev_run_str;
51 static const char *udev_log_str;
52 static struct udev_rules rules;
55 void log_message(int priority, const char *format, ...)
59 if (priority > udev_log_priority)
62 va_start(args, format);
63 vsyslog(priority, format, args);
69 struct list_head node;
71 char subsys[NAME_SIZE];
74 /* sort files in lexical order */
75 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
77 struct device *loop_device;
78 struct device *new_device;
79 const char *devpath = &path[strlen(sysfs_path)];
81 dbg("insert: '%s'\n", devpath);
83 list_for_each_entry(loop_device, device_list, node) {
84 if (strcmp(loop_device->path, devpath) > 0) {
89 new_device = malloc(sizeof(struct device));
90 if (new_device == NULL) {
95 strlcpy(new_device->path, devpath, sizeof(new_device->path));
96 strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys));
97 list_add_tail(&new_device->node, &loop_device->node);
98 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
102 /* list of devices that we should run last due to any one of a number of reasons */
103 static char *last_list[] = {
104 "/block/dm", /* on here because dm wants to have the block devices around before it */
108 /* list of devices that we should run first due to any one of a number of reasons */
109 static char *first_list[] = {
115 static int add_device(const char *devpath, const char *subsystem)
118 struct sysfs_class_device *class_dev;
119 char path[PATH_SIZE];
121 /* clear and set environment for next event */
123 setenv("ACTION", "add", 1);
124 setenv("DEVPATH", devpath, 1);
125 setenv("SUBSYSTEM", subsystem, 1);
126 setenv("UDEV_START", "1", 1);
128 setenv("UDEV_LOG", udev_log_str, 1);
130 setenv("UDEV_RUN", udev_run_str, 1);
131 dbg("add '%s'", devpath);
133 snprintf(path, sizeof(path), "%s%s", sysfs_path, devpath);
134 path[sizeof(path)-1] = '\0';
135 class_dev = sysfs_open_class_device_path(path);
136 if (class_dev == NULL) {
137 dbg("sysfs_open_class_device_path failed");
141 udev_init_device(&udev, &class_dev->path[strlen(sysfs_path)], subsystem, "add");
142 udev.devt = get_devt(class_dev);
143 if (!udev.devt && udev.type != DEV_NET) {
144 dbg("sysfs_open_class_device_path failed");
147 udev_rules_get_name(&rules, &udev, class_dev);
148 if (udev.ignore_device) {
149 dbg("device event will be ignored");
152 if (udev.name[0] == '\0') {
153 dbg("device node creation supressed");
157 udev_add_device(&udev, class_dev);
159 if (udev_run && !list_empty(&udev.run_list)) {
160 struct name_entry *name_loop;
162 dbg("executing run list");
163 list_for_each_entry(name_loop, &udev.run_list, node) {
164 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
165 pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, "add");
167 run_program(name_loop->name, udev.subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
171 sysfs_close_class_device(class_dev);
172 udev_cleanup_device(&udev);
177 static void exec_list(struct list_head *device_list)
179 struct device *loop_device;
180 struct device *tmp_device;
183 /* handle the "first" type devices first */
184 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
185 for (i = 0; first_list[i] != NULL; i++) {
186 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
187 add_device(loop_device->path, loop_device->subsys);
188 list_del(&loop_device->node);
195 /* handle the devices we are allowed to, excluding the "last" type devices */
196 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
198 for (i = 0; last_list[i] != NULL; i++) {
199 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
207 add_device(loop_device->path, loop_device->subsys);
208 list_del(&loop_device->node);
212 /* handle the rest of the devices left over, if any */
213 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
214 add_device(loop_device->path, loop_device->subsys);
215 list_del(&loop_device->node);
220 static int has_devt(const char *path)
222 char filename[PATH_SIZE];
225 snprintf(filename, sizeof(filename), "%s/dev", path);
226 filename[sizeof(filename)-1] = '\0';
228 if (stat(filename, &statbuf) == 0)
234 static void udev_scan_block(struct list_head *device_list)
236 char base[PATH_SIZE];
240 snprintf(base, sizeof(base), "%s/block", sysfs_path);
241 base[sizeof(base)-1] = '\0';
245 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
246 char dirname[PATH_SIZE];
248 struct dirent *dent2;
250 if (dent->d_name[0] == '.')
253 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
254 dirname[sizeof(dirname)-1] = '\0';
255 if (has_devt(dirname))
256 device_list_insert(dirname, "block", device_list);
260 /* look for partitions */
261 dir2 = opendir(dirname);
263 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
264 char dirname2[PATH_SIZE];
266 if (dent2->d_name[0] == '.')
269 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
270 dirname2[sizeof(dirname2)-1] = '\0';
272 if (has_devt(dirname2))
273 device_list_insert(dirname2, "block", device_list);
282 static void udev_scan_class(struct list_head *device_list)
284 char base[PATH_SIZE];
288 snprintf(base, sizeof(base), "%s/class", sysfs_path);
289 base[sizeof(base)-1] = '\0';
293 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
294 char dirname[PATH_SIZE];
296 struct dirent *dent2;
298 if (dent->d_name[0] == '.')
301 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
302 dirname[sizeof(dirname)-1] = '\0';
304 dir2 = opendir(dirname);
306 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
307 char dirname2[PATH_SIZE];
309 if (dent2->d_name[0] == '.')
312 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
313 dirname2[sizeof(dirname2)-1] = '\0';
315 if (has_devt(dirname2) || strcmp(dent->d_name, "net") == 0)
316 device_list_insert(dirname2, dent->d_name, device_list);
325 static void asmlinkage sig_handler(int signum)
336 int main(int argc, char *argv[], char *envp[])
338 LIST_HEAD(device_list);
339 struct sigaction act;
341 logging_init("udevstart");
343 dbg("version %s", UDEV_VERSION);
345 udev_run_str = getenv("UDEV_RUN");
346 udev_log_str = getenv("UDEV_LOG");
348 /* disable all logging if not explicitely requested */
349 if (udev_log_str == NULL)
350 udev_log_priority = 0;
352 /* set signal handlers */
353 memset(&act, 0x00, sizeof(act));
354 act.sa_handler = (void (*) (int))sig_handler;
355 sigemptyset (&act.sa_mask);
357 sigaction(SIGALRM, &act, NULL);
358 sigaction(SIGINT, &act, NULL);
359 sigaction(SIGTERM, &act, NULL);
361 /* trigger timeout to prevent hanging processes */
362 alarm(UDEV_ALARM_TIMEOUT);
364 udev_rules_init(&rules, 1);
366 udev_scan_class(&device_list);
367 udev_scan_block(&device_list);
368 exec_list(&device_list);
370 udev_rules_close(&rules);