]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udevadm-monitor.c
udevadm,scsi_id: add short options to help strings and to the man page
[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"
1bc33626 36
578cd510 37static bool udev_exit;
1bc33626 38
619b97ff 39static void sig_handler(int signum)
9ae611be 40{
912541b0
KS
41 if (signum == SIGINT || signum == SIGTERM)
42 udev_exit = true;
9ae611be
KS
43}
44
b0d5e0df 45static void print_device(struct udev_device *device, const char *source, int prop)
0de33a61 46{
912541b0
KS
47 struct timespec ts;
48
49 clock_gettime(CLOCK_MONOTONIC, &ts);
50 printf("%-6s[%llu.%06u] %-8s %s (%s)\n",
51 source,
52 (unsigned long long) ts.tv_sec, (unsigned int) ts.tv_nsec/1000,
53 udev_device_get_action(device),
54 udev_device_get_devpath(device),
55 udev_device_get_subsystem(device));
56 if (prop) {
57 struct udev_list_entry *list_entry;
58
59 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device))
60 printf("%s=%s\n",
61 udev_list_entry_get_name(list_entry),
62 udev_list_entry_get_value(list_entry));
63 printf("\n");
64 }
0de33a61
KS
65}
66
7643ac9a
ZJS
67static void help(void) {
68 printf("Usage: udevadm monitor [--property] [--kernel] [--udev] [--help]\n"
69 " -p,--property print the event properties\n"
70 " -k,--kernel print kernel uevents\n"
71 " -u,--udev print udev events\n"
72 " -s,--subsystem-match=SUBSYSTEM[/DEVTYPE] filter events by subsystem\n"
73 " -t,--tag-match=TAG filter events by tag\n"
74 " -h,--help\n\n");
75}
76
1985c76e 77static int adm_monitor(struct udev *udev, int argc, char *argv[])
1bc33626 78{
9d458c09 79 struct sigaction act = {};
912541b0
KS
80 sigset_t mask;
81 int option;
82 bool prop = false;
83 bool print_kernel = false;
84 bool print_udev = false;
85 struct udev_list subsystem_match_list;
86 struct udev_list tag_match_list;
87 struct udev_monitor *udev_monitor = NULL;
88 struct udev_monitor *kernel_monitor = NULL;
89 int fd_ep = -1;
90 int fd_kernel = -1, fd_udev = -1;
91 struct epoll_event ep_kernel, ep_udev;
7643ac9a 92 int rc = 0, c;
912541b0
KS
93
94 static const struct option options[] = {
7643ac9a
ZJS
95 { "property", no_argument, NULL, 'p' },
96 { "environment", no_argument, NULL, 'e' }, /* alias for -p */
97 { "kernel", no_argument, NULL, 'k' },
98 { "udev", no_argument, NULL, 'u' },
912541b0 99 { "subsystem-match", required_argument, NULL, 's' },
7643ac9a
ZJS
100 { "tag-match", required_argument, NULL, 't' },
101 { "help", no_argument, NULL, 'h' },
912541b0
KS
102 {}
103 };
104
105 udev_list_init(udev, &subsystem_match_list, true);
106 udev_list_init(udev, &tag_match_list, true);
107
7643ac9a
ZJS
108 while((c = getopt_long(argc, argv, "pekus:t:h", options, NULL)) >= 0)
109 switch (c) {
912541b0
KS
110 case 'p':
111 case 'e':
112 prop = true;
113 break;
114 case 'k':
115 print_kernel = true;
116 break;
117 case 'u':
118 print_udev = true;
119 break;
120 case 's':
121 {
122 char subsys[UTIL_NAME_SIZE];
123 char *devtype;
124
d5a89d7d 125 strscpy(subsys, sizeof(subsys), optarg);
912541b0
KS
126 devtype = strchr(subsys, '/');
127 if (devtype != NULL) {
128 devtype[0] = '\0';
129 devtype++;
130 }
131 udev_list_entry_add(&subsystem_match_list, subsys, devtype);
132 break;
133 }
134 case 't':
135 udev_list_entry_add(&tag_match_list, optarg, NULL);
136 break;
137 case 'h':
7643ac9a 138 help();
912541b0
KS
139 goto out;
140 default:
141 rc = 1;
142 goto out;
143 }
912541b0
KS
144
145 if (!print_kernel && !print_udev) {
146 print_kernel = true;
147 print_udev = true;
148 }
149
150 /* set signal handlers */
912541b0 151 act.sa_handler = sig_handler;
912541b0
KS
152 act.sa_flags = SA_RESTART;
153 sigaction(SIGINT, &act, NULL);
154 sigaction(SIGTERM, &act, NULL);
155 sigemptyset(&mask);
156 sigaddset(&mask, SIGINT);
157 sigaddset(&mask, SIGTERM);
158 sigprocmask(SIG_UNBLOCK, &mask, NULL);
159
160 fd_ep = epoll_create1(EPOLL_CLOEXEC);
161 if (fd_ep < 0) {
baa30fbc 162 log_error("error creating epoll fd: %m\n");
912541b0
KS
163 goto out;
164 }
165
166 printf("monitor will print the received events for:\n");
167 if (print_udev) {
168 struct udev_list_entry *entry;
169
170 udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
171 if (udev_monitor == NULL) {
172 fprintf(stderr, "error: unable to create netlink socket\n");
173 rc = 1;
174 goto out;
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 rc = 2;
197 goto out;
198 }
199
200 memset(&ep_udev, 0, sizeof(struct epoll_event));
201 ep_udev.events = EPOLLIN;
202 ep_udev.data.fd = fd_udev;
203 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) {
baa30fbc 204 log_error("fail to add fd to epoll: %m\n");
912541b0
KS
205 goto out;
206 }
207
208 printf("UDEV - the event which udev sends out after rule processing\n");
209 }
210
211 if (print_kernel) {
212 struct udev_list_entry *entry;
213
214 kernel_monitor = udev_monitor_new_from_netlink(udev, "kernel");
215 if (kernel_monitor == NULL) {
216 fprintf(stderr, "error: unable to create netlink socket\n");
217 rc = 3;
218 goto out;
219 }
220 udev_monitor_set_receive_buffer_size(kernel_monitor, 128*1024*1024);
221 fd_kernel = udev_monitor_get_fd(kernel_monitor);
222
223 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
224 const char *subsys = udev_list_entry_get_name(entry);
225
226 if (udev_monitor_filter_add_match_subsystem_devtype(kernel_monitor, subsys, NULL) < 0)
227 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
228 }
229
230 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
231 fprintf(stderr, "error: unable to subscribe to kernel events\n");
232 rc = 4;
233 goto out;
234 }
235
236 memset(&ep_kernel, 0, sizeof(struct epoll_event));
237 ep_kernel.events = EPOLLIN;
238 ep_kernel.data.fd = fd_kernel;
239 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_kernel, &ep_kernel) < 0) {
baa30fbc 240 log_error("fail to add fd to epoll: %m\n");
912541b0
KS
241 goto out;
242 }
243
244 printf("KERNEL - the kernel uevent\n");
245 }
246 printf("\n");
247
248 while (!udev_exit) {
249 int fdcount;
250 struct epoll_event ev[4];
251 int i;
252
8fef0ff2 253 fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
912541b0
KS
254 if (fdcount < 0) {
255 if (errno != EINTR)
256 fprintf(stderr, "error receiving uevent message: %m\n");
257 continue;
258 }
259
260 for (i = 0; i < fdcount; i++) {
261 if (ev[i].data.fd == fd_kernel && ev[i].events & EPOLLIN) {
262 struct udev_device *device;
263
264 device = udev_monitor_receive_device(kernel_monitor);
265 if (device == NULL)
266 continue;
267 print_device(device, "KERNEL", prop);
268 udev_device_unref(device);
269 } else if (ev[i].data.fd == fd_udev && ev[i].events & EPOLLIN) {
270 struct udev_device *device;
271
272 device = udev_monitor_receive_device(udev_monitor);
273 if (device == NULL)
274 continue;
275 print_device(device, "UDEV", prop);
276 udev_device_unref(device);
277 }
278 }
279 }
40caaeec 280out:
912541b0
KS
281 if (fd_ep >= 0)
282 close(fd_ep);
283 udev_monitor_unref(udev_monitor);
284 udev_monitor_unref(kernel_monitor);
285 udev_list_cleanup(&subsystem_match_list);
286 udev_list_cleanup(&tag_match_list);
287 return rc;
1bc33626 288}
1985c76e
KS
289
290const struct udevadm_cmd udevadm_monitor = {
912541b0
KS
291 .name = "monitor",
292 .cmd = adm_monitor,
293 .help = "listen to kernel and udev events",
1985c76e 294};