2 * Copyright (C) 2004-2008 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/select.h>
31 #include <linux/types.h>
32 #include <linux/netlink.h>
38 static void asmlinkage sig_handler(int signum)
40 if (signum == SIGINT || signum == SIGTERM)
44 static void print_device(struct udev_device *device, const char *source, int env)
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[])
74 struct udev_list_node subsystem_match_list;
75 struct udev_monitor *udev_monitor = NULL;
76 struct udev_monitor *kernel_monitor = NULL;
80 static const struct option options[] = {
81 { "environment", no_argument, NULL, 'e' },
82 { "kernel", no_argument, NULL, 'k' },
83 { "udev", no_argument, NULL, 'u' },
84 { "subsystem-match", required_argument, NULL, 's' },
85 { "help", no_argument, NULL, 'h' },
89 udev_list_init(&subsystem_match_list);
91 option = getopt_long(argc, argv, "ekus:h", options, NULL);
106 udev_list_entry_add(udev, &subsystem_match_list, optarg, NULL, 1, 0);
109 printf("Usage: udevadm monitor [--environment] [--kernel] [--udev] [--help]\n"
110 " --env print the whole event environment\n"
111 " --kernel print kernel uevents\n"
112 " --udev print udev events\n"
119 if (!print_kernel && !print_udev) {
124 /* set signal handlers */
125 memset(&act, 0x00, sizeof(struct sigaction));
126 act.sa_handler = (void (*)(int)) sig_handler;
127 sigemptyset(&act.sa_mask);
128 act.sa_flags = SA_RESTART;
129 sigaction(SIGINT, &act, NULL);
130 sigaction(SIGTERM, &act, NULL);
132 printf("monitor will print the received events for:\n");
134 struct udev_list_entry *entry;
136 udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
137 if (udev_monitor == NULL) {
138 fprintf(stderr, "error: unable to create netlink socket\n");
143 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
144 const char *subsys = udev_list_entry_get_name(entry);
146 if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, subsys, NULL) < 0)
147 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
150 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
151 fprintf(stderr, "error: unable to subscribe to udev events\n");
155 printf("UDEV - the event which udev sends out after rule processing\n");
158 kernel_monitor = udev_monitor_new_from_netlink(udev, "kernel");
159 if (kernel_monitor == NULL) {
160 fprintf(stderr, "error: unable to create netlink socket\n");
164 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
165 fprintf(stderr, "error: unable to subscribe to kernel events\n");
169 printf("KERNEL - the kernel uevent\n");
177 if (kernel_monitor != NULL)
178 FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds);
179 if (udev_monitor != NULL)
180 FD_SET(udev_monitor_get_fd(udev_monitor), &readfds);
182 fdcount = select(UDEV_MAX(udev_monitor_get_fd(kernel_monitor), udev_monitor_get_fd(udev_monitor))+1,
183 &readfds, NULL, NULL, NULL);
186 fprintf(stderr, "error receiving uevent message: %m\n");
190 if ((kernel_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) {
191 struct udev_device *device;
193 device = udev_monitor_receive_device(kernel_monitor);
196 print_device(device, "KERNEL", env);
197 udev_device_unref(device);
200 if ((udev_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(udev_monitor), &readfds)) {
201 struct udev_device *device;
203 device = udev_monitor_receive_device(udev_monitor);
206 print_device(device, "UDEV", env);
207 udev_device_unref(device);
212 udev_monitor_unref(udev_monitor);
213 udev_monitor_unref(kernel_monitor);
214 udev_list_cleanup_entries(udev, &subsystem_match_list);