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.
17 /* Multi-process, multi-threaded MPM for OS/2
20 * - a main, parent process
21 * - a small, static number of child processes
23 * The parent process's job is to manage the child processes. This involves
24 * spawning children as required to ensure there are always ap_daemons_to_start
25 * processes accepting connections.
27 * Each child process consists of a a pool of worker threads and a
28 * main thread that accepts connections & passes them to the workers via
29 * a work queue. The worker thread pool is dynamic, managed by a maintanence
30 * thread so that the number of idle threads is kept between
31 * min_spare_threads & max_spare_threads.
37 - Enforce MaxRequestWorkers somehow
41 #define INCL_DOSERRORS
43 #include "ap_config.h"
45 #include "mpm_default.h"
46 #include "http_main.h"
48 #include "http_config.h"
49 #include "http_core.h" /* for get_remote_host */
50 #include "http_connection.h"
52 #include "ap_listen.h"
53 #include "apr_portable.h"
54 #include "mpm_common.h"
55 #include "scoreboard.h"
56 #include "apr_strings.h"
60 /* We don't need many processes,
61 * they're only for redundancy in the event of a crash
63 #define HARD_SERVER_LIMIT 10
65 /* Limit on the total number of threads per process
67 #ifndef HARD_THREAD_LIMIT
68 #define HARD_THREAD_LIMIT 256
71 server_rec *ap_server_conf;
72 static apr_pool_t *pconf = NULL; /* Pool for config stuff */
75 static int one_process = 0;
76 static int ap_daemons_to_start = 0;
77 static int ap_thread_limit = 0;
78 int ap_min_spare_threads = 0;
79 int ap_max_spare_threads = 0;
81 /* Keep track of a few interesting statistics */
82 int ap_max_daemons_limit = -1;
84 /* volatile just in case */
85 static int volatile shutdown_pending;
86 static int volatile restart_pending;
87 static int volatile is_graceful = 0;
88 ap_generation_t volatile ap_my_generation=0; /* Used by the scoreboard */
89 static int is_parent_process=TRUE;
90 HMTX ap_mpm_accept_mutex = 0;
92 /* An array of these is stored in a shared memory area for passing
93 * sockets from the parent to child processes
96 struct sockaddr_in name;
97 apr_os_sock_t listen_fd;
102 listen_socket_t listeners[1];
105 static char master_main();
106 static void spawn_child(int slot);
107 void ap_mpm_child_main(apr_pool_t *pconf);
108 static void set_signals();
111 static int mpmt_os2_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s )
113 char *listener_shm_name;
114 parent_info_t *parent_info;
120 DosSetMaxFH(ap_thread_limit * 2);
121 listener_shm_name = apr_psprintf(pconf, "/sharemem/httpd/parent_info.%d", getppid());
122 rc = DosGetNamedSharedMem((PPVOID)&parent_info, listener_shm_name, PAG_READ);
123 is_parent_process = rc != 0;
124 ap_scoreboard_fname = apr_psprintf(pconf, "/sharemem/httpd/scoreboard.%d", is_parent_process ? getpid() : getppid());
129 int num_listeners = 0;
131 ap_mpm_accept_mutex = parent_info->accept_mutex;
133 /* Set up a default listener if necessary */
134 if (ap_listeners == NULL) {
135 ap_listen_rec *lr = apr_pcalloc(s->process->pool, sizeof(ap_listen_rec));
137 apr_sockaddr_info_get(&lr->bind_addr, "0.0.0.0", APR_UNSPEC,
138 DEFAULT_HTTP_PORT, 0, s->process->pool);
139 apr_socket_create(&lr->sd, lr->bind_addr->family,
140 SOCK_STREAM, 0, s->process->pool);
143 for (lr = ap_listeners; lr; lr = lr->next) {
145 apr_os_sock_put(&lr->sd, &parent_info->listeners[num_listeners].listen_fd, pconf);
146 apr_socket_addr_get(&sa, APR_LOCAL, lr->sd);
150 DosFreeMem(parent_info);
153 ap_mpm_child_main(pconf);
161 is_parent_process = TRUE;
163 if (ap_setup_listeners(ap_server_conf) < 1) {
164 ap_log_error(APLOG_MARK, APLOG_ALERT, 0, s,
165 "no listening sockets available, shutting down");
169 ap_log_pid(pconf, ap_pid_fname);
171 restart = master_main();
173 ap_scoreboard_image->global->running_generation = ap_my_generation;
176 ap_remove_pid(pconf, ap_pid_fname);
177 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
178 "caught SIGTERM, shutting down");
181 } /* Parent process */
183 return 0; /* Restart */
188 /* Main processing of the parent process
189 * returns TRUE if restarting
191 static char master_main()
193 server_rec *s = ap_server_conf;
195 parent_info_t *parent_info;
196 char *listener_shm_name;
197 int listener_num, num_listeners, slot;
200 printf("%s \n", ap_get_server_description());
203 if (ap_setup_listeners(ap_server_conf) < 1) {
204 ap_log_error(APLOG_MARK, APLOG_ALERT, 0, s,
205 "no listening sockets available, shutting down");
209 /* Allocate a shared memory block for the array of listeners */
210 for (num_listeners = 0, lr = ap_listeners; lr; lr = lr->next) {
214 listener_shm_name = apr_psprintf(pconf, "/sharemem/httpd/parent_info.%d", getpid());
215 rc = DosAllocSharedMem((PPVOID)&parent_info, listener_shm_name,
216 sizeof(parent_info_t) + num_listeners * sizeof(listen_socket_t),
217 PAG_READ|PAG_WRITE|PAG_COMMIT);
220 ap_log_error(APLOG_MARK, APLOG_ALERT, APR_FROM_OS_ERROR(rc), s,
221 "failure allocating shared memory, shutting down");
225 /* Store the listener sockets in the shared memory area for our children to see */
226 for (listener_num = 0, lr = ap_listeners; lr; lr = lr->next, listener_num++) {
227 apr_os_sock_get(&parent_info->listeners[listener_num].listen_fd, lr->sd);
230 /* Create mutex to prevent multiple child processes from detecting
231 * a connection with apr_poll()
234 rc = DosCreateMutexSem(NULL, &ap_mpm_accept_mutex, DC_SEM_SHARED, FALSE);
237 ap_log_error(APLOG_MARK, APLOG_ALERT, APR_FROM_OS_ERROR(rc), s,
238 "failure creating accept mutex, shutting down");
242 parent_info->accept_mutex = ap_mpm_accept_mutex;
244 /* Allocate shared memory for scoreboard */
245 if (ap_scoreboard_image == NULL) {
247 rc = DosAllocSharedMem(&sb_mem, ap_scoreboard_fname,
248 ap_calc_scoreboard_size(),
249 PAG_COMMIT|PAG_READ|PAG_WRITE);
252 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
253 "unable to allocate shared memory for scoreboard , exiting");
257 ap_init_scoreboard(sb_mem);
260 ap_scoreboard_image->global->restart_time = apr_time_now();
261 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
262 "%s configured -- resuming normal operations",
263 ap_get_server_description());
264 ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
265 "Server built: %s", ap_get_server_built());
267 ap_scoreboard_image->parent[0].pid = getpid();
268 ap_mpm_child_main(pconf);
272 while (!restart_pending && !shutdown_pending) {
275 int active_children = 0;
277 /* Count number of active children */
278 for (slot=0; slot < HARD_SERVER_LIMIT; slot++) {
279 active_children += ap_scoreboard_image->parent[slot].pid != 0 &&
280 !ap_scoreboard_image->parent[slot].quiescing;
283 /* Spawn children if needed */
284 for (slot=0; slot < HARD_SERVER_LIMIT && active_children < ap_daemons_to_start; slot++) {
285 if (ap_scoreboard_image->parent[slot].pid == 0) {
291 rc = DosWaitChild(DCWA_PROCESSTREE, DCWW_NOWAIT, &proc_rc, &child_pid, 0);
294 /* A child has terminated, remove its scoreboard entry & terminate if necessary */
295 for (slot=0; ap_scoreboard_image->parent[slot].pid != child_pid && slot < HARD_SERVER_LIMIT; slot++);
297 if (slot < HARD_SERVER_LIMIT) {
298 ap_scoreboard_image->parent[slot].pid = 0;
299 ap_scoreboard_image->parent[slot].quiescing = 0;
301 if (proc_rc.codeTerminate == TC_EXIT) {
302 /* Child terminated normally, check its exit code and
303 * terminate server if child indicates a fatal error
305 if (proc_rc.codeResult == APEXIT_CHILDFATAL)
309 } else if (rc == ERROR_CHILD_NOT_COMPLETE) {
310 /* No child exited, lets sleep for a while.... */
311 apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
315 /* Signal children to shut down, either gracefully or immediately */
316 for (slot=0; slot<HARD_SERVER_LIMIT; slot++) {
317 kill(ap_scoreboard_image->parent[slot].pid, is_graceful ? SIGHUP : SIGTERM);
320 DosFreeMem(parent_info);
321 return restart_pending;
326 static void spawn_child(int slot)
330 char fail_module[100];
331 char progname[CCHMAXPATH];
335 ap_scoreboard_image->parent[slot].generation = ap_my_generation;
336 DosGetInfoBlocks(&ptib, &ppib);
337 DosQueryModuleName(ppib->pib_hmte, sizeof(progname), progname);
338 rc = DosExecPgm(fail_module, sizeof(fail_module), EXEC_ASYNCRESULT,
339 ppib->pib_pchcmd, NULL, &proc_rc, progname);
342 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
343 "error spawning child, slot %d", slot);
346 if (ap_max_daemons_limit < slot) {
347 ap_max_daemons_limit = slot;
350 ap_scoreboard_image->parent[slot].pid = proc_rc.codeTerminate;
355 /* Signal handling routines */
357 static void sig_term(int sig)
359 shutdown_pending = 1;
360 signal(SIGTERM, SIG_DFL);
365 static void sig_restart(int sig)
367 if (sig == SIGUSR1) {
376 static void set_signals()
380 sigemptyset(&sa.sa_mask);
382 sa.sa_handler = sig_term;
384 if (sigaction(SIGTERM, &sa, NULL) < 0)
385 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
387 if (sigaction(SIGINT, &sa, NULL) < 0)
388 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");
390 sa.sa_handler = sig_restart;
392 if (sigaction(SIGHUP, &sa, NULL) < 0)
393 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");
394 if (sigaction(SIGUSR1, &sa, NULL) < 0)
395 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGUSR1)");
400 /* Enquiry functions used get MPM status info */
402 static apr_status_t mpmt_os2_query(int query_code, int *result, apr_status_t *rv)
406 switch (query_code) {
407 case AP_MPMQ_MAX_DAEMON_USED:
408 *result = ap_max_daemons_limit;
411 case AP_MPMQ_IS_THREADED:
412 *result = AP_MPMQ_DYNAMIC;
415 case AP_MPMQ_IS_FORKED:
416 *result = AP_MPMQ_NOT_SUPPORTED;
419 case AP_MPMQ_HARD_LIMIT_DAEMONS:
420 *result = HARD_SERVER_LIMIT;
423 case AP_MPMQ_HARD_LIMIT_THREADS:
424 *result = HARD_THREAD_LIMIT;
427 case AP_MPMQ_MIN_SPARE_DAEMONS:
431 case AP_MPMQ_MAX_SPARE_DAEMONS:
435 case AP_MPMQ_MAX_REQUESTS_DAEMON:
436 *result = ap_max_requests_per_child;
439 case AP_MPMQ_GENERATION:
440 *result = ap_my_generation;
454 static const char *mpmt_os2_get_name(void)
462 /* Configuration handling stuff */
464 static int mpmt_os2_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
466 one_process = ap_exists_config_define("ONE_PROCESS") ||
467 ap_exists_config_define("DEBUG");
469 ap_listen_pre_config();
470 ap_daemons_to_start = DEFAULT_START_DAEMON;
471 ap_thread_limit = HARD_THREAD_LIMIT;
472 ap_extended_status = 0;
473 ap_min_spare_threads = DEFAULT_MIN_SPARE_THREAD;
474 ap_max_spare_threads = DEFAULT_MAX_SPARE_THREAD;
475 ap_sys_privileges_handlers(1);
482 static int mpmt_os2_check_config(apr_pool_t *p, apr_pool_t *plog,
483 apr_pool_t *ptemp, server_rec *s)
485 static int restart_num = 0;
488 /* we want this only the first time around */
489 if (restart_num++ == 0) {
493 if (ap_daemons_to_start < 0) {
495 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
496 "WARNING: StartServers of %d not allowed, "
497 "increasing to 1.", ap_daemons_to_start);
499 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
500 "StartServers of %d not allowed, increasing to 1",
501 ap_daemons_to_start);
503 ap_daemons_to_start = 1;
506 if (ap_min_spare_threads < 1) {
508 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
509 "WARNING: MinSpareThreads of %d not allowed, "
510 "increasing to 1", ap_min_spare_threads);
511 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
512 " to avoid almost certain server failure.");
513 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
514 " Please read the documentation.");
516 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
517 "MinSpareThreads of %d not allowed, increasing to 1",
518 ap_min_spare_threads);
520 ap_min_spare_threads = 1;
528 static void mpmt_os2_hooks(apr_pool_t *p)
530 ap_hook_pre_config(mpmt_os2_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
531 ap_hook_check_config(mpmt_os2_check_config, NULL, NULL, APR_HOOK_MIDDLE);
532 ap_hook_mpm(mpmt_os2_run, NULL, NULL, APR_HOOK_MIDDLE);
533 ap_hook_mpm_query(mpmt_os2_query, NULL, NULL, APR_HOOK_MIDDLE);
534 ap_hook_mpm_get_name(mpmt_os2_get_name, NULL, NULL, APR_HOOK_MIDDLE);
539 static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy, const char *arg)
541 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
547 ap_daemons_to_start = atoi(arg);
553 static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy,
556 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
562 ap_min_spare_threads = atoi(arg);
568 static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy,
571 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
577 ap_max_spare_threads = atoi(arg);
583 static const char *ignore_cmd(cmd_parms *cmd, void *dummy, const char *arg)
590 static const command_rec mpmt_os2_cmds[] = {
592 AP_INIT_TAKE1( "StartServers", set_daemons_to_start, NULL, RSRC_CONF,
593 "Number of child processes launched at server startup" ),
594 AP_INIT_TAKE1("MinSpareThreads", set_min_spare_threads, NULL, RSRC_CONF,
595 "Minimum number of idle children, to handle request spikes"),
596 AP_INIT_TAKE1("MaxSpareThreads", set_max_spare_threads, NULL, RSRC_CONF,
597 "Maximum number of idle children"),
598 AP_INIT_TAKE1("User", ignore_cmd, NULL, RSRC_CONF,
599 "Not applicable on this platform"),
600 AP_INIT_TAKE1("Group", ignore_cmd, NULL, RSRC_CONF,
601 "Not applicable on this platform"),
602 AP_INIT_TAKE1("ScoreBoardFile", ignore_cmd, NULL, RSRC_CONF, \
603 "Not applicable on this platform"),
607 AP_DECLARE_MODULE(mpm_mpmt_os2) = {
609 NULL, /* hook to run before apache parses args */
610 NULL, /* create per-directory config structure */
611 NULL, /* merge per-directory config structures */
612 NULL, /* create per-server config structure */
613 NULL, /* merge per-server config structures */
614 mpmt_os2_cmds, /* command apr_table_t */
615 mpmt_os2_hooks, /* register_hooks */