e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim/* Licensed to the Apache Software Foundation (ASF) under one or more
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * contributor license agreements. See the NOTICE file distributed with
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * this work for additional information regarding copyright ownership.
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * The ASF licenses this file to You under the Apache License, Version 2.0
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * (the "License"); you may not use this file except in compliance with
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * the License. You may obtain a copy of the License at
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim *
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * http://www.apache.org/licenses/LICENSE-2.0
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim *
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * Unless required by applicable law or agreed to in writing, software
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * distributed under the License is distributed on an "AS IS" BASIS,
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * See the License for the specific language governing permissions and
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * limitations under the License.
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "apr.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "apr_portable.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "apr_strings.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "apr_thread_proc.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "apr_signal.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#define APR_WANT_STDIO
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#define APR_WANT_STRFUNC
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "apr_want.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#if APR_HAVE_UNISTD_H
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include <unistd.h>
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#endif
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#if APR_HAVE_SYS_TYPES_H
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include <sys/types.h>
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#endif
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "ap_config.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "httpd.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "mpm_default.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "http_main.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "http_log.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "http_config.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "http_core.h" /* for get_remote_host */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "http_connection.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "scoreboard.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "ap_mpm.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "util_mutex.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "unixd.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "http_vhost.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "mpm_common.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "ap_listen.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "ap_mmn.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "apr_poll.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "apr_skiplist.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "apr_thread_pool.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include "util_time.h"
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include <stdlib.h>
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#ifdef HAVE_TIME_H
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include <time.h>
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#endif
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#ifdef HAVE_SYS_PROCESSOR_H
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include <sys/processor.h> /* for bindprocessor() */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#endif
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include <signal.h>
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#include <sys/times.h>
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim/* Limit on the total --- clients will be locked out if more servers than
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * this are needed. It is intended solely to keep the server from crashing
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * when things get out of hand.
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim *
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * We keep a hard maximum number of servers, for two reasons --- first off,
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * in case something goes seriously wrong, we want to stop the fork bomb
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * short of actually crashing the machine we're running on by filling some
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * kernel table. Secondly, it keeps the size of the scoreboard file small
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * enough that we can read the whole thing without worrying too much about
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * the overhead.
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#ifndef DEFAULT_SERVER_LIMIT
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#define DEFAULT_SERVER_LIMIT 256
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#endif
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim/* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT. We want
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * some sort of compile-time limit to help catch typos.
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#ifndef MAX_SERVER_LIMIT
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#define MAX_SERVER_LIMIT 200000
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#endif
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim/* Limit on the threads per process. Clients will be locked out if more than
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * this are needed.
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim *
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * We keep this for one reason it keeps the size of the scoreboard file small
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * enough that we can read the whole thing without worrying too much about
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * the overhead.
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#ifndef DEFAULT_THREAD_LIMIT
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#define DEFAULT_THREAD_LIMIT 64
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#endif
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim/* Admin can't tune ThreadLimit beyond MAX_THREAD_LIMIT. We want
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * some sort of compile-time limit to help catch typos.
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#ifndef MAX_THREAD_LIMIT
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#define MAX_THREAD_LIMIT 100000
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#endif
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim#define MPM_CHILD_PID(i) (ap_scoreboard_image->parent[i].pid)
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim/**
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * typedefs
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim/* data retained by prefork across load/unload of the module
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * allocated on first call to pre-config hook; located on
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * subsequent calls to pre-config hook
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimtypedef struct motorz_core_t motorz_core_t;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimstruct motorz_core_t {
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim int first_server_limit;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim int module_loads;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim ap_generation_t my_generation;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim int volatile is_graceful; /* set from signal handler */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim int maxclients_reported;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim /*
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * The max child slot ever assigned, preserved across restarts. Necessary
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * to deal with MaxRequestWorkers changes across AP_SIG_GRACEFUL restarts. We
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim * use this value to optimize routines that have to scan the entire scoreboard.
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim int max_daemons_limit;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim apr_pool_t *pool;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim apr_thread_mutex_t *mtx;
ccb1d3d23d96790217807954139c625ec9763350jim apr_pollset_t *pollset;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim apr_skiplist *timer_ring;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim apr_thread_pool_t *workers;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim};
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimtypedef struct motorz_child_bucket motorz_child_bucket;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimstruct motorz_child_bucket {
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim ap_pod_t *pod;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim ap_listen_rec *listeners;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim apr_proc_mutex_t *mutex;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim};
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimtypedef enum
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim{
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim PT_CSD,
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim PT_ACCEPT,
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim PT_USER
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim} motorz_poll_type_e;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimtypedef struct motorz_sb_t motorz_sb_t;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimstruct motorz_sb_t
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim{
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim motorz_poll_type_e type;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim void *baton;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim};
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimtypedef void (*motorz_timer_cb) (motorz_core_t *mz, void *baton);
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimtypedef void (*motorz_io_sock_cb) (motorz_core_t *mz, apr_socket_t *sock,
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim int flags, void *baton);
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimtypedef void (*motorz_io_file_cb) (motorz_core_t *mz, apr_socket_t *sock,
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim int flags, void *baton);
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
a79724574b554eb79b351f9d76ca78f56ac6b476ylavictypedef struct motorz_timer_t motorz_timer_t;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimstruct motorz_timer_t
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim{
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim apr_time_t expires;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim motorz_timer_cb cb;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim void *baton;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim apr_pool_t *pool;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim motorz_core_t *mz;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim};
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimtypedef struct motorz_conn_t motorz_conn_t;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jimstruct motorz_conn_t
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim{
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim apr_pool_t *pool;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim motorz_core_t *mz;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim apr_socket_t *sock;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim apr_bucket_alloc_t *ba;
4ef955e505f09413a07d092dd64802e14ce2d6bcjim ap_sb_handle_t *sbh;
ccb1d3d23d96790217807954139c625ec9763350jim /** connection record this struct refers to */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim conn_rec *c;
ccb1d3d23d96790217807954139c625ec9763350jim /** request record (if any) this struct refers to */
ccb1d3d23d96790217807954139c625ec9763350jim request_rec *r;
ccb1d3d23d96790217807954139c625ec9763350jim /** is the current conn_rec suspended? */
ccb1d3d23d96790217807954139c625ec9763350jim int suspended;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim /** poll file descriptor information */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim apr_pollfd_t pfd;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim /** public parts of the connection state */
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim conn_state_t cs;
6415151dcc1f5a7257cfe440efd0558adcccedc3ylavic /** timer associated with the connection */
6415151dcc1f5a7257cfe440efd0558adcccedc3ylavic motorz_timer_t timer;
e727b3cfc15b8a21efaafccb7ca41795f2a060d5jim};