📄 beos.c
字号:
/* ==================================================================== * 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. *//* The new BeOS MPM! * * This one basically is a single process multi threaded model, but * I couldn't be bothered adding the spmt_ to the front of the name! * Anyway, this is still under development so it isn't yet the default * choice. */ #define CORE_PRIVATE #include "apr_strings.h"#include "apr_portable.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 "beosd.h"#include "ap_listen.h"#include "scoreboard.h" #include <kernel/OS.h>#include "mpm_common.h"#include "mpm.h"#include <unistd.h>#include <sys/socket.h>#include <signal.h>/* * Actual definitions of config globals */int ap_threads_per_child=HARD_THREAD_LIMIT; /* Worker threads per child */int ap_max_requests_per_child=0;static const char *ap_pid_fname=NULL;static int ap_threads_to_start=0;static int min_spare_threads=0;static int max_spare_threads=0;static int ap_thread_limit=0;static int num_listening_sockets = 0; /* set by open_listeners in ap_mpm_run */static apr_socket_t ** listening_sockets;apr_lock_t *accept_mutex = NULL;static apr_pool_t *pconf; /* Pool for config stuff */static apr_pool_t *pchild; /* Pool for httpd child stuff */static int server_pid; /* Keep track of the number of worker threads currently active */static int worker_thread_count;apr_lock_t *worker_thread_count_mutex;/* The structure used to pass unique initialization info to each thread */typedef struct { int slot; apr_pool_t *tpool;} 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_child_assigned = -1;int ap_max_threads_limit = -1;char ap_coredump_dir[MAX_STRING_LEN];static apr_socket_t *udp_sock;static apr_sockaddr_t *udp_sa;/* shared http_main globals... */server_rec *ap_server_conf;/* one_process */static int one_process = 0;#ifdef DEBUG_SIGSTOPint raise_sigstop_flags;#endif/* 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); signal(sig, SIG_DFL); kill(server_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. */}/***************************************************************** * 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 = 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; 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){ ap_start_restart(sig == SIGWINCH);}static void tell_workers_to_exit(void){ apr_size_t len; int i = 0; for (i = 0 ; i < ap_max_child_assigned; i++){ len = 4; if (apr_sendto(udp_sock, udp_sa, 0, "die!", &len) != APR_SUCCESS) break; } }static void set_signals(void){ struct sigaction sa; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (!one_process) { sa.sa_handler = sig_coredump; if (sigaction(SIGSEGV, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGSEGV)"); if (sigaction(SIGBUS, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGBUS)"); if (sigaction(SIGABRT, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABRT)"); if (sigaction(SIGILL, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGILL)"); 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)"); if (sigaction(SIGINT, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)"); sa.sa_handler = SIG_IGN; if (sigaction(SIGPIPE, &sa, NULL) < 0) ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGPIPE)"); /* 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)");}/***************************************************************** * Here follows a long bunch of generic server bookkeeping stuff... */int ap_graceful_stop_signalled(void){ /* XXX - Does this really work? - Manoj */ return is_graceful;}/***************************************************************** * Child process main loop. */static void process_socket(apr_pool_t *p, apr_socket_t *sock, int my_child_num){ conn_rec *current_conn; long conn_id = my_child_num; int csd; (void)apr_os_sock_get(&csd, sock); if (csd >= FD_SETSIZE) { ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, NULL, "filedescriptor (%u) larger than FD_SETSIZE (%u) " "found, you probably need to rebuild Apache with a " "larger FD_SETSIZE", csd, FD_SETSIZE); apr_socket_close(sock); return; } current_conn = ap_new_connection(p, ap_server_conf, sock, conn_id); if (current_conn) { ap_process_connection(current_conn); ap_lingering_close(current_conn); }}static int32 worker_thread(void * dummy){ proc_info * ti = dummy; int child_slot = ti->slot; apr_pool_t *tpool = ti->tpool; apr_socket_t *csd = NULL; apr_pool_t *ptrans; /* Pool for per-transaction stuff */ apr_socket_t *sd = NULL; apr_status_t rv = APR_EINIT; int srv , n; int curr_pollfd = 0, last_pollfd = 0; sigset_t sig_mask; int requests_this_child = ap_max_requests_per_child; apr_pollfd_t *pollset; /* each worker thread is in control of it's own destiny...*/ int this_worker_should_exit = 0; free(ti); /* block the signals for this thread */ sigfillset(&sig_mask); sigprocmask(SIG_BLOCK, &sig_mask, NULL); apr_pool_create(&ptrans, tpool); apr_lock_acquire(worker_thread_count_mutex); worker_thread_count++; apr_lock_release(worker_thread_count_mutex); (void) ap_update_child_status(0, child_slot, SERVER_STARTING, (request_rec*)NULL); apr_poll_setup(&pollset, num_listening_sockets, tpool); while (1) { /* If we're here, then chances are (unless we're the first thread created) we're going to be held up on the accept_muetx, so doing this here shouldn't be a peformance hit. If it is, you probably need more threads... */ for(n=0 ; n <= num_listening_sockets ; n++) apr_poll_socket_add(pollset, listening_sockets[n], APR_POLLIN); this_worker_should_exit |= (ap_max_requests_per_child != 0) && (requests_this_child <= 0); if (this_worker_should_exit) break; (void) ap_update_child_status(0, child_slot, SERVER_READY, (request_rec*)NULL); apr_lock_acquire(accept_mutex); while (!this_worker_should_exit) { apr_int16_t event;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -