chiark / gitweb /
e07e462af3bb861cfeb563543d66a0b441996269
[elogind.git] / src / libudev / libudev-monitor.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <dirent.h>
27 #include <sys/poll.h>
28 #include <sys/stat.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include <arpa/inet.h>
32 #include <linux/netlink.h>
33 #include <linux/filter.h>
34
35 #include "libudev.h"
36 #include "libudev-private.h"
37 #include "socket-util.h"
38
39 /**
40  * SECTION:libudev-monitor
41  * @short_description: device event source
42  *
43  * Connects to a device event source.
44  */
45
46 /**
47  * udev_monitor:
48  *
49  * Opaque object handling an event source.
50  */
51 struct udev_monitor {
52         struct udev *udev;
53         int refcount;
54         int sock;
55         union sockaddr_union snl;
56         union sockaddr_union snl_trusted_sender;
57         union sockaddr_union snl_destination;
58         socklen_t addrlen;
59         struct udev_list filter_subsystem_list;
60         struct udev_list filter_tag_list;
61         bool bound;
62 };
63
64 enum udev_monitor_netlink_group {
65         UDEV_MONITOR_NONE,
66         UDEV_MONITOR_KERNEL,
67         UDEV_MONITOR_UDEV,
68 };
69
70 #define UDEV_MONITOR_MAGIC                0xfeedcafe
71 struct udev_monitor_netlink_header {
72         /* "libudev" prefix to distinguish libudev and kernel messages */
73         char prefix[8];
74         /*
75          * magic to protect against daemon <-> library message format mismatch
76          * used in the kernel from socket filter rules; needs to be stored in network order
77          */
78         unsigned int magic;
79         /* total length of header structure known to the sender */
80         unsigned int header_size;
81         /* properties string buffer */
82         unsigned int properties_off;
83         unsigned int properties_len;
84         /*
85          * hashes of primary device properties strings, to let libudev subscribers
86          * use in-kernel socket filters; values need to be stored in network order
87          */
88         unsigned int filter_subsystem_hash;
89         unsigned int filter_devtype_hash;
90         unsigned int filter_tag_bloom_hi;
91         unsigned int filter_tag_bloom_lo;
92 };
93
94 static struct udev_monitor *udev_monitor_new(struct udev *udev)
95 {
96         struct udev_monitor *udev_monitor;
97
98         udev_monitor = calloc(1, sizeof(struct udev_monitor));
99         if (udev_monitor == NULL)
100                 return NULL;
101         udev_monitor->refcount = 1;
102         udev_monitor->udev = udev;
103         udev_list_init(udev, &udev_monitor->filter_subsystem_list, false);
104         udev_list_init(udev, &udev_monitor->filter_tag_list, true);
105         return udev_monitor;
106 }
107
108 struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd)
109 {
110         struct udev_monitor *udev_monitor;
111         unsigned int group;
112
113         if (udev == NULL)
114                 return NULL;
115
116         if (name == NULL)
117                 group = UDEV_MONITOR_NONE;
118         else if (streq(name, "udev"))
119                 group = UDEV_MONITOR_UDEV;
120         else if (streq(name, "kernel"))
121                 group = UDEV_MONITOR_KERNEL;
122         else
123                 return NULL;
124
125         /*
126          * We do not support subscribing to uevents if no instance of udev
127          * is running. Uevents would otherwise broadcast the processing data
128          * of the host into containers, which is not acceptable. Containers
129          * will currently just not get any uevents.
130          */
131         
132
133         udev_monitor = udev_monitor_new(udev);
134         if (udev_monitor == NULL)
135                 return NULL;
136
137         if (fd < 0) {
138                 udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
139                 if (udev_monitor->sock == -1) {
140                         udev_err(udev, "error getting socket: %m\n");
141                         free(udev_monitor);
142                         return NULL;
143                 }
144         } else {
145                 udev_monitor->bound = true;
146                 udev_monitor->sock = fd;
147         }
148
149         udev_monitor->snl.nl.nl_family = AF_NETLINK;
150         udev_monitor->snl.nl.nl_groups = group;
151
152         /* default destination for sending */
153         udev_monitor->snl_destination.nl.nl_family = AF_NETLINK;
154         udev_monitor->snl_destination.nl.nl_groups = UDEV_MONITOR_UDEV;
155
156         return udev_monitor;
157 }
158
159 /**
160  * udev_monitor_new_from_netlink:
161  * @udev: udev library context
162  * @name: name of event source
163  *
164  * Create new udev monitor and connect to a specified event
165  * source. Valid sources identifiers are "udev" and "kernel".
166  *
167  * Applications should usually not connect directly to the
168  * "kernel" events, because the devices might not be useable
169  * at that time, before udev has configured them, and created
170  * device nodes. Accessing devices at the same time as udev,
171  * might result in unpredictable behavior. The "udev" events
172  * are sent out after udev has finished its event processing,
173  * all rules have been processed, and needed device nodes are
174  * created.
175  *
176  * The initial refcount is 1, and needs to be decremented to
177  * release the resources of the udev monitor.
178  *
179  * Returns: a new udev monitor, or #NULL, in case of an error
180  **/
181 _public_ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
182 {
183         return udev_monitor_new_from_netlink_fd(udev, name, -1);
184 }
185
186 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
187                             unsigned short code, unsigned int data)
188 {
189         struct sock_filter *ins = &inss[*i];
190
191         ins->code = code;
192         ins->k = data;
193         (*i)++;
194 }
195
196 static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i,
197                            unsigned short code, unsigned int data,
198                            unsigned short jt, unsigned short jf)
199 {
200         struct sock_filter *ins = &inss[*i];
201
202         ins->code = code;
203         ins->jt = jt;
204         ins->jf = jf;
205         ins->k = data;
206         (*i)++;
207 }
208
209 /**
210  * udev_monitor_filter_update:
211  * @udev_monitor: monitor
212  *
213  * Update the installed socket filter. This is only needed,
214  * if the filter was removed or changed.
215  *
216  * Returns: 0 on success, otherwise a negative error value.
217  */
218 _public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
219 {
220         struct sock_filter ins[512];
221         struct sock_fprog filter;
222         unsigned int i;
223         struct udev_list_entry *list_entry;
224         int err;
225
226         if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL &&
227             udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
228                 return 0;
229
230         memset(ins, 0x00, sizeof(ins));
231         i = 0;
232
233         /* load magic in A */
234         bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic));
235         /* jump if magic matches */
236         bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
237         /* wrong magic, pass packet */
238         bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
239
240         if (udev_list_get_entry(&udev_monitor->filter_tag_list) != NULL) {
241                 int tag_matches;
242
243                 /* count tag matches, to calculate end of tag match block */
244                 tag_matches = 0;
245                 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list))
246                         tag_matches++;
247
248                 /* add all tags matches */
249                 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
250                         uint64_t tag_bloom_bits = util_string_bloom64(udev_list_entry_get_name(list_entry));
251                         uint32_t tag_bloom_hi = tag_bloom_bits >> 32;
252                         uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff;
253
254                         /* load device bloom bits in A */
255                         bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi));
256                         /* clear bits (tag bits & bloom bits) */
257                         bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi);
258                         /* jump to next tag if it does not match */
259                         bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3);
260
261                         /* load device bloom bits in A */
262                         bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo));
263                         /* clear bits (tag bits & bloom bits) */
264                         bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo);
265                         /* jump behind end of tag match block if tag matches */
266                         tag_matches--;
267                         bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0);
268                 }
269
270                 /* nothing matched, drop packet */
271                 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
272         }
273
274         /* add all subsystem matches */
275         if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) != NULL) {
276                 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
277                         unsigned int hash = util_string_hash32(udev_list_entry_get_name(list_entry));
278
279                         /* load device subsystem value in A */
280                         bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash));
281                         if (udev_list_entry_get_value(list_entry) == NULL) {
282                                 /* jump if subsystem does not match */
283                                 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
284                         } else {
285                                 /* jump if subsystem does not match */
286                                 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
287
288                                 /* load device devtype value in A */
289                                 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash));
290                                 /* jump if value does not match */
291                                 hash = util_string_hash32(udev_list_entry_get_value(list_entry));
292                                 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
293                         }
294
295                         /* matched, pass packet */
296                         bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
297
298                         if (i+1 >= ELEMENTSOF(ins))
299                                 return -E2BIG;
300                 }
301
302                 /* nothing matched, drop packet */
303                 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
304         }
305
306         /* matched, pass packet */
307         bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
308
309         /* install filter */
310         memset(&filter, 0x00, sizeof(filter));
311         filter.len = i;
312         filter.filter = ins;
313         err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
314         return err < 0 ? -errno : 0;
315 }
316
317 int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
318 {
319         udev_monitor->snl_trusted_sender.nl.nl_pid = sender->snl.nl.nl_pid;
320         return 0;
321 }
322 /**
323  * udev_monitor_enable_receiving:
324  * @udev_monitor: the monitor which should receive events
325  *
326  * Binds the @udev_monitor socket to the event source.
327  *
328  * Returns: 0 on success, otherwise a negative error value.
329  */
330 _public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
331 {
332         int err = 0;
333         const int on = 1;
334
335         udev_monitor_filter_update(udev_monitor);
336
337         if (!udev_monitor->bound) {
338                 err = bind(udev_monitor->sock,
339                            &udev_monitor->snl.sa, sizeof(struct sockaddr_nl));
340                 if (err == 0)
341                         udev_monitor->bound = true;
342         }
343
344         if (err >= 0) {
345                 union sockaddr_union snl;
346                 socklen_t addrlen;
347
348                 /*
349                  * get the address the kernel has assigned us
350                  * it is usually, but not necessarily the pid
351                  */
352                 addrlen = sizeof(struct sockaddr_nl);
353                 err = getsockname(udev_monitor->sock, &snl.sa, &addrlen);
354                 if (err == 0)
355                         udev_monitor->snl.nl.nl_pid = snl.nl.nl_pid;
356         } else {
357                 udev_err(udev_monitor->udev, "bind failed: %m\n");
358                 return -errno;
359         }
360
361         /* enable receiving of sender credentials */
362         setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
363         return 0;
364 }
365
366 /**
367  * udev_monitor_set_receive_buffer_size:
368  * @udev_monitor: the monitor which should receive events
369  * @size: the size in bytes
370  *
371  * Set the size of the kernel socket buffer. This call needs the
372  * appropriate privileges to succeed.
373  *
374  * Returns: 0 on success, otherwise -1 on error.
375  */
376 _public_ int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
377 {
378         if (udev_monitor == NULL)
379                 return -EINVAL;
380         return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
381 }
382
383 int udev_monitor_disconnect(struct udev_monitor *udev_monitor)
384 {
385         int err;
386
387         err = close(udev_monitor->sock);
388         udev_monitor->sock = -1;
389         return err < 0 ? -errno : 0;
390 }
391
392 /**
393  * udev_monitor_ref:
394  * @udev_monitor: udev monitor
395  *
396  * Take a reference of a udev monitor.
397  *
398  * Returns: the passed udev monitor
399  **/
400 _public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
401 {
402         if (udev_monitor == NULL)
403                 return NULL;
404         udev_monitor->refcount++;
405         return udev_monitor;
406 }
407
408 /**
409  * udev_monitor_unref:
410  * @udev_monitor: udev monitor
411  *
412  * Drop a reference of a udev monitor. If the refcount reaches zero,
413  * the bound socket will be closed, and the resources of the monitor
414  * will be released.
415  *
416  * Returns: #NULL
417  **/
418 _public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monitor)
419 {
420         if (udev_monitor == NULL)
421                 return NULL;
422         udev_monitor->refcount--;
423         if (udev_monitor->refcount > 0)
424                 return NULL;
425         if (udev_monitor->sock >= 0)
426                 close(udev_monitor->sock);
427         udev_list_cleanup(&udev_monitor->filter_subsystem_list);
428         udev_list_cleanup(&udev_monitor->filter_tag_list);
429         free(udev_monitor);
430         return NULL;
431 }
432
433 /**
434  * udev_monitor_get_udev:
435  * @udev_monitor: udev monitor
436  *
437  * Retrieve the udev library context the monitor was created with.
438  *
439  * Returns: the udev library context
440  **/
441 _public_ struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
442 {
443         if (udev_monitor == NULL)
444                 return NULL;
445         return udev_monitor->udev;
446 }
447
448 /**
449  * udev_monitor_get_fd:
450  * @udev_monitor: udev monitor
451  *
452  * Retrieve the socket file descriptor associated with the monitor.
453  *
454  * Returns: the socket file descriptor
455  **/
456 _public_ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
457 {
458         if (udev_monitor == NULL)
459                 return -EINVAL;
460         return udev_monitor->sock;
461 }
462
463 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
464 {
465         struct udev_list_entry *list_entry;
466
467         if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
468                 goto tag;
469         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
470                 const char *subsys = udev_list_entry_get_name(list_entry);
471                 const char *dsubsys = udev_device_get_subsystem(udev_device);
472                 const char *devtype;
473                 const char *ddevtype;
474
475                 if (!streq(dsubsys, subsys))
476                         continue;
477
478                 devtype = udev_list_entry_get_value(list_entry);
479                 if (devtype == NULL)
480                         goto tag;
481                 ddevtype = udev_device_get_devtype(udev_device);
482                 if (ddevtype == NULL)
483                         continue;
484                 if (streq(ddevtype, devtype))
485                         goto tag;
486         }
487         return 0;
488
489 tag:
490         if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
491                 return 1;
492         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
493                 const char *tag = udev_list_entry_get_name(list_entry);
494
495                 if (udev_device_has_tag(udev_device, tag))
496                         return 1;
497         }
498         return 0;
499 }
500
501 /**
502  * udev_monitor_receive_device:
503  * @udev_monitor: udev monitor
504  *
505  * Receive data from the udev monitor socket, allocate a new udev
506  * device, fill in the received data, and return the device.
507  *
508  * Only socket connections with uid=0 are accepted.
509  *
510  * The monitor socket is by default set to NONBLOCK. A variant of poll() on
511  * the file descriptor returned by udev_monitor_get_fd() should to be used to
512  * wake up when new devices arrive, or alternatively the file descriptor
513  * switched into blocking mode.
514  *
515  * The initial refcount is 1, and needs to be decremented to
516  * release the resources of the udev device.
517  *
518  * Returns: a new udev device, or #NULL, in case of an error
519  **/
520 _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
521 {
522         struct udev_device *udev_device;
523         struct msghdr smsg;
524         struct iovec iov;
525         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
526         struct cmsghdr *cmsg;
527         union sockaddr_union snl;
528         struct ucred *cred;
529         char buf[8192];
530         ssize_t buflen;
531         ssize_t bufpos;
532
533 retry:
534         if (udev_monitor == NULL)
535                 return NULL;
536         iov.iov_base = &buf;
537         iov.iov_len = sizeof(buf);
538         memset (&smsg, 0x00, sizeof(struct msghdr));
539         smsg.msg_iov = &iov;
540         smsg.msg_iovlen = 1;
541         smsg.msg_control = cred_msg;
542         smsg.msg_controllen = sizeof(cred_msg);
543         smsg.msg_name = &snl;
544         smsg.msg_namelen = sizeof(snl);
545
546         buflen = recvmsg(udev_monitor->sock, &smsg, 0);
547         if (buflen < 0) {
548                 if (errno != EINTR)
549                         udev_dbg(udev_monitor->udev, "unable to receive message\n");
550                 return NULL;
551         }
552
553         if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
554                 udev_dbg(udev_monitor->udev, "invalid message length\n");
555                 return NULL;
556         }
557
558         if (snl.nl.nl_groups == 0) {
559                 /* unicast message, check if we trust the sender */
560                 if (udev_monitor->snl_trusted_sender.nl.nl_pid == 0 ||
561                     snl.nl.nl_pid != udev_monitor->snl_trusted_sender.nl.nl_pid) {
562                         udev_dbg(udev_monitor->udev, "unicast netlink message ignored\n");
563                         return NULL;
564                 }
565         } else if (snl.nl.nl_groups == UDEV_MONITOR_KERNEL) {
566                 if (snl.nl.nl_pid > 0) {
567                         udev_dbg(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n",
568                              snl.nl.nl_pid);
569                         return NULL;
570                 }
571         }
572
573         cmsg = CMSG_FIRSTHDR(&smsg);
574         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
575                 udev_dbg(udev_monitor->udev, "no sender credentials received, message ignored\n");
576                 return NULL;
577         }
578
579         cred = (struct ucred *)CMSG_DATA(cmsg);
580         if (cred->uid != 0) {
581                 udev_dbg(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
582                 return NULL;
583         }
584
585         udev_device = udev_device_new(udev_monitor->udev);
586         if (udev_device == NULL)
587                 return NULL;
588
589         if (memcmp(buf, "libudev", 8) == 0) {
590                 struct udev_monitor_netlink_header *nlh;
591
592                 /* udev message needs proper version magic */
593                 nlh = (struct udev_monitor_netlink_header *) buf;
594                 if (nlh->magic != htonl(UDEV_MONITOR_MAGIC)) {
595                         udev_err(udev_monitor->udev, "unrecognized message signature (%x != %x)\n",
596                                  nlh->magic, htonl(UDEV_MONITOR_MAGIC));
597                         udev_device_unref(udev_device);
598                         return NULL;
599                 }
600                 if (nlh->properties_off+32 > (size_t)buflen) {
601                         udev_device_unref(udev_device);
602                         return NULL;
603                 }
604
605                 bufpos = nlh->properties_off;
606
607                 /* devices received from udev are always initialized */
608                 udev_device_set_is_initialized(udev_device);
609         } else {
610                 /* kernel message with header */
611                 bufpos = strlen(buf) + 1;
612                 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
613                         udev_dbg(udev_monitor->udev, "invalid message length\n");
614                         udev_device_unref(udev_device);
615                         return NULL;
616                 }
617
618                 /* check message header */
619                 if (strstr(buf, "@/") == NULL) {
620                         udev_dbg(udev_monitor->udev, "unrecognized message header\n");
621                         udev_device_unref(udev_device);
622                         return NULL;
623                 }
624         }
625
626         udev_device_set_info_loaded(udev_device);
627
628         while (bufpos < buflen) {
629                 char *key;
630                 size_t keylen;
631
632                 key = &buf[bufpos];
633                 keylen = strlen(key);
634                 if (keylen == 0)
635                         break;
636                 bufpos += keylen + 1;
637                 udev_device_add_property_from_string_parse(udev_device, key);
638         }
639
640         if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
641                 udev_dbg(udev_monitor->udev, "missing values, invalid device\n");
642                 udev_device_unref(udev_device);
643                 return NULL;
644         }
645
646         /* skip device, if it does not pass the current filter */
647         if (!passes_filter(udev_monitor, udev_device)) {
648                 struct pollfd pfd[1];
649                 int rc;
650
651                 udev_device_unref(udev_device);
652
653                 /* if something is queued, get next device */
654                 pfd[0].fd = udev_monitor->sock;
655                 pfd[0].events = POLLIN;
656                 rc = poll(pfd, 1, 0);
657                 if (rc > 0)
658                         goto retry;
659                 return NULL;
660         }
661
662         return udev_device;
663 }
664
665 int udev_monitor_send_device(struct udev_monitor *udev_monitor,
666                              struct udev_monitor *destination, struct udev_device *udev_device)
667 {
668         const char *buf;
669         ssize_t blen;
670         ssize_t count;
671         struct msghdr smsg;
672         struct iovec iov[2];
673         const char *val;
674         struct udev_monitor_netlink_header nlh;
675         struct udev_list_entry *list_entry;
676         uint64_t tag_bloom_bits;
677
678         blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
679         if (blen < 32)
680                 return -EINVAL;
681
682         /* add versioned header */
683         memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
684         memcpy(nlh.prefix, "libudev", 8);
685         nlh.magic = htonl(UDEV_MONITOR_MAGIC);
686         nlh.header_size = sizeof(struct udev_monitor_netlink_header);
687         val = udev_device_get_subsystem(udev_device);
688         nlh.filter_subsystem_hash = htonl(util_string_hash32(val));
689         val = udev_device_get_devtype(udev_device);
690         if (val != NULL)
691                 nlh.filter_devtype_hash = htonl(util_string_hash32(val));
692         iov[0].iov_base = &nlh;
693         iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
694
695         /* add tag bloom filter */
696         tag_bloom_bits = 0;
697         udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
698                 tag_bloom_bits |= util_string_bloom64(udev_list_entry_get_name(list_entry));
699         if (tag_bloom_bits > 0) {
700                 nlh.filter_tag_bloom_hi = htonl(tag_bloom_bits >> 32);
701                 nlh.filter_tag_bloom_lo = htonl(tag_bloom_bits & 0xffffffff);
702         }
703
704         /* add properties list */
705         nlh.properties_off = iov[0].iov_len;
706         nlh.properties_len = blen;
707         iov[1].iov_base = (char *)buf;
708         iov[1].iov_len = blen;
709
710         memset(&smsg, 0x00, sizeof(struct msghdr));
711         smsg.msg_iov = iov;
712         smsg.msg_iovlen = 2;
713         /*
714          * Use custom address for target, or the default one.
715          *
716          * If we send to a multicast group, we will get
717          * ECONNREFUSED, which is expected.
718          */
719         if (destination != NULL)
720                 smsg.msg_name = &destination->snl;
721         else
722                 smsg.msg_name = &udev_monitor->snl_destination;
723         smsg.msg_namelen = sizeof(struct sockaddr_nl);
724         count = sendmsg(udev_monitor->sock, &smsg, 0);
725         udev_dbg(udev_monitor->udev, "passed %zi bytes to netlink monitor %p\n", count, udev_monitor);
726         return count;
727 }
728
729 /**
730  * udev_monitor_filter_add_match_subsystem_devtype:
731  * @udev_monitor: the monitor
732  * @subsystem: the subsystem value to match the incoming devices against
733  * @devtype: the devtype value to match the incoming devices against
734  *
735  * This filter is efficiently executed inside the kernel, and libudev subscribers
736  * will usually not be woken up for devices which do not match.
737  *
738  * The filter must be installed before the monitor is switched to listening mode.
739  *
740  * Returns: 0 on success, otherwise a negative error value.
741  */
742 _public_ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
743 {
744         if (udev_monitor == NULL)
745                 return -EINVAL;
746         if (subsystem == NULL)
747                 return -EINVAL;
748         if (udev_list_entry_add(&udev_monitor->filter_subsystem_list, subsystem, devtype) == NULL)
749                 return -ENOMEM;
750         return 0;
751 }
752
753 /**
754  * udev_monitor_filter_add_match_tag:
755  * @udev_monitor: the monitor
756  * @tag: the name of a tag
757  *
758  * This filter is efficiently executed inside the kernel, and libudev subscribers
759  * will usually not be woken up for devices which do not match.
760  *
761  * The filter must be installed before the monitor is switched to listening mode.
762  *
763  * Returns: 0 on success, otherwise a negative error value.
764  */
765 _public_ int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag)
766 {
767         if (udev_monitor == NULL)
768                 return -EINVAL;
769         if (tag == NULL)
770                 return -EINVAL;
771         if (udev_list_entry_add(&udev_monitor->filter_tag_list, tag, NULL) == NULL)
772                 return -ENOMEM;
773         return 0;
774 }
775
776 /**
777  * udev_monitor_filter_remove:
778  * @udev_monitor: monitor
779  *
780  * Remove all filters from monitor.
781  *
782  * Returns: 0 on success, otherwise a negative error value.
783  */
784 _public_ int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
785 {
786         static struct sock_fprog filter = { 0, NULL };
787
788         udev_list_cleanup(&udev_monitor->filter_subsystem_list);
789         return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
790 }