Blame SOURCES/httpd-2.4.35-systemd.patch

e71654
--- httpd-2.4.33/modules/arch/unix/config5.m4.systemd
e71654
+++ httpd-2.4.33/modules/arch/unix/config5.m4
e71654
@@ -18,6 +18,16 @@
e71654
   fi
e71654
 ])
e71654
 
e71654
+APACHE_MODULE(systemd, Systemd support, , , all, [
e71654
+  if test "${ac_cv_header_systemd_sd_daemon_h}" = "no" || test -z "${SYSTEMD_LIBS}"; then
e71654
+    AC_MSG_WARN([Your system does not support systemd.])
e71654
+    enable_systemd="no"
e71654
+  else
e71654
+    APR_ADDTO(MOD_SYSTEMD_LDADD, [$SYSTEMD_LIBS])
e71654
+    enable_systemd="yes"
e71654
+  fi
e71654
+])
e71654
+
e71654
 APR_ADDTO(INCLUDES, [-I\$(top_srcdir)/$modpath_current])
e71654
 
e71654
 APACHE_MODPATH_FINISH
e71654
--- httpd-2.4.33/modules/arch/unix/mod_systemd.c.systemd
e71654
+++ httpd-2.4.33/modules/arch/unix/mod_systemd.c
e71654
@@ -0,0 +1,223 @@
e71654
+/* Licensed to the Apache Software Foundation (ASF) under one or more
e71654
+ * contributor license agreements.  See the NOTICE file distributed with
e71654
+ * this work for additional information regarding copyright ownership.
e71654
+ * The ASF licenses this file to You under the Apache License, Version 2.0
e71654
+ * (the "License"); you may not use this file except in compliance with
e71654
+ * the License.  You may obtain a copy of the License at
e71654
+ *
e71654
+ *     http://www.apache.org/licenses/LICENSE-2.0
e71654
+ *
e71654
+ * Unless required by applicable law or agreed to in writing, software
e71654
+ * distributed under the License is distributed on an "AS IS" BASIS,
e71654
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
e71654
+ * See the License for the specific language governing permissions and
e71654
+ * limitations under the License.
e71654
+ * 
e71654
+ */
e71654
+
e71654
+#include <stdint.h>
e71654
+#include <ap_config.h>
e71654
+#include "ap_mpm.h"
e71654
+#include <http_core.h>
e71654
+#include <httpd.h>
e71654
+#include <http_log.h>
e71654
+#include <apr_version.h>
e71654
+#include <apr_pools.h>
e71654
+#include <apr_strings.h>
e71654
+#include "unixd.h"
e71654
+#include "scoreboard.h"
e71654
+#include "mpm_common.h"
e71654
+
e71654
+#include "systemd/sd-daemon.h"
e71654
+#include "systemd/sd-journal.h"
e71654
+
e71654
+#if APR_HAVE_UNISTD_H
e71654
+#include <unistd.h>
e71654
+#endif
e71654
+
e71654
+static int shutdown_timer = 0;
e71654
+static int shutdown_counter = 0;
e71654
+static unsigned long bytes_served;
e71654
+static pid_t mainpid;
e71654
+static char describe_listeners[50];
e71654
+
e71654
+static int systemd_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
e71654
+                              apr_pool_t *ptemp)
e71654
+{
e71654
+    sd_notify(0,
e71654
+              "RELOADING=1\n"
e71654
+              "STATUS=Reading configuration...\n");
e71654
+    ap_extended_status = 1;
e71654
+    return OK;
e71654
+}
e71654
+
e71654
+static char *dump_listener(ap_listen_rec *lr, apr_pool_t *p)
e71654
+{
e71654
+    apr_sockaddr_t *sa = lr->bind_addr;
e71654
+    char addr[128];
e71654
+
e71654
+    if (apr_sockaddr_is_wildcard(sa)) {
e71654
+        return apr_pstrcat(p, "port ", apr_itoa(p, sa->port), NULL);
e71654
+    }
e71654
+
e71654
+    apr_sockaddr_ip_getbuf(addr, sizeof addr, sa);
e71654
+
e71654
+    return apr_psprintf(p, "%s port %u", addr, sa->port);
e71654
+}
e71654
+
e71654
+static int systemd_post_config(apr_pool_t *pconf, apr_pool_t *plog,
e71654
+                               apr_pool_t *ptemp, server_rec *s)
e71654
+{
e71654
+    ap_listen_rec *lr;
e71654
+    apr_size_t plen = sizeof describe_listeners;
e71654
+    char *p = describe_listeners;
e71654
+
e71654
+    if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG)
e71654
+        return OK;
e71654
+
e71654
+    for (lr = ap_listeners; lr; lr = lr->next) {
e71654
+        char *s = dump_listener(lr, ptemp);
e71654
+
e71654
+        if (strlen(s) + 3 < plen) {
e71654
+            char *newp = apr_cpystrn(p, s, plen);
e71654
+            if (lr->next)
e71654
+                newp = apr_cpystrn(newp, ", ", 3);
e71654
+            plen -= newp - p;
e71654
+            p = newp;
e71654
+        }
e71654
+        else {
e71654
+            if (plen < 4) {
e71654
+                p = describe_listeners + sizeof describe_listeners - 4;
e71654
+                plen = 4;
e71654
+            }
e71654
+            apr_cpystrn(p, "...", plen);
e71654
+            break;
e71654
+        }
e71654
+    }
e71654
+
e71654
+    sd_journal_print(LOG_INFO, "Server configured, listening on: %s", describe_listeners);
e71654
+
e71654
+    return OK;
e71654
+}
e71654
+
e71654
+static int systemd_pre_mpm(apr_pool_t *p, ap_scoreboard_e sb_type)
e71654
+{
e71654
+    int rv;
e71654
+
e71654
+    mainpid = getpid();
e71654
+
e71654
+    rv = sd_notifyf(0, "READY=1\n"
e71654
+                    "STATUS=Started, listening on: %s\n"
e71654
+                    "MAINPID=%" APR_PID_T_FMT,
e71654
+                    describe_listeners, mainpid);
e71654
+    if (rv < 0) {
e71654
+        ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, APLOGNO(02395)
e71654
+                     "sd_notifyf returned an error %d", rv);
e71654
+    }
e71654
+
e71654
+    return OK;
e71654
+}
e71654
+
e71654
+static int systemd_monitor(apr_pool_t *p, server_rec *s)
e71654
+{
e71654
+    ap_sload_t sload;
e71654
+    apr_interval_time_t up_time;
e71654
+    char bps[5];
e71654
+    int rv;
e71654
+
e71654
+    if (!ap_extended_status) {
e71654
+        /* Nothing useful to report if ExtendedStatus disabled. */
e71654
+        return DECLINED;
e71654
+    }
e71654
+    
e71654
+    ap_get_sload(&sload);
e71654
+
e71654
+    if (sload.access_count == 0) {
e71654
+        rv = sd_notifyf(0, "READY=1\n"
e71654
+                        "STATUS=Running, listening on: %s\n",
e71654
+                        describe_listeners);
e71654
+    }
e71654
+    else {
e71654
+        /* up_time in seconds */
e71654
+        up_time = (apr_uint32_t) apr_time_sec(apr_time_now() -
e71654
+                                              ap_scoreboard_image->global->restart_time);
e71654
+
e71654
+        apr_strfsize((unsigned long)((float) (sload.bytes_served)
e71654
+                                     / (float) up_time), bps);
e71654
+
e71654
+        rv = sd_notifyf(0, "READY=1\n"
e71654
+                        "STATUS=Total requests: %lu; Idle/Busy workers %d/%d;"
e71654
+                        "Requests/sec: %.3g; Bytes served/sec: %sB/sec\n",
e71654
+                        sload.access_count, sload.idle, sload.busy,
e71654
+                        ((float) sload.access_count) / (float) up_time, bps);
e71654
+    }
e71654
+
e71654
+    if (rv < 0) {
e71654
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02396)
e71654
+                     "sd_notifyf returned an error %d", rv);
e71654
+    }
e71654
+
e71654
+    /* Shutdown httpd when nothing is sent for shutdown_timer seconds. */
e71654
+    if (sload.bytes_served == bytes_served) {
e71654
+        /* mpm_common.c: INTERVAL_OF_WRITABLE_PROBES is 10 */
e71654
+        shutdown_counter += 10;
e71654
+        if (shutdown_timer > 0 && shutdown_counter >= shutdown_timer) {
e71654
+            rv = sd_notifyf(0, "READY=1\n"
e71654
+                            "STATUS=Stopped as result of IdleShutdown "
e71654
+                            "timeout.");
e71654
+            if (rv < 0) {
e71654
+                ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02804)
e71654
+                            "sd_notifyf returned an error %d", rv);
e71654
+            }
e71654
+            kill(mainpid, AP_SIG_GRACEFUL);
e71654
+        }
e71654
+    }
e71654
+    else {
e71654
+        shutdown_counter = 0;
e71654
+    }
e71654
+
e71654
+    bytes_served = sload.bytes_served;
e71654
+
e71654
+    return DECLINED;
e71654
+}
e71654
+
e71654
+static void systemd_register_hooks(apr_pool_t *p)
e71654
+{
e71654
+    /* Enable ap_extended_status. */
e71654
+    ap_hook_pre_config(systemd_pre_config, NULL, NULL, APR_HOOK_LAST);
e71654
+    /* Grab the listener config. */
e71654
+    ap_hook_post_config(systemd_post_config, NULL, NULL, APR_HOOK_LAST);
e71654
+    /* We know the PID in this hook ... */
e71654
+    ap_hook_pre_mpm(systemd_pre_mpm, NULL, NULL, APR_HOOK_LAST);
e71654
+    /* Used to update httpd's status line using sd_notifyf */
e71654
+    ap_hook_monitor(systemd_monitor, NULL, NULL, APR_HOOK_MIDDLE);
e71654
+}
e71654
+
e71654
+static const char *set_shutdown_timer(cmd_parms *cmd, void *dummy,
e71654
+                                      const char *arg)
e71654
+{
e71654
+    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
e71654
+    if (err != NULL) {
e71654
+        return err;
e71654
+    }
e71654
+
e71654
+    shutdown_timer = atoi(arg);
e71654
+    return NULL;
e71654
+}
e71654
+
e71654
+static const command_rec systemd_cmds[] =
e71654
+{
e71654
+AP_INIT_TAKE1("IdleShutdown", set_shutdown_timer, NULL, RSRC_CONF,
e71654
+     "Number of seconds in idle-state after which httpd is shutdown"),
e71654
+    {NULL}
e71654
+};
e71654
+
e71654
+AP_DECLARE_MODULE(systemd) = {
e71654
+    STANDARD20_MODULE_STUFF,
e71654
+    NULL,
e71654
+    NULL,
e71654
+    NULL,
e71654
+    NULL,
e71654
+    systemd_cmds,
e71654
+    systemd_register_hooks,
e71654
+};