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