[PATCH weston 1/2] compositor: Add systemd notification support

Ander Conselvan de Oliveira ander.conselvan.de.oliveira at intel.com
Tue Jun 4 06:43:42 PDT 2013


Call sd_notify() when the server is ready to receive connections. This
allows weston to be called as systemd service with the 'notify' type, in
turn allowing systemd to delay execution of processes that need Weston
until it is ready to receive connections.

The call to sd_notify() is made through weston-launch if Weston is
launched by it, so that systemd receives the notification from what it
considers the main process.
---
 configure.ac        |    6 ++++++
 src/Makefile.am     |   10 ++++++----
 src/compositor.c    |   38 ++++++++++++++++++++++++++++++++++++++
 src/weston-launch.c |   19 +++++++++++++++++++
 src/weston-launch.h |    3 ++-
 5 files changed, 71 insertions(+), 5 deletions(-)

diff --git a/configure.ac b/configure.ac
index ba1fcac..b3cf249 100644
--- a/configure.ac
+++ b/configure.ac
@@ -53,6 +53,12 @@ if test x$enable_egl = xyes; then
 	COMPOSITOR_MODULES="$COMPOSITOR_MODULES egl >= 7.10 glesv2"
 fi
 
+PKG_CHECK_MODULES(SYSTEMD_DAEMON, [libsystemd-daemon],
+		  [enable_systemd_daemon=yes], [enable_systemd_daemon=no])
+if test x$enable_systemd_daemon = xyes; then
+	AC_DEFINE(HAVE_SYSTEMD_DAEMON, 1, [Have libsystemd-daemon support])
+fi
+
 PKG_CHECK_MODULES(COMPOSITOR, [$COMPOSITOR_MODULES])
 
 AC_ARG_ENABLE(setuid-install, [  --enable-setuid-install],,
diff --git a/src/Makefile.am b/src/Makefile.am
index 106ccba..8229936 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -9,8 +9,9 @@ AM_CPPFLAGS =					\
 	-DIN_WESTON
 
 weston_LDFLAGS = -export-dynamic
-weston_CFLAGS = $(GCC_CFLAGS) $(COMPOSITOR_CFLAGS) $(LIBUNWIND_CFLAGS)
-weston_LDADD = $(COMPOSITOR_LIBS) $(LIBUNWIND_LIBS) \
+weston_CFLAGS = $(GCC_CFLAGS) $(COMPOSITOR_CFLAGS) $(LIBUNWIND_CFLAGS) \
+	$(SYSTEMD_DAEMON_CFLAGS)
+weston_LDADD = $(COMPOSITOR_LIBS) $(LIBUNWIND_LIBS) $(SYSTEMD_DAEMON_LIBS) \
 	$(DLOPEN_LIBS) -lm ../shared/libshared.la
 
 weston_SOURCES =				\
@@ -73,8 +74,9 @@ weston_launch = weston-launch
 weston_launch_SOURCES = weston-launch.c weston-launch.h
 weston_launch_CFLAGS= $(GCC_CFLAGS)
 weston_launch_CPPFLAGS = $(WESTON_LAUNCH_CFLAGS) $(SYSTEMD_LOGIN_CFLAGS) \
-		 -DBINDIR='"$(bindir)"'
-weston_launch_LDADD = $(WESTON_LAUNCH_LIBS) $(SYSTEMD_LOGIN_LIBS)
+	$(SYSTEMD_DAEMON_CFLAGS) -DBINDIR='"$(bindir)"'
+weston_launch_LDADD = $(WESTON_LAUNCH_LIBS) $(SYSTEMD_LOGIN_LIBS) \
+	$(SYSTEMD_DAEMON_LIBS)
 
 if ENABLE_SETUID_INSTALL
 install-exec-hook:
diff --git a/src/compositor.c b/src/compositor.c
index 37fce0a..f0208eb 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -48,18 +48,24 @@
 #include <setjmp.h>
 #include <sys/time.h>
 #include <time.h>
+#include <errno.h>
 
 #ifdef HAVE_LIBUNWIND
 #define UNW_LOCAL_ONLY
 #include <libunwind.h>
 #endif
 
+#ifdef HAVE_SYSTEMD_DAEMON
+#include <systemd/sd-daemon.h>
+#endif
+
 #include <wayland-server.h>
 #include "compositor.h"
 #include "subsurface-server-protocol.h"
 #include "../shared/os-compatibility.h"
 #include "git-version.h"
 #include "version.h"
+#include "weston-launch.h"
 
 static struct wl_list child_process_list;
 static struct weston_compositor *segv_compositor;
@@ -3121,6 +3127,36 @@ verify_xdg_runtime_dir(void)
 	}
 }
 
