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