~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to server/mpm/beos/beos.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
16
 
 
17
/* The BeOS MPM!
 
18
 *
 
19
 * This is a single process, with multiple worker threads.
 
20
 *
 
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.
 
25
 *
 
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!
 
28
 *
 
29
 * TODO Items
 
30
 *
 
31
 * - on exit most worker threads segfault trying to access a kernel page.
 
32
 */
 
33
 
 
34
#define CORE_PRIVATE
 
35
 
 
36
#include <kernel/OS.h>
 
37
#include <unistd.h>
 
38
#include <sys/socket.h>
 
39
#include <signal.h>
 
40
 
 
41
#include "apr_strings.h"
 
42
#include "apr_portable.h"
 
43
#include "httpd.h"
 
44
#include "http_main.h"
 
45
#include "http_log.h"
 
46
#include "http_config.h"        /* for read_config */
 
47
#include "http_core.h"          /* for get_remote_host */
 
48
#include "http_connection.h"
 
49
#include "ap_mpm.h"
 
50
#include "beosd.h"
 
51
#include "ap_listen.h"
 
52
#include "scoreboard.h"
 
53
#include "mpm_common.h"
 
54
#include "mpm.h"
 
55
#include "mpm_default.h"
 
56
#include "apr_thread_mutex.h"
 
57
#include "apr_poll.h"
 
58
 
 
59
extern int _kset_fd_limit_(int num);
 
60
 
 
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.
 
64
 *
 
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
 
71
 *    the overhead.
 
72
 */
 
73
 
 
74
/* we only ever have 1 main process running... */
 
75
#define HARD_SERVER_LIMIT 1
 
76
 
 
77
/* Limit on the threads per process.  Clients will be locked out if more than
 
78
 * this  * HARD_SERVER_LIMIT are needed.
 
79
 *
 
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
 
82
 * the overhead.
 
83
 */
 
84
#ifdef NO_THREADS
 
85
#define HARD_THREAD_LIMIT 1
 
86
#endif
 
87
#ifndef HARD_THREAD_LIMIT
 
88
#define HARD_THREAD_LIMIT 50
 
89
#endif
 
90
 
 
91
/*
 
92
 * Actual definitions of config globals
 
93
 */
 
94
 
 
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;
 
103
 
 
104
static apr_pool_t *pconf;    /* Pool for config stuff */
 
105
 
 
106
static int server_pid;
 
107
 
 
108
 
 
109
/*
 
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.
 
113
 */
 
114
int ap_max_child_assigned = -1;
 
115
int ap_max_threads_limit = -1;
 
116
 
 
117
static apr_socket_t *udp_sock;
 
118
static apr_sockaddr_t *udp_sa;
 
119
 
 
120
server_rec *ap_server_conf;
 
121
 
 
122
/* one_process */
 
123
static int one_process = 0;
 
124
 
 
125
#ifdef DEBUG_SIGSTOP
 
126
int raise_sigstop_flags;
 
127
#endif
 
128
 
 
129
static void check_restart(void *data);
 
130
 
 
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
 
136
 * it's thread_id.
 
137
 *
 
138
 * TODO - use the status we set to determine if we need to restart the
 
139
 *        thread.
 
140
 */
 
141
static void clean_child_exit(int code, int slot)
 
142
{
 
143
    (void) ap_update_child_status_from_indexes(0, slot, SERVER_DEAD,
 
144
                                               (request_rec*)NULL);
 
145
    ap_scoreboard_image->servers[0][slot].tid = 0;
 
146
    exit_thread(code);
 
147
}
 
148
 
 
149
 
 
150
/*****************************************************************
 
151
 * Connection structures and accounting...
 
152
 */
 
153
 
 
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;
 
160
 
 
161
/*
 
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.
 
170
 *
 
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.
 
178
 */
 
179
 
 
180
static void ap_start_shutdown(void)
 
181
{
 
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
 
185
     * pointless.
 
186
     */
 
187
    if (shutdown_pending == 1)
 
188
        return;
 
189
 
 
190
    shutdown_pending = 1;
 
191
}
 
192
 
 
193
/* do a graceful restart if graceful == 1 */
 
194
static void ap_start_restart(int graceful)
 
195
{
 
196
    if (restart_pending == 1) {
 
197
        /* Probably not an error - don't bother reporting it */
 
198
        return;
 
199
    }
 
200
    restart_pending = 1;
 
201
    is_graceful = graceful;
 
202
}
 
203
 
 
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.
 
211
 *
 
212
 * If we called abort() the parent would only see SIGABRT which doesn't provide
 
213
 * as much information.
 
214
 */
 
215
static void sig_coredump(int sig)
 
216
{
 
217
    chdir(ap_coredump_dir);
 
218
    signal(sig, SIG_DFL);
 
219
    kill(server_pid, sig);
 
220
}
 
221
 
 
222
static void sig_term(int sig)
 
223
{
 
224
    ap_start_shutdown();
 
225
}
 
226
 
 
227
static void restart(int sig)
 
228
{
 
229
    ap_start_restart(sig == AP_SIG_GRACEFUL);
 
230
}
 
231
 
 
232
/* Handle queries about our inner workings... */
 
233
AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
 
234
{
 
235
    switch(query_code){
 
236
        case AP_MPMQ_MAX_DAEMON_USED:
 
237
            *result = ap_max_child_assigned;
 
238
            return APR_SUCCESS;
 
239
        case AP_MPMQ_IS_THREADED:
 
240
            *result = AP_MPMQ_DYNAMIC;
 
241
            return APR_SUCCESS;
 
242
        case AP_MPMQ_IS_FORKED:
 
243
            *result = AP_MPMQ_NOT_SUPPORTED;
 
244
            return APR_SUCCESS;
 
245
        case AP_MPMQ_HARD_LIMIT_DAEMONS:
 
246
            *result = HARD_SERVER_LIMIT;
 
247
            return APR_SUCCESS;
 
248
        case AP_MPMQ_HARD_LIMIT_THREADS:
 
249
            *result = HARD_THREAD_LIMIT;
 
250
            return APR_SUCCESS;
 
251
        case AP_MPMQ_MAX_THREADS:
 
252
            *result = HARD_THREAD_LIMIT;
 
253
            return APR_SUCCESS;
 
254
        case AP_MPMQ_MIN_SPARE_DAEMONS:
 
255
            *result = 0;
 
256
            return APR_SUCCESS;
 
257
        case AP_MPMQ_MIN_SPARE_THREADS:
 
258
            *result = max_spare_threads;
 
259
            return APR_SUCCESS;
 
260
        case AP_MPMQ_MAX_SPARE_DAEMONS:
 
261
            *result = 0;
 
262
            return APR_SUCCESS;
 
263
        case AP_MPMQ_MAX_SPARE_THREADS:
 
264
            *result = min_spare_threads;
 
265
            return APR_SUCCESS;
 
266
        case AP_MPMQ_MAX_REQUESTS_DAEMON:
 
267
            *result = ap_max_requests_per_thread;
 
268
            return APR_SUCCESS;
 
269
        case AP_MPMQ_MAX_DAEMONS:
 
270
            *result = HARD_SERVER_LIMIT;
 
271
            return APR_SUCCESS;
 
272
        case AP_MPMQ_MPM_STATE:
 
273
            *result = mpm_state;
 
274
            return APR_SUCCESS;
 
275
    }
 
276
    return APR_ENOTIMPL;
 
277
}
 
278
 
 
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.
 
281
 */
 
282
static apr_status_t beos_accept(void **accepted, ap_listen_rec *lr, apr_pool_t *ptrans)
 
283
{
 
284
    apr_socket_t *csd;
 
285
    apr_status_t status;
 
286
    int sockdes;
 
287
 
 
288
    *accepted = NULL;
 
289
    status = apr_socket_accept(&csd, lr->sd, ptrans);
 
290
    if (status == APR_SUCCESS) {
 
291
        *accepted = csd;
 
292
        apr_os_sock_get(&sockdes, csd);
 
293
        return status;
 
294
    }
 
295
 
 
296
    if (APR_STATUS_IS_EINTR(status)) {
 
297
        return status;
 
298
    }
 
299
        /* This switch statement provides us with better error details. */
 
300
    switch (status) {
 
301
#ifdef ECONNABORTED
 
302
        case ECONNABORTED:
 
303
#endif
 
304
#ifdef ETIMEDOUT
 
305
        case ETIMEDOUT:
 
306
#endif
 
307
#ifdef EHOSTUNREACH
 
308
        case EHOSTUNREACH:
 
309
#endif
 
310
#ifdef ENETUNREACH
 
311
        case ENETUNREACH:
 
312
#endif
 
313
            break;
 
314
#ifdef ENETDOWN
 
315
        case ENETDOWN:
 
316
            /*
 
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.
 
329
             */
 
330
            ap_log_error(APLOG_MARK, APLOG_EMERG, status, ap_server_conf,
 
331
                         "apr_socket_accept: giving up.");
 
332
            return APR_EGENERAL;
 
333
#endif /*ENETDOWN*/
 
334
 
 
335
        default:
 
336
            ap_log_error(APLOG_MARK, APLOG_ERR, status, ap_server_conf,
 
337
                         "apr_socket_accept: (client socket)");
 
338
            return APR_EGENERAL;
 
339
    }
 
340
    return status;
 
341
}
 
