]> git.ipfire.org Git - thirdparty/apache/httpd.git/commitdiff
Remove libsystemd dependency from main httpd binary
authorJoe Orton <jorton@apache.org>
Tue, 12 Apr 2022 15:43:04 +0000 (15:43 +0000)
committerJoe Orton <jorton@apache.org>
Tue, 12 Apr 2022 15:43:04 +0000 (15:43 +0000)
Until this change httpd was linking libsystemd to the main httpd binary. If you want to run lightweight version of httpd in container, sometimes you just want to install
httpd binary with as little dependencies as possible to make container small in size and do not pull uncencessary dependencies and libraries.

This change will move all systemd library calls from listen.c to mod_systemd module and remove systemd linking from the main httpd bin.
Fixed mixed declaration and wrongly declared variable.

Submitted by: Luboš Uhliarik <luhliari redhat.com>

Github: closes #312

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1899784 13f79535-47bb-0310-9956-ffa450edef68

CHANGES
acinclude.m4
include/ap_listen.h
modules/arch/unix/mod_systemd.c
server/listen.c

diff --git a/CHANGES b/CHANGES
index dfc95693ce4ab845ddad3d90dd040c37d44ea787..ae5bcce9cf41de131fa7d8240c9e15353c584275 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,10 @@
                                                          -*- coding: utf-8 -*-
 Changes with Apache 2.5.1
 
+  *) mod_systemd: Systemd socket activation can now be enabled at
+     build time but disabled at run time, if mod_systemd is not
+     loaded.  [Lubos Uhliarik <luhliari redhat.com>]
+
   *) ab: Add an optional ramp delay when starting concurrent connections so
      as to not trigger denial of service protection in the network. Report
      levels of concurrency achieved in cases where the test completes before
index a879eaabf2791aef94ada29d4591fd121068c774..c40cbd2304b4499c00c0e68fbae811753906044c 100644 (file)
@@ -831,7 +831,6 @@ case $host in
       if test "${ac_cv_header_systemd_sd_daemon_h}" = "no"; then
         AC_MSG_WARN([Your system does not support systemd.])
       else
-        APR_ADDTO(HTTPD_LIBS, [$SYSTEMD_LIBS])
         AC_DEFINE(HAVE_SYSTEMD, 1, [Define if systemd is supported])
       fi
    fi
index 1f151f98a48aaac8319b81b7f6e5242ab2eb2dc6..184f352574eb0696d79ee4efc90d9086c9f5f0df 100644 (file)
@@ -29,6 +29,7 @@
 #include "apr_network_io.h"
 #include "httpd.h"
 #include "http_config.h"
+#include "apr_optional.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -161,6 +162,15 @@ AP_DECLARE_NONSTD(const char *) ap_set_accept_errors_nonfatal(cmd_parms *cmd,
                                                            void *dummy,
                                                            int flag);
 
+#ifdef HAVE_SYSTEMD
+APR_DECLARE_OPTIONAL_FN(int,
+                        ap_find_systemd_socket, (process_rec *, apr_port_t));
+
+APR_DECLARE_OPTIONAL_FN(int,
+                        ap_systemd_listen_fds, (int));
+#endif
+
+
 #define LISTEN_COMMANDS \
 AP_INIT_TAKE1("ListenBacklog", ap_set_listenbacklog, NULL, RSRC_CONF, \
   "Maximum length of the queue of pending connections, as used by listen(2)"), \
index c3e7082df1f57ccd065b6234a05105c37a8f6cfa..6439a5c8ef95cd083d2bc8e2f65fa006b82012a3 100644 (file)
 #include <unistd.h>
 #endif
 
+APR_DECLARE_OPTIONAL_FN(int,
+                        ap_find_systemd_socket, (process_rec *, apr_port_t));
+
+APR_DECLARE_OPTIONAL_FN(int,
+                        ap_systemd_listen_fds, (int));
+
 static int systemd_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
                               apr_pool_t *ptemp)
 {
@@ -96,8 +102,42 @@ static int systemd_monitor(apr_pool_t *p, server_rec *s)
     return DECLINED;
 }
 
