chiark / gitweb /
update SUSE rules
[elogind.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 <time.h>
27 #include <errno.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 #include "udev_libc_wrapper.h"
37
38 static int uevent_netlink_sock;
39 static int udev_monitor_sock;
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                 return -1;
66         }
67
68         /* enable receiving of the sender credentials */
69         setsockopt(udev_monitor_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
70
71         return 0;
72 }
73
74 static int init_uevent_netlink_sock(void)
75 {
76         struct sockaddr_nl snl;
77         int retval;
78
79         memset(&snl, 0x00, sizeof(struct sockaddr_nl));
80         snl.nl_family = AF_NETLINK;
81         snl.nl_pid = getpid();
82         snl.nl_groups = 0xffffffff;
83
84         uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
85         if (uevent_netlink_sock == -1) {
86                 fprintf(stderr, "error getting socket, %s\n", strerror(errno));
87                 return -1;
88         }
89
90         retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl,
91                       sizeof(struct sockaddr_nl));
92         if (retval < 0) {
93                 fprintf(stderr, "bind failed, %s\n", strerror(errno));
94                 close(uevent_netlink_sock);
95                 uevent_netlink_sock = -1;
96                 return -1;
97         }
98
99         return 0;
100 }
101
102 int main(int argc, char *argv[])
103 {
104         int env = 0;
105         int maxsockplus;
106         fd_set readfds;
107         int retval;
108
109         if (getuid() != 0) {
110                 printf("need to be root, exit\n");
111                 exit(1);
112         }
113
114         if (argc == 2 && strstr(argv[1], "--env"))
115                 env = 1;
116
117         init_uevent_netlink_sock();
118         init_udev_monitor_socket();
119
120         FD_ZERO(&readfds);
121         FD_SET(uevent_netlink_sock, &readfds);
122         FD_SET(udev_monitor_sock, &readfds);
123         maxsockplus = udev_monitor_sock+1;
124
125         while (1) {
126                 static char buf[UEVENT_BUFFER_SIZE*2];
127                 ssize_t buflen;
128                 fd_set workreadfds;
129
130                 buflen = 0;
131                 workreadfds = readfds;
132
133                 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
134                 if (retval < 0) {
135                         if (errno != EINTR)
136                                 fprintf(stderr, "error receiving uevent message\n");
137                         continue;
138                 }
139
140                 if (FD_ISSET(uevent_netlink_sock, &workreadfds)) {
141                         buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
142                         if (buflen <=  0) {
143                                 fprintf(stderr, "error receiving uevent message\n");
144                                 continue;
145                         }
146                         printf("UEVENT[%i] %s\n", time(NULL), buf);
147                 }
148
149                 if (FD_ISSET(udev_monitor_sock, &workreadfds)) {
150                         buflen = recv(udev_monitor_sock, &buf, sizeof(buf), 0);
151                         if (buflen <=  0) {
152                                 fprintf(stderr, "error receiving udev message\n");
153                                 continue;
154                         }
155                         printf("UDEV  [%i] %s\n", time(NULL), buf);
156                 }
157
158                 if (buflen == 0)
159                         continue;
160
161                 /* print environment */
162                 if (env) {
163                         size_t bufpos;
164
165                         /* start of payload */
166                         bufpos = strlen(buf) + 1;
167
168                         while (bufpos < (size_t)buflen) {
169                                 int keylen;
170                                 char *key;
171
172                                 key = &buf[bufpos];
173                                 keylen = strlen(key);
174                                 if (keylen == 0)
175                                         break;
176                                 printf("%s\n", key);
177                                 bufpos += keylen + 1;
178                         }
179                         printf("\n");
180                 }
181         }
182
183         close(uevent_netlink_sock);
184         close(udev_monitor_sock);
185         exit(1);
186 }