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