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