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