X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udev.c;h=5ad7e425e6fc212433148a52b16489aca28afc4d;hp=5aa9b43d43134d1092c38036e526d4079baa99c7;hb=7757db1f859616171693ed9a54d1d16d3d5ed8e9;hpb=8b16416dce3d5feab0827668e1184301a19074a8 diff --git a/udev.c b/udev.c index 5aa9b43d4..5ad7e425e 100644 --- a/udev.c +++ b/udev.c @@ -3,8 +3,8 @@ * * Userspace devfs * - * Copyright (C) 2003 Greg Kroah-Hartman - * + * Copyright (C) 2003,2004 Greg Kroah-Hartman + * Copyright (C) 2004 Kay Sievers * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -21,278 +21,217 @@ * */ +#include +#include #include #include -#include #include -#include +#include #include +#include +#include +#include "libsysfs/sysfs/libsysfs.h" #include "udev.h" +#include "udev_utils.h" +#include "udev_sysfs.h" #include "udev_version.h" +#include "namedev.h" +#include "logging.h" -static char *get_action(void) -{ - char *action; - - action = getenv("ACTION"); - return action; -} - - -static char *get_device(void) +#ifdef LOG +unsigned char logname[LOGNAME_SIZE]; +void log_message(int level, const char *format, ...) { - static char device[255]; - char *temp; + va_list args; - temp = getenv("DEVPATH"); - if (temp == NULL) - return NULL; - strcpy(device, SYSFS_ROOT); - strcat(device, temp); + if (!udev_log) + return; - return device; + va_start(args, format); + vsyslog(level, format, args); + va_end(args); } +#endif -/* - * Right now the major/minor of a device is stored in a file called - * "dev" in sysfs. - * The number is stored as: - * MMmm - * MM is the major - * mm is the minor - * The value is in hex. - * Yes, this will probably change when we go to a bigger major/minor - * range, and will have to be changed at that time. - */ -static int get_major_minor (char *dev, int *major, int *minor) -{ - char filename[255]; - char line[20]; - char temp[3]; +/* (for now) true if udevsend is the helper */ +static int manage_hotplug_event(void) { + char helper[256]; int fd; - int retval = 0; - - /* add the dev file to the directory and see if it's present */ - strncpy(filename, dev, sizeof(filename)); - strncat(filename, DEV_FILE, sizeof(filename)); - fd = open(filename, O_RDONLY); - if (fd < 0) { - dbg("Can't open %s", filename); - return -ENODEV; - } + int len; - /* get the major/minor */ - retval = read(fd, line, sizeof(line)); - if (retval < 0) { - dbg("read error on %s", dev); + fd = open("/proc/sys/kernel/hotplug", O_RDONLY); + if (fd < 0) goto exit; - } - temp[0] = line[0]; - temp[1] = line[1]; - temp[2] = 0x00; - *major = (int)strtol(&temp[0], NULL, 16); - - temp[0] = line[2]; - temp[1] = line[3]; - temp[2] = 0x00; - *minor = (int)strtol(&temp[0], NULL, 16); - - dbg("found major = %d, minor = %d", *major, *minor); - - retval = 0; -exit: + len = read(fd, helper, 256); close(fd); - return retval; -} - -/* - * Here would go a call to the naming deamon, to get the name we want to have - * for this device. But for now, let's just default to whatever the kernel is - * calling the device as that will keep the "old-style" naming policy - */ -static char *get_name(char *dev, int major, int minor) -{ - static char name[100]; - char *temp; - - temp = strrchr(dev, '/'); - if (temp == NULL) - return NULL; - strncpy(name, &temp[1], sizeof(name)); - - dbg("name is %s", name); - - return &name[0]; -} -/* - * Again, this will live in the naming deamon - */ -static int get_mode(char *name, char *dev, int major, int minor) -{ - /* just default everyone to rw for the world! */ - return 0666; -} - -/* - * We also want to add some permissions here, and possibly some symlinks - */ -static int create_node(char *name, char type, int major, int minor, int mode) -{ - char *argv[7]; - char mode_string[100]; - char type_string[3]; - char major_string[20]; - char minor_string[20]; - char filename[255]; - int retval = 0; - - strncpy(filename, UDEV_ROOT, sizeof(filename)); - strncat(filename, name, sizeof(filename)); - - snprintf(mode_string, sizeof(mode_string), "--mode=%#o", mode); - snprintf(type_string, sizeof(type_string), "%c", type); - snprintf(major_string, sizeof(major_string), "%d", major); - snprintf(minor_string, sizeof(minor_string), "%d", minor); - - argv[0] = MKNOD; - argv[1] = mode_string; - argv[2] = filename; - argv[3] = type_string; - argv[4] = major_string; - argv[5] = minor_string; - argv[6] = NULL; - dbg ("executing %s %s %s %s %s %s", - argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]); - switch (fork()) { - case 0: - /* we are the child, so lets run the program */ - execv (MKNOD, argv); - exit(0); - break; - case (-1): - dbg ("fork failed."); - retval = -EFAULT; - break; - default: - break; - } - return retval; -} - -/* - * We also want to clean up any symlinks that were created in create_node() - */ -static int delete_node(char *name) -{ - char filename[255]; - - strncpy(filename, UDEV_ROOT, sizeof(filename)); - strncat(filename, name, sizeof(filename)); - - dbg("unlinking %s", filename); - return unlink(filename); -} - -static int add_device(char *device, char type) -{ - char *name; - int major; - int minor; - int mode; - int retval = -EINVAL; - - retval = get_major_minor(device, &major, &minor); - if (retval) { - dbg ("get_major_minor failed"); + if (len < 0) goto exit; - } + helper[len] = '\0'; - name = get_name(device, major, minor); - if (name == NULL) { - dbg ("get_name failed"); - retval = -ENODEV; - goto exit; - } - - mode = get_mode(name, device, major, minor); - if (mode < 0) { - dbg ("get_mode failed"); - retval = -EINVAL; - goto exit; - } - - return create_node(name, type, major, minor, mode); + if (strstr(helper, "udevsend")) + return 1; exit: - return retval; + return 0; } -static int remove_device(char *device) +static void asmlinkage sig_handler(int signum) { - char *name; - int retval = 0; - - name = get_name(device, 0, 0); - if (name == NULL) { - dbg ("get_name failed"); - retval = -ENODEV; - goto exit; + switch (signum) { + case SIGALRM: + exit(1); + case SIGINT: + case SIGTERM: + exit(20 + signum); } - - return delete_node(name); - -exit: - return retval; } -int main(int argc, char *argv[]) +int main(int argc, char *argv[], char *envp[]) { - char *subsystem; - char *action; - char *device; - char type; + struct sigaction act; + struct sysfs_class_device *class_dev; + struct sysfs_device *devices_dev; + struct udevice udev; + char path[SYSFS_PATH_MAX]; int retval = -EINVAL; - - if (argc != 2) { - dbg ("unknown number of arguments"); + const char *error; + const char *action = getenv("ACTION"); + const char *devpath = getenv("DEVPATH"); + const char *subsystem = argv[1]; + + dbg("version %s", UDEV_VERSION); + logging_init("udev"); + udev_init_config(); + + /* set signal handlers */ + memset(&act, 0x00, sizeof(act)); + act.sa_handler = (void (*) (int))sig_handler; + sigemptyset (&act.sa_mask); + act.sa_flags = 0; + /* alarm must not restart syscalls*/ + sigaction(SIGALRM, &act, NULL); + sigaction(SIGINT, &act, NULL); + sigaction(SIGTERM, &act, NULL); + + /* trigger timeout to interrupt blocking syscalls */ + alarm(ALARM_TIMEOUT); + + udev_set_values(&udev, devpath, subsystem, action); + + if (strstr(argv[0], "udevstart") || (argv[1] != NULL && strstr(argv[1], "udevstart"))) { + dbg("udevstart"); + + /* disable all logging, as it's much too slow on some facilities */ + udev_log = 0; + + namedev_init(); + retval = udev_start(); goto exit; } - /* for now, the block layer is the only place where block devices are */ - subsystem = argv[1]; - if (strcmp(subsystem, "block") == 0) - type = 'b'; - else - type = 'c'; - - action = get_action(); if (!action) { - dbg ("no action?"); - goto exit; + dbg("no action"); + goto hotplug; } - device = get_device(); - if (!device) { - dbg ("no device?"); - goto exit; + if (!subsystem) { + dbg("no subsystem"); + goto hotplug; } - dbg("looking at %s", device); - if (strcmp(action, "add") == 0) - return add_device(device, type); + if (!devpath) { + dbg("no devpath"); + goto hotplug; + } - if (strcmp(action, "remove") == 0) - return remove_device(device); + /* export logging flag, as called scripts may want to do the same as udev */ + if (udev_log) + setenv("UDEV_LOG", "1", 1); + + if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) { + if (strcmp(action, "add") == 0) { + /* wait for sysfs and possibly add node */ + dbg("udev add"); + + /* skip blacklisted subsystems */ + if (udev.type != 'n' && subsystem_expect_no_dev(udev.subsystem)) { + dbg("don't care about '%s' devices", udev.subsystem); + goto hotplug; + }; + + snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath); + class_dev = wait_class_device_open(path); + if (class_dev == NULL) { + dbg ("open class device failed"); + goto hotplug; + } + dbg("opened class_dev->name='%s'", class_dev->name); + + wait_for_class_device(class_dev, &error); + + /* init rules, permissions */ + namedev_init(); + + /* name, create node, store in db */ + retval = udev_add_device(&udev, class_dev); + + /* run dev.d/ scripts if we created a node or changed a netif name */ + if (udev_dev_d && udev.devname[0] != '\0') { + setenv("DEVNAME", udev.devname, 1); + udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX); + } + + sysfs_close_class_device(class_dev); + } else if (strcmp(action, "remove") == 0) { + /* possibly remove a node */ + dbg("udev remove"); + + /* get node from db, remove db-entry, delete created node */ + retval = udev_remove_device(&udev); + + /* Set the DEVNAME if known */ + if (udev.devname[0] != '\0') { + setenv("DEVNAME", udev.devname, 1); + } + /* run dev.d/ scripts if we're not instructed to ignore the event */ + if (udev_dev_d && udev.devname[0] != '\0') { + setenv("DEVNAME", udev.devname, 1); + udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX); + } + + } + } else if ((strncmp(devpath, "/devices/", 9) == 0)) { + if (strcmp(action, "add") == 0) { + /* wait for sysfs */ + dbg("devices add"); + + snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath); + devices_dev = wait_devices_device_open(path); + if (!devices_dev) { + dbg("devices device unavailable (probably remove has beaten us)"); + goto hotplug; + } + dbg("devices device opened '%s'", path); + + wait_for_devices_device(devices_dev, &error); + + sysfs_close_device(devices_dev); + } else if (strcmp(action, "remove") == 0) { + dbg("devices remove"); + } + } else { + dbg("unhandled"); + } - dbg("Unknown action: %s", action); - return -EINVAL; +hotplug: + if (manage_hotplug_event()) + udev_multiplex_directory(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX); - retval = 0; -exit: +exit: + logging_close(); return retval; } -