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