Daniel P. Berrange 731c6b
From c0bc172383c2c955394589e5808457935ae06f1d Mon Sep 17 00:00:00 2001
Daniel P. Berrange 731c6b
From: "Daniel P. Berrange" <berrange@redhat.com>
Daniel P. Berrange 731c6b
Date: Mon, 6 Jun 2016 15:03:27 +0100
Daniel P. Berrange 731c6b
Subject: [PATCH] systemd: directly notify systemd instead of using sd_notify
Daniel P. Berrange 731c6b
Daniel P. Berrange 731c6b
The sd_notify method is used to tell systemd when libvirtd
Daniel P. Berrange 731c6b
has finished starting up. All it does is send a datagram
Daniel P. Berrange 731c6b
containing the string parameter to systemd on a UNIX socket
Daniel P. Berrange 731c6b
named in the NOTIFY_SOCKET environment variable. Rather than
Daniel P. Berrange 731c6b
pulling in the systemd libraries for this, just code the
Daniel P. Berrange 731c6b
notification directly in libvirt as this is a stable ABI
Daniel P. Berrange 731c6b
from systemd's POV which explicitly allows independant
Daniel P. Berrange 731c6b
implementations:
Daniel P. Berrange 731c6b
Daniel P. Berrange 731c6b
See "Reimplementable Independently" column in the
Daniel P. Berrange 731c6b
"$NOTIFY_SOCKET Daemon Notifications" row:
Daniel P. Berrange 731c6b
Daniel P. Berrange 731c6b
https://www.freedesktop.org/wiki/Software/systemd/InterfacePortabilityAndStabilityChart/
Daniel P. Berrange 731c6b
Daniel P. Berrange 731c6b
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1314881
Daniel P. Berrange 731c6b
Daniel P. Berrange 731c6b
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Daniel P. Berrange 731c6b
---
Daniel P. Berrange 731c6b
 configure.ac              |  2 --
Daniel P. Berrange 731c6b
 libvirt.spec.in           | 12 -----------
Daniel P. Berrange 731c6b
 m4/virt-systemd-daemon.m4 | 34 ------------------------------
Daniel P. Berrange 731c6b
 src/Makefile.am           |  4 ++--
Daniel P. Berrange 731c6b
 src/util/virsystemd.c     | 53 ++++++++++++++++++++++++++++++++++++++++++-----
Daniel P. Berrange 731c6b
 5 files changed, 50 insertions(+), 55 deletions(-)
Daniel P. Berrange 731c6b
 delete mode 100644 m4/virt-systemd-daemon.m4
Daniel P. Berrange 731c6b
Daniel P. Berrange 731c6b
diff --git a/configure.ac b/configure.ac
Daniel P. Berrange 731c6b
index f2554a4..12eb3b3 100644
Daniel P. Berrange 731c6b
--- a/configure.ac
Daniel P. Berrange 731c6b
+++ b/configure.ac
Daniel P. Berrange 731c6b
@@ -256,7 +256,6 @@ LIBVIRT_CHECK_SANLOCK
Daniel P. Berrange 731c6b
 LIBVIRT_CHECK_SASL
Daniel P. Berrange 731c6b
 LIBVIRT_CHECK_SELINUX
Daniel P. Berrange 731c6b
 LIBVIRT_CHECK_SSH2
Daniel P. Berrange 731c6b
-LIBVIRT_CHECK_SYSTEMD_DAEMON
Daniel P. Berrange 731c6b
 LIBVIRT_CHECK_UDEV
Daniel P. Berrange 731c6b
 LIBVIRT_CHECK_WIRESHARK
Daniel P. Berrange 731c6b
 LIBVIRT_CHECK_NSS
Daniel P. Berrange 731c6b
@@ -2787,7 +2786,6 @@ LIBVIRT_RESULT_SANLOCK
Daniel P. Berrange 731c6b
 LIBVIRT_RESULT_SASL
Daniel P. Berrange 731c6b
 LIBVIRT_RESULT_SELINUX
