Blame server/listen.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr_network_io.h"
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_main.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "ap_listen.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "mpm_common.h"
Packit 90a5c9
Packit 90a5c9
#include <stdlib.h>
Packit 90a5c9
#if APR_HAVE_UNISTD_H
Packit 90a5c9
#include <unistd.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 93cf16
#ifdef HAVE_SYSTEMD
Packit 93cf16
#include <systemd/sd-daemon.h>
Packit 93cf16
#endif
Packit 93cf16
Packit 90a5c9
/* we know core's module_index is 0 */
Packit 90a5c9
#undef APLOG_MODULE_INDEX
Packit 90a5c9
#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_DATA ap_listen_rec *ap_listeners = NULL;
Packit 90a5c9
Packit 90a5c9
/* Let ap_num_listen_buckets be global so that it can
Packit 90a5c9
 * be printed by ap_log_mpm_common(), but keep the listeners
Packit 90a5c9
 * buckets static since it is used only here to close them
Packit 90a5c9
 * all (including duplicated) with ap_close_listeners().
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE_DATA int ap_num_listen_buckets;
Packit 90a5c9
static ap_listen_rec **ap_listen_buckets;
Packit 90a5c9
Packit 90a5c9
/* Determine once, at runtime, whether or not SO_REUSEPORT
Packit 90a5c9
 * is usable on this platform, and hence whether or not
Packit 90a5c9
 * listeners can be duplicated (if configured).
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE_DATA int ap_have_so_reuseport = -1;
Packit 90a5c9
Packit 90a5c9
static ap_listen_rec *old_listeners;
Packit 90a5c9
static int ap_listenbacklog;
Packit 90a5c9
static int ap_listencbratio;
Packit 90a5c9
static int send_buffer_size;
Packit 90a5c9
static int receive_buffer_size;
Packit 39b23f
static int ap_listenfreebind;
Packit 93cf16
#ifdef HAVE_SYSTEMD
Packit 93cf16
static int use_systemd = -1;
Packit 93cf16
#endif
Packit 90a5c9
Packit 90a5c9
/* TODO: make_sock is just begging and screaming for APR abstraction */
Packit 93cf16
static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server, int do_bind_listen)
Packit 90a5c9
{
Packit 90a5c9
    apr_socket_t *s = server->sd;
Packit 90a5c9
    int one = 1;
Packit 90a5c9
#if APR_HAVE_IPV6
Packit 90a5c9
#ifdef AP_ENABLE_V4_MAPPED
Packit 90a5c9
    int v6only_setting = 0;
Packit 90a5c9
#else
Packit 90a5c9
    int v6only_setting = 1;
Packit 90a5c9
#endif
Packit 90a5c9
#endif
Packit 90a5c9
    apr_status_t stat;
Packit 90a5c9
Packit 90a5c9
#ifndef WIN32
Packit 90a5c9
    stat = apr_socket_opt_set(s, APR_SO_REUSEADDR, one);
Packit 90a5c9
    if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
Packit 90a5c9
        ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(00067)
Packit 90a5c9
                      "make_sock: for address %pI, apr_socket_opt_set: (SO_REUSEADDR)",
Packit 90a5c9
                      server->bind_addr);
Packit 90a5c9
        apr_socket_close(s);
Packit 90a5c9
        return stat;
Packit 90a5c9
    }
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
    stat = apr_socket_opt_set(s, APR_SO_KEEPALIVE, one);
Packit 90a5c9
    if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
Packit 90a5c9
        ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(00068)
Packit 90a5c9
                      "make_sock: for address %pI, apr_socket_opt_set: (SO_KEEPALIVE)",
Packit 90a5c9
                      server->bind_addr);
Packit 90a5c9
        apr_socket_close(s);
Packit 90a5c9
        return stat;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * To send data over high bandwidth-delay connections at full
Packit 90a5c9
     * speed we must force the TCP window to open wide enough to keep the
Packit 90a5c9
     * pipe full.  The default window size on many systems
Packit 90a5c9
     * is only 4kB.  Cross-country WAN connections of 100ms
Packit 90a5c9
     * at 1Mb/s are not impossible for well connected sites.
Packit 90a5c9
     * If we assume 100ms cross-country latency,
Packit 90a5c9
     * a 4kB buffer limits throughput to 40kB/s.
Packit 90a5c9
     *
Packit 90a5c9
     * To avoid this problem I've added the SendBufferSize directive
Packit 90a5c9
     * to allow the web master to configure send buffer size.
Packit 90a5c9
     *
Packit 90a5c9
     * The trade-off of larger buffers is that more kernel memory
Packit 90a5c9
     * is consumed.  YMMV, know your customers and your network!
Packit 90a5c9
     *
Packit 90a5c9
     * -John Heidemann <johnh@isi.edu> 25-Oct-96
Packit 90a5c9
     *
Packit 90a5c9
     * If no size is specified, use the kernel default.
Packit 90a5c9
     */
