chiark / gitweb /
9fe6da906f986ab81895efb67e332d7087a62a16
[elogind.git] / udevmonitor.c
1 /*
2  * udevmonitor.c
3  *
4  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <time.h>
27 #include <errno.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 #include "udev_utils.h"
37 #include "udev_libc_wrapper.h"
38
39 static int uevent_netlink_sock;
40 static int udev_monitor_sock;
41
42 static int init_udev_monitor_socket(void)
43 {
44         struct sockaddr_un saddr;
45         socklen_t addrlen;
46         const int feature_on = 1;
47         int retval;
48
49         memset(&saddr, 0x00, sizeof(saddr));
50         saddr.sun_family = AF_LOCAL;
51         /* use abstract namespace for socket path */
52         strcpy(&saddr.sun_path[1], "/org/kernel/udev/monitor");
53         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
54
55         udev_monitor_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
56         if (udev_monitor_sock == -1) {
57                 fprintf(stderr, "error getting socket, %s\n", strerror(errno));
58                 return -1;
59         }
60
61         /* the bind takes care of ensuring only one copy running */
62         retval = bind(udev_monitor_sock, (struct sockaddr *) &saddr, addrlen);
63         if (retval < 0) {
64                 fprintf(stderr, "bind failed, %s\n", strerror(errno));
65                 close(udev_monitor_sock);
66                 udev_monitor_sock = -1;
67                 return -1;
68         }
69
70         /* enable receiving of the sender credentials */
71         setsockopt(udev_monitor_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
72
73         return 0;
74 }
75
76 static int init_uevent_netlink_sock(void)
77 {
78         struct sockaddr_nl snl;
79         int retval;
80
81         memset(&snl, 0x00, sizeof(struct sockaddr_nl));
82         snl.nl_family = AF_NETLINK;
83         snl.nl_pid = getpid();
84         snl.nl_groups = 0xffffffff;
85
86         uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
87         if (uevent_netlink_sock == -1) {
88                 fprintf(stderr, "error getting socket, %s\n", strerror(errno));
89                 return -1;
90         }
91
92         retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl,
93                       sizeof(struct sockaddr_nl));
94         if (retval < 0) {
95                 fprintf(stderr, "bind failed, %s\n", strerror(errno));
96                 close(uevent_netlink_sock);
97                 uevent_netlink_sock = -1;
98                 return -1;
99         }
100
101         return 0;
102 }
103
104 int main(int argc, char *argv[])
105 {
106         int env = 0;
107         fd_set readfds;
108         int i;
109         int retval = 0;
110
111         for (i = 1 ; i < argc; i++) {
112                 char *arg = argv[i];
113                 if (strcmp(arg, "--env") == 0 || strcmp(arg, "-e") == 0) {
114                         env = 1;
115                 }
116                 else if (strcmp(arg, "--help") == 0  || strcmp(arg, "-h") == 0){
117                         printf("Usage: udevmonitor [--env]\n"
118                                 "  --env    print the whole event environment\n"
119                                 "  --help   print this help text\n\n");
120                         exit(0);
121                 } else {
122                         fprintf(stderr, "unknown option\n\n");
123                         exit(1);
124                 }
125         }
126
127         if (getuid() != 0) {
128                 fprintf(stderr, "need to be root, exit\n\n");
129                 exit(2);
130         }
131
132         retval = init_udev_monitor_socket();
133         if (retval)
134                 goto out;
135         retval = init_uevent_netlink_sock();
136         if (retval)
137                 goto out;
138
139         printf("udevmonitor prints the received event from the kernel [UEVENT]\n"
140                "and the event which udev sends out after rule processing [UDEV]\n\n");
141
142         while (1) {
143                 static char buf[UEVENT_BUFFER_SIZE*2];
144                 ssize_t buflen;
145                 int fdcount;
146
147                 buflen = 0;
148                 FD_ZERO(&readfds);
149                 if (uevent_netlink_sock > 0)
150                         FD_SET(uevent_netlink_sock, &readfds);
151                 if (udev_monitor_sock > 0)
152                         FD_SET(udev_monitor_sock, &readfds);
153
154                 fdcount = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_sock)+1, &readfds, NULL, NULL, NULL);
155                 if (fdcount < 0) {
156                         if (errno != EINTR)
157                                 fprintf(stderr, "error receiving uevent message\n");
158                         continue;
159                 }
160
161                 if ((uevent_netlink_sock > 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
162                         buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
163                         if (buflen <=  0) {
164                                 fprintf(stderr, "error receiving uevent message\n");
165                                 continue;
166                         }
167                         printf("UEVENT[%i] %s\n", time(NULL), buf);
168                 }
169
170                 if ((udev_monitor_sock > 0) && FD_ISSET(udev_monitor_sock, &readfds)) {
171                         buflen = recv(udev_monitor_sock, &buf, sizeof(buf), 0);
172                         if (buflen <=  0) {
173                                 fprintf(stderr, "error receiving udev message\n");
174                                 continue;
175                         }
176                         printf("UDEV  [%i] %s\n", time(NULL), buf);
177                 }
178
179                 if (buflen == 0)
180                         continue;
181
182                 /* print environment */
183                 if (env) {
184                         size_t bufpos;
185
186                         /* start of payload */
187                         bufpos = strlen(buf) + 1;
188
189                         while (bufpos < (size_t)buflen) {
190                                 int keylen;
191                                 char *key;
192
193                                 key = &buf[bufpos];
194                                 keylen = strlen(key);
195                                 if (keylen == 0)
196                                         break;
197                                 printf("%s\n", key);
198                                 bufpos += keylen + 1;
199                         }
200                         printf("\n");
201                 }
202         }
203
204 out:
205         if (uevent_netlink_sock > 0)
206                 close(uevent_netlink_sock);
207         if (udev_monitor_sock > 0)
208                 close(udev_monitor_sock);
209
210         if (retval)
211                 return 3;
212         return 0;
213 }