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 #define INCL_DOSERRORS
21 #include "ap_config.h"
23 #include "mpm_default.h"
24 #include "http_main.h"
26 #include "http_config.h"
27 #include "http_core.h" /* for get_remote_host */
28 #include "http_connection.h"
29 #include "scoreboard.h"
31 #include "ap_listen.h"
32 #include "apr_portable.h"
34 #include "mpm_common.h"
35 #include "apr_strings.h"
39 /* XXXXXX move these to header file private to this MPM */
41 /* We don't need many processes,
42 * they're only for redundancy in the event of a crash
44 #define HARD_SERVER_LIMIT 10
46 /* Limit on the total number of threads per process
48 #ifndef HARD_THREAD_LIMIT
49 #define HARD_THREAD_LIMIT 256
52 #define ID_FROM_CHILD_THREAD(c, t) ((c * HARD_THREAD_LIMIT) + t)
56 apr_socket_t *conn_sd;
59 #define WORKTYPE_CONN 0
60 #define WORKTYPE_EXIT 1
62 static apr_pool_t *pchild = NULL;
63 static int child_slot;
64 static int shutdown_pending = 0;
65 extern int ap_my_generation;
66 static int volatile is_graceful = 1;
67 HEV shutdown_event; /* signaled when this child is shutting down */
69 /* grab some MPM globals */
70 extern int ap_min_spare_threads;
71 extern int ap_max_spare_threads;
72 extern HMTX ap_mpm_accept_mutex;
74 static void worker_main(void *vpArg);
75 static void clean_child_exit(int code);
76 static void set_signals();
77 static void server_maintenance(void *vpArg);
80 static void clean_child_exit(int code)
83 apr_pool_destroy(pchild);
91 void ap_mpm_child_main(apr_pool_t *pconf)
93 ap_listen_rec *lr = NULL;
94 int requests_this_child = 0;
96 unsigned long ulTimes;
97 int my_pid = getpid();
100 apr_pollset_t *pollset;
102 TID server_maint_tid;
105 /* Stop Ctrl-C/Ctrl-Break signals going to child processes */
106 DosSetSignalExceptionFocus(0, &ulTimes);
109 /* Create pool for child */
110 apr_pool_create(&pchild, pconf);
112 ap_run_child_init(pchild, ap_server_conf);
114 /* Create an event semaphore used to trigger other threads to shutdown */
115 rc = DosCreateEventSem(NULL, &shutdown_event, 0, FALSE);
118 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
119 "unable to create shutdown semaphore, exiting");
120 clean_child_exit(APEXIT_CHILDFATAL);
123 /* Gain access to the scoreboard. */
124 rc = DosGetNamedSharedMem(&sb_mem, ap_scoreboard_fname,
128 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
129 "scoreboard not readable in child, exiting");
130 clean_child_exit(APEXIT_CHILDFATAL);
133 ap_calc_scoreboard_size();
134 ap_init_scoreboard(sb_mem);
136 /* Gain access to the accpet mutex */
137 rc = DosOpenMutexSem(NULL, &ap_mpm_accept_mutex);
140 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
141 "accept mutex couldn't be accessed in child, exiting");
142 clean_child_exit(APEXIT_CHILDFATAL);
145 /* Find our pid in the scoreboard so we know what slot our parent allocated us */
146 for (child_slot = 0; ap_scoreboard_image->parent[child_slot].pid != my_pid && child_slot < HARD_SERVER_LIMIT; child_slot++);
148 if (child_slot == HARD_SERVER_LIMIT) {
149 ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
150 "child pid not found in scoreboard, exiting");
151 clean_child_exit(APEXIT_CHILDFATAL);
154 ap_my_generation = ap_scoreboard_image->parent[child_slot].generation;
155 memset(ap_scoreboard_image->servers[child_slot], 0, sizeof(worker_score) * HARD_THREAD_LIMIT);
157 /* Set up an OS/2 queue for passing connections & termination requests
160 rc = DosCreateQueue(&workq, QUE_FIFO, apr_psprintf(pchild, "/queues/httpd/work.%d", my_pid));
163 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
164 "unable to create work queue, exiting");
165 clean_child_exit(APEXIT_CHILDFATAL);
168 /* Create initial pool of worker threads */
169 for (c = 0; c < ap_min_spare_threads; c++) {
170 // ap_scoreboard_image->servers[child_slot][c].tid = _beginthread(worker_main, NULL, 128*1024, (void *)c);
173 /* Start maintenance thread */
174 server_maint_tid = _beginthread(server_maintenance, NULL, 32768, NULL);
177 for (num_listeners = 0, lr = ap_listeners; lr; lr = lr->next) {
181 apr_pollset_create(&pollset, num_listeners, pchild, 0);
183 for (lr = ap_listeners; lr != NULL; lr = lr->next) {
184 apr_pollfd_t pfd = { 0 };
186 pfd.desc_type = APR_POLL_SOCKET;
188 pfd.reqevents = APR_POLLIN;
189 pfd.client_data = lr;
190 apr_pollset_add(pollset, &pfd);
193 /* Main connection accept loop */
196 worker_args_t *worker_args;
197 int last_poll_idx = 0;
199 apr_pool_create(&pconn, pchild);
200 worker_args = apr_palloc(pconn, sizeof(worker_args_t));
201 worker_args->pconn = pconn;
203 if (num_listeners == 1) {
204 rv = apr_socket_accept(&worker_args->conn_sd, ap_listeners->sd, pconn);
206 const apr_pollfd_t *poll_results;
207 apr_int32_t num_poll_results;
209 rc = DosRequestMutexSem(ap_mpm_accept_mutex, SEM_INDEFINITE_WAIT);
211 if (shutdown_pending) {
212 DosReleaseMutexSem(ap_mpm_accept_mutex);
216 rv = APR_FROM_OS_ERROR(rc);
218 if (rv == APR_SUCCESS) {
219 rv = apr_pollset_poll(pollset, -1, &num_poll_results, &poll_results);
220 DosReleaseMutexSem(ap_mpm_accept_mutex);
223 if (rv == APR_SUCCESS) {
224 if (last_poll_idx >= num_listeners) {
228 lr = poll_results[last_poll_idx++].client_data;
229 rv = apr_socket_accept(&worker_args->conn_sd, lr->sd, pconn);
234 if (rv != APR_SUCCESS) {
235 if (!APR_STATUS_IS_EINTR(rv)) {
236 ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf,
237 "apr_socket_accept");
238 clean_child_exit(APEXIT_CHILDFATAL);
241 DosWriteQueue(workq, WORKTYPE_CONN, sizeof(worker_args_t), worker_args, 0);
242 requests_this_child++;
245 if (ap_max_requests_per_child != 0 && requests_this_child >= ap_max_requests_per_child)
247 } while (!shutdown_pending && ap_my_generation == ap_scoreboard_image->global->running_generation);
249 ap_scoreboard_image->parent[child_slot].quiescing = 1;
250 DosPostEventSem(shutdown_event);
251 DosWaitThread(&server_maint_tid, DCWW_WAIT);
256 /* tell our worker threads to exit */
257 for (c=0; c<HARD_THREAD_LIMIT; c++) {
258 if (ap_scoreboard_image->servers[child_slot][c].status != SERVER_DEAD) {
259 DosWriteQueue(workq, WORKTYPE_EXIT, 0, NULL, 0);
266 for (c=0; c<HARD_THREAD_LIMIT; c++) {
267 if (ap_scoreboard_image->servers[child_slot][c].status != SERVER_DEAD) {
275 DosPurgeQueue(workq);
277 for (c=0; c<HARD_THREAD_LIMIT; c++) {
278 if (ap_scoreboard_image->servers[child_slot][c].status != SERVER_DEAD) {
279 DosKillThread(ap_scoreboard_image->servers[child_slot][c].tid);
284 apr_pool_destroy(pchild);
293 /* Find a free thread slot */
294 for (thread_slot=0; thread_slot < HARD_THREAD_LIMIT; thread_slot++) {
295 if (ap_scoreboard_image->servers[child_slot][thread_slot].status == SERVER_DEAD) {
296 ap_scoreboard_image->servers[child_slot][thread_slot].status = SERVER_STARTING;
297 ap_scoreboard_image->servers[child_slot][thread_slot].tid =
298 _beginthread(worker_main, NULL, 128*1024, (void *)thread_slot);
306 ULONG APIENTRY thread_exception_handler(EXCEPTIONREPORTRECORD *pReportRec,
307 EXCEPTIONREGISTRATIONRECORD *pRegRec,
308 CONTEXTRECORD *pContext,
313 if (pReportRec->fHandlerFlags & EH_NESTED_CALL) {
314 return XCPT_CONTINUE_SEARCH;
317 if (pReportRec->ExceptionNum == XCPT_ACCESS_VIOLATION ||
318 pReportRec->ExceptionNum == XCPT_INTEGER_DIVIDE_BY_ZERO) {
319 ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
320 "caught exception in worker thread, initiating child shutdown pid=%d", getpid());
321 for (c=0; c<HARD_THREAD_LIMIT; c++) {
322 if (ap_scoreboard_image->servers[child_slot][c].tid == _gettid()) {
323 ap_scoreboard_image->servers[child_slot][c].status = SERVER_DEAD;
328 /* Shut down process ASAP, it could be quite unhealthy & leaking resources */
329 shutdown_pending = 1;
330 ap_scoreboard_image->parent[child_slot].quiescing = 1;
331 kill(getpid(), SIGHUP);
332 DosUnwindException(UNWIND_ALL, 0, 0);
335 return XCPT_CONTINUE_SEARCH;
340 static void worker_main(void *vpArg)
343 conn_rec *current_conn;
345 apr_allocator_t *allocator;
346 apr_bucket_alloc_t *bucket_alloc;
347 worker_args_t *worker_args;
354 int thread_slot = (int)vpArg;
355 EXCEPTIONREGISTRATIONRECORD reg_rec = { NULL, thread_exception_handler };
358 /* Trap exceptions in this thread so we don't take down the whole process */
359 DosSetExceptionHandler( ®_rec );
361 rc = DosOpenQueue(&owner, &workq,
362 apr_psprintf(pchild, "/queues/httpd/work.%d", getpid()));
365 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
366 "unable to open work queue, exiting");
367 ap_scoreboard_image->servers[child_slot][thread_slot].tid = 0;
370 conn_id = ID_FROM_CHILD_THREAD(child_slot, thread_slot);
371 ap_update_child_status_from_indexes(child_slot, thread_slot, SERVER_READY,
374 apr_allocator_create(&allocator);
375 apr_allocator_max_free_set(allocator, ap_max_mem_free);
376 bucket_alloc = apr_bucket_alloc_create_ex(allocator);
378 while (rc = DosReadQueue(workq, &rd, &len, (PPVOID)&worker_args, 0, DCWW_WAIT, &priority, NULLHANDLE),
379 rc == 0 && rd.ulData != WORKTYPE_EXIT) {
380 pconn = worker_args->pconn;
381 ap_create_sb_handle(&sbh, pconn, child_slot, thread_slot);
382 current_conn = ap_run_create_connection(pconn, ap_server_conf,
383 worker_args->conn_sd, conn_id,
387 ap_process_connection(current_conn, worker_args->conn_sd);
388 ap_lingering_close(current_conn);
391 apr_pool_destroy(pconn);
392 ap_update_child_status_from_indexes(child_slot, thread_slot,
396 ap_update_child_status_from_indexes(child_slot, thread_slot, SERVER_DEAD,
399 apr_bucket_alloc_destroy(bucket_alloc);
400 apr_allocator_destroy(allocator);
405 static void server_maintenance(void *vpArg)
407 int num_idle, num_needed;
408 ULONG num_pending = 0;
414 rc = DosOpenQueue(&owner, &workq,
415 apr_psprintf(pchild, "/queues/httpd/work.%d", getpid()));
418 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
419 "unable to open work queue in maintenance thread");
424 for (num_idle=0, threadnum=0; threadnum < HARD_THREAD_LIMIT; threadnum++) {
425 num_idle += ap_scoreboard_image->servers[child_slot][threadnum].status == SERVER_READY;
428 DosQueryQueue(workq, &num_pending);
429 num_needed = ap_min_spare_threads - num_idle + num_pending;
431 if (num_needed > 0) {
432 for (threadnum=0; threadnum < num_needed; threadnum++) {
437 if (num_idle - num_pending > ap_max_spare_threads) {
438 DosWriteQueue(workq, WORKTYPE_EXIT, 0, NULL, 0);
440 } while (DosWaitEventSem(shutdown_event, 500) == ERROR_TIMEOUT);
445 /* Signal handling routines */
447 static void sig_term(int sig)
449 shutdown_pending = 1;
451 signal(SIGTERM, SIG_DFL);
456 static void sig_hup(int sig)
458 shutdown_pending = 1;
464 static void set_signals()
468 sigemptyset(&sa.sa_mask);
470 sa.sa_handler = sig_term;
472 if (sigaction(SIGTERM, &sa, NULL) < 0)
473 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
475 sa.sa_handler = sig_hup;
477 if (sigaction(SIGHUP, &sa, NULL) < 0)
478 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");