X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibudev%2Flibudev-monitor.c;h=186e5e1e87360e46ce458331138fa9686c7a5db7;hb=5e273efec5fcf60ddb55e6bc0aaa850cb1be86c6;hp=e07e462af3bb861cfeb563543d66a0b441996269;hpb=ec76ec1fde7da753676b18ff45926740fcd08072;p=elogind.git diff --git a/src/libudev/libudev-monitor.c b/src/libudev/libudev-monitor.c index e07e462af..186e5e1e8 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,50 @@ 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 = { + .handle.handle_bytes = MAX_HANDLE_SZ + }; + + _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) + udev_err(udev, "name_to_handle_at on /dev: %m\n"); + 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,21 +160,29 @@ struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const c if (name == NULL) group = UDEV_MONITOR_NONE; - else if (streq(name, "udev")) - group = UDEV_MONITOR_UDEV; - else if (streq(name, "kernel")) + 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)) { + udev_dbg(udev, "the udev service seems not to be active, disable the monitor\n"); + group = UDEV_MONITOR_NONE; + } else + group = UDEV_MONITOR_UDEV; + } else if (streq(name, "kernel")) group = UDEV_MONITOR_KERNEL; else return NULL; - /* - * 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 acceptable. Containers - * will currently just not get any uevents. - */ - - udev_monitor = udev_monitor_new(udev); if (udev_monitor == NULL) return NULL; @@ -227,7 +280,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 */ @@ -307,7 +360,7 @@ _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)); @@ -535,7 +588,7 @@ retry: 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; @@ -680,7 +733,7 @@ int udev_monitor_send_device(struct udev_monitor *udev_monitor, 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); @@ -707,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; /*