Coverage Report

Created: 2026-12-01 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/httpd/server/mpm_common.c
Line
Count
Source
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 purpose of this file is to store the code that MOST mpm's will need
18
 * this does not mean a function only goes into this file if every MPM needs
19
 * it.  It means that if a function is needed by more than one MPM, and
20
 * future maintenance would be served by making the code common, then the
21
 * function belongs here.
22
 *
23
 * This is going in src/main because it is not platform specific, it is
24
 * specific to multi-process servers, but NOT to Unix.  Which is why it
25
 * does not belong in src/os/unix
26
 */
27
28
#include "apr.h"
29
#include "apr_thread_proc.h"
30
#include "apr_signal.h"
31
#include "apr_strings.h"
32
#define APR_WANT_STRFUNC
33
#include "apr_want.h"
34
#include "apr_getopt.h"
35
#include "apr_optional.h"
36
#include "apr_allocator.h"
37
#include "apr_atomic.h"
38
39
#include "httpd.h"
40
#include "http_config.h"
41
#include "http_core.h"
42
#include "http_log.h"
43
#include "http_main.h"
44
#include "mpm_common.h"
45
#include "mod_core.h"
46
#include "ap_mpm.h"
47
#include "ap_listen.h"
48
#include "util_mutex.h"
49
50
#include "scoreboard.h"
51
52
#ifdef HAVE_PWD_H
53
#include <pwd.h>
54
#endif
55
#ifdef HAVE_GRP_H
56
#include <grp.h>
57
#endif
58
#if APR_HAVE_UNISTD_H
59
#include <unistd.h>
60
#endif
61
62
/* we know core's module_index is 0 */
63
#undef APLOG_MODULE_INDEX
64
#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
65
66
#define DEFAULT_HOOK_LINKS \
67
    APR_HOOK_LINK(monitor) \
68
    APR_HOOK_LINK(drop_privileges) \
69
    APR_HOOK_LINK(mpm) \
70
    APR_HOOK_LINK(mpm_query) \
71
    APR_HOOK_LINK(mpm_register_timed_callback) \
72
    APR_HOOK_LINK(mpm_register_poll_callback) \
73
    APR_HOOK_LINK(mpm_register_poll_callback_timeout) \
74
    APR_HOOK_LINK(mpm_get_name) \
75
    APR_HOOK_LINK(mpm_resume_suspended) \
76
    APR_HOOK_LINK(end_generation) \
77
    APR_HOOK_LINK(child_status) \
78
    APR_HOOK_LINK(output_pending) \
79
    APR_HOOK_LINK(input_pending) \
80
    APR_HOOK_LINK(suspend_connection) \
81
    APR_HOOK_LINK(resume_connection) \
82
    APR_HOOK_LINK(child_stopping) \
83
    APR_HOOK_LINK(child_stopped)
84
85
#if AP_ENABLE_EXCEPTION_HOOK
86
APR_HOOK_STRUCT(
87
    APR_HOOK_LINK(fatal_exception)
88
    DEFAULT_HOOK_LINKS
89
)
90
AP_IMPLEMENT_HOOK_RUN_ALL(int, fatal_exception,
91
                          (ap_exception_info_t *ei), (ei), OK, DECLINED)
92
#else
93
APR_HOOK_STRUCT(
94
    DEFAULT_HOOK_LINKS
95
)
96
#endif
97
AP_IMPLEMENT_HOOK_RUN_ALL(int, monitor,
98
                          (apr_pool_t *p, server_rec *s), (p, s), OK, DECLINED)
99
AP_IMPLEMENT_HOOK_RUN_ALL(int, drop_privileges,
100
                          (apr_pool_t * pchild, server_rec * s),
101
                          (pchild, s), OK, DECLINED)
102
103
AP_IMPLEMENT_HOOK_RUN_FIRST(int, mpm,
104
                            (apr_pool_t *pconf, apr_pool_t *plog, server_rec *s),
105
                            (pconf, plog, s), DECLINED)
