8335b1
--- httpd-2.4.3/modules/arch/unix/config5.m4.systemd
8335b1
+++ httpd-2.4.3/modules/arch/unix/config5.m4
8335b1
@@ -18,6 +18,19 @@ APACHE_MODULE(privileges, Per-virtualhos
8335b1
   fi
8335b1
 ])
8335b1
 
8335b1
+
8335b1
+APACHE_MODULE(systemd, Systemd support, , , $unixd_mods_enabled, [
8335b1
+  AC_CHECK_LIB(systemd-daemon, sd_notify, SYSTEMD_LIBS="-lsystemd-daemon")
8335b1
+  AC_CHECK_HEADERS(systemd/sd-daemon.h, [ap_HAVE_SD_DAEMON_H="yes"], [ap_HAVE_SD_DAEMON_H="no"])
8335b1
+  if test $ap_HAVE_SD_DAEMON_H = "no" || test -z "${SYSTEMD_LIBS}"; then
8335b1
+    AC_MSG_WARN([Your system does not support systemd.])
8335b1
+    enable_systemd="no"
8335b1
+  else
8335b1
+    APR_ADDTO(MOD_SYSTEMD_LDADD, [$SYSTEMD_LIBS])
8335b1
+    enable_systemd="yes"
8335b1
+  fi
8335b1
+])
8335b1
+
8335b1
 APR_ADDTO(INCLUDES, [-I\$(top_srcdir)/$modpath_current])
8335b1
 
8335b1
 APACHE_MODPATH_FINISH
