chiark / gitweb /
use the same email address everywhere
[elogind.git] / src / udev / udevadm-monitor.c
1 /*
2  * Copyright (C) 2004-2010 Kay Sievers <kay@vrfy.org>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <signal.h>
26 #include <getopt.h>
27 #include <time.h>
28 #include <sys/time.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include <sys/epoll.h>
32 #include <linux/types.h>
33 #include <linux/netlink.h>
34
35 #include "udev.h"
36
37 static bool udev_exit;
38
39 static void sig_handler(int signum)
40 {
41         if (signum == SIGINT || signum == SIGTERM)
42                 udev_exit = true;
43 }
44
45 static void print_device(struct udev_device *device, const char *source, int prop)
46 {
47         struct timespec ts;
48
49         clock_gettime(CLOCK_MONOTONIC, &ts);
50         printf("%-6s[%llu.%06u] %-8s %s (%s)\n",
51                source,
52                (unsigned long long) ts.tv_sec, (unsigned int) ts.tv_nsec/1000,
53                udev_device_get_action(device),
54                udev_device_get_devpath(device),
55                udev_device_get_subsystem(device));
56         if (prop) {
57                 struct udev_list_entry *list_entry;
58
59                 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device))
60                         printf("%s=%s\n",
61                                udev_list_entry_get_name(list_entry),
62                                udev_list_entry_get_value(list_entry));
63                 printf("\n");
64         }
65 }
66
67 static int adm_monitor(struct udev *udev, int argc, char *argv[])
68 {
69         struct sigaction act;
70         sigset_t mask;
71         int option;
72         bool prop = false;
73         bool print_kernel = false;
74         bool print_udev = false;
75         struct udev_list subsystem_match_list;
76         struct udev_list tag_match_list;
77         struct udev_monitor *udev_monitor = NULL;
78         struct udev_monitor *kernel_monitor = NULL;
79         int fd_ep = -1;
80         int fd_kernel = -1, fd_udev = -1;
81         struct epoll_event ep_kernel, ep_udev;
82         int rc = 0;
83
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' },
92                 {}
93         };
94
95         udev_list_init(udev, &subsystem_match_list, true);
96         udev_list_init(udev, &tag_match_list, true);
97
98         for (;;) {
99                 option = getopt_long(argc, argv, "pekus:t:h", options, NULL);
100                 if (option == -1)
101                         break;
102
103                 switch (option) {
104                 case 'p':
105                 case 'e':
106                         prop = true;
107                         break;
108                 case 'k':
109                         print_kernel = true;
110                         break;
111                 case 'u':
112                         print_udev = true;
113                         break;
114                 case 's':
115                         {
116                                 char subsys[UTIL_NAME_SIZE];
117                                 char *devtype;
118
119                                 util_strscpy(subsys, sizeof(subsys), optarg);
120                                 devtype = strchr(subsys, '/');
121                                 if (devtype != NULL) {
122                                         devtype[0] = '\0';
123                                         devtype++;
124                                 }
125                                 udev_list_entry_add(&subsystem_match_list, subsys, devtype);
126                                 break;
127                         }
128                 case 't':
129                         udev_list_entry_add(&tag_match_list, optarg, NULL);
130                         break;
131                 case 'h':
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"
138                                "  --help\n\n");
139                         goto out;
140                 default:
141                         rc = 1;
142                         goto out;
143                 }
144         }
145
146         if (!print_kernel && !print_udev) {
147                 print_kernel = true;
148                 print_udev = true;
149         }
150
151         /* set signal handlers */
152         memset(&act, 0x00, sizeof(struct sigaction));
153         act.sa_handler = sig_handler;
154         sigemptyset(&act.sa_mask);
155         act.sa_flags = SA_RESTART;
156         sigaction(SIGINT, &act, NULL);
157         sigaction(SIGTERM, &act, NULL);
158         sigemptyset(&mask);
159         sigaddset(&mask, SIGINT);
160         sigaddset(&mask, SIGTERM);
161         sigprocmask(SIG_UNBLOCK, &mask, NULL);
162
163         fd_ep = epoll_create1(EPOLL_CLOEXEC);
164         if (fd_ep < 0) {
165                 log_error("error creating epoll fd: %m\n");
166                 goto out;
167         }
168
169         printf("monitor will print the received events for:\n");
170         if (print_udev) {
171                 struct udev_list_entry *entry;
172
173                 udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
174                 if (udev_monitor == NULL) {
175                         fprintf(stderr, "error: unable to create netlink socket\n");
176                         rc = 1;
177                         goto out;
178                 }
179                 udev_monitor_set_receive_buffer_size(udev_monitor, 128*1024*1024);
180                 fd_udev = udev_monitor_get_fd(udev_monitor);
181
182                 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
183                         const char *subsys = udev_list_entry_get_name(entry);
184                         const char *devtype = udev_list_entry_get_value(entry);
185
186                         if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, subsys, devtype) < 0)
187                                 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
188                 }
189
190                 udev_list_entry_foreach(entry, udev_list_get_entry(&tag_match_list)) {
191                         const char *tag = udev_list_entry_get_name(entry);
192
193                         if (udev_monitor_filter_add_match_tag(udev_monitor, tag) < 0)
194                                 fprintf(stderr, "error: unable to apply tag filter '%s'\n", tag);
195                 }
196
197                 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
198                         fprintf(stderr, "error: unable to subscribe to udev events\n");
199                         rc = 2;
200                         goto out;
201                 }
202
203                 memset(&ep_udev, 0, sizeof(struct epoll_event));
204                 ep_udev.events = EPOLLIN;
205                 ep_udev.data.fd = fd_udev;
206                 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) {
207                         log_error("fail to add fd to epoll: %m\n");
208                         goto out;
209                 }
210
211                 printf("UDEV - the event which udev sends out after rule processing\n");
212         }
213
214         if (print_kernel) {
215                 struct udev_list_entry *entry;
216
217                 kernel_monitor = udev_monitor_new_from_netlink(udev, "kernel");
218                 if (kernel_monitor == NULL) {
219                         fprintf(stderr, "error: unable to create netlink socket\n");
220                         rc = 3;
221                         goto out;
222                 }
223                 udev_monitor_set_receive_buffer_size(kernel_monitor, 128*1024*1024);
224                 fd_kernel = udev_monitor_get_fd(kernel_monitor);
225
226                 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
227                         const char *subsys = udev_list_entry_get_name(entry);
228
229                         if (udev_monitor_filter_add_match_subsystem_devtype(kernel_monitor, subsys, NULL) < 0)
230                                 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
231                 }
232
233                 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
234                         fprintf(stderr, "error: unable to subscribe to kernel events\n");
235                         rc = 4;
236                         goto out;
237                 }
238
239                 memset(&ep_kernel, 0, sizeof(struct epoll_event));
240                 ep_kernel.events = EPOLLIN;
241                 ep_kernel.data.fd = fd_kernel;
242                 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_kernel, &ep_kernel) < 0) {
243                         log_error("fail to add fd to epoll: %m\n");
244                         goto out;
245                 }
246
247                 printf("KERNEL - the kernel uevent\n");
248         }
249         printf("\n");
250
251         while (!udev_exit) {
252                 int fdcount;
253                 struct epoll_event ev[4];
254                 int i;
255
256                 fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
257                 if (fdcount < 0) {
258                         if (errno != EINTR)
259                                 fprintf(stderr, "error receiving uevent message: %m\n");
260                         continue;
261                 }
262
263                 for (i = 0; i < fdcount; i++) {
264                         if (ev[i].data.fd == fd_kernel && ev[i].events & EPOLLIN) {
265                                 struct udev_device *device;
266
267                                 device = udev_monitor_receive_device(kernel_monitor);
268                                 if (device == NULL)
269                                         continue;
270                                 print_device(device, "KERNEL", prop);
271                                 udev_device_unref(device);
272                         } else if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) {
273                                 struct udev_device *device;
274
275                                 device = udev_monitor_receive_device(udev_monitor);
276                                 if (device == NULL)
277                                         continue;
278                                 print_device(device, "UDEV", prop);
279                                 udev_device_unref(device);
280                         }
281                 }
282         }
283 out:
284         if (fd_ep >= 0)
285                 close(fd_ep);
286         udev_monitor_unref(udev_monitor);
287         udev_monitor_unref(kernel_monitor);
288         udev_list_cleanup(&subsystem_match_list);
289         udev_list_cleanup(&tag_match_list);
290         return rc;
291 }
292
293 const struct udevadm_cmd udevadm_monitor = {
294         .name = "monitor",
295         .cmd = adm_monitor,
296         .help = "listen to kernel and udev events",
297 };