/*
- * Copyright (C) 2004-2010 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2004-2010 Kay Sievers <kay@vrfy.org>
*
* 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
#include <linux/netlink.h>
#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));
}
}
-static int adm_monitor(struct udev *udev, int argc, char *argv[])
-{
- struct sigaction act;
+static void help(void) {
+ printf("%s monitor [--property] [--kernel] [--udev] [--help]\n\n"
+ "Listen to kernel and udev events.\n\n"
+ " -h --help Show this help\n"
+ " --version Show package version\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"
+ , program_invocation_short_name);
+}
+
+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;
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';
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=<subsystem[/devtype]> filter events by subsystem\n"
- " --tag-match=<tag> 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;
}
/* 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);
fd_ep = epoll_create1(EPOLL_CLOEXEC);
if (fd_ep < 0) {
- err(udev, "error creating epoll fd: %m\n");
- goto out;
+ log_error_errno(errno, "error creating epoll fd: %m");
+ return 1;
}
printf("monitor will print the received events for:\n");
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);
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) {
- err(udev, "fail to add fd to epoll: %m\n");
- goto out;
+ log_error_errno(errno, "fail to add fd to epoll: %m");
+ return 2;
}
printf("UDEV - the event which udev sends out after rule processing\n");
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);
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) {
- err(udev, "fail to add fd to epoll: %m\n");
- goto out;
+ log_error_errno(errno, "fail to add fd to epoll: %m");
+ return 5;
}
printf("KERNEL - the kernel uevent\n");
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");
}
}
}
-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 = {
.name = "monitor",
.cmd = adm_monitor,
- .help = "listen to kernel and udev events",
+ .help = "Listen to kernel and udev events",
};