chiark / gitweb /
util: make sure all our name_to_handle_at() code makes use of file_handle_union
[elogind.git] / src / libudev / libudev-monitor.c
index a6dba893763a0f55aa0de3321d0abac62fd09bc0..c05cb3e511bd840146ed67e4fcc4e65f1defc8be 100644 (file)
@@ -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 = {
+                .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)
+                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 (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;
@@ -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 */
@@ -299,7 +357,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));
@@ -527,7 +585,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;
@@ -672,7 +730,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);
@@ -699,7 +757,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;
         /*