Add discussion of signals.
[mod_fastcgi.git] / fcgi_pm.c
blobefe54ac9720db2577425236a8cbe9fb714eae510
1 /*
2 * $Id: fcgi_pm.c,v 1.61 2001/11/17 00:50:20 robs Exp $
3 */
6 #include "fcgi.h"
8 #ifdef _HPUX_SOURCE
9 #include <unistd.h>
10 #define seteuid(arg) setresuid(-1, (arg), -1)
11 #endif
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;
21 #ifdef WIN32
22 #pragma warning ( disable : 4100 4102 )
23 static BOOL bTimeToDie = FALSE; /* process termination flag */
24 HANDLE fcgi_event_handles[3];
25 #ifndef SIGKILL
26 #define SIGKILL 9
27 #endif
28 #endif
31 #ifndef WIN32
32 static int seteuid_root(void)
34 int rc = seteuid((uid_t)0);
35 if (rc == -1) {
36 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
37 "FastCGI: seteuid(0) failed");
39 return rc;
42 static int seteuid_user(void)
44 int rc = seteuid(ap_user_id);
45 if (rc == -1) {
46 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
47 "FastCGI: seteuid(%u) failed", (unsigned)ap_user_id);
49 return rc;
51 #endif
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;
69 #ifdef WIN32
70 if (sig == SIGTERM)
72 SetEvent(process->terminationEvent);
74 else if (sig == SIGKILL)
76 TerminateProcess(process->handle, 1);
78 else
80 ap_assert(0);
82 #else
83 if (fcgi_wrapper)
85 seteuid_root();
88 kill(process->pid, sig);
90 if (fcgi_wrapper)
92 seteuid_user();
94 #endif
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;
106 while (s)
108 ServerProcess *proc = s->procs;
109 int i;
110 int numChildren = (s->directive == APP_CLASS_DYNAMIC)
111 ? dynamicMaxClassProcs
112 : s->numProcesses;
114 #ifndef WIN32
115 if (s->socket_path != NULL && s->directive != APP_CLASS_EXTERNAL)
117 /* Remove the socket file */
118 if (unlink(s->socket_path) != 0 && errno != ENOENT) {
119 ap_log_error(FCGI_LOG_ERR, fcgi_apache_main_server,
120 "FastCGI: unlink() failed to remove socket file \"%s\" for%s server \"%s\"",
121 s->socket_path,
122 (s->directive == APP_CLASS_DYNAMIC) ? " (dynamic)" : "", s->fs_path);
125 #endif
127 /* Send TERM to all processes */
128 for (i = 0; i < numChildren; i++, proc++)
130 if (proc->state == FCGI_RUNNING_STATE)
132 fcgi_kill(proc, SIGTERM);
136 s = s->next;
140 static int init_listen_sock(fcgi_server * fs)
142 ap_assert(fs->directive != APP_CLASS_EXTERNAL);
144 /* Create the socket */
145 if ((fs->listenFd = socket(fs->socket_addr->sa_family, SOCK_STREAM, 0)) < 0)
147 #ifdef WIN32
148 errno = WSAGetLastError(); // Not sure if this will work as expected
149 #endif
150 ap_log_error(FCGI_LOG_CRIT_ERRNO, fcgi_apache_main_server,
151 "FastCGI: can't create %sserver \"%s\": socket() failed",
152 (fs->directive == APP_CLASS_DYNAMIC) ? "(dynamic) " : "",
153 fs->fs_path);
154 return -1;
157 #ifndef WIN32
158 if (fs->socket_addr->sa_family == AF_UNIX)
160 /* Remove any existing socket file.. just in case */
161 unlink(((struct sockaddr_un *)fs->socket_addr)->sun_path);
163 else
164 #endif
166 int flag = 1;
167 setsockopt(fs->listenFd, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof(flag));
170 /* Bind it to the socket_addr */
171 if (bind(fs->listenFd, fs->socket_addr, fs->socket_addr_len))
173 char port[11];
175 #ifdef WIN32
176 errno = WSAGetLastError();
177 #endif
178 ap_snprintf(port, sizeof(port), "port=%d",
179 ((struct sockaddr_in *)fs->socket_addr)->sin_port);
181 ap_log_error(FCGI_LOG_CRIT_ERRNO, fcgi_apache_main_server,
182 "FastCGI: can't create %sserver \"%s\": bind() failed [%s]",
183 (fs->directive == APP_CLASS_DYNAMIC) ? "(dynamic) " : "",
184 fs->fs_path,
185 #ifndef WIN32
186 (fs->socket_addr->sa_family == AF_UNIX) ?
187 ((struct sockaddr_un *)fs->socket_addr)->sun_path :
188 #endif
189 port);
192 #ifndef WIN32
193 /* Twiddle Unix socket permissions */
194 else if (fs->socket_addr->sa_family == AF_UNIX
195 && chmod(((struct sockaddr_un *)fs->socket_addr)->sun_path, S_IRUSR | S_IWUSR))
197 ap_log_error(FCGI_LOG_CRIT, fcgi_apache_main_server,
198 "FastCGI: can't create %sserver \"%s\": chmod() of socket failed",
199 (fs->directive == APP_CLASS_DYNAMIC) ? "(dynamic) " : "",
200 fs->fs_path);
202 #endif
204 /* Set to listen */
205 else if (listen(fs->listenFd, fs->listenQueueDepth))
207 #ifdef WIN32
208 errno = WSAGetLastError();
209 #endif
210 ap_log_error(FCGI_LOG_CRIT_ERRNO, fcgi_apache_main_server,
211 "FastCGI: can't create %sserver \"%s\": listen() failed",
212 (fs->directive == APP_CLASS_DYNAMIC) ? "(dynamic) " : "",
213 fs->fs_path);
215 else
217 return 0;
220 ap_pclosesocket(fcgi_config_pool, fs->listenFd);
221 fs->listenFd = -1;
222 return -2;
226 *----------------------------------------------------------------------
228 * pm_main
230 * The FastCGI process manager, which runs as a separate
231 * process responsible for:
232 * - Starting all the FastCGI proceses.
233 * - Restarting any of these processes that die (indicated
234 * by SIGCHLD).
235 * - Catching SIGTERM and relaying it to all the FastCGI
236 * processes before exiting.
238 * Inputs:
239 * Uses global variable fcgi_servers.
241 * Results:
242 * Does not return.
244 * Side effects:
245 * Described above.
247 *----------------------------------------------------------------------
249 #ifndef WIN32
250 static int caughtSigTerm = FALSE;
251 static int caughtSigChld = FALSE;
252 static int caughtSigAlarm = FALSE;
254 static void signal_handler(int signo)
256 if ((signo == SIGTERM) || (signo == SIGUSR1) || (signo == SIGHUP)) {
257 /* SIGUSR1 & SIGHUP are sent by apache to its process group
258 * when apache get 'em. Apache follows up (1.2.x) with attacks
259 * on each of its child processes, but we've got the KillMgr
260 * sitting between us so we never see the KILL. The main loop
261 * in ProcMgr also checks to see if the KillMgr has terminated,
262 * and if it has, we handl it as if we should shutdown too. */
263 caughtSigTerm = TRUE;
264 } else if(signo == SIGCHLD) {
265 caughtSigChld = TRUE;
266 } else if(signo == SIGALRM) {
267 caughtSigAlarm = TRUE;
270 #endif
273 *----------------------------------------------------------------------
275 * spawn_fs_process --
277 * Fork and exec the specified fcgi process.
279 * Results:
280 * 0 for successful fork, -1 for failed fork.
282 * In case the child fails before or in the exec, the child
283 * obtains the error log by calling getErrLog, logs
284 * the error, and exits with exit status = errno of
285 * the failed system call.
287 * Side effects:
288 * Child process created.
290 *----------------------------------------------------------------------
292 static pid_t spawn_fs_process(fcgi_server *fs, ServerProcess *process)
294 #ifndef WIN32
295 pid_t child_pid;
296 int i;
297 char *dirName;
298 char *dnEnd, *failedSysCall;
300 child_pid = fork();
301 if (child_pid) {
302 return child_pid;
305 /* We're the child. We're gonna exec() so pools don't matter. */
307 dnEnd = strrchr(fs->fs_path, '/');
308 if (dnEnd == NULL) {
309 dirName = "./";
310 } else {
311 dirName = ap_pcalloc(fcgi_config_pool, dnEnd - fs->fs_path + 1);
312 dirName = memcpy(dirName, fs->fs_path, dnEnd - fs->fs_path);
314 if (chdir(dirName) < 0) {
315 failedSysCall = "chdir()";
316 goto FailedSystemCallExit;
319 #ifndef __EMX__
320 /* OS/2 dosen't support nice() */
321 if (fs->processPriority != 0) {
322 if (nice(fs->processPriority) == -1) {
323 failedSysCall = "nice()";
324 goto FailedSystemCallExit;
327 #endif
329 /* Open the listenFd on spec'd fd */
330 if (fs->listenFd != FCGI_LISTENSOCK_FILENO)
331 dup2(fs->listenFd, FCGI_LISTENSOCK_FILENO);
333 /* Close all other open fds, except stdout/stderr. Leave these two open so
334 * FastCGI applications don't have to find and fix ALL 3rd party libs that
335 * write to stdout/stderr inadvertantly. For now, just leave 'em open to the
336 * main server error_log - @@@ provide a directive control where this goes.
338 ap_error_log2stderr(fcgi_apache_main_server);
339 dup2(STDERR_FILENO, STDOUT_FILENO);
340 for (i = 0; i < FCGI_MAX_FD; i++) {
341 if (i != FCGI_LISTENSOCK_FILENO && i != STDERR_FILENO && i != STDOUT_FILENO) {
342 close(i);
346 /* Ignore SIGPIPE by default rather than terminate. The fs SHOULD
347 * install its own handler. */
348 signal(SIGPIPE, SIG_IGN);
350 if (fcgi_wrapper && (fcgi_user_id != fs->uid || fcgi_group_id != fs->gid)) {
351 char *shortName = strrchr(fs->fs_path, '/') + 1;
353 /* Relinquish our root real uid powers */
354 seteuid_root();
355 setuid(ap_user_id);
357 do {
358 execle(fcgi_wrapper, fcgi_wrapper, fs->username, fs->group, shortName, NULL, fs->envp);
359 } while (errno == EINTR);
361 else {
362 do {
363 execle(fs->fs_path, fs->fs_path, NULL, fs->envp);
364 } while (errno == EINTR);
367 failedSysCall = "execle()";
369 FailedSystemCallExit:
370 fprintf(stderr, "FastCGI: can't start server \"%s\" (pid %ld), %s failed: %s\n",
371 fs->fs_path, (long) getpid(), failedSysCall, strerror(errno));
372 exit(-1);
374 /* avoid an irrelevant compiler warning */
375 return(0);
377 #else
379 /* Adapted from Apache's util_script.c ap_call_exec() */
380 char *interpreter = NULL;
381 char *quoted_filename;
382 char *pCommand;
383 char *pEnvBlock, *pNext;
385 int i = 0;
386 int iEnvBlockLen = 1;
388 file_type_e fileType;
390 STARTUPINFO si;
391 PROCESS_INFORMATION pi;
393 request_rec r;
394 pid_t pid = -1;
396 pool * tp = ap_make_sub_pool(fcgi_config_pool);
398 HANDLE listen_handle;
399 char * termination_env_string = NULL;
401 process->terminationEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
402 if (process->terminationEvent == NULL)
404 ap_log_error(FCGI_LOG_CRIT, fcgi_apache_main_server,
405 "FastCGI: can't create termination event for server \"%s\", "
406 "CreateEvent() failed", fs->fs_path);
407 exit(0);
409 SetHandleInformation(process->terminationEvent, HANDLE_FLAG_INHERIT, TRUE);
411 termination_env_string = ap_psprintf(tp,
412 "_FCGI_SHUTDOWN_EVENT_=%ld", process->terminationEvent);
414 if (fs->socket_path)
416 SECURITY_ATTRIBUTES sa;
418 sa.lpSecurityDescriptor = NULL;
419 sa.bInheritHandle = TRUE;
420 sa.nLength = sizeof(sa);
422 listen_handle = CreateNamedPipe(fs->socket_path,
423 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
424 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
425 PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, &sa);
427 if (listen_handle == INVALID_HANDLE_VALUE)
429 ap_log_error(FCGI_LOG_CRIT, fcgi_apache_main_server,
430 "FastCGI: can't exec server \"%s\", CreateNamedPipe() failed", fs->fs_path);
431 exit(0);
434 // SetHandleInformation(listen_handle, HANDLE_FLAG_INHERIT, TRUE);
436 else
438 listen_handle = (HANDLE) fs->listenFd;
441 memset(&si, 0, sizeof(si));
442 memset(&pi, 0, sizeof(pi));
443 memset(&r, 0, sizeof(r));
445 // Can up a fake request to pass to ap_get_win32_interpreter()
446 r.per_dir_config = fcgi_apache_main_server->lookup_defaults;
447 r.server = fcgi_apache_main_server;
448 r.filename = (char *) fs->fs_path;
449 r.pool = tp;
451 fileType = ap_get_win32_interpreter(&r, &interpreter);
453 if (fileType == eFileTypeUNKNOWN) {
454 ap_log_error(FCGI_LOG_ERR_NOERRNO, fcgi_apache_main_server,
455 "FastCGI: %s is not executable; ensure interpreted scripts have "
456 "\"#!\" as their first line",
457 fs->fs_path);
458 ap_destroy_pool(tp);
459 return (pid);
463 * We have the interpreter (if there is one) and we have
464 * the arguments (if there are any).
465 * Build the command string to pass to CreateProcess.
467 quoted_filename = ap_pstrcat(tp, "\"", fs->fs_path, "\"", NULL);
468 if (interpreter && *interpreter) {
469 pCommand = ap_pstrcat(tp, interpreter, " ", quoted_filename, NULL);
471 else {
472 pCommand = quoted_filename;
476 * Make child process use hPipeOutputWrite as standard out,
477 * and make sure it does not show on screen.
479 si.cb = sizeof(si);
480 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
481 si.wShowWindow = SW_HIDE;
482 si.hStdInput = listen_handle;
484 // XXX These should be open to the error_log
485 si.hStdOutput = INVALID_HANDLE_VALUE;
486 si.hStdError = INVALID_HANDLE_VALUE;
489 * Win32's CreateProcess call requires that the environment
490 * be passed in an environment block, a null terminated block of
491 * null terminated strings.
492 * @todo we should store the env in this format for win32.
494 while (fs->envp[i])
496 iEnvBlockLen += strlen(fs->envp[i]) + 1;
497 i++;
500 iEnvBlockLen += strlen(termination_env_string) + 1;
501 iEnvBlockLen += strlen(fs->mutex_env_string) + 1;
503 pEnvBlock = (char *) ap_pcalloc(tp, iEnvBlockLen);
505 i = 0;
506 pNext = pEnvBlock;
507 while (fs->envp[i])
509 strcpy(pNext, fs->envp[i]);
510 pNext += strlen(pNext) + 1;
511 i++;
514 strcpy(pNext, termination_env_string);
515 pNext += strlen(pNext) + 1;
516 strcpy(pNext, fs->mutex_env_string);
518 if (CreateProcess(NULL, pCommand, NULL, NULL, TRUE,
520 pEnvBlock,
521 ap_make_dirstr_parent(tp, fs->fs_path),
522 &si, &pi))
524 /* Hack to get 16-bit CGI's working. It works for all the
525 * standard modules shipped with Apache. pi.dwProcessId is 0
526 * for 16-bit CGIs and all the Unix specific code that calls
527 * ap_call_exec interprets this as a failure case. And we can't
528 * use -1 either because it is mapped to 0 by the caller.
530 pid = (fileType == eFileTypeEXE16) ? -2 : pi.dwProcessId;
532 process->handle = pi.hProcess;
533 CloseHandle(pi.hThread);
536 if (fs->socket_path)
538 CloseHandle(listen_handle);
541 ap_destroy_pool(tp);
543 return pid;
545 #endif
548 #ifndef WIN32
549 static void reduce_privileges(void)
551 char *name;
553 if (geteuid() != 0)
554 return;
556 #ifndef __EMX__
557 /* Get username if passed as a uid */
558 if (ap_user_name[0] == '#') {
559 uid_t uid = atoi(&ap_user_name[1]);
560 struct passwd *ent = getpwuid(uid);
562 if (ent == NULL) {
563 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
564 "FastCGI: process manager exiting, getpwuid(%u) couldn't determine user name, "
565 "you probably need to modify the User directive", (unsigned)uid);
566 exit(1);
568 name = ent->pw_name;
570 else
571 name = ap_user_name;
573 /* Change Group */
574 if (setgid(ap_group_id) == -1) {
575 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
576 "FastCGI: process manager exiting, setgid(%u) failed", (unsigned)ap_group_id);
577 exit(1);
580 /* See Apache PR2580. Until its resolved, do it the same way CGI is done.. */
582 /* Initialize supplementary groups */
583 if (initgroups(name, ap_group_id) == -1) {
584 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
585 "FastCGI: process manager exiting, initgroups(%s,%u) failed",
586 name, (unsigned)ap_group_id);
587 exit(1);
589 #endif /* __EMX__ */
591 /* Change User */
592 if (fcgi_wrapper) {
593 if (seteuid_user() == -1) {
594 ap_log_error(FCGI_LOG_ALERT_NOERRNO, fcgi_apache_main_server,
595 "FastCGI: process manager exiting, failed to reduce privileges");
596 exit(1);
599 else {
600 if (setuid(ap_user_id) == -1) {
601 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
602 "FastCGI: process manager exiting, setuid(%u) failed", (unsigned)ap_user_id);
603 exit(1);
608 /*************
609 * Change the name of this process - best we can easily.
611 static void change_process_name(const char * const name)
613 strncpy(ap_server_argv0, name, strlen(ap_server_argv0));
615 #endif
617 static void schedule_start(fcgi_server *s, int proc)
619 /* If we've started one recently, don't register another */
620 time_t time_passed = now - s->restartTime;
622 if ((s->procs[proc].pid && (time_passed < (int) s->restartDelay))
623 || ((s->procs[proc].pid == 0) && (time_passed < (int) s->initStartDelay)))
625 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);
626 return;
629 FCGIDBG3("scheduling_start: %s (%d)", s->fs_path, proc);
630 s->procs[proc].state = FCGI_START_STATE;
631 if (proc == (int)dynamicMaxClassProcs - 1) {
632 ap_log_error(FCGI_LOG_WARN_NOERRNO, fcgi_apache_main_server,
633 "FastCGI: scheduled the %sstart of the last (dynamic) server "
634 "\"%s\" process: reached dynamicMaxClassProcs (%d)",
635 s->procs[proc].pid ? "re" : "", s->fs_path, dynamicMaxClassProcs);
640 *----------------------------------------------------------------------
642 * dynamic_read_msgs
644 * Removes the records written by request handlers and decodes them.
645 * We also update the data structures to reflect the changes.
647 *----------------------------------------------------------------------
650 static void dynamic_read_msgs(int read_ready)
652 fcgi_server *s;
653 int rc;
655 #ifndef WIN32
656 static int buflen = 0;
657 static char buf[FCGI_MSGS_BUFSIZE + 1];
658 char *ptr1, *ptr2, opcode;
659 char execName[FCGI_MAXPATH + 1];
660 char user[MAX_USER_NAME_LEN + 2];
661 char group[MAX_GID_CHAR_LEN + 1];
662 unsigned long q_usec = 0UL, req_usec = 0UL;
663 #else
664 fcgi_pm_job *joblist = NULL;
665 fcgi_pm_job *cjob = NULL;
666 #endif
668 pool *sp = NULL, *tp;
670 #ifndef WIN32
671 user[MAX_USER_NAME_LEN + 1] = group[MAX_GID_CHAR_LEN] = '\0';
672 #endif
675 * To prevent the idle application from running indefinitely, we
676 * check the timer and if it is expired, we recompute the values
677 * for each running application class. Then, when FCGI_REQUEST_COMPLETE_JOB
678 * message is received, only updates are made to the data structures.
680 if (fcgi_dynamic_last_analyzed == 0) {
681 fcgi_dynamic_last_analyzed = now;
683 if ((now - fcgi_dynamic_last_analyzed) >= (int)dynamicUpdateInterval) {
684 for (s = fcgi_servers; s != NULL; s = s->next) {
685 if (s->directive != APP_CLASS_DYNAMIC)
686 break;
688 /* Advance the last analyzed timestamp by the elapsed time since
689 * it was last set. Round the increase down to the nearest
690 * multiple of dynamicUpdateInterval */
692 fcgi_dynamic_last_analyzed += (((long)(now-fcgi_dynamic_last_analyzed)/dynamicUpdateInterval)*dynamicUpdateInterval);
693 s->smoothConnTime = (unsigned long) ((1.0-dynamicGain)*s->smoothConnTime + dynamicGain*s->totalConnTime);
694 s->totalConnTime = 0UL;
695 s->totalQueueTime = 0UL;
699 if (read_ready <= 0) {
700 return;
703 #ifndef WIN32
704 rc = read(fcgi_pm_pipe[0], (void *)(buf + buflen), FCGI_MSGS_BUFSIZE - buflen);
705 if (rc <= 0) {
706 if (!caughtSigTerm) {
707 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
708 "FastCGI: read() from pipe failed (%d)", rc);
709 if (rc == 0) {
710 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
711 "FastCGI: the PM is shutting down, Apache seems to have disappeared - bye");
712 caughtSigTerm = TRUE;
715 return;
717 buflen += rc;
718 buf[buflen] = '\0';
720 #else
722 /* dynamic_read_msgs() is called when a MBOX_EVENT is received (a
723 * request to do something) and/or when a timeout expires.
724 * There really should be no reason why this wait would get stuck
725 * but there's no point in waiting forever. */
727 rc = WaitForSingleObject(fcgi_dynamic_mbox_mutex, FCGI_MBOX_MUTEX_TIMEOUT);
729 if (rc != WAIT_OBJECT_0 && rc != WAIT_ABANDONED)
731 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
732 "FastCGI: failed to aquire the dynamic mbox mutex - something is broke?!");
733 return;
736 joblist = fcgi_dynamic_mbox;
737 fcgi_dynamic_mbox = NULL;
739 if (! ReleaseMutex(fcgi_dynamic_mbox_mutex))
741 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
742 "FastCGI: failed to release the dynamic mbox mutex - something is broke?!");
745 cjob = joblist;
746 #endif
748 tp = ap_make_sub_pool(fcgi_config_pool);
750 #ifndef WIN32
751 for (ptr1 = buf; ptr1; ptr1 = ptr2) {
752 int scan_failed = 0;
754 ptr2 = strchr(ptr1, '*');
755 if (ptr2) {
756 *ptr2++ = '\0';
758 else {
759 break;
762 opcode = *ptr1;
764 switch (opcode)
766 case FCGI_SERVER_START_JOB:
767 case FCGI_SERVER_RESTART_JOB:
769 if (sscanf(ptr1, "%c %s %16s %15s",
770 &opcode, execName, user, group) != 4)
772 scan_failed = 1;
774 break;
776 case FCGI_REQUEST_TIMEOUT_JOB:
778 if (sscanf(ptr1, "%c %s %16s %15s",
779 &opcode, execName, user, group) != 4)
781 scan_failed = 1;
783 break;
785 case FCGI_REQUEST_COMPLETE_JOB:
787 if (sscanf(ptr1, "%c %s %16s %15s %lu %lu",
788 &opcode, execName, user, group, &q_usec, &req_usec) != 6)
790 scan_failed = 1;
792 break;
794 default:
796 scan_failed = 1;
797 break;
800 if (scan_failed) {
801 ap_log_error(FCGI_LOG_ERR_NOERRNO, fcgi_apache_main_server,
802 "FastCGI: bogus message, sscanf() failed: \"%s\"", ptr1);
803 goto NextJob;
805 #else
806 /* Update data structures for processing */
807 while (cjob != NULL) {
808 joblist = cjob->next;
809 FCGIDBG7("read_job: %c %s %s %s %lu %lu", cjob->id, cjob->fs_path, cjob->user, cjob->group, cjob->qsec, cjob->start_time);
810 #endif
812 #ifndef WIN32
813 s = fcgi_util_fs_get(execName, user, group);
814 #else
815 s = fcgi_util_fs_get(cjob->fs_path, cjob->user, cjob->group);
816 #endif
818 #ifndef WIN32
819 if (s==NULL && opcode != FCGI_REQUEST_COMPLETE_JOB)
820 #else
821 if (s==NULL && cjob->id != FCGI_REQUEST_COMPLETE_JOB)
822 #endif
824 #ifdef WIN32
826 HANDLE mutex = CreateMutex(NULL, FALSE, "cjob->fs_path");
828 if (mutex == NULL)
830 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
831 "FastCGI: can't create accept mutex "
832 "for (dynamic) server \"%s\"", cjob->fs_path);
833 goto BagNewServer;
836 SetHandleInformation(mutex, HANDLE_FLAG_INHERIT, TRUE);
837 #else
838 int fd;
839 const char *err;
840 #endif
842 /* Create a perm subpool to hold the new server data,
843 * we can destroy it if something doesn't pan out */
844 sp = ap_make_sub_pool(fcgi_config_pool);
846 /* Create a new "dynamic" server */
847 s = fcgi_util_fs_new(sp);
849 s->directive = APP_CLASS_DYNAMIC;
850 s->restartDelay = dynamicRestartDelay;
851 s->listenQueueDepth = dynamicListenQueueDepth;
852 s->initStartDelay = dynamicInitStartDelay;
853 s->envp = dynamicEnvp;
854 s->flush = dynamicFlush;
856 #ifdef WIN32
857 s->mutex_env_string = ap_psprintf(sp, "_FCGI_MUTEX_=%ld", mutex);
858 s->fs_path = ap_pstrdup(sp, cjob->fs_path);
859 #else
860 s->fs_path = ap_pstrdup(sp, execName);
861 #endif
862 ap_getparents(s->fs_path);
863 ap_no2slash(s->fs_path);
864 s->procs = fcgi_util_fs_create_procs(sp, dynamicMaxClassProcs);
866 /* XXX the socket_path (both Unix and Win) *is* deducible and
867 * thus can and will be used by other apache instances without
868 * the use of shared data regarding the processes serving the
869 * requests. This can result in slightly unintuitive process
870 * counts and security implications. This is prevented
871 * if suexec (Unix) is in use. This is both a feature and a flaw.
872 * Changing it now would break existing installations. */
874 #ifndef WIN32
875 /* Create socket file's path */
876 s->socket_path = fcgi_util_socket_hash_filename(tp, execName, user, group);
877 s->socket_path = fcgi_util_socket_make_path_absolute(sp, s->socket_path, 1);
879 /* Create sockaddr, prealloc it so it won't get created in tp */
880 s->socket_addr = ap_pcalloc(sp, sizeof(struct sockaddr_un));
881 err = fcgi_util_socket_make_domain_addr(tp, (struct sockaddr_un **)&s->socket_addr,
882 &s->socket_addr_len, s->socket_path);
883 if (err) {
884 ap_log_error(FCGI_LOG_CRIT, fcgi_apache_main_server,
885 "FastCGI: can't create (dynamic) server \"%s\": %s", execName, err);
886 goto BagNewServer;
889 if (init_listen_sock(s)) {
890 goto BagNewServer;
893 /* If a wrapper is being used, config user/group info */
894 if (fcgi_wrapper) {
895 if (user[0] == '~') {
896 /* its a user dir uri, the rest is a username, not a uid */
897 struct passwd *pw = getpwnam(&user[1]);
899 if (!pw) {
900 ap_log_error(FCGI_LOG_CRIT, fcgi_apache_main_server,
901 "FastCGI: can't create (dynamic) server \"%s\": can't get uid/gid for wrapper: getpwnam(%s) failed",
902 execName, &user[1]);
903 goto BagNewServer;
905 s->uid = pw->pw_uid;
906 s->user = ap_pstrdup(sp, user);
907 s->username = s->user;
909 s->gid = pw->pw_gid;
910 s->group = ap_psprintf(sp, "%ld", (long)s->gid);
912 else {
913 struct passwd *pw;
915 s->uid = (uid_t)atol(user);
916 pw = getpwuid(s->uid);
917 if (!pw) {
918 ap_log_error(FCGI_LOG_CRIT, fcgi_apache_main_server,
919 "FastCGI: can't create (dynamic) server \"%s\": can't get uid/gid for wrapper: getwpuid(%ld) failed",
920 execName, (long)s->uid);
921 goto BagNewServer;
923 s->user = ap_pstrdup(sp, user);
924 s->username = ap_pstrdup(sp, pw->pw_name);
926 s->gid = (gid_t)atol(group);
927 s->group = ap_pstrdup(sp, group);
930 #else
931 /* Create socket file's path */
932 s->socket_path = fcgi_util_socket_hash_filename(tp, cjob->fs_path, cjob->user, cjob->group);
933 s->socket_path = fcgi_util_socket_make_path_absolute(sp, s->socket_path, 1);
934 s->listenFd = 0;
935 #endif
937 fcgi_util_fs_add(s);
939 else {
940 #ifndef WIN32
941 if (opcode == FCGI_SERVER_RESTART_JOB) {
942 #else
943 if (cjob->id==FCGI_SERVER_RESTART_JOB) {
944 #endif
945 /* Check to see if the binary has changed. If so,
946 * kill the FCGI application processes, and
947 * restart them.
949 struct stat stbuf;
950 int i;
952 #ifndef WIN32
953 if ((stat(execName, &stbuf)==0) &&
954 #else
955 if ((stat(cjob->fs_path, &stbuf)==0) &&
956 #endif
957 (stbuf.st_mtime > s->restartTime)) {
958 /* kill old server(s) */
959 for (i = 0; i < dynamicMaxClassProcs; i++) {
960 if (s->procs[i].pid > 0) {
961 fcgi_kill(&s->procs[i], SIGTERM);
965 ap_log_error(FCGI_LOG_WARN_NOERRNO, fcgi_apache_main_server,
966 "FastCGI: restarting server \"%s\" processes, newer version found",
967 #ifndef WIN32
968 execName);
969 #else
970 cjob->fs_path);
971 #endif
974 /* If dynamicAutoRestart, don't mark any new processes
975 * for starting because we probably got the
976 * FCGI_SERVER_START_JOB due to dynamicAutoUpdate and the ProcMgr
977 * will be restarting all of those we just killed.
979 if (dynamicAutoRestart)
980 goto NextJob;
982 #ifndef WIN32
983 else if (opcode == FCGI_SERVER_START_JOB) {
984 #else
985 else if (cjob->id==FCGI_SERVER_START_JOB) {
986 #endif
987 /* we've been asked to start a process--only start
988 * it if we're not already running at least one
989 * instance.
991 int i;
993 for (i = 0; i < dynamicMaxClassProcs; i++) {
994 if (s->procs[i].state == FCGI_RUNNING_STATE)
995 break;
997 /* if already running, don't start another one */
998 if (i < dynamicMaxClassProcs) {
999 goto NextJob;
1004 #ifndef WIN32
1005 switch (opcode)
1006 #else
1007 switch (cjob->id)
1008 #endif
1010 int i, start;
1012 case FCGI_SERVER_RESTART_JOB:
1014 start = FALSE;
1016 /* We just waxed 'em all. Try to find an idle slot. */
1018 for (i = 0; i < dynamicMaxClassProcs; ++i)
1020 if (s->procs[i].state == FCGI_START_STATE
1021 || s->procs[i].state == FCGI_RUNNING_STATE)
1023 break;
1025 else if (s->procs[i].state == FCGI_KILLED_STATE
1026 || s->procs[i].state == FCGI_READY_STATE)
1028 start = TRUE;
1029 break;
1033 /* Nope, just use the first slot */
1034 if (i == dynamicMaxClassProcs)
1036 start = TRUE;
1037 i = 0;
1040 if (start)
1042 schedule_start(s, i);
1045 break;
1047 case FCGI_SERVER_START_JOB:
1048 case FCGI_REQUEST_TIMEOUT_JOB:
1050 if ((fcgi_dynamic_total_proc_count + 1) > (int) dynamicMaxProcs) {
1052 * Extra instances should have been
1053 * terminated beforehand, probably need
1054 * to increase ProcessSlack parameter
1056 ap_log_error(FCGI_LOG_WARN_NOERRNO, fcgi_apache_main_server,
1057 "FastCGI: can't schedule the start of another (dynamic) server \"%s\" process: "
1058 "exceeded dynamicMaxProcs (%d)", s->fs_path, dynamicMaxProcs);
1059 goto NextJob;
1062 /* find next free slot */
1063 for (i = 0; i < dynamicMaxClassProcs; i++)
1065 if (s->procs[i].state == FCGI_START_STATE)
1067 FCGIDBG2("ignore_job: slot (%d) is already scheduled for starting", i);
1068 break;
1070 else if (s->procs[i].state == FCGI_RUNNING_STATE)
1072 continue;
1075 schedule_start(s, i);
1076 break;
1079 #ifdef FCGI_DEBUG
1080 if (i >= dynamicMaxClassProcs) {
1081 FCGIDBG1("ignore_job: slots are max'd");
1083 #endif
1084 break;
1085 case FCGI_REQUEST_COMPLETE_JOB:
1086 /* only record stats if we have a structure */
1087 if (s) {
1088 #ifndef WIN32
1089 s->totalConnTime += req_usec;
1090 s->totalQueueTime += q_usec;
1091 #else
1092 s->totalConnTime += cjob->start_time;
1093 s->totalQueueTime += cjob->qsec;
1094 #endif
1096 break;
1099 NextJob:
1101 #ifdef WIN32
1102 /* Cleanup job data */
1103 free(cjob->fs_path);
1104 free(cjob->user);
1105 free(cjob->group);
1106 free(cjob);
1107 cjob = joblist;
1108 #endif
1110 continue;
1112 BagNewServer:
1113 if (sp) ap_destroy_pool(sp);
1115 #ifdef WIN32
1116 free(cjob->fs_path);
1117 free(cjob);
1118 cjob = joblist;
1119 #endif
1122 #ifndef WIN32
1123 if (ptr1 == buf) {
1124 ap_log_error(FCGI_LOG_ERR_NOERRNO, fcgi_apache_main_server,
1125 "FastCGI: really bogus message: \"%s\"", ptr1);
1126 ptr1 += strlen(buf);
1129 buflen -= ptr1 - buf;
1130 if (buflen) {
1131 memmove(buf, ptr1, buflen);
1133 #endif
1135 ap_destroy_pool(tp);
1139 *----------------------------------------------------------------------
1141 * dynamic_kill_idle_fs_procs
1143 * Implement a kill policy for the dynamic FastCGI applications.
1144 * We also update the data structures to reflect the changes.
1146 * Side effects:
1147 * Processes are marked for deletion possibly killed.
1149 *----------------------------------------------------------------------
1151 static void dynamic_kill_idle_fs_procs(void)
1153 fcgi_server *s;
1154 int victims = 0;
1156 for (s = fcgi_servers; s != NULL; s = s->next)
1159 * server's smoothed running time, or if that's 0, the current total
1161 unsigned long connTime;
1164 * maximum number of microseconds that all of a server's running
1165 * processes together could have spent running since the last check
1167 unsigned long totalTime;
1170 * percentage, 0-100, of totalTime that the processes actually used
1172 int loadFactor;
1174 int i;
1176 if (s->directive != APP_CLASS_DYNAMIC || s->numProcesses == 0)
1178 continue;
1181 connTime = s->smoothConnTime ? s->smoothConnTime : s->totalConnTime;
1182 totalTime = s->numProcesses * (now - fcgi_dynamic_epoch) * 1000000 + 1;
1184 loadFactor = 100 * connTime / totalTime;
1186 if (s->numProcesses == 1)
1188 if (loadFactor >= dynamicThreshold1)
1190 continue;
1193 else
1195 int load = s->numProcesses / (s->numProcesses - 1) * loadFactor;
1197 if (load >= dynamicThresholdN)
1199 continue;
1204 * Run through the procs to see if we can get away w/o waxing one.
1206 for (i = 0; i < dynamicMaxClassProcs; ++i)
1208 if (s->procs[i].state == FCGI_START_STATE)
1210 s->procs[i].state = FCGI_READY_STATE;
1211 break;
1213 else if (s->procs[i].state == FCGI_VICTIM_STATE)
1215 break;
1219 if (i < dynamicMaxClassProcs)
1221 continue;
1224 for (i = 0; i < dynamicMaxClassProcs; ++i)
1226 if (s->procs[i].state != FCGI_RUNNING_STATE)
1228 continue;
1231 ap_log_error(FCGI_LOG_WARN_NOERRNO, fcgi_apache_main_server,
1232 "FastCGI: (dynamic) server \"%s\" (pid %ld) termination signaled",
1233 s->fs_path, s->procs[i].pid);
1235 fcgi_kill(&s->procs[i], SIGTERM);
1237 victims++;
1238 break;
1242 * If the number of non-victims is less than or equal to
1243 * the minimum that may be running without being killed off,
1244 * don't select any more victims.
1246 if (fcgi_dynamic_total_proc_count - victims <= dynamicMinProcs)
1248 break;
1253 #ifdef WIN32
1255 // This is a little bogus, there's gotta be a better way to do this
1256 // Can we use WaitForMultipleObjects()
1257 #define FCGI_PROC_WAIT_TIME 100
1259 void child_wait_thread_main(void *dummy) {
1260 fcgi_server *s;
1261 DWORD dwRet = WAIT_TIMEOUT;
1262 int numChildren;
1263 int i;
1264 int waited;
1266 while (!bTimeToDie) {
1267 waited = 0;
1269 for (s = fcgi_servers; s != NULL; s = s->next) {
1270 if (s->directive == APP_CLASS_EXTERNAL || s->listenFd < 0) {
1271 continue;
1273 if (s->directive == APP_CLASS_DYNAMIC) {
1274 numChildren = dynamicMaxClassProcs;
1276 else {
1277 numChildren = s->numProcesses;
1280 for (i=0; i < numChildren; i++) {
1281 if (s->procs[i].handle != INVALID_HANDLE_VALUE)
1283 DWORD exitStatus = 0;
1285 /* timeout is currently set for 100 miliecond */
1286 /* it may need to be longer or user customizable */
1287 dwRet = WaitForSingleObject(s->procs[i].handle, FCGI_PROC_WAIT_TIME);
1289 waited = 1;
1291 if (dwRet != WAIT_TIMEOUT && dwRet != WAIT_FAILED) {
1292 /* a child fs has died */
1293 /* mark the child as dead */
1295 if (s->directive == APP_CLASS_STANDARD) {
1296 /* restart static app */
1297 s->procs[i].state = FCGI_START_STATE;
1298 s->numFailures++;
1300 else {
1301 s->numProcesses--;
1302 fcgi_dynamic_total_proc_count--;
1303 FCGIDBG2("-- fcgi_dynamic_total_proc_count=%d", fcgi_dynamic_total_proc_count);
1305 if (s->procs[i].state == FCGI_VICTIM_STATE) {
1306 s->procs[i].state = FCGI_KILLED_STATE;
1308 else {
1309 /* dynamic app shouldn't have died or dynamicAutoUpdate killed it*/
1310 s->numFailures++;
1312 if (dynamicAutoRestart || (s->numProcesses <= 0 && dynamicThreshold1 == 0)) {
1313 s->procs[i].state = FCGI_START_STATE;
1315 else {
1316 s->procs[i].state = FCGI_READY_STATE;
1321 GetExitCodeProcess(s->procs[i].handle, &exitStatus);
1323 ap_log_error(FCGI_LOG_WARN_NOERRNO, fcgi_apache_main_server,
1324 "FastCGI:%s server \"%s\" (pid %d) terminated with exit with status '%d'",
1325 (s->directive == APP_CLASS_DYNAMIC) ? " (dynamic)" : "",
1326 s->fs_path, s->procs[i].pid, exitStatus);
1328 CloseHandle(s->procs[i].handle);
1329 s->procs[i].handle = INVALID_HANDLE_VALUE;
1330 s->procs[i].pid = -1;
1332 /* wake up the main thread */
1333 SetEvent(fcgi_event_handles[WAKE_EVENT]);
1338 Sleep(waited ? 0 : FCGI_PROC_WAIT_TIME);
1341 #endif
1343 #ifndef WIN32
1344 static void setup_signals(void)
1346 struct sigaction sa;
1348 /* Setup handlers */
1350 sa.sa_handler = signal_handler;
1351 sigemptyset(&sa.sa_mask);
1352 sa.sa_flags = 0;
1354 if (sigaction(SIGTERM, &sa, NULL) < 0) {
1355 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
1356 "sigaction(SIGTERM) failed");
1358 /* httpd restart */
1359 if (sigaction(SIGHUP, &sa, NULL) < 0) {
1360 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
1361 "sigaction(SIGHUP) failed");
1363 /* httpd graceful restart */
1364 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
1365 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
1366 "sigaction(SIGUSR1) failed");
1368 /* read messages from request handlers - kill interval expired */
1369 if (sigaction(SIGALRM, &sa, NULL) < 0) {
1370 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
1371 "sigaction(SIGALRM) failed");
1373 if (sigaction(SIGCHLD, &sa, NULL) < 0) {
1374 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
1375 "sigaction(SIGCHLD) failed");
1378 #endif
1380 #ifndef WIN32
1381 int fcgi_pm_main(void *dummy, child_info *info)
1382 #else
1383 void fcgi_pm_main(void *dummy)
1384 #endif
1386 fcgi_server *s;
1387 unsigned int i;
1388 int read_ready = 0;
1389 int alarmLeft = 0;
1391 #ifdef WIN32
1392 DWORD dwRet;
1393 HANDLE child_wait_thread = INVALID_HANDLE_VALUE;
1394 #else
1395 int callWaitPid, callDynamicProcs;
1396 #endif
1398 #ifdef WIN32
1399 // Add SystemRoot to the dynamic environment
1400 char ** envp = dynamicEnvp;
1401 for (i = 0; *envp; ++i) {
1402 ++envp;
1404 fcgi_config_set_env_var(fcgi_config_pool, dynamicEnvp, &i, "SystemRoot");
1406 #else
1407 reduce_privileges();
1409 close(fcgi_pm_pipe[1]);
1410 change_process_name("fcgi-pm");
1411 setup_signals();
1413 if (fcgi_wrapper) {
1414 ap_log_error(FCGI_LOG_NOTICE_NOERRNO, fcgi_apache_main_server,
1415 "FastCGI: wrapper mechanism enabled (wrapper: %s)", fcgi_wrapper);
1417 #endif
1419 /* Initialize AppClass */
1420 for (s = fcgi_servers; s != NULL; s = s->next)
1422 if (s->directive != APP_CLASS_STANDARD)
1423 continue;
1425 #ifdef WIN32
1426 if (s->socket_path)
1427 s->listenFd = 0;
1428 #endif
1430 for (i = 0; i < s->numProcesses; ++i)
1431 s->procs[i].state = FCGI_START_STATE;
1434 #ifdef WIN32
1435 ap_log_error(FCGI_LOG_NOTICE_NOERRNO, fcgi_apache_main_server,
1436 "FastCGI: process manager initialized");
1437 #else
1438 ap_log_error(FCGI_LOG_NOTICE_NOERRNO, fcgi_apache_main_server,
1439 "FastCGI: process manager initialized (pid %ld)", (long)getpid());
1440 #endif
1442 now = time(NULL);
1444 child_wait_thread = CreateThread(NULL, 0,
1445 (LPTHREAD_START_ROUTINE) child_wait_thread_main, NULL, 0, NULL);
1446 if (child_wait_thread == NULL)
1448 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
1449 "FastCGI: failed to create process manager's wait thread!");
1453 * Loop until SIGTERM
1455 for (;;) {
1456 int sleepSeconds = min(dynamicKillInterval, dynamicUpdateInterval);
1457 #ifdef WIN32
1458 time_t expire;
1459 #else
1460 pid_t childPid;
1461 int waitStatus;
1462 #endif
1463 unsigned int numChildren;
1466 * If we came out of sigsuspend() for any reason other than
1467 * SIGALRM, pick up where we left off.
1469 if (alarmLeft)
1470 sleepSeconds = alarmLeft;
1473 * Examine each configured AppClass for a process that needs
1474 * starting. Compute the earliest time when the start should
1475 * be attempted, starting it now if the time has passed. Also,
1476 * remember that we do NOT need to restart externally managed
1477 * FastCGI applications.
1479 for (s = fcgi_servers; s != NULL; s = s->next)
1481 if (s->directive == APP_CLASS_EXTERNAL)
1482 continue;
1484 numChildren = (s->directive == APP_CLASS_DYNAMIC)
1485 ? dynamicMaxClassProcs
1486 : s->numProcesses;
1488 for (i = 0; i < numChildren; ++i)
1490 if (s->procs[i].pid <= 0 && s->procs[i].state == FCGI_START_STATE)
1492 int restart = (s->procs[i].pid < 0);
1493 time_t restartTime = s->restartTime;
1495 restartTime += (restart) ? s->restartDelay : s->initStartDelay;
1497 if (restartTime <= now)
1499 if (s->listenFd < 0 && init_listen_sock(s))
1501 if (sleepSeconds > s->initStartDelay)
1502 sleepSeconds = s->initStartDelay;
1503 break;
1505 #ifndef WIN32
1506 if (caughtSigTerm) {
1507 goto ProcessSigTerm;
1509 #endif
1510 s->procs[i].pid = spawn_fs_process(s, &s->procs[i]);
1511 if (s->procs[i].pid <= 0) {
1512 ap_log_error(FCGI_LOG_CRIT, fcgi_apache_main_server,
1513 "FastCGI: can't start%s server \"%s\": spawn_fs_process() failed",
1514 (s->directive == APP_CLASS_DYNAMIC) ? " (dynamic)" : "",
1515 s->fs_path);
1517 sleepSeconds = min(sleepSeconds,
1518 max((int) s->restartDelay, FCGI_MIN_EXEC_RETRY_DELAY));
1520 ap_assert(s->procs[i].pid < 0);
1521 break;
1524 s->restartTime = now;
1526 if (s->directive == APP_CLASS_DYNAMIC) {
1527 s->numProcesses++;
1528 fcgi_dynamic_total_proc_count++;
1529 FCGIDBG2("++ fcgi_dynamic_total_proc_count=%d", fcgi_dynamic_total_proc_count);
1532 s->procs[i].state = FCGI_RUNNING_STATE;
1534 if (restart)
1535 s->numRestarts++;
1537 if (fcgi_wrapper) {
1538 ap_log_error(FCGI_LOG_WARN_NOERRNO, fcgi_apache_main_server,
1539 "FastCGI:%s server \"%s\" (uid %ld, gid %ld) %sstarted (pid %ld)",
1540 (s->directive == APP_CLASS_DYNAMIC) ? " (dynamic)" : "",
1541 s->fs_path, (long)s->uid, (long)s->gid,
1542 restart ? "re" : "", (long)s->procs[i].pid);
1544 else {
1545 ap_log_error(FCGI_LOG_WARN_NOERRNO, fcgi_apache_main_server,
1546 "FastCGI:%s server \"%s\" %sstarted (pid %ld)",
1547 (s->directive == APP_CLASS_DYNAMIC) ? " (dynamic)" : "",
1548 s->fs_path, restart ? "re" : "", (long)s->procs[i].pid);
1550 ap_assert(s->procs[i].pid > 0);
1551 } else {
1552 sleepSeconds = min(sleepSeconds, restartTime - now);
1558 #ifndef WIN32
1560 if(caughtSigTerm) {
1561 goto ProcessSigTerm;
1563 if((!caughtSigChld) && (!caughtSigAlarm)) {
1564 fd_set rfds;
1566 alarm(sleepSeconds);
1568 FD_ZERO(&rfds);
1569 FD_SET(fcgi_pm_pipe[0], &rfds);
1570 read_ready = ap_select(fcgi_pm_pipe[0] + 1, &rfds, NULL, NULL, NULL);
1572 alarmLeft = alarm(0);
1574 callWaitPid = caughtSigChld;
1575 caughtSigChld = FALSE;
1576 callDynamicProcs = caughtSigAlarm;
1577 caughtSigAlarm = FALSE;
1579 now = time(NULL);
1582 * Dynamic fcgi process management
1584 if((callDynamicProcs) || (!callWaitPid)) {
1585 dynamic_read_msgs(read_ready);
1586 if(fcgi_dynamic_epoch == 0) {
1587 fcgi_dynamic_epoch = now;
1589 if(((long)(now-fcgi_dynamic_epoch)>=dynamicKillInterval) ||
1590 ((fcgi_dynamic_total_proc_count+dynamicProcessSlack)>=dynamicMaxProcs)) {
1591 dynamic_kill_idle_fs_procs();
1592 fcgi_dynamic_epoch = now;
1596 if(!callWaitPid) {
1597 continue;
1600 /* We've caught SIGCHLD, so find out who it was using waitpid,
1601 * write a log message and update its data structure. */
1603 for (;;) {
1604 if (caughtSigTerm)
1605 goto ProcessSigTerm;
1607 childPid = waitpid(-1, &waitStatus, WNOHANG);
1609 if (childPid == -1 || childPid == 0)
1610 break;
1612 for (s = fcgi_servers; s != NULL; s = s->next) {
1613 if (s->directive == APP_CLASS_EXTERNAL)
1614 continue;
1616 if (s->directive == APP_CLASS_DYNAMIC)
1617 numChildren = dynamicMaxClassProcs;
1618 else
1619 numChildren = s->numProcesses;
1621 for (i = 0; i < numChildren; i++) {
1622 if (s->procs[i].pid == childPid)
1623 goto ChildFound;
1627 /* TODO: print something about this unknown child */
1628 continue;
1630 ChildFound:
1631 s->procs[i].pid = -1;
1633 if (s->directive == APP_CLASS_STANDARD) {
1634 /* Always restart static apps */
1635 s->procs[i].state = FCGI_START_STATE;
1636 s->numFailures++;
1638 else {
1639 s->numProcesses--;
1640 fcgi_dynamic_total_proc_count--;
1642 if (s->procs[i].state == FCGI_VICTIM_STATE) {
1643 s->procs[i].state = FCGI_KILLED_STATE;
1645 else {
1646 /* A dynamic app died or exited without provocation from the PM */
1647 s->numFailures++;
1649 if (dynamicAutoRestart || (s->numProcesses <= 0 && dynamicThreshold1 == 0))
1650 s->procs[i].state = FCGI_START_STATE;
1651 else
1652 s->procs[i].state = FCGI_READY_STATE;
1656 if (WIFEXITED(waitStatus)) {
1657 ap_log_error(FCGI_LOG_WARN_NOERRNO, fcgi_apache_main_server,
1658 "FastCGI:%s server \"%s\" (pid %d) terminated by calling exit with status '%d'",
1659 (s->directive == APP_CLASS_DYNAMIC) ? " (dynamic)" : "",
1660 s->fs_path, (int)childPid, WEXITSTATUS(waitStatus));
1662 else if (WIFSIGNALED(waitStatus)) {
1663 ap_log_error(FCGI_LOG_WARN_NOERRNO, fcgi_apache_main_server,
1664 "FastCGI:%s server \"%s\" (pid %d) terminated due to uncaught signal '%d' (%s)%s",
1665 (s->directive == APP_CLASS_DYNAMIC) ? " (dynamic)" : "",
1666 s->fs_path, (int)childPid, WTERMSIG(waitStatus), SYS_SIGLIST[WTERMSIG(waitStatus)],
1667 #ifdef WCOREDUMP
1668 WCOREDUMP(waitStatus) ? ", a core file may have been generated" : "");
1669 #else
1670 "");
1671 #endif
1673 else if (WIFSTOPPED(waitStatus)) {
1674 ap_log_error(FCGI_LOG_WARN_NOERRNO, fcgi_apache_main_server,
1675 "FastCGI:%s server \"%s\" (pid %d) stopped due to uncaught signal '%d' (%s)",
1676 (s->directive == APP_CLASS_DYNAMIC) ? " (dynamic)" : "",
1677 s->fs_path, (int)childPid, WTERMSIG(waitStatus), SYS_SIGLIST[WTERMSIG(waitStatus)]);
1679 } /* for (;;), waitpid() */
1681 #else /* WIN32 */
1683 /* wait for an event to occur or timer expires */
1684 expire = time(NULL) + sleepSeconds;
1685 dwRet = WaitForMultipleObjects(3, (HANDLE *) fcgi_event_handles, FALSE, sleepSeconds * 1000);
1687 if (dwRet == WAIT_FAILED) {
1688 /* There is something seriously wrong here */
1689 ap_log_error(FCGI_LOG_CRIT, fcgi_apache_main_server,
1690 "FastCGI: WaitForMultipleObjects() failed on event handles -- pm is shuting down");
1691 bTimeToDie = TRUE;
1694 if (dwRet != WAIT_TIMEOUT) {
1695 now = time(NULL);
1697 if (now < expire)
1698 alarmLeft = expire - now;
1702 * Dynamic fcgi process management
1704 if ((dwRet == MBOX_EVENT) || (dwRet == WAIT_TIMEOUT)) {
1705 if (dwRet == MBOX_EVENT) {
1706 read_ready = 1;
1709 now = time(NULL);
1711 dynamic_read_msgs(read_ready);
1713 if(fcgi_dynamic_epoch == 0) {
1714 fcgi_dynamic_epoch = now;
1717 if (((long)(now-fcgi_dynamic_epoch) >= (int)dynamicKillInterval) ||
1718 ((fcgi_dynamic_total_proc_count+dynamicProcessSlack) >= dynamicMaxProcs)) {
1719 dynamic_kill_idle_fs_procs();
1720 fcgi_dynamic_epoch = now;
1722 read_ready = 0;
1724 else if (dwRet == WAKE_EVENT) {
1725 continue;
1727 else if (dwRet == TERM_EVENT) {
1728 ap_log_error(FCGI_LOG_INFO_NOERRNO, fcgi_apache_main_server,
1729 "FastCGI: Termination event received process manager shutting down");
1731 bTimeToDie = TRUE;
1732 dwRet = WaitForSingleObject(child_wait_thread, INFINITE);
1734 goto ProcessSigTerm;
1736 else {
1737 // Have an received an unknown event - should not happen
1738 ap_log_error(FCGI_LOG_CRIT, fcgi_apache_main_server,
1739 "FastCGI: WaitForMultipleobjects() return an unrecognized event");
1741 bTimeToDie = TRUE;
1742 dwRet = WaitForSingleObject(child_wait_thread, INFINITE);
1744 goto ProcessSigTerm;
1747 #endif /* WIN32 */
1749 } /* for (;;), the whole shoot'n match */
1751 ProcessSigTerm:
1753 * Kill off the children, then exit.
1755 shutdown_all();
1757 #ifdef WIN32
1758 return;
1759 #else
1760 exit(0);
1761 #endif
1764 #ifdef WIN32
1765 int fcgi_pm_add_job(fcgi_pm_job *new_job)
1767 int rv = WaitForSingleObject(fcgi_dynamic_mbox_mutex, FCGI_MBOX_MUTEX_TIMEOUT);
1769 if (rv != WAIT_OBJECT_0 && rv != WAIT_ABANDONED)
1771 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
1772 "FastCGI: failed to aquire the dynamic mbox mutex - something is broke?!");
1773 return -1;
1776 new_job->next = fcgi_dynamic_mbox;
1777 fcgi_dynamic_mbox = new_job;
1779 if (! ReleaseMutex(fcgi_dynamic_mbox_mutex))
1781 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
1782 "FastCGI: failed to release the dynamic mbox mutex - something is broke?!");
1785 return 0;
1787 #endif