Blame SOURCES/httpd-2.4.10-mod_systemd.patch

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