X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udevtest.c;h=4b94442608641b677e07beebf5a878dcdbf55308;hp=9f10cc212539466e5e03edc05016976d90069e24;hb=8b36cc0f179ee35176016ab67ae53078df4110fa;hpb=bb051f66571121e2c9c97422b65aa89a37ec8feb diff --git a/udevtest.c b/udevtest.c index 9f10cc212..4b9444260 100644 --- a/udevtest.c +++ b/udevtest.c @@ -27,174 +27,93 @@ #include #include +#include "libsysfs/sysfs/libsysfs.h" #include "udev.h" +#include "udev_sysfs.h" +#include "udev_utils.h" #include "udev_version.h" -#include "udev_dbus.h" -#include "logging.h" #include "namedev.h" -#include "udevdb.h" -#include "libsysfs/libsysfs.h" +#include "logging.h" -/* global variables */ -char **main_argv; -char **main_envp; #ifdef LOG -unsigned char logname[42]; void log_message (int level, const char *format, ...) { - va_list args; - - if (!udev_log) - return; + va_list args; va_start(args, format); - vsyslog(level, format, args); + vprintf(format, args); va_end(args); + if (format[strlen(format)-1] != '\n') + printf("\n"); } #endif -static void sig_handler(int signum) -{ - switch (signum) { - case SIGINT: - case SIGTERM: - sysbus_disconnect(); - udevdb_exit(); - exit(20 + signum); - default: - dbg("unhandled signal"); - } -} - -static inline char *get_action(void) -{ - char *action; - - action = getenv("ACTION"); - return action; -} - -static inline char *get_devpath(void) -{ - char *devpath; - - devpath = getenv("DEVPATH"); - return devpath; -} - -static inline char *get_seqnum(void) -{ - char *seqnum; - - seqnum = getenv("SEQNUM"); - return seqnum; -} - -static char *subsystem_blacklist[] = { - "net", - "scsi_host", - "scsi_device", - "usb_host", - "pci_bus", - "", -}; - -static int udev_hotplug(int argc, char **argv) +int main(int argc, char *argv[], char *envp[]) { - char *action; + struct sysfs_class_device *class_dev; char *devpath; - char *subsystem; - int retval = -EINVAL; - int i; - struct sigaction act; - - action = get_action(); - if (!action) { - dbg ("no action?"); - goto exit; - } + char path[SYSFS_PATH_MAX]; + char temp[NAME_SIZE]; + struct udevice udev; + char *subsystem = NULL; - devpath = get_devpath(); - if (!devpath) { - dbg ("no devpath?"); - goto exit; - } - dbg("looking at '%s'", devpath); - - /* we only care about class devices and block stuff */ - if (!strstr(devpath, "class") && - !strstr(devpath, "block")) { - dbg("not a block or class device"); - goto exit; - } + info("version %s", UDEV_VERSION); - /* skip blacklisted subsystems */ - subsystem = argv[1]; - i = 0; - while (subsystem_blacklist[i][0] != '\0') { - if (strcmp(subsystem, subsystem_blacklist[i]) == 0) { - dbg("don't care about '%s' devices", subsystem); - goto exit; - } - i++; + if (argc < 2 || argc > 3) { + info("Usage: udevtest [subsystem]"); + return 1; } - /* connect to the system message bus */ - sysbus_connect(); - /* initialize our configuration */ udev_init_config(); - /* initialize udev database */ - retval = udevdb_init(UDEVDB_DEFAULT); - if (retval != 0) { - dbg("unable to initialize database"); - goto exit_sysbus; + /* remove sysfs_path if given */ + if (strncmp(argv[1], sysfs_path, strlen(sysfs_path)) == 0) { + devpath = &argv[1][strlen(sysfs_path)] ; } + else + if (argv[1][0] != '/') { + /* prepend '/' if missing */ + strfieldcpy(temp, "/"); + strfieldcat(temp, argv[1]); + devpath = temp; + } else { + devpath = argv[1]; + } - /* set up a default signal handler for now */ - act.sa_handler = sig_handler; - sigemptyset (&act.sa_mask); - act.sa_flags = SA_RESTART; - sigaction(SIGINT, &act, NULL); - sigaction(SIGTERM, &act, NULL); + info("looking at '%s'", devpath); /* initialize the naming deamon */ namedev_init(); - if (strcmp(action, "add") == 0) - retval = udev_add_device(devpath, subsystem); + if (argc == 3) + subsystem = argv[2]; - else if (strcmp(action, "remove") == 0) - retval = udev_remove_device(devpath, subsystem); + /* fill in values and test_run flag*/ + udev_init_device(&udev, devpath, subsystem); - else { - dbg("unknown action '%s'", action); - retval = -EINVAL; + /* skip subsystems without "dev", but handle net devices */ + if (udev.type != 'n' && subsystem_expect_no_dev(udev.subsystem)) { + info("don't care about '%s' devices", udev.subsystem); + return 2; } - udevdb_exit(); -exit_sysbus: - /* disconnect from the system message bus */ - sysbus_disconnect(); - -exit: - if (retval > 0) - retval = 0; + /* open the device */ + snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath); + class_dev = sysfs_open_class_device_path(path); + if (class_dev == NULL) { + info("sysfs_open_class_device_path failed"); + return 1; + } - return -retval; -} + info("opened class_dev->name='%s'", class_dev->name); -int main(int argc, char **argv, char **envp) -{ - main_argv = argv; - main_envp = envp; + /* simulate node creation with test flag */ + udev.test_run = 1; + udev_add_device(&udev, class_dev); - init_logging("udev"); - dbg("version %s", UDEV_VERSION); + sysfs_close_class_device(class_dev); - return udev_hotplug(argc, argv); + return 0; } - -