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 int print_properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data)
46 printf("%s=%s\n", key, value);
50 static void print_properties(struct udev_device *device)
52 udev_device_get_properties(device, print_properties_cb, NULL);
56 int udevadm_monitor(struct udev *udev, int argc, char *argv[])
63 struct udev_monitor *udev_monitor = NULL;
64 struct udev_monitor *kernel_monitor = NULL;
68 static const struct option options[] = {
69 { "environment", 0, NULL, 'e' },
70 { "kernel", 0, NULL, 'k' },
71 { "udev", 0, NULL, 'u' },
72 { "help", 0, NULL, 'h' },
77 option = getopt_long(argc, argv, "ekuh", options, NULL);
92 printf("Usage: udevadm monitor [--environment] [--kernel] [--udev] [--help]\n"
93 " --env print the whole event environment\n"
94 " --kernel print kernel uevents\n"
95 " --udev print udev events\n"
96 " --help print this help text\n\n");
102 if (!print_kernel && !print_udev) {
107 if (getuid() != 0 && print_kernel) {
108 fprintf(stderr, "root privileges needed to subscribe to kernel events\n");
112 /* set signal handlers */
113 memset(&act, 0x00, sizeof(struct sigaction));
114 act.sa_handler = (void (*)(int)) sig_handler;
115 sigemptyset(&act.sa_mask);
116 act.sa_flags = SA_RESTART;
117 sigaction(SIGINT, &act, NULL);
118 sigaction(SIGTERM, &act, NULL);
120 printf("monitor will print the received events for:\n");
122 udev_monitor = udev_monitor_new_from_socket(udev, "@/org/kernel/udev/monitor");
123 if (udev_monitor == NULL) {
127 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
131 printf("UDEV the event which udev sends out after rule processing\n");
134 kernel_monitor = udev_monitor_new_from_netlink(udev);
135 if (kernel_monitor == NULL) {
139 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
143 printf("UEVENT the kernel uevent\n");
154 if (kernel_monitor != NULL)
155 FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds);
156 if (udev_monitor != NULL)
157 FD_SET(udev_monitor_get_fd(udev_monitor), &readfds);
159 fdcount = select(UDEV_MAX(udev_monitor_get_fd(kernel_monitor), udev_monitor_get_fd(udev_monitor))+1,
160 &readfds, NULL, NULL, NULL);
163 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
167 if (gettimeofday(&tv, &tz) == 0) {
168 snprintf(timestr, sizeof(timestr), "%llu.%06u",
169 (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec);
173 if ((kernel_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) {
174 struct udev_device *device = udev_monitor_receive_device(kernel_monitor);
177 printf("UEVENT[%s] %-8s %s (%s)\n", timestr,
178 udev_device_get_action(device),
179 udev_device_get_devpath(device),
180 udev_device_get_subsystem(device));
182 print_properties(device);
183 udev_device_unref(device);
186 if ((udev_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(udev_monitor), &readfds)) {
187 struct udev_device *device = udev_monitor_receive_device(udev_monitor);
190 printf("UDEV [%s] %-8s %s (%s)\n", timestr,
191 udev_device_get_action(device),
192 udev_device_get_devpath(device),
193 udev_device_get_subsystem(device));
195 print_properties(device);
196 udev_device_unref(device);
201 udev_monitor_unref(udev_monitor);
202 udev_monitor_unref(kernel_monitor);