chiark / gitweb /
85252bb7d98e1bd61f10b4e6809d47053688d127
[elogind.git] / udev / udevadm-monitor.c
1 /*
2  * Copyright (C) 2004-2010 Kay Sievers <kay.sievers@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 <sys/time.h>
28 #include <sys/socket.h>
29 #include <sys/un.h>
30 #include <sys/select.h>
31 #include <linux/types.h>
32 #include <linux/netlink.h>
33
34 #include "udev.h"
35
36 static int udev_exit;
37
38 static void sig_handler(int signum)
39 {
40         if (signum == SIGINT || signum == SIGTERM)
41                 udev_exit = 1;
42 }
43
44 static void print_device(struct udev_device *device, const char *source, int prop)
45 {
46         struct timeval tv;
47         struct timezone tz;
48
49         gettimeofday(&tv, &tz);
50         printf("%-6s[%llu.%06u] %-8s %s (%s)\n",
51                source,
52                (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec,
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         }
65 }
66
67 int udevadm_monitor(struct udev *udev, int argc, char *argv[])
68 {
69         struct sigaction act;
70         sigset_t mask;
71         int option;
72         int prop = 0;
73         int print_kernel = 0;
74         int print_udev = 0;
75         struct udev_list_node subsystem_match_list;
76         struct udev_list_node tag_match_list;
77         struct udev_monitor *udev_monitor = NULL;
78         struct udev_monitor *kernel_monitor = NULL;
79         fd_set readfds;
80         int rc = 0;
81
82         static const struct option options[] = {
83                 { "property", no_argument, NULL, 'p' },
84                 { "environment", no_argument, NULL, 'e' },
85                 { "kernel", no_argument, NULL, 'k' },
86                 { "udev", no_argument, NULL, 'u' },
87                 { "subsystem-match", required_argument, NULL, 's' },
88                 { "tag-match", required_argument, NULL, 't' },
89                 { "help", no_argument, NULL, 'h' },
90                 {}
91         };
92
93         udev_list_init(&subsystem_match_list);
94         udev_list_init(&tag_match_list);
95         for (;;) {
96                 option = getopt_long(argc, argv, "pekus:t:h", options, NULL);
97                 if (option == -1)
98                         break;
99
100                 switch (option) {
101                 case 'p':
102                 case 'e':
103                         prop = 1;
104                         break;
105                 case 'k':
106                         print_kernel = 1;
107                         break;
108                 case 'u':
109                         print_udev = 1;
110                         break;
111                 case 's':
112                         {
113                                 char subsys[UTIL_NAME_SIZE];
114                                 char *devtype;
115
116                                 util_strscpy(subsys, sizeof(subsys), optarg);
117                                 devtype = strchr(subsys, '/');
118                                 if (devtype != NULL) {
119                                         devtype[0] = '\0';
120                                         devtype++;
121                                 }
122                                 udev_list_entry_add(udev, &subsystem_match_list, subsys, devtype, 0, 0);
123                                 break;
124                         }
125                 case 't':
126                         udev_list_entry_add(udev, &tag_match_list, optarg, NULL, 0, 0);
127                         break;
128                 case 'h':
129                         printf("Usage: udevadm monitor [--property] [--kernel] [--udev] [--help]\n"
130                                "  --property                              print the event properties\n"
131                                "  --kernel                                print kernel uevents\n"
132                                "  --udev                                  print udev events\n"
133                                "  --subsystem-match=<subsystem[/devtype]> filter events by subsystem\n"
134                                "  --tag-match=<tag>                       filter events by tag\n"
135                                "  --help\n\n");
136                 default:
137                         goto out;
138                 }
139         }
140
141         if (!print_kernel && !print_udev) {
142                 print_kernel = 1;
143                 print_udev =1;
144         }
145
146         /* set signal handlers */
147         memset(&act, 0x00, sizeof(struct sigaction));
148         act.sa_handler = sig_handler;
149         sigemptyset(&act.sa_mask);
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         printf("monitor will print the received events for:\n");
159         if (print_udev) {
160                 struct udev_list_entry *entry;
161
162                 udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
163                 if (udev_monitor == NULL) {
164                         fprintf(stderr, "error: unable to create netlink socket\n");
165                         rc = 1;
166                         goto out;
167                 }
168                 udev_monitor_set_receive_buffer_size(udev_monitor, 128*1024*1024);
169
170                 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
171                         const char *subsys = udev_list_entry_get_name(entry);
172                         const char *devtype = udev_list_entry_get_value(entry);
173
174                         if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, subsys, devtype) < 0)
175                                 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
176                 }
177
178                 udev_list_entry_foreach(entry, udev_list_get_entry(&tag_match_list)) {
179                         const char *tag = udev_list_entry_get_name(entry);
180
181                         if (udev_monitor_filter_add_match_tag(udev_monitor, tag) < 0)
182                                 fprintf(stderr, "error: unable to apply tag filter '%s'\n", tag);
183                 }
184
185                 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
186                         fprintf(stderr, "error: unable to subscribe to udev events\n");
187                         rc = 2;
188                         goto out;
189                 }
190                 printf("UDEV - the event which udev sends out after rule processing\n");
191         }
192         if (print_kernel) {
193                 struct udev_list_entry *entry;
194
195                 kernel_monitor = udev_monitor_new_from_netlink(udev, "kernel");
196                 if (kernel_monitor == NULL) {
197                         fprintf(stderr, "error: unable to create netlink socket\n");
198                         rc = 3;
199                         goto out;
200                 }
201                 udev_monitor_set_receive_buffer_size(kernel_monitor, 128*1024*1024);
202
203                 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
204                         const char *subsys = udev_list_entry_get_name(entry);
205
206                         if (udev_monitor_filter_add_match_subsystem_devtype(kernel_monitor, subsys, NULL) < 0)
207                                 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
208                 }
209
210                 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
211                         fprintf(stderr, "error: unable to subscribe to kernel events\n");
212                         rc = 4;
213                         goto out;
214                 }
215                 printf("KERNEL - the kernel uevent\n");
216         }
217         printf("\n");
218
219         while (!udev_exit) {
220                 int fdcount;
221
222                 FD_ZERO(&readfds);
223                 if (kernel_monitor != NULL)
224                         FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds);
225                 if (udev_monitor != NULL)
226                         FD_SET(udev_monitor_get_fd(udev_monitor), &readfds);
227
228                 fdcount = select(MAX(udev_monitor_get_fd(kernel_monitor), udev_monitor_get_fd(udev_monitor))+1,
229                                  &readfds, NULL, NULL, NULL);
230                 if (fdcount < 0) {
231                         if (errno != EINTR)
232                                 fprintf(stderr, "error receiving uevent message: %m\n");
233                         continue;
234                 }
235
236                 if ((kernel_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) {
237                         struct udev_device *device;
238
239                         device = udev_monitor_receive_device(kernel_monitor);
240                         if (device == NULL)
241                                 continue;
242                         print_device(device, "KERNEL", prop);
243                         udev_device_unref(device);
244                 }
245
246                 if ((udev_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(udev_monitor), &readfds)) {
247                         struct udev_device *device;
248
249                         device = udev_monitor_receive_device(udev_monitor);
250                         if (device == NULL)
251                                 continue;
252                         print_device(device, "UDEV", prop);
253                         udev_device_unref(device);
254                 }
255         }
256
257 out:
258         udev_monitor_unref(udev_monitor);
259         udev_monitor_unref(kernel_monitor);
260         udev_list_cleanup_entries(udev, &subsystem_match_list);
261         udev_list_cleanup_entries(udev, &tag_match_list);
262         return rc;
263 }