8335b1
--- httpd-2.4.3/modules/arch/unix/mod_systemd.c.systemd
8335b1
+++ httpd-2.4.3/modules/arch/unix/mod_systemd.c
8335b1
@@ -0,0 +1,138 @@
8335b1
+/* Licensed to the Apache Software Foundation (ASF) under one or more
8335b1
+ * contributor license agreements.  See the NOTICE file distributed with
8335b1
+ * this work for additional information regarding copyright ownership.
8335b1
+ * The ASF licenses this file to You under the Apache License, Version 2.0
8335b1
+ * (the "License"); you may not use this file except in compliance with
8335b1
+ * the License.  You may obtain a copy of the License at
8335b1
+ *
8335b1
+ *     http://www.apache.org/licenses/LICENSE-2.0
8335b1
+ *
8335b1
+ * Unless required by applicable law or agreed to in writing, software
8335b1
+ * distributed under the License is distributed on an "AS IS" BASIS,
8335b1
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8335b1
+ * See the License for the specific language governing permissions and
8335b1
+ * limitations under the License.
8335b1
+ * 
8335b1
+ */
8335b1
+
8335b1
+#include <stdint.h>
8335b1
+#include <ap_config.h>
8335b1
+#include "ap_mpm.h"
8335b1
+#include <http_core.h>
8335b1
+#include <http_log.h>
8335b1
+#include <apr_version.h>
8335b1
+#include <apr_pools.h>
8335b1
+#include <apr_strings.h>
8335b1
+#include "unixd.h"
8335b1
+#include "scoreboard.h"
8335b1
+#include "mpm_common.h"
8335b1
+
8335b1
+#include "systemd/sd-daemon.h"
8335b1
+
8335b1
+#if APR_HAVE_UNISTD_H
8335b1
+#include <unistd.h>
8335b1
+#endif
8335b1
+
8335b1
+#define KBYTE 1024
8335b1
+
8335b1
+static pid_t pid;	/* PID of the main httpd instance */
8335b1
+static int server_limit, thread_limit, threads_per_child, max_servers;
8335b1
+static time_t last_update_time;
8335b1
+static unsigned long last_update_access;
8335b1
+static unsigned long last_update_kbytes;
8335b1
+
8335b1
+static int systemd_pre_mpm(apr_pool_t *p, ap_scoreboard_e sb_type)
8335b1
+{
8335b1
+    int rv;
8335b1
+    last_update_time = time(0);
8335b1
+
8335b1
+    ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
8335b1
+    ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
8335b1
+    ap_mpm_query(AP_MPMQ_MAX_THREADS, &threads_per_child);
8335b1
+    /* work around buggy MPMs */
8335b1
+    if (threads_per_child == 0)
8335b1
+        threads_per_child = 1;
8335b1
+    ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_servers);
8335b1
+
8335b1
+    pid = getpid();
8335b1
+    
8335b1
+    rv = sd_notifyf(0, "READY=1\n"
8335b1
+                    "STATUS=Processing requests...\n"
8335b1
+                    "MAINPID=%lu",
8335b1
+                    (unsigned long) pid);
8335b1
+    if (rv < 0) {
8335b1
+        ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, 
8335b1
+                     "sd_notifyf returned an error %d", rv);
8335b1
+    }
8335b1
+
8335b1
+    return OK;
8335b1
+}
8335b1
+
8335b1
+static int systemd_monitor(apr_pool_t *p, server_rec *s)
8335b1
+{
8335b1
+    int i, j, res, rv;
8335b1
+    process_score *ps_record;
8335b1
+    worker_score *ws_record;
8335b1
+    unsigned long access = 0;
8335b1
+    unsigned long bytes = 0;
8335b1
+    unsigned long kbytes = 0;
8335b1
+    char bps[5];
8335b1
+    time_t now = time(0);
8335b1
+    time_t elapsed = now - last_update_time;
8335b1
+
8335b1
+    for (i = 0; i < server_limit; ++i) {
8335b1
+        ps_record = ap_get_scoreboard_process(i);
8335b1
+        for (j = 0; j < thread_limit; ++j) {
8335b1
+            ws_record = ap_get_scoreboard_worker_from_indexes(i, j);
8335b1
+            if (ap_extended_status && !ps_record->quiescing && ps_record->pid) {
8335b1
+                res = ws_record->status;
8335b1
+                if (ws_record->access_count != 0 || 
8335b1
+                    (res != SERVER_READY && res != SERVER_DEAD)) {
8335b1
+                    access += ws_record->access_count;
8335b1
+                    bytes += ws_record->bytes_served;
8335b1
+                    if (bytes >= KBYTE) {
8335b1
+                        kbytes += (bytes >> 10);
8335b1
+                        bytes = bytes & 0x3ff;
8335b1
+                    }
8335b1
+                }
8335b1
+            }
8335b1
+        }
8335b1
+    }
8335b1
+
8335b1
+    apr_strfsize((unsigned long)(KBYTE *(float) (kbytes - last_update_kbytes)
8335b1
+                                 / (float) elapsed), bps);
8335b1
+
8335b1
+    rv = sd_notifyf(0, "READY=1\n"
8335b1
+                    "STATUS=Total requests: %lu; Current requests/sec: %.3g; "
8335b1
+                    "Current traffic: %sB/sec\n", access,
8335b1
+                    ((float)access - last_update_access) / (float) elapsed, bps);
8335b1
+    if (rv < 0) {
8335b1
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00000)
8335b1
+                     "sd_notifyf returned an error %d", rv);
8335b1
+    }
8335b1
+
8335b1
+    last_update_access = access;
8335b1
+    last_update_kbytes = kbytes;
8335b1
+    last_update_time = now;
8335b1
+
8335b1
+    return DECLINED;
8335b1
+}
8335b1
+
8335b1
+static void systemd_register_hooks(apr_pool_t *p)
8335b1
+{
8335b1
+    /* We know the PID in this hook ... */
8335b1
+    ap_hook_pre_mpm(systemd_pre_mpm, NULL, NULL, APR_HOOK_LAST);
8335b1
+    /* Used to update httpd's status line using sd_notifyf */
8335b1
+    ap_hook_monitor(systemd_monitor, NULL, NULL, APR_HOOK_MIDDLE);
8335b1
+}
8335b1
+
8335b1
+module AP_MODULE_DECLARE_DATA systemd_module =
8335b1
+{
8335b1
+    STANDARD20_MODULE_STUFF,
8335b1
+    NULL,
8335b1
+    NULL,
8335b1
+    NULL,
8335b1
+    NULL,
8335b1
+    NULL,
8335b1
+    systemd_register_hooks,
8335b1
+};