motorz.h revision e727b3cfc15b8a21efaafccb7ca41795f2a060d5
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering/* Licensed to the Apache Software Foundation (ASF) under one or more
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering * contributor license agreements. See the NOTICE file distributed with
12b42c76672a66c2d4ea7212c14f8f1b5a62b78dTom Gundersen * this work for additional information regarding copyright ownership.
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering * The ASF licenses this file to You under the Apache License, Version 2.0
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering * (the "License"); you may not use this file except in compliance with
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering * the License. You may obtain a copy of the License at
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering *
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering * http://www.apache.org/licenses/LICENSE-2.0
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering *
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering * Unless required by applicable law or agreed to in writing, software
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering * distributed under the License is distributed on an "AS IS" BASIS,
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering * See the License for the specific language governing permissions and
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering * limitations under the License.
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering */
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#include "apr.h"
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#include "apr_portable.h"
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#include "apr_strings.h"
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#include "apr_thread_proc.h"
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#include "apr_signal.h"
14f01575e8efba8709f456e234514ec980516381Felipe Sateler
14f01575e8efba8709f456e234514ec980516381Felipe Sateler#define APR_WANT_STDIO
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#define APR_WANT_STRFUNC
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "apr_want.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#if APR_HAVE_UNISTD_H
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#include <unistd.h>
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#endif
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#if APR_HAVE_SYS_TYPES_H
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include <sys/types.h>
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#endif
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "ap_config.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "httpd.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "mpm_default.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "http_main.h"
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#include "http_log.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "http_config.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "http_core.h" /* for get_remote_host */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "http_connection.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "scoreboard.h"
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#include "ap_mpm.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "util_mutex.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "unixd.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "http_vhost.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "mpm_common.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "ap_listen.h"
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#include "ap_mmn.h"
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include "apr_poll.h"
14f01575e8efba8709f456e234514ec980516381Felipe Sateler#include "apr_skiplist.h"
14f01575e8efba8709f456e234514ec980516381Felipe Sateler#include "apr_thread_pool.h"
14f01575e8efba8709f456e234514ec980516381Felipe Sateler#include "util_time.h"
14f01575e8efba8709f456e234514ec980516381Felipe Sateler
14f01575e8efba8709f456e234514ec980516381Felipe Sateler#include <stdlib.h>
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#ifdef HAVE_TIME_H
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#include <time.h>
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#endif
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#ifdef HAVE_SYS_PROCESSOR_H
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#include <sys/processor.h> /* for bindprocessor() */
b938cb902c3b5bca807a94b277672c64d6767886Jan Engelhardt#endif
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek
3ba3a79df4ae094d1008c04a9af8d1ff970124c4Zbigniew Jędrzejewski-Szmek#include <signal.h>
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#include <sys/times.h>
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering
14f01575e8efba8709f456e234514ec980516381Felipe Sateler/* Limit on the total --- clients will be locked out if more servers than
14f01575e8efba8709f456e234514ec980516381Felipe Sateler * this are needed. It is intended solely to keep the server from crashing
14f01575e8efba8709f456e234514ec980516381Felipe Sateler * when things get out of hand.
14f01575e8efba8709f456e234514ec980516381Felipe Sateler *
14f01575e8efba8709f456e234514ec980516381Felipe Sateler * We keep a hard maximum number of servers, for two reasons --- first off,
14f01575e8efba8709f456e234514ec980516381Felipe Sateler * in case something goes seriously wrong, we want to stop the fork bomb
14f01575e8efba8709f456e234514ec980516381Felipe Sateler * short of actually crashing the machine we're running on by filling some
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * kernel table. Secondly, it keeps the size of the scoreboard file small
3ba3a79df4ae094d1008c04a9af8d1ff970124c4Zbigniew Jędrzejewski-Szmek * enough that we can read the whole thing without worrying too much about
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * the overhead.
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek */
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#ifndef DEFAULT_SERVER_LIMIT
14f01575e8efba8709f456e234514ec980516381Felipe Sateler#define DEFAULT_SERVER_LIMIT 256
14f01575e8efba8709f456e234514ec980516381Felipe Sateler#endif
14f01575e8efba8709f456e234514ec980516381Felipe Sateler
14f01575e8efba8709f456e234514ec980516381Felipe Sateler/* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT. We want
14f01575e8efba8709f456e234514ec980516381Felipe Sateler * some sort of compile-time limit to help catch typos.
14f01575e8efba8709f456e234514ec980516381Felipe Sateler */
14f01575e8efba8709f456e234514ec980516381Felipe Sateler#ifndef MAX_SERVER_LIMIT
14f01575e8efba8709f456e234514ec980516381Felipe Sateler#define MAX_SERVER_LIMIT 200000
14f01575e8efba8709f456e234514ec980516381Felipe Sateler#endif
14f01575e8efba8709f456e234514ec980516381Felipe Sateler
14f01575e8efba8709f456e234514ec980516381Felipe Sateler/* Limit on the threads per process. Clients will be locked out if more than
14f01575e8efba8709f456e234514ec980516381Felipe Sateler * this are needed.
14f01575e8efba8709f456e234514ec980516381Felipe Sateler *
14f01575e8efba8709f456e234514ec980516381Felipe Sateler * We keep this for one reason it keeps the size of the scoreboard file small
14f01575e8efba8709f456e234514ec980516381Felipe Sateler * enough that we can read the whole thing without worrying too much about
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * the overhead.
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#ifndef DEFAULT_THREAD_LIMIT
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek#define DEFAULT_THREAD_LIMIT 64
3ba3a79df4ae094d1008c04a9af8d1ff970124c4Zbigniew Jędrzejewski-Szmek#endif
3ba3a79df4ae094d1008c04a9af8d1ff970124c4Zbigniew Jędrzejewski-Szmek
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek/* Admin can't tune ThreadLimit beyond MAX_THREAD_LIMIT. We want
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * some sort of compile-time limit to help catch typos.
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering */
9393a8774c1acd60deea40007061b9ffc783bf7eLennart Poettering#ifndef MAX_THREAD_LIMIT
#define MAX_THREAD_LIMIT 100000
#endif
#define MPM_CHILD_PID(i) (ap_scoreboard_image->parent[i].pid)
/**
* typedefs
*/
/* data retained by prefork across load/unload of the module
* allocated on first call to pre-config hook; located on
* subsequent calls to pre-config hook
*/
typedef struct motorz_core_t motorz_core_t;
struct motorz_core_t {
int first_server_limit;
int module_loads;
ap_generation_t my_generation;
int volatile is_graceful; /* set from signal handler */
int maxclients_reported;
/*
* The max child slot ever assigned, preserved across restarts. Necessary
* to deal with MaxRequestWorkers changes across AP_SIG_GRACEFUL restarts. We
* use this value to optimize routines that have to scan the entire scoreboard.
*/
int max_daemons_limit;
apr_pool_t *pool;
apr_thread_mutex_t *mtx;
apr_pollcb_t *pollcb;
apr_skiplist *timer_ring;
apr_thread_pool_t *workers;
};
static motorz_core_t *g_motorz_core;
typedef struct motorz_child_bucket motorz_child_bucket;
struct motorz_child_bucket {
ap_pod_t *pod;
ap_listen_rec *listeners;
apr_proc_mutex_t *mutex;
};
typedef enum
{
PT_CSD,
PT_ACCEPT,
PT_USER
} motorz_poll_type_e;
typedef struct motorz_sb_t motorz_sb_t;
struct motorz_sb_t
{
motorz_poll_type_e type;
void *baton;
};
typedef void (*motorz_timer_cb) (motorz_core_t *mz, void *baton);
typedef void (*motorz_io_sock_cb) (motorz_core_t *mz, apr_socket_t *sock,
int flags, void *baton);
typedef void (*motorz_io_file_cb) (motorz_core_t *mz, apr_socket_t *sock,
int flags, void *baton);
typedef struct motorz_timer_t motorz_timer_t;
struct motorz_timer_t
{
apr_time_t expires;
motorz_timer_cb cb;
void *baton;
apr_pool_t *pool;
motorz_core_t *mz;
};
typedef struct motorz_conn_t motorz_conn_t;
struct motorz_conn_t
{
apr_pool_t *pool;
motorz_core_t *mz;
apr_socket_t *sock;
apr_bucket_alloc_t *ba;
conn_rec *c;
/** poll file descriptor information */
apr_pollfd_t pfd;
/** public parts of the connection state */
conn_state_t cs;
};
/**
* config globals
*/
static int threads_per_child = 0;
static int ap_num_kids=0;
static int ap_daemons_min_free=0;
static int ap_daemons_max_free=0;
static int ap_daemons_limit=0; /* MaxRequestWorkers */
static int server_limit = 0;
static int mpm_state = AP_MPMQ_STARTING;
/* one_process --- debugging mode variable; can be set from the command line
* with the -X flag. If set, this gets you the child_main loop running
* in the process which originally started up (no detach, no make_child),
* which is a pretty nice debugging environment. (You'll get a SIGHUP
* early in standalone_main; just continue through. This is the server
* trying to kill off any child processes which it might have lying
* around --- Apache doesn't keep track of their pids, it just sends
* SIGHUP to the process group, ignoring it in the root process.
* Continue through and you'll be fine.).
*/
static int one_process = 0;
static apr_pool_t *pconf; /* Pool for config stuff */
static apr_pool_t *pchild; /* Pool for httpd child stuff */
static pid_t ap_my_pid; /* it seems silly to call getpid all the time */
static pid_t parent_pid;
static int my_child_num;
static int num_buckets; /* Number of listeners buckets */
static motorz_child_bucket *all_buckets, /* All listeners buckets */
*my_bucket; /* Current child bucket */
static void clean_child_exit(int code) __attribute__ ((noreturn));