X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fudev%2Fudevadm-monitor.c;h=e776fb99dcfb14bc061fad69e4e1757bbf32d96d;hb=79008bddf679a5e0900369950eb346c9fa687107;hp=6a202f04ca7ce65cf5fff3777374c1d8119c04cd;hpb=baa30fbc2c04b23209d0b8fb3c86cd15ef9ea81a;p=elogind.git diff --git a/src/udev/udevadm-monitor.c b/src/udev/udevadm-monitor.c index 6a202f04c..e776fb99d 100644 --- a/src/udev/udevadm-monitor.c +++ b/src/udev/udevadm-monitor.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004-2010 Kay Sievers + * Copyright (C) 2004-2010 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 @@ -33,23 +33,22 @@ #include #include "udev.h" +#include "udev-util.h" static bool udev_exit; -static void sig_handler(int signum) -{ +static void sig_handler(int signum) { if (signum == SIGINT || signum == SIGTERM) udev_exit = true; } -static void print_device(struct udev_device *device, const char *source, int prop) -{ +static void print_device(struct udev_device *device, const char *source, int prop) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); - printf("%-6s[%llu.%06u] %-8s %s (%s)\n", + printf("%-6s[%"PRI_TIME".%06ld] %-8s %s (%s)\n", source, - (unsigned long long) ts.tv_sec, (unsigned int) ts.tv_nsec/1000, + ts.tv_sec, ts.tv_nsec/1000, udev_device_get_action(device), udev_device_get_devpath(device), udev_device_get_subsystem(device)); @@ -64,43 +63,47 @@ static void print_device(struct udev_device *device, const char *source, int pro } } -static int adm_monitor(struct udev *udev, int argc, char *argv[]) -{ - struct sigaction act; +static void help(void) { + printf("Usage: udevadm monitor [--property] [--kernel] [--udev] [--help]\n" + " -p,--property print the event properties\n" + " -k,--kernel print kernel uevents\n" + " -u,--udev print udev events\n" + " -s,--subsystem-match=SUBSYSTEM[/DEVTYPE] filter events by subsystem\n" + " -t,--tag-match=TAG filter events by tag\n" + " -h,--help\n\n"); +} + +static int adm_monitor(struct udev *udev, int argc, char *argv[]) { + struct sigaction act = {}; sigset_t mask; - int option; bool prop = false; bool print_kernel = false; bool print_udev = false; - struct udev_list subsystem_match_list; - struct udev_list tag_match_list; - struct udev_monitor *udev_monitor = NULL; - struct udev_monitor *kernel_monitor = NULL; - int fd_ep = -1; + _cleanup_udev_list_cleanup_ struct udev_list subsystem_match_list; + _cleanup_udev_list_cleanup_ struct udev_list tag_match_list; + _cleanup_udev_monitor_unref_ struct udev_monitor *udev_monitor = NULL; + _cleanup_udev_monitor_unref_ struct udev_monitor *kernel_monitor = NULL; + _cleanup_close_ int fd_ep = -1; int fd_kernel = -1, fd_udev = -1; struct epoll_event ep_kernel, ep_udev; - int rc = 0; + int c; static const struct option options[] = { - { "property", no_argument, NULL, 'p' }, - { "environment", no_argument, NULL, 'e' }, - { "kernel", no_argument, NULL, 'k' }, - { "udev", no_argument, NULL, 'u' }, + { "property", no_argument, NULL, 'p' }, + { "environment", no_argument, NULL, 'e' }, /* alias for -p */ + { "kernel", no_argument, NULL, 'k' }, + { "udev", no_argument, NULL, 'u' }, { "subsystem-match", required_argument, NULL, 's' }, - { "tag-match", required_argument, NULL, 't' }, - { "help", no_argument, NULL, 'h' }, + { "tag-match", required_argument, NULL, 't' }, + { "help", no_argument, NULL, 'h' }, {} }; udev_list_init(udev, &subsystem_match_list, true); udev_list_init(udev, &tag_match_list, true); - for (;;) { - option = getopt_long(argc, argv, "pekus:t:h", options, NULL); - if (option == -1) - break; - - switch (option) { + while((c = getopt_long(argc, argv, "pekus:t:h", options, NULL)) >= 0) + switch (c) { case 'p': case 'e': prop = true; @@ -116,7 +119,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[]) char subsys[UTIL_NAME_SIZE]; char *devtype; - util_strscpy(subsys, sizeof(subsys), optarg); + strscpy(subsys, sizeof(subsys), optarg); devtype = strchr(subsys, '/'); if (devtype != NULL) { devtype[0] = '\0'; @@ -129,19 +132,11 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[]) udev_list_entry_add(&tag_match_list, optarg, NULL); break; case 'h': - printf("Usage: udevadm monitor [--property] [--kernel] [--udev] [--help]\n" - " --property print the event properties\n" - " --kernel print kernel uevents\n" - " --udev print udev events\n" - " --subsystem-match= filter events by subsystem\n" - " --tag-match= filter events by tag\n" - " --help\n\n"); - goto out; + help(); + return 0; default: - rc = 1; - goto out; + return 1; } - } if (!print_kernel && !print_udev) { print_kernel = true; @@ -149,9 +144,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[]) } /* set signal handlers */ - memset(&act, 0x00, sizeof(struct sigaction)); act.sa_handler = sig_handler; - sigemptyset(&act.sa_mask); act.sa_flags = SA_RESTART; sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); @@ -162,8 +155,8 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[]) fd_ep = epoll_create1(EPOLL_CLOEXEC); if (fd_ep < 0) { - log_error("error creating epoll fd: %m\n"); - goto out; + log_error("error creating epoll fd: %m"); + return 1; } printf("monitor will print the received events for:\n"); @@ -173,8 +166,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[]) udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); if (udev_monitor == NULL) { fprintf(stderr, "error: unable to create netlink socket\n"); - rc = 1; - goto out; + return 1; } udev_monitor_set_receive_buffer_size(udev_monitor, 128*1024*1024); fd_udev = udev_monitor_get_fd(udev_monitor); @@ -196,16 +188,15 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[]) if (udev_monitor_enable_receiving(udev_monitor) < 0) { fprintf(stderr, "error: unable to subscribe to udev events\n"); - rc = 2; - goto out; + return 2; } - memset(&ep_udev, 0, sizeof(struct epoll_event)); + memzero(&ep_udev, sizeof(struct epoll_event)); ep_udev.events = EPOLLIN; ep_udev.data.fd = fd_udev; if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) { - log_error("fail to add fd to epoll: %m\n"); - goto out; + log_error("fail to add fd to epoll: %m"); + return 2; } printf("UDEV - the event which udev sends out after rule processing\n"); @@ -217,8 +208,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[]) kernel_monitor = udev_monitor_new_from_netlink(udev, "kernel"); if (kernel_monitor == NULL) { fprintf(stderr, "error: unable to create netlink socket\n"); - rc = 3; - goto out; + return 3; } udev_monitor_set_receive_buffer_size(kernel_monitor, 128*1024*1024); fd_kernel = udev_monitor_get_fd(kernel_monitor); @@ -232,16 +222,15 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[]) if (udev_monitor_enable_receiving(kernel_monitor) < 0) { fprintf(stderr, "error: unable to subscribe to kernel events\n"); - rc = 4; - goto out; + return 4; } - memset(&ep_kernel, 0, sizeof(struct epoll_event)); + memzero(&ep_kernel, sizeof(struct epoll_event)); ep_kernel.events = EPOLLIN; ep_kernel.data.fd = fd_kernel; if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_kernel, &ep_kernel) < 0) { - log_error("fail to add fd to epoll: %m\n"); - goto out; + log_error("fail to add fd to epoll: %m"); + return 5; } printf("KERNEL - the kernel uevent\n"); @@ -253,7 +242,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[]) struct epoll_event ev[4]; int i; - fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), -1); + fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1); if (fdcount < 0) { if (errno != EINTR) fprintf(stderr, "error receiving uevent message: %m\n"); @@ -280,14 +269,8 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[]) } } } -out: - if (fd_ep >= 0) - close(fd_ep); - udev_monitor_unref(udev_monitor); - udev_monitor_unref(kernel_monitor); - udev_list_cleanup(&subsystem_match_list); - udev_list_cleanup(&tag_match_list); - return rc; + + return 0; } const struct udevadm_cmd udevadm_monitor = {