1
2 /* $Id: fpm.c,v 1.23 2008/07/20 16:38:31 anight Exp $ */
3 /* (c) 2007,2008 Andrei Nigmatulin */
4
5 #include "fpm_config.h"
6
7 #include <stdlib.h> /* for exit */
8
9 #include "fpm.h"
10 #include "fpm_children.h"
11 #include "fpm_signals.h"
12 #include "fpm_env.h"
13 #include "fpm_events.h"
14 #include "fpm_cleanup.h"
15 #include "fpm_php.h"
16 #include "fpm_sockets.h"
17 #include "fpm_unix.h"
18 #include "fpm_process_ctl.h"
19 #include "fpm_conf.h"
20 #include "fpm_worker_pool.h"
21 #include "fpm_scoreboard.h"
22 #include "fpm_stdio.h"
23 #include "fpm_log.h"
24 #include "zlog.h"
25
26 struct fpm_globals_s fpm_globals = {
27 .parent_pid = 0,
28 .argc = 0,
29 .argv = NULL,
30 .config = NULL,
31 .prefix = NULL,
32 .pid = NULL,
33 .running_children = 0,
34 .error_log_fd = 0,
35 .log_level = 0,
36 .listening_socket = 0,
37 .max_requests = 0,
38 .is_child = 0,
39 .test_successful = 0,
40 .heartbeat = 0,
41 .run_as_root = 0,
42 .send_config_pipe = {0, 0},
43 };
44
fpm_init(int argc,char ** argv,char * config,char * prefix,char * pid,int test_conf,int run_as_root,int force_daemon)45 int fpm_init(int argc, char **argv, char *config, char *prefix, char *pid, int test_conf, int run_as_root, int force_daemon) /* {{{ */
46 {
47 fpm_globals.argc = argc;
48 fpm_globals.argv = argv;
49 if (config && *config) {
50 fpm_globals.config = strdup(config);
51 }
52 fpm_globals.prefix = prefix;
53 fpm_globals.pid = pid;
54 fpm_globals.run_as_root = run_as_root;
55
56 if (0 > fpm_php_init_main() ||
57 0 > fpm_stdio_init_main() ||
58 0 > fpm_conf_init_main(test_conf, force_daemon) ||
59 0 > fpm_unix_init_main() ||
60 0 > fpm_scoreboard_init_main() ||
61 0 > fpm_pctl_init_main() ||
62 0 > fpm_env_init_main() ||
63 0 > fpm_signals_init_main() ||
64 0 > fpm_children_init_main() ||
65 0 > fpm_sockets_init_main() ||
66 0 > fpm_worker_pool_init_main() ||
67 0 > fpm_event_init_main()) {
68
69 if (fpm_globals.test_successful) {
70 exit(FPM_EXIT_OK);
71 } else {
72 zlog(ZLOG_ERROR, "FPM initialization failed");
73 return -1;
74 }
75 }
76
77 if (0 > fpm_conf_write_pid()) {
78 zlog(ZLOG_ERROR, "FPM initialization failed");
79 return -1;
80 }
81
82 fpm_stdio_init_final();
83 zlog(ZLOG_NOTICE, "fpm is running, pid %d", (int) fpm_globals.parent_pid);
84
85 return 0;
86 }
87 /* }}} */
88
89 /* children: return listening socket
90 parent: never return */
fpm_run(int * max_requests)91 int fpm_run(int *max_requests) /* {{{ */
92 {
93 struct fpm_worker_pool_s *wp;
94
95 /* create initial children in all pools */
96 for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
97 int is_parent;
98
99 is_parent = fpm_children_create_initial(wp);
100
101 if (!is_parent) {
102 goto run_child;
103 }
104
105 /* handle error */
106 if (is_parent == 2) {
107 fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
108 fpm_event_loop(1);
109 }
110 }
111
112 /* run event loop forever */
113 fpm_event_loop(0);
114
115 run_child: /* only workers reach this point */
116
117 fpm_cleanups_run(FPM_CLEANUP_CHILD);
118
119 *max_requests = fpm_globals.max_requests;
120 return fpm_globals.listening_socket;
121 }
122 /* }}} */
123
124