+static int ap_find_systemd_socket(process_rec * process, apr_port_t port) {
+    int fdcount, fd;
+    int sdc = sd_listen_fds(0);
+
+    if (sdc < 0) {
+        ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02486)
+                      "find_systemd_socket: Error parsing enviroment, sd_listen_fds returned %d",
+                      sdc);
+        return -1;
+    }
+
+    if (sdc == 0) {
+        ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
+                      "find_systemd_socket: At least one socket must be set.");
+        return -1;
+    }
+
+    fdcount = atoi(getenv("LISTEN_FDS"));
+    for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fdcount; fd++) {
+        if (sd_is_socket_inet(fd, 0, 0, -1, port) > 0) {
+            return fd;
+        }
+    }
+
+    return -1;
+}
+
+static int ap_systemd_listen_fds(int unset_environment){
+    return sd_listen_fds(unset_environment);
+}
+
 static void systemd_register_hooks(apr_pool_t *p)
 {
+    APR_REGISTER_OPTIONAL_FN(ap_systemd_listen_fds);
+    APR_REGISTER_OPTIONAL_FN(ap_find_systemd_socket);
+
     /* Enable ap_extended_status. */
     ap_hook_pre_config(systemd_pre_config, NULL, NULL, APR_HOOK_LAST);
     /* Signal service is ready. */
index 812bdd8bb3867ec63cea30257a2fc98eaed7b7fc..93158fff7bdbb688c29329fa728980fb3338e49e 100644 (file)
 #include <unistd.h>
 #endif
 
-#ifdef HAVE_SYSTEMD
-#include <systemd/sd-daemon.h>
-#endif
-
 /* we know core's module_index is 0 */
 #undef APLOG_MODULE_INDEX
 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
@@ -309,34 +305,6 @@ static apr_status_t close_listeners_on_exec(void *v)
 
 #ifdef HAVE_SYSTEMD
 
-static int find_systemd_socket(process_rec * process, apr_port_t port)
-{
-    int fdcount, fd;
-    int sdc = sd_listen_fds(0);
-
-    if (sdc < 0) {
-        ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02486)
-                      "find_systemd_socket: Error parsing environment, sd_listen_fds returned %d",
-                      sdc);
-        return -1;
-    }
-
-    if (sdc == 0) {
-        ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
-                      "find_systemd_socket: At least one socket must be set.");
-        return -1;
-    }
-
-    fdcount = atoi(getenv("LISTEN_FDS"));
-    for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fdcount; fd++) {
-        if (sd_is_socket_inet(fd, 0, 0, -1, port) > 0) {
-            return fd;
-        }
-    }
-
-    return -1;
-}
-
 static apr_status_t alloc_systemd_listener(process_rec * process,
                                            int fd, const char *proto,
                                            ap_listen_rec **out_rec)
@@ -395,7 +363,16 @@ static const char *set_systemd_listener(process_rec *process, apr_port_t port,
 {
     ap_listen_rec *last, *new;
     apr_status_t rv;
-    int fd = find_systemd_socket(process, port);
+    APR_OPTIONAL_FN_TYPE(ap_find_systemd_socket) *find_systemd_socket;
+    int fd;
+
+    find_systemd_socket = APR_RETRIEVE_OPTIONAL_FN(ap_find_systemd_socket);
+
+    if (!find_systemd_socket)
+       return "Systemd socket activation is used, but mod_systemd is probably "
+               "not loaded";
+
+    fd = find_systemd_socket(process, port);
     if (fd < 0) {
         return "Systemd socket activation is used, but this port is not "
                 "configured in systemd";
@@ -420,7 +397,6 @@ static const char *set_systemd_listener(process_rec *process, apr_port_t port,
 
     return NULL;
 }
-
 #endif /* HAVE_SYSTEMD */
 
 /* Returns non-zero if socket address SA matches hostname, port and
@@ -761,6 +737,9 @@ AP_DECLARE(int) ap_setup_listeners(server_rec *s)
     int num_listeners = 0;
     const char* proto;
     int found;
+#ifdef HAVE_SYSTEMD
+    APR_OPTIONAL_FN_TYPE(ap_systemd_listen_fds) *systemd_listen_fds;
+#endif
 
     for (ls = s; ls; ls = ls->next) {
         proto = ap_get_server_protocol(ls);
@@ -800,7 +779,10 @@ AP_DECLARE(int) ap_setup_listeners(server_rec *s)
                                 apr_pool_cleanup_null, s->process->pool);
         }
         else {
-            sd_listen_fds(1);
+            systemd_listen_fds = APR_RETRIEVE_OPTIONAL_FN(ap_systemd_listen_fds);
+            if (systemd_listen_fds != NULL) {
+                systemd_listen_fds(1);
+            }
         }        
     }
     else
@@ -1070,6 +1052,9 @@ AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
     apr_status_t rv;
     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
     apr_uint32_t flags = 0;
+#ifdef HAVE_SYSTEMD
+    APR_OPTIONAL_FN_TYPE(ap_systemd_listen_fds) *systemd_listen_fds;
+#endif
 
     if (err != NULL) {
         return err;
@@ -1080,7 +1065,12 @@ AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
     }
 #ifdef HAVE_SYSTEMD
     if (use_systemd == -1) {
-        use_systemd = sd_listen_fds(0) > 0;
+        systemd_listen_fds = APR_RETRIEVE_OPTIONAL_FN(ap_systemd_listen_fds);
+        if (systemd_listen_fds != NULL) {
+            use_systemd = systemd_listen_fds(0) > 0;
+        } else {
+            use_systemd = 0;
+        }
     }
 #endif