From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Luis R. Rodriguez" Subject: [PATCH v4 04/15] cxenstored: add support for systemd active sockets Date: Tue, 29 Apr 2014 18:11:57 -0700 Message-ID: <1398820328-15132-5-git-send-email-mcgrof@do-not-panic.com> References: <1398820328-15132-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 1WfJ4f-0000B3-0I for xen-devel@lists.xenproject.org; Wed, 30 Apr 2014 01:12:37 +0000 Received: by mail-pa0-f49.google.com with SMTP id kq14so1079490pab.22 for ; Tue, 29 Apr 2014 18:12:33 -0700 (PDT) In-Reply-To: <1398820328-15132-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/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 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 #include +#if defined(HAVE_SYSTEMD) +#include +#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))) + 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