342
 
 
343
static void tell_workers_to_exit(void)
 
344
{
 
345
    apr_size_t len;
 
346
    int i = 0;
 
347
    for (i = 0 ; i < ap_max_child_assigned; i++){
 
348
        len = 4;
 
349
        if (apr_socket_sendto(udp_sock, udp_sa, 0, "die!", &len) != APR_SUCCESS)
 
350
            break;
 
351
    }
 
352
}
 
353
 
 
354
static void set_signals(void)
 
355
{
 
356
    struct sigaction sa;
 
357
 
 
358
    sigemptyset(&sa.sa_mask);
 
359
    sa.sa_flags = 0;
 
360
 
 
361
    /* The first batch get handled by sig_coredump */
 
362
    if (!one_process) {
 
363
        sa.sa_handler = sig_coredump;
 
364
 
 
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)");
 
373
        sa.sa_flags = 0;
 
374
    }
 
375
 
 
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)");
 
382
 
 
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)");
 
387
 
 
388
    /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy
 
389
     * processing one */
 
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 ")");
 
397
}
 
398
 
 
399
/*****************************************************************
 
400
 * Here follows a long bunch of generic server bookkeeping stuff...
 
401
 */
 
402
 
 
403
int ap_graceful_stop_signalled(void)
 
404
{
 
405
    return is_graceful;
 
406
}
 
407
 
 
408
/* This is the thread that actually does all the work. */
 
409
static int32 worker_thread(void *dummy)
 
410
{
 
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;
 
416
    sigset_t sig_mask;
 
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;
 
421
    int i;
 
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.
 
429
     */
 
430
    apr_pool_t *ptrans = NULL;
 
431
    apr_pool_t *pworker = NULL;
 
432
 
 
433
    mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this
 
434
                                  * child initializes
 
435
                                  */
 
436
 
 
437
    on_exit_thread(check_restart, (void*)worker_slot);
 
438
 
 
439
    /* block the signals for this thread only if we're not running as a
 
440
     * single process.
 
441
     */
 
442
    if (!one_process) {
 
443
        sigfillset(&sig_mask);
 
444
        sigprocmask(SIG_BLOCK, &sig_mask, NULL);
 
445
    }
 
446
 
 
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.
 
451
     */
 
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);
 
456
 
 
457
    apr_pool_create(&ptrans, pworker);
 
458
    apr_pool_tag(ptrans, "transaction");
 
459
 
 
460
    ap_create_sb_handle(&sbh, pworker, 0, worker_slot);
 
