xref: /PHP-8.3/sapi/fpm/fpm/fpm.c (revision 5b13e830)
1 	/* (c) 2007,2008 Andrei Nigmatulin */
2 
3 #include "fpm_config.h"
4 
5 #include <stdlib.h> /* for exit */
6 
7 #include "fpm.h"
8 #include "fpm_children.h"
9 #include "fpm_signals.h"
10 #include "fpm_env.h"
11 #include "fpm_events.h"
12 #include "fpm_cleanup.h"
13 #include "fpm_php.h"
14 #include "fpm_sockets.h"
15 #include "fpm_unix.h"
16 #include "fpm_process_ctl.h"
17 #include "fpm_conf.h"
18 #include "fpm_worker_pool.h"
19 #include "fpm_scoreboard.h"
20 #include "fpm_stdio.h"
21 #include "fpm_log.h"
22 #include "zlog.h"
23 
24 struct fpm_globals_s fpm_globals = {
25 	.parent_pid = 0,
26 	.argc = 0,
27 	.argv = NULL,
28 	.config = NULL,
29 	.prefix = NULL,
30 	.pid = NULL,
31 	.running_children = 0,
32 	.error_log_fd = 0,
33 	.log_level = 0,
34 	.listening_socket = 0,
35 	.max_requests = 0,
36 	.is_child = 0,
37 	.test_successful = 0,
38 	.heartbeat = 0,
39 	.run_as_root = 0,
40 	.force_stderr = 0,
41 	.send_config_pipe = {0, 0},
42 };
43 
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)44 enum fpm_init_return_status 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 {
46 	fpm_globals.argc = argc;
47 	fpm_globals.argv = argv;
48 	if (config && *config) {
49 		fpm_globals.config = strdup(config);
50 	}
51 	fpm_globals.prefix = prefix;
52 	fpm_globals.pid = pid;
53 	fpm_globals.run_as_root = run_as_root;
54 	fpm_globals.force_stderr = force_stderr;
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 			return FPM_INIT_EXIT_OK;
71 		} else {
72 			zlog(ZLOG_ERROR, "FPM initialization failed");
73 			return FPM_INIT_ERROR;
74 		}
75 	}
76 
77 	if (0 > fpm_conf_write_pid()) {
78 		zlog(ZLOG_ERROR, "FPM initialization failed");
79 		return FPM_INIT_ERROR;
80 	}
81 
82 	fpm_stdio_init_final();
83 	zlog(ZLOG_NOTICE, "fpm is running, pid %d", (int) fpm_globals.parent_pid);
84 
85 	return FPM_INIT_CONTINUE;
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