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