1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
* contributor license agreements. See the NOTICE file distributed with
3
* this work for additional information regarding copyright ownership.
4
* The ASF licenses this file to You under the Apache License, Version 2.0
5
* (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
19
* This is a single process, with multiple worker threads.
21
* Under testing I found that given the inability of BeOS to handle threads
22
* and forks it didn't make sense to try and have a set of "children" threads
23
* that spawned the "worker" threads, so just missed out the middle mand and
24
* somehow arrived here.
26
* For 2.1 this has been rewritten to have simpler logic, though there is still
27
* some simplification that can be done. It's still a work in progress!
31
* - on exit most worker threads segfault trying to access a kernel page.
36
#include <kernel/OS.h>
38
#include <sys/socket.h>
41
#include "apr_strings.h"
42
#include "apr_portable.h"
44
#include "http_main.h"
46
#include "http_config.h" /* for read_config */
47
#include "http_core.h" /* for get_remote_host */
48
#include "http_connection.h"
51
#include "ap_listen.h"
52
#include "scoreboard.h"
53
#include "mpm_common.h"
55
#include "mpm_default.h"
56
#include "apr_thread_mutex.h"
59
extern int _kset_fd_limit_(int num);
61
/* Limit on the total --- clients will be locked out if more servers than
62
* this are needed. It is intended solely to keep the server from crashing
63
* when things get out of hand.
65
* We keep a hard maximum number of servers, for two reasons:
66
* 1) in case something goes seriously wrong, we want to stop the server starting
67
* threads ad infinitum and crashing the server (remember that BeOS has a 192
68
* thread per team limit).
69
* 2) it keeps the size of the scoreboard file small
70
* enough that we can read the whole thing without worrying too much about
74
/* we only ever have 1 main process running... */
75
#define HARD_SERVER_LIMIT 1
77
/* Limit on the threads per process. Clients will be locked out if more than
78
* this * HARD_SERVER_LIMIT are needed.
80
* We keep this for one reason it keeps the size of the scoreboard file small
81
* enough that we can read the whole thing without worrying too much about
85
#define HARD_THREAD_LIMIT 1
87
#ifndef HARD_THREAD_LIMIT
88
#define HARD_THREAD_LIMIT 50
92
* Actual definitions of config globals
95
static int ap_threads_to_start=0;
96
static int ap_max_requests_per_thread = 0;
97
static int min_spare_threads=0;
98
static int max_spare_threads=0;
99
static int ap_thread_limit=0;
100
static int num_listening_sockets = 0;
101
static int mpm_state = AP_MPMQ_STARTING;
102
apr_thread_mutex_t *accept_mutex = NULL;
104
static apr_pool_t *pconf; /* Pool for config stuff */
106
static int server_pid;
110
* The max child slot ever assigned, preserved across restarts. Necessary
111
* to deal with MaxClients changes across AP_SIG_GRACEFUL restarts. We use
112
* this value to optimize routines that have to scan the entire scoreboard.
114
int ap_max_child_assigned = -1;
115
int ap_max_threads_limit = -1;
117
static apr_socket_t *udp_sock;
118
static apr_sockaddr_t *udp_sa;
120
server_rec *ap_server_conf;
123
static int one_process = 0;
126
int raise_sigstop_flags;
129
static void check_restart(void *data);
131
/* When a worker thread gets to the end of it's life it dies with an
132
* exit value of the code supplied to this function. The thread has
133
* already had check_restart() registered to be called when dying, so
134
* we don't concern ourselves with restarting at all here. We do however
135
* mark the scoreboard slot as belonging to a dead server and zero out
138
* TODO - use the status we set to determine if we need to restart the
141
static void clean_child_exit(int code, int slot)
143
(void) ap_update_child_status_from_indexes(0, slot, SERVER_DEAD,
145
ap_scoreboard_image->servers[0][slot].tid = 0;
150
/*****************************************************************
151
* Connection structures and accounting...
154
/* volatile just in case */
155
static int volatile shutdown_pending;
156
static int volatile restart_pending;
157
static int volatile is_graceful;
158
static int volatile child_fatal;
159
ap_generation_t volatile ap_my_generation = 0;
162
* ap_start_shutdown() and ap_start_restart(), below, are a first stab at
163
* functions to initiate shutdown or restart without relying on signals.
164
* Previously this was initiated in sig_term() and restart() signal handlers,
165
* but we want to be able to start a shutdown/restart from other sources --
166
* e.g. on Win32, from the service manager. Now the service manager can
167
* call ap_start_shutdown() or ap_start_restart() as appropiate. Note that
168
* these functions can also be called by the child processes, since global
169
* variables are no longer used to pass on the required action to the parent.
171
* These should only be called from the parent process itself, since the
172
* parent process will use the shutdown_pending and restart_pending variables
173
* to determine whether to shutdown or restart. The child process should
174
* call signal_parent() directly to tell the parent to die -- this will
175
* cause neither of those variable to be set, which the parent will
176
* assume means something serious is wrong (which it will be, for the
177
* child to force an exit) and so do an exit anyway.
180
static void ap_start_shutdown(void)
182
/* If the user tries to shut us down twice in quick succession then we
183
* may well get triggered while we are working through previous attempt
184
* to shutdown. We won't worry about even reporting it as it seems a little
187
if (shutdown_pending == 1)
190
shutdown_pending = 1;
193
/* do a graceful restart if graceful == 1 */
194
static void ap_start_restart(int graceful)
196
if (restart_pending == 1) {
197
/* Probably not an error - don't bother reporting it */
201
is_graceful = graceful;
204
/* sig_coredump attempts to handle all the potential signals we
205
* may get that should result in a core dump. This is called from
206
* the signal handler routine, so when we enter we are essentially blocked
207
* on the signal. Once we exit we will allow the signal to be processed by
208
* system, which may or may not produce a .core file. All this function does
209
* is try and respect the users wishes about where that file should be
210
* located (chdir) and then signal the parent with the signal.
212
* If we called abort() the parent would only see SIGABRT which doesn't provide
213
* as much information.
215
static void sig_coredump(int sig)
217
chdir(ap_coredump_dir);
218
signal(sig, SIG_DFL);
219
kill(server_pid, sig);
222
static void sig_term(int sig)
227
static void restart(int sig)
229
ap_start_restart(sig == AP_SIG_GRACEFUL);
232
/* Handle queries about our inner workings... */
233
AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
236
case AP_MPMQ_MAX_DAEMON_USED:
237
*result = ap_max_child_assigned;
239
case AP_MPMQ_IS_THREADED:
240
*result = AP_MPMQ_DYNAMIC;
242
case AP_MPMQ_IS_FORKED:
243
*result = AP_MPMQ_NOT_SUPPORTED;
245
case AP_MPMQ_HARD_LIMIT_DAEMONS:
246
*result = HARD_SERVER_LIMIT;
248
case AP_MPMQ_HARD_LIMIT_THREADS:
249
*result = HARD_THREAD_LIMIT;
251
case AP_MPMQ_MAX_THREADS:
252
*result = HARD_THREAD_LIMIT;
254
case AP_MPMQ_MIN_SPARE_DAEMONS:
257
case AP_MPMQ_MIN_SPARE_THREADS:
258
*result = max_spare_threads;
260
case AP_MPMQ_MAX_SPARE_DAEMONS:
263
case AP_MPMQ_MAX_SPARE_THREADS:
264
*result = min_spare_threads;
266
case AP_MPMQ_MAX_REQUESTS_DAEMON:
267
*result = ap_max_requests_per_thread;
269
case AP_MPMQ_MAX_DAEMONS:
270
*result = HARD_SERVER_LIMIT;
272
case AP_MPMQ_MPM_STATE:
279
/* This accepts a connection and allows us to handle the error codes better than
280
* the previous code, while also making it more obvious.
282
static apr_status_t beos_accept(void **accepted, ap_listen_rec *lr, apr_pool_t *ptrans)
289
status = apr_socket_accept(&csd, lr->sd, ptrans);
290
if (status == APR_SUCCESS) {
292
apr_os_sock_get(&sockdes, csd);
296
if (APR_STATUS_IS_EINTR(status)) {
299
/* This switch statement provides us with better error details. */
317
* When the network layer has been shut down, there
318
* is not much use in simply exiting: the parent
319
* would simply re-create us (and we'd fail again).
320
* Use the CHILDFATAL code to tear the server down.
321
* @@@ Martin's idea for possible improvement:
322
* A different approach would be to define
323
* a new APEXIT_NETDOWN exit code, the reception
324
* of which would make the parent shutdown all
325
* children, then idle-loop until it detected that
326
* the network is up again, and restart the children.
327
* Ben Hyde noted that temporary ENETDOWN situations
328
* occur in mobile IP.
330
ap_log_error(APLOG_MARK, APLOG_EMERG, status, ap_server_conf,
331
"apr_socket_accept: giving up.");
336
ap_log_error(APLOG_MARK, APLOG_ERR, status, ap_server_conf,
337
"apr_socket_accept: (client socket)");
343
static void tell_workers_to_exit(void)
347
for (i = 0 ; i < ap_max_child_assigned; i++){
349
if (apr_socket_sendto(udp_sock, udp_sa, 0, "die!", &len) != APR_SUCCESS)
354
static void set_signals(void)
358
sigemptyset(&sa.sa_mask);
361
/* The first batch get handled by sig_coredump */
363
sa.sa_handler = sig_coredump;
365
if (sigaction(SIGSEGV, &sa, NULL) < 0)
366
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGSEGV)");
367
if (sigaction(SIGBUS, &sa, NULL) < 0)
368
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGBUS)");
369
if (sigaction(SIGABRT, &sa, NULL) < 0)
370
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABRT)");
371
if (sigaction(SIGILL, &sa, NULL) < 0)
372
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGILL)");
376
/* These next two are handled by sig_term */
377
sa.sa_handler = sig_term;
378
if (sigaction(SIGTERM, &sa, NULL) < 0)
379
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
380
if (sigaction(SIGINT, &sa, NULL) < 0)
381
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");
383
/* We ignore SIGPIPE */
384
sa.sa_handler = SIG_IGN;
385
if (sigaction(SIGPIPE, &sa, NULL) < 0)
386
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGPIPE)");
388
/* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy
390
sigaddset(&sa.sa_mask, SIGHUP);
391
sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL);
392
sa.sa_handler = restart;
393
if (sigaction(SIGHUP, &sa, NULL) < 0)
394
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");
395
if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0)
396
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(" AP_SIG_GRACEFUL_STRING ")");
399
/*****************************************************************
400
* Here follows a long bunch of generic server bookkeeping stuff...
403
int ap_graceful_stop_signalled(void)
408
/* This is the thread that actually does all the work. */
409
static int32 worker_thread(void *dummy)
411
int worker_slot = (int)dummy;
412
apr_allocator_t *allocator;
413
apr_bucket_alloc_t *bucket_alloc;
414
apr_status_t rv = APR_EINIT;
415
int last_poll_idx = 0;
417
int requests_this_child = 0;
418
apr_pollset_t *pollset = NULL;
419
ap_listen_rec *lr = NULL;
420
ap_sb_handle_t *sbh = NULL;
422
/* each worker thread is in control of its own destiny...*/
423
int this_worker_should_exit = 0;
424
/* We have 2 pools that we create/use throughout the lifetime of this
425
* worker. The first and longest lived is the pworker pool. From
426
* this we create the ptrans pool, the lifetime of which is the same
427
* as each connection and is reset prior to each attempt to
428
* process a connection.
430
apr_pool_t *ptrans = NULL;
431
apr_pool_t *pworker = NULL;
433
mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this
437
on_exit_thread(check_restart, (void*)worker_slot);
439
/* block the signals for this thread only if we're not running as a
443
sigfillset(&sig_mask);
444
sigprocmask(SIG_BLOCK, &sig_mask, NULL);
447
/* Each worker thread is fully in control of it's destinay and so
448
* to allow each thread to handle the lifetime of it's own resources
449
* we create and use a subcontext for every thread.
450
* The subcontext is a child of the pconf pool.
452
apr_allocator_create(&allocator);
453
apr_allocator_max_free_set(allocator, ap_max_mem_free);
454
apr_pool_create_ex(&pworker, pconf, NULL, allocator);
455
apr_allocator_owner_set(allocator, pworker);
457
apr_pool_create(&ptrans, pworker);
458
apr_pool_tag(ptrans, "transaction");
460
ap_create_sb_handle(&sbh, pworker, 0, worker_slot);
461
(void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
463
/* We add an extra socket here as we add the udp_sock we use for signalling
464
* death. This gets added after the others.
466
apr_pollset_create(&pollset, num_listening_sockets + 1, pworker, 0);
468
for (lr = ap_listeners, i = num_listening_sockets; i--; lr = lr->next) {
469
apr_pollfd_t pfd = {0};
471
pfd.desc_type = APR_POLL_SOCKET;
473
pfd.reqevents = APR_POLLIN;
474
pfd.client_data = lr;
476
apr_pollset_add(pollset, &pfd);
479
apr_pollfd_t pfd = {0};
481
pfd.desc_type = APR_POLL_SOCKET;
482
pfd.desc.s = udp_sock;
483
pfd.reqevents = APR_POLLIN;
485
apr_pollset_add(pollset, &pfd);
488
bucket_alloc = apr_bucket_alloc_create(pworker);
490
mpm_state = AP_MPMQ_RUNNING;
492
while (!this_worker_should_exit) {
493
conn_rec *current_conn;
496
/* (Re)initialize this child to a pre-connection state. */
497
apr_pool_clear(ptrans);
499
if ((ap_max_requests_per_thread > 0
500
&& requests_this_child++ >= ap_max_requests_per_thread))
501
clean_child_exit(0, worker_slot);
503
(void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
505
apr_thread_mutex_lock(accept_mutex);
507
/* We always (presently) have at least 2 sockets we listen on, so
508
* we don't have the ability for a fast path for a single socket
509
* as some MPM's allow :(
512
apr_int32_t numdesc = 0;
513
const apr_pollfd_t *pdesc = NULL;
515
rv = apr_pollset_poll(pollset, -1, &numdesc, &pdesc);
516
if (rv != APR_SUCCESS) {
517
if (APR_STATUS_IS_EINTR(rv)) {
518
if (one_process && shutdown_pending)
522
ap_log_error(APLOG_MARK, APLOG_ERR, rv,
523
ap_server_conf, "apr_pollset_poll: (listen)");
524
clean_child_exit(1, worker_slot);
526
/* We can always use pdesc[0], but sockets at position N
527
* could end up completely starved of attention in a very
528
* busy server. Therefore, we round-robin across the
529
* returned set of descriptors. While it is possible that
530
* the returned set of descriptors might flip around and
531
* continue to starve some sockets, we happen to know the
532
* internal pollset implementation retains ordering
533
* stability of the sockets. Thus, the round-robin should
534
* ensure that a socket will eventually be serviced.
536
if (last_poll_idx >= numdesc)
539
/* Grab a listener record from the client_data of the poll
540
* descriptor, and advance our saved index to round-robin
543
* ### hmm... this descriptor might have POLLERR rather
547
lr = pdesc[last_poll_idx++].client_data;
549
/* The only socket we add without client_data is the first, the UDP socket
550
* we listen on for restart signals. If we've therefore gotten a hit on that
551
* listener lr will be NULL here and we know we've been told to die.
552
* Before we jump to the end of the while loop with this_worker_should_exit
553
* set to 1 (causing us to exit normally we hope) we release the accept_mutex
554
* as we want every thread to go through this same routine :)
555
* Bit of a hack, but compared to what I had before...
558
this_worker_should_exit = 1;
559
apr_thread_mutex_unlock(accept_mutex);
560
goto got_a_black_spot;
565
/* Run beos_accept to accept the connection and set things up to
566
* allow us to process it. We always release the accept_lock here,
567
* even if we failt o accept as otherwise we'll starve other workers
568
* which would be bad.
570
rv = beos_accept(&csd, lr, ptrans);
571
apr_thread_mutex_unlock(accept_mutex);
573
if (rv == APR_EGENERAL) {
574
/* resource shortage or should-not-occur occured */
575
clean_child_exit(1, worker_slot);
576
} else if (rv != APR_SUCCESS)
579
current_conn = ap_run_create_connection(ptrans, ap_server_conf, csd, worker_slot, sbh, bucket_alloc);
581
ap_process_connection(current_conn, csd);
582
ap_lingering_close(current_conn);
585
if (ap_my_generation !=
586
ap_scoreboard_image->global->running_generation) { /* restart? */
587
/* yeah, this could be non-graceful restart, in which case the
588
* parent will kill us soon enough, but why bother checking?
590
this_worker_should_exit = 1;
595
apr_pool_destroy(ptrans);
596
apr_pool_destroy(pworker);
598
clean_child_exit(0, worker_slot);
601
static int make_worker(int slot)
605
if (slot + 1 > ap_max_child_assigned)
606
ap_max_child_assigned = slot + 1;
608
(void) ap_update_child_status_from_indexes(0, slot, SERVER_STARTING, (request_rec*)NULL);
612
ap_scoreboard_image->parent[0].pid = getpid();
613
ap_scoreboard_image->servers[0][slot].tid = find_thread(NULL);
617
tid = spawn_thread(worker_thread, "apache_worker", B_NORMAL_PRIORITY,
619
if (tid < B_NO_ERROR) {
620
ap_log_error(APLOG_MARK, APLOG_ERR, errno, NULL,
621
"spawn_thread: Unable to start a new thread");
622
/* In case system resources are maxed out, we don't want
623
* Apache running away with the CPU trying to fork over and
624
* over and over again.
626
(void) ap_update_child_status_from_indexes(0, slot, SERVER_DEAD,
634
ap_scoreboard_image->servers[0][slot].tid = tid;
638
/* When a worker thread exits, this function is called. If we are not in
639
* a shutdown situation then we restart the worker in the slot that was
642
static void check_restart(void *data)
644
if (!restart_pending && !shutdown_pending) {
645
int slot = (int)data;
647
ap_log_error(APLOG_MARK, APLOG_INFO, 0, NULL,
648
"spawning a new worker thread in slot %d", slot);
652
/* Start number_to_start children. This is used to start both the
653
* initial 'pool' of workers but also to replace existing workers who
654
* have reached the end of their time. It walks through the scoreboard to find
655
* an empty slot and starts the worker thread in that slot.
657
static void startup_threads(int number_to_start)
661
for (i = 0; number_to_start && i < ap_thread_limit; ++i) {
662
if (ap_scoreboard_image->servers[0][i].tid)
665
if (make_worker(i) < 0)
674
* spawn_rate is the number of children that will be spawned on the
675
* next maintenance cycle if there aren't enough idle servers. It is
676
* doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
677
* without the need to spawn.
679
static int spawn_rate = 1;
680
#ifndef MAX_SPAWN_RATE
681
#define MAX_SPAWN_RATE (32)
683
static int hold_off_on_exponential_spawning;
685
static void perform_idle_server_maintenance(void)
689
int free_slots[MAX_SPAWN_RATE];
690
int last_non_dead = -1;
692
/* initialize the free_list */
695
for (i = 0; i < ap_thread_limit; ++i) {
696
if (ap_scoreboard_image->servers[0][i].tid == 0) {
697
if (free_length < spawn_rate) {
698
free_slots[free_length] = i;
706
if (i >= ap_max_child_assigned && free_length >= spawn_rate) {
710
ap_max_child_assigned = last_non_dead + 1;
712
if (free_length > 0) {
713
for (i = 0; i < free_length; ++i) {
714
make_worker(free_slots[i]);
716
/* the next time around we want to spawn twice as many if this
717
* wasn't good enough, but not if we've just done a graceful
719
if (hold_off_on_exponential_spawning) {
720
--hold_off_on_exponential_spawning;
721
} else if (spawn_rate < MAX_SPAWN_RATE) {
729
static void server_main_loop(int remaining_threads_to_start)
732
apr_exit_why_e exitwhy;
737
while (!restart_pending && !shutdown_pending) {
739
ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
742
if (ap_process_child_status(&pid, exitwhy, status) == APEXIT_CHILDFATAL) {
743
shutdown_pending = 1;
747
/* non-fatal death... note that it's gone in the scoreboard. */
749
for (i = 0; i < ap_max_child_assigned; ++i) {
750
if (ap_scoreboard_image->servers[0][i].tid == pid.pid) {
755
if (child_slot >= 0) {
756
ap_scoreboard_image->servers[0][child_slot].tid = 0;
757
(void) ap_update_child_status_from_indexes(0, child_slot,
761
if (remaining_threads_to_start
762
&& child_slot < ap_thread_limit) {
763
/* we're still doing a 1-for-1 replacement of dead
764
* children with new children
766
make_worker(child_slot);
767
--remaining_threads_to_start;
770
#if APR_HAS_OTHER_CHILD
772
else if (apr_proc_other_child_refresh(&pid, status) == 0) {
776
else if (is_graceful) {
777
/* Great, we've probably just lost a slot in the
778
* scoreboard. Somehow we don't know about this
781
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, ap_server_conf,
782
"long lost child came home! (pid %ld)", pid.pid);
785
/* Don't perform idle maintenance when a child dies,
786
* only do it when there's a timeout. Remember only a
787
* finite number of children can die, and it's pretty
788
* pathological for a lot to die suddenly.
792
else if (remaining_threads_to_start) {
793
/* we hit a 1 second timeout in which none of the previous
794
* generation of children needed to be reaped... so assume
795
* they're all done, and pick up the slack if any is left.
797
startup_threads(remaining_threads_to_start);
798
remaining_threads_to_start = 0;
799
/* In any event we really shouldn't do the code below because
800
* few of the servers we just started are in the IDLE state
801
* yet, so we'd mistakenly create an extra server.
805
perform_idle_server_maintenance();
809
/* This is called to not only setup and run for the initial time, but also
810
* when we've asked for a restart. This means it must be able to handle both
811
* situations. It also means that when we exit here we should have tidied
812
* up after ourselves fully.
814
int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
816
int remaining_threads_to_start, i,j;
822
/* Increase the available pool of fd's. This code from
823
* Joe Kloss <joek@be.com>
825
if( FD_SETSIZE > 128 && (i = _kset_fd_limit_( 128 )) < 0 ){
826
ap_log_error(APLOG_MARK, APLOG_ERR, i, s,
827
"could not set FD_SETSIZE (_kset_fd_limit_ failed)");
830
/* BeOS R5 doesn't support pipes on select() calls, so we use a
831
* UDP socket as these are supported in both R5 and BONE. If we only cared
832
* about BONE we'd use a pipe, but there it is.
833
* As we have UDP support in APR, now use the APR functions and check all the
836
if (apr_sockaddr_info_get(&udp_sa, "127.0.0.1", APR_UNSPEC, 7772, 0, _pconf)
838
ap_log_error(APLOG_MARK, APLOG_ALERT, errno, s,
839
"couldn't create control socket information, shutting down");
842
if (apr_socket_create(&udp_sock, udp_sa->family, SOCK_DGRAM, 0,
843
_pconf) != APR_SUCCESS){
844
ap_log_error(APLOG_MARK, APLOG_ALERT, errno, s,
845
"couldn't create control socket, shutting down");
848
if (apr_socket_bind(udp_sock, udp_sa) != APR_SUCCESS){
849
ap_log_error(APLOG_MARK, APLOG_ALERT, errno, s,
850
"couldn't bind UDP socket!");
854
if ((num_listening_sockets = ap_setup_listeners(ap_server_conf)) < 1) {
855
ap_log_error(APLOG_MARK, APLOG_ALERT, 0, s,
856
"no listening sockets available, shutting down");
860
ap_log_pid(pconf, ap_pid_fname);
863
* Create our locks...
867
* used to lock around select so we only have one thread
868
* in select at a time
870
rv = apr_thread_mutex_create(&accept_mutex, 0, pconf);
871
if (rv != APR_SUCCESS) {
872
/* tsch tsch, can't have more than one thread in the accept loop
873
at a time so we need to fall on our sword... */
874
ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
875
"Couldn't create accept lock");
880
* Startup/shutdown...
884
/* setup the scoreboard shared memory */
885
if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
889
for (i = 0; i < HARD_SERVER_LIMIT; i++) {
890
ap_scoreboard_image->parent[i].pid = 0;
891
for (j = 0;j < HARD_THREAD_LIMIT; j++)
892
ap_scoreboard_image->servers[i][j].tid = 0;
896
if (HARD_SERVER_LIMIT == 1)
897
ap_scoreboard_image->parent[0].pid = getpid();
901
/* Sanity checks to avoid thrashing... */
902
if (max_spare_threads < min_spare_threads )
903
max_spare_threads = min_spare_threads;
905
/* If we're doing a graceful_restart then we're going to see a lot
906
* of threads exiting immediately when we get into the main loop
907
* below (because we just sent them AP_SIG_GRACEFUL). This happens
908
* pretty rapidly... and for each one that exits we'll start a new one
909
* until we reach at least threads_min_free. But we may be permitted to
910
* start more than that, so we'll just keep track of how many we're
911
* supposed to start up without the 1 second penalty between each fork.
913
remaining_threads_to_start = ap_threads_to_start;
914
/* sanity check on the number to start... */
915
if (remaining_threads_to_start > ap_thread_limit) {
916
remaining_threads_to_start = ap_thread_limit;
919
/* If we're doing the single process thing or we're in a graceful_restart
920
* then we don't start threads here.
921
* if we're in one_process mode we don't want to start threads
924
if (!is_graceful && !one_process) {
925
startup_threads(remaining_threads_to_start);
926
remaining_threads_to_start = 0;
928
/* give the system some time to recover before kicking into
929
* exponential mode */
930
hold_off_on_exponential_spawning = 10;
934
* record that we've entered the world !
936
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
937
"%s configured -- resuming normal operations",
938
ap_get_server_version());
940
ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
941
"Server built: %s", ap_get_server_built());
943
restart_pending = shutdown_pending = 0;
945
mpm_state = AP_MPMQ_RUNNING;
947
/* We sit in the server_main_loop() until we somehow manage to exit. When
948
* we do, we need to kill the workers we have, so we start by using the
949
* tell_workers_to_exit() function, but as it sometimes takes a short while
950
* to accomplish this we have a pause builtin to allow them the chance to
954
server_main_loop(remaining_threads_to_start);
955
tell_workers_to_exit();
958
worker_thread((void*)0);
960
mpm_state = AP_MPMQ_STOPPING;
962
/* close the UDP socket we've been using... */
963
apr_socket_close(udp_sock);
965
if ((one_process || shutdown_pending) && !child_fatal) {
966
const char *pidfile = NULL;
967
pidfile = ap_server_root_relative (pconf, ap_pid_fname);
968
if ( pidfile != NULL && unlink(pidfile) == 0)
969
ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
970
"removed PID file %s (pid=%ld)", pidfile,
979
* If we get here we're shutting down...
981
if (shutdown_pending) {
982
/* Time to gracefully shut down:
983
* Kill child processes, tell them to call child_exit, etc...
985
if (beosd_killpg(getpgrp(), SIGTERM) < 0)
986
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
989
/* use ap_reclaim_child_processes starting with SIGTERM */
990
ap_reclaim_child_processes(1);
992
if (!child_fatal) { /* already recorded */
993
/* record the shutdown in the log */
994
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
995
"caught SIGTERM, shutting down");
1001
/* we've been told to restart */
1002
signal(SIGHUP, SIG_IGN);
1005
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1006
AP_SIG_GRACEFUL_STRING " received. Doing graceful restart");
1008
/* Kill 'em all. Since the child acts the same on the parents SIGTERM
1009
* and a SIGHUP, we may as well use the same signal, because some user
1010
* pthreads are stealing signals from us left and right.
1013
ap_reclaim_child_processes(1); /* Start with SIGTERM */
1014
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1015
"SIGHUP received. Attempting to restart");
1018
/* just before we go, tidy up the lock we created to prevent a
1019
* potential leak of semaphores...
1021
apr_thread_mutex_destroy(accept_mutex);
1026
static int beos_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
1028
static int restart_num = 0;
1029
int no_detach, debug, foreground;
1032
mpm_state = AP_MPMQ_STARTING;
1034
debug = ap_exists_config_define("DEBUG");
1037
foreground = one_process = 1;
1042
one_process = ap_exists_config_define("ONE_PROCESS");
1043
no_detach = ap_exists_config_define("NO_DETACH");
1044
foreground = ap_exists_config_define("FOREGROUND");
1047
/* sigh, want this only the second time around */
1048
if (restart_num++ == 1) {
1051
if (!one_process && !foreground) {
1052
rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
1053
: APR_PROC_DETACH_DAEMONIZE);
1054
if (rv != APR_SUCCESS) {
1055
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
1056
"apr_proc_detach failed");
1057
return HTTP_INTERNAL_SERVER_ERROR;
1061
server_pid = getpid();
1065
ap_listen_pre_config();
1066
ap_threads_to_start = DEFAULT_START_THREADS;
1067
min_spare_threads = DEFAULT_MIN_FREE_THREADS;
1068
max_spare_threads = DEFAULT_MAX_FREE_THREADS;
1069
ap_thread_limit = HARD_THREAD_LIMIT;
1070
ap_pid_fname = DEFAULT_PIDLOG;
1071
ap_max_requests_per_thread = DEFAULT_MAX_REQUESTS_PER_THREAD;
1072
#ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
1073
ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
1076
apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
1081
static void beos_hooks(apr_pool_t *p)
1085
ap_hook_pre_config(beos_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1088
static const char *set_threads_to_start(cmd_parms *cmd, void *dummy, const char *arg)
1090
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1095
ap_threads_to_start = atoi(arg);
1096
if (ap_threads_to_start < 0) {
1097
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1098
"StartThreads set to a value less than 0, reset to 1");
1099
ap_threads_to_start = 1;
1104
static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy, const char *arg)
1106
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1111
min_spare_threads = atoi(arg);
1112
if (min_spare_threads <= 0) {
1113
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1114
"WARNING: detected MinSpareThreads set to non-positive.");
1115
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1116
"Resetting to 1 to avoid almost certain Apache failure.");
1117
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1118
"Please read the documentation.");
1119
min_spare_threads = 1;
1125
static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy, const char *arg)
1127
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1132
max_spare_threads = atoi(arg);
1136
static const char *set_threads_limit (cmd_parms *cmd, void *dummy, const char *arg)
1138
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1143
ap_thread_limit = atoi(arg);
1144
if (ap_thread_limit > HARD_THREAD_LIMIT) {
1145
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1146
"WARNING: MaxClients of %d exceeds compile time limit "
1147
"of %d servers,", ap_thread_limit, HARD_THREAD_LIMIT);
1148
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1149
" lowering MaxClients to %d. To increase, please "
1150
"see the", HARD_THREAD_LIMIT);
1151
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1152
" HARD_THREAD_LIMIT define in server/mpm/beos/mpm_default.h.");
1153
ap_thread_limit = HARD_THREAD_LIMIT;
1155
else if (ap_thread_limit < 1) {
1156
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1157
"WARNING: Require MaxClients > 0, setting to %d", HARD_THREAD_LIMIT);
1158
ap_thread_limit = HARD_THREAD_LIMIT;
1163
static const char *set_max_requests_per_thread (cmd_parms *cmd, void *dummy, const char *arg)
1165
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1170
ap_max_requests_per_thread = atoi(arg);
1171
if (ap_max_requests_per_thread < 0) {
1172
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
1173
"WARNING: MaxRequestsPerThread was set below 0"
1174
"reset to 0, but this may not be what you want.");
1175
ap_max_requests_per_thread = 0;
1181
static const command_rec beos_cmds[] = {
1182
BEOS_DAEMON_COMMANDS,
1184
AP_INIT_TAKE1( "StartThreads", set_threads_to_start, NULL, RSRC_CONF,
1185
"Number of threads to launch at server startup"),
1186
AP_INIT_TAKE1( "MinSpareThreads", set_min_spare_threads, NULL, RSRC_CONF,
1187
"Minimum number of idle children, to handle request spikes"),
1188
AP_INIT_TAKE1( "MaxSpareThreads", set_max_spare_threads, NULL, RSRC_CONF,
1189
"Maximum number of idle children" ),
1190
AP_INIT_TAKE1( "MaxClients", set_threads_limit, NULL, RSRC_CONF,
1191
"Maximum number of children alive at the same time (max threads)" ),
1192
AP_INIT_TAKE1( "MaxRequestsPerThread", set_max_requests_per_thread, NULL, RSRC_CONF,
1193
"Maximum number of requests served by a thread" ),
1197
module AP_MODULE_DECLARE_DATA mpm_beos_module = {
1199
NULL, /* hook to run before apache parses args */
1200
NULL, /* create per-directory config structure */
1201
NULL, /* merge per-directory config structures */
1202
NULL, /* create per-server config structure */
1203
NULL, /* merge per-server config structures */
1204
beos_cmds, /* command apr_table_t */
1205
beos_hooks /* register_hooks */