106
AP_IMPLEMENT_HOOK_RUN_FIRST(int, mpm_query,
107
                            (int query_code, int *result, apr_status_t *_rv),
108
                            (query_code, result, _rv), DECLINED)
109
AP_IMPLEMENT_HOOK_RUN_FIRST(apr_status_t, mpm_register_timed_callback,
110
                            (apr_time_t t, ap_mpm_callback_fn_t *cbfn, void *baton),
111
                            (t, cbfn, baton), APR_ENOTIMPL)
112
AP_IMPLEMENT_HOOK_RUN_FIRST(apr_status_t, mpm_resume_suspended,
113
                            (conn_rec *c),
114
                            (c), APR_ENOTIMPL)
115
AP_IMPLEMENT_HOOK_RUN_FIRST(apr_status_t, mpm_register_poll_callback,
116
                            (apr_pool_t *p, const apr_array_header_t *pds,
117
                             ap_mpm_callback_fn_t *cbfn, void *baton),
118
                            (p, pds, cbfn, baton), APR_ENOTIMPL)
119
AP_IMPLEMENT_HOOK_RUN_FIRST(apr_status_t, mpm_register_poll_callback_timeout,
120
                            (apr_pool_t *p, const apr_array_header_t *pds,
121
                             ap_mpm_callback_fn_t *cbfn,
122
                             ap_mpm_callback_fn_t *tofn,
123
                             void *baton, apr_time_t timeout),
124
                            (p, pds, cbfn, tofn, baton, timeout), APR_ENOTIMPL)
125
AP_IMPLEMENT_HOOK_RUN_FIRST(int, output_pending,
126
                            (conn_rec *c), (c), DECLINED)
127
AP_IMPLEMENT_HOOK_RUN_FIRST(int, input_pending,
128
                            (conn_rec *c), (c), DECLINED)
129
130
AP_IMPLEMENT_HOOK_VOID(end_generation,
131
                       (server_rec *s, ap_generation_t gen),
132
                       (s, gen))
133
AP_IMPLEMENT_HOOK_VOID(child_status,
134
                       (server_rec *s, pid_t pid, ap_generation_t gen, int slot, mpm_child_status status),
135
                       (s,pid,gen,slot,status))
136
AP_IMPLEMENT_HOOK_VOID(suspend_connection,
137
                       (conn_rec *c, request_rec *r),
138
                       (c, r))
139
AP_IMPLEMENT_HOOK_VOID(resume_connection,
140
                       (conn_rec *c, request_rec *r),
141
                       (c, r))
142
AP_IMPLEMENT_HOOK_VOID(child_stopping,
143
                       (apr_pool_t *pchild, int graceful),
144
                       (pchild, graceful))
145
AP_IMPLEMENT_HOOK_VOID(child_stopped,
146
                       (apr_pool_t *pchild, int graceful),
147
                       (pchild, graceful))
148
149
/* hooks with no args are implemented last, after disabling APR hook probes */
150
#if defined(APR_HOOK_PROBES_ENABLED)
151
#undef APR_HOOK_PROBES_ENABLED
152
#undef APR_HOOK_PROBE_ENTRY
153
#define APR_HOOK_PROBE_ENTRY(ud,ns,name,args)
154
#undef APR_HOOK_PROBE_RETURN
155
#define APR_HOOK_PROBE_RETURN(ud,ns,name,rv,args)
156
#undef APR_HOOK_PROBE_INVOKE
157
#define APR_HOOK_PROBE_INVOKE(ud,ns,name,src,args)
158
#undef APR_HOOK_PROBE_COMPLETE
159
#define APR_HOOK_PROBE_COMPLETE(ud,ns,name,src,rv,args)
160
#undef APR_HOOK_INT_DCL_UD
161
#define APR_HOOK_INT_DCL_UD
162
#endif
163
AP_IMPLEMENT_HOOK_RUN_FIRST(const char *, mpm_get_name,
164
                            (void),
165
                            (), NULL)
166
167
typedef struct mpm_gen_info_t {
168
    APR_RING_ENTRY(mpm_gen_info_t) link;
169
    int gen;          /* which gen? */
170
    int active;       /* number of active processes */
171
    int done;         /* gen finished? (whether or not active processes) */
172
} mpm_gen_info_t;
173
174
APR_RING_HEAD(mpm_gen_info_head_t, mpm_gen_info_t);
175
static struct mpm_gen_info_head_t *geninfo, *unused_geninfo;
176
static int gen_head_init; /* yuck */
177
178
/* variables representing config directives implemented here */
179
AP_DECLARE_DATA const char *ap_pid_fname;
180
AP_DECLARE_DATA int ap_max_requests_per_child;
181
AP_DECLARE_DATA char ap_coredump_dir[MAX_STRING_LEN];
182
AP_DECLARE_DATA int ap_coredumpdir_configured;
183
AP_DECLARE_DATA int ap_graceful_shutdown_timeout;
184
AP_DECLARE_DATA apr_size_t ap_thread_stacksize;
185
186
0
#define ALLOCATOR_MAX_FREE_DEFAULT (2048*1024)
187
AP_DECLARE_DATA apr_uint32_t ap_max_mem_free = ALLOCATOR_MAX_FREE_DEFAULT;
188
189
static apr_uint32_t extra_connection_count = 0; /* Number of open connections that the MPM does not own */
190
191
static void ap_mpm_note_extra_connection_added(void)
192
0
{
193
0
    apr_atomic_inc32(&extra_connection_count);
194
0
}
195
196
static void ap_mpm_note_extra_connection_removed(void)
197
0
{
198
0
    apr_atomic_dec32(&extra_connection_count);
199
0
}
200
201
AP_DECLARE(void) ap_mpm_register_extra_connection_fns(void)
202
0
{
203
0
    APR_REGISTER_OPTIONAL_FN(ap_mpm_note_extra_connection_added);
204
0
    APR_REGISTER_OPTIONAL_FN(ap_mpm_note_extra_connection_removed);
205
0
}
206
207
AP_DECLARE(void) ap_mpm_wait_for_extra_connections(void)
208
0
{
209
0
    apr_uint32_t count = apr_atomic_read32(&extra_connection_count);
210
0
    apr_time_t graceful, timeout, deadline;
211
212
0
    if (count == 0) {
213
0
        return;
214
0
    }
215
216
0
    graceful = apr_time_from_sec(ap_graceful_shutdown_timeout);
217
0
    timeout = (graceful > ap_server_conf->timeout) ? graceful : ap_server_conf->timeout;
218
0
    deadline = apr_time_now() + timeout;
219
220
0
    do {
221
0
        apr_sleep(apr_time_from_msec(100));
222
0
        count = apr_atomic_read32(&extra_connection_count);
223
0
    } while (count > 0 && apr_time_now() < deadline);
224
225
0
    if (count > 0) {
226
0
        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, ap_server_conf,
227
0
                     APLOGNO(10644)
228
0
                     "Child: %u connection(s) noted by modules did not "
229
0
                     "finish within %" APR_TIME_T_FMT " seconds, "
230
0
                     "exiting anyway",
231
0
                     count, apr_time_sec(timeout));
232
0
    }
233
0
}
234
235
/* Set defaults for config directives implemented here.  This is
236
 * called from core's pre-config hook, so MPMs which need to override
237
 * one of these should run their pre-config hook after that of core.
238
 */
239
void mpm_common_pre_config(apr_pool_t *pconf)
240
0
{
241
0
    ap_pid_fname = DEFAULT_PIDLOG;
242
0
    ap_max_requests_per_child = 0; /* unlimited */
243
0
    apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
244
0
    ap_coredumpdir_configured = 0;
245
0
    ap_graceful_shutdown_timeout = 0; /* unlimited */
246
0
    ap_max_mem_free = ALLOCATOR_MAX_FREE_DEFAULT;
247
0
    ap_thread_stacksize = 0; /* use system default */
248
0
}
249
250
/* number of calls to wait_or_timeout between writable probes */
251
#ifndef INTERVAL_OF_WRITABLE_PROBES
252
0
#define INTERVAL_OF_WRITABLE_PROBES 10
253
#endif
254
static int wait_or_timeout_counter;
255
256
AP_DECLARE(void) ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode,
257
                                    apr_proc_t *ret, apr_pool_t *p,
