]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-monitor.c
Merge pull request #3012 from martinpitt/hwdb
[thirdparty/systemd.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 <errno.h>
19 #include <getopt.h>
20 #include <signal.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/epoll.h>
25 #include <sys/time.h>
26 #include <time.h>
27
28 #include "fd-util.h"
29 #include "formats-util.h"
30 #include "udev-util.h"
31 #include "udev.h"
32
33 static bool udev_exit;
34
35 static void sig_handler(int signum) {
36 if (signum == SIGINT || signum == SIGTERM)
37 udev_exit = true;
38 }
39
40 static void print_device(struct udev_device *device, const char *source, int prop) {
41 struct timespec ts;
42
43 assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
44 printf("%-6s[%"PRI_TIME".%06ld] %-8s %s (%s)\n",
45 source,
46 ts.tv_sec, ts.tv_nsec/1000,
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 }
59 }
60
61 static void help(void) {
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);
72 }
73
74 static int adm_monitor(struct udev *udev, int argc, char *argv[]) {
75 struct sigaction act = {};
76 sigset_t mask;
77 bool prop = false;
78 bool print_kernel = false;
79 bool print_udev = false;
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;
85 int fd_kernel = -1, fd_udev = -1;
86 struct epoll_event ep_kernel, ep_udev;
87 int c;
88
89 static const struct option options[] = {
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' },
94 { "subsystem-match", required_argument, NULL, 's' },
95 { "tag-match", required_argument, NULL, 't' },
96 { "help", no_argument, NULL, 'h' },
97 {}
98 };
99
100 udev_list_init(udev, &subsystem_match_list, true);
101 udev_list_init(udev, &tag_match_list, true);
102
103 while ((c = getopt_long(argc, argv, "pekus:t:h", options, NULL)) >= 0)
104 switch (c) {
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
120 strscpy(subsys, sizeof(subsys), optarg);
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':
133 help();
134 return 0;
135 default:
136 return 1;
137 }
138
139 if (!print_kernel && !print_udev) {
140 print_kernel = true;
141 print_udev = true;
142 }
143
144 /* set signal handlers */
145 act.sa_handler = sig_handler;
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) {
156 log_error_errno(errno, "error creating epoll fd: %m");
157 return 1;
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");
167 return 1;
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");
189 return 2;
190 }
191
192 memzero(&ep_udev, sizeof(struct epoll_event));
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) {
196 log_error_errno(errno, "fail to add fd to epoll: %m");
197 return 2;
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");
209 return 3;
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");
223 return 4;
224 }
225
226 memzero(&ep_kernel, sizeof(struct epoll_event));
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) {
230 log_error_errno(errno, "fail to add fd to epoll: %m");
231 return 5;
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
243 fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
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 }
270
271 return 0;
272 }
273
274 const struct udevadm_cmd udevadm_monitor = {
275 .name = "monitor",
276 .cmd = adm_monitor,
277 .help = "Listen to kernel and udev events",
278 };