From: Hannes Reinecke <h...@suse.de>

Implement code to fetch sockets from systemd, and add socket
and service files.

Signed-off-by: Hannes Reinecke <h...@suse.de>
---
 utils/open-isns/isns.h        |  1 +
 utils/open-isns/isnsd.c       | 15 +++++++--
 utils/open-isns/isnsd.service | 15 +++++++++
 utils/open-isns/isnsd.socket  |  9 ++++++
 utils/open-isns/socket.c      | 73 ++++++++++++++++++++++++++++++++++++++++---
 5 files changed, 107 insertions(+), 6 deletions(-)
 create mode 100644 utils/open-isns/isnsd.service
 create mode 100644 utils/open-isns/isnsd.socket

diff --git a/utils/open-isns/isns.h b/utils/open-isns/isns.h
index d17e18259f30..fa6ec3725c84 100644
--- a/utils/open-isns/isns.h
+++ b/utils/open-isns/isns.h
@@ -485,6 +485,7 @@ extern isns_socket_t *      isns_create_server_socket(const 
char *hostname, const cha
                                int af_hint, int sock_type);
 extern isns_socket_t * isns_create_client_socket(const char *hostname, const 
char *portname,
                                int af_hint, int sock_type);
+extern isns_socket_t *  isns_create_systemd_socket(int index);
 extern isns_socket_t * isns_create_bound_client_socket(const char *myaddr,
                                const char *hostname, const char *portname,
                                int af_hint, int sock_type);
diff --git a/utils/open-isns/isnsd.c b/utils/open-isns/isnsd.c
index 47dbb69af600..f98ef20c6337 100644
--- a/utils/open-isns/isnsd.c
+++ b/utils/open-isns/isnsd.c
@@ -235,20 +235,31 @@ run_server(isns_server_t *server, isns_db_t *db)
        if (status != ISNS_SUCCESS)
                isns_fatal("Problem loading Discovery Domains from database\n");
 
+
+       /* First socket is the control socket */
+       sock = isns_create_systemd_socket(0);
+       if (sock) {
+               /* Second socket is the iSNS port */
+               sock = isns_create_systemd_socket(1);
+               isns_debug_socket("Using systemd fds\n");
+               goto set_security;
+       }
+
        if (isns_config.ic_control_socket) {
                sock = isns_create_server_socket(isns_config.ic_control_socket,
                                NULL, AF_UNSPEC, SOCK_STREAM);
                if (sock == NULL)
                        isns_fatal("Unable to create control socket\n");
                /*
-               isns_socket_set_security_ctx(sock, ctx);
-                  */
+                * isns_socket_set_security_ctx(sock, ctx);
+                */
        }
 
        sock = isns_create_server_socket(isns_config.ic_bind_address,
                        "isns", opt_af, SOCK_STREAM);
        if (sock == NULL)
                isns_fatal("Unable to create server socket\n");
