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