xref: /PHP-5.6/sapi/fpm/fpm/fpm.c (revision 98e6c8ca)
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 	.force_stderr = 0,
43 	.send_config_pipe = {0, 0},
44 };
45 
fpm_init(int argc,char ** argv,char * config,char * prefix,char * pid,int test_conf,int run_as_root,int force_daemon,int force_stderr)46 int fpm_init(int argc, char **argv, char *config, char *prefix, char *pid, int test_conf, int run_as_root, int force_daemon, int force_stderr) /* {{{ */
47 {
48 	fpm_globals.argc = argc;
49 	fpm_globals.argv = argv;
50 	if (config && *config) {
51 		fpm_globals.config = strdup(config);
52 	}
53 	fpm_globals.prefix = prefix;
54 	fpm_globals.pid = pid;
55 	fpm_globals.run_as_root = run_as_root;
56 	fpm_globals.force_stderr = force_stderr;
57 
58 	if (0 > fpm_php_init_main()           ||
59 	    0 > fpm_stdio_init_main()         ||
60 	    0 > fpm_conf_init_main(test_conf, force_daemon) ||
61 	    0 > fpm_unix_init_main()          ||
62 	    0 > fpm_scoreboard_init_main()    ||
63 	    0 > fpm_pctl_init_main()          ||
64 	    0 > fpm_env_init_main()           ||
65 	    0 > fpm_signals_init_main()       ||
66 	    0 > fpm_children_init_main()      ||
67 	    0 > fpm_sockets_init_main()       ||
68 	    0 > fpm_worker_pool_init_main()   ||
69 	    0 > fpm_event_init_main()) {
70 
71 		if (fpm_globals.test_successful) {
72 			exit(FPM_EXIT_OK);
73 		} else {
74 			zlog(ZLOG_ERROR, "FPM initialization failed");
75 			return -1;
76 		}
77 	}
78 
79 	if (0 > fpm_conf_write_pid()) {
80 		zlog(ZLOG_ERROR, "FPM initialization failed");
81 		return -1;
82 	}
83 
84 	fpm_stdio_init_final();
85 	zlog(ZLOG_NOTICE, "fpm is running, pid %d", (int) fpm_globals.parent_pid);
86 
87 	return 0;
88 }
89 /* }}} */
90 
91 /*	children: return listening socket
92 	parent: never return */
fpm_run(int * max_requests)93 int fpm_run(int *max_requests) /* {{{ */
94 {
95 	struct fpm_worker_pool_s *wp;
96 
97 	/* create initial children in all pools */
98 	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
99 		int is_parent;
100 
101 		is_parent = fpm_children_create_initial(wp);
102 
103 		if (!is_parent) {
104 			goto run_child;
105 		}
106 
107 		/* handle error */
108 		if (is_parent == 2) {
109 			fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
110 			fpm_event_loop(1);
111 		}
112 	}
113 
114 	/* run event loop forever */
115 	fpm_event_loop(0);
116 
117 run_child: /* only workers reach this point */
118 
119 	fpm_cleanups_run(FPM_CLEANUP_CHILD);
120 
121 	*max_requests = fpm_globals.max_requests;
122 	return fpm_globals.listening_socket;
123 }
124 /* }}} */
125 
126