]>
Commit | Line | Data |
---|---|---|
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" | |
ba6929f6 | 29 | |
ce1d6d7f KS |
30 | /** |
31 | * SECTION:libudev-monitor | |
32 | * @short_description: device event source | |
33 | * | |
34 | * Connects to a device event source. | |
35 | */ | |
36 | ||
ce1d6d7f KS |
37 | /** |
38 | * udev_monitor: | |
39 | * | |
50579295 | 40 | * Opaque object handling an event source. |
ce1d6d7f | 41 | */ |
ba6929f6 | 42 | struct udev_monitor { |
912541b0 KS |
43 | struct udev *udev; |
44 | int refcount; | |
45 | int sock; | |
46 | struct sockaddr_nl snl; | |
47 | struct sockaddr_nl snl_trusted_sender; | |
48 | struct sockaddr_nl snl_destination; | |
49 | struct sockaddr_un sun; | |
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 | 56 | enum 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 | 63 | struct 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 | ||
86 | static 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 | * | |
50579295 KS |
105 | * This function should not be used in any new application. The |
106 | * kernel's netlink socket multiplexes messages to all interested | |
107 | * clients. Creating custom sockets from udev to applications | |
108 | * should be avoided. | |
109 | * | |
110 | * Create a new udev monitor and connect to a specified socket. The | |
ff109b8d KS |
111 | * path to a socket either points to an existing socket file, or if |
112 | * the socket path starts with a '@' character, an abstract namespace | |
7d8787b3 KS |
113 | * socket will be used. |
114 | * | |
ff109b8d KS |
115 | * A socket file will not be created. If it does not already exist, |
116 | * it will fall-back and connect to an abstract namespace socket with | |
117 | * the given path. The permissions adjustment of a socket file, as | |
118 | * well as the later cleanup, needs to be done by the caller. | |
119 | * | |
7d8787b3 | 120 | * The initial refcount is 1, and needs to be decremented to |
be7de409 | 121 | * release the resources of the udev monitor. |
7d8787b3 KS |
122 | * |
123 | * Returns: a new udev monitor, or #NULL, in case of an error | |
124 | **/ | |
54cf0b7f | 125 | _public_ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path) |
ba6929f6 | 126 | { |
912541b0 KS |
127 | struct udev_monitor *udev_monitor; |
128 | struct stat statbuf; | |
129 | ||
130 | if (udev == NULL) | |
131 | return NULL; | |
132 | if (socket_path == NULL) | |
133 | return NULL; | |
134 | udev_monitor = udev_monitor_new(udev); | |
135 | if (udev_monitor == NULL) | |
136 | return NULL; | |
137 | ||
138 | udev_monitor->sun.sun_family = AF_LOCAL; | |
139 | if (socket_path[0] == '@') { | |
140 | /* translate leading '@' to abstract namespace */ | |
141 | util_strscpy(udev_monitor->sun.sun_path, sizeof(udev_monitor->sun.sun_path), socket_path); | |
142 | udev_monitor->sun.sun_path[0] = '\0'; | |
143 | udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path); | |
144 | } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) { | |
145 | /* existing socket file */ | |
146 | util_strscpy(udev_monitor->sun.sun_path, sizeof(udev_monitor->sun.sun_path), socket_path); | |
147 | udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path); | |
148 | } else { | |
149 | /* no socket file, assume abstract namespace socket */ | |
150 | util_strscpy(&udev_monitor->sun.sun_path[1], sizeof(udev_monitor->sun.sun_path)-1, socket_path); | |
151 | udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1; | |
152 | } | |
153 | udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0); | |
154 | if (udev_monitor->sock == -1) { | |
155 | err(udev, "error getting socket: %m\n"); | |
156 | free(udev_monitor); | |
157 | return NULL; | |
158 | } | |
159 | ||
912541b0 | 160 | return udev_monitor; |
d59f11e1 | 161 | } |
ba6929f6 | 162 | |
7459bcdc | 163 | struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd) |
1c7047ea | 164 | { |
912541b0 KS |
165 | struct udev_monitor *udev_monitor; |
166 | unsigned int group; | |
167 | ||
168 | if (udev == NULL) | |
169 | return NULL; | |
170 | ||
171 | if (name == NULL) | |
172 | group = UDEV_MONITOR_NONE; | |
173 | else if (strcmp(name, "udev") == 0) | |
174 | group = UDEV_MONITOR_UDEV; | |
175 | else if (strcmp(name, "kernel") == 0) | |
176 | group = UDEV_MONITOR_KERNEL; | |
177 | else | |
178 | return NULL; | |
179 | ||
180 | udev_monitor = udev_monitor_new(udev); | |
181 | if (udev_monitor == NULL) | |
182 | return NULL; | |
183 | ||
184 | if (fd < 0) { | |
185 | udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT); | |
186 | if (udev_monitor->sock == -1) { | |
187 | err(udev, "error getting socket: %m\n"); | |
188 | free(udev_monitor); | |
189 | return NULL; | |
190 | } | |
191 | } else { | |
192 | udev_monitor->bound = true; | |
193 | udev_monitor->sock = fd; | |
194 | } | |
195 | ||
196 | udev_monitor->snl.nl_family = AF_NETLINK; | |
197 | udev_monitor->snl.nl_groups = group; | |
198 | ||
199 | /* default destination for sending */ | |
200 | udev_monitor->snl_destination.nl_family = AF_NETLINK; | |
201 | udev_monitor->snl_destination.nl_groups = UDEV_MONITOR_UDEV; | |
202 | ||
912541b0 | 203 | return udev_monitor; |
1c7047ea KS |
204 | } |
205 | ||
7459bcdc KS |
206 | /** |
207 | * udev_monitor_new_from_netlink: | |
208 | * @udev: udev library context | |
209 | * @name: name of event source | |
210 | * | |
211 | * Create new udev monitor and connect to a specified event | |
212 | * source. Valid sources identifiers are "udev" and "kernel". | |
213 | * | |
214 | * Applications should usually not connect directly to the | |
215 | * "kernel" events, because the devices might not be useable | |
216 | * at that time, before udev has configured them, and created | |
50579295 KS |
217 | * device nodes. Accessing devices at the same time as udev, |
218 | * might result in unpredictable behavior. The "udev" events | |
219 | * are sent out after udev has finished its event processing, | |
220 | * all rules have been processed, and needed device nodes are | |
221 | * created. | |
7459bcdc KS |
222 | * |
223 | * The initial refcount is 1, and needs to be decremented to | |
224 | * release the resources of the udev monitor. | |
225 | * | |
226 | * Returns: a new udev monitor, or #NULL, in case of an error | |
227 | **/ | |
54cf0b7f | 228 | _public_ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name) |
7459bcdc | 229 | { |
912541b0 | 230 | return udev_monitor_new_from_netlink_fd(udev, name, -1); |
7459bcdc KS |
231 | } |
232 | ||
e14bdd88 | 233 | static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i, |
912541b0 | 234 | unsigned short code, unsigned int data) |
e14bdd88 | 235 | { |
912541b0 | 236 | struct sock_filter *ins = &inss[*i]; |
e14bdd88 | 237 | |
912541b0 KS |
238 | ins->code = code; |
239 | ins->k = data; | |
240 | (*i)++; | |
e14bdd88 KS |
241 | } |
242 | ||
243 | static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i, | |
912541b0 KS |
244 | unsigned short code, unsigned int data, |
245 | unsigned short jt, unsigned short jf) | |
e14bdd88 | 246 | { |
912541b0 | 247 | struct sock_filter *ins = &inss[*i]; |
e14bdd88 | 248 | |
912541b0 KS |
249 | ins->code = code; |
250 | ins->jt = jt; | |
251 | ins->jf = jf; | |
252 | ins->k = data; | |
253 | (*i)++; | |
e14bdd88 KS |
254 | } |
255 | ||
ce1d6d7f KS |
256 | /** |
257 | * udev_monitor_filter_update: | |
258 | * @udev_monitor: monitor | |
259 | * | |
50579295 KS |
260 | * Update the installed socket filter. This is only needed, |
261 | * if the filter was removed or changed. | |
ce1d6d7f KS |
262 | * |
263 | * Returns: 0 on success, otherwise a negative error value. | |
264 | */ | |
54cf0b7f | 265 | _public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor) |
e14bdd88 | 266 | { |
912541b0 KS |
267 | struct sock_filter ins[512]; |
268 | struct sock_fprog filter; | |
269 | unsigned int i; | |
270 | struct udev_list_entry *list_entry; | |
271 | int err; | |
272 | ||
273 | if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL && | |
274 | udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL) | |
275 | return 0; | |
276 | ||
277 | memset(ins, 0x00, sizeof(ins)); | |
278 | i = 0; | |
279 | ||
280 | /* load magic in A */ | |
281 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic)); | |
282 | /* jump if magic matches */ | |
283 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0); | |
284 | /* wrong magic, pass packet */ | |
285 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); | |
286 | ||
287 | if (udev_list_get_entry(&udev_monitor->filter_tag_list) != NULL) { | |
288 | int tag_matches; | |
289 | ||
290 | /* count tag matches, to calculate end of tag match block */ | |
291 | tag_matches = 0; | |
292 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) | |
293 | tag_matches++; | |
294 | ||
295 | /* add all tags matches */ | |
296 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) { | |
297 | uint64_t tag_bloom_bits = util_string_bloom64(udev_list_entry_get_name(list_entry)); | |
298 | uint32_t tag_bloom_hi = tag_bloom_bits >> 32; | |
299 | uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff; | |
300 | ||
301 | /* load device bloom bits in A */ | |
302 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi)); | |
303 | /* clear bits (tag bits & bloom bits) */ | |
304 | bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi); | |
305 | /* jump to next tag if it does not match */ | |
306 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3); | |
307 | ||
308 | /* load device bloom bits in A */ | |
309 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo)); | |
310 | /* clear bits (tag bits & bloom bits) */ | |
311 | bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo); | |
312 | /* jump behind end of tag match block if tag matches */ | |
313 | tag_matches--; | |
314 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0); | |
315 | } | |
316 | ||
317 | /* nothing matched, drop packet */ | |
318 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0); | |
319 | } | |
320 | ||
321 | /* add all subsystem matches */ | |
322 | if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) != NULL) { | |
323 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) { | |
324 | unsigned int hash = util_string_hash32(udev_list_entry_get_name(list_entry)); | |
325 | ||
326 | /* load device subsystem value in A */ | |
327 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash)); | |
328 | if (udev_list_entry_get_value(list_entry) == NULL) { | |
329 | /* jump if subsystem does not match */ | |
330 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1); | |
331 | } else { | |
332 | /* jump if subsystem does not match */ | |
333 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3); | |
334 | ||
335 | /* load device devtype value in A */ | |
336 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash)); | |
337 | /* jump if value does not match */ | |
338 | hash = util_string_hash32(udev_list_entry_get_value(list_entry)); | |
339 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1); | |
340 | } | |
341 | ||
342 | /* matched, pass packet */ | |
343 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); | |
344 | ||
345 | if (i+1 >= ARRAY_SIZE(ins)) | |
346 | return -1; | |
347 | } | |
348 | ||
349 | /* nothing matched, drop packet */ | |
350 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0); | |
351 | } | |
352 | ||
353 | /* matched, pass packet */ | |
354 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); | |
355 | ||
356 | /* install filter */ | |
357 | memset(&filter, 0x00, sizeof(filter)); | |
358 | filter.len = i; | |
359 | filter.filter = ins; | |
360 | err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)); | |
361 | return err; | |
e14bdd88 KS |
362 | } |
363 | ||
1e03b754 KS |
364 | int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender) |
365 | { | |
912541b0 KS |
366 | udev_monitor->snl_trusted_sender.nl_pid = sender->snl.nl_pid; |
367 | return 0; | |
1e03b754 | 368 | } |
ce1d6d7f KS |
369 | /** |
370 | * udev_monitor_enable_receiving: | |
371 | * @udev_monitor: the monitor which should receive events | |
372 | * | |
373 | * Binds the @udev_monitor socket to the event source. | |
374 | * | |
375 | * Returns: 0 on success, otherwise a negative error value. | |
376 | */ | |
54cf0b7f | 377 | _public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor) |
d59f11e1 | 378 | { |
912541b0 KS |
379 | int err = 0; |
380 | const int on = 1; | |
381 | ||
382 | if (udev_monitor->sun.sun_family != 0) { | |
383 | if (!udev_monitor->bound) { | |
384 | err = bind(udev_monitor->sock, | |
385 | (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen); | |
386 | if (err == 0) | |
387 | udev_monitor->bound = true; | |
388 | } | |
389 | } else if (udev_monitor->snl.nl_family != 0) { | |
390 | udev_monitor_filter_update(udev_monitor); | |
391 | if (!udev_monitor->bound) { | |
392 | err = bind(udev_monitor->sock, | |
393 | (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl)); | |
394 | if (err == 0) | |
395 | udev_monitor->bound = true; | |
396 | } | |
397 | if (err == 0) { | |
398 | struct sockaddr_nl snl; | |
399 | socklen_t addrlen; | |
400 | ||
401 | /* | |
402 | * get the address the kernel has assigned us | |
403 | * it is usually, but not necessarily the pid | |
404 | */ | |
405 | addrlen = sizeof(struct sockaddr_nl); | |
406 | err = getsockname(udev_monitor->sock, (struct sockaddr *)&snl, &addrlen); | |
407 | if (err == 0) | |
408 | udev_monitor->snl.nl_pid = snl.nl_pid; | |
409 | } | |
410 | } else { | |
411 | return -EINVAL; | |
412 | } | |
413 | ||
414 | if (err < 0) { | |
415 | err(udev_monitor->udev, "bind failed: %m\n"); | |
416 | return err; | |
417 | } | |
418 | ||
419 | /* enable receiving of sender credentials */ | |
420 | setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); | |
421 | return 0; | |
ba6929f6 KS |
422 | } |
423 | ||
f712894d KS |
424 | /** |
425 | * udev_monitor_set_receive_buffer_size: | |
426 | * @udev_monitor: the monitor which should receive events | |
427 | * @size: the size in bytes | |
428 | * | |
429 | * Set the size of the kernel socket buffer. This call needs the | |
430 | * appropriate privileges to succeed. | |
431 | * | |
432 | * Returns: 0 on success, otherwise -1 on error. | |
433 | */ | |
54cf0b7f | 434 | _public_ int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size) |
cb25a958 | 435 | { |
912541b0 KS |
436 | if (udev_monitor == NULL) |
437 | return -1; | |
438 | return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size)); | |
cb25a958 KS |
439 | } |
440 | ||
1e03b754 KS |
441 | int udev_monitor_disconnect(struct udev_monitor *udev_monitor) |
442 | { | |
912541b0 | 443 | int err; |
1e03b754 | 444 | |
912541b0 KS |
445 | err = close(udev_monitor->sock); |
446 | udev_monitor->sock = -1; | |
447 | return err; | |
1e03b754 KS |
448 | } |
449 | ||
7d8787b3 KS |
450 | /** |
451 | * udev_monitor_ref: | |
452 | * @udev_monitor: udev monitor | |
453 | * | |
454 | * Take a reference of a udev monitor. | |
455 | * | |
456 | * Returns: the passed udev monitor | |
457 | **/ | |
54cf0b7f | 458 | _public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor) |
ba6929f6 | 459 | { |
912541b0 KS |
460 | if (udev_monitor == NULL) |
461 | return NULL; | |
462 | udev_monitor->refcount++; | |
463 | return udev_monitor; | |
ba6929f6 KS |
464 | } |
465 | ||
7d8787b3 KS |
466 | /** |
467 | * udev_monitor_unref: | |
468 | * @udev_monitor: udev monitor | |
469 | * | |
ff109b8d | 470 | * Drop a reference of a udev monitor. If the refcount reaches zero, |
be7de409 | 471 | * the bound socket will be closed, and the resources of the monitor |
7d8787b3 KS |
472 | * will be released. |
473 | * | |
474 | **/ | |
54cf0b7f | 475 | _public_ void udev_monitor_unref(struct udev_monitor *udev_monitor) |
ba6929f6 | 476 | { |
912541b0 KS |
477 | if (udev_monitor == NULL) |
478 | return; | |
479 | udev_monitor->refcount--; | |
480 | if (udev_monitor->refcount > 0) | |
481 | return; | |
482 | if (udev_monitor->sock >= 0) | |
483 | close(udev_monitor->sock); | |
484 | udev_list_cleanup(&udev_monitor->filter_subsystem_list); | |
485 | udev_list_cleanup(&udev_monitor->filter_tag_list); | |
912541b0 | 486 | free(udev_monitor); |
ba6929f6 KS |
487 | } |
488 | ||
7d8787b3 KS |
489 | /** |
490 | * udev_monitor_get_udev: | |
491 | * @udev_monitor: udev monitor | |
492 | * | |
b98fd840 | 493 | * Retrieve the udev library context the monitor was created with. |
7d8787b3 KS |
494 | * |
495 | * Returns: the udev library context | |
496 | **/ | |
54cf0b7f | 497 | _public_ struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor) |
ba6929f6 | 498 | { |
912541b0 KS |
499 | if (udev_monitor == NULL) |
500 | return NULL; | |
501 | return udev_monitor->udev; | |
ba6929f6 KS |
502 | } |
503 | ||
7d8787b3 KS |
504 | /** |
505 | * udev_monitor_get_fd: | |
506 | * @udev_monitor: udev monitor | |
507 | * | |
508 | * Retrieve the socket file descriptor associated with the monitor. | |
509 | * | |
510 | * Returns: the socket file descriptor | |
511 | **/ | |
54cf0b7f | 512 | _public_ int udev_monitor_get_fd(struct udev_monitor *udev_monitor) |
ba6929f6 | 513 | { |
912541b0 KS |
514 | if (udev_monitor == NULL) |
515 | return -1; | |
516 | return udev_monitor->sock; | |
ba6929f6 KS |
517 | } |
518 | ||
e14bdd88 KS |
519 | static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device) |
520 | { | |
912541b0 KS |
521 | struct udev_list_entry *list_entry; |
522 | ||
523 | if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL) | |
524 | goto tag; | |
525 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) { | |
526 | const char *subsys = udev_list_entry_get_name(list_entry); | |
527 | const char *dsubsys = udev_device_get_subsystem(udev_device); | |
528 | const char *devtype; | |
529 | const char *ddevtype; | |
530 | ||
531 | if (strcmp(dsubsys, subsys) != 0) | |
532 | continue; | |
533 | ||
534 | devtype = udev_list_entry_get_value(list_entry); | |
535 | if (devtype == NULL) | |
536 | goto tag; | |
537 | ddevtype = udev_device_get_devtype(udev_device); | |
538 | if (ddevtype == NULL) | |
539 | continue; | |
540 | if (strcmp(ddevtype, devtype) == 0) | |
541 | goto tag; | |
542 | } | |
543 | return 0; | |
28460195 KS |
544 | |
545 | tag: | |
912541b0 KS |
546 | if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL) |
547 | return 1; | |
548 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) { | |
549 | const char *tag = udev_list_entry_get_name(list_entry); | |
550 | ||
551 | if (udev_device_has_tag(udev_device, tag)) | |
552 | return 1; | |
553 | } | |
554 | return 0; | |
e14bdd88 KS |
555 | } |
556 | ||
7d8787b3 | 557 | /** |
d59f11e1 | 558 | * udev_monitor_receive_device: |
7d8787b3 KS |
559 | * @udev_monitor: udev monitor |
560 | * | |
d59f11e1 | 561 | * Receive data from the udev monitor socket, allocate a new udev |
b98fd840 | 562 | * device, fill in the received data, and return the device. |
7d8787b3 | 563 | * |
50579295 | 564 | * Only socket connections with uid=0 are accepted. |
7d8787b3 KS |
565 | * |
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; | |
578 | struct sockaddr_nl snl; | |
579 | struct ucred *cred; | |
580 | char buf[8192]; | |
581 | ssize_t buflen; | |
582 | ssize_t bufpos; | |
583 | struct udev_monitor_netlink_header *nlh; | |
ba6929f6 | 584 | |
e14bdd88 | 585 | retry: |
912541b0 KS |
586 | if (udev_monitor == NULL) |
587 | return NULL; | |
912541b0 KS |
588 | iov.iov_base = &buf; |
589 | iov.iov_len = sizeof(buf); | |
590 | memset (&smsg, 0x00, sizeof(struct msghdr)); | |
591 | smsg.msg_iov = &iov; | |
592 | smsg.msg_iovlen = 1; | |
593 | smsg.msg_control = cred_msg; | |
594 | smsg.msg_controllen = sizeof(cred_msg); | |
595 | ||
596 | if (udev_monitor->snl.nl_family != 0) { | |
597 | smsg.msg_name = &snl; | |
598 | smsg.msg_namelen = sizeof(snl); | |
599 | } | |
600 | ||
601 | buflen = recvmsg(udev_monitor->sock, &smsg, 0); | |
602 | if (buflen < 0) { | |
603 | if (errno != EINTR) | |
baa30fbc | 604 | dbg(udev_monitor->udev, "unable to receive message\n"); |
912541b0 KS |
605 | return NULL; |
606 | } | |
607 | ||
608 | if (buflen < 32 || (size_t)buflen >= sizeof(buf)) { | |
baa30fbc | 609 | dbg(udev_monitor->udev, "invalid message length\n"); |
912541b0 KS |
610 | return NULL; |
611 | } | |
612 | ||
613 | if (udev_monitor->snl.nl_family != 0) { | |
614 | if (snl.nl_groups == 0) { | |
615 | /* unicast message, check if we trust the sender */ | |
616 | if (udev_monitor->snl_trusted_sender.nl_pid == 0 || | |
617 | snl.nl_pid != udev_monitor->snl_trusted_sender.nl_pid) { | |
baa30fbc | 618 | dbg(udev_monitor->udev, "unicast netlink message ignored\n"); |
912541b0 KS |
619 | return NULL; |
620 | } | |
621 | } else if (snl.nl_groups == UDEV_MONITOR_KERNEL) { | |
622 | if (snl.nl_pid > 0) { | |
baa30fbc | 623 | dbg(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", |
912541b0 KS |
624 | snl.nl_pid); |
625 | return NULL; | |
626 | } | |
627 | } | |
628 | } | |
629 | ||
630 | cmsg = CMSG_FIRSTHDR(&smsg); | |
631 | if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) { | |
baa30fbc | 632 | dbg(udev_monitor->udev, "no sender credentials received, message ignored\n"); |
912541b0 KS |
633 | return NULL; |
634 | } | |
635 | ||
636 | cred = (struct ucred *)CMSG_DATA(cmsg); | |
637 | if (cred->uid != 0) { | |
baa30fbc | 638 | dbg(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid); |
912541b0 KS |
639 | return NULL; |
640 | } | |
641 | ||
642 | if (memcmp(buf, "libudev", 8) == 0) { | |
643 | /* udev message needs proper version magic */ | |
644 | nlh = (struct udev_monitor_netlink_header *) buf; | |
645 | if (nlh->magic != htonl(UDEV_MONITOR_MAGIC)) { | |
646 | err(udev_monitor->udev, "unrecognized message signature (%x != %x)\n", | |
647 | nlh->magic, htonl(UDEV_MONITOR_MAGIC)); | |
648 | return NULL; | |
649 | } | |
650 | if (nlh->properties_off+32 > buflen) | |
651 | return NULL; | |
652 | bufpos = nlh->properties_off; | |
653 | } else { | |
654 | /* kernel message with header */ | |
655 | bufpos = strlen(buf) + 1; | |
656 | if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) { | |
baa30fbc | 657 | dbg(udev_monitor->udev, "invalid message length\n"); |
912541b0 KS |
658 | return NULL; |
659 | } | |
660 | ||
661 | /* check message header */ | |
662 | if (strstr(buf, "@/") == NULL) { | |
baa30fbc | 663 | dbg(udev_monitor->udev, "unrecognized message header\n"); |
912541b0 KS |
664 | return NULL; |
665 | } | |
666 | } | |
667 | ||
668 | udev_device = udev_device_new(udev_monitor->udev); | |
669 | if (udev_device == NULL) | |
670 | return NULL; | |
671 | udev_device_set_info_loaded(udev_device); | |
672 | ||
673 | while (bufpos < buflen) { | |
674 | char *key; | |
675 | size_t keylen; | |
676 | ||
677 | key = &buf[bufpos]; | |
678 | keylen = strlen(key); | |
679 | if (keylen == 0) | |
680 | break; | |
681 | bufpos += keylen + 1; | |
682 | udev_device_add_property_from_string_parse(udev_device, key); | |
683 | } | |
684 | ||
685 | if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) { | |
baa30fbc | 686 | dbg(udev_monitor->udev, "missing values, invalid device\n"); |
912541b0 KS |
687 | udev_device_unref(udev_device); |
688 | return NULL; | |
689 | } | |
690 | ||
691 | /* skip device, if it does not pass the current filter */ | |
692 | if (!passes_filter(udev_monitor, udev_device)) { | |
693 | struct pollfd pfd[1]; | |
694 | int rc; | |
695 | ||
696 | udev_device_unref(udev_device); | |
697 | ||
698 | /* if something is queued, get next device */ | |
699 | pfd[0].fd = udev_monitor->sock; | |
700 | pfd[0].events = POLLIN; | |
701 | rc = poll(pfd, 1, 0); | |
702 | if (rc > 0) | |
703 | goto retry; | |
704 | return NULL; | |
705 | } | |
706 | ||
707 | return udev_device; | |
ba6929f6 | 708 | } |
9925ab04 | 709 | |
1e03b754 | 710 | int udev_monitor_send_device(struct udev_monitor *udev_monitor, |
912541b0 | 711 | struct udev_monitor *destination, struct udev_device *udev_device) |
9925ab04 | 712 | { |
912541b0 KS |
713 | const char *buf; |
714 | ssize_t blen; | |
715 | ssize_t count; | |
716 | ||
717 | blen = udev_device_get_properties_monitor_buf(udev_device, &buf); | |
718 | if (blen < 32) | |
719 | return -EINVAL; | |
720 | ||
721 | if (udev_monitor->sun.sun_family != 0) { | |
722 | struct msghdr smsg; | |
723 | struct iovec iov[2]; | |
724 | const char *action; | |
725 | char header[2048]; | |
726 | char *s; | |
727 | ||
728 | /* header <action>@<devpath> */ | |
729 | action = udev_device_get_action(udev_device); | |
730 | if (action == NULL) | |
731 | return -EINVAL; | |
732 | s = header; | |
733 | if (util_strpcpyl(&s, sizeof(header), action, "@", udev_device_get_devpath(udev_device), NULL) == 0) | |
734 | return -EINVAL; | |
735 | iov[0].iov_base = header; | |
736 | iov[0].iov_len = (s - header)+1; | |
737 | ||
738 | /* add properties list */ | |
739 | iov[1].iov_base = (char *)buf; | |
740 | iov[1].iov_len = blen; | |
741 | ||
742 | memset(&smsg, 0x00, sizeof(struct msghdr)); | |
743 | smsg.msg_iov = iov; | |
744 | smsg.msg_iovlen = 2; | |
745 | smsg.msg_name = &udev_monitor->sun; | |
746 | smsg.msg_namelen = udev_monitor->addrlen; | |
747 | count = sendmsg(udev_monitor->sock, &smsg, 0); | |
baa30fbc | 748 | dbg(udev_monitor->udev, "passed %zi bytes to socket monitor %p\n", count, udev_monitor); |
912541b0 KS |
749 | return count; |
750 | } | |
751 | ||
752 | if (udev_monitor->snl.nl_family != 0) { | |
753 | struct msghdr smsg; | |
754 | struct iovec iov[2]; | |
755 | const char *val; | |
756 | struct udev_monitor_netlink_header nlh; | |
757 | struct udev_list_entry *list_entry; | |
758 | uint64_t tag_bloom_bits; | |
759 | ||
760 | /* add versioned header */ | |
761 | memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header)); | |
762 | memcpy(nlh.prefix, "libudev", 8); | |
763 | nlh.magic = htonl(UDEV_MONITOR_MAGIC); | |
764 | nlh.header_size = sizeof(struct udev_monitor_netlink_header); | |
765 | val = udev_device_get_subsystem(udev_device); | |
766 | nlh.filter_subsystem_hash = htonl(util_string_hash32(val)); | |
767 | val = udev_device_get_devtype(udev_device); | |
768 | if (val != NULL) | |
769 | nlh.filter_devtype_hash = htonl(util_string_hash32(val)); | |
770 | iov[0].iov_base = &nlh; | |
771 | iov[0].iov_len = sizeof(struct udev_monitor_netlink_header); | |
772 | ||
773 | /* add tag bloom filter */ | |
774 | tag_bloom_bits = 0; | |
775 | udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device)) | |
776 | tag_bloom_bits |= util_string_bloom64(udev_list_entry_get_name(list_entry)); | |
777 | if (tag_bloom_bits > 0) { | |
778 | nlh.filter_tag_bloom_hi = htonl(tag_bloom_bits >> 32); | |
779 | nlh.filter_tag_bloom_lo = htonl(tag_bloom_bits & 0xffffffff); | |
780 | } | |
781 | ||
782 | /* add properties list */ | |
783 | nlh.properties_off = iov[0].iov_len; | |
784 | nlh.properties_len = blen; | |
785 | iov[1].iov_base = (char *)buf; | |
786 | iov[1].iov_len = blen; | |
787 | ||
788 | memset(&smsg, 0x00, sizeof(struct msghdr)); | |
789 | smsg.msg_iov = iov; | |
790 | smsg.msg_iovlen = 2; | |
791 | /* | |
792 | * Use custom address for target, or the default one. | |
793 | * | |
794 | * If we send to a multicast group, we will get | |
795 | * ECONNREFUSED, which is expected. | |
796 | */ | |
797 | if (destination != NULL) | |
798 | smsg.msg_name = &destination->snl; | |
799 | else | |
800 | smsg.msg_name = &udev_monitor->snl_destination; | |
801 | smsg.msg_namelen = sizeof(struct sockaddr_nl); | |
802 | count = sendmsg(udev_monitor->sock, &smsg, 0); | |
baa30fbc | 803 | dbg(udev_monitor->udev, "passed %zi bytes to netlink monitor %p\n", count, udev_monitor); |
912541b0 KS |
804 | return count; |
805 | } | |
806 | ||
807 | return -EINVAL; | |
9925ab04 | 808 | } |
e14bdd88 | 809 | |
ce1d6d7f KS |
810 | /** |
811 | * udev_monitor_filter_add_match_subsystem_devtype: | |
812 | * @udev_monitor: the monitor | |
813 | * @subsystem: the subsystem value to match the incoming devices against | |
214a6c79 | 814 | * @devtype: the devtype value to match the incoming devices against |
ce1d6d7f | 815 | * |
50579295 | 816 | * This filter is efficiently executed inside the kernel, and libudev subscribers |
28460195 KS |
817 | * will usually not be woken up for devices which do not match. |
818 | * | |
ce1d6d7f KS |
819 | * The filter must be installed before the monitor is switched to listening mode. |
820 | * | |
821 | * Returns: 0 on success, otherwise a negative error value. | |
822 | */ | |
54cf0b7f | 823 | _public_ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype) |
e14bdd88 | 824 | { |
912541b0 KS |
825 | if (udev_monitor == NULL) |
826 | return -EINVAL; | |
827 | if (subsystem == NULL) | |
828 | return -EINVAL; | |
829 | if (udev_list_entry_add(&udev_monitor->filter_subsystem_list, subsystem, devtype) == NULL) | |
830 | return -ENOMEM; | |
831 | return 0; | |
e14bdd88 | 832 | } |
08a7a795 | 833 | |
28460195 KS |
834 | /** |
835 | * udev_monitor_filter_add_match_tag: | |
836 | * @udev_monitor: the monitor | |
837 | * @tag: the name of a tag | |
838 | * | |
50579295 | 839 | * This filter is efficiently executed inside the kernel, and libudev subscribers |
28460195 KS |
840 | * will usually not be woken up for devices which do not match. |
841 | * | |
842 | * The filter must be installed before the monitor is switched to listening mode. | |
843 | * | |
844 | * Returns: 0 on success, otherwise a negative error value. | |
845 | */ | |
54cf0b7f | 846 | _public_ int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag) |
28460195 | 847 | { |
912541b0 KS |
848 | if (udev_monitor == NULL) |
849 | return -EINVAL; | |
850 | if (tag == NULL) | |
851 | return -EINVAL; | |
852 | if (udev_list_entry_add(&udev_monitor->filter_tag_list, tag, NULL) == NULL) | |
853 | return -ENOMEM; | |
854 | return 0; | |
28460195 KS |
855 | } |
856 | ||
ce1d6d7f KS |
857 | /** |
858 | * udev_monitor_filter_remove: | |
859 | * @udev_monitor: monitor | |
860 | * | |
861 | * Remove all filters from monitor. | |
862 | * | |
863 | * Returns: 0 on success, otherwise a negative error value. | |
864 | */ | |
54cf0b7f | 865 | _public_ int udev_monitor_filter_remove(struct udev_monitor *udev_monitor) |
08a7a795 | 866 | { |
912541b0 | 867 | static struct sock_fprog filter = { 0, NULL }; |
08a7a795 | 868 | |
912541b0 KS |
869 | udev_list_cleanup(&udev_monitor->filter_subsystem_list); |
870 | return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)); | |
08a7a795 | 871 | } |