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

📄 threaded.c

📁 Apache V2.0.15 Alpha For Linuxhttpd-2_0_15-alpha.tar.Z
💻 C
📖 第 1 页 / 共 4 页
字号:
/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *       "This product includes software developed by the *        Apache Software Foundation (http://www.apache.org/)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written *    permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", *    nor may "Apache" appear in their name, without prior written *    permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign. */#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"#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#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 "unixd.h"#include "mpm_common.h"#include "ap_listen.h"#include "scoreboard.h" #include <signal.h>/* * Actual definitions of config globals */int ap_threads_per_child=0;         /* Worker threads per child */int ap_max_requests_per_child=0;static const char *ap_pid_fname=NULL;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 workers_may_exit = 0;static int requests_this_child;static int num_listensocks = 0;static apr_socket_t **listensocks;/* The structure used to pass unique initialization info to each thread */typedef struct {    int pid;    int tid;    int sd;    apr_pool_t *tpool; /* "pthread" would be confusing */} proc_info;/* * The max child slot ever assigned, preserved across restarts.  Necessary * to deal with MaxClients changes across SIGWINCH restarts.  We use this * value to optimize routines that have to scan the entire scoreboard. */int ap_max_daemons_limit = -1;char ap_coredump_dir[MAX_STRING_LEN];static apr_file_t *pipe_of_death_in = NULL;static apr_file_t *pipe_of_death_out = NULL;static apr_lock_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_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 *//* Keep track of the number of worker threads currently active */static int worker_thread_count;static apr_lock_t *worker_thread_count_mutex;/* Locks for accept serialization */static apr_lock_t *accept_mutex;static const char *lock_fname;#ifdef NO_SERIALIZED_ACCEPT#define SAFE_ACCEPT(stmt) APR_SUCCESS#else#define SAFE_ACCEPT(stmt) (stmt)#endifAP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result){    switch(query_code){        case AP_MPMQ_MAX_DAEMONS:            *result = ap_max_daemons_limit;            return APR_SUCCESS;        case AP_MPMQ_IS_THREADED:            *result = 1;            return APR_SUCCESS;        case AP_MPMQ_IS_FORKED:            *result = 1;            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){    if (pchild) {	apr_pool_destroy(pchild);    }    exit(code);}/* handle all varieties of core dumping signals */static void sig_coredump(int sig){    chdir(ap_coredump_dir);    apr_signal(sig, SIG_DFL);    kill(ap_my_pid, sig);    /* At this point we've got sig blocked, because we're still inside     * the signal handler.  When we leave the signal handler it will     * be unblocked, and we'll take the signal... and coredump or whatever     * is appropriate for this particular Unix.  In addition the parent     * will see the real signal we received -- whereas if we called     * abort() here, the parent would only see SIGABRT.     */}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;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 * 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;    if (is_graceful) {        apr_pool_cleanup_kill(pconf, NULL, ap_cleanup_scoreboard);    }}static void sig_term(int sig){    ap_start_shutdown();}static void restart(int sig){#ifndef WIN32    ap_start_restart(sig == SIGWINCH);#else    ap_start_restart(1);#endif}static void set_signals(void){#ifndef NO_USE_SIGACTION    struct sigaction sa;    sigemptyset(&sa.sa_mask);    sa.sa_flags = 0;    if (!one_process) {	sa.sa_handler = sig_coredump;#if defined(SA_ONESHOT)	sa.sa_flags = SA_ONESHOT;#elif defined(SA_RESETHAND)	sa.sa_flags = SA_RESETHAND;#endif	if (sigaction(SIGSEGV, &sa, NULL) < 0)	    ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGSEGV)");#ifdef SIGBUS	if (sigaction(SIGBUS, &sa, NULL) < 0)	    ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGBUS)");#endif#ifdef SIGABORT	if (sigaction(SIGABORT, &sa, NULL) < 0)	    ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABORT)");#endif#ifdef SIGABRT	if (sigaction(SIGABRT, &sa, NULL) < 0)	    ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABRT)");#endif#ifdef SIGILL	if (sigaction(SIGILL, &sa, NULL) < 0)	    ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGILL)");#endif	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#ifdef SIGXCPU    sa.sa_handler = SIG_DFL;    if (sigaction(SIGXCPU, &sa, NULL) < 0)	ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXCPU)");#endif#ifdef SIGXFSZ    sa.sa_handler = SIG_DFL;    if (sigaction(SIGXFSZ, &sa, NULL) < 0)	ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXFSZ)");#endif#ifdef SIGPIPE    sa.sa_handler = SIG_IGN;    if (sigaction(SIGPIPE, &sa, NULL) < 0)	ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGPIPE)");#endif    /* we want to ignore HUPs and WINCH while we're busy processing one */    sigaddset(&sa.sa_mask, SIGHUP);    sigaddset(&sa.sa_mask, SIGWINCH);    sa.sa_handler = restart;    if (sigaction(SIGHUP, &sa, NULL) < 0)	ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");    if (sigaction(SIGWINCH, &sa, NULL) < 0)	ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGWINCH)");

⌨️ 快捷键说明

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