⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 worker.c

📁 apache的软件linux版本
💻 C
📖 第 1 页 / 共 5 页
字号:
/* 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. *//* The purpose of this MPM is to fix the design flaws in the threaded * model.  Because of the way that pthreads and mutex locks interact, * it is basically impossible to cleanly gracefully shutdown a child * process if multiple threads are all blocked in accept.  This model * fixes those problems. */#include "apr.h"#include "apr_portable.h"#include "apr_strings.h"#include "apr_file_io.h"#include "apr_thread_proc.h"#include "apr_signal.h"#include "apr_thread_mutex.h"#include "apr_proc_mutex.h"#include "apr_poll.h"#define APR_WANT_STRFUNC#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_HAVE_SYS_WAIT_H#include <sys/wait.h> #endif#ifdef HAVE_SYS_PROCESSOR_H#include <sys/processor.h> /* for bindprocessor() */#endif#if !APR_HAS_THREADS#error The Worker 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_connection.h"#include "ap_mpm.h"#include "pod.h"#include "mpm_common.h"#include "ap_listen.h"#include "scoreboard.h" #include "fdqueue.h"#include "mpm_default.h"#include <signal.h>#include <limits.h>             /* for INT_MAX *//* 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 16#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/* 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/* * Actual definitions of config globals */int ap_threads_per_child = 0;         /* Worker threads per child */static int ap_daemons_to_start = 0;static int min_spare_threads = 0;static int max_spare_threads = 0;static int ap_daemons_limit = 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 dying = 0;static int workers_may_exit = 0;static int start_thread_may_exit = 0;static int listener_may_exit = 0;static int requests_this_child;static int num_listensocks = 0;static int resource_shortage = 0;static fd_queue_t *worker_queue;static fd_queue_info_t *worker_queue_info;static int mpm_state = AP_MPMQ_STARTING;static int sick_child_detected;/* The structure used to pass unique initialization info to each thread */typedef struct {    int pid;    int tid;    int sd;} proc_info;/* Structure used to pass information to the thread responsible for  * creating the rest of the threads. */typedef struct {    apr_thread_t **threads;    apr_thread_t *listener;    int child_num_arg;    apr_threadattr_t *threadattr;} thread_starter;#define ID_FROM_CHILD_THREAD(c, t)    ((c * thread_limit) + t)/* * The max child slot ever assigned, preserved across restarts.  Necessary * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts.  We  * use this value to optimize routines that have to scan the entire  * scoreboard. */int ap_max_daemons_limit = -1;static ap_pod_t *pod;/* *Non*-shared http_main globals... */server_rec *ap_server_conf;/* The worker MPM respects a couple of runtime flags that can aid * in debugging. Setting the -DNO_DETACH flag will prevent the root process * from detaching from its controlling terminal. Additionally, setting * the -DONE_PROCESS flag (which implies -DNO_DETACH) will get you the * child_main loop running in the process which originally started up. * This gives you 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_SIGSTOPint raise_sigstop_flags;#endifstatic apr_pool_t *pconf;                 /* Pool for config stuff */static apr_pool_t *pchild;                /* Pool for httpd child stuff */static pid_t ap_my_pid; /* Linux getpid() doesn't work except in main                            thread. Use this instead */static pid_t parent_pid;static apr_os_thread_t *listener_os_thread;/* Locks for accept serialization */static apr_proc_mutex_t *accept_mutex;#ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT#define SAFE_ACCEPT(stmt) (ap_listeners->next ? (stmt) : APR_SUCCESS)#else#define SAFE_ACCEPT(stmt) (stmt)#endif/* The LISTENER_SIGNAL signal will be sent from the main thread to the  * listener thread to wake it up for graceful termination (what a child  * process from an old generation does when the admin does "apachectl  * graceful").  This signal will be blocked in all threads of a child * process except for the listener thread. */#define LISTENER_SIGNAL     SIGHUP/* An array of socket descriptors in use by each thread used to * perform a non-graceful (forced) shutdown of the server. */static apr_socket_t **worker_sockets;static void close_worker_sockets(void){    int i;    for (i = 0; i < ap_threads_per_child; i++) {        if (worker_sockets[i]) {            apr_socket_close(worker_sockets[i]);            worker_sockets[i] = NULL;        }    }}  static void wakeup_listener(void){    listener_may_exit = 1;    if (!listener_os_thread) {        /* XXX there is an obscure path that this doesn't handle perfectly:         *     right after listener thread is created but before          *     listener_os_thread is set, the first worker thread hits an         *     error and starts graceful termination         */        return;    }    /*     * we should just be able to "kill(ap_my_pid, LISTENER_SIGNAL)" on all     * platforms and wake up the listener thread since it is the only thread      * with SIGHUP unblocked, but that doesn't work on Linux     */#ifdef HAVE_PTHREAD_KILL    pthread_kill(*listener_os_thread, LISTENER_SIGNAL);#else    kill(ap_my_pid, LISTENER_SIGNAL);#endif}#define ST_INIT              0#define ST_GRACEFUL          1#define ST_UNGRACEFUL        2static int terminate_mode = ST_INIT;static void signal_threads(int mode){    if (terminate_mode == mode) {        return;    }    terminate_mode = mode;    mpm_state = AP_MPMQ_STOPPING;    /* in case we weren't called from the listener thread, wake up the     * listener thread     */    wakeup_listener();    /* for ungraceful termination, let the workers exit now;     * for graceful termination, the listener thread will notify the     * workers to exit once it has stopped accepting new connections     */    if (mode == ST_UNGRACEFUL) {        workers_may_exit = 1;        ap_queue_interrupt_all(worker_queue);        ap_queue_info_term(worker_queue_info);        close_worker_sockets(); /* forcefully kill all current connections */    }}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_STATIC;            return APR_SUCCESS;        case AP_MPMQ_IS_FORKED:            *result = AP_MPMQ_DYNAMIC;            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 = ap_threads_per_child;            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 = ap_daemons_limit;            return APR_SUCCESS;        case AP_MPMQ_MPM_STATE:            *result = mpm_state;            return APR_SUCCESS;    }    return APR_ENOTIMPL;}/* a clean exit from a child with proper cleanup */ static void clean_child_exit(int code) __attribute__ ((noreturn));static void clean_child_exit(int code){    mpm_state = AP_MPMQ_STOPPING;    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 volatile int child_fatal;ap_generation_t volatile ap_my_generation;/* * 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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -