[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v4 04/15] cxenstored: add support for systemd active sockets



From: "Luis R. Rodriguez" <mcgrof@xxxxxxxx>

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@xxxxxxxx>
---
 tools/xenstore/xs.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 125 insertions(+)

diff --git a/tools/xenstore/xs.c b/tools/xenstore/xs.c
index f1d0f99..9bbf736 100644
--- a/tools/xenstore/xs.c
+++ b/tools/xenstore/xs.c
@@ -1,6 +1,7 @@
 /* 
     Xen Store Daemon interface providing simple tree-like database.
     Copyright (C) 2005 Rusty Russell IBM Corporation
+    Copyright (C) 2014 Luis R. Rodriguez <mcgrof@xxxxxxxx>
 
     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public
@@ -34,6 +35,10 @@
 #include <errno.h>
 #include <config.h>
 
+#if defined(HAVE_SYSTEMD)
+#include <systemd/sd-daemon.h>
+#endif
+
 #include "xenstore.h"
 #include "list.h"
 #include "utils.h"
@@ -182,11 +187,131 @@ int xs_fileno(struct xs_handle *h)
        return h->watch_pipe[0];
 }
 
+#if defined(HAVE_SYSTEMD)
+
+/* Conforms to what we should send sd_is_socket_unix() */
+struct xen_systemd_active_socket {
+       int fd;
+       int type;
+       int listening;
+       const char *path;
+       size_t length;
+};
+
+/*
+ * We list stdin, stdout and stderr simply for documentation purposes
+ * and to help our array size fit the number of expected sockets we
+ * as sd_listen_fds() will return 5 for example if you set the socket
+ * service with 2 sockets.
+ */
+static struct xen_systemd_active_socket xenstore_active_sockets[] = {
+       {
+               .fd = SD_LISTEN_FDS_START -3,
+               .type = 0,
+               .listening = 0,
+               .path = "stdin",
+               .length = 0,
+       },
+       {
+               .fd = SD_LISTEN_FDS_START - 2,
+               .type = 0,
+               .listening = 0,
+               .path = "stderr",
+               .length = 0,
+       },
+       {
+               .fd = SD_LISTEN_FDS_START - 1,
+               .type = 0,
+               .listening = 0,
+               .path = "stderr",
+               .length = 0,
+       },
+       {
+               .fd = SD_LISTEN_FDS_START,
+               .type = SOCK_STREAM,
+               .listening = 0,
+               .path = "/var/run/xenstored/socket",
+               .length = 0,
+       },
+       {
+               .fd = SD_LISTEN_FDS_START + 1,
+               .type = SOCK_STREAM,
+               .listening = 0,
+               .path = "/var/run/xenstored/socket_ro",
+               .length = 0,
+       },
+};
+
+static struct xen_systemd_active_socket *get_xen_active_socket(const char 
*connect_to)
+{
+       unsigned int i;
+
+       for (i=0; i<ARRAY_SIZE(xenstore_active_sockets); i++) {
+               if (!strcmp(connect_to, xenstore_active_sockets[i].path)) {
+                       if (!xenstore_active_sockets[i].type)
+                               return NULL;
+                       return &xenstore_active_sockets[i];
+               }
+       }
+
+       return NULL;
+}
+
+static int xs_get_active_socket(const char *connect_to)
+{
+       int n, r;
+       struct xen_systemd_active_socket *active_socket;
+
+       active_socket = get_xen_active_socket(connect_to);
+       if (!active_socket)
+               return -1;
+
+       n = sd_listen_fds(0);
+       if (n <= 0)
+               return -1;
+       else if (n >= (ARRAY_SIZE(xenstore_active_sockets)))
+               return -1;
+
+       r = sd_is_socket_unix(active_socket->fd,
+                             active_socket->type,
+                             active_socket->listening,
+                             active_socket->path,
+                             active_socket->length);
+       if (r < 0)
+               return r;
+
+       return active_socket->fd;
+}
+
+/*
+ * If xenstored was built to depend on systemd libraries
+ * we assume you want all the bells and whistles with
+ * systemd.
+ */
+static int xs_active_socket_required(void)
+{
+       return 1;
+}
+#else
+static int xs_get_active_socket(const char *connect_to)
+{
+       return -1;
+}
+
+static int xs_active_socket_required(void)
+{
+       return 0;
+}
+#endif
+
 static int get_socket(const char *connect_to)
 {
        struct sockaddr_un addr;
        int sock, saved_errno, flags;
 
+       if (xs_active_socket_required())
+               return xs_get_active_socket(connect_to);
+
        sock = socket(PF_UNIX, SOCK_STREAM, 0);
        if (sock < 0)
                return -1;
-- 
1.9.0


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.