Back to index

php5  5.3.10
fpm.c
Go to the documentation of this file.
00001 
00002        /* $Id: fpm.c,v 1.23 2008/07/20 16:38:31 anight Exp $ */
00003        /* (c) 2007,2008 Andrei Nigmatulin */
00004 
00005 #include "fpm_config.h"
00006 
00007 #include <stdlib.h> /* for exit */
00008 
00009 #include "fpm.h"
00010 #include "fpm_children.h"
00011 #include "fpm_signals.h"
00012 #include "fpm_env.h"
00013 #include "fpm_events.h"
00014 #include "fpm_cleanup.h"
00015 #include "fpm_php.h"
00016 #include "fpm_sockets.h"
00017 #include "fpm_unix.h"
00018 #include "fpm_process_ctl.h"
00019 #include "fpm_conf.h"
00020 #include "fpm_worker_pool.h"
00021 #include "fpm_scoreboard.h"
00022 #include "fpm_stdio.h"
00023 #include "fpm_log.h"
00024 #include "zlog.h"
00025 
00026 struct fpm_globals_s fpm_globals = {
00027        .parent_pid = 0, 
00028        .argc = 0,
00029        .argv = NULL,
00030        .config = NULL,
00031        .prefix = NULL,
00032        .pid = NULL,
00033        .running_children = 0,
00034        .error_log_fd = 0,
00035        .log_level = 0,
00036        .listening_socket = 0,
00037        .max_requests = 0,
00038        .is_child = 0,
00039        .test_successful = 0,
00040        .heartbeat = 0
00041 };
00042 
00043 int fpm_init(int argc, char **argv, char *config, char *prefix, char *pid, int test_conf) /* {{{ */
00044 {
00045        fpm_globals.argc = argc;
00046        fpm_globals.argv = argv;
00047        if (config && *config) {
00048               fpm_globals.config = strdup(config);
00049        }
00050        fpm_globals.prefix = prefix;
00051        fpm_globals.pid = pid;
00052 
00053        if (0 > fpm_php_init_main()           ||
00054            0 > fpm_stdio_init_main()         ||
00055            0 > fpm_conf_init_main(test_conf) ||
00056            0 > fpm_unix_init_main()          ||
00057            0 > fpm_scoreboard_init_main()    ||
00058            0 > fpm_pctl_init_main()          ||
00059            0 > fpm_env_init_main()           ||
00060            0 > fpm_signals_init_main()       ||
00061            0 > fpm_children_init_main()      ||
00062            0 > fpm_sockets_init_main()       ||
00063            0 > fpm_worker_pool_init_main()   ||
00064            0 > fpm_event_init_main()) {
00065 
00066               if (fpm_globals.test_successful) {
00067                      exit(0);
00068               } else {
00069                      zlog(ZLOG_ERROR, "FPM initialization failed");
00070                      return -1;
00071               }
00072        }
00073 
00074        if (0 > fpm_conf_write_pid()) {
00075               zlog(ZLOG_ERROR, "FPM initialization failed");
00076               return -1;
00077        }
00078 
00079        fpm_stdio_init_final();
00080        zlog(ZLOG_NOTICE, "fpm is running, pid %d", (int) fpm_globals.parent_pid);
00081 
00082        return 0;
00083 }
00084 /* }}} */
00085 
00086 /*     children: return listening socket
00087        parent: never return */
00088 int fpm_run(int *max_requests) /* {{{ */
00089 {
00090        struct fpm_worker_pool_s *wp;
00091 
00092        /* create initial children in all pools */
00093        for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
00094               int is_parent;
00095 
00096               is_parent = fpm_children_create_initial(wp);
00097 
00098               if (!is_parent) {
00099                      goto run_child;
00100               }
00101 
00102               /* handle error */
00103               if (is_parent == 2) {
00104                      fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
00105                      fpm_event_loop(1);
00106               }
00107        }
00108 
00109        /* run event loop forever */
00110        fpm_event_loop(0);
00111 
00112 run_child: /* only workers reach this point */
00113 
00114        fpm_cleanups_run(FPM_CLEANUP_CHILD);
00115 
00116        *max_requests = fpm_globals.max_requests;
00117        return fpm_globals.listening_socket;
00118 }
00119 /* }}} */
00120