chiark / gitweb /
move udev_ctrl to libudev-private
[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 uevent_netlink_sock = -1;
38 static int udev_monitor_sock = -1;
39 static volatile int udev_exit;
40
41 static int init_udev_monitor_socket(void)
42 {
43         struct sockaddr_un saddr;
44         socklen_t addrlen;
45         int retval;
46
47         memset(&saddr, 0x00, sizeof(saddr));
48         saddr.sun_family = AF_LOCAL;
49         /* use abstract namespace for socket path */
50         strcpy(&saddr.sun_path[1], "/org/kernel/udev/monitor");
51         addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
52
53         udev_monitor_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
54         if (udev_monitor_sock == -1) {
55                 fprintf(stderr, "error getting socket: %s\n", strerror(errno));
56                 return -1;
57         }
58
59         /* the bind takes care of ensuring only one copy running */
60         retval = bind(udev_monitor_sock, (struct sockaddr *) &saddr, addrlen);
61         if (retval < 0) {
62                 fprintf(stderr, "bind failed: %s\n", strerror(errno));
63                 close(udev_monitor_sock);
64                 udev_monitor_sock = -1;
65                 return -1;
66         }
67
68         return 0;
69 }
70
71 static int init_uevent_netlink_sock(void)
72 {
73         struct sockaddr_nl snl;
74         int retval;
75
76         memset(&snl, 0x00, sizeof(struct sockaddr_nl));
77         snl.nl_family = AF_NETLINK;
78         snl.nl_pid = getpid();
79         snl.nl_groups = 1;
80
81         uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
82         if (uevent_netlink_sock == -1) {
83                 fprintf(stderr, "error getting socket: %s\n", strerror(errno));
84                 return -1;
85         }
86
87         retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl,
88                       sizeof(struct sockaddr_nl));
89         if (retval < 0) {
90                 fprintf(stderr, "bind failed: %s\n", strerror(errno));
91                 close(uevent_netlink_sock);
92                 uevent_netlink_sock = -1;
93                 return -1;
94         }
95
96         return 0;
97 }
98
99 static void asmlinkage sig_handler(int signum)
100 {
101         if (signum == SIGINT || signum == SIGTERM)
102                 udev_exit = 1;
103 }
104
105 static const char *search_key(const char *searchkey, const char *buf, size_t buflen)
106 {
107         size_t bufpos = 0;
108         size_t searchkeylen = strlen(searchkey);
109
110         while (bufpos < buflen) {
111                 const char *key;
112                 int keylen;
113
114                 key = &buf[bufpos];
115                 keylen = strlen(key);
116                 if (keylen == 0)
117                         break;
118                  if ((strncmp(searchkey, key, searchkeylen) == 0) && key[searchkeylen] == '=')
119                         return &key[searchkeylen + 1];
120                 bufpos += keylen + 1;
121         }
122         return NULL;
123 }
124
125 int udevadm_monitor(struct udev *udev, int argc, char *argv[])
126 {
127         struct sigaction act;
128         int option;
129         int env = 0;
130         int print_kernel = 0;
131         int print_udev = 0;
132         fd_set readfds;
133         int retval = 0;
134
135         static const struct option options[] = {
136                 { "environment", 0, NULL, 'e' },
137                 { "kernel", 0, NULL, 'k' },
138                 { "udev", 0, NULL, 'u' },
139                 { "help", 0, NULL, 'h' },
140                 {}
141         };
142
143         while (1) {
144                 option = getopt_long(argc, argv, "ekuh", options, NULL);
145                 if (option == -1)
146                         break;
147
148                 switch (option) {
149                 case 'e':
150                         env = 1;
151                         break;
152                 case 'k':
153                         print_kernel = 1;
154                         break;
155                 case 'u':
156                         print_udev = 1;
157                         break;
158                 case 'h':
159                         printf("Usage: udevadm monitor [--environment] [--kernel] [--udev] [--help]\n"
160                                "  --env    print the whole event environment\n"
161                                "  --kernel print kernel uevents\n"
162                                "  --udev   print udev events\n"
163                                "  --help   print this help text\n\n");
164                 default:
165                         goto out;
166                 }
167         }
168
169         if (!print_kernel && !print_udev) {
170                 print_kernel = 1;
171                 print_udev =1;
172         }
173
174         if (getuid() != 0 && print_kernel) {
175                 fprintf(stderr, "root privileges needed to subscribe to kernel events\n");
176                 goto out;
177         }
178
179         /* set signal handlers */
180         memset(&act, 0x00, sizeof(struct sigaction));
181         act.sa_handler = (void (*)(int)) sig_handler;
182         sigemptyset(&act.sa_mask);
183         act.sa_flags = SA_RESTART;
184         sigaction(SIGINT, &act, NULL);
185         sigaction(SIGTERM, &act, NULL);
186
187         printf("monitor will print the received events for:\n");
188         if (print_udev) {
189                 retval = init_udev_monitor_socket();
190                 if (retval)
191                         goto out;
192                 printf("UDEV the event which udev sends out after rule processing\n");
193         }
194         if (print_kernel) {
195                 retval = init_uevent_netlink_sock();
196                 if (retval)
197                         goto out;
198                 printf("UEVENT the kernel uevent\n");
199         }
200         printf("\n");
201
202         while (!udev_exit) {
203                 char buf[UEVENT_BUFFER_SIZE*2];
204                 ssize_t buflen;
205                 ssize_t bufpos;
206                 ssize_t keys;
207                 int fdcount;
208                 struct timeval tv;
209                 struct timezone tz;
210                 char timestr[64];
211                 const char *source = NULL;
212                 const char *devpath, *action, *subsys;
213
214                 buflen = 0;
215                 FD_ZERO(&readfds);
216                 if (uevent_netlink_sock >= 0)
217                         FD_SET(uevent_netlink_sock, &readfds);
218                 if (udev_monitor_sock >= 0)
219                         FD_SET(udev_monitor_sock, &readfds);
220
221                 fdcount = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_sock)+1, &readfds, NULL, NULL, NULL);
222                 if (fdcount < 0) {
223                         if (errno != EINTR)
224                                 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
225                         continue;
226                 }
227
228                 if (gettimeofday(&tv, &tz) == 0) {
229                         snprintf(timestr, sizeof(timestr), "%llu.%06u",
230                                  (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec);
231                 } else
232                         timestr[0] = '\0';
233
234                 if ((uevent_netlink_sock >= 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
235                         buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
236                         if (buflen <= 0) {
237                                 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
238                                 continue;
239                         }
240                         source = "UEVENT";
241                 }
242
243                 if ((udev_monitor_sock >= 0) && FD_ISSET(udev_monitor_sock, &readfds)) {
244                         buflen = recv(udev_monitor_sock, &buf, sizeof(buf), 0);
245                         if (buflen <= 0) {
246                                 fprintf(stderr, "error receiving udev message: %s\n", strerror(errno));
247                                 continue;
248                         }
249                         source = "UDEV  ";
250                 }
251
252                 if (buflen == 0)
253                         continue;
254
255                 keys = strlen(buf) + 1; /* start of payload */
256                 devpath = search_key("DEVPATH", &buf[keys], buflen);
257                 action = search_key("ACTION", &buf[keys], buflen);
258                 subsys = search_key("SUBSYSTEM", &buf[keys], buflen);
259                 printf("%s[%s] %-8s %s (%s)\n", source, timestr, action, devpath, subsys);
260
261                 /* print environment */
262                 bufpos = keys;
263                 if (env) {
264                         while (bufpos < buflen) {
265                                 int keylen;
266                                 char *key;
267
268                                 key = &buf[bufpos];
269                                 keylen = strlen(key);
270                                 if (keylen == 0)
271                                         break;
272                                 printf("%s\n", key);
273                                 bufpos += keylen + 1;
274                         }
275                         printf("\n");
276                 }
277         }
278
279 out:
280         if (uevent_netlink_sock >= 0)
281                 close(uevent_netlink_sock);
282         if (udev_monitor_sock >= 0)
283                 close(udev_monitor_sock);
284
285         if (retval)
286                 return 1;
287         return 0;
288 }