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