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