chiark / gitweb /
libudev: udev_device_get_devname -> udev_device_get_devnode
[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 int print_properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data)
45 {
46         printf("%s=%s\n", key, value);
47         return 0;
48 }
49
50 static void print_properties(struct udev_device *device)
51 {
52         udev_device_get_properties(device, print_properties_cb, NULL);
53         printf("\n");
54 }
55
56 int udevadm_monitor(struct udev *udev, int argc, char *argv[])
57 {
58         struct sigaction act;
59         int option;
60         int env = 0;
61         int print_kernel = 0;
62         int print_udev = 0;
63         struct udev_monitor *udev_monitor = NULL;
64         struct udev_monitor *kernel_monitor = NULL;
65         fd_set readfds;
66         int rc = 0;
67
68         static const struct option options[] = {
69                 { "environment", 0, NULL, 'e' },
70                 { "kernel", 0, NULL, 'k' },
71                 { "udev", 0, NULL, 'u' },
72                 { "help", 0, NULL, 'h' },
73                 {}
74         };
75
76         while (1) {
77                 option = getopt_long(argc, argv, "ekuh", options, NULL);
78                 if (option == -1)
79                         break;
80
81                 switch (option) {
82                 case 'e':
83                         env = 1;
84                         break;
85                 case 'k':
86                         print_kernel = 1;
87                         break;
88                 case 'u':
89                         print_udev = 1;
90                         break;
91                 case 'h':
92                         printf("Usage: udevadm monitor [--environment] [--kernel] [--udev] [--help]\n"
93                                "  --env    print the whole event environment\n"
94                                "  --kernel print kernel uevents\n"
95                                "  --udev   print udev events\n"
96                                "  --help   print this help text\n\n");
97                 default:
98                         goto out;
99                 }
100         }
101
102         if (!print_kernel && !print_udev) {
103                 print_kernel = 1;
104                 print_udev =1;
105         }
106
107         if (getuid() != 0 && print_kernel) {
108                 fprintf(stderr, "root privileges needed to subscribe to kernel events\n");
109                 goto out;
110         }
111
112         /* set signal handlers */
113         memset(&act, 0x00, sizeof(struct sigaction));
114         act.sa_handler = (void (*)(int)) sig_handler;
115         sigemptyset(&act.sa_mask);
116         act.sa_flags = SA_RESTART;
117         sigaction(SIGINT, &act, NULL);
118         sigaction(SIGTERM, &act, NULL);
119
120         printf("monitor will print the received events for:\n");
121         if (print_udev) {
122                 udev_monitor = udev_monitor_new_from_socket(udev, "@/org/kernel/udev/monitor");
123                 if (udev_monitor == NULL) {
124                         rc = 1;
125                         goto out;
126                 }
127                 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
128                         rc = 2;
129                         goto out;
130                 }
131                 printf("UDEV the event which udev sends out after rule processing\n");
132         }
133         if (print_kernel) {
134                 kernel_monitor = udev_monitor_new_from_netlink(udev);
135                 if (kernel_monitor == NULL) {
136                         rc = 3;
137                         goto out;
138                 }
139                 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
140                         rc = 4;
141                         goto out;
142                 }
143                 printf("UEVENT the kernel uevent\n");
144         }
145         printf("\n");
146
147         while (!udev_exit) {
148                 int fdcount;
149                 struct timeval tv;
150                 struct timezone tz;
151                 char timestr[64];
152
153                 FD_ZERO(&readfds);
154                 if (kernel_monitor != NULL)
155                         FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds);
156                 if (udev_monitor != NULL)
157                         FD_SET(udev_monitor_get_fd(udev_monitor), &readfds);
158
159                 fdcount = select(UDEV_MAX(udev_monitor_get_fd(kernel_monitor), udev_monitor_get_fd(udev_monitor))+1,
160                                  &readfds, NULL, NULL, NULL);
161                 if (fdcount < 0) {
162                         if (errno != EINTR)
163                                 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
164                         continue;
165                 }
166
167                 if (gettimeofday(&tv, &tz) == 0) {
168                         snprintf(timestr, sizeof(timestr), "%llu.%06u",
169                                  (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec);
170                 } else
171                         timestr[0] = '\0';
172
173                 if ((kernel_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) {
174                         struct udev_device *device = udev_monitor_receive_device(kernel_monitor);
175                         if (device == NULL)
176                                 continue;
177                         printf("UEVENT[%s] %-8s %s (%s)\n", timestr,
178                                udev_device_get_action(device),
179                                udev_device_get_devpath(device),
180                                udev_device_get_subsystem(device));
181                         if (env)
182                                 print_properties(device);
183                         udev_device_unref(device);
184                 }
185
186                 if ((udev_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(udev_monitor), &readfds)) {
187                         struct udev_device *device = udev_monitor_receive_device(udev_monitor);
188                         if (device == NULL)
189                                 continue;
190                         printf("UDEV  [%s] %-8s %s (%s)\n", timestr,
191                                udev_device_get_action(device),
192                                udev_device_get_devpath(device),
193                                udev_device_get_subsystem(device));
194                         if (env)
195                                 print_properties(device);
196                         udev_device_unref(device);
197                 }
198         }
199
200 out:
201         udev_monitor_unref(udev_monitor);
202         udev_monitor_unref(kernel_monitor);
203         return rc;
204 }