2 * Copyright (C) 2004-2010 Kay Sievers <kay.sievers@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/>.
28 #include <sys/socket.h>
30 #include <sys/epoll.h>
31 #include <linux/types.h>
32 #include <linux/netlink.h>
36 static bool udev_exit;
38 static void sig_handler(int signum)
40 if (signum == SIGINT || signum == SIGTERM)
44 static void print_device(struct udev_device *device, const char *source, int prop)
49 gettimeofday(&tv, &tz);
50 printf("%-6s[%llu.%06u] %-8s %s (%s)\n",
52 (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec,
53 udev_device_get_action(device),
54 udev_device_get_devpath(device),
55 udev_device_get_subsystem(device));
57 struct udev_list_entry *list_entry;
59 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device))
61 udev_list_entry_get_name(list_entry),
62 udev_list_entry_get_value(list_entry));
67 int udevadm_monitor(struct udev *udev, int argc, char *argv[])
73 bool print_kernel = false;
74 bool print_udev = false;
75 struct udev_list_node subsystem_match_list;
76 struct udev_list_node tag_match_list;
77 struct udev_monitor *udev_monitor = NULL;
78 struct udev_monitor *kernel_monitor = NULL;
80 int fd_kernel = -1, fd_udev = -1;
81 struct epoll_event ep_kernel, ep_udev;
84 static const struct option options[] = {
85 { "property", no_argument, NULL, 'p' },
86 { "environment", no_argument, NULL, 'e' },
87 { "kernel", no_argument, NULL, 'k' },
88 { "udev", no_argument, NULL, 'u' },
89 { "subsystem-match", required_argument, NULL, 's' },
90 { "tag-match", required_argument, NULL, 't' },
91 { "help", no_argument, NULL, 'h' },
95 udev_list_init(&subsystem_match_list);
96 udev_list_init(&tag_match_list);
99 option = getopt_long(argc, argv, "pekus:t:h", options, NULL);
116 char subsys[UTIL_NAME_SIZE];
119 util_strscpy(subsys, sizeof(subsys), optarg);
120 devtype = strchr(subsys, '/');
121 if (devtype != NULL) {
125 udev_list_entry_add(udev, &subsystem_match_list, subsys, devtype, 0);
129 udev_list_entry_add(udev, &tag_match_list, optarg, NULL, 0);
132 printf("Usage: udevadm monitor [--property] [--kernel] [--udev] [--help]\n"
133 " --property print the event properties\n"
134 " --kernel print kernel uevents\n"
135 " --udev print udev events\n"
136 " --subsystem-match=<subsystem[/devtype]> filter events by subsystem\n"
137 " --tag-match=<tag> filter events by tag\n"
143 if (!print_kernel && !print_udev) {
148 /* set signal handlers */
149 memset(&act, 0x00, sizeof(struct sigaction));
150 act.sa_handler = sig_handler;
151 sigemptyset(&act.sa_mask);
152 act.sa_flags = SA_RESTART;
153 sigaction(SIGINT, &act, NULL);
154 sigaction(SIGTERM, &act, NULL);
156 sigaddset(&mask, SIGINT);
157 sigaddset(&mask, SIGTERM);
158 sigprocmask(SIG_UNBLOCK, &mask, NULL);
160 fd_ep = epoll_create1(EPOLL_CLOEXEC);
162 err(udev, "error creating epoll fd: %m\n");
166 printf("monitor will print the received events for:\n");
168 struct udev_list_entry *entry;
170 udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
171 if (udev_monitor == NULL) {
172 fprintf(stderr, "error: unable to create netlink socket\n");
176 udev_monitor_set_receive_buffer_size(udev_monitor, 128*1024*1024);
177 fd_udev = udev_monitor_get_fd(udev_monitor);
179 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
180 const char *subsys = udev_list_entry_get_name(entry);
181 const char *devtype = udev_list_entry_get_value(entry);
183 if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, subsys, devtype) < 0)
184 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
187 udev_list_entry_foreach(entry, udev_list_get_entry(&tag_match_list)) {
188 const char *tag = udev_list_entry_get_name(entry);
190 if (udev_monitor_filter_add_match_tag(udev_monitor, tag) < 0)
191 fprintf(stderr, "error: unable to apply tag filter '%s'\n", tag);
194 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
195 fprintf(stderr, "error: unable to subscribe to udev events\n");
200 memset(&ep_udev, 0, sizeof(struct epoll_event));
201 ep_udev.events = EPOLLIN;
202 ep_udev.data.fd = fd_udev;
203 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) {
204 err(udev, "fail to add fd to epoll: %m\n");
208 printf("UDEV - the event which udev sends out after rule processing\n");
212 struct udev_list_entry *entry;
214 kernel_monitor = udev_monitor_new_from_netlink(udev, "kernel");
215 if (kernel_monitor == NULL) {
216 fprintf(stderr, "error: unable to create netlink socket\n");
220 udev_monitor_set_receive_buffer_size(kernel_monitor, 128*1024*1024);
221 fd_kernel = udev_monitor_get_fd(kernel_monitor);
223 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
224 const char *subsys = udev_list_entry_get_name(entry);
226 if (udev_monitor_filter_add_match_subsystem_devtype(kernel_monitor, subsys, NULL) < 0)
227 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
230 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
231 fprintf(stderr, "error: unable to subscribe to kernel events\n");
236 memset(&ep_kernel, 0, sizeof(struct epoll_event));
237 ep_kernel.events = EPOLLIN;
238 ep_kernel.data.fd = fd_kernel;
239 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_kernel, &ep_kernel) < 0) {
240 err(udev, "fail to add fd to epoll: %m\n");
244 printf("KERNEL - the kernel uevent\n");
250 struct epoll_event ev[4];
253 fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), -1);
256 fprintf(stderr, "error receiving uevent message: %m\n");
260 for (i = 0; i < fdcount; i++) {
261 if (ev[i].data.fd == fd_kernel && ev[i].events & EPOLLIN) {
262 struct udev_device *device;
264 device = udev_monitor_receive_device(kernel_monitor);
267 print_device(device, "KERNEL", prop);
268 udev_device_unref(device);
269 } else if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) {
270 struct udev_device *device;
272 device = udev_monitor_receive_device(udev_monitor);
275 print_device(device, "UDEV", prop);
276 udev_device_unref(device);
283 udev_monitor_unref(udev_monitor);
284 udev_monitor_unref(kernel_monitor);
285 udev_list_cleanup_entries(udev, &subsystem_match_list);
286 udev_list_cleanup_entries(udev, &tag_match_list);