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