--- /dev/null
+<?xml version="1.0"?>
+<!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
+<?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
+<!-- $LastChangedRevision: 1611791 $ -->
+
+<!--
+ 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.
+-->
+
+<modulesynopsis metafile="mod_systemd.xml.meta">
+
+<name>mod_systemd</name>
+<description>Provides better support for systemd integration</description>
+<status>Extension</status>
+<sourcefile>mod_systemd.c</sourcefile>
+<identifier>systemd_module</identifier>
+
+<summary>
+ <p>This module provides support for systemd integration. It allows
+ starting httpd as a service with systemd <code>Type=notify</code>
+ (see systemd.service(5) manual page for more information). It also
+ provides statistics in <code>systemctl status</code> output and adds
+ various directives useful for systemd integration.
+ </p>
+</summary>
+
+<directivesynopsis>
+<name>IdleShutdown</name>
+<description>Enable shutting down the httpd when it is idle for some time.</description>
+<syntax>IdleShutdown seconds</syntax>
+<default>IdleShutdown 0</default>
+<contextlist><context>server config</context></contextlist>
+
+<usage>
+ <p>The <directive>IdleShutdown</directive> directive enables shutting
+ down the httpd when it is idle for some time. The idleness is based on
+ bytes served, so if there are no bytes sent for some time defined by this
+ directive, httpd will shutdown. By default, IdleShutdown is set to 0
+ meaning this feature is disabled.
+ </p>
+
+ <p>This feature is useful in a combination with systemd socket activation
+ (see systemd.socket(5) manual page). When httpd is started by systemd on
+ some request, using this directive you can stop the httpd automatically
+ when all the requests are served.
+ </p>
+
+ <note type="warning"><title>Implementation warning</title><p>
+ Because of implementation details, idleness is checked only every 10
+ seconds. That means that if you specify <code>IdleShutdown 14</code>,
+ httpd will stop itself after 20 seconds of idleness.
+ </p></note>
+</usage>
+</directivesynopsis>
+
+
+</modulesynopsis>
#include <unistd.h>
#endif
+static int shutdown_timer = 0;
+static int shutdown_counter = 0;
+static unsigned long bytes_served;
+static pid_t mainpid;
+
static int systemd_pre_mpm(apr_pool_t *p, ap_scoreboard_e sb_type)
{
int rv;
ap_extended_status = 1;
+ mainpid = getpid();
rv = sd_notifyf(0, "READY=1\n"
"STATUS=Processing requests...\n"
- "MAINPID=%" APR_PID_T_FMT, getpid());
+ "MAINPID=%" APR_PID_T_FMT, mainpid);
if (rv < 0) {
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, APLOGNO(02395)
"sd_notifyf returned an error %d", rv);
"sd_notifyf returned an error %d", rv);
}
+ /* Shutdown httpd when nothing is sent for shutdown_timer seconds. */
+ if (sload.bytes_served == bytes_served) {
+ shutdown_counter += 10;
+ if (shutdown_timer > 0 && shutdown_counter >= shutdown_timer) {
+ rv = sd_notifyf(0, "READY=1\n"
+ "STATUS=Stopped as result of IdleShutdown "
+ "timeout.");
+ if (rv < 0) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02804)
+ "sd_notifyf returned an error %d", rv);
+ }
+ kill(mainpid, SIGWINCH);
+ }
+ }
+ else {
+ shutdown_counter = 0;
+ }
+
+ bytes_served = sload.bytes_served;
+
return DECLINED;
}
ap_hook_monitor(systemd_monitor, NULL, NULL, APR_HOOK_MIDDLE);
}
+static const char *set_shutdown_timer(cmd_parms *cmd, void *dummy,
+ const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ shutdown_timer = atoi(arg);
+ return NULL;
+}
+
+static const command_rec systemd_cmds[] =
+{
+AP_INIT_TAKE1("IdleShutdown", set_shutdown_timer, NULL, RSRC_CONF,
+ "Number of seconds in idle-state after which httpd is shutdown"),
+ {NULL}
+};
+
AP_DECLARE_MODULE(systemd) = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
- NULL,
+ systemd_cmds,
systemd_register_hooks,
};