906948
diff --git a/server/listen.c b/server/listen.c
906948
index 5242c2a..e2e028a 100644
906948
--- a/server/listen.c
906948
+++ b/server/listen.c
906948
@@ -34,6 +34,10 @@
906948
 #include <unistd.h>
906948
 #endif
906948
 
906948
+#ifdef HAVE_SYSTEMD
906948
+#include <systemd/sd-daemon.h>
906948
+#endif
906948
+
906948
 /* we know core's module_index is 0 */
906948
 #undef APLOG_MODULE_INDEX
906948
 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
906948
@@ -59,9 +63,12 @@ static int ap_listenbacklog;
906948
 static int ap_listencbratio;
906948
 static int send_buffer_size;
906948
 static int receive_buffer_size;
906948
+#ifdef HAVE_SYSTEMD
906948
+static int use_systemd = -1;
906948
+#endif
906948
 
906948
 /* TODO: make_sock is just begging and screaming for APR abstraction */
906948
-static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
906948
+static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server, int do_bind_listen)
906948
 {
906948
     apr_socket_t *s = server->sd;
906948
     int one = 1;
906948
@@ -94,20 +101,6 @@ static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
906948
         return stat;
906948
     }
906948
 
906948
-#if APR_HAVE_IPV6
906948
-    if (server->bind_addr->family == APR_INET6) {
906948
-        stat = apr_socket_opt_set(s, APR_IPV6_V6ONLY, v6only_setting);
906948
-        if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
906948
-            ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(00069)
906948
-                          "make_sock: for address %pI, apr_socket_opt_set: "
906948
-                          "(IPV6_V6ONLY)",
906948
-                          server->bind_addr);
906948
-            apr_socket_close(s);
906948
-            return stat;
906948
-        }
906948
-    }
906948
-#endif
906948
-
906948
     /*
906948
      * To send data over high bandwidth-delay connections at full
906948
      * speed we must force the TCP window to open wide enough to keep the
906948
@@ -169,21 +162,37 @@ static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
906948
     }
906948
 #endif
906948
 
906948
-    if ((stat = apr_socket_bind(s, server->bind_addr)) != APR_SUCCESS) {
906948
-        ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, stat, p, APLOGNO(00072)
906948
-                      "make_sock: could not bind to address %pI",
906948
-                      server->bind_addr);
906948
-        apr_socket_close(s);
906948
-        return stat;
906948
-    }
906948
+    if (do_bind_listen) {
906948
+#if APR_HAVE_IPV6
906948
+        if (server->bind_addr->family == APR_INET6) {
906948
+            stat = apr_socket_opt_set(s, APR_IPV6_V6ONLY, v6only_setting);
906948
+            if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
906948
+                ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(00069)
906948
+                              "make_sock: for address %pI, apr_socket_opt_set: "
906948
+                              "(IPV6_V6ONLY)",
906948
+                              server->bind_addr);
906948
+                apr_socket_close(s);
906948
+                return stat;
906948
+            }
906948
+        }
906948
+#endif
906948
 
906948
-    if ((stat = apr_socket_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
906948
-        ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, stat, p, APLOGNO(00073)
906948
-                      "make_sock: unable to listen for connections "
906948
-                      "on address %pI",
906948
-                      server->bind_addr);
906948
-        apr_socket_close(s);
906948
-        return stat;
906948
+        if ((stat = apr_socket_bind(s, server->bind_addr)) != APR_SUCCESS) {
906948
+            ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, stat, p, APLOGNO(00072)
906948
+                          "make_sock: could not bind to address %pI",
906948
+                          server->bind_addr);
906948
+            apr_socket_close(s);
906948
+            return stat;
906948
+        }
906948
+
906948
+        if ((stat = apr_socket_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
906948
+            ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, stat, p, APLOGNO(00073)
906948
+                          "make_sock: unable to listen for connections "
906948
+                          "on address %pI",
906948
+                          server->bind_addr);
906948
+            apr_socket_close(s);
906948
+            return stat;
906948
+        }
906948
     }
906948
 
906948
 #ifdef WIN32
906948
@@ -315,6 +324,123 @@ static int find_listeners(ap_listen_rec **from, ap_listen_rec **to,
906948
     return found;
906948
 }
906948
 
906948
+#ifdef HAVE_SYSTEMD
906948
+
906948
+static int find_systemd_socket(process_rec * process, apr_port_t port) {
906948
+    int fdcount, fd;
906948
+    int sdc = sd_listen_fds(0);
906948
+
906948
+    if (sdc < 0) {
906948
+        ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02486)
906948
+                      "find_systemd_socket: Error parsing enviroment, sd_listen_fds returned %d",
906948
+                      sdc);
906948
+        return -1;
906948
+    }
906948
+
906948
+    if (sdc == 0) {
906948
+        ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
906948
+                      "find_systemd_socket: At least one socket must be set.");
906948
+        return -1;
906948
+    }
906948
+
906948
+    fdcount = atoi(getenv("LISTEN_FDS"));
906948
+    for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fdcount; fd++) {
906948
+        if (sd_is_socket_inet(fd, 0, 0, -1, port) > 0) {
906948
+            return fd;
906948
+        }
906948
+    }
906948
+
906948
+    return -1;
906948
+}
906948
+
906948
+static apr_status_t alloc_systemd_listener(process_rec * process,
906948
+                                           int fd, const char *proto,
906948
+                                           ap_listen_rec **out_rec)
906948
+{
906948
+    apr_status_t rv;
906948
+    struct sockaddr sa;
906948
+    socklen_t len = sizeof(struct sockaddr);
906948
+    apr_os_sock_info_t si;
906948
+    ap_listen_rec *rec;
906948
+    *out_rec = NULL;
906948
+
906948
+    memset(&si, 0, sizeof(si));
906948
+
906948
+    rv = getsockname(fd, &sa, &len;;
906948
+
906948
+    if (rv != 0) {
906948
+        rv = apr_get_netos_error();
906948
+        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02489)
906948
+                      "getsockname on %d failed.", fd);
906948
+        return rv;
906948
+    }
906948
+
906948
+    si.os_sock = &fd;
906948
+    si.family = sa.sa_family;
906948
+    si.local = &sa;
906948
+    si.type = SOCK_STREAM;
906948
+    si.protocol = APR_PROTO_TCP;
906948
+
906948
+    rec = apr_palloc(process->pool, sizeof(ap_listen_rec));
906948
+    rec->active = 0;
906948
+    rec->next = 0;
906948
+
906948
+
906948
+    rv = apr_os_sock_make(&rec->sd, &si, process->pool);
906948
+    if (rv != APR_SUCCESS) {
906948
+        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02490)
906948
+                      "apr_os_sock_make on %d failed.", fd);
906948
+        return rv;
906948
+    }
906948
+
906948
+    rv = apr_socket_addr_get(&rec->bind_addr, APR_LOCAL, rec->sd);
906948
+    if (rv != APR_SUCCESS) {
906948
+        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02491)
906948
+                      "apr_socket_addr_get on %d failed.", fd);
906948
+        return rv;
906948
+    }
906948
+
906948
+    rec->protocol = apr_pstrdup(process->pool, proto);
906948
+
906948
+    *out_rec = rec;
906948
+
906948
+    return make_sock(process->pool, rec, 0);
906948
+}
906948
+
906948
+static const char *set_systemd_listener(process_rec *process, apr_port_t port,
906948
+                                        const char *proto)
906948
+{
906948
+    ap_listen_rec *last, *new;
906948
+    apr_status_t rv;
906948
+    int fd = find_systemd_socket(process, port);
906948
+    if (fd < 0) {
906948
+        return "Systemd socket activation is used, but this port is not "
906948
+                "configured in systemd";
906948
+    }
906948
+
906948
+    last = ap_listeners;
906948
+    while (last && last->next) {
906948
+        last = last->next;
906948
+    }
906948
+
906948
+    rv = alloc_systemd_listener(process, fd, proto, &new;;
906948
+    if (rv != APR_SUCCESS) {
906948
+        return "Failed to setup socket passed by systemd using socket activation";
906948
+    }
906948
+
906948
+    if (last == NULL) {
906948
+        ap_listeners = last = new;
906948
+    }
906948
+    else {
906948
+        last->next = new;
906948
+        last = new;
906948
+    }
906948
+
906948
+    return NULL;
906948
+}
906948
+
906948
+#endif /* HAVE_SYSTEMD */
906948
+
906948
 static const char *alloc_listener(process_rec *process, const char *addr,
906948
                                   apr_port_t port, const char* proto,
906948
                                   void *slave)
