2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
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.
21 #include <sys/socket.h>
23 #include <arpa/inet.h>
24 #include <linux/netlink.h>
25 #include <linux/filter.h>
28 #include "libudev-private.h"
34 struct sockaddr_nl snl
;
35 struct sockaddr_nl snl_peer
;
36 struct sockaddr_un sun
;
38 struct udev_list_node filter_subsystem_list
;
41 enum udev_monitor_netlink_group
{
42 UDEV_MONITOR_KERNEL
= 1,
43 UDEV_MONITOR_UDEV
= 2,
46 #define UDEV_MONITOR_MAGIC 0xcafe1dea
47 struct udev_monitor_netlink_header
{
48 /* udev version text */
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
55 /* properties buffer */
56 unsigned short properties_off
;
57 unsigned short properties_len
;
59 * crc32 of some common device properties to filter with socket filters in the client
60 * used in the kernel from socket filter rules; needs to be stored in network order
62 unsigned int filter_subsystem
;
65 static struct udev_monitor
*udev_monitor_new(struct udev
*udev
)
67 struct udev_monitor
*udev_monitor
;
69 udev_monitor
= calloc(1, sizeof(struct udev_monitor
));
70 if (udev_monitor
== NULL
)
72 udev_monitor
->refcount
= 1;
73 udev_monitor
->udev
= udev
;
74 udev_list_init(&udev_monitor
->filter_subsystem_list
);
79 * udev_monitor_new_from_socket:
80 * @udev: udev library context
81 * @socket_path: unix socket path
83 * Create new udev monitor and connect to a specified socket. The
84 * path to a socket either points to an existing socket file, or if
85 * the socket path starts with a '@' character, an abstract namespace
86 * socket will be used.
88 * A socket file will not be created. If it does not already exist,
89 * it will fall-back and connect to an abstract namespace socket with
90 * the given path. The permissions adjustment of a socket file, as
91 * well as the later cleanup, needs to be done by the caller.
93 * The initial refcount is 1, and needs to be decremented to
94 * release the resources of the udev monitor.
96 * Returns: a new udev monitor, or #NULL, in case of an error
98 struct udev_monitor
*udev_monitor_new_from_socket(struct udev
*udev
, const char *socket_path
)
100 struct udev_monitor
*udev_monitor
;
105 if (socket_path
== NULL
)
107 udev_monitor
= udev_monitor_new(udev
);
108 if (udev_monitor
== NULL
)
111 udev_monitor
->sun
.sun_family
= AF_LOCAL
;
112 if (socket_path
[0] == '@') {
113 /* translate leading '@' to abstract namespace */
114 util_strlcpy(udev_monitor
->sun
.sun_path
, socket_path
, sizeof(udev_monitor
->sun
.sun_path
));
115 udev_monitor
->sun
.sun_path
[0] = '\0';
116 udev_monitor
->addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(socket_path
);
117 } else if (stat(socket_path
, &statbuf
) == 0 && S_ISSOCK(statbuf
.st_mode
)) {
118 /* existing socket file */
119 util_strlcpy(udev_monitor
->sun
.sun_path
, socket_path
, sizeof(udev_monitor
->sun
.sun_path
));
120 udev_monitor
->addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(socket_path
);
122 /* no socket file, assume abstract namespace socket */
123 util_strlcpy(&udev_monitor
->sun
.sun_path
[1], socket_path
, sizeof(udev_monitor
->sun
.sun_path
)-1);
124 udev_monitor
->addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(socket_path
)+1;
126 udev_monitor
->sock
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
127 if (udev_monitor
->sock
== -1) {
128 err(udev
, "error getting socket: %m\n");
132 util_set_fd_cloexec(udev_monitor
->sock
);
134 dbg(udev
, "monitor %p created with '%s'\n", udev_monitor
, socket_path
);
139 * udev_monitor_new_from_netlink:
140 * @udev: udev library context
141 * @name: name of event source
143 * Create new udev monitor and connect to a specified event
144 * source. Valid sources identifiers are "udev" and "kernel".
146 * Applications should usually not connect directly to the
147 * "kernel" events, because the devices might not be useable
148 * at that time, before udev has configured them, and created
151 * Accessing devices at the same time as udev, might result
152 * in unpredictable behavior.
154 * The "udev" events are sent out after udev has finished its
155 * event processing, all rules have been processed, and needed
156 * device nodes are created.
158 * The initial refcount is 1, and needs to be decremented to
159 * release the resources of the udev monitor.
161 * Returns: a new udev monitor, or #NULL, in case of an error
163 struct udev_monitor
*udev_monitor_new_from_netlink(struct udev
*udev
, const char *name
)
165 struct udev_monitor
*udev_monitor
;
173 if (strcmp(name
, "kernel") == 0)
174 group
= UDEV_MONITOR_KERNEL
;
175 else if (strcmp(name
, "udev") == 0)
176 group
= UDEV_MONITOR_UDEV
;
180 udev_monitor
= udev_monitor_new(udev
);
181 if (udev_monitor
== NULL
)
184 udev_monitor
->sock
= socket(PF_NETLINK
, SOCK_DGRAM
, NETLINK_KOBJECT_UEVENT
);
185 if (udev_monitor
->sock
== -1) {
186 err(udev
, "error getting socket: %m\n");
190 util_set_fd_cloexec(udev_monitor
->sock
);
192 udev_monitor
->snl
.nl_family
= AF_NETLINK
;
193 udev_monitor
->snl
.nl_groups
= group
;
194 udev_monitor
->snl_peer
.nl_family
= AF_NETLINK
;
195 udev_monitor
->snl_peer
.nl_groups
= UDEV_MONITOR_UDEV
;
197 dbg(udev
, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor
, group
);
201 static inline void bpf_stmt(struct sock_filter
*inss
, unsigned int *i
,
202 unsigned short code
, unsigned int data
)
204 struct sock_filter
*ins
= &inss
[*i
];
211 static inline void bpf_jmp(struct sock_filter
*inss
, unsigned int *i
,
212 unsigned short code
, unsigned int data
,
213 unsigned short jt
, unsigned short jf
)
215 struct sock_filter
*ins
= &inss
[*i
];
224 static int filter_apply(struct udev_monitor
*udev_monitor
)
226 static struct sock_filter ins
[256];
227 static struct sock_fprog filter
;
229 struct udev_list_entry
*list_entry
;
232 if (udev_list_get_entry(&udev_monitor
->filter_subsystem_list
) == NULL
)
235 memset(ins
, 0x00, sizeof(ins
));
238 /* load magic in accu */
239 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(struct udev_monitor_netlink_header
, magic
));
240 /* jump if magic matches */
241 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, UDEV_MONITOR_MAGIC
, 1, 0);
242 /* wrong magic, drop packet */
243 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0);
245 /* load filter_subsystem value in accu */
246 bpf_stmt(ins
, &i
, BPF_LD
|BPF_W
|BPF_ABS
, offsetof(struct udev_monitor_netlink_header
, filter_subsystem
));
247 /* add all subsystem match values */
248 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_subsystem_list
)) {
249 const char *subsys
= udev_list_entry_get_name(list_entry
);
252 crc
= util_crc32((unsigned char *)subsys
, strlen(subsys
));
253 /* jump if value does not match */
254 bpf_jmp(ins
, &i
, BPF_JMP
|BPF_JEQ
|BPF_K
, crc
, 0, 1);
255 /* matched, pass packet */
256 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0xffffffff);
258 if (i
+1 >= ARRAY_SIZE(ins
))
261 /* nothing matched, drop packet */
262 bpf_stmt(ins
, &i
, BPF_RET
|BPF_K
, 0);
267 err
= setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &filter
, sizeof(filter
));
271 int udev_monitor_enable_receiving(struct udev_monitor
*udev_monitor
)
276 if (udev_monitor
->sun
.sun_family
!= 0) {
277 err
= bind(udev_monitor
->sock
,
278 (struct sockaddr
*)&udev_monitor
->sun
, udev_monitor
->addrlen
);
279 } else if (udev_monitor
->snl
.nl_family
!= 0) {
280 filter_apply(udev_monitor
);
281 err
= bind(udev_monitor
->sock
,
282 (struct sockaddr
*)&udev_monitor
->snl
, sizeof(struct sockaddr_nl
));
288 err(udev_monitor
->udev
, "bind failed: %m\n");
292 /* enable receiving of sender credentials */
293 setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
297 int udev_monitor_set_receive_buffer_size(struct udev_monitor
*udev_monitor
, int size
)
299 if (udev_monitor
== NULL
)
301 return setsockopt(udev_monitor
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &size
, sizeof(size
));
306 * @udev_monitor: udev monitor
308 * Take a reference of a udev monitor.
310 * Returns: the passed udev monitor
312 struct udev_monitor
*udev_monitor_ref(struct udev_monitor
*udev_monitor
)
314 if (udev_monitor
== NULL
)
316 udev_monitor
->refcount
++;
321 * udev_monitor_unref:
322 * @udev_monitor: udev monitor
324 * Drop a reference of a udev monitor. If the refcount reaches zero,
325 * the bound socket will be closed, and the resources of the monitor
329 void udev_monitor_unref(struct udev_monitor
*udev_monitor
)
331 if (udev_monitor
== NULL
)
333 udev_monitor
->refcount
--;
334 if (udev_monitor
->refcount
> 0)
336 if (udev_monitor
->sock
>= 0)
337 close(udev_monitor
->sock
);
338 udev_list_cleanup_entries(udev_monitor
->udev
, &udev_monitor
->filter_subsystem_list
);
339 dbg(udev_monitor
->udev
, "monitor %p released\n", udev_monitor
);
344 * udev_monitor_get_udev:
345 * @udev_monitor: udev monitor
347 * Retrieve the udev library context the monitor was created with.
349 * Returns: the udev library context
351 struct udev
*udev_monitor_get_udev(struct udev_monitor
*udev_monitor
)
353 if (udev_monitor
== NULL
)
355 return udev_monitor
->udev
;
359 * udev_monitor_get_fd:
360 * @udev_monitor: udev monitor
362 * Retrieve the socket file descriptor associated with the monitor.
364 * Returns: the socket file descriptor
366 int udev_monitor_get_fd(struct udev_monitor
*udev_monitor
)
368 if (udev_monitor
== NULL
)
370 return udev_monitor
->sock
;
373 static int passes_filter(struct udev_monitor
*udev_monitor
, struct udev_device
*udev_device
)
375 struct udev_list_entry
*list_entry
;
378 if (udev_list_get_entry(&udev_monitor
->filter_subsystem_list
) == NULL
)
381 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_monitor
->filter_subsystem_list
)) {
382 const char *subsys
= udev_device_get_subsystem(udev_device
);
384 if (strcmp(udev_list_entry_get_name(list_entry
), subsys
) == 0) {
393 * udev_monitor_receive_device:
394 * @udev_monitor: udev monitor
396 * Receive data from the udev monitor socket, allocate a new udev
397 * device, fill in the received data, and return the device.
399 * Only socket connections with uid=0 are accepted. The caller
400 * needs to make sure that there is data to read from the socket.
401 * The call will block until the socket becomes readable.
403 * The initial refcount is 1, and needs to be decremented to
404 * release the resources of the udev device.
406 * Returns: a new udev device, or #NULL, in case of an error
408 struct udev_device
*udev_monitor_receive_device(struct udev_monitor
*udev_monitor
)
410 struct udev_device
*udev_device
;
413 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
414 struct cmsghdr
*cmsg
;
415 struct sockaddr_nl snl
;
419 struct udev_monitor_netlink_header
*nlh
;
421 int subsystem_set
= 0;
427 if (udev_monitor
== NULL
)
429 memset(buf
, 0x00, sizeof(buf
));
431 iov
.iov_len
= sizeof(buf
);
432 memset (&smsg
, 0x00, sizeof(struct msghdr
));
435 smsg
.msg_control
= cred_msg
;
436 smsg
.msg_controllen
= sizeof(cred_msg
);
438 if (udev_monitor
->snl
.nl_family
!= 0) {
439 smsg
.msg_name
= &snl
;
440 smsg
.msg_namelen
= sizeof(snl
);
443 if (recvmsg(udev_monitor
->sock
, &smsg
, 0) < 0) {
445 info(udev_monitor
->udev
, "unable to receive message\n");
449 if (udev_monitor
->snl
.nl_family
!= 0) {
450 if (snl
.nl_groups
== 0) {
451 info(udev_monitor
->udev
, "unicast netlink message ignored\n");
454 if ((snl
.nl_groups
== UDEV_MONITOR_KERNEL
) && (snl
.nl_pid
> 0)) {
455 info(udev_monitor
->udev
, "multicast kernel netlink message from pid %d ignored\n", snl
.nl_pid
);
460 cmsg
= CMSG_FIRSTHDR(&smsg
);
461 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
462 info(udev_monitor
->udev
, "no sender credentials received, message ignored\n");
466 cred
= (struct ucred
*)CMSG_DATA(cmsg
);
467 if (cred
->uid
!= 0) {
468 info(udev_monitor
->udev
, "sender uid=%d, message ignored\n", cred
->uid
);
472 nlh
= (struct udev_monitor_netlink_header
*) buf
;
473 if (nlh
->magic
== ntohl(UDEV_MONITOR_MAGIC
)) {
474 /* udev message with version magic */
475 if (nlh
->properties_off
< sizeof(struct udev_monitor_netlink_header
))
477 if (nlh
->properties_off
+32U > sizeof(buf
))
479 bufpos
= nlh
->properties_off
;
481 /* kernel message with header */
482 bufpos
= strlen(buf
) + 1;
483 if (bufpos
< sizeof("a@/d") || bufpos
>= sizeof(buf
)) {
484 info(udev_monitor
->udev
, "invalid message length\n");
488 /* check message header */
489 if (strstr(buf
, "@/") == NULL
) {
490 info(udev_monitor
->udev
, "unrecognized message header\n");
495 udev_device
= device_new(udev_monitor
->udev
);
496 if (udev_device
== NULL
) {
500 while (bufpos
< sizeof(buf
)) {
505 keylen
= strlen(key
);
508 bufpos
+= keylen
+ 1;
510 if (strncmp(key
, "DEVPATH=", 8) == 0) {
511 char path
[UTIL_PATH_SIZE
];
513 util_strlcpy(path
, udev_get_sys_path(udev_monitor
->udev
), sizeof(path
));
514 util_strlcat(path
, &key
[8], sizeof(path
));
515 udev_device_set_syspath(udev_device
, path
);
517 } else if (strncmp(key
, "SUBSYSTEM=", 10) == 0) {
518 udev_device_set_subsystem(udev_device
, &key
[10]);
520 } else if (strncmp(key
, "DEVTYPE=", 8) == 0) {
521 udev_device_set_devtype(udev_device
, &key
[8]);
522 } else if (strncmp(key
, "DEVNAME=", 8) == 0) {
523 udev_device_set_devnode(udev_device
, &key
[8]);
524 } else if (strncmp(key
, "DEVLINKS=", 9) == 0) {
525 char devlinks
[UTIL_PATH_SIZE
];
529 util_strlcpy(devlinks
, &key
[9], sizeof(devlinks
));
531 next
= strchr(slink
, ' ');
532 while (next
!= NULL
) {
534 udev_device_add_devlink(udev_device
, slink
);
536 next
= strchr(slink
, ' ');
538 if (slink
[0] != '\0')
539 udev_device_add_devlink(udev_device
, slink
);
540 } else if (strncmp(key
, "DRIVER=", 7) == 0) {
541 udev_device_set_driver(udev_device
, &key
[7]);
542 } else if (strncmp(key
, "ACTION=", 7) == 0) {
543 udev_device_set_action(udev_device
, &key
[7]);
545 } else if (strncmp(key
, "MAJOR=", 6) == 0) {
546 maj
= strtoull(&key
[6], NULL
, 10);
547 } else if (strncmp(key
, "MINOR=", 6) == 0) {
548 min
= strtoull(&key
[6], NULL
, 10);
549 } else if (strncmp(key
, "DEVPATH_OLD=", 12) == 0) {
550 udev_device_set_devpath_old(udev_device
, &key
[12]);
551 } else if (strncmp(key
, "PHYSDEVPATH=", 12) == 0) {
552 udev_device_set_physdevpath(udev_device
, &key
[12]);
553 } else if (strncmp(key
, "SEQNUM=", 7) == 0) {
554 udev_device_set_seqnum(udev_device
, strtoull(&key
[7], NULL
, 10));
555 } else if (strncmp(key
, "TIMEOUT=", 8) == 0) {
556 udev_device_set_timeout(udev_device
, strtoull(&key
[8], NULL
, 10));
557 } else if (strncmp(key
, "PHYSDEV", 7) == 0) {
558 /* skip deprecated values */
561 udev_device_add_property_from_string(udev_device
, key
);
564 if (!devpath_set
|| !subsystem_set
|| !action_set
) {
565 info(udev_monitor
->udev
, "missing values, skip\n");
566 udev_device_unref(udev_device
);
570 /* skip device, if it does not pass the current filter */
571 if (!passes_filter(udev_monitor
, udev_device
)) {
572 struct pollfd pfd
[1];
575 udev_device_unref(udev_device
);
577 /* if something is queued, get next device */
578 pfd
[0].fd
= udev_monitor
->sock
;
579 pfd
[0].events
= POLLIN
;
580 rc
= poll(pfd
, 1, 0);
587 udev_device_set_devnum(udev_device
, makedev(maj
, min
));
588 udev_device_set_info_loaded(udev_device
);
592 int udev_monitor_send_device(struct udev_monitor
*udev_monitor
, struct udev_device
*udev_device
)
600 blen
= udev_device_get_properties_monitor_buf(udev_device
, &buf
);
604 if (udev_monitor
->sun
.sun_family
!= 0) {
609 /* header <action>@<devpath> */
610 action
= udev_device_get_action(udev_device
);
613 util_strlcpy(header
, action
, sizeof(header
));
614 util_strlcat(header
, "@", sizeof(header
));
615 hlen
= util_strlcat(header
, udev_device_get_devpath(udev_device
), sizeof(header
))+1;
616 if (hlen
>= sizeof(header
))
618 iov
[0].iov_base
= header
;
619 iov
[0].iov_len
= hlen
;
621 /* add properties list */
622 iov
[1].iov_base
= (char *)buf
;
623 iov
[1].iov_len
= blen
;
625 memset(&smsg
, 0x00, sizeof(struct msghdr
));
628 smsg
.msg_name
= &udev_monitor
->sun
;
629 smsg
.msg_namelen
= udev_monitor
->addrlen
;
630 } else if (udev_monitor
->snl
.nl_family
!= 0) {
633 struct udev_monitor_netlink_header nlh
;
636 /* add versioned header */
637 memset(&nlh
, 0x00, sizeof(struct udev_monitor_netlink_header
));
638 util_strlcpy(nlh
.version
, "udev-" VERSION
, sizeof(nlh
.version
));
639 nlh
.magic
= htonl(UDEV_MONITOR_MAGIC
);
640 val
= udev_device_get_subsystem(udev_device
);
642 nlh
.filter_subsystem
= htonl(util_crc32((unsigned char *)val
, vlen
));
643 iov
[0].iov_base
= &nlh
;
644 iov
[0].iov_len
= sizeof(struct udev_monitor_netlink_header
);
646 /* add properties list */
647 nlh
.properties_off
= iov
[0].iov_len
;
648 nlh
.properties_len
= blen
;
649 iov
[1].iov_base
= (char *)buf
;
650 iov
[1].iov_len
= blen
;
652 memset(&smsg
, 0x00, sizeof(struct msghdr
));
655 /* no destination besides the muticast group, we will always get ECONNREFUSED */
656 smsg
.msg_name
= &udev_monitor
->snl_peer
;
657 smsg
.msg_namelen
= sizeof(struct sockaddr_nl
);
662 count
= sendmsg(udev_monitor
->sock
, &smsg
, 0);
663 info(udev_monitor
->udev
, "passed %zi bytes to monitor %p\n", count
, udev_monitor
);
667 int udev_monitor_filter_add_match_subsystem(struct udev_monitor
*udev_monitor
, const char *subsystem
)
669 if (udev_monitor
== NULL
)
671 if (subsystem
== NULL
)
673 if (udev_list_entry_add(udev_monitor
->udev
,
674 &udev_monitor
->filter_subsystem_list
, subsystem
, NULL
, 1, 0) == NULL
)