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