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