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