Daniel P. Berrange 731c6b
 LIBVIRT_RESULT_SSH2
Daniel P. Berrange 731c6b
-LIBVIRT_RESULT_SYSTEMD_DAEMON
Daniel P. Berrange 731c6b
 LIBVIRT_RESULT_UDEV
Daniel P. Berrange 731c6b
 LIBVIRT_RESULT_WIRESHARK
Daniel P. Berrange 731c6b
 LIBVIRT_RESULT_NSS
Daniel P. Berrange 731c6b
diff --git a/libvirt.spec.in b/libvirt.spec.in
Daniel P. Berrange 731c6b
index 8b88eef..b93a53c 100644
Daniel P. Berrange 731c6b
--- a/libvirt.spec.in
Daniel P. Berrange 731c6b
+++ b/libvirt.spec.in
Daniel P. Berrange 731c6b
@@ -79,7 +79,6 @@
Daniel P. Berrange 731c6b
 %define with_firewalld     0%{!?_without_firewalld:0}
Daniel P. Berrange 731c6b
 %define with_libssh2       0%{!?_without_libssh2:0}
Daniel P. Berrange 731c6b
 %define with_wireshark     0%{!?_without_wireshark:0}
Daniel P. Berrange 731c6b
-%define with_systemd_daemon 0%{!?_without_systemd_daemon:0}
Daniel P. Berrange 731c6b
 %define with_pm_utils      1
Daniel P. Berrange 731c6b
 
Daniel P. Berrange 731c6b
 # Finally set the OS / architecture specific special cases
Daniel P. Berrange 731c6b
@@ -133,7 +132,6 @@
Daniel P. Berrange 731c6b
 # Fedora has systemd, libvirt still used sysvinit there.
Daniel P. Berrange 731c6b
 %if 0%{?fedora} || 0%{?rhel} >= 7
Daniel P. Berrange 731c6b
     %define with_systemd 1
Daniel P. Berrange 731c6b
-    %define with_systemd_daemon 1
Daniel P. Berrange 731c6b
     %define with_pm_utils 0
Daniel P. Berrange 731c6b
 %endif
Daniel P. Berrange 731c6b
 
Daniel P. Berrange 731c6b
@@ -268,9 +266,6 @@ BuildRequires: python
Daniel P. Berrange 731c6b
 %if %{with_systemd}
Daniel P. Berrange 731c6b
 BuildRequires: systemd-units
Daniel P. Berrange 731c6b
 %endif
Daniel P. Berrange 731c6b
-%if %{with_systemd_daemon}
Daniel P. Berrange 731c6b
-BuildRequires: systemd-devel
Daniel P. Berrange 731c6b
-%endif
Daniel P. Berrange 731c6b
 %if %{with_xen} || %{with_libxl}
Daniel P. Berrange 731c6b
 BuildRequires: xen-devel
Daniel P. Berrange 731c6b
 %endif
Daniel P. Berrange 731c6b
@@ -1061,12 +1056,6 @@ rm -rf .git
Daniel P. Berrange 731c6b
     %define arg_wireshark --without-wireshark-dissector
Daniel P. Berrange 731c6b
 %endif
Daniel P. Berrange 731c6b
 
Daniel P. Berrange 731c6b
-%if %{with_systemd_daemon}
Daniel P. Berrange 731c6b
-    %define arg_systemd_daemon --with-systemd-daemon
Daniel P. Berrange 731c6b
-%else
Daniel P. Berrange 731c6b
-    %define arg_systemd_daemon --without-systemd-daemon
Daniel P. Berrange 731c6b
-%endif
Daniel P. Berrange 731c6b
-
Daniel P. Berrange 731c6b
 %if %{with_pm_utils}
Daniel P. Berrange 731c6b
     %define arg_pm_utils --with-pm-utils
Daniel P. Berrange 731c6b
 %else
Daniel P. Berrange 731c6b
@@ -1157,7 +1146,6 @@ rm -f po/stamp-po
Daniel P. Berrange 731c6b
            --with-driver-modules \
