chiark / gitweb /
add udevmonitor, to debug netlink+udev events at the same time
[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/netlink.h>
32 #include <asm/types.h>
33
34 #include "udev.h"
35 #include "udevd.h"
36
37 static int uevent_netlink_sock;
38 static int udev_monitor_sock;
39
40 static int init_udev_monitor_socket(void)
41 {
42         struct sockaddr_un saddr;
43         socklen_t addrlen;
44         const int feature_on = 1;
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) + strlen(saddr.sun_path+1) + 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                 return -1;
65         }
66
67         /* enable receiving of the sender credentials */
68         setsockopt(udev_monitor_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
69
70         return 0;
71 }
72
73 static int init_uevent_netlink_sock(void)
74 {
75         struct sockaddr_nl snl;
76         int retval;
77
78         memset(&snl, 0x00, sizeof(struct sockaddr_nl));
79         snl.nl_family = AF_NETLINK;
80         snl.nl_pid = getpid();
81         snl.nl_groups = 0xffffffff;
82
83         uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
84         if (uevent_netlink_sock == -1) {
85                 fprintf(stderr, "error getting socket, %s\n", strerror(errno));
86                 return -1;
87         }
88
89         retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl,
90                       sizeof(struct sockaddr_nl));
91         if (retval < 0) {
92                 fprintf(stderr, "bind failed, %s\n", strerror(errno));
93                 close(uevent_netlink_sock);
94                 uevent_netlink_sock = -1;
95                 return -1;
96         }
97
98         return 0;
99 }
100
101 int main(int argc, char *argv[])
102 {
103         int env = 0;
104         int maxsockplus;
105         fd_set readfds;
106         int retval;
107
108         if (getuid() != 0) {
109                 printf("need to be root, exit\n");
110                 exit(1);
111         }
112
113         if (argc == 2 && strstr(argv[1], "--env"))
114                 env = 1;
115
116         init_uevent_netlink_sock();
117         init_udev_monitor_socket();
118
119         FD_ZERO(&readfds);
120         FD_SET(uevent_netlink_sock, &readfds);
121         FD_SET(udev_monitor_sock, &readfds);
122         maxsockplus = udev_monitor_sock+1;
123
124         while (1) {
125                 static char buf[UEVENT_BUFFER_SIZE*2];
126                 ssize_t buflen;
127                 fd_set workreadfds;
128
129                 buflen = 0;
130                 workreadfds = readfds;
131
132                 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
133                 if (retval < 0) {
134                         if (errno != EINTR)
135                                 fprintf(stderr, "error receiving uevent message\n");
136                         continue;
137                 }
138
139                 if (FD_ISSET(uevent_netlink_sock, &workreadfds)) {
140                         buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
141                         if (buflen <=  0) {
142                                 fprintf(stderr, "error receiving uevent message\n");
143                                 continue;
144                         }
145                         printf("UEVENT[%i] %s\n", time(NULL), buf);
146                 }
147
148                 if (FD_ISSET(udev_monitor_sock, &workreadfds)) {
149                         buflen = recv(udev_monitor_sock, &buf, sizeof(buf), 0);
150                         if (buflen <=  0) {
151                                 fprintf(stderr, "error receiving udev message\n");
152                                 continue;
153                         }
154                         printf("UDEV  [%i] %s\n", time(NULL), buf);
155                 }
156
157                 if (buflen == 0)
158                         continue;
159
160                 /* print environment */
161                 if (env) {
162                         size_t bufpos;
163
164                         /* start of payload */
165                         bufpos = strlen(buf) + 1;
166
167                         while (bufpos < (size_t)buflen) {
168                                 int keylen;
169                                 char *key;
170
171                                 key = &buf[bufpos];
172                                 keylen = strlen(key);
173                                 if (keylen == 0)
174                                         break;
175                                 printf("%s\n", key);
176                                 bufpos += keylen + 1;
177                         }
178                         printf("\n");
179                 }
180         }
181
182         close(uevent_netlink_sock);
183         close(udev_monitor_sock);
184         exit(1);
185 }