258
                                    server_rec *s)
259
0
{
260
0
    apr_status_t rv;
261
262
0
    ++wait_or_timeout_counter;
263
0
    if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
264
0
        wait_or_timeout_counter = 0;
265
0
        ap_run_monitor(p, s);
266
0
    }
267
268
0
    rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
269
0
    ap_update_global_status();
270
271
0
    if (APR_STATUS_IS_EINTR(rv)) {
272
0
        ret->pid = -1;
273
0
        return;
274
0
    }
275
276
0
    if (APR_STATUS_IS_CHILD_DONE(rv)) {
277
0
        return;
278
0
    }
279
280
0
    apr_sleep(apr_time_from_sec(1));
281
0
    ret->pid = -1;
282
0
}
283
284
#if defined(TCP_NODELAY)
285
void ap_sock_disable_nagle(apr_socket_t *s)
286
0
{
287
    /* The Nagle algorithm says that we should delay sending partial
288
     * packets in hopes of getting more data.  We don't want to do
289
     * this; we are not telnet.  There are bad interactions between
290
     * persistent connections and Nagle's algorithm that have very severe
291
     * performance penalties.  (Failing to disable Nagle is not much of a
292
     * problem with simple HTTP.)
293
     *
294
     * In spite of these problems, failure here is not a shooting offense.
295
     */
296
0
    apr_status_t status = apr_socket_opt_set(s, APR_TCP_NODELAY, 1);
297
298
0
    if (status != APR_SUCCESS) {
299
0
        ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf, APLOGNO(00542)
300
0
                     "apr_socket_opt_set: (TCP_NODELAY)");
301
0
    }
302
0
}
303
#endif
304
305
#ifdef HAVE_GETPWNAM
306
AP_DECLARE(uid_t) ap_uname2id(const char *name)
307
0
{
308
0
    struct passwd *ent;
309
310
0
    if (name[0] == '#')
311
0
        return (atoi(&name[1]));
312
313
0
    if (!(ent = getpwnam(name))) {
314
0
        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, APLOGNO(00543)
315
0
                     "%s: bad user name %s", ap_server_argv0, name);
316
0
        exit(1);
317
0
    }
318
319
0
    return (ent->pw_uid);
320
0
}
321
#endif
322
323
#ifdef HAVE_GETGRNAM
324
AP_DECLARE(gid_t) ap_gname2id(const char *name)
325
0
{
326
0
    struct group *ent;
327
328
0
    if (name[0] == '#')
329
0
        return (atoi(&name[1]));
330
331
0
    if (!(ent = getgrnam(name))) {
332
0
        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, APLOGNO(00544)
333
0
                     "%s: bad group name %s", ap_server_argv0, name);
334
0
        exit(1);
335
0
    }
336
337
0
    return (ent->gr_gid);
338
0
}
339
#endif
340
341
#ifndef HAVE_INITGROUPS
342
int initgroups(const char *name, gid_t basegid)
343
{
344
#if defined(_OSD_POSIX) || defined(OS2) || defined(WIN32) || defined(NETWARE)
345
    return 0;
346
#else
347
    gid_t groups[NGROUPS_MAX];
348
    struct group *g;
349
    int index = 0;
350
351
    setgrent();
352
353
    groups[index++] = basegid;
354
355
    while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) {
356
        if (g->gr_gid != basegid) {
357
            char **names;
358
359
            for (names = g->gr_mem; *names != NULL; ++names) {
360
                if (!strcmp(*names, name))
361
                    groups[index++] = g->gr_gid;
362
            }
363
        }
364
    }
365
366
    endgrent();
367
368
    return setgroups(index, groups);
369
#endif
370
}
371
#endif /* def HAVE_INITGROUPS */
372
373
/* standard mpm configuration handling */
374
375
const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
376
                               const char *arg)
