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