2 * Copyright (C) 2004-2010 Kay Sievers <kay@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/socket.h>
31 #include <sys/epoll.h>
32 #include <linux/types.h>
33 #include <linux/netlink.h>
36 #include "udev-util.h"
38 static bool udev_exit;
40 static void sig_handler(int signum) {
41 if (signum == SIGINT || signum == SIGTERM)
45 static void print_device(struct udev_device *device, const char *source, int prop) {
48 clock_gettime(CLOCK_MONOTONIC, &ts);
49 printf("%-6s[%"PRI_TIME".%06ld] %-8s %s (%s)\n",
51 ts.tv_sec, ts.tv_nsec/1000,
52 udev_device_get_action(device),
53 udev_device_get_devpath(device),
54 udev_device_get_subsystem(device));
56 struct udev_list_entry *list_entry;
58 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device))
60 udev_list_entry_get_name(list_entry),
61 udev_list_entry_get_value(list_entry));
66 static void help(void) {
67 printf("%s monitor [--property] [--kernel] [--udev] [--help]\n\n"
68 "Listen to kernel and udev events.\n\n"
69 " -h --help Show this help\n"
70 " --version Show package version\n"
71 " -p --property Print the event properties\n"
72 " -k --kernel Print kernel uevents\n"
73 " -u --udev Print udev events\n"
74 " -s --subsystem-match=SUBSYSTEM[/DEVTYPE] Filter events by subsystem\n"
75 " -t --tag-match=TAG Filter events by tag\n"
76 , program_invocation_short_name);
79 static int adm_monitor(struct udev *udev, int argc, char *argv[]) {
80 struct sigaction act = {};
83 bool print_kernel = false;
84 bool print_udev = false;
85 _cleanup_udev_list_cleanup_ struct udev_list subsystem_match_list;
86 _cleanup_udev_list_cleanup_ struct udev_list tag_match_list;
87 _cleanup_udev_monitor_unref_ struct udev_monitor *udev_monitor = NULL;
88 _cleanup_udev_monitor_unref_ struct udev_monitor *kernel_monitor = NULL;
89 _cleanup_close_ int fd_ep = -1;
90 int fd_kernel = -1, fd_udev = -1;
91 struct epoll_event ep_kernel, ep_udev;
94 static const struct option options[] = {
95 { "property", no_argument, NULL, 'p' },
96 { "environment", no_argument, NULL, 'e' }, /* alias for -p */
97 { "kernel", no_argument, NULL, 'k' },
98 { "udev", no_argument, NULL, 'u' },
99 { "subsystem-match", required_argument, NULL, 's' },
100 { "tag-match", required_argument, NULL, 't' },
101 { "help", no_argument, NULL, 'h' },
105 udev_list_init(udev, &subsystem_match_list, true);
106 udev_list_init(udev, &tag_match_list, true);
108 while((c = getopt_long(argc, argv, "pekus:t:h", options, NULL)) >= 0)
122 char subsys[UTIL_NAME_SIZE];
125 strscpy(subsys, sizeof(subsys), optarg);
126 devtype = strchr(subsys, '/');
127 if (devtype != NULL) {
131 udev_list_entry_add(&subsystem_match_list, subsys, devtype);
135 udev_list_entry_add(&tag_match_list, optarg, NULL);
144 if (!print_kernel && !print_udev) {
149 /* set signal handlers */
150 act.sa_handler = sig_handler;
151 act.sa_flags = SA_RESTART;
152 sigaction(SIGINT, &act, NULL);
153 sigaction(SIGTERM, &act, NULL);
155 sigaddset(&mask, SIGINT);
156 sigaddset(&mask, SIGTERM);
157 sigprocmask(SIG_UNBLOCK, &mask, NULL);
159 fd_ep = epoll_create1(EPOLL_CLOEXEC);
161 log_error_errno(errno, "error creating epoll fd: %m");
165 printf("monitor will print the received events for:\n");
167 struct udev_list_entry *entry;
169 udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
170 if (udev_monitor == NULL) {
171 fprintf(stderr, "error: unable to create netlink socket\n");
174 udev_monitor_set_receive_buffer_size(udev_monitor, 128*1024*1024);
175 fd_udev = udev_monitor_get_fd(udev_monitor);
177 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
178 const char *subsys = udev_list_entry_get_name(entry);
179 const char *devtype = udev_list_entry_get_value(entry);
181 if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, subsys, devtype) < 0)
182 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
185 udev_list_entry_foreach(entry, udev_list_get_entry(&tag_match_list)) {
186 const char *tag = udev_list_entry_get_name(entry);
188 if (udev_monitor_filter_add_match_tag(udev_monitor, tag) < 0)
189 fprintf(stderr, "error: unable to apply tag filter '%s'\n", tag);
192 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
193 fprintf(stderr, "error: unable to subscribe to udev events\n");
197 memzero(&ep_udev, sizeof(struct epoll_event));
198 ep_udev.events = EPOLLIN;
199 ep_udev.data.fd = fd_udev;
200 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) {
201 log_error_errno(errno, "fail to add fd to epoll: %m");
205 printf("UDEV - the event which udev sends out after rule processing\n");
209 struct udev_list_entry *entry;
211 kernel_monitor = udev_monitor_new_from_netlink(udev, "kernel");
212 if (kernel_monitor == NULL) {
213 fprintf(stderr, "error: unable to create netlink socket\n");
216 udev_monitor_set_receive_buffer_size(kernel_monitor, 128*1024*1024);
217 fd_kernel = udev_monitor_get_fd(kernel_monitor);
219 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
220 const char *subsys = udev_list_entry_get_name(entry);
222 if (udev_monitor_filter_add_match_subsystem_devtype(kernel_monitor, subsys, NULL) < 0)
223 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
226 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
227 fprintf(stderr, "error: unable to subscribe to kernel events\n");
231 memzero(&ep_kernel, sizeof(struct epoll_event));
232 ep_kernel.events = EPOLLIN;
233 ep_kernel.data.fd = fd_kernel;
234 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_kernel, &ep_kernel) < 0) {
235 log_error_errno(errno, "fail to add fd to epoll: %m");
239 printf("KERNEL - the kernel uevent\n");
245 struct epoll_event ev[4];
248 fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
251 fprintf(stderr, "error receiving uevent message: %m\n");
255 for (i = 0; i < fdcount; i++) {
256 if (ev[i].data.fd == fd_kernel && ev[i].events & EPOLLIN) {
257 struct udev_device *device;
259 device = udev_monitor_receive_device(kernel_monitor);
262 print_device(device, "KERNEL", prop);
263 udev_device_unref(device);
264 } else if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) {
265 struct udev_device *device;
267 device = udev_monitor_receive_device(udev_monitor);
270 print_device(device, "UDEV", prop);
271 udev_device_unref(device);
279 const struct udevadm_cmd udevadm_monitor = {
282 .help = "Listen to kernel and udev events",