2 * $Id: fcgi_pm.c,v 1.54 2001/05/03 22:03:53 robs Exp $
10 #define seteuid(arg) setresuid(-1, (arg), -1)
13 int fcgi_dynamic_total_proc_count
= 0; /* number of running apps */
14 time_t fcgi_dynamic_epoch
= 0; /* last time kill_procs was
15 * invoked by process mgr */
16 time_t fcgi_dynamic_last_analyzed
= 0; /* last time calculation was
17 * made for the dynamic procs */
19 static time_t now
= 0;
22 #pragma warning ( disable : 4100 4102 )
23 static BOOL bTimeToDie
= FALSE
; /* process termination flag */
24 HANDLE fcgi_event_handles
[3];
32 static int seteuid_root(void)
34 int rc
= seteuid((uid_t
)0);
36 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
37 "FastCGI: seteuid(0) failed");
42 static int seteuid_user(void)
44 int rc
= seteuid(ap_user_id
);
46 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
47 "FastCGI: seteuid(%u) failed", (unsigned)ap_user_id
);
54 * Signal the process to exit. How (or if) the process responds
55 * depends on the FastCGI application library (esp. on Win32) and
56 * possibly application code (signal handlers and whether or not
57 * SA_RESTART is on). At any rate, we send the signal with the
58 * hopes that the process will exit on its own. Later, as we
59 * review the state of application processes, if we see one marked
60 * for death, but that hasn't died within a specified period of
61 * time, fcgi_kill() is called again with a KILL)
63 static void fcgi_kill(ServerProcess
*process
, int sig
)
65 FCGIDBG3("fcgi_kill(%ld, %d)", process
->pid
, sig
);
67 process
->state
= FCGI_VICTIM_STATE
;
72 SetEvent(process
->terminationEvent
);
74 else if (sig
== SIGKILL
)
76 TerminateProcess(process
->handle
, 1);
88 kill(process
->pid
, sig
);
97 /*******************************************************************************
98 * Send SIGTERM to each process in the server class, remove socket
99 * file if appropriate. Currently this is only called when the PM is shutting
100 * down and thus memory isn't freed and sockets and files aren't closed.
102 static void shutdown_all()
104 fcgi_server
*s
= fcgi_servers
;
108 ServerProcess
*proc
= s
->procs
;
110 int numChildren
= (s
->directive
== APP_CLASS_DYNAMIC
)
111 ? dynamicMaxClassProcs
114 /* Remove the socket file */
115 if (s
->socket_path
!= NULL
&& s
->directive
!= APP_CLASS_EXTERNAL
) {
117 if (unlink(s
->socket_path
) != 0) {
118 ap_log_error(FCGI_LOG_ERR
, fcgi_apache_main_server
,
119 "FastCGI: unlink() failed to remove socket file \"%s\" for%s server \"%s\"",
121 (s
->directive
== APP_CLASS_DYNAMIC
) ? " (dynamic)" : "", s
->fs_path
);
124 CloseHandle((HANDLE
)s
->listenFd
);
128 /* Send TERM to all processes */
129 for (i
= 0; i
< numChildren
; i
++, proc
++)
131 if (proc
->state
== FCGI_RUNNING_STATE
)
133 fcgi_kill(proc
, SIGTERM
);
137 fcgi_servers
= s
->next
;
141 static int init_listen_sock(fcgi_server
* fs
)
143 ap_assert(fs
->directive
!= APP_CLASS_EXTERNAL
);
145 /* Create the socket */
146 if ((fs
->listenFd
= ap_psocket(fcgi_config_pool
, fs
->socket_addr
->sa_family
, SOCK_STREAM
, 0)) < 0)
149 errno
= WSAGetLastError(); // Not sure if this will work as expected
151 ap_log_error(FCGI_LOG_CRIT_ERRNO
, fcgi_apache_main_server
,
152 "FastCGI: can't create %sserver \"%s\": socket() failed",
153 (fs
->directive
== APP_CLASS_DYNAMIC
) ? "(dynamic) " : "",
159 if (fs
->socket_addr
->sa_family
== AF_UNIX
)
161 /* Remove any existing socket file.. just in case */
162 unlink(((struct sockaddr_un
*)fs
->socket_addr
)->sun_path
);
168 setsockopt(fs
->listenFd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&flag
, sizeof(flag
));
171 /* Bind it to the socket_addr */
172 if (bind(fs
->listenFd
, fs
->socket_addr
, fs
->socket_addr_len
))
177 errno
= WSAGetLastError();
179 ap_snprintf(port
, sizeof(port
), "port=%d",
180 ((struct sockaddr_in
*)fs
->socket_addr
)->sin_port
);
182 ap_log_error(FCGI_LOG_CRIT_ERRNO
, fcgi_apache_main_server
,
183 "FastCGI: can't create %sserver \"%s\": bind() failed [%s]",
184 (fs
->directive
== APP_CLASS_DYNAMIC
) ? "(dynamic) " : "",
187 (fs
->socket_addr
->sa_family
== AF_UNIX
) ?
188 ((struct sockaddr_un
*)fs
->socket_addr
)->sun_path
:
194 /* Twiddle Unix socket permissions */
195 else if (fs
->socket_addr
->sa_family
== AF_UNIX
196 && chmod(((struct sockaddr_un
*)fs
->socket_addr
)->sun_path
, S_IRUSR
| S_IWUSR
))
198 ap_log_error(FCGI_LOG_CRIT
, fcgi_apache_main_server
,
199 "FastCGI: can't create %sserver \"%s\": chmod() of socket failed",
200 (fs
->directive
== APP_CLASS_DYNAMIC
) ? "(dynamic) " : "",
206 else if (listen(fs
->listenFd
, fs
->listenQueueDepth
))
209 errno
= WSAGetLastError();
211 ap_log_error(FCGI_LOG_CRIT_ERRNO
, fcgi_apache_main_server
,
212 "FastCGI: can't create %sserver \"%s\": listen() failed",
213 (fs
->directive
== APP_CLASS_DYNAMIC
) ? "(dynamic) " : "",
221 ap_pclosesocket(fcgi_config_pool
, fs
->listenFd
);
227 *----------------------------------------------------------------------
231 * The FastCGI process manager, which runs as a separate
232 * process responsible for:
233 * - Starting all the FastCGI proceses.
234 * - Restarting any of these processes that die (indicated
236 * - Catching SIGTERM and relaying it to all the FastCGI
237 * processes before exiting.
240 * Uses global variable fcgi_servers.
248 *----------------------------------------------------------------------
251 static int caughtSigTerm
= FALSE
;
252 static int caughtSigChld
= FALSE
;
253 static int caughtSigUsr2
= FALSE
;
255 static void signal_handler(int signo
)
257 if ((signo
== SIGTERM
) || (signo
== SIGUSR1
) || (signo
== SIGHUP
)) {
258 /* SIGUSR1 & SIGHUP are sent by apache to its process group
259 * when apache get 'em. Apache follows up (1.2.x) with attacks
260 * on each of its child processes, but we've got the KillMgr
261 * sitting between us so we never see the KILL. The main loop
262 * in ProcMgr also checks to see if the KillMgr has terminated,
263 * and if it has, we handl it as if we should shutdown too. */
264 caughtSigTerm
= TRUE
;
265 } else if(signo
== SIGCHLD
) {
266 caughtSigChld
= TRUE
;
267 } else if(signo
== SIGALRM
) {
268 caughtSigUsr2
= TRUE
;
274 *----------------------------------------------------------------------
276 * spawn_fs_process --
278 * Fork and exec the specified fcgi process.
281 * 0 for successful fork, -1 for failed fork.
283 * In case the child fails before or in the exec, the child
284 * obtains the error log by calling getErrLog, logs
285 * the error, and exits with exit status = errno of
286 * the failed system call.
289 * Child process created.
291 *----------------------------------------------------------------------
294 static pid_t
spawn_fs_process(fcgi_server
*fs
, ServerProcess
*process
)
300 char *dnEnd
, *failedSysCall
;
307 /* We're the child. We're gonna exec() so pools don't matter. */
309 dnEnd
= strrchr(fs
->fs_path
, '/');
313 dirName
= ap_pcalloc(fcgi_config_pool
, dnEnd
- fs
->fs_path
+ 1);
314 dirName
= memcpy(dirName
, fs
->fs_path
, dnEnd
- fs
->fs_path
);
316 if (chdir(dirName
) < 0) {
317 failedSysCall
= "chdir()";
318 goto FailedSystemCallExit
;
322 /* OS/2 dosen't support nice() */
323 if (fs
->processPriority
!= 0) {
324 if (nice(fs
->processPriority
) == -1) {
325 failedSysCall
= "nice()";
326 goto FailedSystemCallExit
;
331 /* Open the listenFd on spec'd fd */
332 if (fs
->listenFd
!= FCGI_LISTENSOCK_FILENO
)
333 dup2(fs
->listenFd
, FCGI_LISTENSOCK_FILENO
);
335 /* Close all other open fds, except stdout/stderr. Leave these two open so
336 * FastCGI applications don't have to find and fix ALL 3rd party libs that
337 * write to stdout/stderr inadvertantly. For now, just leave 'em open to the
338 * main server error_log - @@@ provide a directive control where this goes.
340 ap_error_log2stderr(fcgi_apache_main_server
);
341 dup2(STDERR_FILENO
, STDOUT_FILENO
);
342 for (i
= 0; i
< FCGI_MAX_FD
; i
++) {
343 if (i
!= FCGI_LISTENSOCK_FILENO
&& i
!= STDERR_FILENO
&& i
!= STDOUT_FILENO
) {
348 /* Ignore SIGPIPE by default rather than terminate. The fs SHOULD
349 * install its own handler. */
350 signal(SIGPIPE
, SIG_IGN
);
352 if (fcgi_wrapper
&& (fcgi_user_id
!= fs
->uid
|| fcgi_group_id
!= fs
->gid
)) {
353 char *shortName
= strrchr(fs
->fs_path
, '/') + 1;
355 /* Relinquish our root real uid powers */
360 execle(fcgi_wrapper
, fcgi_wrapper
, fs
->username
, fs
->group
, shortName
, NULL
, fs
->envp
);
361 } while (errno
== EINTR
);
365 execle(fs
->fs_path
, fs
->fs_path
, NULL
, fs
->envp
);
366 } while (errno
== EINTR
);
369 failedSysCall
= "execle()";
371 FailedSystemCallExit
:
372 fprintf(stderr
, "FastCGI: can't start server \"%s\" (pid %ld), %s failed: %s\n",
373 fs
->fs_path
, (long) getpid(), failedSysCall
, strerror(errno
));
376 /* avoid an irrelevant compiler warning */
381 /* Adapted from Apache's util_script.c ap_call_exec() */
382 char *interpreter
= NULL
;
383 char *quoted_filename
;
385 char *pEnvBlock
, *pNext
;
388 int iEnvBlockLen
= 1;
390 file_type_e fileType
;
393 PROCESS_INFORMATION pi
;
398 pool
* tp
= ap_make_sub_pool(fcgi_config_pool
);
400 HANDLE listen_handle
;
401 char * termination_env_string
= NULL
;
403 process
->terminationEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
404 SetHandleInformation(process
->terminationEvent
, HANDLE_FLAG_INHERIT
, TRUE
);
406 termination_env_string
= ap_psprintf(tp
,
407 "_FCGI_SHUTDOWN_EVENT_=%ld", process
->terminationEvent
);
411 SECURITY_ATTRIBUTES sa
;
413 sa
.lpSecurityDescriptor
= NULL
;
414 sa
.bInheritHandle
= TRUE
;
415 sa
.nLength
= sizeof(sa
);
417 listen_handle
= CreateNamedPipe(fs
->socket_path
,
418 PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
419 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
| PIPE_WAIT
,
420 PIPE_UNLIMITED_INSTANCES
, 4096, 4096, 0, &sa
);
422 if (listen_handle
== INVALID_HANDLE_VALUE
)
424 ap_log_error(FCGI_LOG_CRIT
, fcgi_apache_main_server
,
425 "FastCGI: can't exec server \"%s\", CreateNamedPipe() failed", fs
->fs_path
);
429 // SetHandleInformation(listen_handle, HANDLE_FLAG_INHERIT, TRUE);
433 listen_handle
= (HANDLE
) fs
->listenFd
;
436 memset(&si
, 0, sizeof(si
));
437 memset(&pi
, 0, sizeof(pi
));
438 memset(&r
, 0, sizeof(r
));
440 // Can up a fake request to pass to ap_get_win32_interpreter()
441 r
.per_dir_config
= fcgi_apache_main_server
->lookup_defaults
;
442 r
.server
= fcgi_apache_main_server
;
443 r
.filename
= (char *) fs
->fs_path
;
446 fileType
= ap_get_win32_interpreter(&r
, &interpreter
);
448 if (fileType
== eFileTypeUNKNOWN
) {
449 ap_log_error(FCGI_LOG_ERR_NOERRNO
, fcgi_apache_main_server
,
450 "FastCGI: %s is not executable; ensure interpreted scripts have "
451 "\"#!\" as their first line",
458 * We have the interpreter (if there is one) and we have
459 * the arguments (if there are any).
460 * Build the command string to pass to CreateProcess.
462 quoted_filename
= ap_pstrcat(tp
, "\"", fs
->fs_path
, "\"", NULL
);
463 if (interpreter
&& *interpreter
) {
464 pCommand
= ap_pstrcat(tp
, interpreter
, " ", quoted_filename
, NULL
);
467 pCommand
= quoted_filename
;
471 * Make child process use hPipeOutputWrite as standard out,
472 * and make sure it does not show on screen.
475 si
.dwFlags
= STARTF_USESHOWWINDOW
| STARTF_USESTDHANDLES
;
476 si
.wShowWindow
= SW_HIDE
;
477 si
.hStdInput
= listen_handle
;
479 // XXX These should be open to the error_log
480 si
.hStdOutput
= INVALID_HANDLE_VALUE
;
481 si
.hStdError
= INVALID_HANDLE_VALUE
;
484 * Win32's CreateProcess call requires that the environment
485 * be passed in an environment block, a null terminated block of
486 * null terminated strings.
487 * @todo we should store the env in this format for win32.
491 iEnvBlockLen
+= strlen(fs
->envp
[i
]) + 1;
495 iEnvBlockLen
+= strlen(termination_env_string
) + 1;
496 iEnvBlockLen
+= strlen(fs
->mutex_env_string
) + 1;
498 pEnvBlock
= (char *) ap_pcalloc(tp
, iEnvBlockLen
);
504 strcpy(pNext
, fs
->envp
[i
]);
505 pNext
+= strlen(pNext
) + 1;
509 strcpy(pNext
, termination_env_string
);
510 pNext
+= strlen(pNext
) + 1;
511 strcpy(pNext
, fs
->mutex_env_string
);
513 if (CreateProcess(NULL
, pCommand
, NULL
, NULL
, TRUE
,
516 ap_make_dirstr_parent(tp
, fs
->fs_path
),
519 /* Hack to get 16-bit CGI's working. It works for all the
520 * standard modules shipped with Apache. pi.dwProcessId is 0
521 * for 16-bit CGIs and all the Unix specific code that calls
522 * ap_call_exec interprets this as a failure case. And we can't
523 * use -1 either because it is mapped to 0 by the caller.
525 pid
= (fileType
== eFileTypeEXE16
) ? -2 : pi
.dwProcessId
;
527 process
->handle
= pi
.hProcess
;
528 CloseHandle(pi
.hThread
);
533 CloseHandle(listen_handle
);
544 static void reduce_privileges(void)
552 /* Get username if passed as a uid */
553 if (ap_user_name
[0] == '#') {
554 uid_t uid
= atoi(&ap_user_name
[1]);
555 struct passwd
*ent
= getpwuid(uid
);
558 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
559 "FastCGI: process manager exiting, getpwuid(%u) couldn't determine user name, "
560 "you probably need to modify the User directive", (unsigned)uid
);
569 if (setgid(ap_group_id
) == -1) {
570 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
571 "FastCGI: process manager exiting, setgid(%u) failed", (unsigned)ap_group_id
);
575 /* See Apache PR2580. Until its resolved, do it the same way CGI is done.. */
577 /* Initialize supplementary groups */
578 if (initgroups(name
, ap_group_id
) == -1) {
579 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
580 "FastCGI: process manager exiting, initgroups(%s,%u) failed",
581 name
, (unsigned)ap_group_id
);
588 if (seteuid_user() == -1) {
589 ap_log_error(FCGI_LOG_ALERT_NOERRNO
, fcgi_apache_main_server
,
590 "FastCGI: process manager exiting, failed to reduce privileges");
595 if (setuid(ap_user_id
) == -1) {
596 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
597 "FastCGI: process manager exiting, setuid(%u) failed", (unsigned)ap_user_id
);
604 * Change the name of this process - best we can easily.
606 static void change_process_name(const char * const name
)
608 strncpy(ap_server_argv0
, name
, strlen(ap_server_argv0
));
612 static void schedule_start(fcgi_server
*s
, int proc
)
614 /* If we've started one recently, don't register another */
615 time_t time_passed
= now
- s
->restartTime
;
617 if ((s
->procs
[proc
].pid
&& (time_passed
< (int) s
->restartDelay
))
618 || ((s
->procs
[proc
].pid
== 0) && (time_passed
< (int) s
->initStartDelay
)))
620 FCGIDBG6("ignore_job: slot=%d, pid=%ld, time_passed=%ld, initStartDelay=%ld, restartDelay=%ld", proc
, s
->procs
[proc
].pid
, time_passed
, s
->initStartDelay
, s
->restartDelay
);
624 FCGIDBG3("scheduling_start: %s (%d)", s
->fs_path
, proc
);
625 s
->procs
[proc
].state
= FCGI_START_STATE
;
626 if (proc
== (int)dynamicMaxClassProcs
- 1) {
627 ap_log_error(FCGI_LOG_WARN_NOERRNO
, fcgi_apache_main_server
,
628 "FastCGI: scheduled the %sstart of the last (dynamic) server "
629 "\"%s\" process: reached dynamicMaxClassProcs (%d)",
630 s
->procs
[proc
].pid
? "re" : "", s
->fs_path
, dynamicMaxClassProcs
);
635 *----------------------------------------------------------------------
639 * Removes the records written by request handlers and decodes them.
640 * We also update the data structures to reflect the changes.
642 *----------------------------------------------------------------------
645 static void dynamic_read_msgs(int read_ready
)
651 static int buflen
= 0;
652 static char buf
[FCGI_MSGS_BUFSIZE
+ 1];
653 char *ptr1
, *ptr2
, opcode
;
654 char execName
[FCGI_MAXPATH
+ 1];
655 char user
[MAX_USER_NAME_LEN
+ 2];
656 char group
[MAX_GID_CHAR_LEN
+ 1];
657 unsigned long q_usec
= 0UL, req_usec
= 0UL;
659 fcgi_pm_job
*joblist
= NULL
;
660 fcgi_pm_job
*cjob
= NULL
;
666 user
[MAX_USER_NAME_LEN
+ 1] = group
[MAX_GID_CHAR_LEN
] = '\0';
670 * To prevent the idle application from running indefinitely, we
671 * check the timer and if it is expired, we recompute the values
672 * for each running application class. Then, when REQ_COMPLETE
673 * message is received, only updates are made to the data structures.
675 if (fcgi_dynamic_last_analyzed
== 0) {
676 fcgi_dynamic_last_analyzed
= now
;
678 if ((now
- fcgi_dynamic_last_analyzed
) >= (int)dynamicUpdateInterval
) {
679 for (s
= fcgi_servers
; s
!= NULL
; s
= s
->next
) {
680 if (s
->directive
!= APP_CLASS_DYNAMIC
)
683 /* Advance the last analyzed timestamp by the elapsed time since
684 * it was last set. Round the increase down to the nearest
685 * multiple of dynamicUpdateInterval */
687 fcgi_dynamic_last_analyzed
+= (((long)(now
-fcgi_dynamic_last_analyzed
)/dynamicUpdateInterval
)*dynamicUpdateInterval
);
688 s
->smoothConnTime
= (unsigned long) ((1.0-dynamicGain
)*s
->smoothConnTime
+ dynamicGain
*s
->totalConnTime
);
689 s
->totalConnTime
= 0UL;
690 s
->totalQueueTime
= 0UL;
694 if (read_ready
<= 0) {
699 rc
= read(fcgi_pm_pipe
[0], (void *)(buf
+ buflen
), FCGI_MSGS_BUFSIZE
- buflen
);
701 if (!caughtSigTerm
) {
702 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
703 "FastCGI: read() from pipe failed (%d)", rc
);
705 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
706 "FastCGI: the PM is shutting down, Apache seems to have disappeared - bye");
707 caughtSigTerm
= TRUE
;
715 if (ap_acquire_mutex(fcgi_dynamic_mbox_mutex
) != MULTI_OK
) {
716 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
717 "FastCGI: Failed to aquire the dynamic mbox mutex!");
720 joblist
= fcgi_dynamic_mbox
;
721 fcgi_dynamic_mbox
= NULL
;
723 if (! ap_release_mutex(fcgi_dynamic_mbox_mutex
)) {
724 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
725 "FastCGI: Failed to release the dynamic mbox mutex!");
731 tp
= ap_make_sub_pool(fcgi_config_pool
);
734 for (ptr1
= buf
; ptr1
; ptr1
= ptr2
) {
737 ptr2
= strchr(ptr1
, '*');
749 case FCGI_SERVER_START_JOB
:
750 case FCGI_SERVER_RESTART_JOB
:
752 if (sscanf(ptr1
, "%c %s %16s %15s",
753 &opcode
, execName
, user
, group
) != 4)
759 case FCGI_REQUEST_TIMEOUT_JOB
:
761 if (sscanf(ptr1
, "%c %s %16s %15s",
762 &opcode
, execName
, user
, group
) != 4)
770 if (sscanf(ptr1
, "%c %s %16s %15s %lu %lu",
771 &opcode
, execName
, user
, group
, &q_usec
, &req_usec
) != 6)
784 ap_log_error(FCGI_LOG_ERR_NOERRNO
, fcgi_apache_main_server
,
785 "FastCGI: bogus message, sscanf() failed: \"%s\"", ptr1
);
789 /* Update data structures for processing */
790 while (cjob
!= NULL
) {
791 joblist
= cjob
->next
;
792 FCGIDBG7("read_job: %c %s %s %s %lu %lu", cjob
->id
, cjob
->fs_path
, cjob
->user
, cjob
->group
, cjob
->qsec
, cjob
->start_time
);
796 s
= fcgi_util_fs_get(execName
, user
, group
);
798 s
= fcgi_util_fs_get(cjob
->fs_path
, cjob
->user
, cjob
->group
);
802 if (s
==NULL
&& opcode
!= FCGI_REQUEST_COMPLETE_JOB
)
804 if (s
==NULL
&& cjob
->id
!= FCGI_REQUEST_COMPLETE_JOB
)
808 HANDLE mutex
= ap_create_mutex(NULL
);
810 SetHandleInformation(mutex
, HANDLE_FLAG_INHERIT
, TRUE
);
816 /* Create a perm subpool to hold the new server data,
817 * we can destroy it if something doesn't pan out */
818 sp
= ap_make_sub_pool(fcgi_config_pool
);
820 /* Create a new "dynamic" server */
821 s
= fcgi_util_fs_new(sp
);
823 s
->directive
= APP_CLASS_DYNAMIC
;
824 s
->restartDelay
= dynamicRestartDelay
;
825 s
->listenQueueDepth
= dynamicListenQueueDepth
;
826 s
->initStartDelay
= dynamicInitStartDelay
;
827 s
->envp
= dynamicEnvp
;
830 s
->mutex_env_string
= ap_psprintf(sp
, "_FCGI_MUTEX_=%ld", mutex
);
831 s
->fs_path
= ap_pstrdup(sp
, cjob
->fs_path
);
833 s
->fs_path
= ap_pstrdup(sp
, execName
);
835 ap_getparents(s
->fs_path
);
836 ap_no2slash(s
->fs_path
);
837 s
->procs
= fcgi_util_fs_create_procs(sp
, dynamicMaxClassProcs
);
840 /* Create socket file's path */
841 s
->socket_path
= fcgi_util_socket_hash_filename(tp
, execName
, user
, group
);
842 s
->socket_path
= fcgi_util_socket_make_path_absolute(sp
, s
->socket_path
, 1);
844 /* Create sockaddr, prealloc it so it won't get created in tp */
845 s
->socket_addr
= ap_pcalloc(sp
, sizeof(struct sockaddr_un
));
846 err
= fcgi_util_socket_make_domain_addr(tp
, (struct sockaddr_un
**)&s
->socket_addr
,
847 &s
->socket_addr_len
, s
->socket_path
);
849 ap_log_error(FCGI_LOG_CRIT
, fcgi_apache_main_server
,
850 "FastCGI: can't create (dynamic) server \"%s\": %s", execName
, err
);
854 if (init_listen_sock(s
)) {
858 /* If a wrapper is being used, config user/group info */
860 if (user
[0] == '~') {
861 /* its a user dir uri, the rest is a username, not a uid */
862 struct passwd
*pw
= getpwnam(&user
[1]);
865 ap_log_error(FCGI_LOG_CRIT
, fcgi_apache_main_server
,
866 "FastCGI: can't create (dynamic) server \"%s\": can't get uid/gid for wrapper: getpwnam(%s) failed",
871 s
->user
= ap_pstrdup(sp
, user
);
872 s
->username
= s
->user
;
875 s
->group
= ap_psprintf(sp
, "%ld", (long)s
->gid
);
880 s
->uid
= (uid_t
)atol(user
);
881 pw
= getpwuid(s
->uid
);
883 ap_log_error(FCGI_LOG_CRIT
, fcgi_apache_main_server
,
884 "FastCGI: can't create (dynamic) server \"%s\": can't get uid/gid for wrapper: getwpuid(%ld) failed",
885 execName
, (long)s
->uid
);
888 s
->user
= ap_pstrdup(sp
, user
);
889 s
->username
= ap_pstrdup(sp
, pw
->pw_name
);
891 s
->gid
= (gid_t
)atol(group
);
892 s
->group
= ap_pstrdup(sp
, group
);
896 /* Create socket file's path */
897 s
->socket_path
= fcgi_util_socket_hash_filename(tp
, cjob
->fs_path
, cjob
->user
, cjob
->group
);
898 s
->socket_path
= fcgi_util_socket_make_path_absolute(sp
, s
->socket_path
, 1);
906 if (opcode
== FCGI_SERVER_RESTART_JOB
) {
908 if (cjob
->id
==FCGI_SERVER_RESTART_JOB
) {
910 /* Check to see if the binary has changed. If so,
911 * kill the FCGI application processes, and
918 if ((stat(execName
, &stbuf
)==0) &&
920 if ((stat(cjob
->fs_path
, &stbuf
)==0) &&
922 (stbuf
.st_mtime
> s
->restartTime
)) {
923 /* kill old server(s) */
924 for (i
= 0; i
< dynamicMaxClassProcs
; i
++) {
925 if (s
->procs
[i
].pid
> 0) {
926 fcgi_kill(&s
->procs
[i
], SIGTERM
);
930 ap_log_error(FCGI_LOG_WARN_NOERRNO
, fcgi_apache_main_server
,
931 "FastCGI: restarting server \"%s\" processes, newer version found",
939 /* If dynamicAutoRestart, don't mark any new processes
940 * for starting because we probably got the
941 * FCGI_SERVER_START_JOB due to dynamicAutoUpdate and the ProcMgr
942 * will be restarting all of those we just killed.
944 if (dynamicAutoRestart
)
948 else if (opcode
== FCGI_SERVER_START_JOB
) {
950 else if (cjob
->id
==FCGI_SERVER_START_JOB
) {
952 /* we've been asked to start a process--only start
953 * it if we're not already running at least one
958 for (i
= 0; i
< dynamicMaxClassProcs
; i
++) {
959 if (s
->procs
[i
].state
== FCGI_RUNNING_STATE
)
962 /* if already running, don't start another one */
963 if (i
< dynamicMaxClassProcs
) {
977 case FCGI_SERVER_RESTART_JOB
:
981 /* We just waxed 'em all. Try to find an idle slot. */
983 for (i
= 0; i
< dynamicMaxClassProcs
; ++i
)
985 if (s
->procs
[i
].state
== FCGI_START_STATE
986 || s
->procs
[i
].state
== FCGI_RUNNING_STATE
)
990 else if (s
->procs
[i
].state
== FCGI_KILLED_STATE
991 || s
->procs
[i
].state
== FCGI_READY_STATE
)
998 /* Nope, just use the first slot */
999 if (i
== dynamicMaxClassProcs
)
1007 schedule_start(s
, i
);
1012 case FCGI_SERVER_START_JOB
:
1013 case FCGI_REQUEST_TIMEOUT_JOB
:
1015 if ((fcgi_dynamic_total_proc_count
+ 1) > (int) dynamicMaxProcs
) {
1017 * Extra instances should have been
1018 * terminated beforehand, probably need
1019 * to increase ProcessSlack parameter
1021 ap_log_error(FCGI_LOG_WARN_NOERRNO
, fcgi_apache_main_server
,
1022 "FastCGI: can't schedule the start of another (dynamic) server \"%s\" process: "
1023 "exceeded dynamicMaxProcs (%d)", s
->fs_path
, dynamicMaxProcs
);
1027 /* find next free slot */
1028 for (i
= 0; i
< dynamicMaxClassProcs
; i
++)
1030 if (s
->procs
[i
].state
== FCGI_START_STATE
)
1032 FCGIDBG2("ignore_job: slot (%d) is already scheduled for starting", i
);
1035 else if (s
->procs
[i
].state
== FCGI_RUNNING_STATE
)
1040 schedule_start(s
, i
);
1045 if (i
>= dynamicMaxClassProcs
) {
1046 FCGIDBG1("ignore_job: slots are max'd");
1050 case FCGI_REQUEST_COMPLETE_JOB
:
1051 /* only record stats if we have a structure */
1054 s
->totalConnTime
+= req_usec
;
1055 s
->totalQueueTime
+= q_usec
;
1057 s
->totalConnTime
+= cjob
->start_time
;
1058 s
->totalQueueTime
+= cjob
->qsec
;
1067 /* Cleanup job data */
1068 free(cjob
->fs_path
);
1078 ap_destroy_pool(sp
);
1081 free(cjob
->fs_path
);
1089 ap_log_error(FCGI_LOG_ERR_NOERRNO
, fcgi_apache_main_server
,
1090 "FastCGI: really bogus message: \"%s\"", ptr1
);
1091 ptr1
+= strlen(buf
);
1094 buflen
-= ptr1
- buf
;
1096 memmove(buf
, ptr1
, buflen
);
1100 ap_destroy_pool(tp
);
1104 *----------------------------------------------------------------------
1106 * dynamic_kill_idle_fs_procs
1108 * Implement a kill policy for the dynamic FastCGI applications.
1109 * We also update the data structures to reflect the changes.
1112 * Processes are marked for deletion possibly killed.
1114 *----------------------------------------------------------------------
1116 static void dynamic_kill_idle_fs_procs(void)
1121 for (s
= fcgi_servers
; s
!= NULL
; s
= s
->next
)
1124 * server's smoothed running time, or if that's 0, the current total
1126 unsigned long connTime
;
1129 * maximum number of microseconds that all of a server's running
1130 * processes together could have spent running since the last check
1132 unsigned long totalTime
;
1135 * percentage, 0-100, of totalTime that the processes actually used
1141 if (s
->directive
!= APP_CLASS_DYNAMIC
|| s
->numProcesses
== 0)
1146 connTime
= s
->smoothConnTime
? s
->smoothConnTime
: s
->totalConnTime
;
1147 totalTime
= s
->numProcesses
* (now
- fcgi_dynamic_epoch
) * 1000000 + 1;
1149 loadFactor
= 100 * connTime
/ totalTime
;
1151 if (s
->numProcesses
== 1)
1153 if (loadFactor
>= dynamicThreshold1
)
1160 int load
= s
->numProcesses
/ (s
->numProcesses
- 1) * loadFactor
;
1162 if (load
>= dynamicThresholdN
)
1169 * Run through the procs to see if we can get away w/o waxing one.
1171 for (i
= 0; i
< dynamicMaxClassProcs
; ++i
)
1173 if (s
->procs
[i
].state
== FCGI_START_STATE
)
1175 s
->procs
[i
].state
= FCGI_READY_STATE
;
1178 else if (s
->procs
[i
].state
== FCGI_VICTIM_STATE
)
1184 if (i
< dynamicMaxClassProcs
)
1189 for (i
= 0; i
< dynamicMaxClassProcs
; ++i
)
1191 if (s
->procs
[i
].state
!= FCGI_RUNNING_STATE
)
1196 ap_log_error(FCGI_LOG_WARN_NOERRNO
, fcgi_apache_main_server
,
1197 "FastCGI: (dynamic) server \"%s\" (pid %ld) termination signaled",
1198 s
->fs_path
, s
->procs
[i
].pid
);
1200 fcgi_kill(&s
->procs
[i
], SIGTERM
);
1207 * If the number of non-victims is less than or equal to
1208 * the minimum that may be running without being killed off,
1209 * don't select any more victims.
1211 if (fcgi_dynamic_total_proc_count
- victims
<= dynamicMinProcs
)
1220 // This is a little bogus, there's gotta be a better way to do this
1221 // Can we use WaitForMultipleObjects()
1222 #define FCGI_PROC_WAIT_TIME 100
1224 void child_wait_thread(void *dummy
) {
1226 DWORD dwRet
= WAIT_TIMEOUT
;
1231 while (!bTimeToDie
) {
1234 for (s
= fcgi_servers
; s
!= NULL
; s
= s
->next
) {
1235 if (s
->directive
== APP_CLASS_EXTERNAL
|| s
->listenFd
< 0) {
1238 if (s
->directive
== APP_CLASS_DYNAMIC
) {
1239 numChildren
= dynamicMaxClassProcs
;
1242 numChildren
= s
->numProcesses
;
1245 for (i
=0; i
< numChildren
; i
++) {
1246 if (s
->procs
[i
].handle
!= INVALID_HANDLE_VALUE
)
1248 DWORD exitStatus
= 0;
1250 /* timeout is currently set for 100 miliecond */
1251 /* it may need t longer or user customizable */
1252 dwRet
= WaitForSingleObject(s
->procs
[i
].handle
, FCGI_PROC_WAIT_TIME
);
1256 if (dwRet
!= WAIT_TIMEOUT
&& dwRet
!= WAIT_FAILED
) {
1257 /* a child fs has died */
1258 /* mark the child as dead */
1260 if (s
->directive
== APP_CLASS_STANDARD
) {
1261 /* restart static app */
1262 s
->procs
[i
].state
= FCGI_START_STATE
;
1267 fcgi_dynamic_total_proc_count
--;
1268 FCGIDBG2("-- fcgi_dynamic_total_proc_count=%d", fcgi_dynamic_total_proc_count
);
1270 if (s
->procs
[i
].state
== FCGI_VICTIM_STATE
) {
1271 s
->procs
[i
].state
= FCGI_KILLED_STATE
;
1274 /* dynamic app shouldn't have died or dynamicAutoUpdate killed it*/
1277 if (dynamicAutoRestart
|| (s
->numProcesses
<= 0 && dynamicThreshold1
== 0)) {
1278 s
->procs
[i
].state
= FCGI_START_STATE
;
1281 s
->procs
[i
].state
= FCGI_READY_STATE
;
1286 GetExitCodeProcess(s
->procs
[i
].handle
, &exitStatus
);
1288 ap_log_error(FCGI_LOG_WARN_NOERRNO
, fcgi_apache_main_server
,
1289 "FastCGI:%s server \"%s\" (pid %d) terminated with exit with status '%d'",
1290 (s
->directive
== APP_CLASS_DYNAMIC
) ? " (dynamic)" : "",
1291 s
->fs_path
, s
->procs
[i
].pid
, exitStatus
);
1293 CloseHandle(s
->procs
[i
].handle
);
1294 s
->procs
[i
].handle
= INVALID_HANDLE_VALUE
;
1295 s
->procs
[i
].pid
= -1;
1297 /* wake up the main thread */
1298 SetEvent(fcgi_event_handles
[WAKE_EVENT
]);
1303 Sleep(waited
? 0 : FCGI_PROC_WAIT_TIME
);
1309 static void setup_signals(void)
1312 struct sigaction sa
;
1316 sigaddset(&mask
, SIGUSR2
);
1317 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
1319 /* Setup handlers */
1321 sa
.sa_handler
= signal_handler
;
1322 sigemptyset(&sa
.sa_mask
);
1325 if (sigaction(SIGTERM
, &sa
, NULL
) < 0) {
1326 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
1327 "sigaction(SIGTERM) failed");
1330 if (sigaction(SIGHUP
, &sa
, NULL
) < 0) {
1331 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
1332 "sigaction(SIGHUP) failed");
1334 /* httpd graceful restart */
1335 if (sigaction(SIGUSR1
, &sa
, NULL
) < 0) {
1336 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
1337 "sigaction(SIGUSR1) failed");
1339 /* read messages from request handlers - kill interval expired */
1340 if (sigaction(SIGALRM
, &sa
, NULL
) < 0) {
1341 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
1342 "sigaction(SIGALRM) failed");
1344 if (sigaction(SIGCHLD
, &sa
, NULL
) < 0) {
1345 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
1346 "sigaction(SIGCHLD) failed");
1352 int fcgi_pm_main(void *dummy
, child_info
*info
)
1354 void fcgi_pm_main(void *dummy
)
1365 HANDLE wait_thread
= INVALID_HANDLE_VALUE
;
1367 int callWaitPid
, callDynamicProcs
;
1371 // Add SystemRoot to the dynamic environment
1372 char ** envp
= dynamicEnvp
;
1373 for (i
= 0; *envp
; ++i
) {
1376 fcgi_config_set_env_var(fcgi_config_pool
, dynamicEnvp
, &i
, "SystemRoot");
1379 reduce_privileges();
1381 close(fcgi_pm_pipe
[1]);
1382 change_process_name("fcgi-pm");
1386 ap_log_error(FCGI_LOG_NOTICE_NOERRNO
, fcgi_apache_main_server
,
1387 "FastCGI: wrapper mechanism enabled (wrapper: %s)", fcgi_wrapper
);
1391 /* Initialize AppClass */
1392 for (s
= fcgi_servers
; s
!= NULL
; s
= s
->next
)
1394 if (s
->directive
!= APP_CLASS_STANDARD
)
1402 for (i
= 0; i
< s
->numProcesses
; ++i
)
1403 s
->procs
[i
].state
= FCGI_START_STATE
;
1407 ap_log_error(FCGI_LOG_NOTICE_NOERRNO
, fcgi_apache_main_server
,
1408 "FastCGI: process manager initialized");
1410 ap_log_error(FCGI_LOG_NOTICE_NOERRNO
, fcgi_apache_main_server
,
1411 "FastCGI: process manager initialized (pid %ld)", (long)getpid());
1417 * Loop until SIGTERM
1420 int sleepSeconds
= min(dynamicKillInterval
, dynamicUpdateInterval
);
1427 unsigned int numChildren
;
1430 * If we came out of sigsuspend() for any reason other than
1431 * SIGALRM, pick up where we left off.
1434 sleepSeconds
= alarmLeft
;
1437 * Examine each configured AppClass for a process that needs
1438 * starting. Compute the earliest time when the start should
1439 * be attempted, starting it now if the time has passed. Also,
1440 * remember that we do NOT need to restart externally managed
1441 * FastCGI applications.
1443 for (s
= fcgi_servers
; s
!= NULL
; s
= s
->next
)
1445 if (s
->directive
== APP_CLASS_EXTERNAL
)
1448 numChildren
= (s
->directive
== APP_CLASS_DYNAMIC
)
1449 ? dynamicMaxClassProcs
1452 for (i
= 0; i
< numChildren
; ++i
)
1454 if (s
->procs
[i
].pid
<= 0 && s
->procs
[i
].state
== FCGI_START_STATE
)
1456 int restart
= (s
->procs
[i
].pid
< 0);
1457 time_t restartTime
= s
->restartTime
;
1459 restartTime
+= (restart
) ? s
->restartDelay
: s
->initStartDelay
;
1461 if (restartTime
<= now
)
1463 if (s
->listenFd
< 0 && init_listen_sock(s
))
1465 if (sleepSeconds
> s
->initStartDelay
)
1466 sleepSeconds
= s
->initStartDelay
;
1470 if (caughtSigTerm
) {
1471 goto ProcessSigTerm
;
1474 s
->procs
[i
].pid
= spawn_fs_process(s
, &s
->procs
[i
]);
1475 if (s
->procs
[i
].pid
<= 0) {
1476 ap_log_error(FCGI_LOG_CRIT
, fcgi_apache_main_server
,
1477 "FastCGI: can't start%s server \"%s\": spawn_fs_process() failed",
1478 (s
->directive
== APP_CLASS_DYNAMIC
) ? " (dynamic)" : "",
1481 sleepSeconds
= min(sleepSeconds
,
1482 max((int) s
->restartDelay
, FCGI_MIN_EXEC_RETRY_DELAY
));
1484 ap_assert(s
->procs
[i
].pid
< 0);
1488 s
->restartTime
= now
;
1490 if (s
->directive
== APP_CLASS_DYNAMIC
) {
1492 fcgi_dynamic_total_proc_count
++;
1493 FCGIDBG2("++ fcgi_dynamic_total_proc_count=%d", fcgi_dynamic_total_proc_count
);
1496 s
->procs
[i
].state
= FCGI_RUNNING_STATE
;
1502 ap_log_error(FCGI_LOG_WARN_NOERRNO
, fcgi_apache_main_server
,
1503 "FastCGI:%s server \"%s\" (uid %ld, gid %ld) %sstarted (pid %ld)",
1504 (s
->directive
== APP_CLASS_DYNAMIC
) ? " (dynamic)" : "",
1505 s
->fs_path
, (long)s
->uid
, (long)s
->gid
,
1506 restart
? "re" : "", (long)s
->procs
[i
].pid
);
1509 ap_log_error(FCGI_LOG_WARN_NOERRNO
, fcgi_apache_main_server
,
1510 "FastCGI:%s server \"%s\" %sstarted (pid %ld)",
1511 (s
->directive
== APP_CLASS_DYNAMIC
) ? " (dynamic)" : "",
1512 s
->fs_path
, restart
? "re" : "", (long)s
->procs
[i
].pid
);
1514 ap_assert(s
->procs
[i
].pid
> 0);
1516 sleepSeconds
= min(sleepSeconds
, restartTime
- now
);
1524 goto ProcessSigTerm
;
1526 if((!caughtSigChld
) && (!caughtSigUsr2
)) {
1529 alarm(sleepSeconds
);
1532 FD_SET(fcgi_pm_pipe
[0], &rfds
);
1533 read_ready
= ap_select(fcgi_pm_pipe
[0] + 1, &rfds
, NULL
, NULL
, NULL
);
1535 alarmLeft
= alarm(0);
1537 callWaitPid
= caughtSigChld
;
1538 caughtSigChld
= FALSE
;
1539 callDynamicProcs
= caughtSigUsr2
;
1540 caughtSigUsr2
= FALSE
;
1545 * Dynamic fcgi process management
1547 if((callDynamicProcs
) || (!callWaitPid
)) {
1548 dynamic_read_msgs(read_ready
);
1549 if(fcgi_dynamic_epoch
== 0) {
1550 fcgi_dynamic_epoch
= now
;
1552 if(((long)(now
-fcgi_dynamic_epoch
)>=dynamicKillInterval
) ||
1553 ((fcgi_dynamic_total_proc_count
+dynamicProcessSlack
)>=dynamicMaxProcs
)) {
1554 dynamic_kill_idle_fs_procs();
1555 fcgi_dynamic_epoch
= now
;
1563 /* We've caught SIGCHLD, so find out who it was using waitpid,
1564 * write a log message and update its data structure. */
1568 goto ProcessSigTerm
;
1570 childPid
= waitpid(-1, &waitStatus
, WNOHANG
);
1572 if (childPid
== -1 || childPid
== 0)
1575 for (s
= fcgi_servers
; s
!= NULL
; s
= s
->next
) {
1576 if (s
->directive
== APP_CLASS_EXTERNAL
)
1579 if (s
->directive
== APP_CLASS_DYNAMIC
)
1580 numChildren
= dynamicMaxClassProcs
;
1582 numChildren
= s
->numProcesses
;
1584 for (i
= 0; i
< numChildren
; i
++) {
1585 if (s
->procs
[i
].pid
== childPid
)
1590 /* TODO: print something about this unknown child */
1594 s
->procs
[i
].pid
= -1;
1596 if (s
->directive
== APP_CLASS_STANDARD
) {
1597 /* Always restart static apps */
1598 s
->procs
[i
].state
= FCGI_START_STATE
;
1603 fcgi_dynamic_total_proc_count
--;
1605 if (s
->procs
[i
].state
== FCGI_VICTIM_STATE
) {
1606 s
->procs
[i
].state
= FCGI_KILLED_STATE
;
1609 /* A dynamic app died or exited without provocation from the PM */
1612 if (dynamicAutoRestart
|| (s
->numProcesses
<= 0 && dynamicThreshold1
== 0))
1613 s
->procs
[i
].state
= FCGI_START_STATE
;
1615 s
->procs
[i
].state
= FCGI_READY_STATE
;
1619 if (WIFEXITED(waitStatus
)) {
1620 ap_log_error(FCGI_LOG_WARN_NOERRNO
, fcgi_apache_main_server
,
1621 "FastCGI:%s server \"%s\" (pid %d) terminated by calling exit with status '%d'",
1622 (s
->directive
== APP_CLASS_DYNAMIC
) ? " (dynamic)" : "",
1623 s
->fs_path
, (int)childPid
, WEXITSTATUS(waitStatus
));
1625 else if (WIFSIGNALED(waitStatus
)) {
1626 ap_log_error(FCGI_LOG_WARN_NOERRNO
, fcgi_apache_main_server
,
1627 "FastCGI:%s server \"%s\" (pid %d) terminated due to uncaught signal '%d' (%s)%s",
1628 (s
->directive
== APP_CLASS_DYNAMIC
) ? " (dynamic)" : "",
1629 s
->fs_path
, (int)childPid
, WTERMSIG(waitStatus
), SYS_SIGLIST
[WTERMSIG(waitStatus
)],
1631 WCOREDUMP(waitStatus
) ? ", a core file may have been generated" : "");
1636 else if (WIFSTOPPED(waitStatus
)) {
1637 ap_log_error(FCGI_LOG_WARN_NOERRNO
, fcgi_apache_main_server
,
1638 "FastCGI:%s server \"%s\" (pid %d) stopped due to uncaught signal '%d' (%s)",
1639 (s
->directive
== APP_CLASS_DYNAMIC
) ? " (dynamic)" : "",
1640 s
->fs_path
, (int)childPid
, WTERMSIG(waitStatus
), SYS_SIGLIST
[WTERMSIG(waitStatus
)]);
1642 } /* for (;;), waitpid() */
1645 /* Start the child wait thread */
1646 wait_thread
= CreateThread(NULL
, 0, (LPTHREAD_START_ROUTINE
)child_wait_thread
, NULL
, 0, NULL
);
1650 /* wait for an event to occur or timer expires */
1651 expire
= time(NULL
) + sleepSeconds
;
1652 dwRet
= WaitForMultipleObjects(3, (HANDLE
*) fcgi_event_handles
, FALSE
, sleepSeconds
* 1000);
1654 if (dwRet
== WAIT_FAILED
) {
1655 /* There is something seriously wrong here */
1656 ap_log_error(FCGI_LOG_CRIT
, fcgi_apache_main_server
,
1657 "FastCGI: WaitForMultipleObjects() failed on event handles -- pm is shuting down");
1661 if (dwRet
!= WAIT_TIMEOUT
) {
1665 alarmLeft
= expire
- now
;
1669 * Dynamic fcgi process management
1671 if ((dwRet
== MBOX_EVENT
) || (dwRet
== WAIT_TIMEOUT
)) {
1672 if (dwRet
== MBOX_EVENT
) {
1678 dynamic_read_msgs(read_ready
);
1680 if(fcgi_dynamic_epoch
== 0) {
1681 fcgi_dynamic_epoch
= now
;
1684 if (((long)(now
-fcgi_dynamic_epoch
) >= (int)dynamicKillInterval
) ||
1685 ((fcgi_dynamic_total_proc_count
+dynamicProcessSlack
) >= dynamicMaxProcs
)) {
1686 dynamic_kill_idle_fs_procs();
1687 fcgi_dynamic_epoch
= now
;
1691 else if (dwRet
== WAKE_EVENT
) {
1694 else if (dwRet
== TERM_EVENT
) {
1695 ap_log_error(FCGI_LOG_INFO_NOERRNO
, fcgi_apache_main_server
,
1696 "FastCGI: Termination event received process manager shutting down");
1699 dwRet
= WaitForSingleObject(wait_thread
, INFINITE
);
1700 goto ProcessSigTerm
;
1703 // Have an received an unknown event - should not happen
1704 ap_log_error(FCGI_LOG_CRIT
, fcgi_apache_main_server
,
1705 "FastCGI: WaitForMultipleobjects() return an unrecognized event");
1707 dwRet
= WaitForSingleObject(wait_thread
, INFINITE
);
1708 goto ProcessSigTerm
;
1711 } /* for (;;), the whole shoot'n match */
1715 * Kill off the children, then exit.
1727 int fcgi_pm_add_job(fcgi_pm_job
*new_job
) {
1729 if (new_job
== NULL
)
1732 if (ap_acquire_mutex(fcgi_dynamic_mbox_mutex
) != MULTI_OK
) {
1733 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
1734 "FastCGI: Failed to aquire the dynamic mbox mutex!");
1737 new_job
->next
= fcgi_dynamic_mbox
;
1738 fcgi_dynamic_mbox
= new_job
;
1740 if (! ap_release_mutex(fcgi_dynamic_mbox_mutex
)) {
1741 ap_log_error(FCGI_LOG_ALERT
, fcgi_apache_main_server
,
1742 "FastCGI: Failed to release the dynamic mbox mutex!");