85eacfc96a04547ef25aabbc06440039715084c2jorton/* Licensed to the Apache Software Foundation (ASF) under one or more
85eacfc96a04547ef25aabbc06440039715084c2jorton * contributor license agreements. See the NOTICE file distributed with
85eacfc96a04547ef25aabbc06440039715084c2jorton * this work for additional information regarding copyright ownership.
85eacfc96a04547ef25aabbc06440039715084c2jorton * The ASF licenses this file to You under the Apache License, Version 2.0
85eacfc96a04547ef25aabbc06440039715084c2jorton * (the "License"); you may not use this file except in compliance with
85eacfc96a04547ef25aabbc06440039715084c2jorton * the License. You may obtain a copy of the License at
85eacfc96a04547ef25aabbc06440039715084c2jorton *
85eacfc96a04547ef25aabbc06440039715084c2jorton * http://www.apache.org/licenses/LICENSE-2.0
85eacfc96a04547ef25aabbc06440039715084c2jorton *
85eacfc96a04547ef25aabbc06440039715084c2jorton * Unless required by applicable law or agreed to in writing, software
85eacfc96a04547ef25aabbc06440039715084c2jorton * distributed under the License is distributed on an "AS IS" BASIS,
85eacfc96a04547ef25aabbc06440039715084c2jorton * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
85eacfc96a04547ef25aabbc06440039715084c2jorton * See the License for the specific language governing permissions and
85eacfc96a04547ef25aabbc06440039715084c2jorton * limitations under the License.
85eacfc96a04547ef25aabbc06440039715084c2jorton *
85eacfc96a04547ef25aabbc06440039715084c2jorton */
85eacfc96a04547ef25aabbc06440039715084c2jorton
85eacfc96a04547ef25aabbc06440039715084c2jorton#include <stdint.h>
85eacfc96a04547ef25aabbc06440039715084c2jorton#include <ap_config.h>
85eacfc96a04547ef25aabbc06440039715084c2jorton#include "ap_mpm.h"
85eacfc96a04547ef25aabbc06440039715084c2jorton#include <http_core.h>
85eacfc96a04547ef25aabbc06440039715084c2jorton#include <httpd.h>
85eacfc96a04547ef25aabbc06440039715084c2jorton#include <http_log.h>
85eacfc96a04547ef25aabbc06440039715084c2jorton#include <apr_version.h>
85eacfc96a04547ef25aabbc06440039715084c2jorton#include <apr_pools.h>
85eacfc96a04547ef25aabbc06440039715084c2jorton#include <apr_strings.h>
85eacfc96a04547ef25aabbc06440039715084c2jorton#include "unixd.h"
85eacfc96a04547ef25aabbc06440039715084c2jorton#include "scoreboard.h"
85eacfc96a04547ef25aabbc06440039715084c2jorton#include "mpm_common.h"
85eacfc96a04547ef25aabbc06440039715084c2jorton
85eacfc96a04547ef25aabbc06440039715084c2jorton#include "systemd/sd-daemon.h"
85eacfc96a04547ef25aabbc06440039715084c2jorton
85eacfc96a04547ef25aabbc06440039715084c2jorton#if APR_HAVE_UNISTD_H
85eacfc96a04547ef25aabbc06440039715084c2jorton#include <unistd.h>
85eacfc96a04547ef25aabbc06440039715084c2jorton#endif
85eacfc96a04547ef25aabbc06440039715084c2jorton
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluzastatic int shutdown_timer = 0;
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluzastatic int shutdown_counter = 0;
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluzastatic unsigned long bytes_served;
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluzastatic pid_t mainpid;
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza
85eacfc96a04547ef25aabbc06440039715084c2jortonstatic int systemd_pre_mpm(apr_pool_t *p, ap_scoreboard_e sb_type)
85eacfc96a04547ef25aabbc06440039715084c2jorton{
85eacfc96a04547ef25aabbc06440039715084c2jorton int rv;
85eacfc96a04547ef25aabbc06440039715084c2jorton
85eacfc96a04547ef25aabbc06440039715084c2jorton ap_extended_status = 1;
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza mainpid = getpid();
85eacfc96a04547ef25aabbc06440039715084c2jorton
85eacfc96a04547ef25aabbc06440039715084c2jorton rv = sd_notifyf(0, "READY=1\n"
85eacfc96a04547ef25aabbc06440039715084c2jorton "STATUS=Processing requests...\n"
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza "MAINPID=%" APR_PID_T_FMT, mainpid);
85eacfc96a04547ef25aabbc06440039715084c2jorton if (rv < 0) {
85eacfc96a04547ef25aabbc06440039715084c2jorton ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, APLOGNO(02395)
85eacfc96a04547ef25aabbc06440039715084c2jorton "sd_notifyf returned an error %d", rv);
85eacfc96a04547ef25aabbc06440039715084c2jorton }
85eacfc96a04547ef25aabbc06440039715084c2jorton
85eacfc96a04547ef25aabbc06440039715084c2jorton return OK;
85eacfc96a04547ef25aabbc06440039715084c2jorton}
85eacfc96a04547ef25aabbc06440039715084c2jorton
85eacfc96a04547ef25aabbc06440039715084c2jortonstatic int systemd_monitor(apr_pool_t *p, server_rec *s)
85eacfc96a04547ef25aabbc06440039715084c2jorton{
85eacfc96a04547ef25aabbc06440039715084c2jorton ap_sload_t sload;
85eacfc96a04547ef25aabbc06440039715084c2jorton apr_interval_time_t up_time;
85eacfc96a04547ef25aabbc06440039715084c2jorton char bps[5];
85eacfc96a04547ef25aabbc06440039715084c2jorton int rv;
85eacfc96a04547ef25aabbc06440039715084c2jorton
85eacfc96a04547ef25aabbc06440039715084c2jorton ap_get_sload(&sload);
85eacfc96a04547ef25aabbc06440039715084c2jorton /* up_time in seconds */
85eacfc96a04547ef25aabbc06440039715084c2jorton up_time = (apr_uint32_t) apr_time_sec(apr_time_now() -
85eacfc96a04547ef25aabbc06440039715084c2jorton ap_scoreboard_image->global->restart_time);
85eacfc96a04547ef25aabbc06440039715084c2jorton
85eacfc96a04547ef25aabbc06440039715084c2jorton apr_strfsize((unsigned long)((float) (sload.bytes_served)
85eacfc96a04547ef25aabbc06440039715084c2jorton / (float) up_time), bps);
85eacfc96a04547ef25aabbc06440039715084c2jorton
85eacfc96a04547ef25aabbc06440039715084c2jorton rv = sd_notifyf(0, "READY=1\n"
85eacfc96a04547ef25aabbc06440039715084c2jorton "STATUS=Total requests: %lu; Idle/Busy workers %d/%d;"
85eacfc96a04547ef25aabbc06440039715084c2jorton "Requests/sec: %.3g; Bytes served/sec: %sB/sec\n",
85eacfc96a04547ef25aabbc06440039715084c2jorton sload.access_count, sload.idle, sload.busy,
85eacfc96a04547ef25aabbc06440039715084c2jorton ((float) sload.access_count) / (float) up_time, bps);
85eacfc96a04547ef25aabbc06440039715084c2jorton
85eacfc96a04547ef25aabbc06440039715084c2jorton if (rv < 0) {
85eacfc96a04547ef25aabbc06440039715084c2jorton ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02396)
85eacfc96a04547ef25aabbc06440039715084c2jorton "sd_notifyf returned an error %d", rv);
85eacfc96a04547ef25aabbc06440039715084c2jorton }
85eacfc96a04547ef25aabbc06440039715084c2jorton
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza /* Shutdown httpd when nothing is sent for shutdown_timer seconds. */
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza if (sload.bytes_served == bytes_served) {
58e5bc883b8fcd6093c8569d10ea551ed9894e8ejkaluza /* mpm_common.c: INTERVAL_OF_WRITABLE_PROBES is 10 */
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza shutdown_counter += 10;
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza if (shutdown_timer > 0 && shutdown_counter >= shutdown_timer) {
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza rv = sd_notifyf(0, "READY=1\n"
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza "STATUS=Stopped as result of IdleShutdown "
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza "timeout.");
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza if (rv < 0) {
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02804)
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza "sd_notifyf returned an error %d", rv);
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza }
58e5bc883b8fcd6093c8569d10ea551ed9894e8ejkaluza kill(mainpid, AP_SIG_GRACEFUL);
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza }
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza }
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza else {
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza shutdown_counter = 0;
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza }
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza bytes_served = sload.bytes_served;
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza
85eacfc96a04547ef25aabbc06440039715084c2jorton return DECLINED;
85eacfc96a04547ef25aabbc06440039715084c2jorton}
85eacfc96a04547ef25aabbc06440039715084c2jorton
85eacfc96a04547ef25aabbc06440039715084c2jortonstatic void systemd_register_hooks(apr_pool_t *p)
85eacfc96a04547ef25aabbc06440039715084c2jorton{
85eacfc96a04547ef25aabbc06440039715084c2jorton /* We know the PID in this hook ... */
85eacfc96a04547ef25aabbc06440039715084c2jorton ap_hook_pre_mpm(systemd_pre_mpm, NULL, NULL, APR_HOOK_LAST);
85eacfc96a04547ef25aabbc06440039715084c2jorton /* Used to update httpd's status line using sd_notifyf */
85eacfc96a04547ef25aabbc06440039715084c2jorton ap_hook_monitor(systemd_monitor, NULL, NULL, APR_HOOK_MIDDLE);
85eacfc96a04547ef25aabbc06440039715084c2jorton}
85eacfc96a04547ef25aabbc06440039715084c2jorton
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluzastatic const char *set_shutdown_timer(cmd_parms *cmd, void *dummy,
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza const char *arg)
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza{
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza if (err != NULL) {
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza return err;
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza }
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza shutdown_timer = atoi(arg);
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza return NULL;
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza}
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluzastatic const command_rec systemd_cmds[] =
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza{
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluzaAP_INIT_TAKE1("IdleShutdown", set_shutdown_timer, NULL, RSRC_CONF,
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza "Number of seconds in idle-state after which httpd is shutdown"),
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza {NULL}
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza};
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza
cfa0f146c1940c7b75f8fac3cbdc513263a6bf78jortonAP_DECLARE_MODULE(systemd) = {
85eacfc96a04547ef25aabbc06440039715084c2jorton STANDARD20_MODULE_STUFF,
85eacfc96a04547ef25aabbc06440039715084c2jorton NULL,
85eacfc96a04547ef25aabbc06440039715084c2jorton NULL,
85eacfc96a04547ef25aabbc06440039715084c2jorton NULL,
85eacfc96a04547ef25aabbc06440039715084c2jorton NULL,
da5dc6a3cb274b5e747f50713086a212c5afc738jkaluza systemd_cmds,
85eacfc96a04547ef25aabbc06440039715084c2jorton systemd_register_hooks,
85eacfc96a04547ef25aabbc06440039715084c2jorton};