X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flibudev%2Flibudev-monitor.c;h=da2b63473f3aebe5a83b46202eea3a13077e34c1;hp=b02ea8808cf949a41de9caca803dba7e7b859f01;hb=2695c5c44e028d1bc07404baf3357e5a6017fc57;hpb=f274ece0f76b5709408821e317e87aef76123db6 diff --git a/src/libudev/libudev-monitor.c b/src/libudev/libudev-monitor.c index b02ea8808..da2b63473 100644 --- a/src/libudev/libudev-monitor.c +++ b/src/libudev/libudev-monitor.c @@ -35,6 +35,7 @@ #include "libudev.h" #include "libudev-private.h" #include "socket-util.h" +#include "missing.h" /** * SECTION:libudev-monitor @@ -95,7 +96,7 @@ static struct udev_monitor *udev_monitor_new(struct udev *udev) { struct udev_monitor *udev_monitor; - udev_monitor = calloc(1, sizeof(struct udev_monitor)); + udev_monitor = new0(struct udev_monitor, 1); if (udev_monitor == NULL) return NULL; udev_monitor->refcount = 1; @@ -105,6 +106,47 @@ static struct udev_monitor *udev_monitor_new(struct udev *udev) return udev_monitor; } +/* we consider udev running when /dev is on devtmpfs */ +static bool udev_has_devtmpfs(struct udev *udev) { + + union file_handle_union h = FILE_HANDLE_INIT; + _cleanup_fclose_ FILE *f = NULL; + char line[LINE_MAX], *e; + int mount_id; + int r; + + r = name_to_handle_at(AT_FDCWD, "/dev", &h.handle, &mount_id, 0); + if (r < 0) { + if (errno != EOPNOTSUPP) + log_debug_errno(errno, "name_to_handle_at on /dev: %m"); + return false; + } + + f = fopen("/proc/self/mountinfo", "re"); + if (!f) + return false; + + FOREACH_LINE(line, f, return false) { + int mid; + + if (sscanf(line, "%i", &mid) != 1) + continue; + + if (mid != mount_id) + continue; + + e = strstr(line, " - "); + if (!e) + continue; + + /* accept any name that starts with the currently expected type */ + if (startswith(e + 3, "devtmpfs")) + return true; + } + + return false; +} + struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd) { struct udev_monitor *udev_monitor; @@ -115,9 +157,25 @@ struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const c if (name == NULL) group = UDEV_MONITOR_NONE; - else if (strcmp(name, "udev") == 0) - group = UDEV_MONITOR_UDEV; - else if (strcmp(name, "kernel") == 0) + else if (streq(name, "udev")) { + /* + * We do not support subscribing to uevents if no instance of + * udev is running. Uevents would otherwise broadcast the + * processing data of the host into containers, which is not + * desired. + * + * Containers will currently not get any udev uevents, until + * a supporting infrastructure is available. + * + * We do not set a netlink multicast group here, so the socket + * will not receive any messages. + */ + if (access("/run/udev/control", F_OK) < 0 && !udev_has_devtmpfs(udev)) { + log_debug("the udev service seems not to be active, disable the monitor"); + group = UDEV_MONITOR_NONE; + } else + group = UDEV_MONITOR_UDEV; + } else if (streq(name, "kernel")) group = UDEV_MONITOR_KERNEL; else return NULL; @@ -129,7 +187,7 @@ struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const c if (fd < 0) { udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT); if (udev_monitor->sock == -1) { - udev_err(udev, "error getting socket: %m\n"); + log_debug_errno(errno, "error getting socket: %m"); free(udev_monitor); return NULL; } @@ -219,7 +277,7 @@ _public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor) udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL) return 0; - memset(ins, 0x00, sizeof(ins)); + memzero(ins, sizeof(ins)); i = 0; /* load magic in A */ @@ -288,7 +346,7 @@ _public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor) bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); if (i+1 >= ELEMENTSOF(ins)) - return -1; + return -E2BIG; } /* nothing matched, drop packet */ @@ -299,11 +357,11 @@ _public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor) bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff); /* install filter */ - memset(&filter, 0x00, sizeof(filter)); + memzero(&filter, sizeof(filter)); filter.len = i; filter.filter = ins; err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)); - return err; + return err < 0 ? -errno : 0; } int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender) @@ -324,9 +382,6 @@ _public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor) int err = 0; const int on = 1; - if (udev_monitor->snl.nl.nl_family == 0) - return -EINVAL; - udev_monitor_filter_update(udev_monitor); if (!udev_monitor->bound) { @@ -349,12 +404,15 @@ _public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor) if (err == 0) udev_monitor->snl.nl.nl_pid = snl.nl.nl_pid; } else { - udev_err(udev_monitor->udev, "bind failed: %m\n"); - return err; + log_debug_errno(errno, "bind failed: %m"); + return -errno; } /* enable receiving of sender credentials */ - setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); + err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); + if (err < 0) + log_debug_errno(errno, "setting SO_PASSCRED failed: %m"); + return 0; } @@ -371,7 +429,7 @@ _public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor) _public_ int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size) { if (udev_monitor == NULL) - return -1; + return -EINVAL; return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size)); } @@ -381,7 +439,7 @@ int udev_monitor_disconnect(struct udev_monitor *udev_monitor) err = close(udev_monitor->sock); udev_monitor->sock = -1; - return err; + return err < 0 ? -errno : 0; } /** @@ -408,7 +466,7 @@ _public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor * the bound socket will be closed, and the resources of the monitor * will be released. * - * Returns: the passed udev monitor if it has still an active reference, or #NULL otherwise. + * Returns: #NULL **/ _public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monitor) { @@ -416,7 +474,7 @@ _public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monit return NULL; udev_monitor->refcount--; if (udev_monitor->refcount > 0) - return udev_monitor; + return NULL; if (udev_monitor->sock >= 0) close(udev_monitor->sock); udev_list_cleanup(&udev_monitor->filter_subsystem_list); @@ -451,7 +509,7 @@ _public_ struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor) _public_ int udev_monitor_get_fd(struct udev_monitor *udev_monitor) { if (udev_monitor == NULL) - return -1; + return -EINVAL; return udev_monitor->sock; } @@ -467,7 +525,7 @@ static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device * const char *devtype; const char *ddevtype; - if (strcmp(dsubsys, subsys) != 0) + if (!streq(dsubsys, subsys)) continue; devtype = udev_list_entry_get_value(list_entry); @@ -476,7 +534,7 @@ static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device * ddevtype = udev_device_get_devtype(udev_device); if (ddevtype == NULL) continue; - if (strcmp(ddevtype, devtype) == 0) + if (streq(ddevtype, devtype)) goto tag; } return 0; @@ -521,104 +579,110 @@ _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *ud struct cmsghdr *cmsg; union sockaddr_union snl; struct ucred *cred; - char buf[8192]; + union { + struct udev_monitor_netlink_header nlh; + char raw[8192]; + } buf; ssize_t buflen; ssize_t bufpos; - struct udev_monitor_netlink_header *nlh; retry: if (udev_monitor == NULL) return NULL; iov.iov_base = &buf; iov.iov_len = sizeof(buf); - memset (&smsg, 0x00, sizeof(struct msghdr)); + memzero(&smsg, sizeof(struct msghdr)); smsg.msg_iov = &iov; smsg.msg_iovlen = 1; smsg.msg_control = cred_msg; smsg.msg_controllen = sizeof(cred_msg); - - if (udev_monitor->snl.nl.nl_family != 0) { - smsg.msg_name = &snl; - smsg.msg_namelen = sizeof(snl); - } + smsg.msg_name = &snl; + smsg.msg_namelen = sizeof(snl); buflen = recvmsg(udev_monitor->sock, &smsg, 0); if (buflen < 0) { if (errno != EINTR) - udev_dbg(udev_monitor->udev, "unable to receive message\n"); + log_debug("unable to receive message"); return NULL; } - if (buflen < 32 || (size_t)buflen >= sizeof(buf)) { - udev_dbg(udev_monitor->udev, "invalid message length\n"); + if (buflen < 32 || (smsg.msg_flags & MSG_TRUNC)) { + log_debug("invalid message length"); return NULL; } - if (udev_monitor->snl.nl.nl_family != 0) { - if (snl.nl.nl_groups == 0) { - /* unicast message, check if we trust the sender */ - if (udev_monitor->snl_trusted_sender.nl.nl_pid == 0 || - snl.nl.nl_pid != udev_monitor->snl_trusted_sender.nl.nl_pid) { - udev_dbg(udev_monitor->udev, "unicast netlink message ignored\n"); - return NULL; - } - } else if (snl.nl.nl_groups == UDEV_MONITOR_KERNEL) { - if (snl.nl.nl_pid > 0) { - udev_dbg(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", - snl.nl.nl_pid); - return NULL; - } + if (snl.nl.nl_groups == 0) { + /* unicast message, check if we trust the sender */ + if (udev_monitor->snl_trusted_sender.nl.nl_pid == 0 || + snl.nl.nl_pid != udev_monitor->snl_trusted_sender.nl.nl_pid) { + log_debug("unicast netlink message ignored"); + return NULL; + } + } else if (snl.nl.nl_groups == UDEV_MONITOR_KERNEL) { + if (snl.nl.nl_pid > 0) { + log_debug("multicast kernel netlink message from pid %d ignored", + snl.nl.nl_pid); + return NULL; } } cmsg = CMSG_FIRSTHDR(&smsg); if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) { - udev_dbg(udev_monitor->udev, "no sender credentials received, message ignored\n"); + log_debug("no sender credentials received, message ignored"); return NULL; } cred = (struct ucred *)CMSG_DATA(cmsg); if (cred->uid != 0) { - udev_dbg(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid); + log_debug("sender uid=%d, message ignored", cred->uid); return NULL; } - if (memcmp(buf, "libudev", 8) == 0) { + udev_device = udev_device_new(udev_monitor->udev); + if (udev_device == NULL) + return NULL; + + if (memcmp(buf.raw, "libudev", 8) == 0) { /* udev message needs proper version magic */ - nlh = (struct udev_monitor_netlink_header *) buf; - if (nlh->magic != htonl(UDEV_MONITOR_MAGIC)) { - udev_err(udev_monitor->udev, "unrecognized message signature (%x != %x)\n", - nlh->magic, htonl(UDEV_MONITOR_MAGIC)); + if (buf.nlh.magic != htonl(UDEV_MONITOR_MAGIC)) { + log_debug("unrecognized message signature (%x != %x)", + buf.nlh.magic, htonl(UDEV_MONITOR_MAGIC)); + udev_device_unref(udev_device); return NULL; } - if (nlh->properties_off+32 > (size_t)buflen) + if (buf.nlh.properties_off+32 > (size_t)buflen) { + udev_device_unref(udev_device); return NULL; - bufpos = nlh->properties_off; + } + + bufpos = buf.nlh.properties_off; + + /* devices received from udev are always initialized */ + udev_device_set_is_initialized(udev_device); } else { /* kernel message with header */ - bufpos = strlen(buf) + 1; + bufpos = strlen(buf.raw) + 1; if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) { - udev_dbg(udev_monitor->udev, "invalid message length\n"); + log_debug("invalid message length"); + udev_device_unref(udev_device); return NULL; } /* check message header */ - if (strstr(buf, "@/") == NULL) { - udev_dbg(udev_monitor->udev, "unrecognized message header\n"); + if (strstr(buf.raw, "@/") == NULL) { + log_debug("unrecognized message header"); + udev_device_unref(udev_device); return NULL; } } - udev_device = udev_device_new(udev_monitor->udev); - if (udev_device == NULL) - return NULL; udev_device_set_info_loaded(udev_device); while (bufpos < buflen) { char *key; size_t keylen; - key = &buf[bufpos]; + key = &buf.raw[bufpos]; keylen = strlen(key); if (keylen == 0) break; @@ -627,7 +691,7 @@ retry: } if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) { - udev_dbg(udev_monitor->udev, "missing values, invalid device\n"); + log_debug("missing values, invalid device"); udev_device_unref(udev_device); return NULL; } @@ -664,15 +728,12 @@ int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_list_entry *list_entry; uint64_t tag_bloom_bits; - if (udev_monitor->snl.nl.nl_family == 0) - return -EINVAL; - blen = udev_device_get_properties_monitor_buf(udev_device, &buf); if (blen < 32) return -EINVAL; /* add versioned header */ - memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header)); + memzero(&nlh, sizeof(struct udev_monitor_netlink_header)); memcpy(nlh.prefix, "libudev", 8); nlh.magic = htonl(UDEV_MONITOR_MAGIC); nlh.header_size = sizeof(struct udev_monitor_netlink_header); @@ -699,7 +760,7 @@ int udev_monitor_send_device(struct udev_monitor *udev_monitor, iov[1].iov_base = (char *)buf; iov[1].iov_len = blen; - memset(&smsg, 0x00, sizeof(struct msghdr)); + memzero(&smsg, sizeof(struct msghdr)); smsg.msg_iov = iov; smsg.msg_iovlen = 2; /* @@ -714,7 +775,7 @@ int udev_monitor_send_device(struct udev_monitor *udev_monitor, smsg.msg_name = &udev_monitor->snl_destination; smsg.msg_namelen = sizeof(struct sockaddr_nl); count = sendmsg(udev_monitor->sock, &smsg, 0); - udev_dbg(udev_monitor->udev, "passed %zi bytes to netlink monitor %p\n", count, udev_monitor); + log_debug("passed %zi bytes to netlink monitor %p", count, udev_monitor); return count; }