906948
@@ -495,7 +621,7 @@ static int open_listeners(apr_pool_t *pool)
906948
                 }
906948
             }
906948
 #endif
906948
-            if (make_sock(pool, lr) == APR_SUCCESS) {
906948
+            if (make_sock(pool, lr, 1) == APR_SUCCESS) {
906948
                 ++num_open;
906948
             }
906948
             else {
906948
@@ -607,8 +733,28 @@ AP_DECLARE(int) ap_setup_listeners(server_rec *s)
906948
         }
906948
     }
906948
 
906948
-    if (open_listeners(s->process->pool)) {
906948
-        return 0;
906948
+#ifdef HAVE_SYSTEMD
906948
+    if (use_systemd) {
906948
+        const char *userdata_key = "ap_open_systemd_listeners";
906948
+        void *data;
906948
+        /* clear the enviroment on our second run
906948
+        * so that none of our future children get confused.
906948
+        */
906948
+        apr_pool_userdata_get(&data, userdata_key, s->process->pool);
906948
+        if (!data) {
906948
+            apr_pool_userdata_set((const void *)1, userdata_key,
906948
+                                apr_pool_cleanup_null, s->process->pool);
906948
+        }
906948
+        else {
906948
+            sd_listen_fds(1);
906948
+        }
906948
+    }
906948
+    else
906948
+#endif
906948
+    {
906948
+        if (open_listeners(s->process->pool)) {
906948
+            return 0;
906948
+        }
906948
     }
906948
 
906948
     for (lr = ap_listeners; lr; lr = lr->next) {
906948
@@ -698,7 +844,7 @@ AP_DECLARE(apr_status_t) ap_duplicate_listeners(apr_pool_t *p, server_rec *s,
906948
                             duplr->bind_addr);
906948
                 return stat;
906948
             }
906948
-            make_sock(p, duplr);
906948
+            make_sock(p, duplr, 1);
906948
 #if AP_NONBLOCK_WHEN_MULTI_LISTEN
906948
             use_nonblock = (ap_listeners && ap_listeners->next);
906948
             stat = apr_socket_opt_set(duplr->sd, APR_SO_NONBLOCK, use_nonblock);
906948
@@ -825,6 +971,11 @@ AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
906948
     if (argc < 1 || argc > 2) {
906948
         return "Listen requires 1 or 2 arguments.";
906948
     }
906948
+#ifdef HAVE_SYSTEMD
906948
+    if (use_systemd == -1) {
906948
+        use_systemd = sd_listen_fds(0) > 0;
906948
+    }
906948
+#endif
906948
 
906948
     rv = apr_parse_addr_port(&host, &scope_id, &port, argv[0], cmd->pool);
906948
     if (rv != APR_SUCCESS) {
906948
@@ -856,6 +1007,12 @@ AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
906948
         ap_str_tolower(proto);
906948
     }
906948
 
906948
+#ifdef HAVE_SYSTEMD
906948
+    if (use_systemd) {
906948
+        return set_systemd_listener(cmd->server->process, port, proto);
906948
+    }
906948
+#endif
906948
+
906948
     return alloc_listener(cmd->server->process, host, port, proto, NULL);
906948
 }
906948