2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2009 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_trusted_sender;
36 struct sockaddr_nl snl_destination;
37 struct sockaddr_un sun;
39 struct udev_list_node filter_subsystem_list;
42 enum udev_monitor_netlink_group {
48 #define UDEV_MONITOR_MAGIC 0xcafe1dea
49 struct udev_monitor_netlink_header {
50 /* udev version text */
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
57 /* properties buffer */
58 unsigned short properties_off;
59 unsigned short properties_len;
61 * hashes of some common device properties strings to filter with socket filters in
62 * the client used in the kernel from socket filter rules; needs to be stored in
65 unsigned int filter_subsystem;
66 unsigned int filter_devtype;
69 static struct udev_monitor *udev_monitor_new(struct udev *udev)
71 struct udev_monitor *udev_monitor;
73 udev_monitor = calloc(1, sizeof(struct udev_monitor));
74 if (udev_monitor == NULL)
76 udev_monitor->refcount = 1;
77 udev_monitor->udev = udev;
78 udev_list_init(&udev_monitor->filter_subsystem_list);
83 * udev_monitor_new_from_socket:
84 * @udev: udev library context
85 * @socket_path: unix socket path
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
90 * socket will be used.
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.
97 * The initial refcount is 1, and needs to be decremented to
98 * release the resources of the udev monitor.
100 * Returns: a new udev monitor, or #NULL, in case of an error
102 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
104 struct udev_monitor *udev_monitor;
109 if (socket_path == NULL)
111 udev_monitor = udev_monitor_new(udev);
112 if (udev_monitor == NULL)
115 udev_monitor->sun.sun_family = AF_LOCAL;
116 if (socket_path[0] == '@') {
117 /* translate leading '@' to abstract namespace */
118 util_strscpy(udev_monitor->sun.sun_path, sizeof(udev_monitor->sun.sun_path), socket_path);
119 udev_monitor->sun.sun_path[0] = '\0';
120 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
121 } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) {
122 /* existing socket file */
123 util_strscpy(udev_monitor->sun.sun_path, sizeof(udev_monitor->sun.sun_path), socket_path);
124 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
126 /* no socket file, assume abstract namespace socket */
127 util_strscpy(&udev_monitor->sun.sun_path[1], sizeof(udev_monitor->sun.sun_path)-1, socket_path);
128 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1;
130 udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
131 if (udev_monitor->sock == -1) {
132 err(udev, "error getting socket: %m\n");
136 util_set_fd_cloexec(udev_monitor->sock);
138 dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
143 * udev_monitor_new_from_netlink:
144 * @udev: udev library context
145 * @name: name of event source
147 * Create new udev monitor and connect to a specified event
148 * source. Valid sources identifiers are "udev" and "kernel".
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
155 * Accessing devices at the same time as udev, might result
156 * in unpredictable behavior.
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.
162 * The initial refcount is 1, and needs to be decremented to
163 * release the resources of the udev monitor.
165 * Returns: a new udev monitor, or #NULL, in case of an error
167 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
169 struct udev_monitor *udev_monitor;
176 group = UDEV_MONITOR_NONE;
177 else if (strcmp(name, "udev") == 0)
178 group = UDEV_MONITOR_UDEV;
179 else if (strcmp(name, "kernel") == 0)
180 group = UDEV_MONITOR_KERNEL;
184 udev_monitor = udev_monitor_new(udev);
185 if (udev_monitor == NULL)
188 udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
189 if (udev_monitor->sock == -1) {
190 err(udev, "error getting socket: %m\n");
194 util_set_fd_cloexec(udev_monitor->sock);
196 udev_monitor->snl.nl_family = AF_NETLINK;
197 udev_monitor->snl.nl_groups = group;
199 /* default destination for sending */
200 udev_monitor->snl_destination.nl_family = AF_NETLINK;
201 udev_monitor->snl_destination.nl_groups = UDEV_MONITOR_UDEV;
203 dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor, group);
207 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
208 unsigned short code, unsigned int data)
210 struct sock_filter *ins = &inss[*i];
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)
221 struct sock_filter *ins = &inss[*i];
230 int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
232 static struct sock_filter ins[256];
233 static struct sock_fprog filter;
235 struct udev_list_entry *list_entry;
238 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
241 memset(ins, 0x00, sizeof(ins));
244 /* load magic in A */
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);
248 /* wrong magic, pass packet */
249 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
251 /* add all subsystem match values */
252 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
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);
262 /* jump if subsystem does not match */
263 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
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);
272 /* matched, pass packet */
273 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
275 if (i+1 >= ARRAY_SIZE(ins))
278 /* nothing matched, drop packet */
279 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
284 err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
288 int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
290 udev_monitor->snl_trusted_sender.nl_pid = sender->snl.nl_pid;
294 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
299 if (udev_monitor->sun.sun_family != 0) {
300 err = bind(udev_monitor->sock,
301 (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
302 } else if (udev_monitor->snl.nl_family != 0) {
303 udev_monitor_filter_update(udev_monitor);
304 err = bind(udev_monitor->sock,
305 (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
307 struct sockaddr_nl snl;
311 * get the address the kernel has assigned us
312 * it is usually, but not neccessarily the pid
314 addrlen = sizeof(struct sockaddr_nl);
315 err = getsockname(udev_monitor->sock, (struct sockaddr *)&snl, &addrlen);
317 udev_monitor->snl.nl_pid = snl.nl_pid;
324 err(udev_monitor->udev, "bind failed: %m\n");
328 /* enable receiving of sender credentials */
329 setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
333 int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
335 if (udev_monitor == NULL)
337 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
340 int udev_monitor_disconnect(struct udev_monitor *udev_monitor)
344 err = close(udev_monitor->sock);
345 udev_monitor->sock = -1;
351 * @udev_monitor: udev monitor
353 * Take a reference of a udev monitor.
355 * Returns: the passed udev monitor
357 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
359 if (udev_monitor == NULL)
361 udev_monitor->refcount++;
366 * udev_monitor_unref:
367 * @udev_monitor: udev monitor
369 * Drop a reference of a udev monitor. If the refcount reaches zero,
370 * the bound socket will be closed, and the resources of the monitor
374 void udev_monitor_unref(struct udev_monitor *udev_monitor)
376 if (udev_monitor == NULL)
378 udev_monitor->refcount--;
379 if (udev_monitor->refcount > 0)
381 if (udev_monitor->sock >= 0)
382 close(udev_monitor->sock);
383 udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list);
384 dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
389 * udev_monitor_get_udev:
390 * @udev_monitor: udev monitor
392 * Retrieve the udev library context the monitor was created with.
394 * Returns: the udev library context
396 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
398 if (udev_monitor == NULL)
400 return udev_monitor->udev;
404 * udev_monitor_get_fd:
405 * @udev_monitor: udev monitor
407 * Retrieve the socket file descriptor associated with the monitor.
409 * Returns: the socket file descriptor
411 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
413 if (udev_monitor == NULL)
415 return udev_monitor->sock;
418 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
420 struct udev_list_entry *list_entry;
422 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
425 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
426 const char *subsys = udev_list_entry_get_name(list_entry);
427 const char *dsubsys = udev_device_get_subsystem(udev_device);
429 const char *ddevtype;
431 if (strcmp(dsubsys, subsys) != 0)
434 devtype = udev_list_entry_get_value(list_entry);
437 ddevtype = udev_device_get_devtype(udev_device);
438 if (ddevtype == NULL)
440 if (strcmp(ddevtype, devtype) == 0)
447 * udev_monitor_receive_device:
448 * @udev_monitor: udev monitor
450 * Receive data from the udev monitor socket, allocate a new udev
451 * device, fill in the received data, and return the device.
453 * Only socket connections with uid=0 are accepted. The caller
454 * needs to make sure that there is data to read from the socket.
455 * The call will block until the socket becomes readable.
457 * The initial refcount is 1, and needs to be decremented to
458 * release the resources of the udev device.
460 * Returns: a new udev device, or #NULL, in case of an error
462 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
464 struct udev_device *udev_device;
467 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
468 struct cmsghdr *cmsg;
469 struct sockaddr_nl snl;
474 struct udev_monitor_netlink_header *nlh;
476 int subsystem_set = 0;
483 if (udev_monitor == NULL)
485 memset(buf, 0x00, sizeof(buf));
487 iov.iov_len = sizeof(buf);
488 memset (&smsg, 0x00, sizeof(struct msghdr));
491 smsg.msg_control = cred_msg;
492 smsg.msg_controllen = sizeof(cred_msg);
494 if (udev_monitor->snl.nl_family != 0) {
495 smsg.msg_name = &snl;
496 smsg.msg_namelen = sizeof(snl);
499 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
502 info(udev_monitor->udev, "unable to receive message\n");
506 if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
507 info(udev_monitor->udev, "invalid message length\n");
511 if (udev_monitor->snl.nl_family != 0) {
512 if (snl.nl_groups == 0) {
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");
519 } else if (snl.nl_groups == UDEV_MONITOR_KERNEL) {
520 if (snl.nl_pid > 0) {
521 info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", snl.nl_pid);
528 cmsg = CMSG_FIRSTHDR(&smsg);
529 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
530 info(udev_monitor->udev, "no sender credentials received, message ignored\n");
534 cred = (struct ucred *)CMSG_DATA(cmsg);
535 if (cred->uid != 0) {
536 info(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
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))
545 if (nlh->properties_off < sizeof(struct udev_monitor_netlink_header))
547 if (nlh->properties_off+32U > buflen)
549 bufpos = nlh->properties_off;
551 /* kernel message with header */
552 bufpos = strlen(buf) + 1;
553 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
554 info(udev_monitor->udev, "invalid message length\n");
558 /* check message header */
559 if (strstr(buf, "@/") == NULL) {
560 info(udev_monitor->udev, "unrecognized message header\n");
565 udev_device = device_new(udev_monitor->udev);
566 if (udev_device == NULL) {
570 while (bufpos < buflen) {
575 keylen = strlen(key);
578 bufpos += keylen + 1;
580 if (strncmp(key, "DEVPATH=", 8) == 0) {
581 char path[UTIL_PATH_SIZE];
583 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_monitor->udev), &key[8], NULL);
584 udev_device_set_syspath(udev_device, path);
586 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
587 udev_device_set_subsystem(udev_device, &key[10]);
589 } else if (strncmp(key, "DEVTYPE=", 8) == 0) {
590 udev_device_set_devtype(udev_device, &key[8]);
591 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
593 udev_device_set_knodename(udev_device, &key[8]);
595 udev_device_set_devnode(udev_device, &key[8]);
596 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
597 char devlinks[UTIL_PATH_SIZE];
601 util_strscpy(devlinks, sizeof(devlinks), &key[9]);
603 next = strchr(slink, ' ');
604 while (next != NULL) {
606 udev_device_add_devlink(udev_device, slink);
608 next = strchr(slink, ' ');
610 if (slink[0] != '\0')
611 udev_device_add_devlink(udev_device, slink);
612 } else if (strncmp(key, "DRIVER=", 7) == 0) {
613 udev_device_set_driver(udev_device, &key[7]);
614 } else if (strncmp(key, "ACTION=", 7) == 0) {
615 udev_device_set_action(udev_device, &key[7]);
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) {
622 udev_device_set_devpath_old(udev_device, &key[12]);
623 } else if (strncmp(key, "SEQNUM=", 7) == 0) {
624 udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10));
625 } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
626 udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
628 udev_device_add_property_from_string(udev_device, key);
631 if (!devpath_set || !subsystem_set || !action_set) {
632 info(udev_monitor->udev, "missing values, skip\n");
633 udev_device_unref(udev_device);
637 /* skip device, if it does not pass the current filter */
638 if (!passes_filter(udev_monitor, udev_device)) {
639 struct pollfd pfd[1];
642 udev_device_unref(udev_device);
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);
654 udev_device_set_devnum(udev_device, makedev(maj, min));
655 udev_device_set_info_loaded(udev_device);
659 int udev_monitor_send_device(struct udev_monitor *udev_monitor,
660 struct udev_monitor *destination, struct udev_device *udev_device)
668 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
672 if (udev_monitor->sun.sun_family != 0) {
677 /* header <action>@<devpath> */
678 action = udev_device_get_action(udev_device);
682 if (util_strpcpyl(&s, sizeof(header), action, "@", udev_device_get_devpath(udev_device), NULL) == 0)
684 iov[0].iov_base = header;
685 iov[0].iov_len = (s - header)+1;
687 /* add properties list */
688 iov[1].iov_base = (char *)buf;
689 iov[1].iov_len = blen;
691 memset(&smsg, 0x00, sizeof(struct msghdr));
694 smsg.msg_name = &udev_monitor->sun;
695 smsg.msg_namelen = udev_monitor->addrlen;
696 } else if (udev_monitor->snl.nl_family != 0) {
698 struct udev_monitor_netlink_header nlh;
701 /* add versioned header */
702 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
703 util_strscpy(nlh.version, sizeof(nlh.version), "udev-" VERSION);
704 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
705 val = udev_device_get_subsystem(udev_device);
706 nlh.filter_subsystem = htonl(util_string_hash32(val));
707 val = udev_device_get_devtype(udev_device);
709 nlh.filter_devtype = htonl(util_string_hash32(val));
710 iov[0].iov_base = &nlh;
711 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
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;
719 memset(&smsg, 0x00, sizeof(struct msghdr));
723 * Use custom address for target, or the default one.
725 * If we send to a muticast group, we will get
726 * ECONNREFUSED, which is expected.
728 if (destination != NULL)
729 smsg.msg_name = &destination->snl;
731 smsg.msg_name = &udev_monitor->snl_destination;
732 smsg.msg_namelen = sizeof(struct sockaddr_nl);
737 count = sendmsg(udev_monitor->sock, &smsg, 0);
738 info(udev_monitor->udev, "passed %zi bytes to monitor %p\n", count, udev_monitor);
742 int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
744 if (udev_monitor == NULL)
746 if (subsystem == NULL)
748 if (udev_list_entry_add(udev_monitor->udev,
749 &udev_monitor->filter_subsystem_list, subsystem, devtype, 0, 0) == NULL)
754 int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
756 static struct sock_fprog filter = { 0, NULL };
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));