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