chiark / gitweb /
Also merge into the top-level Makefile.am the simpler extras.
[elogind.git] / udev / udevadm-monitor.c
1 /*
2  * Copyright (C) 2004-2009 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         int option;
71         int prop = 0;
72         int print_kernel = 0;
73         int print_udev = 0;
74         struct udev_list_node subsystem_match_list;
75         struct udev_monitor *udev_monitor = NULL;
76         struct udev_monitor *kernel_monitor = NULL;
77         fd_set readfds;
78         int rc = 0;
79
80         static const struct option options[] = {
81                 { "property", no_argument, NULL, 'p' },
82                 { "environment", no_argument, NULL, 'e' },
83                 { "kernel", no_argument, NULL, 'k' },
84                 { "udev", no_argument, NULL, 'u' },
85                 { "subsystem-match", required_argument, NULL, 's' },
86                 { "help", no_argument, NULL, 'h' },
87                 {}
88         };
89
90         udev_list_init(&subsystem_match_list);
91         while (1) {
92                 option = getopt_long(argc, argv, "epkus:h", options, NULL);
93                 if (option == -1)
94                         break;
95
96                 switch (option) {
97                 case 'p':
98                 case 'e':
99                         prop = 1;
100                         break;
101                 case 'k':
102                         print_kernel = 1;
103                         break;
104                 case 'u':
105                         print_udev = 1;
106                         break;
107                 case 's':
108                         {
109                                 char subsys[UTIL_NAME_SIZE];
110                                 char *devtype;
111
112                                 util_strscpy(subsys, sizeof(subsys), optarg);
113                                 devtype = strchr(subsys, ':');
114                                 if (devtype != NULL) {
115                                         devtype[0] = '\0';
116                                         devtype++;
117                                 }
118                                 udev_list_entry_add(udev, &subsystem_match_list, subsys, devtype, 0, 0);
119                                 break;
120                         }
121                 case 'h':
122                         printf("Usage: udevadm monitor [--property] [--kernel] [--udev] [--help]\n"
123                                "  --property                    print the event properties\n"
124                                "  --kernel                      print kernel uevents\n"
125                                "  --udev                        print udev events\n"
126                                "  --subsystem-match=<subsystem> filter events\n"
127                                "  --help\n\n");
128                 default:
129                         goto out;
130                 }
131         }
132
133         if (!print_kernel && !print_udev) {
134                 print_kernel = 1;
135                 print_udev =1;
136         }
137
138         /* set signal handlers */
139         memset(&act, 0x00, sizeof(struct sigaction));
140         act.sa_handler = sig_handler;
141         sigemptyset(&act.sa_mask);
142         act.sa_flags = SA_RESTART;
143         sigaction(SIGINT, &act, NULL);
144         sigaction(SIGTERM, &act, NULL);
145
146         printf("monitor will print the received events for:\n");
147         if (print_udev) {
148                 struct udev_list_entry *entry;
149
150                 udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
151                 if (udev_monitor == NULL) {
152                         fprintf(stderr, "error: unable to create netlink socket\n");
153                         rc = 1;
154                         goto out;
155                 }
156
157                 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
158                         const char *subsys = udev_list_entry_get_name(entry);
159                         const char *devtype = udev_list_entry_get_value(entry);
160
161                         if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, subsys, devtype) < 0)
162                                 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
163                 }
164
165                 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
166                         fprintf(stderr, "error: unable to subscribe to udev events\n");
167                         rc = 2;
168                         goto out;
169                 }
170                 printf("UDEV - the event which udev sends out after rule processing\n");
171         }
172         if (print_kernel) {
173                 struct udev_list_entry *entry;
174
175                 kernel_monitor = udev_monitor_new_from_netlink(udev, "kernel");
176                 if (kernel_monitor == NULL) {
177                         fprintf(stderr, "error: unable to create netlink socket\n");
178                         rc = 3;
179                         goto out;
180                 }
181
182                 udev_list_entry_foreach(entry, udev_list_get_entry(&subsystem_match_list)) {
183                         const char *subsys = udev_list_entry_get_name(entry);
184
185                         if (udev_monitor_filter_add_match_subsystem_devtype(kernel_monitor, subsys, NULL) < 0)
186                                 fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
187                 }
188
189                 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
190                         fprintf(stderr, "error: unable to subscribe to kernel events\n");
191                         rc = 4;
192                         goto out;
193                 }
194                 printf("KERNEL - the kernel uevent\n");
195         }
196         printf("\n");
197
198         while (!udev_exit) {
199                 int fdcount;
200
201                 FD_ZERO(&readfds);
202                 if (kernel_monitor != NULL)
203                         FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds);
204                 if (udev_monitor != NULL)
205                         FD_SET(udev_monitor_get_fd(udev_monitor), &readfds);
206
207                 fdcount = select(MAX(udev_monitor_get_fd(kernel_monitor), udev_monitor_get_fd(udev_monitor))+1,
208                                  &readfds, NULL, NULL, NULL);
209                 if (fdcount < 0) {
210                         if (errno != EINTR)
211                                 fprintf(stderr, "error receiving uevent message: %m\n");
212                         continue;
213                 }
214
215                 if ((kernel_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) {
216                         struct udev_device *device;
217
218                         device = udev_monitor_receive_device(kernel_monitor);
219                         if (device == NULL)
220                                 continue;
221                         print_device(device, "KERNEL", prop);
222                         udev_device_unref(device);
223                 }
224
225                 if ((udev_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(udev_monitor), &readfds)) {
226                         struct udev_device *device;
227
228                         device = udev_monitor_receive_device(udev_monitor);
229                         if (device == NULL)
230                                 continue;
231                         print_device(device, "UDEV", prop);
232                         udev_device_unref(device);
233                 }
234         }
235
236 out:
237         udev_monitor_unref(udev_monitor);
238         udev_monitor_unref(kernel_monitor);
239         udev_list_cleanup_entries(udev, &subsystem_match_list);
240         return rc;
241 }