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