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