461
    (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
 
462
 
 
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.
 
465
     */
 
466
    apr_pollset_create(&pollset, num_listening_sockets + 1, pworker, 0);
 
467
 
 
468
    for (lr = ap_listeners, i = num_listening_sockets; i--; lr = lr->next) {
 
469
        apr_pollfd_t pfd = {0};
 
470
 
 
471
        pfd.desc_type = APR_POLL_SOCKET;
 
472
        pfd.desc.s = lr->sd;
 
473
        pfd.reqevents = APR_POLLIN;
 
474
        pfd.client_data = lr;
 
475
 
 
476
        apr_pollset_add(pollset, &pfd);
 
477
    }
 
478
    {
 
479
        apr_pollfd_t pfd = {0};
 
480
 
 
481
        pfd.desc_type = APR_POLL_SOCKET;
 
482
        pfd.desc.s = udp_sock;
 
483
        pfd.reqevents = APR_POLLIN;
 
484
 
 
485
        apr_pollset_add(pollset, &pfd);
 
486
    }
 
487
 
 
488
    bucket_alloc = apr_bucket_alloc_create(pworker);
 
489
 
 
490
    mpm_state = AP_MPMQ_RUNNING;
 
491
 
 
492
        while (!this_worker_should_exit) {
 
493
        conn_rec *current_conn;
 
494
        void *csd;
 
495
 
 
496
        /* (Re)initialize this child to a pre-connection state. */
 
497
        apr_pool_clear(ptrans);
 
498
 
 
499
        if ((ap_max_requests_per_thread > 0
 
500
             && requests_this_child++ >= ap_max_requests_per_thread))
 
501
            clean_child_exit(0, worker_slot);
 
502
 
 
503
        (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
 
504
 
 
505
        apr_thread_mutex_lock(accept_mutex);
 
506
 
 
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 :(
 
510
         */
 
511
        for (;;) {
 
512
            apr_int32_t numdesc = 0;
 
513
            const apr_pollfd_t *pdesc = NULL;
 
514
 
 
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)
 
519
                        return;
 
520
                    continue;
 
521
                }
 
522
                ap_log_error(APLOG_MARK, APLOG_ERR, rv,
 
523
                             ap_server_conf, "apr_pollset_poll: (listen)");
 
524
                clean_child_exit(1, worker_slot);
 
525
            }
 
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.
 
535
             */
 
536
            if (last_poll_idx >= numdesc)
 
537
                last_poll_idx = 0;
 
538
 
 
539
            /* Grab a listener record from the client_data of the poll
 
540
             * descriptor, and advance our saved index to round-robin
 
541
             * the next fetch.
 
542
             *
 
543
             * ### hmm... this descriptor might have POLLERR rather
 
544
             * ### than POLLIN
 
545
             */
 
546
 
 
547
            lr = pdesc[last_poll_idx++].client_data;
 
548
 
 
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...
 
556
             */
 
557
            if (lr == NULL) {
 
558
                this_worker_should_exit = 1;
 
559
                apr_thread_mutex_unlock(accept_mutex);
 
560
                goto got_a_black_spot;
 
561
            }
 
562
            goto got_fd;
 
563
        }
 
564
got_fd:
 
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.
 
569
         */
 
570
        rv = beos_accept(&csd, lr, ptrans);
 
571
        apr_thread_mutex_unlock(accept_mutex);
 
572
 
 
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)
 
577
            continue;
 
578
 
 
579
        current_conn = ap_run_create_connection(ptrans, ap_server_conf, csd, worker_slot, sbh, bucket_alloc);
 
580
        if (current_conn) {
 
581
            ap_process_connection(current_conn, csd);
 
582
            ap_lingering_close(current_conn);
 
583
        }
 
584
 
 
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?
 
589
             */
 
590
            this_worker_should_exit = 1;
 
591
        }
 
592
got_a_black_spot:
 
593
    }
 
594
 
 
595
    apr_pool_destroy(ptrans);
 
596
    apr_pool_destroy(pworker);
 
597
 
 
598
    clean_child_exit(0, worker_slot);
 
599
}
 
600
 
 
601
static int make_worker(int slot)
 
602
{
 
603
    thread_id tid;
 
604
 
 
605
    if (slot + 1 > ap_max_child_assigned)
 
606
            ap_max_child_assigned = slot + 1;
 
607
 
 
608
    (void) ap_update_child_status_from_indexes(0, slot, SERVER_STARTING, (request_rec*)NULL);
 
609
 
 
610
    if (one_process) {
 
611
        set_signals();
 
612
        ap_scoreboard_image->parent[0].pid = getpid();
 
613
        ap_scoreboard_image->servers[0][slot].tid = find_thread(NULL);
 
614
        return 0;
 
615
    }
 
616
 
 
617
    tid = spawn_thread(worker_thread, "apache_worker", B_NORMAL_PRIORITY,
 
618
                       (void *)slot);
 
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.
 
625
         */
 
626
        (void) ap_update_child_status_from_indexes(0, slot, SERVER_DEAD,
 
627
                                                   (request_rec*)NULL);
 
628
 
 
629
        sleep(10);
 
630
        return -1;
 
631
    }
 
632
    resume_thread(tid);
 
633
 
 
634
    ap_scoreboard_image->servers[0][slot].tid = tid;
 
635
    return 0;
 
636
}
 
637
 
 
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
 
640
 * just vacated.
 
641
 */
 
642
static void check_restart(void *data)
 
