From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Luis R. Rodriguez" Subject: [PATCH v5 03/14] cxenstored: add support for systemd active sockets Date: Tue, 20 May 2014 05:31:24 -0700 Message-ID: <1400589095-3872-4-git-send-email-mcgrof@do-not-panic.com> References: <1400589095-3872-1-git-send-email-mcgrof@do-not-panic.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WmjD4-0006Aw-8p for xen-devel@lists.xenproject.org; Tue, 20 May 2014 12:31:58 +0000 Received: by mail-pd0-f172.google.com with SMTP id x10so285945pdj.3 for ; Tue, 20 May 2014 05:31:54 -0700 (PDT) In-Reply-To: <1400589095-3872-1-git-send-email-mcgrof@do-not-panic.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xenproject.org Cc: "Luis R. Rodriguez" List-Id: xen-devel@lists.xenproject.org From: "Luis R. Rodriguez" 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 --- 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 #include #include +#include #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