Blame SOURCES/httpd-2.4.3-mod_systemd.patch

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