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