Blame modules/arch/unix/mod_systemd.c

Packit 8ce744
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 8ce744
 * contributor license agreements.  See the NOTICE file distributed with
Packit 8ce744
 * this work for additional information regarding copyright ownership.
Packit 8ce744
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 8ce744
 * (the "License"); you may not use this file except in compliance with
Packit 8ce744
 * the License.  You may obtain a copy of the License at
Packit 8ce744
 *
Packit 8ce744
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 8ce744
 *
Packit 8ce744
 * Unless required by applicable law or agreed to in writing, software
Packit 8ce744
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 8ce744
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 8ce744
 * See the License for the specific language governing permissions and
Packit 8ce744
 * limitations under the License.
Packit 8ce744
 * 
Packit 8ce744
 */
Packit 8ce744
Packit 8ce744
#include <stdint.h>
Packit 8ce744
#include <ap_config.h>
Packit 8ce744
#include "ap_mpm.h"
Packit 8ce744
#include <http_core.h>
Packit 8ce744
#include <httpd.h>
Packit 8ce744
#include <http_log.h>
Packit 8ce744
#include <apr_version.h>
Packit 8ce744
#include <apr_pools.h>
Packit 8ce744
#include <apr_strings.h>
Packit 8ce744
#include "unixd.h"
Packit 8ce744
#include "scoreboard.h"
Packit 8ce744
#include "mpm_common.h"
Packit 8ce744
Packit 8ce744
#include "systemd/sd-daemon.h"
Packit 8ce744
#include "systemd/sd-journal.h"
Packit 8ce744
Packit 8ce744
#if APR_HAVE_UNISTD_H
Packit 8ce744
#include <unistd.h>
Packit 8ce744
#endif
Packit 8ce744
Packit 8ce744
static int shutdown_timer = 0;
Packit 8ce744
static int shutdown_counter = 0;
Packit 8ce744
static unsigned long bytes_served;
Packit 8ce744
static pid_t mainpid;
Packit 8ce744
static char describe_listeners[50];
Packit 8ce744
Packit 8ce744
static int systemd_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
Packit 8ce744
                              apr_pool_t *ptemp)
Packit 8ce744
{
Packit 8ce744
    sd_notify(0,
Packit 8ce744
              "RELOADING=1\n"
Packit 8ce744
              "STATUS=Reading configuration...\n");
Packit 8ce744
    ap_extended_status = 1;
Packit 8ce744
    return OK;
Packit 8ce744
}
Packit 8ce744
Packit 8ce744
static char *dump_listener(ap_listen_rec *lr, apr_pool_t *p)
Packit 8ce744
{
Packit 8ce744
    apr_sockaddr_t *sa = lr->bind_addr;
Packit 8ce744
    char addr[128];
Packit 8ce744
Packit 8ce744
    if (apr_sockaddr_is_wildcard(sa)) {
Packit 8ce744
        return apr_pstrcat(p, "port ", apr_itoa(p, sa->port), NULL);
Packit 8ce744
    }
Packit 8ce744
Packit 8ce744
    apr_sockaddr_ip_getbuf(addr, sizeof addr, sa);
Packit 8ce744
Packit 8ce744
    return apr_psprintf(p, "%s port %u", addr, sa->port);
Packit 8ce744
}
Packit 8ce744
Packit 8ce744
static int systemd_post_config(apr_pool_t *pconf, apr_pool_t *plog,
Packit 8ce744
                               apr_pool_t *ptemp, server_rec *s)
Packit 8ce744
{
Packit 8ce744
    ap_listen_rec *lr;
Packit 8ce744
    apr_size_t plen = sizeof describe_listeners;
Packit 8ce744
    char *p = describe_listeners;
Packit 8ce744
Packit 8ce744
    if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG)
Packit 8ce744
        return OK;
Packit 8ce744
Packit 8ce744
    for (lr = ap_listeners; lr; lr = lr->next) {
Packit 8ce744
        char *s = dump_listener(lr, ptemp);
Packit 8ce744
Packit 8ce744
        if (strlen(s) + 3 < plen) {
Packit 8ce744
            char *newp = apr_cpystrn(p, s, plen);
Packit 8ce744
            if (lr->next)
Packit 8ce744
                newp = apr_cpystrn(newp, ", ", 3);
Packit 8ce744
            plen -= newp - p;
Packit 8ce744
            p = newp;
Packit 8ce744
        }
Packit 8ce744
        else {
Packit 8ce744
            if (plen < 4) {
Packit 8ce744
                p = describe_listeners + sizeof describe_listeners - 4;
Packit 8ce744
                plen = 4;
Packit 8ce744
            }
Packit 8ce744
            apr_cpystrn(p, "...", plen);
Packit 8ce744
            break;
Packit 8ce744
        }
Packit 8ce744
    }
