chiark / gitweb /
udev: remove seqnum API and all assumptions about seqnums
[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 #include "udev-util.h"
37
38 static bool udev_exit;
39
40 static void sig_handler(int signum)
41 {
42         if (signum == SIGINT || signum == SIGTERM)
43                 udev_exit = true;
44 }
45
46 static void print_device(struct udev_device *device, const char *source, int prop)
47 {
48         struct timespec ts;
49
50         clock_gettime(CLOCK_MONOTONIC, &ts);
51         printf("%-6s[%llu.%06u] %-8s %s (%s)\n",
52                source,
53                (unsigned long long) ts.tv_sec, (unsigned int) ts.tv_nsec/1000,
54                udev_device_get_action(device),
55                udev_device_get_devpath(device),
56                udev_device_get_subsystem(device));
57         if (prop) {
58                 struct udev_list_entry *list_entry;
59
60                 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device))
61                         printf("%s=%s\n",
62                                udev_list_entry_get_name(list_entry),
63                                udev_list_entry_get_value(list_entry));
64                 printf("\n");
65         }
66 }
67
68 static void help(void) {
69         printf("Usage: udevadm monitor [--property] [--kernel] [--udev] [--help]\n"
70                "  -p,--property                            print the event properties\n"
71                "  -k,--kernel                              print kernel uevents\n"
72                "  -u,--udev                                print udev events\n"
73                "  -s,--subsystem-match=SUBSYSTEM[/DEVTYPE] filter events by subsystem\n"
74                "  -t,--tag-match=TAG                       filter events by tag\n"
75                "  -h,--help\n\n");
76 }
77
78 static int adm_monitor(struct udev *udev, int argc, char *argv[])
79 {
80         struct sigaction act = {};
81         sigset_t mask;
82         bool prop = false;
83         bool print_kernel = false;
84         bool print_udev = false;
85         _cleanup_udev_list_cleanup_ struct udev_list subsystem_match_list;
86         _cleanup_udev_list_cleanup_ struct udev_list tag_match_list;
87         _cleanup_udev_monitor_unref_ struct udev_monitor *udev_monitor = NULL;
88         _cleanup_udev_monitor_unref_ struct udev_monitor *kernel_monitor = NULL;
89         _cleanup_close_ int fd_ep = -1;
90         int fd_kernel = -1, fd_udev = -1;
91         struct epoll_event ep_kernel, ep_udev;
92         int c;
93
94         static const struct option options[] = {
95                 { "property",        no_argument,       NULL, 'p' },
96                 { "environment",     no_argument,       NULL, 'e' }, /* alias for -p */
97                 { "kernel",          no_argument,       NULL, 'k' },
98                 { "udev",            no_argument,       NULL, 'u' },
99                 { "subsystem-match", required_argument, NULL, 's' },
100                 { "tag-match",       required_argument, NULL, 't' },
101                 { "help",            no_argument,       NULL, 'h' },
102                 {}
103         };
104
105         udev_list_init(udev, &subsystem_match_list, true);
106         udev_list_init(udev, &tag_match_list, true);
107
108         while((c = getopt_long(argc, argv, "pekus:t:h", options, NULL)) >= 0)
109                 switch (c) {
110                 case 'p':
111                 case 'e':
112                         prop = true;
113                         break;
114                 case 'k':
115                         print_kernel = true;
116                         break;
117                 case 'u':
118                         print_udev = true;
119                         break;
120                 case 's':
121                         {
122                                 char subsys[UTIL_NAME_SIZE];
123                                 char *devtype;
124
125                                 strscpy(subsys, sizeof(subsys), optarg);
126                                 devtype = strchr(subsys, '/');
127                                 if (devtype != NULL) {
128                                         devtype[0] = '\0';
129                                         devtype++;
130                                 }
131                                 udev_list_entry_add(&subsystem_match_list, subsys, devtype);
132                                 break;
133                         }
134                 case 't':
135                         udev_list_entry_add(&tag_match_list, optarg, NULL);
136                         break;
137                 case 'h':
138                         help();
139                         return 0;
140                 default:
141                         return 1;
142                 }
143
144         if (!print_kernel && !print_udev) {
145                 print_kernel = true;
146                 print_udev = true;
147         }
148
149         /* set signal handlers */
150         act.sa_handler = sig_handler;
151         act.sa_flags = SA_RESTART;
152         sigaction(SIGINT, &act, NULL);
153         sigaction(SIGTERM, &act, NULL);
154         sigemptyset(&mask);
155         sigaddset(&mask, SIGINT);
156         sigaddset(&mask, SIGTERM);
157         sigprocmask(SIG_UNBLOCK, &mask, NULL);
158
159         fd_ep = epoll_create1(EPOLL_CLOEXEC);
160         if (fd_ep < 0) {
161                 log_error("error creating epoll fd: %m");
162                 return 1;
163         }
164
165         printf("monitor will print the received events for:\n");
166         if (print_udev) {
167                 struct udev_list_entry *entry;
168
169                 udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
170                 if (udev_monitor == NULL) {
171                         fprintf(stderr, "error: unable to create netlink socket\n");
172                         return 1;
173                 }
174                 udev_monitor_set_receive_buffer_size(udev_monitor, 128*1024*1024);
175                 fd_udev = udev_monitor_get_fd(udev_monitor);
176
177                 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
178                         const char *subsys = udev_list_entry_get_name(entry);
179                         const char *devtype = udev_list_entry_get_value(entry);
180
181                         if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, subsys, devtype) < 0)
182                                 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
183                 }
184
185                 udev_list_entry_foreach(entry, udev_list_get_entry(&tag_match_list)) {
186                         const char *tag = udev_list_entry_get_name(entry);
187
188                         if (udev_monitor_filter_add_match_tag(udev_monitor, tag) < 0)
189                                 fprintf(stderr, "error: unable to apply tag filter '%s'\n", tag);
190                 }
191
192                 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
193                         fprintf(stderr, "error: unable to subscribe to udev events\n");
194                         return 2;
195                 }
196
197                 memzero(&ep_udev, sizeof(struct epoll_event));
198                 ep_udev.events = EPOLLIN;
199                 ep_udev.data.fd = fd_udev;
200                 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) {
201                         log_error("fail to add fd to epoll: %m");
202                         return 2;
203                 }
204
205                 printf("UDEV - the event which udev sends out after rule processing\n");
206         }
207
208         if (print_kernel) {
209                 struct udev_list_entry *entry;
210
211                 kernel_monitor = udev_monitor_new_from_netlink(udev, "kernel");
212                 if (kernel_monitor == NULL) {
213                         fprintf(stderr, "error: unable to create netlink socket\n");
214                         return 3;
215                 }
216                 udev_monitor_set_receive_buffer_size(kernel_monitor, 128*1024*1024);
217                 fd_kernel = udev_monitor_get_fd(kernel_monitor);
218
219                 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
220                         const char *subsys = udev_list_entry_get_name(entry);
221
222                         if (udev_monitor_filter_add_match_subsystem_devtype(kernel_monitor, subsys, NULL) < 0)
223                                 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
224                 }
225
226                 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
227                         fprintf(stderr, "error: unable to subscribe to kernel events\n");
228                         return 4;
229                 }
230
231                 memzero(&ep_kernel, sizeof(struct epoll_event));
232                 ep_kernel.events = EPOLLIN;
233                 ep_kernel.data.fd = fd_kernel;
234                 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_kernel, &ep_kernel) < 0) {
235                         log_error("fail to add fd to epoll: %m");
236                         return 5;
237                 }
238
239                 printf("KERNEL - the kernel uevent\n");
240         }
241         printf("\n");
242
243         while (!udev_exit) {
244                 int fdcount;
245                 struct epoll_event ev[4];
246                 int i;
247
248                 fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
249                 if (fdcount < 0) {
250                         if (errno != EINTR)
251                                 fprintf(stderr, "error receiving uevent message: %m\n");
252                         continue;
253                 }
254
255                 for (i = 0; i < fdcount; i++) {
256                         if (ev[i].data.fd == fd_kernel && ev[i].events & EPOLLIN) {
257                                 struct udev_device *device;
258
259                                 device = udev_monitor_receive_device(kernel_monitor);
260                                 if (device == NULL)
261                                         continue;
262                                 print_device(device, "KERNEL", prop);
263                                 udev_device_unref(device);
264                         } else if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) {
265                                 struct udev_device *device;
266
267                                 device = udev_monitor_receive_device(udev_monitor);
268                                 if (device == NULL)
269                                         continue;
270                                 print_device(device, "UDEV", prop);
271                                 udev_device_unref(device);
272                         }
273                 }
274         }
275
276         return 0;
277 }
278
279 const struct udevadm_cmd udevadm_monitor = {
280         .name = "monitor",
281         .cmd = adm_monitor,
282         .help = "listen to kernel and udev events",
283 };