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