377
0
{
378
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
379
0
    if (err != NULL) {
380
0
        return err;
381
0
    }
382
383
0
    if (cmd->server->is_virtual) {
384
0
        return "PidFile directive not allowed in <VirtualHost>";
385
0
    }
386
387
0
    ap_pid_fname = arg;
388
0
    return NULL;
389
0
}
390
391
void ap_mpm_dump_pidfile(apr_pool_t *p, apr_file_t *out)
392
0
{
393
0
    apr_file_printf(out, "PidFile: \"%s\"\n",
394
0
                    ap_runtime_dir_relative(p, ap_pid_fname));
395
0
}
396
397
const char *ap_mpm_set_max_requests(cmd_parms *cmd, void *dummy,
398
                                    const char *arg)
399
0
{
400
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
401
0
    if (err != NULL) {
402
0
        return err;
403
0
    }
404
405
0
    if (!strcasecmp(cmd->cmd->name, "MaxRequestsPerChild")) {
406
0
        ap_log_error(APLOG_MARK, APLOG_INFO, 0, NULL, APLOGNO(00545)
407
0
                     "MaxRequestsPerChild is deprecated, use "
408
0
                     "MaxConnectionsPerChild instead.");
409
0
    }
410
411
0
    ap_max_requests_per_child = atoi(arg);
412
413
0
    return NULL;
414
0
}
415
416
const char *ap_mpm_set_coredumpdir(cmd_parms *cmd, void *dummy,
417
                                   const char *arg)
418
0
{
419
0
    apr_finfo_t finfo;
420
0
    const char *fname;
421
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
422
0
    if (err != NULL) {
423
0
        return err;
424
0
    }
425
426
0
    fname = ap_server_root_relative(cmd->temp_pool, arg);
427
0
    if (!fname) {
428
0
        return apr_pstrcat(cmd->pool, "Invalid CoreDumpDirectory path ",
429
0
                           arg, NULL);
430
0
    }
431
0
    if (apr_stat(&finfo, fname, APR_FINFO_TYPE, cmd->pool) != APR_SUCCESS) {
432
0
        return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
433
0
                           " does not exist", NULL);
434
0
    }
435
0
    if (finfo.filetype != APR_DIR) {
436
0
        return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
437
0
                           " is not a directory", NULL);
438
0
    }
439
0
    apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
440
0
    ap_coredumpdir_configured = 1;
441
0
    return NULL;
442
0
}
443
444
AP_DECLARE(const char *)ap_mpm_set_graceful_shutdown(cmd_parms *cmd,
445
                                                     void *dummy,
446
                                                     const char *arg)
447
0
{
448
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
449
0
    if (err != NULL) {
450
0
        return err;
451
0
    }
452
0
    ap_graceful_shutdown_timeout = atoi(arg);
453
0
    return NULL;
454
0
}
455
456
const char *ap_mpm_set_max_mem_free(cmd_parms *cmd, void *dummy,
457
                                    const char *arg)
458
0
{
459
0
    long value;
460
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
461
0
    if (err != NULL) {
462
0
        return err;
463
0
    }
464
465
0
    errno = 0;
466
0
    value = strtol(arg, NULL, 10);
467
0
    if (value < 0 || errno == ERANGE)
468
0
        return apr_pstrcat(cmd->pool, "Invalid MaxMemFree value: ",
469
0
                           arg, NULL);
470
471
0
    ap_max_mem_free = (apr_uint32_t)value * 1024;
472
473
0
    return NULL;
474
0
}
475
476
const char *ap_mpm_set_thread_stacksize(cmd_parms *cmd, void *dummy,
477
                                        const char *arg)
