X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udevtest.c;h=2854f7558ff3cd8a9c75b5dd2ccce38e10682376;hp=dd8375aa5cfe774814fa3c7872cbec5bd2924dd1;hb=64e6d9dd705f418307fb474a5185d31470f21989;hpb=eb10f97f2816ae09f949060a2bb8c46b8c1788ed diff --git a/udevtest.c b/udevtest.c index dd8375aa5..2854f7558 100644 --- a/udevtest.c +++ b/udevtest.c @@ -1,9 +1,6 @@ /* - * udev.c - * - * Userspace devfs - * - * Copyright (C) 2003,2004 Greg Kroah-Hartman + * Copyright (C) 2003-2004 Greg Kroah-Hartman + * Copyright (C) 2004-2006 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 @@ -16,37 +13,32 @@ * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., - * 675 Mass Ave, Cambridge, MA 02139, USA. + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * */ #include #include #include +#include +#include #include #include #include +#include #include "udev.h" -#include "udev_version.h" -#include "logging.h" -#include "namedev.h" -#include "libsysfs/libsysfs.h" - -/* global variables */ -char **main_argv; -char **main_envp; - -#ifdef LOG -unsigned char logname[42]; -void log_message (int level, const char *format, ...) +#include "udev_rules.h" + + +#ifdef USE_LOG +void log_message (int priority, const char *format, ...) { - va_list args; + va_list args; -// if (!udev_log) -// return; + if (priority > udev_log_priority) + return; - /* FIXME use level... */ va_start(args, format); vprintf(format, args); va_end(args); @@ -55,113 +47,91 @@ void log_message (int level, const char *format, ...) } #endif -static void sig_handler(int signum) +int main(int argc, char *argv[], char *envp[]) { - switch (signum) { - case SIGINT: - case SIGTERM: - exit(20 + signum); - default: - dbg("unhandled signal"); - } -} - -static inline char *get_action(void) -{ - char *action; - - action = getenv("ACTION"); - return action; -} + struct udev_rules rules = {}; + char *devpath = NULL; + struct udevice *udev; + struct sysfs_device *dev; + int i; + int retval; + int rc = 0; -static inline char *get_devpath(void) -{ - char *devpath; + info("version %s", UDEV_VERSION); + udev_config_init(); + if (udev_log_priority < LOG_INFO) + udev_log_priority = LOG_INFO; - devpath = getenv("DEVPATH"); - return devpath; -} + for (i = 1 ; i < argc; i++) { + char *arg = argv[i]; -static inline char *get_seqnum(void) -{ - char *seqnum; + if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) { + printf("Usage: udevtest [--help] \n"); + goto exit; + } else + devpath = arg; + } - seqnum = getenv("SEQNUM"); - return seqnum; -} + if (devpath == NULL) { + fprintf(stderr, "devpath parameter missing\n"); + rc = 1; + goto exit; + } -static char *subsystem_blacklist[] = { - "net", - "scsi_host", - "scsi_device", - "usb_host", - "pci_bus", - "", -}; + sysfs_init(); + udev_rules_init(&rules, 0); -static int udev_hotplug(int argc, char **argv) -{ - char *devpath; - char *subsystem; - int retval = -EINVAL; - int i; - struct sigaction act; + /* remove /sys if given */ + if (strncmp(devpath, sysfs_path, strlen(sysfs_path)) == 0) + devpath = &devpath[strlen(sysfs_path)]; - devpath = argv[1]; - if (!devpath) { - dbg("no devpath?"); + dev = sysfs_device_get(devpath); + if (dev == NULL) { + fprintf(stderr, "unable to open device '%s'\n", devpath); + rc = 2; 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"); + udev = udev_device_init(); + if (udev == NULL) { + fprintf(stderr, "error initializing device\n"); + rc = 3; goto exit; } - /* 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++; - } + /* override built-in sysfs device */ + udev->dev = dev; + strcpy(udev->action, "add"); + udev->devt = udev_device_get_devt(udev); - /* initialize our configuration */ - udev_init_config(); + /* simulate node creation with test flag */ + udev->test_run = 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); + setenv("DEVPATH", udev->dev->devpath, 1); + setenv("SUBSYSTEM", udev->dev->subsystem, 1); + setenv("ACTION", "add", 1); - /* initialize the naming deamon */ - namedev_init(); + printf("This program is for debugging only, it does not create any node,\n" + "or run any program specified by a RUN key. It may show incorrect results,\n" + "if rules match against subsystem specfic kernel event variables.\n" + "\n"); - retval = udev_add_device(devpath, subsystem, 1); + info("looking at device '%s' from subsystem '%s'", udev->dev->devpath, udev->dev->subsystem); + retval = udev_device_event(&rules, udev); + if (retval == 0 && !udev->ignore_device && udev_run) { + struct name_entry *name_loop; -exit: - if (retval > 0) - retval = 0; + list_for_each_entry(name_loop, &udev->run_list, node) { + char program[PATH_SIZE]; - return -retval; -} - -int main(int argc, char **argv, char **envp) -{ - main_argv = argv; - main_envp = envp; - - dbg("version %s", UDEV_VERSION); + strlcpy(program, name_loop->name, sizeof(program)); + udev_rules_apply_format(udev, program, sizeof(program)); + info("run: '%s'", program); + } + } - return udev_hotplug(argc, argv); +exit: + udev_rules_cleanup(&rules); + sysfs_cleanup(); + return rc; } - -