chiark / gitweb /
replace strerror() usage with threadsafe "%m" format string
[elogind.git] / udev / udevadm-monitor.c
1 /*
2  * Copyright (C) 2004-2008 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 asmlinkage 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 env)
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 (env) {
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 env = 0;
72         int print_kernel = 0;
73         int print_udev = 0;
74         struct udev_monitor *udev_monitor = NULL;
75         struct udev_monitor *kernel_monitor = NULL;
76         fd_set readfds;
77         int rc = 0;
78
79         static const struct option options[] = {
80                 { "environment", 0, NULL, 'e' },
81                 { "kernel", 0, NULL, 'k' },
82                 { "udev", 0, NULL, 'u' },
83                 { "help", 0, NULL, 'h' },
84                 {}
85         };
86
87         while (1) {
88                 option = getopt_long(argc, argv, "ekuh", options, NULL);
89                 if (option == -1)
90                         break;
91
92                 switch (option) {
93                 case 'e':
94                         env = 1;
95                         break;
96                 case 'k':
97                         print_kernel = 1;
98                         break;
99                 case 'u':
100                         print_udev = 1;
101                         break;
102                 case 'h':
103                         printf("Usage: udevadm monitor [--environment] [--kernel] [--udev] [--help]\n"
104                                "  --env    print the whole event environment\n"
105                                "  --kernel print kernel uevents\n"
106                                "  --udev   print udev events\n"
107                                "  --help   print this help text\n\n");
108                 default:
109                         goto out;
110                 }
111         }
112
113         if (!print_kernel && !print_udev) {
114                 print_kernel = 1;
115                 print_udev =1;
116         }
117
118         if (getuid() != 0 && print_kernel) {
119                 fprintf(stderr, "root privileges needed to subscribe to kernel events\n");
120                 goto out;
121         }
122
123         /* set signal handlers */
124         memset(&act, 0x00, sizeof(struct sigaction));
125         act.sa_handler = (void (*)(int)) sig_handler;
126         sigemptyset(&act.sa_mask);
127         act.sa_flags = SA_RESTART;
128         sigaction(SIGINT, &act, NULL);
129         sigaction(SIGTERM, &act, NULL);
130
131         printf("monitor will print the received events for:\n");
132         if (print_udev) {
133                 udev_monitor = udev_monitor_new_from_socket(udev, "@/org/kernel/udev/monitor");
134                 if (udev_monitor == NULL) {
135                         rc = 1;
136                         goto out;
137                 }
138                 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
139                         rc = 2;
140                         goto out;
141                 }
142                 printf("UDEV the event which udev sends out after rule processing\n");
143         }
144         if (print_kernel) {
145                 kernel_monitor = udev_monitor_new_from_netlink(udev);
146                 if (kernel_monitor == NULL) {
147                         rc = 3;
148                         goto out;
149                 }
150                 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
151                         rc = 4;
152                         goto out;
153                 }
154                 printf("UEVENT the kernel uevent\n");
155         }
156         printf("\n");
157
158         while (!udev_exit) {
159                 int fdcount;
160
161                 FD_ZERO(&readfds);
162                 if (kernel_monitor != NULL)
163                         FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds);
164                 if (udev_monitor != NULL)
165                         FD_SET(udev_monitor_get_fd(udev_monitor), &readfds);
166
167                 fdcount = select(UDEV_MAX(udev_monitor_get_fd(kernel_monitor), udev_monitor_get_fd(udev_monitor))+1,
168                                  &readfds, NULL, NULL, NULL);
169                 if (fdcount < 0) {
170                         if (errno != EINTR)
171                                 fprintf(stderr, "error receiving uevent message: %m\n");
172                         continue;
173                 }
174
175                 if ((kernel_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) {
176                         struct udev_device *device;
177
178                         device = udev_monitor_receive_device(kernel_monitor);
179                         if (device == NULL)
180                                 continue;
181                         print_device(device, "UEVENT", env);
182                         udev_device_unref(device);
183                 }
184
185                 if ((udev_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(udev_monitor), &readfds)) {
186                         struct udev_device *device;
187
188                         device = udev_monitor_receive_device(udev_monitor);
189                         if (device == NULL)
190                                 continue;
191                         print_device(device, "UDEV", env);
192                         udev_device_unref(device);
193                 }
194         }
195
196 out:
197         udev_monitor_unref(udev_monitor);
198         udev_monitor_unref(kernel_monitor);
199         return rc;
200 }