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