]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libudev/libudev-monitor.c
bootctl: always show slashes not backslashes
[thirdparty/systemd.git] / src / libudev / libudev-monitor.c
CommitLineData
88a6477e
KS
1/***
2 This file is part of systemd.
3
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
ba6929f6 19
ba6929f6
KS
20#include <stdio.h>
21#include <stdlib.h>
22#include <stddef.h>
23#include <unistd.h>
24#include <errno.h>
25#include <string.h>
26#include <dirent.h>
e14bdd88 27#include <sys/poll.h>
ba6929f6
KS
28#include <sys/stat.h>
29#include <sys/socket.h>
30#include <sys/un.h>
e14bdd88 31#include <arpa/inet.h>
1c7047ea 32#include <linux/netlink.h>
e14bdd88 33#include <linux/filter.h>
ba6929f6
KS
34
35#include "libudev.h"
36#include "libudev-private.h"
b49d9b50 37#include "socket-util.h"
ba6929f6 38
ce1d6d7f
KS
39/**
40 * SECTION:libudev-monitor
41 * @short_description: device event source
42 *
43 * Connects to a device event source.
44 */
45
ce1d6d7f
KS
46/**
47 * udev_monitor:
48 *
50579295 49 * Opaque object handling an event source.
ce1d6d7f 50 */
ba6929f6 51struct udev_monitor {
912541b0
KS
52 struct udev *udev;
53 int refcount;
54 int sock;
b49d9b50
KS
55 union sockaddr_union snl;
56 union sockaddr_union snl_trusted_sender;
57 union sockaddr_union snl_destination;
912541b0
KS
58 socklen_t addrlen;
59 struct udev_list filter_subsystem_list;
60 struct udev_list filter_tag_list;
61 bool bound;
ba6929f6
KS
62};
63
f2b93744 64enum udev_monitor_netlink_group {
912541b0
KS
65 UDEV_MONITOR_NONE,
66 UDEV_MONITOR_KERNEL,
67 UDEV_MONITOR_UDEV,
f2b93744
KS
68};
69
912541b0 70#define UDEV_MONITOR_MAGIC 0xfeedcafe
e14bdd88 71struct udev_monitor_netlink_header {
912541b0
KS
72 /* "libudev" prefix to distinguish libudev and kernel messages */
73 char prefix[8];
74 /*
75 * magic to protect against daemon <-> library message format mismatch
76 * used in the kernel from socket filter rules; needs to be stored in network order
77 */
78 unsigned int magic;
79 /* total length of header structure known to the sender */
80 unsigned int header_size;
81 /* properties string buffer */
82 unsigned int properties_off;
83 unsigned int properties_len;
84 /*
85 * hashes of primary device properties strings, to let libudev subscribers
86 * use in-kernel socket filters; values need to be stored in network order
87 */
88 unsigned int filter_subsystem_hash;
89 unsigned int filter_devtype_hash;
90 unsigned int filter_tag_bloom_hi;
91 unsigned int filter_tag_bloom_lo;
e14bdd88
KS
92};
93
94static struct udev_monitor *udev_monitor_new(struct udev *udev)
95{
912541b0
KS
96 struct udev_monitor *udev_monitor;
97
98 udev_monitor = calloc(1, sizeof(struct udev_monitor));
99 if (udev_monitor == NULL)
100 return NULL;
101 udev_monitor->refcount = 1;
102 udev_monitor->udev = udev;
103 udev_list_init(udev, &udev_monitor->filter_subsystem_list, false);
104 udev_list_init(udev, &udev_monitor->filter_tag_list, true);
105 return udev_monitor;
e14bdd88
KS
106}
107
7459bcdc 108struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd)
1c7047ea 109{
912541b0
KS
110 struct udev_monitor *udev_monitor;
111 unsigned int group;
112
113 if (udev == NULL)
114 return NULL;
115
116 if (name == NULL)
117 group = UDEV_MONITOR_NONE;
118 else if (strcmp(name, "udev") == 0)
119 group = UDEV_MONITOR_UDEV;
120 else if (strcmp(name, "kernel") == 0)
121 group = UDEV_MONITOR_KERNEL;
122 else
123 return NULL;
124
125 udev_monitor = udev_monitor_new(udev);
126 if (udev_monitor == NULL)
127 return NULL;
128
129 if (fd < 0) {
130 udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
131 if (udev_monitor->sock == -1) {
c8f8394a 132 udev_err(udev, "error getting socket: %m\n");
912541b0
KS
133 free(udev_monitor);
134 return NULL;
135 }
136 } else {
137 udev_monitor->bound = true;
138 udev_monitor->sock = fd;
139 }
140
b49d9b50
KS
141 udev_monitor->snl.nl.nl_family = AF_NETLINK;
142 udev_monitor->snl.nl.nl_groups = group;
912541b0
KS
143
144 /* default destination for sending */
b49d9b50
KS
145 udev_monitor->snl_destination.nl.nl_family = AF_NETLINK;
146 udev_monitor->snl_destination.nl.nl_groups = UDEV_MONITOR_UDEV;
912541b0 147
912541b0 148 return udev_monitor;
1c7047ea
KS
149}
150
7459bcdc
KS
151/**
152 * udev_monitor_new_from_netlink:
153 * @udev: udev library context
154 * @name: name of event source
155 *
156 * Create new udev monitor and connect to a specified event
157 * source. Valid sources identifiers are "udev" and "kernel".
158 *
159 * Applications should usually not connect directly to the
160 * "kernel" events, because the devices might not be useable
161 * at that time, before udev has configured them, and created
50579295
KS
162 * device nodes. Accessing devices at the same time as udev,
163 * might result in unpredictable behavior. The "udev" events
164 * are sent out after udev has finished its event processing,
165 * all rules have been processed, and needed device nodes are
166 * created.
7459bcdc
KS
167 *
168 * The initial refcount is 1, and needs to be decremented to
169 * release the resources of the udev monitor.
170 *
171 * Returns: a new udev monitor, or #NULL, in case of an error
172 **/
54cf0b7f 173_public_ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
7459bcdc 174{
912541b0 175 return udev_monitor_new_from_netlink_fd(udev, name, -1);
7459bcdc
KS
176}
177
e14bdd88 178static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
912541b0 179 unsigned short code, unsigned int data)
e14bdd88 180{
912541b0 181 struct sock_filter *ins = &inss[*i];
e14bdd88 182
912541b0
KS
183 ins->code = code;
184 ins->k = data;
185 (*i)++;
e14bdd88
KS
186}
187
188static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i,
912541b0
KS
189 unsigned short code, unsigned int data,
190 unsigned short jt, unsigned short jf)
e14bdd88 191{
912541b0 192 struct sock_filter *ins = &inss[*i];
e14bdd88 193
912541b0
KS
194 ins->code = code;
195 ins->jt = jt;
196 ins->jf = jf;
197 ins->k = data;
198 (*i)++;
e14bdd88
KS
199}
200
ce1d6d7f
KS
201/**
202 * udev_monitor_filter_update:
203 * @udev_monitor: monitor
204 *
50579295
KS
205 * Update the installed socket filter. This is only needed,
206 * if the filter was removed or changed.
ce1d6d7f
KS
207 *
208 * Returns: 0 on success, otherwise a negative error value.
209 */
54cf0b7f 210_public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
e14bdd88 211{
912541b0
KS
212 struct sock_filter ins[512];
213 struct sock_fprog filter;
214 unsigned int i;
215 struct udev_list_entry *list_entry;
216 int err;
217
218 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL &&
219 udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
220 return 0;
221
222 memset(ins, 0x00, sizeof(ins));
223 i = 0;
224
225 /* load magic in A */
226 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic));
227 /* jump if magic matches */
228 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
229 /* wrong magic, pass packet */
230 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
231
232 if (udev_list_get_entry(&udev_monitor->filter_tag_list) != NULL) {
233 int tag_matches;
234
235 /* count tag matches, to calculate end of tag match block */
236 tag_matches = 0;
237 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list))
238 tag_matches++;
239
240 /* add all tags matches */
241 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
242 uint64_t tag_bloom_bits = util_string_bloom64(udev_list_entry_get_name(list_entry));
243 uint32_t tag_bloom_hi = tag_bloom_bits >> 32;
244 uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff;
245
246 /* load device bloom bits in A */
247 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi));
248 /* clear bits (tag bits & bloom bits) */
249 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi);
250 /* jump to next tag if it does not match */
251 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3);
252
253 /* load device bloom bits in A */
254 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo));
255 /* clear bits (tag bits & bloom bits) */
256 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo);
257 /* jump behind end of tag match block if tag matches */
258 tag_matches--;
259 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0);
260 }
261
262 /* nothing matched, drop packet */
263 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
264 }
265
266 /* add all subsystem matches */
267 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) != NULL) {
268 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
269 unsigned int hash = util_string_hash32(udev_list_entry_get_name(list_entry));
270
271 /* load device subsystem value in A */
272 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash));
273 if (udev_list_entry_get_value(list_entry) == NULL) {
274 /* jump if subsystem does not match */
275 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
276 } else {
277 /* jump if subsystem does not match */
278 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
279
280 /* load device devtype value in A */
281 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash));
282 /* jump if value does not match */
283 hash = util_string_hash32(udev_list_entry_get_value(list_entry));
284 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
285 }
286
287 /* matched, pass packet */
288 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
289
8fef0ff2 290 if (i+1 >= ELEMENTSOF(ins))
912541b0
KS
291 return -1;
292 }
293
294 /* nothing matched, drop packet */
295 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
296 }
297
298 /* matched, pass packet */
299 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
300
301 /* install filter */
302 memset(&filter, 0x00, sizeof(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;
e14bdd88
KS
307}
308
1e03b754
KS
309int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
310{
b49d9b50 311 udev_monitor->snl_trusted_sender.nl.nl_pid = sender->snl.nl.nl_pid;
912541b0 312 return 0;
1e03b754 313}
ce1d6d7f
KS
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 */
54cf0b7f 322_public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
d59f11e1 323{
912541b0
KS
324 int err = 0;
325 const int on = 1;
326
b49d9b50 327 if (udev_monitor->snl.nl.nl_family == 0)
912541b0 328 return -EINVAL;
2d13da88
KS
329
330 udev_monitor_filter_update(udev_monitor);
331
332 if (!udev_monitor->bound) {
333 err = bind(udev_monitor->sock,
b49d9b50 334 &udev_monitor->snl.sa, sizeof(struct sockaddr_nl));
2d13da88
KS
335 if (err == 0)
336 udev_monitor->bound = true;
912541b0
KS
337 }
338
2d13da88 339 if (err >= 0) {
b49d9b50 340 union sockaddr_union snl;
2d13da88
KS
341 socklen_t addrlen;
342
343 /*
344 * get the address the kernel has assigned us
345 * it is usually, but not necessarily the pid
346 */
347 addrlen = sizeof(struct sockaddr_nl);
b49d9b50 348 err = getsockname(udev_monitor->sock, &snl.sa, &addrlen);
2d13da88 349 if (err == 0)
b49d9b50 350 udev_monitor->snl.nl.nl_pid = snl.nl.nl_pid;
2d13da88 351 } else {
c8f8394a 352 udev_err(udev_monitor->udev, "bind failed: %m\n");
912541b0
KS
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;
ba6929f6
KS
359}
360
f712894d
KS
361/**
362 * udev_monitor_set_receive_buffer_size:
363 * @udev_monitor: the monitor which should receive events
364 * @size: the size in bytes
365 *
366 * Set the size of the kernel socket buffer. This call needs the
367 * appropriate privileges to succeed.
368 *
369 * Returns: 0 on success, otherwise -1 on error.
370 */
54cf0b7f 371_public_ int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
cb25a958 372{
912541b0
KS
373 if (udev_monitor == NULL)
374 return -1;
375 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
cb25a958
KS
376}
377
1e03b754
KS
378int udev_monitor_disconnect(struct udev_monitor *udev_monitor)
379{
912541b0 380 int err;
1e03b754 381
912541b0
KS
382 err = close(udev_monitor->sock);
383 udev_monitor->sock = -1;
384 return err;
1e03b754
KS
385}
386
7d8787b3
KS
387/**
388 * udev_monitor_ref:
389 * @udev_monitor: udev monitor
390 *
391 * Take a reference of a udev monitor.
392 *
393 * Returns: the passed udev monitor
394 **/
54cf0b7f 395_public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
ba6929f6 396{
912541b0
KS
397 if (udev_monitor == NULL)
398 return NULL;
399 udev_monitor->refcount++;
400 return udev_monitor;
ba6929f6
KS
401}
402
7d8787b3
KS
403/**
404 * udev_monitor_unref:
405 * @udev_monitor: udev monitor
406 *
ff109b8d 407 * Drop a reference of a udev monitor. If the refcount reaches zero,
be7de409 408 * the bound socket will be closed, and the resources of the monitor
7d8787b3
KS
409 * will be released.
410 *
c1959569 411 * Returns: the passed udev monitor if it has still an active reference, or #NULL otherwise.
7d8787b3 412 **/
20bbd54f 413_public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monitor)
ba6929f6 414{
912541b0 415 if (udev_monitor == NULL)
20bbd54f 416 return NULL;
912541b0
KS
417 udev_monitor->refcount--;
418 if (udev_monitor->refcount > 0)
20bbd54f 419 return udev_monitor;
912541b0
KS
420 if (udev_monitor->sock >= 0)
421 close(udev_monitor->sock);
422 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
423 udev_list_cleanup(&udev_monitor->filter_tag_list);
912541b0 424 free(udev_monitor);
20bbd54f 425 return NULL;
ba6929f6
KS
426}
427
7d8787b3
KS
428/**
429 * udev_monitor_get_udev:
430 * @udev_monitor: udev monitor
431 *
b98fd840 432 * Retrieve the udev library context the monitor was created with.
7d8787b3
KS
433 *
434 * Returns: the udev library context
435 **/
54cf0b7f 436_public_ struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
ba6929f6 437{
912541b0
KS
438 if (udev_monitor == NULL)
439 return NULL;
440 return udev_monitor->udev;
ba6929f6
KS
441}
442
7d8787b3
KS
443/**
444 * udev_monitor_get_fd:
445 * @udev_monitor: udev monitor
446 *
447 * Retrieve the socket file descriptor associated with the monitor.
448 *
449 * Returns: the socket file descriptor
450 **/
54cf0b7f 451_public_ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
ba6929f6 452{
912541b0
KS
453 if (udev_monitor == NULL)
454 return -1;
455 return udev_monitor->sock;
ba6929f6
KS
456}
457
e14bdd88
KS
458static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
459{
912541b0
KS
460 struct udev_list_entry *list_entry;
461
462 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
463 goto tag;
464 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
465 const char *subsys = udev_list_entry_get_name(list_entry);
466 const char *dsubsys = udev_device_get_subsystem(udev_device);
467 const char *devtype;
468 const char *ddevtype;
469
470 if (strcmp(dsubsys, subsys) != 0)
471 continue;
472
473 devtype = udev_list_entry_get_value(list_entry);
474 if (devtype == NULL)
475 goto tag;
476 ddevtype = udev_device_get_devtype(udev_device);
477 if (ddevtype == NULL)
478 continue;
479 if (strcmp(ddevtype, devtype) == 0)
480 goto tag;
481 }
482 return 0;
28460195
KS
483
484tag:
912541b0
KS
485 if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
486 return 1;
487 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
488 const char *tag = udev_list_entry_get_name(list_entry);
489
490 if (udev_device_has_tag(udev_device, tag))
491 return 1;
492 }
493 return 0;
e14bdd88
KS
494}
495
7d8787b3 496/**
d59f11e1 497 * udev_monitor_receive_device:
7d8787b3
KS
498 * @udev_monitor: udev monitor
499 *
d59f11e1 500 * Receive data from the udev monitor socket, allocate a new udev
b98fd840 501 * device, fill in the received data, and return the device.
7d8787b3 502 *
50579295 503 * Only socket connections with uid=0 are accepted.
7d8787b3 504 *
b30b4260
KS
505 * The monitor socket is by default set to NONBLOCK. A variant of poll() on
506 * the file descriptor returned by udev_monitor_get_fd() should to be used to
507 * wake up when new devices arrive, or alternatively the file descriptor
508 * switched into blocking mode.
509 *
7d8787b3 510 * The initial refcount is 1, and needs to be decremented to
be7de409 511 * release the resources of the udev device.
7d8787b3
KS
512 *
513 * Returns: a new udev device, or #NULL, in case of an error
514 **/
54cf0b7f 515_public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
ba6929f6 516{
912541b0
KS
517 struct udev_device *udev_device;
518 struct msghdr smsg;
519 struct iovec iov;
520 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
521 struct cmsghdr *cmsg;
b49d9b50 522 union sockaddr_union snl;
912541b0
KS
523 struct ucred *cred;
524 char buf[8192];
525 ssize_t buflen;
526 ssize_t bufpos;
527 struct udev_monitor_netlink_header *nlh;
ba6929f6 528
e14bdd88 529retry:
912541b0
KS
530 if (udev_monitor == NULL)
531 return NULL;
912541b0
KS
532 iov.iov_base = &buf;
533 iov.iov_len = sizeof(buf);
534 memset (&smsg, 0x00, sizeof(struct msghdr));
535 smsg.msg_iov = &iov;
536 smsg.msg_iovlen = 1;
537 smsg.msg_control = cred_msg;
538 smsg.msg_controllen = sizeof(cred_msg);
539
b49d9b50 540 if (udev_monitor->snl.nl.nl_family != 0) {
912541b0
KS
541 smsg.msg_name = &snl;
542 smsg.msg_namelen = sizeof(snl);
543 }
544
545 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
546 if (buflen < 0) {
547 if (errno != EINTR)
c8f8394a 548 udev_dbg(udev_monitor->udev, "unable to receive message\n");
912541b0
KS
549 return NULL;
550 }
551
552 if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
c8f8394a 553 udev_dbg(udev_monitor->udev, "invalid message length\n");
912541b0
KS
554 return NULL;
555 }
556
b49d9b50
KS
557 if (udev_monitor->snl.nl.nl_family != 0) {
558 if (snl.nl.nl_groups == 0) {
912541b0 559 /* unicast message, check if we trust the sender */
b49d9b50
KS
560 if (udev_monitor->snl_trusted_sender.nl.nl_pid == 0 ||
561 snl.nl.nl_pid != udev_monitor->snl_trusted_sender.nl.nl_pid) {
c8f8394a 562 udev_dbg(udev_monitor->udev, "unicast netlink message ignored\n");
912541b0
KS
563 return NULL;
564 }
b49d9b50
KS
565 } else if (snl.nl.nl_groups == UDEV_MONITOR_KERNEL) {
566 if (snl.nl.nl_pid > 0) {
c8f8394a 567 udev_dbg(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n",
b49d9b50 568 snl.nl.nl_pid);
912541b0
KS
569 return NULL;
570 }
571 }
572 }
573
574 cmsg = CMSG_FIRSTHDR(&smsg);
575 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
c8f8394a 576 udev_dbg(udev_monitor->udev, "no sender credentials received, message ignored\n");
912541b0
KS
577 return NULL;
578 }
579
580 cred = (struct ucred *)CMSG_DATA(cmsg);
581 if (cred->uid != 0) {
c8f8394a 582 udev_dbg(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
912541b0
KS
583 return NULL;
584 }
585
586 if (memcmp(buf, "libudev", 8) == 0) {
587 /* udev message needs proper version magic */
588 nlh = (struct udev_monitor_netlink_header *) buf;
589 if (nlh->magic != htonl(UDEV_MONITOR_MAGIC)) {
c8f8394a 590 udev_err(udev_monitor->udev, "unrecognized message signature (%x != %x)\n",
912541b0
KS
591 nlh->magic, htonl(UDEV_MONITOR_MAGIC));
592 return NULL;
593 }
ee56538f 594 if (nlh->properties_off+32 > (size_t)buflen)
912541b0
KS
595 return NULL;
596 bufpos = nlh->properties_off;
597 } else {
598 /* kernel message with header */
599 bufpos = strlen(buf) + 1;
600 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
c8f8394a 601 udev_dbg(udev_monitor->udev, "invalid message length\n");
912541b0
KS
602 return NULL;
603 }
604
605 /* check message header */
606 if (strstr(buf, "@/") == NULL) {
c8f8394a 607 udev_dbg(udev_monitor->udev, "unrecognized message header\n");
912541b0
KS
608 return NULL;
609 }
610 }
611
612 udev_device = udev_device_new(udev_monitor->udev);
613 if (udev_device == NULL)
614 return NULL;
615 udev_device_set_info_loaded(udev_device);
616
617 while (bufpos < buflen) {
618 char *key;
619 size_t keylen;
620
621 key = &buf[bufpos];
622 keylen = strlen(key);
623 if (keylen == 0)
624 break;
625 bufpos += keylen + 1;
626 udev_device_add_property_from_string_parse(udev_device, key);
627 }
628
629 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
c8f8394a 630 udev_dbg(udev_monitor->udev, "missing values, invalid device\n");
912541b0
KS
631 udev_device_unref(udev_device);
632 return NULL;
633 }
634
635 /* skip device, if it does not pass the current filter */
636 if (!passes_filter(udev_monitor, udev_device)) {
637 struct pollfd pfd[1];
638 int rc;
639
640 udev_device_unref(udev_device);
641
642 /* if something is queued, get next device */
643 pfd[0].fd = udev_monitor->sock;
644 pfd[0].events = POLLIN;
645 rc = poll(pfd, 1, 0);
646 if (rc > 0)
647 goto retry;
648 return NULL;
649 }
650
651 return udev_device;
ba6929f6 652}
9925ab04 653
1e03b754 654int udev_monitor_send_device(struct udev_monitor *udev_monitor,
912541b0 655 struct udev_monitor *destination, struct udev_device *udev_device)
9925ab04 656{
912541b0
KS
657 const char *buf;
658 ssize_t blen;
659 ssize_t count;
2d13da88
KS
660 struct msghdr smsg;
661 struct iovec iov[2];
662 const char *val;
663 struct udev_monitor_netlink_header nlh;
664 struct udev_list_entry *list_entry;
665 uint64_t tag_bloom_bits;
666
b49d9b50 667 if (udev_monitor->snl.nl.nl_family == 0)
2d13da88 668 return -EINVAL;
912541b0
KS
669
670 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
671 if (blen < 32)
672 return -EINVAL;
673
2d13da88
KS
674 /* add versioned header */
675 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
676 memcpy(nlh.prefix, "libudev", 8);
677 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
678 nlh.header_size = sizeof(struct udev_monitor_netlink_header);
679 val = udev_device_get_subsystem(udev_device);
680 nlh.filter_subsystem_hash = htonl(util_string_hash32(val));
681 val = udev_device_get_devtype(udev_device);
682 if (val != NULL)
683 nlh.filter_devtype_hash = htonl(util_string_hash32(val));
684 iov[0].iov_base = &nlh;
685 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
686
687 /* add tag bloom filter */
688 tag_bloom_bits = 0;
689 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
690 tag_bloom_bits |= util_string_bloom64(udev_list_entry_get_name(list_entry));
691 if (tag_bloom_bits > 0) {
692 nlh.filter_tag_bloom_hi = htonl(tag_bloom_bits >> 32);
693 nlh.filter_tag_bloom_lo = htonl(tag_bloom_bits & 0xffffffff);
912541b0
KS
694 }
695
2d13da88
KS
696 /* add properties list */
697 nlh.properties_off = iov[0].iov_len;
698 nlh.properties_len = blen;
699 iov[1].iov_base = (char *)buf;
700 iov[1].iov_len = blen;
912541b0 701
2d13da88
KS
702 memset(&smsg, 0x00, sizeof(struct msghdr));
703 smsg.msg_iov = iov;
704 smsg.msg_iovlen = 2;
705 /*
706 * Use custom address for target, or the default one.
707 *
708 * If we send to a multicast group, we will get
709 * ECONNREFUSED, which is expected.
710 */
711 if (destination != NULL)
712 smsg.msg_name = &destination->snl;
713 else
714 smsg.msg_name = &udev_monitor->snl_destination;
715 smsg.msg_namelen = sizeof(struct sockaddr_nl);
716 count = sendmsg(udev_monitor->sock, &smsg, 0);
c8f8394a 717 udev_dbg(udev_monitor->udev, "passed %zi bytes to netlink monitor %p\n", count, udev_monitor);
2d13da88 718 return count;
9925ab04 719}
e14bdd88 720
ce1d6d7f
KS
721/**
722 * udev_monitor_filter_add_match_subsystem_devtype:
723 * @udev_monitor: the monitor
724 * @subsystem: the subsystem value to match the incoming devices against
214a6c79 725 * @devtype: the devtype value to match the incoming devices against
ce1d6d7f 726 *
50579295 727 * This filter is efficiently executed inside the kernel, and libudev subscribers
28460195
KS
728 * will usually not be woken up for devices which do not match.
729 *
ce1d6d7f
KS
730 * The filter must be installed before the monitor is switched to listening mode.
731 *
732 * Returns: 0 on success, otherwise a negative error value.
733 */
54cf0b7f 734_public_ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
e14bdd88 735{
912541b0
KS
736 if (udev_monitor == NULL)
737 return -EINVAL;
738 if (subsystem == NULL)
739 return -EINVAL;
740 if (udev_list_entry_add(&udev_monitor->filter_subsystem_list, subsystem, devtype) == NULL)
741 return -ENOMEM;
742 return 0;
e14bdd88 743}
08a7a795 744
28460195
KS
745/**
746 * udev_monitor_filter_add_match_tag:
747 * @udev_monitor: the monitor
748 * @tag: the name of a tag
749 *
50579295 750 * This filter is efficiently executed inside the kernel, and libudev subscribers
28460195
KS
751 * will usually not be woken up for devices which do not match.
752 *
753 * The filter must be installed before the monitor is switched to listening mode.
754 *
755 * Returns: 0 on success, otherwise a negative error value.
756 */
54cf0b7f 757_public_ int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag)
28460195 758{
912541b0
KS
759 if (udev_monitor == NULL)
760 return -EINVAL;
761 if (tag == NULL)
762 return -EINVAL;
763 if (udev_list_entry_add(&udev_monitor->filter_tag_list, tag, NULL) == NULL)
764 return -ENOMEM;
765 return 0;
28460195
KS
766}
767
ce1d6d7f
KS
768/**
769 * udev_monitor_filter_remove:
770 * @udev_monitor: monitor
771 *
772 * Remove all filters from monitor.
773 *
774 * Returns: 0 on success, otherwise a negative error value.
775 */
54cf0b7f 776_public_ int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
08a7a795 777{
912541b0 778 static struct sock_fprog filter = { 0, NULL };
08a7a795 779
912541b0
KS
780 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
781 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
08a7a795 782}