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

[Xen-devel] [PATCH v5 03/14] 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/xenstored_core.c | 49 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index 47f0722..e456e67 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -40,10 +40,12 @@
 #include <signal.h>
 #include <assert.h>
 #include <setjmp.h>
+#include <config.h>
 
 #include "utils.h"
 #include "list.h"
 #include "talloc.h"
+#include "xenstore.h"
 #include "xenstore_lib.h"
 #include "xenstored_core.h"
 #include "xenstored_watch.h"
@@ -1706,6 +1708,11 @@ static void init_sockets(int **psock, int **pro_sock)
        static int minus_one = -1;
        *psock = *pro_sock = &minus_one;
 }
+static void xen_claim_active_sockets(struct xs_sd_ctx *ctx, int **psock, int 
**pro_sock)
+{
+       static int minus_one = -1;
+       *psock = *pro_sock = &minus_one;
+}
 #else
 static int destroy_fd(void *_fd)
 {
@@ -1714,6 +1721,26 @@ static int destroy_fd(void *_fd)
        return 0;
 }
 
+static void xen_claim_active_sockets(struct xs_sd_ctx *ctx, 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();
+
+       *psock = sock = talloc(talloc_autofree_context(), int);
+       *sock = xs_claim_active_socket(ctx, soc_str);
+       if (*sock <= 0)
+               barf_perror("%s", soc_str);
+
+       *pro_sock = ro_sock = talloc(talloc_autofree_context(), int);
+       *ro_sock = xs_claim_active_socket(ctx, 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);
+}
+
 static void init_sockets(int **psock, int **pro_sock)
 {
        struct sockaddr_un addr;
@@ -1822,8 +1849,10 @@ int main(int argc, char *argv[])
        bool dofork = true;
        bool outputpid = false;
        bool no_domain_init = false;
+       bool sd_booted = false;
        const char *pidfile = NULL;
        int timeout;
+       struct xs_sd_ctx *ctx;
 
        while ((opt = getopt_long(argc, argv, "DE:F:HNPS:t:T:RLVW:", options,
                                  NULL)) != -1) {
@@ -1884,6 +1913,16 @@ int main(int argc, char *argv[])
        if (optind != argc)
                barf("%s: No arguments desired", argv[0]);
 
+       ctx = xs_get_sd_ctx();
+       /* xs_sd_ctx can be NULL and that's OK */
+       if (xs_load_sd_required(ctx)) {
+               sd_booted = true;
+               dofork = false;
+               if (pidfile)
+                       barf("%s: PID file not needed on systemd", argv[0]);
+               pidfile = NULL;
+       }
+
        reopen_log();
 
        /* make sure xenstored directories exist */
@@ -1905,7 +1944,11 @@ int main(int argc, char *argv[])
        /* Don't kill us with SIGPIPE. */
        signal(SIGPIPE, SIG_IGN);
 
-       init_sockets(&sock, &ro_sock);
+       if (sd_booted)
+               xen_claim_active_sockets(ctx, &sock, &ro_sock);
+       else
+               init_sockets(&sock, &ro_sock);
+
        init_pipe(reopen_log_pipe);
 
        /* Setup the database */
@@ -1936,6 +1979,9 @@ int main(int argc, char *argv[])
        /* Tell the kernel we're up and running. */
        xenbus_notify_running();
 
+       if (sd_booted)
+               xs_sd_notify_ready(ctx);
+
        /* Main loop. */
        for (;;) {
                struct connection *conn, *next;
@@ -2047,6 +2093,7 @@ int main(int argc, char *argv[])
                initialize_fds(*sock, &sock_pollfd_idx, *ro_sock,
                               &ro_sock_pollfd_idx, &timeout);
        }
+       xs_free_sd_ctx(ctx);
 }
 
 /*
-- 
2.0.0.rc3.18.g00a5b79


_______________________________________________
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®.