4 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
28 #include <sys/socket.h>
30 #include <sys/select.h>
31 #include <linux/types.h>
32 #include <linux/netlink.h>
36 #include "udev_utils.h"
37 #include "udev_libc_wrapper.h"
39 static int uevent_netlink_sock;
40 static int udev_monitor_sock;
42 static int init_udev_monitor_socket(void)
44 struct sockaddr_un saddr;
46 const int feature_on = 1;
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;
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));
61 /* the bind takes care of ensuring only one copy running */
62 retval = bind(udev_monitor_sock, (struct sockaddr *) &saddr, addrlen);
64 fprintf(stderr, "bind failed, %s\n", strerror(errno));
65 close(udev_monitor_sock);
69 /* enable receiving of the sender credentials */
70 setsockopt(udev_monitor_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
75 static int init_uevent_netlink_sock(void)
77 struct sockaddr_nl snl;
80 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
81 snl.nl_family = AF_NETLINK;
82 snl.nl_pid = getpid();
83 snl.nl_groups = 0xffffffff;
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));
91 retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl,
92 sizeof(struct sockaddr_nl));
94 fprintf(stderr, "bind failed, %s\n", strerror(errno));
95 close(uevent_netlink_sock);
96 uevent_netlink_sock = -1;
103 int main(int argc, char *argv[])
110 for (i = 1 ; i < argc; i++) {
112 if (strcmp(arg, "--env") == 0 || strcmp(arg, "-e") == 0) {
115 else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0){
116 printf("Usage: udevmonitor [--env]\n"
117 " --env print the whole event environment\n"
118 " --help print this help text\n\n");
121 fprintf(stderr, "unknown option\n\n");
127 fprintf(stderr, "need to be root, exit\n\n");
131 retval = init_udev_monitor_socket();
134 init_uevent_netlink_sock();
136 printf("udevmonitor prints received from the kernel [UEVENT] and after\n"
137 "the udev processing, the event which udev [UDEV] has generated\n\n");
140 static char buf[UEVENT_BUFFER_SIZE*2];
146 if (uevent_netlink_sock > 0)
147 FD_SET(uevent_netlink_sock, &readfds);
148 if (udev_monitor_sock > 0)
149 FD_SET(udev_monitor_sock, &readfds);
151 fdcount = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_sock)+1, &readfds, NULL, NULL, NULL);
154 fprintf(stderr, "error receiving uevent message\n");
158 if ((uevent_netlink_sock > 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
159 buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
161 fprintf(stderr, "error receiving uevent message\n");
164 printf("UEVENT[%i] %s\n", time(NULL), buf);
167 if ((udev_monitor_sock > 0) && FD_ISSET(udev_monitor_sock, &readfds)) {
168 buflen = recv(udev_monitor_sock, &buf, sizeof(buf), 0);
170 fprintf(stderr, "error receiving udev message\n");
173 printf("UDEV [%i] %s\n", time(NULL), buf);
179 /* print environment */
183 /* start of payload */
184 bufpos = strlen(buf) + 1;
186 while (bufpos < (size_t)buflen) {
191 keylen = strlen(key);
195 bufpos += keylen + 1;
202 if (uevent_netlink_sock > 0)
203 close(uevent_netlink_sock);
204 if (udev_monitor_sock > 0)
205 close(udev_monitor_sock);