Packit 8ce744
Packit 8ce744
    sd_journal_print(LOG_INFO, "Server configured, listening on: %s", describe_listeners);
Packit 8ce744
Packit 8ce744
    return OK;
Packit 8ce744
}
Packit 8ce744
Packit 7e67f6
/* Report the service is ready in post_config, which could be during
Packit 7e67f6
 * startup or after a reload.  The server could still hit a fatal
Packit 7e67f6
 * startup error after this point during ap_run_mpm(), so this is
Packit 7e67f6
 * perhaps too early, but by post_config listen() has been called on
Packit 7e67f6
 * the TCP ports so new connections will not be rejected.  There will
Packit 7e67f6
 * always be a possible async failure event simultaneous to the
Packit 7e67f6
 * service reporting "ready", so this should be good enough. */
Packit 7e67f6
static int systemd_post_config_last(apr_pool_t *p, apr_pool_t *plog,
Packit 7e67f6
                               apr_pool_t *ptemp, server_rec *main_server)
Packit 7e67f6
{
Packit 7e67f6
    sd_notify(0, "READY=1\n"
Packit 7e67f6
              "STATUS=Configuration loaded.\n");
Packit 7e67f6
    return OK;
Packit 7e67f6
}
Packit 7e67f6
Packit 8ce744
static int systemd_pre_mpm(apr_pool_t *p, ap_scoreboard_e sb_type)
Packit 8ce744
{
Packit 8ce744
    int rv;
Packit 8ce744
Packit 8ce744
    mainpid = getpid();
Packit 8ce744
Packit 8ce744
    rv = sd_notifyf(0, "READY=1\n"
Packit 8ce744
                    "STATUS=Started, listening on: %s\n"
Packit 8ce744
                    "MAINPID=%" APR_PID_T_FMT,
Packit 8ce744
                    describe_listeners, mainpid);
Packit 8ce744
    if (rv < 0) {
Packit 8ce744
        ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, APLOGNO(02395)
Packit 8ce744
                     "sd_notifyf returned an error %d", rv);
Packit 8ce744
    }
Packit 8ce744
Packit 8ce744
    return OK;
Packit 8ce744
}
Packit 8ce744
Packit 8ce744
static int systemd_monitor(apr_pool_t *p, server_rec *s)
Packit 8ce744
{
Packit 8ce744
    ap_sload_t sload;
Packit 8ce744
    apr_interval_time_t up_time;
Packit 8ce744
    char bps[5];
Packit 8ce744
    int rv;
Packit 8ce744
Packit 8ce744
    if (!ap_extended_status) {
Packit 8ce744
        /* Nothing useful to report if ExtendedStatus disabled. */
Packit 8ce744
        return DECLINED;
Packit 8ce744
    }
Packit 8ce744
    
Packit 8ce744
    ap_get_sload(&sload);
Packit 8ce744
Packit 8ce744
    if (sload.access_count == 0) {
Packit 8ce744
        rv = sd_notifyf(0, "READY=1\n"
Packit 8ce744
                        "STATUS=Running, listening on: %s\n",
Packit 8ce744
                        describe_listeners);
Packit 8ce744
    }
Packit 8ce744
    else {
Packit 8ce744
        /* up_time in seconds */
Packit 8ce744
        up_time = (apr_uint32_t) apr_time_sec(apr_time_now() -
Packit 8ce744
                                              ap_scoreboard_image->global->restart_time);
Packit 8ce744
Packit 8ce744
        apr_strfsize((unsigned long)((float) (sload.bytes_served)
Packit 8ce744
                                     / (float) up_time), bps);
Packit 8ce744
Packit 8ce744
        rv = sd_notifyf(0, "READY=1\n"
Packit 8ce744
                        "STATUS=Total requests: %lu; Idle/Busy workers %d/%d;"
Packit 8ce744
                        "Requests/sec: %.3g; Bytes served/sec: %sB/sec\n",
Packit 8ce744
                        sload.access_count, sload.idle, sload.busy,
Packit 8ce744
                        ((float) sload.access_count) / (float) up_time, bps);
Packit 8ce744
    }
Packit 8ce744
Packit 8ce744
    if (rv < 0) {
Packit 8ce744
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02396)
Packit 8ce744
                     "sd_notifyf returned an error %d", rv);