478
0
{
479
0
    long value;
480
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
481
0
    if (err != NULL) {
482
0
        return err;
483
0
    }
484
485
0
    errno = 0;
486
0
    value = strtol(arg, NULL, 10);
487
0
    if (value < 0 || errno == ERANGE)
488
0
        return apr_pstrcat(cmd->pool, "Invalid ThreadStackSize value: ",
489
0
                           arg, NULL);
490
491
0
    ap_thread_stacksize = (apr_size_t)value;
492
493
0
    return NULL;
494
0
}
495
496
AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
497
0
{
498
0
    apr_status_t rv;
499
500
0
    if (ap_run_mpm_query(query_code, result, &rv) == DECLINED) {
501
0
        rv = APR_EGENERAL;
502
0
    }
503
504
0
    return rv;
505
0
}
506
507
static void end_gen(mpm_gen_info_t *gi)
508
0
{
509
0
    ap_log_error(APLOG_MARK, APLOG_TRACE4, 0, ap_server_conf,
510
0
                 "end of generation %d", gi->gen);
511
0
    ap_run_end_generation(ap_server_conf, gi->gen);
512
0
    APR_RING_REMOVE(gi, link);
513
0
    APR_RING_INSERT_HEAD(unused_geninfo, gi, mpm_gen_info_t, link);
514
0
}
515
516
apr_status_t ap_mpm_end_gen_helper(void *unused) /* cleanup on pconf */
517
0
{
518
0
    int gen = ap_config_generation - 1; /* differs from MPM generation */
519
0
    mpm_gen_info_t *cur;
520
521
0
    if (geninfo == NULL) {
522
        /* initial pconf teardown, MPM hasn't run */
523
0
        return APR_SUCCESS;
524
0
    }
525
526
0
    cur = APR_RING_FIRST(geninfo);
527
0
    while (cur != APR_RING_SENTINEL(geninfo, mpm_gen_info_t, link) &&
528
0
           cur->gen != gen) {
529
0
        cur = APR_RING_NEXT(cur, link);
530
0
    }
531
532
0
    if (cur == APR_RING_SENTINEL(geninfo, mpm_gen_info_t, link)) {
533
        /* last child of generation already exited */
534
0
        ap_log_error(APLOG_MARK, APLOG_TRACE4, 0, ap_server_conf,
535
0
                     "no record of generation %d", gen);
536
0
    }
537
0
    else {
538
0
        cur->done = 1;
539
0
        if (cur->active == 0) {
540
0
            end_gen(cur);
541
0
        }
542
0
    }
543
544
0
    return APR_SUCCESS;
545
0
}
546
547
/* core's child-status hook
548
 * tracks number of remaining children per generation and
549
 * runs the end-generation hook when the last child of
550
 * a generation exits
551
 */
552
void ap_core_child_status(server_rec *s, pid_t pid,
553
                          ap_generation_t gen, int slot,
554
                          mpm_child_status status)
555
0
{
556
0
    mpm_gen_info_t *cur;
557
0
    const char *status_msg = "unknown status";
558
559
0
    if (!gen_head_init) { /* where to run this? */
560
0
        gen_head_init = 1;
561
0
        geninfo = apr_pcalloc(s->process->pool, sizeof *geninfo);
562
0
        unused_geninfo = apr_pcalloc(s->process->pool, sizeof *unused_geninfo);
563
0
        APR_RING_INIT(geninfo, mpm_gen_info_t, link);
564
0
        APR_RING_INIT(unused_geninfo, mpm_gen_info_t, link);
565
0
    }
566
567
0
    cur = APR_RING_FIRST(geninfo);
568
0
    while (cur != APR_RING_SENTINEL(geninfo, mpm_gen_info_t, link) &&
569
0
           cur->gen != gen) {
570
0
        cur = APR_RING_NEXT(cur, link);
571
0
    }
572
573
0
    switch(status) {
574
0
    case MPM_CHILD_STARTED:
575
0
        status_msg = "started";
576
0
        if (cur == APR_RING_SENTINEL(geninfo, mpm_gen_info_t, link)) {
577
            /* first child for this generation */
578
0
            if (!APR_RING_EMPTY(unused_geninfo, mpm_gen_info_t, link)) {
579
0
                cur = APR_RING_FIRST(unused_geninfo);
580
0
                APR_RING_REMOVE(cur, link);
581
0
                cur->active = cur->done = 0;
582
0
            }
583
0
            else {
584
0
                cur = apr_pcalloc(s->process->pool, sizeof *cur);
585
0
            }
586
0
            cur->gen = gen;
587
0
            APR_RING_ELEM_INIT(cur, link);
588
0
            APR_RING_INSERT_HEAD(geninfo, cur, mpm_gen_info_t, link);
589
0
        }
590
0
        ap_random_parent_after_fork();
591
0
        ++cur->active;
592
0
        break;
593
0
    case MPM_CHILD_EXITED:
594
0
        ap_update_global_status();
595
0
        status_msg = "exited";
596
0
        if (cur == APR_RING_SENTINEL(geninfo, mpm_gen_info_t, link)) {
597
0
            ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00546)
598
0
                         "no record of generation %d of exiting child %" APR_PID_T_FMT,
599
0
                         gen, pid);
600
0
        }
