xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: "Luis R. Rodriguez" <mcgrof@do-not-panic.com>
To: xen-devel@lists.xenproject.org
Cc: "Luis R. Rodriguez" <mcgrof@suse.com>
Subject: [PATCH v6 02/13] cxenstored: add support for systemd active sockets
Date: Thu, 12 Jun 2014 18:18:40 -0700	[thread overview]
Message-ID: <1402622331-4282-3-git-send-email-mcgrof@do-not-panic.com> (raw)
In-Reply-To: <1402622331-4282-1-git-send-email-mcgrof@do-not-panic.com>

From: "Luis R. Rodriguez" <mcgrof@suse.com>

This adds systemd socket activation support for the C xenstored.
Active sockets enable xenstored to be loaded only if required by a system
onto which Xen is installed on. Socket activation is handled by
systemd, once a port for a service which claims a socket is used
systemd will start the required services for it, on demand. For more
details on socket activation refer to Lennart's socket-activation
post regarding this [0].

Right now this code adds a no-op for this functionality, leaving the
enablement to be done later once systemd is properly hooked into
the build system. The socket activation is ordered in aligment with
the socket activation order passed on to systemd.

[0] http://0pointer.de/blog/projects/socket-activation2.html

Signed-off-by: Luis R. Rodriguez <mcgrof@suse.com>
---
 tools/xenstore/xenstored_core.c | 110 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 109 insertions(+), 1 deletion(-)

diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index 47f0722..6270618 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -40,6 +40,7 @@
 #include <signal.h>
 #include <assert.h>
 #include <setjmp.h>
+#include <config.h>
 
 #include "utils.h"
 #include "list.h"
@@ -54,6 +55,16 @@
 
 #include "hashtable.h"
 
+#ifndef NO_SOCKETS
+#if defined(HAVE_SYSTEMD)
+#define XEN_SYSTEMD_ENABLED 1
+#endif
+#endif
+
+#if defined(XEN_SYSTEMD_ENABLED)
+#include <systemd/sd-daemon.h>
+#endif
+
 extern xc_evtchn *xce_handle; /* in xenstored_domain.c */
 static int xce_pollfd_idx = -1;
 static struct pollfd *fds;
@@ -1714,6 +1725,81 @@ static int destroy_fd(void *_fd)
 	return 0;
 }
 
