📄 perchild.c
字号:
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "apr_hash.h"
#include "apr_strings.h"
#include "apr_pools.h"
#include "apr_portable.h"
#include "apr_file_io.h"
#include "apr_signal.h"
#define APR_WANT_IOVEC
#include "apr_want.h"
#if APR_HAVE_UNISTD_H
#include <unistd.h>
#endif
#if APR_HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#if !APR_HAS_THREADS
#error The perchild MPM requires APR threads, but they are unavailable.
#endif
#define CORE_PRIVATE
#include "ap_config.h"
#include "httpd.h"
#include "http_main.h"
#include "http_log.h"
#include "http_config.h" /* for read_config */
#include "http_core.h" /* for get_remote_host */
#include "http_protocol.h"
#include "http_connection.h"
#include "ap_mpm.h"
#include "unixd.h"
#include "mpm_common.h"
#include "ap_listen.h"
#include "mpm_default.h"
#include "mpm.h"
#include "scoreboard.h"
#include "util_filter.h"
#include "apr_poll.h"
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#ifdef HAVE_SYS_POLL_H
#include <sys/poll.h>
#endif
/* ### should be APR-ized */
#include <grp.h>
#include <pwd.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <setjmp.h>
#ifdef HAVE_SYS_PROCESSOR_H
#include <sys/processor.h> /* for bindprocessor() */
#endif
/*
* Define some magic numbers that we use for the state of the incomming
* request. These must be < 0 so they don't collide with a file descriptor.
*/
#define AP_PERCHILD_THISCHILD -1
#define AP_PERCHILD_OTHERCHILD -2
/* Limit on the threads per process. Clients will be locked out if more than
* this * server_limit are needed.
*
* We keep this for one reason it keeps the size of the scoreboard file small
* enough that we can read the whole thing without worrying too much about
* the overhead.
*/
#ifndef DEFAULT_THREAD_LIMIT
#define DEFAULT_THREAD_LIMIT 64
#endif
/* Admin can't tune ThreadLimit beyond MAX_THREAD_LIMIT. We want
* some sort of compile-time limit to help catch typos.
*/
#ifndef MAX_THREAD_LIMIT
#define MAX_THREAD_LIMIT 20000
#endif
/* Limit on the total --- clients will be locked out if more servers than
* this are needed. It is intended solely to keep the server from crashing
* when things get out of hand.
*
* We keep a hard maximum number of servers, for two reasons --- first off,
* in case something goes seriously wrong, we want to stop the fork bomb
* short of actually crashing the machine we're running on by filling some
* kernel table. Secondly, it keeps the size of the scoreboard file small
* enough that we can read the whole thing without worrying too much about
* the overhead.
*/
#ifndef DEFAULT_SERVER_LIMIT
#define DEFAULT_SERVER_LIMIT 8
#endif
/* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT. We want
* some sort of compile-time limit to help catch typos.
*/
#ifndef MAX_SERVER_LIMIT
#define MAX_SERVER_LIMIT 20000
#endif
/*
* Actual definitions of config globals
*/
static int threads_to_start = 0; /* Worker threads per child */
static int min_spare_threads = 0;
static int max_spare_threads = 0;
static int max_threads = 0;
static int server_limit = DEFAULT_SERVER_LIMIT;
static int first_server_limit;
static int thread_limit = DEFAULT_THREAD_LIMIT;
static int first_thread_limit;
static int changed_limit_at_restart;
static int num_daemons = 0;
static int curr_child_num = 0;
static int workers_may_exit = 0;
static int requests_this_child;
static int num_listensocks = 0;
static ap_pod_t *pod;
static jmp_buf jmpbuffer;
struct child_info_t {
uid_t uid;
gid_t gid;
int input; /* The socket descriptor */
int output; /* The socket descriptor */
};
typedef struct {
const char *sockname; /* The base name for the socket */
const char *fullsockname; /* socket base name + extension */
int input; /* The socket descriptor */
int output; /* The socket descriptor */
} perchild_server_conf;
typedef struct child_info_t child_info_t;
/* Tables used to determine the user and group each child process should
* run as. The hash table is used to correlate a server name with a child
* process.
*/
static child_info_t *child_info_table;
static int *thread_socket_table;
struct ap_ctable *ap_child_table;
/*
* The max child slot ever assigned, preserved across restarts. Necessary
* to deal with NumServers changes across AP_SIG_GRACEFUL restarts. We
* use this value to optimize routines that have to scan the entire child
* table.
*
* XXX - It might not be worth keeping this code in. There aren't very
* many child processes in this MPM.
*/
int ap_max_daemons_limit = -1;
int ap_threads_per_child; /* XXX not part of API! axe it! */
module AP_MODULE_DECLARE_DATA mpm_perchild_module;
static apr_file_t *pipe_of_death_in = NULL;
static apr_file_t *pipe_of_death_out = NULL;
static apr_thread_mutex_t *pipe_of_death_mutex;
/* *Non*-shared http_main globals... */
server_rec *ap_server_conf;
/* 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;
#ifdef DEBUG_SIGSTOP
int raise_sigstop_flags;
#endif
static apr_pool_t *pconf; /* Pool for config stuff */
static apr_pool_t *pchild; /* Pool for httpd child stuff */
static apr_pool_t *thread_pool_parent; /* Parent of per-thread pools */
static apr_thread_mutex_t *thread_pool_parent_mutex;
static int child_num;
static unsigned int my_pid; /* Linux getpid() doesn't work except in
main thread. Use this instead */
/* Keep track of the number of worker threads currently active */
static int worker_thread_count;
static apr_thread_mutex_t *worker_thread_count_mutex;
static int *worker_thread_free_ids;
static apr_threadattr_t *worker_thread_attr;
/* Keep track of the number of idle worker threads */
static int idle_thread_count;
static apr_thread_mutex_t *idle_thread_count_mutex;
/* Locks for accept serialization */
#ifdef NO_SERIALIZED_ACCEPT
#define SAFE_ACCEPT(stmt) APR_SUCCESS
#else
#define SAFE_ACCEPT(stmt) (stmt)
static apr_proc_mutex_t *process_accept_mutex;
#endif /* NO_SERIALIZED_ACCEPT */
static apr_thread_mutex_t *thread_accept_mutex;
AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
{
switch(query_code){
case AP_MPMQ_MAX_DAEMON_USED:
*result = ap_max_daemons_limit;
return APR_SUCCESS;
case AP_MPMQ_IS_THREADED:
*result = AP_MPMQ_DYNAMIC;
return APR_SUCCESS;
case AP_MPMQ_IS_FORKED:
*result = AP_MPMQ_STATIC;
return APR_SUCCESS;
case AP_MPMQ_HARD_LIMIT_DAEMONS:
*result = server_limit;
return APR_SUCCESS;
case AP_MPMQ_HARD_LIMIT_THREADS:
*result = thread_limit;
return APR_SUCCESS;
case AP_MPMQ_MAX_THREADS:
*result = max_threads;
return APR_SUCCESS;
case AP_MPMQ_MIN_SPARE_DAEMONS:
*result = 0;
return APR_SUCCESS;
case AP_MPMQ_MIN_SPARE_THREADS:
*result = min_spare_threads;
return APR_SUCCESS;
case AP_MPMQ_MAX_SPARE_DAEMONS:
*result = 0;
return APR_SUCCESS;
case AP_MPMQ_MAX_SPARE_THREADS:
*result = max_spare_threads;
return APR_SUCCESS;
case AP_MPMQ_MAX_REQUESTS_DAEMON:
*result = ap_max_requests_per_child;
return APR_SUCCESS;
case AP_MPMQ_MAX_DAEMONS:
*result = num_daemons;
return APR_SUCCESS;
}
return APR_ENOTIMPL;
}
/* a clean exit from a child with proper cleanup */
static void clean_child_exit(int code)
{
if (pchild) {
apr_pool_destroy(pchild);
}
exit(code);
}
static void just_die(int sig)
{
clean_child_exit(0);
}
/*****************************************************************
* Connection structures and accounting...
*/
/* volatile just in case */
static int volatile shutdown_pending;
static int volatile restart_pending;
static int volatile is_graceful;
static int volatile child_fatal;
/* we don't currently track ap_my_generation, but mod_status
* references it so it must be defined */
ap_generation_t volatile ap_my_generation=0;
/*
* ap_start_shutdown() and ap_start_restart(), below, are a first stab at
* functions to initiate shutdown or restart without relying on signals.
* Previously this was initiated in sig_term() and restart() signal handlers,
* but we want to be able to start a shutdown/restart from other sources --
* e.g. on Win32, from the service manager. Now the service manager can
* call ap_start_shutdown() or ap_start_restart() as appropiate. Note that
* these functions can also be called by the child processes, since global
* variables are no longer used to pass on the required action to the parent.
*
* These should only be called from the parent process itself, since the
* parent process will use the shutdown_pending and restart_pending variables
* to determine whether to shutdown or restart. The child process should
* call signal_parent() directly to tell the parent to die -- this will
* cause neither of those variable to be set, which the parent will
* assume means something serious is wrong (which it will be, for the
* child to force an exit) and so do an exit anyway.
*/
static void ap_start_shutdown(void)
{
if (shutdown_pending == 1) {
/* Um, is this _probably_ not an error, if the user has
* tried to do a shutdown twice quickly, so we won't
* worry about reporting it.
*/
return;
}
shutdown_pending = 1;
}
/* do a graceful restart if graceful == 1 */
static void ap_start_restart(int graceful)
{
if (restart_pending == 1) {
/* Probably not an error - don't bother reporting it */
return;
}
restart_pending = 1;
is_graceful = graceful;
}
static void sig_term(int sig)
{
ap_start_shutdown();
}
static void restart(int sig)
{
#ifndef WIN32
ap_start_restart(sig == AP_SIG_GRACEFUL);
#else
ap_start_restart(1);
#endif
}
static void set_signals(void)
{
#ifndef NO_USE_SIGACTION
struct sigaction sa;
#endif
if (!one_process) {
ap_fatal_signal_setup(ap_server_conf, pconf);
}
#ifndef NO_USE_SIGACTION
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = sig_term;
if (sigaction(SIGTERM, &sa, NULL) < 0)
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
"sigaction(SIGTERM)");
#ifdef SIGINT
if (sigaction(SIGINT, &sa, NULL) < 0)
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
"sigaction(SIGINT)");
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -