xref: /PHP-7.3/sapi/fpm/fpm/fpm_request.c (revision f47798e6)
1 	/* (c) 2007,2008 Andrei Nigmatulin */
2 #ifdef HAVE_TIMES
3 #include <sys/times.h>
4 #endif
5 
6 #include "fpm_config.h"
7 
8 #include "fpm.h"
9 #include "fpm_php.h"
10 #include "fpm_str.h"
11 #include "fpm_clock.h"
12 #include "fpm_conf.h"
13 #include "fpm_trace.h"
14 #include "fpm_php_trace.h"
15 #include "fpm_process_ctl.h"
16 #include "fpm_children.h"
17 #include "fpm_scoreboard.h"
18 #include "fpm_status.h"
19 #include "fpm_stdio.h"
20 #include "fpm_request.h"
21 #include "fpm_log.h"
22 
23 #include "zlog.h"
24 
25 static const char *requests_stages[] = {
26 	[FPM_REQUEST_ACCEPTING]       = "Idle",
27 	[FPM_REQUEST_READING_HEADERS] = "Reading headers",
28 	[FPM_REQUEST_INFO]            = "Getting request informations",
29 	[FPM_REQUEST_EXECUTING]       = "Running",
30 	[FPM_REQUEST_END]             = "Ending",
31 	[FPM_REQUEST_FINISHED]        = "Finishing",
32 };
33 
fpm_request_get_stage_name(int stage)34 const char *fpm_request_get_stage_name(int stage) {
35 	return requests_stages[stage];
36 }
37 
fpm_request_accepting()38 void fpm_request_accepting() /* {{{ */
39 {
40 	struct fpm_scoreboard_proc_s *proc;
41 	struct timeval now;
42 
43 	fpm_clock_get(&now);
44 
45 	proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
46 	if (proc == NULL) {
47 		zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
48 		return;
49 	}
50 
51 	proc->request_stage = FPM_REQUEST_ACCEPTING;
52 	proc->tv = now;
53 	fpm_scoreboard_proc_release(proc);
54 
55 	/* idle++, active-- */
56 	fpm_scoreboard_update(1, -1, 0, 0, 0, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
57 }
58 /* }}} */
59 
fpm_request_reading_headers()60 void fpm_request_reading_headers() /* {{{ */
61 {
62 	struct fpm_scoreboard_proc_s *proc;
63 
64 	struct timeval now;
65 	clock_t now_epoch;
66 #ifdef HAVE_TIMES
67 	struct tms cpu;
68 #endif
69 
70 	fpm_clock_get(&now);
71 	now_epoch = time(NULL);
72 #ifdef HAVE_TIMES
73 	times(&cpu);
74 #endif
75 
76 	proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
77 	if (proc == NULL) {
78 		zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
79 		return;
80 	}
81 
82 	proc->request_stage = FPM_REQUEST_READING_HEADERS;
83 	proc->tv = now;
84 	proc->accepted = now;
85 	proc->accepted_epoch = now_epoch;
86 #ifdef HAVE_TIMES
87 	proc->cpu_accepted = cpu;
88 #endif
89 	proc->requests++;
90 	proc->request_uri[0] = '\0';
91 	proc->request_method[0] = '\0';
92 	proc->script_filename[0] = '\0';
93 	proc->query_string[0] = '\0';
94 	proc->auth_user[0] = '\0';
95 	proc->content_length = 0;
96 	fpm_scoreboard_proc_release(proc);
97 
98 	/* idle--, active++, request++ */
99 	fpm_scoreboard_update(-1, 1, 0, 0, 1, 0, 0, FPM_SCOREBOARD_ACTION_INC, NULL);
100 }
101 /* }}} */
102 
fpm_request_info()103 void fpm_request_info() /* {{{ */
104 {
105 	struct fpm_scoreboard_proc_s *proc;
106 	char *request_uri = fpm_php_request_uri();
107 	char *request_method = fpm_php_request_method();
108 	char *script_filename = fpm_php_script_filename();
109 	char *query_string = fpm_php_query_string();
110 	char *auth_user = fpm_php_auth_user();
111 	size_t content_length = fpm_php_content_length();
112 	struct timeval now;
113 
114 	fpm_clock_get(&now);
115 
116 	proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
117 	if (proc == NULL) {
118 		zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
119 		return;
120 	}
121 
122 	proc->request_stage = FPM_REQUEST_INFO;
123 	proc->tv = now;
124 
125 	if (request_uri) {
126 		strlcpy(proc->request_uri, request_uri, sizeof(proc->request_uri));
127 	}
128 
129 	if (request_method) {
130 		strlcpy(proc->request_method, request_method, sizeof(proc->request_method));
131 	}
132 
133 	if (query_string) {
134 		strlcpy(proc->query_string, query_string, sizeof(proc->query_string));
135 	}
136 
137 	if (auth_user) {
138 		strlcpy(proc->auth_user, auth_user, sizeof(proc->auth_user));
139 	}
140 
141 	proc->content_length = content_length;
142 
143 	/* if cgi.fix_pathinfo is set to "1" and script cannot be found (404)
144 		the sapi_globals.request_info.path_translated is set to NULL */
145 	if (script_filename) {
146 		strlcpy(proc->script_filename, script_filename, sizeof(proc->script_filename));
147 	}
148 
149 	fpm_scoreboard_proc_release(proc);
150 }
151 /* }}} */
152 
fpm_request_executing()153 void fpm_request_executing() /* {{{ */
154 {
155 	struct fpm_scoreboard_proc_s *proc;
156 	struct timeval now;
157 
158 	fpm_clock_get(&now);
159 
160 	proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
161 	if (proc == NULL) {
162 		zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
163 		return;
164 	}
165 
166 	proc->request_stage = FPM_REQUEST_EXECUTING;
167 	proc->tv = now;
168 	fpm_scoreboard_proc_release(proc);
169 }
170 /* }}} */
171 
fpm_request_end(void)172 void fpm_request_end(void) /* {{{ */
173 {
174 	struct fpm_scoreboard_proc_s *proc;
175 	struct timeval now;
176 #ifdef HAVE_TIMES
177 	struct tms cpu;
178 #endif
179 	size_t memory = zend_memory_peak_usage(1);
180 
181 	fpm_clock_get(&now);
182 #ifdef HAVE_TIMES
183 	times(&cpu);
184 #endif
185 
186 	proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
187 	if (proc == NULL) {
188 		zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
189 		return;
190 	}
191 	proc->request_stage = FPM_REQUEST_FINISHED;
192 	proc->tv = now;
193 	timersub(&now, &proc->accepted, &proc->duration);
194 #ifdef HAVE_TIMES
195 	timersub(&proc->tv, &proc->accepted, &proc->cpu_duration);
196 	proc->last_request_cpu.tms_utime = cpu.tms_utime - proc->cpu_accepted.tms_utime;
197 	proc->last_request_cpu.tms_stime = cpu.tms_stime - proc->cpu_accepted.tms_stime;
198 	proc->last_request_cpu.tms_cutime = cpu.tms_cutime - proc->cpu_accepted.tms_cutime;
199 	proc->last_request_cpu.tms_cstime = cpu.tms_cstime - proc->cpu_accepted.tms_cstime;
200 #endif
201 	proc->memory = memory;
202 	fpm_scoreboard_proc_release(proc);
203 	fpm_stdio_flush_child();
204 }
205 /* }}} */
206 
fpm_request_finished()207 void fpm_request_finished() /* {{{ */
208 {
209 	struct fpm_scoreboard_proc_s *proc;
210 	struct timeval now;
211 
212 	fpm_clock_get(&now);
213 
214 	proc = fpm_scoreboard_proc_acquire(NULL, -1, 0);
215 	if (proc == NULL) {
216 		zlog(ZLOG_WARNING, "failed to acquire proc scoreboard");
217 		return;
218 	}
219 
220 	proc->request_stage = FPM_REQUEST_FINISHED;
221 	proc->tv = now;
222 	fpm_scoreboard_proc_release(proc);
223 }
224 /* }}} */
225 
fpm_request_check_timed_out(struct fpm_child_s * child,struct timeval * now,int terminate_timeout,int slowlog_timeout,int track_finished)226 void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *now, int terminate_timeout, int slowlog_timeout, int track_finished) /* {{{ */
227 {
228 	struct fpm_scoreboard_proc_s proc, *proc_p;
229 
230 	proc_p = fpm_scoreboard_proc_acquire(child->wp->scoreboard, child->scoreboard_i, 1);
231 	if (!proc_p) {
232 		zlog(ZLOG_WARNING, "failed to acquire scoreboard");
233 		return;
234 	}
235 
236 	proc = *proc_p;
237 	fpm_scoreboard_proc_release(proc_p);
238 
239 #if HAVE_FPM_TRACE
240 	if (child->slow_logged.tv_sec) {
241 		if (child->slow_logged.tv_sec != proc.accepted.tv_sec || child->slow_logged.tv_usec != proc.accepted.tv_usec) {
242 			child->slow_logged.tv_sec = 0;
243 			child->slow_logged.tv_usec = 0;
244 		}
245 	}
246 #endif
247 
248 	if (proc.request_stage > FPM_REQUEST_ACCEPTING && ((proc.request_stage < FPM_REQUEST_END) || track_finished)) {
249 		char purified_script_filename[sizeof(proc.script_filename)];
250 		struct timeval tv;
251 
252 		timersub(now, &proc.accepted, &tv);
253 
254 #if HAVE_FPM_TRACE
255 		if (child->slow_logged.tv_sec == 0 && slowlog_timeout &&
256 				proc.request_stage == FPM_REQUEST_EXECUTING && tv.tv_sec >= slowlog_timeout) {
257 
258 			str_purify_filename(purified_script_filename, proc.script_filename, sizeof(proc.script_filename));
259 
260 			child->slow_logged = proc.accepted;
261 			child->tracer = fpm_php_trace;
262 
263 			fpm_trace_signal(child->pid);
264 
265 			zlog(ZLOG_WARNING, "[pool %s] child %d, script '%s' (request: \"%s %s%s%s\") executing too slow (%d.%06d sec), logging",
266 				child->wp->config->name, (int) child->pid, purified_script_filename, proc.request_method, proc.request_uri,
267 				(proc.query_string[0] ? "?" : ""), proc.query_string,
268 				(int) tv.tv_sec, (int) tv.tv_usec);
269 		}
270 		else
271 #endif
272 		if (terminate_timeout && tv.tv_sec >= terminate_timeout) {
273 			str_purify_filename(purified_script_filename, proc.script_filename, sizeof(proc.script_filename));
274 			fpm_pctl_kill(child->pid, FPM_PCTL_TERM);
275 
276 			zlog(ZLOG_WARNING, "[pool %s] child %d, script '%s' (request: \"%s %s%s%s\") execution timed out (%d.%06d sec), terminating",
277 				child->wp->config->name, (int) child->pid, purified_script_filename, proc.request_method, proc.request_uri,
278 				(proc.query_string[0] ? "?" : ""), proc.query_string,
279 				(int) tv.tv_sec, (int) tv.tv_usec);
280 		}
281 	}
282 }
283 /* }}} */
284 
fpm_request_is_idle(struct fpm_child_s * child)285 int fpm_request_is_idle(struct fpm_child_s *child) /* {{{ */
286 {
287 	struct fpm_scoreboard_proc_s *proc;
288 
289 	/* no need in atomicity here */
290 	proc = fpm_scoreboard_proc_get_from_child(child);
291 	if (!proc) {
292 		return 0;
293 	}
294 
295 	return proc->request_stage == FPM_REQUEST_ACCEPTING;
296 }
297 /* }}} */
298 
fpm_request_last_activity(struct fpm_child_s * child,struct timeval * tv)299 int fpm_request_last_activity(struct fpm_child_s *child, struct timeval *tv) /* {{{ */
300 {
301 	struct fpm_scoreboard_proc_s *proc;
302 
303 	if (!tv) return -1;
304 
305 	proc = fpm_scoreboard_proc_get_from_child(child);
306 	if (!proc) {
307 		return -1;
308 	}
309 
310 	*tv = proc->tv;
311 
312 	return 1;
313 }
314 /* }}} */
315