+#if defined(XEN_SYSTEMD_ENABLED)
+/* Will work regardless of the order systemd gives them to us */
+int xs_get_sd_fd(const char *connect_to)
+{
+	int fd = SD_LISTEN_FDS_START;
+	int r;
+
+	while (fd <= SD_LISTEN_FDS_START + 1) {
+		r = sd_is_socket_unix(fd, SOCK_STREAM, 1, connect_to, 0);
+		if (r > 0)
+			return fd;
+		fd++;
+	}
+
+	return -EBADR;
+}
+
+int xs_validate_active_socket(const char *connect_to)
+{
+	char sock[30];
+
+	/* We have to null terminate the socket path */
+	memset(sock, '\0', sizeof(sock));
+	memcpy(sock, connect_to, strlen(connect_to));
+
+	if ((strncmp("/var/run/xenstored/socket_ro", sock, 28) != 0) &&
+	    (strncmp("/var/run/xenstored/socket", sock, 25) != 0)) {
+		sd_notifyf(0, "STATUS=unexpected socket: %s\n"
+			   "ERRNO=%i",
+			   connect_to,
+			   EBADR);
+		return -EBADR;
+	}
+
+	return xs_get_sd_fd(sock);
+}
+
+static void xen_claim_active_sockets(int **psock, int **pro_sock)
+{
+	int *sock, *ro_sock;
+	const char *soc_str = xs_daemon_socket();
+	const char *soc_str_ro = xs_daemon_socket_ro();
+	int n;
+
+	n = sd_listen_fds(0);
+	if (n <= 0) {
+		sd_notifyf(0, "STATUS=Failed to get any active sockets: %s\n"
+			   "ERRNO=%i",
+			   strerror(errno),
+			   errno);
+		barf_perror("sd_listen_fds() failed\n");
+	} else if (n > 2) {
+		fprintf(stderr, SD_ERR "Expected 2 fds but given %d\n", n);
+		sd_notifyf(0, "STATUS=Mismatch on number (2): %s\n"
+			   "ERRNO=%d",
+			   strerror(EBADR),
+			   EBADR);
+		barf_perror("sd_listen_fds() gave too many fds\n");
+	}
+
+	*psock = sock = talloc(talloc_autofree_context(), int);
+	*sock = xs_validate_active_socket(soc_str);
+	if (*sock <= 0)
+		barf_perror("%s", soc_str);
+
+	*pro_sock = ro_sock = talloc(talloc_autofree_context(), int);
+	*ro_sock = xs_validate_active_socket(soc_str_ro);
+	if (*ro_sock <= 0)
+		barf_perror("%s", soc_str_ro);
+
+	talloc_set_destructor(sock, destroy_fd);
+	talloc_set_destructor(ro_sock, destroy_fd);
+}
+#endif
+
 static void init_sockets(int **psock, int **pro_sock)
 {
 	struct sockaddr_un addr;
@@ -1884,6 +1970,15 @@ int main(int argc, char *argv[])
 	if (optind != argc)
 		barf("%s: No arguments desired", argv[0]);
 
+#if defined(XEN_SYSTEMD_ENABLED)
+	if (sd_booted()) {
+		dofork = false;
+		if (pidfile)
+			barf("%s: PID file not needed on systemd", argv[0]);
+		pidfile = NULL;
+	}
+#endif
+
 	reopen_log();
 
 	/* make sure xenstored directories exist */
@@ -1905,7 +2000,13 @@ int main(int argc, char *argv[])
 	/* Don't kill us with SIGPIPE. */
 	signal(SIGPIPE, SIG_IGN);
 
-	init_sockets(&sock, &ro_sock);
+#if defined(XEN_SYSTEMD_ENABLED)
+	if (sd_booted())
+		xen_claim_active_sockets(&sock, &ro_sock);
+	else
+#endif
+		init_sockets(&sock, &ro_sock);
+
 	init_pipe(reopen_log_pipe);
 
 	/* Setup the database */
@@ -1936,6 +2037,13 @@ int main(int argc, char *argv[])
 	/* Tell the kernel we're up and running. */
 	xenbus_notify_running();
 
+#if defined(XEN_SYSTEMD_ENABLED)
+	if (sd_booted()) {
+		sd_notify(1, "READY=1");
+		fprintf(stderr, SD_NOTICE "xenstored is ready\n");
+	}
+#endif
+
 	/* Main loop. */
 	for (;;) {
 		struct connection *conn, *next;
-- 
2.0.0.rc3.18.g00a5b79

  parent reply	other threads:[~2014-06-13  1:19 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-13  1:18 [PATCH v6 00/13] xen: add systemd support Luis R. Rodriguez
2014-06-13  1:18 ` [PATCH v6 01/13] xenstored: enable usage of config.h on both xenstored and oxenstored Luis R. Rodriguez
2014-06-13  1:18 ` Luis R. Rodriguez [this message]
2014-07-02 13:02   ` [PATCH v6 02/13] cxenstored: add support for systemd active sockets Ian Campbell
2014-07-02 19:00     ` Luis R. Rodriguez
2014-07-03  9:13       ` Ian Campbell
2014-07-03 17:06         ` Luis R. Rodriguez
2014-07-04 14:32           ` Ian Campbell
2014-07-08  3:08             ` Luis R. Rodriguez
2014-07-17 19:45           ` Luis R. Rodriguez
2014-07-17 20:16             ` Dave Scott
2014-07-17 23:13               ` Luis R. Rodriguez
2014-07-18  9:27                 ` Anil Madhavapeddy
2014-06-13  1:18 ` [PATCH v6 03/13] oxenstored: " Luis R. Rodriguez
2014-07-02 13:08   ` Ian Campbell
2014-07-02 19:09     ` Luis R. Rodriguez
2014-07-03  8:42       ` Dave Scott
2014-07-03 17:43         ` Luis R. Rodriguez
2014-06-13  1:18 ` [PATCH v6 04/13] oxenstored: force FD_CLOEXEC with Unix.set_close_on_exec on LSB init Luis R. Rodriguez
2014-07-02 13:08   ` Ian Campbell
2014-06-13  1:18 ` [PATCH v6 05/13] tools/xendomains: make xl the default and remove xm usage Luis R. Rodriguez
2014-06-13  1:18 ` [PATCH v6 06/13] tools/xendomains: do space cleanups Luis R. Rodriguez
2014-06-13  1:18 ` [PATCH v6 07/13] tools/xendomains: move to libexec and use a smaller init helper Luis R. Rodriguez
2014-06-13  7:06   ` Jacek Konieczny
2014-06-13 18:32     ` Luis R. Rodriguez
2014-07-02 13:12   ` Ian Campbell
2014-06-13  1:18 ` [PATCH v6 08/13] autoconf: xen: force a refresh with autoconf Luis R. Rodriguez
2014-06-13  1:18 ` [PATCH v6 09/13] autoconf: update m4/pkg.m4 Luis R. Rodriguez
2014-07-02 13:13   ` Ian Campbell
2014-07-02 19:10     ` Luis R. Rodriguez
2014-06-13  1:18 ` [PATCH v6 10/13] autoconf: xen: move standard path variables to config/Paths.mk.in Luis R. Rodriguez
2014-07-02 13:34   ` Ian Campbell
2014-07-02 19:32     ` Luis R. Rodriguez
2014-07-03  9:15       ` Ian Campbell
2014-07-03 18:30         ` Luis R. Rodriguez
2014-07-04 14:33           ` Ian Campbell
2014-07-08 18:41             ` Luis R. Rodriguez
2014-07-08 18:53               ` Luis R. Rodriguez
2014-07-10 16:18                 ` Ian Campbell
2014-07-14 23:41                   ` Luis R. Rodriguez
2014-07-03 18:53     ` Luis R. Rodriguez
2014-07-04 14:36       ` Ian Campbell
2014-07-08 18:52         ` Luis R. Rodriguez
2014-07-10 16:16           ` Ian Campbell
2014-07-14 23:42             ` Luis R. Rodriguez
2014-06-13  1:18 ` [PATCH v6 11/13] xencommons: move module list into a generic place Luis R. Rodriguez
2014-07-02 13:44   ` Ian Campbell
2014-07-02 21:56     ` Luis R. Rodriguez
2014-07-03  9:15       ` Ian Campbell
2014-06-13  1:18 ` [PATCH v6 12/13] autoconf: xen: enable explicit preference option for xenstored preference Luis R. Rodriguez
2014-07-02 13:54   ` Ian Campbell
2014-07-02 20:09     ` Luis R. Rodriguez
2014-07-03  9:20       ` Ian Campbell
2014-07-03 18:57         ` Luis R. Rodriguez
2014-07-04 14:37           ` Ian Campbell
2014-07-08 18:54             ` Luis R. Rodriguez
2014-06-13  1:18 ` [PATCH v6 13/13] systemd: add xen systemd service and module files Luis R. Rodriguez
2014-06-13 19:55   ` Luis R. Rodriguez
2014-06-19 19:47   ` Bastian Blank
2014-06-19 20:21     ` Luis R. Rodriguez
2014-06-19 20:26       ` Luis R. Rodriguez
2014-06-20  5:59         ` David Sutton
2014-07-02 14:02   ` Ian Campbell
2014-07-02 20:25     ` Luis R. Rodriguez
2014-06-19 19:03 ` [PATCH v6 00/13] xen: add systemd support Luis R. Rodriguez
2014-07-02 14:48 ` Ian Campbell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1402622331-4282-3-git-send-email-mcgrof@do-not-panic.com \
    --to=mcgrof@do-not-panic.com \
    --cc=mcgrof@suse.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).