643
{
 
644
    if (!restart_pending && !shutdown_pending) {
 
645
        int slot = (int)data;
 
646
        make_worker(slot);
 
647
        ap_log_error(APLOG_MARK, APLOG_INFO, 0, NULL,
 
648
                     "spawning a new worker thread in slot %d", slot);
 
649
    }
 
650
}
 
651
 
 
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.
 
656
 */
 
657
static void startup_threads(int number_to_start)
 
658
{
 
659
    int i;
 
660
 
 
661
    for (i = 0; number_to_start && i < ap_thread_limit; ++i) {
 
662
        if (ap_scoreboard_image->servers[0][i].tid)
 
663
            continue;
 
664
 
 
665
        if (make_worker(i) < 0)
 
666
                break;
 
667
 
 
668
        --number_to_start;
 
669
    }
 
670
}
 
671
 
 
672
 
 
673
/*
 
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.
 
678
 */
 
679
static int spawn_rate = 1;
 
680
#ifndef MAX_SPAWN_RATE
 
681
#define MAX_SPAWN_RATE  (32)
 
682
#endif
 
683
static int hold_off_on_exponential_spawning;
 
684
 
 
685
static void perform_idle_server_maintenance(void)
 
686
{
 
687
    int i;
 
688
    int free_length;
 
689
    int free_slots[MAX_SPAWN_RATE];
 
690
    int last_non_dead  = -1;
 
691
 
 
692
    /* initialize the free_list */
 
693
    free_length = 0;
 
694
 
 
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;
 
699
                ++free_length;
 
700
            }
 
701
        }
 
702
        else {
 
703
            last_non_dead = i;
 
704
        }
 
705
 
 
706
        if (i >= ap_max_child_assigned && free_length >= spawn_rate) {
 
707
                 break;
 
708
        }
 
709
    }
 
710
    ap_max_child_assigned = last_non_dead + 1;
 
711
 
 
712
    if (free_length > 0) {
 
713
        for (i = 0; i < free_length; ++i) {
 
714
                make_worker(free_slots[i]);
 
715
        }
 
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
 
718
         */
 
719
        if (hold_off_on_exponential_spawning) {
 
720
            --hold_off_on_exponential_spawning;
 
721
        } else if (spawn_rate < MAX_SPAWN_RATE) {
 
722
            spawn_rate *= 2;
 
723
        }
 
724
    } else {
 
725
        spawn_rate = 1;
 
726
    }
 
727
}
 
728
 
 
729
static void server_main_loop(int remaining_threads_to_start)
 
730
{
 
731
    int child_slot;
 
732
    apr_exit_why_e exitwhy;
 
733
    int status;
 
734
    apr_proc_t pid;
 
735
    int i;
 
736
 
 
737
    while (!restart_pending && !shutdown_pending) {
 
738
 
 
739
        ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
 
740
 
 
741
        if (pid.pid >= 0) {
 
742
            if (ap_process_child_status(&pid, exitwhy, status) == APEXIT_CHILDFATAL) {
 
743
                shutdown_pending = 1;
 
744
                child_fatal = 1;
 
745
                return;
 
746
            }
 
747
            /* non-fatal death... note that it's gone in the scoreboard. */
 
748
            child_slot = -1;
 
749
            for (i = 0; i < ap_max_child_assigned; ++i) {
 
750
                if (ap_scoreboard_image->servers[0][i].tid == pid.pid) {
 
751
                    child_slot = i;
 
752
                    break;
 
753
                }
 
754
            }
 
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,
 
758
                                                           SERVER_DEAD,
 
759
                                                           (request_rec*)NULL);
 
760
 
 
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
 
765
                     */
 
766
                    make_worker(child_slot);
 
767
                    --remaining_threads_to_start;
 
768
                        }
 
769
/* TODO
 
770
#if APR_HAS_OTHER_CHILD
 
771
            }
 
772
            else if (apr_proc_other_child_refresh(&pid, status) == 0) {
 
773
#endif
 
774
*/
 
775
            }
 
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
 
779
                 * child.
 
780
                 */
 
781
                 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, ap_server_conf,
 
782
                                          "long lost child came home! (pid %ld)", pid.pid);
 
783
            }
 
784
 
 
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.
 
789
             */
 
790
             continue;
 
791
         }
 
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.
 
796
              */
 
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.
 
802
               */
 
803
              continue;
 
