From ef8172c7f06f2fe137079061f2472bb03c142de8 Mon Sep 17 00:00:00 2001 From: Joe Orton Date: Mon, 3 Feb 2020 07:56:05 +0000 Subject: [PATCH] Merge r1393976, r1393997, r1484554, r1528032, r1528034, r1614821, r1618579, r1618588, r1802251, r1840554, r1840555, r1872761, r1872763 from trunk: * modules/arch/unix/mod_systemd.c: New module. Submitted by: Jan Kaluza * modules/arch/unix/mod_systemd.c (systemd_pre_mpm): Simplify code slightly; no functional change. Use AP_DECLARE_MODULE. * configure.in: Simplify/fix systemd detection: move later to fix autoconf warnings; define HAVE_SYSTEMD rather than using CPPFLAGS. * server/listen.c: Use HAVE_SYSTEMD for systemd #define. * modules/arch/unix/config5.m4: Update systemd headers check. Don't link support/* against systemd libs. * Makefile.in: Link httpd using $(HTTPD_LIBS). * configure.in: Add systemd to HTTPD_LIBS rather than LIBS. Add PCRE libs to HTTPD_LIBS. * configure.in, acinclude.m4: Move systemd check to APACHE_CHECK_SYSTEMD and use pkg-config where available. mod_systemd: Add IdleShutdown - number of seconds in idle-state after which httpd is shutdown. This is useful in a combination with socket activation. Add mod_systemd documentation. mod_systemd: Use AP_SIG_GRACEFUL instead of SIGWINCH. Fix overriding ExtendedStatus to "off" with mod_systemd loaded, and give more feedback to systemd during a reload. * modules/arch/unix/mod_systemd.c (systemd_pre_config): New function; tell systemd the service is reloading here. (systemd_pre_mpm): Don't set ap_extended_status here, do nothing if ExtendedStatus is off. (register_hooks): Register pre_config hook. * modules/arch/unix/mod_systemd.c (systemd_post_config): Fix systemd service getting stuck reloading if "ExtendedStatus off" is configured; regression in r1802251. PR: 62697 * modules/arch/unix/mod_systemd.c (systemd_pre_mpm, systemd_monitor): Ignore sd_notify{,f} failure cases as currently recommended by the systemd API docs. * modules/arch/unix/mod_systemd.c: Remove IdleShutdown feature which was buggy per sf's feedback in 2.4.x backport proposal, and would probably be more appropriate outside this module anyway. * modules/arch/unix/config5.m4: Don't override enable_systemd, fixing --enable-systemd=static per covener's suggestion in Also fix the APACHE_MODULE() usage; disable the module by default for the "all" modules selection. PR: 57632 Submitted by: jkaluza, jorton, nd Reviewed by: jorton, jim, elukey git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x@1873519 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES | 2 + acinclude.m4 | 24 +++++++ configure.in | 2 + modules/arch/unix/config5.m4 | 9 +++ modules/arch/unix/mod_systemd.c | 119 ++++++++++++++++++++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 modules/arch/unix/mod_systemd.c diff --git a/CHANGES b/CHANGES index e12ef389501..a39a4a2f2b7 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,8 @@ -*- coding: utf-8 -*- Changes with Apache 2.4.42 + *) mod_systemd: New module providing integration with systemd. [Jan Kaluza] + *) mod_lua: Add r:headers_in_table, r:headers_out_table, r:err_headers_out_table, r:notes_table, r:subprocess_env_table as read-only native table alternatives that can be iterated over. [Eric Covener] diff --git a/acinclude.m4 b/acinclude.m4 index ce1d637dd9a..6b38ca841dd 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -606,6 +606,30 @@ AC_DEFUN([APACHE_CHECK_OPENSSL],[ fi ]) +AC_DEFUN([APACHE_CHECK_SYSTEMD], [ +dnl Check for systemd support for listen.c's socket activation. +case $host in +*-linux-*) + if test -n "$PKGCONFIG" && $PKGCONFIG --exists libsystemd; then + SYSTEMD_LIBS=`$PKGCONFIG --libs libsystemd` + elif test -n "$PKGCONFIG" && $PKGCONFIG --exists libsystemd-daemon; then + SYSTEMD_LIBS=`$PKGCONFIG --libs libsystemd-daemon` + else + AC_CHECK_LIB(systemd-daemon, sd_notify, SYSTEMD_LIBS="-lsystemd-daemon") + fi + if test -n "$SYSTEMD_LIBS"; then + AC_CHECK_HEADERS(systemd/sd-daemon.h) + if test "${ac_cv_header_systemd_sd_daemon_h}" = "no" || test -z "${SYSTEMD_LIBS}"; then + AC_MSG_WARN([Your system does not support systemd.]) + else + APR_ADDTO(HTTPD_LIBS, [$SYSTEMD_LIBS]) + AC_DEFINE(HAVE_SYSTEMD, 1, [Define if systemd is supported]) + fi + fi + ;; +esac +]) + dnl dnl APACHE_EXPORT_ARGUMENTS dnl Export (via APACHE_SUBST) the various path-related variables that diff --git a/configure.in b/configure.in index 9feacebf98d..f8f9442bad0 100644 --- a/configure.in +++ b/configure.in @@ -504,6 +504,8 @@ if test "$ac_cv_struct_tm_gmtoff" = "yes"; then AC_DEFINE(HAVE_GMTOFF, 1, [Define if struct tm has a tm_gmtoff field]) fi +APACHE_CHECK_SYSTEMD + dnl ## Set up any appropriate OS-specific environment variables for apachectl case $host in diff --git a/modules/arch/unix/config5.m4 b/modules/arch/unix/config5.m4 index 77027a8584d..3d099f86b75 100644 --- a/modules/arch/unix/config5.m4 +++ b/modules/arch/unix/config5.m4 @@ -18,6 +18,15 @@ APACHE_MODULE(privileges, Per-virtualhost Unix UserIDs and enhanced security for fi ]) +APACHE_MODULE(systemd, Systemd support, , , no, [ + if test "${ac_cv_header_systemd_sd_daemon_h}" = "no" || test -z "${SYSTEMD_LIBS}"; then + AC_MSG_WARN([Your system does not support systemd.]) + enable_systemd="no" + else + APR_ADDTO(MOD_SYSTEMD_LDADD, [$SYSTEMD_LIBS]) + fi +]) + APR_ADDTO(INCLUDES, [-I\$(top_srcdir)/$modpath_current]) APACHE_MODPATH_FINISH diff --git a/modules/arch/unix/mod_systemd.c b/modules/arch/unix/mod_systemd.c new file mode 100644 index 00000000000..c3e7082df1f --- /dev/null +++ b/modules/arch/unix/mod_systemd.c @@ -0,0 +1,119 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include "ap_mpm.h" +#include +#include +#include +#include +#include +#include +#include "unixd.h" +#include "scoreboard.h" +#include "mpm_common.h" + +#include "systemd/sd-daemon.h" + +#if APR_HAVE_UNISTD_H +#include +#endif + +static int systemd_pre_config(apr_pool_t *pconf, apr_pool_t *plog, + apr_pool_t *ptemp) +{ + sd_notify(0, + "RELOADING=1\n" + "STATUS=Reading configuration...\n"); + ap_extended_status = 1; + return OK; +} + +/* Report the service is ready in post_config, which could be during + * startup or after a reload. The server could still hit a fatal + * startup error after this point during ap_run_mpm(), so this is + * perhaps too early, but by post_config listen() has been called on + * the TCP ports so new connections will not be rejected. There will + * always be a possible async failure event simultaneous to the + * service reporting "ready", so this should be good enough. */ +static int systemd_post_config(apr_pool_t *p, apr_pool_t *plog, + apr_pool_t *ptemp, server_rec *main_server) +{ + sd_notify(0, "READY=1\n" + "STATUS=Configuration loaded.\n"); + return OK; +} + +static int systemd_pre_mpm(apr_pool_t *p, ap_scoreboard_e sb_type) +{ + sd_notifyf(0, "READY=1\n" + "STATUS=Processing requests...\n" + "MAINPID=%" APR_PID_T_FMT, getpid()); + + return OK; +} + +static int systemd_monitor(apr_pool_t *p, server_rec *s) +{ + ap_sload_t sload; + apr_interval_time_t up_time; + char bps[5]; + + if (!ap_extended_status) { + /* Nothing useful to report with ExtendedStatus disabled. */ + return DECLINED; + } + + ap_get_sload(&sload); + /* up_time in seconds */ + up_time = (apr_uint32_t) apr_time_sec(apr_time_now() - + ap_scoreboard_image->global->restart_time); + + apr_strfsize((unsigned long)((float) (sload.bytes_served) + / (float) up_time), bps); + + sd_notifyf(0, "READY=1\n" + "STATUS=Total requests: %lu; Idle/Busy workers %d/%d;" + "Requests/sec: %.3g; Bytes served/sec: %sB/sec\n", + sload.access_count, sload.idle, sload.busy, + ((float) sload.access_count) / (float) up_time, bps); + + return DECLINED; +} + +static void systemd_register_hooks(apr_pool_t *p) +{ + /* Enable ap_extended_status. */ + ap_hook_pre_config(systemd_pre_config, NULL, NULL, APR_HOOK_LAST); + /* Signal service is ready. */ + ap_hook_post_config(systemd_post_config, NULL, NULL, APR_HOOK_REALLY_LAST); + /* We know the PID in this hook ... */ + ap_hook_pre_mpm(systemd_pre_mpm, NULL, NULL, APR_HOOK_LAST); + /* Used to update httpd's status line using sd_notifyf */ + ap_hook_monitor(systemd_monitor, NULL, NULL, APR_HOOK_MIDDLE); +} + +AP_DECLARE_MODULE(systemd) = { + STANDARD20_MODULE_STUFF, + NULL, + NULL, + NULL, + NULL, + NULL, + systemd_register_hooks, +}; -- 2.47.3