2 * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
28 #include <sys/socket.h>
30 #include <sys/select.h>
31 #include <linux/types.h>
32 #include <linux/netlink.h>
37 static int uevent_netlink_sock = -1;
38 static int udev_monitor_sock = -1;
39 static volatile int udev_exit;
41 static int init_udev_monitor_socket(void)
43 struct sockaddr_un saddr;
45 const int feature_on = 1;
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;
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));
60 /* the bind takes care of ensuring only one copy running */
61 retval = bind(udev_monitor_sock, (struct sockaddr *) &saddr, addrlen);
63 fprintf(stderr, "bind failed: %s\n", strerror(errno));
64 close(udev_monitor_sock);
65 udev_monitor_sock = -1;
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();
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 static void asmlinkage sig_handler(int signum)
105 if (signum == SIGINT || signum == SIGTERM)
109 static const char *search_key(const char *searchkey, const char *buf, size_t buflen)
112 size_t searchkeylen = strlen(searchkey);
114 while (bufpos < buflen) {
119 keylen = strlen(key);
122 if ((strncmp(searchkey, key, searchkeylen) == 0) && key[searchkeylen] == '=')
123 return &key[searchkeylen + 1];
124 bufpos += keylen + 1;
129 int main(int argc, char *argv[])
131 struct sigaction act;
137 for (i = 1 ; i < argc; i++) {
139 if (strcmp(arg, "--env") == 0 || strcmp(arg, "-e") == 0)
141 else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0){
142 printf("Usage: udevmonitor [--help] [--env]\n"
143 " --env print the whole event environment\n"
144 " --help print this help text\n\n");
147 fprintf(stderr, "unrecognized option '%s'\n", arg);
153 fprintf(stderr, "root privileges required\n");
157 /* set signal handlers */
158 memset(&act, 0x00, sizeof(struct sigaction));
159 act.sa_handler = (void (*)(int)) sig_handler;
160 sigemptyset(&act.sa_mask);
161 act.sa_flags = SA_RESTART;
162 sigaction(SIGINT, &act, NULL);
163 sigaction(SIGTERM, &act, NULL);
165 retval = init_udev_monitor_socket();
169 retval = init_uevent_netlink_sock();
173 printf("udevmonitor prints the received event from the kernel [UEVENT]\n"
174 "and the event which udev sends out after rule processing [UDEV]\n\n");
177 char buf[UEVENT_BUFFER_SIZE*2];
185 const char *source = NULL;
186 const char *devpath, *action, *subsys;
190 if (uevent_netlink_sock >= 0)
191 FD_SET(uevent_netlink_sock, &readfds);
192 if (udev_monitor_sock >= 0)
193 FD_SET(udev_monitor_sock, &readfds);
195 fdcount = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_sock)+1, &readfds, NULL, NULL, NULL);
198 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
202 if (gettimeofday(&tv, &tz) == 0) {
203 snprintf(timestr, sizeof(timestr), "%llu.%06u",
204 (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec);
208 if ((uevent_netlink_sock >= 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
209 buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
211 fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
217 if ((udev_monitor_sock >= 0) && FD_ISSET(udev_monitor_sock, &readfds)) {
218 buflen = recv(udev_monitor_sock, &buf, sizeof(buf), 0);
220 fprintf(stderr, "error receiving udev message: %s\n", strerror(errno));
229 keys = strlen(buf) + 1; /* start of payload */
230 devpath = search_key("DEVPATH", &buf[keys], buflen);
231 action = search_key("ACTION", &buf[keys], buflen);
232 subsys = search_key("SUBSYSTEM", &buf[keys], buflen);
233 printf("%s[%s] %-8s %s (%s)\n", source, timestr, action, devpath, subsys);
235 /* print environment */
238 while (bufpos < buflen) {
243 keylen = strlen(key);
247 bufpos += keylen + 1;
254 if (uevent_netlink_sock >= 0)
255 close(uevent_netlink_sock);
256 if (udev_monitor_sock >= 0)
257 close(udev_monitor_sock);