Packit 8ce744
    }
Packit 8ce744
Packit 8ce744
    /* Shutdown httpd when nothing is sent for shutdown_timer seconds. */
Packit 8ce744
    if (sload.bytes_served == bytes_served) {
Packit 8ce744
        /* mpm_common.c: INTERVAL_OF_WRITABLE_PROBES is 10 */
Packit 8ce744
        shutdown_counter += 10;
Packit 8ce744
        if (shutdown_timer > 0 && shutdown_counter >= shutdown_timer) {
Packit 8ce744
            rv = sd_notifyf(0, "READY=1\n"
Packit 8ce744
                            "STATUS=Stopped as result of IdleShutdown "
Packit 8ce744
                            "timeout.");
Packit 8ce744
            if (rv < 0) {
Packit 8ce744
                ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02804)
Packit 8ce744
                            "sd_notifyf returned an error %d", rv);
Packit 8ce744
            }
Packit 8ce744
            kill(mainpid, AP_SIG_GRACEFUL);
Packit 8ce744
        }
Packit 8ce744
    }
Packit 8ce744
    else {
Packit 8ce744
        shutdown_counter = 0;
Packit 8ce744
    }
Packit 8ce744
Packit 8ce744
    bytes_served = sload.bytes_served;
Packit 8ce744
Packit 8ce744
    return DECLINED;
Packit 8ce744
}
Packit 8ce744
Packit 8ce744
static void systemd_register_hooks(apr_pool_t *p)
Packit 8ce744
{
Packit 8ce744
    /* Enable ap_extended_status. */
Packit 8ce744
    ap_hook_pre_config(systemd_pre_config, NULL, NULL, APR_HOOK_LAST);
Packit 8ce744
    /* Grab the listener config. */
Packit 8ce744
    ap_hook_post_config(systemd_post_config, NULL, NULL, APR_HOOK_LAST);
Packit 7e67f6
    /* Signal service is ready. */
Packit 7e67f6
    ap_hook_post_config(systemd_post_config_last, NULL, NULL, APR_HOOK_REALLY_LAST);
Packit 8ce744
    /* We know the PID in this hook ... */
Packit 8ce744
    ap_hook_pre_mpm(systemd_pre_mpm, NULL, NULL, APR_HOOK_LAST);
Packit 8ce744
    /* Used to update httpd's status line using sd_notifyf */
Packit 8ce744
    ap_hook_monitor(systemd_monitor, NULL, NULL, APR_HOOK_MIDDLE);
Packit 8ce744
}
Packit 8ce744
Packit 8ce744
static const char *set_shutdown_timer(cmd_parms *cmd, void *dummy,
Packit 8ce744
                                      const char *arg)
Packit 8ce744
{
Packit 8ce744
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
Packit 8ce744
    if (err != NULL) {
Packit 8ce744
        return err;
Packit 8ce744
    }
Packit 8ce744
Packit 8ce744
    shutdown_timer = atoi(arg);
Packit 8ce744
    return NULL;
Packit 8ce744
}
Packit 8ce744
Packit 8ce744
static const command_rec systemd_cmds[] =
Packit 8ce744
{
Packit 8ce744
AP_INIT_TAKE1("IdleShutdown", set_shutdown_timer, NULL, RSRC_CONF,
Packit 8ce744
     "Number of seconds in idle-state after which httpd is shutdown"),
Packit 8ce744
    {NULL}
Packit 8ce744
};
Packit 8ce744
Packit 8ce744
AP_DECLARE_MODULE(systemd) = {
Packit 8ce744
    STANDARD20_MODULE_STUFF,
Packit 8ce744
    NULL,
Packit 8ce744
    NULL,
Packit 8ce744
    NULL,
Packit 8ce744
    NULL,
Packit 8ce744
    systemd_cmds,
Packit 8ce744
    systemd_register_hooks,
Packit 8ce744
};