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