]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevmonitor.c
fold multiple consecutive whitespace chars into single '_'
[thirdparty/systemd.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 #include "udev_utils.h"
38 #include "udev_libc_wrapper.h"
39
40 static int uevent_netlink_sock;
41 static int udev_monitor_sock;
42 static volatile int udev_exit;
43
44 static int init_udev_monitor_socket(void)
45 {
46 struct sockaddr_un saddr;
47 socklen_t addrlen;
48 const int feature_on = 1;
49 int retval;
50
51 memset(&saddr, 0x00, sizeof(saddr));
52 saddr.sun_family = AF_LOCAL;
53 /* use abstract namespace for socket path */
54 strcpy(&saddr.sun_path[1], "/org/kernel/udev/monitor");
55 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
56
57 udev_monitor_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
58 if (udev_monitor_sock == -1) {
59 fprintf(stderr, "error getting socket: %s\n", strerror(errno));
60 return -1;
61 }
62
63 /* the bind takes care of ensuring only one copy running */
64 retval = bind(udev_monitor_sock, (struct sockaddr *) &saddr, addrlen);
65 if (retval < 0) {
66 fprintf(stderr, "bind failed: %s\n", strerror(errno));
67 close(udev_monitor_sock);
68 udev_monitor_sock = -1;
69 return -1;
70 }
71
72 /* enable receiving of the sender credentials */
73 setsockopt(udev_monitor_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
74
75 return 0;
76 }
77
78 static int init_uevent_netlink_sock(void)
79 {
80 struct sockaddr_nl snl;
81 int retval;
82
83 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
84 snl.nl_family = AF_NETLINK;
85 snl.nl_pid = getpid();
86 snl.nl_groups = 0xffffffff;
87
88 uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
89 if (uevent_netlink_sock == -1) {
90 fprintf(stderr, "error getting socket: %s\n", strerror(errno));
91 return -1;
92 }
93
94 retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl,
95 sizeof(struct sockaddr_nl));
96 if (retval < 0) {
97 fprintf(stderr, "bind failed: %s\n", strerror(errno));
98 close(uevent_netlink_sock);
99 uevent_netlink_sock = -1;
100 return -1;
101 }
102
103 return 0;
104 }
105
106 static void asmlinkage sig_handler(int signum)
107 {
108 if (signum == SIGINT || signum == SIGTERM)
109 udev_exit = 1;
110 }
111
112 int main(int argc, char *argv[])
113 {
114 struct sigaction act;
115 int env = 0;
116 fd_set readfds;
117 int i;
118 int retval = 0;
119
120 for (i = 1 ; i < argc; i++) {
121 char *arg = argv[i];
122 if (strcmp(arg, "--env") == 0 || strcmp(arg, "-e") == 0) {
123 env = 1;
124 }
125 else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0){
126 printf("Usage: udevmonitor [--env]\n"
127 " --env print the whole event environment\n"
128 " --help print this help text\n\n");
129 exit(0);
130 } else {
131 fprintf(stderr, "unknown option\n\n");
132 exit(1);
133 }
134 }
135
136 if (getuid() != 0) {
137 fprintf(stderr, "need to be root, exit\n\n");
138 exit(2);
139 }
140
141 /* set signal handlers */
142 memset(&act, 0x00, sizeof(struct sigaction));
143 act.sa_handler = (void (*)(int)) sig_handler;
144 sigemptyset(&act.sa_mask);
145 act.sa_flags = SA_RESTART;
146 sigaction(SIGINT, &act, NULL);
147 sigaction(SIGTERM, &act, NULL);
148
149 retval = init_udev_monitor_socket();
150 if (retval)
151 goto out;
152 retval = init_uevent_netlink_sock();
153 if (retval)
154 goto out;
155
156 printf("udevmonitor prints the received event from the kernel [UEVENT]\n"
157 "and the event which udev sends out after rule processing [UDEV]\n\n");
158
159 while (!udev_exit) {
160 static char buf[UEVENT_BUFFER_SIZE*2];
161 ssize_t buflen;
162 int fdcount;
163 struct timeval tv;
164 struct timezone tz;
165 char timestr[64];
166
167 buflen = 0;
168 FD_ZERO(&readfds);
169 if (uevent_netlink_sock > 0)
170 FD_SET(uevent_netlink_sock, &readfds);
171 if (udev_monitor_sock > 0)
172 FD_SET(udev_monitor_sock, &readfds);
173
174 fdcount = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_sock)+1, &readfds, NULL, NULL, NULL);
175 if (fdcount < 0) {
176 if (errno != EINTR)
177 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
178 continue;
179 }
180
181 if (gettimeofday(&tv, &tz) == 0) {
182 snprintf(timestr, sizeof(timestr), "%llu.%06u",
183 (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec);
184 } else
185 timestr[0] = '\0';
186
187 if ((uevent_netlink_sock > 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
188 buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
189 if (buflen <= 0) {
190 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
191 continue;
192 }
193 printf("UEVENT[%s] %s\n", timestr, buf);
194 }
195
196 if ((udev_monitor_sock > 0) && FD_ISSET(udev_monitor_sock, &readfds)) {
197 buflen = recv(udev_monitor_sock, &buf, sizeof(buf), 0);
198 if (buflen <= 0) {
199 fprintf(stderr, "error receiving udev message: %s\n", strerror(errno));
200 continue;
201 }
202 printf("UDEV [%s] %s\n", timestr, buf);
203 }
204
205 if (buflen == 0)
206 continue;
207
208 /* print environment */
209 if (env) {
210 size_t bufpos;
211
212 /* start of payload */
213 bufpos = strlen(buf) + 1;
214
215 while (bufpos < (size_t)buflen) {
216 int keylen;
217 char *key;
218
219 key = &buf[bufpos];
220 keylen = strlen(key);
221 if (keylen == 0)
222 break;
223 printf("%s\n", key);
224 bufpos += keylen + 1;
225 }
226 printf("\n");
227 }
228 }
229
230 out:
231 if (uevent_netlink_sock > 0)
232 close(uevent_netlink_sock);
233 if (udev_monitor_sock > 0)
234 close(udev_monitor_sock);
235
236 if (retval)
237 return 3;
238 return 0;
239 }