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