chiark / gitweb /
e68bdf5bac208b1c5db84e7c269fd20dd54614c6
[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 library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stddef.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <dirent.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <linux/netlink.h>
23
24 #include "libudev.h"
25 #include "libudev-private.h"
26
27 struct udev_monitor {
28         struct udev *udev;
29         int refcount;
30         int sock;
31         struct sockaddr_nl snl;
32         struct sockaddr_nl snl_peer;
33         struct sockaddr_un sun;
34         socklen_t addrlen;
35 };
36
37 enum udev_monitor_netlink_group {
38         UDEV_MONITOR_KERNEL     = 1,
39         UDEV_MONITOR_UDEV       = 2,
40 };
41
42 /**
43  * udev_monitor_new_from_socket:
44  * @udev: udev library context
45  * @socket_path: unix socket path
46  *
47  * Create new udev monitor and connect to a specified socket. The
48  * path to a socket either points to an existing socket file, or if
49  * the socket path starts with a '@' character, an abstract namespace
50  * socket will be used.
51  *
52  * A socket file will not be created. If it does not already exist,
53  * it will fall-back and connect to an abstract namespace socket with
54  * the given path. The permissions adjustment of a socket file, as
55  * well as the later cleanup, needs to be done by the caller.
56  *
57  * The initial refcount is 1, and needs to be decremented to
58  * release the resources 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         struct stat statbuf;
66
67         if (udev == NULL)
68                 return NULL;
69         if (socket_path == NULL)
70                 return NULL;
71         udev_monitor = calloc(1, sizeof(struct udev_monitor));
72         if (udev_monitor == NULL)
73                 return NULL;
74         udev_monitor->refcount = 1;
75         udev_monitor->udev = udev;
76
77         udev_monitor->sun.sun_family = AF_LOCAL;
78         if (socket_path[0] == '@') {
79                 /* translate leading '@' to abstract namespace */
80                 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
81                 udev_monitor->sun.sun_path[0] = '\0';
82                 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
83         } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) {
84                 /* existing socket file */
85                 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
86                 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
87         } else {
88                 /* no socket file, assume abstract namespace socket */
89                 util_strlcpy(&udev_monitor->sun.sun_path[1], socket_path, sizeof(udev_monitor->sun.sun_path)-1);
90                 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1;
91         }
92         udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
93         if (udev_monitor->sock == -1) {
94                 err(udev, "error getting socket: %m\n");
95                 free(udev_monitor);
96                 return NULL;
97         }
98         util_set_fd_cloexec(udev_monitor->sock);
99
100         dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
101         return udev_monitor;
102 }
103
104 /**
105  * udev_monitor_new_from_netlink:
106  * @udev: udev library context
107  * @name: name of event source
108  *
109  * Create new udev monitor and connect to a specified event
110  * source. Valid sources identifiers are "udev" and "kernel".
111  *
112  * Applications should usually not connect directly to the
113  * "kernel" events, because the devices might not be useable
114  * at that time, before udev has configured them, and created
115  * device nodes.
116  *
117  * Accessing devices at the same time as udev, might result
118  * in unpredictable behavior.
119  *
120  * The "udev" events are sent out after udev has finished its
121  * event processing, all rules have been processed, and needed
122  * device nodes are created.
123  *
124  * The initial refcount is 1, and needs to be decremented to
125  * release the resources of the udev monitor.
126  *
127  * Returns: a new udev monitor, or #NULL, in case of an error
128  **/
129 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
130 {
131         struct udev_monitor *udev_monitor;
132         unsigned int group;
133
134         if (udev == NULL)
135                 return NULL;
136
137         if (name == NULL)
138                 return NULL;
139         if (strcmp(name, "kernel") == 0)
140                 group = UDEV_MONITOR_KERNEL;
141         else if (strcmp(name, "udev") == 0)
142                 group = UDEV_MONITOR_UDEV;
143         else
144                 return NULL;
145
146         udev_monitor = calloc(1, sizeof(struct udev_monitor));
147         if (udev_monitor == NULL)
148                 return NULL;
149         udev_monitor->refcount = 1;
150         udev_monitor->udev = udev;
151
152         udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
153         if (udev_monitor->sock == -1) {
154                 err(udev, "error getting socket: %m\n");
155                 free(udev_monitor);
156                 return NULL;
157         }
158         util_set_fd_cloexec(udev_monitor->sock);
159
160         udev_monitor->snl.nl_family = AF_NETLINK;
161         udev_monitor->snl.nl_groups = group;
162         udev_monitor->snl_peer.nl_family = AF_NETLINK;
163         udev_monitor->snl_peer.nl_groups = UDEV_MONITOR_UDEV;
164
165         dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor, group);
166         return udev_monitor;
167 }
168
169 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
170 {
171         int err;
172         const int on = 1;
173
174         if (udev_monitor->sun.sun_family != 0)
175                 err = bind(udev_monitor->sock,
176                            (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
177         else if (udev_monitor->snl.nl_family != 0)
178                 err = bind(udev_monitor->sock,
179                            (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
180         else
181                 return -EINVAL;
182
183         if (err < 0) {
184                 err(udev_monitor->udev, "bind failed: %m\n");
185                 return err;
186         }
187
188         /* enable receiving of sender credentials */
189         setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
190         return 0;
191 }
192
193 int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
194 {
195         if (udev_monitor == NULL)
196                 return -1;
197         return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
198 }
199
200 /**
201  * udev_monitor_ref:
202  * @udev_monitor: udev monitor
203  *
204  * Take a reference of a udev monitor.
205  *
206  * Returns: the passed udev monitor
207  **/
208 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
209 {
210         if (udev_monitor == NULL)
211                 return NULL;
212         udev_monitor->refcount++;
213         return udev_monitor;
214 }
215
216 /**
217  * udev_monitor_unref:
218  * @udev_monitor: udev monitor
219  *
220  * Drop a reference of a udev monitor. If the refcount reaches zero,
221  * the bound socket will be closed, and the resources of the monitor
222  * will be released.
223  *
224  **/
225 void udev_monitor_unref(struct udev_monitor *udev_monitor)
226 {
227         if (udev_monitor == NULL)
228                 return;
229         udev_monitor->refcount--;
230         if (udev_monitor->refcount > 0)
231                 return;
232         if (udev_monitor->sock >= 0)
233                 close(udev_monitor->sock);
234         dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
235         free(udev_monitor);
236 }
237
238 /**
239  * udev_monitor_get_udev:
240  * @udev_monitor: udev monitor
241  *
242  * Retrieve the udev library context the monitor was created with.
243  *
244  * Returns: the udev library context
245  **/
246 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
247 {
248         if (udev_monitor == NULL)
249                 return NULL;
250         return udev_monitor->udev;
251 }
252
253 /**
254  * udev_monitor_get_fd:
255  * @udev_monitor: udev monitor
256  *
257  * Retrieve the socket file descriptor associated with the monitor.
258  *
259  * Returns: the socket file descriptor
260  **/
261 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
262 {
263         if (udev_monitor == NULL)
264                 return -1;
265         return udev_monitor->sock;
266 }
267
268 /**
269  * udev_monitor_receive_device:
270  * @udev_monitor: udev monitor
271  *
272  * Receive data from the udev monitor socket, allocate a new udev
273  * device, fill in the received data, and return the device.
274  *
275  * Only socket connections with uid=0 are accepted. The caller
276  * needs to make sure that there is data to read from the socket.
277  * The call will block until the socket becomes readable.
278  *
279  * The initial refcount is 1, and needs to be decremented to
280  * release the resources of the udev device.
281  *
282  * Returns: a new udev device, or #NULL, in case of an error
283  **/
284 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
285 {
286         struct udev_device *udev_device;
287         struct msghdr smsg;
288         struct iovec iov;
289         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
290         struct cmsghdr *cmsg;
291         struct sockaddr_nl snl;
292         struct ucred *cred;
293         char buf[4096];
294         size_t bufpos;
295         int devpath_set = 0;
296         int subsystem_set = 0;
297         int action_set = 0;
298         int maj = 0;
299         int min = 0;
300
301         if (udev_monitor == NULL)
302                 return NULL;
303         memset(buf, 0x00, sizeof(buf));
304         iov.iov_base = &buf;
305         iov.iov_len = sizeof(buf);
306         memset (&smsg, 0x00, sizeof(struct msghdr));
307         smsg.msg_iov = &iov;
308         smsg.msg_iovlen = 1;
309         smsg.msg_control = cred_msg;
310         smsg.msg_controllen = sizeof(cred_msg);
311
312         if (udev_monitor->snl.nl_family != 0) {
313                 smsg.msg_name = &snl;
314                 smsg.msg_namelen = sizeof snl;
315         }
316
317         if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
318                 if (errno != EINTR)
319                         info(udev_monitor->udev, "unable to receive message\n");
320                 return NULL;
321         }
322
323         if (udev_monitor->snl.nl_family != 0) {
324                 if (snl.nl_groups == 0) {
325                         info(udev_monitor->udev, "unicast netlink message ignored\n");
326                         return NULL;
327                 }
328                 if ((snl.nl_groups == UDEV_MONITOR_KERNEL) && (snl.nl_pid > 0)) {
329                         info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", snl.nl_pid);
330                         return NULL;
331                 }
332         }
333
334         cmsg = CMSG_FIRSTHDR(&smsg);
335         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
336                 info(udev_monitor->udev, "no sender credentials received, message ignored\n");
337                 return NULL;
338         }
339
340         cred = (struct ucred *)CMSG_DATA(cmsg);
341         if (cred->uid != 0) {
342                 info(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
343                 return NULL;
344         }
345
346         /* skip header */
347         bufpos = strlen(buf) + 1;
348         if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
349                 info(udev_monitor->udev, "invalid message length\n");
350                 return NULL;
351         }
352
353         /* check message header */
354         if (strstr(buf, "@/") == NULL) {
355                 info(udev_monitor->udev, "unrecognized message header\n");
356                 return NULL;
357         }
358
359         udev_device = device_new(udev_monitor->udev);
360         if (udev_device == NULL) {
361                 return NULL;
362         }
363
364         while (bufpos < sizeof(buf)) {
365                 char *key;
366                 size_t keylen;
367
368                 key = &buf[bufpos];
369                 keylen = strlen(key);
370                 if (keylen == 0)
371                         break;
372                 bufpos += keylen + 1;
373
374                 if (strncmp(key, "DEVPATH=", 8) == 0) {
375                         char path[UTIL_PATH_SIZE];
376
377                         util_strlcpy(path, udev_get_sys_path(udev_monitor->udev), sizeof(path));
378                         util_strlcat(path, &key[8], sizeof(path));
379                         udev_device_set_syspath(udev_device, path);
380                         devpath_set = 1;
381                 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
382                         udev_device_set_subsystem(udev_device, &key[10]);
383                         subsystem_set = 1;
384                 } else if (strncmp(key, "DEVTYPE=", 8) == 0) {
385                         udev_device_set_devtype(udev_device, &key[8]);
386                 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
387                         udev_device_set_devnode(udev_device, &key[8]);
388                 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
389                         char devlinks[UTIL_PATH_SIZE];
390                         char *slink;
391                         char *next;
392
393                         util_strlcpy(devlinks, &key[9], sizeof(devlinks));
394                         slink = devlinks;
395                         next = strchr(slink, ' ');
396                         while (next != NULL) {
397                                 next[0] = '\0';
398                                 udev_device_add_devlink(udev_device, slink);
399                                 slink = &next[1];
400                                 next = strchr(slink, ' ');
401                         }
402                         if (slink[0] != '\0')
403                                 udev_device_add_devlink(udev_device, slink);
404                 } else if (strncmp(key, "DRIVER=", 7) == 0) {
405                         udev_device_set_driver(udev_device, &key[7]);
406                 } else if (strncmp(key, "ACTION=", 7) == 0) {
407                         udev_device_set_action(udev_device, &key[7]);
408                         action_set = 1;
409                 } else if (strncmp(key, "MAJOR=", 6) == 0) {
410                         maj = strtoull(&key[6], NULL, 10);
411                 } else if (strncmp(key, "MINOR=", 6) == 0) {
412                         min = strtoull(&key[6], NULL, 10);
413                 } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) {
414                         udev_device_set_devpath_old(udev_device, &key[12]);
415                 } else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) {
416                         udev_device_set_physdevpath(udev_device, &key[12]);
417                 } else if (strncmp(key, "SEQNUM=", 7) == 0) {
418                         udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10));
419                 } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
420                         udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
421                 } else if (strncmp(key, "PHYSDEV", 7) == 0) {
422                         /* skip deprecated values */
423                         continue;
424                 } else {
425                         udev_device_add_property_from_string(udev_device, key);
426                 }
427         }
428         if (!devpath_set || !subsystem_set || !action_set) {
429                 info(udev_monitor->udev, "missing values, skip\n");
430                 udev_device_unref(udev_device);
431                 return NULL;
432         }
433         if (maj > 0)
434                 udev_device_set_devnum(udev_device, makedev(maj, min));
435         udev_device_set_info_loaded(udev_device);
436         return udev_device;
437 }
438
439 int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
440 {
441         const char *buf;
442         ssize_t len;
443         ssize_t count;
444
445         len = udev_device_get_properties_monitor_buf(udev_device, &buf);
446         if (len < 32)
447                 return -1;
448         if (udev_monitor->sun.sun_family != 0)
449                 count = sendto(udev_monitor->sock,
450                                buf, len, 0,
451                                (struct sockaddr *)&udev_monitor->sun,
452                                udev_monitor->addrlen);
453         else if (udev_monitor->snl.nl_family != 0)
454                 /* no destination besides the muticast group, we will always get ECONNREFUSED */
455                 count = sendto(udev_monitor->sock,
456                                buf, len, 0,
457                                (struct sockaddr *)&udev_monitor->snl_peer,
458                                sizeof(struct sockaddr_nl));
459         else
460                 return -1;
461
462         info(udev_monitor->udev, "passed %zi bytes to monitor %p\n", count, udev_monitor);
463         return count;
464 }