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