2 * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <sys/socket.h>
31 #include <sys/select.h>
32 #include <linux/types.h>
33 #include <linux/netlink.h>
39 static void asmlinkage sig_handler(int signum)
41 if (signum == SIGINT || signum == SIGTERM)
45 static int print_properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data)
47 printf("%s=%s\n", key, value);
51 static void print_properties(struct udev_device *device)
53 udev_device_get_properties(device, print_properties_cb, NULL);
57 int udevadm_monitor(struct udev *udev, int argc, char *argv[])
64 struct udev_monitor *udev_monitor = NULL;
65 struct udev_monitor *kernel_monitor = NULL;
69 static const struct option options[] = {
70 { "environment", 0, NULL, 'e' },
71 { "kernel", 0, NULL, 'k' },
72 { "udev", 0, NULL, 'u' },
73 { "help", 0, NULL, 'h' },
78 option = getopt_long(argc, argv, "ekuh", options, NULL);
93 printf("Usage: udevadm monitor [--environment] [--kernel] [--udev] [--help]\n"
94 " --env print the whole event environment\n"
95 " --kernel print kernel uevents\n"
96 " --udev print udev events\n"
97 " --help print this help text\n\n");
103 if (!print_kernel && !print_udev) {
108 if (getuid() != 0 && print_kernel) {
109 fprintf(stderr, "root privileges needed to subscribe to kernel events\n");
113 /* set signal handlers */
114 memset(&act, 0x00, sizeof(struct sigaction));
115 act.sa_handler = (void (*)(int)) sig_handler;
116 sigemptyset(&act.sa_mask);
117 act.sa_flags = SA_RESTART;
118 sigaction(SIGINT, &act, NULL);
119 sigaction(SIGTERM, &act, NULL);
121 printf("monitor will print the received events for:\n");
123 udev_monitor = udev_monitor_new_from_socket(udev, "@/org/kernel/udev/monitor");
124 if (udev_monitor == NULL) {
128 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
132 printf("UDEV the event which udev sends out after rule processing\n");
135 kernel_monitor = udev_monitor_new_from_netlink(udev);
136 if (kernel_monitor == NULL) {
140 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
144 printf("UEVENT the kernel uevent\n");
155 if (kernel_monitor != NULL)
156 FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds);
157 if (udev_monitor != NULL)
158 FD_SET(udev_monitor_get_fd(udev_monitor), &readfds);
160 fdcount = select(UDEV_MAX(udev_monitor_get_fd(kernel_monitor), udev_monitor_get_fd(udev_monitor))+1,
161 &readfds, NULL, NULL, NULL);
164 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
168 if (gettimeofday(&tv, &tz) == 0) {
169 snprintf(timestr, sizeof(timestr), "%llu.%06u",
170 (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec);
174 if ((kernel_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) {
175 struct udev_device *device = udev_monitor_receive_device(kernel_monitor);
178 printf("UEVENT[%s] %-8s %s (%s)\n", timestr,
179 udev_device_get_action(device),
180 udev_device_get_devpath(device),
181 udev_device_get_subsystem(device));
183 print_properties(device);
184 udev_device_unref(device);
187 if ((udev_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(udev_monitor), &readfds)) {
188 struct udev_device *device = udev_monitor_receive_device(udev_monitor);
191 printf("UDEV [%s] %-8s %s (%s)\n", timestr,
192 udev_device_get_action(device),
193 udev_device_get_devpath(device),
194 udev_device_get_subsystem(device));
196 print_properties(device);
197 udev_device_unref(device);
202 udev_monitor_unref(udev_monitor);
203 udev_monitor_unref(kernel_monitor);