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 * hashes of some common device propertie strings to filter with socket filters in
60 * the client used in the kernel from socket filter rules; needs to be stored in
63 unsigned int filter_subsystem;
66 static struct udev_monitor *udev_monitor_new(struct udev *udev)
68 struct udev_monitor *udev_monitor;
70 udev_monitor = calloc(1, sizeof(struct udev_monitor));
71 if (udev_monitor == NULL)
73 udev_monitor->refcount = 1;
74 udev_monitor->udev = udev;
75 udev_list_init(&udev_monitor->filter_subsystem_list);
80 * udev_monitor_new_from_socket:
81 * @udev: udev library context
82 * @socket_path: unix socket path
84 * Create new udev monitor and connect to a specified socket. The
85 * path to a socket either points to an existing socket file, or if
86 * the socket path starts with a '@' character, an abstract namespace
87 * socket will be used.
89 * A socket file will not be created. If it does not already exist,
90 * it will fall-back and connect to an abstract namespace socket with
91 * the given path. The permissions adjustment of a socket file, as
92 * well as the later cleanup, needs to be done by the caller.
94 * The initial refcount is 1, and needs to be decremented to
95 * release the resources of the udev monitor.
97 * Returns: a new udev monitor, or #NULL, in case of an error
99 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
101 struct udev_monitor *udev_monitor;
106 if (socket_path == NULL)
108 udev_monitor = udev_monitor_new(udev);
109 if (udev_monitor == NULL)
112 udev_monitor->sun.sun_family = AF_LOCAL;
113 if (socket_path[0] == '@') {
114 /* translate leading '@' to abstract namespace */
115 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
116 udev_monitor->sun.sun_path[0] = '\0';
117 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
118 } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) {
119 /* existing socket file */
120 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
121 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
123 /* no socket file, assume abstract namespace socket */
124 util_strlcpy(&udev_monitor->sun.sun_path[1], socket_path, sizeof(udev_monitor->sun.sun_path)-1);
125 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1;
127 udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
128 if (udev_monitor->sock == -1) {
129 err(udev, "error getting socket: %m\n");
133 util_set_fd_cloexec(udev_monitor->sock);
135 dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
140 * udev_monitor_new_from_netlink:
141 * @udev: udev library context
142 * @name: name of event source
144 * Create new udev monitor and connect to a specified event
145 * source. Valid sources identifiers are "udev" and "kernel".
147 * Applications should usually not connect directly to the
148 * "kernel" events, because the devices might not be useable
149 * at that time, before udev has configured them, and created
152 * Accessing devices at the same time as udev, might result
153 * in unpredictable behavior.
155 * The "udev" events are sent out after udev has finished its
156 * event processing, all rules have been processed, and needed
157 * device nodes are created.
159 * The initial refcount is 1, and needs to be decremented to
160 * release the resources of the udev monitor.
162 * Returns: a new udev monitor, or #NULL, in case of an error
164 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
166 struct udev_monitor *udev_monitor;
174 if (strcmp(name, "kernel") == 0)
175 group = UDEV_MONITOR_KERNEL;
176 else if (strcmp(name, "udev") == 0)
177 group = UDEV_MONITOR_UDEV;
181 udev_monitor = udev_monitor_new(udev);
182 if (udev_monitor == NULL)
185 udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
186 if (udev_monitor->sock == -1) {
187 err(udev, "error getting socket: %m\n");
191 util_set_fd_cloexec(udev_monitor->sock);
193 udev_monitor->snl.nl_family = AF_NETLINK;
194 udev_monitor->snl.nl_groups = group;
195 udev_monitor->snl_peer.nl_family = AF_NETLINK;
196 udev_monitor->snl_peer.nl_groups = UDEV_MONITOR_UDEV;
198 dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor, group);
202 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
203 unsigned short code, unsigned int data)
205 struct sock_filter *ins = &inss[*i];
212 static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i,
213 unsigned short code, unsigned int data,
214 unsigned short jt, unsigned short jf)
216 struct sock_filter *ins = &inss[*i];
225 static int filter_apply(struct udev_monitor *udev_monitor)
227 static struct sock_filter ins[256];
228 static struct sock_fprog filter;
230 struct udev_list_entry *list_entry;
233 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
236 memset(ins, 0x00, sizeof(ins));
239 /* load magic in accu */
240 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic));
241 /* jump if magic matches */
242 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
243 /* wrong magic, drop packet */
244 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
246 /* load filter_subsystem value in accu */
247 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem));
248 /* add all subsystem match values */
249 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
250 const char *subsys = udev_list_entry_get_name(list_entry);
253 hash = util_string_hash32(subsys);
254 /* jump if value does not match */
255 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
256 /* matched, pass packet */
257 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
259 if (i+1 >= ARRAY_SIZE(ins))
262 /* nothing matched, drop packet */
263 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
268 err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
272 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
277 if (udev_monitor->sun.sun_family != 0) {
278 err = bind(udev_monitor->sock,
279 (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
280 } else if (udev_monitor->snl.nl_family != 0) {
281 filter_apply(udev_monitor);
282 err = bind(udev_monitor->sock,
283 (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
289 err(udev_monitor->udev, "bind failed: %m\n");
293 /* enable receiving of sender credentials */
294 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
298 int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
300 if (udev_monitor == NULL)
302 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
307 * @udev_monitor: udev monitor
309 * Take a reference of a udev monitor.
311 * Returns: the passed udev monitor
313 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
315 if (udev_monitor == NULL)
317 udev_monitor->refcount++;
322 * udev_monitor_unref:
323 * @udev_monitor: udev monitor
325 * Drop a reference of a udev monitor. If the refcount reaches zero,
326 * the bound socket will be closed, and the resources of the monitor
330 void udev_monitor_unref(struct udev_monitor *udev_monitor)
332 if (udev_monitor == NULL)
334 udev_monitor->refcount--;
335 if (udev_monitor->refcount > 0)
337 if (udev_monitor->sock >= 0)
338 close(udev_monitor->sock);
339 udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list);
340 dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
345 * udev_monitor_get_udev:
346 * @udev_monitor: udev monitor
348 * Retrieve the udev library context the monitor was created with.
350 * Returns: the udev library context
352 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
354 if (udev_monitor == NULL)
356 return udev_monitor->udev;
360 * udev_monitor_get_fd:
361 * @udev_monitor: udev monitor
363 * Retrieve the socket file descriptor associated with the monitor.
365 * Returns: the socket file descriptor
367 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
369 if (udev_monitor == NULL)
371 return udev_monitor->sock;
374 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
376 struct udev_list_entry *list_entry;
379 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
382 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
383 const char *subsys = udev_device_get_subsystem(udev_device);
385 if (strcmp(udev_list_entry_get_name(list_entry), subsys) == 0) {
394 * udev_monitor_receive_device:
395 * @udev_monitor: udev monitor
397 * Receive data from the udev monitor socket, allocate a new udev
398 * device, fill in the received data, and return the device.
400 * Only socket connections with uid=0 are accepted. The caller
401 * needs to make sure that there is data to read from the socket.
402 * The call will block until the socket becomes readable.
404 * The initial refcount is 1, and needs to be decremented to
405 * release the resources of the udev device.
407 * Returns: a new udev device, or #NULL, in case of an error
409 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
411 struct udev_device *udev_device;
414 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
415 struct cmsghdr *cmsg;
416 struct sockaddr_nl snl;
421 struct udev_monitor_netlink_header *nlh;
423 int subsystem_set = 0;
429 if (udev_monitor == NULL)
431 memset(buf, 0x00, sizeof(buf));
433 iov.iov_len = sizeof(buf);
434 memset (&smsg, 0x00, sizeof(struct msghdr));
437 smsg.msg_control = cred_msg;
438 smsg.msg_controllen = sizeof(cred_msg);
440 if (udev_monitor->snl.nl_family != 0) {
441 smsg.msg_name = &snl;
442 smsg.msg_namelen = sizeof(snl);
445 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
448 info(udev_monitor->udev, "unable to receive message\n");
452 if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
453 info(udev_monitor->udev, "invalid message length\n");
457 if (udev_monitor->snl.nl_family != 0) {
458 if (snl.nl_groups == 0) {
459 info(udev_monitor->udev, "unicast netlink message ignored\n");
462 if ((snl.nl_groups == UDEV_MONITOR_KERNEL) && (snl.nl_pid > 0)) {
463 info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", snl.nl_pid);
468 cmsg = CMSG_FIRSTHDR(&smsg);
469 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
470 info(udev_monitor->udev, "no sender credentials received, message ignored\n");
474 cred = (struct ucred *)CMSG_DATA(cmsg);
475 if (cred->uid != 0) {
476 info(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
480 if (strncmp(buf, "udev-", 5) == 0) {
481 /* udev message needs proper version magic */
482 nlh = (struct udev_monitor_netlink_header *) buf;
483 if (nlh->magic != htonl(UDEV_MONITOR_MAGIC))
485 if (nlh->properties_off < sizeof(struct udev_monitor_netlink_header))
487 if (nlh->properties_off+32U > buflen)
489 bufpos = nlh->properties_off;
491 /* kernel message with header */
492 bufpos = strlen(buf) + 1;
493 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
494 info(udev_monitor->udev, "invalid message length\n");
498 /* check message header */
499 if (strstr(buf, "@/") == NULL) {
500 info(udev_monitor->udev, "unrecognized message header\n");
505 udev_device = device_new(udev_monitor->udev);
506 if (udev_device == NULL) {
510 while (bufpos < buflen) {
515 keylen = strlen(key);
518 bufpos += keylen + 1;
520 if (strncmp(key, "DEVPATH=", 8) == 0) {
521 char path[UTIL_PATH_SIZE];
523 util_strlcpy(path, udev_get_sys_path(udev_monitor->udev), sizeof(path));
524 util_strlcat(path, &key[8], sizeof(path));
525 udev_device_set_syspath(udev_device, path);
527 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
528 udev_device_set_subsystem(udev_device, &key[10]);
530 } else if (strncmp(key, "DEVTYPE=", 8) == 0) {
531 udev_device_set_devtype(udev_device, &key[8]);
532 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
533 udev_device_set_devnode(udev_device, &key[8]);
534 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
535 char devlinks[UTIL_PATH_SIZE];
539 util_strlcpy(devlinks, &key[9], sizeof(devlinks));
541 next = strchr(slink, ' ');
542 while (next != NULL) {
544 udev_device_add_devlink(udev_device, slink);
546 next = strchr(slink, ' ');
548 if (slink[0] != '\0')
549 udev_device_add_devlink(udev_device, slink);
550 } else if (strncmp(key, "DRIVER=", 7) == 0) {
551 udev_device_set_driver(udev_device, &key[7]);
552 } else if (strncmp(key, "ACTION=", 7) == 0) {
553 udev_device_set_action(udev_device, &key[7]);
555 } else if (strncmp(key, "MAJOR=", 6) == 0) {
556 maj = strtoull(&key[6], NULL, 10);
557 } else if (strncmp(key, "MINOR=", 6) == 0) {
558 min = strtoull(&key[6], NULL, 10);
559 } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) {
560 udev_device_set_devpath_old(udev_device, &key[12]);
561 } else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) {
562 udev_device_set_physdevpath(udev_device, &key[12]);
563 } else if (strncmp(key, "SEQNUM=", 7) == 0) {
564 udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10));
565 } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
566 udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
567 } else if (strncmp(key, "PHYSDEV", 7) == 0) {
568 /* skip deprecated values */
571 udev_device_add_property_from_string(udev_device, key);
574 if (!devpath_set || !subsystem_set || !action_set) {
575 info(udev_monitor->udev, "missing values, skip\n");
576 udev_device_unref(udev_device);
580 /* skip device, if it does not pass the current filter */
581 if (!passes_filter(udev_monitor, udev_device)) {
582 struct pollfd pfd[1];
585 udev_device_unref(udev_device);
587 /* if something is queued, get next device */
588 pfd[0].fd = udev_monitor->sock;
589 pfd[0].events = POLLIN;
590 rc = poll(pfd, 1, 0);
597 udev_device_set_devnum(udev_device, makedev(maj, min));
598 udev_device_set_info_loaded(udev_device);
602 int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
610 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
614 if (udev_monitor->sun.sun_family != 0) {
619 /* header <action>@<devpath> */
620 action = udev_device_get_action(udev_device);
623 util_strlcpy(header, action, sizeof(header));
624 util_strlcat(header, "@", sizeof(header));
625 hlen = util_strlcat(header, udev_device_get_devpath(udev_device), sizeof(header))+1;
626 if (hlen >= sizeof(header))
628 iov[0].iov_base = header;
629 iov[0].iov_len = hlen;
631 /* add properties list */
632 iov[1].iov_base = (char *)buf;
633 iov[1].iov_len = blen;
635 memset(&smsg, 0x00, sizeof(struct msghdr));
638 smsg.msg_name = &udev_monitor->sun;
639 smsg.msg_namelen = udev_monitor->addrlen;
640 } else if (udev_monitor->snl.nl_family != 0) {
642 struct udev_monitor_netlink_header nlh;
645 /* add versioned header */
646 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
647 util_strlcpy(nlh.version, "udev-" VERSION, sizeof(nlh.version));
648 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
649 val = udev_device_get_subsystem(udev_device);
650 nlh.filter_subsystem = htonl(util_string_hash32(val));
651 iov[0].iov_base = &nlh;
652 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
654 /* add properties list */
655 nlh.properties_off = iov[0].iov_len;
656 nlh.properties_len = blen;
657 iov[1].iov_base = (char *)buf;
658 iov[1].iov_len = blen;
660 memset(&smsg, 0x00, sizeof(struct msghdr));
663 /* no destination besides the muticast group, we will always get ECONNREFUSED */
664 smsg.msg_name = &udev_monitor->snl_peer;
665 smsg.msg_namelen = sizeof(struct sockaddr_nl);
670 count = sendmsg(udev_monitor->sock, &smsg, 0);
671 info(udev_monitor->udev, "passed %zi bytes to monitor %p\n", count, udev_monitor);
675 int udev_monitor_filter_add_match_subsystem(struct udev_monitor *udev_monitor, const char *subsystem)
677 if (udev_monitor == NULL)
679 if (subsystem == NULL)
681 if (udev_list_entry_add(udev_monitor->udev,
682 &udev_monitor->filter_subsystem_list, subsystem, NULL, 1, 0) == NULL)