X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udevstart.c;h=d9912466ca91f5f5aea9258d0237e7543dab317f;hp=6b032a199969818dc9744a780ed4cb8762a63a64;hb=a72e3f666b897f94a64547ace3af74785d2f6771;hpb=63f61c5cf639953aa38e025485919b0aa1c49b59 diff --git a/udevstart.c b/udevstart.c index 6b032a199..d9912466c 100644 --- a/udevstart.c +++ b/udevstart.c @@ -37,20 +37,34 @@ #include "libsysfs/sysfs/libsysfs.h" #include "udev_libc_wrapper.h" +#include "udev_sysfs.h" #include "udev.h" +#include "udev_version.h" #include "logging.h" -#include "namedev.h" +#include "udev_rules.h" #include "udev_utils.h" #include "list.h" +static const char *udev_run_str; +static const char *udev_log_str; +static struct udev_rules rules; + #ifdef USE_LOG -void log_message(int level, const char *format, ...) +void log_message(int priority, const char *format, ...) { + va_list args; + + if (priority > udev_log_priority) + return; + + va_start(args, format); + vsyslog(priority, format, args); + va_end(args); } #endif struct device { - struct list_head list; + struct list_head node; char path[PATH_SIZE]; char subsys[NAME_SIZE]; }; @@ -63,7 +77,7 @@ static int device_list_insert(const char *path, char *subsystem, struct list_hea dbg("insert: '%s'\n", path); - list_for_each_entry(loop_device, device_list, list) { + list_for_each_entry(loop_device, device_list, node) { if (strcmp(loop_device->path, path) > 0) { break; } @@ -77,7 +91,7 @@ static int device_list_insert(const char *path, char *subsystem, struct list_hea strlcpy(new_device->path, path, sizeof(new_device->path)); strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys)); - list_add_tail(&new_device->list, &loop_device->list); + list_add_tail(&new_device->node, &loop_device->node); dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys); return 0; } @@ -102,27 +116,52 @@ static int add_device(const char *path, const char *subsystem) devpath = &path[strlen(sysfs_path)]; - /* set environment for callouts and dev.d/ */ + /* clear and set environment for next event */ + clearenv(); + setenv("ACTION", "add", 1); setenv("DEVPATH", devpath, 1); setenv("SUBSYSTEM", subsystem, 1); - + setenv("UDEV_START", "1", 1); + if (udev_log_str) + setenv("UDEV_LOG", udev_log_str, 1); + if (udev_run_str) + setenv("UDEV_RUN", udev_run_str, 1); dbg("exec: '%s' (%s)\n", devpath, path); class_dev = sysfs_open_class_device_path(path); if (class_dev == NULL) { - dbg ("sysfs_open_class_device_path failed"); - return -ENODEV; + dbg("sysfs_open_class_device_path failed"); + return -1; } - udev_init_device(&udev, devpath, subsystem); - udev_add_device(&udev, class_dev); + udev_init_device(&udev, devpath, subsystem, "add"); + udev.devt = get_devt(class_dev); + if (!udev.devt && udev.type != DEV_NET) { + dbg("sysfs_open_class_device_path failed"); + return -1; + } + udev_rules_get_name(&rules, &udev, class_dev); + if (udev.ignore_device) { + dbg("device event will be ignored"); + goto exit; + } + if (udev.name[0] == '\0') { + dbg("device node creation supressed"); + goto run; + } - /* run dev.d/ scripts if we created a node or changed a netif name */ - if (udev_dev_d && udev.devname[0] != '\0') { + udev_add_device(&udev, class_dev); + if (udev.devname[0] != '\0') setenv("DEVNAME", udev.devname, 1); - udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX); - } +run: + if (udev_run && !list_empty(&udev.run_list)) { + struct name_entry *name_loop; + dbg("executing run list"); + list_for_each_entry(name_loop, &udev.run_list, node) + execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL); + } +exit: sysfs_close_class_device(class_dev); udev_cleanup_device(&udev); @@ -136,11 +175,11 @@ static void exec_list(struct list_head *device_list) int i; /* handle the "first" type devices first */ - list_for_each_entry_safe(loop_device, tmp_device, device_list, list) { + list_for_each_entry_safe(loop_device, tmp_device, device_list, node) { for (i = 0; first_list[i] != NULL; i++) { if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) { add_device(loop_device->path, loop_device->subsys); - list_del(&loop_device->list); + list_del(&loop_device->node); free(loop_device); break; } @@ -148,7 +187,7 @@ static void exec_list(struct list_head *device_list) } /* handle the devices we are allowed to, excluding the "last" type devices */ - list_for_each_entry_safe(loop_device, tmp_device, device_list, list) { + list_for_each_entry_safe(loop_device, tmp_device, device_list, node) { int found = 0; for (i = 0; last_list[i] != NULL; i++) { if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) { @@ -160,14 +199,14 @@ static void exec_list(struct list_head *device_list) continue; add_device(loop_device->path, loop_device->subsys); - list_del(&loop_device->list); + list_del(&loop_device->node); free(loop_device); } /* handle the rest of the devices left over, if any */ - list_for_each_entry_safe(loop_device, tmp_device, device_list, list) { + list_for_each_entry_safe(loop_device, tmp_device, device_list, node) { add_device(loop_device->path, loop_device->subsys); - list_del(&loop_device->list); + list_del(&loop_device->node); free(loop_device); } } @@ -299,7 +338,16 @@ int main(int argc, char *argv[], char *envp[]) { struct sigaction act; + logging_init("udevstart"); udev_init_config(); + dbg("version %s", UDEV_VERSION); + + udev_run_str = getenv("UDEV_RUN"); + udev_log_str = getenv("UDEV_LOG"); + + /* disable all logging if not explicitely requested */ + if (udev_log_str == NULL) + udev_log_priority = 0; /* set signal handlers */ memset(&act, 0x00, sizeof(act)); @@ -311,16 +359,14 @@ int main(int argc, char *argv[], char *envp[]) sigaction(SIGTERM, &act, NULL); /* trigger timeout to prevent hanging processes */ - alarm(ALARM_TIMEOUT); - - /* set environment for executed programs */ - setenv("ACTION", "add", 1); - setenv("UDEV_START", "1", 1); + alarm(UDEV_ALARM_TIMEOUT); - namedev_init(); + udev_rules_init(&rules, 1); udev_scan_block(); udev_scan_class(); + udev_rules_close(&rules); + logging_close(); return 0; }