summaryrefslogtreecommitdiff
path: root/server/mpm
diff options
context:
space:
mode:
authorStefan Fritsch <sf@sfritsch.de>2011-12-27 19:42:03 +0100
committerStefan Fritsch <sf@sfritsch.de>2011-12-27 19:42:03 +0100
commit80db94fff6a9620fb469ee911347ed973e3f7735 (patch)
tree35ccde4018b7e6b84103e5e85dc1085ef9e7d6c2 /server/mpm
downloadapache2-80db94fff6a9620fb469ee911347ed973e3f7735.tar.gz
Upstream tarball 2.2.3upstream/2.2.3
Diffstat (limited to 'server/mpm')
-rw-r--r--server/mpm/MPM.NAMING15
-rw-r--r--server/mpm/Makefile.in4
-rw-r--r--server/mpm/beos/Makefile.in5
-rw-r--r--server/mpm/beos/beos.c1207
-rw-r--r--server/mpm/beos/beos.h34
-rw-r--r--server/mpm/beos/config5.m47
-rw-r--r--server/mpm/beos/mpm.h50
-rw-r--r--server/mpm/beos/mpm_default.h84
-rw-r--r--server/mpm/config.m479
-rw-r--r--server/mpm/experimental/event/Makefile.in5
-rw-r--r--server/mpm/experimental/event/config5.m46
-rw-r--r--server/mpm/experimental/event/event.c2456
-rw-r--r--server/mpm/experimental/event/fdqueue.c420
-rw-r--r--server/mpm/experimental/event/fdqueue.h82
-rw-r--r--server/mpm/experimental/event/mpm.h62
-rw-r--r--server/mpm/experimental/event/mpm_default.h79
-rw-r--r--server/mpm/experimental/event/pod.c109
-rw-r--r--server/mpm/experimental/event/pod.h60
-rw-r--r--server/mpm/mpmt_os2/Makefile.in5
-rw-r--r--server/mpm/mpmt_os2/config5.m45
-rw-r--r--server/mpm/mpmt_os2/mpm.h43
-rw-r--r--server/mpm/mpmt_os2/mpm_default.h68
-rw-r--r--server/mpm/mpmt_os2/mpmt_os2.c577
-rw-r--r--server/mpm/mpmt_os2/mpmt_os2_child.c480
-rw-r--r--server/mpm/netware/mpm.h58
-rw-r--r--server/mpm/netware/mpm_default.h122
-rw-r--r--server/mpm/netware/mpm_netware.c1291
-rw-r--r--server/mpm/prefork/Makefile.in5
-rw-r--r--server/mpm/prefork/config.m43
-rw-r--r--server/mpm/prefork/mpm.h62
-rw-r--r--server/mpm/prefork/mpm_default.h74
-rw-r--r--server/mpm/prefork/prefork.c1478
-rw-r--r--server/mpm/winnt/Win9xConHook.c697
-rw-r--r--server/mpm/winnt/Win9xConHook.def10
-rw-r--r--server/mpm/winnt/Win9xConHook.dsp103
-rw-r--r--server/mpm/winnt/Win9xConHook.h66
-rw-r--r--server/mpm/winnt/child.c1157
-rw-r--r--server/mpm/winnt/mpm.h49
-rw-r--r--server/mpm/winnt/mpm_default.h89
-rw-r--r--server/mpm/winnt/mpm_winnt.c1724
-rw-r--r--server/mpm/winnt/mpm_winnt.h130
-rw-r--r--server/mpm/winnt/nt_eventlog.c189
-rw-r--r--server/mpm/winnt/service.c1346
-rw-r--r--server/mpm/worker/Makefile.in5
-rw-r--r--server/mpm/worker/config5.m46
-rw-r--r--server/mpm/worker/fdqueue.c384
-rw-r--r--server/mpm/worker/fdqueue.h73
-rw-r--r--server/mpm/worker/mpm.h62
-rw-r--r--server/mpm/worker/mpm_default.h78
-rw-r--r--server/mpm/worker/pod.c110
-rw-r--r--server/mpm/worker/pod.h59
-rw-r--r--server/mpm/worker/worker.c2262
52 files changed, 17634 insertions, 0 deletions
diff --git a/server/mpm/MPM.NAMING b/server/mpm/MPM.NAMING
new file mode 100644
index 00000000..83c0694d
--- /dev/null
+++ b/server/mpm/MPM.NAMING
@@ -0,0 +1,15 @@
+
+The following MPMs currently exist:
+
+ prefork ....... Multi Process Model with Preforking (Apache 1.3)
+ perchild ...... Multi Process Model with Threading.
+ Constant number of processes, variable number of threads
+ each child process can have a different uid/gid.
+ mpmt_os2 ...... Multi Process Model with Threading on OS/2
+ Constant number of processes, variable number of threads.
+ One acceptor thread per process, multiple workers threads.
+ winnt ......... Single Process Model with Threading on Windows NT
+ worker ........ Multi Process model with threads. One acceptor thread,
+ multiple worker threads.
+ netware ....... Multi-threaded MPM for Netware
+ beos .......... Single Process Model with Threading on BeOS
diff --git a/server/mpm/Makefile.in b/server/mpm/Makefile.in
new file mode 100644
index 00000000..2decbde6
--- /dev/null
+++ b/server/mpm/Makefile.in
@@ -0,0 +1,4 @@
+
+SUBDIRS = $(MPM_SUBDIR_NAME)
+
+include $(top_builddir)/build/rules.mk
diff --git a/server/mpm/beos/Makefile.in b/server/mpm/beos/Makefile.in
new file mode 100644
index 00000000..3f88b041
--- /dev/null
+++ b/server/mpm/beos/Makefile.in
@@ -0,0 +1,5 @@
+
+LTLIBRARY_NAME = libbeos.la
+LTLIBRARY_SOURCES = beos.c
+
+include $(top_srcdir)/build/ltlib.mk
diff --git a/server/mpm/beos/beos.c b/server/mpm/beos/beos.c
new file mode 100644
index 00000000..b7e4671b
--- /dev/null
+++ b/server/mpm/beos/beos.c
@@ -0,0 +1,1207 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* The BeOS MPM!
+ *
+ * This is a single process, with multiple worker threads.
+ *
+ * Under testing I found that given the inability of BeOS to handle threads
+ * and forks it didn't make sense to try and have a set of "children" threads
+ * that spawned the "worker" threads, so just missed out the middle mand and
+ * somehow arrived here.
+ *
+ * For 2.1 this has been rewritten to have simpler logic, though there is still
+ * some simplification that can be done. It's still a work in progress!
+ *
+ * TODO Items
+ *
+ * - on exit most worker threads segfault trying to access a kernel page.
+ */
+
+#define CORE_PRIVATE
+
+#include <kernel/OS.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <signal.h>
+
+#include "apr_strings.h"
+#include "apr_portable.h"
+#include "httpd.h"
+#include "http_main.h"
+#include "http_log.h"
+#include "http_config.h" /* for read_config */
+#include "http_core.h" /* for get_remote_host */
+#include "http_connection.h"
+#include "ap_mpm.h"
+#include "beosd.h"
+#include "ap_listen.h"
+#include "scoreboard.h"
+#include "mpm_common.h"
+#include "mpm.h"
+#include "mpm_default.h"
+#include "apr_thread_mutex.h"
+#include "apr_poll.h"
+
+extern int _kset_fd_limit_(int num);
+
+/* Limit on the total --- clients will be locked out if more servers than
+ * this are needed. It is intended solely to keep the server from crashing
+ * when things get out of hand.
+ *
+ * We keep a hard maximum number of servers, for two reasons:
+ * 1) in case something goes seriously wrong, we want to stop the server starting
+ * threads ad infinitum and crashing the server (remember that BeOS has a 192
+ * thread per team limit).
+ * 2) it keeps the size of the scoreboard file small
+ * enough that we can read the whole thing without worrying too much about
+ * the overhead.
+ */
+
+/* we only ever have 1 main process running... */
+#define HARD_SERVER_LIMIT 1
+
+/* Limit on the threads per process. Clients will be locked out if more than
+ * this * HARD_SERVER_LIMIT are needed.
+ *
+ * We keep this for one reason it keeps the size of the scoreboard file small
+ * enough that we can read the whole thing without worrying too much about
+ * the overhead.
+ */
+#ifdef NO_THREADS
+#define HARD_THREAD_LIMIT 1
+#endif
+#ifndef HARD_THREAD_LIMIT
+#define HARD_THREAD_LIMIT 50
+#endif
+
+/*
+ * Actual definitions of config globals
+ */
+
+static int ap_threads_to_start=0;
+static int ap_max_requests_per_thread = 0;
+static int min_spare_threads=0;
+static int max_spare_threads=0;
+static int ap_thread_limit=0;
+static int num_listening_sockets = 0;
+static int mpm_state = AP_MPMQ_STARTING;
+apr_thread_mutex_t *accept_mutex = NULL;
+
+static apr_pool_t *pconf; /* Pool for config stuff */
+
+static int server_pid;
+
+
+/*
+ * The max child slot ever assigned, preserved across restarts. Necessary
+ * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts. We use
+ * this value to optimize routines that have to scan the entire scoreboard.
+ */
+int ap_max_child_assigned = -1;
+int ap_max_threads_limit = -1;
+
+static apr_socket_t *udp_sock;
+static apr_sockaddr_t *udp_sa;
+
+server_rec *ap_server_conf;
+
+/* one_process */
+static int one_process = 0;
+
+#ifdef DEBUG_SIGSTOP
+int raise_sigstop_flags;
+#endif
+
+static void check_restart(void *data);
+
+/* When a worker thread gets to the end of it's life it dies with an
+ * exit value of the code supplied to this function. The thread has
+ * already had check_restart() registered to be called when dying, so
+ * we don't concern ourselves with restarting at all here. We do however
+ * mark the scoreboard slot as belonging to a dead server and zero out
+ * it's thread_id.
+ *
+ * TODO - use the status we set to determine if we need to restart the
+ * thread.
+ */
+static void clean_child_exit(int code, int slot)
+{
+ (void) ap_update_child_status_from_indexes(0, slot, SERVER_DEAD,
+ (request_rec*)NULL);
+ ap_scoreboard_image->servers[0][slot].tid = 0;
+ exit_thread(code);
+}
+
+
+/*****************************************************************
+ * Connection structures and accounting...
+ */
+
+/* volatile just in case */
+static int volatile shutdown_pending;
+static int volatile restart_pending;
+static int volatile is_graceful;
+static int volatile child_fatal;
+ap_generation_t volatile ap_my_generation = 0;
+
+/*
+ * ap_start_shutdown() and ap_start_restart(), below, are a first stab at
+ * functions to initiate shutdown or restart without relying on signals.
+ * Previously this was initiated in sig_term() and restart() signal handlers,
+ * but we want to be able to start a shutdown/restart from other sources --
+ * e.g. on Win32, from the service manager. Now the service manager can
+ * call ap_start_shutdown() or ap_start_restart() as appropiate. Note that
+ * these functions can also be called by the child processes, since global
+ * variables are no longer used to pass on the required action to the parent.
+ *
+ * These should only be called from the parent process itself, since the
+ * parent process will use the shutdown_pending and restart_pending variables
+ * to determine whether to shutdown or restart. The child process should
+ * call signal_parent() directly to tell the parent to die -- this will
+ * cause neither of those variable to be set, which the parent will
+ * assume means something serious is wrong (which it will be, for the
+ * child to force an exit) and so do an exit anyway.
+ */
+
+static void ap_start_shutdown(void)
+{
+ /* If the user tries to shut us down twice in quick succession then we
+ * may well get triggered while we are working through previous attempt
+ * to shutdown. We won't worry about even reporting it as it seems a little
+ * pointless.
+ */
+ if (shutdown_pending == 1)
+ return;
+
+ shutdown_pending = 1;
+}
+
+/* do a graceful restart if graceful == 1 */
+static void ap_start_restart(int graceful)
+{
+ if (restart_pending == 1) {
+ /* Probably not an error - don't bother reporting it */
+ return;
+ }
+ restart_pending = 1;
+ is_graceful = graceful;
+}
+
+/* sig_coredump attempts to handle all the potential signals we
+ * may get that should result in a core dump. This is called from
+ * the signal handler routine, so when we enter we are essentially blocked
+ * on the signal. Once we exit we will allow the signal to be processed by
+ * system, which may or may not produce a .core file. All this function does
+ * is try and respect the users wishes about where that file should be
+ * located (chdir) and then signal the parent with the signal.
+ *
+ * If we called abort() the parent would only see SIGABRT which doesn't provide
+ * as much information.
+ */
+static void sig_coredump(int sig)
+{
+ chdir(ap_coredump_dir);
+ signal(sig, SIG_DFL);
+ kill(server_pid, sig);
+}
+
+static void sig_term(int sig)
+{
+ ap_start_shutdown();
+}
+
+static void restart(int sig)
+{
+ ap_start_restart(sig == AP_SIG_GRACEFUL);
+}
+
+/* Handle queries about our inner workings... */
+AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
+{
+ switch(query_code){
+ case AP_MPMQ_MAX_DAEMON_USED:
+ *result = ap_max_child_assigned;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_THREADED:
+ *result = AP_MPMQ_DYNAMIC;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_FORKED:
+ *result = AP_MPMQ_NOT_SUPPORTED;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_DAEMONS:
+ *result = HARD_SERVER_LIMIT;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_THREADS:
+ *result = HARD_THREAD_LIMIT;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_THREADS:
+ *result = HARD_THREAD_LIMIT;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_THREADS:
+ *result = max_spare_threads;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_THREADS:
+ *result = min_spare_threads;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_REQUESTS_DAEMON:
+ *result = ap_max_requests_per_thread;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_DAEMONS:
+ *result = HARD_SERVER_LIMIT;
+ return APR_SUCCESS;
+ case AP_MPMQ_MPM_STATE:
+ *result = mpm_state;
+ return APR_SUCCESS;
+ }
+ return APR_ENOTIMPL;
+}
+
+/* This accepts a connection and allows us to handle the error codes better than
+ * the previous code, while also making it more obvious.
+ */
+static apr_status_t beos_accept(void **accepted, ap_listen_rec *lr, apr_pool_t *ptrans)
+{
+ apr_socket_t *csd;
+ apr_status_t status;
+ int sockdes;
+
+ *accepted = NULL;
+ status = apr_socket_accept(&csd, lr->sd, ptrans);
+ if (status == APR_SUCCESS) {
+ *accepted = csd;
+ apr_os_sock_get(&sockdes, csd);
+ return status;
+ }
+
+ if (APR_STATUS_IS_EINTR(status)) {
+ return status;
+ }
+ /* This switch statement provides us with better error details. */
+ switch (status) {
+#ifdef ECONNABORTED
+ case ECONNABORTED:
+#endif
+#ifdef ETIMEDOUT
+ case ETIMEDOUT:
+#endif
+#ifdef EHOSTUNREACH
+ case EHOSTUNREACH:
+#endif
+#ifdef ENETUNREACH
+ case ENETUNREACH:
+#endif
+ break;
+#ifdef ENETDOWN
+ case ENETDOWN:
+ /*
+ * When the network layer has been shut down, there
+ * is not much use in simply exiting: the parent
+ * would simply re-create us (and we'd fail again).
+ * Use the CHILDFATAL code to tear the server down.
+ * @@@ Martin's idea for possible improvement:
+ * A different approach would be to define
+ * a new APEXIT_NETDOWN exit code, the reception
+ * of which would make the parent shutdown all
+ * children, then idle-loop until it detected that
+ * the network is up again, and restart the children.
+ * Ben Hyde noted that temporary ENETDOWN situations
+ * occur in mobile IP.
+ */
+ ap_log_error(APLOG_MARK, APLOG_EMERG, status, ap_server_conf,
+ "apr_socket_accept: giving up.");
+ return APR_EGENERAL;
+#endif /*ENETDOWN*/
+
+ default:
+ ap_log_error(APLOG_MARK, APLOG_ERR, status, ap_server_conf,
+ "apr_socket_accept: (client socket)");
+ return APR_EGENERAL;
+ }
+ return status;
+}
+
+static void tell_workers_to_exit(void)
+{
+ apr_size_t len;
+ int i = 0;
+ for (i = 0 ; i < ap_max_child_assigned; i++){
+ len = 4;
+ if (apr_socket_sendto(udp_sock, udp_sa, 0, "die!", &len) != APR_SUCCESS)
+ break;
+ }
+}
+
+static void set_signals(void)
+{
+ struct sigaction sa;
+
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+
+ /* The first batch get handled by sig_coredump */
+ if (!one_process) {
+ sa.sa_handler = sig_coredump;
+
+ if (sigaction(SIGSEGV, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGSEGV)");
+ if (sigaction(SIGBUS, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGBUS)");
+ if (sigaction(SIGABRT, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABRT)");
+ if (sigaction(SIGILL, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGILL)");
+ sa.sa_flags = 0;
+ }
+
+ /* These next two are handled by sig_term */
+ sa.sa_handler = sig_term;
+ if (sigaction(SIGTERM, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
+ if (sigaction(SIGINT, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");
+
+ /* We ignore SIGPIPE */
+ sa.sa_handler = SIG_IGN;
+ if (sigaction(SIGPIPE, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGPIPE)");
+
+ /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy
+ * processing one */
+ sigaddset(&sa.sa_mask, SIGHUP);
+ sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL);
+ sa.sa_handler = restart;
+ if (sigaction(SIGHUP, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");
+ if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(" AP_SIG_GRACEFUL_STRING ")");
+}
+
+/*****************************************************************
+ * Here follows a long bunch of generic server bookkeeping stuff...
+ */
+
+int ap_graceful_stop_signalled(void)
+{
+ return is_graceful;
+}
+
+/* This is the thread that actually does all the work. */
+static int32 worker_thread(void *dummy)
+{
+ int worker_slot = (int)dummy;
+ apr_allocator_t *allocator;
+ apr_bucket_alloc_t *bucket_alloc;
+ apr_status_t rv = APR_EINIT;
+ int last_poll_idx = 0;
+ sigset_t sig_mask;
+ int requests_this_child = 0;
+ apr_pollset_t *pollset = NULL;
+ ap_listen_rec *lr = NULL;
+ ap_sb_handle_t *sbh = NULL;
+ int i;
+ /* each worker thread is in control of its own destiny...*/
+ int this_worker_should_exit = 0;
+ /* We have 2 pools that we create/use throughout the lifetime of this
+ * worker. The first and longest lived is the pworker pool. From
+ * this we create the ptrans pool, the lifetime of which is the same
+ * as each connection and is reset prior to each attempt to
+ * process a connection.
+ */
+ apr_pool_t *ptrans = NULL;
+ apr_pool_t *pworker = NULL;
+
+ mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this
+ * child initializes
+ */
+
+ on_exit_thread(check_restart, (void*)worker_slot);
+
+ /* block the signals for this thread only if we're not running as a
+ * single process.
+ */
+ if (!one_process) {
+ sigfillset(&sig_mask);
+ sigprocmask(SIG_BLOCK, &sig_mask, NULL);
+ }
+
+ /* Each worker thread is fully in control of it's destinay and so
+ * to allow each thread to handle the lifetime of it's own resources
+ * we create and use a subcontext for every thread.
+ * The subcontext is a child of the pconf pool.
+ */
+ apr_allocator_create(&allocator);
+ apr_allocator_max_free_set(allocator, ap_max_mem_free);
+ apr_pool_create_ex(&pworker, pconf, NULL, allocator);
+ apr_allocator_owner_set(allocator, pworker);
+
+ apr_pool_create(&ptrans, pworker);
+ apr_pool_tag(ptrans, "transaction");
+
+ ap_create_sb_handle(&sbh, pworker, 0, worker_slot);
+ (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
+
+ /* We add an extra socket here as we add the udp_sock we use for signalling
+ * death. This gets added after the others.
+ */
+ apr_pollset_create(&pollset, num_listening_sockets + 1, pworker, 0);
+
+ for (lr = ap_listeners, i = num_listening_sockets; i--; lr = lr->next) {
+ apr_pollfd_t pfd = {0};
+
+ pfd.desc_type = APR_POLL_SOCKET;
+ pfd.desc.s = lr->sd;
+ pfd.reqevents = APR_POLLIN;
+ pfd.client_data = lr;
+
+ apr_pollset_add(pollset, &pfd);
+ }
+ {
+ apr_pollfd_t pfd = {0};
+
+ pfd.desc_type = APR_POLL_SOCKET;
+ pfd.desc.s = udp_sock;
+ pfd.reqevents = APR_POLLIN;
+
+ apr_pollset_add(pollset, &pfd);
+ }
+
+ bucket_alloc = apr_bucket_alloc_create(pworker);
+
+ mpm_state = AP_MPMQ_RUNNING;
+
+ while (!this_worker_should_exit) {
+ conn_rec *current_conn;
+ void *csd;
+
+ /* (Re)initialize this child to a pre-connection state. */
+ apr_pool_clear(ptrans);
+
+ if ((ap_max_requests_per_thread > 0
+ && requests_this_child++ >= ap_max_requests_per_thread))
+ clean_child_exit(0, worker_slot);
+
+ (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
+
+ apr_thread_mutex_lock(accept_mutex);
+
+ /* We always (presently) have at least 2 sockets we listen on, so
+ * we don't have the ability for a fast path for a single socket
+ * as some MPM's allow :(
+ */
+ for (;;) {
+ apr_int32_t numdesc = 0;
+ const apr_pollfd_t *pdesc = NULL;
+
+ rv = apr_pollset_poll(pollset, -1, &numdesc, &pdesc);
+ if (rv != APR_SUCCESS) {
+ if (APR_STATUS_IS_EINTR(rv)) {
+ if (one_process && shutdown_pending)
+ return;
+ continue;
+ }
+ ap_log_error(APLOG_MARK, APLOG_ERR, rv,
+ ap_server_conf, "apr_pollset_poll: (listen)");
+ clean_child_exit(1, worker_slot);
+ }
+ /* We can always use pdesc[0], but sockets at position N
+ * could end up completely starved of attention in a very
+ * busy server. Therefore, we round-robin across the
+ * returned set of descriptors. While it is possible that
+ * the returned set of descriptors might flip around and
+ * continue to starve some sockets, we happen to know the
+ * internal pollset implementation retains ordering
+ * stability of the sockets. Thus, the round-robin should
+ * ensure that a socket will eventually be serviced.
+ */
+ if (last_poll_idx >= numdesc)
+ last_poll_idx = 0;
+
+ /* Grab a listener record from the client_data of the poll
+ * descriptor, and advance our saved index to round-robin
+ * the next fetch.
+ *
+ * ### hmm... this descriptor might have POLLERR rather
+ * ### than POLLIN
+ */
+
+ lr = pdesc[last_poll_idx++].client_data;
+
+ /* The only socket we add without client_data is the first, the UDP socket
+ * we listen on for restart signals. If we've therefore gotten a hit on that
+ * listener lr will be NULL here and we know we've been told to die.
+ * Before we jump to the end of the while loop with this_worker_should_exit
+ * set to 1 (causing us to exit normally we hope) we release the accept_mutex
+ * as we want every thread to go through this same routine :)
+ * Bit of a hack, but compared to what I had before...
+ */
+ if (lr == NULL) {
+ this_worker_should_exit = 1;
+ apr_thread_mutex_unlock(accept_mutex);
+ goto got_a_black_spot;
+ }
+ goto got_fd;
+ }
+got_fd:
+ /* Run beos_accept to accept the connection and set things up to
+ * allow us to process it. We always release the accept_lock here,
+ * even if we failt o accept as otherwise we'll starve other workers
+ * which would be bad.
+ */
+ rv = beos_accept(&csd, lr, ptrans);
+ apr_thread_mutex_unlock(accept_mutex);
+
+ if (rv == APR_EGENERAL) {
+ /* resource shortage or should-not-occur occured */
+ clean_child_exit(1, worker_slot);
+ } else if (rv != APR_SUCCESS)
+ continue;
+
+ current_conn = ap_run_create_connection(ptrans, ap_server_conf, csd, worker_slot, sbh, bucket_alloc);
+ if (current_conn) {
+ ap_process_connection(current_conn, csd);
+ ap_lingering_close(current_conn);
+ }
+
+ if (ap_my_generation !=
+ ap_scoreboard_image->global->running_generation) { /* restart? */
+ /* yeah, this could be non-graceful restart, in which case the
+ * parent will kill us soon enough, but why bother checking?
+ */
+ this_worker_should_exit = 1;
+ }
+got_a_black_spot:
+ }
+
+ apr_pool_destroy(ptrans);
+ apr_pool_destroy(pworker);
+
+ clean_child_exit(0, worker_slot);
+}
+
+static int make_worker(int slot)
+{
+ thread_id tid;
+
+ if (slot + 1 > ap_max_child_assigned)
+ ap_max_child_assigned = slot + 1;
+
+ (void) ap_update_child_status_from_indexes(0, slot, SERVER_STARTING, (request_rec*)NULL);
+
+ if (one_process) {
+ set_signals();
+ ap_scoreboard_image->parent[0].pid = getpid();
+ ap_scoreboard_image->servers[0][slot].tid = find_thread(NULL);
+ return 0;
+ }
+
+ tid = spawn_thread(worker_thread, "apache_worker", B_NORMAL_PRIORITY,
+ (void *)slot);
+ if (tid < B_NO_ERROR) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, errno, NULL,
+ "spawn_thread: Unable to start a new thread");
+ /* In case system resources are maxed out, we don't want
+ * Apache running away with the CPU trying to fork over and
+ * over and over again.
+ */
+ (void) ap_update_child_status_from_indexes(0, slot, SERVER_DEAD,
+ (request_rec*)NULL);
+
+ sleep(10);
+ return -1;
+ }
+ resume_thread(tid);
+
+ ap_scoreboard_image->servers[0][slot].tid = tid;
+ return 0;
+}
+
+/* When a worker thread exits, this function is called. If we are not in
+ * a shutdown situation then we restart the worker in the slot that was
+ * just vacated.
+ */
+static void check_restart(void *data)
+{
+ if (!restart_pending && !shutdown_pending) {
+ int slot = (int)data;
+ make_worker(slot);
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0, NULL,
+ "spawning a new worker thread in slot %d", slot);
+ }
+}
+
+/* Start number_to_start children. This is used to start both the
+ * initial 'pool' of workers but also to replace existing workers who
+ * have reached the end of their time. It walks through the scoreboard to find
+ * an empty slot and starts the worker thread in that slot.
+ */
+static void startup_threads(int number_to_start)
+{
+ int i;
+
+ for (i = 0; number_to_start && i < ap_thread_limit; ++i) {
+ if (ap_scoreboard_image->servers[0][i].tid)
+ continue;
+
+ if (make_worker(i) < 0)
+ break;
+
+ --number_to_start;
+ }
+}
+
+
+/*
+ * spawn_rate is the number of children that will be spawned on the
+ * next maintenance cycle if there aren't enough idle servers. It is
+ * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
+ * without the need to spawn.
+ */
+static int spawn_rate = 1;
+#ifndef MAX_SPAWN_RATE
+#define MAX_SPAWN_RATE (32)
+#endif
+static int hold_off_on_exponential_spawning;
+
+static void perform_idle_server_maintenance(void)
+{
+ int i;
+ int free_length;
+ int free_slots[MAX_SPAWN_RATE];
+ int last_non_dead = -1;
+
+ /* initialize the free_list */
+ free_length = 0;
+
+ for (i = 0; i < ap_thread_limit; ++i) {
+ if (ap_scoreboard_image->servers[0][i].tid == 0) {
+ if (free_length < spawn_rate) {
+ free_slots[free_length] = i;
+ ++free_length;
+ }
+ }
+ else {
+ last_non_dead = i;
+ }
+
+ if (i >= ap_max_child_assigned && free_length >= spawn_rate) {
+ break;
+ }
+ }
+ ap_max_child_assigned = last_non_dead + 1;
+
+ if (free_length > 0) {
+ for (i = 0; i < free_length; ++i) {
+ make_worker(free_slots[i]);
+ }
+ /* the next time around we want to spawn twice as many if this
+ * wasn't good enough, but not if we've just done a graceful
+ */
+ if (hold_off_on_exponential_spawning) {
+ --hold_off_on_exponential_spawning;
+ } else if (spawn_rate < MAX_SPAWN_RATE) {
+ spawn_rate *= 2;
+ }
+ } else {
+ spawn_rate = 1;
+ }
+}
+
+static void server_main_loop(int remaining_threads_to_start)
+{
+ int child_slot;
+ apr_exit_why_e exitwhy;
+ int status;
+ apr_proc_t pid;
+ int i;
+
+ while (!restart_pending && !shutdown_pending) {
+
+ ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
+
+ if (pid.pid >= 0) {
+ if (ap_process_child_status(&pid, exitwhy, status) == APEXIT_CHILDFATAL) {
+ shutdown_pending = 1;
+ child_fatal = 1;
+ return;
+ }
+ /* non-fatal death... note that it's gone in the scoreboard. */
+ child_slot = -1;
+ for (i = 0; i < ap_max_child_assigned; ++i) {
+ if (ap_scoreboard_image->servers[0][i].tid == pid.pid) {
+ child_slot = i;
+ break;
+ }
+ }
+ if (child_slot >= 0) {
+ ap_scoreboard_image->servers[0][child_slot].tid = 0;
+ (void) ap_update_child_status_from_indexes(0, child_slot,
+ SERVER_DEAD,
+ (request_rec*)NULL);
+
+ if (remaining_threads_to_start
+ && child_slot < ap_thread_limit) {
+ /* we're still doing a 1-for-1 replacement of dead
+ * children with new children
+ */
+ make_worker(child_slot);
+ --remaining_threads_to_start;
+ }
+/* TODO
+#if APR_HAS_OTHER_CHILD
+ }
+ else if (apr_proc_other_child_refresh(&pid, status) == 0) {
+#endif
+*/
+ }
+ else if (is_graceful) {
+ /* Great, we've probably just lost a slot in the
+ * scoreboard. Somehow we don't know about this
+ * child.
+ */
+ ap_log_error(APLOG_MARK, APLOG_WARNING, 0, ap_server_conf,
+ "long lost child came home! (pid %ld)", pid.pid);
+ }
+
+ /* Don't perform idle maintenance when a child dies,
+ * only do it when there's a timeout. Remember only a
+ * finite number of children can die, and it's pretty
+ * pathological for a lot to die suddenly.
+ */
+ continue;
+ }
+ else if (remaining_threads_to_start) {
+ /* we hit a 1 second timeout in which none of the previous
+ * generation of children needed to be reaped... so assume
+ * they're all done, and pick up the slack if any is left.
+ */
+ startup_threads(remaining_threads_to_start);
+ remaining_threads_to_start = 0;
+ /* In any event we really shouldn't do the code below because
+ * few of the servers we just started are in the IDLE state
+ * yet, so we'd mistakenly create an extra server.
+ */
+ continue;
+ }
+ perform_idle_server_maintenance();
+ }
+}
+
+/* This is called to not only setup and run for the initial time, but also
+ * when we've asked for a restart. This means it must be able to handle both
+ * situations. It also means that when we exit here we should have tidied
+ * up after ourselves fully.
+ */
+int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
+{
+ int remaining_threads_to_start, i,j;
+ apr_status_t rv;
+ ap_listen_rec *lr;
+ pconf = _pconf;
+ ap_server_conf = s;
+
+ /* Increase the available pool of fd's. This code from
+ * Joe Kloss <joek@be.com>
+ */
+ if( FD_SETSIZE > 128 && (i = _kset_fd_limit_( 128 )) < 0 ){
+ ap_log_error(APLOG_MARK, APLOG_ERR, i, s,
+ "could not set FD_SETSIZE (_kset_fd_limit_ failed)");
+ }
+
+ /* BeOS R5 doesn't support pipes on select() calls, so we use a
+ * UDP socket as these are supported in both R5 and BONE. If we only cared
+ * about BONE we'd use a pipe, but there it is.
+ * As we have UDP support in APR, now use the APR functions and check all the
+ * return values...
+ */
+ if (apr_sockaddr_info_get(&udp_sa, "127.0.0.1", APR_UNSPEC, 7772, 0, _pconf)
+ != APR_SUCCESS){
+ ap_log_error(APLOG_MARK, APLOG_ALERT, errno, s,
+ "couldn't create control socket information, shutting down");
+ return 1;
+ }
+ if (apr_socket_create(&udp_sock, udp_sa->family, SOCK_DGRAM, 0,
+ _pconf) != APR_SUCCESS){
+ ap_log_error(APLOG_MARK, APLOG_ALERT, errno, s,
+ "couldn't create control socket, shutting down");
+ return 1;
+ }
+ if (apr_socket_bind(udp_sock, udp_sa) != APR_SUCCESS){
+ ap_log_error(APLOG_MARK, APLOG_ALERT, errno, s,
+ "couldn't bind UDP socket!");
+ return 1;
+ }
+
+ if ((num_listening_sockets = ap_setup_listeners(ap_server_conf)) < 1) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, 0, s,
+ "no listening sockets available, shutting down");
+ return 1;
+ }
+
+ ap_log_pid(pconf, ap_pid_fname);
+
+ /*
+ * Create our locks...
+ */
+
+ /* accept_mutex
+ * used to lock around select so we only have one thread
+ * in select at a time
+ */
+ rv = apr_thread_mutex_create(&accept_mutex, 0, pconf);
+ if (rv != APR_SUCCESS) {
+ /* tsch tsch, can't have more than one thread in the accept loop
+ at a time so we need to fall on our sword... */
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
+ "Couldn't create accept lock");
+ return 1;
+ }
+
+ /*
+ * Startup/shutdown...
+ */
+
+ if (!is_graceful) {
+ /* setup the scoreboard shared memory */
+ if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
+ return 1;
+ }
+
+ for (i = 0; i < HARD_SERVER_LIMIT; i++) {
+ ap_scoreboard_image->parent[i].pid = 0;
+ for (j = 0;j < HARD_THREAD_LIMIT; j++)
+ ap_scoreboard_image->servers[i][j].tid = 0;
+ }
+ }
+
+ if (HARD_SERVER_LIMIT == 1)
+ ap_scoreboard_image->parent[0].pid = getpid();
+
+ set_signals();
+
+ /* Sanity checks to avoid thrashing... */
+ if (max_spare_threads < min_spare_threads )
+ max_spare_threads = min_spare_threads;
+
+ /* If we're doing a graceful_restart then we're going to see a lot
+ * of threads exiting immediately when we get into the main loop
+ * below (because we just sent them AP_SIG_GRACEFUL). This happens
+ * pretty rapidly... and for each one that exits we'll start a new one
+ * until we reach at least threads_min_free. But we may be permitted to
+ * start more than that, so we'll just keep track of how many we're
+ * supposed to start up without the 1 second penalty between each fork.
+ */
+ remaining_threads_to_start = ap_threads_to_start;
+ /* sanity check on the number to start... */
+ if (remaining_threads_to_start > ap_thread_limit) {
+ remaining_threads_to_start = ap_thread_limit;
+ }
+
+ /* If we're doing the single process thing or we're in a graceful_restart
+ * then we don't start threads here.
+ * if we're in one_process mode we don't want to start threads
+ * do we??
+ */
+ if (!is_graceful && !one_process) {
+ startup_threads(remaining_threads_to_start);
+ remaining_threads_to_start = 0;
+ } else {
+ /* give the system some time to recover before kicking into
+ * exponential mode */
+ hold_off_on_exponential_spawning = 10;
+ }
+
+ /*
+ * record that we've entered the world !
+ */
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "%s configured -- resuming normal operations",
+ ap_get_server_version());
+
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
+ "Server built: %s", ap_get_server_built());
+
+ restart_pending = shutdown_pending = 0;
+
+ mpm_state = AP_MPMQ_RUNNING;
+
+ /* We sit in the server_main_loop() until we somehow manage to exit. When
+ * we do, we need to kill the workers we have, so we start by using the
+ * tell_workers_to_exit() function, but as it sometimes takes a short while
+ * to accomplish this we have a pause builtin to allow them the chance to
+ * gracefully exit.
+ */
+ if (!one_process) {
+ server_main_loop(remaining_threads_to_start);
+ tell_workers_to_exit();
+ snooze(1000000);
+ } else {
+ worker_thread((void*)0);
+ }
+ mpm_state = AP_MPMQ_STOPPING;
+
+ /* close the UDP socket we've been using... */
+ apr_socket_close(udp_sock);
+
+ if ((one_process || shutdown_pending) && !child_fatal) {
+ const char *pidfile = NULL;
+ pidfile = ap_server_root_relative (pconf, ap_pid_fname);
+ if ( pidfile != NULL && unlink(pidfile) == 0)
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
+ "removed PID file %s (pid=%ld)", pidfile,
+ (long)getpid());
+ }
+
+ if (one_process) {
+ return 1;
+ }
+
+ /*
+ * If we get here we're shutting down...
+ */
+ if (shutdown_pending) {
+ /* Time to gracefully shut down:
+ * Kill child processes, tell them to call child_exit, etc...
+ */
+ if (beosd_killpg(getpgrp(), SIGTERM) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "killpg SIGTERM");
+
+ /* use ap_reclaim_child_processes starting with SIGTERM */
+ ap_reclaim_child_processes(1);
+
+ if (!child_fatal) { /* already recorded */
+ /* record the shutdown in the log */
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "caught SIGTERM, shutting down");
+ }
+
+ return 1;
+ }
+
+ /* we've been told to restart */
+ signal(SIGHUP, SIG_IGN);
+
+ if (is_graceful) {
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ AP_SIG_GRACEFUL_STRING " received. Doing graceful restart");
+ } else {
+ /* Kill 'em all. Since the child acts the same on the parents SIGTERM
+ * and a SIGHUP, we may as well use the same signal, because some user
+ * pthreads are stealing signals from us left and right.
+ */
+
+ ap_reclaim_child_processes(1); /* Start with SIGTERM */
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "SIGHUP received. Attempting to restart");
+ }
+
+ /* just before we go, tidy up the lock we created to prevent a
+ * potential leak of semaphores...
+ */
+ apr_thread_mutex_destroy(accept_mutex);
+
+ return 0;
+}
+
+static int beos_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
+{
+ static int restart_num = 0;
+ int no_detach, debug, foreground;
+ apr_status_t rv;
+
+ mpm_state = AP_MPMQ_STARTING;
+
+ debug = ap_exists_config_define("DEBUG");
+
+ if (debug) {
+ foreground = one_process = 1;
+ no_detach = 0;
+ }
+ else
+ {
+ one_process = ap_exists_config_define("ONE_PROCESS");
+ no_detach = ap_exists_config_define("NO_DETACH");
+ foreground = ap_exists_config_define("FOREGROUND");
+ }
+
+ /* sigh, want this only the second time around */
+ if (restart_num++ == 1) {
+ is_graceful = 0;
+
+ if (!one_process && !foreground) {
+ rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
+ : APR_PROC_DETACH_DAEMONIZE);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
+ "apr_proc_detach failed");
+ return HTTP_INTERNAL_SERVER_ERROR;
+ }
+ }
+
+ server_pid = getpid();
+ }
+
+ beosd_pre_config();
+ ap_listen_pre_config();
+ ap_threads_to_start = DEFAULT_START_THREADS;
+ min_spare_threads = DEFAULT_MIN_FREE_THREADS;
+ max_spare_threads = DEFAULT_MAX_FREE_THREADS;
+ ap_thread_limit = HARD_THREAD_LIMIT;
+ ap_pid_fname = DEFAULT_PIDLOG;
+ ap_max_requests_per_thread = DEFAULT_MAX_REQUESTS_PER_THREAD;
+#ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
+ ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
+#endif
+
+ apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
+
+ return OK;
+}
+
+static void beos_hooks(apr_pool_t *p)
+{
+ one_process = 0;
+
+ ap_hook_pre_config(beos_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
+}
+
+static const char *set_threads_to_start(cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_threads_to_start = atoi(arg);
+ if (ap_threads_to_start < 0) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "StartThreads set to a value less than 0, reset to 1");
+ ap_threads_to_start = 1;
+ }
+ return NULL;
+}
+
+static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ min_spare_threads = atoi(arg);
+ if (min_spare_threads <= 0) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: detected MinSpareThreads set to non-positive.");
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Resetting to 1 to avoid almost certain Apache failure.");
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Please read the documentation.");
+ min_spare_threads = 1;
+ }
+
+ return NULL;
+}
+
+static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ max_spare_threads = atoi(arg);
+ return NULL;
+}
+
+static const char *set_threads_limit (cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_thread_limit = atoi(arg);
+ if (ap_thread_limit > HARD_THREAD_LIMIT) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: MaxClients of %d exceeds compile time limit "
+ "of %d servers,", ap_thread_limit, HARD_THREAD_LIMIT);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " lowering MaxClients to %d. To increase, please "
+ "see the", HARD_THREAD_LIMIT);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " HARD_THREAD_LIMIT define in server/mpm/beos/mpm_default.h.");
+ ap_thread_limit = HARD_THREAD_LIMIT;
+ }
+ else if (ap_thread_limit < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require MaxClients > 0, setting to %d", HARD_THREAD_LIMIT);
+ ap_thread_limit = HARD_THREAD_LIMIT;
+ }
+ return NULL;
+}
+
+static const char *set_max_requests_per_thread (cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_max_requests_per_thread = atoi(arg);
+ if (ap_max_requests_per_thread < 0) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: MaxRequestsPerThread was set below 0"
+ "reset to 0, but this may not be what you want.");
+ ap_max_requests_per_thread = 0;
+ }
+
+ return NULL;
+}
+
+static const command_rec beos_cmds[] = {
+BEOS_DAEMON_COMMANDS,
+LISTEN_COMMANDS,
+AP_INIT_TAKE1( "StartThreads", set_threads_to_start, NULL, RSRC_CONF,
+ "Number of threads to launch at server startup"),
+AP_INIT_TAKE1( "MinSpareThreads", set_min_spare_threads, NULL, RSRC_CONF,
+ "Minimum number of idle children, to handle request spikes"),
+AP_INIT_TAKE1( "MaxSpareThreads", set_max_spare_threads, NULL, RSRC_CONF,
+ "Maximum number of idle children" ),
+AP_INIT_TAKE1( "MaxClients", set_threads_limit, NULL, RSRC_CONF,
+ "Maximum number of children alive at the same time (max threads)" ),
+AP_INIT_TAKE1( "MaxRequestsPerThread", set_max_requests_per_thread, NULL, RSRC_CONF,
+ "Maximum number of requests served by a thread" ),
+{ NULL }
+};
+
+module AP_MODULE_DECLARE_DATA mpm_beos_module = {
+ MPM20_MODULE_STUFF,
+ NULL, /* 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 */
+ beos_cmds, /* command apr_table_t */
+ beos_hooks /* register_hooks */
+};
+
diff --git a/server/mpm/beos/beos.h b/server/mpm/beos/beos.h
new file mode 100644
index 00000000..8b7aed7d
--- /dev/null
+++ b/server/mpm/beos/beos.h
@@ -0,0 +1,34 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file beos/beos.h
+ * @brief Extern functions/values for BEOS MPM
+ *
+ * @addtogroup APACHE_MPM_BEOS
+ * @{
+ */
+#ifndef APACHE_MPM_BEOS_H
+#define APACHE_MPM_BEOS_H
+
+extern int ap_threads_per_child;
+extern int ap_pipe_of_death[2];
+extern int ap_extended_status;
+extern void clean_child_exit(int);
+extern int max_daemons_limit;
+
+#endif /* APACHE_MPM_BEOS_H */
+/** @} */
diff --git a/server/mpm/beos/config5.m4 b/server/mpm/beos/config5.m4
new file mode 100644
index 00000000..4f201408
--- /dev/null
+++ b/server/mpm/beos/config5.m4
@@ -0,0 +1,7 @@
+dnl ## XXX - Need a more thorough check of the proper flags to use
+
+if test "$MPM_NAME" = "beos" ; then
+ apache_apr_flags="--enable-threads"
+
+ APACHE_FAST_OUTPUT(server/mpm/$MPM_NAME/Makefile)
+fi
diff --git a/server/mpm/beos/mpm.h b/server/mpm/beos/mpm.h
new file mode 100644
index 00000000..d61594b0
--- /dev/null
+++ b/server/mpm/beos/mpm.h
@@ -0,0 +1,50 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file beos/mpm.h
+ * @brief BEOS MPM
+ *
+ * @defgroup APACHE_MPM_BEOS BEOS MPM
+ * @ingroup APACHE_MPM APACHE_OS_BEOS
+ * @{
+ */
+
+#ifndef APACHE_MPM_BEOS_H
+#define APACHE_MPM_BEOS_H
+
+#define BEOS_MPM
+#include "scoreboard.h"
+
+#define MPM_NAME "Beos"
+#define MPM_CHILD_PID(i) (ap_scoreboard_image->servers[0][i].tid)
+#define MPM_NOTE_CHILD_KILLED(i) (MPM_CHILD_PID(i) = 0)
+
+#define AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
+#define AP_MPM_WANT_WAIT_OR_TIMEOUT
+#define AP_MPM_WANT_PROCESS_CHILD_STATUS
+#define AP_MPM_WANT_SET_PIDFILE
+#define AP_MPM_WANT_SET_SCOREBOARD
+#define AP_MPM_WANT_SET_MAX_REQUESTS
+#define AP_MPM_WANT_SET_COREDUMPDIR
+#define AP_MPM_WANT_SET_MAX_MEM_FREE
+
+extern int ap_max_child_assigned;
+extern server_rec *ap_server_conf;
+extern int ap_threads_per_child;
+
+#endif /* APACHE_MPM_BEOS_H */
+/** @} */
diff --git a/server/mpm/beos/mpm_default.h b/server/mpm/beos/mpm_default.h
new file mode 100644
index 00000000..316a4c09
--- /dev/null
+++ b/server/mpm/beos/mpm_default.h
@@ -0,0 +1,84 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file beos/mpm_default.h
+ * @brief beos MPM defaults
+ *
+ * @addtogroup APACHE_MPM_BEOS
+ * @{
+ */
+#ifndef APACHE_MPM_DEFAULT_H
+#define APACHE_MPM_DEFAULT_H
+
+/* we use the child (c) as zero in our code... */
+#define AP_ID_FROM_CHILD_THREAD(c, t) t
+/* as the child is always zero, just return the id... */
+#define AP_CHILD_THREAD_FROM_ID(i) 0 , i
+
+/* Number of threads to spawn off by default --- also, if fewer than
+ * this free when the caretaker checks, it will spawn more.
+ */
+#ifndef DEFAULT_START_THREADS
+#define DEFAULT_START_THREADS 10
+#endif
+
+#ifdef NO_THREADS
+#define DEFAULT_THREADS 1
+#endif
+#ifndef DEFAULT_THREADS
+#define DEFAULT_THREADS 10
+#endif
+
+/* The following 2 settings are used to control the number of threads
+ * we have available. Normally the DEFAULT_MAX_FREE_THREADS is set
+ * to the same as the HARD_THREAD_LIMIT to avoid churning of starting
+ * new threads to replace threads killed off...
+ */
+
+/* Maximum number of *free* threads --- more than this, and
+ * they will die off.
+ */
+#ifndef DEFAULT_MAX_FREE_THREADS
+#define DEFAULT_MAX_FREE_THREADS HARD_THREAD_LIMIT
+#endif
+
+/* Minimum --- fewer than this, and more will be created */
+#ifndef DEFAULT_MIN_FREE_THREADS
+#define DEFAULT_MIN_FREE_THREADS 1
+#endif
+
+/* Where the main/parent process's pid is logged */
+#ifndef DEFAULT_PIDLOG
+#define DEFAULT_PIDLOG DEFAULT_REL_RUNTIMEDIR "/httpd.pid"
+#endif
+
+/*
+ * Interval, in microseconds, between scoreboard maintenance.
+ */
+#ifndef SCOREBOARD_MAINTENANCE_INTERVAL
+#define SCOREBOARD_MAINTENANCE_INTERVAL 1000000
+#endif
+
+/* Number of requests to try to handle in a single process. If == 0,
+ * the children don't die off.
+ */
+#ifndef DEFAULT_MAX_REQUESTS_PER_THREAD
+#define DEFAULT_MAX_REQUESTS_PER_THREAD 0
+#endif
+
+#endif /* AP_MPM_DEFAULT_H */
+/** @} */
diff --git a/server/mpm/config.m4 b/server/mpm/config.m4
new file mode 100644
index 00000000..6ea447d1
--- /dev/null
+++ b/server/mpm/config.m4
@@ -0,0 +1,79 @@
+AC_MSG_CHECKING(which MPM to use)
+AC_ARG_WITH(mpm,
+APACHE_HELP_STRING(--with-mpm=MPM,Choose the process model for Apache to use.
+ MPM={beos|event|worker|prefork|mpmt_os2}),[
+ APACHE_MPM=$withval
+],[
+ if test "x$APACHE_MPM" = "x"; then
+ APACHE_MPM=prefork
+ fi
+])
+AC_MSG_RESULT($APACHE_MPM)
+
+apache_cv_mpm=$APACHE_MPM
+
+ap_mpm_is_threaded ()
+{
+ if test "$apache_cv_mpm" = "worker" -o "$apache_cv_mpm" = "event" ; then
+ return 0
+ else
+ return 1
+ fi
+}
+
+ap_mpm_is_experimental ()
+{
+ if test "$apache_cv_mpm" = "event" ; then
+ return 0
+ else
+ return 1
+ fi
+}
+
+if ap_mpm_is_threaded; then
+ APR_CHECK_APR_DEFINE(APR_HAS_THREADS)
+
+ if test "x$ac_cv_define_APR_HAS_THREADS" = "xno"; then
+ AC_MSG_RESULT(The currently selected MPM requires threads which your system seems to lack)
+ AC_MSG_CHECKING(checking for replacement)
+ AC_MSG_RESULT(prefork selected)
+ apache_cv_mpm=prefork
+ else
+ case $host in
+ *-linux-*)
+ case `uname -r` in
+ 2.0* )
+ dnl Threaded MPM's are not supported on Linux 2.0
+ dnl as on 2.0 the linuxthreads library uses SIGUSR1
+ dnl and SIGUSR2 internally
+ echo "Threaded MPM's are not supported on this platform"
+ AC_MSG_CHECKING(checking for replacement)
+ AC_MSG_RESULT(prefork selected)
+ apache_cv_mpm=prefork
+ ;;
+ esac
+ ;;
+ esac
+ fi
+fi
+
+APACHE_FAST_OUTPUT(server/mpm/Makefile)
+
+MPM_NAME=$apache_cv_mpm
+if ap_mpm_is_experimental; then
+ AC_MSG_WARN(You have selected an EXPERIMENTAL MPM. Be warned!)
+ MPM_SUBDIR_NAME=experimental/$MPM_NAME
+else
+ MPM_SUBDIR_NAME=$MPM_NAME
+fi
+MPM_DIR=server/mpm/$MPM_SUBDIR_NAME
+MPM_LIB=$MPM_DIR/lib${MPM_NAME}.la
+
+if test ! -f "$abs_srcdir/$MPM_DIR/mpm.h"; then
+ AC_MSG_ERROR(the selected mpm -- $apache_cv_mpm -- is not supported)
+fi
+
+APACHE_SUBST(MPM_NAME)
+APACHE_SUBST(MPM_SUBDIR_NAME)
+MODLIST="$MODLIST mpm_${MPM_NAME}"
+
diff --git a/server/mpm/experimental/event/Makefile.in b/server/mpm/experimental/event/Makefile.in
new file mode 100644
index 00000000..7c2a1a7a
--- /dev/null
+++ b/server/mpm/experimental/event/Makefile.in
@@ -0,0 +1,5 @@
+
+LTLIBRARY_NAME = libevent.la
+LTLIBRARY_SOURCES = event.c fdqueue.c pod.c
+
+include $(top_srcdir)/build/ltlib.mk
diff --git a/server/mpm/experimental/event/config5.m4 b/server/mpm/experimental/event/config5.m4
new file mode 100644
index 00000000..5e1db539
--- /dev/null
+++ b/server/mpm/experimental/event/config5.m4
@@ -0,0 +1,6 @@
+dnl ## XXX - Need a more thorough check of the proper flags to use
+
+if test "$MPM_NAME" = "event" ; then
+ AC_CHECK_FUNCS(pthread_kill)
+ APACHE_FAST_OUTPUT(server/mpm/$MPM_SUBDIR_NAME/Makefile)
+fi
diff --git a/server/mpm/experimental/event/event.c b/server/mpm/experimental/event/event.c
new file mode 100644
index 00000000..c47e857d
--- /dev/null
+++ b/server/mpm/experimental/event/event.c
@@ -0,0 +1,2456 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * This MPM tries to fix the 'keep alive problem' in HTTP.
+ *
+ * After a client completes the first request, the client can keep the
+ * connection open to send more requests with the same socket. This can save
+ * signifigant overhead in creating TCP connections. However, the major
+ * disadvantage is that Apache traditionally keeps an entire child
+ * process/thread waiting for data from the client. To solve this problem,
+ * this MPM has a dedicated thread for handling both the Listenting sockets,
+ * and all sockets that are in a Keep Alive status.
+ *
+ * The MPM assumes the underlying apr_pollset implmentation is somewhat
+ * threadsafe. This currently is only compatible with KQueue and EPoll. This
+ * enables the MPM to avoid extra high level locking or having to wake up the
+ * listener thread when a keep-alive socket needs to be sent to it.
+ *
+ * This MPM not preform well on older platforms that do not have very good
+ * threading, like Linux with a 2.4 kernel, but this does not matter, since we
+ * require EPoll or KQueue.
+ *
+ * For FreeBSD, use 5.3. It is possible to run this MPM on FreeBSD 5.2.1, if
+ * you use libkse (see `man libmap.conf`).
+ *
+ * For NetBSD, use at least 2.0.
+ *
+ * For Linux, you should use a 2.6 kernel, and make sure your glibc has epoll
+ * support compiled in.
+ *
+ */
+
+#include "apr.h"
+#include "apr_portable.h"
+#include "apr_strings.h"
+#include "apr_file_io.h"
+#include "apr_thread_proc.h"
+#include "apr_signal.h"
+#include "apr_thread_mutex.h"
+#include "apr_proc_mutex.h"
+#include "apr_poll.h"
+#include "apr_ring.h"
+#include "apr_queue.h"
+#define APR_WANT_STRFUNC
+#include "apr_want.h"
+
+#if APR_HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#if APR_HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+#if APR_HAVE_SYS_WAIT_H
+#include <sys/wait.h>
+#endif
+#ifdef HAVE_SYS_PROCESSOR_H
+#include <sys/processor.h> /* for bindprocessor() */
+#endif
+
+#if !APR_HAS_THREADS
+#error The Event MPM requires APR threads, but they are unavailable.
+#endif
+
+#define CORE_PRIVATE
+
+#include "ap_config.h"
+#include "httpd.h"
+#include "http_main.h"
+#include "http_log.h"
+#include "http_config.h" /* for read_config */
+#include "http_core.h" /* for get_remote_host */
+#include "http_connection.h"
+#include "ap_mpm.h"
+#include "pod.h"
+#include "mpm_common.h"
+#include "ap_listen.h"
+#include "scoreboard.h"
+#include "fdqueue.h"
+#include "mpm_default.h"
+#include "http_vhost.h"
+
+#include <signal.h>
+#include <limits.h> /* for INT_MAX */
+
+/* Limit on the total --- clients will be locked out if more servers than
+ * this are needed. It is intended solely to keep the server from crashing
+ * when things get out of hand.
+ *
+ * We keep a hard maximum number of servers, for two reasons --- first off,
+ * in case something goes seriously wrong, we want to stop the fork bomb
+ * short of actually crashing the machine we're running on by filling some
+ * kernel table. Secondly, it keeps the size of the scoreboard file small
+ * enough that we can read the whole thing without worrying too much about
+ * the overhead.
+ */
+#ifndef DEFAULT_SERVER_LIMIT
+#define DEFAULT_SERVER_LIMIT 16
+#endif
+
+/* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT. We want
+ * some sort of compile-time limit to help catch typos.
+ */
+#ifndef MAX_SERVER_LIMIT
+#define MAX_SERVER_LIMIT 20000
+#endif
+
+/* Limit on the threads per process. Clients will be locked out if more than
+ * this are needed.
+ *
+ * We keep this for one reason it keeps the size of the scoreboard file small
+ * enough that we can read the whole thing without worrying too much about
+ * the overhead.
+ */
+#ifndef DEFAULT_THREAD_LIMIT
+#define DEFAULT_THREAD_LIMIT 64
+#endif
+
+/* Admin can't tune ThreadLimit beyond MAX_THREAD_LIMIT. We want
+ * some sort of compile-time limit to help catch typos.
+ */
+#ifndef MAX_THREAD_LIMIT
+#define MAX_THREAD_LIMIT 100000
+#endif
+
+/*
+ * Actual definitions of config globals
+ */
+
+int ap_threads_per_child = 0; /* Worker threads per child */
+static int ap_daemons_to_start = 0;
+static int min_spare_threads = 0;
+static int max_spare_threads = 0;
+static int ap_daemons_limit = 0;
+static int server_limit = DEFAULT_SERVER_LIMIT;
+static int first_server_limit = 0;
+static int thread_limit = DEFAULT_THREAD_LIMIT;
+static int first_thread_limit = 0;
+static int changed_limit_at_restart;
+static int dying = 0;
+static int workers_may_exit = 0;
+static int start_thread_may_exit = 0;
+static int listener_may_exit = 0;
+static int requests_this_child;
+static int num_listensocks = 0;
+static int resource_shortage = 0;
+static fd_queue_t *worker_queue;
+static fd_queue_info_t *worker_queue_info;
+static int mpm_state = AP_MPMQ_STARTING;
+static int sick_child_detected;
+
+apr_thread_mutex_t *timeout_mutex;
+APR_RING_HEAD(timeout_head_t, conn_state_t);
+static struct timeout_head_t timeout_head;
+
+static apr_pollset_t *event_pollset;
+
+/* The structure used to pass unique initialization info to each thread */
+typedef struct
+{
+ int pid;
+ int tid;
+ int sd;
+} proc_info;
+
+/* Structure used to pass information to the thread responsible for
+ * creating the rest of the threads.
+ */
+typedef struct
+{
+ apr_thread_t **threads;
+ apr_thread_t *listener;
+ int child_num_arg;
+ apr_threadattr_t *threadattr;
+} thread_starter;
+
+typedef enum
+{
+ PT_CSD,
+ PT_ACCEPT
+} poll_type_e;
+
+typedef struct
+{
+ poll_type_e type;
+ int status; /*XXX what is this for? 0 and 1 don't make it clear */
+ void *baton;
+} listener_poll_type;
+
+#define ID_FROM_CHILD_THREAD(c, t) ((c * thread_limit) + t)
+
+/*
+ * The max child slot ever assigned, preserved across restarts. Necessary
+ * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts. We
+ * use this value to optimize routines that have to scan the entire
+ * scoreboard.
+ */
+int ap_max_daemons_limit = -1;
+
+static ap_pod_t *pod;
+
+/* *Non*-shared http_main globals... */
+
+server_rec *ap_server_conf;
+
+/* The worker MPM respects a couple of runtime flags that can aid
+ * in debugging. Setting the -DNO_DETACH flag will prevent the root process
+ * from detaching from its controlling terminal. Additionally, setting
+ * the -DONE_PROCESS flag (which implies -DNO_DETACH) will get you the
+ * child_main loop running in the process which originally started up.
+ * This gives you a pretty nice debugging environment. (You'll get a SIGHUP
+ * early in standalone_main; just continue through. This is the server
+ * trying to kill off any child processes which it might have lying
+ * around --- Apache doesn't keep track of their pids, it just sends
+ * SIGHUP to the process group, ignoring it in the root process.
+ * Continue through and you'll be fine.).
+ */
+
+static int one_process = 0;
+
+#ifdef DEBUG_SIGSTOP
+int raise_sigstop_flags;
+#endif
+
+static apr_pool_t *pconf; /* Pool for config stuff */
+static apr_pool_t *pchild; /* Pool for httpd child stuff */
+
+static pid_t ap_my_pid; /* Linux getpid() doesn't work except in main
+ thread. Use this instead */
+static pid_t parent_pid;
+static apr_os_thread_t *listener_os_thread;
+
+/* The LISTENER_SIGNAL signal will be sent from the main thread to the
+ * listener thread to wake it up for graceful termination (what a child
+ * process from an old generation does when the admin does "apachectl
+ * graceful"). This signal will be blocked in all threads of a child
+ * process except for the listener thread.
+ */
+#define LISTENER_SIGNAL SIGHUP
+
+/* An array of socket descriptors in use by each thread used to
+ * perform a non-graceful (forced) shutdown of the server.
+ */
+static apr_socket_t **worker_sockets;
+
+static void close_worker_sockets(void)
+{
+ int i;
+ for (i = 0; i < ap_threads_per_child; i++) {
+ if (worker_sockets[i]) {
+ apr_socket_close(worker_sockets[i]);
+ worker_sockets[i] = NULL;
+ }
+ }
+}
+
+static void wakeup_listener(void)
+{
+ listener_may_exit = 1;
+ if (!listener_os_thread) {
+ /* XXX there is an obscure path that this doesn't handle perfectly:
+ * right after listener thread is created but before
+ * listener_os_thread is set, the first worker thread hits an
+ * error and starts graceful termination
+ */
+ return;
+ }
+ /*
+ * we should just be able to "kill(ap_my_pid, LISTENER_SIGNAL)" on all
+ * platforms and wake up the listener thread since it is the only thread
+ * with SIGHUP unblocked, but that doesn't work on Linux
+ */
+#ifdef HAVE_PTHREAD_KILL
+ pthread_kill(*listener_os_thread, LISTENER_SIGNAL);
+#else
+ kill(ap_my_pid, LISTENER_SIGNAL);
+#endif
+}
+
+#define ST_INIT 0
+#define ST_GRACEFUL 1
+#define ST_UNGRACEFUL 2
+
+static int terminate_mode = ST_INIT;
+
+static void signal_threads(int mode)
+{
+ if (terminate_mode == mode) {
+ return;
+ }
+ terminate_mode = mode;
+ mpm_state = AP_MPMQ_STOPPING;
+
+ /* in case we weren't called from the listener thread, wake up the
+ * listener thread
+ */
+ wakeup_listener();
+
+ /* for ungraceful termination, let the workers exit now;
+ * for graceful termination, the listener thread will notify the
+ * workers to exit once it has stopped accepting new connections
+ */
+ if (mode == ST_UNGRACEFUL) {
+ workers_may_exit = 1;
+ ap_queue_interrupt_all(worker_queue);
+ ap_queue_info_term(worker_queue_info);
+ close_worker_sockets(); /* forcefully kill all current connections */
+ }
+}
+
+AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
+{
+ switch (query_code) {
+ case AP_MPMQ_MAX_DAEMON_USED:
+ *result = ap_max_daemons_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_THREADED:
+ *result = AP_MPMQ_STATIC;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_FORKED:
+ *result = AP_MPMQ_DYNAMIC;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_ASYNC:
+ *result = 1;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_DAEMONS:
+ *result = server_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_THREADS:
+ *result = thread_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_THREADS:
+ *result = ap_threads_per_child;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_THREADS:
+ *result = min_spare_threads;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_THREADS:
+ *result = max_spare_threads;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_REQUESTS_DAEMON:
+ *result = ap_max_requests_per_child;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_DAEMONS:
+ *result = ap_daemons_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_MPM_STATE:
+ *result = mpm_state;
+ return APR_SUCCESS;
+ }
+ return APR_ENOTIMPL;
+}
+
+/* a clean exit from a child with proper cleanup */
+static void clean_child_exit(int code) __attribute__ ((noreturn));
+static void clean_child_exit(int code)
+{
+ mpm_state = AP_MPMQ_STOPPING;
+ if (pchild) {
+ apr_pool_destroy(pchild);
+ }
+ exit(code);
+}
+
+static void just_die(int sig)
+{
+ clean_child_exit(0);
+}
+
+/*****************************************************************
+ * Connection structures and accounting...
+ */
+
+/* volatile just in case */
+static int volatile shutdown_pending;
+static int volatile restart_pending;
+static int volatile is_graceful;
+static volatile int child_fatal;
+ap_generation_t volatile ap_my_generation;
+
+/*
+ * ap_start_shutdown() and ap_start_restart(), below, are a first stab at
+ * functions to initiate shutdown or restart without relying on signals.
+ * Previously this was initiated in sig_term() and restart() signal handlers,
+ * but we want to be able to start a shutdown/restart from other sources --
+ * e.g. on Win32, from the service manager. Now the service manager can
+ * call ap_start_shutdown() or ap_start_restart() as appropiate. Note that
+ * these functions can also be called by the child processes, since global
+ * variables are no longer used to pass on the required action to the parent.
+ *
+ * These should only be called from the parent process itself, since the
+ * parent process will use the shutdown_pending and restart_pending variables
+ * to determine whether to shutdown or restart. The child process should
+ * call signal_parent() directly to tell the parent to die -- this will
+ * cause neither of those variable to be set, which the parent will
+ * assume means something serious is wrong (which it will be, for the
+ * child to force an exit) and so do an exit anyway.
+ */
+
+static void ap_start_shutdown(int graceful)
+{
+ mpm_state = AP_MPMQ_STOPPING;
+ if (shutdown_pending == 1) {
+ /* Um, is this _probably_ not an error, if the user has
+ * tried to do a shutdown twice quickly, so we won't
+ * worry about reporting it.
+ */
+ return;
+ }
+ shutdown_pending = 1;
+ is_graceful = graceful;
+}
+
+/* do a graceful restart if graceful == 1 */
+static void ap_start_restart(int graceful)
+{
+ mpm_state = AP_MPMQ_STOPPING;
+ if (restart_pending == 1) {
+ /* Probably not an error - don't bother reporting it */
+ return;
+ }
+ restart_pending = 1;
+ is_graceful = graceful;
+}
+
+static void sig_term(int sig)
+{
+ ap_start_shutdown(sig == AP_SIG_GRACEFUL_STOP);
+}
+
+static void restart(int sig)
+{
+ ap_start_restart(sig == AP_SIG_GRACEFUL);
+}
+
+static void set_signals(void)
+{
+#ifndef NO_USE_SIGACTION
+ struct sigaction sa;
+#endif
+
+ if (!one_process) {
+ ap_fatal_signal_setup(ap_server_conf, pconf);
+ }
+
+#ifndef NO_USE_SIGACTION
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+
+ sa.sa_handler = sig_term;
+ if (sigaction(SIGTERM, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(SIGTERM)");
+#ifdef AP_SIG_GRACEFUL_STOP
+ if (sigaction(AP_SIG_GRACEFUL_STOP, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(" AP_SIG_GRACEFUL_STOP_STRING ")");
+#endif
+#ifdef SIGINT
+ if (sigaction(SIGINT, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(SIGINT)");
+#endif
+#ifdef SIGXCPU
+ sa.sa_handler = SIG_DFL;
+ if (sigaction(SIGXCPU, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(SIGXCPU)");
+#endif
+#ifdef SIGXFSZ
+ sa.sa_handler = SIG_DFL;
+ if (sigaction(SIGXFSZ, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(SIGXFSZ)");
+#endif
+#ifdef SIGPIPE
+ sa.sa_handler = SIG_IGN;
+ if (sigaction(SIGPIPE, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(SIGPIPE)");
+#endif
+
+ /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy
+ * processing one */
+ sigaddset(&sa.sa_mask, SIGHUP);
+ sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL);
+ sa.sa_handler = restart;
+ if (sigaction(SIGHUP, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(SIGHUP)");
+ if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(" AP_SIG_GRACEFUL_STRING ")");
+#else
+ if (!one_process) {
+#ifdef SIGXCPU
+ apr_signal(SIGXCPU, SIG_DFL);
+#endif /* SIGXCPU */
+#ifdef SIGXFSZ
+ apr_signal(SIGXFSZ, SIG_DFL);
+#endif /* SIGXFSZ */
+ }
+
+ apr_signal(SIGTERM, sig_term);
+#ifdef SIGHUP
+ apr_signal(SIGHUP, restart);
+#endif /* SIGHUP */
+#ifdef AP_SIG_GRACEFUL
+ apr_signal(AP_SIG_GRACEFUL, restart);
+#endif /* AP_SIG_GRACEFUL */
+#ifdef AP_SIG_GRACEFUL_STOP
+ apr_signal(AP_SIG_GRACEFUL_STOP, sig_term);
+#endif /* AP_SIG_GRACEFUL_STOP */
+#ifdef SIGPIPE
+ apr_signal(SIGPIPE, SIG_IGN);
+#endif /* SIGPIPE */
+
+#endif
+}
+
+/*****************************************************************
+ * Here follows a long bunch of generic server bookkeeping stuff...
+ */
+
+int ap_graceful_stop_signalled(void)
+ /* XXX this is really a bad confusing obsolete name
+ * maybe it should be ap_mpm_process_exiting?
+ */
+{
+ /* note: for a graceful termination, listener_may_exit will be set before
+ * workers_may_exit, so check listener_may_exit
+ */
+ return listener_may_exit;
+}
+
+/*****************************************************************
+ * Child process main loop.
+ */
+
+static int process_socket(apr_pool_t * p, apr_socket_t * sock,
+ conn_state_t * cs, int my_child_num,
+ int my_thread_num)
+{
+ conn_rec *c;
+ listener_poll_type *pt;
+ long conn_id = ID_FROM_CHILD_THREAD(my_child_num, my_thread_num);
+ int csd;
+ int rc;
+ apr_time_t time_now = 0;
+ ap_sb_handle_t *sbh;
+
+ ap_create_sb_handle(&sbh, p, my_child_num, my_thread_num);
+ apr_os_sock_get(&csd, sock);
+
+ time_now = apr_time_now();
+
+ if (cs == NULL) { /* This is a new connection */
+
+ cs = apr_pcalloc(p, sizeof(conn_state_t));
+
+ pt = apr_pcalloc(p, sizeof(*pt));
+
+ cs->bucket_alloc = apr_bucket_alloc_create(p);
+ c = ap_run_create_connection(p, ap_server_conf, sock,
+ conn_id, sbh, cs->bucket_alloc);
+ cs->c = c;
+ c->cs = cs;
+ cs->p = p;
+ cs->pfd.desc_type = APR_POLL_SOCKET;
+ cs->pfd.reqevents = APR_POLLIN;
+ cs->pfd.desc.s = sock;
+ pt->type = PT_CSD;
+ pt->status = 1;
+ pt->baton = cs;
+ cs->pfd.client_data = pt;
+
+ ap_update_vhost_given_ip(c);
+
+ rc = ap_run_pre_connection(c, sock);
+ if (rc != OK && rc != DONE) {
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "process_socket: connection aborted");
+ c->aborted = 1;
+ }
+
+ /**
+ * XXX If the platform does not have a usable way of bundling
+ * accept() with a socket readability check, like Win32,
+ * and there are measurable delays before the
+ * socket is readable due to the first data packet arriving,
+ * it might be better to create the cs on the listener thread
+ * with the state set to CONN_STATE_CHECK_REQUEST_LINE_READABLE
+ *
+ * FreeBSD users will want to enable the HTTP accept filter
+ * module in their kernel for the highest performance
+ * When the accept filter is active, sockets are kept in the
+ * kernel until a HTTP request is received.
+ */
+ cs->state = CONN_STATE_READ_REQUEST_LINE;
+
+ }
+ else {
+ c = cs->c;
+ c->sbh = sbh;
+ }
+
+ if (cs->state == CONN_STATE_READ_REQUEST_LINE) {
+ if (!c->aborted) {
+ ap_run_process_connection(c);
+
+ /* state will be updated upon return
+ * fall thru to either wait for readability/timeout or
+ * do lingering close
+ */
+ }
+ else {
+ cs->state = CONN_STATE_LINGER;
+ }
+ }
+
+ if (cs->state == CONN_STATE_LINGER) {
+ ap_lingering_close(c);
+ apr_bucket_alloc_destroy(cs->bucket_alloc);
+ apr_pool_clear(p);
+ ap_push_pool(worker_queue_info, p);
+ return 1;
+ }
+ else if (cs->state == CONN_STATE_CHECK_REQUEST_LINE_READABLE) {
+ apr_status_t rc;
+ listener_poll_type *pt = (listener_poll_type *) cs->pfd.client_data;
+
+ /* It greatly simplifies the logic to use a single timeout value here
+ * because the new element can just be added to the end of the list and
+ * it will stay sorted in expiration time sequence. If brand new
+ * sockets are sent to the event thread for a readability check, this
+ * will be a slight behavior change - they use the non-keepalive
+ * timeout today. With a normal client, the socket will be readable in
+ * a few milliseconds anyway.
+ */
+ cs->expiration_time = ap_server_conf->keep_alive_timeout + time_now;
+ apr_thread_mutex_lock(timeout_mutex);
+ APR_RING_INSERT_TAIL(&timeout_head, cs, conn_state_t, timeout_list);
+
+ pt->status = 0;
+ /* Add work to pollset. These are always read events */
+ rc = apr_pollset_add(event_pollset, &cs->pfd);
+
+ apr_thread_mutex_unlock(timeout_mutex);
+
+ if (rc != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, rc, ap_server_conf,
+ "process_socket: apr_pollset_add failure");
+ AP_DEBUG_ASSERT(rc == APR_SUCCESS);
+ }
+ }
+ return 0;
+}
+
+/* requests_this_child has gone to zero or below. See if the admin coded
+ "MaxRequestsPerChild 0", and keep going in that case. Doing it this way
+ simplifies the hot path in worker_thread */
+static void check_infinite_requests(void)
+{
+ if (ap_max_requests_per_child) {
+ signal_threads(ST_GRACEFUL);
+ }
+ else {
+ /* wow! if you're executing this code, you may have set a record.
+ * either this child process has served over 2 billion requests, or
+ * you're running a threaded 2.0 on a 16 bit machine.
+ *
+ * I'll buy pizza and beers at Apachecon for the first person to do
+ * the former without cheating (dorking with INT_MAX, or running with
+ * uncommitted performance patches, for example).
+ *
+ * for the latter case, you probably deserve a beer too. Greg Ames
+ */
+
+ requests_this_child = INT_MAX; /* keep going */
+ }
+}
+
+static void unblock_signal(int sig)
+{
+ sigset_t sig_mask;
+
+ sigemptyset(&sig_mask);
+ sigaddset(&sig_mask, sig);
+#if defined(SIGPROCMASK_SETS_THREAD_MASK)
+ sigprocmask(SIG_UNBLOCK, &sig_mask, NULL);
+#else
+ pthread_sigmask(SIG_UNBLOCK, &sig_mask, NULL);
+#endif
+}
+
+static void dummy_signal_handler(int sig)
+{
+ /* XXX If specifying SIG_IGN is guaranteed to unblock a syscall,
+ * then we don't need this goofy function.
+ */
+}
+
+static apr_status_t push2worker(const apr_pollfd_t * pfd,
+ apr_pollset_t * pollset)
+{
+ listener_poll_type *pt = (listener_poll_type *) pfd->client_data;
+ conn_state_t *cs = (conn_state_t *) pt->baton;
+ apr_status_t rc;
+
+ if (pt->status == 1) {
+ return 0;
+ }
+
+ pt->status = 1;
+
+ rc = apr_pollset_remove(pollset, pfd);
+
+ /*
+ * Some of the pollset backends, like KQueue or Epoll
+ * automagically remove the FD if the socket is closed,
+ * therefore, we can accept _SUCCESS or _NOTFOUND,
+ * and we still want to keep going
+ */
+ if (rc != APR_SUCCESS && rc != APR_NOTFOUND) {
+ cs->state = CONN_STATE_LINGER;
+ }
+
+ rc = ap_queue_push(worker_queue, cs->pfd.desc.s, cs, cs->p);
+ if (rc != APR_SUCCESS) {
+ /* trash the connection; we couldn't queue the connected
+ * socket to a worker
+ */
+ apr_bucket_alloc_destroy(cs->bucket_alloc);
+ apr_socket_close(cs->pfd.desc.s);
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rc,
+ ap_server_conf, "push2worker: ap_queue_push failed");
+ apr_pool_clear(cs->p);
+ ap_push_pool(worker_queue_info, cs->p);
+ }
+
+ return APR_SUCCESS;
+}
+
+/* get_worker:
+ * reserve a worker thread, block if all are currently busy.
+ * this prevents the worker queue from overflowing and lets
+ * other processes accept new connections in the mean time.
+ */
+static int get_worker(int *have_idle_worker_p)
+{
+ apr_status_t rc;
+
+ if (!*have_idle_worker_p) {
+ rc = ap_queue_info_wait_for_idler(worker_queue_info);
+
+ if (rc == APR_SUCCESS) {
+ *have_idle_worker_p = 1;
+ return 1;
+ }
+ else {
+ if (!APR_STATUS_IS_EOF(rc)) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, rc, ap_server_conf,
+ "ap_queue_info_wait_for_idler failed. "
+ "Attempting to shutdown process gracefully");
+ signal_threads(ST_GRACEFUL);
+ }
+ return 0;
+ }
+ }
+ else {
+ /* already reserved a worker thread - must have hit a
+ * transient error on a previous pass
+ */
+ return 1;
+ }
+}
+
+static void *listener_thread(apr_thread_t * thd, void *dummy)
+{
+ apr_status_t rc;
+ proc_info *ti = dummy;
+ int process_slot = ti->pid;
+ apr_pool_t *tpool = apr_thread_pool_get(thd);
+ void *csd = NULL;
+ apr_pool_t *ptrans; /* Pool for per-transaction stuff */
+ ap_listen_rec *lr;
+ int have_idle_worker = 0;
+ conn_state_t *cs;
+ const apr_pollfd_t *out_pfd;
+ apr_int32_t num = 0;
+ apr_time_t time_now = 0;
+ apr_interval_time_t timeout_interval;
+ apr_time_t timeout_time;
+ listener_poll_type *pt;
+
+ free(ti);
+
+ /* We set this to force apr_pollset to wakeup if there hasn't been any IO
+ * on any of its sockets. This allows sockets to have been added
+ * when no other keepalive operations where going on.
+ *
+ * current value is 1 second
+ */
+ timeout_interval = 1000000;
+
+ /* the following times out events that are really close in the future
+ * to prevent extra poll calls
+ *
+ * current value is .1 second
+ */
+#define TIMEOUT_FUDGE_FACTOR 100000
+
+ /* POLLSET_SCALE_FACTOR * ap_threads_per_child sets the size of
+ * the pollset. I've seen 15 connections per active worker thread
+ * running SPECweb99.
+ *
+ * However, with the newer apr_pollset, this is the number of sockets that
+ * we will return to any *one* call to poll(). Therefore, there is no
+ * reason to make it more than ap_threads_per_child.
+ */
+#define POLLSET_SCALE_FACTOR 1
+
+ rc = apr_thread_mutex_create(&timeout_mutex, APR_THREAD_MUTEX_DEFAULT,
+ tpool);
+ if (rc != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, rc, ap_server_conf,
+ "creation of the timeout mutex failed. Attempting to "
+ "shutdown process gracefully");
+ signal_threads(ST_GRACEFUL);
+ return NULL;
+ }
+
+ APR_RING_INIT(&timeout_head, conn_state_t, timeout_list);
+
+ /* Create the main pollset */
+ rc = apr_pollset_create(&event_pollset,
+ ap_threads_per_child * POLLSET_SCALE_FACTOR,
+ tpool, APR_POLLSET_THREADSAFE);
+ if (rc != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, rc, ap_server_conf,
+ "apr_pollset_create with Thread Safety failed. "
+ "Attempting to shutdown process gracefully");
+ signal_threads(ST_GRACEFUL);
+ return NULL;
+ }
+
+ for (lr = ap_listeners; lr != NULL; lr = lr->next) {
+ apr_pollfd_t pfd = { 0 };
+ pt = apr_pcalloc(tpool, sizeof(*pt));
+ pfd.desc_type = APR_POLL_SOCKET;
+ pfd.desc.s = lr->sd;
+ pfd.reqevents = APR_POLLIN;
+
+ pt->type = PT_ACCEPT;
+ pt->baton = lr;
+
+ pfd.client_data = pt;
+
+ apr_socket_opt_set(pfd.desc.s, APR_SO_NONBLOCK, 1);
+ apr_pollset_add(event_pollset, &pfd);
+ }
+
+ /* Unblock the signal used to wake this thread up, and set a handler for
+ * it.
+ */
+ unblock_signal(LISTENER_SIGNAL);
+ apr_signal(LISTENER_SIGNAL, dummy_signal_handler);
+
+ while (!listener_may_exit) {
+
+ if (requests_this_child <= 0) {
+ check_infinite_requests();
+ }
+
+ rc = apr_pollset_poll(event_pollset, timeout_interval, &num,
+ &out_pfd);
+
+ if (rc != APR_SUCCESS) {
+ if (APR_STATUS_IS_EINTR(rc)) {
+ continue;
+ }
+ if (!APR_STATUS_IS_TIMEUP(rc)) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, rc, ap_server_conf,
+ "apr_pollset_poll failed. Attempting to "
+ "shutdown process gracefully");
+ signal_threads(ST_GRACEFUL);
+ }
+ }
+
+ if (listener_may_exit)
+ break;
+
+ while (num && get_worker(&have_idle_worker)) {
+ pt = (listener_poll_type *) out_pfd->client_data;
+ if (pt->type == PT_CSD) {
+ /* one of the sockets is readable */
+ cs = (conn_state_t *) pt->baton;
+ switch (cs->state) {
+ case CONN_STATE_CHECK_REQUEST_LINE_READABLE:
+ cs->state = CONN_STATE_READ_REQUEST_LINE;
+ break;
+ default:
+ ap_log_error(APLOG_MARK, APLOG_ERR, rc,
+ ap_server_conf,
+ "event_loop: unexpected state %d",
+ cs->state);
+ AP_DEBUG_ASSERT(0);
+ }
+
+ apr_thread_mutex_lock(timeout_mutex);
+ APR_RING_REMOVE(cs, timeout_list);
+ apr_thread_mutex_unlock(timeout_mutex);
+
+ rc = push2worker(out_pfd, event_pollset);
+ if (rc != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rc,
+ ap_server_conf, "push2worker failed");
+ }
+ else {
+ have_idle_worker = 0;
+ }
+ }
+ else {
+ /* A Listener Socket is ready for an accept() */
+ apr_pool_t *recycled_pool = NULL;
+
+ lr = (ap_listen_rec *) pt->baton;
+
+ ap_pop_pool(&recycled_pool, worker_queue_info);
+
+ if (recycled_pool == NULL) {
+ /* create a new transaction pool for each accepted socket */
+ apr_allocator_t *allocator;
+
+ apr_allocator_create(&allocator);
+ apr_allocator_max_free_set(allocator,
+ ap_max_mem_free);
+ apr_pool_create_ex(&ptrans, pconf, NULL, allocator);
+ apr_allocator_owner_set(allocator, ptrans);
+ if (ptrans == NULL) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rc,
+ ap_server_conf,
+ "Failed to create transaction pool");
+ signal_threads(ST_GRACEFUL);
+ return NULL;
+ }
+ }
+ else {
+ ptrans = recycled_pool;
+ }
+
+ apr_pool_tag(ptrans, "transaction");
+
+ rc = lr->accept_func(&csd, lr, ptrans);
+
+ /* later we trash rv and rely on csd to indicate
+ * success/failure
+ */
+ AP_DEBUG_ASSERT(rc == APR_SUCCESS || !csd);
+
+ if (rc == APR_EGENERAL) {
+ /* E[NM]FILE, ENOMEM, etc */
+ resource_shortage = 1;
+ signal_threads(ST_GRACEFUL);
+ }
+
+ if (csd != NULL) {
+ rc = ap_queue_push(worker_queue, csd, NULL, ptrans);
+ if (rc != APR_SUCCESS) {
+ /* trash the connection; we couldn't queue the connected
+ * socket to a worker
+ */
+ apr_socket_close(csd);
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rc,
+ ap_server_conf,
+ "ap_queue_push failed");
+ apr_pool_clear(ptrans);
+ ap_push_pool(worker_queue_info, ptrans);
+ }
+ else {
+ have_idle_worker = 0;
+ }
+ }
+ else {
+ apr_pool_clear(ptrans);
+ ap_push_pool(worker_queue_info, ptrans);
+ }
+ } /* if:else on pt->type */
+ out_pfd++;
+ num--;
+ } /* while for processing poll */
+
+ /* XXX possible optimization: stash the current time for use as
+ * r->request_time for new requests
+ */
+ time_now = apr_time_now();
+
+ /* handle timed out sockets */
+ apr_thread_mutex_lock(timeout_mutex);
+
+ cs = APR_RING_FIRST(&timeout_head);
+ timeout_time = time_now + TIMEOUT_FUDGE_FACTOR;
+ while (!APR_RING_EMPTY(&timeout_head, conn_state_t, timeout_list)
+ && cs->expiration_time < timeout_time
+ && get_worker(&have_idle_worker)) {
+
+ cs->state = CONN_STATE_LINGER;
+
+ APR_RING_REMOVE(cs, timeout_list);
+
+ rc = push2worker(&cs->pfd, event_pollset);
+
+ if (rc != APR_SUCCESS) {
+ return NULL;
+ /* XXX return NULL looks wrong - not an init failure
+ * that bypasses all the cleanup outside the main loop
+ * break seems more like it
+ * need to evaluate seriousness of push2worker failures
+ */
+ }
+ have_idle_worker = 0;
+ cs = APR_RING_FIRST(&timeout_head);
+ }
+ apr_thread_mutex_unlock(timeout_mutex);
+
+ } /* listener main loop */
+
+ ap_close_listeners();
+ ap_queue_term(worker_queue);
+ dying = 1;
+ ap_scoreboard_image->parent[process_slot].quiescing = 1;
+
+ /* wake up the main thread */
+ kill(ap_my_pid, SIGTERM);
+
+ apr_thread_exit(thd, APR_SUCCESS);
+ return NULL;
+}
+
+/* XXX For ungraceful termination/restart, we definitely don't want to
+ * wait for active connections to finish but we may want to wait
+ * for idle workers to get out of the queue code and release mutexes,
+ * since those mutexes are cleaned up pretty soon and some systems
+ * may not react favorably (i.e., segfault) if operations are attempted
+ * on cleaned-up mutexes.
+ */
+static void *APR_THREAD_FUNC worker_thread(apr_thread_t * thd, void *dummy)
+{
+ proc_info *ti = dummy;
+ int process_slot = ti->pid;
+ int thread_slot = ti->tid;
+ apr_socket_t *csd = NULL;
+ conn_state_t *cs;
+ apr_pool_t *ptrans; /* Pool for per-transaction stuff */
+ apr_status_t rv;
+ int is_idle = 0;
+
+ free(ti);
+
+ ap_scoreboard_image->servers[process_slot][thread_slot].pid = ap_my_pid;
+ ap_scoreboard_image->servers[process_slot][thread_slot].generation = ap_my_generation;
+ ap_update_child_status_from_indexes(process_slot, thread_slot,
+ SERVER_STARTING, NULL);
+
+ while (!workers_may_exit) {
+ if (!is_idle) {
+ rv = ap_queue_info_set_idle(worker_queue_info, NULL);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
+ "ap_queue_info_set_idle failed. Attempting to "
+ "shutdown process gracefully.");
+ signal_threads(ST_GRACEFUL);
+ break;
+ }
+ is_idle = 1;
+ }
+
+ ap_update_child_status_from_indexes(process_slot, thread_slot,
+ SERVER_READY, NULL);
+ worker_pop:
+ if (workers_may_exit) {
+ break;
+ }
+ rv = ap_queue_pop(worker_queue, &csd, &cs, &ptrans);
+
+ if (rv != APR_SUCCESS) {
+ /* We get APR_EOF during a graceful shutdown once all the
+ * connections accepted by this server process have been handled.
+ */
+ if (APR_STATUS_IS_EOF(rv)) {
+ break;
+ }
+ /* We get APR_EINTR whenever ap_queue_pop() has been interrupted
+ * from an explicit call to ap_queue_interrupt_all(). This allows
+ * us to unblock threads stuck in ap_queue_pop() when a shutdown
+ * is pending.
+ *
+ * If workers_may_exit is set and this is ungraceful termination/
+ * restart, we are bound to get an error on some systems (e.g.,
+ * AIX, which sanity-checks mutex operations) since the queue
+ * may have already been cleaned up. Don't log the "error" if
+ * workers_may_exit is set.
+ */
+ else if (APR_STATUS_IS_EINTR(rv)) {
+ goto worker_pop;
+ }
+ /* We got some other error. */
+ else if (!workers_may_exit) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "ap_queue_pop failed");
+ }
+ continue;
+ }
+ is_idle = 0;
+ worker_sockets[thread_slot] = csd;
+ rv = process_socket(ptrans, csd, cs, process_slot, thread_slot);
+ if (!rv) {
+ requests_this_child--;
+ }
+ worker_sockets[thread_slot] = NULL;
+ }
+
+ ap_update_child_status_from_indexes(process_slot, thread_slot,
+ (dying) ? SERVER_DEAD :
+ SERVER_GRACEFUL,
+ (request_rec *) NULL);
+
+ apr_thread_exit(thd, APR_SUCCESS);
+ return NULL;
+}
+
+static int check_signal(int signum)
+{
+ switch (signum) {
+ case SIGTERM:
+ case SIGINT:
+ return 1;
+ }
+ return 0;
+}
+
+
+
+static void create_listener_thread(thread_starter * ts)
+{
+ int my_child_num = ts->child_num_arg;
+ apr_threadattr_t *thread_attr = ts->threadattr;
+ proc_info *my_info;
+ apr_status_t rv;
+
+ my_info = (proc_info *) malloc(sizeof(proc_info));
+ my_info->pid = my_child_num;
+ my_info->tid = -1; /* listener thread doesn't have a thread slot */
+ my_info->sd = 0;
+ rv = apr_thread_create(&ts->listener, thread_attr, listener_thread,
+ my_info, pchild);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
+ "apr_thread_create: unable to create listener thread");
+ /* let the parent decide how bad this really is */
+ clean_child_exit(APEXIT_CHILDSICK);
+ }
+ apr_os_thread_get(&listener_os_thread, ts->listener);
+}
+
+/* XXX under some circumstances not understood, children can get stuck
+ * in start_threads forever trying to take over slots which will
+ * never be cleaned up; for now there is an APLOG_DEBUG message issued
+ * every so often when this condition occurs
+ */
+static void *APR_THREAD_FUNC start_threads(apr_thread_t * thd, void *dummy)
+{
+ thread_starter *ts = dummy;
+ apr_thread_t **threads = ts->threads;
+ apr_threadattr_t *thread_attr = ts->threadattr;
+ int child_num_arg = ts->child_num_arg;
+ int my_child_num = child_num_arg;
+ proc_info *my_info;
+ apr_status_t rv;
+ int i;
+ int threads_created = 0;
+ int listener_started = 0;
+ int loops;
+ int prev_threads_created;
+
+ /* We must create the fd queues before we start up the listener
+ * and worker threads. */
+ worker_queue = apr_pcalloc(pchild, sizeof(*worker_queue));
+ rv = ap_queue_init(worker_queue, ap_threads_per_child, pchild);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
+ "ap_queue_init() failed");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ rv = ap_queue_info_create(&worker_queue_info, pchild,
+ ap_threads_per_child);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
+ "ap_queue_info_create() failed");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ worker_sockets = apr_pcalloc(pchild, ap_threads_per_child
+ * sizeof(apr_socket_t *));
+
+ loops = prev_threads_created = 0;
+ while (1) {
+ /* ap_threads_per_child does not include the listener thread */
+ for (i = 0; i < ap_threads_per_child; i++) {
+ int status =
+ ap_scoreboard_image->servers[child_num_arg][i].status;
+
+ if (status != SERVER_GRACEFUL && status != SERVER_DEAD) {
+ continue;
+ }
+
+ my_info = (proc_info *) malloc(sizeof(proc_info));
+ if (my_info == NULL) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
+ "malloc: out of memory");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+ my_info->pid = my_child_num;
+ my_info->tid = i;
+ my_info->sd = 0;
+
+ /* We are creating threads right now */
+ ap_update_child_status_from_indexes(my_child_num, i,
+ SERVER_STARTING, NULL);
+ /* We let each thread update its own scoreboard entry. This is
+ * done because it lets us deal with tid better.
+ */
+ rv = apr_thread_create(&threads[i], thread_attr,
+ worker_thread, my_info, pchild);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
+ "apr_thread_create: unable to create worker thread");
+ /* let the parent decide how bad this really is */
+ clean_child_exit(APEXIT_CHILDSICK);
+ }
+ threads_created++;
+ }
+
+ /* Start the listener only when there are workers available */
+ if (!listener_started && threads_created) {
+ create_listener_thread(ts);
+ listener_started = 1;
+ }
+
+
+ if (start_thread_may_exit || threads_created == ap_threads_per_child) {
+ break;
+ }
+ /* wait for previous generation to clean up an entry */
+ apr_sleep(apr_time_from_sec(1));
+ ++loops;
+ if (loops % 120 == 0) { /* every couple of minutes */
+ if (prev_threads_created == threads_created) {
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "child %" APR_PID_T_FMT " isn't taking over "
+ "slots very quickly (%d of %d)",
+ ap_my_pid, threads_created,
+ ap_threads_per_child);
+ }
+ prev_threads_created = threads_created;
+ }
+ }
+
+ /* What state should this child_main process be listed as in the
+ * scoreboard...?
+ * ap_update_child_status_from_indexes(my_child_num, i, SERVER_STARTING,
+ * (request_rec *) NULL);
+ *
+ * This state should be listed separately in the scoreboard, in some kind
+ * of process_status, not mixed in with the worker threads' status.
+ * "life_status" is almost right, but it's in the worker's structure, and
+ * the name could be clearer. gla
+ */
+ apr_thread_exit(thd, APR_SUCCESS);
+ return NULL;
+}
+
+static void join_workers(apr_thread_t * listener, apr_thread_t ** threads)
+{
+ int i;
+ apr_status_t rv, thread_rv;
+
+ if (listener) {
+ int iter;
+
+ /* deal with a rare timing window which affects waking up the
+ * listener thread... if the signal sent to the listener thread
+ * is delivered between the time it verifies that the
+ * listener_may_exit flag is clear and the time it enters a
+ * blocking syscall, the signal didn't do any good... work around
+ * that by sleeping briefly and sending it again
+ */
+
+ iter = 0;
+ while (iter < 10 &&
+#ifdef HAVE_PTHREAD_KILL
+ pthread_kill(*listener_os_thread, 0)
+#else
+ kill(ap_my_pid, 0)
+#endif
+ == 0) {
+ /* listener not dead yet */
+ apr_sleep(apr_time_make(0, 500000));
+ wakeup_listener();
+ ++iter;
+ }
+ if (iter >= 10) {
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "the listener thread didn't exit");
+ }
+ else {
+ rv = apr_thread_join(&thread_rv, listener);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "apr_thread_join: unable to join listener thread");
+ }
+ }
+ }
+
+ for (i = 0; i < ap_threads_per_child; i++) {
+ if (threads[i]) { /* if we ever created this thread */
+ rv = apr_thread_join(&thread_rv, threads[i]);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "apr_thread_join: unable to join worker "
+ "thread %d", i);
+ }
+ }
+ }
+}
+
+static void join_start_thread(apr_thread_t * start_thread_id)
+{
+ apr_status_t rv, thread_rv;
+
+ start_thread_may_exit = 1; /* tell it to give up in case it is still
+ * trying to take over slots from a
+ * previous generation
+ */
+ rv = apr_thread_join(&thread_rv, start_thread_id);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "apr_thread_join: unable to join the start " "thread");
+ }
+}
+
+static void child_main(int child_num_arg)
+{
+ apr_thread_t **threads;
+ apr_status_t rv;
+ thread_starter *ts;
+ apr_threadattr_t *thread_attr;
+ apr_thread_t *start_thread_id;
+
+ mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this
+ * child initializes
+ */
+ ap_my_pid = getpid();
+ ap_fatal_signal_child_setup(ap_server_conf);
+ apr_pool_create(&pchild, pconf);
+
+ /*stuff to do before we switch id's, so we have permissions. */
+ ap_reopen_scoreboard(pchild, NULL, 0);
+
+ if (unixd_setup_child()) {
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ ap_run_child_init(pchild, ap_server_conf);
+
+ /* done with init critical section */
+
+ /* Just use the standard apr_setup_signal_thread to block all signals
+ * from being received. The child processes no longer use signals for
+ * any communication with the parent process.
+ */
+ rv = apr_setup_signal_thread();
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
+ "Couldn't initialize signal thread");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ if (ap_max_requests_per_child) {
+ requests_this_child = ap_max_requests_per_child;
+ }
+ else {
+ /* coding a value of zero means infinity */
+ requests_this_child = INT_MAX;
+ }
+
+ /* Setup worker threads */
+
+ /* clear the storage; we may not create all our threads immediately,
+ * and we want a 0 entry to indicate a thread which was not created
+ */
+ threads = (apr_thread_t **) calloc(1,
+ sizeof(apr_thread_t *) *
+ ap_threads_per_child);
+ if (threads == NULL) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
+ "malloc: out of memory");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ ts = (thread_starter *) apr_palloc(pchild, sizeof(*ts));
+
+ apr_threadattr_create(&thread_attr, pchild);
+ /* 0 means PTHREAD_CREATE_JOINABLE */
+ apr_threadattr_detach_set(thread_attr, 0);
+
+ if (ap_thread_stacksize != 0) {
+ apr_threadattr_stacksize_set(thread_attr, ap_thread_stacksize);
+ }
+
+ ts->threads = threads;
+ ts->listener = NULL;
+ ts->child_num_arg = child_num_arg;
+ ts->threadattr = thread_attr;
+
+ rv = apr_thread_create(&start_thread_id, thread_attr, start_threads,
+ ts, pchild);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
+ "apr_thread_create: unable to create worker thread");
+ /* let the parent decide how bad this really is */
+ clean_child_exit(APEXIT_CHILDSICK);
+ }
+
+ mpm_state = AP_MPMQ_RUNNING;
+
+ /* If we are only running in one_process mode, we will want to
+ * still handle signals. */
+ if (one_process) {
+ /* Block until we get a terminating signal. */
+ apr_signal_thread(check_signal);
+ /* make sure the start thread has finished; signal_threads()
+ * and join_workers() depend on that
+ */
+ /* XXX join_start_thread() won't be awakened if one of our
+ * threads encounters a critical error and attempts to
+ * shutdown this child
+ */
+ join_start_thread(start_thread_id);
+
+ /* helps us terminate a little more quickly than the dispatch of the
+ * signal thread; beats the Pipe of Death and the browsers
+ */
+ signal_threads(ST_UNGRACEFUL);
+
+ /* A terminating signal was received. Now join each of the
+ * workers to clean them up.
+ * If the worker already exited, then the join frees
+ * their resources and returns.
+ * If the worker hasn't exited, then this blocks until
+ * they have (then cleans up).
+ */
+ join_workers(ts->listener, threads);
+ }
+ else { /* !one_process */
+ /* remove SIGTERM from the set of blocked signals... if one of
+ * the other threads in the process needs to take us down
+ * (e.g., for MaxRequestsPerChild) it will send us SIGTERM
+ */
+ unblock_signal(SIGTERM);
+ apr_signal(SIGTERM, dummy_signal_handler);
+ /* Watch for any messages from the parent over the POD */
+ while (1) {
+ rv = ap_mpm_pod_check(pod);
+ if (rv == AP_NORESTART) {
+ /* see if termination was triggered while we slept */
+ switch (terminate_mode) {
+ case ST_GRACEFUL:
+ rv = AP_GRACEFUL;
+ break;
+ case ST_UNGRACEFUL:
+ rv = AP_RESTART;
+ break;
+ }
+ }
+ if (rv == AP_GRACEFUL || rv == AP_RESTART) {
+ /* make sure the start thread has finished;
+ * signal_threads() and join_workers depend on that
+ */
+ join_start_thread(start_thread_id);
+ signal_threads(rv ==
+ AP_GRACEFUL ? ST_GRACEFUL : ST_UNGRACEFUL);
+ break;
+ }
+ }
+
+ /* A terminating signal was received. Now join each of the
+ * workers to clean them up.
+ * If the worker already exited, then the join frees
+ * their resources and returns.
+ * If the worker hasn't exited, then this blocks until
+ * they have (then cleans up).
+ */
+ join_workers(ts->listener, threads);
+ }
+
+ free(threads);
+
+ clean_child_exit(resource_shortage ? APEXIT_CHILDSICK : 0);
+}
+
+static int make_child(server_rec * s, int slot)
+{
+ int pid;
+
+ if (slot + 1 > ap_max_daemons_limit) {
+ ap_max_daemons_limit = slot + 1;
+ }
+
+ if (one_process) {
+ set_signals();
+ ap_scoreboard_image->parent[slot].pid = getpid();
+ child_main(slot);
+ }
+
+ if ((pid = fork()) == -1) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, errno, s,
+ "fork: Unable to fork new process");
+
+ /* fork didn't succeed. Fix the scoreboard or else
+ * it will say SERVER_STARTING forever and ever
+ */
+ ap_update_child_status_from_indexes(slot, 0, SERVER_DEAD, NULL);
+
+ /* In case system resources are maxxed out, we don't want
+ Apache running away with the CPU trying to fork over and
+ over and over again. */
+ apr_sleep(apr_time_from_sec(10));
+
+ return -1;
+ }
+
+ if (!pid) {
+#ifdef HAVE_BINDPROCESSOR
+ /* By default, AIX binds to a single processor. This bit unbinds
+ * children which will then bind to another CPU.
+ */
+ int status = bindprocessor(BINDPROCESS, (int) getpid(),
+ PROCESSOR_CLASS_ANY);
+ if (status != OK)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno,
+ ap_server_conf,
+ "processor unbind failed %d", status);
+#endif
+ RAISE_SIGSTOP(MAKE_CHILD);
+
+ apr_signal(SIGTERM, just_die);
+ child_main(slot);
+
+ clean_child_exit(0);
+ }
+ /* else */
+ ap_scoreboard_image->parent[slot].quiescing = 0;
+ ap_scoreboard_image->parent[slot].pid = pid;
+ return 0;
+}
+
+/* start up a bunch of children */
+static void startup_children(int number_to_start)
+{
+ int i;
+
+ for (i = 0; number_to_start && i < ap_daemons_limit; ++i) {
+ if (ap_scoreboard_image->parent[i].pid != 0) {
+ continue;
+ }
+ if (make_child(ap_server_conf, i) < 0) {
+ break;
+ }
+ --number_to_start;
+ }
+}
+
+
+/*
+ * idle_spawn_rate is the number of children that will be spawned on the
+ * next maintenance cycle if there aren't enough idle servers. It is
+ * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
+ * without the need to spawn.
+ */
+static int idle_spawn_rate = 1;
+#ifndef MAX_SPAWN_RATE
+#define MAX_SPAWN_RATE (32)
+#endif
+static int hold_off_on_exponential_spawning;
+
+static void perform_idle_server_maintenance(void)
+{
+ int i, j;
+ int idle_thread_count;
+ worker_score *ws;
+ process_score *ps;
+ int free_length;
+ int totally_free_length = 0;
+ int free_slots[MAX_SPAWN_RATE];
+ int last_non_dead;
+ int total_non_dead;
+ int active_thread_count = 0;
+
+ /* initialize the free_list */
+ free_length = 0;
+
+ idle_thread_count = 0;
+ last_non_dead = -1;
+ total_non_dead = 0;
+
+ for (i = 0; i < ap_daemons_limit; ++i) {
+ /* Initialization to satisfy the compiler. It doesn't know
+ * that ap_threads_per_child is always > 0 */
+ int status = SERVER_DEAD;
+ int any_dying_threads = 0;
+ int any_dead_threads = 0;
+ int all_dead_threads = 1;
+
+ if (i >= ap_max_daemons_limit
+ && totally_free_length == idle_spawn_rate)
+ break;
+ ps = &ap_scoreboard_image->parent[i];
+ for (j = 0; j < ap_threads_per_child; j++) {
+ ws = &ap_scoreboard_image->servers[i][j];
+ status = ws->status;
+
+ /* XXX any_dying_threads is probably no longer needed GLA */
+ any_dying_threads = any_dying_threads ||
+ (status == SERVER_GRACEFUL);
+ any_dead_threads = any_dead_threads || (status == SERVER_DEAD);
+ all_dead_threads = all_dead_threads &&
+ (status == SERVER_DEAD || status == SERVER_GRACEFUL);
+
+ /* We consider a starting server as idle because we started it
+ * at least a cycle ago, and if it still hasn't finished starting
+ * then we're just going to swamp things worse by forking more.
+ * So we hopefully won't need to fork more if we count it.
+ * This depends on the ordering of SERVER_READY and SERVER_STARTING.
+ */
+ if (ps->pid != 0) { /* XXX just set all_dead_threads in outer
+ for loop if no pid? not much else matters */
+ if (status <= SERVER_READY &&
+ !ps->quiescing && ps->generation == ap_my_generation) {
+ ++idle_thread_count;
+ }
+ if (status >= SERVER_READY && status < SERVER_GRACEFUL) {
+ ++active_thread_count;
+ }
+ }
+ }
+ if (any_dead_threads
+ && totally_free_length < idle_spawn_rate
+ && free_length < MAX_SPAWN_RATE
+ && (!ps->pid /* no process in the slot */
+ || ps->quiescing)) { /* or at least one is going away */
+ if (all_dead_threads) {
+ /* great! we prefer these, because the new process can
+ * start more threads sooner. So prioritize this slot
+ * by putting it ahead of any slots with active threads.
+ *
+ * first, make room by moving a slot that's potentially still
+ * in use to the end of the array
+ */
+ free_slots[free_length] = free_slots[totally_free_length];
+ free_slots[totally_free_length++] = i;
+ }
+ else {
+ /* slot is still in use - back of the bus
+ */
+ free_slots[free_length] = i;
+ }
+ ++free_length;
+ }
+ /* XXX if (!ps->quiescing) is probably more reliable GLA */
+ if (!any_dying_threads) {
+ last_non_dead = i;
+ ++total_non_dead;
+ }
+ }
+
+ if (sick_child_detected) {
+ if (active_thread_count > 0) {
+ /* some child processes appear to be working. don't kill the
+ * whole server.
+ */
+ sick_child_detected = 0;
+ }
+ else {
+ /* looks like a basket case. give up.
+ */
+ shutdown_pending = 1;
+ child_fatal = 1;
+ ap_log_error(APLOG_MARK, APLOG_ALERT, 0,
+ ap_server_conf,
+ "No active workers found..."
+ " Apache is exiting!");
+ /* the child already logged the failure details */
+ return;
+ }
+ }
+
+ ap_max_daemons_limit = last_non_dead + 1;
+
+ if (idle_thread_count > max_spare_threads) {
+ /* Kill off one child */
+ ap_mpm_pod_signal(pod, TRUE);
+ idle_spawn_rate = 1;
+ }
+ else if (idle_thread_count < min_spare_threads) {
+ /* terminate the free list */
+ if (free_length == 0) {
+ /* only report this condition once */
+ static int reported = 0;
+
+ if (!reported) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0,
+ ap_server_conf,
+ "server reached MaxClients setting, consider"
+ " raising the MaxClients setting");
+ reported = 1;
+ }
+ idle_spawn_rate = 1;
+ }
+ else {
+ if (free_length > idle_spawn_rate) {
+ free_length = idle_spawn_rate;
+ }
+ if (idle_spawn_rate >= 8) {
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0,
+ ap_server_conf,
+ "server seems busy, (you may need "
+ "to increase StartServers, ThreadsPerChild "
+ "or Min/MaxSpareThreads), "
+ "spawning %d children, there are around %d idle "
+ "threads, and %d total children", free_length,
+ idle_thread_count, total_non_dead);
+ }
+ for (i = 0; i < free_length; ++i) {
+ make_child(ap_server_conf, free_slots[i]);
+ }
+ /* the next time around we want to spawn twice as many if this
+ * wasn't good enough, but not if we've just done a graceful
+ */
+ if (hold_off_on_exponential_spawning) {
+ --hold_off_on_exponential_spawning;
+ }
+ else if (idle_spawn_rate < MAX_SPAWN_RATE) {
+ idle_spawn_rate *= 2;
+ }
+ }
+ }
+ else {
+ idle_spawn_rate = 1;
+ }
+}
+
+static void server_main_loop(int remaining_children_to_start)
+{
+ int child_slot;
+ apr_exit_why_e exitwhy;
+ int status, processed_status;
+ apr_proc_t pid;
+ int i;
+
+ while (!restart_pending && !shutdown_pending) {
+ ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
+
+ if (pid.pid != -1) {
+ processed_status = ap_process_child_status(&pid, exitwhy, status);
+ if (processed_status == APEXIT_CHILDFATAL) {
+ shutdown_pending = 1;
+ child_fatal = 1;
+ return;
+ }
+ else if (processed_status == APEXIT_CHILDSICK) {
+ /* tell perform_idle_server_maintenance to check into this
+ * on the next timer pop
+ */
+ sick_child_detected = 1;
+ }
+ /* non-fatal death... note that it's gone in the scoreboard. */
+ child_slot = find_child_by_pid(&pid);
+ if (child_slot >= 0) {
+ for (i = 0; i < ap_threads_per_child; i++)
+ ap_update_child_status_from_indexes(child_slot, i,
+ SERVER_DEAD,
+ (request_rec *) NULL);
+
+ ap_scoreboard_image->parent[child_slot].pid = 0;
+ ap_scoreboard_image->parent[child_slot].quiescing = 0;
+ if (processed_status == APEXIT_CHILDSICK) {
+ /* resource shortage, minimize the fork rate */
+ idle_spawn_rate = 1;
+ }
+ else if (remaining_children_to_start
+ && child_slot < ap_daemons_limit) {
+ /* we're still doing a 1-for-1 replacement of dead
+ * children with new children
+ */
+ make_child(ap_server_conf, child_slot);
+ --remaining_children_to_start;
+ }
+#if APR_HAS_OTHER_CHILD
+ }
+ else if (apr_proc_other_child_alert(&pid, APR_OC_REASON_DEATH,
+ status) == 0) {
+ /* handled */
+#endif
+ }
+ else if (is_graceful) {
+ /* Great, we've probably just lost a slot in the
+ * scoreboard. Somehow we don't know about this child.
+ */
+ ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
+ ap_server_conf,
+ "long lost child came home! (pid %ld)",
+ (long) pid.pid);
+ }
+ /* Don't perform idle maintenance when a child dies,
+ * only do it when there's a timeout. Remember only a
+ * finite number of children can die, and it's pretty
+ * pathological for a lot to die suddenly.
+ */
+ continue;
+ }
+ else if (remaining_children_to_start) {
+ /* we hit a 1 second timeout in which none of the previous
+ * generation of children needed to be reaped... so assume
+ * they're all done, and pick up the slack if any is left.
+ */
+ startup_children(remaining_children_to_start);
+ remaining_children_to_start = 0;
+ /* In any event we really shouldn't do the code below because
+ * few of the servers we just started are in the IDLE state
+ * yet, so we'd mistakenly create an extra server.
+ */
+ continue;
+ }
+
+ perform_idle_server_maintenance();
+ }
+}
+
+int ap_mpm_run(apr_pool_t * _pconf, apr_pool_t * plog, server_rec * s)
+{
+ int remaining_children_to_start;
+
+ ap_log_pid(pconf, ap_pid_fname);
+
+ first_server_limit = server_limit;
+ first_thread_limit = thread_limit;
+
+ if (changed_limit_at_restart) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
+ "WARNING: Attempt to change ServerLimit or ThreadLimit "
+ "ignored during restart");
+ changed_limit_at_restart = 0;
+ }
+
+ if (!is_graceful) {
+ if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
+ mpm_state = AP_MPMQ_STOPPING;
+ return 1;
+ }
+ /* fix the generation number in the global score; we just got a new,
+ * cleared scoreboard
+ */
+ ap_scoreboard_image->global->running_generation = ap_my_generation;
+ }
+
+ set_signals();
+ /* Don't thrash... */
+ if (max_spare_threads < min_spare_threads + ap_threads_per_child)
+ max_spare_threads = min_spare_threads + ap_threads_per_child;
+
+ /* If we're doing a graceful_restart then we're going to see a lot
+ * of children exiting immediately when we get into the main loop
+ * below (because we just sent them AP_SIG_GRACEFUL). This happens pretty
+ * rapidly... and for each one that exits we'll start a new one until
+ * we reach at least daemons_min_free. But we may be permitted to
+ * start more than that, so we'll just keep track of how many we're
+ * supposed to start up without the 1 second penalty between each fork.
+ */
+ remaining_children_to_start = ap_daemons_to_start;
+ if (remaining_children_to_start > ap_daemons_limit) {
+ remaining_children_to_start = ap_daemons_limit;
+ }
+ if (!is_graceful) {
+ startup_children(remaining_children_to_start);
+ remaining_children_to_start = 0;
+ }
+ else {
+ /* give the system some time to recover before kicking into
+ * exponential mode */
+ hold_off_on_exponential_spawning = 10;
+ }
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "%s configured -- resuming normal operations",
+ ap_get_server_version());
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
+ "Server built: %s", ap_get_server_built());
+
+ restart_pending = shutdown_pending = 0;
+ mpm_state = AP_MPMQ_RUNNING;
+
+ server_main_loop(remaining_children_to_start);
+ mpm_state = AP_MPMQ_STOPPING;
+
+ if (shutdown_pending && !is_graceful) {
+ /* Time to shut down:
+ * Kill child processes, tell them to call child_exit, etc...
+ */
+ ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
+ ap_reclaim_child_processes(1); /* Start with SIGTERM */
+
+ if (!child_fatal) {
+ /* cleanup pid file on normal shutdown */
+ const char *pidfile = NULL;
+ pidfile = ap_server_root_relative(pconf, ap_pid_fname);
+ if (pidfile != NULL && unlink(pidfile) == 0)
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0,
+ ap_server_conf,
+ "removed PID file %s (pid=%ld)",
+ pidfile, (long) getpid());
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0,
+ ap_server_conf, "caught SIGTERM, shutting down");
+ }
+ return 1;
+ } else if (shutdown_pending) {
+ /* Time to gracefully shut down:
+ * Kill child processes, tell them to call child_exit, etc...
+ */
+ int active_children;
+ int index;
+ apr_time_t cutoff = 0;
+
+ /* Close our listeners, and then ask our children to do same */
+ ap_close_listeners();
+ ap_mpm_pod_killpg(pod, ap_daemons_limit, TRUE);
+ ap_relieve_child_processes();
+
+ if (!child_fatal) {
+ /* cleanup pid file on normal shutdown */
+ const char *pidfile = NULL;
+ pidfile = ap_server_root_relative (pconf, ap_pid_fname);
+ if ( pidfile != NULL && unlink(pidfile) == 0)
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0,
+ ap_server_conf,
+ "removed PID file %s (pid=%ld)",
+ pidfile, (long)getpid());
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "caught " AP_SIG_GRACEFUL_STOP_STRING
+ ", shutting down gracefully");
+ }
+
+ if (ap_graceful_shutdown_timeout) {
+ cutoff = apr_time_now() +
+ apr_time_from_sec(ap_graceful_shutdown_timeout);
+ }
+
+ /* Don't really exit until each child has finished */
+ shutdown_pending = 0;
+ do {
+ /* Pause for a second */
+ apr_sleep(apr_time_from_sec(1));
+
+ /* Relieve any children which have now exited */
+ ap_relieve_child_processes();
+
+ active_children = 0;
+ for (index = 0; index < ap_daemons_limit; ++index) {
+ if (MPM_CHILD_PID(index) != 0) {
+ if (kill(MPM_CHILD_PID(index), 0) == 0) {
+ active_children = 1;
+ /* Having just one child is enough to stay around */
+ break;
+ }
+ }
+ }
+ } while (!shutdown_pending && active_children &&
+ (!ap_graceful_shutdown_timeout || apr_time_now() < cutoff));
+
+ /* We might be here because we received SIGTERM, either
+ * way, try and make sure that all of our processes are
+ * really dead.
+ */
+ ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
+ ap_reclaim_child_processes(1);
+
+ return 1;
+ }
+
+ /* we've been told to restart */
+ apr_signal(SIGHUP, SIG_IGN);
+
+ if (one_process) {
+ /* not worth thinking about */
+ return 1;
+ }
+
+ /* advance to the next generation */
+ /* XXX: we really need to make sure this new generation number isn't in
+ * use by any of the children.
+ */
+ ++ap_my_generation;
+ ap_scoreboard_image->global->running_generation = ap_my_generation;
+
+ if (is_graceful) {
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ AP_SIG_GRACEFUL_STRING
+ " received. Doing graceful restart");
+ /* wake up the children...time to die. But we'll have more soon */
+ ap_mpm_pod_killpg(pod, ap_daemons_limit, TRUE);
+
+
+ /* This is mostly for debugging... so that we know what is still
+ * gracefully dealing with existing request.
+ */
+
+ }
+ else {
+ /* Kill 'em all. Since the child acts the same on the parents SIGTERM
+ * and a SIGHUP, we may as well use the same signal, because some user
+ * pthreads are stealing signals from us left and right.
+ */
+ ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
+
+ ap_reclaim_child_processes(1); /* Start with SIGTERM */
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "SIGHUP received. Attempting to restart");
+ }
+
+ return 0;
+}
+
+/* This really should be a post_config hook, but the error log is already
+ * redirected by that point, so we need to do this in the open_logs phase.
+ */
+static int worker_open_logs(apr_pool_t * p, apr_pool_t * plog,
+ apr_pool_t * ptemp, server_rec * s)
+{
+ apr_status_t rv;
+
+ pconf = p;
+ ap_server_conf = s;
+
+ if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT | APLOG_STARTUP, 0,
+ NULL, "no listening sockets available, shutting down");
+ return DONE;
+ }
+
+ if (!one_process) {
+ if ((rv = ap_mpm_pod_open(pconf, &pod))) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT | APLOG_STARTUP, rv, NULL,
+ "Could not open pipe-of-death.");
+ return DONE;
+ }
+ }
+ return OK;
+}
+
+static int worker_pre_config(apr_pool_t * pconf, apr_pool_t * plog,
+ apr_pool_t * ptemp)
+{
+ static int restart_num = 0;
+ int no_detach, debug, foreground;
+ ap_directive_t *pdir;
+ ap_directive_t *max_clients = NULL;
+ apr_status_t rv;
+
+ mpm_state = AP_MPMQ_STARTING;
+
+ /* make sure that "ThreadsPerChild" gets set before "MaxClients" */
+ for (pdir = ap_conftree; pdir != NULL; pdir = pdir->next) {
+ if (strncasecmp(pdir->directive, "ThreadsPerChild", 15) == 0) {
+ if (!max_clients) {
+ /* we're in the clear, got ThreadsPerChild first */
+ break;
+ }
+ else {
+ /* now to swap the data */
+ ap_directive_t temp;
+
+ temp.directive = pdir->directive;
+ temp.args = pdir->args;
+ /* Make sure you don't change 'next', or you may get loops! */
+ /* XXX: first_child, parent, and data can never be set
+ * for these directives, right? -aaron */
+ temp.filename = pdir->filename;
+ temp.line_num = pdir->line_num;
+
+ pdir->directive = max_clients->directive;
+ pdir->args = max_clients->args;
+ pdir->filename = max_clients->filename;
+ pdir->line_num = max_clients->line_num;
+
+ max_clients->directive = temp.directive;
+ max_clients->args = temp.args;
+ max_clients->filename = temp.filename;
+ max_clients->line_num = temp.line_num;
+ break;
+ }
+ }
+ else if (!max_clients
+ && strncasecmp(pdir->directive, "MaxClients", 10) == 0) {
+ max_clients = pdir;
+ }
+ }
+
+ debug = ap_exists_config_define("DEBUG");
+
+ if (debug) {
+ foreground = one_process = 1;
+ no_detach = 0;
+ }
+ else {
+ one_process = ap_exists_config_define("ONE_PROCESS");
+ no_detach = ap_exists_config_define("NO_DETACH");
+ foreground = ap_exists_config_define("FOREGROUND");
+ }
+
+ /* sigh, want this only the second time around */
+ if (restart_num++ == 1) {
+ is_graceful = 0;
+ rv = apr_pollset_create(&event_pollset, 1, plog,
+ APR_POLLSET_THREADSAFE);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
+ "Couldn't create a Thread Safe Pollset. "
+ "Is it supported on your platform?");
+ return HTTP_INTERNAL_SERVER_ERROR;
+ }
+ apr_pollset_destroy(event_pollset);
+
+ if (!one_process && !foreground) {
+ rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
+ : APR_PROC_DETACH_DAEMONIZE);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
+ "apr_proc_detach failed");
+ return HTTP_INTERNAL_SERVER_ERROR;
+ }
+ }
+ parent_pid = ap_my_pid = getpid();
+ }
+
+ unixd_pre_config(ptemp);
+ ap_listen_pre_config();
+ ap_daemons_to_start = DEFAULT_START_DAEMON;
+ min_spare_threads = DEFAULT_MIN_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
+ max_spare_threads = DEFAULT_MAX_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
+ ap_daemons_limit = server_limit;
+ ap_threads_per_child = DEFAULT_THREADS_PER_CHILD;
+ ap_pid_fname = DEFAULT_PIDLOG;
+ ap_lock_fname = DEFAULT_LOCKFILE;
+ ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
+ ap_extended_status = 0;
+#ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
+ ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
+#endif
+
+ apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
+
+ return OK;
+}
+
+static void event_hooks(apr_pool_t * p)
+{
+ /* The worker open_logs phase must run before the core's, or stderr
+ * will be redirected to a file, and the messages won't print to the
+ * console.
+ */
+ static const char *const aszSucc[] = { "core.c", NULL };
+ one_process = 0;
+
+ ap_hook_open_logs(worker_open_logs, NULL, aszSucc, APR_HOOK_MIDDLE);
+ /* we need to set the MPM state before other pre-config hooks use MPM query
+ * to retrieve it, so register as REALLY_FIRST
+ */
+ ap_hook_pre_config(worker_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
+}
+
+static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy,
+ const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_daemons_to_start = atoi(arg);
+ return NULL;
+}
+
+static const char *set_min_spare_threads(cmd_parms * cmd, void *dummy,
+ const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ min_spare_threads = atoi(arg);
+ if (min_spare_threads <= 0) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: detected MinSpareThreads set to non-positive.");
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Resetting to 1 to avoid almost certain Apache failure.");
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Please read the documentation.");
+ min_spare_threads = 1;
+ }
+
+ return NULL;
+}
+
+static const char *set_max_spare_threads(cmd_parms * cmd, void *dummy,
+ const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ max_spare_threads = atoi(arg);
+ return NULL;
+}
+
+static const char *set_max_clients(cmd_parms * cmd, void *dummy,
+ const char *arg)
+{
+ int max_clients;
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ /* It is ok to use ap_threads_per_child here because we are
+ * sure that it gets set before MaxClients in the pre_config stage. */
+ max_clients = atoi(arg);
+ if (max_clients < ap_threads_per_child) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: MaxClients (%d) must be at least as large",
+ max_clients);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " as ThreadsPerChild (%d). Automatically",
+ ap_threads_per_child);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " increasing MaxClients to %d.", ap_threads_per_child);
+ max_clients = ap_threads_per_child;
+ }
+ ap_daemons_limit = max_clients / ap_threads_per_child;
+ if ((max_clients > 0) && (max_clients % ap_threads_per_child)) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: MaxClients (%d) is not an integer multiple",
+ max_clients);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " of ThreadsPerChild (%d), lowering MaxClients to %d",
+ ap_threads_per_child,
+ ap_daemons_limit * ap_threads_per_child);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " for a maximum of %d child processes,",
+ ap_daemons_limit);
+ max_clients = ap_daemons_limit * ap_threads_per_child;
+ }
+ if (ap_daemons_limit > server_limit) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: MaxClients of %d would require %d servers,",
+ max_clients, ap_daemons_limit);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " and would exceed the ServerLimit value of %d.",
+ server_limit);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " Automatically lowering MaxClients to %d. To increase,",
+ server_limit * ap_threads_per_child);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " please see the ServerLimit directive.");
+ ap_daemons_limit = server_limit;
+ }
+ else if (ap_daemons_limit < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require MaxClients > 0, setting to 1");
+ ap_daemons_limit = 1;
+ }
+ return NULL;
+}
+
+static const char *set_threads_per_child(cmd_parms * cmd, void *dummy,
+ const 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 > thread_limit) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: ThreadsPerChild of %d exceeds ThreadLimit "
+ "value of %d", ap_threads_per_child, thread_limit);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "threads, lowering ThreadsPerChild to %d. To increase, "
+ "please see the", thread_limit);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " ThreadLimit directive.");
+ ap_threads_per_child = thread_limit;
+ }
+ else if (ap_threads_per_child < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require ThreadsPerChild > 0, setting to 1");
+ ap_threads_per_child = 1;
+ }
+ return NULL;
+}
+static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg)
+{
+ int tmp_server_limit;
+
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ tmp_server_limit = atoi(arg);
+ /* you cannot change ServerLimit across a restart; ignore
+ * any such attempts
+ */
+ if (first_server_limit &&
+ tmp_server_limit != server_limit) {
+ /* how do we log a message? the error log is a bit bucket at this
+ * point; we'll just have to set a flag so that ap_mpm_run()
+ * logs a warning later
+ */
+ changed_limit_at_restart = 1;
+ return NULL;
+ }
+ server_limit = tmp_server_limit;
+
+ if (server_limit > MAX_SERVER_LIMIT) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: ServerLimit of %d exceeds compile time limit "
+ "of %d servers,", server_limit, MAX_SERVER_LIMIT);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " lowering ServerLimit to %d.", MAX_SERVER_LIMIT);
+ server_limit = MAX_SERVER_LIMIT;
+ }
+ else if (server_limit < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require ServerLimit > 0, setting to 1");
+ server_limit = 1;
+ }
+ return NULL;
+}
+
+static const char *set_thread_limit(cmd_parms * cmd, void *dummy,
+ const char *arg)
+{
+ int tmp_thread_limit;
+
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ tmp_thread_limit = atoi(arg);
+ /* you cannot change ThreadLimit across a restart; ignore
+ * any such attempts
+ */
+ if (first_thread_limit && tmp_thread_limit != thread_limit) {
+ /* how do we log a message? the error log is a bit bucket at this
+ * point; we'll just have to set a flag so that ap_mpm_run()
+ * logs a warning later
+ */
+ changed_limit_at_restart = 1;
+ return NULL;
+ }
+ thread_limit = tmp_thread_limit;
+
+ if (thread_limit > MAX_THREAD_LIMIT) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: ThreadLimit of %d exceeds compile time limit "
+ "of %d servers,", thread_limit, MAX_THREAD_LIMIT);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " lowering ThreadLimit to %d.", MAX_THREAD_LIMIT);
+ thread_limit = MAX_THREAD_LIMIT;
+ }
+ else if (thread_limit < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require ThreadLimit > 0, setting to 1");
+ thread_limit = 1;
+ }
+ return NULL;
+}
+
+static const command_rec event_cmds[] = {
+ UNIX_DAEMON_COMMANDS,
+ LISTEN_COMMANDS,
+ AP_INIT_TAKE1("StartServers", set_daemons_to_start, NULL, RSRC_CONF,
+ "Number of child processes launched at server startup"),
+ AP_INIT_TAKE1("ServerLimit", set_server_limit, NULL, RSRC_CONF,
+ "Maximum number of child processes for this run of Apache"),
+ AP_INIT_TAKE1("MinSpareThreads", set_min_spare_threads, NULL, RSRC_CONF,
+ "Minimum number of idle threads, to handle request spikes"),
+ AP_INIT_TAKE1("MaxSpareThreads", set_max_spare_threads, NULL, RSRC_CONF,
+ "Maximum number of idle threads"),
+ AP_INIT_TAKE1("MaxClients", set_max_clients, NULL, RSRC_CONF,
+ "Maximum number of threads alive at the same time"),
+ AP_INIT_TAKE1("ThreadsPerChild", set_threads_per_child, NULL, RSRC_CONF,
+ "Number of threads each child creates"),
+ AP_INIT_TAKE1("ThreadLimit", set_thread_limit, NULL, RSRC_CONF,
+ "Maximum number of worker threads per child process for this "
+ "run of Apache - Upper limit for ThreadsPerChild"),
+ AP_GRACEFUL_SHUTDOWN_TIMEOUT_COMMAND,
+ {NULL}
+};
+
+module AP_MODULE_DECLARE_DATA mpm_event_module = {
+ MPM20_MODULE_STUFF,
+ ap_mpm_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 */
+ event_cmds, /* command apr_table_t */
+ event_hooks /* register_hooks */
+};
diff --git a/server/mpm/experimental/event/fdqueue.c b/server/mpm/experimental/event/fdqueue.c
new file mode 100644
index 00000000..51952ced
--- /dev/null
+++ b/server/mpm/experimental/event/fdqueue.c
@@ -0,0 +1,420 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "fdqueue.h"
+#include "apr_atomic.h"
+
+typedef struct recycled_pool
+{
+ apr_pool_t *pool;
+ struct recycled_pool *next;
+} recycled_pool;
+
+struct fd_queue_info_t
+{
+ apr_int32_t idlers; /**
+ * 0 or positive: number of idle worker threads
+ * negative: number of threads blocked waiting
+ * for an idle worker
+ */
+ apr_thread_mutex_t *idlers_mutex;
+ apr_thread_cond_t *wait_for_idler;
+ int terminated;
+ int max_idlers;
+ recycled_pool *recycled_pools;
+};
+
+static apr_status_t queue_info_cleanup(void *data_)
+{
+ fd_queue_info_t *qi = data_;
+ apr_thread_cond_destroy(qi->wait_for_idler);
+ apr_thread_mutex_destroy(qi->idlers_mutex);
+
+ /* Clean up any pools in the recycled list */
+ for (;;) {
+ struct recycled_pool *first_pool = qi->recycled_pools;
+ if (first_pool == NULL) {
+ break;
+ }
+ if (apr_atomic_casptr
+ ((volatile void **) &(qi->recycled_pools), first_pool->next,
+ first_pool) == first_pool) {
+ apr_pool_destroy(first_pool->pool);
+ }
+ }
+
+ return APR_SUCCESS;
+}
+
+apr_status_t ap_queue_info_create(fd_queue_info_t ** queue_info,
+ apr_pool_t * pool, int max_idlers)
+{
+ apr_status_t rv;
+ fd_queue_info_t *qi;
+
+ qi = apr_palloc(pool, sizeof(*qi));
+ memset(qi, 0, sizeof(*qi));
+
+ rv = apr_thread_mutex_create(&qi->idlers_mutex, APR_THREAD_MUTEX_DEFAULT,
+ pool);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ rv = apr_thread_cond_create(&qi->wait_for_idler, pool);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ qi->recycled_pools = NULL;
+ qi->max_idlers = max_idlers;
+ apr_pool_cleanup_register(pool, qi, queue_info_cleanup,
+ apr_pool_cleanup_null);
+
+ *queue_info = qi;
+
+ return APR_SUCCESS;
+}
+
+apr_status_t ap_queue_info_set_idle(fd_queue_info_t * queue_info,
+ apr_pool_t * pool_to_recycle)
+{
+ apr_status_t rv;
+ int prev_idlers;
+
+ ap_push_pool(queue_info, pool_to_recycle);
+
+ /* Atomically increment the count of idle workers */
+ prev_idlers = apr_atomic_inc32(&(queue_info->idlers));
+
+ /* If other threads are waiting on a worker, wake one up */
+ if (prev_idlers < 0) {
+ rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
+ if (rv != APR_SUCCESS) {
+ AP_DEBUG_ASSERT(0);
+ return rv;
+ }
+ rv = apr_thread_cond_signal(queue_info->wait_for_idler);
+ if (rv != APR_SUCCESS) {
+ apr_thread_mutex_unlock(queue_info->idlers_mutex);
+ return rv;
+ }
+ rv = apr_thread_mutex_unlock(queue_info->idlers_mutex);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ }
+
+ return APR_SUCCESS;
+}
+
+apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t * queue_info)
+{
+ apr_status_t rv;
+ int prev_idlers;
+
+ /* Atomically decrement the idle worker count, saving the old value */
+ prev_idlers = apr_atomic_add32(&(queue_info->idlers), -1);
+
+ /* Block if there weren't any idle workers */
+ if (prev_idlers <= 0) {
+ rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
+ if (rv != APR_SUCCESS) {
+ AP_DEBUG_ASSERT(0);
+ apr_atomic_inc32(&(queue_info->idlers)); /* back out dec */
+ return rv;
+ }
+ /* Re-check the idle worker count to guard against a
+ * race condition. Now that we're in the mutex-protected
+ * region, one of two things may have happened:
+ * - If the idle worker count is still negative, the
+ * workers are all still busy, so it's safe to
+ * block on a condition variable.
+ * - If the idle worker count is non-negative, then a
+ * worker has become idle since the first check
+ * of queue_info->idlers above. It's possible
+ * that the worker has also signaled the condition
+ * variable--and if so, the listener missed it
+ * because it wasn't yet blocked on the condition
+ * variable. But if the idle worker count is
+ * now non-negative, it's safe for this function to
+ * return immediately.
+ *
+ * A negative value in queue_info->idlers tells how many
+ * threads are waiting on an idle worker.
+ */
+ if (queue_info->idlers < 0) {
+ rv = apr_thread_cond_wait(queue_info->wait_for_idler,
+ queue_info->idlers_mutex);
+ if (rv != APR_SUCCESS) {
+ apr_status_t rv2;
+ AP_DEBUG_ASSERT(0);
+ rv2 = apr_thread_mutex_unlock(queue_info->idlers_mutex);
+ if (rv2 != APR_SUCCESS) {
+ return rv2;
+ }
+ return rv;
+ }
+ }
+ rv = apr_thread_mutex_unlock(queue_info->idlers_mutex);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ }
+
+ if (queue_info->terminated) {
+ return APR_EOF;
+ }
+ else {
+ return APR_SUCCESS;
+ }
+}
+
+
+void ap_push_pool(fd_queue_info_t * queue_info,
+ apr_pool_t * pool_to_recycle)
+{
+ /* If we have been given a pool to recycle, atomically link
+ * it into the queue_info's list of recycled pools
+ */
+ if (pool_to_recycle) {
+ struct recycled_pool *new_recycle;
+ new_recycle = (struct recycled_pool *) apr_palloc(pool_to_recycle,
+ sizeof
+ (*new_recycle));
+ new_recycle->pool = pool_to_recycle;
+ for (;;) {
+ new_recycle->next = queue_info->recycled_pools;
+ if (apr_atomic_casptr
+ ((volatile void **) &(queue_info->recycled_pools),
+ new_recycle, new_recycle->next) == new_recycle->next) {
+ break;
+ }
+ }
+ }
+}
+
+void ap_pop_pool(apr_pool_t ** recycled_pool, fd_queue_info_t * queue_info)
+{
+ /* Atomically pop a pool from the recycled list */
+
+ /* This function is safe only as long as it is single threaded because
+ * it reaches into the queue and accesses "next" which can change.
+ * We are OK today because it is only called from the listener thread.
+ * cas-based pushes do not have the same limitation - any number can
+ * happen concurrently with a single cas-based pop.
+ */
+
+ *recycled_pool = NULL;
+
+
+ /* Atomically pop a pool from the recycled list */
+ for (;;) {
+ struct recycled_pool *first_pool = queue_info->recycled_pools;
+ if (first_pool == NULL) {
+ break;
+ }
+ if (apr_atomic_casptr
+ ((volatile void **) &(queue_info->recycled_pools),
+ first_pool->next, first_pool) == first_pool) {
+ *recycled_pool = first_pool->pool;
+ break;
+ }
+ }
+}
+
+apr_status_t ap_queue_info_term(fd_queue_info_t * queue_info)
+{
+ apr_status_t rv;
+ rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ queue_info->terminated = 1;
+ apr_thread_cond_broadcast(queue_info->wait_for_idler);
+ return apr_thread_mutex_unlock(queue_info->idlers_mutex);
+}
+
+/**
+ * Detects when the fd_queue_t is full. This utility function is expected
+ * to be called from within critical sections, and is not threadsafe.
+ */
+#define ap_queue_full(queue) ((queue)->nelts == (queue)->bounds)
+
+/**
+ * Detects when the fd_queue_t is empty. This utility function is expected
+ * to be called from within critical sections, and is not threadsafe.
+ */
+#define ap_queue_empty(queue) ((queue)->nelts == 0)
+
+/**
+ * Callback routine that is called to destroy this
+ * fd_queue_t when its pool is destroyed.
+ */
+static apr_status_t ap_queue_destroy(void *data)
+{
+ fd_queue_t *queue = data;
+
+ /* Ignore errors here, we can't do anything about them anyway.
+ * XXX: We should at least try to signal an error here, it is
+ * indicative of a programmer error. -aaron */
+ apr_thread_cond_destroy(queue->not_empty);
+ apr_thread_mutex_destroy(queue->one_big_mutex);
+
+ return APR_SUCCESS;
+}
+
+/**
+ * Initialize the fd_queue_t.
+ */
+apr_status_t ap_queue_init(fd_queue_t * queue, int queue_capacity,
+ apr_pool_t * a)
+{
+ int i;
+ apr_status_t rv;
+
+ if ((rv = apr_thread_mutex_create(&queue->one_big_mutex,
+ APR_THREAD_MUTEX_DEFAULT,
+ a)) != APR_SUCCESS) {
+ return rv;
+ }
+ if ((rv = apr_thread_cond_create(&queue->not_empty, a)) != APR_SUCCESS) {
+ return rv;
+ }
+
+ queue->data = apr_palloc(a, queue_capacity * sizeof(fd_queue_elem_t));
+ queue->bounds = queue_capacity;
+ queue->nelts = 0;
+
+ /* Set all the sockets in the queue to NULL */
+ for (i = 0; i < queue_capacity; ++i)
+ queue->data[i].sd = NULL;
+
+ apr_pool_cleanup_register(a, queue, ap_queue_destroy,
+ apr_pool_cleanup_null);
+
+ return APR_SUCCESS;
+}
+
+/**
+ * Push a new socket onto the queue.
+ *
+ * precondition: ap_queue_info_wait_for_idler has already been called
+ * to reserve an idle worker thread
+ */
+apr_status_t ap_queue_push(fd_queue_t * queue, apr_socket_t * sd,
+ conn_state_t * cs, apr_pool_t * p)
+{
+ fd_queue_elem_t *elem;
+ apr_status_t rv;
+
+ if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
+ return rv;
+ }
+
+ AP_DEBUG_ASSERT(!queue->terminated);
+ AP_DEBUG_ASSERT(!ap_queue_full(queue));
+
+ elem = &queue->data[queue->nelts];
+ elem->sd = sd;
+ elem->cs = cs;
+ elem->p = p;
+ queue->nelts++;
+
+ apr_thread_cond_signal(queue->not_empty);
+
+ if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
+ return rv;
+ }
+
+ return APR_SUCCESS;
+}
+
+/**
+ * Retrieves the next available socket from the queue. If there are no
+ * sockets available, it will block until one becomes available.
+ * Once retrieved, the socket is placed into the address specified by
+ * 'sd'.
+ */
+apr_status_t ap_queue_pop(fd_queue_t * queue, apr_socket_t ** sd,
+ conn_state_t ** cs, apr_pool_t ** p)
+{
+ fd_queue_elem_t *elem;
+ apr_status_t rv;
+
+ if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
+ return rv;
+ }
+
+ /* Keep waiting until we wake up and find that the queue is not empty. */
+ if (ap_queue_empty(queue)) {
+ if (!queue->terminated) {
+ apr_thread_cond_wait(queue->not_empty, queue->one_big_mutex);
+ }
+ /* If we wake up and it's still empty, then we were interrupted */
+ if (ap_queue_empty(queue)) {
+ rv = apr_thread_mutex_unlock(queue->one_big_mutex);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ if (queue->terminated) {
+ return APR_EOF; /* no more elements ever again */
+ }
+ else {
+ return APR_EINTR;
+ }
+ }
+ }
+
+ elem = &queue->data[--queue->nelts];
+ *sd = elem->sd;
+ *cs = elem->cs;
+ *p = elem->p;
+#ifdef AP_DEBUG
+ elem->sd = NULL;
+ elem->p = NULL;
+#endif /* AP_DEBUG */
+
+ rv = apr_thread_mutex_unlock(queue->one_big_mutex);
+ return rv;
+}
+
+apr_status_t ap_queue_interrupt_all(fd_queue_t * queue)
+{
+ apr_status_t rv;
+
+ if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
+ return rv;
+ }
+ apr_thread_cond_broadcast(queue->not_empty);
+ return apr_thread_mutex_unlock(queue->one_big_mutex);
+}
+
+apr_status_t ap_queue_term(fd_queue_t * queue)
+{
+ apr_status_t rv;
+
+ if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
+ return rv;
+ }
+ /* we must hold one_big_mutex when setting this... otherwise,
+ * we could end up setting it and waking everybody up just after a
+ * would-be popper checks it but right before they block
+ */
+ queue->terminated = 1;
+ if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
+ return rv;
+ }
+ return ap_queue_interrupt_all(queue);
+}
diff --git a/server/mpm/experimental/event/fdqueue.h b/server/mpm/experimental/event/fdqueue.h
new file mode 100644
index 00000000..bd18adfd
--- /dev/null
+++ b/server/mpm/experimental/event/fdqueue.h
@@ -0,0 +1,82 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file event/fdqueue.h
+ * @brief fd queue declarations
+ *
+ * @addtogroup APACHE_MPM_EVENT
+ * @{
+ */
+
+#ifndef FDQUEUE_H
+#define FDQUEUE_H
+#include "httpd.h"
+#include <stdlib.h>
+#if APR_HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <apr_thread_mutex.h>
+#include <apr_thread_cond.h>
+#include <sys/types.h>
+#if APR_HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+#include <apr_errno.h>
+
+typedef struct fd_queue_info_t fd_queue_info_t;
+
+apr_status_t ap_queue_info_create(fd_queue_info_t ** queue_info,
+ apr_pool_t * pool, int max_idlers);
+apr_status_t ap_queue_info_set_idle(fd_queue_info_t * queue_info,
+ apr_pool_t * pool_to_recycle);
+apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t * queue_info);
+apr_status_t ap_queue_info_term(fd_queue_info_t * queue_info);
+
+struct fd_queue_elem_t
+{
+ apr_socket_t *sd;
+ apr_pool_t *p;
+ conn_state_t *cs;
+};
+typedef struct fd_queue_elem_t fd_queue_elem_t;
+
+struct fd_queue_t
+{
+ fd_queue_elem_t *data;
+ int nelts;
+ int bounds;
+ apr_thread_mutex_t *one_big_mutex;
+ apr_thread_cond_t *not_empty;
+ int terminated;
+};
+typedef struct fd_queue_t fd_queue_t;
+
+void ap_pop_pool(apr_pool_t ** recycled_pool, fd_queue_info_t * queue_info);
+void ap_push_pool(fd_queue_info_t * queue_info,
+ apr_pool_t * pool_to_recycle);
+
+apr_status_t ap_queue_init(fd_queue_t * queue, int queue_capacity,
+ apr_pool_t * a);
+apr_status_t ap_queue_push(fd_queue_t * queue, apr_socket_t * sd,
+ conn_state_t * cs, apr_pool_t * p);
+apr_status_t ap_queue_pop(fd_queue_t * queue, apr_socket_t ** sd,
+ conn_state_t ** cs, apr_pool_t ** p);
+apr_status_t ap_queue_interrupt_all(fd_queue_t * queue);
+apr_status_t ap_queue_term(fd_queue_t * queue);
+
+#endif /* FDQUEUE_H */
+/** @} */
diff --git a/server/mpm/experimental/event/mpm.h b/server/mpm/experimental/event/mpm.h
new file mode 100644
index 00000000..cf4e61d9
--- /dev/null
+++ b/server/mpm/experimental/event/mpm.h
@@ -0,0 +1,62 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file event/mpm.h
+ * @brief Unix Exent driven MPM
+ *
+ * @defgroup APACHE_MPM_EVENT Unix Event MPM
+ * @ingroup APACHE_OS_UNIX APACHE_MPM
+ * @{
+ */
+
+#include "scoreboard.h"
+#include "unixd.h"
+
+#ifndef APACHE_MPM_EVENT_H
+#define APACHE_MPM_EVENT_H
+
+#define EVENT_MPM
+
+#define MPM_NAME "Event"
+
+#define AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
+#define AP_MPM_WANT_WAIT_OR_TIMEOUT
+#define AP_MPM_WANT_PROCESS_CHILD_STATUS
+#define AP_MPM_WANT_SET_PIDFILE
+#define AP_MPM_WANT_SET_SCOREBOARD
+#define AP_MPM_WANT_SET_LOCKFILE
+#define AP_MPM_WANT_SET_MAX_REQUESTS
+#define AP_MPM_WANT_SET_COREDUMPDIR
+#define AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
+#define AP_MPM_WANT_SIGNAL_SERVER
+#define AP_MPM_WANT_SET_MAX_MEM_FREE
+#define AP_MPM_WANT_SET_STACKSIZE
+#define AP_MPM_WANT_SET_GRACEFUL_SHUTDOWN
+#define AP_MPM_WANT_FATAL_SIGNAL_HANDLER
+#define AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
+
+#define MPM_CHILD_PID(i) (ap_scoreboard_image->parent[i].pid)
+#define MPM_NOTE_CHILD_KILLED(i) (MPM_CHILD_PID(i) = 0)
+#define MPM_ACCEPT_FUNC unixd_accept
+
+extern int ap_threads_per_child;
+extern int ap_max_daemons_limit;
+extern server_rec *ap_server_conf;
+extern char ap_coredump_dir[MAX_STRING_LEN];
+
+#endif /* APACHE_MPM_EVENT_H */
+/** @} */
diff --git a/server/mpm/experimental/event/mpm_default.h b/server/mpm/experimental/event/mpm_default.h
new file mode 100644
index 00000000..c26ee714
--- /dev/null
+++ b/server/mpm/experimental/event/mpm_default.h
@@ -0,0 +1,79 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+/**
+ * @file event/mpm_default.h
+ * @brief Event MPM defaults
+ *
+ * @addtogroup APACHE_MPM_EVENT
+ * @{
+ */
+
+#ifndef APACHE_MPM_DEFAULT_H
+#define APACHE_MPM_DEFAULT_H
+
+/* Number of servers to spawn off by default --- also, if fewer than
+ * this free when the caretaker checks, it will spawn more.
+ */
+#ifndef DEFAULT_START_DAEMON
+#define DEFAULT_START_DAEMON 3
+#endif
+
+/* Maximum number of *free* server processes --- more than this, and
+ * they will die off.
+ */
+
+#ifndef DEFAULT_MAX_FREE_DAEMON
+#define DEFAULT_MAX_FREE_DAEMON 10
+#endif
+
+/* Minimum --- fewer than this, and more will be created */
+
+#ifndef DEFAULT_MIN_FREE_DAEMON
+#define DEFAULT_MIN_FREE_DAEMON 3
+#endif
+
+#ifndef DEFAULT_THREADS_PER_CHILD
+#define DEFAULT_THREADS_PER_CHILD 25
+#endif
+
+/* File used for accept locking, when we use a file */
+#ifndef DEFAULT_LOCKFILE
+#define DEFAULT_LOCKFILE DEFAULT_REL_RUNTIMEDIR "/accept.lock"
+#endif
+
+/* Where the main/parent process's pid is logged */
+#ifndef DEFAULT_PIDLOG
+#define DEFAULT_PIDLOG DEFAULT_REL_RUNTIMEDIR "/httpd.pid"
+#endif
+
+/*
+ * Interval, in microseconds, between scoreboard maintenance.
+ */
+#ifndef SCOREBOARD_MAINTENANCE_INTERVAL
+#define SCOREBOARD_MAINTENANCE_INTERVAL 1000000
+#endif
+
+/* Number of requests to try to handle in a single process. If <= 0,
+ * the children don't die off.
+ */
+#ifndef DEFAULT_MAX_REQUESTS_PER_CHILD
+#define DEFAULT_MAX_REQUESTS_PER_CHILD 10000
+#endif
+
+#endif /* AP_MPM_DEFAULT_H */
+/** @} */
diff --git a/server/mpm/experimental/event/pod.c b/server/mpm/experimental/event/pod.c
new file mode 100644
index 00000000..5a9999f7
--- /dev/null
+++ b/server/mpm/experimental/event/pod.c
@@ -0,0 +1,109 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "pod.h"
+
+#if APR_HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t * p, ap_pod_t ** pod)
+{
+ apr_status_t rv;
+
+ *pod = apr_palloc(p, sizeof(**pod));
+ rv = apr_file_pipe_create(&((*pod)->pod_in), &((*pod)->pod_out), p);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+/*
+ apr_file_pipe_timeout_set((*pod)->pod_in, 0);
+*/
+ (*pod)->p = p;
+
+ /* close these before exec. */
+ apr_file_inherit_unset((*pod)->pod_in);
+ apr_file_inherit_unset((*pod)->pod_out);
+
+ return APR_SUCCESS;
+}
+
+AP_DECLARE(int) ap_mpm_pod_check(ap_pod_t * pod)
+{
+ char c;
+ apr_os_file_t fd;
+ int rc;
+
+ /* we need to surface EINTR so we'll have to grab the
+ * native file descriptor and do the OS read() ourselves
+ */
+ apr_os_file_get(&fd, pod->pod_in);
+ rc = read(fd, &c, 1);
+ if (rc == 1) {
+ switch (c) {
+ case RESTART_CHAR:
+ return AP_RESTART;
+ case GRACEFUL_CHAR:
+ return AP_GRACEFUL;
+ }
+ }
+ return AP_NORESTART;
+}
+
+AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t * pod)
+{
+ apr_status_t rv;
+
+ rv = apr_file_close(pod->pod_out);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+
+ rv = apr_file_close(pod->pod_in);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ return rv;
+}
+
+static apr_status_t pod_signal_internal(ap_pod_t * pod, int graceful)
+{
+ apr_status_t rv;
+ char char_of_death = graceful ? GRACEFUL_CHAR : RESTART_CHAR;
+ apr_size_t one = 1;
+
+ rv = apr_file_write(pod->pod_out, &char_of_death, &one);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
+ "write pipe_of_death");
+ }
+ return rv;
+}
+
+AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t * pod, int graceful)
+{
+ return pod_signal_internal(pod, graceful);
+}
+
+AP_DECLARE(void) ap_mpm_pod_killpg(ap_pod_t * pod, int num, int graceful)
+{
+ int i;
+ apr_status_t rv = APR_SUCCESS;
+
+ for (i = 0; i < num && rv == APR_SUCCESS; i++) {
+ rv = pod_signal_internal(pod, graceful);
+ }
+}
diff --git a/server/mpm/experimental/event/pod.h b/server/mpm/experimental/event/pod.h
new file mode 100644
index 00000000..78e2b9d7
--- /dev/null
+++ b/server/mpm/experimental/event/pod.h
@@ -0,0 +1,60 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file event/pod.h
+ * @brief pod definitions
+ *
+ * @addtogroup APACHE_MPM_EVENT
+ * @{
+ */
+
+#include "apr.h"
+#include "apr_strings.h"
+#define APR_WANT_STRFUNC
+#include "apr_want.h"
+
+#include "httpd.h"
+#include "http_config.h"
+#include "http_log.h"
+#include "http_main.h"
+#include "mpm.h"
+#include "mpm_common.h"
+#include "ap_mpm.h"
+#include "ap_listen.h"
+#include "mpm_default.h"
+
+#define RESTART_CHAR '$'
+#define GRACEFUL_CHAR '!'
+
+#define AP_RESTART 0
+#define AP_GRACEFUL 1
+
+typedef struct ap_pod_t ap_pod_t;
+
+struct ap_pod_t
+{
+ apr_file_t *pod_in;
+ apr_file_t *pod_out;
+ apr_pool_t *p;
+};
+
+AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t * p, ap_pod_t ** pod);
+AP_DECLARE(int) ap_mpm_pod_check(ap_pod_t * pod);
+AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t * pod);
+AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t * pod, int graceful);
+AP_DECLARE(void) ap_mpm_pod_killpg(ap_pod_t * pod, int num, int graceful);
+/** @} */
diff --git a/server/mpm/mpmt_os2/Makefile.in b/server/mpm/mpmt_os2/Makefile.in
new file mode 100644
index 00000000..38e598ed
--- /dev/null
+++ b/server/mpm/mpmt_os2/Makefile.in
@@ -0,0 +1,5 @@
+
+LTLIBRARY_NAME = libmpmt_os2.la
+LTLIBRARY_SOURCES = mpmt_os2.c mpmt_os2_child.c
+
+include $(top_srcdir)/build/ltlib.mk
diff --git a/server/mpm/mpmt_os2/config5.m4 b/server/mpm/mpmt_os2/config5.m4
new file mode 100644
index 00000000..b27c296d
--- /dev/null
+++ b/server/mpm/mpmt_os2/config5.m4
@@ -0,0 +1,5 @@
+if test "$MPM_NAME" = "mpmt_os2" ; then
+ AC_CACHE_SAVE
+ APACHE_FAST_OUTPUT(server/mpm/$MPM_NAME/Makefile)
+ APR_ADDTO(CFLAGS,-Zmt)
+fi
diff --git a/server/mpm/mpmt_os2/mpm.h b/server/mpm/mpmt_os2/mpm.h
new file mode 100644
index 00000000..f8bbc30e
--- /dev/null
+++ b/server/mpm/mpmt_os2/mpm.h
@@ -0,0 +1,43 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file mpmt_os2/mpm.h
+ * @brief MPM for os2
+ *
+ * @defgroup APACHE_MPM_OS2 os2 MPM
+ * @ingroup APACHE_OS_OS2 APACHE_MPM
+ */
+
+#ifndef APACHE_MPM_MPMT_OS2_H
+#define APACHE_MPM_MPMT_OS2_H
+
+#define MPMT_OS2_MPM
+
+#include "httpd.h"
+#include "mpm_default.h"
+#include "scoreboard.h"
+
+#define MPM_NAME "MPMT_OS2"
+
+extern server_rec *ap_server_conf;
+#define AP_MPM_WANT_SET_PIDFILE
+#define AP_MPM_WANT_SET_MAX_REQUESTS
+#define AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
+#define AP_MPM_WANT_SET_MAX_MEM_FREE
+
+#endif /* APACHE_MPM_MPMT_OS2_H */
+/** @} */
diff --git a/server/mpm/mpmt_os2/mpm_default.h b/server/mpm/mpmt_os2/mpm_default.h
new file mode 100644
index 00000000..2a21cd50
--- /dev/null
+++ b/server/mpm/mpmt_os2/mpm_default.h
@@ -0,0 +1,68 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file mpmt_os2/mpm_default.h
+ * @brief os2 MPM defaults
+ *
+ * @addtogroup APACHE_MPM_OS2
+ * @{
+ */
+
+#ifndef APACHE_MPM_DEFAULT_H
+#define APACHE_MPM_DEFAULT_H
+
+/* Number of servers processes to spawn off by default
+ */
+#ifndef DEFAULT_START_DAEMON
+#define DEFAULT_START_DAEMON 2
+#endif
+
+/* Maximum number of *free* server threads --- more than this, and
+ * they will die off.
+ */
+
+#ifndef DEFAULT_MAX_SPARE_THREAD
+#define DEFAULT_MAX_SPARE_THREAD 10
+#endif
+
+/* Minimum --- fewer than this, and more will be created */
+
+#ifndef DEFAULT_MIN_SPARE_THREAD
+#define DEFAULT_MIN_SPARE_THREAD 5
+#endif
+
+/* Where the main/parent process's pid is logged */
+#ifndef DEFAULT_PIDLOG
+#define DEFAULT_PIDLOG DEFAULT_REL_RUNTIMEDIR "/httpd.pid"
+#endif
+
+/*
+ * Interval, in microseconds, between scoreboard maintenance.
+ */
+#ifndef SCOREBOARD_MAINTENANCE_INTERVAL
+#define SCOREBOARD_MAINTENANCE_INTERVAL 1000000
+#endif
+
+/* Number of requests to try to handle in a single process. If <= 0,
+ * the children don't die off.
+ */
+#ifndef DEFAULT_MAX_REQUESTS_PER_CHILD
+#define DEFAULT_MAX_REQUESTS_PER_CHILD 10000
+#endif
+
+#endif /* AP_MPM_DEFAULT_H */
+/** @} */
diff --git a/server/mpm/mpmt_os2/mpmt_os2.c b/server/mpm/mpmt_os2/mpmt_os2.c
new file mode 100644
index 00000000..8fb10ce9
--- /dev/null
+++ b/server/mpm/mpmt_os2/mpmt_os2.c
@@ -0,0 +1,577 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* Multi-process, multi-threaded MPM for OS/2
+ *
+ * Server consists of
+ * - a main, parent process
+ * - a small, static number of child processes
+ *
+ * The parent process's job is to manage the child processes. This involves
+ * spawning children as required to ensure there are always ap_daemons_to_start
+ * processes accepting connections.
+ *
+ * Each child process consists of a a pool of worker threads and a
+ * main thread that accepts connections & passes them to the workers via
+ * a work queue. The worker thread pool is dynamic, managed by a maintanence
+ * thread so that the number of idle threads is kept between
+ * min_spare_threads & max_spare_threads.
+ *
+ */
+
+/*
+ Todo list
+ - Enforce MaxClients somehow
+*/
+#define CORE_PRIVATE
+#define INCL_NOPMAPI
+#define INCL_DOS
+#define INCL_DOSERRORS
+
+#include "ap_config.h"
+#include "httpd.h"
+#include "mpm_default.h"
+#include "http_main.h"
+#include "http_log.h"
+#include "http_config.h"
+#include "http_core.h" /* for get_remote_host */
+#include "http_connection.h"
+#include "mpm.h"
+#include "ap_mpm.h"
+#include "ap_listen.h"
+#include "apr_portable.h"
+#include "mpm_common.h"
+#include "apr_strings.h"
+#include <os2.h>
+#include <process.h>
+
+/* We don't need many processes,
+ * they're only for redundancy in the event of a crash
+ */
+#define HARD_SERVER_LIMIT 10
+
+/* Limit on the total number of threads per process
+ */
+#ifndef HARD_THREAD_LIMIT
+#define HARD_THREAD_LIMIT 256
+#endif
+
+server_rec *ap_server_conf;
+static apr_pool_t *pconf = NULL; /* Pool for config stuff */
+static const char *ap_pid_fname=NULL;
+
+/* Config globals */
+static int one_process = 0;
+static int ap_daemons_to_start = 0;
+static int ap_thread_limit = 0;
+static int ap_max_requests_per_child = 0;
+int ap_min_spare_threads = 0;
+int ap_max_spare_threads = 0;
+
+/* Keep track of a few interesting statistics */
+int ap_max_daemons_limit = -1;
+
+/* volatile just in case */
+static int volatile shutdown_pending;
+static int volatile restart_pending;
+static int volatile is_graceful = 0;
+ap_generation_t volatile ap_my_generation=0; /* Used by the scoreboard */
+static int is_parent_process=TRUE;
+HMTX ap_mpm_accept_mutex = 0;
+
+/* An array of these is stored in a shared memory area for passing
+ * sockets from the parent to child processes
+ */
+typedef struct {
+ struct sockaddr_in name;
+ apr_os_sock_t listen_fd;
+} listen_socket_t;
+
+typedef struct {
+ HMTX accept_mutex;
+ listen_socket_t listeners[1];
+} parent_info_t;
+
+static char master_main();
+static void spawn_child(int slot);
+void ap_mpm_child_main(apr_pool_t *pconf);
+static void set_signals();
+
+
+int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s )
+{
+ char *listener_shm_name;
+ parent_info_t *parent_info;
+ ULONG rc;
+ pconf = _pconf;
+ ap_server_conf = s;
+ restart_pending = 0;
+
+ DosSetMaxFH(ap_thread_limit * 2);
+ listener_shm_name = apr_psprintf(pconf, "/sharemem/httpd/parent_info.%d", getppid());
+ rc = DosGetNamedSharedMem((PPVOID)&parent_info, listener_shm_name, PAG_READ);
+ is_parent_process = rc != 0;
+ ap_scoreboard_fname = apr_psprintf(pconf, "/sharemem/httpd/scoreboard.%d", is_parent_process ? getpid() : getppid());
+
+ if (rc == 0) {
+ /* Child process */
+ ap_listen_rec *lr;
+ int num_listeners = 0;
+
+ ap_mpm_accept_mutex = parent_info->accept_mutex;
+
+ /* Set up a default listener if necessary */
+ if (ap_listeners == NULL) {
+ ap_listen_rec *lr = apr_pcalloc(s->process->pool, sizeof(ap_listen_rec));
+ ap_listeners = lr;
+ apr_sockaddr_info_get(&lr->bind_addr, "0.0.0.0", APR_UNSPEC,
+ DEFAULT_HTTP_PORT, 0, s->process->pool);
+ apr_socket_create(&lr->sd, lr->bind_addr->family,
+ SOCK_STREAM, 0, s->process->pool);
+ }
+
+ for (lr = ap_listeners; lr; lr = lr->next) {
+ apr_sockaddr_t *sa;
+ apr_os_sock_put(&lr->sd, &parent_info->listeners[num_listeners].listen_fd, pconf);
+ apr_socket_addr_get(&sa, APR_LOCAL, lr->sd);
+ num_listeners++;
+ }
+
+ DosFreeMem(parent_info);
+
+ /* Do the work */
+ ap_mpm_child_main(pconf);
+
+ /* Outta here */
+ return 1;
+ }
+ else {
+ /* Parent process */
+ char restart;
+ is_parent_process = TRUE;
+
+ if (ap_setup_listeners(ap_server_conf) < 1) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, 0, s,
+ "no listening sockets available, shutting down");
+ return 1;
+ }
+
+ ap_log_pid(pconf, ap_pid_fname);
+
+ restart = master_main();
+ ++ap_my_generation;
+ ap_scoreboard_image->global->running_generation = ap_my_generation;
+
+ if (!restart) {
+ const char *pidfile = ap_server_root_relative(pconf, ap_pid_fname);
+
+ if (pidfile != NULL && remove(pidfile) == 0) {
+ ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS,
+ ap_server_conf, "removed PID file %s (pid=%d)",
+ pidfile, getpid());
+ }
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "caught SIGTERM, shutting down");
+ return 1;
+ }
+ } /* Parent process */
+
+ return 0; /* Restart */
+}
+
+
+
+/* Main processing of the parent process
+ * returns TRUE if restarting
+ */
+static char master_main()
+{
+ server_rec *s = ap_server_conf;
+ ap_listen_rec *lr;
+ parent_info_t *parent_info;
+ char *listener_shm_name;
+ int listener_num, num_listeners, slot;
+ ULONG rc;
+
+ printf("%s \n", ap_get_server_version());
+ set_signals();
+
+ if (ap_setup_listeners(ap_server_conf) < 1) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, 0, s,
+ "no listening sockets available, shutting down");
+ return FALSE;
+ }
+
+ /* Allocate a shared memory block for the array of listeners */
+ for (num_listeners = 0, lr = ap_listeners; lr; lr = lr->next) {
+ num_listeners++;
+ }
+
+ listener_shm_name = apr_psprintf(pconf, "/sharemem/httpd/parent_info.%d", getpid());
+ rc = DosAllocSharedMem((PPVOID)&parent_info, listener_shm_name,
+ sizeof(parent_info_t) + num_listeners * sizeof(listen_socket_t),
+ PAG_READ|PAG_WRITE|PAG_COMMIT);
+
+ if (rc) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, APR_FROM_OS_ERROR(rc), s,
+ "failure allocating shared memory, shutting down");
+ return FALSE;
+ }
+
+ /* Store the listener sockets in the shared memory area for our children to see */
+ for (listener_num = 0, lr = ap_listeners; lr; lr = lr->next, listener_num++) {
+ apr_os_sock_get(&parent_info->listeners[listener_num].listen_fd, lr->sd);
+ }
+
+ /* Create mutex to prevent multiple child processes from detecting
+ * a connection with apr_poll()
+ */
+
+ rc = DosCreateMutexSem(NULL, &ap_mpm_accept_mutex, DC_SEM_SHARED, FALSE);
+
+ if (rc) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, APR_FROM_OS_ERROR(rc), s,
+ "failure creating accept mutex, shutting down");
+ return FALSE;
+ }
+
+ parent_info->accept_mutex = ap_mpm_accept_mutex;
+
+ /* Allocate shared memory for scoreboard */
+ if (ap_scoreboard_image == NULL) {
+ void *sb_mem;
+ rc = DosAllocSharedMem(&sb_mem, ap_scoreboard_fname,
+ ap_calc_scoreboard_size(),
+ PAG_COMMIT|PAG_READ|PAG_WRITE);
+
+ if (rc) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
+ "unable to allocate shared memory for scoreboard , exiting");
+ return FALSE;
+ }
+
+ ap_init_scoreboard(sb_mem);
+ }
+
+ ap_scoreboard_image->global->restart_time = apr_time_now();
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "%s configured -- resuming normal operations",
+ ap_get_server_version());
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
+ "Server built: %s", ap_get_server_built());
+#ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "AcceptMutex: %s (default: %s)",
+ apr_proc_mutex_name(accept_mutex),
+ apr_proc_mutex_defname());
+#endif
+ if (one_process) {
+ ap_scoreboard_image->parent[0].pid = getpid();
+ ap_mpm_child_main(pconf);
+ return FALSE;
+ }
+
+ while (!restart_pending && !shutdown_pending) {
+ RESULTCODES proc_rc;
+ PID child_pid;
+ int active_children = 0;
+
+ /* Count number of active children */
+ for (slot=0; slot < HARD_SERVER_LIMIT; slot++) {
+ active_children += ap_scoreboard_image->parent[slot].pid != 0 &&
+ !ap_scoreboard_image->parent[slot].quiescing;
+ }
+
+ /* Spawn children if needed */
+ for (slot=0; slot < HARD_SERVER_LIMIT && active_children < ap_daemons_to_start; slot++) {
+ if (ap_scoreboard_image->parent[slot].pid == 0) {
+ spawn_child(slot);
+ active_children++;
+ }
+ }
+
+ rc = DosWaitChild(DCWA_PROCESSTREE, DCWW_NOWAIT, &proc_rc, &child_pid, 0);
+
+ if (rc == 0) {
+ /* A child has terminated, remove its scoreboard entry & terminate if necessary */
+ for (slot=0; ap_scoreboard_image->parent[slot].pid != child_pid && slot < HARD_SERVER_LIMIT; slot++);
+
+ if (slot < HARD_SERVER_LIMIT) {
+ ap_scoreboard_image->parent[slot].pid = 0;
+ ap_scoreboard_image->parent[slot].quiescing = 0;
+
+ if (proc_rc.codeTerminate == TC_EXIT) {
+ /* Child terminated normally, check its exit code and
+ * terminate server if child indicates a fatal error
+ */
+ if (proc_rc.codeResult == APEXIT_CHILDFATAL)
+ break;
+ }
+ }
+ } else if (rc == ERROR_CHILD_NOT_COMPLETE) {
+ /* No child exited, lets sleep for a while.... */
+ apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
+ }
+ }
+
+ /* Signal children to shut down, either gracefully or immediately */
+ for (slot=0; slot<HARD_SERVER_LIMIT; slot++) {
+ kill(ap_scoreboard_image->parent[slot].pid, is_graceful ? SIGHUP : SIGTERM);
+ }
+
+ DosFreeMem(parent_info);
+ return restart_pending;
+}
+
+
+
+static void spawn_child(int slot)
+{
+ PPIB ppib;
+ PTIB ptib;
+ char fail_module[100];
+ char progname[CCHMAXPATH];
+ RESULTCODES proc_rc;
+ ULONG rc;
+
+ ap_scoreboard_image->parent[slot].generation = ap_my_generation;
+ DosGetInfoBlocks(&ptib, &ppib);
+ DosQueryModuleName(ppib->pib_hmte, sizeof(progname), progname);
+ rc = DosExecPgm(fail_module, sizeof(fail_module), EXEC_ASYNCRESULT,
+ ppib->pib_pchcmd, NULL, &proc_rc, progname);
+
+ if (rc) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
+ "error spawning child, slot %d", slot);
+ }
+
+ if (ap_max_daemons_limit < slot) {
+ ap_max_daemons_limit = slot;
+ }
+
+ ap_scoreboard_image->parent[slot].pid = proc_rc.codeTerminate;
+}
+
+
+
+/* Signal handling routines */
+
+static void sig_term(int sig)
+{
+ shutdown_pending = 1;
+ signal(SIGTERM, SIG_DFL);
+}
+
+
+
+static void sig_restart(int sig)
+{
+ if (sig == SIGUSR1) {
+ is_graceful = 1;
+ }
+
+ restart_pending = 1;
+}
+
+
+
+static void set_signals()
+{
+ struct sigaction sa;
+
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ sa.sa_handler = sig_term;
+
+ if (sigaction(SIGTERM, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
+
+ if (sigaction(SIGINT, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");
+
+ sa.sa_handler = sig_restart;
+
+ if (sigaction(SIGHUP, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");
+ if (sigaction(SIGUSR1, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGUSR1)");
+}
+
+
+
+/* Enquiry functions used get MPM status info */
+
+AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
+{
+ switch (query_code) {
+ case AP_MPMQ_MAX_DAEMON_USED:
+ *result = ap_max_daemons_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_THREADED:
+ *result = AP_MPMQ_DYNAMIC;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_FORKED:
+ *result = AP_MPMQ_NOT_SUPPORTED;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_DAEMONS:
+ *result = HARD_SERVER_LIMIT;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_THREADS:
+ *result = HARD_THREAD_LIMIT;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_REQUESTS_DAEMON:
+ *result = ap_max_requests_per_child;
+ return APR_SUCCESS;
+ }
+ return APR_ENOTIMPL;
+}
+
+
+
+int ap_graceful_stop_signalled(void)
+{
+ return is_graceful;
+}
+
+
+
+/* Configuration handling stuff */
+
+static int mpmt_os2_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
+{
+ one_process = ap_exists_config_define("ONE_PROCESS") ||
+ ap_exists_config_define("DEBUG");
+ is_graceful = 0;
+ ap_listen_pre_config();
+ ap_daemons_to_start = DEFAULT_START_DAEMON;
+ ap_thread_limit = HARD_THREAD_LIMIT;
+ ap_pid_fname = DEFAULT_PIDLOG;
+ ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
+ ap_extended_status = 0;
+ ap_min_spare_threads = DEFAULT_MIN_SPARE_THREAD;
+ ap_max_spare_threads = DEFAULT_MAX_SPARE_THREAD;
+#ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
+ ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
+#endif
+
+ return OK;
+}
+
+
+
+static void mpmt_os2_hooks(apr_pool_t *p)
+{
+ ap_hook_pre_config(mpmt_os2_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
+}
+
+
+
+static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_daemons_to_start = atoi(arg);
+ return NULL;
+}
+
+
+
+static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy,
+ const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_min_spare_threads = atoi(arg);
+
+ if (ap_min_spare_threads <= 0) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: detected MinSpareThreads set to non-positive.");
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Resetting to 1 to avoid almost certain Apache failure.");
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Please read the documentation.");
+ ap_min_spare_threads = 1;
+ }
+
+ return NULL;
+}
+
+
+
+static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy,
+ const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_max_spare_threads = atoi(arg);
+ return NULL;
+}
+
+
+
+static const char *ignore_cmd(cmd_parms *cmd, void *dummy, const char *arg)
+{
+ return NULL;
+}
+
+
+
+static const command_rec mpmt_os2_cmds[] = {
+LISTEN_COMMANDS,
+AP_INIT_TAKE1( "StartServers", set_daemons_to_start, NULL, RSRC_CONF,
+ "Number of child processes launched at server startup" ),
+AP_INIT_TAKE1("MinSpareThreads", set_min_spare_threads, NULL, RSRC_CONF,
+ "Minimum number of idle children, to handle request spikes"),
+AP_INIT_TAKE1("MaxSpareThreads", set_max_spare_threads, NULL, RSRC_CONF,
+ "Maximum number of idle children"),
+AP_INIT_TAKE1("User", ignore_cmd, NULL, RSRC_CONF,
+ "Not applicable on this platform"),
+AP_INIT_TAKE1("Group", ignore_cmd, NULL, RSRC_CONF,
+ "Not applicable on this platform"),
+AP_INIT_TAKE1("ScoreBoardFile", ignore_cmd, NULL, RSRC_CONF, \
+ "Not applicable on this platform"),
+{ NULL }
+};
+
+module AP_MODULE_DECLARE_DATA mpm_mpmt_os2_module = {
+ MPM20_MODULE_STUFF,
+ NULL, /* 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 */
+ mpmt_os2_cmds, /* command apr_table_t */
+ mpmt_os2_hooks, /* register_hooks */
+};
diff --git a/server/mpm/mpmt_os2/mpmt_os2_child.c b/server/mpm/mpmt_os2/mpmt_os2_child.c
new file mode 100644
index 00000000..bc313315
--- /dev/null
+++ b/server/mpm/mpmt_os2/mpmt_os2_child.c
@@ -0,0 +1,480 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define CORE_PRIVATE
+#define INCL_NOPMAPI
+#define INCL_DOS
+#define INCL_DOSERRORS
+
+#include "ap_config.h"
+#include "httpd.h"
+#include "mpm_default.h"
+#include "http_main.h"
+#include "http_log.h"
+#include "http_config.h"
+#include "http_core.h" /* for get_remote_host */
+#include "http_connection.h"
+#include "mpm.h"
+#include "ap_mpm.h"
+#include "ap_listen.h"
+#include "apr_portable.h"
+#include "apr_poll.h"
+#include "mpm_common.h"
+#include "apr_strings.h"
+#include <os2.h>
+#include <process.h>
+
+/* XXXXXX move these to header file private to this MPM */
+
+/* We don't need many processes,
+ * they're only for redundancy in the event of a crash
+ */
+#define HARD_SERVER_LIMIT 10
+
+/* Limit on the total number of threads per process
+ */
+#ifndef HARD_THREAD_LIMIT
+#define HARD_THREAD_LIMIT 256
+#endif
+
+#define ID_FROM_CHILD_THREAD(c, t) ((c * HARD_THREAD_LIMIT) + t)
+
+typedef struct {
+ apr_pool_t *pconn;
+ apr_socket_t *conn_sd;
+} worker_args_t;
+
+#define WORKTYPE_CONN 0
+#define WORKTYPE_EXIT 1
+
+static apr_pool_t *pchild = NULL;
+static int child_slot;
+static int shutdown_pending = 0;
+extern int ap_my_generation;
+static int volatile is_graceful = 1;
+HEV shutdown_event; /* signaled when this child is shutting down */
+
+/* grab some MPM globals */
+extern int ap_min_spare_threads;
+extern int ap_max_spare_threads;
+extern HMTX ap_mpm_accept_mutex;
+
+static void worker_main(void *vpArg);
+static void clean_child_exit(int code);
+static void set_signals();
+static void server_maintenance(void *vpArg);
+
+
+static void clean_child_exit(int code)
+{
+ if (pchild) {
+ apr_pool_destroy(pchild);
+ }
+
+ exit(code);
+}
+
+
+
+void ap_mpm_child_main(apr_pool_t *pconf)
+{
+ ap_listen_rec *lr = NULL;
+ int requests_this_child = 0;
+ int rv = 0;
+ unsigned long ulTimes;
+ int my_pid = getpid();
+ ULONG rc, c;
+ HQUEUE workq;
+ apr_pollset_t *pollset;
+ int num_listeners;
+ TID server_maint_tid;
+ void *sb_mem;
+
+ /* Stop Ctrl-C/Ctrl-Break signals going to child processes */
+ DosSetSignalExceptionFocus(0, &ulTimes);
+ set_signals();
+
+ /* Create pool for child */
+ apr_pool_create(&pchild, pconf);
+
+ ap_run_child_init(pchild, ap_server_conf);
+
+ /* Create an event semaphore used to trigger other threads to shutdown */
+ rc = DosCreateEventSem(NULL, &shutdown_event, 0, FALSE);
+
+ if (rc) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
+ "unable to create shutdown semaphore, exiting");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ /* Gain access to the scoreboard. */
+ rc = DosGetNamedSharedMem(&sb_mem, ap_scoreboard_fname,
+ PAG_READ|PAG_WRITE);
+
+ if (rc) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
+ "scoreboard not readable in child, exiting");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ ap_calc_scoreboard_size();
+ ap_init_scoreboard(sb_mem);
+
+ /* Gain access to the accpet mutex */
+ rc = DosOpenMutexSem(NULL, &ap_mpm_accept_mutex);
+
+ if (rc) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
+ "accept mutex couldn't be accessed in child, exiting");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ /* Find our pid in the scoreboard so we know what slot our parent allocated us */
+ for (child_slot = 0; ap_scoreboard_image->parent[child_slot].pid != my_pid && child_slot < HARD_SERVER_LIMIT; child_slot++);
+
+ if (child_slot == HARD_SERVER_LIMIT) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
+ "child pid not found in scoreboard, exiting");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ ap_my_generation = ap_scoreboard_image->parent[child_slot].generation;
+ memset(ap_scoreboard_image->servers[child_slot], 0, sizeof(worker_score) * HARD_THREAD_LIMIT);
+
+ /* Set up an OS/2 queue for passing connections & termination requests
+ * to worker threads
+ */
+ rc = DosCreateQueue(&workq, QUE_FIFO, apr_psprintf(pchild, "/queues/httpd/work.%d", my_pid));
+
+ if (rc) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
+ "unable to create work queue, exiting");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ /* Create initial pool of worker threads */
+ for (c = 0; c < ap_min_spare_threads; c++) {
+// ap_scoreboard_image->servers[child_slot][c].tid = _beginthread(worker_main, NULL, 128*1024, (void *)c);
+ }
+
+ /* Start maintenance thread */
+ server_maint_tid = _beginthread(server_maintenance, NULL, 32768, NULL);
+
+ /* Set up poll */
+ for (num_listeners = 0, lr = ap_listeners; lr; lr = lr->next) {
+ num_listeners++;
+ }
+
+ apr_pollset_create(&pollset, num_listeners, pchild, 0);
+
+ for (lr = ap_listeners; lr != NULL; lr = lr->next) {
+ apr_pollfd_t pfd = { 0 };
+
+ pfd.desc_type = APR_POLL_SOCKET;
+ pfd.desc.s = lr->sd;
+ pfd.reqevents = APR_POLLIN;
+ pfd.client_data = lr;
+ apr_pollset_add(pollset, &pfd);
+ }
+
+ /* Main connection accept loop */
+ do {
+ apr_pool_t *pconn;
+ worker_args_t *worker_args;
+ int last_poll_idx = 0;
+
+ apr_pool_create(&pconn, pchild);
+ worker_args = apr_palloc(pconn, sizeof(worker_args_t));
+ worker_args->pconn = pconn;
+
+ if (num_listeners == 1) {
+ rv = apr_socket_accept(&worker_args->conn_sd, ap_listeners->sd, pconn);
+ } else {
+ const apr_pollfd_t *poll_results;
+ apr_int32_t num_poll_results;
+
+ rc = DosRequestMutexSem(ap_mpm_accept_mutex, SEM_INDEFINITE_WAIT);
+
+ if (shutdown_pending) {
+ DosReleaseMutexSem(ap_mpm_accept_mutex);
+ break;
+ }
+
+ rv = APR_FROM_OS_ERROR(rc);
+
+ if (rv == APR_SUCCESS) {
+ rv = apr_pollset_poll(pollset, -1, &num_poll_results, &poll_results);
+ DosReleaseMutexSem(ap_mpm_accept_mutex);
+ }
+
+ if (rv == APR_SUCCESS) {
+ if (last_poll_idx >= num_listeners) {
+ last_poll_idx = 0;
+ }
+
+ lr = poll_results[last_poll_idx++].client_data;
+ rv = apr_socket_accept(&worker_args->conn_sd, lr->sd, pconn);
+ last_poll_idx++;
+ }
+ }
+
+ if (rv != APR_SUCCESS) {
+ if (!APR_STATUS_IS_EINTR(rv)) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf,
+ "apr_socket_accept");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+ } else {
+ DosWriteQueue(workq, WORKTYPE_CONN, sizeof(worker_args_t), worker_args, 0);
+ requests_this_child++;
+ }
+
+ if (ap_max_requests_per_child != 0 && requests_this_child >= ap_max_requests_per_child)
+ break;
+ } while (!shutdown_pending && ap_my_generation == ap_scoreboard_image->global->running_generation);
+
+ ap_scoreboard_image->parent[child_slot].quiescing = 1;
+ DosPostEventSem(shutdown_event);
+ DosWaitThread(&server_maint_tid, DCWW_WAIT);
+
+ if (is_graceful) {
+ char someleft;
+
+ /* tell our worker threads to exit */
+ for (c=0; c<HARD_THREAD_LIMIT; c++) {
+ if (ap_scoreboard_image->servers[child_slot][c].status != SERVER_DEAD) {
+ DosWriteQueue(workq, WORKTYPE_EXIT, 0, NULL, 0);
+ }
+ }
+
+ do {
+ someleft = 0;
+
+ for (c=0; c<HARD_THREAD_LIMIT; c++) {
+ if (ap_scoreboard_image->servers[child_slot][c].status != SERVER_DEAD) {
+ someleft = 1;
+ DosSleep(1000);
+ break;
+ }
+ }
+ } while (someleft);
+ } else {
+ DosPurgeQueue(workq);
+
+ for (c=0; c<HARD_THREAD_LIMIT; c++) {
+ if (ap_scoreboard_image->servers[child_slot][c].status != SERVER_DEAD) {
+ DosKillThread(ap_scoreboard_image->servers[child_slot][c].tid);
+ }
+ }
+ }
+
+ apr_pool_destroy(pchild);
+}
+
+
+
+void add_worker()
+{
+ int thread_slot;
+
+ /* Find a free thread slot */
+ for (thread_slot=0; thread_slot < HARD_THREAD_LIMIT; thread_slot++) {
+ if (ap_scoreboard_image->servers[child_slot][thread_slot].status == SERVER_DEAD) {
+ ap_scoreboard_image->servers[child_slot][thread_slot].status = SERVER_STARTING;
+ ap_scoreboard_image->servers[child_slot][thread_slot].tid =
+ _beginthread(worker_main, NULL, 128*1024, (void *)thread_slot);
+ break;
+ }
+ }
+}
+
+
+
+ULONG APIENTRY thread_exception_handler(EXCEPTIONREPORTRECORD *pReportRec,
+ EXCEPTIONREGISTRATIONRECORD *pRegRec,
+ CONTEXTRECORD *pContext,
+ PVOID p)
+{
+ int c;
+
+ if (pReportRec->fHandlerFlags & EH_NESTED_CALL) {
+ return XCPT_CONTINUE_SEARCH;
+ }
+
+ if (pReportRec->ExceptionNum == XCPT_ACCESS_VIOLATION ||
+ pReportRec->ExceptionNum == XCPT_INTEGER_DIVIDE_BY_ZERO) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
+ "caught exception in worker thread, initiating child shutdown pid=%d", getpid());
+ for (c=0; c<HARD_THREAD_LIMIT; c++) {
+ if (ap_scoreboard_image->servers[child_slot][c].tid == _gettid()) {
+ ap_scoreboard_image->servers[child_slot][c].status = SERVER_DEAD;
+ break;
+ }
+ }
+
+ /* Shut down process ASAP, it could be quite unhealthy & leaking resources */
+ shutdown_pending = 1;
+ ap_scoreboard_image->parent[child_slot].quiescing = 1;
+ kill(getpid(), SIGHUP);
+ DosUnwindException(UNWIND_ALL, 0, 0);
+ }
+
+ return XCPT_CONTINUE_SEARCH;
+}
+
+
+
+static void worker_main(void *vpArg)
+{
+ long conn_id;
+ conn_rec *current_conn;
+ apr_pool_t *pconn;
+ apr_allocator_t *allocator;
+ apr_bucket_alloc_t *bucket_alloc;
+ worker_args_t *worker_args;
+ HQUEUE workq;
+ PID owner;
+ int rc;
+ REQUESTDATA rd;
+ ULONG len;
+ BYTE priority;
+ int thread_slot = (int)vpArg;
+ EXCEPTIONREGISTRATIONRECORD reg_rec = { NULL, thread_exception_handler };
+ ap_sb_handle_t *sbh;
+
+ /* Trap exceptions in this thread so we don't take down the whole process */
+ DosSetExceptionHandler( &reg_rec );
+
+ rc = DosOpenQueue(&owner, &workq,
+ apr_psprintf(pchild, "/queues/httpd/work.%d", getpid()));
+
+ if (rc) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
+ "unable to open work queue, exiting");
+ ap_scoreboard_image->servers[child_slot][thread_slot].tid = 0;
+ }
+
+ conn_id = ID_FROM_CHILD_THREAD(child_slot, thread_slot);
+ ap_update_child_status_from_indexes(child_slot, thread_slot, SERVER_READY,
+ NULL);
+
+ apr_allocator_create(&allocator);
+ apr_allocator_max_free_set(allocator, ap_max_mem_free);
+ bucket_alloc = apr_bucket_alloc_create_ex(allocator);
+
+ while (rc = DosReadQueue(workq, &rd, &len, (PPVOID)&worker_args, 0, DCWW_WAIT, &priority, NULLHANDLE),
+ rc == 0 && rd.ulData != WORKTYPE_EXIT) {
+ pconn = worker_args->pconn;
+ ap_create_sb_handle(&sbh, pconn, child_slot, thread_slot);
+ current_conn = ap_run_create_connection(pconn, ap_server_conf,
+ worker_args->conn_sd, conn_id,
+ sbh, bucket_alloc);
+
+ if (current_conn) {
+ ap_process_connection(current_conn, worker_args->conn_sd);
+ ap_lingering_close(current_conn);
+ }
+
+ apr_pool_destroy(pconn);
+ ap_update_child_status_from_indexes(child_slot, thread_slot,
+ SERVER_READY, NULL);
+ }
+
+ ap_update_child_status_from_indexes(child_slot, thread_slot, SERVER_DEAD,
+ NULL);
+
+ apr_bucket_alloc_destroy(bucket_alloc);
+ apr_allocator_destroy(allocator);
+}
+
+
+
+static void server_maintenance(void *vpArg)
+{
+ int num_idle, num_needed;
+ ULONG num_pending = 0;
+ int threadnum;
+ HQUEUE workq;
+ ULONG rc;
+ PID owner;
+
+ rc = DosOpenQueue(&owner, &workq,
+ apr_psprintf(pchild, "/queues/httpd/work.%d", getpid()));
+
+ if (rc) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
+ "unable to open work queue in maintenance thread");
+ return;
+ }
+
+ do {
+ for (num_idle=0, threadnum=0; threadnum < HARD_THREAD_LIMIT; threadnum++) {
+ num_idle += ap_scoreboard_image->servers[child_slot][threadnum].status == SERVER_READY;
+ }
+
+ DosQueryQueue(workq, &num_pending);
+ num_needed = ap_min_spare_threads - num_idle + num_pending;
+
+ if (num_needed > 0) {
+ for (threadnum=0; threadnum < num_needed; threadnum++) {
+ add_worker();
+ }
+ }
+
+ if (num_idle - num_pending > ap_max_spare_threads) {
+ DosWriteQueue(workq, WORKTYPE_EXIT, 0, NULL, 0);
+ }
+ } while (DosWaitEventSem(shutdown_event, 500) == ERROR_TIMEOUT);
+}
+
+
+
+/* Signal handling routines */
+
+static void sig_term(int sig)
+{
+ shutdown_pending = 1;
+ is_graceful = 0;
+ signal(SIGTERM, SIG_DFL);
+}
+
+
+
+static void sig_hup(int sig)
+{
+ shutdown_pending = 1;
+ is_graceful = 1;
+}
+
+
+
+static void set_signals()
+{
+ struct sigaction sa;
+
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ sa.sa_handler = sig_term;
+
+ if (sigaction(SIGTERM, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
+
+ sa.sa_handler = sig_hup;
+
+ if (sigaction(SIGHUP, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");
+}
diff --git a/server/mpm/netware/mpm.h b/server/mpm/netware/mpm.h
new file mode 100644
index 00000000..106d62a5
--- /dev/null
+++ b/server/mpm/netware/mpm.h
@@ -0,0 +1,58 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file netware/mpm.h
+ * @brief Netware MPM
+ *
+ * @defgroup APACHE_MPM_NETWARE Netware MPM
+ * @ingroup APACHE_OS_NETWARE APACHE_MPM
+ * @{
+ */
+
+#include "scoreboard.h"
+
+#ifndef APACHE_MPM_THREADED_H
+#define APACHE_MPM_THREADED_H
+
+#define THREADED_MPM
+
+#define MPM_NAME "NetWare_Threaded"
+
+/*#define AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
+ #define AP_MPM_WANT_WAIT_OR_TIMEOUT
+ #define AP_MPM_WANT_PROCESS_CHILD_STATUS
+ #define AP_MPM_WANT_SET_PIDFILE
+ #define AP_MPM_WANT_SET_SCOREBOARD
+ #define AP_MPM_WANT_SET_LOCKFILE
+*/
+#define AP_MPM_WANT_SET_MAX_REQUESTS
+#define AP_MPM_WANT_SET_MAX_MEM_FREE
+#define AP_MPM_WANT_SET_STACKSIZE
+#define AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
+/*#define AP_MPM_WANT_SET_COREDUMPDIR
+ #define AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
+*/
+
+#define MPM_CHILD_PID(i) (ap_scoreboard_image->parent[i].pid)
+#define MPM_NOTE_CHILD_KILLED(i) (MPM_CHILD_PID(i) = 0)
+
+extern int ap_threads_per_child;
+extern int ap_max_workers_limit;
+extern server_rec *ap_server_conf;
+
+#endif /* APACHE_MPM_THREADED_H */
+/** @} */
diff --git a/server/mpm/netware/mpm_default.h b/server/mpm/netware/mpm_default.h
new file mode 100644
index 00000000..96c5aa09
--- /dev/null
+++ b/server/mpm/netware/mpm_default.h
@@ -0,0 +1,122 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file netware/mpm_default.h
+ * @brief Defaults for Netware MPM
+ *
+ * @addtogroup APACHE_MPM_NETWARE
+ * @{
+ */
+#ifndef APACHE_MPM_DEFAULT_H
+#define APACHE_MPM_DEFAULT_H
+
+/* Number of servers to spawn off by default --- also, if fewer than
+ * this free when the caretaker checks, it will spawn more.
+ */
+#ifndef DEFAULT_START_DAEMON
+#define DEFAULT_START_DAEMON 1
+#endif
+
+/* Maximum number of *free* server processes --- more than this, and
+ * they will die off.
+ */
+
+#ifndef DEFAULT_MAX_FREE_DAEMON
+#define DEFAULT_MAX_FREE_DAEMON 1
+#endif
+
+/* Minimum --- fewer than this, and more will be created */
+
+#ifndef DEFAULT_MIN_FREE_DAEMON
+#define DEFAULT_MIN_FREE_DAEMON 1
+#endif
+
+/* Limit on the threads per process. Clients will be locked out if more than
+ * this * HARD_SERVER_LIMIT are needed.
+ *
+ * We keep this for one reason it keeps the size of the scoreboard file small
+ * enough that we can read the whole thing without worrying too much about
+ * the overhead.
+ */
+#ifndef HARD_THREAD_LIMIT
+#define HARD_THREAD_LIMIT 2048
+#endif
+
+#ifndef DEFAULT_THREADS_PER_CHILD
+#define DEFAULT_THREADS_PER_CHILD 50
+#endif
+
+/* Number of threads to spawn off by default --- also, if fewer than
+ * this free when the caretaker checks, it will spawn more.
+ */
+#ifndef DEFAULT_START_THREADS
+#define DEFAULT_START_THREADS DEFAULT_THREADS_PER_CHILD
+#endif
+
+/* Maximum number of *free* threads --- more than this, and
+ * they will die off.
+ */
+
+#ifndef DEFAULT_MAX_FREE_THREADS
+#define DEFAULT_MAX_FREE_THREADS 100
+#endif
+
+/* Minimum --- fewer than this, and more will be created */
+
+#ifndef DEFAULT_MIN_FREE_THREADS
+#define DEFAULT_MIN_FREE_THREADS 10
+#endif
+
+/* Check for definition of DEFAULT_REL_RUNTIMEDIR */
+#ifndef DEFAULT_REL_RUNTIMEDIR
+#define DEFAULT_REL_RUNTIMEDIR "logs"
+#endif
+
+/* File used for accept locking, when we use a file */
+/*#ifndef DEFAULT_LOCKFILE
+ #define DEFAULT_LOCKFILE DEFAULT_REL_RUNTIMEDIR "/accept.lock"
+ #endif
+*/
+
+/* Where the main/parent process's pid is logged */
+/*#ifndef DEFAULT_PIDLOG
+ #define DEFAULT_PIDLOG DEFAULT_REL_RUNTIMEDIR "/httpd.pid"
+ #endif
+*/
+
+/*
+ * Interval, in microseconds, between scoreboard maintenance.
+ */
+#ifndef SCOREBOARD_MAINTENANCE_INTERVAL
+#define SCOREBOARD_MAINTENANCE_INTERVAL 1000000
+#endif
+
+/* Number of requests to try to handle in a single process. If <= 0,
+ * the children don't die off.
+ */
+#ifndef DEFAULT_MAX_REQUESTS_PER_CHILD
+#define DEFAULT_MAX_REQUESTS_PER_CHILD 0
+#endif
+
+/* Default stack size allocated for each worker thread.
+ */
+#ifndef DEFAULT_THREAD_STACKSIZE
+#define DEFAULT_THREAD_STACKSIZE 65536
+#endif
+
+#endif /* AP_MPM_DEFAULT_H */
+/** @} */
diff --git a/server/mpm/netware/mpm_netware.c b/server/mpm/netware/mpm_netware.c
new file mode 100644
index 00000000..dd4cb359
--- /dev/null
+++ b/server/mpm/netware/mpm_netware.c
@@ -0,0 +1,1291 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * httpd.c: simple http daemon for answering WWW file requests
+ *
+ *
+ * 03-21-93 Rob McCool wrote original code (up to NCSA HTTPd 1.3)
+ *
+ * 03-06-95 blong
+ * changed server number for child-alone processes to 0 and changed name
+ * of processes
+ *
+ * 03-10-95 blong
+ * Added numerous speed hacks proposed by Robert S. Thau (rst@ai.mit.edu)
+ * including set group before fork, and call gettime before to fork
+ * to set up libraries.
+ *
+ * 04-14-95 rst / rh
+ * Brandon's code snarfed from NCSA 1.4, but tinkered to work with the
+ * Apache server, and also to have child processes do accept() directly.
+ *
+ * April-July '95 rst
+ * Extensive rework for Apache.
+ */
+
+#include "apr.h"
+#include "apr_portable.h"
+#include "apr_strings.h"
+#include "apr_thread_proc.h"
+#include "apr_signal.h"
+#include "apr_tables.h"
+#include "apr_getopt.h"
+#include "apr_thread_mutex.h"
+
+#define APR_WANT_STDIO
+#define APR_WANT_STRFUNC
+#include "apr_want.h"
+
+#if APR_HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#if APR_HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#ifndef USE_WINSOCK
+#include <sys/select.h>
+#endif
+
+#define CORE_PRIVATE
+
+#include "ap_config.h"
+#include "httpd.h"
+#include "mpm_default.h"
+#include "http_main.h"
+#include "http_log.h"
+#include "http_config.h"
+#include "http_core.h" /* for get_remote_host */
+#include "http_connection.h"
+#include "scoreboard.h"
+#include "ap_mpm.h"
+#include "mpm_common.h"
+#include "ap_listen.h"
+#include "ap_mmn.h"
+
+#ifdef HAVE_TIME_H
+#include <time.h>
+#endif
+
+#include <signal.h>
+
+#include <netware.h>
+#include <nks/netware.h>
+#include <library.h>
+#include <screen.h>
+
+/* Limit on the total --- clients will be locked out if more servers than
+ * this are needed. It is intended solely to keep the server from crashing
+ * when things get out of hand.
+ *
+ * We keep a hard maximum number of servers, for two reasons --- first off,
+ * in case something goes seriously wrong, we want to stop the fork bomb
+ * short of actually crashing the machine we're running on by filling some
+ * kernel table. Secondly, it keeps the size of the scoreboard file small
+ * enough that we can read the whole thing without worrying too much about
+ * the overhead.
+ */
+#ifndef HARD_SERVER_LIMIT
+#define HARD_SERVER_LIMIT 1
+#endif
+
+#define WORKER_DEAD SERVER_DEAD
+#define WORKER_STARTING SERVER_STARTING
+#define WORKER_READY SERVER_READY
+#define WORKER_IDLE_KILL SERVER_IDLE_KILL
+
+/* config globals */
+
+int ap_threads_per_child=0; /* Worker threads per child */
+static int ap_threads_to_start=0;
+static int ap_threads_min_free=0;
+static int ap_threads_max_free=0;
+static int ap_threads_limit=0;
+static int mpm_state = AP_MPMQ_STARTING;
+
+/*
+ * The max child slot ever assigned, preserved across restarts. Necessary
+ * to deal with MaxClients changes across SIGWINCH restarts. We use this
+ * value to optimize routines that have to scan the entire scoreboard.
+ */
+int ap_max_workers_limit = -1;
+server_rec *ap_server_conf;
+
+/* *Non*-shared http_main globals... */
+
+int hold_screen_on_exit = 0; /* Indicates whether the screen should be held open */
+
+static fd_set listenfds;
+static int listenmaxfd;
+
+static apr_pool_t *pconf; /* Pool for config stuff */
+static apr_pool_t *pmain; /* Pool for httpd child stuff */
+
+static pid_t ap_my_pid; /* it seems silly to call getpid all the time */
+static char *ap_my_addrspace = NULL;
+
+static int die_now = 0;
+
+/* Keep track of the number of worker threads currently active */
+static unsigned long worker_thread_count;
+static int request_count;
+
+/* Structure used to register/deregister a console handler with the OS */
+static int InstallConsoleHandler(void);
+static void RemoveConsoleHandler(void);
+static int CommandLineInterpreter(scr_t screenID, const char *commandLine);
+static CommandParser_t ConsoleHandler = {0, NULL, 0};
+#define HANDLEDCOMMAND 0
+#define NOTMYCOMMAND 1
+
+static int show_settings = 0;
+
+//#define DBINFO_ON
+//#define DBPRINT_ON
+#ifdef DBPRINT_ON
+#define DBPRINT0(s) printf(s)
+#define DBPRINT1(s,v1) printf(s,v1)
+#define DBPRINT2(s,v1,v2) printf(s,v1,v2)
+#else
+#define DBPRINT0(s)
+#define DBPRINT1(s,v1)
+#define DBPRINT2(s,v1,v2)
+#endif
+
+/* volatile just in case */
+static int volatile shutdown_pending;
+static int volatile restart_pending;
+static int volatile is_graceful;
+static int volatile wait_to_finish=1;
+ap_generation_t volatile ap_my_generation=0;
+
+/* a clean exit from a child with proper cleanup */
+static void clean_child_exit(int code, int worker_num, apr_pool_t *ptrans,
+ apr_bucket_alloc_t *bucket_alloc) __attribute__ ((noreturn));
+static void clean_child_exit(int code, int worker_num, apr_pool_t *ptrans,
+ apr_bucket_alloc_t *bucket_alloc)
+{
+ apr_bucket_alloc_destroy(bucket_alloc);
+ if (!shutdown_pending) {
+ apr_pool_destroy(ptrans);
+ }
+
+ atomic_dec (&worker_thread_count);
+ if (worker_num >=0)
+ ap_update_child_status_from_indexes(0, worker_num, WORKER_DEAD,
+ (request_rec *) NULL);
+ NXThreadExit((void*)&code);
+}
+
+AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
+{
+ switch(query_code){
+ case AP_MPMQ_MAX_DAEMON_USED:
+ *result = 1;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_THREADED:
+ *result = AP_MPMQ_DYNAMIC;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_FORKED:
+ *result = AP_MPMQ_NOT_SUPPORTED;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_DAEMONS:
+ *result = HARD_SERVER_LIMIT;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_THREADS:
+ *result = HARD_THREAD_LIMIT;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_THREADS:
+ *result = ap_threads_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_THREADS:
+ *result = ap_threads_min_free;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_THREADS:
+ *result = ap_threads_max_free;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_REQUESTS_DAEMON:
+ *result = ap_max_requests_per_child;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_DAEMONS:
+ *result = 1;
+ return APR_SUCCESS;
+ case AP_MPMQ_MPM_STATE:
+ *result = mpm_state;
+ return APR_SUCCESS;
+ }
+ return APR_ENOTIMPL;
+}
+
+
+/*****************************************************************
+ * Connection structures and accounting...
+ */
+
+static void mpm_term(void)
+{
+ RemoveConsoleHandler();
+ wait_to_finish = 0;
+ NXThreadYield();
+}
+
+static void sig_term(int sig)
+{
+ if (shutdown_pending == 1) {
+ /* Um, is this _probably_ not an error, if the user has
+ * tried to do a shutdown twice quickly, so we won't
+ * worry about reporting it.
+ */
+ return;
+ }
+ shutdown_pending = 1;
+
+ DBPRINT0 ("waiting for threads\n");
+ while (wait_to_finish) {
+ apr_thread_yield();
+ }
+ DBPRINT0 ("goodbye\n");
+}
+
+/* restart() is the signal handler for SIGHUP and SIGWINCH
+ * in the parent process, unless running in ONE_PROCESS mode
+ */
+static void restart(void)
+{
+ if (restart_pending == 1) {
+ /* Probably not an error - don't bother reporting it */
+ return;
+ }
+ restart_pending = 1;
+ is_graceful = 1;
+}
+
+static void set_signals(void)
+{
+ apr_signal(SIGTERM, sig_term);
+ apr_signal(SIGABRT, sig_term);
+}
+
+int nlmUnloadSignaled(int wait)
+{
+ shutdown_pending = 1;
+
+ if (wait) {
+ while (wait_to_finish) {
+ NXThreadYield();
+ }
+ }
+
+ return 0;
+}
+
+/*****************************************************************
+ * Child process main loop.
+ * The following vars are static to avoid getting clobbered by longjmp();
+ * they are really private to child_main.
+ */
+
+
+int ap_graceful_stop_signalled(void)
+{
+ /* not ever called anymore... */
+ return 0;
+}
+
+#define MAX_WB_RETRIES 3
+#ifdef DBINFO_ON
+static int would_block = 0;
+static int retry_success = 0;
+static int retry_fail = 0;
+static int avg_retries = 0;
+#endif
+
+/*static */
+void worker_main(void *arg)
+{
+ ap_listen_rec *lr, *first_lr, *last_lr = NULL;
+ apr_pool_t *ptrans;
+ apr_pool_t *pbucket;
+ apr_allocator_t *allocator;
+ apr_bucket_alloc_t *bucket_alloc;
+ conn_rec *current_conn;
+ apr_status_t stat = APR_EINIT;
+ ap_sb_handle_t *sbh;
+
+ int my_worker_num = (int)arg;
+ apr_socket_t *csd = NULL;
+ int requests_this_child = 0;
+ apr_socket_t *sd = NULL;
+ fd_set main_fds;
+
+ int sockdes;
+ int srv;
+ struct timeval tv;
+ int wouldblock_retry;
+
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+
+ apr_allocator_create(&allocator);
+ apr_allocator_max_free_set(allocator, ap_max_mem_free);
+
+ apr_pool_create_ex(&ptrans, pmain, NULL, allocator);
+ apr_allocator_owner_set(allocator, ptrans);
+ apr_pool_tag(ptrans, "transaction");
+
+ bucket_alloc = apr_bucket_alloc_create_ex(allocator);
+
+ atomic_inc (&worker_thread_count);
+
+ while (!die_now) {
+ /*
+ * (Re)initialize this child to a pre-connection state.
+ */
+ current_conn = NULL;
+ apr_pool_clear(ptrans);
+
+ if ((ap_max_requests_per_child > 0
+ && requests_this_child++ >= ap_max_requests_per_child)) {
+ DBPRINT1 ("\n**Thread slot %d is shutting down", my_worker_num);
+ clean_child_exit(0, my_worker_num, ptrans, bucket_alloc);
+ }
+
+ ap_update_child_status_from_indexes(0, my_worker_num, WORKER_READY,
+ (request_rec *) NULL);
+
+ /*
+ * Wait for an acceptable connection to arrive.
+ */
+
+ for (;;) {
+ if (shutdown_pending || restart_pending || (ap_scoreboard_image->servers[0][my_worker_num].status == WORKER_IDLE_KILL)) {
+ DBPRINT1 ("\nThread slot %d is shutting down\n", my_worker_num);
+ clean_child_exit(0, my_worker_num, ptrans, bucket_alloc);
+ }
+
+ /* Check the listen queue on all sockets for requests */
+ memcpy(&main_fds, &listenfds, sizeof(fd_set));
+ srv = select(listenmaxfd + 1, &main_fds, NULL, NULL, &tv);
+
+ if (srv <= 0) {
+ if (srv < 0) {
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "select() failed on listen socket");
+ apr_thread_yield();
+ }
+ continue;
+ }
+
+ /* remember the last_lr we searched last time around so that
+ we don't end up starving any particular listening socket */
+ if (last_lr == NULL) {
+ lr = ap_listeners;
+ }
+ else {
+ lr = last_lr->next;
+ if (!lr)
+ lr = ap_listeners;
+ }
+ first_lr = lr;
+ do {
+ apr_os_sock_get(&sockdes, lr->sd);
+ if (FD_ISSET(sockdes, &main_fds))
+ goto got_listener;
+ lr = lr->next;
+ if (!lr)
+ lr = ap_listeners;
+ } while (lr != first_lr);
+ /* if we get here, something unexpected happened. Go back
+ into the select state and try again.
+ */
+ continue;
+ got_listener:
+ last_lr = lr;
+ sd = lr->sd;
+
+ wouldblock_retry = MAX_WB_RETRIES;
+
+ while (wouldblock_retry) {
+ if ((stat = apr_socket_accept(&csd, sd, ptrans)) == APR_SUCCESS) {
+ break;
+ }
+ else {
+ /* if the error is a wouldblock then maybe we were too
+ quick try to pull the next request from the listen
+ queue. Try a few more times then return to our idle
+ listen state. */
+ if (!APR_STATUS_IS_EAGAIN(stat)) {
+ break;
+ }
+
+ if (wouldblock_retry--) {
+ apr_thread_yield();
+ }
+ }
+ }
+
+ /* If we got a new socket, set it to non-blocking mode and process
+ it. Otherwise handle the error. */
+ if (stat == APR_SUCCESS) {
+ apr_socket_opt_set(csd, APR_SO_NONBLOCK, 0);
+#ifdef DBINFO_ON
+ if (wouldblock_retry < MAX_WB_RETRIES) {
+ retry_success++;
+ avg_retries += (MAX_WB_RETRIES-wouldblock_retry);
+ }
+#endif
+ break; /* We have a socket ready for reading */
+ }
+ else {
+#ifdef DBINFO_ON
+ if (APR_STATUS_IS_EAGAIN(stat)) {
+ would_block++;
+ retry_fail++;
+ }
+ else if (
+#else
+ if (APR_STATUS_IS_EAGAIN(stat) ||
+#endif
+ APR_STATUS_IS_ECONNRESET(stat) ||
+ APR_STATUS_IS_ETIMEDOUT(stat) ||
+ APR_STATUS_IS_EHOSTUNREACH(stat) ||
+ APR_STATUS_IS_ENETUNREACH(stat)) {
+ ;
+ }
+#ifdef USE_WINSOCK
+ else if (APR_STATUS_IS_ENETDOWN(stat)) {
+ /*
+ * When the network layer has been shut down, there
+ * is not much use in simply exiting: the parent
+ * would simply re-create us (and we'd fail again).
+ * Use the CHILDFATAL code to tear the server down.
+ * @@@ Martin's idea for possible improvement:
+ * A different approach would be to define
+ * a new APEXIT_NETDOWN exit code, the reception
+ * of which would make the parent shutdown all
+ * children, then idle-loop until it detected that
+ * the network is up again, and restart the children.
+ * Ben Hyde noted that temporary ENETDOWN situations
+ * occur in mobile IP.
+ */
+ ap_log_error(APLOG_MARK, APLOG_EMERG, stat, ap_server_conf,
+ "apr_socket_accept: giving up.");
+ clean_child_exit(APEXIT_CHILDFATAL, my_worker_num, ptrans,
+ bucket_alloc);
+ }
+#endif
+ else {
+ ap_log_error(APLOG_MARK, APLOG_ERR, stat, ap_server_conf,
+ "apr_socket_accept: (client socket)");
+ clean_child_exit(1, my_worker_num, ptrans, bucket_alloc);
+ }
+ }
+ }
+
+ ap_create_sb_handle(&sbh, ptrans, 0, my_worker_num);
+ /*
+ * We now have a connection, so set it up with the appropriate
+ * socket options, file descriptors, and read/write buffers.
+ */
+ current_conn = ap_run_create_connection(ptrans, ap_server_conf, csd,
+ my_worker_num, sbh,
+ bucket_alloc);
+ if (current_conn) {
+ ap_process_connection(current_conn, csd);
+ ap_lingering_close(current_conn);
+ }
+ request_count++;
+ }
+ clean_child_exit(0, my_worker_num, ptrans, bucket_alloc);
+}
+
+
+static int make_child(server_rec *s, int slot)
+{
+ int tid;
+ int err=0;
+ NXContext_t ctx;
+
+ if (slot + 1 > ap_max_workers_limit) {
+ ap_max_workers_limit = slot + 1;
+ }
+
+ ap_update_child_status_from_indexes(0, slot, WORKER_STARTING,
+ (request_rec *) NULL);
+
+ if (ctx = NXContextAlloc((void (*)(void *)) worker_main, (void*)slot, NX_PRIO_MED, ap_thread_stacksize, NX_CTX_NORMAL, &err)) {
+ char threadName[32];
+
+ sprintf (threadName, "Apache_Worker %d", slot);
+ NXContextSetName(ctx, threadName);
+ err = NXThreadCreate(ctx, NX_THR_BIND_CONTEXT, &tid);
+ if (err) {
+ NXContextFree (ctx);
+ }
+ }
+
+ if (err) {
+ /* create thread didn't succeed. Fix the scoreboard or else
+ * it will say SERVER_STARTING forever and ever
+ */
+ ap_update_child_status_from_indexes(0, slot, WORKER_DEAD,
+ (request_rec *) NULL);
+
+ /* In case system resources are maxxed out, we don't want
+ Apache running away with the CPU trying to fork over and
+ over and over again. */
+ apr_thread_yield();
+
+ return -1;
+ }
+
+ ap_scoreboard_image->servers[0][slot].tid = tid;
+
+ return 0;
+}
+
+
+/* start up a bunch of worker threads */
+static void startup_workers(int number_to_start)
+{
+ int i;
+
+ for (i = 0; number_to_start && i < ap_threads_limit; ++i) {
+ if (ap_scoreboard_image->servers[0][i].status != WORKER_DEAD) {
+ continue;
+ }
+ if (make_child(ap_server_conf, i) < 0) {
+ break;
+ }
+ --number_to_start;
+ }
+}
+
+
+/*
+ * idle_spawn_rate is the number of children that will be spawned on the
+ * next maintenance cycle if there aren't enough idle servers. It is
+ * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
+ * without the need to spawn.
+ */
+static int idle_spawn_rate = 1;
+#ifndef MAX_SPAWN_RATE
+#define MAX_SPAWN_RATE (64)
+#endif
+static int hold_off_on_exponential_spawning;
+
+static void perform_idle_server_maintenance(apr_pool_t *p)
+{
+ int i;
+ int to_kill;
+ int idle_count;
+ worker_score *ws;
+ int free_length;
+ int free_slots[MAX_SPAWN_RATE];
+ int last_non_dead;
+ int total_non_dead;
+
+ /* initialize the free_list */
+ free_length = 0;
+
+ to_kill = -1;
+ idle_count = 0;
+ last_non_dead = -1;
+ total_non_dead = 0;
+
+ for (i = 0; i < ap_threads_limit; ++i) {
+ int status;
+
+ if (i >= ap_max_workers_limit && free_length == idle_spawn_rate)
+ break;
+ ws = &ap_scoreboard_image->servers[0][i];
+ status = ws->status;
+ if (status == WORKER_DEAD) {
+ /* try to keep children numbers as low as possible */
+ if (free_length < idle_spawn_rate) {
+ free_slots[free_length] = i;
+ ++free_length;
+ }
+ }
+ else if (status == WORKER_IDLE_KILL) {
+ /* If it is already marked to die, skip it */
+ continue;
+ }
+ else {
+ /* We consider a starting server as idle because we started it
+ * at least a cycle ago, and if it still hasn't finished starting
+ * then we're just going to swamp things worse by forking more.
+ * So we hopefully won't need to fork more if we count it.
+ * This depends on the ordering of SERVER_READY and SERVER_STARTING.
+ */
+ if (status <= WORKER_READY) {
+ ++ idle_count;
+ /* always kill the highest numbered child if we have to...
+ * no really well thought out reason ... other than observing
+ * the server behaviour under linux where lower numbered children
+ * tend to service more hits (and hence are more likely to have
+ * their data in cpu caches).
+ */
+ to_kill = i;
+ }
+
+ ++total_non_dead;
+ last_non_dead = i;
+ }
+ }
+ DBPRINT2("Total: %d Idle Count: %d \r", total_non_dead, idle_count);
+ ap_max_workers_limit = last_non_dead + 1;
+ if (idle_count > ap_threads_max_free) {
+ /* kill off one child... we use the pod because that'll cause it to
+ * shut down gracefully, in case it happened to pick up a request
+ * while we were counting
+ */
+ idle_spawn_rate = 1;
+ ap_update_child_status_from_indexes(0, last_non_dead, WORKER_IDLE_KILL,
+ (request_rec *) NULL);
+ DBPRINT1("\nKilling idle thread: %d\n", last_non_dead);
+ }
+ else if (idle_count < ap_threads_min_free) {
+ /* terminate the free list */
+ if (free_length == 0) {
+ /* only report this condition once */
+ static int reported = 0;
+
+ if (!reported) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
+ "server reached MaxClients setting, consider"
+ " raising the MaxClients setting");
+ reported = 1;
+ }
+ idle_spawn_rate = 1;
+ }
+ else {
+ if (idle_spawn_rate >= 8) {
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
+ "server seems busy, (you may need "
+ "to increase StartServers, or Min/MaxSpareServers), "
+ "spawning %d children, there are %d idle, and "
+ "%d total children", idle_spawn_rate,
+ idle_count, total_non_dead);
+ }
+ DBPRINT0("\n");
+ for (i = 0; i < free_length; ++i) {
+ DBPRINT1("Spawning additional thread slot: %d\n", free_slots[i]);
+ make_child(ap_server_conf, free_slots[i]);
+ }
+ /* the next time around we want to spawn twice as many if this
+ * wasn't good enough, but not if we've just done a graceful
+ */
+ if (hold_off_on_exponential_spawning) {
+ --hold_off_on_exponential_spawning;
+ }
+ else if (idle_spawn_rate < MAX_SPAWN_RATE) {
+ idle_spawn_rate *= 2;
+ }
+ }
+ }
+ else {
+ idle_spawn_rate = 1;
+ }
+}
+
+static void display_settings ()
+{
+ int status_array[SERVER_NUM_STATUS];
+ int i, status, total=0;
+ int reqs = request_count;
+#ifdef DBINFO_ON
+ int wblock = would_block;
+
+ would_block = 0;
+#endif
+
+ request_count = 0;
+
+ ClearScreen (getscreenhandle());
+ printf("%s \n", ap_get_server_version());
+
+ for (i=0;i<SERVER_NUM_STATUS;i++) {
+ status_array[i] = 0;
+ }
+
+ for (i = 0; i < ap_threads_limit; ++i) {
+ status = (ap_scoreboard_image->servers[0][i]).status;
+ status_array[status]++;
+ }
+
+ for (i=0;i<SERVER_NUM_STATUS;i++) {
+ switch(i)
+ {
+ case SERVER_DEAD:
+ printf ("Available:\t%d\n", status_array[i]);
+ break;
+ case SERVER_STARTING:
+ printf ("Starting:\t%d\n", status_array[i]);
+ break;
+ case SERVER_READY:
+ printf ("Ready:\t\t%d\n", status_array[i]);
+ break;
+ case SERVER_BUSY_READ:
+ printf ("Busy:\t\t%d\n", status_array[i]);
+ break;
+ case SERVER_BUSY_WRITE:
+ printf ("Busy Write:\t%d\n", status_array[i]);
+ break;
+ case SERVER_BUSY_KEEPALIVE:
+ printf ("Busy Keepalive:\t%d\n", status_array[i]);
+ break;
+ case SERVER_BUSY_LOG:
+ printf ("Busy Log:\t%d\n", status_array[i]);
+ break;
+ case SERVER_BUSY_DNS:
+ printf ("Busy DNS:\t%d\n", status_array[i]);
+ break;
+ case SERVER_CLOSING:
+ printf ("Closing:\t%d\n", status_array[i]);
+ break;
+ case SERVER_GRACEFUL:
+ printf ("Restart:\t%d\n", status_array[i]);
+ break;
+ case SERVER_IDLE_KILL:
+ printf ("Idle Kill:\t%d\n", status_array[i]);
+ break;
+ default:
+ printf ("Unknown Status:\t%d\n", status_array[i]);
+ break;
+ }
+ if (i != SERVER_DEAD)
+ total+=status_array[i];
+ }
+ printf ("Total Running:\t%d\tout of: \t%d\n", total, ap_threads_limit);
+ printf ("Requests per interval:\t%d\n", reqs);
+
+#ifdef DBINFO_ON
+ printf ("Would blocks:\t%d\n", wblock);
+ printf ("Successful retries:\t%d\n", retry_success);
+ printf ("Failed retries:\t%d\n", retry_fail);
+ printf ("Avg retries:\t%d\n", retry_success == 0 ? 0 : avg_retries / retry_success);
+#endif
+}
+
+static void show_server_data()
+{
+ ap_listen_rec *lr;
+ module **m;
+
+ printf("%s\n", ap_get_server_version());
+ if (ap_my_addrspace && (ap_my_addrspace[0] != 'O') && (ap_my_addrspace[1] != 'S'))
+ printf(" Running in address space %s\n", ap_my_addrspace);
+
+
+ /* Display listening ports */
+ printf(" Listening on port(s):");
+ lr = ap_listeners;
+ do {
+ printf(" %d", lr->bind_addr->port);
+ lr = lr->next;
+ } while(lr && lr != ap_listeners);
+
+ /* Display dynamic modules loaded */
+ printf("\n");
+ for (m = ap_loaded_modules; *m != NULL; m++) {
+ if (((module*)*m)->dynamic_load_handle) {
+ printf(" Loaded dynamic module %s\n", ((module*)*m)->name);
+ }
+ }
+}
+
+
+static int setup_listeners(server_rec *s)
+{
+ ap_listen_rec *lr;
+ int sockdes;
+
+ if (ap_setup_listeners(s) < 1 ) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, 0, s,
+ "no listening sockets available, shutting down");
+ return -1;
+ }
+
+ listenmaxfd = -1;
+ FD_ZERO(&listenfds);
+ for (lr = ap_listeners; lr; lr = lr->next) {
+ apr_os_sock_get(&sockdes, lr->sd);
+ FD_SET(sockdes, &listenfds);
+ if (sockdes > listenmaxfd) {
+ listenmaxfd = sockdes;
+ }
+ }
+ return 0;
+}
+
+static int shutdown_listeners()
+{
+ ap_listen_rec *lr;
+
+ for (lr = ap_listeners; lr; lr = lr->next) {
+ apr_socket_close(lr->sd);
+ }
+ ap_listeners = NULL;
+ return 0;
+}
+
+/*****************************************************************
+ * Executive routines.
+ */
+
+int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
+{
+ apr_status_t status=0;
+
+ pconf = _pconf;
+ ap_server_conf = s;
+
+ if (setup_listeners(s)) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, status, s,
+ "no listening sockets available, shutting down");
+ return -1;
+ }
+
+ restart_pending = shutdown_pending = 0;
+ worker_thread_count = 0;
+
+ if (!is_graceful) {
+ if (ap_run_pre_mpm(s->process->pool, SB_NOT_SHARED) != OK) {
+ return 1;
+ }
+ }
+
+ /* Only set slot 0 since that is all NetWare will ever have. */
+ ap_scoreboard_image->parent[0].pid = getpid();
+
+ set_signals();
+
+ apr_pool_create(&pmain, pconf);
+ ap_run_child_init(pmain, ap_server_conf);
+
+ if (ap_threads_max_free < ap_threads_min_free + 1) /* Don't thrash... */
+ ap_threads_max_free = ap_threads_min_free + 1;
+ request_count = 0;
+
+ startup_workers(ap_threads_to_start);
+
+ /* Allow the Apache screen to be closed normally on exit() only if it
+ has not been explicitly forced to close on exit(). (ie. the -E flag
+ was specified at startup) */
+ if (hold_screen_on_exit > 0) {
+ hold_screen_on_exit = 0;
+ }
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "%s configured -- resuming normal operations",
+ ap_get_server_version());
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
+ "Server built: %s", ap_get_server_built());
+#ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "AcceptMutex: %s (default: %s)",
+ apr_proc_mutex_name(accept_mutex),
+ apr_proc_mutex_defname());
+#endif
+ show_server_data();
+
+ mpm_state = AP_MPMQ_RUNNING;
+ while (!restart_pending && !shutdown_pending) {
+ perform_idle_server_maintenance(pconf);
+ if (show_settings)
+ display_settings();
+ apr_thread_yield();
+ apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
+ }
+ mpm_state = AP_MPMQ_STOPPING;
+
+
+ /* Shutdown the listen sockets so that we don't get stuck in a blocking call.
+ shutdown_listeners();*/
+
+ if (shutdown_pending) { /* Got an unload from the console */
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "caught SIGTERM, shutting down");
+
+ while (worker_thread_count > 0) {
+ printf ("\rShutdown pending. Waiting for %d thread(s) to terminate...",
+ worker_thread_count);
+ apr_thread_yield();
+ }
+
+ return 1;
+ }
+ else { /* the only other way out is a restart */
+ /* advance to the next generation */
+ /* XXX: we really need to make sure this new generation number isn't in
+ * use by any of the children.
+ */
+ ++ap_my_generation;
+ ap_scoreboard_image->global->running_generation = ap_my_generation;
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "Graceful restart requested, doing restart");
+
+ /* Wait for all of the threads to terminate before initiating the restart */
+ while (worker_thread_count > 0) {
+ printf ("\rRestart pending. Waiting for %d thread(s) to terminate...",
+ worker_thread_count);
+ apr_thread_yield();
+ }
+ printf ("\nRestarting...\n");
+ }
+
+ return 0;
+}
+
+static int netware_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
+{
+ int debug;
+ char *addrname = NULL;
+
+ mpm_state = AP_MPMQ_STARTING;
+
+ debug = ap_exists_config_define("DEBUG");
+
+ is_graceful = 0;
+ ap_my_pid = getpid();
+ addrname = getaddressspacename (NULL, NULL);
+ if (addrname) {
+ ap_my_addrspace = apr_pstrdup (p, addrname);
+ free (addrname);
+ }
+
+#ifndef USE_WINSOCK
+ /* The following call has been moved to the mod_nw_ssl pre-config handler */
+ ap_listen_pre_config();
+#endif
+
+ ap_threads_to_start = DEFAULT_START_THREADS;
+ ap_threads_min_free = DEFAULT_MIN_FREE_THREADS;
+ ap_threads_max_free = DEFAULT_MAX_FREE_THREADS;
+ ap_threads_limit = HARD_THREAD_LIMIT;
+ ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
+ ap_extended_status = 0;
+ ap_thread_stacksize = DEFAULT_THREAD_STACKSIZE;
+#ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
+ ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
+#endif
+
+ return OK;
+}
+
+static void netware_mpm_hooks(apr_pool_t *p)
+{
+ ap_hook_pre_config(netware_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
+}
+
+void netware_rewrite_args(process_rec *process)
+{
+ char *def_server_root;
+ char optbuf[3];
+ const char *opt_arg;
+ apr_getopt_t *opt;
+ apr_array_header_t *mpm_new_argv;
+
+
+ atexit (mpm_term);
+ InstallConsoleHandler();
+
+ /* Make sure to hold the Apache screen open if exit() is called */
+ hold_screen_on_exit = 1;
+
+ /* Rewrite process->argv[];
+ *
+ * add default -d serverroot from the path of this executable
+ *
+ * The end result will look like:
+ * The -d serverroot default from the running executable
+ */
+ if (process->argc > 0) {
+ char *s = apr_pstrdup (process->pconf, process->argv[0]);
+ if (s) {
+ int i, len = strlen(s);
+
+ for (i=len; i; i--) {
+ if (s[i] == '\\' || s[i] == '/') {
+ s[i] = '\0';
+ apr_filepath_merge(&def_server_root, NULL, s,
+ APR_FILEPATH_TRUENAME, process->pool);
+ break;
+ }
+ }
+ /* 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_array_make(process->pool, process->argc + 2,
+ sizeof(const char *));
+ *(const char **)apr_array_push(mpm_new_argv) = process->argv[0];
+ *(const char **)apr_array_push(mpm_new_argv) = "-d";
+ *(const char **)apr_array_push(mpm_new_argv) = def_server_root;
+
+ optbuf[0] = '-';
+ optbuf[2] = '\0';
+ apr_getopt_init(&opt, process->pool, process->argc, (char**) process->argv);
+ while (apr_getopt(opt, AP_SERVER_BASEARGS"n:", optbuf + 1, &opt_arg) == APR_SUCCESS) {
+ switch (optbuf[1]) {
+ case 'n':
+ if (opt_arg) {
+ renamescreen(opt_arg);
+ }
+ break;
+ case 'E':
+ /* Don't need to hold the screen open if the output is going to a file */
+ hold_screen_on_exit = -1;
+ default:
+ *(const char **)apr_array_push(mpm_new_argv) =
+ apr_pstrdup(process->pool, optbuf);
+
+ if (opt_arg) {
+ *(const char **)apr_array_push(mpm_new_argv) = opt_arg;
+ }
+ break;
+ }
+ }
+ process->argc = mpm_new_argv->nelts;
+ process->argv = (const char * const *) mpm_new_argv->elts;
+ }
+ }
+}
+
+static int CommandLineInterpreter(scr_t screenID, const char *commandLine)
+{
+ char *szCommand = "APACHE2 ";
+ int iCommandLen = 8;
+ char szcommandLine[256];
+ char *pID;
+ screenID = screenID;
+
+
+ if (commandLine == NULL)
+ return NOTMYCOMMAND;
+ if (strlen(commandLine) <= strlen(szCommand))
+ return NOTMYCOMMAND;
+
+ strncpy (szcommandLine, commandLine, sizeof(szcommandLine)-1);
+
+ /* All added commands begin with "APACHE2 " */
+
+ if (!strnicmp(szCommand, szcommandLine, iCommandLen)) {
+ ActivateScreen (getscreenhandle());
+
+ /* If an instance id was not given but the nlm is loaded in
+ protected space, then the the command belongs to the
+ OS address space instance to pass it on. */
+ pID = strstr (szcommandLine, "-p");
+ if ((pID == NULL) && nlmisloadedprotected())
+ return NOTMYCOMMAND;
+
+ /* If we got an instance id but it doesn't match this
+ instance of the nlm, pass it on. */
+ if (pID) {
+ pID = &pID[2];
+ while (*pID && (*pID == ' '))
+ pID++;
+ }
+ if (pID && ap_my_addrspace && strnicmp(pID, ap_my_addrspace, strlen(ap_my_addrspace)))
+ return NOTMYCOMMAND;
+
+ /* If we have determined that this command belongs to this
+ instance of the nlm, then handle it. */
+ if (!strnicmp("RESTART",&szcommandLine[iCommandLen],3)) {
+ printf("Restart Requested...\n");
+ restart();
+ }
+ else if (!strnicmp("VERSION",&szcommandLine[iCommandLen],3)) {
+ printf("Server version: %s\n", ap_get_server_version());
+ printf("Server built: %s\n", ap_get_server_built());
+ }
+ else if (!strnicmp("MODULES",&szcommandLine[iCommandLen],3)) {
+ ap_show_modules();
+ }
+ else if (!strnicmp("DIRECTIVES",&szcommandLine[iCommandLen],3)) {
+ ap_show_directives();
+ }
+ else if (!strnicmp("SHUTDOWN",&szcommandLine[iCommandLen],3)) {
+ printf("Shutdown Requested...\n");
+ shutdown_pending = 1;
+ }
+ else if (!strnicmp("SETTINGS",&szcommandLine[iCommandLen],3)) {
+ if (show_settings) {
+ show_settings = 0;
+ ClearScreen (getscreenhandle());
+ show_server_data();
+ }
+ else {
+ show_settings = 1;
+ display_settings();
+ }
+ }
+ else {
+ show_settings = 0;
+ if (strnicmp("HELP",&szcommandLine[iCommandLen],3))
+ printf("Unknown APACHE2 command %s\n", &szcommandLine[iCommandLen]);
+ printf("Usage: APACHE2 [command] [-p <instance ID>]\n");
+ printf("Commands:\n");
+ printf("\tDIRECTIVES - Show directives\n");
+ printf("\tHELP - Display this help information\n");
+ printf("\tMODULES - Show a list of the loaded modules\n");
+ printf("\tRESTART - Reread the configuration file and restart Apache\n");
+ printf("\tSETTINGS - Show current thread status\n");
+ printf("\tSHUTDOWN - Shutdown Apache\n");
+ printf("\tVERSION - Display the server version information\n");
+ }
+
+ /* Tell NetWare we handled the command */
+ return HANDLEDCOMMAND;
+ }
+
+ /* Tell NetWare that the command isn't mine */
+ return NOTMYCOMMAND;
+}
+
+static int InstallConsoleHandler(void)
+{
+ /* Our command line handler interfaces the system operator
+ with this NLM */
+
+ NX_WRAP_INTERFACE(CommandLineInterpreter, 2, (void*)&(ConsoleHandler.parser));
+
+ ConsoleHandler.rTag = AllocateResourceTag(getnlmhandle(), "Command Line Processor",
+ ConsoleCommandSignature);
+ if (!ConsoleHandler.rTag)
+ {
+ printf("Error on allocate resource tag\n");
+ return 1;
+ }
+
+ RegisterConsoleCommand(&ConsoleHandler);
+
+ /* The Remove procedure unregisters the console handler */
+
+ return 0;
+}
+
+static void RemoveConsoleHandler(void)
+{
+ UnRegisterConsoleCommand(&ConsoleHandler);
+ NX_UNWRAP_INTERFACE(ConsoleHandler.parser);
+}
+
+static const char *set_threads_to_start(cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_threads_to_start = atoi(arg);
+ return NULL;
+}
+
+static const char *set_min_free_threads(cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_threads_min_free = atoi(arg);
+ if (ap_threads_min_free <= 0) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: detected MinSpareServers set to non-positive.");
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Resetting to 1 to avoid almost certain Apache failure.");
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Please read the documentation.");
+ ap_threads_min_free = 1;
+ }
+
+ return NULL;
+}
+
+static const char *set_max_free_threads(cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_threads_max_free = atoi(arg);
+ return NULL;
+}
+
+static const char *set_thread_limit (cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_threads_limit = atoi(arg);
+ if (ap_threads_limit > HARD_THREAD_LIMIT) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: MaxThreads of %d exceeds compile time limit "
+ "of %d threads,", ap_threads_limit, HARD_THREAD_LIMIT);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " lowering MaxThreads to %d. To increase, please "
+ "see the", HARD_THREAD_LIMIT);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " HARD_THREAD_LIMIT define in %s.",
+ AP_MPM_HARD_LIMITS_FILE);
+ ap_threads_limit = HARD_THREAD_LIMIT;
+ }
+ else if (ap_threads_limit < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require MaxThreads > 0, setting to 1");
+ ap_threads_limit = 1;
+ }
+ return NULL;
+}
+
+static const command_rec netware_mpm_cmds[] = {
+LISTEN_COMMANDS,
+AP_INIT_TAKE1("StartThreads", set_threads_to_start, NULL, RSRC_CONF,
+ "Number of worker threads launched at server startup"),
+AP_INIT_TAKE1("MinSpareThreads", set_min_free_threads, NULL, RSRC_CONF,
+ "Minimum number of idle threads, to handle request spikes"),
+AP_INIT_TAKE1("MaxSpareThreads", set_max_free_threads, NULL, RSRC_CONF,
+ "Maximum number of idle threads"),
+AP_INIT_TAKE1("MaxThreads", set_thread_limit, NULL, RSRC_CONF,
+ "Maximum number of worker threads alive at the same time"),
+{ NULL }
+};
+
+module AP_MODULE_DECLARE_DATA mpm_netware_module = {
+ MPM20_MODULE_STUFF,
+ netware_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 */
+ netware_mpm_cmds, /* command apr_table_t */
+ netware_mpm_hooks, /* register hooks */
+};
diff --git a/server/mpm/prefork/Makefile.in b/server/mpm/prefork/Makefile.in
new file mode 100644
index 00000000..034bf5ce
--- /dev/null
+++ b/server/mpm/prefork/Makefile.in
@@ -0,0 +1,5 @@
+
+LTLIBRARY_NAME = libprefork.la
+LTLIBRARY_SOURCES = prefork.c
+
+include $(top_srcdir)/build/ltlib.mk
diff --git a/server/mpm/prefork/config.m4 b/server/mpm/prefork/config.m4
new file mode 100644
index 00000000..9c189a86
--- /dev/null
+++ b/server/mpm/prefork/config.m4
@@ -0,0 +1,3 @@
+if test "$MPM_NAME" = "prefork" ; then
+ APACHE_FAST_OUTPUT(server/mpm/$MPM_NAME/Makefile)
+fi
diff --git a/server/mpm/prefork/mpm.h b/server/mpm/prefork/mpm.h
new file mode 100644
index 00000000..bf1fb949
--- /dev/null
+++ b/server/mpm/prefork/mpm.h
@@ -0,0 +1,62 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file prefork/mpm.h
+ * @brief Unix Prefork MPM (default for Uinx systems)
+ *
+ * @defgroup APACHE_MPM_PREFORK Unix Prefork
+ * @ingroup APACHE_MPM APACHE_OS_UNIX
+ * @{
+ */
+
+#include "httpd.h"
+#include "mpm_default.h"
+#include "scoreboard.h"
+#include "unixd.h"
+
+#ifndef APACHE_MPM_PREFORK_H
+#define APACHE_MPM_PREFORK_H
+
+#define PREFORK_MPM
+
+#define MPM_NAME "Prefork"
+
+#define AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
+#define AP_MPM_WANT_WAIT_OR_TIMEOUT
+#define AP_MPM_WANT_PROCESS_CHILD_STATUS
+#define AP_MPM_WANT_SET_PIDFILE
+#define AP_MPM_WANT_SET_SCOREBOARD
+#define AP_MPM_WANT_SET_LOCKFILE
+#define AP_MPM_WANT_SET_MAX_REQUESTS
+#define AP_MPM_WANT_SET_COREDUMPDIR
+#define AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
+#define AP_MPM_WANT_SIGNAL_SERVER
+#define AP_MPM_WANT_SET_MAX_MEM_FREE
+#define AP_MPM_WANT_FATAL_SIGNAL_HANDLER
+#define AP_MPM_WANT_SET_GRACEFUL_SHUTDOWN
+#define AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
+
+#define AP_MPM_USES_POD 1
+#define MPM_CHILD_PID(i) (ap_scoreboard_image->parent[i].pid)
+#define MPM_NOTE_CHILD_KILLED(i) (MPM_CHILD_PID(i) = 0)
+#define MPM_ACCEPT_FUNC unixd_accept
+
+extern int ap_threads_per_child;
+extern int ap_max_daemons_limit;
+extern server_rec *ap_server_conf;
+#endif /* APACHE_MPM_PREFORK_H */
+/** @} */
diff --git a/server/mpm/prefork/mpm_default.h b/server/mpm/prefork/mpm_default.h
new file mode 100644
index 00000000..d548a7ec
--- /dev/null
+++ b/server/mpm/prefork/mpm_default.h
@@ -0,0 +1,74 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file prefork/mpm_default.h
+ * @brief Prefork MPM defaults
+ *
+ * @addtogroup APACHE_MPM_PREFORK
+ * @{
+ */
+
+#ifndef APACHE_MPM_DEFAULT_H
+#define APACHE_MPM_DEFAULT_H
+
+/* Number of servers to spawn off by default --- also, if fewer than
+ * this free when the caretaker checks, it will spawn more.
+ */
+#ifndef DEFAULT_START_DAEMON
+#define DEFAULT_START_DAEMON 5
+#endif
+
+/* Maximum number of *free* server processes --- more than this, and
+ * they will die off.
+ */
+
+#ifndef DEFAULT_MAX_FREE_DAEMON
+#define DEFAULT_MAX_FREE_DAEMON 10
+#endif
+
+/* Minimum --- fewer than this, and more will be created */
+
+#ifndef DEFAULT_MIN_FREE_DAEMON
+#define DEFAULT_MIN_FREE_DAEMON 5
+#endif
+
+/* File used for accept locking, when we use a file */
+#ifndef DEFAULT_LOCKFILE
+#define DEFAULT_LOCKFILE DEFAULT_REL_RUNTIMEDIR "/accept.lock"
+#endif
+
+/* Where the main/parent process's pid is logged */
+#ifndef DEFAULT_PIDLOG
+#define DEFAULT_PIDLOG DEFAULT_REL_RUNTIMEDIR "/httpd.pid"
+#endif
+
+/*
+ * Interval, in microseconds, between scoreboard maintenance.
+ */
+#ifndef SCOREBOARD_MAINTENANCE_INTERVAL
+#define SCOREBOARD_MAINTENANCE_INTERVAL 1000000
+#endif
+
+/* Number of requests to try to handle in a single process. If <= 0,
+ * the children don't die off.
+ */
+#ifndef DEFAULT_MAX_REQUESTS_PER_CHILD
+#define DEFAULT_MAX_REQUESTS_PER_CHILD 10000
+#endif
+
+#endif /* AP_MPM_DEFAULT_H */
+/** @} */
diff --git a/server/mpm/prefork/prefork.c b/server/mpm/prefork/prefork.c
new file mode 100644
index 00000000..3858c290
--- /dev/null
+++ b/server/mpm/prefork/prefork.c
@@ -0,0 +1,1478 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "apr.h"
+#include "apr_portable.h"
+#include "apr_strings.h"
+#include "apr_thread_proc.h"
+#include "apr_signal.h"
+
+#define APR_WANT_STDIO
+#define APR_WANT_STRFUNC
+#include "apr_want.h"
+
+#if APR_HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#if APR_HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#define CORE_PRIVATE
+
+#include "ap_config.h"
+#include "httpd.h"
+#include "mpm_default.h"
+#include "http_main.h"
+#include "http_log.h"
+#include "http_config.h"
+#include "http_core.h" /* for get_remote_host */
+#include "http_connection.h"
+#include "scoreboard.h"
+#include "ap_mpm.h"
+#include "unixd.h"
+#include "mpm_common.h"
+#include "ap_listen.h"
+#include "ap_mmn.h"
+#include "apr_poll.h"
+
+#ifdef HAVE_BSTRING_H
+#include <bstring.h> /* for IRIX, FD_SET calls bzero() */
+#endif
+#ifdef HAVE_TIME_H
+#include <time.h>
+#endif
+#ifdef HAVE_SYS_PROCESSOR_H
+#include <sys/processor.h> /* for bindprocessor() */
+#endif
+
+#include <signal.h>
+#include <sys/times.h>
+
+/* Limit on the total --- clients will be locked out if more servers than
+ * this are needed. It is intended solely to keep the server from crashing
+ * when things get out of hand.
+ *
+ * We keep a hard maximum number of servers, for two reasons --- first off,
+ * in case something goes seriously wrong, we want to stop the fork bomb
+ * short of actually crashing the machine we're running on by filling some
+ * kernel table. Secondly, it keeps the size of the scoreboard file small
+ * enough that we can read the whole thing without worrying too much about
+ * the overhead.
+ */
+#ifndef DEFAULT_SERVER_LIMIT
+#define DEFAULT_SERVER_LIMIT 256
+#endif
+
+/* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT. We want
+ * some sort of compile-time limit to help catch typos.
+ */
+#ifndef MAX_SERVER_LIMIT
+#define MAX_SERVER_LIMIT 200000
+#endif
+
+#ifndef HARD_THREAD_LIMIT
+#define HARD_THREAD_LIMIT 1
+#endif
+
+/* config globals */
+
+int ap_threads_per_child=0; /* Worker threads per child */
+static apr_proc_mutex_t *accept_mutex;
+static int ap_daemons_to_start=0;
+static int ap_daemons_min_free=0;
+static int ap_daemons_max_free=0;
+static int ap_daemons_limit=0; /* MaxClients */
+static int server_limit = DEFAULT_SERVER_LIMIT;
+static int first_server_limit = 0;
+static int changed_limit_at_restart;
+static int mpm_state = AP_MPMQ_STARTING;
+static ap_pod_t *pod;
+
+/*
+ * The max child slot ever assigned, preserved across restarts. Necessary
+ * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts. We
+ * use this value to optimize routines that have to scan the entire scoreboard.
+ */
+int ap_max_daemons_limit = -1;
+server_rec *ap_server_conf;
+
+/* one_process --- debugging mode variable; can be set from the command line
+ * with the -X flag. If set, this gets you the child_main loop running
+ * in the process which originally started up (no detach, no make_child),
+ * which is a pretty nice debugging environment. (You'll get a SIGHUP
+ * early in standalone_main; just continue through. This is the server
+ * trying to kill off any child processes which it might have lying
+ * around --- Apache doesn't keep track of their pids, it just sends
+ * SIGHUP to the process group, ignoring it in the root process.
+ * Continue through and you'll be fine.).
+ */
+
+static int one_process = 0;
+
+static apr_pool_t *pconf; /* Pool for config stuff */
+static apr_pool_t *pchild; /* Pool for httpd child stuff */
+
+static pid_t ap_my_pid; /* it seems silly to call getpid all the time */
+static pid_t parent_pid;
+#ifndef MULTITHREAD
+static int my_child_num;
+#endif
+ap_generation_t volatile ap_my_generation=0;
+
+#ifdef TPF
+int tpf_child = 0;
+char tpf_server_name[INETD_SERVNAME_LENGTH+1];
+#endif /* TPF */
+
+static volatile int die_now = 0;
+
+#ifdef GPROF
+/*
+ * change directory for gprof to plop the gmon.out file
+ * configure in httpd.conf:
+ * GprofDir $RuntimeDir/ -> $ServerRoot/$RuntimeDir/gmon.out
+ * GprofDir $RuntimeDir/% -> $ServerRoot/$RuntimeDir/gprof.$pid/gmon.out
+ */
+static void chdir_for_gprof(void)
+{
+ core_server_config *sconf =
+ ap_get_module_config(ap_server_conf->module_config, &core_module);
+ char *dir = sconf->gprof_dir;
+ const char *use_dir;
+
+ if(dir) {
+ apr_status_t res;
+ char *buf = NULL ;
+ int len = strlen(sconf->gprof_dir) - 1;
+ if(*(dir + len) == '%') {
+ dir[len] = '\0';
+ buf = ap_append_pid(pconf, dir, "gprof.");
+ }
+ use_dir = ap_server_root_relative(pconf, buf ? buf : dir);
+ res = apr_dir_make(use_dir,
+ APR_UREAD | APR_UWRITE | APR_UEXECUTE |
+ APR_GREAD | APR_GEXECUTE |
+ APR_WREAD | APR_WEXECUTE, pconf);
+ if(res != APR_SUCCESS && !APR_STATUS_IS_EEXIST(res)) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, res, ap_server_conf,
+ "gprof: error creating directory %s", dir);
+ }
+ }
+ else {
+ use_dir = ap_server_root_relative(pconf, DEFAULT_REL_RUNTIMEDIR);
+ }
+
+ chdir(use_dir);
+}
+#else
+#define chdir_for_gprof()
+#endif
+
+/* XXX - I don't know if TPF will ever use this module or not, so leave
+ * the ap_check_signals calls in but disable them - manoj */
+#define ap_check_signals()
+
+/* a clean exit from a child with proper cleanup */
+static void clean_child_exit(int code) __attribute__ ((noreturn));
+static void clean_child_exit(int code)
+{
+ mpm_state = AP_MPMQ_STOPPING;
+
+ if (pchild) {
+ apr_pool_destroy(pchild);
+ }
+ ap_mpm_pod_close(pod);
+ chdir_for_gprof();
+ exit(code);
+}
+
+static void accept_mutex_on(void)
+{
+ apr_status_t rv = apr_proc_mutex_lock(accept_mutex);
+ if (rv != APR_SUCCESS) {
+ const char *msg = "couldn't grab the accept mutex";
+
+ if (ap_my_generation !=
+ ap_scoreboard_image->global->running_generation) {
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL, "%s", msg);
+ clean_child_exit(0);
+ }
+ else {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, NULL, "%s", msg);
+ exit(APEXIT_CHILDFATAL);
+ }
+ }
+}
+
+static void accept_mutex_off(void)
+{
+ apr_status_t rv = apr_proc_mutex_unlock(accept_mutex);
+ if (rv != APR_SUCCESS) {
+ const char *msg = "couldn't release the accept mutex";
+
+ if (ap_my_generation !=
+ ap_scoreboard_image->global->running_generation) {
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL, "%s", msg);
+ /* don't exit here... we have a connection to
+ * process, after which point we'll see that the
+ * generation changed and we'll exit cleanly
+ */
+ }
+ else {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, NULL, "%s", msg);
+ exit(APEXIT_CHILDFATAL);
+ }
+ }
+}
+
+/* On some architectures it's safe to do unserialized accept()s in the single
+ * Listen case. But it's never safe to do it in the case where there's
+ * multiple Listen statements. Define SINGLE_LISTEN_UNSERIALIZED_ACCEPT
+ * when it's safe in the single Listen case.
+ */
+#ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
+#define SAFE_ACCEPT(stmt) do {if (ap_listeners->next) {stmt;}} while(0)
+#else
+#define SAFE_ACCEPT(stmt) do {stmt;} while(0)
+#endif
+
+AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
+{
+ switch(query_code){
+ case AP_MPMQ_MAX_DAEMON_USED:
+ *result = ap_daemons_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_THREADED:
+ *result = AP_MPMQ_NOT_SUPPORTED;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_FORKED:
+ *result = AP_MPMQ_DYNAMIC;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_DAEMONS:
+ *result = server_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_THREADS:
+ *result = HARD_THREAD_LIMIT;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_THREADS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_DAEMONS:
+ *result = ap_daemons_min_free;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_THREADS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_DAEMONS:
+ *result = ap_daemons_max_free;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_THREADS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_REQUESTS_DAEMON:
+ *result = ap_max_requests_per_child;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_DAEMONS:
+ *result = server_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_MPM_STATE:
+ *result = mpm_state;
+ return APR_SUCCESS;
+ }
+ return APR_ENOTIMPL;
+}
+
+#if defined(NEED_WAITPID)
+/*
+ Systems without a real waitpid sometimes lose a child's exit while waiting
+ for another. Search through the scoreboard for missing children.
+ */
+int reap_children(int *exitcode, apr_exit_why_e *status)
+{
+ int n, pid;
+
+ for (n = 0; n < ap_max_daemons_limit; ++n) {
+ if (ap_scoreboard_image->servers[n][0].status != SERVER_DEAD &&
+ kill((pid = ap_scoreboard_image->parent[n].pid), 0) == -1) {
+ ap_update_child_status_from_indexes(n, 0, SERVER_DEAD, NULL);
+ /* just mark it as having a successful exit status */
+ *status = APR_PROC_EXIT;
+ *exitcode = 0;
+ return(pid);
+ }
+ }
+ return 0;
+}
+#endif
+
+/*****************************************************************
+ * Connection structures and accounting...
+ */
+
+static void just_die(int sig)
+{
+ clean_child_exit(0);
+}
+
+static void stop_listening(int sig)
+{
+ ap_close_listeners();
+
+ /* For a graceful stop, we want the child to exit when done */
+ die_now = 1;
+}
+
+/* volatile just in case */
+static int volatile shutdown_pending;
+static int volatile restart_pending;
+static int volatile is_graceful;
+
+static void sig_term(int sig)
+{
+ if (shutdown_pending == 1) {
+ /* Um, is this _probably_ not an error, if the user has
+ * tried to do a shutdown twice quickly, so we won't
+ * worry about reporting it.
+ */
+ return;
+ }
+ shutdown_pending = 1;
+ is_graceful = (sig == AP_SIG_GRACEFUL_STOP);
+}
+
+/* restart() is the signal handler for SIGHUP and AP_SIG_GRACEFUL
+ * in the parent process, unless running in ONE_PROCESS mode
+ */
+static void restart(int sig)
+{
+ if (restart_pending == 1) {
+ /* Probably not an error - don't bother reporting it */
+ return;
+ }
+ restart_pending = 1;
+ is_graceful = (sig == AP_SIG_GRACEFUL);
+}
+
+static void set_signals(void)
+{
+#ifndef NO_USE_SIGACTION
+ struct sigaction sa;
+#endif
+
+ if (!one_process) {
+ ap_fatal_signal_setup(ap_server_conf, pconf);
+ }
+
+#ifndef NO_USE_SIGACTION
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+
+ sa.sa_handler = sig_term;
+ if (sigaction(SIGTERM, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
+#ifdef AP_SIG_GRACEFUL_STOP
+ if (sigaction(AP_SIG_GRACEFUL_STOP, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(" AP_SIG_GRACEFUL_STOP_STRING ")");
+#endif
+#ifdef SIGINT
+ if (sigaction(SIGINT, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");
+#endif
+#ifdef SIGXCPU
+ sa.sa_handler = SIG_DFL;
+ if (sigaction(SIGXCPU, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXCPU)");
+#endif
+#ifdef SIGXFSZ
+ sa.sa_handler = SIG_DFL;
+ if (sigaction(SIGXFSZ, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXFSZ)");
+#endif
+#ifdef SIGPIPE
+ sa.sa_handler = SIG_IGN;
+ if (sigaction(SIGPIPE, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGPIPE)");
+#endif
+
+ /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy
+ * processing one
+ */
+ sigaddset(&sa.sa_mask, SIGHUP);
+ sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL);
+ sa.sa_handler = restart;
+ if (sigaction(SIGHUP, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");
+ if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(" AP_SIG_GRACEFUL_STRING ")");
+#else
+ if (!one_process) {
+#ifdef SIGXCPU
+ apr_signal(SIGXCPU, SIG_DFL);
+#endif /* SIGXCPU */
+#ifdef SIGXFSZ
+ apr_signal(SIGXFSZ, SIG_DFL);
+#endif /* SIGXFSZ */
+ }
+
+ apr_signal(SIGTERM, sig_term);
+#ifdef SIGHUP
+ apr_signal(SIGHUP, restart);
+#endif /* SIGHUP */
+#ifdef AP_SIG_GRACEFUL
+ apr_signal(AP_SIG_GRACEFUL, restart);
+#endif /* AP_SIG_GRACEFUL */
+#ifdef AP_SIG_GRACEFUL_STOP
+ apr_signal(AP_SIG_GRACEFUL_STOP, sig_term);
+#endif /* AP_SIG_GRACEFUL */
+#ifdef SIGPIPE
+ apr_signal(SIGPIPE, SIG_IGN);
+#endif /* SIGPIPE */
+
+#endif
+}
+
+/*****************************************************************
+ * Child process main loop.
+ * The following vars are static to avoid getting clobbered by longjmp();
+ * they are really private to child_main.
+ */
+
+static int requests_this_child;
+static int num_listensocks = 0;
+
+
+int ap_graceful_stop_signalled(void)
+{
+ /* not ever called anymore... */
+ return 0;
+}
+
+
+static void child_main(int child_num_arg)
+{
+ apr_pool_t *ptrans;
+ apr_allocator_t *allocator;
+ apr_status_t status;
+ int i;
+ ap_listen_rec *lr;
+ apr_pollset_t *pollset;
+ ap_sb_handle_t *sbh;
+ apr_bucket_alloc_t *bucket_alloc;
+ int last_poll_idx = 0;
+
+ mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this
+ * child initializes
+ */
+
+ my_child_num = child_num_arg;
+ ap_my_pid = getpid();
+ requests_this_child = 0;
+
+ ap_fatal_signal_child_setup(ap_server_conf);
+
+ /* Get a sub context for global allocations in this child, so that
+ * we can have cleanups occur when the child exits.
+ */
+ apr_allocator_create(&allocator);
+ apr_allocator_max_free_set(allocator, ap_max_mem_free);
+ apr_pool_create_ex(&pchild, pconf, NULL, allocator);
+ apr_allocator_owner_set(allocator, pchild);
+
+ apr_pool_create(&ptrans, pchild);
+ apr_pool_tag(ptrans, "transaction");
+
+ /* needs to be done before we switch UIDs so we have permissions */
+ ap_reopen_scoreboard(pchild, NULL, 0);
+ status = apr_proc_mutex_child_init(&accept_mutex, ap_lock_fname, pchild);
+ if (status != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, status, ap_server_conf,
+ "Couldn't initialize cross-process lock in child "
+ "(%s) (%d)", ap_lock_fname, ap_accept_lock_mech);
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ if (unixd_setup_child()) {
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ ap_run_child_init(pchild, ap_server_conf);
+
+ ap_create_sb_handle(&sbh, pchild, my_child_num, 0);
+
+ (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
+
+ /* Set up the pollfd array */
+ /* ### check the status */
+ (void) apr_pollset_create(&pollset, num_listensocks, pchild, 0);
+
+ for (lr = ap_listeners, i = num_listensocks; i--; lr = lr->next) {
+ apr_pollfd_t pfd = { 0 };
+
+ pfd.desc_type = APR_POLL_SOCKET;
+ pfd.desc.s = lr->sd;
+ pfd.reqevents = APR_POLLIN;
+ pfd.client_data = lr;
+
+ /* ### check the status */
+ (void) apr_pollset_add(pollset, &pfd);
+ }
+
+ mpm_state = AP_MPMQ_RUNNING;
+
+ bucket_alloc = apr_bucket_alloc_create(pchild);
+
+ while (!die_now) {
+ conn_rec *current_conn;
+ void *csd;
+
+ /*
+ * (Re)initialize this child to a pre-connection state.
+ */
+
+ apr_pool_clear(ptrans);
+
+ if ((ap_max_requests_per_child > 0
+ && requests_this_child++ >= ap_max_requests_per_child)) {
+ clean_child_exit(0);
+ }
+
+ (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
+
+ /*
+ * Wait for an acceptable connection to arrive.
+ */
+
+ /* Lock around "accept", if necessary */
+ SAFE_ACCEPT(accept_mutex_on());
+
+ if (num_listensocks == 1) {
+ /* There is only one listener record, so refer to that one. */
+ lr = ap_listeners;
+ }
+ else {
+ /* multiple listening sockets - need to poll */
+ for (;;) {
+ apr_int32_t numdesc;
+ const apr_pollfd_t *pdesc;
+
+ /* timeout == -1 == wait forever */
+ status = apr_pollset_poll(pollset, -1, &numdesc, &pdesc);
+ if (status != APR_SUCCESS) {
+ if (APR_STATUS_IS_EINTR(status)) {
+ if (one_process && shutdown_pending) {
+ return;
+ }
+ continue;
+ }
+ /* Single Unix documents select as returning errnos
+ * EBADF, EINTR, and EINVAL... and in none of those
+ * cases does it make sense to continue. In fact
+ * on Linux 2.0.x we seem to end up with EFAULT
+ * occasionally, and we'd loop forever due to it.
+ */
+ ap_log_error(APLOG_MARK, APLOG_ERR, status,
+ ap_server_conf, "apr_pollset_poll: (listen)");
+ clean_child_exit(1);
+ }
+
+ /* We can always use pdesc[0], but sockets at position N
+ * could end up completely starved of attention in a very
+ * busy server. Therefore, we round-robin across the
+ * returned set of descriptors. While it is possible that
+ * the returned set of descriptors might flip around and
+ * continue to starve some sockets, we happen to know the
+ * internal pollset implementation retains ordering
+ * stability of the sockets. Thus, the round-robin should
+ * ensure that a socket will eventually be serviced.
+ */
+ if (last_poll_idx >= numdesc)
+ last_poll_idx = 0;
+
+ /* Grab a listener record from the client_data of the poll
+ * descriptor, and advance our saved index to round-robin
+ * the next fetch.
+ *
+ * ### hmm... this descriptor might have POLLERR rather
+ * ### than POLLIN
+ */
+ lr = pdesc[last_poll_idx++].client_data;
+ goto got_fd;
+ }
+ }
+ got_fd:
+ /* if we accept() something we don't want to die, so we have to
+ * defer the exit
+ */
+ status = lr->accept_func(&csd, lr, ptrans);
+
+ SAFE_ACCEPT(accept_mutex_off()); /* unlock after "accept" */
+
+ if (status == APR_EGENERAL) {
+ /* resource shortage or should-not-occur occured */
+ clean_child_exit(1);
+ }
+ else if (status != APR_SUCCESS) {
+ continue;
+ }
+
+ /*
+ * We now have a connection, so set it up with the appropriate
+ * socket options, file descriptors, and read/write buffers.
+ */
+
+ current_conn = ap_run_create_connection(ptrans, ap_server_conf, csd, my_child_num, sbh, bucket_alloc);
+ if (current_conn) {
+ ap_process_connection(current_conn, csd);
+ ap_lingering_close(current_conn);
+ }
+
+ /* Check the pod and the generation number after processing a
+ * connection so that we'll go away if a graceful restart occurred
+ * while we were processing the connection or we are the lucky
+ * idle server process that gets to die.
+ */
+ if (ap_mpm_pod_check(pod) == APR_SUCCESS) { /* selected as idle? */
+ die_now = 1;
+ }
+ else if (ap_my_generation !=
+ ap_scoreboard_image->global->running_generation) { /* restart? */
+ /* yeah, this could be non-graceful restart, in which case the
+ * parent will kill us soon enough, but why bother checking?
+ */
+ die_now = 1;
+ }
+ }
+ clean_child_exit(0);
+}
+
+
+static int make_child(server_rec *s, int slot)
+{
+ int pid;
+
+ if (slot + 1 > ap_max_daemons_limit) {
+ ap_max_daemons_limit = slot + 1;
+ }
+
+ if (one_process) {
+ apr_signal(SIGHUP, sig_term);
+ /* Don't catch AP_SIG_GRACEFUL in ONE_PROCESS mode :) */
+ apr_signal(SIGINT, sig_term);
+#ifdef SIGQUIT
+ apr_signal(SIGQUIT, SIG_DFL);
+#endif
+ apr_signal(SIGTERM, sig_term);
+ child_main(slot);
+ return 0;
+ }
+
+ (void) ap_update_child_status_from_indexes(slot, 0, SERVER_STARTING,
+ (request_rec *) NULL);
+
+
+#ifdef _OSD_POSIX
+ /* BS2000 requires a "special" version of fork() before a setuid() call */
+ if ((pid = os_fork(unixd_config.user_name)) == -1) {
+#elif defined(TPF)
+ if ((pid = os_fork(s, slot)) == -1) {
+#else
+ if ((pid = fork()) == -1) {
+#endif
+ ap_log_error(APLOG_MARK, APLOG_ERR, errno, s, "fork: Unable to fork new process");
+
+ /* fork didn't succeed. Fix the scoreboard or else
+ * it will say SERVER_STARTING forever and ever
+ */
+ (void) ap_update_child_status_from_indexes(slot, 0, SERVER_DEAD,
+ (request_rec *) NULL);
+
+ /* In case system resources are maxxed out, we don't want
+ * Apache running away with the CPU trying to fork over and
+ * over and over again.
+ */
+ sleep(10);
+
+ return -1;
+ }
+
+ if (!pid) {
+#ifdef HAVE_BINDPROCESSOR
+ /* by default AIX binds to a single processor
+ * this bit unbinds children which will then bind to another cpu
+ */
+ int status = bindprocessor(BINDPROCESS, (int)getpid(),
+ PROCESSOR_CLASS_ANY);
+ if (status != OK) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno,
+ ap_server_conf, "processor unbind failed %d", status);
+ }
+#endif
+ RAISE_SIGSTOP(MAKE_CHILD);
+ AP_MONCONTROL(1);
+ /* Disable the parent's signal handlers and set up proper handling in
+ * the child.
+ */
+ apr_signal(SIGHUP, just_die);
+ apr_signal(SIGTERM, just_die);
+ /* The child process just closes listeners on AP_SIG_GRACEFUL.
+ * The pod is used for signalling the graceful restart.
+ */
+ apr_signal(AP_SIG_GRACEFUL, stop_listening);
+ child_main(slot);
+ }
+
+ ap_scoreboard_image->parent[slot].pid = pid;
+
+ return 0;
+}
+
+
+/* start up a bunch of children */
+static void startup_children(int number_to_start)
+{
+ int i;
+
+ for (i = 0; number_to_start && i < ap_daemons_limit; ++i) {
+ if (ap_scoreboard_image->servers[i][0].status != SERVER_DEAD) {
+ continue;
+ }
+ if (make_child(ap_server_conf, i) < 0) {
+ break;
+ }
+ --number_to_start;
+ }
+}
+
+
+/*
+ * idle_spawn_rate is the number of children that will be spawned on the
+ * next maintenance cycle if there aren't enough idle servers. It is
+ * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
+ * without the need to spawn.
+ */
+static int idle_spawn_rate = 1;
+#ifndef MAX_SPAWN_RATE
+#define MAX_SPAWN_RATE (32)
+#endif
+static int hold_off_on_exponential_spawning;
+
+static void perform_idle_server_maintenance(apr_pool_t *p)
+{
+ int i;
+ int to_kill;
+ int idle_count;
+ worker_score *ws;
+ int free_length;
+ int free_slots[MAX_SPAWN_RATE];
+ int last_non_dead;
+ int total_non_dead;
+
+ /* initialize the free_list */
+ free_length = 0;
+
+ to_kill = -1;
+ idle_count = 0;
+ last_non_dead = -1;
+ total_non_dead = 0;
+
+ for (i = 0; i < ap_daemons_limit; ++i) {
+ int status;
+
+ if (i >= ap_max_daemons_limit && free_length == idle_spawn_rate)
+ break;
+ ws = &ap_scoreboard_image->servers[i][0];
+ status = ws->status;
+ if (status == SERVER_DEAD) {
+ /* try to keep children numbers as low as possible */
+ if (free_length < idle_spawn_rate) {
+ free_slots[free_length] = i;
+ ++free_length;
+ }
+ }
+ else {
+ /* We consider a starting server as idle because we started it
+ * at least a cycle ago, and if it still hasn't finished starting
+ * then we're just going to swamp things worse by forking more.
+ * So we hopefully won't need to fork more if we count it.
+ * This depends on the ordering of SERVER_READY and SERVER_STARTING.
+ */
+ if (status <= SERVER_READY) {
+ ++ idle_count;
+ /* always kill the highest numbered child if we have to...
+ * no really well thought out reason ... other than observing
+ * the server behaviour under linux where lower numbered children
+ * tend to service more hits (and hence are more likely to have
+ * their data in cpu caches).
+ */
+ to_kill = i;
+ }
+
+ ++total_non_dead;
+ last_non_dead = i;
+ }
+ }
+ ap_max_daemons_limit = last_non_dead + 1;
+ if (idle_count > ap_daemons_max_free) {
+ /* kill off one child... we use the pod because that'll cause it to
+ * shut down gracefully, in case it happened to pick up a request
+ * while we were counting
+ */
+ ap_mpm_pod_signal(pod);
+ idle_spawn_rate = 1;
+ }
+ else if (idle_count < ap_daemons_min_free) {
+ /* terminate the free list */
+ if (free_length == 0) {
+ /* only report this condition once */
+ static int reported = 0;
+
+ if (!reported) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
+ "server reached MaxClients setting, consider"
+ " raising the MaxClients setting");
+ reported = 1;
+ }
+ idle_spawn_rate = 1;
+ }
+ else {
+ if (idle_spawn_rate >= 8) {
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
+ "server seems busy, (you may need "
+ "to increase StartServers, or Min/MaxSpareServers), "
+ "spawning %d children, there are %d idle, and "
+ "%d total children", idle_spawn_rate,
+ idle_count, total_non_dead);
+ }
+ for (i = 0; i < free_length; ++i) {
+#ifdef TPF
+ if (make_child(ap_server_conf, free_slots[i]) == -1) {
+ if(free_length == 1) {
+ shutdown_pending = 1;
+ ap_log_error(APLOG_MARK, APLOG_EMERG, 0, ap_server_conf,
+ "No active child processes: shutting down");
+ }
+ }
+#else
+ make_child(ap_server_conf, free_slots[i]);
+#endif /* TPF */
+ }
+ /* the next time around we want to spawn twice as many if this
+ * wasn't good enough, but not if we've just done a graceful
+ */
+ if (hold_off_on_exponential_spawning) {
+ --hold_off_on_exponential_spawning;
+ }
+ else if (idle_spawn_rate < MAX_SPAWN_RATE) {
+ idle_spawn_rate *= 2;
+ }
+ }
+ }
+ else {
+ idle_spawn_rate = 1;
+ }
+}
+
+/*****************************************************************
+ * Executive routines.
+ */
+
+int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
+{
+ int index;
+ int remaining_children_to_start;
+ apr_status_t rv;
+
+ ap_log_pid(pconf, ap_pid_fname);
+
+ first_server_limit = server_limit;
+ if (changed_limit_at_restart) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
+ "WARNING: Attempt to change ServerLimit "
+ "ignored during restart");
+ changed_limit_at_restart = 0;
+ }
+
+ /* Initialize cross-process accept lock */
+ ap_lock_fname = apr_psprintf(_pconf, "%s.%" APR_PID_T_FMT,
+ ap_server_root_relative(_pconf, ap_lock_fname),
+ ap_my_pid);
+
+ rv = apr_proc_mutex_create(&accept_mutex, ap_lock_fname,
+ ap_accept_lock_mech, _pconf);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
+ "Couldn't create accept lock (%s) (%d)",
+ ap_lock_fname, ap_accept_lock_mech);
+ mpm_state = AP_MPMQ_STOPPING;
+ return 1;
+ }
+
+#if APR_USE_SYSVSEM_SERIALIZE
+ if (ap_accept_lock_mech == APR_LOCK_DEFAULT ||
+ ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
+#else
+ if (ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
+#endif
+ rv = unixd_set_proc_mutex_perms(accept_mutex);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
+ "Couldn't set permissions on cross-process lock; "
+ "check User and Group directives");
+ mpm_state = AP_MPMQ_STOPPING;
+ return 1;
+ }
+ }
+
+ if (!is_graceful) {
+ if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
+ mpm_state = AP_MPMQ_STOPPING;
+ return 1;
+ }
+ /* fix the generation number in the global score; we just got a new,
+ * cleared scoreboard
+ */
+ ap_scoreboard_image->global->running_generation = ap_my_generation;
+ }
+
+ set_signals();
+
+ if (one_process) {
+ AP_MONCONTROL(1);
+ make_child(ap_server_conf, 0);
+ }
+ else {
+ if (ap_daemons_max_free < ap_daemons_min_free + 1) /* Don't thrash... */
+ ap_daemons_max_free = ap_daemons_min_free + 1;
+
+ /* If we're doing a graceful_restart then we're going to see a lot
+ * of children exiting immediately when we get into the main loop
+ * below (because we just sent them AP_SIG_GRACEFUL). This happens pretty
+ * rapidly... and for each one that exits we'll start a new one until
+ * we reach at least daemons_min_free. But we may be permitted to
+ * start more than that, so we'll just keep track of how many we're
+ * supposed to start up without the 1 second penalty between each fork.
+ */
+ remaining_children_to_start = ap_daemons_to_start;
+ if (remaining_children_to_start > ap_daemons_limit) {
+ remaining_children_to_start = ap_daemons_limit;
+ }
+ if (!is_graceful) {
+ startup_children(remaining_children_to_start);
+ remaining_children_to_start = 0;
+ }
+ else {
+ /* give the system some time to recover before kicking into
+ * exponential mode
+ */
+ hold_off_on_exponential_spawning = 10;
+ }
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "%s configured -- resuming normal operations",
+ ap_get_server_version());
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
+ "Server built: %s", ap_get_server_built());
+#ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "AcceptMutex: %s (default: %s)",
+ apr_proc_mutex_name(accept_mutex),
+ apr_proc_mutex_defname());
+#endif
+ restart_pending = shutdown_pending = 0;
+
+ mpm_state = AP_MPMQ_RUNNING;
+
+ while (!restart_pending && !shutdown_pending) {
+ int child_slot;
+ apr_exit_why_e exitwhy;
+ int status, processed_status;
+ /* this is a memory leak, but I'll fix it later. */
+ apr_proc_t pid;
+
+ ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
+
+ /* XXX: if it takes longer than 1 second for all our children
+ * to start up and get into IDLE state then we may spawn an
+ * extra child
+ */
+ if (pid.pid != -1) {
+ processed_status = ap_process_child_status(&pid, exitwhy, status);
+ if (processed_status == APEXIT_CHILDFATAL) {
+ mpm_state = AP_MPMQ_STOPPING;
+ return 1;
+ }
+
+ /* non-fatal death... note that it's gone in the scoreboard. */
+ child_slot = find_child_by_pid(&pid);
+ if (child_slot >= 0) {
+ (void) ap_update_child_status_from_indexes(child_slot, 0, SERVER_DEAD,
+ (request_rec *) NULL);
+ if (processed_status == APEXIT_CHILDSICK) {
+ /* child detected a resource shortage (E[NM]FILE, ENOBUFS, etc)
+ * cut the fork rate to the minimum
+ */
+ idle_spawn_rate = 1;
+ }
+ else if (remaining_children_to_start
+ && child_slot < ap_daemons_limit) {
+ /* we're still doing a 1-for-1 replacement of dead
+ * children with new children
+ */
+ make_child(ap_server_conf, child_slot);
+ --remaining_children_to_start;
+ }
+#if APR_HAS_OTHER_CHILD
+ }
+ else if (apr_proc_other_child_alert(&pid, APR_OC_REASON_DEATH, status) == APR_SUCCESS) {
+ /* handled */
+#endif
+ }
+ else if (is_graceful) {
+ /* Great, we've probably just lost a slot in the
+ * scoreboard. Somehow we don't know about this
+ * child.
+ */
+ ap_log_error(APLOG_MARK, APLOG_WARNING,
+ 0, ap_server_conf,
+ "long lost child came home! (pid %ld)", (long)pid.pid);
+ }
+ /* Don't perform idle maintenance when a child dies,
+ * only do it when there's a timeout. Remember only a
+ * finite number of children can die, and it's pretty
+ * pathological for a lot to die suddenly.
+ */
+ continue;
+ }
+ else if (remaining_children_to_start) {
+ /* we hit a 1 second timeout in which none of the previous
+ * generation of children needed to be reaped... so assume
+ * they're all done, and pick up the slack if any is left.
+ */
+ startup_children(remaining_children_to_start);
+ remaining_children_to_start = 0;
+ /* In any event we really shouldn't do the code below because
+ * few of the servers we just started are in the IDLE state
+ * yet, so we'd mistakenly create an extra server.
+ */
+ continue;
+ }
+
+ perform_idle_server_maintenance(pconf);
+#ifdef TPF
+ shutdown_pending = os_check_server(tpf_server_name);
+ ap_check_signals();
+ sleep(1);
+#endif /*TPF */
+ }
+ } /* one_process */
+
+ mpm_state = AP_MPMQ_STOPPING;
+
+ if (shutdown_pending && !is_graceful) {
+ /* Time to shut down:
+ * Kill child processes, tell them to call child_exit, etc...
+ */
+ if (unixd_killpg(getpgrp(), SIGTERM) < 0) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGTERM");
+ }
+ ap_reclaim_child_processes(1); /* Start with SIGTERM */
+
+ /* cleanup pid file on normal shutdown */
+ {
+ const char *pidfile = NULL;
+ pidfile = ap_server_root_relative (pconf, ap_pid_fname);
+ if ( pidfile != NULL && unlink(pidfile) == 0)
+ ap_log_error(APLOG_MARK, APLOG_INFO,
+ 0, ap_server_conf,
+ "removed PID file %s (pid=%ld)",
+ pidfile, (long)getpid());
+ }
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "caught SIGTERM, shutting down");
+
+ return 1;
+ } else if (shutdown_pending) {
+ /* Time to perform a graceful shut down:
+ * Reap the inactive children, and ask the active ones
+ * to close their listeners, then wait until they are
+ * all done to exit.
+ */
+ int active_children;
+ apr_time_t cutoff = 0;
+
+ /* Stop listening */
+ ap_close_listeners();
+
+ /* kill off the idle ones */
+ ap_mpm_pod_killpg(pod, ap_max_daemons_limit);
+
+ /* Send SIGUSR1 to the active children */
+ active_children = 0;
+ for (index = 0; index < ap_daemons_limit; ++index) {
+ if (ap_scoreboard_image->servers[index][0].status != SERVER_DEAD) {
+ /* Ask each child to close its listeners. */
+ kill(MPM_CHILD_PID(index), AP_SIG_GRACEFUL);
+ active_children++;
+ }
+ }
+
+ /* Allow each child which actually finished to exit */
+ ap_relieve_child_processes();
+
+ /* cleanup pid file */
+ {
+ const char *pidfile = NULL;
+ pidfile = ap_server_root_relative (pconf, ap_pid_fname);
+ if ( pidfile != NULL && unlink(pidfile) == 0)
+ ap_log_error(APLOG_MARK, APLOG_INFO,
+ 0, ap_server_conf,
+ "removed PID file %s (pid=%ld)",
+ pidfile, (long)getpid());
+ }
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "caught " AP_SIG_GRACEFUL_STOP_STRING ", shutting down gracefully");
+
+ if (ap_graceful_shutdown_timeout) {
+ cutoff = apr_time_now() +
+ apr_time_from_sec(ap_graceful_shutdown_timeout);
+ }
+
+ /* Don't really exit until each child has finished */
+ shutdown_pending = 0;
+ do {
+ /* Pause for a second */
+ sleep(1);
+
+ /* Relieve any children which have now exited */
+ ap_relieve_child_processes();
+
+ active_children = 0;
+ for (index = 0; index < ap_daemons_limit; ++index) {
+ if (MPM_CHILD_PID(index) != 0) {
+ if (kill(MPM_CHILD_PID(index), 0) == 0) {
+ active_children = 1;
+ /* Having just one child is enough to stay around */
+ break;
+ }
+ }
+ }
+ } while (!shutdown_pending && active_children &&
+ (!ap_graceful_shutdown_timeout || apr_time_now() < cutoff));
+
+ /* We might be here because we received SIGTERM, either
+ * way, try and make sure that all of our processes are
+ * really dead.
+ */
+ unixd_killpg(getpgrp(), SIGTERM);
+
+ return 1;
+ }
+
+ /* we've been told to restart */
+ apr_signal(SIGHUP, SIG_IGN);
+ apr_signal(AP_SIG_GRACEFUL, SIG_IGN);
+ if (one_process) {
+ /* not worth thinking about */
+ return 1;
+ }
+
+ /* advance to the next generation */
+ /* XXX: we really need to make sure this new generation number isn't in
+ * use by any of the children.
+ */
+ ++ap_my_generation;
+ ap_scoreboard_image->global->running_generation = ap_my_generation;
+
+ if (is_graceful) {
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "Graceful restart requested, doing restart");
+
+ /* kill off the idle ones */
+ ap_mpm_pod_killpg(pod, ap_max_daemons_limit);
+
+ /* This is mostly for debugging... so that we know what is still
+ * gracefully dealing with existing request. This will break
+ * in a very nasty way if we ever have the scoreboard totally
+ * file-based (no shared memory)
+ */
+ for (index = 0; index < ap_daemons_limit; ++index) {
+ if (ap_scoreboard_image->servers[index][0].status != SERVER_DEAD) {
+ ap_scoreboard_image->servers[index][0].status = SERVER_GRACEFUL;
+ /* Ask each child to close its listeners.
+ *
+ * NOTE: we use the scoreboard, because if we send SIGUSR1
+ * to every process in the group, this may include CGI's,
+ * piped loggers, etc. They almost certainly won't handle
+ * it gracefully.
+ */
+ kill(ap_scoreboard_image->parent[index].pid, AP_SIG_GRACEFUL);
+ }
+ }
+ }
+ else {
+ /* Kill 'em off */
+ if (unixd_killpg(getpgrp(), SIGHUP) < 0) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGHUP");
+ }
+ ap_reclaim_child_processes(0); /* Not when just starting up */
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "SIGHUP received. Attempting to restart");
+ }
+
+ return 0;
+}
+
+/* This really should be a post_config hook, but the error log is already
+ * redirected by that point, so we need to do this in the open_logs phase.
+ */
+static int prefork_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
+{
+ apr_status_t rv;
+
+ pconf = p;
+ ap_server_conf = s;
+
+ if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0,
+ NULL, "no listening sockets available, shutting down");
+ return DONE;
+ }
+
+ if ((rv = ap_mpm_pod_open(pconf, &pod))) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT|APLOG_STARTUP, rv, NULL,
+ "Could not open pipe-of-death.");
+ return DONE;
+ }
+ return OK;
+}
+
+static int prefork_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
+{
+ static int restart_num = 0;
+ int no_detach, debug, foreground;
+ apr_status_t rv;
+
+ mpm_state = AP_MPMQ_STARTING;
+
+ debug = ap_exists_config_define("DEBUG");
+
+ if (debug) {
+ foreground = one_process = 1;
+ no_detach = 0;
+ }
+ else
+ {
+ no_detach = ap_exists_config_define("NO_DETACH");
+ one_process = ap_exists_config_define("ONE_PROCESS");
+ foreground = ap_exists_config_define("FOREGROUND");
+ }
+
+ /* sigh, want this only the second time around */
+ if (restart_num++ == 1) {
+ is_graceful = 0;
+
+ if (!one_process && !foreground) {
+ rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
+ : APR_PROC_DETACH_DAEMONIZE);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
+ "apr_proc_detach failed");
+ return HTTP_INTERNAL_SERVER_ERROR;
+ }
+ }
+
+ parent_pid = ap_my_pid = getpid();
+ }
+
+ unixd_pre_config(ptemp);
+ ap_listen_pre_config();
+ ap_daemons_to_start = DEFAULT_START_DAEMON;
+ ap_daemons_min_free = DEFAULT_MIN_FREE_DAEMON;
+ ap_daemons_max_free = DEFAULT_MAX_FREE_DAEMON;
+ ap_daemons_limit = server_limit;
+ ap_pid_fname = DEFAULT_PIDLOG;
+ ap_lock_fname = DEFAULT_LOCKFILE;
+ ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
+ ap_extended_status = 0;
+#ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
+ ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
+#endif
+
+ apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
+
+ return OK;
+}
+
+static void prefork_hooks(apr_pool_t *p)
+{
+ /* The prefork open_logs phase must run before the core's, or stderr
+ * will be redirected to a file, and the messages won't print to the
+ * console.
+ */
+ static const char *const aszSucc[] = {"core.c", NULL};
+
+#ifdef AUX3
+ (void) set42sig();
+#endif
+
+ ap_hook_open_logs(prefork_open_logs, NULL, aszSucc, APR_HOOK_MIDDLE);
+ /* we need to set the MPM state before other pre-config hooks use MPM query
+ * to retrieve it, so register as REALLY_FIRST
+ */
+ ap_hook_pre_config(prefork_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
+}
+
+static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_daemons_to_start = atoi(arg);
+ return NULL;
+}
+
+static const char *set_min_free_servers(cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_daemons_min_free = atoi(arg);
+ if (ap_daemons_min_free <= 0) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: detected MinSpareServers set to non-positive.");
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Resetting to 1 to avoid almost certain Apache failure.");
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Please read the documentation.");
+ ap_daemons_min_free = 1;
+ }
+
+ return NULL;
+}
+
+static const char *set_max_free_servers(cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_daemons_max_free = atoi(arg);
+ return NULL;
+}
+
+static const char *set_max_clients (cmd_parms *cmd, void *dummy, const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_daemons_limit = atoi(arg);
+ if (ap_daemons_limit > server_limit) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: MaxClients of %d exceeds ServerLimit value "
+ "of %d servers,", ap_daemons_limit, server_limit);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " lowering MaxClients to %d. To increase, please "
+ "see the ServerLimit", server_limit);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " directive.");
+ ap_daemons_limit = server_limit;
+ }
+ else if (ap_daemons_limit < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require MaxClients > 0, setting to 1");
+ ap_daemons_limit = 1;
+ }
+ return NULL;
+}
+
+static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg)
+{
+ int tmp_server_limit;
+
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ tmp_server_limit = atoi(arg);
+ /* you cannot change ServerLimit across a restart; ignore
+ * any such attempts
+ */
+ if (first_server_limit &&
+ tmp_server_limit != server_limit) {
+ /* how do we log a message? the error log is a bit bucket at this
+ * point; we'll just have to set a flag so that ap_mpm_run()
+ * logs a warning later
+ */
+ changed_limit_at_restart = 1;
+ return NULL;
+ }
+ server_limit = tmp_server_limit;
+
+ if (server_limit > MAX_SERVER_LIMIT) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: ServerLimit of %d exceeds compile time limit "
+ "of %d servers,", server_limit, MAX_SERVER_LIMIT);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " lowering ServerLimit to %d.", MAX_SERVER_LIMIT);
+ server_limit = MAX_SERVER_LIMIT;
+ }
+ else if (server_limit < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require ServerLimit > 0, setting to 1");
+ server_limit = 1;
+ }
+ return NULL;
+}
+
+static const command_rec prefork_cmds[] = {
+UNIX_DAEMON_COMMANDS,
+LISTEN_COMMANDS,
+AP_INIT_TAKE1("StartServers", set_daemons_to_start, NULL, RSRC_CONF,
+ "Number of child processes launched at server startup"),
+AP_INIT_TAKE1("MinSpareServers", set_min_free_servers, NULL, RSRC_CONF,
+ "Minimum number of idle children, to handle request spikes"),
+AP_INIT_TAKE1("MaxSpareServers", set_max_free_servers, NULL, RSRC_CONF,
+ "Maximum number of idle children"),
+AP_INIT_TAKE1("MaxClients", set_max_clients, NULL, RSRC_CONF,
+ "Maximum number of children alive at the same time"),
+AP_INIT_TAKE1("ServerLimit", set_server_limit, NULL, RSRC_CONF,
+ "Maximum value of MaxClients for this run of Apache"),
+AP_GRACEFUL_SHUTDOWN_TIMEOUT_COMMAND,
+{ NULL }
+};
+
+module AP_MODULE_DECLARE_DATA mpm_prefork_module = {
+ MPM20_MODULE_STUFF,
+ ap_mpm_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 */
+ prefork_cmds, /* command apr_table_t */
+ prefork_hooks, /* register hooks */
+};
diff --git a/server/mpm/winnt/Win9xConHook.c b/server/mpm/winnt/Win9xConHook.c
new file mode 100644
index 00000000..0dbcf80d
--- /dev/null
+++ b/server/mpm/winnt/Win9xConHook.c
@@ -0,0 +1,697 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifdef WIN32
+
+/*
+ * Win9xConHook.dll - a hook proc to clean up Win95/98 console behavior.
+ *
+ * It is well(?) documented by Microsoft that the Win9x HandlerRoutine
+ * hooked by the SetConsoleCtrlHandler never receives the CTRL_CLOSE_EVENT,
+ * CTRL_LOGOFF_EVENT or CTRL_SHUTDOWN_EVENT signals.
+ *
+ * It is possible to have a second window to monitor the WM_ENDSESSION
+ * message, but the close button still fails..
+ *
+ * There is a 16bit polling method for the close window option, but this
+ * is CPU intensive and requires thunking.
+ *
+ * Attempts to subclass the 'tty' console fail, since that message thread
+ * is actually owned by the 16 bit winoldap.mod process, although the
+ * window reports it is owned by the process/thread of the console app.
+ *
+ * Win9xConHook is thunks the WM_CLOSE and WM_ENDSESSION messages,
+ * first through a window hook procedure in the winoldap context, into
+ * a subclass WndProc, and on to a second hidden monitor window in the
+ * console application's context that dispatches them to the console app's
+ * registered HandlerRoutine.
+ */
+
+/* This debugging define turns on output to COM1, although you better init
+ * the port first (even using hyperterm). It's the only way to catch the
+ * goings on within system logoff/shutdown.
+ * #define DBG 1
+ */
+
+#include <windows.h>
+
+/* Variables used within any process context:
+ * hookwndmsg is a shared message to send Win9xConHook signals
+ * origwndprop is a wndprop atom to store the orig wndproc of the tty
+ * hookwndprop is a wndprop atom to store the hwnd of the hidden child
+ * is_service reminds us to unmark this process on the way out
+ */
+static UINT hookwndmsg = 0;
+static LPCTSTR origwndprop;
+static LPCTSTR hookwndprop;
+static BOOL is_service = 0;
+//static HMODULE hmodThis = NULL;
+
+/* Variables used within the tty processes' context:
+ * is_tty flags this process; -1 == unknown, 1 == if tty, 0 == if not
+ * hw_tty is the handle of the top level tty in this process context
+ * is_subclassed is toggled to assure DllMain removes the subclass on unload
+ * hmodLock is there to try and prevent this dll from being unloaded if the
+ * hook is removed while we are subclassed
+ */
+static int is_tty = -1;
+static HWND hwtty = NULL;
+static BOOL is_subclassed = 0;
+
+// This simply causes a gpfault the moment it tries to FreeLibrary within
+// the subclass procedure ... not good.
+//static HMODULE hmodLock = NULL;
+
+/* Variables used within the service or console app's context:
+ * hmodHook is the instance handle of this module for registering the hooks
+ * hhkGetMessage is the hook handle for catching Posted messages
+ * hhkGetMessage is the hook handle for catching Sent messages
+ * monitor_hwnd is the invisible window that handles our tty messages
+ * the tty_info strucure is used to pass args into the hidden window's thread
+ */
+static HMODULE hmodHook = NULL;
+static HHOOK hhkGetMessage;
+//static HHOOK hhkCallWndProc;
+static HWND monitor_hwnd = NULL;
+
+typedef struct {
+ PHANDLER_ROUTINE phandler;
+ HINSTANCE instance;
+ HWND parent;
+ INT type;
+ LPCSTR name;
+} tty_info;
+
+/* These are the GetWindowLong offsets for the hidden window's internal info
+ * gwltty_phandler is the address of the app's HandlerRoutine
+ * gwltty_ttywnd is the tty this hidden window will handle messages from
+ */
+#define gwltty_phandler 0
+#define gwltty_ttywnd 4
+
+/* Forward declaration prototypes for internal functions
+ */
+static BOOL CALLBACK EnumttyWindow(HWND wnd, LPARAM retwnd);
+static LRESULT WINAPI RegisterWindows9xService(BOOL set_service);
+static LRESULT CALLBACK ttyConsoleCtrlWndProc(HWND hwnd, UINT msg,
+ WPARAM wParam, LPARAM lParam);
+static DWORD WINAPI ttyConsoleCtrlThread(LPVOID tty);
+static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
+ WPARAM wParam, LPARAM lParam);
+static int HookProc(int hc, HWND *hwnd, UINT *msg,
+ WPARAM *wParam, LPARAM *lParam);
+#ifdef DBG
+static VOID DbgPrintf(LPTSTR fmt, ...);
+#endif
+
+
+/* DllMain is invoked by every process in the entire system that is hooked
+ * by our window hooks, notably the tty processes' context, and by the user
+ * who wants tty messages (the app). Keep it light and simple.
+ */
+BOOL __declspec(dllexport) APIENTRY DllMain(HINSTANCE hModule, ULONG ulReason,
+ LPVOID pctx)
+{
+ if (ulReason == DLL_PROCESS_ATTACH)
+ {
+ //hmodThis = hModule;
+ if (!hookwndmsg) {
+ origwndprop = MAKEINTATOM(GlobalAddAtom("Win9xConHookOrigProc"));
+ hookwndprop = MAKEINTATOM(GlobalAddAtom("Win9xConHookThunkWnd"));
+ hookwndmsg = RegisterWindowMessage("Win9xConHookMsg");
+ }
+#ifdef DBG
+// DbgPrintf("H ProcessAttach:%8.8x\r\n",
+// GetCurrentProcessId());
+#endif
+ }
+ else if ( ulReason == DLL_PROCESS_DETACH )
+ {
+#ifdef DBG
+// DbgPrintf("H ProcessDetach:%8.8x\r\n", GetCurrentProcessId());
+#endif
+ if (monitor_hwnd)
+ SendMessage(monitor_hwnd, WM_DESTROY, 0, 0);
+ if (is_subclassed)
+ SendMessage(hwtty, hookwndmsg, 0, (LPARAM)hwtty);
+ if (hmodHook)
+ {
+ if (hhkGetMessage) {
+ UnhookWindowsHookEx(hhkGetMessage);
+ hhkGetMessage = NULL;
+ }
+ //if (hhkCallWndProc) {
+ // UnhookWindowsHookEx(hhkCallWndProc);
+ // hhkCallWndProc = NULL;
+ //}
+ FreeLibrary(hmodHook);
+ hmodHook = NULL;
+ }
+ if (is_service)
+ RegisterWindows9xService(FALSE);
+ if (hookwndmsg) {
+ GlobalDeleteAtom((ATOM)origwndprop);
+ GlobalDeleteAtom((ATOM)hookwndprop);
+ hookwndmsg = 0;
+ }
+ }
+ return TRUE;
+}
+
+
+/* This group of functions are provided for the service/console app
+ * to register itself a HandlerRoutine to accept tty or service messages
+ */
+
+
+/* Exported function that creates a Win9x 'service' via a hidden window,
+ * that notifies the process via the HandlerRoutine messages.
+ */
+BOOL __declspec(dllexport) WINAPI Windows9xServiceCtrlHandler(
+ PHANDLER_ROUTINE phandler,
+ LPCSTR name)
+{
+ /* If we have not yet done so */
+ FreeConsole();
+
+ if (name)
+ {
+ DWORD tid;
+ HANDLE hThread;
+ /* NOTE: this is static so the module can continue to
+ * access these args while we go on to other things
+ */
+ static tty_info tty;
+ tty.instance = GetModuleHandle(NULL);
+ tty.phandler = phandler;
+ tty.parent = NULL;
+ tty.name = name;
+ tty.type = 2;
+ RegisterWindows9xService(TRUE);
+ hThread = CreateThread(NULL, 0, ttyConsoleCtrlThread,
+ (LPVOID)&tty, 0, &tid);
+ if (hThread)
+ {
+ CloseHandle(hThread);
+ return TRUE;
+ }
+ }
+ else /* remove */
+ {
+ if (monitor_hwnd)
+ SendMessage(monitor_hwnd, WM_DESTROY, 0, 0);
+ RegisterWindows9xService(FALSE);
+ return TRUE;
+ }
+ return FALSE;
+}
+
+
+/* Exported function that registers a HandlerRoutine to accept missing
+ * Win9x CTRL_EVENTs from the tty window, as NT does without a hassle.
+ * If add is 1 or 2, register the handler, if 2 also mark it as a service.
+ * If add is 0 deregister the handler, and unmark if a service
+ */
+BOOL __declspec(dllexport) WINAPI FixConsoleCtrlHandler(
+ PHANDLER_ROUTINE phandler,
+ INT add)
+{
+ HWND parent;
+
+ if (add)
+ {
+ HANDLE hThread;
+ DWORD tid;
+ /* NOTE: this is static so the module can continue to
+ * access these args while we go on to other things
+ */
+ static tty_info tty;
+ EnumWindows(EnumttyWindow, (LPARAM)&parent);
+ if (!parent) {
+#ifdef DBG
+ DbgPrintf("A EnumttyWindow failed (%d)\r\n", GetLastError());
+#endif
+ return FALSE;
+ }
+ tty.instance = GetModuleHandle(NULL);
+ tty.phandler = phandler;
+ tty.parent = parent;
+ tty.type = add;
+ if (add == 2) {
+ tty.name = "ttyService";
+ RegisterWindows9xService(TRUE);
+ }
+ else
+ tty.name = "ttyMonitor";
+ hThread = CreateThread(NULL, 0, ttyConsoleCtrlThread,
+ (LPVOID)&tty, 0, &tid);
+ if (!hThread)
+ return FALSE;
+ CloseHandle(hThread);
+ hmodHook = LoadLibrary("Win9xConHook.dll");
+ if (hmodHook)
+ {
+ hhkGetMessage = SetWindowsHookEx(WH_GETMESSAGE,
+ (HOOKPROC)GetProcAddress(hmodHook, "GetMsgProc"), hmodHook, 0);
+ //hhkCallWndProc = SetWindowsHookEx(WH_CALLWNDPROC,
+ // (HOOKPROC)GetProcAddress(hmodHook, "CallWndProc"), hmodHook, 0);
+ }
+ return TRUE;
+ }
+ else /* remove */
+ {
+ if (monitor_hwnd) {
+ SendMessage(monitor_hwnd, WM_DESTROY, 0, 0);
+ }
+ if (hmodHook)
+ {
+ if (hhkGetMessage) {
+ UnhookWindowsHookEx(hhkGetMessage);
+ hhkGetMessage = NULL;
+ }
+ //if (hhkCallWndProc) {
+ // UnhookWindowsHookEx(hhkCallWndProc);
+ // hhkCallWndProc = NULL;
+ //}
+ FreeLibrary(hmodHook);
+ hmodHook = NULL;
+ }
+ if (is_service)
+ RegisterWindows9xService(FALSE);
+ return TRUE;
+ }
+ return FALSE;
+}
+
+
+/* The following internal helpers are only used within the app's context
+ */
+
+/* ttyConsoleCreateThread is the process that runs within the user app's
+ * context. It creates and pumps the messages of a hidden monitor window,
+ * watching for messages from the system, or the associated subclassed tty
+ * window. Things can happen in our context that can't be done from the
+ * tty's context, and visa versa, so the subclass procedure and this hidden
+ * window work together to make it all happen.
+ */
+static DWORD WINAPI ttyConsoleCtrlThread(LPVOID tty)
+{
+ WNDCLASS wc;
+ MSG msg;
+ wc.style = CS_GLOBALCLASS;
+ wc.lpfnWndProc = ttyConsoleCtrlWndProc;
+ wc.cbClsExtra = 0;
+ wc.cbWndExtra = 8;
+ wc.hInstance = NULL;
+ wc.hIcon = NULL;
+ wc.hCursor = NULL;
+ wc.hbrBackground = NULL;
+ wc.lpszMenuName = NULL;
+ if (((tty_info*)tty)->parent)
+ wc.lpszClassName = "ttyConHookChild";
+ else
+ wc.lpszClassName = "ApacheWin95ServiceMonitor";
+
+ if (!RegisterClass(&wc)) {
+#ifdef DBG
+ DbgPrintf("A proc %8.8x Error creating class %s (%d)\r\n",
+ GetCurrentProcessId(), wc.lpszClassName, GetLastError());
+#endif
+ return 0;
+ }
+
+ /* Create an invisible window */
+ monitor_hwnd = CreateWindow(wc.lpszClassName, ((tty_info*)tty)->name,
+ WS_OVERLAPPED & ~WS_VISIBLE,
+ CW_USEDEFAULT, CW_USEDEFAULT,
+ CW_USEDEFAULT, CW_USEDEFAULT,
+ NULL, NULL,
+ ((tty_info*)tty)->instance, tty);
+
+ if (!monitor_hwnd) {
+#ifdef DBG
+ DbgPrintf("A proc %8.8x Error creating window %s %s (%d)\r\n",
+ GetCurrentProcessId(), wc.lpszClassName,
+ ((tty_info*)tty)->name, GetLastError());
+#endif
+ return 0;
+ }
+
+ while (GetMessage(&msg, NULL, 0, 0))
+ {
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
+
+ /* Tag again as deleted, just in case we missed WM_DESTROY */
+ monitor_hwnd = NULL;
+ return 0;
+}
+
+
+/* This is the WndProc procedure for our invisible window.
+ * When our subclasssed tty window receives the WM_CLOSE, WM_ENDSESSION,
+ * or WM_QUERYENDSESSION messages, the message is dispatched to our hidden
+ * window (this message process), and we call the installed HandlerRoutine
+ * that was registered by the app.
+ */
+static LRESULT CALLBACK ttyConsoleCtrlWndProc(HWND hwnd, UINT msg,
+ WPARAM wParam, LPARAM lParam)
+{
+ if (msg == WM_CREATE)
+ {
+ tty_info *tty = (tty_info*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
+ SetWindowLong(hwnd, gwltty_phandler, (LONG)tty->phandler);
+ SetWindowLong(hwnd, gwltty_ttywnd, (LONG)tty->parent);
+#ifdef DBG
+ DbgPrintf("A proc %8.8x created %8.8x %s for tty wnd %8.8x\r\n",
+ GetCurrentProcessId(), hwnd,
+ tty->name, tty->parent);
+#endif
+ if (tty->parent) {
+ SetProp(tty->parent, hookwndprop, hwnd);
+ PostMessage(tty->parent, hookwndmsg,
+ tty->type, (LPARAM)tty->parent);
+ }
+ return 0;
+ }
+ else if (msg == WM_DESTROY)
+ {
+ HWND parent = (HWND)GetWindowLong(hwnd, gwltty_ttywnd);
+#ifdef DBG
+ DbgPrintf("A proc %8.8x destroyed %8.8x ttyConHookChild\r\n",
+ GetCurrentProcessId(), hwnd);
+#endif
+ if (parent) {
+ RemoveProp(parent, hookwndprop);
+ SendMessage(parent, hookwndmsg, 0, (LPARAM)parent);
+ }
+ monitor_hwnd = NULL;
+ }
+ else if (msg == WM_CLOSE)
+ {
+ PHANDLER_ROUTINE phandler =
+ (PHANDLER_ROUTINE)GetWindowLong(hwnd, gwltty_phandler);
+ LRESULT rv = phandler(CTRL_CLOSE_EVENT);
+#ifdef DBG
+ DbgPrintf("A proc %8.8x invoked CTRL_CLOSE_EVENT "
+ "returning %d\r\n",
+ GetCurrentProcessId(), rv);
+#endif
+ if (rv)
+ return !rv;
+ }
+ else if ((msg == WM_QUERYENDSESSION) || (msg == WM_ENDSESSION))
+ {
+ if (lParam & ENDSESSION_LOGOFF)
+ {
+ PHANDLER_ROUTINE phandler =
+ (PHANDLER_ROUTINE)GetWindowLong(hwnd, gwltty_phandler);
+ LRESULT rv = phandler(CTRL_LOGOFF_EVENT);
+#ifdef DBG
+ DbgPrintf("A proc %8.8x invoked CTRL_LOGOFF_EVENT "
+ "returning %d\r\n",
+ GetCurrentProcessId(), rv);
+#endif
+ if (rv)
+ return ((msg == WM_QUERYENDSESSION) ? rv : !rv);
+ }
+ else
+ {
+ PHANDLER_ROUTINE phandler =
+ (PHANDLER_ROUTINE)GetWindowLong(hwnd, gwltty_phandler);
+ LRESULT rv = phandler(CTRL_SHUTDOWN_EVENT);
+#ifdef DBG
+ DbgPrintf("A proc %8.8x invoked CTRL_SHUTDOWN_EVENT "
+ "returning %d\r\n", GetCurrentProcessId(), rv);
+#endif
+ if (rv)
+ return ((msg == WM_QUERYENDSESSION) ? rv : !rv);
+ }
+ }
+ return (DefWindowProc(hwnd, msg, wParam, lParam));
+}
+
+
+/* The following internal helpers are invoked by the hooked tty and our app
+ */
+
+
+/* Register or deregister the current process as a Windows9x style service.
+ * Experience shows this call is ignored across processes, so the second
+ * arg to RegisterServiceProcess (process group id) is effectively useless.
+ */
+static LRESULT WINAPI RegisterWindows9xService(BOOL set_service)
+{
+ static HINSTANCE hkernel;
+ static DWORD (WINAPI *register_service_process)(DWORD, DWORD) = NULL;
+ BOOL rv;
+
+ if (set_service == is_service)
+ return 1;
+
+#ifdef DBG
+ DbgPrintf("R %s proc %8.8x as a service\r\n",
+ set_service ? "installing" : "removing",
+ GetCurrentProcessId());
+#endif
+
+ if (!register_service_process)
+ {
+ /* Obtain a handle to the kernel library */
+ hkernel = LoadLibrary("KERNEL32.DLL");
+ if (!hkernel)
+ return 0;
+
+ /* Find the RegisterServiceProcess function */
+ register_service_process = (DWORD (WINAPI *)(DWORD, DWORD))
+ GetProcAddress(hkernel, "RegisterServiceProcess");
+ if (register_service_process == NULL) {
+ FreeLibrary(hkernel);
+ return 0;
+ }
+ }
+
+ /* Register this process as a service */
+ rv = register_service_process(0, set_service != FALSE);
+ if (rv)
+ is_service = set_service;
+
+ if (!is_service)
+ {
+ /* Unload the kernel library */
+ FreeLibrary(hkernel);
+ register_service_process = NULL;
+ }
+ return rv;
+}
+
+
+/*
+ * This function only works when this process is the active process
+ * (e.g. once it is running a child process, it can no longer determine
+ * which console window is its own.)
+ */
+static BOOL CALLBACK EnumttyWindow(HWND wnd, LPARAM retwnd)
+{
+ char tmp[8];
+ if (GetClassName(wnd, tmp, sizeof(tmp)) && !strcmp(tmp, "tty"))
+ {
+ DWORD wndproc, thisproc = GetCurrentProcessId();
+ GetWindowThreadProcessId(wnd, &wndproc);
+ if (wndproc == thisproc) {
+ *((HWND*)retwnd) = wnd;
+ return FALSE;
+ }
+ }
+ return TRUE;
+}
+
+
+/* The remaining code all executes --in the tty's own process context--
+ *
+ * That means special attention must be paid to what it's doing...
+ */
+
+/* Subclass message process for the tty window
+ *
+ * This code -handles- WM_CLOSE, WM_ENDSESSION and WM_QUERYENDSESSION
+ * by dispatching them to the window identified by the hookwndprop
+ * property atom set against our window. Messages are then dispatched
+ * to origwndprop property atom we set against the window when we
+ * injected this subclass. This trick did not work with simply a hook.
+ */
+static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
+ WPARAM wParam, LPARAM lParam)
+{
+ WNDPROC origproc = (WNDPROC) GetProp(hwnd, origwndprop);
+ if (!origproc)
+ return 0;
+
+ if (msg == WM_NCDESTROY
+ || (msg == hookwndmsg && !LOWORD(wParam) && (HWND)lParam == hwnd))
+ {
+ if (is_subclassed) {
+#ifdef DBG
+ DbgPrintf("W proc %08x hwnd:%08x Subclass removed\r\n",
+ GetCurrentProcessId(), hwnd);
+#endif
+ if (is_service)
+ RegisterWindows9xService(FALSE);
+ SetWindowLong(hwnd, GWL_WNDPROC, (LONG)origproc);
+ RemoveProp(hwnd, origwndprop);
+ RemoveProp(hwnd, hookwndprop);
+ is_subclassed = FALSE;
+ //if (hmodLock)
+ // FreeLibrary(hmodLock);
+ //hmodLock = NULL;
+ }
+ }
+ else if (msg == WM_CLOSE || msg == WM_ENDSESSION
+ || msg == WM_QUERYENDSESSION)
+ {
+ HWND child = (HWND)GetProp(hwnd, hookwndprop);
+ if (child) {
+#ifdef DBG
+ DbgPrintf("W proc %08x hwnd:%08x forwarded msg:%d\r\n",
+ GetCurrentProcessId(), hwnd, msg);
+#endif
+ return SendMessage(child, msg, wParam, lParam);
+ }
+ }
+ return CallWindowProc(origproc, hwnd, msg, wParam, lParam);
+}
+
+
+/* HookProc, once installed, is responsible for subclassing the system
+ * tty windows. It generally does nothing special itself, since
+ * research indicates that it cannot deal well with the messages we are
+ * interested in, that is, WM_CLOSE, WM_QUERYSHUTDOWN and WM_SHUTDOWN
+ * of the tty process.
+ *
+ * Respond and subclass only when a WM_NULL is received by the window.
+ */
+int HookProc(int hc, HWND *hwnd, UINT *msg, WPARAM *wParam, LPARAM *lParam)
+{
+ if (is_tty == -1 && *hwnd)
+ {
+ char ttybuf[8];
+ HWND htty;
+ hwtty = *hwnd;
+ while (htty = GetParent(hwtty))
+ hwtty = htty;
+ is_tty = (GetClassName(hwtty, ttybuf, sizeof(ttybuf))
+ && !strcmp(ttybuf, "tty"));
+#ifdef DBG
+ if (is_tty)
+ DbgPrintf("H proc %08x tracking hwnd %08x\r\n",
+ GetCurrentProcessId(), hwtty);
+#endif
+ }
+
+ if (*msg == hookwndmsg && *wParam && *lParam == (LPARAM)hwtty && is_tty)
+ {
+ WNDPROC origproc = (WNDPROC)GetWindowLong(hwtty, GWL_WNDPROC);
+ //char myname[MAX_PATH];
+ //if (GetModuleFileName(hmodThis, myname, sizeof(myname)))
+ // hmodLock = LoadLibrary(myname);
+ SetProp(hwtty, origwndprop, origproc);
+ SetWindowLong(hwtty, GWL_WNDPROC, (LONG)WndProc);
+ is_subclassed = TRUE;
+#ifdef DBG
+ DbgPrintf("H proc %08x hwnd:%08x Subclassed\r\n",
+ GetCurrentProcessId(), hwtty);
+#endif
+ if (LOWORD(*wParam) == 2)
+ RegisterWindows9xService(TRUE);
+ }
+
+ return -1;
+}
+
+
+/*
+ * PostMessage Hook:
+ */
+LRESULT __declspec(dllexport) CALLBACK GetMsgProc(INT hc, WPARAM wParam,
+ LPARAM lParam)
+{
+ PMSG pmsg;
+
+ pmsg = (PMSG)lParam;
+
+ if (pmsg) {
+ int rv = HookProc(hc, &pmsg->hwnd, &pmsg->message,
+ &pmsg->wParam, &pmsg->lParam);
+ if (rv != -1)
+ return rv;
+ }
+ /*
+ * CallNextHookEx apparently ignores the hhook argument, so pass NULL
+ */
+ return CallNextHookEx(NULL, hc, wParam, lParam);
+}
+
+
+/*
+ * SendMessage Hook:
+ */
+LRESULT __declspec(dllexport) CALLBACK CallWndProc(INT hc, WPARAM wParam,
+ LPARAM lParam)
+{
+ PCWPSTRUCT pcwps = (PCWPSTRUCT)lParam;
+
+ if (pcwps) {
+ int rv = HookProc(hc, &pcwps->hwnd, &pcwps->message,
+ &pcwps->wParam, &pcwps->lParam);
+ if (rv != -1)
+ return rv;
+ }
+ /*
+ * CallNextHookEx apparently ignores the hhook argument, so pass NULL
+ */
+ return CallNextHookEx(NULL, hc, wParam, lParam);
+}
+
+
+#ifdef DBG
+VOID DbgPrintf(
+ LPTSTR fmt,
+ ...
+ )
+{
+ static HANDLE mutex;
+ va_list marker;
+ TCHAR szBuf[256];
+ DWORD t;
+ HANDLE gDbgOut;
+
+ va_start(marker, fmt);
+ wvsprintf(szBuf, fmt, marker);
+ va_end(marker);
+
+ if (!mutex)
+ mutex = CreateMutex(NULL, FALSE, "Win9xConHookDbgOut");
+ WaitForSingleObject(mutex, INFINITE);
+ gDbgOut = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0,
+ NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
+ WriteFile(gDbgOut, szBuf, strlen(szBuf), &t, NULL);
+ CloseHandle(gDbgOut);
+ ReleaseMutex(mutex);
+}
+#endif
+
+#endif /* WIN32 */
diff --git a/server/mpm/winnt/Win9xConHook.def b/server/mpm/winnt/Win9xConHook.def
new file mode 100644
index 00000000..85ec1664
--- /dev/null
+++ b/server/mpm/winnt/Win9xConHook.def
@@ -0,0 +1,10 @@
+LIBRARY Win9xConHook
+
+EXETYPE WINDOWS
+
+EXPORTS
+ DllMain
+ GetMsgProc
+ CallWndProc
+ FixConsoleCtrlHandler
+ Windows9xServiceCtrlHandler
diff --git a/server/mpm/winnt/Win9xConHook.dsp b/server/mpm/winnt/Win9xConHook.dsp
new file mode 100644
index 00000000..77cbacfc
--- /dev/null
+++ b/server/mpm/winnt/Win9xConHook.dsp
@@ -0,0 +1,103 @@
+# Microsoft Developer Studio Project File - Name="Win9xConHook" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
+
+CFG=Win9xConHook - Win32 Release
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE
+!MESSAGE NMAKE /f "Win9xConHook.mak".
+!MESSAGE
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "Win9xConHook.mak" CFG="Win9xConHook - Win32 Release"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "Win9xConHook - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
+!MESSAGE "Win9xConHook - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+MTL=midl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "Win9xConHook - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir ".\Release"
+# PROP BASE Intermediate_Dir ".\Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "Release"
+# PROP Intermediate_Dir "Release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /MD /W3 /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /FD /c
+# ADD CPP /nologo /MD /W3 /O2 /D "NDEBUG" /D "WIN32" /D "_WINDOWS" /D "SHARED_MODULE" /Fd"Release\Win9xConHook" /FD /c
+# ADD BASE MTL /nologo /D "NDEBUG" /win32
+# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD BASE RSC /l 0x809 /d "NDEBUG"
+# ADD RSC /l 0x809 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib /nologo /subsystem:windows /dll /incremental:no /base:"0x1c0f0000"
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib /nologo /subsystem:windows /dll /incremental:no /base:"0x1c0f0000" /opt:ref
+
+!ELSEIF "$(CFG)" == "Win9xConHook - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "Debug"
+# PROP Intermediate_Dir "Debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /MDd /W3 /EHsc /Zi /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /FD /c
+# ADD CPP /nologo /MDd /W3 /EHsc /Zi /Od /D "_DEBUG" /D "WIN32" /D "_WINDOWS" /D "SHARED_MODULE" /Fd"Debug\Win9xConHook" /FD /c
+# ADD BASE MTL /nologo /D "_DEBUG" /win32
+# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
+# ADD BASE RSC /l 0x809 /d "_DEBUG"
+# ADD RSC /l 0x809 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib /nologo /subsystem:windows /dll /incremental:no /debug /base:"0x1c0f0000"
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib /nologo /subsystem:windows /dll /incremental:no /debug /base:"0x1c0f0000"
+
+!ENDIF
+
+# Begin Target
+
+# Name "Win9xConHook - Win32 Release"
+# Name "Win9xConHook - Win32 Debug"
+# Begin Source File
+
+SOURCE=.\Win9xConHook.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\Win9xConHook.def
+# End Source File
+# Begin Source File
+
+SOURCE=.\Win9xConHook.h
+# End Source File
+# End Target
+# End Project
diff --git a/server/mpm/winnt/Win9xConHook.h b/server/mpm/winnt/Win9xConHook.h
new file mode 100644
index 00000000..8b42da57
--- /dev/null
+++ b/server/mpm/winnt/Win9xConHook.h
@@ -0,0 +1,66 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file Win9xConHook.h
+ * @brief ???
+ *
+ * @addtogroup APACHE_MPM_WINNT
+ * @{
+ */
+
+#ifndef AP_WIN9XCONHOOK_H
+#define AP_WIN9XCONHOOK_H
+
+#ifdef WIN32
+
+/* Windows9xServiceCtrlHandler registers a handler routine, frees the
+ * console window, and registers this process as a service in Win9x.
+ * It creats a hidden window of class "ApacheWin95ServiceMonitor"
+ * and titled by the name passed, which passes the WM_SHUTDOWN message
+ * through the given HandlerRoutine's CTRL_SHUTDOWN event.
+ * Call with name of NULL to remove the Service handler.
+ */
+BOOL WINAPI Windows9xServiceCtrlHandler(PHANDLER_ROUTINE phandler, LPCSTR name);
+
+
+/* FixConsoleControlHandler registers a handler routine with the
+ * Win9xConHook.dll, creating a hidden window and forwarding the
+ * WM_ENDSESSION and WM_CLOSE messages to the given HandlerRoutine
+ * as CTRL_SHUTDOWN_EVENT, CTRL_LOGOFF_EVENT and CTRL_CLOSE_EVENT.
+ * The application should still use SetConsoleCtrlHandler to grab
+ * the CTRL_BREAK_EVENT and CTRL_C_EVENT, if desired.
+ */
+BOOL WINAPI FixConsoleCtrlHandler(PHANDLER_ROUTINE phandler, BOOL add);
+
+
+/*
+ * Exported PostMessage Hook, never use this directly:
+ *
+ * LRESULT CALLBACK GetMsgProc(INT hc, WPARAM wParam, LPARAM lParam);
+ */
+
+
+/*
+ * Exported SendMessage Hook, never use this directly:
+ *
+ * LRESULT CALLBACK CallWndProc(INT hc, WPARAM wParam, LPARAM lParam);
+ */
+
+#endif /* WIN32 */
+
+#endif AP_WIN9XCONHOOK_H
+/** @} */
diff --git a/server/mpm/winnt/child.c b/server/mpm/winnt/child.c
new file mode 100644
index 00000000..8996bfd8
--- /dev/null
+++ b/server/mpm/winnt/child.c
@@ -0,0 +1,1157 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifdef WIN32
+
+#define CORE_PRIVATE
+#include "httpd.h"
+#include "http_main.h"
+#include "http_log.h"
+#include "http_config.h" /* for read_config */
+#include "http_core.h" /* for get_remote_host */
+#include "http_connection.h"
+#include "apr_portable.h"
+#include "apr_thread_proc.h"
+#include "apr_getopt.h"
+#include "apr_strings.h"
+#include "apr_lib.h"
+#include "apr_shm.h"
+#include "apr_thread_mutex.h"
+#include "ap_mpm.h"
+#include "ap_config.h"
+#include "ap_listen.h"
+#include "mpm_default.h"
+#include "mpm_winnt.h"
+#include "mpm_common.h"
+#include <malloc.h>
+#include "apr_atomic.h"
+
+/* shared with mpm_winnt.c */
+extern DWORD my_pid;
+
+/* used by parent to signal the child to start and exit */
+/* shared with mpm_winnt.c, but should be private to child.c */
+apr_proc_mutex_t *start_mutex;
+HANDLE exit_event;
+
+/* child_main() should never need to modify is_graceful!?! */
+extern int volatile is_graceful;
+
+/* Queue for managing the passing of COMP_CONTEXTs between
+ * the accept and worker threads.
+ */
+static apr_pool_t *pchild;
+static int shutdown_in_progress = 0;
+static int workers_may_exit = 0;
+static unsigned int g_blocked_threads = 0;
+static HANDLE max_requests_per_child_event;
+
+static apr_thread_mutex_t *child_lock;
+static apr_thread_mutex_t *qlock;
+static PCOMP_CONTEXT qhead = NULL;
+static PCOMP_CONTEXT qtail = NULL;
+static int num_completion_contexts = 0;
+static int max_num_completion_contexts = 0;
+static HANDLE ThreadDispatchIOCP = NULL;
+static HANDLE qwait_event = NULL;
+
+
+void mpm_recycle_completion_context(PCOMP_CONTEXT context)
+{
+ /* Recycle the completion context.
+ * - clear the ptrans pool
+ * - put the context on the queue to be consumed by the accept thread
+ * Note:
+ * context->accept_socket may be in a disconnected but reusable
+ * state so -don't- close it.
+ */
+ if (context) {
+ apr_pool_clear(context->ptrans);
+ context->next = NULL;
+ ResetEvent(context->Overlapped.hEvent);
+ apr_thread_mutex_lock(qlock);
+ if (qtail) {
+ qtail->next = context;
+ } else {
+ qhead = context;
+ SetEvent(qwait_event);
+ }
+ qtail = context;
+ apr_thread_mutex_unlock(qlock);
+ }
+}
+
+PCOMP_CONTEXT mpm_get_completion_context(void)
+{
+ apr_status_t rv;
+ PCOMP_CONTEXT context = NULL;
+
+ while (1) {
+ /* Grab a context off the queue */
+ apr_thread_mutex_lock(qlock);
+ if (qhead) {
+ context = qhead;
+ qhead = qhead->next;
+ if (!qhead)
+ qtail = NULL;
+ } else {
+ ResetEvent(qwait_event);
+ }
+ apr_thread_mutex_unlock(qlock);
+
+ if (!context) {
+ /* We failed to grab a context off the queue, consider allocating
+ * a new one out of the child pool. There may be up to
+ * (ap_threads_per_child + num_listeners) contexts in the system
+ * at once.
+ */
+ if (num_completion_contexts >= max_num_completion_contexts) {
+ /* All workers are busy, need to wait for one */
+ static int reported = 0;
+ if (!reported) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, 0, ap_server_conf,
+ "Server ran out of threads to serve requests. Consider "
+ "raising the ThreadsPerChild setting");
+ reported = 1;
+ }
+
+ /* Wait for a worker to free a context. Once per second, give
+ * the caller a chance to check for shutdown. If the wait
+ * succeeds, get the context off the queue. It must be available,
+ * since there's only one consumer.
+ */
+ rv = WaitForSingleObject(qwait_event, 1000);
+ if (rv == WAIT_OBJECT_0)
+ continue;
+ else /* Hopefully, WAIT_TIMEOUT */
+ return NULL;
+ } else {
+ /* Allocate another context.
+ * Note:
+ * Multiple failures in the next two steps will cause the pchild pool
+ * to 'leak' storage. I don't think this is worth fixing...
+ */
+ apr_allocator_t *allocator;
+
+ apr_thread_mutex_lock(child_lock);
+ context = (PCOMP_CONTEXT) apr_pcalloc(pchild, sizeof(COMP_CONTEXT));
+
+ context->Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+ if (context->Overlapped.hEvent == NULL) {
+ /* Hopefully this is a temporary condition ... */
+ ap_log_error(APLOG_MARK,APLOG_WARNING, apr_get_os_error(), ap_server_conf,
+ "mpm_get_completion_context: CreateEvent failed.");
+
+ apr_thread_mutex_unlock(child_lock);
+ return NULL;
+ }
+
+ /* Create the tranaction pool */
+ apr_allocator_create(&allocator);
+ apr_allocator_max_free_set(allocator, ap_max_mem_free);
+ rv = apr_pool_create_ex(&context->ptrans, pchild, NULL, allocator);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK,APLOG_WARNING, rv, ap_server_conf,
+ "mpm_get_completion_context: Failed to create the transaction pool.");
+ CloseHandle(context->Overlapped.hEvent);
+
+ apr_thread_mutex_unlock(child_lock);
+ return NULL;
+ }
+ apr_allocator_owner_set(allocator, context->ptrans);
+ apr_pool_tag(context->ptrans, "transaction");
+
+ context->accept_socket = INVALID_SOCKET;
+ context->ba = apr_bucket_alloc_create(pchild);
+ apr_atomic_inc32(&num_completion_contexts);
+
+ apr_thread_mutex_unlock(child_lock);
+ break;
+ }
+ } else {
+ /* Got a context from the queue */
+ break;
+ }
+ }
+
+ return context;
+}
+
+apr_status_t mpm_post_completion_context(PCOMP_CONTEXT context,
+ io_state_e state)
+{
+ LPOVERLAPPED pOverlapped;
+ if (context)
+ pOverlapped = &context->Overlapped;
+ else
+ pOverlapped = NULL;
+
+ PostQueuedCompletionStatus(ThreadDispatchIOCP, 0, state, pOverlapped);
+ return APR_SUCCESS;
+}
+
+
+/*
+ * find_ready_listener()
+ * Only used by Win9* and should go away when the win9*_accept() function is
+ * reimplemented using apr_poll().
+ */
+static ap_listen_rec *head_listener;
+
+static APR_INLINE ap_listen_rec *find_ready_listener(fd_set * main_fds)
+{
+ ap_listen_rec *lr;
+ SOCKET nsd;
+
+ lr = head_listener;
+ do {
+ apr_os_sock_get(&nsd, lr->sd);
+ if (FD_ISSET(nsd, main_fds)) {
+ head_listener = lr->next;
+ if (!head_listener) {
+ head_listener = ap_listeners;
+ }
+ return lr;
+ }
+ lr = lr->next;
+ if (!lr) {
+ lr = ap_listeners;
+ }
+ } while (lr != head_listener);
+ return NULL;
+}
+
+
+/* Windows 9x specific code...
+ * Accept processing for on Windows 95/98 uses a producer/consumer queue
+ * model. A single thread accepts connections and queues the accepted socket
+ * to the accept queue for consumption by a pool of worker threads.
+ *
+ * win9x_accept()
+ * The accept threads runs this function, which accepts connections off
+ * the network and calls add_job() to queue jobs to the accept_queue.
+ * add_job()/remove_job()
+ * Add or remove an accepted socket from the list of sockets
+ * connected to clients. allowed_globals.jobmutex protects
+ * against multiple concurrent access to the linked list of jobs.
+ * win9x_get_connection()
+ * Calls remove_job() to pull a job from the accept queue. All the worker
+ * threads block on remove_job.
+ */
+
+typedef struct joblist_s {
+ struct joblist_s *next;
+ SOCKET sock;
+} joblist;
+
+typedef struct globals_s {
+ HANDLE jobsemaphore;
+ joblist *jobhead;
+ joblist *jobtail;
+ apr_thread_mutex_t *jobmutex;
+ int jobcount;
+} globals;
+
+globals allowed_globals = {NULL, NULL, NULL, NULL, 0};
+
+#define MAX_SELECT_ERRORS 100
+
+
+static void add_job(SOCKET sock)
+{
+ joblist *new_job;
+
+ new_job = (joblist *) malloc(sizeof(joblist));
+ if (new_job == NULL) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Ouch! Out of memory in add_job()!");
+ return;
+ }
+ new_job->next = NULL;
+ new_job->sock = sock;
+
+ apr_thread_mutex_lock(allowed_globals.jobmutex);
+
+ if (allowed_globals.jobtail != NULL)
+ allowed_globals.jobtail->next = new_job;
+ allowed_globals.jobtail = new_job;
+ if (!allowed_globals.jobhead)
+ allowed_globals.jobhead = new_job;
+ allowed_globals.jobcount++;
+ ReleaseSemaphore(allowed_globals.jobsemaphore, 1, NULL);
+
+ apr_thread_mutex_unlock(allowed_globals.jobmutex);
+}
+
+
+static SOCKET remove_job(void)
+{
+ joblist *job;
+ SOCKET sock;
+
+ WaitForSingleObject(allowed_globals.jobsemaphore, INFINITE);
+ apr_thread_mutex_lock(allowed_globals.jobmutex);
+
+ if (shutdown_in_progress && !allowed_globals.jobhead) {
+ apr_thread_mutex_unlock(allowed_globals.jobmutex);
+ return (INVALID_SOCKET);
+ }
+ job = allowed_globals.jobhead;
+ ap_assert(job);
+ allowed_globals.jobhead = job->next;
+ if (allowed_globals.jobhead == NULL)
+ allowed_globals.jobtail = NULL;
+ apr_thread_mutex_unlock(allowed_globals.jobmutex);
+ sock = job->sock;
+ free(job);
+
+ return (sock);
+}
+
+
+static unsigned int __stdcall win9x_accept(void * dummy)
+{
+ struct timeval tv;
+ fd_set main_fds;
+ int wait_time = 1;
+ SOCKET csd;
+ SOCKET nsd = INVALID_SOCKET;
+ int count_select_errors = 0;
+ int rc;
+ int clen;
+ ap_listen_rec *lr;
+ struct fd_set listenfds;
+#if APR_HAVE_IPV6
+ struct sockaddr_in6 sa_client;
+#else
+ struct sockaddr_in sa_client;
+#endif
+
+ /* Setup the listeners
+ * ToDo: Use apr_poll()
+ */
+ FD_ZERO(&listenfds);
+ for (lr = ap_listeners; lr; lr = lr->next) {
+ if (lr->sd != NULL) {
+ apr_os_sock_get(&nsd, lr->sd);
+ FD_SET(nsd, &listenfds);
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "Child %d: Listening on port %d.", my_pid, lr->bind_addr->port);
+ }
+ }
+
+ head_listener = ap_listeners;
+
+ while (!shutdown_in_progress) {
+ tv.tv_sec = wait_time;
+ tv.tv_usec = 0;
+ memcpy(&main_fds, &listenfds, sizeof(fd_set));
+
+ /* First parameter of select() is ignored on Windows */
+ rc = select(0, &main_fds, NULL, NULL, &tv);
+
+ if (rc == 0 || (rc == SOCKET_ERROR && APR_STATUS_IS_EINTR(apr_get_netos_error()))) {
+ count_select_errors = 0; /* reset count of errors */
+ continue;
+ }
+ else if (rc == SOCKET_ERROR) {
+ /* A "real" error occurred, log it and increment the count of
+ * select errors. This count is used to ensure we don't go into
+ * a busy loop of continuous errors.
+ */
+ ap_log_error(APLOG_MARK, APLOG_INFO, apr_get_netos_error(), ap_server_conf,
+ "select failed with error %d", apr_get_netos_error());
+ count_select_errors++;
+ if (count_select_errors > MAX_SELECT_ERRORS) {
+ shutdown_in_progress = 1;
+ ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_netos_error(), ap_server_conf,
+ "Too many errors in select loop. Child process exiting.");
+ break;
+ }
+ } else {
+ ap_listen_rec *lr;
+
+ lr = find_ready_listener(&main_fds);
+ if (lr != NULL) {
+ /* fetch the native socket descriptor */
+ apr_os_sock_get(&nsd, lr->sd);
+ }
+ }
+
+ do {
+ clen = sizeof(sa_client);
+ csd = accept(nsd, (struct sockaddr *) &sa_client, &clen);
+ } while (csd < 0 && APR_STATUS_IS_EINTR(apr_get_netos_error()));
+
+ if (csd < 0) {
+ if (APR_STATUS_IS_ECONNABORTED(apr_get_netos_error())) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_netos_error(), ap_server_conf,
+ "accept: (client socket)");
+ }
+ }
+ else {
+ add_job(csd);
+ }
+ }
+ SetEvent(exit_event);
+ return 0;
+}
+
+
+static PCOMP_CONTEXT win9x_get_connection(PCOMP_CONTEXT context)
+{
+ apr_os_sock_info_t sockinfo;
+ int len, salen;
+#if APR_HAVE_IPV6
+ salen = sizeof(struct sockaddr_in6);
+#else
+ salen = sizeof(struct sockaddr_in);
+#endif
+
+
+ if (context == NULL) {
+ /* allocate the completion context and the transaction pool */
+ apr_allocator_t *allocator;
+ apr_thread_mutex_lock(child_lock);
+ context = apr_pcalloc(pchild, sizeof(COMP_CONTEXT));
+ apr_allocator_create(&allocator);
+ apr_allocator_max_free_set(allocator, ap_max_mem_free);
+ apr_pool_create_ex(&context->ptrans, pchild, NULL, allocator);
+ apr_allocator_owner_set(allocator, context->ptrans);
+ apr_pool_tag(context->ptrans, "transaction");
+ context->ba = apr_bucket_alloc_create(pchild);
+ apr_thread_mutex_unlock(child_lock);
+ }
+
+ while (1) {
+ apr_pool_clear(context->ptrans);
+ context->accept_socket = remove_job();
+ if (context->accept_socket == INVALID_SOCKET) {
+ return NULL;
+ }
+ len = salen;
+ context->sa_server = apr_palloc(context->ptrans, len);
+ if (getsockname(context->accept_socket,
+ context->sa_server, &len)== SOCKET_ERROR) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, apr_get_netos_error(), ap_server_conf,
+ "getsockname failed");
+ continue;
+ }
+ len = salen;
+ context->sa_client = apr_palloc(context->ptrans, len);
+ if ((getpeername(context->accept_socket,
+ context->sa_client, &len)) == SOCKET_ERROR) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, apr_get_netos_error(), ap_server_conf,
+ "getpeername failed");
+ memset(&context->sa_client, '\0', sizeof(context->sa_client));
+ }
+ sockinfo.os_sock = &context->accept_socket;
+ sockinfo.local = context->sa_server;
+ sockinfo.remote = context->sa_client;
+ sockinfo.family = context->sa_server->sa_family;
+ sockinfo.type = SOCK_STREAM;
+ apr_os_sock_make(&context->sock, &sockinfo, context->ptrans);
+
+ return context;
+ }
+}
+
+
+/* Windows NT/2000 specific code...
+ * Accept processing for on Windows NT uses a producer/consumer queue
+ * model. An accept thread accepts connections off the network then issues
+ * PostQueuedCompletionStatus() to awake a thread blocked on the ThreadDispatch
+ * IOCompletionPort.
+ *
+ * winnt_accept()
+ * One or more accept threads run in this function, each of which accepts
+ * connections off the network and calls PostQueuedCompletionStatus() to
+ * queue an io completion packet to the ThreadDispatch IOCompletionPort.
+ * winnt_get_connection()
+ * Worker threads block on the ThreadDispatch IOCompletionPort awaiting
+ * connections to service.
+ */
+#define MAX_ACCEPTEX_ERR_COUNT 100
+static unsigned int __stdcall winnt_accept(void *lr_)
+{
+ ap_listen_rec *lr = (ap_listen_rec *)lr_;
+ apr_os_sock_info_t sockinfo;
+ PCOMP_CONTEXT context = NULL;
+ DWORD BytesRead;
+ SOCKET nlsd;
+ int rv, err_count = 0;
+#if APR_HAVE_IPV6
+ SOCKADDR_STORAGE ss_listen;
+ int namelen = sizeof(ss_listen);
+#endif
+
+ apr_os_sock_get(&nlsd, lr->sd);
+
+#if APR_HAVE_IPV6
+ if (getsockname(nlsd, (struct sockaddr *)&ss_listen, &namelen) == SOCKET_ERROR) {
+ ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_netos_error(), ap_server_conf,
+ "winnt_accept: getsockname error on listening socket, is IPv6 available?");
+ return 1;
+ }
+#endif
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "Child %d: Starting thread to listen on port %d.", my_pid, lr->bind_addr->port);
+ while (!shutdown_in_progress) {
+ if (!context) {
+ context = mpm_get_completion_context();
+ if (!context) {
+ /* Temporary resource constraint? */
+ Sleep(0);
+ continue;
+ }
+ }
+
+ /* Create and initialize the accept socket */
+#if APR_HAVE_IPV6
+ if (context->accept_socket == INVALID_SOCKET) {
+ context->accept_socket = socket(ss_listen.ss_family, SOCK_STREAM, IPPROTO_TCP);
+ context->socket_family = ss_listen.ss_family;
+ }
+ else if (context->socket_family != ss_listen.ss_family) {
+ closesocket(context->accept_socket);
+ context->accept_socket = socket(ss_listen.ss_family, SOCK_STREAM, IPPROTO_TCP);
+ context->socket_family = ss_listen.ss_family;
+ }
+
+ if (context->accept_socket == INVALID_SOCKET) {
+ ap_log_error(APLOG_MARK,APLOG_WARNING, apr_get_netos_error(), ap_server_conf,
+ "winnt_accept: Failed to allocate an accept socket. "
+ "Temporary resource constraint? Try again.");
+ Sleep(100);
+ continue;
+ }
+#else
+ if (context->accept_socket == INVALID_SOCKET) {
+ context->accept_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (context->accept_socket == INVALID_SOCKET) {
+ /* Another temporary condition? */
+ ap_log_error(APLOG_MARK,APLOG_WARNING, apr_get_netos_error(), ap_server_conf,
+ "winnt_accept: Failed to allocate an accept socket. "
+ "Temporary resource constraint? Try again.");
+ Sleep(100);
+ continue;
+ }
+ }
+#endif
+ /* AcceptEx on the completion context. The completion context will be
+ * signaled when a connection is accepted.
+ */
+ if (!AcceptEx(nlsd, context->accept_socket,
+ context->buff,
+ 0,
+ PADDED_ADDR_SIZE,
+ PADDED_ADDR_SIZE,
+ &BytesRead,
+ &context->Overlapped)) {
+ rv = apr_get_netos_error();
+ if ((rv == APR_FROM_OS_ERROR(WSAEINVAL)) ||
+ (rv == APR_FROM_OS_ERROR(WSAENOTSOCK))) {
+ /* We can get here when:
+ * 1) the client disconnects early
+ * 2) TransmitFile does not properly recycle the accept socket (typically
+ * because the client disconnected)
+ * 3) there is VPN or Firewall software installed with buggy AcceptEx implementation
+ * 4) the webserver is using a dynamic address that has changed
+ */
+ ++err_count;
+ closesocket(context->accept_socket);
+ context->accept_socket = INVALID_SOCKET;
+ if (err_count > MAX_ACCEPTEX_ERR_COUNT) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf,
+ "Child %d: Encountered too many errors accepting client connections. "
+ "Possible causes: dynamic address renewal, or incompatible VPN or firewall software. "
+ "Try using the Win32DisableAcceptEx directive.", my_pid);
+ err_count = 0;
+ }
+ continue;
+ }
+ else if ((rv != APR_FROM_OS_ERROR(ERROR_IO_PENDING)) &&
+ (rv != APR_FROM_OS_ERROR(WSA_IO_PENDING))) {
+ ++err_count;
+ if (err_count > MAX_ACCEPTEX_ERR_COUNT) {
+ ap_log_error(APLOG_MARK,APLOG_ERR, rv, ap_server_conf,
+ "Child %d: Encountered too many errors accepting client connections. "
+ "Possible causes: Unknown. "
+ "Try using the Win32DisableAcceptEx directive.", my_pid);
+ err_count = 0;
+ }
+ closesocket(context->accept_socket);
+ context->accept_socket = INVALID_SOCKET;
+ continue;
+ }
+ err_count = 0;
+
+ /* Wait for pending i/o.
+ * Wake up once per second to check for shutdown .
+ * XXX: We should be waiting on exit_event instead of polling
+ */
+ while (1) {
+ rv = WaitForSingleObject(context->Overlapped.hEvent, 1000);
+ if (rv == WAIT_OBJECT_0) {
+ if (context->accept_socket == INVALID_SOCKET) {
+ /* socket already closed */
+ break;
+ }
+ if (!GetOverlappedResult((HANDLE)context->accept_socket,
+ &context->Overlapped,
+ &BytesRead, FALSE)) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING,
+ apr_get_os_error(), ap_server_conf,
+ "winnt_accept: Asynchronous AcceptEx failed.");
+ closesocket(context->accept_socket);
+ context->accept_socket = INVALID_SOCKET;
+ }
+ break;
+ }
+ /* WAIT_TIMEOUT */
+ if (shutdown_in_progress) {
+ closesocket(context->accept_socket);
+ context->accept_socket = INVALID_SOCKET;
+ break;
+ }
+ }
+ if (context->accept_socket == INVALID_SOCKET) {
+ continue;
+ }
+ }
+ err_count = 0;
+ /* Inherit the listen socket settings. Required for
+ * shutdown() to work
+ */
+ if (setsockopt(context->accept_socket, SOL_SOCKET,
+ SO_UPDATE_ACCEPT_CONTEXT, (char *)&nlsd,
+ sizeof(nlsd))) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, apr_get_netos_error(), ap_server_conf,
+ "setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed.");
+ /* Not a failure condition. Keep running. */
+ }
+
+ /* Get the local & remote address */
+ GetAcceptExSockaddrs(context->buff,
+ 0,
+ PADDED_ADDR_SIZE,
+ PADDED_ADDR_SIZE,
+ &context->sa_server,
+ &context->sa_server_len,
+ &context->sa_client,
+ &context->sa_client_len);
+
+ sockinfo.os_sock = &context->accept_socket;
+ sockinfo.local = context->sa_server;
+ sockinfo.remote = context->sa_client;
+ sockinfo.family = context->sa_server->sa_family;
+ sockinfo.type = SOCK_STREAM;
+ apr_os_sock_make(&context->sock, &sockinfo, context->ptrans);
+
+ /* When a connection is received, send an io completion notification to
+ * the ThreadDispatchIOCP. This function could be replaced by
+ * mpm_post_completion_context(), but why do an extra function call...
+ */
+ PostQueuedCompletionStatus(ThreadDispatchIOCP, 0, IOCP_CONNECTION_ACCEPTED,
+ &context->Overlapped);
+ context = NULL;
+ }
+ if (!shutdown_in_progress) {
+ /* Yow, hit an irrecoverable error! Tell the child to die. */
+ SetEvent(exit_event);
+ }
+ ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, ap_server_conf,
+ "Child %d: Accept thread exiting.", my_pid);
+ return 0;
+}
+
+
+static PCOMP_CONTEXT winnt_get_connection(PCOMP_CONTEXT context)
+{
+ int rc;
+ DWORD BytesRead;
+ LPOVERLAPPED pol;
+#ifdef _WIN64
+ ULONG_PTR CompKey;
+#else
+ DWORD CompKey;
+#endif
+
+ mpm_recycle_completion_context(context);
+
+ apr_atomic_inc32(&g_blocked_threads);
+ while (1) {
+ if (workers_may_exit) {
+ apr_atomic_dec32(&g_blocked_threads);
+ return NULL;
+ }
+ rc = GetQueuedCompletionStatus(ThreadDispatchIOCP, &BytesRead, &CompKey,
+ &pol, INFINITE);
+ if (!rc) {
+ rc = apr_get_os_error();
+ ap_log_error(APLOG_MARK,APLOG_DEBUG, rc, ap_server_conf,
+ "Child %d: GetQueuedComplationStatus returned %d", my_pid, rc);
+ continue;
+ }
+
+ switch (CompKey) {
+ case IOCP_CONNECTION_ACCEPTED:
+ context = CONTAINING_RECORD(pol, COMP_CONTEXT, Overlapped);
+ break;
+ case IOCP_SHUTDOWN:
+ apr_atomic_dec32(&g_blocked_threads);
+ return NULL;
+ default:
+ apr_atomic_dec32(&g_blocked_threads);
+ return NULL;
+ }
+ break;
+ }
+ apr_atomic_dec32(&g_blocked_threads);
+
+ return context;
+}
+
+
+/*
+ * worker_main()
+ * Main entry point for the worker threads. Worker threads block in
+ * win*_get_connection() awaiting a connection to service.
+ */
+static unsigned int __stdcall worker_main(void *thread_num_val)
+{
+ static int requests_this_child = 0;
+ PCOMP_CONTEXT context = NULL;
+ int thread_num = (int)thread_num_val;
+ ap_sb_handle_t *sbh;
+
+ while (1) {
+ conn_rec *c;
+ apr_int32_t disconnected;
+
+ ap_update_child_status_from_indexes(0, thread_num, SERVER_READY, NULL);
+
+ /* Grab a connection off the network */
+ if (use_acceptex) {
+ context = winnt_get_connection(context);
+ }
+ else {
+ context = win9x_get_connection(context);
+ }
+
+ if (!context) {
+ /* Time for the thread to exit */
+ break;
+ }
+
+ /* Have we hit MaxRequestPerChild connections? */
+ if (ap_max_requests_per_child) {
+ requests_this_child++;
+ if (requests_this_child > ap_max_requests_per_child) {
+ SetEvent(max_requests_per_child_event);
+ }
+ }
+
+ ap_create_sb_handle(&sbh, context->ptrans, 0, thread_num);
+ c = ap_run_create_connection(context->ptrans, ap_server_conf,
+ context->sock, thread_num, sbh,
+ context->ba);
+
+ if (c) {
+ ap_process_connection(c, context->sock);
+ apr_socket_opt_get(context->sock, APR_SO_DISCONNECTED,
+ &disconnected);
+ if (!disconnected) {
+ context->accept_socket = INVALID_SOCKET;
+ ap_lingering_close(c);
+ }
+ else if (!use_acceptex) {
+ /* If the socket is disconnected but we are not using acceptex,
+ * we cannot reuse the socket. Disconnected sockets are removed
+ * from the apr_socket_t struct by apr_sendfile() to prevent the
+ * socket descriptor from being inadvertently closed by a call
+ * to apr_socket_close(), so close it directly.
+ */
+ closesocket(context->accept_socket);
+ context->accept_socket = INVALID_SOCKET;
+ }
+ }
+ else {
+ /* ap_run_create_connection closes the socket on failure */
+ context->accept_socket = INVALID_SOCKET;
+ }
+ }
+
+ ap_update_child_status_from_indexes(0, thread_num, SERVER_DEAD,
+ (request_rec *) NULL);
+
+ return 0;
+}
+
+
+static void cleanup_thread(HANDLE *handles, int *thread_cnt, int thread_to_clean)
+{
+ int i;
+
+ CloseHandle(handles[thread_to_clean]);
+ for (i = thread_to_clean; i < ((*thread_cnt) - 1); i++)
+ handles[i] = handles[i + 1];
+ (*thread_cnt)--;
+}
+
+
+/*
+ * child_main()
+ * Entry point for the main control thread for the child process.
+ * This thread creates the accept thread, worker threads and
+ * monitors the child process for maintenance and shutdown
+ * events.
+ */
+static void create_listener_thread()
+{
+ int tid;
+ int num_listeners = 0;
+ if (!use_acceptex) {
+ _beginthreadex(NULL, 0, win9x_accept,
+ NULL, 0, &tid);
+ } else {
+ /* Start an accept thread per listener
+ * XXX: Why would we have a NULL sd in our listeners?
+ */
+ ap_listen_rec *lr;
+
+ /* Number of completion_contexts allowed in the system is
+ * (ap_threads_per_child + num_listeners). We need the additional
+ * completion contexts to prevent server hangs when ThreadsPerChild
+ * is configured to something less than or equal to the number
+ * of listeners. This is not a usual case, but people have
+ * encountered it.
+ * */
+ for (lr = ap_listeners; lr ; lr = lr->next) {
+ num_listeners++;
+ }
+ max_num_completion_contexts = ap_threads_per_child + num_listeners;
+
+ /* Now start a thread per listener */
+ for (lr = ap_listeners; lr; lr = lr->next) {
+ if (lr->sd != NULL) {
+ _beginthreadex(NULL, 1000, winnt_accept,
+ (void *) lr, 0, &tid);
+ }
+ }
+ }
+}
+
+
+void child_main(apr_pool_t *pconf)
+{
+ apr_status_t status;
+ apr_hash_t *ht;
+ ap_listen_rec *lr;
+ HANDLE child_events[2];
+ int threads_created = 0;
+ int listener_started = 0;
+ int tid;
+ HANDLE *child_handles;
+ int rv;
+ time_t end_time;
+ int i;
+ int cld;
+
+ apr_pool_create(&pchild, pconf);
+ apr_pool_tag(pchild, "pchild");
+
+ ap_run_child_init(pchild, ap_server_conf);
+ ht = apr_hash_make(pchild);
+
+ /* Initialize the child_events */
+ max_requests_per_child_event = CreateEvent(NULL, TRUE, FALSE, NULL);
+ if (!max_requests_per_child_event) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Child %d: Failed to create a max_requests event.", my_pid);
+ exit(APEXIT_CHILDINIT);
+ }
+ child_events[0] = exit_event;
+ child_events[1] = max_requests_per_child_event;
+
+ allowed_globals.jobsemaphore = CreateSemaphore(NULL, 0, 1000000, NULL);
+ apr_thread_mutex_create(&allowed_globals.jobmutex,
+ APR_THREAD_MUTEX_DEFAULT, pchild);
+
+ /*
+ * Wait until we have permission to start accepting connections.
+ * start_mutex is used to ensure that only one child ever
+ * goes into the listen/accept loop at once.
+ */
+ status = apr_proc_mutex_lock(start_mutex);
+ if (status != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK,APLOG_ERR, status, ap_server_conf,
+ "Child %d: Failed to acquire the start_mutex. Process will exit.", my_pid);
+ exit(APEXIT_CHILDINIT);
+ }
+ ap_log_error(APLOG_MARK,APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Child %d: Acquired the start mutex.", my_pid);
+
+ /*
+ * Create the worker thread dispatch IOCompletionPort
+ * on Windows NT/2000
+ */
+ if (use_acceptex) {
+ /* Create the worker thread dispatch IOCP */
+ ThreadDispatchIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
+ NULL,
+ 0,
+ 0); /* CONCURRENT ACTIVE THREADS */
+ apr_thread_mutex_create(&qlock, APR_THREAD_MUTEX_DEFAULT, pchild);
+ qwait_event = CreateEvent(NULL, TRUE, FALSE, NULL);
+ if (!qwait_event) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Child %d: Failed to create a qwait event.", my_pid);
+ exit(APEXIT_CHILDINIT);
+ }
+ }
+
+ /*
+ * Create the pool of worker threads
+ */
+ ap_log_error(APLOG_MARK,APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Child %d: Starting %d worker threads.", my_pid, ap_threads_per_child);
+ child_handles = (HANDLE) apr_pcalloc(pchild, ap_threads_per_child * sizeof(HANDLE));
+ apr_thread_mutex_create(&child_lock, APR_THREAD_MUTEX_DEFAULT, pchild);
+
+ while (1) {
+ for (i = 0; i < ap_threads_per_child; i++) {
+ int *score_idx;
+ int status = ap_scoreboard_image->servers[0][i].status;
+ if (status != SERVER_GRACEFUL && status != SERVER_DEAD) {
+ continue;
+ }
+ ap_update_child_status_from_indexes(0, i, SERVER_STARTING, NULL);
+ child_handles[i] = (HANDLE) _beginthreadex(NULL, (unsigned)ap_thread_stacksize,
+ worker_main, (void *) i, 0, &tid);
+ if (child_handles[i] == 0) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Child %d: _beginthreadex failed. Unable to create all worker threads. "
+ "Created %d of the %d threads requested with the ThreadsPerChild configuration directive.",
+ my_pid, threads_created, ap_threads_per_child);
+ ap_signal_parent(SIGNAL_PARENT_SHUTDOWN);
+ goto shutdown;
+ }
+ threads_created++;
+ /* Save the score board index in ht keyed to the thread handle. We need this
+ * when cleaning up threads down below...
+ */
+ apr_thread_mutex_lock(child_lock);
+ score_idx = apr_pcalloc(pchild, sizeof(int));
+ *score_idx = i;
+ apr_hash_set(ht, &child_handles[i], sizeof(HANDLE), score_idx);
+ apr_thread_mutex_unlock(child_lock);
+ }
+ /* Start the listener only when workers are available */
+ if (!listener_started && threads_created) {
+ create_listener_thread();
+ listener_started = 1;
+ winnt_mpm_state = AP_MPMQ_RUNNING;
+ }
+ if (threads_created == ap_threads_per_child) {
+ break;
+ }
+ /* Check to see if the child has been told to exit */
+ if (WaitForSingleObject(exit_event, 0) != WAIT_TIMEOUT) {
+ break;
+ }
+ /* wait for previous generation to clean up an entry in the scoreboard */
+ apr_sleep(1 * APR_USEC_PER_SEC);
+ }
+
+ /* Wait for one of three events:
+ * exit_event:
+ * The exit_event is signaled by the parent process to notify
+ * the child that it is time to exit.
+ *
+ * max_requests_per_child_event:
+ * This event is signaled by the worker threads to indicate that
+ * the process has handled MaxRequestsPerChild connections.
+ *
+ * TIMEOUT:
+ * To do periodic maintenance on the server (check for thread exits,
+ * number of completion contexts, etc.)
+ *
+ * XXX: thread exits *aren't* being checked.
+ *
+ * XXX: other_child - we need the process handles to the other children
+ * in order to map them to apr_proc_other_child_read (which is not
+ * named well, it's more like a_p_o_c_died.)
+ *
+ * XXX: however - if we get a_p_o_c handle inheritance working, and
+ * the parent process creates other children and passes the pipes
+ * to our worker processes, then we have no business doing such
+ * things in the child_main loop, but should happen in master_main.
+ */
+ while (1) {
+#if !APR_HAS_OTHER_CHILD
+ rv = WaitForMultipleObjects(2, (HANDLE *) child_events, FALSE, INFINITE);
+ cld = rv - WAIT_OBJECT_0;
+#else
+ rv = WaitForMultipleObjects(2, (HANDLE *) child_events, FALSE, 1000);
+ cld = rv - WAIT_OBJECT_0;
+ if (rv == WAIT_TIMEOUT) {
+ apr_proc_other_child_refresh_all(APR_OC_REASON_RUNNING);
+ }
+ else
+#endif
+ if (rv == WAIT_FAILED) {
+ /* Something serious is wrong */
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Child %d: WAIT_FAILED -- shutting down server", my_pid);
+ break;
+ }
+ else if (cld == 0) {
+ /* Exit event was signaled */
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Child %d: Exit event signaled. Child process is ending.", my_pid);
+ break;
+ }
+ else {
+ /* MaxRequestsPerChild event set by the worker threads.
+ * Signal the parent to restart
+ */
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Child %d: Process exiting because it reached "
+ "MaxRequestsPerChild. Signaling the parent to "
+ "restart a new child process.", my_pid);
+ ap_signal_parent(SIGNAL_PARENT_RESTART);
+ break;
+ }
+ }
+
+ /*
+ * Time to shutdown the child process
+ */
+
+ shutdown:
+
+ winnt_mpm_state = AP_MPMQ_STOPPING;
+ /* Setting is_graceful will cause threads handling keep-alive connections
+ * to close the connection after handling the current request.
+ */
+ is_graceful = 1;
+
+ /* Close the listening sockets. Note, we must close the listeners
+ * before closing any accept sockets pending in AcceptEx to prevent
+ * memory leaks in the kernel.
+ */
+ for (lr = ap_listeners; lr ; lr = lr->next) {
+ apr_socket_close(lr->sd);
+ }
+
+ /* Shutdown listener threads and pending AcceptEx socksts
+ * but allow the worker threads to continue consuming from
+ * the queue of accepted connections.
+ */
+ shutdown_in_progress = 1;
+
+ Sleep(1000);
+
+ /* Tell the worker threads to exit */
+ workers_may_exit = 1;
+
+ /* Release the start_mutex to let the new process (in the restart
+ * scenario) a chance to begin accepting and servicing requests
+ */
+ rv = apr_proc_mutex_unlock(start_mutex);
+ if (rv == APR_SUCCESS) {
+ ap_log_error(APLOG_MARK,APLOG_NOTICE, rv, ap_server_conf,
+ "Child %d: Released the start mutex", my_pid);
+ }
+ else {
+ ap_log_error(APLOG_MARK,APLOG_ERR, rv, ap_server_conf,
+ "Child %d: Failure releasing the start mutex", my_pid);
+ }
+
+ /* Shutdown the worker threads */
+ if (!use_acceptex) {
+ for (i = 0; i < threads_created; i++) {
+ add_job(INVALID_SOCKET);
+ }
+ }
+ else { /* Windows NT/2000 */
+ /* Post worker threads blocked on the ThreadDispatch IOCompletion port */
+ while (g_blocked_threads > 0) {
+ ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, ap_server_conf,
+ "Child %d: %d threads blocked on the completion port", my_pid, g_blocked_threads);
+ for (i=g_blocked_threads; i > 0; i--) {
+ PostQueuedCompletionStatus(ThreadDispatchIOCP, 0, IOCP_SHUTDOWN, NULL);
+ }
+ Sleep(1000);
+ }
+ /* Empty the accept queue of completion contexts */
+ apr_thread_mutex_lock(qlock);
+ while (qhead) {
+ CloseHandle(qhead->Overlapped.hEvent);
+ closesocket(qhead->accept_socket);
+ qhead = qhead->next;
+ }
+ apr_thread_mutex_unlock(qlock);
+ }
+
+ /* Give busy worker threads a chance to service their connections */
+ ap_log_error(APLOG_MARK,APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Child %d: Waiting for %d worker threads to exit.", my_pid, threads_created);
+ end_time = time(NULL) + 180;
+ while (threads_created) {
+ rv = wait_for_many_objects(threads_created, child_handles, (DWORD)(end_time - time(NULL)));
+ if (rv != WAIT_TIMEOUT) {
+ rv = rv - WAIT_OBJECT_0;
+ ap_assert((rv >= 0) && (rv < threads_created));
+ cleanup_thread(child_handles, &threads_created, rv);
+ continue;
+ }
+ break;
+ }
+
+ /* Kill remaining threads off the hard way */
+ if (threads_created) {
+ ap_log_error(APLOG_MARK,APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Child %d: Terminating %d threads that failed to exit.",
+ my_pid, threads_created);
+ }
+ for (i = 0; i < threads_created; i++) {
+ int *score_idx;
+ TerminateThread(child_handles[i], 1);
+ CloseHandle(child_handles[i]);
+ /* Reset the scoreboard entry for the thread we just whacked */
+ score_idx = apr_hash_get(ht, &child_handles[i], sizeof(HANDLE));
+ ap_update_child_status_from_indexes(0, *score_idx, SERVER_DEAD, NULL);
+ }
+ ap_log_error(APLOG_MARK,APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Child %d: All worker threads have exited.", my_pid);
+
+ CloseHandle(allowed_globals.jobsemaphore);
+ apr_thread_mutex_destroy(allowed_globals.jobmutex);
+ apr_thread_mutex_destroy(child_lock);
+
+ if (use_acceptex) {
+ apr_thread_mutex_destroy(qlock);
+ CloseHandle(qwait_event);
+ }
+
+ apr_pool_destroy(pchild);
+ CloseHandle(exit_event);
+}
+
+#endif /* def WIN32 */
diff --git a/server/mpm/winnt/mpm.h b/server/mpm/winnt/mpm.h
new file mode 100644
index 00000000..536ee6aa
--- /dev/null
+++ b/server/mpm/winnt/mpm.h
@@ -0,0 +1,49 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file winnt/mpm.h
+ * @brief MPM for Windows NT
+ *
+ * this is the place to make declarations that are MPM specific but that must be
+ * shared with non-mpm specific code in the server. Hummm, perhaps we can
+ * move most of this stuff to mpm_common.h?
+ *
+ * @defgroup APACHE_MPM_WINNT WinNT MPM
+ * @ingroup APACHE_OS_WIN32 APACHE_MPM
+ * @{
+ */
+
+#ifndef APACHE_MPM_H
+#define APACHE_MPM_H
+
+#include "scoreboard.h"
+
+#define MPM_NAME "WinNT"
+
+#define AP_MPM_WANT_SET_PIDFILE
+#define AP_MPM_WANT_SET_MAX_REQUESTS
+#define AP_MPM_WANT_SET_COREDUMPDIR
+#define AP_MPM_WANT_SET_SCOREBOARD
+#define AP_MPM_WANT_SET_MAX_MEM_FREE
+#define AP_MPM_WANT_SET_STACKSIZE
+
+extern int ap_threads_per_child;
+extern int ap_thread_limit;
+extern server_rec *ap_server_conf;
+
+#endif /* APACHE_MPM_H */
+/** @} */
diff --git a/server/mpm/winnt/mpm_default.h b/server/mpm/winnt/mpm_default.h
new file mode 100644
index 00000000..990eb47a
--- /dev/null
+++ b/server/mpm/winnt/mpm_default.h
@@ -0,0 +1,89 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file winnt/mpm_default.h
+ * @brief win32 MPM defaults
+ *
+ * @addtogroup APACHE_MPM_WINNT
+ * @{
+ */
+
+#ifndef APACHE_MPM_DEFAULT_H
+#define APACHE_MPM_DEFAULT_H
+
+/* Default limit on the maximum setting of the ThreadsPerChild configuration
+ * directive. This limit can be overridden with the ThreadLimit directive.
+ * This limit directly influences the amount of shared storage that is allocated
+ * for the scoreboard. DEFAULT_THREAD_LIMIT represents a good compromise
+ * between scoreboard size and the ability of the server to handle the most
+ * common installation requirements.
+ */
+#ifndef DEFAULT_THREAD_LIMIT
+#define DEFAULT_THREAD_LIMIT 1920
+#endif
+
+/* The ThreadLimit directive can be used to override the DEFAULT_THREAD_LIMIT.
+ * ThreadLimit cannot be tuned larger than MAX_THREAD_LIMIT.
+ * This is a sort of compile-time limit to help catch typos.
+ */
+#ifndef MAX_THREAD_LIMIT
+#define MAX_THREAD_LIMIT 15000
+#endif
+
+/* Number of threads started in the child process in the absence
+ * of a ThreadsPerChild configuration directive
+ */
+#ifndef DEFAULT_THREADS_PER_CHILD
+#define DEFAULT_THREADS_PER_CHILD 64
+#endif
+
+/* Max number of child processes allowed.
+ */
+#define HARD_SERVER_LIMIT 1
+
+/* Number of servers to spawn off by default
+ */
+#ifndef DEFAULT_NUM_DAEMON
+#define DEFAULT_NUM_DAEMON 1
+#endif
+
+/* Check for definition of DEFAULT_REL_RUNTIMEDIR */
+#ifndef DEFAULT_REL_RUNTIMEDIR
+#define DEFAULT_REL_RUNTIMEDIR "logs"
+#endif
+
+/* Where the main/parent process's pid is logged */
+#ifndef DEFAULT_PIDLOG
+#define DEFAULT_PIDLOG DEFAULT_REL_RUNTIMEDIR "/httpd.pid"
+#endif
+
+/*
+ * Interval, in microseconds, between scoreboard maintenance.
+ */
+#ifndef SCOREBOARD_MAINTENANCE_INTERVAL
+#define SCOREBOARD_MAINTENANCE_INTERVAL 1000000
+#endif
+
+/* Number of requests to try to handle in a single process. If <= 0,
+ * the children don't die off.
+ */
+#ifndef DEFAULT_MAX_REQUESTS_PER_CHILD
+#define DEFAULT_MAX_REQUESTS_PER_CHILD 0
+#endif
+
+#endif /* AP_MPM_DEFAULT_H */
+/** @} */
diff --git a/server/mpm/winnt/mpm_winnt.c b/server/mpm/winnt/mpm_winnt.c
new file mode 100644
index 00000000..03985354
--- /dev/null
+++ b/server/mpm/winnt/mpm_winnt.c
@@ -0,0 +1,1724 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifdef WIN32
+
+#define CORE_PRIVATE
+#include "httpd.h"
+#include "http_main.h"
+#include "http_log.h"
+#include "http_config.h" /* for read_config */
+#include "http_core.h" /* for get_remote_host */
+#include "http_connection.h"
+#include "apr_portable.h"
+#include "apr_thread_proc.h"
+#include "apr_getopt.h"
+#include "apr_strings.h"
+#include "apr_lib.h"
+#include "apr_shm.h"
+#include "apr_thread_mutex.h"
+#include "ap_mpm.h"
+#include "ap_config.h"
+#include "ap_listen.h"
+#include "mpm_default.h"
+#include "mpm_winnt.h"
+#include "mpm_common.h"
+#include <malloc.h>
+#include "apr_atomic.h"
+
+
+/* scoreboard.c does the heavy lifting; all we do is create the child
+ * score by moving a handle down the pipe into the child's stdin.
+ */
+extern apr_shm_t *ap_scoreboard_shm;
+server_rec *ap_server_conf;
+
+/* Definitions of WINNT MPM specific config globals */
+static HANDLE shutdown_event; /* used to signal the parent to shutdown */
+static HANDLE restart_event; /* used to signal the parent to restart */
+
+static char ap_coredump_dir[MAX_STRING_LEN];
+
+static int one_process = 0;
+static char const* signal_arg = NULL;
+
+OSVERSIONINFO osver; /* VER_PLATFORM_WIN32_NT */
+
+static DWORD parent_pid;
+DWORD my_pid;
+
+int ap_threads_per_child = 0;
+int use_acceptex = 1;
+static int thread_limit = DEFAULT_THREAD_LIMIT;
+static int first_thread_limit = 0;
+static int changed_limit_at_restart;
+int winnt_mpm_state = AP_MPMQ_STARTING;
+
+/* ap_my_generation are used by the scoreboard code */
+ap_generation_t volatile ap_my_generation=0;
+
+
+/* shared by service.c as global, although
+ * perhaps it should be private.
+ */
+apr_pool_t *pconf;
+
+
+/* definitions from child.c */
+void child_main(apr_pool_t *pconf);
+
+/* used by parent to signal the child to start and exit
+ * NOTE: these are not sophisticated enough for multiple children
+ * so they ultimately should not be shared with child.c
+ */
+extern apr_proc_mutex_t *start_mutex;
+extern HANDLE exit_event;
+
+
+/* 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;
+}
+
+/*
+ * Command processors
+ */
+
+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 > thread_limit) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: ThreadsPerChild of %d exceeds ThreadLimit "
+ "value of %d threads,", ap_threads_per_child,
+ thread_limit);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " lowering ThreadsPerChild to %d. To increase, please"
+ " see the", thread_limit);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " ThreadLimit directive.");
+ ap_threads_per_child = thread_limit;
+ }
+ else if (ap_threads_per_child < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require ThreadsPerChild > 0, setting to 1");
+ ap_threads_per_child = 1;
+ }
+ return NULL;
+}
+static const char *set_thread_limit (cmd_parms *cmd, void *dummy, const char *arg)
+{
+ int tmp_thread_limit;
+
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ tmp_thread_limit = atoi(arg);
+ /* you cannot change ThreadLimit across a restart; ignore
+ * any such attempts
+ */
+ if (first_thread_limit &&
+ tmp_thread_limit != thread_limit) {
+ /* how do we log a message? the error log is a bit bucket at this
+ * point; we'll just have to set a flag so that ap_mpm_run()
+ * logs a warning later
+ */
+ changed_limit_at_restart = 1;
+ return NULL;
+ }
+ thread_limit = tmp_thread_limit;
+
+ if (thread_limit > MAX_THREAD_LIMIT) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: ThreadLimit of %d exceeds compile time limit "
+ "of %d threads,", thread_limit, MAX_THREAD_LIMIT);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " lowering ThreadLimit to %d.", MAX_THREAD_LIMIT);
+ thread_limit = MAX_THREAD_LIMIT;
+ }
+ else if (thread_limit < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require ThreadLimit > 0, setting to 1");
+ thread_limit = 1;
+ }
+ return NULL;
+}
+static const char *set_disable_acceptex(cmd_parms *cmd, void *dummy, char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+ if (use_acceptex) {
+ use_acceptex = 0;
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, NULL,
+ "Disabled use of AcceptEx() WinSock2 API");
+ }
+ return NULL;
+}
+
+static const command_rec winnt_cmds[] = {
+LISTEN_COMMANDS,
+AP_INIT_TAKE1("ThreadsPerChild", set_threads_per_child, NULL, RSRC_CONF,
+ "Number of threads each child creates" ),
+AP_INIT_TAKE1("ThreadLimit", set_thread_limit, NULL, RSRC_CONF,
+ "Maximum worker threads in a server for this run of Apache"),
+AP_INIT_NO_ARGS("Win32DisableAcceptEx", set_disable_acceptex, NULL, RSRC_CONF,
+ "Disable use of the high performance AcceptEx WinSock2 API to work around buggy VPN or Firewall software"),
+
+{ NULL }
+};
+
+
+/*
+ * Signalling Apache on NT.
+ *
+ * Under Unix, Apache can be told to shutdown or restart by sending various
+ * signals (HUP, USR, TERM). On NT we don't have easy access to signals, so
+ * we use "events" instead. The parent apache process goes into a loop
+ * where it waits forever for a set of events. Two of those events are
+ * called
+ *
+ * apPID_shutdown
+ * apPID_restart
+ *
+ * (where PID is the PID of the apache parent process). When one of these
+ * is signalled, the Apache parent performs the appropriate action. The events
+ * can become signalled through internal Apache methods (e.g. if the child
+ * finds a fatal error and needs to kill its parent), via the service
+ * control manager (the control thread will signal the shutdown event when
+ * requested to stop the Apache service), from the -k Apache command line,
+ * or from any external program which finds the Apache PID from the
+ * httpd.pid file.
+ *
+ * The signal_parent() function, below, is used to signal one of these events.
+ * It can be called by any child or parent process, since it does not
+ * rely on global variables.
+ *
+ * On entry, type gives the event to signal. 0 means shutdown, 1 means
+ * graceful restart.
+ */
+/*
+ * Initialise the signal names, in the global variables signal_name_prefix,
+ * signal_restart_name and signal_shutdown_name.
+ */
+#define MAX_SIGNAL_NAME 30 /* Long enough for apPID_shutdown, where PID is an int */
+char signal_name_prefix[MAX_SIGNAL_NAME];
+char signal_restart_name[MAX_SIGNAL_NAME];
+char signal_shutdown_name[MAX_SIGNAL_NAME];
+void setup_signal_names(char *prefix)
+{
+ apr_snprintf(signal_name_prefix, sizeof(signal_name_prefix), prefix);
+ apr_snprintf(signal_shutdown_name, sizeof(signal_shutdown_name),
+ "%s_shutdown", signal_name_prefix);
+ apr_snprintf(signal_restart_name, sizeof(signal_restart_name),
+ "%s_restart", signal_name_prefix);
+}
+
+int volatile is_graceful = 0;
+
+AP_DECLARE(int) ap_graceful_stop_signalled(void)
+{
+ return is_graceful;
+}
+
+AP_DECLARE(void) ap_signal_parent(ap_signal_parent_e type)
+{
+ HANDLE e;
+ char *signal_name;
+
+ if (parent_pid == my_pid) {
+ switch(type) {
+ case SIGNAL_PARENT_SHUTDOWN:
+ {
+ SetEvent(shutdown_event);
+ break;
+ }
+ /* This MPM supports only graceful restarts right now */
+ case SIGNAL_PARENT_RESTART:
+ case SIGNAL_PARENT_RESTART_GRACEFUL:
+ {
+ is_graceful = 1;
+ SetEvent(restart_event);
+ break;
+ }
+ }
+ return;
+ }
+
+ switch(type) {
+ case SIGNAL_PARENT_SHUTDOWN:
+ {
+ signal_name = signal_shutdown_name;
+ break;
+ }
+ /* This MPM supports only graceful restarts right now */
+ case SIGNAL_PARENT_RESTART:
+ case SIGNAL_PARENT_RESTART_GRACEFUL:
+ {
+ signal_name = signal_restart_name;
+ is_graceful = 1;
+ break;
+ }
+ default:
+ return;
+ }
+
+ e = OpenEvent(EVENT_MODIFY_STATE, FALSE, signal_name);
+ if (!e) {
+ /* Um, problem, can't signal the parent, which means we can't
+ * signal ourselves to die. Ignore for now...
+ */
+ ap_log_error(APLOG_MARK, APLOG_EMERG, apr_get_os_error(), ap_server_conf,
+ "OpenEvent on %s event", signal_name);
+ return;
+ }
+ if (SetEvent(e) == 0) {
+ /* Same problem as above */
+ ap_log_error(APLOG_MARK, APLOG_EMERG, apr_get_os_error(), ap_server_conf,
+ "SetEvent on %s event", signal_name);
+ CloseHandle(e);
+ return;
+ }
+ CloseHandle(e);
+}
+
+
+/*
+ * Passed the following handles [in sync with send_handles_to_child()]
+ *
+ * ready event [signal the parent immediately, then close]
+ * exit event [save to poll later]
+ * start mutex [signal from the parent to begin accept()]
+ * scoreboard shm handle [to recreate the ap_scoreboard]
+ */
+void get_handles_from_parent(server_rec *s, HANDLE *child_exit_event,
+ apr_proc_mutex_t **child_start_mutex,
+ apr_shm_t **scoreboard_shm)
+{
+ HANDLE pipe;
+ HANDLE hScore;
+ HANDLE ready_event;
+ HANDLE os_start;
+ DWORD BytesRead;
+ void *sb_shared;
+ apr_status_t rv;
+
+ pipe = GetStdHandle(STD_INPUT_HANDLE);
+ if (!ReadFile(pipe, &ready_event, sizeof(HANDLE),
+ &BytesRead, (LPOVERLAPPED) NULL)
+ || (BytesRead != sizeof(HANDLE))) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Child %d: Unable to retrieve the ready event from the parent", my_pid);
+ exit(APEXIT_CHILDINIT);
+ }
+
+ SetEvent(ready_event);
+ CloseHandle(ready_event);
+
+ if (!ReadFile(pipe, child_exit_event, sizeof(HANDLE),
+ &BytesRead, (LPOVERLAPPED) NULL)
+ || (BytesRead != sizeof(HANDLE))) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Child %d: Unable to retrieve the exit event from the parent", my_pid);
+ exit(APEXIT_CHILDINIT);
+ }
+
+ if (!ReadFile(pipe, &os_start, sizeof(os_start),
+ &BytesRead, (LPOVERLAPPED) NULL)
+ || (BytesRead != sizeof(os_start))) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Child %d: Unable to retrieve the start_mutex from the parent", my_pid);
+ exit(APEXIT_CHILDINIT);
+ }
+ *child_start_mutex = NULL;
+ if ((rv = apr_os_proc_mutex_put(child_start_mutex, &os_start, s->process->pool))
+ != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Child %d: Unable to access the start_mutex from the parent", my_pid);
+ exit(APEXIT_CHILDINIT);
+ }
+
+ if (!ReadFile(pipe, &hScore, sizeof(hScore),
+ &BytesRead, (LPOVERLAPPED) NULL)
+ || (BytesRead != sizeof(hScore))) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Child %d: Unable to retrieve the scoreboard from the parent", my_pid);
+ exit(APEXIT_CHILDINIT);
+ }
+ *scoreboard_shm = NULL;
+ if ((rv = apr_os_shm_put(scoreboard_shm, &hScore, s->process->pool))
+ != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Child %d: Unable to access the scoreboard from the parent", my_pid);
+ exit(APEXIT_CHILDINIT);
+ }
+
+ rv = ap_reopen_scoreboard(s->process->pool, scoreboard_shm, 1);
+ if (rv || !(sb_shared = apr_shm_baseaddr_get(*scoreboard_shm))) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
+ "Child %d: Unable to reopen the scoreboard from the parent", my_pid);
+ exit(APEXIT_CHILDINIT);
+ }
+ /* We must 'initialize' the scoreboard to relink all the
+ * process-local pointer arrays into the shared memory block.
+ */
+ ap_init_scoreboard(sb_shared);
+
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "Child %d: Retrieved our scoreboard from the parent.", my_pid);
+}
+
+
+static int send_handles_to_child(apr_pool_t *p,
+ HANDLE child_ready_event,
+ HANDLE child_exit_event,
+ apr_proc_mutex_t *child_start_mutex,
+ apr_shm_t *scoreboard_shm,
+ HANDLE hProcess,
+ apr_file_t *child_in)
+{
+ apr_status_t rv;
+ HANDLE hCurrentProcess = GetCurrentProcess();
+ HANDLE hDup;
+ HANDLE os_start;
+ HANDLE hScore;
+ apr_size_t BytesWritten;
+
+ if (!DuplicateHandle(hCurrentProcess, child_ready_event, hProcess, &hDup,
+ EVENT_MODIFY_STATE | SYNCHRONIZE, FALSE, 0)) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Parent: Unable to duplicate the ready event handle for the child");
+ return -1;
+ }
+ if ((rv = apr_file_write_full(child_in, &hDup, sizeof(hDup), &BytesWritten))
+ != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Parent: Unable to send the exit event handle to the child");
+ return -1;
+ }
+ if (!DuplicateHandle(hCurrentProcess, child_exit_event, hProcess, &hDup,
+ EVENT_MODIFY_STATE | SYNCHRONIZE, FALSE, 0)) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Parent: Unable to duplicate the exit event handle for the child");
+ return -1;
+ }
+ if ((rv = apr_file_write_full(child_in, &hDup, sizeof(hDup), &BytesWritten))
+ != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Parent: Unable to send the exit event handle to the child");
+ return -1;
+ }
+ if ((rv = apr_os_proc_mutex_get(&os_start, child_start_mutex)) != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Parent: Unable to retrieve the start mutex for the child");
+ return -1;
+ }
+ if (!DuplicateHandle(hCurrentProcess, os_start, hProcess, &hDup,
+ SYNCHRONIZE, FALSE, 0)) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Parent: Unable to duplicate the start mutex to the child");
+ return -1;
+ }
+ if ((rv = apr_file_write_full(child_in, &hDup, sizeof(hDup), &BytesWritten))
+ != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Parent: Unable to send the start mutex to the child");
+ return -1;
+ }
+ if ((rv = apr_os_shm_get(&hScore, scoreboard_shm)) != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Parent: Unable to retrieve the scoreboard handle for the child");
+ return -1;
+ }
+ if (!DuplicateHandle(hCurrentProcess, hScore, hProcess, &hDup,
+ FILE_MAP_READ | FILE_MAP_WRITE, FALSE, 0)) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Parent: Unable to duplicate the scoreboard handle to the child");
+ return -1;
+ }
+ if ((rv = apr_file_write_full(child_in, &hDup, sizeof(hDup), &BytesWritten))
+ != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Parent: Unable to send the scoreboard handle to the child");
+ return -1;
+ }
+
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "Parent: Sent the scoreboard to the child");
+ return 0;
+}
+
+
+/*
+ * get_listeners_from_parent()
+ * The listen sockets are opened in the parent. This function, which runs
+ * exclusively in the child process, receives them from the parent and
+ * makes them availeble in the child.
+ */
+void get_listeners_from_parent(server_rec *s)
+{
+ WSAPROTOCOL_INFO WSAProtocolInfo;
+ HANDLE pipe;
+ ap_listen_rec *lr;
+ DWORD BytesRead;
+ int lcnt = 0;
+ SOCKET nsd;
+
+ /* Set up a default listener if necessary */
+ if (ap_listeners == NULL) {
+ ap_listen_rec *lr;
+ lr = apr_palloc(s->process->pool, sizeof(ap_listen_rec));
+ lr->sd = NULL;
+ lr->next = ap_listeners;
+ ap_listeners = lr;
+ }
+
+ /* Open the pipe to the parent process to receive the inherited socket
+ * data. The sockets have been set to listening in the parent process.
+ */
+ pipe = GetStdHandle(STD_INPUT_HANDLE);
+
+ for (lr = ap_listeners; lr; lr = lr->next, ++lcnt) {
+ if (!ReadFile(pipe, &WSAProtocolInfo, sizeof(WSAPROTOCOL_INFO),
+ &BytesRead, (LPOVERLAPPED) NULL)) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "setup_inherited_listeners: Unable to read socket data from parent");
+ exit(APEXIT_CHILDINIT);
+ }
+ nsd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
+ &WSAProtocolInfo, 0, 0);
+ if (nsd == INVALID_SOCKET) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_netos_error(), ap_server_conf,
+ "Child %d: setup_inherited_listeners(), WSASocket failed to open the inherited socket.", my_pid);
+ exit(APEXIT_CHILDINIT);
+ }
+
+ if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
+ HANDLE hProcess = GetCurrentProcess();
+ HANDLE dup;
+ if (DuplicateHandle(hProcess, (HANDLE) nsd, hProcess, &dup,
+ 0, FALSE, DUPLICATE_SAME_ACCESS)) {
+ closesocket(nsd);
+ nsd = (SOCKET) dup;
+ }
+ }
+ else {
+ /* A different approach. Many users report errors such as
+ * (32538)An operation was attempted on something that is not
+ * a socket. : Parent: WSADuplicateSocket failed...
+ *
+ * This appears that the duplicated handle is no longer recognized
+ * as a socket handle. SetHandleInformation should overcome that
+ * problem by not altering the handle identifier. But this won't
+ * work on 9x - it's unsupported.
+ */
+ if (!SetHandleInformation((HANDLE)nsd, HANDLE_FLAG_INHERIT, 0)) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), ap_server_conf,
+ "set_listeners_noninheritable: SetHandleInformation failed.");
+ }
+ }
+ apr_os_sock_put(&lr->sd, &nsd, s->process->pool);
+ }
+
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "Child %d: retrieved %d listeners from parent", my_pid, lcnt);
+}
+
+
+static int send_listeners_to_child(apr_pool_t *p, DWORD dwProcessId,
+ apr_file_t *child_in)
+{
+ apr_status_t rv;
+ int lcnt = 0;
+ ap_listen_rec *lr;
+ LPWSAPROTOCOL_INFO lpWSAProtocolInfo;
+ apr_size_t BytesWritten;
+
+ /* Run the chain of open sockets. For each socket, duplicate it
+ * for the target process then send the WSAPROTOCOL_INFO
+ * (returned by dup socket) to the child.
+ */
+ for (lr = ap_listeners; lr; lr = lr->next, ++lcnt) {
+ apr_os_sock_t nsd;
+ lpWSAProtocolInfo = apr_pcalloc(p, sizeof(WSAPROTOCOL_INFO));
+ apr_os_sock_get(&nsd,lr->sd);
+ ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, ap_server_conf,
+ "Parent: Duplicating socket %d and sending it to child process %d",
+ nsd, dwProcessId);
+ if (WSADuplicateSocket(nsd, dwProcessId,
+ lpWSAProtocolInfo) == SOCKET_ERROR) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_netos_error(), ap_server_conf,
+ "Parent: WSADuplicateSocket failed for socket %d. Check the FAQ.", lr->sd );
+ return -1;
+ }
+
+ if ((rv = apr_file_write_full(child_in, lpWSAProtocolInfo,
+ sizeof(WSAPROTOCOL_INFO), &BytesWritten))
+ != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Parent: Unable to write duplicated socket %d to the child.", lr->sd );
+ return -1;
+ }
+ }
+
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "Parent: Sent %d listeners to child %d", lcnt, dwProcessId);
+ return 0;
+}
+
+enum waitlist_e {
+ waitlist_ready = 0,
+ waitlist_term = 1
+};
+
+static int create_process(apr_pool_t *p, HANDLE *child_proc, HANDLE *child_exit_event,
+ DWORD *child_pid)
+{
+ /* These NEVER change for the lifetime of this parent
+ */
+ static char **args = NULL;
+ static char **env = NULL;
+ static char pidbuf[28];
+
+ apr_status_t rv;
+ apr_pool_t *ptemp;
+ apr_procattr_t *attr;
+ apr_file_t *child_out;
+ apr_file_t *child_err;
+ apr_proc_t new_child;
+ HANDLE hExitEvent;
+ HANDLE waitlist[2]; /* see waitlist_e */
+ char *cmd;
+ char *cwd;
+
+ apr_pool_create_ex(&ptemp, p, NULL, NULL);
+
+ /* Build the command line. Should look something like this:
+ * C:/apache/bin/apache.exe -f ap_server_confname
+ * First, get the path to the executable...
+ */
+ apr_procattr_create(&attr, ptemp);
+ apr_procattr_cmdtype_set(attr, APR_PROGRAM);
+ apr_procattr_detach_set(attr, 1);
+ if (((rv = apr_filepath_get(&cwd, 0, ptemp)) != APR_SUCCESS)
+ || ((rv = apr_procattr_dir_set(attr, cwd)) != APR_SUCCESS)) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Parent: Failed to get the current path");
+ }
+
+ if (!args) {
+ /* Build the args array, only once since it won't change
+ * for the lifetime of this parent process.
+ */
+ if ((rv = ap_os_proc_filepath(&cmd, ptemp))
+ != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, ERROR_BAD_PATHNAME, ap_server_conf,
+ "Parent: Failed to get full path of %s",
+ ap_server_conf->process->argv[0]);
+ apr_pool_destroy(ptemp);
+ return -1;
+ }
+
+ args = malloc((ap_server_conf->process->argc + 1) * sizeof (char*));
+ memcpy(args + 1, ap_server_conf->process->argv + 1,
+ (ap_server_conf->process->argc - 1) * sizeof (char*));
+ args[0] = malloc(strlen(cmd) + 1);
+ strcpy(args[0], cmd);
+ args[ap_server_conf->process->argc] = NULL;
+ }
+ else {
+ cmd = args[0];
+ }
+
+ /* Create a pipe to send handles to the child */
+ if ((rv = apr_procattr_io_set(attr, APR_FULL_BLOCK,
+ APR_NO_PIPE, APR_NO_PIPE)) != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Parent: Unable to create child stdin pipe.");
+ apr_pool_destroy(ptemp);
+ return -1;
+ }
+
+ /* Open a null handle to soak info from the child */
+ if (((rv = apr_file_open(&child_out, "NUL", APR_READ | APR_WRITE,
+ APR_OS_DEFAULT, ptemp)) != APR_SUCCESS)
+ || ((rv = apr_procattr_child_out_set(attr, child_out, NULL))
+ != APR_SUCCESS)) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Parent: Unable to connect child stdout to NUL.");
+ apr_pool_destroy(ptemp);
+ return -1;
+ }
+
+ /* Connect the child's initial stderr to our main server error log
+ * or share our own stderr handle.
+ */
+ if (ap_server_conf->error_log) {
+ child_err = ap_server_conf->error_log;
+ }
+ else {
+ rv = apr_file_open_stderr(&child_err, ptemp);
+ }
+ if (rv == APR_SUCCESS) {
+ if ((rv = apr_procattr_child_err_set(attr, child_err, NULL))
+ != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Parent: Unable to connect child stderr.");
+ apr_pool_destroy(ptemp);
+ return -1;
+ }
+ }
+
+ /* Create the child_ready_event */
+ waitlist[waitlist_ready] = CreateEvent(NULL, TRUE, FALSE, NULL);
+ if (!waitlist[waitlist_ready]) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Parent: Could not create ready event for child process");
+ apr_pool_destroy (ptemp);
+ return -1;
+ }
+
+ /* Create the child_exit_event */
+ hExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+ if (!hExitEvent) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "Parent: Could not create exit event for child process");
+ apr_pool_destroy(ptemp);
+ CloseHandle(waitlist[waitlist_ready]);
+ return -1;
+ }
+
+ if (!env)
+ {
+ /* Build the env array, only once since it won't change
+ * for the lifetime of this parent process.
+ */
+ int envc;
+ for (envc = 0; _environ[envc]; ++envc) {
+ ;
+ }
+ env = malloc((envc + 2) * sizeof (char*));
+ memcpy(env, _environ, envc * sizeof (char*));
+ apr_snprintf(pidbuf, sizeof(pidbuf), "AP_PARENT_PID=%i", parent_pid);
+ env[envc] = pidbuf;
+ env[envc + 1] = NULL;
+ }
+
+ rv = apr_proc_create(&new_child, cmd, args, env, attr, ptemp);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "Parent: Failed to create the child process.");
+ apr_pool_destroy(ptemp);
+ CloseHandle(hExitEvent);
+ CloseHandle(waitlist[waitlist_ready]);
+ CloseHandle(new_child.hproc);
+ return -1;
+ }
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Parent: Created child process %d", new_child.pid);
+
+ if (send_handles_to_child(ptemp, waitlist[waitlist_ready], hExitEvent,
+ start_mutex, ap_scoreboard_shm,
+ new_child.hproc, new_child.in)) {
+ /*
+ * This error is fatal, mop up the child and move on
+ * We toggle the child's exit event to cause this child
+ * to quit even as it is attempting to start.
+ */
+ SetEvent(hExitEvent);
+ apr_pool_destroy(ptemp);
+ CloseHandle(hExitEvent);
+ CloseHandle(waitlist[waitlist_ready]);
+ CloseHandle(new_child.hproc);
+ return -1;
+ }
+
+ /* Important:
+ * Give the child process a chance to run before dup'ing the sockets.
+ * We have already set the listening sockets noninheritable, but if
+ * WSADuplicateSocket runs before the child process initializes
+ * the listeners will be inherited anyway.
+ */
+ waitlist[waitlist_term] = new_child.hproc;
+ rv = WaitForMultipleObjects(2, waitlist, FALSE, INFINITE);
+ CloseHandle(waitlist[waitlist_ready]);
+ if (rv != WAIT_OBJECT_0) {
+ /*
+ * Outch... that isn't a ready signal. It's dead, Jim!
+ */
+ SetEvent(hExitEvent);
+ apr_pool_destroy(ptemp);
+ CloseHandle(hExitEvent);
+ CloseHandle(new_child.hproc);
+ return -1;
+ }
+
+ if (send_listeners_to_child(ptemp, new_child.pid, new_child.in)) {
+ /*
+ * This error is fatal, mop up the child and move on
+ * We toggle the child's exit event to cause this child
+ * to quit even as it is attempting to start.
+ */
+ SetEvent(hExitEvent);
+ apr_pool_destroy(ptemp);
+ CloseHandle(hExitEvent);
+ CloseHandle(new_child.hproc);
+ return -1;
+ }
+
+ *child_exit_event = hExitEvent;
+ *child_proc = new_child.hproc;
+ *child_pid = new_child.pid;
+
+ return 0;
+}
+
+/***********************************************************************
+ * master_main()
+ * master_main() runs in the parent process. It creates the child
+ * process which handles HTTP requests then waits on one of three
+ * events:
+ *
+ * restart_event
+ * -------------
+ * The restart event causes master_main to start a new child process and
+ * tells the old child process to exit (by setting the child_exit_event).
+ * The restart event is set as a result of one of the following:
+ * 1. An apache -k restart command on the command line
+ * 2. A command received from Windows service manager which gets
+ * translated into an ap_signal_parent(SIGNAL_PARENT_RESTART)
+ * call by code in service.c.
+ * 3. The child process calling ap_signal_parent(SIGNAL_PARENT_RESTART)
+ * as a result of hitting MaxRequestsPerChild.
+ *
+ * shutdown_event
+ * --------------
+ * The shutdown event causes master_main to tell the child process to
+ * exit and that the server is shutting down. The shutdown event is
+ * set as a result of one of the following:
+ * 1. An apache -k shutdown command on the command line
+ * 2. A command received from Windows service manager which gets
+ * translated into an ap_signal_parent(SIGNAL_PARENT_SHUTDOWN)
+ * call by code in service.c.
+ *
+ * child process handle
+ * --------------------
+ * The child process handle will be signaled if the child process
+ * exits for any reason. In a normal running server, the signaling
+ * of this event means that the child process has exited prematurely
+ * due to a seg fault or other irrecoverable error. For server
+ * robustness, master_main will restart the child process under this
+ * condtion.
+ *
+ * master_main uses the child_exit_event to signal the child process
+ * to exit.
+ **********************************************************************/
+#define NUM_WAIT_HANDLES 3
+#define CHILD_HANDLE 0
+#define SHUTDOWN_HANDLE 1
+#define RESTART_HANDLE 2
+static int master_main(server_rec *s, HANDLE shutdown_event, HANDLE restart_event)
+{
+ int rv, cld;
+ int restart_pending;
+ int shutdown_pending;
+ HANDLE child_exit_event;
+ HANDLE event_handles[NUM_WAIT_HANDLES];
+ DWORD child_pid;
+
+ restart_pending = shutdown_pending = 0;
+
+ event_handles[SHUTDOWN_HANDLE] = shutdown_event;
+ event_handles[RESTART_HANDLE] = restart_event;
+
+ /* Create a single child process */
+ rv = create_process(pconf, &event_handles[CHILD_HANDLE],
+ &child_exit_event, &child_pid);
+ if (rv < 0)
+ {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "master_main: create child process failed. Exiting.");
+ shutdown_pending = 1;
+ goto die_now;
+ }
+ if (!strcasecmp(signal_arg, "runservice")) {
+ mpm_service_started();
+ }
+
+ /* Update the scoreboard. Note that there is only a single active
+ * child at once.
+ */
+ ap_scoreboard_image->parent[0].quiescing = 0;
+ ap_scoreboard_image->parent[0].pid = child_pid;
+
+ /* Wait for shutdown or restart events or for child death */
+ winnt_mpm_state = AP_MPMQ_RUNNING;
+ rv = WaitForMultipleObjects(NUM_WAIT_HANDLES, (HANDLE *) event_handles, FALSE, INFINITE);
+ cld = rv - WAIT_OBJECT_0;
+ if (rv == WAIT_FAILED) {
+ /* Something serious is wrong */
+ ap_log_error(APLOG_MARK,APLOG_CRIT, apr_get_os_error(), ap_server_conf,
+ "master_main: WaitForMultipeObjects WAIT_FAILED -- doing server shutdown");
+ shutdown_pending = 1;
+ }
+ else if (rv == WAIT_TIMEOUT) {
+ /* Hey, this cannot happen */
+ ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
+ "master_main: WaitForMultipeObjects with INFINITE wait exited with WAIT_TIMEOUT");
+ shutdown_pending = 1;
+ }
+ else if (cld == SHUTDOWN_HANDLE) {
+ /* shutdown_event signalled */
+ shutdown_pending = 1;
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, APR_SUCCESS, s,
+ "Parent: Received shutdown signal -- Shutting down the server.");
+ if (ResetEvent(shutdown_event) == 0) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
+ "ResetEvent(shutdown_event)");
+ }
+ }
+ else if (cld == RESTART_HANDLE) {
+ /* Received a restart event. Prepare the restart_event to be reused
+ * then signal the child process to exit.
+ */
+ restart_pending = 1;
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
+ "Parent: Received restart signal -- Restarting the server.");
+ if (ResetEvent(restart_event) == 0) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
+ "Parent: ResetEvent(restart_event) failed.");
+ }
+ if (SetEvent(child_exit_event) == 0) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
+ "Parent: SetEvent for child process %d failed.",
+ event_handles[CHILD_HANDLE]);
+ }
+ /* Don't wait to verify that the child process really exits,
+ * just move on with the restart.
+ */
+ CloseHandle(event_handles[CHILD_HANDLE]);
+ event_handles[CHILD_HANDLE] = NULL;
+ }
+ else {
+ /* The child process exited prematurely due to a fatal error. */
+ DWORD exitcode;
+ if (!GetExitCodeProcess(event_handles[CHILD_HANDLE], &exitcode)) {
+ /* HUH? We did exit, didn't we? */
+ exitcode = APEXIT_CHILDFATAL;
+ }
+ if ( exitcode == APEXIT_CHILDFATAL
+ || exitcode == APEXIT_CHILDINIT
+ || exitcode == APEXIT_INIT) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, 0, ap_server_conf,
+ "Parent: child process exited with status %u -- Aborting.", exitcode);
+ shutdown_pending = 1;
+ }
+ else {
+ int i;
+ restart_pending = 1;
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Parent: child process exited with status %u -- Restarting.", exitcode);
+ for (i = 0; i < ap_threads_per_child; i++) {
+ ap_update_child_status_from_indexes(0, i, SERVER_DEAD, NULL);
+ }
+ }
+ CloseHandle(event_handles[CHILD_HANDLE]);
+ event_handles[CHILD_HANDLE] = NULL;
+ }
+ if (restart_pending) {
+ ++ap_my_generation;
+ ap_scoreboard_image->global->running_generation = ap_my_generation;
+ }
+die_now:
+ if (shutdown_pending)
+ {
+ int timeout = 30000; /* Timeout is milliseconds */
+ winnt_mpm_state = AP_MPMQ_STOPPING;
+
+ /* This shutdown is only marginally graceful. We will give the
+ * child a bit of time to exit gracefully. If the time expires,
+ * the child will be wacked.
+ */
+ if (!strcasecmp(signal_arg, "runservice")) {
+ mpm_service_stopping();
+ }
+ /* Signal the child processes to exit */
+ if (SetEvent(child_exit_event) == 0) {
+ ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), ap_server_conf,
+ "Parent: SetEvent for child process %d failed", event_handles[CHILD_HANDLE]);
+ }
+ if (event_handles[CHILD_HANDLE]) {
+ rv = WaitForSingleObject(event_handles[CHILD_HANDLE], timeout);
+ if (rv == WAIT_OBJECT_0) {
+ ap_log_error(APLOG_MARK,APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Parent: Child process exited successfully.");
+ CloseHandle(event_handles[CHILD_HANDLE]);
+ event_handles[CHILD_HANDLE] = NULL;
+ }
+ else {
+ ap_log_error(APLOG_MARK,APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Parent: Forcing termination of child process %d ", event_handles[CHILD_HANDLE]);
+ TerminateProcess(event_handles[CHILD_HANDLE], 1);
+ CloseHandle(event_handles[CHILD_HANDLE]);
+ event_handles[CHILD_HANDLE] = NULL;
+ }
+ }
+ return 0; /* Tell the caller we do not want to restart */
+ }
+ winnt_mpm_state = AP_MPMQ_STARTING;
+ return 1; /* Tell the caller we want a restart */
+}
+
+/* service_nt_main_fn needs to append the StartService() args
+ * outside of our call stack and thread as the service starts...
+ */
+apr_array_header_t *mpm_new_argv;
+
+/* Remember service_to_start failures to log and fail in pre_config.
+ * Remember inst_argc and inst_argv for installing or starting the
+ * service after we preflight the config.
+ */
+
+AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
+{
+ switch(query_code){
+ case AP_MPMQ_MAX_DAEMON_USED:
+ *result = MAXIMUM_WAIT_OBJECTS;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_THREADED:
+ *result = AP_MPMQ_STATIC;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_FORKED:
+ *result = AP_MPMQ_NOT_SUPPORTED;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_DAEMONS:
+ *result = HARD_SERVER_LIMIT;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_THREADS:
+ *result = thread_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_THREADS:
+ *result = ap_threads_per_child;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_THREADS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_THREADS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_REQUESTS_DAEMON:
+ *result = ap_max_requests_per_child;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MPM_STATE:
+ *result = winnt_mpm_state;
+ return APR_SUCCESS;
+ }
+ return APR_ENOTIMPL;
+}
+
+#define SERVICE_UNSET (-1)
+static apr_status_t service_set = SERVICE_UNSET;
+static apr_status_t service_to_start_success;
+static int inst_argc;
+static const char * const *inst_argv;
+static char *service_name = NULL;
+
+void winnt_rewrite_args(process_rec *process)
+{
+ /* Handle the following SCM aspects in this phase:
+ *
+ * -k runservice [transition for WinNT, nothing for Win9x]
+ * -k install
+ * -k config
+ * -k uninstall
+ * -k stop
+ * -k shutdown (same as -k stop). Maintained for backward compatability.
+ *
+ * We can't leave this phase until we know our identity
+ * and modify the command arguments appropriately.
+ *
+ * We do not care if the .conf file exists or is parsable when
+ * attempting to stop or uninstall a service.
+ */
+ apr_status_t rv;
+ char *def_server_root;
+ char *binpath;
+ char optbuf[3];
+ const char *optarg;
+ int fixed_args;
+ char *pid;
+ apr_getopt_t *opt;
+ int running_as_service = 1;
+ int errout = 0;
+
+ pconf = process->pconf;
+
+ osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+ GetVersionEx(&osver);
+
+ /* AP_PARENT_PID is only valid in the child */
+ pid = getenv("AP_PARENT_PID");
+ if (pid)
+ {
+ /* This is the child */
+ my_pid = GetCurrentProcessId();
+ parent_pid = (DWORD) atol(pid);
+
+ /* Prevent holding open the (nonexistant) console */
+ ap_real_exit_code = 0;
+
+ /* The parent is responsible for providing the
+ * COMPLETE ARGUMENTS REQUIRED to the child.
+ *
+ * No further argument parsing is needed, but
+ * for good measure we will provide a simple
+ * signal string for later testing.
+ */
+ signal_arg = "runchild";
+ return;
+ }
+
+ /* This is the parent, we have a long way to go :-) */
+ parent_pid = my_pid = GetCurrentProcessId();
+
+ /* This behavior is voided by setting real_exit_code to 0 */
+ atexit(hold_console_open_on_error);
+
+ /* Rewrite process->argv[];
+ *
+ * strip out -k signal into signal_arg
+ * strip out -n servicename and set the names
+ * add default -d serverroot from the path of this executable
+ *
+ * The end result will look like:
+ *
+ * The invocation command (%0)
+ * The -d serverroot default from the running executable
+ * The requested service's (-n) registry ConfigArgs
+ * The WinNT SCM's StartService() args
+ */
+ if ((rv = ap_os_proc_filepath(&binpath, process->pconf))
+ != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK,APLOG_CRIT, rv, NULL,
+ "Failed to get the full path of %s", process->argv[0]);
+ exit(APEXIT_INIT);
+ }
+ /* WARNING: There is an implict assumption here that the
+ * executable resides in ServerRoot or ServerRoot\bin
+ */
+ def_server_root = (char *) apr_filepath_name_get(binpath);
+ if (def_server_root > binpath) {
+ *(def_server_root - 1) = '\0';
+ def_server_root = (char *) apr_filepath_name_get(binpath);
+ if (!strcasecmp(def_server_root, "bin"))
+ *(def_server_root - 1) = '\0';
+ }
+ apr_filepath_merge(&def_server_root, NULL, binpath,
+ APR_FILEPATH_TRUENAME, process->pool);
+
+ /* 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_array_make(process->pool, process->argc + 2,
+ sizeof(const char *));
+ *(const char **)apr_array_push(mpm_new_argv) = process->argv[0];
+ *(const char **)apr_array_push(mpm_new_argv) = "-d";
+ *(const char **)apr_array_push(mpm_new_argv) = def_server_root;
+
+ fixed_args = mpm_new_argv->nelts;
+
+ optbuf[0] = '-';
+ optbuf[2] = '\0';
+ apr_getopt_init(&opt, process->pool, process->argc, (char**) process->argv);
+ opt->errfn = NULL;
+ while ((rv = apr_getopt(opt, "wn:k:" AP_SERVER_BASEARGS,
+ optbuf + 1, &optarg)) == APR_SUCCESS) {
+ switch (optbuf[1]) {
+
+ /* Shortcuts; include the -w option to hold the window open on error.
+ * This must not be toggled once we reset ap_real_exit_code to 0!
+ */
+ case 'w':
+ if (ap_real_exit_code)
+ ap_real_exit_code = 2;
+ break;
+
+ case 'n':
+ service_set = mpm_service_set_name(process->pool, &service_name,
+ optarg);
+ break;
+
+ case 'k':
+ signal_arg = optarg;
+ break;
+
+ case 'E':
+ errout = 1;
+ /* Fall through so the Apache main() handles the 'E' arg */
+ default:
+ *(const char **)apr_array_push(mpm_new_argv) =
+ apr_pstrdup(process->pool, optbuf);
+
+ if (optarg) {
+ *(const char **)apr_array_push(mpm_new_argv) = optarg;
+ }
+ break;
+ }
+ }
+
+ /* back up to capture the bad argument */
+ if (rv == APR_BADCH || rv == APR_BADARG) {
+ opt->ind--;
+ }
+
+ while (opt->ind < opt->argc) {
+ *(const char **)apr_array_push(mpm_new_argv) =
+ apr_pstrdup(process->pool, opt->argv[opt->ind++]);
+ }
+
+ /* 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.
+ *
+ * The SCM will generally invoke the executable with
+ * the c:\win\system32 default directory. This is very
+ * lethal if folks use ServerRoot /foopath on windows
+ * without a drive letter. Change to the default root
+ * (path to apache root, above /bin) for safety.
+ */
+ apr_filepath_set(def_server_root, process->pool);
+
+ /* 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)
+ {
+ if (!errout) {
+ mpm_nt_eventlog_stderr_open(service_name, process->pool);
+ }
+ service_to_start_success = mpm_service_to_start(&service_name,
+ process->pool);
+ if (service_to_start_success == APR_SUCCESS) {
+ service_set = APR_SUCCESS;
+ }
+ }
+ }
+
+ /* Get the default for any -k option, except run */
+ if (service_set == SERVICE_UNSET && strcasecmp(signal_arg, "run")) {
+ service_set = mpm_service_set_name(process->pool, &service_name,
+ AP_DEFAULT_SERVICE_NAME);
+ }
+
+ if (!strcasecmp(signal_arg, "install")) /* -k install */
+ {
+ if (service_set == APR_SUCCESS)
+ {
+ ap_log_error(APLOG_MARK,APLOG_ERR, 0, NULL,
+ "%s: Service is already installed.", service_name);
+ exit(APEXIT_INIT);
+ }
+ }
+ else if (running_as_service)
+ {
+ if (service_set == APR_SUCCESS)
+ {
+ /* Attempt to Uninstall, or stop, before
+ * we can read the arguments or .conf files
+ */
+ if (!strcasecmp(signal_arg, "uninstall")) {
+ rv = mpm_service_uninstall();
+ exit(rv);
+ }
+
+ if ((!strcasecmp(signal_arg, "stop")) ||
+ (!strcasecmp(signal_arg, "shutdown"))) {
+ mpm_signal_service(process->pool, 0);
+ exit(0);
+ }
+
+ rv = mpm_merge_service_args(process->pool, mpm_new_argv,
+ fixed_args);
+ if (rv == APR_SUCCESS) {
+ ap_log_error(APLOG_MARK,APLOG_INFO, 0, NULL,
+ "Using ConfigArgs of the installed service "
+ "\"%s\".", service_name);
+ }
+ else {
+ ap_log_error(APLOG_MARK,APLOG_WARNING, rv, NULL,
+ "No installed ConfigArgs for the service "
+ "\"%s\", using Apache defaults.", service_name);
+ }
+ }
+ else
+ {
+ ap_log_error(APLOG_MARK,APLOG_ERR, service_set, NULL,
+ "No installed service named \"%s\".", service_name);
+ exit(APEXIT_INIT);
+ }
+ }
+ if (strcasecmp(signal_arg, "install") && service_set && service_set != SERVICE_UNSET)
+ {
+ ap_log_error(APLOG_MARK,APLOG_ERR, service_set, NULL,
+ "No installed service named \"%s\".", service_name);
+ exit(APEXIT_INIT);
+ }
+
+ /* 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;
+
+ /* Now, do service install or reconfigure then proceed to
+ * post_config to test the installed configuration.
+ */
+ if (!strcasecmp(signal_arg, "config")) { /* -k config */
+ /* Reconfigure the service */
+ rv = mpm_service_install(process->pool, inst_argc, inst_argv, 1);
+ if (rv != APR_SUCCESS) {
+ exit(rv);
+ }
+
+ fprintf(stderr,"Testing httpd.conf....\n");
+ fprintf(stderr,"Errors reported here must be corrected before the "
+ "service can be started.\n");
+ }
+ else if (!strcasecmp(signal_arg, "install")) { /* -k install */
+ /* Install the service */
+ rv = mpm_service_install(process->pool, inst_argc, inst_argv, 0);
+ if (rv != APR_SUCCESS) {
+ exit(rv);
+ }
+
+ fprintf(stderr,"Testing httpd.conf....\n");
+ fprintf(stderr,"Errors reported here must be corrected before the "
+ "service can be started.\n");
+ }
+
+ process->argc = mpm_new_argv->nelts;
+ process->argv = (const char * const *) mpm_new_argv->elts;
+}
+
+
+static int 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]
+ */
+
+ /* Initialize shared static objects.
+ * TODO: Put config related statics into an sconf structure.
+ */
+ pconf = pconf_;
+
+ if (ap_exists_config_define("ONE_PROCESS") ||
+ ap_exists_config_define("DEBUG"))
+ 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_CRIT, service_to_start_success, NULL,
+ "%s: Unable to start the service manager.",
+ service_name);
+ exit(APEXIT_INIT);
+ }
+
+ /* Win9x: disable AcceptEx */
+ if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
+ use_acceptex = 0;
+ }
+
+ ap_listen_pre_config();
+ ap_threads_per_child = DEFAULT_THREADS_PER_CHILD;
+ ap_pid_fname = DEFAULT_PIDLOG;
+ ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
+#ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
+ ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
+#endif
+ /* use_acceptex which is enabled by default is not available on Win9x.
+ */
+ if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
+ use_acceptex = 0;
+ }
+
+ apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
+
+ return OK;
+}
+
+static int winnt_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec* s)
+{
+ static int restart_num = 0;
+ apr_status_t rv = 0;
+
+ /* Handle the following SCM aspects in this phase:
+ *
+ * -k install (catch and exit as install was handled in rewrite_args)
+ * -k config (catch and exit as config was handled in rewrite_args)
+ * -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")) {
+ /* Service install happens in the rewrite_args hooks. If we
+ * made it this far, the server configuration is clean and the
+ * service will successfully start.
+ */
+ apr_pool_destroy(s->process->pool);
+ apr_terminate();
+ exit(0);
+ }
+ if (!strcasecmp(signal_arg, "config")) {
+ /* Service reconfiguration happens in the rewrite_args hooks. If we
+ * made it this far, the server configuration is clean and the
+ * service will successfully start.
+ */
+ apr_pool_destroy(s->process->pool);
+ apr_terminate();
+ exit(0);
+ }
+
+ if (!strcasecmp(signal_arg, "start")) {
+ ap_listen_rec *lr;
+
+ /* Close the listening sockets. */
+ for (lr = ap_listeners; lr; lr = lr->next) {
+ apr_socket_close(lr->sd);
+ lr->active = 0;
+ }
+ rv = mpm_service_start(ptemp, inst_argc, inst_argv);
+ apr_pool_destroy(s->process->pool);
+ apr_terminate();
+ exit (rv);
+ }
+
+ if (!strcasecmp(signal_arg, "restart")) {
+ mpm_signal_service(ptemp, 1);
+ apr_pool_destroy(s->process->pool);
+ apr_terminate();
+ 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));
+
+ 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_CRIT, apr_get_os_error(), ap_server_conf,
+ "Parent: Cannot create shutdown event %s", signal_shutdown_name);
+ CleanNullACL((void *)sa);
+ return HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ /* 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_CRIT, apr_get_os_error(), ap_server_conf,
+ "Parent: Cannot create restart event %s", signal_restart_name);
+ CleanNullACL((void *)sa);
+ return HTTP_INTERNAL_SERVER_ERROR;
+ }
+ 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(&service_name,
+ s->process->pool);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK,APLOG_ERR, rv, ap_server_conf,
+ "%s: Unable to start the service manager.",
+ service_name);
+ return HTTP_INTERNAL_SERVER_ERROR;
+ }
+ }
+ }
+ else /* ! -k runservice */
+ {
+ mpm_start_console_handler();
+ }
+
+ /* Create the start mutex, as an unnamed object for security.
+ * Ths start mutex is used during a restart to prevent more than
+ * one child process from entering the accept loop at once.
+ */
+ rv = apr_proc_mutex_create(&start_mutex, NULL,
+ APR_LOCK_DEFAULT,
+ ap_server_conf->process->pool);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK,APLOG_ERR, rv, ap_server_conf,
+ "%s: Unable to create the start_mutex.",
+ service_name);
+ return HTTP_INTERNAL_SERVER_ERROR;
+ }
+ }
+ }
+ else /* parent_pid != my_pid */
+ {
+ mpm_start_child_console_handler();
+ }
+ return OK;
+}
+
+/* This really should be a post_config hook, but the error log is already
+ * redirected by that point, so we need to do this in the open_logs phase.
+ */
+static int winnt_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
+{
+ /* Initialize shared static objects.
+ */
+ ap_server_conf = s;
+
+ if (parent_pid != my_pid) {
+ return OK;
+ }
+
+ /* We cannot initialize our listeners if we are restarting
+ * (the parent process already has glomed on to them)
+ * nor should we do so for service reconfiguration
+ * (since the service may already be running.)
+ */
+ if (!strcasecmp(signal_arg, "restart")
+ || !strcasecmp(signal_arg, "config")) {
+ return OK;
+ }
+
+ if (ap_setup_listeners(s) < 1) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0,
+ NULL, "no listening sockets available, shutting down");
+ return DONE;
+ }
+
+ return OK;
+}
+
+static void winnt_child_init(apr_pool_t *pchild, struct server_rec *s)
+{
+ apr_status_t rv;
+
+ setup_signal_names(apr_psprintf(pchild,"ap%d", parent_pid));
+
+ /* This is a child process, not in single process mode */
+ if (!one_process) {
+ /* Set up events and the scoreboard */
+ get_handles_from_parent(s, &exit_event, &start_mutex,
+ &ap_scoreboard_shm);
+
+ /* Set up the listeners */
+ get_listeners_from_parent(s);
+
+ ap_my_generation = ap_scoreboard_image->global->running_generation;
+ }
+ else {
+ /* Single process mode - this lock doesn't even need to exist */
+ rv = apr_proc_mutex_create(&start_mutex, signal_name_prefix,
+ APR_LOCK_DEFAULT, s->process->pool);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK,APLOG_ERR, rv, ap_server_conf,
+ "%s child %d: Unable to init the start_mutex.",
+ service_name, my_pid);
+ exit(APEXIT_CHILDINIT);
+ }
+
+ /* Borrow the shutdown_even as our _child_ loop exit event */
+ exit_event = shutdown_event;
+ }
+}
+
+
+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" */
+
+ if (!restart) {
+ first_thread_limit = thread_limit;
+ }
+
+ if (changed_limit_at_restart) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, ap_server_conf,
+ "WARNING: Attempt to change ThreadLimit ignored "
+ "during restart");
+ changed_limit_at_restart = 0;
+ }
+
+ /* ### If non-graceful restarts are ever introduced - we need to rerun
+ * the pre_mpm hook on subsequent non-graceful restarts. But Win32
+ * has only graceful style restarts - and we need this hook to act
+ * the same on Win32 as on Unix.
+ */
+ if (!restart && ((parent_pid == my_pid) || one_process)) {
+ /* Set up the scoreboard. */
+ if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
+ return 1;
+ }
+ }
+
+ if ((parent_pid != my_pid) || one_process)
+ {
+ /* The child process or in one_process (debug) mode
+ */
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Child %d: Child process is running", my_pid);
+
+ child_main(pconf);
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, APR_SUCCESS, ap_server_conf,
+ "Child %d: Child process is exiting", my_pid);
+ return 1;
+ }
+ else
+ {
+ /* A real-honest to goodness parent */
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "%s configured -- resuming normal operations",
+ ap_get_server_version());
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "Server built: %s", ap_get_server_built());
+
+ restart = master_main(ap_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_INFO, APR_SUCCESS,
+ ap_server_conf, "removed PID file %s (pid=%ld)",
+ pidfile, GetCurrentProcessId());
+ }
+ apr_proc_mutex_destroy(start_mutex);
+
+ CloseHandle(restart_event);
+ CloseHandle(shutdown_event);
+
+ return 1;
+ }
+ }
+
+ return 0; /* Restart */
+}
+
+static void winnt_hooks(apr_pool_t *p)
+{
+ /* The prefork open_logs phase must run before the core's, or stderr
+ * will be redirected to a file, and the messages won't print to the
+ * console.
+ */
+ static const char *const aszSucc[] = {"core.c", NULL};
+
+ ap_hook_pre_config(winnt_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
+ ap_hook_post_config(winnt_post_config, NULL, NULL, 0);
+ ap_hook_child_init(winnt_child_init, NULL, NULL, APR_HOOK_MIDDLE);
+ ap_hook_open_logs(winnt_open_logs, NULL, aszSucc, APR_HOOK_MIDDLE);
+}
+
+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 */
+};
+
+#endif /* def WIN32 */
diff --git a/server/mpm/winnt/mpm_winnt.h b/server/mpm/winnt/mpm_winnt.h
new file mode 100644
index 00000000..2d9a7526
--- /dev/null
+++ b/server/mpm/winnt/mpm_winnt.h
@@ -0,0 +1,130 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file mpm_winnt.h
+ * @brief WinNT MPM specific
+ *
+ * @addtogroup APACHE_MPM_WINNT
+ * @{
+ */
+
+#ifndef APACHE_MPM_WINNT_H
+#define APACHE_MPM_WINNT_H
+
+#include "ap_listen.h"
+
+/* From service.c: */
+
+#define SERVICE_APACHE_RESTART 128
+
+#ifndef AP_DEFAULT_SERVICE_NAME
+#define AP_DEFAULT_SERVICE_NAME "Apache2"
+#endif
+
+#define SERVICECONFIG9X "Software\\Microsoft\\Windows\\CurrentVersion\\RunServices"
+#define SERVICECONFIG "System\\CurrentControlSet\\Services\\%s"
+#define SERVICEPARAMS "System\\CurrentControlSet\\Services\\%s\\Parameters"
+
+apr_status_t mpm_service_set_name(apr_pool_t *p, const char **display_name,
+ const char *set_name);
+apr_status_t mpm_merge_service_args(apr_pool_t *p, apr_array_header_t *args,
+ int fixed_args);
+
+apr_status_t mpm_service_to_start(const char **display_name, apr_pool_t *p);
+apr_status_t mpm_service_started(void);
+apr_status_t mpm_service_install(apr_pool_t *ptemp, int argc,
+ char const* const* argv, int reconfig);
+apr_status_t mpm_service_uninstall(void);
+
+apr_status_t mpm_service_start(apr_pool_t *ptemp, int argc,
+ char const* const* argv);
+
+void mpm_signal_service(apr_pool_t *ptemp, int signal);
+
+void mpm_service_stopping(void);
+
+void mpm_start_console_handler(void);
+void mpm_start_child_console_handler(void);
+
+/* From nt_eventlog.c: */
+
+void mpm_nt_eventlog_stderr_open(char *display_name, apr_pool_t *p);
+void mpm_nt_eventlog_stderr_flush(void);
+
+/* From winnt.c: */
+
+extern int use_acceptex;
+extern int winnt_mpm_state;
+extern OSVERSIONINFO osver;
+extern void clean_child_exit(int);
+
+void setup_signal_names(char *prefix);
+
+typedef enum {
+ SIGNAL_PARENT_SHUTDOWN,
+ SIGNAL_PARENT_RESTART,
+ SIGNAL_PARENT_RESTART_GRACEFUL
+} ap_signal_parent_e;
+AP_DECLARE(void) ap_signal_parent(ap_signal_parent_e type);
+
+/*
+ * The Windoes MPM uses a queue of completion contexts that it passes
+ * between the accept threads and the worker threads. Declare the
+ * functions to access the queue and the structures passed on the
+ * queue in the header file to enable modules to access them
+ * if necessary. The queue resides in the MPM.
+ */
+#ifdef CONTAINING_RECORD
+#undef CONTAINING_RECORD
+#endif
+#define CONTAINING_RECORD(address, type, field) ((type *)( \
+ (PCHAR)(address) - \
+ (PCHAR)(&((type *)0)->field)))
+#if APR_HAVE_IPV6
+#define PADDED_ADDR_SIZE (sizeof(SOCKADDR_IN6)+16)
+#else
+#define PADDED_ADDR_SIZE (sizeof(SOCKADDR_IN)+16)
+#endif
+
+typedef struct CompContext {
+ struct CompContext *next;
+ OVERLAPPED Overlapped;
+ apr_socket_t *sock;
+ SOCKET accept_socket;
+ char buff[2*PADDED_ADDR_SIZE];
+ struct sockaddr *sa_server;
+ int sa_server_len;
+ struct sockaddr *sa_client;
+ int sa_client_len;
+ apr_pool_t *ptrans;
+ apr_bucket_alloc_t *ba;
+ short socket_family;
+} COMP_CONTEXT, *PCOMP_CONTEXT;
+
+typedef enum {
+ IOCP_CONNECTION_ACCEPTED = 1,
+ IOCP_WAIT_FOR_RECEIVE = 2,
+ IOCP_WAIT_FOR_TRANSMITFILE = 3,
+ IOCP_SHUTDOWN = 4
+} io_state_e;
+
+PCOMP_CONTEXT mpm_get_completion_context(void);
+void mpm_recycle_completion_context(PCOMP_CONTEXT pCompContext);
+apr_status_t mpm_post_completion_context(PCOMP_CONTEXT pCompContext, io_state_e state);
+void hold_console_open_on_error(void);
+#endif /* APACHE_MPM_WINNT_H */
+/** @} */
diff --git a/server/mpm/winnt/nt_eventlog.c b/server/mpm/winnt/nt_eventlog.c
new file mode 100644
index 00000000..a0ae68c9
--- /dev/null
+++ b/server/mpm/winnt/nt_eventlog.c
@@ -0,0 +1,189 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define CORE_PRIVATE
+
+#include "httpd.h"
+#include "http_log.h"
+#include "mpm_winnt.h"
+#include "apr_strings.h"
+#include "apr_lib.h"
+#include "ap_regkey.h"
+
+static char *display_name = NULL;
+static HANDLE stderr_thread = NULL;
+static HANDLE stderr_ready;
+
+static DWORD WINAPI service_stderr_thread(LPVOID hPipe)
+{
+ HANDLE hPipeRead = (HANDLE) hPipe;
+ HANDLE hEventSource;
+ char errbuf[256];
+ char *errmsg = errbuf;
+ const char *errarg[9];
+ DWORD errres;
+ ap_regkey_t *regkey;
+ apr_status_t rv;
+ apr_pool_t *p;
+
+ apr_pool_create_ex(&p, NULL, NULL, NULL);
+
+ errarg[0] = "The Apache service named";
+ errarg[1] = display_name;
+ errarg[2] = "reported the following error:\r\n>>>";
+ errarg[3] = errbuf;
+ errarg[4] = NULL;
+ errarg[5] = NULL;
+ errarg[6] = NULL;
+ errarg[7] = NULL;
+ errarg[8] = NULL;
+
+ /* What are we going to do in here, bail on the user? not. */
+ if ((rv = ap_regkey_open(&regkey, AP_REGKEY_LOCAL_MACHINE,
+ "SYSTEM\\CurrentControlSet\\Services\\"
+ "EventLog\\Application\\Apache Service",
+ APR_READ | APR_WRITE | APR_CREATE, p))
+ == APR_SUCCESS)
+ {
+ DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
+ EVENTLOG_INFORMATION_TYPE;
+
+ /* The stock message file */
+ ap_regkey_value_set(regkey, "EventMessageFile",
+ "%SystemRoot%\\System32\\netmsg.dll",
+ AP_REGKEY_EXPAND, p);
+
+ ap_regkey_value_raw_set(regkey, "TypesSupported", &dwData,
+ sizeof(dwData), REG_DWORD, p);
+ ap_regkey_close(regkey);
+ }
+
+ hEventSource = RegisterEventSourceW(NULL, L"Apache Service");
+
+ SetEvent(stderr_ready);
+
+ while (ReadFile(hPipeRead, errmsg, 1, &errres, NULL) && (errres == 1))
+ {
+ if ((errmsg > errbuf) || !apr_isspace(*errmsg))
+ {
+ ++errmsg;
+ if ((*(errmsg - 1) == '\n')
+ || (errmsg >= errbuf + sizeof(errbuf) - 1))
+ {
+ while ((errmsg > errbuf) && apr_isspace(*(errmsg - 1))) {
+ --errmsg;
+ }
+ *errmsg = '\0';
+
+ /* Generic message: '%1 %2 %3 %4 %5 %6 %7 %8 %9'
+ * The event code in netmsg.dll is 3299
+ */
+ ReportEvent(hEventSource, EVENTLOG_ERROR_TYPE, 0,
+ 3299, NULL, 9, 0, errarg, NULL);
+ errmsg = errbuf;
+ }
+ }
+ }
+
+ if ((errres = GetLastError()) != ERROR_BROKEN_PIPE) {
+ apr_snprintf(errbuf, sizeof(errbuf),
+ "Win32 error %d reading stderr pipe stream\r\n",
+ GetLastError());
+
+ ReportEvent(hEventSource, EVENTLOG_ERROR_TYPE, 0,
+ 3299, NULL, 9, 0, errarg, NULL);
+ }
+
+ CloseHandle(hPipeRead);
+ DeregisterEventSource(hEventSource);
+ CloseHandle(stderr_thread);
+ stderr_thread = NULL;
+ apr_pool_destroy(p);
+ return 0;
+}
+
+
+void mpm_nt_eventlog_stderr_flush(void)
+{
+ HANDLE cleanup_thread = stderr_thread;
+
+ if (cleanup_thread) {
+ HANDLE hErr = GetStdHandle(STD_ERROR_HANDLE);
+ fclose(stderr);
+ CloseHandle(hErr);
+ WaitForSingleObject(cleanup_thread, 30000);
+ CloseHandle(cleanup_thread);
+ }
+}
+
+
+void mpm_nt_eventlog_stderr_open(char *argv0, apr_pool_t *p)
+{
+ SECURITY_ATTRIBUTES sa;
+ HANDLE hProc = GetCurrentProcess();
+ HANDLE hPipeRead = NULL;
+ HANDLE hPipeWrite = NULL;
+ HANDLE hDup = NULL;
+ DWORD threadid;
+ int fd;
+
+ display_name = argv0;
+
+ /* Create a pipe to send stderr messages to the system error log.
+ *
+ * _dup2() duplicates the write handle inheritable for us.
+ */
+ sa.nLength = sizeof(sa);
+ sa.lpSecurityDescriptor = NULL;
+ sa.bInheritHandle = FALSE;
+ CreatePipe(&hPipeRead, &hPipeWrite, NULL, 0);
+ ap_assert(hPipeRead && hPipeWrite);
+
+ stderr_ready = CreateEvent(NULL, FALSE, FALSE, NULL);
+ stderr_thread = CreateThread(NULL, 0, service_stderr_thread,
+ (LPVOID) hPipeRead, 0, &threadid);
+ ap_assert(stderr_ready && stderr_thread);
+
+ WaitForSingleObject(stderr_ready, INFINITE);
+
+ /* Flush stderr and unset its buffer, then commit and replace stderr.
+ * This is typically a noop for Win2K/XP since services with NULL std
+ * handles [but valid FILE *'s, oddly enough], but is required
+ * for NT 4.0 and to use this code outside of services.
+ */
+ fflush(stderr);
+ setvbuf(stderr, NULL, _IONBF, 0);
+ _commit(2 /* stderr */);
+ fd = _open_osfhandle((long) hPipeWrite,
+ _O_WRONLY | _O_BINARY);
+ _dup2(fd, 2);
+ _close(fd);
+ _setmode(2, _O_BINARY);
+
+ /* hPipeWrite was _close()'ed above, and _dup2()'ed
+ * to fd 2 creating a new, inherited Win32 handle.
+ * Recover that real handle from fd 2.
+ */
+ hPipeWrite = (HANDLE)_get_osfhandle(2);
+
+ SetStdHandle(STD_ERROR_HANDLE, hPipeWrite);
+
+ /* The code above _will_ corrupt the StdHandle...
+ * and we must do so anyways. We set this up only
+ * after we initialized the posix stderr API.
+ */
+ ap_open_stderr_log(p);
+}
diff --git a/server/mpm/winnt/service.c b/server/mpm/winnt/service.c
new file mode 100644
index 00000000..3f51aa75
--- /dev/null
+++ b/server/mpm/winnt/service.c
@@ -0,0 +1,1346 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* This module ALONE requires the window message API from user.h
+ * and the default APR include of windows.h will omit it, so
+ * preload the API symbols now...
+ */
+
+#define CORE_PRIVATE
+#define _WINUSER_
+
+#include "httpd.h"
+#include "http_log.h"
+#include "mpm_winnt.h"
+#include "apr_strings.h"
+#include "apr_lib.h"
+#include "ap_regkey.h"
+
+#ifdef NOUSER
+#undef NOUSER
+#endif
+#undef _WINUSER_
+#include <winuser.h>
+
+static char *mpm_service_name = NULL;
+static char *mpm_display_name = NULL;
+
+static struct
+{
+ HANDLE mpm_thread; /* primary thread handle of the apache server */
+ HANDLE service_thread; /* thread service/monitor handle */
+ DWORD service_thread_id;/* thread service/monitor ID */
+ HANDLE service_init; /* controller thread init mutex */
+ HANDLE service_term; /* NT service thread kill signal */
+ SERVICE_STATUS ssStatus;
+ SERVICE_STATUS_HANDLE hServiceStatus;
+} globdat;
+
+static int ReportStatusToSCMgr(int currentState, int exitCode, int waitHint);
+
+
+#define PRODREGKEY "SOFTWARE\\" AP_SERVER_BASEVENDOR "\\" \
+ AP_SERVER_BASEPRODUCT "\\" AP_SERVER_BASEREVISION
+
+/*
+ * Get the server root from the registry into 'dir' which is
+ * size bytes long. Returns 0 if the server root was found
+ * or if the serverroot key does not exist (in which case
+ * dir will contain an empty string), or -1 if there was
+ * an error getting the key.
+ */
+apr_status_t ap_registry_get_server_root(apr_pool_t *p, char **buf)
+{
+ apr_status_t rv;
+ ap_regkey_t *key;
+
+ if ((rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, PRODREGKEY,
+ APR_READ, p)) == APR_SUCCESS) {
+ rv = ap_regkey_value_get(buf, key, "ServerRoot", p);
+ ap_regkey_close(key);
+ if (rv == APR_SUCCESS)
+ return rv;
+ }
+
+ if ((rv = ap_regkey_open(&key, AP_REGKEY_CURRENT_USER, PRODREGKEY,
+ APR_READ, p)) == APR_SUCCESS) {
+ rv = ap_regkey_value_get(buf, key, "ServerRoot", p);
+ ap_regkey_close(key);
+ if (rv == APR_SUCCESS)
+ return rv;
+ }
+
+ *buf = NULL;
+ return rv;
+}
+
+
+/* The service configuration's is stored under the following trees:
+ *
+ * HKLM\System\CurrentControlSet\Services\[service name]
+ *
+ * \DisplayName
+ * \ImagePath
+ * \Parameters\ConfigArgs
+ *
+ * For Win9x, the launch service command is stored under:
+ *
+ * HKLM\Software\Microsoft\Windows\CurrentVersion\RunServices\[service name]
+ */
+
+
+/* exit() for Win32 is macro mapped (horrible, we agree) that allows us
+ * to catch the non-zero conditions and inform the console process that
+ * the application died, and hang on to the console a bit longer.
+ *
+ * The macro only maps for http_main.c and other sources that include
+ * the service.h header, so we best assume it's an error to exit from
+ * _any_ other module.
+ *
+ * If ap_real_exit_code is reset to 0, it will not be set or trigger this
+ * behavior on exit. All service and child processes are expected to
+ * reset this flag to zero to avoid undesireable side effects.
+ */
+AP_DECLARE_DATA int ap_real_exit_code = 1;
+
+void hold_console_open_on_error(void)
+{
+ HANDLE hConIn;
+ HANDLE hConErr;
+ DWORD result;
+ time_t start;
+ time_t remains;
+ char *msg = "Note the errors or messages above, "
+ "and press the <ESC> key to exit. ";
+ CONSOLE_SCREEN_BUFFER_INFO coninfo;
+ INPUT_RECORD in;
+ char count[16];
+
+ if (!ap_real_exit_code)
+ return;
+ hConIn = GetStdHandle(STD_INPUT_HANDLE);
+ hConErr = GetStdHandle(STD_ERROR_HANDLE);
+ if ((hConIn == INVALID_HANDLE_VALUE) || (hConErr == INVALID_HANDLE_VALUE))
+ return;
+ if (!WriteConsole(hConErr, msg, (DWORD)strlen(msg), &result, NULL) || !result)
+ return;
+ if (!GetConsoleScreenBufferInfo(hConErr, &coninfo))
+ return;
+ if (!SetConsoleMode(hConIn, ENABLE_MOUSE_INPUT | 0x80))
+ return;
+
+ start = time(NULL);
+ do
+ {
+ while (PeekConsoleInput(hConIn, &in, 1, &result) && result)
+ {
+ if (!ReadConsoleInput(hConIn, &in, 1, &result) || !result)
+ return;
+ if ((in.EventType == KEY_EVENT) && in.Event.KeyEvent.bKeyDown
+ && (in.Event.KeyEvent.uChar.AsciiChar == 27))
+ return;
+ if (in.EventType == MOUSE_EVENT
+ && (in.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK))
+ return;
+ }
+ remains = ((start + 30) - time(NULL));
+ sprintf (count, "%d...", remains);
+ if (!SetConsoleCursorPosition(hConErr, coninfo.dwCursorPosition))
+ return;
+ if (!WriteConsole(hConErr, count, (DWORD)strlen(count), &result, NULL)
+ || !result)
+ return;
+ }
+ while ((remains > 0) && WaitForSingleObject(hConIn, 1000) != WAIT_FAILED);
+}
+
+static BOOL die_on_logoff = FALSE;
+
+static LRESULT CALLBACK monitor_service_9x_proc(HWND hWnd, UINT msg,
+ WPARAM wParam, LPARAM lParam)
+{
+/* This is the WndProc procedure for our invisible window.
+ * When the user shuts down the system, this window is sent
+ * a signal WM_ENDSESSION. We clean up by signaling Apache
+ * to shut down, and idle until Apache's primary thread quits.
+ */
+ if ((msg == WM_ENDSESSION)
+ && (die_on_logoff || (lParam != ENDSESSION_LOGOFF)))
+ {
+ ap_signal_parent(SIGNAL_PARENT_SHUTDOWN);
+ if (wParam)
+ /* Don't leave this message until we are dead! */
+ WaitForSingleObject(globdat.mpm_thread, 30000);
+ return 0;
+ }
+ return (DefWindowProc(hWnd, msg, wParam, lParam));
+}
+
+static DWORD WINAPI monitor_service_9x_thread(void *service_name)
+{
+ /* When running as a service under Windows 9x, there is no console
+ * window present, and no ConsoleCtrlHandler to call when the system
+ * is shutdown. If the WatchWindow thread is created with a NULL
+ * service_name argument, then the ...SystemMonitor window class is
+ * used to create the "Apache" window to watch for logoff and shutdown.
+ * If the service_name is provided, the ...ServiceMonitor window class
+ * is used to create the window named by the service_name argument,
+ * and the logoff message is ignored.
+ */
+ WNDCLASS wc;
+ HWND hwndMain;
+ MSG msg;
+
+ wc.style = CS_GLOBALCLASS;
+ wc.lpfnWndProc = monitor_service_9x_proc;
+ wc.cbClsExtra = 0;
+ wc.cbWndExtra = 0;
+ wc.hInstance = NULL;
+ wc.hIcon = NULL;
+ wc.hCursor = NULL;
+ wc.hbrBackground = NULL;
+ wc.lpszMenuName = NULL;
+ if (service_name)
+ wc.lpszClassName = "ApacheWin95ServiceMonitor";
+ else
+ wc.lpszClassName = "ApacheWin95SystemMonitor";
+
+ die_on_logoff = service_name ? FALSE : TRUE;
+
+ if (!RegisterClass(&wc))
+ {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(),
+ NULL, "Could not register window class for WatchWindow");
+ globdat.service_thread_id = 0;
+ return 0;
+ }
+
+ /* Create an invisible window */
+ hwndMain = CreateWindow(wc.lpszClassName,
+ service_name ? (char *) service_name : "Apache",
+ WS_OVERLAPPEDWINDOW & ~WS_VISIBLE,
+ CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
+ CW_USEDEFAULT, NULL, NULL, NULL, NULL);
+
+ if (!hwndMain)
+ {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(),
+ NULL, "Could not create WatchWindow");
+ globdat.service_thread_id = 0;
+ return 0;
+ }
+
+ /* If we succeed, eliminate the console window.
+ * Signal the parent we are all set up, and
+ * watch the message queue while the window lives.
+ */
+ FreeConsole();
+ SetEvent(globdat.service_init);
+
+ while (GetMessage(&msg, NULL, 0, 0))
+ {
+ if (msg.message == WM_CLOSE)
+ DestroyWindow(hwndMain);
+ else {
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
+ }
+ globdat.service_thread_id = 0;
+ return 0;
+}
+
+
+static BOOL CALLBACK console_control_handler(DWORD ctrl_type)
+{
+ switch (ctrl_type)
+ {
+ case CTRL_BREAK_EVENT:
+ fprintf(stderr, "Apache server restarting...\n");
+ ap_signal_parent(SIGNAL_PARENT_RESTART);
+ return TRUE;
+ case CTRL_C_EVENT:
+ fprintf(stderr, "Apache server interrupted...\n");
+ /* for Interrupt signals, shut down the server.
+ * Tell the system we have dealt with the signal
+ * without waiting for Apache to terminate.
+ */
+ ap_signal_parent(SIGNAL_PARENT_SHUTDOWN);
+ return TRUE;
+
+ case CTRL_CLOSE_EVENT:
+ case CTRL_LOGOFF_EVENT:
+ case CTRL_SHUTDOWN_EVENT:
+ /* for Terminate signals, shut down the server.
+ * Wait for Apache to terminate, but respond
+ * after a reasonable time to tell the system
+ * that we did attempt to shut ourself down.
+ * THESE EVENTS WILL NOT OCCUR UNDER WIN9x!
+ */
+ fprintf(stderr, "Apache server shutdown initiated...\n");
+ ap_signal_parent(SIGNAL_PARENT_SHUTDOWN);
+ Sleep(30000);
+ return TRUE;
+ }
+
+ /* We should never get here, but this is (mostly) harmless */
+ return FALSE;
+}
+
+
+static void stop_console_handler(void)
+{
+ SetConsoleCtrlHandler(console_control_handler, FALSE);
+}
+
+
+void mpm_start_console_handler(void)
+{
+ SetConsoleCtrlHandler(console_control_handler, TRUE);
+ atexit(stop_console_handler);
+}
+
+
+/* Special situation - children of services need to mind their
+ * P's & Q's and wait quietly, ignoring the mean OS signaling
+ * shutdown and other horrors, to kill them gracefully...
+ */
+
+static BOOL CALLBACK child_control_handler(DWORD ctrl_type)
+{
+ switch (ctrl_type)
+ {
+ case CTRL_C_EVENT:
+ case CTRL_BREAK_EVENT:
+ /* for Interrupt signals, ignore them.
+ * The system will also signal the parent process,
+ * which will terminate Apache.
+ */
+ return TRUE;
+
+ case CTRL_CLOSE_EVENT:
+ case CTRL_LOGOFF_EVENT:
+ case CTRL_SHUTDOWN_EVENT:
+ /* for Shutdown signals, ignore them, but... .
+ * The system will also signal the parent process,
+ * which will terminate Apache, so we need to wait.
+ */
+ Sleep(30000);
+ return TRUE;
+ }
+
+ /* We should never get here, but this is (mostly) harmless */
+ return FALSE;
+}
+
+
+static void stop_child_console_handler(void)
+{
+ SetConsoleCtrlHandler(child_control_handler, FALSE);
+}
+
+
+void mpm_start_child_console_handler(void)
+{
+ if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
+ FreeConsole();
+ }
+ else
+ {
+ SetConsoleCtrlHandler(child_control_handler, TRUE);
+ atexit(stop_child_console_handler);
+ }
+}
+
+
+/**********************************
+ WinNT service control management
+ **********************************/
+
+static int ReportStatusToSCMgr(int currentState, int exitCode, int waitHint)
+{
+ static int checkPoint = 1;
+ int rv = APR_SUCCESS;
+
+ if (globdat.hServiceStatus)
+ {
+ if (currentState == SERVICE_RUNNING) {
+ globdat.ssStatus.dwWaitHint = 0;
+ globdat.ssStatus.dwCheckPoint = 0;
+ globdat.ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
+ }
+ else if (currentState == SERVICE_STOPPED) {
+ globdat.ssStatus.dwWaitHint = 0;
+ globdat.ssStatus.dwCheckPoint = 0;
+ if (!exitCode && globdat.ssStatus.dwCurrentState
+ != SERVICE_STOP_PENDING) {
+ /* An unexpected exit? Better to error! */
+ exitCode = 1;
+ }
+ if (exitCode) {
+ globdat.ssStatus.dwWin32ExitCode =ERROR_SERVICE_SPECIFIC_ERROR;
+ globdat.ssStatus.dwServiceSpecificExitCode = exitCode;
+ }
+ }
+ else {
+ globdat.ssStatus.dwCheckPoint = ++checkPoint;
+ globdat.ssStatus.dwControlsAccepted = 0;
+ if(waitHint)
+ globdat.ssStatus.dwWaitHint = waitHint;
+ }
+
+ globdat.ssStatus.dwCurrentState = currentState;
+
+ rv = SetServiceStatus(globdat.hServiceStatus, &globdat.ssStatus);
+ }
+ return(rv);
+}
+
+/* Set the service description regardless of platform.
+ * We revert to set_service_description on NT/9x, the
+ * very long way so any Apache management program can grab the
+ * description. This would be bad on Win2000, since it wouldn't
+ * notify the service control manager of the name change.
+ */
+
+/* borrowed from mpm_winnt.c */
+extern apr_pool_t *pconf;
+
+/* Windows 2000 alone supports ChangeServiceConfig2 in order to
+ * register our server_version string... so we need some fixups
+ * to avoid binding to that function if we are on WinNT/9x.
+ */
+static void set_service_description(void)
+{
+ const char *full_description;
+ SC_HANDLE schSCManager;
+ BOOL ret = 0;
+
+ /* Nothing to do if we are a console
+ */
+ if (!mpm_service_name)
+ return;
+
+ /* Time to fix up the description, upon each successful restart
+ */
+ full_description = ap_get_server_version();
+
+ if ((osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
+ && (osver.dwMajorVersion > 4)
+ && (ChangeServiceConfig2)
+ && (schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT)))
+ {
+ SC_HANDLE schService = OpenService(schSCManager, mpm_service_name,
+ SERVICE_CHANGE_CONFIG);
+ if (schService) {
+ /* Cast is necessary, ChangeServiceConfig2 handles multiple
+ * object types, some volatile, some not.
+ */
+ /* ###: utf-ize */
+ if (ChangeServiceConfig2(schService,
+ 1 /* SERVICE_CONFIG_DESCRIPTION */,
+ (LPVOID) &full_description)) {
+ full_description = NULL;
+ }
+ CloseServiceHandle(schService);
+ }
+ CloseServiceHandle(schSCManager);
+ }
+
+ if (full_description)
+ {
+ char szPath[MAX_PATH];
+ ap_regkey_t *svckey;
+ apr_status_t rv;
+
+ /* Find the Service key that Monitor Applications iterate */
+ apr_snprintf(szPath, sizeof(szPath),
+ "SYSTEM\\CurrentControlSet\\Services\\%s",
+ mpm_service_name);
+ rv = ap_regkey_open(&svckey, AP_REGKEY_LOCAL_MACHINE, szPath,
+ APR_READ | APR_WRITE, pconf);
+ if (rv != APR_SUCCESS) {
+ return;
+ }
+ /* Attempt to set the Description value for our service */
+ ap_regkey_value_set(svckey, "Description", full_description, 0, pconf);
+ ap_regkey_close(svckey);
+ }
+}
+
+/* handle the SCM's ControlService() callbacks to our service */
+
+static VOID WINAPI service_nt_ctrl(DWORD dwCtrlCode)
+{
+ if (dwCtrlCode == SERVICE_CONTROL_STOP)
+ {
+ ap_signal_parent(SIGNAL_PARENT_SHUTDOWN);
+ ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 30000);
+ return;
+ }
+ if (dwCtrlCode == SERVICE_APACHE_RESTART)
+ {
+ ap_signal_parent(SIGNAL_PARENT_RESTART);
+ ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 30000);
+ return;
+ }
+
+ ReportStatusToSCMgr(globdat.ssStatus.dwCurrentState, NO_ERROR, 0);
+}
+
+
+/* service_nt_main_fn is outside of the call stack and outside of the
+ * primary server thread... so now we _really_ need a placeholder!
+ * The winnt_rewrite_args has created and shared mpm_new_argv with us.
+ */
+extern apr_array_header_t *mpm_new_argv;
+
+/* ###: utf-ize */
+static void __stdcall service_nt_main_fn(DWORD argc, LPTSTR *argv)
+{
+ const char *ignored;
+
+ /* args and service names live in the same pool */
+ mpm_service_set_name(mpm_new_argv->pool, &ignored, argv[0]);
+
+ memset(&globdat.ssStatus, 0, sizeof(globdat.ssStatus));
+ globdat.ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
+ globdat.ssStatus.dwCurrentState = SERVICE_START_PENDING;
+ globdat.ssStatus.dwCheckPoint = 1;
+
+ /* ###: utf-ize */
+ if (!(globdat.hServiceStatus = RegisterServiceCtrlHandler(argv[0], service_nt_ctrl)))
+ {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(),
+ NULL, "Failure registering service handler");
+ return;
+ }
+
+ /* Report status, no errors, and buy 3 more seconds */
+ ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 30000);
+
+ /* We need to append all the command arguments passed via StartService()
+ * to our running service... which just got here via the SCM...
+ * but we hvae no interest in argv[0] for the mpm_new_argv list.
+ */
+ if (argc > 1)
+ {
+ char **cmb_data;
+
+ mpm_new_argv->nalloc = mpm_new_argv->nelts + argc - 1;
+ cmb_data = malloc(mpm_new_argv->nalloc * sizeof(const char *));
+
+ /* mpm_new_argv remains first (of lower significance) */
+ memcpy (cmb_data, mpm_new_argv->elts,
+ mpm_new_argv->elt_size * mpm_new_argv->nelts);
+
+ /* Service args follow from StartService() invocation */
+ memcpy (cmb_data + mpm_new_argv->nelts, argv + 1,
+ mpm_new_argv->elt_size * (argc - 1));
+
+ /* The replacement arg list is complete */
+ mpm_new_argv->elts = (char *)cmb_data;
+ mpm_new_argv->nelts = mpm_new_argv->nalloc;
+ }
+
+ /* Let the main thread continue now... but hang on to the
+ * signal_monitor event so we can take further action
+ */
+ SetEvent(globdat.service_init);
+
+ WaitForSingleObject(globdat.service_term, INFINITE);
+}
+
+
+DWORD WINAPI service_nt_dispatch_thread(LPVOID nada)
+{
+ apr_status_t rv = APR_SUCCESS;
+
+ SERVICE_TABLE_ENTRY dispatchTable[] =
+ {
+ { "", service_nt_main_fn },
+ { NULL, NULL }
+ };
+
+ /* ###: utf-ize */
+ if (!StartServiceCtrlDispatcher(dispatchTable))
+ {
+ /* This is a genuine failure of the SCM. */
+ rv = apr_get_os_error();
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "Error starting service control dispatcher");
+ }
+
+ return (rv);
+}
+
+
+apr_status_t mpm_service_set_name(apr_pool_t *p, const char **display_name,
+ const char *set_name)
+{
+ char key_name[MAX_PATH];
+ ap_regkey_t *key;
+ apr_status_t rv;
+
+ /* ### Needs improvement, on Win2K the user can _easily_
+ * change the display name to a string that doesn't reflect
+ * the internal service name + whitespace!
+ */
+ mpm_service_name = apr_palloc(p, strlen(set_name) + 1);
+ apr_collapse_spaces((char*) mpm_service_name, set_name);
+ apr_snprintf(key_name, sizeof(key_name), SERVICECONFIG, mpm_service_name);
+ rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, key_name, APR_READ, pconf);
+ if (rv == APR_SUCCESS) {
+ rv = ap_regkey_value_get(&mpm_display_name, key, "DisplayName", pconf);
+ ap_regkey_close(key);
+ }
+ if (rv != APR_SUCCESS) {
+ /* Take the given literal name if there is no service entry */
+ mpm_display_name = apr_pstrdup(p, set_name);
+ }
+ *display_name = mpm_display_name;
+ return rv;
+}
+
+
+apr_status_t mpm_merge_service_args(apr_pool_t *p,
+ apr_array_header_t *args,
+ int fixed_args)
+{
+ apr_array_header_t *svc_args = NULL;
+ char conf_key[MAX_PATH];
+ char **cmb_data;
+ apr_status_t rv;
+ ap_regkey_t *key;
+
+ apr_snprintf(conf_key, sizeof(conf_key), SERVICEPARAMS, mpm_service_name);
+ rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, conf_key, APR_READ, p);
+ if (rv == APR_SUCCESS) {
+ rv = ap_regkey_value_array_get(&svc_args, key, "ConfigArgs", p);
+ ap_regkey_close(key);
+ }
+ if (rv != APR_SUCCESS) {
+ if (rv == ERROR_FILE_NOT_FOUND) {
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0, NULL,
+ "No ConfigArgs registered for %s, perhaps "
+ "this service is not installed?",
+ mpm_service_name);
+ return APR_SUCCESS;
+ }
+ else
+ return (rv);
+ }
+
+ if (!svc_args || svc_args->nelts == 0) {
+ return (APR_SUCCESS);
+ }
+
+ /* Now we have the mpm_service_name arg, and the mpm_runservice_nt()
+ * call appended the arguments passed by StartService(), so it's
+ * time to _prepend_ the default arguments for the server from
+ * the service's default arguments (all others override them)...
+ */
+ args->nalloc = args->nelts + svc_args->nelts;
+ cmb_data = malloc(args->nalloc * sizeof(const char *));
+
+ /* First three args (argv[0], -f, path) remain first */
+ memcpy(cmb_data, args->elts, args->elt_size * fixed_args);
+
+ /* Service args follow from service registry array */
+ memcpy(cmb_data + fixed_args, svc_args->elts,
+ svc_args->elt_size * svc_args->nelts);
+
+ /* Remaining new args follow */
+ memcpy(cmb_data + fixed_args + svc_args->nelts,
+ (const char **)args->elts + fixed_args,
+ args->elt_size * (args->nelts - fixed_args));
+
+ args->elts = (char *)cmb_data;
+ args->nelts = args->nalloc;
+
+ return APR_SUCCESS;
+}
+
+
+void service_stopped(void)
+{
+ /* Still have a thread & window to clean up, so signal now */
+ if (globdat.service_thread)
+ {
+ if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
+ {
+ /* Stop logging to the event log */
+ mpm_nt_eventlog_stderr_flush();
+
+ /* Cause the service_nt_main_fn to complete */
+ ReleaseMutex(globdat.service_term);
+
+ ReportStatusToSCMgr(SERVICE_STOPPED, // service state
+ NO_ERROR, // exit code
+ 0); // wait hint
+ }
+ else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
+ {
+ RegisterServiceProcess(0, 0);
+ PostThreadMessage(globdat.service_thread_id, WM_CLOSE, 0, 0);
+ }
+
+ WaitForSingleObject(globdat.service_thread, 5000);
+ CloseHandle(globdat.service_thread);
+ }
+}
+
+
+apr_status_t mpm_service_to_start(const char **display_name, apr_pool_t *p)
+{
+ HANDLE hProc = GetCurrentProcess();
+ HANDLE hThread = GetCurrentThread();
+ HANDLE waitfor[2];
+
+ /* Prevent holding open the (hidden) console */
+ ap_real_exit_code = 0;
+
+ /* GetCurrentThread returns a psuedo-handle, we need
+ * a real handle for another thread to wait upon.
+ */
+ if (!DuplicateHandle(hProc, hThread, hProc, &(globdat.mpm_thread),
+ 0, FALSE, DUPLICATE_SAME_ACCESS)) {
+ return APR_ENOTHREAD;
+ }
+
+ if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
+ {
+ globdat.service_init = CreateEvent(NULL, FALSE, FALSE, NULL);
+ globdat.service_term = CreateMutex(NULL, TRUE, NULL);
+ if (!globdat.service_init || !globdat.service_term) {
+ return APR_EGENERAL;
+ }
+
+ globdat.service_thread = CreateThread(NULL, 0, service_nt_dispatch_thread,
+ NULL, 0, &globdat.service_thread_id);
+ }
+ else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
+ {
+ if (!RegisterServiceProcess(0, 1))
+ return GetLastError();
+
+ globdat.service_init = CreateEvent(NULL, FALSE, FALSE, NULL);
+ if (!globdat.service_init) {
+ return APR_EGENERAL;
+ }
+
+ globdat.service_thread = CreateThread(NULL, 0, monitor_service_9x_thread,
+ (LPVOID) mpm_service_name, 0,
+ &globdat.service_thread_id);
+ }
+
+ if (!globdat.service_thread) {
+ return APR_ENOTHREAD;
+ }
+
+ waitfor[0] = globdat.service_init;
+ waitfor[1] = globdat.service_thread;
+
+ /* Wait for controlling thread init or termination */
+ if (WaitForMultipleObjects(2, waitfor, FALSE, 10000) != WAIT_OBJECT_0) {
+ return APR_ENOTHREAD;
+ }
+
+ atexit(service_stopped);
+ *display_name = mpm_display_name;
+ return APR_SUCCESS;
+}
+
+
+apr_status_t mpm_service_started(void)
+{
+ set_service_description();
+ if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
+ {
+ ReportStatusToSCMgr(SERVICE_RUNNING, // service state
+ NO_ERROR, // exit code
+ 0); // wait hint
+ }
+ return APR_SUCCESS;
+}
+
+
+void mpm_service_stopping(void)
+{
+ if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
+ ReportStatusToSCMgr(SERVICE_STOP_PENDING, // service state
+ NO_ERROR, // exit code
+ 30000); // wait hint
+}
+
+
+apr_status_t mpm_service_install(apr_pool_t *ptemp, int argc,
+ const char * const * argv, int reconfig)
+{
+ char key_name[MAX_PATH];
+ char exe_path[MAX_PATH];
+ char *launch_cmd;
+ ap_regkey_t *key;
+ apr_status_t rv;
+
+ fprintf(stderr,reconfig ? "Reconfiguring the %s service\n"
+ : "Installing the %s service\n", mpm_display_name);
+
+ /* ###: utf-ize */
+ if (GetModuleFileName(NULL, exe_path, sizeof(exe_path)) == 0)
+ {
+ apr_status_t rv = apr_get_os_error();
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "GetModuleFileName failed");
+ return rv;
+ }
+
+ if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
+ {
+ SC_HANDLE schService;
+ SC_HANDLE schSCManager;
+
+ schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
+ SC_MANAGER_CREATE_SERVICE);
+ if (!schSCManager) {
+ rv = apr_get_os_error();
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "Failed to open the WinNT service manager");
+ return (rv);
+ }
+
+ launch_cmd = apr_psprintf(ptemp, "\"%s\" -k runservice", exe_path);
+
+ if (reconfig) {
+ /* ###: utf-ize */
+ schService = OpenService(schSCManager, mpm_service_name,
+ SERVICE_CHANGE_CONFIG);
+ if (!schService) {
+ ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_ERR,
+ apr_get_os_error(), NULL,
+ "OpenService failed");
+ }
+ /* ###: utf-ize */
+ else if (!ChangeServiceConfig(schService,
+ SERVICE_WIN32_OWN_PROCESS,
+ SERVICE_AUTO_START,
+ SERVICE_ERROR_NORMAL,
+ launch_cmd, NULL, NULL,
+ "Tcpip\0Afd\0", NULL, NULL,
+ mpm_display_name)) {
+ ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_ERR,
+ apr_get_os_error(), NULL,
+ "ChangeServiceConfig failed");
+ /* !schService aborts configuration below */
+ CloseServiceHandle(schService);
+ schService = NULL;
+ }
+ }
+ else {
+ /* RPCSS is the Remote Procedure Call (RPC) Locator required
+ * for DCOM communication pipes. I am far from convinced we
+ * should add this to the default service dependencies, but
+ * be warned that future apache modules or ISAPI dll's may
+ * depend on it.
+ */
+ /* ###: utf-ize */
+ schService = CreateService(schSCManager, // SCManager database
+ mpm_service_name, // name of service
+ mpm_display_name, // name to display
+ SERVICE_ALL_ACCESS, // access required
+ SERVICE_WIN32_OWN_PROCESS, // service type
+ SERVICE_AUTO_START, // start type
+ SERVICE_ERROR_NORMAL, // error control type
+ launch_cmd, // service's binary
+ NULL, // no load svc group
+ NULL, // no tag identifier
+ "Tcpip\0Afd\0", // dependencies
+ NULL, // use SYSTEM account
+ NULL); // no password
+
+ if (!schService)
+ {
+ rv = apr_get_os_error();
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "Failed to create WinNT Service Profile");
+ CloseServiceHandle(schSCManager);
+ return (rv);
+ }
+ }
+
+ CloseServiceHandle(schService);
+ CloseServiceHandle(schSCManager);
+ }
+ else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
+ {
+ /* Store the launch command in the registry */
+ launch_cmd = apr_psprintf(ptemp, "\"%s\" -n %s -k runservice",
+ exe_path, mpm_service_name);
+ rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, SERVICECONFIG9X,
+ APR_READ | APR_WRITE | APR_CREATE, pconf);
+ if (rv == APR_SUCCESS) {
+ rv = ap_regkey_value_set(key, mpm_service_name,
+ launch_cmd, 0, pconf);
+ ap_regkey_close(key);
+ }
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "%s: Failed to add the RunServices registry entry.",
+ mpm_display_name);
+ return (rv);
+ }
+
+ apr_snprintf(key_name, sizeof(key_name), SERVICECONFIG, mpm_service_name);
+ rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, key_name,
+ APR_READ | APR_WRITE | APR_CREATE, pconf);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "%s: Failed to create the registry service key.",
+ mpm_display_name);
+ return (rv);
+ }
+ rv = ap_regkey_value_set(key, "ImagePath", launch_cmd, 0, pconf);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "%s: Failed to store ImagePath in the registry.",
+ mpm_display_name);
+ ap_regkey_close(key);
+ return (rv);
+ }
+ rv = ap_regkey_value_set(key, "DisplayName",
+ mpm_display_name, 0, pconf);
+ ap_regkey_close(key);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "%s: Failed to store DisplayName in the registry.",
+ mpm_display_name);
+ return (rv);
+ }
+ }
+
+ set_service_description();
+
+ /* For both WinNT & Win9x store the service ConfigArgs in the registry...
+ */
+ apr_snprintf(key_name, sizeof(key_name), SERVICEPARAMS, mpm_service_name);
+ rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, key_name,
+ APR_READ | APR_WRITE | APR_CREATE, pconf);
+ if (rv == APR_SUCCESS) {
+ rv = ap_regkey_value_array_set(key, "ConfigArgs", argc, argv, pconf);
+ ap_regkey_close(key);
+ }
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "%s: Failed to store the ConfigArgs in the registry.",
+ mpm_display_name);
+ return (rv);
+ }
+ fprintf(stderr,"The %s service is successfully installed.\n", mpm_display_name);
+ return APR_SUCCESS;
+}
+
+
+apr_status_t mpm_service_uninstall(void)
+{
+ char key_name[MAX_PATH];
+ apr_status_t rv;
+
+ if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
+ {
+ SC_HANDLE schService;
+ SC_HANDLE schSCManager;
+
+ fprintf(stderr,"Removing the %s service\n", mpm_display_name);
+
+ schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
+ SC_MANAGER_CONNECT);
+ if (!schSCManager) {
+ rv = apr_get_os_error();
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "Failed to open the WinNT service manager.");
+ return (rv);
+ }
+
+ /* ###: utf-ize */
+ schService = OpenService(schSCManager, mpm_service_name, DELETE);
+
+ if (!schService) {
+ rv = apr_get_os_error();
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "%s: OpenService failed", mpm_display_name);
+ return (rv);
+ }
+
+ /* assure the service is stopped before continuing
+ *
+ * This may be out of order... we might not be able to be
+ * granted all access if the service is running anyway.
+ *
+ * And do we want to make it *this easy* for them
+ * to uninstall their service unintentionally?
+ */
+ // ap_stop_service(schService);
+
+ if (DeleteService(schService) == 0) {
+ rv = apr_get_os_error();
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "%s: Failed to delete the service.", mpm_display_name);
+ return (rv);
+ }
+
+ CloseServiceHandle(schService);
+ CloseServiceHandle(schSCManager);
+ }
+ else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
+ {
+ apr_status_t rv2, rv3;
+ ap_regkey_t *key;
+ fprintf(stderr,"Removing the %s service\n", mpm_display_name);
+
+ /* TODO: assure the service is stopped before continuing */
+
+ rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, SERVICECONFIG9X,
+ APR_READ | APR_WRITE | APR_CREATE, pconf);
+ if (rv == APR_SUCCESS) {
+ rv = ap_regkey_value_remove(key, mpm_service_name, pconf);
+ ap_regkey_close(key);
+ }
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "%s: Failed to remove the RunServices registry "
+ "entry.", mpm_display_name);
+ }
+
+ /* we blast Services/us, not just the Services/us/Parameters branch */
+ apr_snprintf(key_name, sizeof(key_name), SERVICEPARAMS, mpm_service_name);
+ rv2 = ap_regkey_remove(AP_REGKEY_LOCAL_MACHINE, key_name, pconf);
+ apr_snprintf(key_name, sizeof(key_name), SERVICECONFIG, mpm_service_name);
+ rv3 = ap_regkey_remove(AP_REGKEY_LOCAL_MACHINE, key_name, pconf);
+ rv2 = (rv2 != APR_SUCCESS) ? rv2 : rv3;
+ if (rv2 != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv2, NULL,
+ "%s: Failed to remove the service config from the "
+ "registry.", mpm_display_name);
+ }
+ rv = (rv != APR_SUCCESS) ? rv : rv2;
+ if (rv != APR_SUCCESS)
+ return rv;
+ }
+ fprintf(stderr,"The %s service has been removed successfully.\n", mpm_display_name);
+ return APR_SUCCESS;
+}
+
+
+/* signal_service_transition is a simple thunk to signal the service
+ * and monitor its successful transition. If the signal passed is 0,
+ * then the caller is assumed to already have performed some service
+ * operation to be monitored (such as StartService), and no actual
+ * ControlService signal is sent.
+ */
+
+static int signal_service_transition(SC_HANDLE schService, DWORD signal, DWORD pending, DWORD complete)
+{
+ if (signal && !ControlService(schService, signal, &globdat.ssStatus))
+ return FALSE;
+
+ do {
+ Sleep(1000);
+ if (!QueryServiceStatus(schService, &globdat.ssStatus))
+ return FALSE;
+ } while (globdat.ssStatus.dwCurrentState == pending);
+
+ return (globdat.ssStatus.dwCurrentState == complete);
+}
+
+
+apr_status_t mpm_service_start(apr_pool_t *ptemp, int argc,
+ const char * const * argv)
+{
+ apr_status_t rv;
+
+ fprintf(stderr,"Starting the %s service\n", mpm_display_name);
+
+ if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
+ {
+ char **start_argv;
+ SC_HANDLE schService;
+ SC_HANDLE schSCManager;
+
+ schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
+ SC_MANAGER_CONNECT);
+ if (!schSCManager) {
+ rv = apr_get_os_error();
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "Failed to open the WinNT service manager");
+ return (rv);
+ }
+
+ /* ###: utf-ize */
+ schService = OpenService(schSCManager, mpm_service_name,
+ SERVICE_START | SERVICE_QUERY_STATUS);
+ if (!schService) {
+ rv = apr_get_os_error();
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "%s: Failed to open the service.", mpm_display_name);
+ CloseServiceHandle(schSCManager);
+ return (rv);
+ }
+
+ if (QueryServiceStatus(schService, &globdat.ssStatus)
+ && (globdat.ssStatus.dwCurrentState == SERVICE_RUNNING)) {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL,
+ "Service %s is already started!", mpm_display_name);
+ CloseServiceHandle(schService);
+ CloseServiceHandle(schSCManager);
+ return 0;
+ }
+
+ start_argv = malloc((argc + 1) * sizeof(const char **));
+ memcpy(start_argv, argv, argc * sizeof(const char **));
+ start_argv[argc] = NULL;
+
+ rv = APR_EINIT;
+ /* ###: utf-ize */
+ if (StartService(schService, argc, start_argv)
+ && signal_service_transition(schService, 0, /* test only */
+ SERVICE_START_PENDING,
+ SERVICE_RUNNING))
+ rv = APR_SUCCESS;
+
+ if (rv != APR_SUCCESS)
+ rv = apr_get_os_error();
+
+ CloseServiceHandle(schService);
+ CloseServiceHandle(schSCManager);
+ }
+ else /* osver.dwPlatformId != VER_PLATFORM_WIN32_NT */
+ {
+ STARTUPINFO si; /* Filled in prior to call to CreateProcess */
+ PROCESS_INFORMATION pi; /* filled in on call to CreateProcess */
+ char exe_path[MAX_PATH];
+ char exe_cmd[MAX_PATH * 4];
+ char *next_arg;
+ int i;
+
+ /* Locate the active top level window named service_name
+ * provided the class is ApacheWin95ServiceMonitor
+ */
+ if (FindWindow("ApacheWin95ServiceMonitor", mpm_service_name)) {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL,
+ "Service %s is already started!", mpm_display_name);
+ return 0;
+ }
+
+ /* This may not appear intuitive, but Win9x will not allow a process
+ * to detach from the console without releasing the entire console.
+ * Ergo, we must spawn a new process for the service to get back our
+ * console window.
+ * The config is pre-flighted, so there should be no danger of failure.
+ */
+
+ if (GetModuleFileName(NULL, exe_path, sizeof(exe_path)) == 0)
+ {
+ apr_status_t rv = apr_get_os_error();
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
+ "GetModuleFileName failed");
+ return rv;
+ }
+
+ apr_snprintf(exe_cmd, sizeof(exe_cmd),
+ "\"%s\" -n %s -k runservice",
+ exe_path, mpm_service_name);
+ next_arg = strchr(exe_cmd, '\0');
+ for (i = 0; i < argc; ++i) {
+ apr_snprintf(next_arg, sizeof(exe_cmd) - (next_arg - exe_cmd),
+ " \"%s\"", argv[i]);
+ next_arg = strchr(exe_cmd, '\0');
+ }
+
+ memset(&si, 0, sizeof(si));
+ memset(&pi, 0, sizeof(pi));
+ si.cb = sizeof(si);
+ si.dwFlags = STARTF_USESHOWWINDOW;
+ si.wShowWindow = SW_HIDE; /* This might be redundant */
+
+ rv = APR_EINIT;
+ if (CreateProcess(NULL, exe_cmd, NULL, NULL, FALSE,
+ DETACHED_PROCESS, /* Creation flags */
+ NULL, NULL, &si, &pi))
+ {
+ DWORD code;
+ while (GetExitCodeProcess(pi.hProcess, &code) == STILL_ACTIVE) {
+ if (FindWindow("ApacheWin95ServiceMonitor", mpm_service_name)) {
+ rv = APR_SUCCESS;
+ break;
+ }
+ Sleep (1000);
+ }
+ }
+
+ if (rv != APR_SUCCESS)
+ rv = apr_get_os_error();
+
+ CloseHandle(pi.hProcess);
+ CloseHandle(pi.hThread);
+ }
+
+ if (rv == APR_SUCCESS)
+ fprintf(stderr,"The %s service is running.\n", mpm_display_name);
+ else
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
+ "%s: Failed to start the service process.",
+ mpm_display_name);
+
+ return rv;
+}
+
+
+/* signal is zero to stop, non-zero for restart */
+
+void mpm_signal_service(apr_pool_t *ptemp, int signal)
+{
+ int success = FALSE;
+
+ if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
+ {
+ SC_HANDLE schService;
+ SC_HANDLE schSCManager;
+
+ schSCManager = OpenSCManager(NULL, NULL, // default machine & database
+ SC_MANAGER_CONNECT);
+
+ if (!schSCManager) {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(), NULL,
+ "Failed to open the NT Service Manager");
+ return;
+ }
+
+ /* ###: utf-ize */
+ schService = OpenService(schSCManager, mpm_service_name,
+ SERVICE_INTERROGATE | SERVICE_QUERY_STATUS |
+ SERVICE_USER_DEFINED_CONTROL |
+ SERVICE_START | SERVICE_STOP);
+
+ if (schService == NULL) {
+ /* Could not open the service */
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(), NULL,
+ "Failed to open the %s Service", mpm_display_name);
+ CloseServiceHandle(schSCManager);
+ return;
+ }
+
+ if (!QueryServiceStatus(schService, &globdat.ssStatus)) {
+ ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(), NULL,
+ "Query of Service %s failed", mpm_display_name);
+ CloseServiceHandle(schService);
+ CloseServiceHandle(schSCManager);
+ return;
+ }
+
+ if (!signal && (globdat.ssStatus.dwCurrentState == SERVICE_STOPPED)) {
+ fprintf(stderr,"The %s service is not started.\n", mpm_display_name);
+ CloseServiceHandle(schService);
+ CloseServiceHandle(schSCManager);
+ return;
+ }
+
+ fprintf(stderr,"The %s service is %s.\n", mpm_display_name,
+ signal ? "restarting" : "stopping");
+
+ if (!signal)
+ success = signal_service_transition(schService,
+ SERVICE_CONTROL_STOP,
+ SERVICE_STOP_PENDING,
+ SERVICE_STOPPED);
+ else if (globdat.ssStatus.dwCurrentState == SERVICE_STOPPED) {
+ mpm_service_start(ptemp, 0, NULL);
+ CloseServiceHandle(schService);
+ CloseServiceHandle(schSCManager);
+ return;
+ }
+ else
+ success = signal_service_transition(schService,
+ SERVICE_APACHE_RESTART,
+ SERVICE_START_PENDING,
+ SERVICE_RUNNING);
+
+ CloseServiceHandle(schService);
+ CloseServiceHandle(schSCManager);
+ }
+ else /* !isWindowsNT() */
+ {
+ DWORD service_pid;
+ HANDLE hwnd;
+ char prefix[20];
+ /* Locate the active top level window named service_name
+ * provided the class is ApacheWin95ServiceMonitor
+ */
+ hwnd = FindWindow("ApacheWin95ServiceMonitor", mpm_service_name);
+ if (hwnd && GetWindowThreadProcessId(hwnd, &service_pid))
+ globdat.ssStatus.dwCurrentState = SERVICE_RUNNING;
+ else
+ {
+ globdat.ssStatus.dwCurrentState = SERVICE_STOPPED;
+ if (!signal) {
+ fprintf(stderr,"The %s service is not started.\n", mpm_display_name);
+ return;
+ }
+ }
+
+ fprintf(stderr,"The %s service is %s.\n", mpm_display_name,
+ signal ? "restarting" : "stopping");
+
+ apr_snprintf(prefix, sizeof(prefix), "ap%ld", (long)service_pid);
+ setup_signal_names(prefix);
+
+ if (!signal)
+ {
+ int ticks = 60;
+ ap_signal_parent(SIGNAL_PARENT_SHUTDOWN);
+ while (--ticks)
+ {
+ if (!IsWindow(hwnd)) {
+ success = TRUE;
+ break;
+ }
+ Sleep(1000);
+ }
+ }
+ else /* !stop */
+ {
+ /* TODO: Aught to add a little test to the restart logic, and
+ * store the restart counter in the window's user dword.
+ * Then we can hang on and report a successful restart. But
+ * that's a project for another day.
+ */
+ if (globdat.ssStatus.dwCurrentState == SERVICE_STOPPED) {
+ mpm_service_start(ptemp, 0, NULL);
+ return;
+ }
+ else {
+ success = TRUE;
+ ap_signal_parent(SIGNAL_PARENT_RESTART);
+ }
+ }
+ }
+
+ if (success)
+ fprintf(stderr,"The %s service has %s.\n", mpm_display_name,
+ signal ? "restarted" : "stopped");
+ else
+ fprintf(stderr,"Failed to %s the %s service.\n",
+ signal ? "restart" : "stop", mpm_display_name);
+}
diff --git a/server/mpm/worker/Makefile.in b/server/mpm/worker/Makefile.in
new file mode 100644
index 00000000..b45b8483
--- /dev/null
+++ b/server/mpm/worker/Makefile.in
@@ -0,0 +1,5 @@
+
+LTLIBRARY_NAME = libworker.la
+LTLIBRARY_SOURCES = worker.c fdqueue.c pod.c
+
+include $(top_srcdir)/build/ltlib.mk
diff --git a/server/mpm/worker/config5.m4 b/server/mpm/worker/config5.m4
new file mode 100644
index 00000000..cc131348
--- /dev/null
+++ b/server/mpm/worker/config5.m4
@@ -0,0 +1,6 @@
+dnl ## XXX - Need a more thorough check of the proper flags to use
+
+if test "$MPM_NAME" = "worker" ; then
+ AC_CHECK_FUNCS(pthread_kill)
+ APACHE_FAST_OUTPUT(server/mpm/$MPM_NAME/Makefile)
+fi
diff --git a/server/mpm/worker/fdqueue.c b/server/mpm/worker/fdqueue.c
new file mode 100644
index 00000000..6b5485b3
--- /dev/null
+++ b/server/mpm/worker/fdqueue.c
@@ -0,0 +1,384 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "fdqueue.h"
+#include "apr_atomic.h"
+
+typedef struct recycled_pool {
+ apr_pool_t *pool;
+ struct recycled_pool *next;
+} recycled_pool;
+
+struct fd_queue_info_t {
+ apr_uint32_t idlers;
+ apr_thread_mutex_t *idlers_mutex;
+ apr_thread_cond_t *wait_for_idler;
+ int terminated;
+ int max_idlers;
+ recycled_pool *recycled_pools;
+};
+
+static apr_status_t queue_info_cleanup(void *data_)
+{
+ fd_queue_info_t *qi = data_;
+ apr_thread_cond_destroy(qi->wait_for_idler);
+ apr_thread_mutex_destroy(qi->idlers_mutex);
+
+ /* Clean up any pools in the recycled list */
+ for (;;) {
+ struct recycled_pool *first_pool = qi->recycled_pools;
+ if (first_pool == NULL) {
+ break;
+ }
+ if (apr_atomic_casptr((volatile void**)&(qi->recycled_pools), first_pool->next,
+ first_pool) == first_pool) {
+ apr_pool_destroy(first_pool->pool);
+ }
+ }
+
+ return APR_SUCCESS;
+}
+
+apr_status_t ap_queue_info_create(fd_queue_info_t **queue_info,
+ apr_pool_t *pool, int max_idlers)
+{
+ apr_status_t rv;
+ fd_queue_info_t *qi;
+
+ qi = apr_palloc(pool, sizeof(*qi));
+ memset(qi, 0, sizeof(*qi));
+
+ rv = apr_thread_mutex_create(&qi->idlers_mutex, APR_THREAD_MUTEX_DEFAULT,
+ pool);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ rv = apr_thread_cond_create(&qi->wait_for_idler, pool);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ qi->recycled_pools = NULL;
+ qi->max_idlers = max_idlers;
+ apr_pool_cleanup_register(pool, qi, queue_info_cleanup,
+ apr_pool_cleanup_null);
+
+ *queue_info = qi;
+
+ return APR_SUCCESS;
+}
+
+apr_status_t ap_queue_info_set_idle(fd_queue_info_t *queue_info,
+ apr_pool_t *pool_to_recycle)
+{
+ apr_status_t rv;
+ int prev_idlers;
+
+ /* If we have been given a pool to recycle, atomically link
+ * it into the queue_info's list of recycled pools
+ */
+ if (pool_to_recycle) {
+ struct recycled_pool *new_recycle;
+ new_recycle = (struct recycled_pool *)apr_palloc(pool_to_recycle,
+ sizeof(*new_recycle));
+ new_recycle->pool = pool_to_recycle;
+ for (;;) {
+ new_recycle->next = queue_info->recycled_pools;
+ if (apr_atomic_casptr((volatile void**)&(queue_info->recycled_pools),
+ new_recycle, new_recycle->next) ==
+ new_recycle->next) {
+ break;
+ }
+ }
+ }
+
+ /* Atomically increment the count of idle workers */
+ for (;;) {
+ prev_idlers = queue_info->idlers;
+ if (apr_atomic_cas32(&(queue_info->idlers), prev_idlers + 1,
+ prev_idlers) == prev_idlers) {
+ break;
+ }
+ }
+
+ /* If this thread just made the idle worker count nonzero,
+ * wake up the listener. */
+ if (prev_idlers == 0) {
+ rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ rv = apr_thread_cond_signal(queue_info->wait_for_idler);
+ if (rv != APR_SUCCESS) {
+ apr_thread_mutex_unlock(queue_info->idlers_mutex);
+ return rv;
+ }
+ rv = apr_thread_mutex_unlock(queue_info->idlers_mutex);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ }
+
+ return APR_SUCCESS;
+}
+
+apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t *queue_info,
+ apr_pool_t **recycled_pool)
+{
+ apr_status_t rv;
+
+ *recycled_pool = NULL;
+
+ /* Block if the count of idle workers is zero */
+ if (queue_info->idlers == 0) {
+ rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ /* Re-check the idle worker count to guard against a
+ * race condition. Now that we're in the mutex-protected
+ * region, one of two things may have happened:
+ * - If the idle worker count is still zero, the
+ * workers are all still busy, so it's safe to
+ * block on a condition variable.
+ * - If the idle worker count is nonzero, then a
+ * worker has become idle since the first check
+ * of queue_info->idlers above. It's possible
+ * that the worker has also signaled the condition
+ * variable--and if so, the listener missed it
+ * because it wasn't yet blocked on the condition
+ * variable. But if the idle worker count is
+ * now nonzero, it's safe for this function to
+ * return immediately.
+ */
+ if (queue_info->idlers == 0) {
+ rv = apr_thread_cond_wait(queue_info->wait_for_idler,
+ queue_info->idlers_mutex);
+ if (rv != APR_SUCCESS) {
+ apr_status_t rv2;
+ rv2 = apr_thread_mutex_unlock(queue_info->idlers_mutex);
+ if (rv2 != APR_SUCCESS) {
+ return rv2;
+ }
+ return rv;
+ }
+ }
+ rv = apr_thread_mutex_unlock(queue_info->idlers_mutex);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ }
+
+ /* Atomically decrement the idle worker count */
+ apr_atomic_dec32(&(queue_info->idlers));
+
+ /* Atomically pop a pool from the recycled list */
+ for (;;) {
+ struct recycled_pool *first_pool = queue_info->recycled_pools;
+ if (first_pool == NULL) {
+ break;
+ }
+ if (apr_atomic_casptr((volatile void**)&(queue_info->recycled_pools), first_pool->next,
+ first_pool) == first_pool) {
+ *recycled_pool = first_pool->pool;
+ break;
+ }
+ }
+
+ if (queue_info->terminated) {
+ return APR_EOF;
+ }
+ else {
+ return APR_SUCCESS;
+ }
+}
+
+apr_status_t ap_queue_info_term(fd_queue_info_t *queue_info)
+{
+ apr_status_t rv;
+ rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ queue_info->terminated = 1;
+ apr_thread_cond_broadcast(queue_info->wait_for_idler);
+ return apr_thread_mutex_unlock(queue_info->idlers_mutex);
+}
+
+/**
+ * Detects when the fd_queue_t is full. This utility function is expected
+ * to be called from within critical sections, and is not threadsafe.
+ */
+#define ap_queue_full(queue) ((queue)->nelts == (queue)->bounds)
+
+/**
+ * Detects when the fd_queue_t is empty. This utility function is expected
+ * to be called from within critical sections, and is not threadsafe.
+ */
+#define ap_queue_empty(queue) ((queue)->nelts == 0)
+
+/**
+ * Callback routine that is called to destroy this
+ * fd_queue_t when its pool is destroyed.
+ */
+static apr_status_t ap_queue_destroy(void *data)
+{
+ fd_queue_t *queue = data;
+
+ /* Ignore errors here, we can't do anything about them anyway.
+ * XXX: We should at least try to signal an error here, it is
+ * indicative of a programmer error. -aaron */
+ apr_thread_cond_destroy(queue->not_empty);
+ apr_thread_mutex_destroy(queue->one_big_mutex);
+
+ return APR_SUCCESS;
+}
+
+/**
+ * Initialize the fd_queue_t.
+ */
+apr_status_t ap_queue_init(fd_queue_t *queue, int queue_capacity, apr_pool_t *a)
+{
+ int i;
+ apr_status_t rv;
+
+ if ((rv = apr_thread_mutex_create(&queue->one_big_mutex,
+ APR_THREAD_MUTEX_DEFAULT, a)) != APR_SUCCESS) {
+ return rv;
+ }
+ if ((rv = apr_thread_cond_create(&queue->not_empty, a)) != APR_SUCCESS) {
+ return rv;
+ }
+
+ queue->data = apr_palloc(a, queue_capacity * sizeof(fd_queue_elem_t));
+ queue->bounds = queue_capacity;
+ queue->nelts = 0;
+
+ /* Set all the sockets in the queue to NULL */
+ for (i = 0; i < queue_capacity; ++i)
+ queue->data[i].sd = NULL;
+
+ apr_pool_cleanup_register(a, queue, ap_queue_destroy, apr_pool_cleanup_null);
+
+ return APR_SUCCESS;
+}
+
+/**
+ * Push a new socket onto the queue.
+ *
+ * precondition: ap_queue_info_wait_for_idler has already been called
+ * to reserve an idle worker thread
+ */
+apr_status_t ap_queue_push(fd_queue_t *queue, apr_socket_t *sd, apr_pool_t *p)
+{
+ fd_queue_elem_t *elem;
+ apr_status_t rv;
+
+ if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
+ return rv;
+ }
+
+ AP_DEBUG_ASSERT(!queue->terminated);
+ AP_DEBUG_ASSERT(!ap_queue_full(queue));
+
+ elem = &queue->data[queue->nelts];
+ elem->sd = sd;
+ elem->p = p;
+ queue->nelts++;
+
+ apr_thread_cond_signal(queue->not_empty);
+
+ if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
+ return rv;
+ }
+
+ return APR_SUCCESS;
+}
+
+/**
+ * Retrieves the next available socket from the queue. If there are no
+ * sockets available, it will block until one becomes available.
+ * Once retrieved, the socket is placed into the address specified by
+ * 'sd'.
+ */
+apr_status_t ap_queue_pop(fd_queue_t *queue, apr_socket_t **sd, apr_pool_t **p)
+{
+ fd_queue_elem_t *elem;
+ apr_status_t rv;
+
+ if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
+ return rv;
+ }
+
+ /* Keep waiting until we wake up and find that the queue is not empty. */
+ if (ap_queue_empty(queue)) {
+ if (!queue->terminated) {
+ apr_thread_cond_wait(queue->not_empty, queue->one_big_mutex);
+ }
+ /* If we wake up and it's still empty, then we were interrupted */
+ if (ap_queue_empty(queue)) {
+ rv = apr_thread_mutex_unlock(queue->one_big_mutex);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ if (queue->terminated) {
+ return APR_EOF; /* no more elements ever again */
+ }
+ else {
+ return APR_EINTR;
+ }
+ }
+ }
+
+ elem = &queue->data[--queue->nelts];
+ *sd = elem->sd;
+ *p = elem->p;
+#ifdef AP_DEBUG
+ elem->sd = NULL;
+ elem->p = NULL;
+#endif /* AP_DEBUG */
+
+ rv = apr_thread_mutex_unlock(queue->one_big_mutex);
+ return rv;
+}
+
+apr_status_t ap_queue_interrupt_all(fd_queue_t *queue)
+{
+ apr_status_t rv;
+
+ if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
+ return rv;
+ }
+ apr_thread_cond_broadcast(queue->not_empty);
+ return apr_thread_mutex_unlock(queue->one_big_mutex);
+}
+
+apr_status_t ap_queue_term(fd_queue_t *queue)
+{
+ apr_status_t rv;
+
+ if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
+ return rv;
+ }
+ /* we must hold one_big_mutex when setting this... otherwise,
+ * we could end up setting it and waking everybody up just after a
+ * would-be popper checks it but right before they block
+ */
+ queue->terminated = 1;
+ if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
+ return rv;
+ }
+ return ap_queue_interrupt_all(queue);
+}
diff --git a/server/mpm/worker/fdqueue.h b/server/mpm/worker/fdqueue.h
new file mode 100644
index 00000000..7de082a0
--- /dev/null
+++ b/server/mpm/worker/fdqueue.h
@@ -0,0 +1,73 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file worker/fdqueue.h
+ * @brief fd queue declarations
+ *
+ * @addtogroup APACHE_MPM_WORKER
+ * @{
+ */
+
+#ifndef FDQUEUE_H
+#define FDQUEUE_H
+#include "httpd.h"
+#include <stdlib.h>
+#if APR_HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <apr_thread_mutex.h>
+#include <apr_thread_cond.h>
+#include <sys/types.h>
+#if APR_HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+#include <apr_errno.h>
+
+typedef struct fd_queue_info_t fd_queue_info_t;
+
+apr_status_t ap_queue_info_create(fd_queue_info_t **queue_info,
+ apr_pool_t *pool, int max_idlers);
+apr_status_t ap_queue_info_set_idle(fd_queue_info_t *queue_info,
+ apr_pool_t *pool_to_recycle);
+apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t *queue_info,
+ apr_pool_t **recycled_pool);
+apr_status_t ap_queue_info_term(fd_queue_info_t *queue_info);
+
+struct fd_queue_elem_t {
+ apr_socket_t *sd;
+ apr_pool_t *p;
+};
+typedef struct fd_queue_elem_t fd_queue_elem_t;
+
+struct fd_queue_t {
+ fd_queue_elem_t *data;
+ int nelts;
+ int bounds;
+ apr_thread_mutex_t *one_big_mutex;
+ apr_thread_cond_t *not_empty;
+ int terminated;
+};
+typedef struct fd_queue_t fd_queue_t;
+
+apr_status_t ap_queue_init(fd_queue_t *queue, int queue_capacity, apr_pool_t *a);
+apr_status_t ap_queue_push(fd_queue_t *queue, apr_socket_t *sd, apr_pool_t *p);
+apr_status_t ap_queue_pop(fd_queue_t *queue, apr_socket_t **sd, apr_pool_t **p);
+apr_status_t ap_queue_interrupt_all(fd_queue_t *queue);
+apr_status_t ap_queue_term(fd_queue_t *queue);
+
+#endif /* FDQUEUE_H */
+/** @} */
diff --git a/server/mpm/worker/mpm.h b/server/mpm/worker/mpm.h
new file mode 100644
index 00000000..335f4b6a
--- /dev/null
+++ b/server/mpm/worker/mpm.h
@@ -0,0 +1,62 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file worker/mpm.h
+ * @brief Unix Worker MPM
+ *
+ * @defgroup APACHE_MPM_WORKER Unix Worker MPM
+ * @ingroup APACHE_OS_UNIX APACHE_MPM
+ * @{
+ */
+
+#include "scoreboard.h"
+#include "unixd.h"
+
+#ifndef APACHE_MPM_WORKER_H
+#define APACHE_MPM_WORKER_H
+
+#define WORKER_MPM
+
+#define MPM_NAME "Worker"
+
+#define AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
+#define AP_MPM_WANT_WAIT_OR_TIMEOUT
+#define AP_MPM_WANT_PROCESS_CHILD_STATUS
+#define AP_MPM_WANT_SET_PIDFILE
+#define AP_MPM_WANT_SET_SCOREBOARD
+#define AP_MPM_WANT_SET_LOCKFILE
+#define AP_MPM_WANT_SET_MAX_REQUESTS
+#define AP_MPM_WANT_SET_COREDUMPDIR
+#define AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
+#define AP_MPM_WANT_SIGNAL_SERVER
+#define AP_MPM_WANT_SET_MAX_MEM_FREE
+#define AP_MPM_WANT_SET_STACKSIZE
+#define AP_MPM_WANT_SET_GRACEFUL_SHUTDOWN
+#define AP_MPM_WANT_FATAL_SIGNAL_HANDLER
+#define AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
+
+#define MPM_CHILD_PID(i) (ap_scoreboard_image->parent[i].pid)
+#define MPM_NOTE_CHILD_KILLED(i) (MPM_CHILD_PID(i) = 0)
+#define MPM_ACCEPT_FUNC unixd_accept
+
+extern int ap_threads_per_child;
+extern int ap_max_daemons_limit;
+extern server_rec *ap_server_conf;
+extern char ap_coredump_dir[MAX_STRING_LEN];
+
+#endif /* APACHE_MPM_WORKER_H */
+/** @} */
diff --git a/server/mpm/worker/mpm_default.h b/server/mpm/worker/mpm_default.h
new file mode 100644
index 00000000..a48f6725
--- /dev/null
+++ b/server/mpm/worker/mpm_default.h
@@ -0,0 +1,78 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file worker/mpm_default.h
+ * @brief Worker MPM defaults
+ *
+ * @addtogroup APACHE_MPM_WORKER
+ * @{
+ */
+
+#ifndef APACHE_MPM_DEFAULT_H
+#define APACHE_MPM_DEFAULT_H
+
+/* Number of servers to spawn off by default --- also, if fewer than
+ * this free when the caretaker checks, it will spawn more.
+ */
+#ifndef DEFAULT_START_DAEMON
+#define DEFAULT_START_DAEMON 3
+#endif
+
+/* Maximum number of *free* server processes --- more than this, and
+ * they will die off.
+ */
+
+#ifndef DEFAULT_MAX_FREE_DAEMON
+#define DEFAULT_MAX_FREE_DAEMON 10
+#endif
+
+/* Minimum --- fewer than this, and more will be created */
+
+#ifndef DEFAULT_MIN_FREE_DAEMON
+#define DEFAULT_MIN_FREE_DAEMON 3
+#endif
+
+#ifndef DEFAULT_THREADS_PER_CHILD
+#define DEFAULT_THREADS_PER_CHILD 25
+#endif
+
+/* File used for accept locking, when we use a file */
+#ifndef DEFAULT_LOCKFILE
+#define DEFAULT_LOCKFILE DEFAULT_REL_RUNTIMEDIR "/accept.lock"
+#endif
+
+/* Where the main/parent process's pid is logged */
+#ifndef DEFAULT_PIDLOG
+#define DEFAULT_PIDLOG DEFAULT_REL_RUNTIMEDIR "/httpd.pid"
+#endif
+
+/*
+ * Interval, in microseconds, between scoreboard maintenance.
+ */
+#ifndef SCOREBOARD_MAINTENANCE_INTERVAL
+#define SCOREBOARD_MAINTENANCE_INTERVAL 1000000
+#endif
+
+/* Number of requests to try to handle in a single process. If <= 0,
+ * the children don't die off.
+ */
+#ifndef DEFAULT_MAX_REQUESTS_PER_CHILD
+#define DEFAULT_MAX_REQUESTS_PER_CHILD 10000
+#endif
+
+#endif /* AP_MPM_DEFAULT_H */
+/** @} */
diff --git a/server/mpm/worker/pod.c b/server/mpm/worker/pod.c
new file mode 100644
index 00000000..77ad1c90
--- /dev/null
+++ b/server/mpm/worker/pod.c
@@ -0,0 +1,110 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "pod.h"
+
+#if APR_HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
+{
+ apr_status_t rv;
+
+ *pod = apr_palloc(p, sizeof(**pod));
+ rv = apr_file_pipe_create(&((*pod)->pod_in), &((*pod)->pod_out), p);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+/*
+ apr_file_pipe_timeout_set((*pod)->pod_in, 0);
+*/
+ (*pod)->p = p;
+
+ /* close these before exec. */
+ apr_file_inherit_unset((*pod)->pod_in);
+ apr_file_inherit_unset((*pod)->pod_out);
+
+ return APR_SUCCESS;
+}
+
+AP_DECLARE(int) ap_mpm_pod_check(ap_pod_t *pod)
+{
+ char c;
+ apr_os_file_t fd;
+ int rc;
+
+ /* we need to surface EINTR so we'll have to grab the
+ * native file descriptor and do the OS read() ourselves
+ */
+ apr_os_file_get(&fd, pod->pod_in);
+ rc = read(fd, &c, 1);
+ if (rc == 1) {
+ switch(c) {
+ case RESTART_CHAR:
+ return AP_RESTART;
+ case GRACEFUL_CHAR:
+ return AP_GRACEFUL;
+ }
+ }
+ return AP_NORESTART;
+}
+
+AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
+{
+ apr_status_t rv;
+
+ rv = apr_file_close(pod->pod_out);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+
+ rv = apr_file_close(pod->pod_in);
+ if (rv != APR_SUCCESS) {
+ return rv;
+ }
+ return rv;
+}
+
+static apr_status_t pod_signal_internal(ap_pod_t *pod, int graceful)
+{
+ apr_status_t rv;
+ char char_of_death = graceful ? GRACEFUL_CHAR : RESTART_CHAR;
+ apr_size_t one = 1;
+
+ rv = apr_file_write(pod->pod_out, &char_of_death, &one);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
+ "write pipe_of_death");
+ }
+ return rv;
+}
+
+AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod, int graceful)
+{
+ return pod_signal_internal(pod, graceful);
+}
+
+AP_DECLARE(void) ap_mpm_pod_killpg(ap_pod_t *pod, int num, int graceful)
+{
+ int i;
+ apr_status_t rv = APR_SUCCESS;
+
+ for (i = 0; i < num && rv == APR_SUCCESS; i++) {
+ rv = pod_signal_internal(pod, graceful);
+ }
+}
+
diff --git a/server/mpm/worker/pod.h b/server/mpm/worker/pod.h
new file mode 100644
index 00000000..55cad819
--- /dev/null
+++ b/server/mpm/worker/pod.h
@@ -0,0 +1,59 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file worker/pod.h
+ * @brief Worker MPM Pipe of Death
+ *
+ * @addtogroup APACHE_MPM_WORKER
+ * @{
+ */
+
+#include "apr.h"
+#include "apr_strings.h"
+#define APR_WANT_STRFUNC
+#include "apr_want.h"
+
+#include "httpd.h"
+#include "http_config.h"
+#include "http_log.h"
+#include "http_main.h"
+#include "mpm.h"
+#include "mpm_common.h"
+#include "ap_mpm.h"
+#include "ap_listen.h"
+#include "mpm_default.h"
+
+#define RESTART_CHAR '$'
+#define GRACEFUL_CHAR '!'
+
+#define AP_RESTART 0
+#define AP_GRACEFUL 1
+
+typedef struct ap_pod_t ap_pod_t;
+
+struct ap_pod_t {
+ apr_file_t *pod_in;
+ apr_file_t *pod_out;
+ apr_pool_t *p;
+};
+
+AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod);
+AP_DECLARE(int) ap_mpm_pod_check(ap_pod_t *pod);
+AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod);
+AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod, int graceful);
+AP_DECLARE(void) ap_mpm_pod_killpg(ap_pod_t *pod, int num, int graceful);
+/** @} */
diff --git a/server/mpm/worker/worker.c b/server/mpm/worker/worker.c
new file mode 100644
index 00000000..b71cd237
--- /dev/null
+++ b/server/mpm/worker/worker.c
@@ -0,0 +1,2262 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* The purpose of this MPM is to fix the design flaws in the threaded
+ * model. Because of the way that pthreads and mutex locks interact,
+ * it is basically impossible to cleanly gracefully shutdown a child
+ * process if multiple threads are all blocked in accept. This model
+ * fixes those problems.
+ */
+
+#include "apr.h"
+#include "apr_portable.h"
+#include "apr_strings.h"
+#include "apr_file_io.h"
+#include "apr_thread_proc.h"
+#include "apr_signal.h"
+#include "apr_thread_mutex.h"
+#include "apr_proc_mutex.h"
+#include "apr_poll.h"
+#define APR_WANT_STRFUNC
+#include "apr_want.h"
+
+#if APR_HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#if APR_HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+#if APR_HAVE_SYS_WAIT_H
+#include <sys/wait.h>
+#endif
+#ifdef HAVE_SYS_PROCESSOR_H
+#include <sys/processor.h> /* for bindprocessor() */
+#endif
+
+#if !APR_HAS_THREADS
+#error The Worker MPM requires APR threads, but they are unavailable.
+#endif
+
+#define CORE_PRIVATE
+
+#include "ap_config.h"
+#include "httpd.h"
+#include "http_main.h"
+#include "http_log.h"
+#include "http_config.h" /* for read_config */
+#include "http_core.h" /* for get_remote_host */
+#include "http_connection.h"
+#include "ap_mpm.h"
+#include "pod.h"
+#include "mpm_common.h"
+#include "ap_listen.h"
+#include "scoreboard.h"
+#include "fdqueue.h"
+#include "mpm_default.h"
+
+#include <signal.h>
+#include <limits.h> /* for INT_MAX */
+
+/* Limit on the total --- clients will be locked out if more servers than
+ * this are needed. It is intended solely to keep the server from crashing
+ * when things get out of hand.
+ *
+ * We keep a hard maximum number of servers, for two reasons --- first off,
+ * in case something goes seriously wrong, we want to stop the fork bomb
+ * short of actually crashing the machine we're running on by filling some
+ * kernel table. Secondly, it keeps the size of the scoreboard file small
+ * enough that we can read the whole thing without worrying too much about
+ * the overhead.
+ */
+#ifndef DEFAULT_SERVER_LIMIT
+#define DEFAULT_SERVER_LIMIT 16
+#endif
+
+/* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT. We want
+ * some sort of compile-time limit to help catch typos.
+ */
+#ifndef MAX_SERVER_LIMIT
+#define MAX_SERVER_LIMIT 20000
+#endif
+
+/* Limit on the threads per process. Clients will be locked out if more than
+ * this * server_limit are needed.
+ *
+ * We keep this for one reason it keeps the size of the scoreboard file small
+ * enough that we can read the whole thing without worrying too much about
+ * the overhead.
+ */
+#ifndef DEFAULT_THREAD_LIMIT
+#define DEFAULT_THREAD_LIMIT 64
+#endif
+
+/* Admin can't tune ThreadLimit beyond MAX_THREAD_LIMIT. We want
+ * some sort of compile-time limit to help catch typos.
+ */
+#ifndef MAX_THREAD_LIMIT
+#define MAX_THREAD_LIMIT 20000
+#endif
+
+/*
+ * Actual definitions of config globals
+ */
+
+int ap_threads_per_child = 0; /* Worker threads per child */
+static int ap_daemons_to_start = 0;
+static int min_spare_threads = 0;
+static int max_spare_threads = 0;
+static int ap_daemons_limit = 0;
+static int server_limit = DEFAULT_SERVER_LIMIT;
+static int first_server_limit = 0;
+static int thread_limit = DEFAULT_THREAD_LIMIT;
+static int first_thread_limit = 0;
+static int changed_limit_at_restart;
+static int dying = 0;
+static int workers_may_exit = 0;
+static int start_thread_may_exit = 0;
+static int listener_may_exit = 0;
+static int requests_this_child;
+static int num_listensocks = 0;
+static int resource_shortage = 0;
+static fd_queue_t *worker_queue;
+static fd_queue_info_t *worker_queue_info;
+static int mpm_state = AP_MPMQ_STARTING;
+static int sick_child_detected;
+
+/* The structure used to pass unique initialization info to each thread */
+typedef struct {
+ int pid;
+ int tid;
+ int sd;
+} proc_info;
+
+/* Structure used to pass information to the thread responsible for
+ * creating the rest of the threads.
+ */
+typedef struct {
+ apr_thread_t **threads;
+ apr_thread_t *listener;
+ int child_num_arg;
+ apr_threadattr_t *threadattr;
+} thread_starter;
+
+#define ID_FROM_CHILD_THREAD(c, t) ((c * thread_limit) + t)
+
+/*
+ * The max child slot ever assigned, preserved across restarts. Necessary
+ * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts. We
+ * use this value to optimize routines that have to scan the entire
+ * scoreboard.
+ */
+int ap_max_daemons_limit = -1;
+
+static ap_pod_t *pod;
+
+/* *Non*-shared http_main globals... */
+
+server_rec *ap_server_conf;
+
+/* The worker MPM respects a couple of runtime flags that can aid
+ * in debugging. Setting the -DNO_DETACH flag will prevent the root process
+ * from detaching from its controlling terminal. Additionally, setting
+ * the -DONE_PROCESS flag (which implies -DNO_DETACH) will get you the
+ * child_main loop running in the process which originally started up.
+ * This gives you a pretty nice debugging environment. (You'll get a SIGHUP
+ * early in standalone_main; just continue through. This is the server
+ * trying to kill off any child processes which it might have lying
+ * around --- Apache doesn't keep track of their pids, it just sends
+ * SIGHUP to the process group, ignoring it in the root process.
+ * Continue through and you'll be fine.).
+ */
+
+static int one_process = 0;
+
+#ifdef DEBUG_SIGSTOP
+int raise_sigstop_flags;
+#endif
+
+static apr_pool_t *pconf; /* Pool for config stuff */
+static apr_pool_t *pchild; /* Pool for httpd child stuff */
+
+static pid_t ap_my_pid; /* Linux getpid() doesn't work except in main
+ thread. Use this instead */
+static pid_t parent_pid;
+static apr_os_thread_t *listener_os_thread;
+
+/* Locks for accept serialization */
+static apr_proc_mutex_t *accept_mutex;
+
+#ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
+#define SAFE_ACCEPT(stmt) (ap_listeners->next ? (stmt) : APR_SUCCESS)
+#else
+#define SAFE_ACCEPT(stmt) (stmt)
+#endif
+
+/* The LISTENER_SIGNAL signal will be sent from the main thread to the
+ * listener thread to wake it up for graceful termination (what a child
+ * process from an old generation does when the admin does "apachectl
+ * graceful"). This signal will be blocked in all threads of a child
+ * process except for the listener thread.
+ */
+#define LISTENER_SIGNAL SIGHUP
+
+/* The WORKER_SIGNAL signal will be sent from the main thread to the
+ * worker threads during an ungraceful restart or shutdown.
+ * This ensures that on systems (i.e., Linux) where closing the worker
+ * socket doesn't awake the worker thread when it is polling on the socket
+ * (especially in apr_wait_for_io_or_timeout() when handling
+ * Keep-Alive connections), close_worker_sockets() and join_workers()
+ * still function in timely manner and allow ungraceful shutdowns to
+ * proceed to completion. Otherwise join_workers() doesn't return
+ * before the main process decides the child process is non-responsive
+ * and sends a SIGKILL.
+ */
+#define WORKER_SIGNAL AP_SIG_GRACEFUL
+
+/* An array of socket descriptors in use by each thread used to
+ * perform a non-graceful (forced) shutdown of the server. */
+static apr_socket_t **worker_sockets;
+
+static void close_worker_sockets(void)
+{
+ int i;
+ for (i = 0; i < ap_threads_per_child; i++) {
+ if (worker_sockets[i]) {
+ apr_socket_close(worker_sockets[i]);
+ worker_sockets[i] = NULL;
+ }
+ }
+}
+
+static void wakeup_listener(void)
+{
+ listener_may_exit = 1;
+ if (!listener_os_thread) {
+ /* XXX there is an obscure path that this doesn't handle perfectly:
+ * right after listener thread is created but before
+ * listener_os_thread is set, the first worker thread hits an
+ * error and starts graceful termination
+ */
+ return;
+ }
+ /*
+ * we should just be able to "kill(ap_my_pid, LISTENER_SIGNAL)" on all
+ * platforms and wake up the listener thread since it is the only thread
+ * with SIGHUP unblocked, but that doesn't work on Linux
+ */
+#ifdef HAVE_PTHREAD_KILL
+ pthread_kill(*listener_os_thread, LISTENER_SIGNAL);
+#else
+ kill(ap_my_pid, LISTENER_SIGNAL);
+#endif
+}
+
+#define ST_INIT 0
+#define ST_GRACEFUL 1
+#define ST_UNGRACEFUL 2
+
+static int terminate_mode = ST_INIT;
+
+static void signal_threads(int mode)
+{
+ if (terminate_mode == mode) {
+ return;
+ }
+ terminate_mode = mode;
+ mpm_state = AP_MPMQ_STOPPING;
+
+ /* in case we weren't called from the listener thread, wake up the
+ * listener thread
+ */
+ wakeup_listener();
+
+ /* for ungraceful termination, let the workers exit now;
+ * for graceful termination, the listener thread will notify the
+ * workers to exit once it has stopped accepting new connections
+ */
+ if (mode == ST_UNGRACEFUL) {
+ workers_may_exit = 1;
+ ap_queue_interrupt_all(worker_queue);
+ ap_queue_info_term(worker_queue_info);
+ close_worker_sockets(); /* forcefully kill all current connections */
+ }
+}
+
+AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
+{
+ switch(query_code){
+ case AP_MPMQ_MAX_DAEMON_USED:
+ *result = ap_max_daemons_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_THREADED:
+ *result = AP_MPMQ_STATIC;
+ return APR_SUCCESS;
+ case AP_MPMQ_IS_FORKED:
+ *result = AP_MPMQ_DYNAMIC;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_DAEMONS:
+ *result = server_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_HARD_LIMIT_THREADS:
+ *result = thread_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_THREADS:
+ *result = ap_threads_per_child;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MIN_SPARE_THREADS:
+ *result = min_spare_threads;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_DAEMONS:
+ *result = 0;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_SPARE_THREADS:
+ *result = max_spare_threads;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_REQUESTS_DAEMON:
+ *result = ap_max_requests_per_child;
+ return APR_SUCCESS;
+ case AP_MPMQ_MAX_DAEMONS:
+ *result = ap_daemons_limit;
+ return APR_SUCCESS;
+ case AP_MPMQ_MPM_STATE:
+ *result = mpm_state;
+ return APR_SUCCESS;
+ }
+ return APR_ENOTIMPL;
+}
+
+/* a clean exit from a child with proper cleanup */
+static void clean_child_exit(int code) __attribute__ ((noreturn));
+static void clean_child_exit(int code)
+{
+ mpm_state = AP_MPMQ_STOPPING;
+ if (pchild) {
+ apr_pool_destroy(pchild);
+ }
+ exit(code);
+}
+
+static void just_die(int sig)
+{
+ clean_child_exit(0);
+}
+
+/*****************************************************************
+ * Connection structures and accounting...
+ */
+
+/* volatile just in case */
+static int volatile shutdown_pending;
+static int volatile restart_pending;
+static int volatile is_graceful;
+static volatile int child_fatal;
+ap_generation_t volatile ap_my_generation;
+
+/*
+ * ap_start_shutdown() and ap_start_restart(), below, are a first stab at
+ * functions to initiate shutdown or restart without relying on signals.
+ * Previously this was initiated in sig_term() and restart() signal handlers,
+ * but we want to be able to start a shutdown/restart from other sources --
+ * e.g. on Win32, from the service manager. Now the service manager can
+ * call ap_start_shutdown() or ap_start_restart() as appropiate. Note that
+ * these functions can also be called by the child processes, since global
+ * variables are no longer used to pass on the required action to the parent.
+ *
+ * These should only be called from the parent process itself, since the
+ * parent process will use the shutdown_pending and restart_pending variables
+ * to determine whether to shutdown or restart. The child process should
+ * call signal_parent() directly to tell the parent to die -- this will
+ * cause neither of those variable to be set, which the parent will
+ * assume means something serious is wrong (which it will be, for the
+ * child to force an exit) and so do an exit anyway.
+ */
+
+static void ap_start_shutdown(int graceful)
+{
+ mpm_state = AP_MPMQ_STOPPING;
+ if (shutdown_pending == 1) {
+ /* Um, is this _probably_ not an error, if the user has
+ * tried to do a shutdown twice quickly, so we won't
+ * worry about reporting it.
+ */
+ return;
+ }
+ shutdown_pending = 1;
+ is_graceful = graceful;
+}
+
+/* do a graceful restart if graceful == 1 */
+static void ap_start_restart(int graceful)
+{
+ mpm_state = AP_MPMQ_STOPPING;
+ if (restart_pending == 1) {
+ /* Probably not an error - don't bother reporting it */
+ return;
+ }
+ restart_pending = 1;
+ is_graceful = graceful;
+}
+
+static void sig_term(int sig)
+{
+ ap_start_shutdown(sig == AP_SIG_GRACEFUL_STOP);
+}
+
+static void restart(int sig)
+{
+ ap_start_restart(sig == AP_SIG_GRACEFUL);
+}
+
+static void set_signals(void)
+{
+#ifndef NO_USE_SIGACTION
+ struct sigaction sa;
+#endif
+
+ if (!one_process) {
+ ap_fatal_signal_setup(ap_server_conf, pconf);
+ }
+
+#ifndef NO_USE_SIGACTION
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+
+ sa.sa_handler = sig_term;
+ if (sigaction(SIGTERM, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(SIGTERM)");
+#ifdef AP_SIG_GRACEFUL_STOP
+ if (sigaction(AP_SIG_GRACEFUL_STOP, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(" AP_SIG_GRACEFUL_STOP_STRING ")");
+#endif
+#ifdef SIGINT
+ if (sigaction(SIGINT, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(SIGINT)");
+#endif
+#ifdef SIGXCPU
+ sa.sa_handler = SIG_DFL;
+ if (sigaction(SIGXCPU, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(SIGXCPU)");
+#endif
+#ifdef SIGXFSZ
+ sa.sa_handler = SIG_DFL;
+ if (sigaction(SIGXFSZ, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(SIGXFSZ)");
+#endif
+#ifdef SIGPIPE
+ sa.sa_handler = SIG_IGN;
+ if (sigaction(SIGPIPE, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(SIGPIPE)");
+#endif
+
+ /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy
+ * processing one */
+ sigaddset(&sa.sa_mask, SIGHUP);
+ sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL);
+ sa.sa_handler = restart;
+ if (sigaction(SIGHUP, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(SIGHUP)");
+ if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
+ "sigaction(" AP_SIG_GRACEFUL_STRING ")");
+#else
+ if (!one_process) {
+#ifdef SIGXCPU
+ apr_signal(SIGXCPU, SIG_DFL);
+#endif /* SIGXCPU */
+#ifdef SIGXFSZ
+ apr_signal(SIGXFSZ, SIG_DFL);
+#endif /* SIGXFSZ */
+ }
+
+ apr_signal(SIGTERM, sig_term);
+#ifdef SIGHUP
+ apr_signal(SIGHUP, restart);
+#endif /* SIGHUP */
+#ifdef AP_SIG_GRACEFUL
+ apr_signal(AP_SIG_GRACEFUL, restart);
+#endif /* AP_SIG_GRACEFUL */
+#ifdef AP_SIG_GRACEFUL_STOP
+ apr_signal(AP_SIG_GRACEFUL_STOP, sig_term);
+#endif /* AP_SIG_GRACEFUL_STOP */
+#ifdef SIGPIPE
+ apr_signal(SIGPIPE, SIG_IGN);
+#endif /* SIGPIPE */
+
+#endif
+}
+
+/*****************************************************************
+ * Here follows a long bunch of generic server bookkeeping stuff...
+ */
+
+int ap_graceful_stop_signalled(void)
+ /* XXX this is really a bad confusing obsolete name
+ * maybe it should be ap_mpm_process_exiting?
+ */
+{
+ /* note: for a graceful termination, listener_may_exit will be set before
+ * workers_may_exit, so check listener_may_exit
+ */
+ return listener_may_exit;
+}
+
+/*****************************************************************
+ * Child process main loop.
+ */
+
+static void process_socket(apr_pool_t *p, apr_socket_t *sock, int my_child_num,
+ int my_thread_num, apr_bucket_alloc_t *bucket_alloc)
+{
+ conn_rec *current_conn;
+ long conn_id = ID_FROM_CHILD_THREAD(my_child_num, my_thread_num);
+ int csd;
+ ap_sb_handle_t *sbh;
+
+ ap_create_sb_handle(&sbh, p, my_child_num, my_thread_num);
+ apr_os_sock_get(&csd, sock);
+
+ current_conn = ap_run_create_connection(p, ap_server_conf, sock,
+ conn_id, sbh, bucket_alloc);
+ if (current_conn) {
+ ap_process_connection(current_conn, sock);
+ ap_lingering_close(current_conn);
+ }
+}
+
+/* requests_this_child has gone to zero or below. See if the admin coded
+ "MaxRequestsPerChild 0", and keep going in that case. Doing it this way
+ simplifies the hot path in worker_thread */
+static void check_infinite_requests(void)
+{
+ if (ap_max_requests_per_child) {
+ signal_threads(ST_GRACEFUL);
+ }
+ else {
+ /* wow! if you're executing this code, you may have set a record.
+ * either this child process has served over 2 billion requests, or
+ * you're running a threaded 2.0 on a 16 bit machine.
+ *
+ * I'll buy pizza and beers at Apachecon for the first person to do
+ * the former without cheating (dorking with INT_MAX, or running with
+ * uncommitted performance patches, for example).
+ *
+ * for the latter case, you probably deserve a beer too. Greg Ames
+ */
+
+ requests_this_child = INT_MAX; /* keep going */
+ }
+}
+
+static void unblock_signal(int sig)
+{
+ sigset_t sig_mask;
+
+ sigemptyset(&sig_mask);
+ sigaddset(&sig_mask, sig);
+#if defined(SIGPROCMASK_SETS_THREAD_MASK)
+ sigprocmask(SIG_UNBLOCK, &sig_mask, NULL);
+#else
+ pthread_sigmask(SIG_UNBLOCK, &sig_mask, NULL);
+#endif
+}
+
+static void dummy_signal_handler(int sig)
+{
+ /* XXX If specifying SIG_IGN is guaranteed to unblock a syscall,
+ * then we don't need this goofy function.
+ */
+}
+
+static void *listener_thread(apr_thread_t *thd, void * dummy)
+{
+ proc_info * ti = dummy;
+ int process_slot = ti->pid;
+ apr_pool_t *tpool = apr_thread_pool_get(thd);
+ void *csd = NULL;
+ apr_pool_t *ptrans = NULL; /* Pool for per-transaction stuff */
+ apr_pollset_t *pollset;
+ apr_status_t rv;
+ ap_listen_rec *lr;
+ int have_idle_worker = 0;
+ int last_poll_idx = 0;
+
+ free(ti);
+
+ /* ### check the status */
+ (void) apr_pollset_create(&pollset, num_listensocks, tpool, 0);
+
+ for (lr = ap_listeners; lr != NULL; lr = lr->next) {
+ apr_pollfd_t pfd = { 0 };
+
+ pfd.desc_type = APR_POLL_SOCKET;
+ pfd.desc.s = lr->sd;
+ pfd.reqevents = APR_POLLIN;
+ pfd.client_data = lr;
+
+ /* ### check the status */
+ (void) apr_pollset_add(pollset, &pfd);
+ }
+
+ /* Unblock the signal used to wake this thread up, and set a handler for
+ * it.
+ */
+ unblock_signal(LISTENER_SIGNAL);
+ apr_signal(LISTENER_SIGNAL, dummy_signal_handler);
+
+ /* TODO: Switch to a system where threads reuse the results from earlier
+ poll calls - manoj */
+ while (1) {
+ /* TODO: requests_this_child should be synchronized - aaron */
+ if (requests_this_child <= 0) {
+ check_infinite_requests();
+ }
+ if (listener_may_exit) break;
+
+ if (!have_idle_worker) {
+ /* the following pops a recycled ptrans pool off a stack
+ * if there is one, in addition to reserving a worker thread
+ */
+ rv = ap_queue_info_wait_for_idler(worker_queue_info,
+ &ptrans);
+ if (APR_STATUS_IS_EOF(rv)) {
+ break; /* we've been signaled to die now */
+ }
+ else if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
+ "apr_queue_info_wait failed. Attempting to "
+ " shutdown process gracefully.");
+ signal_threads(ST_GRACEFUL);
+ break;
+ }
+ have_idle_worker = 1;
+ }
+
+ /* We've already decremented the idle worker count inside
+ * ap_queue_info_wait_for_idler. */
+
+ if ((rv = SAFE_ACCEPT(apr_proc_mutex_lock(accept_mutex)))
+ != APR_SUCCESS) {
+ int level = APLOG_EMERG;
+
+ if (listener_may_exit) {
+ break;
+ }
+ if (ap_scoreboard_image->parent[process_slot].generation !=
+ ap_scoreboard_image->global->running_generation) {
+ level = APLOG_DEBUG; /* common to get these at restart time */
+ }
+ ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
+ "apr_proc_mutex_lock failed. Attempting to shutdown "
+ "process gracefully.");
+ signal_threads(ST_GRACEFUL);
+ break; /* skip the lock release */
+ }
+
+ if (!ap_listeners->next) {
+ /* Only one listener, so skip the poll */
+ lr = ap_listeners;
+ }
+ else {
+ while (!listener_may_exit) {
+ apr_int32_t numdesc;
+ const apr_pollfd_t *pdesc;
+
+ rv = apr_pollset_poll(pollset, -1, &numdesc, &pdesc);
+ if (rv != APR_SUCCESS) {
+ if (APR_STATUS_IS_EINTR(rv)) {
+ continue;
+ }
+
+ /* apr_pollset_poll() will only return errors in catastrophic
+ * circumstances. Let's try exiting gracefully, for now. */
+ ap_log_error(APLOG_MARK, APLOG_ERR, rv,
+ (const server_rec *) ap_server_conf,
+ "apr_pollset_poll: (listen)");
+ signal_threads(ST_GRACEFUL);
+ }
+
+ if (listener_may_exit) break;
+
+ /* We can always use pdesc[0], but sockets at position N
+ * could end up completely starved of attention in a very
+ * busy server. Therefore, we round-robin across the
+ * returned set of descriptors. While it is possible that
+ * the returned set of descriptors might flip around and
+ * continue to starve some sockets, we happen to know the
+ * internal pollset implementation retains ordering
+ * stability of the sockets. Thus, the round-robin should
+ * ensure that a socket will eventually be serviced.
+ */
+ if (last_poll_idx >= numdesc)
+ last_poll_idx = 0;
+
+ /* Grab a listener record from the client_data of the poll
+ * descriptor, and advance our saved index to round-robin
+ * the next fetch.
+ *
+ * ### hmm... this descriptor might have POLLERR rather
+ * ### than POLLIN
+ */
+ lr = pdesc[last_poll_idx++].client_data;
+ break;
+
+ } /* while */
+
+ } /* if/else */
+
+ if (!listener_may_exit) {
+ if (ptrans == NULL) {
+ /* we can't use a recycled transaction pool this time.
+ * create a new transaction pool */
+ apr_allocator_t *allocator;
+
+ apr_allocator_create(&allocator);
+ apr_allocator_max_free_set(allocator, ap_max_mem_free);
+ apr_pool_create_ex(&ptrans, pconf, NULL, allocator);
+ apr_allocator_owner_set(allocator, ptrans);
+ }
+ apr_pool_tag(ptrans, "transaction");
+ rv = lr->accept_func(&csd, lr, ptrans);
+ /* later we trash rv and rely on csd to indicate success/failure */
+ AP_DEBUG_ASSERT(rv == APR_SUCCESS || !csd);
+
+ if (rv == APR_EGENERAL) {
+ /* E[NM]FILE, ENOMEM, etc */
+ resource_shortage = 1;
+ signal_threads(ST_GRACEFUL);
+ }
+ if ((rv = SAFE_ACCEPT(apr_proc_mutex_unlock(accept_mutex)))
+ != APR_SUCCESS) {
+ int level = APLOG_EMERG;
+
+ if (listener_may_exit) {
+ break;
+ }
+ if (ap_scoreboard_image->parent[process_slot].generation !=
+ ap_scoreboard_image->global->running_generation) {
+ level = APLOG_DEBUG; /* common to get these at restart time */
+ }
+ ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
+ "apr_proc_mutex_unlock failed. Attempting to "
+ "shutdown process gracefully.");
+ signal_threads(ST_GRACEFUL);
+ }
+ if (csd != NULL) {
+ rv = ap_queue_push(worker_queue, csd, ptrans);
+ if (rv) {
+ /* trash the connection; we couldn't queue the connected
+ * socket to a worker
+ */
+ apr_socket_close(csd);
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "ap_queue_push failed");
+ }
+ else {
+ have_idle_worker = 0;
+ }
+ }
+ }
+ else {
+ if ((rv = SAFE_ACCEPT(apr_proc_mutex_unlock(accept_mutex)))
+ != APR_SUCCESS) {
+ int level = APLOG_EMERG;
+
+ if (ap_scoreboard_image->parent[process_slot].generation !=
+ ap_scoreboard_image->global->running_generation) {
+ level = APLOG_DEBUG; /* common to get these at restart time */
+ }
+ ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
+ "apr_proc_mutex_unlock failed. Attempting to "
+ "shutdown process gracefully.");
+ signal_threads(ST_GRACEFUL);
+ }
+ break;
+ }
+ }
+
+ ap_close_listeners();
+ ap_queue_term(worker_queue);
+ dying = 1;
+ ap_scoreboard_image->parent[process_slot].quiescing = 1;
+
+ /* wake up the main thread */
+ kill(ap_my_pid, SIGTERM);
+
+ apr_thread_exit(thd, APR_SUCCESS);
+ return NULL;
+}
+
+/* XXX For ungraceful termination/restart, we definitely don't want to
+ * wait for active connections to finish but we may want to wait
+ * for idle workers to get out of the queue code and release mutexes,
+ * since those mutexes are cleaned up pretty soon and some systems
+ * may not react favorably (i.e., segfault) if operations are attempted
+ * on cleaned-up mutexes.
+ */
+static void * APR_THREAD_FUNC worker_thread(apr_thread_t *thd, void * dummy)
+{
+ proc_info * ti = dummy;
+ int process_slot = ti->pid;
+ int thread_slot = ti->tid;
+ apr_socket_t *csd = NULL;
+ apr_bucket_alloc_t *bucket_alloc;
+ apr_pool_t *last_ptrans = NULL;
+ apr_pool_t *ptrans; /* Pool for per-transaction stuff */
+ apr_status_t rv;
+ int is_idle = 0;
+
+ free(ti);
+
+ ap_scoreboard_image->servers[process_slot][thread_slot].pid = ap_my_pid;
+ ap_scoreboard_image->servers[process_slot][thread_slot].generation = ap_my_generation;
+ ap_update_child_status_from_indexes(process_slot, thread_slot, SERVER_STARTING, NULL);
+
+#ifdef HAVE_PTHREAD_KILL
+ unblock_signal(WORKER_SIGNAL);
+ apr_signal(WORKER_SIGNAL, dummy_signal_handler);
+#endif
+
+ while (!workers_may_exit) {
+ if (!is_idle) {
+ rv = ap_queue_info_set_idle(worker_queue_info, last_ptrans);
+ last_ptrans = NULL;
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
+ "ap_queue_info_set_idle failed. Attempting to "
+ "shutdown process gracefully.");
+ signal_threads(ST_GRACEFUL);
+ break;
+ }
+ is_idle = 1;
+ }
+
+ ap_update_child_status_from_indexes(process_slot, thread_slot, SERVER_READY, NULL);
+worker_pop:
+ if (workers_may_exit) {
+ break;
+ }
+ rv = ap_queue_pop(worker_queue, &csd, &ptrans);
+
+ if (rv != APR_SUCCESS) {
+ /* We get APR_EOF during a graceful shutdown once all the connections
+ * accepted by this server process have been handled.
+ */
+ if (APR_STATUS_IS_EOF(rv)) {
+ break;
+ }
+ /* We get APR_EINTR whenever ap_queue_pop() has been interrupted
+ * from an explicit call to ap_queue_interrupt_all(). This allows
+ * us to unblock threads stuck in ap_queue_pop() when a shutdown
+ * is pending.
+ *
+ * If workers_may_exit is set and this is ungraceful termination/
+ * restart, we are bound to get an error on some systems (e.g.,
+ * AIX, which sanity-checks mutex operations) since the queue
+ * may have already been cleaned up. Don't log the "error" if
+ * workers_may_exit is set.
+ */
+ else if (APR_STATUS_IS_EINTR(rv)) {
+ goto worker_pop;
+ }
+ /* We got some other error. */
+ else if (!workers_may_exit) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "ap_queue_pop failed");
+ }
+ continue;
+ }
+ is_idle = 0;
+ worker_sockets[thread_slot] = csd;
+ bucket_alloc = apr_bucket_alloc_create(ptrans);
+ process_socket(ptrans, csd, process_slot, thread_slot, bucket_alloc);
+ worker_sockets[thread_slot] = NULL;
+ requests_this_child--; /* FIXME: should be synchronized - aaron */
+ apr_pool_clear(ptrans);
+ last_ptrans = ptrans;
+ }
+
+ ap_update_child_status_from_indexes(process_slot, thread_slot,
+ (dying) ? SERVER_DEAD : SERVER_GRACEFUL, (request_rec *) NULL);
+
+ apr_thread_exit(thd, APR_SUCCESS);
+ return NULL;
+}
+
+static int check_signal(int signum)
+{
+ switch (signum) {
+ case SIGTERM:
+ case SIGINT:
+ return 1;
+ }
+ return 0;
+}
+
+static void create_listener_thread(thread_starter *ts)
+{
+ int my_child_num = ts->child_num_arg;
+ apr_threadattr_t *thread_attr = ts->threadattr;
+ proc_info *my_info;
+ apr_status_t rv;
+
+ my_info = (proc_info *)malloc(sizeof(proc_info));
+ my_info->pid = my_child_num;
+ my_info->tid = -1; /* listener thread doesn't have a thread slot */
+ my_info->sd = 0;
+ rv = apr_thread_create(&ts->listener, thread_attr, listener_thread,
+ my_info, pchild);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
+ "apr_thread_create: unable to create listener thread");
+ /* let the parent decide how bad this really is */
+ clean_child_exit(APEXIT_CHILDSICK);
+ }
+ apr_os_thread_get(&listener_os_thread, ts->listener);
+}
+
+/* XXX under some circumstances not understood, children can get stuck
+ * in start_threads forever trying to take over slots which will
+ * never be cleaned up; for now there is an APLOG_DEBUG message issued
+ * every so often when this condition occurs
+ */
+static void * APR_THREAD_FUNC start_threads(apr_thread_t *thd, void *dummy)
+{
+ thread_starter *ts = dummy;
+ apr_thread_t **threads = ts->threads;
+ apr_threadattr_t *thread_attr = ts->threadattr;
+ int child_num_arg = ts->child_num_arg;
+ int my_child_num = child_num_arg;
+ proc_info *my_info;
+ apr_status_t rv;
+ int i;
+ int threads_created = 0;
+ int listener_started = 0;
+ int loops;
+ int prev_threads_created;
+
+ /* We must create the fd queues before we start up the listener
+ * and worker threads. */
+ worker_queue = apr_pcalloc(pchild, sizeof(*worker_queue));
+ rv = ap_queue_init(worker_queue, ap_threads_per_child, pchild);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
+ "ap_queue_init() failed");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ rv = ap_queue_info_create(&worker_queue_info, pchild,
+ ap_threads_per_child);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
+ "ap_queue_info_create() failed");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ worker_sockets = apr_pcalloc(pchild, ap_threads_per_child
+ * sizeof(apr_socket_t *));
+
+ loops = prev_threads_created = 0;
+ while (1) {
+ /* ap_threads_per_child does not include the listener thread */
+ for (i = 0; i < ap_threads_per_child; i++) {
+ int status = ap_scoreboard_image->servers[child_num_arg][i].status;
+
+ if (status != SERVER_GRACEFUL && status != SERVER_DEAD) {
+ continue;
+ }
+
+ my_info = (proc_info *)malloc(sizeof(proc_info));
+ if (my_info == NULL) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
+ "malloc: out of memory");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+ my_info->pid = my_child_num;
+ my_info->tid = i;
+ my_info->sd = 0;
+
+ /* We are creating threads right now */
+ ap_update_child_status_from_indexes(my_child_num, i,
+ SERVER_STARTING, NULL);
+ /* We let each thread update its own scoreboard entry. This is
+ * done because it lets us deal with tid better.
+ */
+ rv = apr_thread_create(&threads[i], thread_attr,
+ worker_thread, my_info, pchild);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
+ "apr_thread_create: unable to create worker thread");
+ /* let the parent decide how bad this really is */
+ clean_child_exit(APEXIT_CHILDSICK);
+ }
+ threads_created++;
+ }
+ /* Start the listener only when there are workers available */
+ if (!listener_started && threads_created) {
+ create_listener_thread(ts);
+ listener_started = 1;
+ }
+ if (start_thread_may_exit || threads_created == ap_threads_per_child) {
+ break;
+ }
+ /* wait for previous generation to clean up an entry */
+ apr_sleep(apr_time_from_sec(1));
+ ++loops;
+ if (loops % 120 == 0) { /* every couple of minutes */
+ if (prev_threads_created == threads_created) {
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "child %" APR_PID_T_FMT " isn't taking over "
+ "slots very quickly (%d of %d)",
+ ap_my_pid, threads_created, ap_threads_per_child);
+ }
+ prev_threads_created = threads_created;
+ }
+ }
+
+ /* What state should this child_main process be listed as in the
+ * scoreboard...?
+ * ap_update_child_status_from_indexes(my_child_num, i, SERVER_STARTING,
+ * (request_rec *) NULL);
+ *
+ * This state should be listed separately in the scoreboard, in some kind
+ * of process_status, not mixed in with the worker threads' status.
+ * "life_status" is almost right, but it's in the worker's structure, and
+ * the name could be clearer. gla
+ */
+ apr_thread_exit(thd, APR_SUCCESS);
+ return NULL;
+}
+
+static void join_workers(apr_thread_t *listener, apr_thread_t **threads)
+{
+ int i;
+ apr_status_t rv, thread_rv;
+
+ if (listener) {
+ int iter;
+
+ /* deal with a rare timing window which affects waking up the
+ * listener thread... if the signal sent to the listener thread
+ * is delivered between the time it verifies that the
+ * listener_may_exit flag is clear and the time it enters a
+ * blocking syscall, the signal didn't do any good... work around
+ * that by sleeping briefly and sending it again
+ */
+
+ iter = 0;
+ while (iter < 10 &&
+#ifdef HAVE_PTHREAD_KILL
+ pthread_kill(*listener_os_thread, 0)
+#else
+ kill(ap_my_pid, 0)
+#endif
+ == 0) {
+ /* listener not dead yet */
+ apr_sleep(apr_time_make(0, 500000));
+ wakeup_listener();
+ ++iter;
+ }
+ if (iter >= 10) {
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "the listener thread didn't exit");
+ }
+ else {
+ rv = apr_thread_join(&thread_rv, listener);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "apr_thread_join: unable to join listener thread");
+ }
+ }
+ }
+
+ for (i = 0; i < ap_threads_per_child; i++) {
+ if (threads[i]) { /* if we ever created this thread */
+#ifdef HAVE_PTHREAD_KILL
+ apr_os_thread_t *worker_os_thread;
+
+ apr_os_thread_get(&worker_os_thread, threads[i]);
+ pthread_kill(*worker_os_thread, WORKER_SIGNAL);
+#endif
+
+ rv = apr_thread_join(&thread_rv, threads[i]);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "apr_thread_join: unable to join worker "
+ "thread %d",
+ i);
+ }
+ }
+ }
+}
+
+static void join_start_thread(apr_thread_t *start_thread_id)
+{
+ apr_status_t rv, thread_rv;
+
+ start_thread_may_exit = 1; /* tell it to give up in case it is still
+ * trying to take over slots from a
+ * previous generation
+ */
+ rv = apr_thread_join(&thread_rv, start_thread_id);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
+ "apr_thread_join: unable to join the start "
+ "thread");
+ }
+}
+
+static void child_main(int child_num_arg)
+{
+ apr_thread_t **threads;
+ apr_status_t rv;
+ thread_starter *ts;
+ apr_threadattr_t *thread_attr;
+ apr_thread_t *start_thread_id;
+
+ mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this
+ * child initializes
+ */
+ ap_my_pid = getpid();
+ ap_fatal_signal_child_setup(ap_server_conf);
+ apr_pool_create(&pchild, pconf);
+
+ /*stuff to do before we switch id's, so we have permissions.*/
+ ap_reopen_scoreboard(pchild, NULL, 0);
+
+ rv = SAFE_ACCEPT(apr_proc_mutex_child_init(&accept_mutex, ap_lock_fname,
+ pchild));
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
+ "Couldn't initialize cross-process lock in child");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ if (unixd_setup_child()) {
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ ap_run_child_init(pchild, ap_server_conf);
+
+ /* done with init critical section */
+
+ /* Just use the standard apr_setup_signal_thread to block all signals
+ * from being received. The child processes no longer use signals for
+ * any communication with the parent process.
+ */
+ rv = apr_setup_signal_thread();
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
+ "Couldn't initialize signal thread");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ if (ap_max_requests_per_child) {
+ requests_this_child = ap_max_requests_per_child;
+ }
+ else {
+ /* coding a value of zero means infinity */
+ requests_this_child = INT_MAX;
+ }
+
+ /* Setup worker threads */
+
+ /* clear the storage; we may not create all our threads immediately,
+ * and we want a 0 entry to indicate a thread which was not created
+ */
+ threads = (apr_thread_t **)calloc(1,
+ sizeof(apr_thread_t *) * ap_threads_per_child);
+ if (threads == NULL) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
+ "malloc: out of memory");
+ clean_child_exit(APEXIT_CHILDFATAL);
+ }
+
+ ts = (thread_starter *)apr_palloc(pchild, sizeof(*ts));
+
+ apr_threadattr_create(&thread_attr, pchild);
+ /* 0 means PTHREAD_CREATE_JOINABLE */
+ apr_threadattr_detach_set(thread_attr, 0);
+
+ if (ap_thread_stacksize != 0) {
+ apr_threadattr_stacksize_set(thread_attr, ap_thread_stacksize);
+ }
+
+ ts->threads = threads;
+ ts->listener = NULL;
+ ts->child_num_arg = child_num_arg;
+ ts->threadattr = thread_attr;
+
+ rv = apr_thread_create(&start_thread_id, thread_attr, start_threads,
+ ts, pchild);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
+ "apr_thread_create: unable to create worker thread");
+ /* let the parent decide how bad this really is */
+ clean_child_exit(APEXIT_CHILDSICK);
+ }
+
+ mpm_state = AP_MPMQ_RUNNING;
+
+ /* If we are only running in one_process mode, we will want to
+ * still handle signals. */
+ if (one_process) {
+ /* Block until we get a terminating signal. */
+ apr_signal_thread(check_signal);
+ /* make sure the start thread has finished; signal_threads()
+ * and join_workers() depend on that
+ */
+ /* XXX join_start_thread() won't be awakened if one of our
+ * threads encounters a critical error and attempts to
+ * shutdown this child
+ */
+ join_start_thread(start_thread_id);
+ signal_threads(ST_UNGRACEFUL); /* helps us terminate a little more
+ * quickly than the dispatch of the signal thread
+ * beats the Pipe of Death and the browsers
+ */
+ /* A terminating signal was received. Now join each of the
+ * workers to clean them up.
+ * If the worker already exited, then the join frees
+ * their resources and returns.
+ * If the worker hasn't exited, then this blocks until
+ * they have (then cleans up).
+ */
+ join_workers(ts->listener, threads);
+ }
+ else { /* !one_process */
+ /* remove SIGTERM from the set of blocked signals... if one of
+ * the other threads in the process needs to take us down
+ * (e.g., for MaxRequestsPerChild) it will send us SIGTERM
+ */
+ unblock_signal(SIGTERM);
+ apr_signal(SIGTERM, dummy_signal_handler);
+ /* Watch for any messages from the parent over the POD */
+ while (1) {
+ rv = ap_mpm_pod_check(pod);
+ if (rv == AP_NORESTART) {
+ /* see if termination was triggered while we slept */
+ switch(terminate_mode) {
+ case ST_GRACEFUL:
+ rv = AP_GRACEFUL;
+ break;
+ case ST_UNGRACEFUL:
+ rv = AP_RESTART;
+ break;
+ }
+ }
+ if (rv == AP_GRACEFUL || rv == AP_RESTART) {
+ /* make sure the start thread has finished;
+ * signal_threads() and join_workers depend on that
+ */
+ join_start_thread(start_thread_id);
+ signal_threads(rv == AP_GRACEFUL ? ST_GRACEFUL : ST_UNGRACEFUL);
+ break;
+ }
+ }
+
+ /* A terminating signal was received. Now join each of the
+ * workers to clean them up.
+ * If the worker already exited, then the join frees
+ * their resources and returns.
+ * If the worker hasn't exited, then this blocks until
+ * they have (then cleans up).
+ */
+ join_workers(ts->listener, threads);
+ }
+
+ free(threads);
+
+ clean_child_exit(resource_shortage ? APEXIT_CHILDSICK : 0);
+}
+
+static int make_child(server_rec *s, int slot)
+{
+ int pid;
+
+ if (slot + 1 > ap_max_daemons_limit) {
+ ap_max_daemons_limit = slot + 1;
+ }
+
+ if (one_process) {
+ set_signals();
+ ap_scoreboard_image->parent[slot].pid = getpid();
+ child_main(slot);
+ }
+
+ if ((pid = fork()) == -1) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, errno, s,
+ "fork: Unable to fork new process");
+
+ /* fork didn't succeed. Fix the scoreboard or else
+ * it will say SERVER_STARTING forever and ever
+ */
+ ap_update_child_status_from_indexes(slot, 0, SERVER_DEAD, NULL);
+
+ /* In case system resources are maxxed out, we don't want
+ Apache running away with the CPU trying to fork over and
+ over and over again. */
+ apr_sleep(apr_time_from_sec(10));
+
+ return -1;
+ }
+
+ if (!pid) {
+#ifdef HAVE_BINDPROCESSOR
+ /* By default, AIX binds to a single processor. This bit unbinds
+ * children which will then bind to another CPU.
+ */
+ int status = bindprocessor(BINDPROCESS, (int)getpid(),
+ PROCESSOR_CLASS_ANY);
+ if (status != OK)
+ ap_log_error(APLOG_MARK, APLOG_WARNING, errno,
+ ap_server_conf,
+ "processor unbind failed %d", status);
+#endif
+ RAISE_SIGSTOP(MAKE_CHILD);
+
+ apr_signal(SIGTERM, just_die);
+ child_main(slot);
+
+ clean_child_exit(0);
+ }
+ /* else */
+ if (ap_scoreboard_image->parent[slot].pid != 0) {
+ /* This new child process is squatting on the scoreboard
+ * entry owned by an exiting child process, which cannot
+ * exit until all active requests complete.
+ * Don't forget about this exiting child process, or we
+ * won't be able to kill it if it doesn't exit by the
+ * time the server is shut down.
+ */
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "taking over scoreboard slot from %" APR_PID_T_FMT "%s",
+ ap_scoreboard_image->parent[slot].pid,
+ ap_scoreboard_image->parent[slot].quiescing ?
+ " (quiescing)" : "");
+ ap_register_extra_mpm_process(ap_scoreboard_image->parent[slot].pid);
+ }
+ ap_scoreboard_image->parent[slot].quiescing = 0;
+ ap_scoreboard_image->parent[slot].pid = pid;
+ return 0;
+}
+
+/* start up a bunch of children */
+static void startup_children(int number_to_start)
+{
+ int i;
+
+ for (i = 0; number_to_start && i < ap_daemons_limit; ++i) {
+ if (ap_scoreboard_image->parent[i].pid != 0) {
+ continue;
+ }
+ if (make_child(ap_server_conf, i) < 0) {
+ break;
+ }
+ --number_to_start;
+ }
+}
+
+
+/*
+ * idle_spawn_rate is the number of children that will be spawned on the
+ * next maintenance cycle if there aren't enough idle servers. It is
+ * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
+ * without the need to spawn.
+ */
+static int idle_spawn_rate = 1;
+#ifndef MAX_SPAWN_RATE
+#define MAX_SPAWN_RATE (32)
+#endif
+static int hold_off_on_exponential_spawning;
+
+static void perform_idle_server_maintenance(void)
+{
+ int i, j;
+ int idle_thread_count;
+ worker_score *ws;
+ process_score *ps;
+ int free_length;
+ int totally_free_length = 0;
+ int free_slots[MAX_SPAWN_RATE];
+ int last_non_dead;
+ int total_non_dead;
+ int active_thread_count = 0;
+
+ /* initialize the free_list */
+ free_length = 0;
+
+ idle_thread_count = 0;
+ last_non_dead = -1;
+ total_non_dead = 0;
+
+ for (i = 0; i < ap_daemons_limit; ++i) {
+ /* Initialization to satisfy the compiler. It doesn't know
+ * that ap_threads_per_child is always > 0 */
+ int status = SERVER_DEAD;
+ int any_dying_threads = 0;
+ int any_dead_threads = 0;
+ int all_dead_threads = 1;
+
+ if (i >= ap_max_daemons_limit && totally_free_length == idle_spawn_rate)
+ break;
+ ps = &ap_scoreboard_image->parent[i];
+ for (j = 0; j < ap_threads_per_child; j++) {
+ ws = &ap_scoreboard_image->servers[i][j];
+ status = ws->status;
+
+ /* XXX any_dying_threads is probably no longer needed GLA */
+ any_dying_threads = any_dying_threads ||
+ (status == SERVER_GRACEFUL);
+ any_dead_threads = any_dead_threads || (status == SERVER_DEAD);
+ all_dead_threads = all_dead_threads &&
+ (status == SERVER_DEAD ||
+ status == SERVER_GRACEFUL);
+
+ /* We consider a starting server as idle because we started it
+ * at least a cycle ago, and if it still hasn't finished starting
+ * then we're just going to swamp things worse by forking more.
+ * So we hopefully won't need to fork more if we count it.
+ * This depends on the ordering of SERVER_READY and SERVER_STARTING.
+ */
+ if (ps->pid != 0) { /* XXX just set all_dead_threads in outer for
+ loop if no pid? not much else matters */
+ if (status <= SERVER_READY &&
+ !ps->quiescing &&
+ ps->generation == ap_my_generation) {
+ ++idle_thread_count;
+ }
+ if (status >= SERVER_READY && status < SERVER_GRACEFUL) {
+ ++active_thread_count;
+ }
+ }
+ }
+ if (any_dead_threads && totally_free_length < idle_spawn_rate
+ && free_length < MAX_SPAWN_RATE
+ && (!ps->pid /* no process in the slot */
+ || ps->quiescing)) { /* or at least one is going away */
+ if (all_dead_threads) {
+ /* great! we prefer these, because the new process can
+ * start more threads sooner. So prioritize this slot
+ * by putting it ahead of any slots with active threads.
+ *
+ * first, make room by moving a slot that's potentially still
+ * in use to the end of the array
+ */
+ free_slots[free_length] = free_slots[totally_free_length];
+ free_slots[totally_free_length++] = i;
+ }
+ else {
+ /* slot is still in use - back of the bus
+ */
+ free_slots[free_length] = i;
+ }
+ ++free_length;
+ }
+ /* XXX if (!ps->quiescing) is probably more reliable GLA */
+ if (!any_dying_threads) {
+ last_non_dead = i;
+ ++total_non_dead;
+ }
+ }
+
+ if (sick_child_detected) {
+ if (active_thread_count > 0) {
+ /* some child processes appear to be working. don't kill the
+ * whole server.
+ */
+ sick_child_detected = 0;
+ }
+ else {
+ /* looks like a basket case. give up.
+ */
+ shutdown_pending = 1;
+ child_fatal = 1;
+ ap_log_error(APLOG_MARK, APLOG_ALERT, 0,
+ ap_server_conf,
+ "No active workers found..."
+ " Apache is exiting!");
+ /* the child already logged the failure details */
+ return;
+ }
+ }
+
+ ap_max_daemons_limit = last_non_dead + 1;
+
+ if (idle_thread_count > max_spare_threads) {
+ /* Kill off one child */
+ ap_mpm_pod_signal(pod, TRUE);
+ idle_spawn_rate = 1;
+ }
+ else if (idle_thread_count < min_spare_threads) {
+ /* terminate the free list */
+ if (free_length == 0) {
+ /* only report this condition once */
+ static int reported = 0;
+
+ if (!reported) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0,
+ ap_server_conf,
+ "server reached MaxClients setting, consider"
+ " raising the MaxClients setting");
+ reported = 1;
+ }
+ idle_spawn_rate = 1;
+ }
+ else {
+ if (free_length > idle_spawn_rate) {
+ free_length = idle_spawn_rate;
+ }
+ if (idle_spawn_rate >= 8) {
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0,
+ ap_server_conf,
+ "server seems busy, (you may need "
+ "to increase StartServers, ThreadsPerChild "
+ "or Min/MaxSpareThreads), "
+ "spawning %d children, there are around %d idle "
+ "threads, and %d total children", free_length,
+ idle_thread_count, total_non_dead);
+ }
+ for (i = 0; i < free_length; ++i) {
+ make_child(ap_server_conf, free_slots[i]);
+ }
+ /* the next time around we want to spawn twice as many if this
+ * wasn't good enough, but not if we've just done a graceful
+ */
+ if (hold_off_on_exponential_spawning) {
+ --hold_off_on_exponential_spawning;
+ }
+ else if (idle_spawn_rate < MAX_SPAWN_RATE) {
+ idle_spawn_rate *= 2;
+ }
+ }
+ }
+ else {
+ idle_spawn_rate = 1;
+ }
+}
+
+static void server_main_loop(int remaining_children_to_start)
+{
+ int child_slot;
+ apr_exit_why_e exitwhy;
+ int status, processed_status;
+ apr_proc_t pid;
+ int i;
+
+ while (!restart_pending && !shutdown_pending) {
+ ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
+
+ if (pid.pid != -1) {
+ processed_status = ap_process_child_status(&pid, exitwhy, status);
+ if (processed_status == APEXIT_CHILDFATAL) {
+ shutdown_pending = 1;
+ child_fatal = 1;
+ return;
+ }
+ else if (processed_status == APEXIT_CHILDSICK) {
+ /* tell perform_idle_server_maintenance to check into this
+ * on the next timer pop
+ */
+ sick_child_detected = 1;
+ }
+ /* non-fatal death... note that it's gone in the scoreboard. */
+ child_slot = find_child_by_pid(&pid);
+ if (child_slot >= 0) {
+ for (i = 0; i < ap_threads_per_child; i++)
+ ap_update_child_status_from_indexes(child_slot, i, SERVER_DEAD,
+ (request_rec *) NULL);
+
+ ap_scoreboard_image->parent[child_slot].pid = 0;
+ ap_scoreboard_image->parent[child_slot].quiescing = 0;
+ if (processed_status == APEXIT_CHILDSICK) {
+ /* resource shortage, minimize the fork rate */
+ idle_spawn_rate = 1;
+ }
+ else if (remaining_children_to_start
+ && child_slot < ap_daemons_limit) {
+ /* we're still doing a 1-for-1 replacement of dead
+ * children with new children
+ */
+ make_child(ap_server_conf, child_slot);
+ --remaining_children_to_start;
+ }
+ }
+ else if (ap_unregister_extra_mpm_process(pid.pid) == 1) {
+ /* handled */
+#if APR_HAS_OTHER_CHILD
+ }
+ else if (apr_proc_other_child_alert(&pid, APR_OC_REASON_DEATH,
+ status) == 0) {
+ /* handled */
+#endif
+ }
+ else if (is_graceful) {
+ /* Great, we've probably just lost a slot in the
+ * scoreboard. Somehow we don't know about this child.
+ */
+ ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
+ ap_server_conf,
+ "long lost child came home! (pid %ld)",
+ (long)pid.pid);
+ }
+ /* Don't perform idle maintenance when a child dies,
+ * only do it when there's a timeout. Remember only a
+ * finite number of children can die, and it's pretty
+ * pathological for a lot to die suddenly.
+ */
+ continue;
+ }
+ else if (remaining_children_to_start) {
+ /* we hit a 1 second timeout in which none of the previous
+ * generation of children needed to be reaped... so assume
+ * they're all done, and pick up the slack if any is left.
+ */
+ startup_children(remaining_children_to_start);
+ remaining_children_to_start = 0;
+ /* In any event we really shouldn't do the code below because
+ * few of the servers we just started are in the IDLE state
+ * yet, so we'd mistakenly create an extra server.
+ */
+ continue;
+ }
+
+ perform_idle_server_maintenance();
+ }
+}
+
+int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
+{
+ int remaining_children_to_start;
+ apr_status_t rv;
+
+ ap_log_pid(pconf, ap_pid_fname);
+
+ first_server_limit = server_limit;
+ first_thread_limit = thread_limit;
+ if (changed_limit_at_restart) {
+ ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
+ "WARNING: Attempt to change ServerLimit or ThreadLimit "
+ "ignored during restart");
+ changed_limit_at_restart = 0;
+ }
+
+ /* Initialize cross-process accept lock */
+ ap_lock_fname = apr_psprintf(_pconf, "%s.%" APR_PID_T_FMT,
+ ap_server_root_relative(_pconf, ap_lock_fname),
+ ap_my_pid);
+
+ rv = apr_proc_mutex_create(&accept_mutex, ap_lock_fname,
+ ap_accept_lock_mech, _pconf);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
+ "Couldn't create accept lock");
+ mpm_state = AP_MPMQ_STOPPING;
+ return 1;
+ }
+
+#if APR_USE_SYSVSEM_SERIALIZE
+ if (ap_accept_lock_mech == APR_LOCK_DEFAULT ||
+ ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
+#else
+ if (ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
+#endif
+ rv = unixd_set_proc_mutex_perms(accept_mutex);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
+ "Couldn't set permissions on cross-process lock; "
+ "check User and Group directives");
+ mpm_state = AP_MPMQ_STOPPING;
+ return 1;
+ }
+ }
+
+ if (!is_graceful) {
+ if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
+ mpm_state = AP_MPMQ_STOPPING;
+ return 1;
+ }
+ /* fix the generation number in the global score; we just got a new,
+ * cleared scoreboard
+ */
+ ap_scoreboard_image->global->running_generation = ap_my_generation;
+ }
+
+ set_signals();
+ /* Don't thrash... */
+ if (max_spare_threads < min_spare_threads + ap_threads_per_child)
+ max_spare_threads = min_spare_threads + ap_threads_per_child;
+
+ /* If we're doing a graceful_restart then we're going to see a lot
+ * of children exiting immediately when we get into the main loop
+ * below (because we just sent them AP_SIG_GRACEFUL). This happens pretty
+ * rapidly... and for each one that exits we'll start a new one until
+ * we reach at least daemons_min_free. But we may be permitted to
+ * start more than that, so we'll just keep track of how many we're
+ * supposed to start up without the 1 second penalty between each fork.
+ */
+ remaining_children_to_start = ap_daemons_to_start;
+ if (remaining_children_to_start > ap_daemons_limit) {
+ remaining_children_to_start = ap_daemons_limit;
+ }
+ if (!is_graceful) {
+ startup_children(remaining_children_to_start);
+ remaining_children_to_start = 0;
+ }
+ else {
+ /* give the system some time to recover before kicking into
+ * exponential mode */
+ hold_off_on_exponential_spawning = 10;
+ }
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "%s configured -- resuming normal operations",
+ ap_get_server_version());
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
+ "Server built: %s", ap_get_server_built());
+#ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
+ "AcceptMutex: %s (default: %s)",
+ apr_proc_mutex_name(accept_mutex),
+ apr_proc_mutex_defname());
+#endif
+ restart_pending = shutdown_pending = 0;
+ mpm_state = AP_MPMQ_RUNNING;
+
+ server_main_loop(remaining_children_to_start);
+ mpm_state = AP_MPMQ_STOPPING;
+
+ if (shutdown_pending && !is_graceful) {
+ /* Time to shut down:
+ * Kill child processes, tell them to call child_exit, etc...
+ */
+ ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
+ ap_reclaim_child_processes(1); /* Start with SIGTERM */
+
+ if (!child_fatal) {
+ /* cleanup pid file on normal shutdown */
+ const char *pidfile = NULL;
+ pidfile = ap_server_root_relative (pconf, ap_pid_fname);
+ if ( pidfile != NULL && unlink(pidfile) == 0)
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0,
+ ap_server_conf,
+ "removed PID file %s (pid=%" APR_PID_T_FMT ")",
+ pidfile, getpid());
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0,
+ ap_server_conf, "caught SIGTERM, shutting down");
+ }
+ return 1;
+ } else if (shutdown_pending) {
+ /* Time to gracefully shut down:
+ * Kill child processes, tell them to call child_exit, etc...
+ */
+ int active_children;
+ int index;
+ apr_time_t cutoff = 0;
+
+ /* Close our listeners, and then ask our children to do same */
+ ap_close_listeners();
+ ap_mpm_pod_killpg(pod, ap_daemons_limit, TRUE);
+ ap_relieve_child_processes();
+
+ if (!child_fatal) {
+ /* cleanup pid file on normal shutdown */
+ const char *pidfile = NULL;
+ pidfile = ap_server_root_relative (pconf, ap_pid_fname);
+ if ( pidfile != NULL && unlink(pidfile) == 0)
+ ap_log_error(APLOG_MARK, APLOG_INFO, 0,
+ ap_server_conf,
+ "removed PID file %s (pid=%" APR_PID_T_FMT ")",
+ pidfile, getpid());
+
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "caught " AP_SIG_GRACEFUL_STOP_STRING
+ ", shutting down gracefully");
+ }
+
+ if (ap_graceful_shutdown_timeout) {
+ cutoff = apr_time_now() +
+ apr_time_from_sec(ap_graceful_shutdown_timeout);
+ }
+
+ /* Don't really exit until each child has finished */
+ shutdown_pending = 0;
+ do {
+ /* Pause for a second */
+ apr_sleep(apr_time_from_sec(1));
+
+ /* Relieve any children which have now exited */
+ ap_relieve_child_processes();
+
+ active_children = 0;
+ for (index = 0; index < ap_daemons_limit; ++index) {
+ if (MPM_CHILD_PID(index) != 0) {
+ if (kill(MPM_CHILD_PID(index), 0) == 0) {
+ active_children = 1;
+ /* Having just one child is enough to stay around */
+ break;
+ }
+ }
+ }
+ } while (!shutdown_pending && active_children &&
+ (!ap_graceful_shutdown_timeout || apr_time_now() < cutoff));
+
+ /* We might be here because we received SIGTERM, either
+ * way, try and make sure that all of our processes are
+ * really dead.
+ */
+ ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
+ ap_reclaim_child_processes(1);
+
+ return 1;
+ }
+
+ /* we've been told to restart */
+ apr_signal(SIGHUP, SIG_IGN);
+
+ if (one_process) {
+ /* not worth thinking about */
+ return 1;
+ }
+
+ /* advance to the next generation */
+ /* XXX: we really need to make sure this new generation number isn't in
+ * use by any of the children.
+ */
+ ++ap_my_generation;
+ ap_scoreboard_image->global->running_generation = ap_my_generation;
+
+ if (is_graceful) {
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ AP_SIG_GRACEFUL_STRING " received. Doing graceful restart");
+ /* wake up the children...time to die. But we'll have more soon */
+ ap_mpm_pod_killpg(pod, ap_daemons_limit, TRUE);
+
+
+ /* This is mostly for debugging... so that we know what is still
+ * gracefully dealing with existing request.
+ */
+
+ }
+ else {
+ /* Kill 'em all. Since the child acts the same on the parents SIGTERM
+ * and a SIGHUP, we may as well use the same signal, because some user
+ * pthreads are stealing signals from us left and right.
+ */
+ ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
+
+ ap_reclaim_child_processes(1); /* Start with SIGTERM */
+ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
+ "SIGHUP received. Attempting to restart");
+ }
+
+ return 0;
+}
+
+/* This really should be a post_config hook, but the error log is already
+ * redirected by that point, so we need to do this in the open_logs phase.
+ */
+static int worker_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
+{
+ apr_status_t rv;
+
+ pconf = p;
+ ap_server_conf = s;
+
+ if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) {
+ ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0,
+ NULL, "no listening sockets available, shutting down");
+ return DONE;
+ }
+
+ if (!one_process) {
+ if ((rv = ap_mpm_pod_open(pconf, &pod))) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT|APLOG_STARTUP, rv, NULL,
+ "Could not open pipe-of-death.");
+ return DONE;
+ }
+ }
+ return OK;
+}
+
+static int worker_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
+ apr_pool_t *ptemp)
+{
+ static int restart_num = 0;
+ int no_detach, debug, foreground;
+ ap_directive_t *pdir;
+ ap_directive_t *max_clients = NULL;
+ apr_status_t rv;
+
+ mpm_state = AP_MPMQ_STARTING;
+
+ /* make sure that "ThreadsPerChild" gets set before "MaxClients" */
+ for (pdir = ap_conftree; pdir != NULL; pdir = pdir->next) {
+ if (strncasecmp(pdir->directive, "ThreadsPerChild", 15) == 0) {
+ if (!max_clients) {
+ break; /* we're in the clear, got ThreadsPerChild first */
+ }
+ else {
+ /* now to swap the data */
+ ap_directive_t temp;
+
+ temp.directive = pdir->directive;
+ temp.args = pdir->args;
+ /* Make sure you don't change 'next', or you may get loops! */
+ /* XXX: first_child, parent, and data can never be set
+ * for these directives, right? -aaron */
+ temp.filename = pdir->filename;
+ temp.line_num = pdir->line_num;
+
+ pdir->directive = max_clients->directive;
+ pdir->args = max_clients->args;
+ pdir->filename = max_clients->filename;
+ pdir->line_num = max_clients->line_num;
+
+ max_clients->directive = temp.directive;
+ max_clients->args = temp.args;
+ max_clients->filename = temp.filename;
+ max_clients->line_num = temp.line_num;
+ break;
+ }
+ }
+ else if (!max_clients
+ && strncasecmp(pdir->directive, "MaxClients", 10) == 0) {
+ max_clients = pdir;
+ }
+ }
+
+ debug = ap_exists_config_define("DEBUG");
+
+ if (debug) {
+ foreground = one_process = 1;
+ no_detach = 0;
+ }
+ else {
+ one_process = ap_exists_config_define("ONE_PROCESS");
+ no_detach = ap_exists_config_define("NO_DETACH");
+ foreground = ap_exists_config_define("FOREGROUND");
+ }
+
+ /* sigh, want this only the second time around */
+ if (restart_num++ == 1) {
+ is_graceful = 0;
+
+ if (!one_process && !foreground) {
+ rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
+ : APR_PROC_DETACH_DAEMONIZE);
+ if (rv != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
+ "apr_proc_detach failed");
+ return HTTP_INTERNAL_SERVER_ERROR;
+ }
+ }
+ parent_pid = ap_my_pid = getpid();
+ }
+
+ unixd_pre_config(ptemp);
+ ap_listen_pre_config();
+ ap_daemons_to_start = DEFAULT_START_DAEMON;
+ min_spare_threads = DEFAULT_MIN_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
+ max_spare_threads = DEFAULT_MAX_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
+ ap_daemons_limit = server_limit;
+ ap_threads_per_child = DEFAULT_THREADS_PER_CHILD;
+ ap_pid_fname = DEFAULT_PIDLOG;
+ ap_lock_fname = DEFAULT_LOCKFILE;
+ ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
+ ap_extended_status = 0;
+#ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
+ ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
+#endif
+
+ apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
+
+ return OK;
+}
+
+static void worker_hooks(apr_pool_t *p)
+{
+ /* The worker open_logs phase must run before the core's, or stderr
+ * will be redirected to a file, and the messages won't print to the
+ * console.
+ */
+ static const char *const aszSucc[] = {"core.c", NULL};
+ one_process = 0;
+
+ ap_hook_open_logs(worker_open_logs, NULL, aszSucc, APR_HOOK_MIDDLE);
+ /* we need to set the MPM state before other pre-config hooks use MPM query
+ * to retrieve it, so register as REALLY_FIRST
+ */
+ ap_hook_pre_config(worker_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
+}
+
+static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy,
+ const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ ap_daemons_to_start = atoi(arg);
+ return NULL;
+}
+
+static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy,
+ const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ min_spare_threads = atoi(arg);
+ if (min_spare_threads <= 0) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: detected MinSpareThreads set to non-positive.");
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Resetting to 1 to avoid almost certain Apache failure.");
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "Please read the documentation.");
+ min_spare_threads = 1;
+ }
+
+ return NULL;
+}
+
+static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy,
+ const char *arg)
+{
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ max_spare_threads = atoi(arg);
+ return NULL;
+}
+
+static const char *set_max_clients (cmd_parms *cmd, void *dummy,
+ const char *arg)
+{
+ int max_clients;
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ /* It is ok to use ap_threads_per_child here because we are
+ * sure that it gets set before MaxClients in the pre_config stage. */
+ max_clients = atoi(arg);
+ if (max_clients < ap_threads_per_child) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: MaxClients (%d) must be at least as large",
+ max_clients);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " as ThreadsPerChild (%d). Automatically",
+ ap_threads_per_child);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " increasing MaxClients to %d.",
+ ap_threads_per_child);
+ max_clients = ap_threads_per_child;
+ }
+ ap_daemons_limit = max_clients / ap_threads_per_child;
+ if ((max_clients > 0) && (max_clients % ap_threads_per_child)) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: MaxClients (%d) is not an integer multiple",
+ max_clients);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " of ThreadsPerChild (%d), lowering MaxClients to %d",
+ ap_threads_per_child,
+ ap_daemons_limit * ap_threads_per_child);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " for a maximum of %d child processes,",
+ ap_daemons_limit);
+ max_clients = ap_daemons_limit * ap_threads_per_child;
+ }
+ if (ap_daemons_limit > server_limit) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: MaxClients of %d would require %d servers,",
+ max_clients, ap_daemons_limit);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " and would exceed the ServerLimit value of %d.",
+ server_limit);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " Automatically lowering MaxClients to %d. To increase,",
+ server_limit * ap_threads_per_child);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " please see the ServerLimit directive.");
+ ap_daemons_limit = server_limit;
+ }
+ else if (ap_daemons_limit < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require MaxClients > 0, setting to 1");
+ ap_daemons_limit = 1;
+ }
+ return NULL;
+}
+
+static const char *set_threads_per_child (cmd_parms *cmd, void *dummy,
+ const 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 > thread_limit) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: ThreadsPerChild of %d exceeds ThreadLimit "
+ "value of %d", ap_threads_per_child,
+ thread_limit);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "threads, lowering ThreadsPerChild to %d. To increase, please"
+ " see the", thread_limit);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " ThreadLimit directive.");
+ ap_threads_per_child = thread_limit;
+ }
+ else if (ap_threads_per_child < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require ThreadsPerChild > 0, setting to 1");
+ ap_threads_per_child = 1;
+ }
+ return NULL;
+}
+
+static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg)
+{
+ int tmp_server_limit;
+
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ tmp_server_limit = atoi(arg);
+ /* you cannot change ServerLimit across a restart; ignore
+ * any such attempts
+ */
+ if (first_server_limit &&
+ tmp_server_limit != server_limit) {
+ /* how do we log a message? the error log is a bit bucket at this
+ * point; we'll just have to set a flag so that ap_mpm_run()
+ * logs a warning later
+ */
+ changed_limit_at_restart = 1;
+ return NULL;
+ }
+ server_limit = tmp_server_limit;
+
+ if (server_limit > MAX_SERVER_LIMIT) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: ServerLimit of %d exceeds compile time limit "
+ "of %d servers,", server_limit, MAX_SERVER_LIMIT);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " lowering ServerLimit to %d.", MAX_SERVER_LIMIT);
+ server_limit = MAX_SERVER_LIMIT;
+ }
+ else if (server_limit < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require ServerLimit > 0, setting to 1");
+ server_limit = 1;
+ }
+ return NULL;
+}
+
+static const char *set_thread_limit (cmd_parms *cmd, void *dummy, const char *arg)
+{
+ int tmp_thread_limit;
+
+ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
+ if (err != NULL) {
+ return err;
+ }
+
+ tmp_thread_limit = atoi(arg);
+ /* you cannot change ThreadLimit across a restart; ignore
+ * any such attempts
+ */
+ if (first_thread_limit &&
+ tmp_thread_limit != thread_limit) {
+ /* how do we log a message? the error log is a bit bucket at this
+ * point; we'll just have to set a flag so that ap_mpm_run()
+ * logs a warning later
+ */
+ changed_limit_at_restart = 1;
+ return NULL;
+ }
+ thread_limit = tmp_thread_limit;
+
+ if (thread_limit > MAX_THREAD_LIMIT) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: ThreadLimit of %d exceeds compile time limit "
+ "of %d servers,", thread_limit, MAX_THREAD_LIMIT);
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ " lowering ThreadLimit to %d.", MAX_THREAD_LIMIT);
+ thread_limit = MAX_THREAD_LIMIT;
+ }
+ else if (thread_limit < 1) {
+ ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
+ "WARNING: Require ThreadLimit > 0, setting to 1");
+ thread_limit = 1;
+ }
+ return NULL;
+}
+
+static const command_rec worker_cmds[] = {
+UNIX_DAEMON_COMMANDS,
+LISTEN_COMMANDS,
+AP_INIT_TAKE1("StartServers", set_daemons_to_start, NULL, RSRC_CONF,
+ "Number of child processes launched at server startup"),
+AP_INIT_TAKE1("MinSpareThreads", set_min_spare_threads, NULL, RSRC_CONF,
+ "Minimum number of idle threads, to handle request spikes"),
+AP_INIT_TAKE1("MaxSpareThreads", set_max_spare_threads, NULL, RSRC_CONF,
+ "Maximum number of idle threads"),
+AP_INIT_TAKE1("MaxClients", set_max_clients, NULL, RSRC_CONF,
+ "Maximum number of threads alive at the same time"),
+AP_INIT_TAKE1("ThreadsPerChild", set_threads_per_child, NULL, RSRC_CONF,
+ "Number of threads each child creates"),
+AP_INIT_TAKE1("ServerLimit", set_server_limit, NULL, RSRC_CONF,
+ "Maximum number of child processes for this run of Apache"),
+AP_INIT_TAKE1("ThreadLimit", set_thread_limit, NULL, RSRC_CONF,
+ "Maximum number of worker threads per child process for this run of Apache - Upper limit for ThreadsPerChild"),
+AP_GRACEFUL_SHUTDOWN_TIMEOUT_COMMAND,
+{ NULL }
+};
+
+module AP_MODULE_DECLARE_DATA mpm_worker_module = {
+ MPM20_MODULE_STUFF,
+ ap_mpm_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 */
+ worker_cmds, /* command apr_table_t */
+ worker_hooks /* register_hooks */
+};
+