Daniel P. Berrange 731c6b
            %{?arg_firewalld} \
Daniel P. Berrange 731c6b
            %{?arg_wireshark} \
Daniel P. Berrange 731c6b
-           %{?arg_systemd_daemon} \
Daniel P. Berrange 731c6b
            %{?arg_pm_utils} \
Daniel P. Berrange 731c6b
            --with-nss-plugin \
Daniel P. Berrange 731c6b
            %{arg_packager} \
Daniel P. Berrange 731c6b
diff --git a/m4/virt-systemd-daemon.m4 b/m4/virt-systemd-daemon.m4
Daniel P. Berrange 731c6b
deleted file mode 100644
Daniel P. Berrange 731c6b
index 8516e41..0000000
Daniel P. Berrange 731c6b
--- a/m4/virt-systemd-daemon.m4
Daniel P. Berrange 731c6b
+++ /dev/null
Daniel P. Berrange 731c6b
@@ -1,34 +0,0 @@
Daniel P. Berrange 731c6b
-dnl The libsystemd-daemon.so library
Daniel P. Berrange 731c6b
-dnl
Daniel P. Berrange 731c6b
-dnl Copyright (C) 2012-2013 Red Hat, Inc.
Daniel P. Berrange 731c6b
-dnl
Daniel P. Berrange 731c6b
-dnl This library is free software; you can redistribute it and/or
Daniel P. Berrange 731c6b
-dnl modify it under the terms of the GNU Lesser General Public
Daniel P. Berrange 731c6b
-dnl License as published by the Free Software Foundation; either
Daniel P. Berrange 731c6b
-dnl version 2.1 of the License, or (at your option) any later version.
Daniel P. Berrange 731c6b
-dnl
Daniel P. Berrange 731c6b
-dnl This library is distributed in the hope that it will be useful,
Daniel P. Berrange 731c6b
-dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel P. Berrange 731c6b
-dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Daniel P. Berrange 731c6b
-dnl Lesser General Public License for more details.
Daniel P. Berrange 731c6b
-dnl
Daniel P. Berrange 731c6b
-dnl You should have received a copy of the GNU Lesser General Public
Daniel P. Berrange 731c6b
-dnl License along with this library.  If not, see
Daniel P. Berrange 731c6b
-dnl <http://www.gnu.org/licenses/>.
Daniel P. Berrange 731c6b
-dnl
Daniel P. Berrange 731c6b
-
Daniel P. Berrange 731c6b
-AC_DEFUN([LIBVIRT_CHECK_SYSTEMD_DAEMON],[
Daniel P. Berrange 731c6b
-  LIBVIRT_CHECK_PKG([SYSTEMD_DAEMON], [libsystemd-daemon], [0.27.1])
Daniel P. Berrange 731c6b
-
Daniel P. Berrange 731c6b
-    old_CFLAGS="$CFLAGS"
Daniel P. Berrange 731c6b
-    old_LIBS="$LIBS"
Daniel P. Berrange 731c6b
-    CFLAGS="$CFLAGS $SYSTEMD_DAEMON_CFLAGS"
Daniel P. Berrange 731c6b
-    LIBS="$LIBS $SYSTEMD_DAEMON_LIBS"
Daniel P. Berrange 731c6b
-    AC_CHECK_FUNCS([sd_notify])
Daniel P. Berrange 731c6b
-    CFLAGS="$old_CFLAGS"
Daniel P. Berrange 731c6b
-    LIBS="$old_LIBS"
Daniel P. Berrange 731c6b
-])
Daniel P. Berrange 731c6b
-
Daniel P. Berrange 731c6b
-AC_DEFUN([LIBVIRT_RESULT_SYSTEMD_DAEMON],[
Daniel P. Berrange 731c6b
-  LIBVIRT_RESULT_LIB([SYSTEMD_DAEMON])
Daniel P. Berrange 731c6b
-])
Daniel P. Berrange 731c6b
diff --git a/src/Makefile.am b/src/Makefile.am
Daniel P. Berrange 731c6b
index f3c9a14..f020b92 100644
Daniel P. Berrange 731c6b
--- a/src/Makefile.am
Daniel P. Berrange 731c6b
+++ b/src/Makefile.am
Daniel P. Berrange 731c6b
@@ -1107,12 +1107,12 @@ libvirt_util_la_SOURCES =					\
Daniel P. Berrange 731c6b
 libvirt_util_la_CFLAGS = $(CAPNG_CFLAGS) $(YAJL_CFLAGS) $(LIBNL_CFLAGS) \