804
         }
 
805
         perform_idle_server_maintenance();
 
806
    }
 
807
}
 
808
 
 
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.
 
813
 */
 
814
int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
 
815
{
 
816
    int remaining_threads_to_start, i,j;
 
817
    apr_status_t rv;
 
818
    ap_listen_rec *lr;
 
819
    pconf = _pconf;
 
820
    ap_server_conf = s;
 
821
 
 
822
    /* Increase the available pool of fd's.  This code from
 
823
     * Joe Kloss <joek@be.com>
 
824
     */
 
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)");
 
828
    }
 
829
 
 
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
 
834
     * return values...
 
835
     */
 
836
    if (apr_sockaddr_info_get(&udp_sa, "127.0.0.1", APR_UNSPEC, 7772, 0, _pconf)
 
837
        != APR_SUCCESS){
 
838
        ap_log_error(APLOG_MARK, APLOG_ALERT, errno, s,
 
839
            "couldn't create control socket information, shutting down");
 
840
        return 1;
 
841
    }
 
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");
 
846
        return 1;
 
847
    }
 
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!");
 
851
        return 1;
 
852
    }
 
853
 
 
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");
 
857
        return 1;
 
858
    }
 
859
 
 
860
    ap_log_pid(pconf, ap_pid_fname);
 
861
 
 
862
    /*
 
863
     * Create our locks...
 
864
     */
 
865
 
 
866
    /* accept_mutex
 
867
     * used to lock around select so we only have one thread
 
868
     * in select at a time
 
869
     */
 
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");
 
876
        return 1;
 
877
    }
 
878
 
 
879
    /*
 
880
     * Startup/shutdown...
 
881
     */
 
882
 
 
883
    if (!is_graceful) {
 
884
        /* setup the scoreboard shared memory */
 
885
        if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
 
886
            return 1;
 
887
        }
 
888
 
 
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;
 
893
        }
 
894
    }
 
895
 
 
896
    if (HARD_SERVER_LIMIT == 1)
 
897
        ap_scoreboard_image->parent[0].pid = getpid();
 
898
 
 
899
    set_signals();
 
900
 
 
901
    /* Sanity checks to avoid thrashing... */
 
902
    if (max_spare_threads < min_spare_threads )
 
903
        max_spare_threads = min_spare_threads;
 
904
 
 
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.
 
912
     */
 
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;
 
917
    }
 
918
 
 
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
 
922
     * do we??
 
923
     */
 
924
    if (!is_graceful && !one_process) {
 
925
            startup_threads(remaining_threads_to_start);
 
926
            remaining_threads_to_start = 0;
 
927
    } else {
 
928
            /* give the system some time to recover before kicking into
 
929
             * exponential mode */
 
930
        hold_off_on_exponential_spawning = 10;
 
931
    }
 
932
 
 
933
    /*
 
934
     * record that we've entered the world !
 
935
     */
 
936
    ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
 
937
                "%s configured -- resuming normal operations",
 
938
                ap_get_server_version());
 
939
 
 
940
    ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
 
941
                "Server built: %s", ap_get_server_built());
 
942
 
 
943
    restart_pending = shutdown_pending = 0;
 
944
 
 
945
    mpm_state = AP_MPMQ_RUNNING;
 
946
 
 
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
 
951
     * gracefully exit.
 
952
     */
 
953
    if (!one_process) {
 
954
        server_main_loop(remaining_threads_to_start);
 
955
        tell_workers_to_exit();
 
956
        snooze(1000000);
 
957
    } else {
 
958
        worker_thread((void*)0);
 
959
    }
 
960
    mpm_state = AP_MPMQ_STOPPING;
 
961
 
 
962
    /* close the UDP socket we've been using... */
 
963
    apr_socket_close(udp_sock);
 
964
 
 
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,
 
971
                         (long)getpid());
 
972
    }
 
973
 
 
974
    if (one_process) {
 
975
        return 1;
 
976
    }
 
977
 
 
978
    /*
 
979
     * If we get here we're shutting down...
 
980
     */
 
981
    if (shutdown_pending) {
 
982
        /* Time to gracefully shut down:
 
983
         * Kill child processes, tell them to call child_exit, etc...
 
984
         */
 
985
        if (beosd_killpg(getpgrp(), SIGTERM) < 0)
 
986
            ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
 
987
             "killpg SIGTERM");
 
