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>
37 static int uevent_netlink_sock = -1;
40 static int init_uevent_netlink_sock(void)
42 struct sockaddr_nl snl;
45 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
46 snl.nl_family = AF_NETLINK;
47 snl.nl_pid = getpid();
50 uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
51 if (uevent_netlink_sock == -1) {
52 fprintf(stderr, "error getting socket: %s\n", strerror(errno));
56 err = bind(uevent_netlink_sock, (struct sockaddr *) &snl,
57 sizeof(struct sockaddr_nl));
59 fprintf(stderr, "bind failed: %s\n", strerror(errno));
60 close(uevent_netlink_sock);
61 uevent_netlink_sock = -1;
68 static void asmlinkage sig_handler(int signum)
70 if (signum == SIGINT || signum == SIGTERM)
74 static const char *search_key(const char *searchkey, const char *buf, size_t buflen)
77 size_t searchkeylen = strlen(searchkey);
79 while (bufpos < buflen) {
87 if ((strncmp(searchkey, key, searchkeylen) == 0) && key[searchkeylen] == '=')
88 return &key[searchkeylen + 1];
94 int udevadm_monitor(struct udev *udev, int argc, char *argv[])
101 struct udev_monitor *udev_monitor = NULL;
105 static const struct option options[] = {
106 { "environment", 0, NULL, 'e' },
107 { "kernel", 0, NULL, 'k' },
108 { "udev", 0, NULL, 'u' },
109 { "help", 0, NULL, 'h' },
114 option = getopt_long(argc, argv, "ekuh", options, NULL);
129 printf("Usage: udevadm monitor [--environment] [--kernel] [--udev] [--help]\n"
130 " --env print the whole event environment\n"
131 " --kernel print kernel uevents\n"
132 " --udev print udev events\n"
133 " --help print this help text\n\n");
139 if (!print_kernel && !print_udev) {
144 if (getuid() != 0 && print_kernel) {
145 fprintf(stderr, "root privileges needed to subscribe to kernel events\n");
149 /* set signal handlers */
150 memset(&act, 0x00, sizeof(struct sigaction));
151 act.sa_handler = (void (*)(int)) sig_handler;
152 sigemptyset(&act.sa_mask);
153 act.sa_flags = SA_RESTART;
154 sigaction(SIGINT, &act, NULL);
155 sigaction(SIGTERM, &act, NULL);
157 printf("monitor will print the received events for:\n");
159 udev_monitor = udev_monitor_new_from_socket(udev, "@/org/kernel/udev/monitor");
160 if (udev_monitor == NULL) {
164 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
168 printf("UDEV the event which udev sends out after rule processing\n");
171 if (init_uevent_netlink_sock() < 0) {
175 printf("UEVENT the kernel uevent\n");
180 char buf[UEVENT_BUFFER_SIZE*2];
188 const char *source = NULL;
189 const char *devpath, *action, *subsys;
193 if (uevent_netlink_sock >= 0)
194 FD_SET(uevent_netlink_sock, &readfds);
195 if (udev_monitor != NULL)
196 FD_SET(udev_monitor_get_fd(udev_monitor), &readfds);
198 fdcount = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_get_fd(udev_monitor))+1,
199 &readfds, NULL, NULL, NULL);
202 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
206 if (gettimeofday(&tv, &tz) == 0) {
207 snprintf(timestr, sizeof(timestr), "%llu.%06u",
208 (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec);
212 if ((uevent_netlink_sock >= 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
213 buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
215 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
221 if ((udev_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(udev_monitor), &readfds)) {
222 buflen = recv(udev_monitor_get_fd(udev_monitor), &buf, sizeof(buf), 0);
224 fprintf(stderr, "error receiving udev message: %s\n", strerror(errno));
233 keys = strlen(buf) + 1; /* start of payload */
234 devpath = search_key("DEVPATH", &buf[keys], buflen);
235 action = search_key("ACTION", &buf[keys], buflen);
236 subsys = search_key("SUBSYSTEM", &buf[keys], buflen);
237 printf("%s[%s] %-8s %s (%s)\n", source, timestr, action, devpath, subsys);
239 /* print environment */
242 while (bufpos < buflen) {
247 keylen = strlen(key);
251 bufpos += keylen + 1;
258 udev_monitor_unref(udev_monitor);
259 if (uevent_netlink_sock >= 0)
260 close(uevent_netlink_sock);