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