chiark / gitweb /
volume_id: provide a custom debug function
[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 <errno.h>
27 #include <signal.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;
39 static int udev_monitor_sock;
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         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 static void asmlinkage sig_handler(int signum)
105 {
106         if (signum == SIGINT || signum == SIGTERM)
107                 udev_exit = 1;
108 }
109
110 int main(int argc, char *argv[])
111 {
112         struct sigaction act;
113         int env = 0;
114         fd_set readfds;
115         int i;
116         int retval = 0;
117
118         for (i = 1 ; i < argc; i++) {
119                 char *arg = argv[i];
120                 if (strcmp(arg, "--env") == 0 || strcmp(arg, "-e") == 0) {
121                         env = 1;
122                 }
123                 else if (strcmp(arg, "--help") == 0  || strcmp(arg, "-h") == 0){
124                         printf("Usage: udevmonitor [--env]\n"
125                                 "  --env    print the whole event environment\n"
126                                 "  --help   print this help text\n\n");
127                         exit(0);
128                 } else {
129                         fprintf(stderr, "unknown option\n\n");
130                         exit(1);
131                 }
132         }
133
134         if (getuid() != 0) {
135                 fprintf(stderr, "need to be root, exit\n\n");
136                 exit(2);
137         }
138
139         /* set signal handlers */
140         memset(&act, 0x00, sizeof(struct sigaction));
141         act.sa_handler = (void (*)(int)) sig_handler;
142         sigemptyset(&act.sa_mask);
143         act.sa_flags = SA_RESTART;
144         sigaction(SIGINT, &act, NULL);
145         sigaction(SIGTERM, &act, NULL);
146
147         retval = init_udev_monitor_socket();
148         if (retval)
149                 goto out;
150         retval = init_uevent_netlink_sock();
151         if (retval)
152                 goto out;
153
154         printf("udevmonitor prints the received event from the kernel [UEVENT]\n"
155                "and the event which udev sends out after rule processing [UDEV]\n\n");
156
157         while (!udev_exit) {
158                 static char buf[UEVENT_BUFFER_SIZE*2];
159                 ssize_t buflen;
160                 int fdcount;
161                 struct timeval tv;
162                 struct timezone tz;
163                 char timestr[64];
164
165                 buflen = 0;
166                 FD_ZERO(&readfds);
167                 if (uevent_netlink_sock > 0)
168                         FD_SET(uevent_netlink_sock, &readfds);
169                 if (udev_monitor_sock > 0)
170                         FD_SET(udev_monitor_sock, &readfds);
171
172                 fdcount = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_sock)+1, &readfds, NULL, NULL, NULL);
173                 if (fdcount < 0) {
174                         if (errno != EINTR)
175                                 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
176                         continue;
177                 }
178
179                 if (gettimeofday(&tv, &tz) == 0) {
180                         snprintf(timestr, sizeof(timestr), "%llu.%06u",
181                                  (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec);
182                 } else
183                         timestr[0] = '\0';
184
185                 if ((uevent_netlink_sock > 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
186                         buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
187                         if (buflen <=  0) {
188                                 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
189                                 continue;
190                         }
191                         printf("UEVENT[%s] %s\n", timestr, buf);
192                 }
193
194                 if ((udev_monitor_sock > 0) && FD_ISSET(udev_monitor_sock, &readfds)) {
195                         buflen = recv(udev_monitor_sock, &buf, sizeof(buf), 0);
196                         if (buflen <=  0) {
197                                 fprintf(stderr, "error receiving udev message: %s\n", strerror(errno));
198                                 continue;
199                         }
200                         printf("UDEV  [%s] %s\n", timestr, buf);
201                 }
202
203                 if (buflen == 0)
204                         continue;
205
206                 /* print environment */
207                 if (env) {
208                         size_t bufpos;
209
210                         /* start of payload */
211                         bufpos = strlen(buf) + 1;
212
213                         while (bufpos < (size_t)buflen) {
214                                 int keylen;
215                                 char *key;
216
217                                 key = &buf[bufpos];
218                                 keylen = strlen(key);
219                                 if (keylen == 0)
220                                         break;
221                                 printf("%s\n", key);
222                                 bufpos += keylen + 1;
223                         }
224                         printf("\n");
225                 }
226         }
227
228 out:
229         if (uevent_netlink_sock > 0)
230                 close(uevent_netlink_sock);
231         if (udev_monitor_sock > 0)
232                 close(udev_monitor_sock);
233
234         if (retval)
235                 return 3;
236         return 0;
237 }