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