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 properties 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;
64 unsigned int filter_devtype;
67 static struct udev_monitor *udev_monitor_new(struct udev *udev)
69 struct udev_monitor *udev_monitor;
71 udev_monitor = calloc(1, sizeof(struct udev_monitor));
72 if (udev_monitor == NULL)
74 udev_monitor->refcount = 1;
75 udev_monitor->udev = udev;
76 udev_list_init(&udev_monitor->filter_subsystem_list);
81 * udev_monitor_new_from_socket:
82 * @udev: udev library context
83 * @socket_path: unix socket path
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
88 * socket will be used.
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.
95 * The initial refcount is 1, and needs to be decremented to
96 * release the resources of the udev monitor.
98 * Returns: a new udev monitor, or #NULL, in case of an error
100 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
102 struct udev_monitor *udev_monitor;
107 if (socket_path == NULL)
109 udev_monitor = udev_monitor_new(udev);
110 if (udev_monitor == NULL)
113 udev_monitor->sun.sun_family = AF_LOCAL;
114 if (socket_path[0] == '@') {
115 /* translate leading '@' to abstract namespace */
116 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
117 udev_monitor->sun.sun_path[0] = '\0';
118 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
119 } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) {
120 /* existing socket file */
121 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
122 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
124 /* no socket file, assume abstract namespace socket */
125 util_strlcpy(&udev_monitor->sun.sun_path[1], socket_path, sizeof(udev_monitor->sun.sun_path)-1);
126 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1;
128 udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
129 if (udev_monitor->sock == -1) {
130 err(udev, "error getting socket: %m\n");
134 util_set_fd_cloexec(udev_monitor->sock);
136 dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
141 * udev_monitor_new_from_netlink:
142 * @udev: udev library context
143 * @name: name of event source
145 * Create new udev monitor and connect to a specified event
146 * source. Valid sources identifiers are "udev" and "kernel".
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
153 * Accessing devices at the same time as udev, might result
154 * in unpredictable behavior.
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.
160 * The initial refcount is 1, and needs to be decremented to
161 * release the resources of the udev monitor.
163 * Returns: a new udev monitor, or #NULL, in case of an error
165 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
167 struct udev_monitor *udev_monitor;
175 if (strcmp(name, "kernel") == 0)
176 group = UDEV_MONITOR_KERNEL;
177 else if (strcmp(name, "udev") == 0)
178 group = UDEV_MONITOR_UDEV;
182 udev_monitor = udev_monitor_new(udev);
183 if (udev_monitor == NULL)
186 udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
187 if (udev_monitor->sock == -1) {
188 err(udev, "error getting socket: %m\n");
192 util_set_fd_cloexec(udev_monitor->sock);
194 udev_monitor->snl.nl_family = AF_NETLINK;
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;
199 dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor, group);
203 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
204 unsigned short code, unsigned int data)
206 struct sock_filter *ins = &inss[*i];
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)
217 struct sock_filter *ins = &inss[*i];
226 int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
228 static struct sock_filter ins[256];
229 static struct sock_fprog filter;
231 struct udev_list_entry *list_entry;
234 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
237 memset(ins, 0x00, sizeof(ins));
240 /* load magic in A */
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);
244 /* wrong magic, pass packet */
245 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
247 /* add all subsystem match values */
248 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
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);
258 /* jump if subsystem does not match */
259 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
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);
268 /* matched, pass packet */
269 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
271 if (i+1 >= ARRAY_SIZE(ins))
274 /* nothing matched, drop packet */
275 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
280 err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
284 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
289 if (udev_monitor->sun.sun_family != 0) {
290 err = bind(udev_monitor->sock,
291 (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
292 } else if (udev_monitor->snl.nl_family != 0) {
293 udev_monitor_filter_update(udev_monitor);
294 err = bind(udev_monitor->sock,
295 (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
301 err(udev_monitor->udev, "bind failed: %m\n");
305 /* enable receiving of sender credentials */
306 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
310 int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
312 if (udev_monitor == NULL)
314 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
319 * @udev_monitor: udev monitor
321 * Take a reference of a udev monitor.
323 * Returns: the passed udev monitor
325 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
327 if (udev_monitor == NULL)
329 udev_monitor->refcount++;
334 * udev_monitor_unref:
335 * @udev_monitor: udev monitor
337 * Drop a reference of a udev monitor. If the refcount reaches zero,
338 * the bound socket will be closed, and the resources of the monitor
342 void udev_monitor_unref(struct udev_monitor *udev_monitor)
344 if (udev_monitor == NULL)
346 udev_monitor->refcount--;
347 if (udev_monitor->refcount > 0)
349 if (udev_monitor->sock >= 0)
350 close(udev_monitor->sock);
351 udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list);
352 dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
357 * udev_monitor_get_udev:
358 * @udev_monitor: udev monitor
360 * Retrieve the udev library context the monitor was created with.
362 * Returns: the udev library context
364 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
366 if (udev_monitor == NULL)
368 return udev_monitor->udev;
372 * udev_monitor_get_fd:
373 * @udev_monitor: udev monitor
375 * Retrieve the socket file descriptor associated with the monitor.
377 * Returns: the socket file descriptor
379 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
381 if (udev_monitor == NULL)
383 return udev_monitor->sock;
386 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
388 struct udev_list_entry *list_entry;
390 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
393 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
394 const char *subsys = udev_list_entry_get_name(list_entry);
395 const char *dsubsys = udev_device_get_subsystem(udev_device);
397 const char *ddevtype;
399 if (strcmp(dsubsys, subsys) != 0)
402 devtype = udev_list_entry_get_value(list_entry);
405 ddevtype = udev_device_get_devtype(udev_device);
406 if (ddevtype == NULL)
408 if (strcmp(ddevtype, devtype) == 0)
415 * udev_monitor_receive_device:
416 * @udev_monitor: udev monitor
418 * Receive data from the udev monitor socket, allocate a new udev
419 * device, fill in the received data, and return the device.
421 * Only socket connections with uid=0 are accepted. The caller
422 * needs to make sure that there is data to read from the socket.
423 * The call will block until the socket becomes readable.
425 * The initial refcount is 1, and needs to be decremented to
426 * release the resources of the udev device.
428 * Returns: a new udev device, or #NULL, in case of an error
430 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
432 struct udev_device *udev_device;
435 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
436 struct cmsghdr *cmsg;
437 struct sockaddr_nl snl;
442 struct udev_monitor_netlink_header *nlh;
444 int subsystem_set = 0;
451 if (udev_monitor == NULL)
453 memset(buf, 0x00, sizeof(buf));
455 iov.iov_len = sizeof(buf);
456 memset (&smsg, 0x00, sizeof(struct msghdr));
459 smsg.msg_control = cred_msg;
460 smsg.msg_controllen = sizeof(cred_msg);
462 if (udev_monitor->snl.nl_family != 0) {
463 smsg.msg_name = &snl;
464 smsg.msg_namelen = sizeof(snl);
467 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
470 info(udev_monitor->udev, "unable to receive message\n");
474 if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
475 info(udev_monitor->udev, "invalid message length\n");
479 if (udev_monitor->snl.nl_family != 0) {
480 if (snl.nl_groups == 0) {
481 info(udev_monitor->udev, "unicast netlink message ignored\n");
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);
493 cmsg = CMSG_FIRSTHDR(&smsg);
494 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
495 info(udev_monitor->udev, "no sender credentials received, message ignored\n");
499 cred = (struct ucred *)CMSG_DATA(cmsg);
500 if (cred->uid != 0) {
501 info(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
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))
510 if (nlh->properties_off < sizeof(struct udev_monitor_netlink_header))
512 if (nlh->properties_off+32U > buflen)
514 bufpos = nlh->properties_off;
516 /* kernel message with header */
517 bufpos = strlen(buf) + 1;
518 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
519 info(udev_monitor->udev, "invalid message length\n");
523 /* check message header */
524 if (strstr(buf, "@/") == NULL) {
525 info(udev_monitor->udev, "unrecognized message header\n");
530 udev_device = device_new(udev_monitor->udev);
531 if (udev_device == NULL) {
535 while (bufpos < buflen) {
540 keylen = strlen(key);
543 bufpos += keylen + 1;
545 if (strncmp(key, "DEVPATH=", 8) == 0) {
546 char path[UTIL_PATH_SIZE];
548 util_strlcpy(path, udev_get_sys_path(udev_monitor->udev), sizeof(path));
549 util_strlcat(path, &key[8], sizeof(path));
550 udev_device_set_syspath(udev_device, path);
552 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
553 udev_device_set_subsystem(udev_device, &key[10]);
555 } else if (strncmp(key, "DEVTYPE=", 8) == 0) {
556 udev_device_set_devtype(udev_device, &key[8]);
557 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
559 udev_device_set_knodename(udev_device, &key[8]);
561 udev_device_set_devnode(udev_device, &key[8]);
562 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
563 char devlinks[UTIL_PATH_SIZE];
567 util_strlcpy(devlinks, &key[9], sizeof(devlinks));
569 next = strchr(slink, ' ');
570 while (next != NULL) {
572 udev_device_add_devlink(udev_device, slink);
574 next = strchr(slink, ' ');
576 if (slink[0] != '\0')
577 udev_device_add_devlink(udev_device, slink);
578 } else if (strncmp(key, "DRIVER=", 7) == 0) {
579 udev_device_set_driver(udev_device, &key[7]);
580 } else if (strncmp(key, "ACTION=", 7) == 0) {
581 udev_device_set_action(udev_device, &key[7]);
583 } else if (strncmp(key, "MAJOR=", 6) == 0) {
584 maj = strtoull(&key[6], NULL, 10);
585 } else if (strncmp(key, "MINOR=", 6) == 0) {
586 min = strtoull(&key[6], NULL, 10);
587 } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) {
588 udev_device_set_devpath_old(udev_device, &key[12]);
589 } else if (strncmp(key, "SEQNUM=", 7) == 0) {
590 udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10));
591 } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
592 udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
594 udev_device_add_property_from_string(udev_device, key);
597 if (!devpath_set || !subsystem_set || !action_set) {
598 info(udev_monitor->udev, "missing values, skip\n");
599 udev_device_unref(udev_device);
603 /* skip device, if it does not pass the current filter */
604 if (!passes_filter(udev_monitor, udev_device)) {
605 struct pollfd pfd[1];
608 udev_device_unref(udev_device);
610 /* if something is queued, get next device */
611 pfd[0].fd = udev_monitor->sock;
612 pfd[0].events = POLLIN;
613 rc = poll(pfd, 1, 0);
620 udev_device_set_devnum(udev_device, makedev(maj, min));
621 udev_device_set_info_loaded(udev_device);
625 int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
633 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
637 if (udev_monitor->sun.sun_family != 0) {
642 /* header <action>@<devpath> */
643 action = udev_device_get_action(udev_device);
646 util_strlcpy(header, action, sizeof(header));
647 util_strlcat(header, "@", sizeof(header));
648 hlen = util_strlcat(header, udev_device_get_devpath(udev_device), sizeof(header))+1;
649 if (hlen >= sizeof(header))
651 iov[0].iov_base = header;
652 iov[0].iov_len = hlen;
654 /* add properties list */
655 iov[1].iov_base = (char *)buf;
656 iov[1].iov_len = blen;
658 memset(&smsg, 0x00, sizeof(struct msghdr));
661 smsg.msg_name = &udev_monitor->sun;
662 smsg.msg_namelen = udev_monitor->addrlen;
663 } else if (udev_monitor->snl.nl_family != 0) {
665 struct udev_monitor_netlink_header nlh;
668 /* add versioned header */
669 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
670 util_strlcpy(nlh.version, "udev-" VERSION, sizeof(nlh.version));
671 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
672 val = udev_device_get_subsystem(udev_device);
673 nlh.filter_subsystem = htonl(util_string_hash32(val));
674 val = udev_device_get_devtype(udev_device);
676 nlh.filter_devtype = htonl(util_string_hash32(val));
677 iov[0].iov_base = &nlh;
678 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
680 /* add properties list */
681 nlh.properties_off = iov[0].iov_len;
682 nlh.properties_len = blen;
683 iov[1].iov_base = (char *)buf;
684 iov[1].iov_len = blen;
686 memset(&smsg, 0x00, sizeof(struct msghdr));
689 /* no destination besides the muticast group, we will always get ECONNREFUSED */
690 smsg.msg_name = &udev_monitor->snl_peer;
691 smsg.msg_namelen = sizeof(struct sockaddr_nl);
696 count = sendmsg(udev_monitor->sock, &smsg, 0);
697 info(udev_monitor->udev, "passed %zi bytes to monitor %p\n", count, udev_monitor);
701 int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
703 if (udev_monitor == NULL)
705 if (subsystem == NULL)
707 if (udev_list_entry_add(udev_monitor->udev,
708 &udev_monitor->filter_subsystem_list, subsystem, devtype, 0, 0) == NULL)
713 int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
715 static struct sock_fprog filter = { 0, NULL };
717 udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list);
718 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));