988
 
 
989
        /* use ap_reclaim_child_processes starting with SIGTERM */
 
990
        ap_reclaim_child_processes(1);
 
991
 
 
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");
 
996
        }
 
997
 
 
998
        return 1;
 
999
    }
 
1000
 
 
1001
    /* we've been told to restart */
 
1002
    signal(SIGHUP, SIG_IGN);
 
1003
 
 
1004
    if (is_graceful) {
 
1005
        ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
 
1006
                    AP_SIG_GRACEFUL_STRING " received.  Doing graceful restart");
 
1007
    } else {
 
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.
 
1011
         */
 
1012
 
 
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");
 
1016
    }
 
1017
 
 
1018
    /* just before we go, tidy up the lock we created to prevent a
 
1019
     * potential leak of semaphores...
 
1020
     */
 
1021
    apr_thread_mutex_destroy(accept_mutex);
 
1022
 
 
1023
    return 0;
 
1024
}
 
1025
 
 
1026
static int beos_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
 
1027
{
 
1028
    static int restart_num = 0;
 
1029
    int no_detach, debug, foreground;
 
1030
    apr_status_t rv;
 
1031
 
 
1032
    mpm_state = AP_MPMQ_STARTING;
 
1033
 
 
1034
    debug = ap_exists_config_define("DEBUG");
 
1035
 
 
1036
    if (debug) {
 
1037
        foreground = one_process = 1;
 
1038
        no_detach = 0;
 
1039
    }
 
1040
    else
 
1041
    {
 
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");
 
1045
    }
 
1046
 
 
1047
    /* sigh, want this only the second time around */
 
1048
    if (restart_num++ == 1) {
 
1049
        is_graceful = 0;
 
1050
 
 
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;
 
1058
            }
 
1059
        }
 
1060
 
 
1061
        server_pid = getpid();
 
1062
    }
 
1063
 
 
1064
    beosd_pre_config();
 
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;
 
1074
#endif
 
1075
 
 
1076
    apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
 
1077
 
 
1078
    return OK;
 
1079
}
 
1080
 
 
1081
static void beos_hooks(apr_pool_t *p)
 
1082
{
 
1083
    one_process = 0;
 
1084
 
 
1085
    ap_hook_pre_config(beos_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
 
1086
}
 
1087
 
 
1088
static const char *set_threads_to_start(cmd_parms *cmd, void *dummy, const char *arg)
 
1089
{
 
1090
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
 
1091
    if (err != NULL) {
 
1092
        return err;
 
1093
    }
 
1094
 
 
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;
 
1100
    }
 
1101
    return NULL;
 
1102
}
 
1103
 
 
1104
static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy, const char *arg)
 
1105
{
 
1106
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
 
1107
    if (err != NULL) {
 
1108
        return err;
 
1109
    }
 
1110
 
 
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;
 
1120
    }
 
1121
 
 
1122
    return NULL;
 
1123
}
 
1124
 
 
1125
static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy, const char *arg)
 
1126
{
 
1127
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
 
1128
    if (err != NULL) {
 
1129
        return err;
 
1130
    }
 
1131
 
 
1132
    max_spare_threads = atoi(arg);
 
1133
    return NULL;
 
1134
}
 
1135
 
 
1136
static const char *set_threads_limit (cmd_parms *cmd, void *dummy, const char *arg)
 
1137
{
 
1138
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
 
1139
    if (err != NULL) {
 
1140
        return err;
 
1141
    }
 
1142
 
 
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;
 
1154
    }
 
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;
 
1159
    }
 
1160
    return NULL;
 
1161
}
 
1162
 
 
1163
static const char *set_max_requests_per_thread (cmd_parms *cmd, void *dummy, const char *arg)
 
1164
{
 
1165
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
 
1166
    if (err != NULL) {
 
1167
        return err;
 
1168
    }
 
1169
 
 
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;
 
1176
    }
 
1177
 
 
1178
    return NULL;
 
1179
}
 
1180
 
 
1181
static const command_rec beos_cmds[] = {
 
1182
BEOS_DAEMON_COMMANDS,
 
1183
LISTEN_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" ),
 
1194
{ NULL }
 
1195
};
 
1196
 
 
1197
module AP_MODULE_DECLARE_DATA mpm_beos_module = {
 
1198
    MPM20_MODULE_STUFF,
 
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 */
 
1206
};
 
1207