+set_security:
        isns_socket_set_security_ctx(sock, ctx);
 
        if (isns_config.ic_slp_register) {
diff --git a/utils/open-isns/isnsd.service b/utils/open-isns/isnsd.service
new file mode 100644
index 000000000000..143757960bdf
--- /dev/null
+++ b/utils/open-isns/isnsd.service
@@ -0,0 +1,15 @@
+[Unit]
+Description=iSNS daemon
+Before=iscsi.service iscsid.service
+After=syslog.target
+DefaultDependencies=no
+Conflicts=shutdown.target
+
+[Service]
+Type=simple
+ExecStart=/usr/sbin/isnsd -f -d all
+LimitCORE=infinity
+
+[Install]
+WantedBy=sysinit.target
+Also=isnsd.socket
diff --git a/utils/open-isns/isnsd.socket b/utils/open-isns/isnsd.socket
new file mode 100644
index 000000000000..652cd46cc9e7
--- /dev/null
+++ b/utils/open-isns/isnsd.socket
@@ -0,0 +1,9 @@
+[Socket]
+ListenStream=/var/run/isnsctl
+PassCredentials=true
+
+[Socket]
+ListenStream=3205
+
+[Install]
+WantedBy=sockets.target
diff --git a/utils/open-isns/socket.c b/utils/open-isns/socket.c
index ecf073874bcd..883df51b8993 100644
--- a/utils/open-isns/socket.c
+++ b/utils/open-isns/socket.c
@@ -44,6 +44,7 @@ enum {
 static isns_socket_t *__isns_create_socket(struct addrinfo *src,
                        struct addrinfo *dst,
                        int sock_type);
+static isns_socket_t *__isns_create_socket_from_fd(int, int);
 static struct addrinfo *isns_get_address_list(const char *, const char *,
                        int, int, int);
 static void    release_addrinfo(struct addrinfo *);
@@ -540,6 +541,37 @@ isns_net_error(isns_socket_t *sock, int err_code)
 }
 
 /*
+ * Create a socket from systemd fd
+ */
+isns_socket_t *
+isns_create_systemd_socket(int idx)
+{
+       const char *env;
+       unsigned int p, fds;
+
+       env = getenv("LISTEN_PID");
+       if (!env)
+               return NULL;
+
+       if (sscanf(env, "%u", &p) != 1)
+               return NULL;
+       if ((pid_t)p != getpid())
+               return NULL;
+
+       env = getenv("LISTEN_FDS");
+       if (!env)
+               return NULL;
+
+       if (sscanf(env, "%u", &fds) != 1)
+               return NULL;
+
+       if (idx >= fds)
+               return NULL;
+
+       return __isns_create_socket_from_fd(idx + 3, SOCK_STREAM);
+}
+
+/*
  * Create a passive socket (server side)
  */
 isns_socket_t *
@@ -1250,6 +1282,40 @@ failed:
        return NULL;
 }
 
+isns_socket_t *
+__isns_create_socket_from_fd(int fd, int sock_type)
+{
+       isns_socket_t *sock;
+       struct sockaddr addr;
+       socklen_t alen;
+       struct addrinfo *src;
+
+       sock = isns_net_alloc(fd);
+       alen = sizeof(addr);
+       if (getsockname(fd, &addr, &alen) < 0) {
+               isns_debug_socket("getsockname on fd %d failed, %m\n", fd);
+               isns_socket_free(sock);
+               return NULL;
+       }
+
+       src = __make_addrinfo(&addr, alen, sock_type);
+       isns_sockaddr_init(&sock->is_dst, NULL);
+       isns_sockaddr_init(&sock->is_src, src);
+       if (sock_type == SOCK_DGRAM) {
+               sock->is_poll_in = isns_net_dgram_recv;
+               sock->is_poll_out = isns_net_dgram_xmit;
+               sock->is_state = ISNS_SOCK_IDLE;
+       } else {
+               sock->is_poll_in = isns_net_stream_accept;
+               sock->is_error = isns_net_stream_error;
+               sock->is_state = ISNS_SOCK_LISTENING;
+       }
+       sock->is_poll_mask = POLLIN;
+
+       isns_list_append(&all_sockets, &sock->is_list);
+       return sock;
+}
+
 /*
  * Connect to the master process
  */
@@ -1723,8 +1789,7 @@ again:
                }
 
                /* Check whether pending messages have timed out. */
-               while ((msg = isns_message_queue_head(&sock->is_pending)) !=
-                       NULL) {
+               while ((msg = isns_message_queue_head(&sock->is_pending))) {
                        if (__timeout_expired(&now, &msg->im_timeout)) {
                                isns_debug_socket("sock %p message %04x timed 
out\n",
                                                sock, msg->im_xid);
@@ -1770,7 +1835,7 @@ again:
                        isns_message_release(msg);
                }
 
-               /* 
+               /*
                 * If the socket on which we're waiting right
                 * now got disconnected, or had any other kind of
                 * error, return right away to let the caller know.
@@ -1825,7 +1890,7 @@ kill_socket:
                if (millisec == 0)
                        millisec += 1;
 
-               debug_verbose2("poll(%p, %u, %d)\n", pfd, count, millisec);
+               isns_debug_socket("poll(%p, %u, %d)\n", pfd, count, millisec);
                r = poll(pfd, count, millisec);
        } else {
                r = poll(pfd, count, -1);
-- 
2.1.2

-- 
You received this message because you are subscribed to the Google Groups 
"open-iscsi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to open-iscsi+unsubscr...@googlegroups.com.
To post to this group, send email to open-iscsi@googlegroups.com.
Visit this group at http://groups.google.com/group/open-iscsi.
For more options, visit https://groups.google.com/d/optout.

Reply via email to