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

📄 prefork.c

📁 Apache官方在今天放出产品系列2.2的最新版本2.2.11的源码包 最流行的HTTP服务器软件之一
💻 C
📖 第 1 页 / 共 4 页
字号:
/* 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.h"#include "apr_portable.h"#include "apr_strings.h"#include "apr_thread_proc.h"#include "apr_signal.h"#define APR_WANT_STDIO#define APR_WANT_STRFUNC#include "apr_want.h"#if APR_HAVE_UNISTD_H#include <unistd.h>#endif#if APR_HAVE_SYS_TYPES_H#include <sys/types.h>#endif#define CORE_PRIVATE#include "ap_config.h"#include "httpd.h"#include "mpm_default.h"#include "http_main.h"#include "http_log.h"#include "http_config.h"#include "http_core.h"          /* for get_remote_host */#include "http_connection.h"#include "scoreboard.h"#include "ap_mpm.h"#include "unixd.h"#include "mpm_common.h"#include "ap_listen.h"#include "ap_mmn.h"#include "apr_poll.h"#ifdef HAVE_BSTRING_H#include <bstring.h>            /* for IRIX, FD_SET calls bzero() */#endif#ifdef HAVE_TIME_H#include <time.h>#endif#ifdef HAVE_SYS_PROCESSOR_H#include <sys/processor.h> /* for bindprocessor() */#endif#include <signal.h>#include <sys/times.h>/* 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 256#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 200000#endif#ifndef HARD_THREAD_LIMIT#define HARD_THREAD_LIMIT 1#endif/* config globals */int ap_threads_per_child=0;         /* Worker threads per child */static apr_proc_mutex_t *accept_mutex;static int ap_daemons_to_start=0;static int ap_daemons_min_free=0;static int ap_daemons_max_free=0;static int ap_daemons_limit=0;      /* MaxClients */static int server_limit = DEFAULT_SERVER_LIMIT;static int first_server_limit = 0;static int changed_limit_at_restart;static int mpm_state = AP_MPMQ_STARTING;static ap_pod_t *pod;/* * 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;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;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;#ifndef MULTITHREADstatic int my_child_num;#endifap_generation_t volatile ap_my_generation=0;#ifdef TPFint tpf_child = 0;char tpf_server_name[INETD_SERVNAME_LENGTH+1];#endif /* TPF */static volatile int die_now = 0;#ifdef GPROF/* * change directory for gprof to plop the gmon.out file * configure in httpd.conf: * GprofDir $RuntimeDir/   -> $ServerRoot/$RuntimeDir/gmon.out * GprofDir $RuntimeDir/%  -> $ServerRoot/$RuntimeDir/gprof.$pid/gmon.out */static void chdir_for_gprof(void){    core_server_config *sconf =        ap_get_module_config(ap_server_conf->module_config, &core_module);    char *dir = sconf->gprof_dir;    const char *use_dir;    if(dir) {        apr_status_t res;        char *buf = NULL ;        int len = strlen(sconf->gprof_dir) - 1;        if(*(dir + len) == '%') {            dir[len] = '\0';            buf = ap_append_pid(pconf, dir, "gprof.");        }        use_dir = ap_server_root_relative(pconf, buf ? buf : dir);        res = apr_dir_make(use_dir,                           APR_UREAD | APR_UWRITE | APR_UEXECUTE |                           APR_GREAD | APR_GEXECUTE |                           APR_WREAD | APR_WEXECUTE, pconf);        if(res != APR_SUCCESS && !APR_STATUS_IS_EEXIST(res)) {            ap_log_error(APLOG_MARK, APLOG_ERR, res, ap_server_conf,                         "gprof: error creating directory %s", dir);        }    }    else {        use_dir = ap_server_root_relative(pconf, DEFAULT_REL_RUNTIMEDIR);    }    chdir(use_dir);}#else#define chdir_for_gprof()#endif/* XXX - I don't know if TPF will ever use this module or not, so leave * the ap_check_signals calls in but disable them - manoj */#define ap_check_signals()/* 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);    }    ap_mpm_pod_close(pod);    chdir_for_gprof();    exit(code);}static void accept_mutex_on(void){    apr_status_t rv = apr_proc_mutex_lock(accept_mutex);    if (rv != APR_SUCCESS) {        const char *msg = "couldn't grab the accept mutex";        if (ap_my_generation !=            ap_scoreboard_image->global->running_generation) {            ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL, "%s", msg);            clean_child_exit(0);        }        else {            ap_log_error(APLOG_MARK, APLOG_EMERG, rv, NULL, "%s", msg);            exit(APEXIT_CHILDFATAL);        }    }}static void accept_mutex_off(void){    apr_status_t rv = apr_proc_mutex_unlock(accept_mutex);    if (rv != APR_SUCCESS) {        const char *msg = "couldn't release the accept mutex";        if (ap_my_generation !=            ap_scoreboard_image->global->running_generation) {            ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL, "%s", msg);            /* don't exit here... we have a connection to             * process, after which point we'll see that the             * generation changed and we'll exit cleanly             */        }        else {            ap_log_error(APLOG_MARK, APLOG_EMERG, rv, NULL, "%s", msg);            exit(APEXIT_CHILDFATAL);        }    }}/* On some architectures it's safe to do unserialized accept()s in the single * Listen case.  But it's never safe to do it in the case where there's * multiple Listen statements.  Define SINGLE_LISTEN_UNSERIALIZED_ACCEPT * when it's safe in the single Listen case. */#ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT#define SAFE_ACCEPT(stmt) do {if (ap_listeners->next) {stmt;}} while(0)#else#define SAFE_ACCEPT(stmt) do {stmt;} while(0)#endifAP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result){    switch(query_code){    case AP_MPMQ_MAX_DAEMON_USED:        *result = ap_daemons_limit;        return APR_SUCCESS;    case AP_MPMQ_IS_THREADED:        *result = AP_MPMQ_NOT_SUPPORTED;        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 = HARD_THREAD_LIMIT;        return APR_SUCCESS;    case AP_MPMQ_MAX_THREADS:        *result = 0;        return APR_SUCCESS;    case AP_MPMQ_MIN_SPARE_DAEMONS:        *result = ap_daemons_min_free;        return APR_SUCCESS;    case AP_MPMQ_MIN_SPARE_THREADS:        *result = 0;        return APR_SUCCESS;    case AP_MPMQ_MAX_SPARE_DAEMONS:        *result = ap_daemons_max_free;        return APR_SUCCESS;    case AP_MPMQ_MAX_SPARE_THREADS:        *result = 0;        return APR_SUCCESS;    case AP_MPMQ_MAX_REQUESTS_DAEMON:        *result = ap_max_requests_per_child;        return APR_SUCCESS;    case AP_MPMQ_MAX_DAEMONS:        *result = server_limit;        return APR_SUCCESS;    case AP_MPMQ_MPM_STATE:        *result = mpm_state;        return APR_SUCCESS;    }    return APR_ENOTIMPL;}#if defined(NEED_WAITPID)/*   Systems without a real waitpid sometimes lose a child's exit while waiting   for another.  Search through the scoreboard for missing children. */int reap_children(int *exitcode, apr_exit_why_e *status){    int n, pid;    for (n = 0; n < ap_max_daemons_limit; ++n) {        if (ap_scoreboard_image->servers[n][0].status != SERVER_DEAD &&                kill((pid = ap_scoreboard_image->parent[n].pid), 0) == -1) {            ap_update_child_status_from_indexes(n, 0, SERVER_DEAD, NULL);            /* just mark it as having a successful exit status */            *status = APR_PROC_EXIT;            *exitcode = 0;            return(pid);        }    }    return 0;}#endif/***************************************************************** * Connection structures and accounting... */static void just_die(int sig){    clean_child_exit(0);}static void stop_listening(int sig){    ap_close_listeners();    /* For a graceful stop, we want the child to exit when done */    die_now = 1;}/* volatile just in case */static int volatile shutdown_pending;static int volatile restart_pending;static int volatile is_graceful;static void sig_term(int sig){    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;    is_graceful = (sig == AP_SIG_GRACEFUL_STOP);}/* restart() is the signal handler for SIGHUP and AP_SIG_GRACEFUL * in the parent process, unless running in ONE_PROCESS mode */static void restart(int sig){    if (restart_pending == 1) {        /* Probably not an error - don't bother reporting it */        return;    }    restart_pending = 1;    is_graceful = (sig == AP_SIG_GRACEFUL);}static void set_signals(void){#ifndef NO_USE_SIGACTION

⌨️ 快捷键说明

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