Daniel P. Berrange 731c6b
 		$(AM_CFLAGS) $(AUDIT_CFLAGS) $(DEVMAPPER_CFLAGS) \
Daniel P. Berrange 731c6b
 		$(DBUS_CFLAGS) $(LDEXP_LIBM) $(NUMACTL_CFLAGS)	\
Daniel P. Berrange 731c6b
-		$(SYSTEMD_DAEMON_CFLAGS) $(POLKIT_CFLAGS) $(GNUTLS_CFLAGS) \
Daniel P. Berrange 731c6b
+		$(POLKIT_CFLAGS) $(GNUTLS_CFLAGS) \
Daniel P. Berrange 731c6b
 		-I$(srcdir)/conf
Daniel P. Berrange 731c6b
 libvirt_util_la_LIBADD = $(CAPNG_LIBS) $(YAJL_LIBS) $(LIBNL_LIBS) \
Daniel P. Berrange 731c6b
 		$(THREAD_LIBS) $(AUDIT_LIBS) $(DEVMAPPER_LIBS) \
Daniel P. Berrange 731c6b
 		$(LIB_CLOCK_GETTIME) $(DBUS_LIBS) $(MSCOM_LIBS) $(LIBXML_LIBS) \
Daniel P. Berrange 731c6b
-		$(SECDRIVER_LIBS) $(NUMACTL_LIBS) $(SYSTEMD_DAEMON_LIBS) \
Daniel P. Berrange 731c6b
+		$(SECDRIVER_LIBS) $(NUMACTL_LIBS) \
Daniel P. Berrange 731c6b
 		$(POLKIT_LIBS)
Daniel P. Berrange 731c6b
 
Daniel P. Berrange 731c6b
 
Daniel P. Berrange 731c6b
diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
Daniel P. Berrange 731c6b
index 4883f94..871db7e 100644
Daniel P. Berrange 731c6b
--- a/src/util/virsystemd.c
Daniel P. Berrange 731c6b
+++ b/src/util/virsystemd.c
Daniel P. Berrange 731c6b
@@ -21,8 +21,9 @@
Daniel P. Berrange 731c6b
 
Daniel P. Berrange 731c6b
 #include <config.h>
Daniel P. Berrange 731c6b
 
Daniel P. Berrange 731c6b
-#ifdef WITH_SYSTEMD_DAEMON
Daniel P. Berrange 731c6b
-# include <systemd/sd-daemon.h>
Daniel P. Berrange 731c6b
+#include <sys/socket.h>
Daniel P. Berrange 731c6b
+#ifdef HAVE_SYS_UN_H
Daniel P. Berrange 731c6b
+# include <sys/un.h>
Daniel P. Berrange 731c6b
 #endif
Daniel P. Berrange 731c6b
 
Daniel P. Berrange 731c6b
 #include "virsystemd.h"
Daniel P. Berrange 731c6b
@@ -34,6 +35,7 @@
Daniel P. Berrange 731c6b
 #include "virutil.h"
Daniel P. Berrange 731c6b
 #include "virlog.h"
Daniel P. Berrange 731c6b
 #include "virerror.h"
Daniel P. Berrange 731c6b
+#include "virfile.h"
Daniel P. Berrange 731c6b
 
Daniel P. Berrange 731c6b
 #define VIR_FROM_THIS VIR_FROM_SYSTEMD
Daniel P. Berrange 731c6b
 
Daniel P. Berrange 731c6b
@@ -480,9 +482,50 @@ int virSystemdTerminateMachine(const char *name)
Daniel P. Berrange 731c6b
 void
