altarch-user / rpms / httpd

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