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.
20 #define INCL_DOSERRORS
22 #include "ap_config.h"
24 #include "mpm_default.h"
25 #include "http_main.h"
27 #include "http_config.h"
28 #include "http_core.h" /* for get_remote_host */
29 #include "http_connection.h"
32 #include "ap_listen.h"
33 #include "apr_portable.h"
35 #include "mpm_common.h"
36 #include "apr_strings.h"
40 /* XXXXXX move these to header file private to this MPM */
42 /* We don't need many processes,
43 * they're only for redundancy in the event of a crash
45 #define HARD_SERVER_LIMIT 10
47 /* Limit on the total number of threads per process
49 #ifndef HARD_THREAD_LIMIT
50 #define HARD_THREAD_LIMIT 256
53 #define ID_FROM_CHILD_THREAD(c, t) ((c * HARD_THREAD_LIMIT) + t)
57 apr_socket_t *conn_sd;
60 #define WORKTYPE_CONN 0
61 #define WORKTYPE_EXIT 1
63 static apr_pool_t *pchild = NULL;
64 static int child_slot;
65 static int shutdown_pending = 0;
66 extern int ap_my_generation;
67 static int volatile is_graceful = 1;
68 HEV shutdown_event; /* signaled when this child is shutting down */
70 /* grab some MPM globals */
71 extern int ap_min_spare_threads;
72 extern int ap_max_spare_threads;
73 extern HMTX ap_mpm_accept_mutex;
75 static void worker_main(void *vpArg);
76 static void clean_child_exit(int code);
77 static void set_signals();
78 static void server_maintenance(void *vpArg);
81 static void clean_child_exit(int code)
84 apr_pool_destroy(pchild);
92 void ap_mpm_child_main(apr_pool_t *pconf)
94 ap_listen_rec *lr = NULL;
95 ap_listen_rec *first_lr = NULL;
96 int requests_this_child = 0;
97 apr_socket_t *sd = ap_listeners->sd;
99 unsigned long ulTimes;
100 int my_pid = getpid();
103 apr_pollfd_t *pollset;
105 TID server_maint_tid;
108 /* Stop Ctrl-C/Ctrl-Break signals going to child processes */
109 DosSetSignalExceptionFocus(0, &ulTimes);
112 /* Create pool for child */
113 apr_pool_create(&pchild, pconf);
115 ap_run_child_init(pchild, ap_server_conf);
117 /* Create an event semaphore used to trigger other threads to shutdown */
118 rc = DosCreateEventSem(NULL, &shutdown_event, 0, FALSE);
121 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
122 "unable to create shutdown semaphore, exiting");
123 clean_child_exit(APEXIT_CHILDFATAL);
126 /* Gain access to the scoreboard. */
127 rc = DosGetNamedSharedMem(&sb_mem, ap_scoreboard_fname,
131 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
132 "scoreboard not readable in child, exiting");
133 clean_child_exit(APEXIT_CHILDFATAL);
136 ap_calc_scoreboard_size();
137 ap_init_scoreboard(sb_mem);
139 /* Gain access to the accpet mutex */
140 rc = DosOpenMutexSem(NULL, &ap_mpm_accept_mutex);
143 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
144 "accept mutex couldn't be accessed in child, exiting");
145 clean_child_exit(APEXIT_CHILDFATAL);
148 /* Find our pid in the scoreboard so we know what slot our parent allocated us */
149 for (child_slot = 0; ap_scoreboard_image->parent[child_slot].pid != my_pid && child_slot < HARD_SERVER_LIMIT; child_slot++);
151 if (child_slot == HARD_SERVER_LIMIT) {
152 ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
153 "child pid not found in scoreboard, exiting");
154 clean_child_exit(APEXIT_CHILDFATAL);
157 ap_my_generation = ap_scoreboard_image->parent[child_slot].generation;
158 memset(ap_scoreboard_image->servers[child_slot], 0, sizeof(worker_score) * HARD_THREAD_LIMIT);
160 /* Set up an OS/2 queue for passing connections & termination requests
163 rc = DosCreateQueue(&workq, QUE_FIFO, apr_psprintf(pchild, "/queues/httpd/work.%d", my_pid));
166 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
167 "unable to create work queue, exiting");
168 clean_child_exit(APEXIT_CHILDFATAL);
171 /* Create initial pool of worker threads */
172 for (c = 0; c < ap_min_spare_threads; c++) {
173 // ap_scoreboard_image->servers[child_slot][c].tid = _beginthread(worker_main, NULL, 128*1024, (void *)c);
176 /* Start maintenance thread */
177 server_maint_tid = _beginthread(server_maintenance, NULL, 32768, NULL);
180 for (num_listeners = 0, lr = ap_listeners; lr; lr = lr->next) {
184 apr_poll_setup(&pollset, num_listeners, pchild);
186 for (lr = ap_listeners; lr; lr = lr->next) {
187 apr_poll_socket_add(pollset, lr->sd, APR_POLLIN);
190 /* Main connection accept loop */
193 worker_args_t *worker_args;
195 apr_pool_create(&pconn, pchild);
196 worker_args = apr_palloc(pconn, sizeof(worker_args_t));
197 worker_args->pconn = pconn;
199 if (num_listeners == 1) {
200 rv = apr_accept(&worker_args->conn_sd, ap_listeners->sd, pconn);
202 rc = DosRequestMutexSem(ap_mpm_accept_mutex, SEM_INDEFINITE_WAIT);
204 if (shutdown_pending) {
205 DosReleaseMutexSem(ap_mpm_accept_mutex);
209 rv = APR_FROM_OS_ERROR(rc);
211 if (rv == APR_SUCCESS) {
212 rv = apr_poll(pollset, num_listeners, &nsds, -1);
213 DosReleaseMutexSem(ap_mpm_accept_mutex);
216 if (rv == APR_SUCCESS) {
217 if (first_lr == NULL) {
218 first_lr = ap_listeners;
226 apr_poll_revents_get(&event, lr->sd, pollset);
228 if (event == APR_POLLIN) {
231 apr_socket_addr_get(&sa, APR_LOCAL, lr->sd);
232 apr_sockaddr_port_get(&port, sa);
241 } while (lr != first_lr);
243 if (lr == first_lr) {
248 rv = apr_accept(&worker_args->conn_sd, sd, pconn);
252 if (rv != APR_SUCCESS) {
253 if (!APR_STATUS_IS_EINTR(rv)) {
254 ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf,
256 clean_child_exit(APEXIT_CHILDFATAL);
259 DosWriteQueue(workq, WORKTYPE_CONN, sizeof(worker_args_t), worker_args, 0);
260 requests_this_child++;
263 if (ap_max_requests_per_child != 0 && requests_this_child >= ap_max_requests_per_child)
265 } while (!shutdown_pending && ap_my_generation == ap_scoreboard_image->global->running_generation);
267 ap_scoreboard_image->parent[child_slot].quiescing = 1;
268 DosPostEventSem(shutdown_event);
269 DosWaitThread(&server_maint_tid, DCWW_WAIT);
274 /* tell our worker threads to exit */
275 for (c=0; c<HARD_THREAD_LIMIT; c++) {
276 if (ap_scoreboard_image->servers[child_slot][c].status != SERVER_DEAD) {
277 DosWriteQueue(workq, WORKTYPE_EXIT, 0, NULL, 0);
284 for (c=0; c<HARD_THREAD_LIMIT; c++) {
285 if (ap_scoreboard_image->servers[child_slot][c].status != SERVER_DEAD) {
293 DosPurgeQueue(workq);
295 for (c=0; c<HARD_THREAD_LIMIT; c++) {
296 if (ap_scoreboard_image->servers[child_slot][c].status != SERVER_DEAD) {
297 DosKillThread(ap_scoreboard_image->servers[child_slot][c].tid);
302 apr_pool_destroy(pchild);
311 /* Find a free thread slot */
312 for (thread_slot=0; thread_slot < HARD_THREAD_LIMIT; thread_slot++) {
313 if (ap_scoreboard_image->servers[child_slot][thread_slot].status == SERVER_DEAD) {
314 ap_scoreboard_image->servers[child_slot][thread_slot].status = SERVER_STARTING;
315 ap_scoreboard_image->servers[child_slot][thread_slot].tid =
316 _beginthread(worker_main, NULL, 128*1024, (void *)thread_slot);
324 ULONG APIENTRY thread_exception_handler(EXCEPTIONREPORTRECORD *pReportRec,
325 EXCEPTIONREGISTRATIONRECORD *pRegRec,
326 CONTEXTRECORD *pContext,
331 if (pReportRec->fHandlerFlags & EH_NESTED_CALL) {
332 return XCPT_CONTINUE_SEARCH;
335 if (pReportRec->ExceptionNum == XCPT_ACCESS_VIOLATION ||
336 pReportRec->ExceptionNum == XCPT_INTEGER_DIVIDE_BY_ZERO) {
337 ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
338 "caught exception in worker thread, initiating child shutdown pid=%d", getpid());
339 for (c=0; c<HARD_THREAD_LIMIT; c++) {
340 if (ap_scoreboard_image->servers[child_slot][c].tid == _gettid()) {
341 ap_scoreboard_image->servers[child_slot][c].status = SERVER_DEAD;
346 /* Shut down process ASAP, it could be quite unhealthy & leaking resources */
347 shutdown_pending = 1;
348 ap_scoreboard_image->parent[child_slot].quiescing = 1;
349 kill(getpid(), SIGHUP);
350 DosUnwindException(UNWIND_ALL, 0, 0);
353 return XCPT_CONTINUE_SEARCH;
358 static void worker_main(void *vpArg)
361 conn_rec *current_conn;
363 apr_allocator_t *allocator;
364 apr_bucket_alloc_t *bucket_alloc;
365 worker_args_t *worker_args;
372 int thread_slot = (int)vpArg;
373 EXCEPTIONREGISTRATIONRECORD reg_rec = { NULL, thread_exception_handler };
376 /* Trap exceptions in this thread so we don't take down the whole process */
377 DosSetExceptionHandler( ®_rec );
379 rc = DosOpenQueue(&owner, &workq,
380 apr_psprintf(pchild, "/queues/httpd/work.%d", getpid()));
383 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
384 "unable to open work queue, exiting");
385 ap_scoreboard_image->servers[child_slot][thread_slot].tid = 0;
388 conn_id = ID_FROM_CHILD_THREAD(child_slot, thread_slot);
389 ap_update_child_status_from_indexes(child_slot, thread_slot, SERVER_READY,
392 apr_allocator_create(&allocator);
393 apr_allocator_max_free_set(allocator, ap_max_mem_free);
394 bucket_alloc = apr_bucket_alloc_create_ex(allocator);
396 while (rc = DosReadQueue(workq, &rd, &len, (PPVOID)&worker_args, 0, DCWW_WAIT, &priority, NULLHANDLE),
397 rc == 0 && rd.ulData != WORKTYPE_EXIT) {
398 pconn = worker_args->pconn;
399 ap_create_sb_handle(&sbh, pconn, child_slot, thread_slot);
400 current_conn = ap_run_create_connection(pconn, ap_server_conf,
401 worker_args->conn_sd, conn_id,
405 ap_process_connection(current_conn, worker_args->conn_sd);
406 ap_lingering_close(current_conn);
409 apr_pool_destroy(pconn);
410 ap_update_child_status_from_indexes(child_slot, thread_slot,
414 ap_update_child_status_from_indexes(child_slot, thread_slot, SERVER_DEAD,
417 apr_bucket_alloc_destroy(bucket_alloc);
418 apr_allocator_destroy(allocator);
423 static void server_maintenance(void *vpArg)
425 int num_idle, num_needed;
426 ULONG num_pending = 0;
432 rc = DosOpenQueue(&owner, &workq,
433 apr_psprintf(pchild, "/queues/httpd/work.%d", getpid()));
436 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(rc), ap_server_conf,
437 "unable to open work queue in maintenance thread");
442 for (num_idle=0, threadnum=0; threadnum < HARD_THREAD_LIMIT; threadnum++) {
443 num_idle += ap_scoreboard_image->servers[child_slot][threadnum].status == SERVER_READY;
446 DosQueryQueue(workq, &num_pending);
447 num_needed = ap_min_spare_threads - num_idle + num_pending;
449 if (num_needed > 0) {
450 for (threadnum=0; threadnum < num_needed; threadnum++) {
455 if (num_idle - num_pending > ap_max_spare_threads) {
456 DosWriteQueue(workq, WORKTYPE_EXIT, 0, NULL, 0);
458 } while (DosWaitEventSem(shutdown_event, 500) == ERROR_TIMEOUT);
463 /* Signal handling routines */
465 static void sig_term(int sig)
467 shutdown_pending = 1;
469 signal(SIGTERM, SIG_DFL);
474 static void sig_hup(int sig)
476 shutdown_pending = 1;
482 static void set_signals()
486 sigemptyset(&sa.sa_mask);
488 sa.sa_handler = sig_term;
490 if (sigaction(SIGTERM, &sa, NULL) < 0)
491 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
493 sa.sa_handler = sig_hup;
495 if (sigaction(SIGHUP, &sa, NULL) < 0)
496 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");