altarch-user / rpms / httpd

Forked from rpms/httpd 2 years ago
Clone

Blame SOURCES/httpd-2.4.28-socket-activation.patch

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