chiark / gitweb /
add (subsystem) to udevmonitor output
[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 <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 #include "udevd.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         const int feature_on = 1;
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) + strlen(saddr.sun_path+1) + 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         /* enable receiving of the sender credentials */
70         setsockopt(udev_monitor_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
71
72         return 0;
73 }
74
75 static int init_uevent_netlink_sock(void)
76 {
77         struct sockaddr_nl snl;
78         int retval;
79
80         memset(&snl, 0x00, sizeof(struct sockaddr_nl));
81         snl.nl_family = AF_NETLINK;
82         snl.nl_pid = getpid();
83         snl.nl_groups = 1;
84
85         uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
86         if (uevent_netlink_sock == -1) {
87                 fprintf(stderr, "error getting socket: %s\n", strerror(errno));
88                 return -1;
89         }
90
91         retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl,
92                       sizeof(struct sockaddr_nl));
93         if (retval < 0) {
94                 fprintf(stderr, "bind failed: %s\n", strerror(errno));
95                 close(uevent_netlink_sock);
96                 uevent_netlink_sock = -1;
97                 return -1;
98         }
99
100         return 0;
101 }
102
103 static void asmlinkage sig_handler(int signum)
104 {
105         if (signum == SIGINT || signum == SIGTERM)
106                 udev_exit = 1;
107 }
108
109 static const char *search_key(const char *searchkey, const char *buf, size_t buflen)
110 {
111         size_t bufpos = 0;
112         size_t searchkeylen = strlen(searchkey);
113
114         while (bufpos < buflen) {
115                 const char *key;
116                 int keylen;
117
118                 key = &buf[bufpos];
119                 keylen = strlen(key);
120                 if (keylen == 0)
121                         break;
122                  if ((strncmp(searchkey, key, searchkeylen) == 0) && key[searchkeylen] == '=')
123                         return &key[searchkeylen + 1];
124                 bufpos += keylen + 1;
125         }
126         return NULL;
127 }
128
129 int main(int argc, char *argv[])
130 {
131         struct sigaction act;
132         int env = 0;
133         fd_set readfds;
134         int i;
135         int retval = 0;
136
137         for (i = 1 ; i < argc; i++) {
138                 char *arg = argv[i];
139                 if (strcmp(arg, "--env") == 0 || strcmp(arg, "-e") == 0)
140                         env = 1;
141                 else if (strcmp(arg, "--help") == 0  || strcmp(arg, "-h") == 0){
142                         printf("Usage: udevmonitor [--help] [--env]\n"
143                                 "  --env    print the whole event environment\n"
144                                 "  --help   print this help text\n\n");
145                         exit(0);
146                 } else {
147                         fprintf(stderr, "unrecognized option '%s'\n", arg);
148                         exit(1);
149                 }
150         }
151
152         if (getuid() != 0) {
153                 fprintf(stderr, "root privileges required\n");
154                 exit(2);
155         }
156
157         /* set signal handlers */
158         memset(&act, 0x00, sizeof(struct sigaction));
159         act.sa_handler = (void (*)(int)) sig_handler;
160         sigemptyset(&act.sa_mask);
161         act.sa_flags = SA_RESTART;
162         sigaction(SIGINT, &act, NULL);
163         sigaction(SIGTERM, &act, NULL);
164
165         retval = init_udev_monitor_socket();
166         if (retval)
167                 goto out;
168
169         retval = init_uevent_netlink_sock();
170         if (retval)
171                 goto out;
172
173         printf("udevmonitor prints the received event from the kernel [UEVENT]\n"
174                "and the event which udev sends out after rule processing [UDEV]\n\n");
175
176         while (!udev_exit) {
177                 char buf[UEVENT_BUFFER_SIZE*2];
178                 ssize_t buflen;
179                 ssize_t bufpos;
180                 ssize_t keys;
181                 int fdcount;
182                 struct timeval tv;
183                 struct timezone tz;
184                 char timestr[64];
185                 const char *source = NULL;
186                 const char *devpath, *action, *subsys;
187
188                 buflen = 0;
189                 FD_ZERO(&readfds);
190                 if (uevent_netlink_sock >= 0)
191                         FD_SET(uevent_netlink_sock, &readfds);
192                 if (udev_monitor_sock >= 0)
193                         FD_SET(udev_monitor_sock, &readfds);
194
195                 fdcount = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_sock)+1, &readfds, NULL, NULL, NULL);
196                 if (fdcount < 0) {
197                         if (errno != EINTR)
198                                 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
199                         continue;
200                 }
201
202                 if (gettimeofday(&tv, &tz) == 0) {
203                         snprintf(timestr, sizeof(timestr), "%llu.%06u",
204                                  (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec);
205                 } else
206                         timestr[0] = '\0';
207
208                 if ((uevent_netlink_sock >= 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
209                         buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
210                         if (buflen <= 0) {
211                                 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
212                                 continue;
213                         }
214                         source = "UEVENT";
215                 }
216
217                 if ((udev_monitor_sock >= 0) && FD_ISSET(udev_monitor_sock, &readfds)) {
218                         buflen = recv(udev_monitor_sock, &buf, sizeof(buf), 0);
219                         if (buflen <= 0) {
220                                 fprintf(stderr, "error receiving udev message: %s\n", strerror(errno));
221                                 continue;
222                         }
223                         source = "UDEV  ";
224                 }
225
226                 if (buflen == 0)
227                         continue;
228
229                 keys = strlen(buf) + 1; /* start of payload */
230                 devpath = search_key("DEVPATH", &buf[keys], buflen);
231                 action = search_key("ACTION", &buf[keys], buflen);
232                 subsys = search_key("SUBSYSTEM", &buf[keys], buflen);
233                 printf("%s[%s] %-8s %s (%s)\n", source, timestr, action, devpath, subsys);
234
235                 /* print environment */
236                 bufpos = keys;
237                 if (env) {
238                         while (bufpos < buflen) {
239                                 int keylen;
240                                 char *key;
241
242                                 key = &buf[bufpos];
243                                 keylen = strlen(key);
244                                 if (keylen == 0)
245                                         break;
246                                 printf("%s\n", key);
247                                 bufpos += keylen + 1;
248                         }
249                         printf("\n");
250                 }
251         }
252
253 out:
254         if (uevent_netlink_sock >= 0)
255                 close(uevent_netlink_sock);
256         if (udev_monitor_sock >= 0)
257                 close(udev_monitor_sock);
258
259         if (retval)
260                 return 3;
261         return 0;
262 }