chiark / gitweb /
move udev_ctrl to libudev-private
[elogind.git] / udev / lib / libudev-monitor.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32
33 #include "libudev.h"
34 #include "libudev-private.h"
35 #include "../udev.h"
36
37 struct udev_monitor {
38         struct udev *udev;
39         int refcount;
40         int sock;
41         struct sockaddr_un saddr;
42         socklen_t addrlen;
43 };
44
45 /**
46  * udev_monitor_new_from_socket:
47  * @udev: udev library context
48  * @socket_path: unix socket path
49  *
50  * Create new udev monitor, setup and connect to a specified socket. The
51  * path to a socket can point to an existing socket file, or it will be
52  * created if needed. If neccessary, the permissions adjustment as well as
53  * the later cleanup of the socket file, needs to be done by the caller.
54  * If the socket path starts with a '@' character, an abstract namespace
55  * socket will be used.
56  *
57  * The initial refcount is 1, and needs to be decremented to
58  * release the ressources of the udev monitor.
59  *
60  * Returns: a new udev monitor, or #NULL, in case of an error
61  **/
62 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
63 {
64         struct udev_monitor *udev_monitor;
65
66         if (udev == NULL)
67                 return NULL;
68         if (socket_path == NULL)
69                 return NULL;
70         udev_monitor = malloc(sizeof(struct udev_monitor));
71         if (udev_monitor == NULL)
72                 return NULL;
73         memset(udev_monitor, 0x00, sizeof(struct udev_monitor));
74         udev_monitor->refcount = 1;
75         udev_monitor->udev = udev;
76
77         udev_monitor->saddr.sun_family = AF_LOCAL;
78         strcpy(udev_monitor->saddr.sun_path, socket_path);
79         udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(udev_monitor->saddr.sun_path);
80
81         /* translate leading '@' to abstract namespace */
82         if (udev_monitor->saddr.sun_path[0] == '@')
83                 udev_monitor->saddr.sun_path[0] = '\0';
84
85         udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
86         if (udev_monitor->sock == -1) {
87                 err(udev, "error getting socket: %s\n", strerror(errno));
88                 free(udev_monitor);
89                 return NULL;
90         }
91         return udev_monitor;
92 }
93
94 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
95 {
96         int err;
97         const int on = 1;
98
99         err = bind(udev_monitor->sock, (struct sockaddr *) &udev_monitor->saddr, udev_monitor->addrlen);
100         if (err < 0) {
101                 err(udev_monitor->udev, "bind failed: %s\n", strerror(errno));
102                 return err;
103         }
104
105         /* enable receiving of the sender credentials */
106         setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
107         info(udev_monitor->udev, "udev_monitor: %p created\n", udev_monitor);
108         return 0;
109 }
110
111 /**
112  * udev_monitor_ref:
113  * @udev_monitor: udev monitor
114  *
115  * Take a reference of a udev monitor.
116  *
117  * Returns: the passed udev monitor
118  **/
119 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
120 {
121         if (udev_monitor == NULL)
122                 return NULL;
123         udev_monitor->refcount++;
124         return udev_monitor;
125 }
126
127 /**
128  * udev_monitor_unref:
129  * @udev_monitor: udev monitor
130  *
131  * Drop a reference ofa udev monitor. If the refcount reaches zero,
132  * the bound socket will be closed, and the ressources of the monitor
133  * will be released.
134  *
135  **/
136 void udev_monitor_unref(struct udev_monitor *udev_monitor)
137 {
138         if (udev_monitor == NULL)
139                 return;
140         udev_monitor->refcount--;
141         if (udev_monitor->refcount > 0)
142                 return;
143         if (udev_monitor->sock >= 0)
144                 close(udev_monitor->sock);
145         info(udev_monitor->udev, "udev_monitor: %p released\n", udev_monitor);
146         free(udev_monitor);
147 }
148
149 /**
150  * udev_monitor_get_udev:
151  * @udev_monitor: udev monitor
152  *
153  * Retrieve the udev library context the monitor was created with.
154  *
155  * Returns: the udev library context
156  **/
157 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
158 {
159         if (udev_monitor == NULL)
160                 return NULL;
161         return udev_monitor->udev;
162 }
163
164 /**
165  * udev_monitor_get_fd:
166  * @udev_monitor: udev monitor
167  *
168  * Retrieve the socket file descriptor associated with the monitor.
169  *
170  * Returns: the socket file descriptor
171  **/
172 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
173 {
174         if (udev_monitor == NULL)
175                 return -1;
176         return udev_monitor->sock;
177 }
178
179 /**
180  * udev_monitor_receive_device:
181  * @udev_monitor: udev monitor
182  *
183  * Receive data from the udev monitor socket, allocate a new udev
184  * device, fill in the received data, and return the device.
185  *
186  * Only socket connections with uid=0 are accepted. The caller
187  * needs to make sure, that there is data to read from the socket,
188  * the call will block until the socket becomes readable.
189  *
190  * The initial refcount is 1, and needs to be decremented to
191  * release the ressources of the udev device.
192  *
193  * Returns: a new udev device, or #NULL, in case of an error
194  **/
195 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
196 {
197         struct udev_device *udev_device;
198         struct msghdr smsg;
199         struct cmsghdr *cmsg;
200         struct iovec iov;
201         struct ucred *cred;
202         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
203         char buf[4096];
204         size_t bufpos;
205
206         if (udev_monitor == NULL)
207                 return NULL;
208         memset(buf, 0x00, sizeof(buf));
209         iov.iov_base = &buf;
210         iov.iov_len = sizeof(buf);
211         memset (&smsg, 0x00, sizeof(struct msghdr));
212         smsg.msg_iov = &iov;
213         smsg.msg_iovlen = 1;
214         smsg.msg_control = cred_msg;
215         smsg.msg_controllen = sizeof(cred_msg);
216
217         if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
218                 if (errno != EINTR)
219                         info(udev_monitor->udev, "unable to receive message");
220                 return NULL;
221         }
222         cmsg = CMSG_FIRSTHDR(&smsg);
223         cred = (struct ucred *)CMSG_DATA (cmsg);
224
225         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
226                 info(udev_monitor->udev, "no sender credentials received, message ignored");
227                 return NULL;
228         }
229
230         if (cred->uid != 0) {
231                 info(udev_monitor->udev, "sender uid=%d, message ignored", cred->uid);
232                 return NULL;
233         }
234
235         /* skip header */
236         bufpos = strlen(buf) + 1;
237         if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
238                 info(udev_monitor->udev, "invalid message length");
239                 return NULL;
240         }
241
242         /* check message header */
243         if (strstr(buf, "@/") == NULL) {
244                 info(udev_monitor->udev, "unrecognized message header");
245                 return NULL;
246         }
247
248         udev_device = device_init(udev_monitor->udev);
249         if (udev_device == NULL) {
250                 return NULL;
251         }
252
253         while (bufpos < sizeof(buf)) {
254                 char *key;
255                 size_t keylen;
256
257                 key = &buf[bufpos];
258                 keylen = strlen(key);
259                 if (keylen == 0)
260                         break;
261                 bufpos += keylen + 1;
262
263                 if (strncmp(key, "DEVPATH=", 8) == 0) {
264                         device_set_devpath(udev_device, &key[8]);
265                 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
266                         device_set_subsystem(udev_device, &key[10]);
267                 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
268                         device_set_devname(udev_device, &key[8]);
269                 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
270                         char *slink = &key[9];
271                         char *next = strchr(slink, ' ');
272
273                         while (next != NULL) {
274                                 next[0] = '\0';
275                                 device_add_devlink(udev_device, slink);
276                                 slink = &next[1];
277                                 next = strchr(slink, ' ');
278                         }
279                         if (slink[0] != '\0')
280                                 device_add_devlink(udev_device, slink);
281                 }
282                 device_add_property(udev_device, key);
283         }
284
285         return udev_device;
286 }