mpm_winnt.c revision 0375f54d32af9c27de0006956d9f85d01b020351
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* ====================================================================
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The Apache Software License, Version 1.1
bc8fd1b0b1afdf89b8d28eefa8cd74e26ba97986fielding *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Copyright (c) 2000 The Apache Software Foundation. All rights
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * reserved.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Redistribution and use in source and binary forms, with or without
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * modification, are permitted provided that the following conditions
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * are met:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 1. Redistributions of source code must retain the above copyright
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * notice, this list of conditions and the following disclaimer.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 2. Redistributions in binary form must reproduce the above copyright
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * notice, this list of conditions and the following disclaimer in
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * the documentation and/or other materials provided with the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * distribution.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 3. The end-user documentation included with the redistribution,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * if any, must include the following acknowledgment:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * "This product includes software developed by the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Apache Software Foundation (http://www.apache.org/)."
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Alternately, this acknowledgment may appear in the software itself,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * if and wherever such third-party acknowledgments normally appear.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 4. The names "Apache" and "Apache Software Foundation" must
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * not be used to endorse or promote products derived from this
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * software without prior written permission. For written
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * permission, please contact apache@apache.org.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 5. Products derived from this software may not be called "Apache",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * nor may "Apache" appear in their name, without prior written
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * permission of the Apache Software Foundation.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * SUCH DAMAGE.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ====================================================================
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * This software consists of voluntary contributions made by many
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * individuals on behalf of the Apache Software Foundation. For more
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * information on the Apache Software Foundation, please see
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * <http://www.apache.org/>.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Portions of this software are based upon public domain software
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * originally written at the National Center for Supercomputing Applications,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * University of Illinois, Urbana-Champaign.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#define CORE_PRIVATE
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "httpd.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "http_main.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "http_log.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "http_config.h" /* for read_config */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "http_core.h" /* for get_remote_host */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "http_connection.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "apr_portable.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "apr_getopt.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "apr_strings.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "ap_mpm.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "ap_config.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "ap_listen.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "mpm_default.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "mpm_winnt.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "mpm_common.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "scoreboard.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbtypedef HANDLE thread;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/*
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Definitions of WINNT MPM specific config globals
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int workers_may_exit = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int shutdown_in_progress = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic unsigned int g_blocked_threads = 0;
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic char *ap_pid_fname = NULL;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbint ap_threads_per_child = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int max_requests_per_child = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic HANDLE shutdown_event; /* used to signal shutdown to parent */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic HANDLE restart_event; /* used to signal a restart to parent */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#define MAX_SIGNAL_NAME 30 /* Long enough for apPID_shutdown, where PID is an int */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzchar signal_name_prefix[MAX_SIGNAL_NAME];
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzchar signal_restart_name[MAX_SIGNAL_NAME];
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzchar signal_shutdown_name[MAX_SIGNAL_NAME];
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzstatic struct fd_set listenfds;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzstatic int num_listenfds = 0;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzstatic SOCKET listenmaxfd = INVALID_SOCKET;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzstatic apr_pool_t *pconf; /* Pool for config stuff */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic char ap_coredump_dir[MAX_STRING_LEN];
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerstatic server_rec *server_conf;
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowestatic HANDLE AcceptExCompPort = NULL;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int one_process = 0;
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerstatic char const* signal_arg;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerOSVERSIONINFO osver; /* VER_PLATFORM_WIN32_NT */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerint ap_max_requests_per_child=0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbint ap_daemons_to_start=0;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic HANDLE exit_event;
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerstatic HANDLE maintenance_event;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbapr_lock_t *start_mutex;
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerDWORD my_pid;
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerDWORD parent_pid;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe/* ap_get_max_daemons and ap_my_generation are used by the scoreboard
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * code
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewroweap_generation_t volatile ap_my_generation=0; /* Used by the scoreboard */
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawickAP_DECLARE(int) ap_get_max_daemons(void)
c2cf53a40a9814eb91db2cdf820f97d943f21628coar{
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe return 1;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe}
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe/* This is the helper code to resolve late bound entry points
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * missing from one or more releases of the Win32 API...
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * but it sure would be nice if we didn't duplicate this code
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * from the APR ;-)
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowestatic const char* const lateDllName[DLL_defined] = {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe "kernel32", "advapi32", "mswsock", "ws2_32" };
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowestatic HMODULE lateDllHandle[DLL_defined] = {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe NULL, NULL, NULL, NULL };
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
3e392a5afd51526de3cb15d57ee46d8cb160ae65gregamesFARPROC ap_load_dll_func(ap_dlltoken_e fnLib, char* fnName, int ordinal)
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe{
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (!lateDllHandle[fnLib]) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe lateDllHandle[fnLib] = LoadLibrary(lateDllName[fnLib]);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (!lateDllHandle[fnLib])
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz return NULL;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz }
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz if (ordinal)
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz return GetProcAddress(lateDllHandle[fnLib], (char *) ordinal);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe else
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe return GetProcAddress(lateDllHandle[fnLib], fnName);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe}
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerstatic apr_status_t socket_cleanup(void *sock)
4f9c22c4f27571d54197be9674e1fc0d528192aestriker{
3e392a5afd51526de3cb15d57ee46d8cb160ae65gregames apr_socket_t *thesocket = sock;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe SOCKET sd;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar if (apr_get_os_sock(&sd, thesocket) == APR_SUCCESS) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe closesocket(sd);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe return APR_SUCCESS;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe}
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe/* A bunch or routines from os/win32/multithread.c that need to be merged into APR
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * or thrown out entirely...
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerstatic HANDLE create_semaphore(int initial)
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe{
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe return(CreateSemaphore(NULL, initial, 1000000, NULL));
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe}
c2cf53a40a9814eb91db2cdf820f97d943f21628coar
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowestatic void acquire_semaphore(HANDLE semaphore_id)
4775dfc34c90fada8c7c4d6a57ed8a3114d55c2dtrawick{
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe int rv;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe rv = WaitForSingleObject(semaphore_id, INFINITE);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar return;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe}
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowestatic int release_semaphore(HANDLE semaphore_id)
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe{
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe return(ReleaseSemaphore(semaphore_id, 1, NULL));
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe}
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowestatic void destroy_semaphore(HANDLE semaphore_id)
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe{
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe CloseHandle(semaphore_id);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe}
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe/* To share the semaphores with other processes, we need a NULL ACL
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * Code from MS KB Q106387
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowestatic PSECURITY_ATTRIBUTES GetNullACL()
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe{
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe PSECURITY_DESCRIPTOR pSD;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe PSECURITY_ATTRIBUTES sa;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe sa = (PSECURITY_ATTRIBUTES) LocalAlloc(LPTR, sizeof(SECURITY_ATTRIBUTES));
4f9c22c4f27571d54197be9674e1fc0d528192aestriker pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe SECURITY_DESCRIPTOR_MIN_LENGTH);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (pSD == NULL || sa == NULL) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe return NULL;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar apr_set_os_error(0);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe || apr_get_os_error()) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe LocalFree( pSD );
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe LocalFree( sa );
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe return NULL;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (!SetSecurityDescriptorDacl(pSD, TRUE, (PACL) NULL, FALSE)
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe || apr_get_os_error()) {
c2cf53a40a9814eb91db2cdf820f97d943f21628coar LocalFree( pSD );
c2cf53a40a9814eb91db2cdf820f97d943f21628coar LocalFree( sa );
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe return NULL;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz }
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz sa->nLength = sizeof(sa);
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz sa->lpSecurityDescriptor = pSD;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe sa->bInheritHandle = TRUE;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker return sa;
c2cf53a40a9814eb91db2cdf820f97d943f21628coar}
c2cf53a40a9814eb91db2cdf820f97d943f21628coar
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowestatic void CleanNullACL( void *sa ) {
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz if( sa ) {
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz LocalFree( ((PSECURITY_ATTRIBUTES)sa)->lpSecurityDescriptor);
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz LocalFree( sa );
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe}
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker/*
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * The Win32 call WaitForMultipleObjects will only allow you to wait for
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * a maximum of MAXIMUM_WAIT_OBJECTS (current 64). Since the threading
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * model in the multithreaded version of apache wants to use this call,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * we are restricted to a maximum of 64 threads. This is a simplistic
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * routine that will increase this size.
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzstatic DWORD wait_for_many_objects(DWORD nCount, CONST HANDLE *lpHandles,
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz DWORD dwSeconds)
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe{
4f9c22c4f27571d54197be9674e1fc0d528192aestriker time_t tStopTime;
c2cf53a40a9814eb91db2cdf820f97d943f21628coar DWORD dwRet = WAIT_TIMEOUT;
c2cf53a40a9814eb91db2cdf820f97d943f21628coar DWORD dwIndex=0;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe BOOL bFirst = TRUE;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz tStopTime = time(NULL) + dwSeconds;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe do {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (!bFirst)
c2cf53a40a9814eb91db2cdf820f97d943f21628coar Sleep(1000);
c2cf53a40a9814eb91db2cdf820f97d943f21628coar else
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe bFirst = FALSE;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz for (dwIndex = 0; dwIndex * MAXIMUM_WAIT_OBJECTS < nCount; dwIndex++) {
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz dwRet = WaitForMultipleObjects(
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe min(MAXIMUM_WAIT_OBJECTS, nCount - (dwIndex * MAXIMUM_WAIT_OBJECTS)),
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe lpHandles + (dwIndex * MAXIMUM_WAIT_OBJECTS),
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe 0, 0);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (dwRet != WAIT_TIMEOUT) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe break;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe }
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe } while((time(NULL) < tStopTime) && (dwRet == WAIT_TIMEOUT));
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz return dwRet;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe}
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe/*
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * Signalling Apache on NT.
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe *
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * Under Unix, Apache can be told to shutdown or restart by sending various
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * signals (HUP, USR, TERM). On NT we don't have easy access to signals, so
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * we use "events" instead. The parent apache process goes into a loop
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * where it waits forever for a set of events. Two of those events are
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * called
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe *
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * apPID_shutdown
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * apPID_restart
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe *
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * (where PID is the PID of the apache parent process). When one of these
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * is signalled, the Apache parent performs the appropriate action. The events
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * can become signalled through internal Apache methods (e.g. if the child
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * finds a fatal error and needs to kill its parent), via the service
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * control manager (the control thread will signal the shutdown event when
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * requested to stop the Apache service), from the -k Apache command line,
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * or from any external program which finds the Apache PID from the
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * httpd.pid file.
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe *
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * The signal_parent() function, below, is used to signal one of these events.
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * It can be called by any child or parent process, since it does not
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * rely on global variables.
4f9c22c4f27571d54197be9674e1fc0d528192aestriker *
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * On entry, type gives the event to signal. 0 means shutdown, 1 means
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * graceful restart.
4f9c22c4f27571d54197be9674e1fc0d528192aestriker */
4f9c22c4f27571d54197be9674e1fc0d528192aestrikervoid signal_parent(int type)
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe{
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe HANDLE e;
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp char *signal_name;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe /* after updating the shutdown_pending or restart flags, we need
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp * to wake up the parent process so it can see the changes. The
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * parent will normally be waiting for either a child process
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * to die, or for a signal on the "spache-signal" event. So set the
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * "apache-signal" event here.
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (one_process) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe return;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe }
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp switch(type) {
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp case 0: signal_name = signal_shutdown_name; break;
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp case 1: signal_name = signal_restart_name; break;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker default: return;
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp }
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp e = OpenEvent(EVENT_ALL_ACCESS, FALSE, signal_name);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (!e) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* Um, problem, can't signal the parent, which means we can't
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * signal ourselves to die. Ignore for now...
4f9c22c4f27571d54197be9674e1fc0d528192aestriker */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_EMERG, apr_get_os_error(), server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "OpenEvent on %s event", signal_name);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker return;
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp }
7a2edaa0193cbb0d79a65a8461a609a9402aea49brianp if (SetEvent(e) == 0) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe /* Same problem as above */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe ap_log_error(APLOG_MARK, APLOG_EMERG, apr_get_os_error(), server_conf,
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe "SetEvent on %s event", signal_name);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe CloseHandle(e);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe return;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker CloseHandle(e);
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp}
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowestatic int volatile is_graceful = 0;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe
cc9582e53aead2a044077c4a92f3dfc3605590b3wroweAP_DECLARE(int) ap_graceful_stop_signalled(void)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb{
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return is_graceful;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb}
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbAP_DECLARE(void) ap_start_shutdown(void)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb{
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb signal_parent(0);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb}
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbAP_DECLARE(void) ap_start_restart(int gracefully)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb{
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb is_graceful = gracefully;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb signal_parent(1);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb}
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/*
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Initialise the signal names, in the global variables signal_name_prefix,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * signal_restart_name and signal_shutdown_name.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbvoid setup_signal_names(char *prefix)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb{
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_snprintf(signal_name_prefix, sizeof(signal_name_prefix), prefix);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_snprintf(signal_shutdown_name, sizeof(signal_shutdown_name),
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "%s_shutdown", signal_name_prefix);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_snprintf(signal_restart_name, sizeof(signal_restart_name),
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "%s_restart", signal_name_prefix);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb}
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawick/*
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * Routines that deal with sockets, some are WIN32 specific...
c2cf53a40a9814eb91db2cdf820f97d943f21628coar */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic void sock_disable_nagle(int s)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb{
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* The Nagle algorithm says that we should delay sending partial
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * packets in hopes of getting more data. We don't want to do
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * this; we are not telnet. There are bad interactions between
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * persistent connections and Nagle's algorithm that have very severe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * performance penalties. (Failing to disable Nagle is not much of a
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * problem with simple HTTP.)
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe *
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * In spite of these problems, failure here is not a shooting offense.
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe int just_say_no = 1;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *) &just_say_no,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe sizeof(int)) < 0) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, server_conf,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe "setsockopt: (TCP_NODELAY)");
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe }
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe}
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz/*
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * Routines to deal with managing the list of listening sockets.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowestatic ap_listen_rec *head_listener;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowestatic apr_inline ap_listen_rec *find_ready_listener(fd_set * main_fds)
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz{
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz ap_listen_rec *lr;
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz SOCKET nsd;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
659ad814f714e556bdd03e1d771cba156baab92ewrowe for (lr = head_listener; lr ; lr = lr->next) {
659ad814f714e556bdd03e1d771cba156baab92ewrowe apr_get_os_sock(&nsd, lr->sd);
659ad814f714e556bdd03e1d771cba156baab92ewrowe if (FD_ISSET(nsd, main_fds)) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe head_listener = lr->next;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar if (head_listener == NULL)
11fb2f3611e6ff9a541e10b13e3108934f828141gregames head_listener = ap_listeners;
11fb2f3611e6ff9a541e10b13e3108934f828141gregames
11fb2f3611e6ff9a541e10b13e3108934f828141gregames return (lr);
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz }
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz }
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz return NULL;
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz}
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowestatic int setup_listeners(server_rec *s)
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe{
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ap_listen_rec *lr;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker int num_listeners = 0;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe SOCKET nsd;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* Setup the listeners */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar FD_ZERO(&listenfds);
2f1949bb0e3c209db94c8d521cba7380b9d11421trawick
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar if (ap_listen_open(s->process, s->port)) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe return 0;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar for (lr = ap_listeners; lr; lr = lr->next) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe num_listeners++;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (lr->sd != NULL) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_get_os_sock(&nsd, lr->sd);
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe FD_SET(nsd, &listenfds);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar if (listenmaxfd == INVALID_SOCKET || nsd > listenmaxfd) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe listenmaxfd = nsd;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe }
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar lr->count = 0;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe }
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe head_listener = ap_listeners;
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz return num_listeners;
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz}
7c301a1818939f85da8f3629cc3e9b5588610ef0jerenkrantz
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerstatic int setup_inherited_listeners(server_rec *s)
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe{
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe WSAPROTOCOL_INFO WSAProtocolInfo;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb HANDLE pipe;
8aefbd756763807188d2e3ce336a8680e4893066wrowe ap_listen_rec *lr;
8aefbd756763807188d2e3ce336a8680e4893066wrowe DWORD BytesRead;
8aefbd756763807188d2e3ce336a8680e4893066wrowe int num_listeners = 0;
8aefbd756763807188d2e3ce336a8680e4893066wrowe SOCKET nsd;
8aefbd756763807188d2e3ce336a8680e4893066wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Setup the listeners */
8aefbd756763807188d2e3ce336a8680e4893066wrowe FD_ZERO(&listenfds);
8aefbd756763807188d2e3ce336a8680e4893066wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Set up a default listener if necessary */
8aefbd756763807188d2e3ce336a8680e4893066wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (ap_listeners == NULL) {
8aefbd756763807188d2e3ce336a8680e4893066wrowe ap_listen_rec *lr;
8aefbd756763807188d2e3ce336a8680e4893066wrowe lr = apr_palloc(s->process->pool, sizeof(ap_listen_rec));
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (!lr)
8aefbd756763807188d2e3ce336a8680e4893066wrowe return 0;
8aefbd756763807188d2e3ce336a8680e4893066wrowe lr->sd = NULL;
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe lr->next = ap_listeners;
8aefbd756763807188d2e3ce336a8680e4893066wrowe ap_listeners = lr;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
8aefbd756763807188d2e3ce336a8680e4893066wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Open the pipe to the parent process to receive the inherited socket
2fa5b5878e7567e2875807c3e2a2b3b0d3ef74bewrowe * data. The sockets have been set to listening in the parent process.
2fa5b5878e7567e2875807c3e2a2b3b0d3ef74bewrowe */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe pipe = GetStdHandle(STD_INPUT_HANDLE);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe for (lr = ap_listeners; lr; lr = lr->next) {
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (!ReadFile(pipe, &WSAProtocolInfo, sizeof(WSAPROTOCOL_INFO),
8aefbd756763807188d2e3ce336a8680e4893066wrowe &BytesRead, (LPOVERLAPPED) NULL)) {
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
a8d11d78181478da6a672f7fbc58b8d523351f49wrowe "setup_inherited_listeners: Unable to read socket data from parent");
4f9c22c4f27571d54197be9674e1fc0d528192aestriker signal_parent(0); /* tell parent to die */
23c6309e36a63b13b61c35999c978017521993d6wrowe exit(1);
23c6309e36a63b13b61c35999c978017521993d6wrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf,
23c6309e36a63b13b61c35999c978017521993d6wrowe "Child %d: setup_inherited_listener() read = %d bytes of WSAProtocolInfo.", my_pid);
8aefbd756763807188d2e3ce336a8680e4893066wrowe nsd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
23c6309e36a63b13b61c35999c978017521993d6wrowe &WSAProtocolInfo, 0, 0);
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawick if (nsd == INVALID_SOCKET) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_netos_error(), server_conf,
23c6309e36a63b13b61c35999c978017521993d6wrowe "Child %d: setup_inherited_listeners(), WSASocket failed to open the inherited socket.", my_pid);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe signal_parent(0); /* tell parent to die */
8aefbd756763807188d2e3ce336a8680e4893066wrowe exit(1);
8aefbd756763807188d2e3ce336a8680e4893066wrowe }
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (nsd >= 0) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe FD_SET(nsd, &listenfds);
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (listenmaxfd == INVALID_SOCKET || nsd > listenmaxfd) {
8aefbd756763807188d2e3ce336a8680e4893066wrowe listenmaxfd = nsd;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe apr_put_os_sock(&lr->sd, &nsd, pconf);
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawick lr->count = 0;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* Now, read the AcceptExCompPort from the parent */
a8d11d78181478da6a672f7fbc58b8d523351f49wrowe ReadFile(pipe, &AcceptExCompPort, sizeof(AcceptExCompPort), &BytesRead, (LPOVERLAPPED) NULL);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe CloseHandle(pipe);
8aefbd756763807188d2e3ce336a8680e4893066wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe for (lr = ap_listeners; lr; lr = lr->next) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe num_listeners++;
cf6ef072483172309861d06e85b1aeff4573c060wrowe }
cf6ef072483172309861d06e85b1aeff4573c060wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe head_listener = ap_listeners;
cf6ef072483172309861d06e85b1aeff4573c060wrowe return num_listeners;
cf6ef072483172309861d06e85b1aeff4573c060wrowe}
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp
cf6ef072483172309861d06e85b1aeff4573c060wrowestatic void bind_listeners_to_completion_port()
4f9c22c4f27571d54197be9674e1fc0d528192aestriker{
cf6ef072483172309861d06e85b1aeff4573c060wrowe /* Associate the open listeners with the completion port.
cf6ef072483172309861d06e85b1aeff4573c060wrowe * Bypass the operation for Windows 95/98
cf6ef072483172309861d06e85b1aeff4573c060wrowe */
cf6ef072483172309861d06e85b1aeff4573c060wrowe ap_listen_rec *lr;
cf6ef072483172309861d06e85b1aeff4573c060wrowe
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe for (lr = ap_listeners; lr; lr = lr->next) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe int nsd;
cf6ef072483172309861d06e85b1aeff4573c060wrowe apr_get_os_sock(&nsd,lr->sd);
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe CreateIoCompletionPort((HANDLE) nsd, AcceptExCompPort, 0, 0);
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe }
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick }
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick}
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick/**********************************************************************
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * Multithreaded implementation
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick *
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * This code is fairly specific to Win32.
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick *
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * The model used to handle requests is a set of threads. One "main"
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * thread listens for new requests. When something becomes
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * available, it does a select and places the newly available socket
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * onto a list of "jobs" (add_job()). Then any one of a fixed number
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * of "worker" threads takes the top job off the job list with
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * remove_job() and handles that connection to completion. After
cf6ef072483172309861d06e85b1aeff4573c060wrowe * the connection has finished the thread is free to take another
cf6ef072483172309861d06e85b1aeff4573c060wrowe * job from the job list.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar *
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * In the code, the "main" thread is running within the child_main()
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * function. The first thing this function does is create the
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * worker threads, which operate in the child_sub_main() function. The
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * main thread then goes into a loop within child_main() where they
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * do a select() on the listening sockets. The select times out once
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * per second so that the thread can check for an "exit" signal
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * from the parent process (see below). If this signal is set, the
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * thread can exit, but only after it has accepted all incoming
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * connections already in the listen queue (since Win32 appears
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * to through away listened but unaccepted connections when a
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * process dies).
23c6309e36a63b13b61c35999c978017521993d6wrowe *
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * Because the main and worker threads exist within a single process
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * they are vulnerable to crashes or memory leaks (crashes can also
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * be caused within modules, of course). There also needs to be a
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * mechanism to perform restarts and shutdowns. This is done by
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * creating the main & worker threads within a subprocess. A
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * main process (the "parent process") creates one (or more)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * processes to do the work, then the parent sits around waiting
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * for the working process to die, in which case it starts a new
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * one. The parent process also handles restarts (by creating
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * a new working process then signalling the previous working process
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * exit ) and shutdowns (by signalling the working process to exit).
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * The parent process operates within the master_main() function. This
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * process also handles requests from the service manager (NT only).
a9a4544168a37b43bd180b3703ccee995f27a80awrowe *
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * Signalling between the parent and working process uses a Win32
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * event. Each child has a unique name for the event, which is
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * passed to it with the -Z argument when the child is spawned. The
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * parent sets (signals) this event to tell the child to die.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * At present all children do a graceful die - they finish all
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * current jobs _and_ empty the listen queue before they exit.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * A non-graceful die would need a second event. The -Z argument in
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * the child is also used to create the shutdown and restart events,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * since the prefix (apPID) contains the parent process PID.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe *
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * The code below starts with functions at the lowest level -
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * worker threads, and works up to the top level - the main()
8aefbd756763807188d2e3ce336a8680e4893066wrowe * function of the parent process.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe *
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * The scoreboard (in process memory) contains details of the worker
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * threads (within the active working process). There is no shared
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * "scoreboard" between processes, since only one is ever active
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * at once (or at most, two, when one has been told to shutdown but
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * is processes outstanding requests, and a new one has been started).
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * This is controlled by a "start_mutex" which ensures only one working
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * process is active at once.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe **********************************************************************/
cf6ef072483172309861d06e85b1aeff4573c060wrowe
cf6ef072483172309861d06e85b1aeff4573c060wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe/*
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * Definition of jobs, shared by main and worker threads.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe */
8aefbd756763807188d2e3ce336a8680e4893066wrowe
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantztypedef struct joblist_s {
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz struct joblist_s *next;
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz int sock;
cf6ef072483172309861d06e85b1aeff4573c060wrowe} joblist;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe/*
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * Globals common to main and worker threads. This structure is not
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * used by the parent process.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowetypedef struct globals_s {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe HANDLE jobsemaphore;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe joblist *jobhead;
8aefbd756763807188d2e3ce336a8680e4893066wrowe joblist *jobtail;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe apr_lock_t *jobmutex;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe int jobcount;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe} globals;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
8aefbd756763807188d2e3ce336a8680e4893066wroweglobals allowed_globals =
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe{NULL, NULL, NULL, NULL, 0};
4f9c22c4f27571d54197be9674e1fc0d528192aestriker#define MAX_SELECT_ERRORS 100
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe#define PADDED_ADDR_SIZE sizeof(SOCKADDR_IN)+16
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe/* Windows 9x specific code...
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * Accept processing for on Windows 95/98 uses a producer/consumer queue
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * model. A single thread accepts connections and queues the accepted socket
59513b1275fdc2021d4949ee03ae8229469abb86wrowe * to the accept queue for consumption by a pool of worker threads.
4f9c22c4f27571d54197be9674e1fc0d528192aestriker *
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * win9x_get_connection()
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * Calls remove_job() to pull a job from the accept queue. All the worker
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * threads block on remove_job.
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * accept_and_queue_connections()
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * The accept threads runs this function, which accepts connections off
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * the network and calls add_job() to queue jobs to the accept_queue.
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * add_job()/remove_job()
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * Add or remove an accepted socket from the list of sockets
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * connected to clients. allowed_globals.jobmutex protects
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * against multiple concurrent access to the linked list of jobs.
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe */
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerstatic void add_job(int sock)
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe{
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe joblist *new_job;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe new_job = (joblist *) malloc(sizeof(joblist));
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe if (new_job == NULL) {
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
cf6ef072483172309861d06e85b1aeff4573c060wrowe "Ouch! Out of memory in add_job()!");
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe return;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker new_job->next = NULL;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe new_job->sock = sock;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_lock(allowed_globals.jobmutex);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe if (allowed_globals.jobtail != NULL)
4f9c22c4f27571d54197be9674e1fc0d528192aestriker allowed_globals.jobtail->next = new_job;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe allowed_globals.jobtail = new_job;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (!allowed_globals.jobhead)
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe allowed_globals.jobhead = new_job;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker allowed_globals.jobcount++;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker release_semaphore(allowed_globals.jobsemaphore);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe apr_unlock(allowed_globals.jobmutex);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe}
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowestatic int remove_job(void)
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe{
4f9c22c4f27571d54197be9674e1fc0d528192aestriker joblist *job;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe int sock;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe acquire_semaphore(allowed_globals.jobsemaphore);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe apr_lock(allowed_globals.jobmutex);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe if (shutdown_in_progress && !allowed_globals.jobhead) {
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe apr_unlock(allowed_globals.jobmutex);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe return (-1);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe }
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe job = allowed_globals.jobhead;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe ap_assert(job);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe allowed_globals.jobhead = job->next;
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (allowed_globals.jobhead == NULL)
4f9c22c4f27571d54197be9674e1fc0d528192aestriker allowed_globals.jobtail = NULL;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_unlock(allowed_globals.jobmutex);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe sock = job->sock;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe free(job);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe return (sock);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe}
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowestatic void accept_and_queue_connections(void * dummy)
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe{
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe int requests_this_child = 0;
38bcc87d9a06e8ba81165421403f275eca4e313btrawick struct timeval tv;
38bcc87d9a06e8ba81165421403f275eca4e313btrawick fd_set main_fds;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe int wait_time = 1;
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz int csd;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe int nsd = INVALID_SOCKET;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe struct sockaddr_in sa_client;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe int count_select_errors = 0;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe int rc;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe int clen;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker while (!shutdown_in_progress) {
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe if (ap_max_requests_per_child && (requests_this_child > ap_max_requests_per_child)) {
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe break;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe }
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe tv.tv_sec = wait_time;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe tv.tv_usec = 0;
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz memcpy(&main_fds, &listenfds, sizeof(fd_set));
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz rc = select(listenmaxfd + 1, &main_fds, NULL, NULL, &tv);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (rc == 0 || (rc == SOCKET_ERROR && APR_STATUS_IS_EINTR(apr_get_netos_error()))) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe count_select_errors = 0; /* reset count of errors */
8aefbd756763807188d2e3ce336a8680e4893066wrowe continue;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe else if (rc == SOCKET_ERROR) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* A "real" error occurred, log it and increment the count of
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * select errors. This count is used to ensure we don't go into
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * a busy loop of continuous errors.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_INFO, apr_get_netos_error(), server_conf,
cf6ef072483172309861d06e85b1aeff4573c060wrowe "select failed with error %d", apr_get_netos_error());
cf6ef072483172309861d06e85b1aeff4573c060wrowe count_select_errors++;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (count_select_errors > MAX_SELECT_ERRORS) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker shutdown_in_progress = 1;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_netos_error(), server_conf,
cf6ef072483172309861d06e85b1aeff4573c060wrowe "Too many errors in select loop. Child process exiting.");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe break;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe } else {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_listen_rec *lr;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz
4f9c22c4f27571d54197be9674e1fc0d528192aestriker lr = find_ready_listener(&main_fds);
548b2980e83f609186a76e98fb245d02e8547bc3jerenkrantz if (lr != NULL) {
548b2980e83f609186a76e98fb245d02e8547bc3jerenkrantz /* fetch the native socket descriptor */
4fca95918a9c0ae93593806544b425d0adc2fcc3wrowe apr_get_os_sock(&nsd, lr->sd);
548b2980e83f609186a76e98fb245d02e8547bc3jerenkrantz }
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz }
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz do {
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar clen = sizeof(sa_client);
8aefbd756763807188d2e3ce336a8680e4893066wrowe csd = accept(nsd, (struct sockaddr *) &sa_client, &clen);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (csd == INVALID_SOCKET) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe csd = -1;
4fca95918a9c0ae93593806544b425d0adc2fcc3wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe } while (csd < 0 && APR_STATUS_IS_EINTR(apr_get_netos_error()));
8aefbd756763807188d2e3ce336a8680e4893066wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (csd < 0) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (APR_STATUS_IS_ECONNABORTED(apr_get_netos_error())) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_netos_error(), server_conf,
8aefbd756763807188d2e3ce336a8680e4893066wrowe "accept: (client socket)");
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe else {
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar add_job(csd);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe requests_this_child++;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
8aefbd756763807188d2e3ce336a8680e4893066wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe SetEvent(exit_event);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker}
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowestatic PCOMP_CONTEXT win9x_get_connection(PCOMP_CONTEXT context)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe{
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe int len;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
f08810eff40a2bddd2bc0103453c4ae775ea62bewrowe if (context == NULL) {
f08810eff40a2bddd2bc0103453c4ae775ea62bewrowe /* allocate the completion context and the transaction pool */
f08810eff40a2bddd2bc0103453c4ae775ea62bewrowe context = apr_pcalloc(pconf, sizeof(COMP_CONTEXT));
f08810eff40a2bddd2bc0103453c4ae775ea62bewrowe if (!context) {
f08810eff40a2bddd2bc0103453c4ae775ea62bewrowe ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "win9x_get_connection: apr_pcalloc() failed. Process will exit.");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe return NULL;
8aefbd756763807188d2e3ce336a8680e4893066wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe apr_create_pool(&context->ptrans, pconf);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe while (1) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe apr_clear_pool(context->ptrans);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context->accept_socket = remove_job();
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (context->accept_socket == -1) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe return NULL;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe len = sizeof(struct sockaddr);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context->sa_server = apr_palloc(context->ptrans, len);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (getsockname(context->accept_socket,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context->sa_server, &len)== SOCKET_ERROR) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_WARNING, apr_get_netos_error(), server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "getsockname failed");
8aefbd756763807188d2e3ce336a8680e4893066wrowe continue;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker len = sizeof(struct sockaddr);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context->sa_client = apr_palloc(context->ptrans, len);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if ((getpeername(context->accept_socket,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar context->sa_client, &len)) == SOCKET_ERROR) {
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_log_error(APLOG_MARK, APLOG_WARNING, apr_get_netos_error(), server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "getpeername failed");
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar memset(&context->sa_client, '\0', sizeof(context->sa_client));
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* do we NEED_DUPPED_CSD ?? */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe return context;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe}
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz/*
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * Windows 2000/NT specific code...
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * create_acceptex_context()
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * reset_acceptex_context()
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * drain_acceptex_complport()
8aefbd756763807188d2e3ce336a8680e4893066wrowe * winnt_get_connection()
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar *
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * TODO: Insert a discussion of 'completion contexts' and what these function do here...
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar */
8aefbd756763807188d2e3ce336a8680e4893066wrowestatic void drain_acceptex_complport(HANDLE hComplPort, BOOLEAN bCleanUp)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe{
4f9c22c4f27571d54197be9674e1fc0d528192aestriker LPOVERLAPPED pol;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz PCOMP_CONTEXT context;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz int rc;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker DWORD BytesRead;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz DWORD CompKey;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe while (1) {
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz context = NULL;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz rc = GetQueuedCompletionStatus(hComplPort, &BytesRead, &CompKey,
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz &pol, 1000);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (!rc) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker rc = apr_get_os_error();
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (rc == APR_FROM_OS_ERROR(ERROR_OPERATION_ABORTED)) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "Child %d: Draining an ABORTED packet off "
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "the AcceptEx completion port.", my_pid);
8aefbd756763807188d2e3ce336a8680e4893066wrowe continue;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe break;
8aefbd756763807188d2e3ce336a8680e4893066wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "Child %d: Draining and discarding an active connection "
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "off the AcceptEx completion port.", my_pid);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context = (PCOMP_CONTEXT) pol;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (context && bCleanUp) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* It is only valid to clean-up in the process that initiated the I/O */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar closesocket(context->accept_socket);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe CloseHandle(context->Overlapped.hEvent);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe}
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowestatic int create_acceptex_context(apr_pool_t *_pconf, ap_listen_rec *lr)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe{
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe PCOMP_CONTEXT context;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker DWORD BytesRead;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe SOCKET nsd;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe int lasterror;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* allocate the completion context */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context = apr_pcalloc(_pconf, sizeof(COMP_CONTEXT));
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (!context) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "create_acceptex_context: apr_pcalloc() failed. Process will exit.");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe return -1;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* initialize the completion context */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz context->lr = lr;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context->Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (context->Overlapped.hEvent == NULL) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "create_acceptex_context: CreateEvent() failed. Process will exit.");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe return -1;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* create and initialize the accept socket */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe apr_get_os_sock(&nsd, context->lr->sd);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar context->accept_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar if (context->accept_socket == INVALID_SOCKET) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_netos_error(), server_conf,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar "create_acceptex_context: socket() failed. Process will exit.");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe return -1;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* SO_UPDATE_ACCEPT_CONTEXT is required for shutdown() to work */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (setsockopt(context->accept_socket, SOL_SOCKET,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker SO_UPDATE_ACCEPT_CONTEXT, (char *)&nsd,
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz sizeof(nsd))) {
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_netos_error(), server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed.");
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz /* Not a failure condition. Keep running. */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe apr_create_pool(&context->ptrans, _pconf);
c2cf53a40a9814eb91db2cdf820f97d943f21628coar
c2cf53a40a9814eb91db2cdf820f97d943f21628coar /* recv_buf must be large enough to hold the remote and local
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * addresses. Note that recv_buf_size is the amount of recv_buf
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * available for AcceptEx to receive bytes into. Since we
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowe * don't want AcceptEx to do a recv, set the size to 0.
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker context->recv_buf = apr_pcalloc(_pconf, 2*PADDED_ADDR_SIZE);
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz context->recv_buf_size = 0;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz /* AcceptEx on the completion context. The completion context will be signaled
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowe * when a connection is accepted. */
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowe if (!AcceptEx(nsd, context->accept_socket,
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz context->recv_buf,
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz context->recv_buf_size,
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz PADDED_ADDR_SIZE, PADDED_ADDR_SIZE,
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowe &BytesRead,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker (LPOVERLAPPED) context)) {
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowe lasterror = apr_get_netos_error();
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowe if (lasterror != APR_FROM_OS_ERROR(ERROR_IO_PENDING)) {
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowe ap_log_error(APLOG_MARK,APLOG_ERR, lasterror, server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "create_acceptex_context: AcceptEx failed. Process will exit.");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe return -1;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
8aefbd756763807188d2e3ce336a8680e4893066wrowe lr->count++;
2520f59894a3e07fefa881ef68aaded763a8d447ben
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz return 0;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar}
8aefbd756763807188d2e3ce336a8680e4893066wrowestatic apr_inline apr_status_t reset_acceptex_context(PCOMP_CONTEXT context)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe{
8aefbd756763807188d2e3ce336a8680e4893066wrowe DWORD BytesRead;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar SOCKET nsd;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe int rc, i;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* reset the buffer pools */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_clear_pool(context->ptrans);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context->sock = NULL;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* recreate and initialize the accept socket if it is not being reused */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_get_os_sock(&nsd, context->lr->sd);
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* AcceptEx on the completion context. The completion context will be signaled
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * when a connection is accepted. Hack Alert: TransmitFile, under certain
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz * circumstances, can 'recycle' accept sockets, saving the overhead of calling
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * socket(). Occasionally this fails (usually when the client closes his end
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz * of the connection early). When this occurs, AcceptEx will fail with 10022,
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz * Invalid Parameter. When this occurs, just open a fresh accept socket and
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * retry the call.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe for (i=0; i<2; i++) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (context->accept_socket == INVALID_SOCKET) {
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz context->accept_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz if (context->accept_socket == INVALID_SOCKET) {
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz rc = apr_get_netos_error();
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz ap_log_error(APLOG_MARK,APLOG_ERR, rc, server_conf,
8aefbd756763807188d2e3ce336a8680e4893066wrowe "reset_acceptex_context: socket() failed. Process will exit.");
4f9c22c4f27571d54197be9674e1fc0d528192aestriker return rc;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* SO_UPDATE_ACCEPT_CONTEXT is required for shutdown() to work */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (setsockopt(context->accept_socket, SOL_SOCKET,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar SO_UPDATE_ACCEPT_CONTEXT, (char *)&nsd, sizeof(nsd))) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_WARNING, apr_get_netos_error(),
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar server_conf,
8aefbd756763807188d2e3ce336a8680e4893066wrowe "setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed.");
cf6ef072483172309861d06e85b1aeff4573c060wrowe }
cf6ef072483172309861d06e85b1aeff4573c060wrowe }
cf6ef072483172309861d06e85b1aeff4573c060wrowe
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (!AcceptEx(nsd, context->accept_socket,
cf6ef072483172309861d06e85b1aeff4573c060wrowe context->recv_buf,
cf6ef072483172309861d06e85b1aeff4573c060wrowe context->recv_buf_size,
cf6ef072483172309861d06e85b1aeff4573c060wrowe PADDED_ADDR_SIZE,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker PADDED_ADDR_SIZE,
cf6ef072483172309861d06e85b1aeff4573c060wrowe &BytesRead,
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe (LPOVERLAPPED) context)) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker rc = apr_get_netos_error();
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe if (rc != APR_FROM_OS_ERROR(ERROR_IO_PENDING)) {
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, server_conf,
cf6ef072483172309861d06e85b1aeff4573c060wrowe "reset_acceptex_context: AcceptEx failed for "
cf6ef072483172309861d06e85b1aeff4573c060wrowe "listening socket: %d and accept socket: %d. Getting a new accept socket.",
cf6ef072483172309861d06e85b1aeff4573c060wrowe nsd, context->accept_socket);
cf6ef072483172309861d06e85b1aeff4573c060wrowe closesocket(context->accept_socket);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context->accept_socket = INVALID_SOCKET;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe continue;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe break;
cf6ef072483172309861d06e85b1aeff4573c060wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
cf6ef072483172309861d06e85b1aeff4573c060wrowe context->lr->count++;
c2cf53a40a9814eb91db2cdf820f97d943f21628coar
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe return APR_SUCCESS;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe}
c2cf53a40a9814eb91db2cdf820f97d943f21628coarstatic PCOMP_CONTEXT winnt_get_connection(PCOMP_CONTEXT context)
c2cf53a40a9814eb91db2cdf820f97d943f21628coar{
8aefbd756763807188d2e3ce336a8680e4893066wrowe int requests_this_child = 0;
cf6ef072483172309861d06e85b1aeff4573c060wrowe int rc;
8aefbd756763807188d2e3ce336a8680e4893066wrowe LPOVERLAPPED pol;
8aefbd756763807188d2e3ce336a8680e4893066wrowe DWORD CompKey;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe DWORD BytesRead;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (context != NULL) {
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (shutdown_in_progress) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* Clean-up the AcceptEx completion context */
cf6ef072483172309861d06e85b1aeff4573c060wrowe CloseHandle(context->Overlapped.hEvent);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (context->accept_socket != INVALID_SOCKET)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe closesocket(context->accept_socket);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe else {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Prepare the completion context for reuse */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if ((rc = reset_acceptex_context(context)) != APR_SUCCESS) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_CRIT, rc, server_conf,
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe "Child %d: winnt_get_connection: reset_acceptex_context failed.",
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe my_pid);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (context->accept_socket != INVALID_SOCKET)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe closesocket(context->accept_socket);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe CloseHandle(context->Overlapped.hEvent);
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Probably should just die now... */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
cf6ef072483172309861d06e85b1aeff4573c060wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* May need to atomize the workers_may_exit check with the
cadddb2c31d24d48f4017db4df0a29687432326cwrowe * g_blocked_threads++ */
cadddb2c31d24d48f4017db4df0a29687432326cwrowe if (workers_may_exit) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe return NULL;
cf6ef072483172309861d06e85b1aeff4573c060wrowe }
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz g_blocked_threads++;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe while (1) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe rc = GetQueuedCompletionStatus(AcceptExCompPort, &BytesRead, &CompKey,
cf6ef072483172309861d06e85b1aeff4573c060wrowe &pol, INFINITE);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (!rc) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker rc = apr_get_os_error();
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (rc != APR_FROM_OS_ERROR(ERROR_OPERATION_ABORTED)) {
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawick /* Is this a deadly condition?
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * We sometimes get ERROR_NETNAME_DELETED when a client
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * disconnects when attempting to reuse sockets. Not sure why
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * we see this now and not during AcceptEx(). Reset the
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * AcceptEx context and continue...
8aefbd756763807188d2e3ce336a8680e4893066wrowe */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK,APLOG_DEBUG, rc, server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "Child %d: - GetQueuedCompletionStatus() failed",
cf6ef072483172309861d06e85b1aeff4573c060wrowe my_pid);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Reset the completion context */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (pol) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context = (PCOMP_CONTEXT) pol;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (context->accept_socket != INVALID_SOCKET)
cf6ef072483172309861d06e85b1aeff4573c060wrowe closesocket(context->accept_socket);
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawick if ((rc = reset_acceptex_context(context)) != APR_SUCCESS) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_CRIT, rc, server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "Child %d: winnt_get_connection: reset_acceptex_context failed.",
4f9c22c4f27571d54197be9674e1fc0d528192aestriker my_pid);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (context->accept_socket != INVALID_SOCKET)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe closesocket(context->accept_socket);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe CloseHandle(context->Overlapped.hEvent);
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Probably should just die now... */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
cf6ef072483172309861d06e85b1aeff4573c060wrowe }
cf6ef072483172309861d06e85b1aeff4573c060wrowe }
cf6ef072483172309861d06e85b1aeff4573c060wrowe else {
cf6ef072483172309861d06e85b1aeff4573c060wrowe /* Sometimes we catch ERROR_OPERATION_ABORTED completion packets
cf6ef072483172309861d06e85b1aeff4573c060wrowe * from the old child process (during a restart). Ignore them.
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe */
cf6ef072483172309861d06e85b1aeff4573c060wrowe ap_log_error(APLOG_MARK,APLOG_DEBUG, rc, server_conf,
cf6ef072483172309861d06e85b1aeff4573c060wrowe "Child %d: - Draining ERROR_OPERATION_ABORTED packet off "
cf6ef072483172309861d06e85b1aeff4573c060wrowe "the completion port.", my_pid);
cf6ef072483172309861d06e85b1aeff4573c060wrowe }
cf6ef072483172309861d06e85b1aeff4573c060wrowe continue;
cf6ef072483172309861d06e85b1aeff4573c060wrowe }
cf6ef072483172309861d06e85b1aeff4573c060wrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (CompKey != 0) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe /* CompKey == my_pid means this thread was unblocked by
cf6ef072483172309861d06e85b1aeff4573c060wrowe * the shutdown code (not by io completion).
cf6ef072483172309861d06e85b1aeff4573c060wrowe */
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (CompKey == my_pid) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe g_blocked_threads--;
cf6ef072483172309861d06e85b1aeff4573c060wrowe return NULL;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Sometimes we catch shutdown io completion packets
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * posted by the old child process (during a restart). Ignore them.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe continue;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker context = (PCOMP_CONTEXT) pol;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe break;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe g_blocked_threads--;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Check to see if we need to create more completion contexts,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * but only if we are not in the process of shutting down
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar if (!shutdown_in_progress) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe apr_lock(allowed_globals.jobmutex);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context->lr->count--;
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (context->lr->count < 2) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe SetEvent(maintenance_event);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe apr_unlock(allowed_globals.jobmutex);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe }
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Received a connection */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe GetAcceptExSockaddrs(context->recv_buf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker context->recv_buf_size,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe PADDED_ADDR_SIZE,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe PADDED_ADDR_SIZE,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe &context->sa_server,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe &context->sa_server_len,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe &context->sa_client,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe &context->sa_client_len);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar return context;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe}
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe/*
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * worker_main() - this is the main loop for the worker threads
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar *
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * Windows 95/98
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * Each thread runs within this function. They wait within remove_job()
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * for a job to become available, then handle all the requests on that
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * connection until it is closed, then return to remove_job().
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe *
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * The worker thread will exit when it removes a job which contains
8aefbd756763807188d2e3ce336a8680e4893066wrowe * socket number -1. This provides a graceful thread exit, since
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * it will never exit during a connection.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe *
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * This code in this function is basically equivalent to the child_main()
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * from the multi-process (Unix) environment, except that we
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar *
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * - do not call child_init_modules (child init API phase)
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * - block in remove_job, and when unblocked we have an already
8aefbd756763807188d2e3ce336a8680e4893066wrowe * accepted socket, instead of blocking on a mutex or select().
8aefbd756763807188d2e3ce336a8680e4893066wrowe */
c2cf53a40a9814eb91db2cdf820f97d943f21628coar
c2cf53a40a9814eb91db2cdf820f97d943f21628coarstatic void worker_main(int thread_num)
c2cf53a40a9814eb91db2cdf820f97d943f21628coar{
c2cf53a40a9814eb91db2cdf820f97d943f21628coar PCOMP_CONTEXT context = NULL;
c2cf53a40a9814eb91db2cdf820f97d943f21628coar apr_os_sock_info_t sockinfo;
8aefbd756763807188d2e3ce336a8680e4893066wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe while (1) {
8aefbd756763807188d2e3ce336a8680e4893066wrowe conn_rec *c;
8aefbd756763807188d2e3ce336a8680e4893066wrowe apr_int32_t disconnected;
8aefbd756763807188d2e3ce336a8680e4893066wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe ap_update_child_status(0, thread_num, SERVER_READY,
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe (request_rec *) NULL);
8aefbd756763807188d2e3ce336a8680e4893066wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Grab a connection off the network */
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
c2cf53a40a9814eb91db2cdf820f97d943f21628coar context = win9x_get_connection(context);
c2cf53a40a9814eb91db2cdf820f97d943f21628coar }
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawick else {
c2cf53a40a9814eb91db2cdf820f97d943f21628coar context = winnt_get_connection(context);
8aefbd756763807188d2e3ce336a8680e4893066wrowe }
8aefbd756763807188d2e3ce336a8680e4893066wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (!context)
8aefbd756763807188d2e3ce336a8680e4893066wrowe break;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe sock_disable_nagle(context->accept_socket);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe sockinfo.os_sock = &context->accept_socket;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe sockinfo.local = context->sa_server;
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe sockinfo.remote = context->sa_client;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar sockinfo.family = APR_INET;
0540a0b469147b52e858587270dba31c2aaa9e09wrowe sockinfo.type = SOCK_STREAM;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar apr_make_os_sock(&context->sock, &sockinfo, context->ptrans);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar
0540a0b469147b52e858587270dba31c2aaa9e09wrowe ap_update_child_status(0, thread_num,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar SERVER_BUSY_READ, (request_rec *) NULL);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe
0540a0b469147b52e858587270dba31c2aaa9e09wrowe c = ap_new_connection(context->ptrans, server_conf, context->sock,
a9a4544168a37b43bd180b3703ccee995f27a80awrowe thread_num);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe
0540a0b469147b52e858587270dba31c2aaa9e09wrowe ap_process_connection(c);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar apr_getsocketopt(context->sock, APR_SO_DISCONNECTED, &disconnected);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe if (!disconnected) {
0540a0b469147b52e858587270dba31c2aaa9e09wrowe context->accept_socket = INVALID_SOCKET;
0540a0b469147b52e858587270dba31c2aaa9e09wrowe ap_lingering_close(c);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
0540a0b469147b52e858587270dba31c2aaa9e09wrowe }
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe
0540a0b469147b52e858587270dba31c2aaa9e09wrowe ap_update_child_status(0, thread_num, SERVER_DEAD, (request_rec *) NULL);
8aefbd756763807188d2e3ce336a8680e4893066wrowe
8aefbd756763807188d2e3ce336a8680e4893066wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, server_conf,
0540a0b469147b52e858587270dba31c2aaa9e09wrowe "Child %d: Thread exiting.", my_pid);
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe#if 0
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
948096a99010fccf648814fecf38f75c689172d7wrowe SetEvent(exit_event);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#endif
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* TODO: Add code to clean-up completion contexts here */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker}
0540a0b469147b52e858587270dba31c2aaa9e09wrowe
948096a99010fccf648814fecf38f75c689172d7wrowestatic void cleanup_thread(thread *handles, int *thread_cnt, int thread_to_clean)
948096a99010fccf648814fecf38f75c689172d7wrowe{
948096a99010fccf648814fecf38f75c689172d7wrowe int i;
948096a99010fccf648814fecf38f75c689172d7wrowe
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe CloseHandle(handles[thread_to_clean]);
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe for (i = thread_to_clean; i < ((*thread_cnt) - 1); i++)
053497224246c4dbef9af594cacf5c00ed271e6cwrowe handles[i] = handles[i + 1];
0540a0b469147b52e858587270dba31c2aaa9e09wrowe (*thread_cnt)--;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz}
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic void create_listeners()
42da244268b11ec661b528510f80a18b73c51727brianp{
42da244268b11ec661b528510f80a18b73c51727brianp#define NUM_LISTENERS 5
948096a99010fccf648814fecf38f75c689172d7wrowe ap_listen_rec *lr;
948096a99010fccf648814fecf38f75c689172d7wrowe for (lr = ap_listeners; lr != NULL; lr = lr->next) {
948096a99010fccf648814fecf38f75c689172d7wrowe while (lr->count < NUM_LISTENERS) {
948096a99010fccf648814fecf38f75c689172d7wrowe if (create_acceptex_context(pconf, lr) == -1) {
948096a99010fccf648814fecf38f75c689172d7wrowe ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
948096a99010fccf648814fecf38f75c689172d7wrowe "Unable to create an AcceptEx completion context -- process will exit");
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz signal_parent(0); /* tell parent to die */
948096a99010fccf648814fecf38f75c689172d7wrowe }
948096a99010fccf648814fecf38f75c689172d7wrowe }
948096a99010fccf648814fecf38f75c689172d7wrowe }
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz}
948096a99010fccf648814fecf38f75c689172d7wrowe/*
948096a99010fccf648814fecf38f75c689172d7wrowe * child_main() runs the main control thread for the child process.
053497224246c4dbef9af594cacf5c00ed271e6cwrowe *
948096a99010fccf648814fecf38f75c689172d7wrowe * The control thread:
948096a99010fccf648814fecf38f75c689172d7wrowe * - sets up the worker thread pool
948096a99010fccf648814fecf38f75c689172d7wrowe * - starts the accept thread (Win 9x)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * - creates AcceptEx contexts (Win NT)
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * - waits for exit_event, maintenance_event or maintenance timeout
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * and does the right thing depending on which event is received.
c2cf53a40a9814eb91db2cdf820f97d943f21628coar */
948096a99010fccf648814fecf38f75c689172d7wrowestatic void child_main()
4f9c22c4f27571d54197be9674e1fc0d528192aestriker{
948096a99010fccf648814fecf38f75c689172d7wrowe apr_status_t status;
948096a99010fccf648814fecf38f75c689172d7wrowe HANDLE child_events[2];
053497224246c4dbef9af594cacf5c00ed271e6cwrowe char* exit_event_name;
a9a4544168a37b43bd180b3703ccee995f27a80awrowe int nthreads = ap_threads_per_child;
948096a99010fccf648814fecf38f75c689172d7wrowe int tid;
a9a4544168a37b43bd180b3703ccee995f27a80awrowe thread *child_handles;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker int rv;
a9a4544168a37b43bd180b3703ccee995f27a80awrowe time_t end_time;
a9a4544168a37b43bd180b3703ccee995f27a80awrowe int i;
a9a4544168a37b43bd180b3703ccee995f27a80awrowe int cld;
a9a4544168a37b43bd180b3703ccee995f27a80awrowe apr_pool_t *pchild;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar /* Set up the scoreboard. The scoreboard in this MPM only applies to the
948096a99010fccf648814fecf38f75c689172d7wrowe * child process and is not shared across processes
948096a99010fccf648814fecf38f75c689172d7wrowe */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_restart_time = apr_now();
948096a99010fccf648814fecf38f75c689172d7wrowe ap_create_scoreboard(pconf, SB_NOT_SHARED);
948096a99010fccf648814fecf38f75c689172d7wrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* This is the child process or we are running in single process
948096a99010fccf648814fecf38f75c689172d7wrowe * mode.
948096a99010fccf648814fecf38f75c689172d7wrowe */
0540a0b469147b52e858587270dba31c2aaa9e09wrowe exit_event_name = apr_psprintf(pconf, "apC%d", my_pid);
948096a99010fccf648814fecf38f75c689172d7wrowe setup_signal_names(apr_psprintf(pconf,"ap%d", parent_pid));
aa047239dedf0d26e8efecfade32e7337f35df19wrowe
948096a99010fccf648814fecf38f75c689172d7wrowe if (one_process) {
948096a99010fccf648814fecf38f75c689172d7wrowe /* Single process mode */
948096a99010fccf648814fecf38f75c689172d7wrowe apr_create_lock(&start_mutex,APR_MUTEX, APR_CROSS_PROCESS,signal_name_prefix,pconf);
948096a99010fccf648814fecf38f75c689172d7wrowe exit_event = CreateEvent(NULL, TRUE, FALSE, exit_event_name);
948096a99010fccf648814fecf38f75c689172d7wrowe
053497224246c4dbef9af594cacf5c00ed271e6cwrowe setup_listeners(server_conf);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe bind_listeners_to_completion_port();
053497224246c4dbef9af594cacf5c00ed271e6cwrowe }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker else {
0540a0b469147b52e858587270dba31c2aaa9e09wrowe /* Child process mode */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_child_init_lock(&start_mutex, signal_name_prefix, pconf);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe exit_event = OpenEvent(EVENT_ALL_ACCESS, FALSE, exit_event_name);
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "Child %d: exit_event_name = %s", my_pid, exit_event_name);
053497224246c4dbef9af594cacf5c00ed271e6cwrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker setup_inherited_listeners(server_conf);
053497224246c4dbef9af594cacf5c00ed271e6cwrowe }
053497224246c4dbef9af594cacf5c00ed271e6cwrowe
053497224246c4dbef9af594cacf5c00ed271e6cwrowe /* Initialize the child_events */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker maintenance_event = CreateEvent(NULL, TRUE, FALSE, NULL);
c2cf53a40a9814eb91db2cdf820f97d943f21628coar child_events[0] = exit_event;
c2cf53a40a9814eb91db2cdf820f97d943f21628coar child_events[1] = maintenance_event;
c2cf53a40a9814eb91db2cdf820f97d943f21628coar
c2cf53a40a9814eb91db2cdf820f97d943f21628coar ap_assert(start_mutex);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_assert(exit_event);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_assert(maintenance_event);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz apr_create_pool(&pchild, pconf);
053497224246c4dbef9af594cacf5c00ed271e6cwrowe allowed_globals.jobsemaphore = create_semaphore(0);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_create_lock(&allowed_globals.jobmutex, APR_MUTEX, APR_INTRAPROCESS, NULL, pchild);
948096a99010fccf648814fecf38f75c689172d7wrowe
948096a99010fccf648814fecf38f75c689172d7wrowe /*
948096a99010fccf648814fecf38f75c689172d7wrowe * Wait until we have permission to start accepting connections.
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * start_mutex is used to ensure that only one child ever
948096a99010fccf648814fecf38f75c689172d7wrowe * goes into the listen/accept loop at once.
948096a99010fccf648814fecf38f75c689172d7wrowe */
948096a99010fccf648814fecf38f75c689172d7wrowe status = apr_lock(start_mutex);
948096a99010fccf648814fecf38f75c689172d7wrowe if (status != APR_SUCCESS) {
948096a99010fccf648814fecf38f75c689172d7wrowe ap_log_error(APLOG_MARK,APLOG_ERR, status, server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "Child %d: Failed to acquire the start_mutex. Process will exit.", my_pid);
948096a99010fccf648814fecf38f75c689172d7wrowe signal_parent(0); /* tell parent to die */
948096a99010fccf648814fecf38f75c689172d7wrowe exit(0);
948096a99010fccf648814fecf38f75c689172d7wrowe }
948096a99010fccf648814fecf38f75c689172d7wrowe ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
948096a99010fccf648814fecf38f75c689172d7wrowe "Child %d: Acquired the start mutex.", my_pid);
948096a99010fccf648814fecf38f75c689172d7wrowe
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Create the worker thread pool */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "Child %d: Starting %d worker threads.", my_pid, nthreads);
948096a99010fccf648814fecf38f75c689172d7wrowe child_handles = (thread) alloca(nthreads * sizeof(int));
0540a0b469147b52e858587270dba31c2aaa9e09wrowe for (i = 0; i < nthreads; i++) {
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_update_child_status(0, i, SERVER_STARTING, (request_rec *) NULL);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar child_handles[i] = (thread) _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) worker_main,
0540a0b469147b52e858587270dba31c2aaa9e09wrowe (void *) i, 0, &tid);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
696218c49632c863d18b25fa52ab63617088cb38wrowe
948096a99010fccf648814fecf38f75c689172d7wrowe /* Begin accepting connections */
0540a0b469147b52e858587270dba31c2aaa9e09wrowe if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
948096a99010fccf648814fecf38f75c689172d7wrowe /* Win95/98: Start the accept thread */
948096a99010fccf648814fecf38f75c689172d7wrowe _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) accept_and_queue_connections,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker (void *) i, 0, &tid);
948096a99010fccf648814fecf38f75c689172d7wrowe } else {
948096a99010fccf648814fecf38f75c689172d7wrowe /* Windows NT/2000: Create AcceptEx completion contexts */
053497224246c4dbef9af594cacf5c00ed271e6cwrowe create_listeners();
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
948096a99010fccf648814fecf38f75c689172d7wrowe
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar /* Wait for one of three events:
053497224246c4dbef9af594cacf5c00ed271e6cwrowe * exit_event:
053497224246c4dbef9af594cacf5c00ed271e6cwrowe * The exit_event is signaled by the parent process to notify
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * the child that it is time to exit.
a9a4544168a37b43bd180b3703ccee995f27a80awrowe *
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * maintenance_event:
948096a99010fccf648814fecf38f75c689172d7wrowe * This event is signaled by the worker thread pool to direct
948096a99010fccf648814fecf38f75c689172d7wrowe * this thread to create more completion contexts.
053497224246c4dbef9af594cacf5c00ed271e6cwrowe *
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * TIMEOUT:
053497224246c4dbef9af594cacf5c00ed271e6cwrowe * To do periodic maintenance on the server (check for thread exits,
053497224246c4dbef9af594cacf5c00ed271e6cwrowe * number of completion contexts, etc.)
948096a99010fccf648814fecf38f75c689172d7wrowe */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar while (1) {
948096a99010fccf648814fecf38f75c689172d7wrowe rv = WaitForMultipleObjects(2, (HANDLE *) child_events, FALSE, INFINITE);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb cld = rv - WAIT_OBJECT_0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (rv == WAIT_FAILED) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Something serious is wrong */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe "Child %d: WAIT_FAILED -- shutting down server");
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb break;
aa047239dedf0d26e8efecfade32e7337f35df19wrowe }
aa047239dedf0d26e8efecfade32e7337f35df19wrowe else if (rv == WAIT_TIMEOUT) {
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Hey, this cannot happen */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_CRIT, APR_SUCCESS, server_conf,
0540a0b469147b52e858587270dba31c2aaa9e09wrowe "Child %d: WAIT_TIMEOUT -- shutting down server", my_pid);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe break;
aa047239dedf0d26e8efecfade32e7337f35df19wrowe }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb else if (cld == 0) {
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe /* Exit event was signaled */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe "Child %d: Exit event signaled. Child process is ending.", my_pid);
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe break;
b5bd19d82874782007a2f9bcb19341a483c1270cwrowe }
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe else {
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe /* Child maintenance event signaled */
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp create_listeners();
aa047239dedf0d26e8efecfade32e7337f35df19wrowe }
aa047239dedf0d26e8efecfade32e7337f35df19wrowe ResetEvent(maintenance_event);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
aa047239dedf0d26e8efecfade32e7337f35df19wrowe "Child %d: Child maintenance event signaled.", my_pid);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe }
0540a0b469147b52e858587270dba31c2aaa9e09wrowe }
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Setting is_graceful will close keep-alive connections */
aa047239dedf0d26e8efecfade32e7337f35df19wrowe is_graceful = 1;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Shutdown the worker threads */
aa047239dedf0d26e8efecfade32e7337f35df19wrowe if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* workers_may_exit = 1; Not used on Win9x */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb shutdown_in_progress = 1;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz for (i = 0; i < nthreads; i++) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb add_job(-1);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb else { /* Windows NT/2000 */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb SOCKET nsd;
aa047239dedf0d26e8efecfade32e7337f35df19wrowe ap_listen_rec *lr;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /*
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * Setting shutdown_in_progress prevents new AcceptEx completion
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * contexts from being queued to the port but allows threads to
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * continue consuming from the port. This gives the server a
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * chance to handle any accepted connections.
c2cf53a40a9814eb91db2cdf820f97d943f21628coar */
aa047239dedf0d26e8efecfade32e7337f35df19wrowe shutdown_in_progress = 1;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker Sleep(1000);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Setting workers_may_exit prevents threads from consumimg from the
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * completion port (especially threads that unblock off of keep-alive
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * connections later on).
a9a4544168a37b43bd180b3703ccee995f27a80awrowe */
a9a4544168a37b43bd180b3703ccee995f27a80awrowe workers_may_exit = 1;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
a9a4544168a37b43bd180b3703ccee995f27a80awrowe /* Unblock threads blocked on the completion port */
a9a4544168a37b43bd180b3703ccee995f27a80awrowe apr_lock(allowed_globals.jobmutex);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe while (g_blocked_threads > 0) {
a9a4544168a37b43bd180b3703ccee995f27a80awrowe ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "Child %d: %d threads blocked on the completion port", my_pid, g_blocked_threads);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar for (i=g_blocked_threads; i > 0; i--) {
aa047239dedf0d26e8efecfade32e7337f35df19wrowe PostQueuedCompletionStatus(AcceptExCompPort, 0, my_pid, NULL);
c2cf53a40a9814eb91db2cdf820f97d943f21628coar }
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar Sleep(1000);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe }
aa047239dedf0d26e8efecfade32e7337f35df19wrowe apr_unlock(allowed_globals.jobmutex);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Cancel any remaining pending AcceptEx completion contexts */
aa047239dedf0d26e8efecfade32e7337f35df19wrowe for (lr = ap_listeners; lr != NULL; lr = lr->next) {
0540a0b469147b52e858587270dba31c2aaa9e09wrowe apr_get_os_sock(&nsd,lr->sd);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe CancelIo((HANDLE) nsd);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe }
aa047239dedf0d26e8efecfade32e7337f35df19wrowe
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Drain the canceled contexts off the port */
aa047239dedf0d26e8efecfade32e7337f35df19wrowe drain_acceptex_complport(AcceptExCompPort, TRUE);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe }
aa047239dedf0d26e8efecfade32e7337f35df19wrowe
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Release the start_mutex to let the new process (in the restart
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * scenario) a chance to begin servicing requests
4f9c22c4f27571d54197be9674e1fc0d528192aestriker */
0540a0b469147b52e858587270dba31c2aaa9e09wrowe ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
0540a0b469147b52e858587270dba31c2aaa9e09wrowe "Child %d: Releasing the start mutex", my_pid);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe apr_unlock(start_mutex);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe
c2cf53a40a9814eb91db2cdf820f97d943f21628coar /* Give busy worker threads a chance to service their connections.
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * Kill them off if they take too long
c2cf53a40a9814eb91db2cdf820f97d943f21628coar */
c2cf53a40a9814eb91db2cdf820f97d943f21628coar ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
aa047239dedf0d26e8efecfade32e7337f35df19wrowe "Child %d: Waiting for %d threads to die.", my_pid, nthreads);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb end_time = time(NULL) + 180;
aa047239dedf0d26e8efecfade32e7337f35df19wrowe while (nthreads) {
aa047239dedf0d26e8efecfade32e7337f35df19wrowe rv = wait_for_many_objects(nthreads, child_handles, end_time - time(NULL));
aa047239dedf0d26e8efecfade32e7337f35df19wrowe if (rv != WAIT_TIMEOUT) {
aa047239dedf0d26e8efecfade32e7337f35df19wrowe rv = rv - WAIT_OBJECT_0;
0540a0b469147b52e858587270dba31c2aaa9e09wrowe ap_assert((rv >= 0) && (rv < nthreads));
aa047239dedf0d26e8efecfade32e7337f35df19wrowe cleanup_thread(child_handles, &nthreads, rv);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe continue;
aa047239dedf0d26e8efecfade32e7337f35df19wrowe }
aa047239dedf0d26e8efecfade32e7337f35df19wrowe break;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker for (i = 0; i < nthreads; i++) {
aa047239dedf0d26e8efecfade32e7337f35df19wrowe TerminateThread(child_handles[i], 1);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe CloseHandle(child_handles[i]);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe }
aa047239dedf0d26e8efecfade32e7337f35df19wrowe ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
aa047239dedf0d26e8efecfade32e7337f35df19wrowe "Child %d: All worker threads have ended.", my_pid);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb CloseHandle(AcceptExCompPort);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar destroy_semaphore(allowed_globals.jobsemaphore);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_destroy_lock(allowed_globals.jobmutex);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe
0540a0b469147b52e858587270dba31c2aaa9e09wrowe apr_destroy_pool(pchild);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar CloseHandle(exit_event);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar}
0540a0b469147b52e858587270dba31c2aaa9e09wrowe
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar/*
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * Spawn a child Apache process. The child process has the command line arguments from
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * argc and argv[], plus a -Z argument giving the name of an event. The child should
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * open and poll or wait on this event. When it is signalled, the child should die.
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * prefix is a prefix string for the event name.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * The child_num argument on entry contains a serial number for this child (used to create
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * a unique event name). On exit, this number will have been incremented by one, ready
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * for the next call.
aa047239dedf0d26e8efecfade32e7337f35df19wrowe *
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * On exit, the value pointed to be *ev will contain the event created
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * to signal the new child process.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The return value is the handle to the child process if successful, else -1. If -1 is
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe * returned the error will already have been logged by ap_log_error().
a9a4544168a37b43bd180b3703ccee995f27a80awrowe */
a9a4544168a37b43bd180b3703ccee995f27a80awrowe
a9a4544168a37b43bd180b3703ccee995f27a80awrowe/**********************************************************************
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * master_main - this is the parent (main) process. We create a
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * child process to do the work, then sit around waiting for either
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe * the child to exit, or a restart or exit signal. If the child dies,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * we just respawn a new one. If we have a shutdown or graceful restart,
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * tell the child to die when it is ready. If it is a non-graceful
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * restart, force the child to die immediately.
aa047239dedf0d26e8efecfade32e7337f35df19wrowe **********************************************************************/
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar
aa047239dedf0d26e8efecfade32e7337f35df19wrowe#define MAX_PROCESSES 50 /* must be < MAX_WAIT_OBJECTS-1 */
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic void cleanup_process(HANDLE *handles, HANDLE *events, int position, int *processes)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb{
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb int i;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb int handle = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb CloseHandle(handles[position]);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb CloseHandle(events[position]);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb handle = (int)handles[position];
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb for (i = position; i < (*processes)-1; i++) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb handles[i] = handles[i + 1];
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb events[i] = events[i + 1];
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb (*processes)--;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb}
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int create_process(apr_pool_t *p, HANDLE *handles, HANDLE *events, int *processes)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb{
4f9c22c4f27571d54197be9674e1fc0d528192aestriker int rv;
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe char buf[1024];
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb char *pCommand;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb char *pEnvVar;
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe char *pEnvBlock;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker int i;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb int iEnvBlockLen;
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe STARTUPINFO si; /* Filled in prior to call to CreateProcess */
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe PROCESS_INFORMATION pi; /* filled in on call to CreateProcess */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe ap_listen_rec *lr;
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe DWORD BytesWritten;
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe HANDLE hPipeRead = NULL;
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe HANDLE hPipeWrite = NULL;
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe SECURITY_ATTRIBUTES sa = {0};
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe HANDLE kill_event;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe LPWSAPROTOCOL_INFO lpWSAProtocolInfo;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe HANDLE hDupedCompPort;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker sa.nLength = sizeof(sa);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe sa.bInheritHandle = TRUE;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe sa.lpSecurityDescriptor = NULL;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* Build the command line. Should look something like this:
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * C:/apache/bin/apache.exe -f ap_server_confname
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * First, get the path to the executable...
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe */
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe rv = GetModuleFileName(NULL, buf, sizeof(buf));
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe if (rv == sizeof(buf)) {
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe ap_log_error(APLOG_MARK, APLOG_CRIT, ERROR_BAD_PATHNAME, server_conf,
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe "Parent: Path to Apache process too long");
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe return -1;
ebe7da316894e2b93b4a905fccd2496d0ed1bc78rbb } else if (rv == 0) {
ebe7da316894e2b93b4a905fccd2496d0ed1bc78rbb ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
ebe7da316894e2b93b4a905fccd2496d0ed1bc78rbb "Parent: GetModuleFileName() returned NULL for current process.");
ebe7da316894e2b93b4a905fccd2496d0ed1bc78rbb return -1;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe /* Build the command line */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker pCommand = apr_psprintf(p, "\"%s\"", buf);
e57e920838f31508f1418aa4c25ce55b345b2cebrbb for (i = 1; i < server_conf->process->argc; i++) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker pCommand = apr_pstrcat(p, pCommand, " \"", server_conf->process->argv[i], "\"", NULL);
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe }
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe
e57e920838f31508f1418aa4c25ce55b345b2cebrbb /* Build the environment, since Win9x disrespects the active env */
e57e920838f31508f1418aa4c25ce55b345b2cebrbb pEnvVar = apr_psprintf(p, "AP_PARENT_PID=%i", parent_pid);
e57e920838f31508f1418aa4c25ce55b345b2cebrbb /*
e57e920838f31508f1418aa4c25ce55b345b2cebrbb * Win32's CreateProcess call requires that the environment
e57e920838f31508f1418aa4c25ce55b345b2cebrbb * be passed in an environment block, a null terminated block of
e57e920838f31508f1418aa4c25ce55b345b2cebrbb * null terminated strings.
e57e920838f31508f1418aa4c25ce55b345b2cebrbb */
e57e920838f31508f1418aa4c25ce55b345b2cebrbb i = 0;
e57e920838f31508f1418aa4c25ce55b345b2cebrbb iEnvBlockLen = 1;
e57e920838f31508f1418aa4c25ce55b345b2cebrbb while (_environ[i]) {
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe iEnvBlockLen += strlen(_environ[i]) + 1;
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe i++;
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe }
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe pEnvBlock = (char *)apr_pcalloc(p, iEnvBlockLen + strlen(pEnvVar) + 1);
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe strcpy(pEnvBlock, pEnvVar);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker pEnvVar = strchr(pEnvBlock, '\0') + 1;
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe i = 0;
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe while (_environ[i]) {
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe strcpy(pEnvVar, _environ[i]);
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe pEnvVar = strchr(pEnvVar, '\0') + 1;
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe i++;
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb pEnvVar = '\0';
c2cf53a40a9814eb91db2cdf820f97d943f21628coar /* Create a pipe to send socket info to the child */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (!CreatePipe(&hPipeRead, &hPipeWrite, &sa, 0)) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "Parent: Unable to create pipe to child process.");
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return -1;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* Give the read end of the pipe (hPipeRead) to the child as stdin. The
68b29bcadd6c46aecdc9fe14c93555a2238ad2aagregames * parent will write the socket data to the child on this pipe.
68b29bcadd6c46aecdc9fe14c93555a2238ad2aagregames */
68b29bcadd6c46aecdc9fe14c93555a2238ad2aagregames memset(&si, 0, sizeof(si));
4f9c22c4f27571d54197be9674e1fc0d528192aestriker memset(&pi, 0, sizeof(pi));
4f9c22c4f27571d54197be9674e1fc0d528192aestriker si.cb = sizeof(si);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb si.wShowWindow = SW_HIDE;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker si.hStdInput = hPipeRead;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (!CreateProcess(NULL, pCommand, NULL, NULL,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb TRUE, /* Inherit handles */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker CREATE_SUSPENDED, /* Creation flags */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb pEnvBlock, /* Environment block */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb NULL,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb &si, &pi)) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar "Parent: Not able to create the child process.");
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /*
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * We must close the handles to the new process and its main thread
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * to prevent handle and memory leaks.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker CloseHandle(pi.hProcess);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar CloseHandle(pi.hThread);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar return -1;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "Parent: Created child process %d", pi.dwProcessId);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
4f9c22c4f27571d54197be9674e1fc0d528192aestriker SetEnvironmentVariable("AP_PARENT_PID",NULL);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /* Create the exit_event, apCchild_pid */
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe sa.nLength = sizeof(sa);
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe sa.bInheritHandle = TRUE;
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe sa.lpSecurityDescriptor = NULL;
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe kill_event = CreateEvent(&sa, TRUE, FALSE, apr_psprintf(pconf,"apC%d", pi.dwProcessId));
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (!kill_event) {
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe "Parent: Could not create exit event for child process");
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe CloseHandle(pi.hProcess);
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe CloseHandle(pi.hThread);
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe return -1;
b45c1c292ff1fa635004ae81fa691f8cb3cdda85rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Assume the child process lives. Update the process and event tables */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb handles[*processes] = pi.hProcess;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb events[*processes] = kill_event;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar (*processes)++;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar /* We never store the thread's handle, so close it now. */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ResumeThread(pi.hThread);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb CloseHandle(pi.hThread);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Run the chain of open sockets. For each socket, duplicate it
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * for the target process then send the WSAPROTOCOL_INFO
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard * (returned by dup socket) to the child */
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard for (lr = ap_listeners; lr; lr = lr->next) {
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard int nsd;
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard lpWSAProtocolInfo = apr_pcalloc(p, sizeof(WSAPROTOCOL_INFO));
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard apr_get_os_sock(&nsd,lr->sd);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard "Parent: Duplicating socket %d and sending it to child process %d", nsd, pi.dwProcessId);
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard if (WSADuplicateSocket(nsd, pi.dwProcessId,
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard lpWSAProtocolInfo) == SOCKET_ERROR) {
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_netos_error(), server_conf,
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard "Parent: WSADuplicateSocket failed for socket %d.", lr->sd );
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return -1;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (!WriteFile(hPipeWrite, lpWSAProtocolInfo, (DWORD) sizeof(WSAPROTOCOL_INFO),
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb &BytesWritten,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb (LPOVERLAPPED) NULL)) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "Parent: Unable to write duplicated socket %d to the child.", lr->sd );
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return -1;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, APR_SUCCESS, server_conf,
efa1a34b0a7785fc72863eff175b0cfc1ecb0e38wrowe "Parent: BytesWritten = %d WSAProtocolInfo = %x20", BytesWritten, *lpWSAProtocolInfo);
117026201e6d8fe7d82416b8a7324830f5a87292wrowe }
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
117026201e6d8fe7d82416b8a7324830f5a87292wrowe /* Now, send the AcceptEx completion port to the child */
117026201e6d8fe7d82416b8a7324830f5a87292wrowe if (!DuplicateHandle(GetCurrentProcess(), AcceptExCompPort,
117026201e6d8fe7d82416b8a7324830f5a87292wrowe pi.hProcess, &hDupedCompPort, 0,
117026201e6d8fe7d82416b8a7324830f5a87292wrowe TRUE, DUPLICATE_SAME_ACCESS)) {
117026201e6d8fe7d82416b8a7324830f5a87292wrowe ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
6c24fd6cfe148639988d5b335185ffb215662801wrowe "Parent: Unable to duplicate AcceptEx completion port. Shutting down.");
117026201e6d8fe7d82416b8a7324830f5a87292wrowe return -1;
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe }
117026201e6d8fe7d82416b8a7324830f5a87292wrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker WriteFile(hPipeWrite, &hDupedCompPort, (DWORD) sizeof(hDupedCompPort), &BytesWritten, (LPOVERLAPPED) NULL);
cadddb2c31d24d48f4017db4df0a29687432326cwrowe }
117026201e6d8fe7d82416b8a7324830f5a87292wrowe
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames CloseHandle(hPipeRead);
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames CloseHandle(hPipeWrite);
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker return 0;
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe}
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowestatic int master_main(server_rec *s, HANDLE shutdown_event, HANDLE restart_event)
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames{
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames int remaining_children_to_start = ap_daemons_to_start;
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames int i;
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames int rv, cld;
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe int child_num = 0;
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe int restart_pending = 0;
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe int shutdown_pending = 0;
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe int current_live_processes = 0; /* number of child process we know about */
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe HANDLE process_handles[MAX_PROCESSES];
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames HANDLE process_kill_events[MAX_PROCESSES];
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames setup_listeners(s);
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe bind_listeners_to_completion_port();
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* Create child process
cadddb2c31d24d48f4017db4df0a29687432326cwrowe * Should only be one in this version of Apache for WIN32
117026201e6d8fe7d82416b8a7324830f5a87292wrowe */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar while (remaining_children_to_start--) {
e7505ba54ac56ae30e4e250f912f3dbaf92ca45fwrowe if (create_process(pconf, process_handles, process_kill_events,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar &current_live_processes) < 0) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
cadddb2c31d24d48f4017db4df0a29687432326cwrowe "master_main: create child process failed. Exiting.");
4f9c22c4f27571d54197be9674e1fc0d528192aestriker shutdown_pending = 1;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker goto die_now;
cadddb2c31d24d48f4017db4df0a29687432326cwrowe }
5b3abd2fecc712f08ad728114aa77137b9f67716wrowe }
85bb5b92490e4f095aae394118fc588a8f4c486fwrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe restart_pending = shutdown_pending = 0;
efa1a34b0a7785fc72863eff175b0cfc1ecb0e38wrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe if (!strcasecmp(signal_arg, "runservice"))
beda1fb2f11c52ca4612460a5d5ba47398143efbwrowe mpm_service_started();
beda1fb2f11c52ca4612460a5d5ba47398143efbwrowe
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* Wait for shutdown or restart events or for child death */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker process_handles[current_live_processes] = shutdown_event;
beda1fb2f11c52ca4612460a5d5ba47398143efbwrowe process_handles[current_live_processes+1] = restart_event;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe rv = WaitForMultipleObjects(current_live_processes+2, (HANDLE *)process_handles,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe FALSE, INFINITE);
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe cld = rv - WAIT_OBJECT_0;
c2cf53a40a9814eb91db2cdf820f97d943f21628coar if (rv == WAIT_FAILED) {
c2cf53a40a9814eb91db2cdf820f97d943f21628coar /* Something serious is wrong */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ap_log_error(APLOG_MARK,APLOG_CRIT, apr_get_os_error(), server_conf,
c2cf53a40a9814eb91db2cdf820f97d943f21628coar "master_main: WaitForMultipeObjects WAIT_FAILED -- doing server shutdown");
117026201e6d8fe7d82416b8a7324830f5a87292wrowe shutdown_pending = 1;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe else if (rv == WAIT_TIMEOUT) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* Hey, this cannot happen */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe "master_main: WaitForMultipeObjects with INFINITE wait exited with WAIT_TIMEOUT");
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar shutdown_pending = 1;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar }
117026201e6d8fe7d82416b8a7324830f5a87292wrowe else if (cld == current_live_processes) {
117026201e6d8fe7d82416b8a7324830f5a87292wrowe /* shutdown_event signalled */
c2cf53a40a9814eb91db2cdf820f97d943f21628coar shutdown_pending = 1;
117026201e6d8fe7d82416b8a7324830f5a87292wrowe printf("shutdown event signaled\n");
1067418d9ed9ed9daeb3ca4f74e72db810c49833wrowe ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, s,
cadddb2c31d24d48f4017db4df0a29687432326cwrowe "Parent: SHUTDOWN EVENT SIGNALED -- Shutting down the server.");
cadddb2c31d24d48f4017db4df0a29687432326cwrowe if (ResetEvent(shutdown_event) == 0) {
cadddb2c31d24d48f4017db4df0a29687432326cwrowe ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
cadddb2c31d24d48f4017db4df0a29687432326cwrowe "ResetEvent(shutdown_event)");
4f9c22c4f27571d54197be9674e1fc0d528192aestriker }
c2cf53a40a9814eb91db2cdf820f97d943f21628coar
c2cf53a40a9814eb91db2cdf820f97d943f21628coar }
cadddb2c31d24d48f4017db4df0a29687432326cwrowe else if (cld == current_live_processes+1) {
cadddb2c31d24d48f4017db4df0a29687432326cwrowe /* restart_event signalled */
cadddb2c31d24d48f4017db4df0a29687432326cwrowe int children_to_kill = current_live_processes;
cadddb2c31d24d48f4017db4df0a29687432326cwrowe restart_pending = 1;
cadddb2c31d24d48f4017db4df0a29687432326cwrowe ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, s,
cadddb2c31d24d48f4017db4df0a29687432326cwrowe "Parent: RESTART EVENT SIGNALED -- Restarting the server.");
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe if (ResetEvent(restart_event) == 0) {
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
cadddb2c31d24d48f4017db4df0a29687432326cwrowe "master_main: ResetEvent(restart_event) failed.");
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe }
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe /* Signal each child process to die
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe * We are making a big assumption here that the child process, once signaled,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * will REALLY go away. Since this is a restart, we do not want to hold the
cadddb2c31d24d48f4017db4df0a29687432326cwrowe * new child process up waiting for the old child to die. Remove the old
cadddb2c31d24d48f4017db4df0a29687432326cwrowe * child out of the process_handles apr_table_t and hope for the best...
4f9c22c4f27571d54197be9674e1fc0d528192aestriker */
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe for (i = 0; i < children_to_kill; i++) {
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe if (SetEvent(process_kill_events[i]) == 0)
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "master_main: SetEvent for child process in slot #%d failed", i);
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe cleanup_process(process_handles, process_kill_events, i, &current_live_processes);
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe }
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe }
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe else {
cadddb2c31d24d48f4017db4df0a29687432326cwrowe /* A child process must have exited because of a fatal error condition (seg fault, etc.).
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * Remove the dead process
1067418d9ed9ed9daeb3ca4f74e72db810c49833wrowe * from the process_handles and process_kill_events apr_table_t and create a new
1067418d9ed9ed9daeb3ca4f74e72db810c49833wrowe * child process.
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * TODO: Consider restarting the child immediately without looping through http_main
1067418d9ed9ed9daeb3ca4f74e72db810c49833wrowe * and without rereading the configuration. Will need this if we ever support multiple
117026201e6d8fe7d82416b8a7324830f5a87292wrowe * children. One option, create a parent thread which waits on child death and restarts it.
117026201e6d8fe7d82416b8a7324830f5a87292wrowe * Consider, however, that if the user makes httpd.conf invalid, we want to die before
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe * our child tries it... otherwise we have a nasty loop.
c2cf53a40a9814eb91db2cdf820f97d943f21628coar */
c2cf53a40a9814eb91db2cdf820f97d943f21628coar restart_pending = 1;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "Parent: CHILD PROCESS FAILED -- Restarting the child process.");
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_assert(cld < current_live_processes);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb cleanup_process(process_handles, process_kill_events, cld, &current_live_processes);
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe /* APD2("main_process: child in slot %d died", rv); */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* restart_child(process_hancles, process_kill_events, cld, &current_live_processes); */
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe
b45c1c292ff1fa635004ae81fa691f8cb3cdda85rbb /* Drain the AcceptEx completion port of any outstanding I/O pending for the dead
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * process. */
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe drain_acceptex_complport(AcceptExCompPort, FALSE);
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe }
ecc4a080f07af3fbc1b91bbd00997ec1d592c6f9wrowe
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowedie_now:
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar if (shutdown_pending)
2d2dadb81bf34e3bc9321eabcd971a738431b364wrowe {
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar int tmstart = time(NULL);
4f9c22c4f27571d54197be9674e1fc0d528192aestriker
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe if (strcasecmp(signal_arg, "runservice")) {
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe mpm_service_stopping();
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe }
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe /* Signal each child processes to die */
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe for (i = 0; i < current_live_processes; i++) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker printf("SetEvent handle = %d\n", process_kill_events[i]);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar if (SetEvent(process_kill_events[i]) == 0)
ecc4a080f07af3fbc1b91bbd00997ec1d592c6f9wrowe ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar "master_main: SetEvent for child process in slot #%d failed", i);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb while (current_live_processes && ((tmstart+60) > time(NULL))) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker rv = WaitForMultipleObjects(current_live_processes, (HANDLE *)process_handles, FALSE, 2000);
5b3abd2fecc712f08ad728114aa77137b9f67716wrowe if (rv == WAIT_TIMEOUT)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb continue;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_assert(rv != WAIT_FAILED);
af7e32b660b02a378e91d40987e59b28864db954jwoolley cld = rv - WAIT_OBJECT_0;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_assert(rv < current_live_processes);
c2cf53a40a9814eb91db2cdf820f97d943f21628coar cleanup_process(process_handles, process_kill_events, cld, &current_live_processes);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe }
0540a0b469147b52e858587270dba31c2aaa9e09wrowe for (i = 0; i < current_live_processes; i++) {
0540a0b469147b52e858587270dba31c2aaa9e09wrowe ap_log_error(APLOG_MARK,APLOG_ERR|APLOG_NOERRNO, APR_SUCCESS, server_conf,
c2cf53a40a9814eb91db2cdf820f97d943f21628coar "Parent: Forcing termination of child #%d (handle %d)", i, process_handles[i]);
c2cf53a40a9814eb91db2cdf820f97d943f21628coar TerminateProcess((HANDLE) process_handles[i], 1);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe }
c2cf53a40a9814eb91db2cdf820f97d943f21628coar return 0; /* Tell the caller we do not want to restart */
0540a0b469147b52e858587270dba31c2aaa9e09wrowe }
0540a0b469147b52e858587270dba31c2aaa9e09wrowe
0540a0b469147b52e858587270dba31c2aaa9e09wrowe return 1; /* Tell the caller we want a restart */
0540a0b469147b52e858587270dba31c2aaa9e09wrowe}
c2cf53a40a9814eb91db2cdf820f97d943f21628coar
0540a0b469147b52e858587270dba31c2aaa9e09wrowe
c2cf53a40a9814eb91db2cdf820f97d943f21628coar#define SERVICE_UNNAMED (-1)
0540a0b469147b52e858587270dba31c2aaa9e09wrowe
7763a4beb8afca9c8f93db0cb6836124901af52awrowe/* service_nt_main_fn needs to append the StartService() args
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * outside of our call stack and thread as the service starts...
0540a0b469147b52e858587270dba31c2aaa9e09wrowe */
c2cf53a40a9814eb91db2cdf820f97d943f21628coarapr_array_header_t *mpm_new_argv;
c2cf53a40a9814eb91db2cdf820f97d943f21628coar
0540a0b469147b52e858587270dba31c2aaa9e09wrowe/* Remember service_to_start failures to log and fail in pre_config.
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * Remember inst_argc and inst_argv for installing or starting the
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * service after we preflight the config.
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz */
0540a0b469147b52e858587270dba31c2aaa9e09wrowe
0540a0b469147b52e858587270dba31c2aaa9e09wrowestatic apr_status_t service_to_start_success;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int inst_argc;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic const char * const *inst_argv;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz
5b3abd2fecc712f08ad728114aa77137b9f67716wrowevoid winnt_rewrite_args(process_rec *process)
5b3abd2fecc712f08ad728114aa77137b9f67716wrowe{
5b3abd2fecc712f08ad728114aa77137b9f67716wrowe /* Handle the following SCM aspects in this phase:
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * -k runservice [transition for WinNT, nothing for Win9x]
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * -k (!)install [error out if name is not installed]
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * We can't leave this phase until we know our identity
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * and modify the command arguments appropriately.
58097d7d8d1a394092374b9f6ddf76b7993724a4rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_status_t service_named = SERVICE_UNNAMED;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_status_t rv;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe char *def_server_root;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb char fnbuf[MAX_PATH];
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb char optbuf[3];
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe const char *optarg;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb int fixed_args;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb char *pid;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_getopt_t *opt;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb int running_as_service = 1;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard GetVersionEx(&osver);
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard /* AP_PARENT_PID is only valid in the child */
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard pid = getenv("AP_PARENT_PID");
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard if (pid)
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard {
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard /* This is the child */
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard my_pid = GetCurrentProcessId();
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard parent_pid = (DWORD) atol(pid);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* The parent is responsible for providing the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * COMPLETE ARGUMENTS REQUIRED to the child.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * No further argument parsing is needed, but
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * for good measure we will provide a simple
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * signal string for later testing.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb signal_arg = "runchild";
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* This is the parent, we have a long way to go :-) */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb parent_pid = my_pid = GetCurrentProcessId();
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Rewrite process->argv[];
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * strip out -k signal into signal_arg
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * strip out -n servicename into service_name & display_name
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * add default -d serverroot from the path of this executable
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The end result will look like:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The invocation command (%0)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The -d serverroot default from the running executable
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The requested service's (-n) registry ConfigArgs
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * The WinNT SCM's StartService() args
4f9c22c4f27571d54197be9674e1fc0d528192aestriker */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (!GetModuleFileName(NULL, fnbuf, sizeof(fnbuf))) {
rv = apr_get_os_error();
ap_log_error(APLOG_MARK,APLOG_ERR, rv, NULL,
"Failed to get the path of Apache.exe");
exit(1);
}
/* WARNING: There is an implict assumption here that the
* executable resides in ServerRoot or ServerRoot\bin
*/
def_server_root = (char *) apr_filename_of_pathname(fnbuf);
if (def_server_root > fnbuf) {
*(def_server_root - 1) = '\0';
def_server_root = (char *) apr_filename_of_pathname(fnbuf);
if (!strcasecmp(def_server_root, "bin"))
*(def_server_root - 1) = '\0';
}
def_server_root = ap_os_canonical_filename(process->pool, fnbuf);
/* Use process->pool so that the rewritten argv
* lasts for the lifetime of the server process,
* because pconf will be destroyed after the
* initial pre-flight of the config parser.
*/
mpm_new_argv = apr_make_array(process->pool, process->argc + 2,
sizeof(const char *));
*(const char **)apr_push_array(mpm_new_argv) = process->argv[0];
*(const char **)apr_push_array(mpm_new_argv) = "-d";
*(const char **)apr_push_array(mpm_new_argv) = def_server_root;
fixed_args = mpm_new_argv->nelts;
optbuf[0] = '-';
optbuf[2] = '\0';
apr_initopt(&opt, process->pool, process->argc, (char**) process->argv);
while (apr_getopt(opt, "n:k:iu" AP_SERVER_BASEARGS,
optbuf + 1, &optarg) == APR_SUCCESS) {
switch (optbuf[1]) {
case 'n':
service_named = mpm_service_set_name(process->pool, optarg);
break;
case 'k':
signal_arg = optarg;
break;
case 'i':
ap_log_error(APLOG_MARK,APLOG_WARNING, 0, NULL,
"-i is deprecated. Use -k install.");
signal_arg = "install";
break;
case 'u':
ap_log_error(APLOG_MARK,APLOG_WARNING, 0, NULL,
"-u is deprecated. Use -k uninstall.");
signal_arg = "uninstall";
break;
default:
*(const char **)apr_push_array(mpm_new_argv) =
apr_pstrdup(process->pool, optbuf);
if (optarg) {
*(const char **)apr_push_array(mpm_new_argv) = optarg;
}
break;
}
}
/* Track the number of args actually entered by the user */
inst_argc = mpm_new_argv->nelts - fixed_args;
/* Provide a default 'run' -k arg to simplify signal_arg tests */
if (!signal_arg)
{
signal_arg = "run";
running_as_service = 0;
}
if (!strcasecmp(signal_arg, "runservice"))
{
/* Start the NT Service _NOW_ because the WinNT SCM is
* expecting us to rapidly assume control of our own
* process, the SCM will tell us our service name, and
* may have extra StartService() command arguments to
* add for us.
*
* Any other process has a console, so we don't to begin
* a Win9x service until the configuration is parsed and
* any command line errors are reported.
*
* We hold the return value so that we can die in pre_config
* after logging begins, and the failure can land in the log.
*/
if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
service_to_start_success = mpm_service_to_start();
if (service_to_start_success == APR_SUCCESS)
service_named = APR_SUCCESS;
}
}
if (service_named == SERVICE_UNNAMED && running_as_service) {
service_named = mpm_service_set_name(process->pool,
DEFAULT_SERVICE_NAME);
}
if (!strcasecmp(signal_arg, "install")) /* -k install */
{
if (service_named == APR_SUCCESS)
{
ap_log_error(APLOG_MARK,APLOG_ERR, 0, NULL,
"%s: Service is already installed.", display_name);
exit(1);
}
}
else if (running_as_service)
{
if (service_named == APR_SUCCESS)
{
rv = mpm_merge_service_args(process->pool, mpm_new_argv,
fixed_args);
if (rv == APR_SUCCESS) {
ap_log_error(APLOG_MARK,APLOG_NOERRNO|APLOG_INFO, 0, NULL,
"Using ConfigArgs of the installed service "
"\"%s\".", display_name);
}
else {
ap_log_error(APLOG_MARK,APLOG_INFO, rv, NULL,
"No installed ConfigArgs for the service "
"\"%s\", using Apache defaults.", display_name);
}
}
else
{
ap_log_error(APLOG_MARK,APLOG_INFO|APLOG_NOERRNO, 0, NULL,
"No installed service named \"%s\".", display_name);
exit(1);
}
}
/* Track the args actually entered by the user.
* These will be used for the -k install parameters, as well as
* for the -k start service override arguments.
*/
inst_argv = (const char * const *)mpm_new_argv->elts
+ mpm_new_argv->nelts - inst_argc;
process->argc = mpm_new_argv->nelts;
process->argv = (const char * const *) mpm_new_argv->elts;
}
static void winnt_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
{
/* Handle the following SCM aspects in this phase:
*
* -k runservice [WinNT errors logged from rewrite_args]
* -k uninstall
* -k stop
*
* in these cases we -don't- care if httpd.conf has config errors!
*/
apr_status_t rv;
if (ap_exists_config_define("ONE_PROCESS"))
one_process = -1;
if (ap_exists_config_define("ONE_PROCESS"))
one_process = -1;
if (!strcasecmp(signal_arg, "runservice")
&& (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
&& (service_to_start_success != APR_SUCCESS)) {
ap_log_error(APLOG_MARK,APLOG_ERR, service_to_start_success, NULL,
"%s: Unable to start the service manager.",
display_name);
exit(1);
}
if (!strcasecmp(signal_arg, "uninstall")) {
rv = mpm_service_uninstall();
exit(rv);
}
if (!strcasecmp(signal_arg, "stop")) {
mpm_signal_service(ptemp, 0);
exit(0);
}
ap_listen_pre_config();
ap_daemons_to_start = DEFAULT_NUM_DAEMON;
ap_threads_per_child = DEFAULT_START_THREAD;
ap_pid_fname = DEFAULT_PIDLOG;
max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
}
static void winnt_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec* server)
{
static int restart_num = 0;
apr_status_t rv = 0;
server_conf = server;
/* Handle the following SCM aspects in this phase:
*
* -k install
* -k start
* -k restart
* -k runservice [Win95, only once - after we parsed the config]
*
* because all of these signals are useful _only_ if there
* is a valid conf\httpd.conf environment to start.
*
* We reached this phase by avoiding errors that would cause
* these options to fail unexpectedly in another process.
*/
if (!strcasecmp(signal_arg, "install")) {
rv = mpm_service_install(ptemp, inst_argc, inst_argv);
exit (rv);
}
if (!strcasecmp(signal_arg, "start")) {
rv = mpm_service_start(ptemp, inst_argc, inst_argv);
exit (rv);
}
if (!strcasecmp(signal_arg, "restart")) {
mpm_signal_service(ptemp, 1);
exit (rv);
}
if (parent_pid == my_pid)
{
if (restart_num++ == 1)
{
/* This code should be run once in the parent and not run
* across a restart
*/
PSECURITY_ATTRIBUTES sa = GetNullACL(); /* returns NULL if invalid (Win95?) */
setup_signal_names(apr_psprintf(pconf,"ap%d", parent_pid));
if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
/* Create the AcceptEx IoCompletionPort once in the parent.
* The completion port persists across restarts.
*/
AcceptExCompPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
NULL,
0,
0); /* CONCURRENT ACTIVE THREADS */
if (AcceptExCompPort == NULL) {
ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
"Parent: Unable to create the AcceptExCompletionPort -- process will exit");
exit(1);
}
}
ap_log_pid(pconf, ap_pid_fname);
/* Create shutdown event, apPID_shutdown, where PID is the parent
* Apache process ID. Shutdown is signaled by 'apache -k shutdown'.
*/
shutdown_event = CreateEvent(sa, FALSE, FALSE, signal_shutdown_name);
if (!shutdown_event) {
ap_log_error(APLOG_MARK, APLOG_EMERG, apr_get_os_error(), server_conf,
"Parent: Cannot create shutdown event %s", signal_shutdown_name);
CleanNullACL((void *)sa);
exit(1);
}
/* Create restart event, apPID_restart, where PID is the parent
* Apache process ID. Restart is signaled by 'apache -k restart'.
*/
restart_event = CreateEvent(sa, FALSE, FALSE, signal_restart_name);
if (!restart_event) {
CloseHandle(shutdown_event);
ap_log_error(APLOG_MARK, APLOG_EMERG, apr_get_os_error(), server_conf,
"Parent: Cannot create restart event %s", signal_restart_name);
CleanNullACL((void *)sa);
exit(1);
}
CleanNullACL((void *)sa);
/* Now that we are flying at 15000 feet...
* wipe out the Win95 service console,
* signal the SCM the WinNT service started, or
* if not a service, setup console handlers instead.
*/
if (!strcasecmp(signal_arg, "runservice"))
{
if (osver.dwPlatformId != VER_PLATFORM_WIN32_NT)
{
rv = mpm_service_to_start();
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK,APLOG_ERR, rv, server_conf,
"%s: Unable to start the service manager.",
display_name);
exit(1);
}
}
}
else /* ! -k runservice */
{
mpm_start_console_handler();
}
/* Create the start mutex, apPID, where PID is the parent Apache process ID.
* Ths start mutex is used during a restart to prevent more than one
* child process from entering the accept loop at once.
*/
apr_create_lock(&start_mutex,APR_MUTEX, APR_CROSS_PROCESS, signal_name_prefix,
server_conf->process->pool);
}
}
else /* parent_pid != my_pid */
{
mpm_start_child_console_handler();
}
}
AP_DECLARE(int) ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s )
{
static int restart = 0; /* Default is "not a restart" */
pconf = _pconf;
server_conf = s;
if ((parent_pid != my_pid) || one_process) {
/* Running as Child process or in one_process (debug) mode */
ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
"Child %d: Child process is running", my_pid);
child_main();
ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
"Child %d: Child process is exiting", my_pid);
return 1;
} /* Child or single process */
else { /* Parent process */
restart = master_main(server_conf, shutdown_event, restart_event);
if (!restart) {
/* Shutting down. Clean up... */
const char *pidfile = ap_server_root_relative (pconf, ap_pid_fname);
if (pidfile != NULL && unlink(pidfile) == 0) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS,
server_conf, "removed PID file %s (pid=%ld)",
pidfile, GetCurrentProcessId());
}
apr_destroy_lock(start_mutex);
CloseHandle(restart_event);
CloseHandle(shutdown_event);
return 1;
}
} /* Parent process */
return 0; /* Restart */
}
static void winnt_hooks(apr_pool_t *p)
{
ap_hook_pre_config(winnt_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_post_config(winnt_post_config, NULL, NULL, 0);
}
/*
* Command processors
*/
static const char *set_pidfile(cmd_parms *cmd, void *dummy, char *arg)
{
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
if (cmd->server->is_virtual) {
return "PidFile directive not allowed in <VirtualHost>";
}
ap_pid_fname = arg;
return NULL;
}
static const char *set_threads_per_child (cmd_parms *cmd, void *dummy, char *arg)
{
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
ap_threads_per_child = atoi(arg);
if (ap_threads_per_child > HARD_THREAD_LIMIT) {
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"WARNING: ThreadsPerChild of %d exceeds compile time"
" limit of %d threads,", ap_threads_per_child,
HARD_THREAD_LIMIT);
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
" lowering ThreadsPerChild to %d. To increase, please"
" see the HARD_THREAD_LIMIT define in %s.",
HARD_THREAD_LIMIT, AP_MPM_HARD_LIMITS_FILE);
ap_threads_per_child = HARD_THREAD_LIMIT;
}
else if (ap_threads_per_child < 1) {
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
"WARNING: Require ThreadsPerChild > 0, setting to 1");
ap_threads_per_child = 1;
}
return NULL;
}
static const char *set_max_requests(cmd_parms *cmd, void *dummy, char *arg)
{
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
max_requests_per_child = atoi(arg);
return NULL;
}
static const char *set_coredumpdir (cmd_parms *cmd, void *dummy, char *arg)
{
apr_finfo_t finfo;
const char *fname;
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
fname = ap_server_root_relative(cmd->pool, arg);
if ((apr_stat(&finfo, fname, APR_FINFO_TYPE, cmd->pool) != APR_SUCCESS)
|| (finfo.filetype != APR_DIR)) {
return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
" does not exist or is not a directory", NULL);
}
apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
return NULL;
}
/* Stub functions until this MPM supports the connection status API */
AP_DECLARE(void) ap_update_connection_status(long conn_id, const char *key, \
const char *value)
{
/* NOP */
}
AP_DECLARE(void) ap_reset_connection_status(long conn_id)
{
/* NOP */
}
AP_DECLARE(apr_array_header_t *) ap_get_status_table(apr_pool_t *p)
{
/* NOP */
return NULL;
}
static const command_rec winnt_cmds[] = {
LISTEN_COMMANDS
{ "PidFile", set_pidfile, NULL, RSRC_CONF, TAKE1,
"A file for logging the server process ID"},
{ "ThreadsPerChild", set_threads_per_child, NULL, RSRC_CONF, TAKE1,
"Number of threads each child creates" },
{ "MaxRequestsPerChild", set_max_requests, NULL, RSRC_CONF, TAKE1,
"Maximum number of requests a particular child serves before dying." },
{ "CoreDumpDirectory", set_coredumpdir, NULL, RSRC_CONF, TAKE1,
"The location of the directory Apache changes to before dumping core" },
{ NULL }
};
AP_MODULE_DECLARE_DATA module mpm_winnt_module = {
MPM20_MODULE_STUFF,
winnt_rewrite_args, /* hook to run before apache parses args */
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
winnt_cmds, /* command apr_table_t */
winnt_hooks /* register_hooks */
};