Packit 90a5c9
    if (send_buffer_size) {
Packit 90a5c9
        stat = apr_socket_opt_set(s, APR_SO_SNDBUF,  send_buffer_size);
Packit 90a5c9
        if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
Packit 90a5c9
            ap_log_perror(APLOG_MARK, APLOG_WARNING, stat, p, APLOGNO(00070)
Packit 90a5c9
                          "make_sock: failed to set SendBufferSize for "
Packit 90a5c9
                          "address %pI, using default",
Packit 90a5c9
                          server->bind_addr);
Packit 90a5c9
            /* not a fatal error */
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    if (receive_buffer_size) {
Packit 90a5c9
        stat = apr_socket_opt_set(s, APR_SO_RCVBUF, receive_buffer_size);
Packit 90a5c9
        if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
Packit 90a5c9
            ap_log_perror(APLOG_MARK, APLOG_WARNING, stat, p, APLOGNO(00071)
Packit 90a5c9
                          "make_sock: failed to set ReceiveBufferSize for "
Packit 90a5c9
                          "address %pI, using default",
Packit 90a5c9
                          server->bind_addr);
Packit 90a5c9
            /* not a fatal error */
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
#if APR_TCP_NODELAY_INHERITED
Packit 90a5c9
    ap_sock_disable_nagle(s);
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#if defined(SO_REUSEPORT)
Packit 90a5c9
    if (ap_have_so_reuseport && ap_listencbratio > 0) {
Packit 90a5c9
        int thesock;
Packit 90a5c9
        apr_os_sock_get(&thesock, s);
Packit 90a5c9
        if (setsockopt(thesock, SOL_SOCKET, SO_REUSEPORT,
Packit 90a5c9
                       (void *)&one, sizeof(int)) < 0) {
Packit 90a5c9
            stat = apr_get_netos_error();
Packit 90a5c9
            ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(02638)
Packit 90a5c9
                          "make_sock: for address %pI, apr_socket_opt_set: "
Packit 90a5c9
                          "(SO_REUSEPORT)",
Packit 90a5c9
                          server->bind_addr);
Packit 90a5c9
            apr_socket_close(s);
Packit 90a5c9
            return stat;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
#endif
Packit 90a5c9
Packit 39b23f
Packit 39b23f
#if defined(APR_SO_FREEBIND)
Packit 39b23f
    if (ap_listenfreebind) {
Packit 39b23f
        if (apr_socket_opt_set(s, APR_SO_FREEBIND, one) < 0) {
Packit 39b23f
            stat = apr_get_netos_error();
Packit 39b23f
            ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(02182)
Packit 39b23f
                          "make_sock: apr_socket_opt_set: "
Packit 39b23f
                          "error setting APR_SO_FREEBIND");
Packit 39b23f
            apr_socket_close(s);
Packit 39b23f
            return stat;
Packit 39b23f
       }
Packit 39b23f
   }
Packit 39b23f
#endif
Packit 39b23f
Packit 39b23f
Packit 93cf16
    if (do_bind_listen) {
Packit 93cf16
#if APR_HAVE_IPV6
Packit 93cf16
        if (server->bind_addr->family == APR_INET6) {
Packit 93cf16
            stat = apr_socket_opt_set(s, APR_IPV6_V6ONLY, v6only_setting);
Packit 93cf16
            if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
Packit 93cf16
                ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(00069)
Packit 93cf16
                              "make_sock: for address %pI, apr_socket_opt_set: "
Packit 93cf16
                              "(IPV6_V6ONLY)",
Packit 93cf16
                              server->bind_addr);
Packit 93cf16
                apr_socket_close(s);
Packit 93cf16
                return stat;
Packit 93cf16
            }
Packit 93cf16
        }
Packit 93cf16
#endif
Packit 90a5c9
Packit 93cf16
        if ((stat = apr_socket_bind(s, server->bind_addr)) != APR_SUCCESS) {
Packit 93cf16
            ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, stat, p, APLOGNO(00072)
Packit 93cf16
                          "make_sock: could not bind to address %pI",
Packit 93cf16
                          server->bind_addr);
Packit 93cf16
            apr_socket_close(s);
Packit 93cf16
            return stat;
Packit 93cf16
        }
Packit 93cf16
Packit 93cf16
        if ((stat = apr_socket_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
Packit 93cf16
            ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, stat, p, APLOGNO(00073)
Packit 93cf16
                          "make_sock: unable to listen for connections "
Packit 93cf16
                          "on address %pI",
Packit 93cf16
                          server->bind_addr);
Packit 93cf16
            apr_socket_close(s);
Packit 93cf16
            return stat;
Packit 93cf16
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
#ifdef WIN32
Packit 90a5c9
    /* I seriously doubt that this would work on Unix; I have doubts that
Packit 90a5c9
     * it entirely solves the problem on Win32.  However, since setting
Packit 90a5c9
     * reuseaddr on the listener -prior- to binding the socket has allowed
Packit 90a5c9
     * us to attach to the same port as an already running instance of
Packit 90a5c9
     * Apache, or even another web server, we cannot identify that this
Packit 90a5c9
     * port was exclusively granted to this instance of Apache.
Packit 90a5c9
     *
Packit 90a5c9
     * So set reuseaddr, but do not attempt to do so until we have the
Packit 90a5c9
     * parent listeners successfully bound.
Packit 90a5c9
     */
Packit 90a5c9
    stat = apr_socket_opt_set(s, APR_SO_REUSEADDR, one);
Packit 90a5c9
    if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
Packit 90a5c9
        ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(00074)
Packit 90a5c9
                    "make_sock: for address %pI, apr_socket_opt_set: (SO_REUSEADDR)",
Packit 90a5c9
                     server->bind_addr);
Packit 90a5c9
        apr_socket_close(s);
Packit 90a5c9
        return stat;
Packit 90a5c9
    }
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
    server->sd = s;
Packit 90a5c9
    server->active = 1;
Packit 90a5c9
Packit 90a5c9
    server->accept_func = NULL;
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char* find_accf_name(server_rec *s, const char *proto)
Packit 90a5c9
{
Packit 90a5c9
    const char* accf;
Packit 90a5c9
    core_server_config *conf = ap_get_core_module_config(s->module_config);
Packit 90a5c9
    if (!proto) {
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    accf = apr_table_get(conf->accf_map, proto);
Packit 90a5c9
Packit 90a5c9
    if (accf && !strcmp("none", accf)) {
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return accf;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void ap_apply_accept_filter(apr_pool_t *p, ap_listen_rec *lis,
Packit 90a5c9
                                           server_rec *server)
Packit 90a5c9
{
Packit 90a5c9
    apr_socket_t *s = lis->sd;
Packit 90a5c9
    const char *accf;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    const char *proto;
Packit 90a5c9
Packit 90a5c9
    proto = lis->protocol;
Packit 90a5c9
Packit 90a5c9
    if (!proto) {
Packit 90a5c9
        proto = ap_get_server_protocol(server);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
    accf = find_accf_name(server, proto);
Packit 90a5c9
Packit 90a5c9
    if (accf) {
Packit 90a5c9
#if APR_HAS_SO_ACCEPTFILTER
Packit 90a5c9
        /* In APR 1.x, the 2nd and 3rd parameters are char * instead of 
Packit 90a5c9
         * const char *, so make a copy of those args here.
Packit 90a5c9
         */
Packit 90a5c9
        rv = apr_socket_accept_filter(s, apr_pstrdup(p, accf),
Packit 90a5c9
                                      apr_pstrdup(p, ""));
Packit 90a5c9
        if (rv != APR_SUCCESS && !APR_STATUS_IS_ENOTIMPL(rv)) {
Packit 90a5c9
            ap_log_perror(APLOG_MARK, APLOG_WARNING, rv, p, APLOGNO(00075)
Packit 90a5c9
                          "Failed to enable the '%s' Accept Filter",
Packit 90a5c9
                          accf);
Packit 90a5c9
        }
Packit 90a5c9
#else
Packit 90a5c9
        rv = apr_socket_opt_set(s, APR_TCP_DEFER_ACCEPT, 30);
Packit 90a5c9
        if (rv != APR_SUCCESS && !APR_STATUS_IS_ENOTIMPL(rv)) {
Packit 90a5c9
            ap_log_perror(APLOG_MARK, APLOG_WARNING, rv, p, APLOGNO(00076)
Packit 90a5c9
                              "Failed to enable APR_TCP_DEFER_ACCEPT");
Packit 90a5c9
        }
Packit 90a5c9
#endif
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t close_listeners_on_exec(void *v)
Packit 90a5c9
{
Packit 90a5c9
    ap_close_listeners();
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int find_listeners(ap_listen_rec **from, ap_listen_rec **to,
Packit 90a5c9
                          const char *addr, apr_port_t port)
Packit 90a5c9
{
Packit 90a5c9
    int found = 0;
Packit 90a5c9
Packit 90a5c9
    while (*from) {
Packit 90a5c9
        apr_sockaddr_t *sa = (*from)->bind_addr;
Packit 90a5c9
Packit 90a5c9
        /* Some listeners are not real so they will not have a bind_addr. */
Packit 90a5c9
        if (sa) {
Packit 90a5c9
            ap_listen_rec *new;
Packit 90a5c9
            apr_port_t oldport;
Packit 90a5c9
Packit 90a5c9
            oldport = sa->port;
Packit 90a5c9
            /* If both ports are equivalent, then if their names are equivalent,
Packit 90a5c9
             * then we will re-use the existing record.
Packit 90a5c9
             */
Packit 90a5c9
            if (port == oldport &&
Packit 90a5c9
                ((!addr && !sa->hostname) ||
Packit 90a5c9
                 ((addr && sa->hostname) && !strcmp(sa->hostname, addr)))) {
Packit 90a5c9
                found = 1;
Packit 90a5c9
                if (!to) {
Packit 90a5c9
                    break;
Packit 90a5c9
                }
Packit 90a5c9
                new = *from;
Packit 90a5c9
                *from = new->next;
Packit 90a5c9
                new->next = *to;
Packit 90a5c9
                *to = new;
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        from = &(*from)->next;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return found;
Packit 90a5c9
}
Packit 90a5c9
Packit 93cf16
#ifdef HAVE_SYSTEMD
Packit 93cf16
Packit 93cf16
static int find_systemd_socket(process_rec * process, apr_port_t port) {
Packit 93cf16
    int fdcount, fd;
Packit 93cf16
    int sdc = sd_listen_fds(0);
Packit 93cf16
Packit 93cf16
    if (sdc < 0) {
Packit 93cf16
        ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02486)
Packit 93cf16
                      "find_systemd_socket: Error parsing enviroment, sd_listen_fds returned %d",
Packit 93cf16
                      sdc);
Packit 93cf16
        return -1;
Packit 93cf16
    }
Packit 93cf16
Packit 93cf16
    if (sdc == 0) {
Packit 93cf16
        ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
Packit 93cf16
                      "find_systemd_socket: At least one socket must be set.");
Packit 93cf16
        return -1;
Packit 93cf16
    }
Packit 93cf16
Packit 93cf16
    fdcount = atoi(getenv("LISTEN_FDS"));
Packit 93cf16
    for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fdcount; fd++) {
Packit 93cf16
        if (sd_is_socket_inet(fd, 0, 0, -1, port) > 0) {
Packit 93cf16
            return fd;
Packit 93cf16
        }
Packit 93cf16
    }
Packit 93cf16
Packit 93cf16
    return -1;
Packit 93cf16
}
Packit 93cf16
Packit 93cf16
static apr_status_t alloc_systemd_listener(process_rec * process,
Packit 93cf16
                                           int fd, const char *proto,
Packit 93cf16
                                           ap_listen_rec **out_rec)
Packit 93cf16
{
Packit 93cf16
    apr_status_t rv;
Packit 93cf16
    struct sockaddr sa;
Packit 93cf16
    socklen_t len = sizeof(struct sockaddr);
Packit 93cf16
    apr_os_sock_info_t si;
Packit 93cf16
    ap_listen_rec *rec;
Packit 93cf16
    *out_rec = NULL;
Packit 93cf16
Packit 93cf16
    memset(&si, 0, sizeof(si));
Packit 93cf16
Packit 93cf16
    rv = getsockname(fd, &sa, &len;;
Packit 93cf16
Packit 93cf16
    if (rv != 0) {
Packit 93cf16
        rv = apr_get_netos_error();
Packit 93cf16
        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02489)
Packit 93cf16
                      "getsockname on %d failed.", fd);
Packit 93cf16
        return rv;
Packit 93cf16
    }
Packit 93cf16
Packit 93cf16
    si.os_sock = &fd;
Packit 93cf16
    si.family = sa.sa_family;
Packit 93cf16
    si.local = &sa;
Packit 93cf16
    si.type = SOCK_STREAM;
Packit 93cf16
    si.protocol = APR_PROTO_TCP;
Packit 93cf16
Packit 93cf16
    rec = apr_palloc(process->pool, sizeof(ap_listen_rec));
Packit 93cf16
    rec->active = 0;
Packit 93cf16
    rec->next = 0;
Packit 93cf16
Packit 93cf16
Packit 93cf16
    rv = apr_os_sock_make(&rec->sd, &si, process->pool);
Packit 93cf16
    if (rv != APR_SUCCESS) {
Packit 93cf16
        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02490)
Packit 93cf16
                      "apr_os_sock_make on %d failed.", fd);
Packit 93cf16
        return rv;
Packit 93cf16
    }
Packit 93cf16
Packit 93cf16
    rv = apr_socket_addr_get(&rec->bind_addr, APR_LOCAL, rec->sd);
Packit 93cf16
    if (rv != APR_SUCCESS) {
Packit 93cf16
        ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02491)
Packit 93cf16
                      "apr_socket_addr_get on %d failed.", fd);
Packit 93cf16
        return rv;
Packit 93cf16
    }
Packit 93cf16
Packit 93cf16
    rec->protocol = apr_pstrdup(process->pool, proto);
Packit 93cf16
Packit 93cf16
    *out_rec = rec;
Packit 93cf16
Packit 93cf16
    return make_sock(process->pool, rec, 0);
Packit 93cf16
}
Packit 93cf16
Packit 93cf16
static const char *set_systemd_listener(process_rec *process, apr_port_t port,
Packit 93cf16
                                        const char *proto)
Packit 93cf16
{
Packit 93cf16
    ap_listen_rec *last, *new;
Packit 93cf16
    apr_status_t rv;
Packit 93cf16
    int fd = find_systemd_socket(process, port);
Packit 93cf16
    if (fd < 0) {
Packit 93cf16
        return "Systemd socket activation is used, but this port is not "
Packit 93cf16
                "configured in systemd";
Packit 93cf16
    }
Packit 93cf16
Packit 93cf16
    last = ap_listeners;
Packit 93cf16
    while (last && last->next) {
Packit 93cf16
        last = last->next;
Packit 93cf16
    }
Packit 93cf16
Packit 93cf16
    rv = alloc_systemd_listener(process, fd, proto, &new;;
Packit 93cf16
    if (rv != APR_SUCCESS) {
Packit 93cf16
        return "Failed to setup socket passed by systemd using socket activation";
Packit 93cf16
    }
Packit 93cf16
Packit 93cf16
    if (last == NULL) {
Packit 93cf16
        ap_listeners = last = new;
Packit 93cf16
    }
Packit 93cf16
    else {
Packit 93cf16
        last->next = new;
Packit 93cf16
        last = new;
Packit 93cf16
    }
Packit 93cf16
Packit 93cf16
    return NULL;
Packit 93cf16
}
Packit 93cf16
Packit 93cf16
#endif /* HAVE_SYSTEMD */
Packit 93cf16
Packit 90a5c9
static const char *alloc_listener(process_rec *process, const char *addr,
Packit 90a5c9
                                  apr_port_t port, const char* proto,
Packit 90a5c9
                                  void *slave)
Packit 90a5c9
{
Packit 90a5c9
    ap_listen_rec *last;
Packit 90a5c9
    apr_status_t status;
Packit 90a5c9
    apr_sockaddr_t *sa;
Packit 90a5c9
Packit 90a5c9
    /* see if we've got a listener for this address:port, which is an error */
Packit 90a5c9
    if (find_listeners(&ap_listeners, NULL, addr, port)) {
Packit 90a5c9
        return "Cannot define multiple Listeners on the same IP:port";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* see if we've got an old listener for this address:port */
Packit 90a5c9
    if (find_listeners(&old_listeners, &ap_listeners, addr, port)) {
Packit 90a5c9
        if (ap_listeners->slave != slave) {
Packit 90a5c9
            return "Cannot define a slave on the same IP:port as a Listener";
Packit 90a5c9
        }
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if ((status = apr_sockaddr_info_get(&sa, addr, APR_UNSPEC, port, 0,
Packit 90a5c9
                                        process->pool))
Packit 90a5c9
        != APR_SUCCESS) {
Packit 90a5c9
        ap_log_perror(APLOG_MARK, APLOG_CRIT, status, process->pool, APLOGNO(00077)
Packit 90a5c9
                      "alloc_listener: failed to set up sockaddr for %s",
Packit 90a5c9
                      addr);
Packit 90a5c9
        return "Listen setup failed";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Initialize to our last configured ap_listener. */
Packit 90a5c9
    last = ap_listeners;
Packit 90a5c9
    while (last && last->next) {
Packit 90a5c9
        last = last->next;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    while (sa) {
Packit 90a5c9
        ap_listen_rec *new;
Packit 90a5c9
Packit 90a5c9
        /* this has to survive restarts */
Packit 90a5c9
        new = apr_palloc(process->pool, sizeof(ap_listen_rec));
Packit 90a5c9
        new->active = 0;
Packit 90a5c9
        new->next = 0;
Packit 90a5c9
        new->bind_addr = sa;
Packit 90a5c9
        new->protocol = apr_pstrdup(process->pool, proto);
Packit 90a5c9
Packit 90a5c9
        /* Go to the next sockaddr. */
Packit 90a5c9
        sa = sa->next;
Packit 90a5c9
Packit 90a5c9
        status = apr_socket_create(&new->sd, new->bind_addr->family,
Packit 90a5c9
                                    SOCK_STREAM, 0, process->pool);
Packit 90a5c9
Packit 90a5c9
#if APR_HAVE_IPV6
Packit 90a5c9
        /* What could happen is that we got an IPv6 address, but this system
Packit 90a5c9
         * doesn't actually support IPv6.  Try the next address.
Packit 90a5c9
         */
Packit 90a5c9
        if (status != APR_SUCCESS && !addr &&
Packit 90a5c9
            new->bind_addr->family == APR_INET6) {
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
#endif
Packit 90a5c9
        if (status != APR_SUCCESS) {
Packit 90a5c9
            ap_log_perror(APLOG_MARK, APLOG_CRIT, status, process->pool, APLOGNO(00078)
Packit 90a5c9
                          "alloc_listener: failed to get a socket for %s",
Packit 90a5c9
                          addr);
Packit 90a5c9
            return "Listen setup failed";
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* We need to preserve the order returned by getaddrinfo() */
Packit 90a5c9
        if (last == NULL) {
Packit 90a5c9
            ap_listeners = last = new;
Packit 90a5c9
        } else {
Packit 90a5c9
            last->next = new;
Packit 90a5c9
            last = new;
Packit 90a5c9
        }
Packit 90a5c9
        new->slave = slave;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
/* Evaluates to true if the (apr_sockaddr_t *) addr argument is the
Packit 90a5c9
 * IPv4 match-any-address, 0.0.0.0. */
Packit 90a5c9
#define IS_INADDR_ANY(addr) ((addr)->family == APR_INET \
Packit 90a5c9
                             && (addr)->sa.sin.sin_addr.s_addr == INADDR_ANY)
Packit 90a5c9
Packit 90a5c9
/* Evaluates to true if the (apr_sockaddr_t *) addr argument is the
Packit 90a5c9
 * IPv6 match-any-address, [::]. */
Packit 90a5c9
#define IS_IN6ADDR_ANY(addr) ((addr)->family == APR_INET6 \
Packit 90a5c9
                              && IN6_IS_ADDR_UNSPECIFIED(&(addr)->sa.sin6.sin6_addr))
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * Create, open, listen, and bind all sockets.
Packit 90a5c9
 * @param process The process record for the currently running server
Packit 90a5c9
 * @return The number of open sockets
Packit 90a5c9
 */
Packit 90a5c9
static int open_listeners(apr_pool_t *pool)
Packit 90a5c9
{
Packit 90a5c9
    ap_listen_rec *lr;
Packit 90a5c9
    ap_listen_rec *next;
Packit 90a5c9
    ap_listen_rec *previous;
Packit 90a5c9
    int num_open;
Packit 90a5c9
    const char *userdata_key = "ap_open_listeners";
Packit 90a5c9
    void *data;
Packit 90a5c9
#if AP_NONBLOCK_WHEN_MULTI_LISTEN
Packit 90a5c9
    int use_nonblock;
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
    /* Don't allocate a default listener.  If we need to listen to a
Packit 90a5c9
     * port, then the user needs to have a Listen directive in their
Packit 90a5c9
     * config file.
Packit 90a5c9
     */
Packit 90a5c9
    num_open = 0;
Packit 90a5c9
    previous = NULL;
Packit 90a5c9
    for (lr = ap_listeners; lr; previous = lr, lr = lr->next) {
Packit 90a5c9
        if (lr->active) {
Packit 90a5c9
            ++num_open;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
#if APR_HAVE_IPV6
Packit 90a5c9
            ap_listen_rec *cur;
Packit 90a5c9
            int v6only_setting;
Packit 90a5c9
            int skip = 0;
Packit 90a5c9
Packit 90a5c9
            /* If we have the unspecified IPv4 address (0.0.0.0) and
Packit 90a5c9
             * the unspecified IPv6 address (::) is next, we need to
Packit 90a5c9
             * swap the order of these in the list. We always try to
Packit 90a5c9
             * bind to IPv6 first, then IPv4, since an IPv6 socket
Packit 90a5c9
             * might be able to receive IPv4 packets if V6ONLY is not
Packit 90a5c9
             * enabled, but never the other way around.
Packit 90a5c9
             * Note: In some configurations, the unspecified IPv6 address
Packit 90a5c9
             * could be even later in the list.  This logic only corrects
Packit 90a5c9
             * the situation where it is next in the list, such as when
Packit 90a5c9
             * apr_sockaddr_info_get() returns an IPv4 and an IPv6 address,
Packit 90a5c9
             * in that order.
Packit 90a5c9
             */
Packit 90a5c9
            if (lr->next != NULL
Packit 90a5c9
                && IS_INADDR_ANY(lr->bind_addr)
Packit 90a5c9
                && lr->bind_addr->port == lr->next->bind_addr->port
Packit 90a5c9
                && IS_IN6ADDR_ANY(lr->next->bind_addr)) {
Packit 90a5c9
                /* Exchange lr and lr->next */
Packit 90a5c9
                next = lr->next;
Packit 90a5c9
                lr->next = next->next;
Packit 90a5c9
                next->next = lr;
Packit 90a5c9
                if (previous) {
Packit 90a5c9
                    previous->next = next;
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    ap_listeners = next;
Packit 90a5c9
                }
Packit 90a5c9
                lr = next;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* If we are trying to bind to 0.0.0.0 and a previous listener
Packit 90a5c9
             * was :: on the same port and in turn that socket does not have
Packit 90a5c9
             * the IPV6_V6ONLY flag set; we must skip the current attempt to
Packit 90a5c9
             * listen (which would generate an error). IPv4 will be handled
Packit 90a5c9
             * on the established IPv6 socket.
Packit 90a5c9
             */
Packit 90a5c9
            if (IS_INADDR_ANY(lr->bind_addr) && previous) {
Packit 90a5c9
                for (cur = ap_listeners; cur != lr; cur = cur->next) {
Packit 90a5c9
                    if (lr->bind_addr->port == cur->bind_addr->port
Packit 90a5c9
                        && IS_IN6ADDR_ANY(cur->bind_addr)
Packit 90a5c9
                        && apr_socket_opt_get(cur->sd, APR_IPV6_V6ONLY,
Packit 90a5c9
                                              &v6only_setting) == APR_SUCCESS
Packit 90a5c9
                        && v6only_setting == 0) {
Packit 90a5c9
Packit 90a5c9
                        /* Remove the current listener from the list */
Packit 90a5c9
                        previous->next = lr->next;
Packit 90a5c9
                        lr = previous; /* maintain current value of previous after
Packit 90a5c9
                                        * post-loop expression is evaluated
Packit 90a5c9
                                        */
Packit 90a5c9
                        skip = 1;
Packit 90a5c9
                        break;
Packit 90a5c9
                    }
Packit 90a5c9
                }
Packit 90a5c9
                if (skip) {
Packit 90a5c9
                    continue;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
#endif
Packit 93cf16
            if (make_sock(pool, lr, 1) == APR_SUCCESS) {
Packit 90a5c9
                ++num_open;
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
#if APR_HAVE_IPV6
Packit 90a5c9
                /* If we tried to bind to ::, and the next listener is
Packit 90a5c9
                 * on 0.0.0.0 with the same port, don't give a fatal
Packit 90a5c9
                 * error. The user will still get a warning from make_sock
Packit 90a5c9
                 * though.
Packit 90a5c9
                 */
Packit 90a5c9
                if (lr->next != NULL
Packit 90a5c9
                    && IS_IN6ADDR_ANY(lr->bind_addr)
Packit 90a5c9
                    && lr->bind_addr->port == lr->next->bind_addr->port
Packit 90a5c9
                    && IS_INADDR_ANY(lr->next->bind_addr)) {
Packit 90a5c9
Packit 90a5c9
                    /* Remove the current listener from the list */
Packit 90a5c9
                    if (previous) {
Packit 90a5c9
                        previous->next = lr->next;
Packit 90a5c9
                    }
Packit 90a5c9
                    else {
Packit 90a5c9
                        ap_listeners = lr->next;
Packit 90a5c9
                    }
Packit 90a5c9
Packit 90a5c9
                    /* Although we've removed ourselves from the list,
Packit 90a5c9
                     * we need to make sure that the next iteration won't
Packit 90a5c9
                     * consider "previous" a working IPv6 '::' socket.
Packit 90a5c9
                     * Changing the family is enough to make sure the
Packit 90a5c9
                     * conditions before make_sock() fail.
Packit 90a5c9
                     */
Packit 90a5c9
                    lr->bind_addr->family = AF_INET;
Packit 90a5c9
Packit 90a5c9
                    continue;
Packit 90a5c9
                }
Packit 90a5c9
#endif
Packit 90a5c9
                /* fatal error */
Packit 90a5c9
                return -1;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* close the old listeners */
Packit 90a5c9
    ap_close_listeners_ex(old_listeners);
Packit 90a5c9
    old_listeners = NULL;
Packit 90a5c9
Packit 90a5c9
#if AP_NONBLOCK_WHEN_MULTI_LISTEN
Packit 90a5c9
    /* if multiple listening sockets, make them non-blocking so that
Packit 90a5c9
     * if select()/poll() reports readability for a reset connection that
Packit 90a5c9
     * is already forgotten about by the time we call accept, we won't
Packit 90a5c9
     * be hung until another connection arrives on that port
Packit 90a5c9
     */
Packit 90a5c9
    use_nonblock = (ap_listeners && ap_listeners->next);
Packit 90a5c9
    for (lr = ap_listeners; lr; lr = lr->next) {
Packit 90a5c9
        apr_status_t status;
Packit 90a5c9
Packit 90a5c9
        status = apr_socket_opt_set(lr->sd, APR_SO_NONBLOCK, use_nonblock);
Packit 90a5c9
        if (status != APR_SUCCESS) {
Packit 90a5c9
            ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, status, pool, APLOGNO(00079)
Packit 90a5c9
                          "unable to control socket non-blocking status");
Packit 90a5c9
            return -1;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
#endif /* AP_NONBLOCK_WHEN_MULTI_LISTEN */
Packit 90a5c9
Packit 90a5c9
    /* we come through here on both passes of the open logs phase
Packit 90a5c9
     * only register the cleanup once... otherwise we try to close
Packit 90a5c9
     * listening sockets twice when cleaning up prior to exec
Packit 90a5c9
     */
Packit 90a5c9
    apr_pool_userdata_get(&data, userdata_key, pool);
Packit 90a5c9
    if (!data) {
Packit 90a5c9
        apr_pool_userdata_set((const void *)1, userdata_key,
Packit 90a5c9
                              apr_pool_cleanup_null, pool);
Packit 90a5c9
        apr_pool_cleanup_register(pool, NULL, apr_pool_cleanup_null,
Packit 90a5c9
                                  close_listeners_on_exec);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return num_open ? 0 : -1;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_setup_listeners(server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    server_rec *ls;
Packit 90a5c9
    server_addr_rec *addr;
Packit 90a5c9
    ap_listen_rec *lr;
Packit 90a5c9
    int num_listeners = 0;
Packit 90a5c9
    const char* proto;
Packit 90a5c9
    int found;
Packit 90a5c9
Packit 90a5c9
    for (ls = s; ls; ls = ls->next) {
Packit 90a5c9
        proto = ap_get_server_protocol(ls);
Packit 90a5c9
        if (!proto) {
Packit 90a5c9
            found = 0;
Packit 90a5c9
            /* No protocol was set for this vhost,
Packit 90a5c9
             * use the default for this listener.
Packit 90a5c9
             */
Packit 90a5c9
            for (addr = ls->addrs; addr && !found; addr = addr->next) {
Packit 90a5c9
                for (lr = ap_listeners; lr; lr = lr->next) {
Packit 90a5c9
                    if (apr_sockaddr_equal(lr->bind_addr, addr->host_addr) &&
Packit 90a5c9
                        lr->bind_addr->port == addr->host_port) {
Packit 90a5c9
                        ap_set_server_protocol(ls, lr->protocol);
Packit 90a5c9
                        found = 1;
Packit 90a5c9
                        break;
Packit 90a5c9
                    }
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (!found) {
Packit 90a5c9
                /* TODO: set protocol defaults per-Port, eg 25=smtp */
Packit 90a5c9
                ap_set_server_protocol(ls, "http");
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 93cf16
#ifdef HAVE_SYSTEMD
Packit 93cf16
    if (use_systemd) {
Packit 93cf16
        const char *userdata_key = "ap_open_systemd_listeners";
Packit 93cf16
        void *data;
Packit 93cf16
        /* clear the enviroment on our second run
Packit 93cf16
        * so that none of our future children get confused.
Packit 93cf16
        */
Packit 93cf16
        apr_pool_userdata_get(&data, userdata_key, s->process->pool);
Packit 93cf16
        if (!data) {
Packit 93cf16
            apr_pool_userdata_set((const void *)1, userdata_key,
Packit 93cf16
                                apr_pool_cleanup_null, s->process->pool);
Packit 93cf16
        }
Packit 93cf16
        else {
Packit 93cf16
            sd_listen_fds(1);
Packit 93cf16
        }
Packit 93cf16
    }
Packit 93cf16
    else
Packit 93cf16
#endif
Packit 93cf16
    {
Packit 93cf16
        if (open_listeners(s->process->pool)) {
Packit 93cf16
            return 0;
Packit 93cf16
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    for (lr = ap_listeners; lr; lr = lr->next) {
Packit 90a5c9
        num_listeners++;
Packit 90a5c9
        found = 0;
Packit 90a5c9
        for (ls = s; ls && !found; ls = ls->next) {
Packit 90a5c9
            for (addr = ls->addrs; addr && !found; addr = addr->next) {
Packit 90a5c9
                if (apr_sockaddr_equal(lr->bind_addr, addr->host_addr) &&
Packit 90a5c9
                    lr->bind_addr->port == addr->host_port) {
Packit 90a5c9
                    found = 1;
Packit 90a5c9
                    ap_apply_accept_filter(s->process->pool, lr, ls);
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (!found) {
Packit 90a5c9
            ap_apply_accept_filter(s->process->pool, lr, s);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return num_listeners;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_duplicate_listeners(apr_pool_t *p, server_rec *s,
Packit 90a5c9
                                                ap_listen_rec ***buckets,
Packit 90a5c9
                                                int *num_buckets)
Packit 90a5c9
{
Packit 90a5c9
    static int warn_once;
Packit 90a5c9
    int i;
Packit 90a5c9
    apr_status_t stat;
Packit 90a5c9
    int use_nonblock = 0;
Packit 90a5c9
    ap_listen_rec *lr;
Packit 90a5c9
Packit 90a5c9
    if (*num_buckets < 1) {
Packit 90a5c9
        *num_buckets = 1;
Packit 90a5c9
        if (ap_listencbratio > 0) {
Packit 90a5c9
#ifdef _SC_NPROCESSORS_ONLN
Packit 90a5c9
            if (ap_have_so_reuseport) {
Packit 90a5c9
                int num_online_cores = sysconf(_SC_NPROCESSORS_ONLN),
Packit 90a5c9
                    val = num_online_cores / ap_listencbratio;
Packit 90a5c9
                if (val > 1) {
Packit 90a5c9
                    *num_buckets = val;
Packit 90a5c9
                }
Packit 90a5c9
                ap_log_perror(APLOG_MARK, APLOG_INFO, 0, p, APLOGNO(02819)
Packit 90a5c9
                              "Using %i listeners bucket(s) based on %i "
Packit 90a5c9
                              "online CPU cores and a ratio of %i",
Packit 90a5c9
                              *num_buckets, num_online_cores,
Packit 90a5c9
                              ap_listencbratio);
Packit 90a5c9
            }
Packit 90a5c9
            else
Packit 90a5c9
#endif
Packit 90a5c9
            if (!warn_once) {
Packit 90a5c9
                ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p, APLOGNO(02820)
Packit 90a5c9
                              "ListenCoresBucketsRatio ignored without "
Packit 90a5c9
                              "SO_REUSEPORT and _SC_NPROCESSORS_ONLN "
Packit 90a5c9
                              "support: using a single listeners bucket");
Packit 90a5c9
                warn_once = 1;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    *buckets = apr_pcalloc(p, *num_buckets * sizeof(ap_listen_rec *));
Packit 90a5c9
    (*buckets)[0] = ap_listeners;
Packit 90a5c9
Packit 90a5c9
    for (i = 1; i < *num_buckets; i++) {
Packit 90a5c9
        ap_listen_rec *last = NULL;
Packit 90a5c9
        lr = ap_listeners;
Packit 90a5c9
        while (lr) {
Packit 90a5c9
            ap_listen_rec *duplr;
Packit 90a5c9
            char *hostname;
Packit 90a5c9
            apr_port_t port;
Packit 90a5c9
            apr_sockaddr_t *sa;
Packit 90a5c9
            duplr = apr_palloc(p, sizeof(ap_listen_rec));
Packit 90a5c9
            duplr->slave = NULL;
Packit 90a5c9
            duplr->protocol = apr_pstrdup(p, lr->protocol);
Packit 90a5c9
            hostname = apr_pstrdup(p, lr->bind_addr->hostname);
Packit 90a5c9
            port = lr->bind_addr->port;
Packit 90a5c9
            apr_sockaddr_info_get(&sa, hostname, APR_UNSPEC, port, 0, p);
Packit 90a5c9
            duplr->bind_addr = sa;
Packit 90a5c9
            duplr->next = NULL;
Packit 90a5c9
            stat = apr_socket_create(&duplr->sd, duplr->bind_addr->family,
Packit 90a5c9
                                     SOCK_STREAM, 0, p);
Packit 90a5c9
            if (stat != APR_SUCCESS) {
Packit 90a5c9
                ap_log_perror(APLOG_MARK, APLOG_CRIT, 0, p, APLOGNO(02640)
Packit 90a5c9
                            "ap_duplicate_listeners: for address %pI, "
Packit 90a5c9
                            "cannot duplicate a new socket!",
Packit 90a5c9
                            duplr->bind_addr);
Packit 90a5c9
                return stat;
Packit 90a5c9
            }
Packit 93cf16
            make_sock(p, duplr, 1);
Packit 90a5c9
#if AP_NONBLOCK_WHEN_MULTI_LISTEN
Packit 90a5c9
            use_nonblock = (ap_listeners && ap_listeners->next);
Packit 90a5c9
            stat = apr_socket_opt_set(duplr->sd, APR_SO_NONBLOCK, use_nonblock);
Packit 90a5c9
            if (stat != APR_SUCCESS) {
Packit 90a5c9
                ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(02641)
Packit 90a5c9
                              "unable to control socket non-blocking status");
Packit 90a5c9
                return stat;
Packit 90a5c9
            }
Packit 90a5c9
#endif
Packit 90a5c9
            ap_apply_accept_filter(p, duplr, s);
Packit 90a5c9
Packit 90a5c9
            if (last == NULL) {
Packit 90a5c9
                (*buckets)[i] = last = duplr;
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                last->next = duplr;
Packit 90a5c9
                last = duplr;
Packit 90a5c9
            }
Packit 90a5c9
            lr = lr->next;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_listen_buckets = *buckets;
Packit 90a5c9
    ap_num_listen_buckets = *num_buckets;
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_NONSTD(void) ap_close_listeners(void)
Packit 90a5c9
{
Packit 90a5c9
    int i;
Packit 90a5c9
Packit 90a5c9
    ap_close_listeners_ex(ap_listeners);
Packit 90a5c9
Packit 90a5c9
    /* Start from index 1 since either ap_duplicate_listeners()
Packit 90a5c9
     * was called and ap_listen_buckets[0] == ap_listeners, or
Packit 90a5c9
     * it wasn't and ap_num_listen_buckets == 0.
Packit 90a5c9
     */
Packit 90a5c9
    for (i = 1; i < ap_num_listen_buckets; i++) {
Packit 90a5c9
        ap_close_listeners_ex(ap_listen_buckets[i]);
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_NONSTD(void) ap_close_listeners_ex(ap_listen_rec *listeners)
Packit 90a5c9
{
Packit 90a5c9
    ap_listen_rec *lr;
Packit 90a5c9
    for (lr = listeners; lr; lr = lr->next) {
Packit 90a5c9
        apr_socket_close(lr->sd);
Packit 90a5c9
        lr->active = 0;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_NONSTD(int) ap_close_selected_listeners(ap_slave_t *slave)
Packit 90a5c9
{
Packit 90a5c9
    ap_listen_rec *lr;
Packit 90a5c9
    int n = 0;
Packit 90a5c9
Packit 90a5c9
    for (lr = ap_listeners; lr; lr = lr->next) {
Packit 90a5c9
        if (lr->slave != slave) {
Packit 90a5c9
            apr_socket_close(lr->sd);
Packit 90a5c9
            lr->active = 0;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            ++n;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    return n;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_listen_pre_config(void)
Packit 90a5c9
{
Packit 90a5c9
    old_listeners = ap_listeners;
Packit 90a5c9
    ap_listeners = NULL;
Packit 90a5c9
    ap_listen_buckets = NULL;
Packit 90a5c9
    ap_num_listen_buckets = 0;
Packit 90a5c9
    ap_listenbacklog = DEFAULT_LISTENBACKLOG;
Packit 90a5c9
    ap_listencbratio = 0;
Packit 90a5c9
Packit 90a5c9
    /* Check once whether or not SO_REUSEPORT is supported. */
Packit 90a5c9
    if (ap_have_so_reuseport < 0) {
Packit 90a5c9
        /* This is limited to Linux with defined SO_REUSEPORT (ie. 3.9+) for
Packit 90a5c9
         * now since the implementation evenly distributes connections across
Packit 90a5c9
         * all the listening threads/processes.
Packit 90a5c9
         *
Packit 90a5c9
         * *BSDs have SO_REUSEPORT too but with a different semantic: the first
Packit 90a5c9
         * wildcard address bound socket or the last non-wildcard address bound
Packit 90a5c9
         * socket will receive connections (no evenness garantee); the rest of
Packit 90a5c9
         * the sockets bound to the same port will not.
Packit 90a5c9
         * This can't (always) work for httpd.
Packit 90a5c9
         *
Packit 90a5c9
         * TODO: latests DragonFlyBSD's SO_REUSEPORT (seems to?) have the same
Packit 90a5c9
         * semantic as Linux, so we may need HAVE_SO_REUSEPORT available from
Packit 90a5c9
         * configure.in some day.
Packit 90a5c9
         */
Packit 90a5c9
#if defined(SO_REUSEPORT) && defined(__linux__)
Packit 90a5c9
        apr_socket_t *sock;
Packit 90a5c9
        if (apr_socket_create(&sock, APR_UNSPEC, SOCK_STREAM, 0,
Packit 90a5c9
                              ap_pglobal) == APR_SUCCESS) {
Packit 90a5c9
            int thesock, on = 1;
Packit 90a5c9
            apr_os_sock_get(&thesock, sock);
Packit 90a5c9
            ap_have_so_reuseport = (setsockopt(thesock, SOL_SOCKET,
Packit 90a5c9
                                               SO_REUSEPORT, (void *)&on,
Packit 90a5c9
                                               sizeof(int)) == 0);
Packit 90a5c9
            apr_socket_close(sock);
Packit 90a5c9
        }
Packit 90a5c9
        else
Packit 90a5c9
#endif
Packit 90a5c9
        ap_have_so_reuseport = 0;
Packit 90a5c9
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 39b23f
Packit 90a5c9
AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
Packit 90a5c9
                                                int argc, char *const argv[])
Packit 90a5c9
{
Packit 90a5c9
    char *host, *scope_id, *proto;
Packit 90a5c9
    apr_port_t port;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
Packit 90a5c9
Packit 90a5c9
    if (err != NULL) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (argc < 1 || argc > 2) {
Packit 90a5c9
        return "Listen requires 1 or 2 arguments.";
Packit 90a5c9
    }
Packit 93cf16
#ifdef HAVE_SYSTEMD
Packit 93cf16
    if (use_systemd == -1) {
Packit 93cf16
        use_systemd = sd_listen_fds(0) > 0;
Packit 93cf16
    }
Packit 93cf16
#endif
Packit 90a5c9
Packit 90a5c9
    rv = apr_parse_addr_port(&host, &scope_id, &port, argv[0], cmd->pool);
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        return "Invalid address or port";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (host && !strcmp(host, "*")) {
Packit 90a5c9
        host = NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (scope_id) {
Packit 90a5c9
        /* XXX scope id support is useful with link-local IPv6 addresses */
Packit 90a5c9
        return "Scope id is not supported";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (!port) {
Packit 90a5c9
        return "Port must be specified";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (argc != 2) {
Packit 90a5c9
        if (port == 443) {
Packit 90a5c9
            proto = "https";
Packit 90a5c9
        } else {
Packit 90a5c9
            proto = "http";
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        proto = apr_pstrdup(cmd->pool, argv[1]);
Packit 90a5c9
        ap_str_tolower(proto);
Packit 90a5c9
    }
Packit 90a5c9
Packit 93cf16
#ifdef HAVE_SYSTEMD
Packit 93cf16
    if (use_systemd) {
Packit 93cf16
        return set_systemd_listener(cmd->server->process, port, proto);
Packit 93cf16
    }
Packit 93cf16
#endif
Packit 93cf16
Packit 90a5c9
    return alloc_listener(cmd->server->process, host, port, proto, NULL);
Packit 90a5c9
}
Packit 90a5c9
Packit 39b23f
AP_DECLARE_NONSTD(const char *) ap_set_freelistener(cmd_parms *cmd, void *dummy,
Packit 39b23f
                                                    int argc,
Packit 39b23f
                                                    char *const argv[])
Packit 39b23f
{
Packit 39b23f
    ap_listenfreebind = 1;
Packit 39b23f
    return ap_set_listener(cmd, dummy, argc, argv);
Packit 39b23f
}
Packit 39b23f
Packit 90a5c9
AP_DECLARE_NONSTD(const char *) ap_set_listenbacklog(cmd_parms *cmd,
Packit 90a5c9
                                                     void *dummy,
Packit 90a5c9
                                                     const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    int b;
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
Packit 90a5c9
Packit 90a5c9
    if (err != NULL) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    b = atoi(arg);
Packit 90a5c9
    if (b < 1) {
Packit 90a5c9
        return "ListenBacklog must be > 0";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_listenbacklog = b;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_NONSTD(const char *) ap_set_listencbratio(cmd_parms *cmd,
Packit 90a5c9
                                                     void *dummy,
Packit 90a5c9
                                                     const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    int b;
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
Packit 90a5c9
Packit 90a5c9
    if (err != NULL) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    b = atoi(arg);
Packit 90a5c9
    if (b < 1) {
Packit 90a5c9
        return "ListenCoresBucketsRatio must be > 0";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_listencbratio = b;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_NONSTD(const char *) ap_set_send_buffer_size(cmd_parms *cmd,
Packit 90a5c9
                                                        void *dummy,
Packit 90a5c9
                                                        const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    int s = atoi(arg);
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
Packit 90a5c9
Packit 90a5c9
    if (err != NULL) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (s < 512 && s != 0) {
Packit 90a5c9
        return "SendBufferSize must be >= 512 bytes, or 0 for system default.";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    send_buffer_size = s;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_NONSTD(const char *) ap_set_receive_buffer_size(cmd_parms *cmd,
Packit 90a5c9
                                                           void *dummy,
Packit 90a5c9
                                                           const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    int s = atoi(arg);
Packit 90a5c9
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
Packit 90a5c9
Packit 90a5c9
    if (err != NULL) {
Packit 90a5c9
        return err;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (s < 512 && s != 0) {
Packit 90a5c9
        return "ReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    receive_buffer_size = s;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}