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