chiark / gitweb /
29f819f43971840a043ad5f52426b7f5423e744f
[elogind.git] / udev / udevadm-monitor.c
1 /*
2  * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
3  *
4  *      This program is free software; you can redistribute it and/or modify it
5  *      under the terms of the GNU General Public License as published by the
6  *      Free Software Foundation version 2 of the License.
7  * 
8  *      This program is distributed in the hope that it will be useful, but
9  *      WITHOUT ANY WARRANTY; without even the implied warranty of
10  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  *      General Public License for more details.
12  * 
13  *      You should have received a copy of the GNU General Public License along
14  *      with this program; if not, write to the Free Software Foundation, Inc.,
15  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16  *
17  */
18
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <signal.h>
27 #include <getopt.h>
28 #include <sys/time.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include <sys/select.h>
32 #include <linux/types.h>
33 #include <linux/netlink.h>
34
35 #include "udev.h"
36
37 static int udev_exit;
38
39 static void asmlinkage sig_handler(int signum)
40 {
41         if (signum == SIGINT || signum == SIGTERM)
42                 udev_exit = 1;
43 }
44
45 static int print_properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data)
46 {
47         printf("%s=%s\n", key, value);
48         return 0;
49 }
50
51 static void print_properties(struct udev_device *device)
52 {
53         udev_device_get_properties(device, print_properties_cb, NULL);
54         printf("\n");
55 }
56
57 int udevadm_monitor(struct udev *udev, int argc, char *argv[])
58 {
59         struct sigaction act;
60         int option;
61         int env = 0;
62         int print_kernel = 0;
63         int print_udev = 0;
64         struct udev_monitor *udev_monitor = NULL;
65         struct udev_monitor *kernel_monitor = NULL;
66         fd_set readfds;
67         int rc = 0;
68
69         static const struct option options[] = {
70                 { "environment", 0, NULL, 'e' },
71                 { "kernel", 0, NULL, 'k' },
72                 { "udev", 0, NULL, 'u' },
73                 { "help", 0, NULL, 'h' },
74                 {}
75         };
76
77         while (1) {
78                 option = getopt_long(argc, argv, "ekuh", options, NULL);
79                 if (option == -1)
80                         break;
81
82                 switch (option) {
83                 case 'e':
84                         env = 1;
85                         break;
86                 case 'k':
87                         print_kernel = 1;
88                         break;
89                 case 'u':
90                         print_udev = 1;
91                         break;
92                 case 'h':
93                         printf("Usage: udevadm monitor [--environment] [--kernel] [--udev] [--help]\n"
94                                "  --env    print the whole event environment\n"
95                                "  --kernel print kernel uevents\n"
96                                "  --udev   print udev events\n"
97                                "  --help   print this help text\n\n");
98                 default:
99                         goto out;
100                 }
101         }
102
103         if (!print_kernel && !print_udev) {
104                 print_kernel = 1;
105                 print_udev =1;
106         }
107
108         if (getuid() != 0 && print_kernel) {
109                 fprintf(stderr, "root privileges needed to subscribe to kernel events\n");
110                 goto out;
111         }
112
113         /* set signal handlers */
114         memset(&act, 0x00, sizeof(struct sigaction));
115         act.sa_handler = (void (*)(int)) sig_handler;
116         sigemptyset(&act.sa_mask);
117         act.sa_flags = SA_RESTART;
118         sigaction(SIGINT, &act, NULL);
119         sigaction(SIGTERM, &act, NULL);
120
121         printf("monitor will print the received events for:\n");
122         if (print_udev) {
123                 udev_monitor = udev_monitor_new_from_socket(udev, "@/org/kernel/udev/monitor");
124                 if (udev_monitor == NULL) {
125                         rc = 1;
126                         goto out;
127                 }
128                 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
129                         rc = 2;
130                         goto out;
131                 }
132                 printf("UDEV the event which udev sends out after rule processing\n");
133         }
134         if (print_kernel) {
135                 kernel_monitor = udev_monitor_new_from_netlink(udev);
136                 if (kernel_monitor == NULL) {
137                         rc = 3;
138                         goto out;
139                 }
140                 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
141                         rc = 4;
142                         goto out;
143                 }
144                 printf("UEVENT the kernel uevent\n");
145         }
146         printf("\n");
147
148         while (!udev_exit) {
149                 int fdcount;
150                 struct timeval tv;
151                 struct timezone tz;
152                 char timestr[64];
153
154                 FD_ZERO(&readfds);
155                 if (kernel_monitor != NULL)
156                         FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds);
157                 if (udev_monitor != NULL)
158                         FD_SET(udev_monitor_get_fd(udev_monitor), &readfds);
159
160                 fdcount = select(UDEV_MAX(udev_monitor_get_fd(kernel_monitor), udev_monitor_get_fd(udev_monitor))+1,
161                                  &readfds, NULL, NULL, NULL);
162                 if (fdcount < 0) {
163                         if (errno != EINTR)
164                                 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
165                         continue;
166                 }
167
168                 if (gettimeofday(&tv, &tz) == 0) {
169                         snprintf(timestr, sizeof(timestr), "%llu.%06u",
170                                  (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec);
171                 } else
172                         timestr[0] = '\0';
173
174                 if ((kernel_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) {
175                         struct udev_device *device = udev_monitor_receive_device(kernel_monitor);
176                         if (device == NULL)
177                                 continue;
178                         printf("UEVENT[%s] %-8s %s (%s)\n", timestr,
179                                udev_device_get_action(device),
180                                udev_device_get_devpath(device),
181                                udev_device_get_subsystem(device));
182                         if (env)
183                                 print_properties(device);
184                         udev_device_unref(device);
185                 }
186
187                 if ((udev_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(udev_monitor), &readfds)) {
188                         struct udev_device *device = udev_monitor_receive_device(udev_monitor);
189                         if (device == NULL)
190                                 continue;
191                         printf("UDEV  [%s] %-8s %s (%s)\n", timestr,
192                                udev_device_get_action(device),
193                                udev_device_get_devpath(device),
194                                udev_device_get_subsystem(device));
195                         if (env)
196                                 print_properties(device);
197                         udev_device_unref(device);
198                 }
199         }
200
201 out:
202         udev_monitor_unref(udev_monitor);
203         udev_monitor_unref(kernel_monitor);
204         return rc;
205 }