Daniel P. Berrange 731c6b
 virSystemdNotifyStartup(void)
Daniel P. Berrange 731c6b
 {
Daniel P. Berrange 731c6b
-#ifdef WITH_SYSTEMD_DAEMON
Daniel P. Berrange 731c6b
-    sd_notify(0, "READY=1");
Daniel P. Berrange 731c6b
-#endif
Daniel P. Berrange 731c6b
+#ifdef HAVE_SYS_UN_H
Daniel P. Berrange 731c6b
+    const char *path;
Daniel P. Berrange 731c6b
+    const char *msg = "READY=1";
Daniel P. Berrange 731c6b
+    int fd;
Daniel P. Berrange 731c6b
+    struct sockaddr_un un = {
Daniel P. Berrange 731c6b
+        .sun_family = AF_UNIX,
Daniel P. Berrange 731c6b
+    };
Daniel P. Berrange 731c6b
+    struct iovec iov = {
Daniel P. Berrange 731c6b
+        .iov_base = (char *)msg,
Daniel P. Berrange 731c6b
+        .iov_len = strlen(msg),
Daniel P. Berrange 731c6b
+    };
Daniel P. Berrange 731c6b
+    struct msghdr mh = {
Daniel P. Berrange 731c6b
+        .msg_name = &un,
Daniel P. Berrange 731c6b
+        .msg_namelen = sizeof(un),
Daniel P. Berrange 731c6b
+        .msg_iov = &iov,
Daniel P. Berrange 731c6b
+        .msg_iovlen = 1,
Daniel P. Berrange 731c6b
+    };
Daniel P. Berrange 731c6b
+
Daniel P. Berrange 731c6b
+    if (!(path = virGetEnvBlockSUID("NOTIFY_SOCKET"))) {
Daniel P. Berrange 731c6b
+        VIR_DEBUG("Skipping systemd notify, not requested");
Daniel P. Berrange 731c6b
+        return;
Daniel P. Berrange 731c6b
+    }
Daniel P. Berrange 731c6b
+
Daniel P. Berrange 731c6b
+    /* NB sun_path field is *not* NUL-terminated, hence >, not >= */
Daniel P. Berrange 731c6b
+    if (strlen(path) > sizeof(un.sun_path)) {
Daniel P. Berrange 731c6b
+        VIR_WARN("Systemd notify socket path '%s' too long", path);
Daniel P. Berrange 731c6b
+        return;
Daniel P. Berrange 731c6b
+    }
Daniel P. Berrange 731c6b
+
Daniel P. Berrange 731c6b
+    memcpy(un.sun_path, path, strlen(path));
Daniel P. Berrange 731c6b
+    if (un.sun_path[0] == '@')
Daniel P. Berrange 731c6b
+        un.sun_path[0] = '\0';
Daniel P. Berrange 731c6b
+
Daniel P. Berrange 731c6b
+    fd = socket(AF_UNIX, SOCK_DGRAM, 0);
Daniel P. Berrange 731c6b
+    if (fd < 0) {
Daniel P. Berrange 731c6b
+        VIR_WARN("Unable to create socket FD");
Daniel P. Berrange 731c6b
+        return;
Daniel P. Berrange 731c6b
+    }
Daniel P. Berrange 731c6b
+
Daniel P. Berrange 731c6b
+    if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0)
Daniel P. Berrange 731c6b
+        VIR_WARN("Failed to notify systemd");
Daniel P. Berrange 731c6b
+
Daniel P. Berrange 731c6b
+    VIR_FORCE_CLOSE(fd);
Daniel P. Berrange 731c6b
+#endif /* HAVE_SYS_UN_H */
Daniel P. Berrange 731c6b
 }
Daniel P. Berrange 731c6b
 
Daniel P. Berrange 731c6b
 static int
Daniel P. Berrange 731c6b
-- 
Daniel P. Berrange 731c6b
2.5.5
Daniel P. Berrange 731c6b