601
0
        else {
602
0
            --cur->active;
603
0
            if (!cur->active && cur->done) { /* no children, server has stopped/restarted */
604
0
                end_gen(cur);
605
0
            }
606
0
        }
607
0
        break;
608
0
    case MPM_CHILD_LOST_SLOT:
609
0
        status_msg = "lost slot";
610
        /* we don't track by slot, so it doesn't matter */
611
0
        break;
612
0
    }
613
0
    ap_log_error(APLOG_MARK, APLOG_TRACE4, 0, s,
614
0
                 "mpm child %" APR_PID_T_FMT " (gen %d/slot %d) %s",
615
0
                 pid, gen, slot, status_msg);
616
0
}
617
618
AP_DECLARE(apr_status_t) ap_mpm_resume_suspended(conn_rec *c)
619
0
{
620
0
    return ap_run_mpm_resume_suspended(c);
621
0
}
622
623
AP_DECLARE(apr_status_t) ap_mpm_register_timed_callback(apr_time_t t,
624
        ap_mpm_callback_fn_t *cbfn, void *baton)
625
0
{
626
0
    return ap_run_mpm_register_timed_callback(t, cbfn, baton);
627
0
}
628
629
AP_DECLARE(apr_status_t) ap_mpm_register_poll_callback(
630
        apr_pool_t *p, const apr_array_header_t *pfds,
631
        ap_mpm_callback_fn_t *cbfn, void *baton)
632
0
{
633
0
    return ap_run_mpm_register_poll_callback(p, pfds, cbfn, baton);
634
0
}
635
636
AP_DECLARE(apr_status_t) ap_mpm_register_poll_callback_timeout(
637
        apr_pool_t *p, const apr_array_header_t *pfds,
638
        ap_mpm_callback_fn_t *cbfn, ap_mpm_callback_fn_t *tofn,
639
        void *baton, apr_time_t timeout)
640
0
{
641
0
    return ap_run_mpm_register_poll_callback_timeout(p, pfds, cbfn, tofn,
642
0
                                                     baton, timeout);
643
0
}
644
645
AP_DECLARE(const char *)ap_show_mpm(void)
646
0
{
647
0
    const char *name = ap_run_mpm_get_name();
648
649
0
    if (!name) {
650
0
        name = "";
651
0
    }
652
653
0
    return name;
654
0
}
655
656
AP_DECLARE(const char *)ap_check_mpm(void)
657
0
{
658
0
    static const char *last_mpm_name = NULL;
659
660
0
    if (!_hooks.link_mpm || _hooks.link_mpm->nelts == 0)
661
0
        return "No MPM loaded.";
662
0
    else if (_hooks.link_mpm->nelts > 1)
663
0
        return "More than one MPM loaded.";
664
665
0
    if (last_mpm_name) {
666
0
        if (strcmp(last_mpm_name, ap_show_mpm())) {
667
0
            return "The MPM cannot be changed during restart.";
668
0
        }
669
0
    }
670
0
    else {
671
0
        last_mpm_name = apr_pstrdup(ap_pglobal, ap_show_mpm());
672
0
    }
673
674
0
    return NULL;
675
0
}