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