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