X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udev%2Fudevadm-monitor.c;h=64913dbd55cbfa9acd9067b48b25d04d7b06d957;hp=fb650846bcd4eca2628cdbe7c13d9fbc588aed3c;hb=aa29418a13dc1e1eedca702663e733cc8d9f9859;hpb=28460195c2ae90892bf556aff2b80705a8f37795 diff --git a/udev/udevadm-monitor.c b/udev/udevadm-monitor.c index fb650846b..64913dbd5 100644 --- a/udev/udevadm-monitor.c +++ b/udev/udevadm-monitor.c @@ -24,32 +24,32 @@ #include #include #include +#include #include #include #include -#include +#include #include #include #include "udev.h" -static int udev_exit; +static bool udev_exit; static void sig_handler(int signum) { if (signum == SIGINT || signum == SIGTERM) - udev_exit = 1; + udev_exit = true; } static void print_device(struct udev_device *device, const char *source, int prop) { - struct timeval tv; - struct timezone tz; + struct timespec ts; - gettimeofday(&tv, &tz); + clock_gettime(CLOCK_MONOTONIC, &ts); printf("%-6s[%llu.%06u] %-8s %s (%s)\n", source, - (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec, + (unsigned long long) ts.tv_sec, (unsigned int) ts.tv_nsec/1000, udev_device_get_action(device), udev_device_get_devpath(device), udev_device_get_subsystem(device)); @@ -64,19 +64,21 @@ static void print_device(struct udev_device *device, const char *source, int pro } } -int udevadm_monitor(struct udev *udev, int argc, char *argv[]) +static int adm_monitor(struct udev *udev, int argc, char *argv[]) { struct sigaction act; sigset_t mask; int option; - int prop = 0; - int print_kernel = 0; - int print_udev = 0; - struct udev_list_node subsystem_match_list; - struct udev_list_node tag_match_list; + 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; - fd_set readfds; + int fd_ep = -1; + int fd_kernel = -1, fd_udev = -1; + struct epoll_event ep_kernel, ep_udev; int rc = 0; static const struct option options[] = { @@ -90,9 +92,10 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[]) {} }; - udev_list_init(&subsystem_match_list); - udev_list_init(&tag_match_list); - while (1) { + 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; @@ -100,13 +103,13 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[]) switch (option) { case 'p': case 'e': - prop = 1; + prop = true; break; case 'k': - print_kernel = 1; + print_kernel = true; break; case 'u': - print_udev = 1; + print_udev = true; break; case 's': { @@ -119,11 +122,11 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[]) devtype[0] = '\0'; devtype++; } - udev_list_entry_add(udev, &subsystem_match_list, subsys, devtype, 0, 0); + udev_list_entry_add(&subsystem_match_list, subsys, devtype); break; } case 't': - udev_list_entry_add(udev, &tag_match_list, optarg, NULL, 0, 0); + udev_list_entry_add(&tag_match_list, optarg, NULL); break; case 'h': printf("Usage: udevadm monitor [--property] [--kernel] [--udev] [--help]\n" @@ -133,14 +136,16 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[]) " --subsystem-match= filter events by subsystem\n" " --tag-match= filter events by tag\n" " --help\n\n"); + goto out; default: + rc = 1; goto out; } } if (!print_kernel && !print_udev) { - print_kernel = 1; - print_udev =1; + print_kernel = true; + print_udev = true; } /* set signal handlers */ @@ -155,6 +160,12 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[]) sigaddset(&mask, SIGTERM); sigprocmask(SIG_UNBLOCK, &mask, NULL); + fd_ep = epoll_create1(EPOLL_CLOEXEC); + if (fd_ep < 0) { + err(udev, "error creating epoll fd: %m\n"); + goto out; + } + printf("monitor will print the received events for:\n"); if (print_udev) { struct udev_list_entry *entry; @@ -166,6 +177,7 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[]) goto out; } udev_monitor_set_receive_buffer_size(udev_monitor, 128*1024*1024); + fd_udev = udev_monitor_get_fd(udev_monitor); udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) { const char *subsys = udev_list_entry_get_name(entry); @@ -187,8 +199,18 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[]) rc = 2; goto out; } + + memset(&ep_udev, 0, 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) { + err(udev, "fail to add fd to epoll: %m\n"); + goto out; + } + printf("UDEV - the event which udev sends out after rule processing\n"); } + if (print_kernel) { struct udev_list_entry *entry; @@ -199,6 +221,7 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[]) goto out; } udev_monitor_set_receive_buffer_size(kernel_monitor, 128*1024*1024); + fd_kernel = udev_monitor_get_fd(kernel_monitor); udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) { const char *subsys = udev_list_entry_get_name(entry); @@ -212,52 +235,63 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[]) rc = 4; goto out; } + + memset(&ep_kernel, 0, 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) { + err(udev, "fail to add fd to epoll: %m\n"); + goto out; + } + printf("KERNEL - the kernel uevent\n"); } printf("\n"); while (!udev_exit) { int fdcount; + struct epoll_event ev[4]; + int i; - FD_ZERO(&readfds); - if (kernel_monitor != NULL) - FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds); - if (udev_monitor != NULL) - FD_SET(udev_monitor_get_fd(udev_monitor), &readfds); - - fdcount = select(MAX(udev_monitor_get_fd(kernel_monitor), udev_monitor_get_fd(udev_monitor))+1, - &readfds, NULL, NULL, NULL); + fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), -1); if (fdcount < 0) { if (errno != EINTR) fprintf(stderr, "error receiving uevent message: %m\n"); continue; } - if ((kernel_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) { - struct udev_device *device; - - device = udev_monitor_receive_device(kernel_monitor); - if (device == NULL) - continue; - print_device(device, "KERNEL", prop); - udev_device_unref(device); - } - - if ((udev_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(udev_monitor), &readfds)) { - struct udev_device *device; - - device = udev_monitor_receive_device(udev_monitor); - if (device == NULL) - continue; - print_device(device, "UDEV", prop); - udev_device_unref(device); + for (i = 0; i < fdcount; i++) { + if (ev[i].data.fd == fd_kernel && ev[i].events & EPOLLIN) { + struct udev_device *device; + + device = udev_monitor_receive_device(kernel_monitor); + if (device == NULL) + continue; + print_device(device, "KERNEL", prop); + udev_device_unref(device); + } else if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) { + struct udev_device *device; + + device = udev_monitor_receive_device(udev_monitor); + if (device == NULL) + continue; + print_device(device, "UDEV", prop); + udev_device_unref(device); + } } } - out: + if (fd_ep >= 0) + close(fd_ep); udev_monitor_unref(udev_monitor); udev_monitor_unref(kernel_monitor); - udev_list_cleanup_entries(udev, &subsystem_match_list); - udev_list_cleanup_entries(udev, &tag_match_list); + udev_list_cleanup(&subsystem_match_list); + udev_list_cleanup(&tag_match_list); return rc; } + +const struct udevadm_cmd udevadm_monitor = { + .name = "monitor", + .cmd = adm_monitor, + .help = "listen to kernel and udev events", +};