+static void
+notify_ready(struct weston_compositor *compositor)
+{
+	struct msghdr msg;
+	struct iovec iov;
+	ssize_t len;
+	struct weston_launcher_message message;
+
+#ifdef HAVE_SYSTEMD_DAEMON
+	if (compositor->launcher_sock == -1)
+		sd_notify(1, "READY=1");
+#endif
+
+	if (compositor->launcher_sock == -1)
+		return;
+
+	memset(&msg, 0, sizeof msg);
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+
+	iov.iov_base = &message;
+	iov.iov_len = sizeof message;
+
+	message.opcode = WESTON_LAUNCHER_NOTIFY_READY;
+
+	do {
+		len = sendmsg(compositor->launcher_sock, &msg, 0);
+	} while (len < 0 && errno == EINTR);
+}
+
 static int
 usage(int error_code)
 {
@@ -3319,6 +3355,8 @@ int main(int argc, char *argv[])
 
 	weston_compositor_wake(ec);
 
+	notify_ready(ec);
+
 	wl_display_run(display);
 
  out:
diff --git a/src/weston-launch.c b/src/weston-launch.c
index ad77476..f93fe01 100644
--- a/src/weston-launch.c
+++ b/src/weston-launch.c
@@ -58,8 +58,14 @@
 #include <systemd/sd-login.h>
 #endif
 
+#ifdef HAVE_SYSTEMD_DAEMON
+#include <systemd/sd-daemon.h>
+#endif
+
 #include "weston-launch.h"
 
+#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
+
 #define MAX_ARGV_SIZE 256
 
 struct weston_launch {
@@ -253,6 +259,16 @@ setenv_fd(const char *env, int fd)
 }
 
 static int
+handle_notify_ready(struct weston_launch *wl, struct msghdr *msg, ssize_t len)
+{
+#ifdef HAVE_SYSTEMD_DAEMON
+	sd_notify(1, "READY=1");
+#endif
+
+	return 0;
+}
+
+static int
 handle_setmaster(struct weston_launch *wl, struct msghdr *msg, ssize_t len)
 {
 	int ret = -1;
@@ -400,6 +416,9 @@ handle_socket_msg(struct weston_launch *wl)
 	case WESTON_LAUNCHER_DRM_SET_MASTER:
 		ret = handle_setmaster(wl, &msg, len);
 		break;
+	case WESTON_LAUNCHER_NOTIFY_READY:
+		ret = handle_notify_ready(wl, &msg, len);
+		break;
 	}
 
 	return ret;
diff --git a/src/weston-launch.h b/src/weston-launch.h
index 5be013e..107bf23 100644
--- a/src/weston-launch.h
+++ b/src/weston-launch.h
@@ -25,7 +25,8 @@
 
 enum weston_launcher_opcode {
 	WESTON_LAUNCHER_OPEN,
-	WESTON_LAUNCHER_DRM_SET_MASTER
+	WESTON_LAUNCHER_DRM_SET_MASTER,
+	WESTON_LAUNCHER_NOTIFY_READY
 };
 
 struct weston_launcher_message {
-- 
1.7.10.4



More information about the wayland-devel mailing list