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