]>
Commit | Line | Data |
---|---|---|
ba6929f6 KS |
1 | /* |
2 | * libudev - interface to udev device information | |
3 | * | |
065db052 | 4 | * Copyright (C) 2008-2009 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 | * | |
40 | * Opaque object handling one event source. | |
41 | */ | |
ba6929f6 KS |
42 | struct udev_monitor { |
43 | struct udev *udev; | |
44 | int refcount; | |
d59f11e1 | 45 | int sock; |
1c7047ea | 46 | struct sockaddr_nl snl; |
1e03b754 KS |
47 | struct sockaddr_nl snl_trusted_sender; |
48 | struct sockaddr_nl snl_destination; | |
1c7047ea | 49 | struct sockaddr_un sun; |
d59f11e1 | 50 | socklen_t addrlen; |
e14bdd88 | 51 | struct udev_list_node filter_subsystem_list; |
ba6929f6 KS |
52 | }; |
53 | ||
f2b93744 | 54 | enum udev_monitor_netlink_group { |
1e03b754 KS |
55 | UDEV_MONITOR_NONE, |
56 | UDEV_MONITOR_KERNEL, | |
57 | UDEV_MONITOR_UDEV, | |
f2b93744 KS |
58 | }; |
59 | ||
e14bdd88 KS |
60 | #define UDEV_MONITOR_MAGIC 0xcafe1dea |
61 | struct udev_monitor_netlink_header { | |
62 | /* udev version text */ | |
63 | char version[16]; | |
64 | /* | |
65 | * magic to protect against daemon <-> library message format mismatch | |
66 | * used in the kernel from socket filter rules; needs to be stored in network order | |
67 | */ | |
68 | unsigned int magic; | |
69 | /* properties buffer */ | |
70 | unsigned short properties_off; | |
71 | unsigned short properties_len; | |
72 | /* | |
dacea9ff | 73 | * hashes of some common device properties strings to filter with socket filters in |
c7dff03e KS |
74 | * the client used in the kernel from socket filter rules; needs to be stored in |
75 | * network order | |
e14bdd88 KS |
76 | */ |
77 | unsigned int filter_subsystem; | |
dacea9ff | 78 | unsigned int filter_devtype; |
e14bdd88 KS |
79 | }; |
80 | ||
81 | static struct udev_monitor *udev_monitor_new(struct udev *udev) | |
82 | { | |
83 | struct udev_monitor *udev_monitor; | |
84 | ||
85 | udev_monitor = calloc(1, sizeof(struct udev_monitor)); | |
86 | if (udev_monitor == NULL) | |
87 | return NULL; | |
88 | udev_monitor->refcount = 1; | |
89 | udev_monitor->udev = udev; | |
90 | udev_list_init(&udev_monitor->filter_subsystem_list); | |
91 | return udev_monitor; | |
92 | } | |
93 | ||
7d8787b3 KS |
94 | /** |
95 | * udev_monitor_new_from_socket: | |
96 | * @udev: udev library context | |
97 | * @socket_path: unix socket path | |
98 | * | |
ff109b8d KS |
99 | * Create new udev monitor and connect to a specified socket. The |
100 | * path to a socket either points to an existing socket file, or if | |
101 | * the socket path starts with a '@' character, an abstract namespace | |
7d8787b3 KS |
102 | * socket will be used. |
103 | * | |
ff109b8d KS |
104 | * A socket file will not be created. If it does not already exist, |
105 | * it will fall-back and connect to an abstract namespace socket with | |
106 | * the given path. The permissions adjustment of a socket file, as | |
107 | * well as the later cleanup, needs to be done by the caller. | |
108 | * | |
7d8787b3 | 109 | * The initial refcount is 1, and needs to be decremented to |
be7de409 | 110 | * release the resources of the udev monitor. |
7d8787b3 KS |
111 | * |
112 | * Returns: a new udev monitor, or #NULL, in case of an error | |
113 | **/ | |
ba6929f6 KS |
114 | struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path) |
115 | { | |
116 | struct udev_monitor *udev_monitor; | |
9925ab04 | 117 | struct stat statbuf; |
ba6929f6 KS |
118 | |
119 | if (udev == NULL) | |
120 | return NULL; | |
121 | if (socket_path == NULL) | |
122 | return NULL; | |
e14bdd88 | 123 | udev_monitor = udev_monitor_new(udev); |
ba6929f6 KS |
124 | if (udev_monitor == NULL) |
125 | return NULL; | |
ba6929f6 | 126 | |
1c7047ea | 127 | udev_monitor->sun.sun_family = AF_LOCAL; |
81d9e221 | 128 | if (socket_path[0] == '@') { |
9925ab04 | 129 | /* translate leading '@' to abstract namespace */ |
065db052 | 130 | util_strscpy(udev_monitor->sun.sun_path, sizeof(udev_monitor->sun.sun_path), socket_path); |
1c7047ea | 131 | udev_monitor->sun.sun_path[0] = '\0'; |
9925ab04 | 132 | udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path); |
81d9e221 | 133 | } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) { |
9925ab04 | 134 | /* existing socket file */ |
065db052 | 135 | util_strscpy(udev_monitor->sun.sun_path, sizeof(udev_monitor->sun.sun_path), socket_path); |
9925ab04 KS |
136 | udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path); |
137 | } else { | |
138 | /* no socket file, assume abstract namespace socket */ | |
065db052 | 139 | util_strscpy(&udev_monitor->sun.sun_path[1], sizeof(udev_monitor->sun.sun_path)-1, socket_path); |
9925ab04 KS |
140 | udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1; |
141 | } | |
26347a4c | 142 | udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0); |
d59f11e1 | 143 | if (udev_monitor->sock == -1) { |
659353f5 | 144 | err(udev, "error getting socket: %m\n"); |
ba6929f6 KS |
145 | free(udev_monitor); |
146 | return NULL; | |
147 | } | |
4b09a2fc | 148 | |
86b57788 | 149 | dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path); |
d59f11e1 KS |
150 | return udev_monitor; |
151 | } | |
ba6929f6 | 152 | |
ff109b8d KS |
153 | /** |
154 | * udev_monitor_new_from_netlink: | |
155 | * @udev: udev library context | |
156 | * @name: name of event source | |
157 | * | |
158 | * Create new udev monitor and connect to a specified event | |
159 | * source. Valid sources identifiers are "udev" and "kernel". | |
160 | * | |
161 | * Applications should usually not connect directly to the | |
162 | * "kernel" events, because the devices might not be useable | |
163 | * at that time, before udev has configured them, and created | |
164 | * device nodes. | |
165 | * | |
166 | * Accessing devices at the same time as udev, might result | |
167 | * in unpredictable behavior. | |
168 | * | |
169 | * The "udev" events are sent out after udev has finished its | |
170 | * event processing, all rules have been processed, and needed | |
171 | * device nodes are created. | |
172 | * | |
173 | * The initial refcount is 1, and needs to be decremented to | |
174 | * release the resources of the udev monitor. | |
175 | * | |
176 | * Returns: a new udev monitor, or #NULL, in case of an error | |
177 | **/ | |
f2b93744 | 178 | struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name) |
1c7047ea KS |
179 | { |
180 | struct udev_monitor *udev_monitor; | |
f2b93744 | 181 | unsigned int group; |
1c7047ea KS |
182 | |
183 | if (udev == NULL) | |
184 | return NULL; | |
f2b93744 KS |
185 | |
186 | if (name == NULL) | |
1e03b754 | 187 | group = UDEV_MONITOR_NONE; |
f2b93744 KS |
188 | else if (strcmp(name, "udev") == 0) |
189 | group = UDEV_MONITOR_UDEV; | |
1e03b754 KS |
190 | else if (strcmp(name, "kernel") == 0) |
191 | group = UDEV_MONITOR_KERNEL; | |
f2b93744 KS |
192 | else |
193 | return NULL; | |
194 | ||
e14bdd88 | 195 | udev_monitor = udev_monitor_new(udev); |
1c7047ea KS |
196 | if (udev_monitor == NULL) |
197 | return NULL; | |
1c7047ea | 198 | |
26347a4c | 199 | udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM|SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT); |
1c7047ea | 200 | if (udev_monitor->sock == -1) { |
659353f5 | 201 | err(udev, "error getting socket: %m\n"); |
1c7047ea KS |
202 | free(udev_monitor); |
203 | return NULL; | |
204 | } | |
205 | ||
206 | udev_monitor->snl.nl_family = AF_NETLINK; | |
11625409 | 207 | udev_monitor->snl.nl_groups = group; |
1e03b754 KS |
208 | |
209 | /* default destination for sending */ | |
210 | udev_monitor->snl_destination.nl_family = AF_NETLINK; | |
211 | udev_monitor->snl_destination.nl_groups = UDEV_MONITOR_UDEV; | |
1c7047ea | 212 | |
11625409 | 213 | dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor, group); |
1c7047ea KS |
214 | return udev_monitor; |
215 | } | |
216 | ||
e14bdd88 KS |
217 | static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i, |
218 | unsigned short code, unsigned int data) | |
219 | { | |
220 | struct sock_filter *ins = &inss[*i]; | |
221 | ||
222 | ins->code = code; | |
223 | ins->k = data; | |
224 | (*i)++; | |
225 | } | |
226 | ||
227 | static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i, | |
228 | unsigned short code, unsigned int data, | |
229 | unsigned short jt, unsigned short jf) | |
230 | { | |
231 | struct sock_filter *ins = &inss[*i]; | |
232 | ||
233 | ins->code = code; | |
234 | ins->jt = jt; | |
235 | ins->jf = jf; | |
236 | ins->k = data; | |
237 | (*i)++; | |
238 | } | |
239 | ||
ce1d6d7f KS |
240 | /** |
241 | * udev_monitor_filter_update: | |
242 | * @udev_monitor: monitor | |
243 | * | |
244 | * Update the installed filter. This might only be needed, if the filter was removed or changed. | |
245 | * | |
246 | * Returns: 0 on success, otherwise a negative error value. | |
247 | */ | |
19d7e87c | 248 | int udev_monitor_filter_update(struct udev_monitor *udev_monitor) |
e14bdd88 KS |
249 | { |
250 | static struct sock_filter ins[256]; | |
251 | static struct sock_fprog filter; | |
252 | unsigned int i; | |
253 | struct udev_list_entry *list_entry; | |
254 | int err; | |
255 | ||
256 | if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL) | |
257 | return 0; | |
258 | ||
259 | memset(ins, 0x00, sizeof(ins)); | |
260 | i = 0; | |
261 | ||
dacea9ff | 262 | /* load magic in A */ |
e14bdd88 KS |
263 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic)); |
264 | /* jump if magic matches */ | |
265 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0); | |
e93c38c3 KS |
266 | /* wrong magic, pass packet */ |
267 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); | |
e14bdd88 | 268 | |
e14bdd88 KS |
269 | /* add all subsystem match values */ |
270 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) { | |
c7dff03e | 271 | unsigned int hash; |
e14bdd88 | 272 | |
dacea9ff KS |
273 | /* load filter_subsystem value in A */ |
274 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem)); | |
275 | hash = util_string_hash32(udev_list_entry_get_name(list_entry)); | |
276 | if (udev_list_entry_get_value(list_entry) == NULL) { | |
277 | /* jump if subsystem does not match */ | |
278 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1); | |
279 | } else { | |
280 | /* jump if subsystem does not match */ | |
281 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3); | |
282 | ||
283 | /* load filter_devtype value in A */ | |
284 | bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype)); | |
285 | /* jump if value does not match */ | |
286 | hash = util_string_hash32(udev_list_entry_get_value(list_entry)); | |
287 | bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1); | |
288 | } | |
289 | ||
e14bdd88 KS |
290 | /* matched, pass packet */ |
291 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); | |
292 | ||
293 | if (i+1 >= ARRAY_SIZE(ins)) | |
294 | return -1; | |
295 | } | |
296 | /* nothing matched, drop packet */ | |
297 | bpf_stmt(ins, &i, BPF_RET|BPF_K, 0); | |
298 | ||
299 | /* install filter */ | |
300 | filter.len = i; | |
301 | filter.filter = ins; | |
302 | err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)); | |
303 | return err; | |
304 | } | |
305 | ||
1e03b754 KS |
306 | int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender) |
307 | { | |
308 | udev_monitor->snl_trusted_sender.nl_pid = sender->snl.nl_pid; | |
309 | return 0; | |
310 | } | |
ce1d6d7f KS |
311 | /** |
312 | * udev_monitor_enable_receiving: | |
313 | * @udev_monitor: the monitor which should receive events | |
314 | * | |
315 | * Binds the @udev_monitor socket to the event source. | |
316 | * | |
317 | * Returns: 0 on success, otherwise a negative error value. | |
318 | */ | |
d59f11e1 KS |
319 | int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor) |
320 | { | |
321 | int err; | |
322 | const int on = 1; | |
323 | ||
e14bdd88 | 324 | if (udev_monitor->sun.sun_family != 0) { |
11625409 KS |
325 | err = bind(udev_monitor->sock, |
326 | (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen); | |
e14bdd88 | 327 | } else if (udev_monitor->snl.nl_family != 0) { |
19d7e87c | 328 | udev_monitor_filter_update(udev_monitor); |
e2b362d9 KS |
329 | err = bind(udev_monitor->sock, |
330 | (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl)); | |
1e03b754 KS |
331 | if (err == 0) { |
332 | struct sockaddr_nl snl; | |
333 | socklen_t addrlen; | |
334 | ||
335 | /* | |
336 | * get the address the kernel has assigned us | |
214a6c79 | 337 | * it is usually, but not necessarily the pid |
1e03b754 KS |
338 | */ |
339 | addrlen = sizeof(struct sockaddr_nl); | |
340 | err = getsockname(udev_monitor->sock, (struct sockaddr *)&snl, &addrlen); | |
341 | if (err == 0) | |
342 | udev_monitor->snl.nl_pid = snl.nl_pid; | |
343 | } | |
e14bdd88 | 344 | } else { |
e2b362d9 | 345 | return -EINVAL; |
e14bdd88 | 346 | } |
e2b362d9 KS |
347 | |
348 | if (err < 0) { | |
349 | err(udev_monitor->udev, "bind failed: %m\n"); | |
350 | return err; | |
ba6929f6 | 351 | } |
e2b362d9 KS |
352 | |
353 | /* enable receiving of sender credentials */ | |
354 | setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); | |
d59f11e1 | 355 | return 0; |
ba6929f6 KS |
356 | } |
357 | ||
cb25a958 KS |
358 | int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size) |
359 | { | |
360 | if (udev_monitor == NULL) | |
361 | return -1; | |
362 | return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size)); | |
363 | } | |
364 | ||
1e03b754 KS |
365 | int udev_monitor_disconnect(struct udev_monitor *udev_monitor) |
366 | { | |
367 | int err; | |
368 | ||
369 | err = close(udev_monitor->sock); | |
370 | udev_monitor->sock = -1; | |
371 | return err; | |
372 | } | |
373 | ||
7d8787b3 KS |
374 | /** |
375 | * udev_monitor_ref: | |
376 | * @udev_monitor: udev monitor | |
377 | * | |
378 | * Take a reference of a udev monitor. | |
379 | * | |
380 | * Returns: the passed udev monitor | |
381 | **/ | |
ba6929f6 KS |
382 | struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor) |
383 | { | |
384 | if (udev_monitor == NULL) | |
385 | return NULL; | |
386 | udev_monitor->refcount++; | |
387 | return udev_monitor; | |
388 | } | |
389 | ||
7d8787b3 KS |
390 | /** |
391 | * udev_monitor_unref: | |
392 | * @udev_monitor: udev monitor | |
393 | * | |
ff109b8d | 394 | * Drop a reference of a udev monitor. If the refcount reaches zero, |
be7de409 | 395 | * the bound socket will be closed, and the resources of the monitor |
7d8787b3 KS |
396 | * will be released. |
397 | * | |
398 | **/ | |
ba6929f6 KS |
399 | void udev_monitor_unref(struct udev_monitor *udev_monitor) |
400 | { | |
401 | if (udev_monitor == NULL) | |
402 | return; | |
403 | udev_monitor->refcount--; | |
404 | if (udev_monitor->refcount > 0) | |
405 | return; | |
d59f11e1 KS |
406 | if (udev_monitor->sock >= 0) |
407 | close(udev_monitor->sock); | |
e14bdd88 | 408 | udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list); |
86b57788 | 409 | dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor); |
ba6929f6 KS |
410 | free(udev_monitor); |
411 | } | |
412 | ||
7d8787b3 KS |
413 | /** |
414 | * udev_monitor_get_udev: | |
415 | * @udev_monitor: udev monitor | |
416 | * | |
b98fd840 | 417 | * Retrieve the udev library context the monitor was created with. |
7d8787b3 KS |
418 | * |
419 | * Returns: the udev library context | |
420 | **/ | |
ba6929f6 KS |
421 | struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor) |
422 | { | |
423 | if (udev_monitor == NULL) | |
424 | return NULL; | |
425 | return udev_monitor->udev; | |
426 | } | |
427 | ||
7d8787b3 KS |
428 | /** |
429 | * udev_monitor_get_fd: | |
430 | * @udev_monitor: udev monitor | |
431 | * | |
432 | * Retrieve the socket file descriptor associated with the monitor. | |
433 | * | |
434 | * Returns: the socket file descriptor | |
435 | **/ | |
ba6929f6 KS |
436 | int udev_monitor_get_fd(struct udev_monitor *udev_monitor) |
437 | { | |
438 | if (udev_monitor == NULL) | |
439 | return -1; | |
d59f11e1 | 440 | return udev_monitor->sock; |
ba6929f6 KS |
441 | } |
442 | ||
e14bdd88 KS |
443 | static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device) |
444 | { | |
445 | struct udev_list_entry *list_entry; | |
e14bdd88 KS |
446 | |
447 | if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL) | |
448 | return 1; | |
449 | ||
450 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) { | |
dacea9ff KS |
451 | const char *subsys = udev_list_entry_get_name(list_entry); |
452 | const char *dsubsys = udev_device_get_subsystem(udev_device); | |
453 | const char *devtype; | |
454 | const char *ddevtype; | |
e14bdd88 | 455 | |
dacea9ff KS |
456 | if (strcmp(dsubsys, subsys) != 0) |
457 | continue; | |
458 | ||
459 | devtype = udev_list_entry_get_value(list_entry); | |
460 | if (devtype == NULL) | |
461 | return 1; | |
462 | ddevtype = udev_device_get_devtype(udev_device); | |
463 | if (ddevtype == NULL) | |
464 | continue; | |
465 | if (strcmp(ddevtype, devtype) == 0) | |
466 | return 1; | |
e14bdd88 | 467 | } |
dacea9ff | 468 | return 0; |
e14bdd88 KS |
469 | } |
470 | ||
7d8787b3 | 471 | /** |
d59f11e1 | 472 | * udev_monitor_receive_device: |
7d8787b3 KS |
473 | * @udev_monitor: udev monitor |
474 | * | |
d59f11e1 | 475 | * Receive data from the udev monitor socket, allocate a new udev |
b98fd840 | 476 | * device, fill in the received data, and return the device. |
7d8787b3 KS |
477 | * |
478 | * Only socket connections with uid=0 are accepted. The caller | |
be7de409 AJ |
479 | * needs to make sure that there is data to read from the socket. |
480 | * The call will block until the socket becomes readable. | |
7d8787b3 KS |
481 | * |
482 | * The initial refcount is 1, and needs to be decremented to | |
be7de409 | 483 | * release the resources of the udev device. |
7d8787b3 KS |
484 | * |
485 | * Returns: a new udev device, or #NULL, in case of an error | |
486 | **/ | |
d59f11e1 | 487 | struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor) |
ba6929f6 KS |
488 | { |
489 | struct udev_device *udev_device; | |
490 | struct msghdr smsg; | |
ba6929f6 | 491 | struct iovec iov; |
ba6929f6 | 492 | char cred_msg[CMSG_SPACE(sizeof(struct ucred))]; |
e2b362d9 | 493 | struct cmsghdr *cmsg; |
e86a923d | 494 | struct sockaddr_nl snl; |
e2b362d9 | 495 | struct ucred *cred; |
c7dff03e KS |
496 | char buf[8192]; |
497 | ssize_t buflen; | |
498 | ssize_t bufpos; | |
e14bdd88 | 499 | struct udev_monitor_netlink_header *nlh; |
81d9e221 | 500 | int devpath_set = 0; |
31f4b036 KS |
501 | int subsystem_set = 0; |
502 | int action_set = 0; | |
37372bbc KS |
503 | int maj = 0; |
504 | int min = 0; | |
ba6929f6 | 505 | |
e14bdd88 | 506 | retry: |
ba6929f6 KS |
507 | if (udev_monitor == NULL) |
508 | return NULL; | |
509 | memset(buf, 0x00, sizeof(buf)); | |
510 | iov.iov_base = &buf; | |
511 | iov.iov_len = sizeof(buf); | |
512 | memset (&smsg, 0x00, sizeof(struct msghdr)); | |
513 | smsg.msg_iov = &iov; | |
514 | smsg.msg_iovlen = 1; | |
515 | smsg.msg_control = cred_msg; | |
516 | smsg.msg_controllen = sizeof(cred_msg); | |
517 | ||
e86a923d SJR |
518 | if (udev_monitor->snl.nl_family != 0) { |
519 | smsg.msg_name = &snl; | |
e14bdd88 | 520 | smsg.msg_namelen = sizeof(snl); |
e86a923d SJR |
521 | } |
522 | ||
c7dff03e KS |
523 | buflen = recvmsg(udev_monitor->sock, &smsg, 0); |
524 | if (buflen < 0) { | |
ba6929f6 | 525 | if (errno != EINTR) |
e86a923d | 526 | info(udev_monitor->udev, "unable to receive message\n"); |
ba6929f6 KS |
527 | return NULL; |
528 | } | |
ba6929f6 | 529 | |
c7dff03e KS |
530 | if (buflen < 32 || (size_t)buflen >= sizeof(buf)) { |
531 | info(udev_monitor->udev, "invalid message length\n"); | |
532 | return NULL; | |
533 | } | |
534 | ||
e86a923d SJR |
535 | if (udev_monitor->snl.nl_family != 0) { |
536 | if (snl.nl_groups == 0) { | |
1e03b754 KS |
537 | /* unicast message, check if we trust the sender */ |
538 | if (udev_monitor->snl_trusted_sender.nl_pid == 0 || | |
539 | snl.nl_pid != udev_monitor->snl_trusted_sender.nl_pid) { | |
540 | info(udev_monitor->udev, "unicast netlink message ignored\n"); | |
541 | return NULL; | |
542 | } | |
543 | } else if (snl.nl_groups == UDEV_MONITOR_KERNEL) { | |
cb14f454 | 544 | if (snl.nl_pid > 0) { |
9cc94b15 KS |
545 | info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", |
546 | snl.nl_pid); | |
cb14f454 KS |
547 | return NULL; |
548 | } | |
e86a923d SJR |
549 | } |
550 | } | |
551 | ||
e2b362d9 KS |
552 | cmsg = CMSG_FIRSTHDR(&smsg); |
553 | if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) { | |
e86a923d | 554 | info(udev_monitor->udev, "no sender credentials received, message ignored\n"); |
e2b362d9 KS |
555 | return NULL; |
556 | } | |
1c7047ea | 557 | |
e2b362d9 KS |
558 | cred = (struct ucred *)CMSG_DATA(cmsg); |
559 | if (cred->uid != 0) { | |
e86a923d | 560 | info(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid); |
e2b362d9 | 561 | return NULL; |
ba6929f6 KS |
562 | } |
563 | ||
c7dff03e KS |
564 | if (strncmp(buf, "udev-", 5) == 0) { |
565 | /* udev message needs proper version magic */ | |
566 | nlh = (struct udev_monitor_netlink_header *) buf; | |
567 | if (nlh->magic != htonl(UDEV_MONITOR_MAGIC)) | |
568 | return NULL; | |
e14bdd88 KS |
569 | if (nlh->properties_off < sizeof(struct udev_monitor_netlink_header)) |
570 | return NULL; | |
93ee84ce | 571 | if (nlh->properties_off+32 > buflen) |
e14bdd88 KS |
572 | return NULL; |
573 | bufpos = nlh->properties_off; | |
574 | } else { | |
575 | /* kernel message with header */ | |
576 | bufpos = strlen(buf) + 1; | |
c7dff03e | 577 | if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) { |
e14bdd88 KS |
578 | info(udev_monitor->udev, "invalid message length\n"); |
579 | return NULL; | |
580 | } | |
ba6929f6 | 581 | |
e14bdd88 KS |
582 | /* check message header */ |
583 | if (strstr(buf, "@/") == NULL) { | |
584 | info(udev_monitor->udev, "unrecognized message header\n"); | |
585 | return NULL; | |
586 | } | |
ba6929f6 KS |
587 | } |
588 | ||
a5710160 | 589 | udev_device = udev_device_new(udev_monitor->udev); |
9cc94b15 | 590 | if (udev_device == NULL) |
ba6929f6 | 591 | return NULL; |
ba6929f6 | 592 | |
c7dff03e | 593 | while (bufpos < buflen) { |
ba6929f6 KS |
594 | char *key; |
595 | size_t keylen; | |
596 | ||
597 | key = &buf[bufpos]; | |
598 | keylen = strlen(key); | |
599 | if (keylen == 0) | |
600 | break; | |
601 | bufpos += keylen + 1; | |
602 | ||
603 | if (strncmp(key, "DEVPATH=", 8) == 0) { | |
8753fadf KS |
604 | char path[UTIL_PATH_SIZE]; |
605 | ||
065db052 | 606 | util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_monitor->udev), &key[8], NULL); |
8cd2e972 | 607 | udev_device_set_syspath(udev_device, path); |
81d9e221 | 608 | devpath_set = 1; |
ba6929f6 | 609 | } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) { |
8cd2e972 | 610 | udev_device_set_subsystem(udev_device, &key[10]); |
31f4b036 | 611 | subsystem_set = 1; |
bf8b2ae1 MH |
612 | } else if (strncmp(key, "DEVTYPE=", 8) == 0) { |
613 | udev_device_set_devtype(udev_device, &key[8]); | |
ba6929f6 | 614 | } else if (strncmp(key, "DEVNAME=", 8) == 0) { |
9cc94b15 | 615 | if (key[8] == '/') |
cb14f454 | 616 | udev_device_set_devnode(udev_device, &key[8]); |
9cc94b15 KS |
617 | else |
618 | udev_device_set_knodename(udev_device, &key[8]); | |
ba6929f6 | 619 | } else if (strncmp(key, "DEVLINKS=", 9) == 0) { |
1e61ff54 KS |
620 | char devlinks[UTIL_PATH_SIZE]; |
621 | char *slink; | |
622 | char *next; | |
ba6929f6 | 623 | |
065db052 | 624 | util_strscpy(devlinks, sizeof(devlinks), &key[9]); |
1e61ff54 KS |
625 | slink = devlinks; |
626 | next = strchr(slink, ' '); | |
ba6929f6 KS |
627 | while (next != NULL) { |
628 | next[0] = '\0'; | |
6c29f2b9 | 629 | udev_device_add_devlink(udev_device, slink, 0); |
ba6929f6 KS |
630 | slink = &next[1]; |
631 | next = strchr(slink, ' '); | |
632 | } | |
633 | if (slink[0] != '\0') | |
6c29f2b9 | 634 | udev_device_add_devlink(udev_device, slink, 0); |
37372bbc | 635 | } else if (strncmp(key, "DRIVER=", 7) == 0) { |
8cd2e972 | 636 | udev_device_set_driver(udev_device, &key[7]); |
37372bbc | 637 | } else if (strncmp(key, "ACTION=", 7) == 0) { |
8cd2e972 | 638 | udev_device_set_action(udev_device, &key[7]); |
31f4b036 | 639 | action_set = 1; |
37372bbc KS |
640 | } else if (strncmp(key, "MAJOR=", 6) == 0) { |
641 | maj = strtoull(&key[6], NULL, 10); | |
642 | } else if (strncmp(key, "MINOR=", 6) == 0) { | |
643 | min = strtoull(&key[6], NULL, 10); | |
644 | } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) { | |
8cd2e972 | 645 | udev_device_set_devpath_old(udev_device, &key[12]); |
37372bbc | 646 | } else if (strncmp(key, "SEQNUM=", 7) == 0) { |
8cd2e972 | 647 | udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10)); |
37372bbc | 648 | } else if (strncmp(key, "TIMEOUT=", 8) == 0) { |
8cd2e972 | 649 | udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10)); |
31f4b036 KS |
650 | } else { |
651 | udev_device_add_property_from_string(udev_device, key); | |
652 | } | |
ba6929f6 | 653 | } |
31f4b036 KS |
654 | if (!devpath_set || !subsystem_set || !action_set) { |
655 | info(udev_monitor->udev, "missing values, skip\n"); | |
81d9e221 KS |
656 | udev_device_unref(udev_device); |
657 | return NULL; | |
658 | } | |
e14bdd88 KS |
659 | |
660 | /* skip device, if it does not pass the current filter */ | |
661 | if (!passes_filter(udev_monitor, udev_device)) { | |
662 | struct pollfd pfd[1]; | |
663 | int rc; | |
664 | ||
665 | udev_device_unref(udev_device); | |
666 | ||
667 | /* if something is queued, get next device */ | |
668 | pfd[0].fd = udev_monitor->sock; | |
669 | pfd[0].events = POLLIN; | |
670 | rc = poll(pfd, 1, 0); | |
671 | if (rc > 0) | |
672 | goto retry; | |
673 | return NULL; | |
674 | } | |
675 | ||
3361a0f1 KS |
676 | if (maj > 0) |
677 | udev_device_set_devnum(udev_device, makedev(maj, min)); | |
8cd2e972 | 678 | udev_device_set_info_loaded(udev_device); |
ba6929f6 KS |
679 | return udev_device; |
680 | } | |
9925ab04 | 681 | |
1e03b754 KS |
682 | int udev_monitor_send_device(struct udev_monitor *udev_monitor, |
683 | struct udev_monitor *destination, struct udev_device *udev_device) | |
9925ab04 | 684 | { |
e14bdd88 KS |
685 | struct msghdr smsg; |
686 | struct iovec iov[2]; | |
c2654402 | 687 | const char *buf; |
e14bdd88 | 688 | ssize_t blen; |
9925ab04 KS |
689 | ssize_t count; |
690 | ||
e14bdd88 KS |
691 | blen = udev_device_get_properties_monitor_buf(udev_device, &buf); |
692 | if (blen < 32) | |
3c67f7d2 | 693 | return -1; |
e14bdd88 KS |
694 | |
695 | if (udev_monitor->sun.sun_family != 0) { | |
696 | const char *action; | |
697 | char header[2048]; | |
065db052 | 698 | char *s; |
e14bdd88 KS |
699 | |
700 | /* header <action>@<devpath> */ | |
701 | action = udev_device_get_action(udev_device); | |
702 | if (action == NULL) | |
703 | return -EINVAL; | |
065db052 KS |
704 | s = header; |
705 | if (util_strpcpyl(&s, sizeof(header), action, "@", udev_device_get_devpath(udev_device), NULL) == 0) | |
e14bdd88 KS |
706 | return -EINVAL; |
707 | iov[0].iov_base = header; | |
065db052 | 708 | iov[0].iov_len = (s - header)+1; |
e14bdd88 KS |
709 | |
710 | /* add properties list */ | |
711 | iov[1].iov_base = (char *)buf; | |
712 | iov[1].iov_len = blen; | |
713 | ||
714 | memset(&smsg, 0x00, sizeof(struct msghdr)); | |
715 | smsg.msg_iov = iov; | |
716 | smsg.msg_iovlen = 2; | |
717 | smsg.msg_name = &udev_monitor->sun; | |
718 | smsg.msg_namelen = udev_monitor->addrlen; | |
719 | } else if (udev_monitor->snl.nl_family != 0) { | |
720 | const char *val; | |
e14bdd88 KS |
721 | struct udev_monitor_netlink_header nlh; |
722 | ||
723 | ||
724 | /* add versioned header */ | |
725 | memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header)); | |
065db052 | 726 | util_strscpy(nlh.version, sizeof(nlh.version), "udev-" VERSION); |
e14bdd88 KS |
727 | nlh.magic = htonl(UDEV_MONITOR_MAGIC); |
728 | val = udev_device_get_subsystem(udev_device); | |
c7dff03e | 729 | nlh.filter_subsystem = htonl(util_string_hash32(val)); |
dacea9ff KS |
730 | val = udev_device_get_devtype(udev_device); |
731 | if (val != NULL) | |
732 | nlh.filter_devtype = htonl(util_string_hash32(val)); | |
e14bdd88 KS |
733 | iov[0].iov_base = &nlh; |
734 | iov[0].iov_len = sizeof(struct udev_monitor_netlink_header); | |
735 | ||
736 | /* add properties list */ | |
737 | nlh.properties_off = iov[0].iov_len; | |
738 | nlh.properties_len = blen; | |
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; | |
1e03b754 KS |
745 | /* |
746 | * Use custom address for target, or the default one. | |
747 | * | |
214a6c79 | 748 | * If we send to a multicast group, we will get |
1e03b754 KS |
749 | * ECONNREFUSED, which is expected. |
750 | */ | |
751 | if (destination != NULL) | |
752 | smsg.msg_name = &destination->snl; | |
753 | else | |
754 | smsg.msg_name = &udev_monitor->snl_destination; | |
e14bdd88 KS |
755 | smsg.msg_namelen = sizeof(struct sockaddr_nl); |
756 | } else { | |
e2b362d9 | 757 | return -1; |
e14bdd88 | 758 | } |
e2b362d9 | 759 | |
e14bdd88 | 760 | count = sendmsg(udev_monitor->sock, &smsg, 0); |
e86a923d | 761 | info(udev_monitor->udev, "passed %zi bytes to monitor %p\n", count, udev_monitor); |
9925ab04 KS |
762 | return count; |
763 | } | |
e14bdd88 | 764 | |
ce1d6d7f KS |
765 | /** |
766 | * udev_monitor_filter_add_match_subsystem_devtype: | |
767 | * @udev_monitor: the monitor | |
768 | * @subsystem: the subsystem value to match the incoming devices against | |
214a6c79 | 769 | * @devtype: the devtype value to match the incoming devices against |
ce1d6d7f KS |
770 | * |
771 | * The filter must be installed before the monitor is switched to listening mode. | |
772 | * | |
773 | * Returns: 0 on success, otherwise a negative error value. | |
774 | */ | |
dacea9ff | 775 | int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype) |
e14bdd88 KS |
776 | { |
777 | if (udev_monitor == NULL) | |
778 | return -EINVAL; | |
779 | if (subsystem == NULL) | |
780 | return 0; | |
781 | if (udev_list_entry_add(udev_monitor->udev, | |
dacea9ff | 782 | &udev_monitor->filter_subsystem_list, subsystem, devtype, 0, 0) == NULL) |
e14bdd88 KS |
783 | return -ENOMEM; |
784 | return 0; | |
785 | } | |
08a7a795 | 786 | |
ce1d6d7f KS |
787 | /** |
788 | * udev_monitor_filter_remove: | |
789 | * @udev_monitor: monitor | |
790 | * | |
791 | * Remove all filters from monitor. | |
792 | * | |
793 | * Returns: 0 on success, otherwise a negative error value. | |
794 | */ | |
08a7a795 KS |
795 | int udev_monitor_filter_remove(struct udev_monitor *udev_monitor) |
796 | { | |
797 | static struct sock_fprog filter = { 0, NULL }; | |
798 | ||
799 | udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list); | |
800 | return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)); | |
801 | } |