]> git.pld-linux.org Git - packages/php.git/blob - php-fpm.patch
Rel 39
[packages/php.git] / php-fpm.patch
1 diff -urNp -x '*.orig' php-5.2.17.org/configure.in php-5.2.17/configure.in
2 --- php-5.2.17.org/configure.in 2021-10-23 19:09:17.423125073 +0200
3 +++ php-5.2.17/configure.in     2021-10-23 19:09:19.823125073 +0200
4 @@ -295,6 +295,12 @@ if test "$enable_maintainer_zts" = "yes"
5    PTHREADS_FLAGS
6  fi
7  
8 +if test "$PHP_FASTCGI" = "yes" -a "$PHP_FPM" = "yes"; then
9 +  PHP_CONFIGURE_PART(Running FastCGI Process Manager checks)
10 +  sinclude(sapi/cgi/fpm/acinclude.m4)
11 +  sinclude(sapi/cgi/fpm/config.m4)
12 +fi
13 +
14  divert(3)
15  
16  dnl ## In diversion 3 we check for compile-time options to the PHP
17 @@ -510,6 +516,7 @@ AC_CHECK_FUNCS(
18  alphasort \
19  asctime_r \
20  chroot \
21 +clearenv \
22  ctime_r \
23  cuserid \
24  crypt \
25 @@ -1247,6 +1254,8 @@ PHP_SUBST_OLD(EXTENSION_DIR)
26  PHP_SUBST_OLD(EXTRA_LDFLAGS)
27  PHP_SUBST_OLD(EXTRA_LDFLAGS_PROGRAM)
28  PHP_SUBST_OLD(EXTRA_LIBS)
29 +PHP_SUBST_OLD(SAPI_EXTRA_LIBS)
30 +PHP_SUBST_OLD(SAPI_EXTRA_DEPS)
31  PHP_SUBST_OLD(ZEND_EXTRA_LIBS)
32  PHP_SUBST_OLD(INCLUDES)
33  PHP_SUBST_OLD(EXTRA_INCLUDES)
34 @@ -1343,7 +1352,7 @@ case $PHP_SAPI in
35      install_targets="$PHP_INSTALL_CLI_TARGET $install_targets"
36      ;;
37    *)
38 -    install_targets="install-sapi $PHP_INSTALL_CLI_TARGET $install_targets"
39 +    install_targets="install-sapi $install_fpm $PHP_INSTALL_CLI_TARGET $install_targets"
40      ;;
41  esac
42  
43 diff -urNp -x '*.orig' php-5.2.17.org/main/php_config.h.in php-5.2.17/main/php_config.h.in
44 --- php-5.2.17.org/main/php_config.h.in 2011-01-07 00:04:47.000000000 +0100
45 +++ php-5.2.17/main/php_config.h.in     2021-10-23 19:09:19.826458407 +0200
46 @@ -170,6 +170,9 @@
47  /* Define if you have the chroot function.  */
48  #undef HAVE_CHROOT
49  
50 +/* Define if you have the clearenv function.  */
51 +#undef HAVE_CLEARENV
52 +
53  /* Define if you have the crypt function.  */
54  #undef HAVE_CRYPT
55  
56 @@ -935,6 +938,9 @@
57  /*   */
58  #undef PHP_FASTCGI
59  
60 +/* Is experimental fastcgi process manager code activated */
61 +#undef PHP_FASTCGI_PM
62 +
63  /*   */
64  #undef FORCE_CGI_REDIRECT
65  
66 @@ -944,6 +950,27 @@
67  /*   */
68  #undef ENABLE_PATHINFO_CHECK
69  
70 +/* do we have libxml? */
71 +#undef HAVE_LIBXML
72 +
73 +/* do we have prctl? */
74 +#undef HAVE_PRCTL
75 +
76 +/* do we have clock_gettime? */
77 +#undef HAVE_CLOCK_GETTIME
78 +
79 +/* do we have clock_get_time? */
80 +#undef HAVE_CLOCK_GET_TIME
81 +
82 +/* do we have ptrace? */
83 +#undef HAVE_PTRACE
84 +
85 +/* do we have mach_vm_read? */
86 +#undef HAVE_MACH_VM_READ
87 +
88 +/* /proc/pid/mem interface */
89 +#undef PROC_MEM_FILE
90 +
91  /* Define if system uses EBCDIC */
92  #undef CHARSET_EBCDIC
93  
94 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/Makefile.frag php-5.2.17/sapi/cgi/Makefile.frag
95 --- php-5.2.17.org/sapi/cgi/Makefile.frag       2021-10-23 19:09:17.386458407 +0200
96 +++ php-5.2.17/sapi/cgi/Makefile.frag   2021-10-23 19:09:19.826458407 +0200
97 @@ -1,2 +1,2 @@
98 -$(SAPI_CGI_PATH): libphp_common.la $(PHP_SAPI_OBJS)
99 +$(SAPI_CGI_PATH): libphp_common.la $(PHP_SAPI_OBJS) $(SAPI_EXTRA_DEPS)
100         $(BUILD_CGI)
101 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/cgi_main.c php-5.2.17/sapi/cgi/cgi_main.c
102 --- php-5.2.17.org/sapi/cgi/cgi_main.c  2021-10-23 19:09:17.429791741 +0200
103 +++ php-5.2.17/sapi/cgi/cgi_main.c      2021-10-23 19:09:19.826458407 +0200
104 @@ -55,6 +55,9 @@
105  #if HAVE_SYS_WAIT_H
106  #include <sys/wait.h>
107  #endif
108 +#if HAVE_FCNTL_H
109 +#include <fcntl.h>
110 +#endif
111  #include "zend.h"
112  #include "zend_extensions.h"
113  #include "php_ini.h"
114 @@ -85,6 +88,11 @@ int __riscosify_control = __RISCOSIFY_ST
115  #if PHP_FASTCGI
116  #include "fastcgi.h"
117  
118 +#if PHP_FASTCGI_PM
119 +#include "fpm/fpm.h"
120 +#include "fpm/fpm_request.h"
121 +#endif
122 +
123  #ifndef PHP_WIN32
124  /* XXX this will need to change later when threaded fastcgi is
125     implemented.  shane */
126 @@ -122,8 +130,12 @@ static int parent_waiting = 0;
127  static pid_t pgroup;
128  #endif
129  
130 +static int request_body_fd;
131 +
132  #endif
133  
134 +static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC);
135 +
136  #define PHP_MODE_STANDARD      1
137  #define PHP_MODE_HIGHLIGHT     2
138  #define PHP_MODE_INDENT                3
139 @@ -153,6 +165,10 @@ static const opt_struct OPTIONS[] = {
140         {'w', 0, "strip"},
141         {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
142         {'v', 0, "version"},
143 +#if PHP_FASTCGI_PM
144 +       {'x', 0, "fpm"},
145 +       {'y', 1, "fpm-config"},
146 +#endif
147         {'z', 1, "zend-extension"},
148  #if PHP_FASTCGI
149         {'T', 1, "timing"},
150 @@ -177,6 +193,7 @@ typedef struct _php_cgi_globals_struct {
151         zend_bool impersonate;
152  # endif
153  #endif
154 +       char *error_header;
155  } php_cgi_globals_struct;
156  
157  #ifdef ZTS
158 @@ -481,7 +498,28 @@ static int sapi_cgi_read_post(char *buff
159  #if PHP_FASTCGI
160                 if (fcgi_is_fastcgi()) {
161                         fcgi_request *request = (fcgi_request*) SG(server_context);
162 -                       tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
163 +
164 +                       if (request_body_fd == -1) {
165 +                               char *request_body_filename = sapi_cgibin_getenv((char *) "REQUEST_BODY_FILE",
166 +                                               sizeof("REQUEST_BODY_FILE")-1 TSRMLS_CC);
167 +
168 +                               if (request_body_filename && *request_body_filename) {
169 +                                       request_body_fd = open(request_body_filename, O_RDONLY);
170 +
171 +                                       if (0 > request_body_fd) {
172 +                                               php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)",
173 +                                                               request_body_filename, strerror(errno), errno);
174 +                                               return 0;
175 +                                       }
176 +                               }
177 +                       }
178 +
179 +                       /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */
180 +                       if (request_body_fd < 0) {
181 +                               tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
182 +                       } else {
183 +                               tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes);
184 +                       }
185                 } else {
186                         tmp_read_bytes = read(STDIN_FILENO, buffer + read_bytes, count_bytes - read_bytes);
187                 }
188 @@ -796,7 +834,12 @@ static void php_cgi_usage(char *argv0)
189                            "  -s               Display colour syntax highlighted source.\n"
190                            "  -v               Version number\n"
191                            "  -w               Display source with stripped comments and whitespace.\n"
192 -                          "  -z <file>        Load Zend extension <file>.\n"
193 +#if PHP_FASTCGI_PM
194 +                          "  -x, --fpm        Run in FastCGI process manager mode.\n"
195 +                          "  -y, --fpm-config <file>\n"
196 +                          "                   Specify alternative path to FastCGI process manager config file.\n"
197 +#endif
198 +                          "  -z <file>        Load Zend extension <file>.\n"
199  #if PHP_FASTCGI
200                            "  -T <count>       Measure execution time of script repeated <count> times.\n"
201  #endif
202 @@ -1262,6 +1305,7 @@ PHP_INI_BEGIN()
203  # ifdef PHP_WIN32
204         STD_PHP_INI_ENTRY("fastcgi.impersonate",     "0",  PHP_INI_SYSTEM, OnUpdateBool,   impersonate, php_cgi_globals_struct, php_cgi_globals)
205  # endif
206 +       STD_PHP_INI_ENTRY("fastcgi.error_header",    NULL, PHP_INI_SYSTEM, OnUpdateString, error_header, php_cgi_globals_struct, php_cgi_globals)
207  #endif
208  PHP_INI_END()
209  
210 @@ -1284,6 +1328,7 @@ static void php_cgi_globals_ctor(php_cgi
211  # ifdef PHP_WIN32
212         php_cgi_globals->impersonate = 0;
213  # endif
214 +       php_cgi_globals->error_header = NULL;
215  #endif
216  }
217  /* }}} */
218 @@ -1316,9 +1361,47 @@ static PHP_MSHUTDOWN_FUNCTION(cgi)
219  static PHP_MINFO_FUNCTION(cgi)
220  {
221         DISPLAY_INI_ENTRIES();
222 +
223 +#if PHP_FASTCGI_PM
224 +
225 +#include "fpm/fpm_autoconf.h"
226 +
227 +       php_info_print_table_start();
228 +       php_info_print_table_row(2, "php-fpm", fpm ? "active" : "inactive");
229 +       php_info_print_table_row(2, "php-fpm version", PHP_FPM_VERSION);
230 +       php_info_print_table_end();
231 +#endif
232 +
233  }
234  /* }}} */
235  
236 +#if PHP_FASTCGI
237 +PHP_FUNCTION(fastcgi_finish_request)
238 +{
239 +       fcgi_request *request = (fcgi_request*) SG(server_context);
240 +
241 +       if (fcgi_is_fastcgi() && request->fd >= 0) {
242 +
243 +               php_end_ob_buffers(1 TSRMLS_CC);
244 +               php_header(TSRMLS_C);
245 +
246 +               fcgi_flush(request, 1);
247 +               fcgi_close(request, 0, 0);
248 +               RETURN_TRUE;
249 +       }
250 +
251 +       RETURN_FALSE;
252 +
253 +}
254 +#endif
255 +
256 +function_entry cgi_fcgi_sapi_functions[] = {
257 +#if PHP_FASTCGI
258 +       PHP_FE(fastcgi_finish_request,                          NULL)
259 +#endif
260 +       {NULL, NULL, NULL}
261 +};
262 +
263  static zend_module_entry cgi_module_entry = {
264         STANDARD_MODULE_HEADER,
265  #if PHP_FASTCGI
266 @@ -1326,7 +1409,7 @@ static zend_module_entry cgi_module_entr
267  #else
268         "cgi",
269  #endif
270 -       NULL, 
271 +       cgi_fcgi_sapi_functions, 
272         PHP_MINIT(cgi), 
273         PHP_MSHUTDOWN(cgi), 
274         NULL, 
275 @@ -1365,6 +1448,7 @@ int main(int argc, char *argv[])
276         int fastcgi = fcgi_is_fastcgi();
277         char *bindpath = NULL;
278         fcgi_request request;
279 +       char *fpm_config = NULL;
280         int repeats = 1;
281         int benchmark = 0;
282  #if HAVE_GETTIMEOFDAY
283 @@ -1502,6 +1586,14 @@ int main(int argc, char *argv[])
284                         case 's': /* generate highlighted HTML from source */
285                                 behavior = PHP_MODE_HIGHLIGHT;
286                                 break;
287 +#if PHP_FASTCGI_PM
288 +                       case 'y':
289 +                               fpm_config = php_optarg;
290 +                               break;
291 +                       case 'x':
292 +                               fpm = 1;
293 +                               break;
294 +#endif
295  
296                 }
297  
298 @@ -1566,6 +1658,19 @@ consult the installation file that came
299  #endif /* FORCE_CGI_REDIRECT */
300  
301  #if PHP_FASTCGI
302 +#if PHP_FASTCGI_PM
303 +       if (fpm) {
304 +               if (0 > fpm_init(argc, argv, fpm_config)) {
305 +                       return FAILURE;
306 +               }
307 +
308 +               fcgi_fd = fpm_run(&max_requests);
309 +
310 +               fcgi_set_is_fastcgi(fastcgi = 1);
311 +       }
312 +       else
313 +#endif
314 +
315         if (bindpath) {
316                 fcgi_fd = fcgi_listen(bindpath, 128);
317                 if (fcgi_fd < 0) {
318 @@ -1580,6 +1685,9 @@ consult the installation file that came
319         
320         if (fastcgi) {
321                 /* How many times to run PHP scripts before dying */
322 +#if PHP_FASTCGI_PM
323 +               if (!fpm)
324 +#endif
325                 if (getenv("PHP_FCGI_MAX_REQUESTS")) {
326                         max_requests = atoi(getenv("PHP_FCGI_MAX_REQUESTS"));
327                         if (max_requests < 0) {
328 @@ -1597,6 +1705,9 @@ consult the installation file that came
329  
330  #ifndef PHP_WIN32
331         /* Pre-fork, if required */
332 +#if PHP_FASTCGI_PM
333 +       if (!fpm)
334 +#endif
335         if (getenv("PHP_FCGI_CHILDREN")) {
336                 char * children_str = getenv("PHP_FCGI_CHILDREN");
337                 children = atoi(children_str);
338 @@ -1754,6 +1865,8 @@ consult the installation file that came
339  #endif
340  
341  #if PHP_FASTCGI
342 +               request_body_fd = -1;
343 +
344                 SG(server_context) = (void *) &request;
345  #else
346                 SG(server_context) = (void *) 1; /* avoid server_context==NULL checks */
347 @@ -1761,6 +1874,10 @@ consult the installation file that came
348                 init_request_info(TSRMLS_C);
349                 CG(interactive) = 0;
350  
351 +#if PHP_FASTCGI_PM
352 +               if (fpm) fpm_request_info();
353 +#endif
354 +
355                 if (!cgi
356  #if PHP_FASTCGI
357                         && !fastcgi
358 @@ -2043,6 +2160,10 @@ consult the installation file that came
359                         }
360                 }
361  
362 +#if PHP_FASTCGI_PM
363 +               if (fpm) fpm_request_executing();
364 +#endif
365 +
366                 switch (behavior) {
367                         case PHP_MODE_STANDARD:
368                                 php_execute_script(&file_handle TSRMLS_CC);
369 @@ -2095,6 +2216,10 @@ consult the installation file that came
370  
371  #if PHP_FASTCGI
372  fastcgi_request_done:
373 +
374 +               if (request_body_fd != -1) close(request_body_fd);
375 +
376 +               request_body_fd = -2;
377  #endif
378                 {
379                         char *path_translated;
380 @@ -2108,6 +2233,16 @@ fastcgi_request_done:
381                                 SG(request_info).path_translated = path_translated;
382                         }
383  
384 +                       if (EG(exit_status) == 255) {
385 +                               if (CGIG(error_header) && *CGIG(error_header)) {
386 +                                       sapi_header_line ctr = {0};
387 +
388 +                                       ctr.line = CGIG(error_header);
389 +                                       ctr.line_len = strlen(CGIG(error_header));
390 +                                       sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
391 +                               }
392 +                       }
393 +                       
394                         php_request_shutdown((void *) 0);
395                         if (exit_status == 0) {
396                                 exit_status = EG(exit_status);
397 @@ -2145,15 +2280,20 @@ fastcgi_request_done:
398                                 if (bindpath) {
399                                         free(bindpath);
400                                 }
401 -                               if (max_requests != 1) {
402 -                                       /* no need to return exit_status of the last request */
403 -                                       exit_status = 0;
404 -                               }
405                                 break;
406                         }
407                         /* end of fastcgi loop */
408                 }
409                 fcgi_shutdown();
410 +
411 +               if (fcgi_in_shutdown() ||                                                               /* graceful shutdown by a signal */
412 +                               (max_requests && (requests == max_requests))    /* we were told to process max_requests and we are done */
413 +                       ) {
414 +                       exit_status = 0;
415 +               }
416 +               else {
417 +                       exit_status = 255;
418 +               }
419  #endif
420  
421                 if (cgi_sapi_module.php_ini_path_override) {
422 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/config9.m4 php-5.2.17/sapi/cgi/config9.m4
423 --- php-5.2.17.org/sapi/cgi/config9.m4  2021-10-23 19:09:17.386458407 +0200
424 +++ php-5.2.17/sapi/cgi/config9.m4      2021-10-23 19:09:19.826458407 +0200
425 @@ -22,6 +22,10 @@ PHP_ARG_ENABLE(path-info-check,,
426  [  --disable-path-info-check CGI: If this is disabled, paths such as
427                              /info.php/test?a=b will fail to work], yes, no)
428  
429 +PHP_ARG_ENABLE(fpm,,
430 +[  --enable-fpm              FastCGI: If this is enabled, the fastcgi support
431 +                            will include experimental process manager code], no, no)
432 +
433  dnl
434  dnl CGI setup
435  dnl
436 @@ -54,6 +58,20 @@ if test "$PHP_SAPI" = "default"; then
437      AC_DEFINE_UNQUOTED(PHP_FASTCGI, $PHP_ENABLE_FASTCGI, [ ])
438      AC_MSG_RESULT($PHP_FASTCGI)
439  
440 +    dnl --enable-fpm
441 +    if test "$PHP_FASTCGI" = "yes"; then
442 +      AC_MSG_CHECKING(whether to enable FastCGI Process Manager)
443 +      if test "$PHP_FPM" = "yes"; then
444 +        PHP_FASTCGI_PM=1
445 +      else
446 +        PHP_FASTCGI_PM=0
447 +      fi
448 +      AC_MSG_RESULT($PHP_FPM)
449 +    else
450 +      PHP_FASTCGI_PM=0
451 +    fi
452 +    AC_DEFINE_UNQUOTED(PHP_FASTCGI_PM, $PHP_FASTCGI_PM, [Is experimental fastcgi process manager code activated])
453 +
454      dnl --enable-force-cgi-redirect
455      AC_MSG_CHECKING(whether to force Apache CGI redirect)
456      if test "$PHP_FORCE_CGI_REDIRECT" = "yes"; then
457 @@ -93,10 +111,10 @@ if test "$PHP_SAPI" = "default"; then
458          BUILD_CGI="echo '\#! .' > php.sym && echo >>php.sym && nm -BCpg \`echo \$(PHP_GLOBAL_OBJS) \$(PHP_SAPI_OBJS) | sed 's/\([A-Za-z0-9_]*\)\.lo/\1.o/g'\` | \$(AWK) '{ if (((\$\$2 == \"T\") || (\$\$2 == \"D\") || (\$\$2 == \"B\")) && (substr(\$\$3,1,1) != \".\")) { print \$\$3 } }' | sort -u >> php.sym && \$(LIBTOOL) --mode=link \$(CC) -export-dynamic \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) -Wl,-brtl -Wl,-bE:php.sym \$(PHP_RPATHS) \$(PHP_GLOBAL_OBJS) \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
459          ;;
460        *darwin*)
461 -        BUILD_CGI="\$(CC) \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(NATIVE_RPATHS) \$(PHP_GLOBAL_OBJS:.lo=.o) \$(PHP_SAPI_OBJS:.lo=.o) \$(PHP_FRAMEWORKS) \$(EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
462 +        BUILD_CGI="\$(CC) \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(NATIVE_RPATHS) \$(PHP_GLOBAL_OBJS:.lo=.o) \$(PHP_SAPI_OBJS:.lo=.o) \$(PHP_FRAMEWORKS) \$(EXTRA_LIBS) \$(SAPI_EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
463        ;;
464        *)
465 -        BUILD_CGI="\$(LIBTOOL) --mode=link \$(CC) -export-dynamic \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(PHP_RPATHS) libphp_common.la \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
466 +        BUILD_CGI="\$(LIBTOOL) --mode=link \$(CC) -export-dynamic \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(PHP_RPATHS) libphp_common.la \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(SAPI_EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
467        ;;
468      esac
469  
470 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fastcgi.c php-5.2.17/sapi/cgi/fastcgi.c
471 --- php-5.2.17.org/sapi/cgi/fastcgi.c   2010-01-03 10:23:27.000000000 +0100
472 +++ php-5.2.17/sapi/cgi/fastcgi.c       2021-10-23 19:09:19.826458407 +0200
473 @@ -27,6 +27,11 @@
474  #include <stdarg.h>
475  #include <errno.h>
476  
477 +#if PHP_FASTCGI_PM
478 +#include "fpm/fpm.h"
479 +#include "fpm/fpm_request.h"
480 +#endif
481 +
482  #ifdef _WIN32
483  
484  #include <windows.h>
485 @@ -234,6 +239,8 @@ int fcgi_init(void)
486                 } else {
487                         return is_fastcgi = 0;
488                 }
489 +
490 +               fcgi_set_allowed_clients(getenv("FCGI_WEB_SERVER_ADDRS"));
491  #endif
492         }
493         return is_fastcgi;
494 @@ -249,14 +256,26 @@ int fcgi_is_fastcgi(void)
495         }
496  }
497  
498 +void fcgi_set_is_fastcgi(int new_value)
499 +{
500 +       is_fastcgi = new_value;
501 +}
502 +
503 +void fcgi_set_in_shutdown(int new_value)
504 +{
505 +       in_shutdown = new_value;
506 +}
507 +
508  void fcgi_shutdown(void)
509  {
510         if (is_initialized) {
511                 zend_hash_destroy(&fcgi_mgmt_vars);
512         }
513         is_fastcgi = 0;
514 +
515         if (allowed_clients) {
516                 free(allowed_clients);
517 +               allowed_clients = 0;
518         }
519  }
520  
521 @@ -330,6 +349,41 @@ out_fail:
522  }
523  #endif
524  
525 +void fcgi_set_allowed_clients(char *ip)
526 +{
527 +    char *cur, *end;
528 +    int n;
529 +           
530 +    if (ip) {
531 +       ip = strdup(ip);
532 +       cur = ip;
533 +       n = 0;
534 +       while (*cur) {
535 +               if (*cur == ',') n++;
536 +               cur++;
537 +       }
538 +               if (allowed_clients) free(allowed_clients);
539 +       allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
540 +       n = 0;
541 +       cur = ip;
542 +       while (cur) {
543 +               end = strchr(cur, ',');
544 +               if (end) {
545 +                       *end = 0;
546 +                       end++;
547 +               }
548 +               allowed_clients[n] = inet_addr(cur);
549 +               if (allowed_clients[n] == INADDR_NONE) {
550 +                               fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
551 +               }
552 +               n++;
553 +               cur = end;
554 +       }
555 +       allowed_clients[n] = INADDR_NONE;
556 +               free(ip);
557 +       }
558 +}
559 +
560  static int is_port_number(const char *bindpath)
561  {
562         while (*bindpath) {
563 @@ -458,38 +512,6 @@ int fcgi_listen(const char *path, int ba
564  
565         if (!tcp) {
566                 chmod(path, 0777);
567 -       } else {
568 -                       char *ip = getenv("FCGI_WEB_SERVER_ADDRS");
569 -                       char *cur, *end;
570 -                       int n;
571 -                       
572 -                       if (ip) {
573 -                               ip = strdup(ip);
574 -                               cur = ip;
575 -                               n = 0;
576 -                               while (*cur) {
577 -                                       if (*cur == ',') n++;
578 -                                       cur++;
579 -                               }
580 -                               allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
581 -                               n = 0;
582 -                               cur = ip;
583 -                               while (cur) {
584 -                                       end = strchr(cur, ',');
585 -                                       if (end) {
586 -                                               *end = 0;
587 -                                               end++;
588 -                                       }
589 -                                       allowed_clients[n] = inet_addr(cur);
590 -                                       if (allowed_clients[n] == INADDR_NONE) {
591 -                                       fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
592 -                                       }
593 -                                       n++;
594 -                                       cur = end;
595 -                               }
596 -                               allowed_clients[n] = INADDR_NONE;
597 -                       free(ip);
598 -               }
599         }
600  
601         if (!is_initialized) {
602 @@ -866,7 +888,7 @@ int fcgi_read(fcgi_request *req, char *s
603         return n;
604  }
605  
606 -static inline void fcgi_close(fcgi_request *req, int force, int destroy)
607 +void fcgi_close(fcgi_request *req, int force, int destroy)
608  {
609         if (destroy) {
610                 zend_hash_destroy(&req->env);
611 @@ -906,6 +928,10 @@ static inline void fcgi_close(fcgi_reque
612                 close(req->fd);
613  #endif
614                 req->fd = -1;
615 +
616 +#if PHP_FASTCGI_PM
617 +               if (fpm) fpm_request_finished();
618 +#endif
619         }
620  }
621  
622 @@ -953,6 +979,10 @@ int fcgi_accept_request(fcgi_request *re
623                                         sa_t sa;
624                                         socklen_t len = sizeof(sa);
625  
626 +#if PHP_FASTCGI_PM
627 +                                       if (fpm) fpm_request_accepting();
628 +#endif
629 +
630                                         FCGI_LOCK(req->listen_socket);
631                                         req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
632                                         FCGI_UNLOCK(req->listen_socket);
633 @@ -988,6 +1018,11 @@ int fcgi_accept_request(fcgi_request *re
634                                 break;
635  #else
636                                 if (req->fd >= 0) {
637 +
638 +#if PHP_FASTCGI_PM
639 +                                       if (fpm) fpm_request_reading_headers();
640 +#endif
641 +
642  #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
643                                         struct pollfd fds;
644                                         int ret;
645 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fastcgi.h php-5.2.17/sapi/cgi/fastcgi.h
646 --- php-5.2.17.org/sapi/cgi/fastcgi.h   2010-01-03 10:23:27.000000000 +0100
647 +++ php-5.2.17/sapi/cgi/fastcgi.h       2021-10-23 19:09:19.826458407 +0200
648 @@ -114,6 +114,9 @@ typedef struct _fcgi_request {
649  int fcgi_init(void);
650  void fcgi_shutdown(void);
651  int fcgi_is_fastcgi(void);
652 +void fcgi_set_is_fastcgi(int);
653 +void fcgi_set_in_shutdown(int);
654 +void fcgi_set_allowed_clients(char *);
655  int fcgi_in_shutdown(void);
656  int fcgi_listen(const char *path, int backlog);
657  void fcgi_init_request(fcgi_request *req, int listen_socket);
658 @@ -128,6 +131,8 @@ int fcgi_read(fcgi_request *req, char *s
659  int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len);
660  int fcgi_flush(fcgi_request *req, int close);
661  
662 +void fcgi_close(fcgi_request *req, int force, int destroy);
663 +
664  #ifdef PHP_WIN32
665  void fcgi_impersonate(void);
666  #endif
667 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/Makefile.frag php-5.2.17/sapi/cgi/fpm/Makefile.frag
668 --- php-5.2.17.org/sapi/cgi/fpm/Makefile.frag   1970-01-01 01:00:00.000000000 +0100
669 +++ php-5.2.17/sapi/cgi/fpm/Makefile.frag       2021-10-23 19:09:19.826458407 +0200
670 @@ -0,0 +1,21 @@
671 +
672 +install-fpm: sapi/cgi/fpm/php-fpm.conf sapi/cgi/fpm/php-fpm
673 +       @echo "Installing FPM config:            $(INSTALL_ROOT)$(php_fpm_conf_path)"
674 +       -@$(mkinstalldirs) \
675 +               $(INSTALL_ROOT)$(prefix)/sbin \
676 +               `dirname "$(INSTALL_ROOT)$(php_fpm_conf_path)"` \
677 +               `dirname "$(INSTALL_ROOT)$(php_fpm_log_path)"` \
678 +               `dirname "$(INSTALL_ROOT)$(php_fpm_pid_path)"`
679 +       -@if test -r "$(INSTALL_ROOT)$(php_fpm_conf_path)" ; then \
680 +               dest=`basename "$(php_fpm_conf_path)"`.default ; \
681 +               echo "                                  (installing as $$dest)" ; \
682 +       else \
683 +               dest=`basename "$(php_fpm_conf_path)"` ; \
684 +       fi ; \
685 +       $(INSTALL_DATA) $(top_builddir)/sapi/cgi/fpm/php-fpm.conf $(INSTALL_ROOT)`dirname "$(php_fpm_conf_path)"`/$$dest
686 +       @echo "Installing init.d script:         $(INSTALL_ROOT)$(prefix)/sbin/php-fpm"
687 +       -@$(INSTALL) -m 0755 $(top_builddir)/sapi/cgi/fpm/php-fpm $(INSTALL_ROOT)$(prefix)/sbin/php-fpm
688 +
689 +$(top_builddir)/libevent/libevent.a: $(top_builddir)/libevent/Makefile
690 +       cd $(top_builddir)/libevent && $(MAKE) libevent.a
691 +
692 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/acinclude.m4 php-5.2.17/sapi/cgi/fpm/acinclude.m4
693 --- php-5.2.17.org/sapi/cgi/fpm/acinclude.m4    1970-01-01 01:00:00.000000000 +0100
694 +++ php-5.2.17/sapi/cgi/fpm/acinclude.m4        2021-10-23 19:09:19.826458407 +0200
695 @@ -0,0 +1,377 @@
696 +
697 +AC_DEFUN([AC_FPM_CHECK_FUNC],
698 +[
699 +       SAVED_CFLAGS="$CFLAGS"
700 +       CFLAGS="$CFLAGS $2"
701 +       SAVED_LIBS="$LIBS"
702 +       LIBS="$LIBS $3"
703 +
704 +       AC_CHECK_FUNC([$1],[$4],[$5])
705 +
706 +       CFLAGS="$SAVED_CFLAGS"
707 +       LIBS="$SAVED_LIBS"
708 +])
709 +
710 +AC_DEFUN([AC_FPM_LIBEVENT],
711 +[
712 +       AC_ARG_WITH([libevent],
713 +       [  --with-libevent=DIR         FPM: libevent install directory])
714 +
715 +       LIBEVENT_CFLAGS=""
716 +       LIBEVENT_LIBS="-levent"
717 +       LIBEVENT_INCLUDE_PATH=""
718 +
719 +       if test "$with_libevent" != "no" -a -n "$with_libevent"; then
720 +               LIBEVENT_CFLAGS="-I$with_libevent/include"
721 +               LIBEVENT_LIBS="-L$with_libevent/lib $LIBEVENT_LIBS"
722 +               LIBEVENT_INCLUDE_PATH="$with_libevent/include"
723 +       fi
724 +
725 +       AC_MSG_CHECKING([for event.h])
726 +
727 +       found=no
728 +
729 +       for dir in "$LIBEVENT_INCLUDE_PATH" /usr/include ; do
730 +               if test -r "$dir/event.h" ; then
731 +                       found=yes
732 +                       break
733 +               fi
734 +       done
735 +
736 +       AC_MSG_RESULT([$found])
737 +
738 +       AC_FPM_CHECK_FUNC([event_set], [$LIBEVENT_CFLAGS], [$LIBEVENT_LIBS], ,
739 +               [AC_MSG_ERROR([Failed to link with libevent. Perhaps --with-libevent=DIR option could help.])])
740 +
741 +       AC_FPM_CHECK_FUNC([event_base_free], [$LIBEVENT_CFLAGS], [$LIBEVENT_LIBS], ,
742 +               [AC_MSG_ERROR([You have too old version. libevent version >= 1.2 is required.])])
743 +
744 +])
745 +
746 +AC_DEFUN([AC_FPM_LIBXML],
747 +[
748 +       AC_MSG_RESULT([checking for XML configuration])
749 +
750 +       AC_ARG_WITH(xml-config,
751 +       [  --with-xml-config=PATH      FPM: use xml-config in PATH to find libxml],
752 +               [XMLCONFIG="$withval"],
753 +               [AC_PATH_PROGS(XMLCONFIG, [xml2-config xml-config], "")]
754 +       )
755 +
756 +       if test "x$XMLCONFIG" = "x"; then
757 +               AC_MSG_ERROR([XML configuration could not be found])
758 +       else
759 +        AC_MSG_CHECKING([for libxml library])
760 +
761 +               if test ! -x "$XMLCONFIG"; then
762 +                       AC_MSG_ERROR([$XMLCONFIG cannot be executed])
763 +               fi
764 +
765 +               LIBXML_LIBS="`$XMLCONFIG --libs`"
766 +               LIBXML_CFLAGS="`$XMLCONFIG --cflags`"
767 +               LIBXML_VERSION="`$XMLCONFIG --version`"
768 +
769 +        AC_MSG_RESULT([yes, $LIBXML_VERSION])
770 +
771 +               AC_FPM_CHECK_FUNC([xmlParseFile], [$LIBXML_CFLAGS], [$LIBXML_LIBS], ,
772 +                       [AC_MSG_ERROR([Failed to link with libxml])])
773 +
774 +               AC_DEFINE(HAVE_LIBXML, 1, [do we have libxml?])
775 +       fi
776 +])
777 +
778 +AC_DEFUN([AC_FPM_JUDY],
779 +[
780 +       AC_ARG_WITH([Judy],
781 +       [  --with-Judy=DIR             FPM: Judy install directory])
782 +
783 +       JUDY_CFLAGS=""
784 +       JUDY_LIBS="-lJudy"
785 +       JUDY_INCLUDE_PATH=""
786 +
787 +       if test "$with_Judy" != "no" -a -n "$with_Judy"; then
788 +               JUDY_INCLUDE_PATH="$with_Judy/include"
789 +               JUDY_CFLAGS="-I$with_Judy/include $JUDY_CFLAGS"
790 +               JUDY_LIBS="-L$with_Judy/lib $JUDY_LIBS"
791 +       fi
792 +
793 +       AC_MSG_CHECKING([for Judy.h])
794 +
795 +       found=no
796 +
797 +       for dir in "$JUDY_INCLUDE_PATH" /usr/include ; do
798 +               if test -r "$dir/Judy.h" ; then
799 +                       found=yes
800 +                       break
801 +               fi
802 +       done
803 +
804 +       AC_MSG_RESULT([$found])
805 +
806 +       AC_FPM_CHECK_FUNC([JudyLCount], [$JUDY_CFLAGS], [$JUDY_LIBS], ,
807 +               [AC_MSG_ERROR([Failed to link with Judy])])
808 +
809 +])
810 +
811 +AC_DEFUN([AC_FPM_CLOCK],
812 +[
813 +       have_clock_gettime=no
814 +
815 +       AC_MSG_CHECKING([for clock_gettime])
816 +
817 +       AC_TRY_LINK([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
818 +               have_clock_gettime=yes
819 +               AC_MSG_RESULT([yes])
820 +       ], [
821 +               AC_MSG_RESULT([no])
822 +       ])
823 +
824 +       if test "$have_clock_gettime" = "no"; then
825 +               AC_MSG_CHECKING([for clock_gettime in -lrt])
826 +
827 +               SAVED_LIBS="$LIBS"
828 +               LIBS="$LIBS -lrt"
829 +
830 +               AC_TRY_LINK([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
831 +                       have_clock_gettime=yes
832 +                       AC_MSG_RESULT([yes])
833 +               ], [
834 +                       LIBS="$SAVED_LIBS"
835 +                       AC_MSG_RESULT([no])
836 +               ])
837 +       fi
838 +
839 +       if test "$have_clock_gettime" = "yes"; then
840 +               AC_DEFINE([HAVE_CLOCK_GETTIME], 1, [do we have clock_gettime?])
841 +       fi
842 +
843 +       have_clock_get_time=no
844 +
845 +       if test "$have_clock_gettime" = "no"; then
846 +               AC_MSG_CHECKING([for clock_get_time])
847 +
848 +               AC_TRY_RUN([ #include <mach/mach.h>
849 +                       #include <mach/clock.h>
850 +                       #include <mach/mach_error.h>
851 +
852 +                       int main()
853 +                       {
854 +                               kern_return_t ret; clock_serv_t aClock; mach_timespec_t aTime;
855 +                               ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &aClock);
856 +
857 +                               if (ret != KERN_SUCCESS) {
858 +                                       return 1;
859 +                               }
860 +
861 +                               ret = clock_get_time(aClock, &aTime);
862 +                               if (ret != KERN_SUCCESS) {
863 +                                       return 2;
864 +                               }
865 +
866 +                               return 0;
867 +                       }
868 +               ], [
869 +                       have_clock_get_time=yes
870 +                       AC_MSG_RESULT([yes])
871 +               ], [
872 +                       AC_MSG_RESULT([no])
873 +               ])
874 +       fi
875 +
876 +       if test "$have_clock_get_time" = "yes"; then
877 +               AC_DEFINE([HAVE_CLOCK_GET_TIME], 1, [do we have clock_get_time?])
878 +       fi
879 +])
880 +
881 +AC_DEFUN([AC_FPM_TRACE],
882 +[
883 +       have_ptrace=no
884 +       have_broken_ptrace=no
885 +
886 +       AC_MSG_CHECKING([for ptrace])
887 +
888 +       AC_TRY_COMPILE([
889 +               #include <sys/types.h>
890 +               #include <sys/ptrace.h> ], [ptrace(0, 0, (void *) 0, 0);], [
891 +               have_ptrace=yes
892 +               AC_MSG_RESULT([yes])
893 +       ], [
894 +               AC_MSG_RESULT([no])
895 +       ])
896 +
897 +       if test "$have_ptrace" = "yes"; then
898 +               AC_MSG_CHECKING([whether ptrace works])
899 +
900 +               AC_TRY_RUN([
901 +                       #include <unistd.h>
902 +                       #include <signal.h>
903 +                       #include <sys/wait.h>
904 +                       #include <sys/types.h>
905 +                       #include <sys/ptrace.h>
906 +                       #include <errno.h>
907 +
908 +                       #if !defined(PTRACE_ATTACH) && defined(PT_ATTACH)
909 +                       #define PTRACE_ATTACH PT_ATTACH
910 +                       #endif
911 +
912 +                       #if !defined(PTRACE_DETACH) && defined(PT_DETACH)
913 +                       #define PTRACE_DETACH PT_DETACH
914 +                       #endif
915 +
916 +                       #if !defined(PTRACE_PEEKDATA) && defined(PT_READ_D)
917 +                       #define PTRACE_PEEKDATA PT_READ_D
918 +                       #endif
919 +
920 +                       int main()
921 +                       {
922 +                               long v1 = (unsigned int) -1; /* copy will fail if sizeof(long) == 8 and we've got "int ptrace()" */
923 +                               long v2;
924 +                               pid_t child;
925 +                               int status;
926 +
927 +                               if ( (child = fork()) ) { /* parent */
928 +                                       int ret = 0;
929 +
930 +                                       if (0 > ptrace(PTRACE_ATTACH, child, 0, 0)) {
931 +                                               return 1;
932 +                                       }
933 +
934 +                                       waitpid(child, &status, 0);
935 +
936 +                       #ifdef PT_IO
937 +                                       struct ptrace_io_desc ptio = {
938 +                                               .piod_op = PIOD_READ_D,
939 +                                               .piod_offs = &v1,
940 +                                               .piod_addr = &v2,
941 +                                               .piod_len = sizeof(v1)
942 +                                       };
943 +
944 +                                       if (0 > ptrace(PT_IO, child, (void *) &ptio, 0)) {
945 +                                               ret = 1;
946 +                                       }
947 +                       #else
948 +                                       errno = 0;
949 +
950 +                                       v2 = ptrace(PTRACE_PEEKDATA, child, (void *) &v1, 0);
951 +
952 +                                       if (errno) {
953 +                                               ret = 1;
954 +                                       }
955 +                       #endif
956 +                                       ptrace(PTRACE_DETACH, child, (void *) 1, 0);
957 +
958 +                                       kill(child, SIGKILL);
959 +
960 +                                       return ret ? ret : (v1 != v2);
961 +                               }
962 +                               else { /* child */
963 +                                       sleep(10);
964 +                                       return 0;
965 +                               }
966 +                       }
967 +               ], [
968 +                       AC_MSG_RESULT([yes])
969 +               ], [
970 +                       have_ptrace=no
971 +                       have_broken_ptrace=yes
972 +                       AC_MSG_RESULT([no])
973 +               ])
974 +       fi
975 +
976 +       if test "$have_ptrace" = "yes"; then
977 +               AC_DEFINE([HAVE_PTRACE], 1, [do we have ptrace?])
978 +       fi
979 +
980 +       have_mach_vm_read=no
981 +
982 +       if test "$have_broken_ptrace" = "yes"; then
983 +               AC_MSG_CHECKING([for mach_vm_read])
984 +
985 +               AC_TRY_COMPILE([ #include <mach/mach.h>
986 +                       #include <mach/mach_vm.h>
987 +               ], [
988 +                       mach_vm_read((vm_map_t)0, (mach_vm_address_t)0, (mach_vm_size_t)0, (vm_offset_t *)0, (mach_msg_type_number_t*)0);
989 +               ], [
990 +                       have_mach_vm_read=yes
991 +                       AC_MSG_RESULT([yes])
992 +               ], [
993 +                       AC_MSG_RESULT([no])
994 +               ])
995 +       fi
996 +
997 +       if test "$have_mach_vm_read" = "yes"; then
998 +               AC_DEFINE([HAVE_MACH_VM_READ], 1, [do we have mach_vm_read?])
999 +       fi
1000 +
1001 +       proc_mem_file=""
1002 +
1003 +       if test -r /proc/$$/mem ; then
1004 +               proc_mem_file="mem"
1005 +       else
1006 +               if test -r /proc/$$/as ; then
1007 +                       proc_mem_file="as"
1008 +               fi
1009 +       fi
1010 +
1011 +       if test -n "$proc_mem_file" ; then
1012 +               AC_MSG_CHECKING([for proc mem file])
1013 +
1014 +               AC_TRY_RUN([
1015 +                       #define _GNU_SOURCE
1016 +                       #define _FILE_OFFSET_BITS 64
1017 +                       #include <stdint.h>
1018 +                       #include <unistd.h>
1019 +                       #include <sys/types.h>
1020 +                       #include <sys/stat.h>
1021 +                       #include <fcntl.h>
1022 +                       #include <stdio.h>
1023 +                       int main()
1024 +                       {
1025 +                               long v1 = (unsigned int) -1, v2 = 0;
1026 +                               char buf[128];
1027 +                               int fd;
1028 +                               sprintf(buf, "/proc/%d/$proc_mem_file", getpid());
1029 +                               fd = open(buf, O_RDONLY);
1030 +                               if (0 > fd) {
1031 +                                       return 1;
1032 +                               }
1033 +                               if (sizeof(long) != pread(fd, &v2, sizeof(long), (uintptr_t) &v1)) {
1034 +                                       close(fd);
1035 +                                       return 1;
1036 +                               }
1037 +                               close(fd);
1038 +                               return v1 != v2;
1039 +                       }
1040 +               ], [
1041 +                       AC_MSG_RESULT([$proc_mem_file])
1042 +               ], [
1043 +                       proc_mem_file=""
1044 +                       AC_MSG_RESULT([no])
1045 +               ])
1046 +       fi
1047 +
1048 +       if test -n "$proc_mem_file"; then
1049 +               AC_DEFINE_UNQUOTED([PROC_MEM_FILE], "$proc_mem_file", [/proc/pid/mem interface])
1050 +       fi
1051 +
1052 +       if test "$have_ptrace" = "yes"; then
1053 +               FPM_SOURCES="$FPM_SOURCES fpm_trace.c fpm_trace_ptrace.c"
1054 +       elif test -n "$proc_mem_file"; then
1055 +               FPM_SOURCES="$FPM_SOURCES fpm_trace.c fpm_trace_pread.c"
1056 +       elif test "$have_mach_vm_read" = "yes" ; then
1057 +               FPM_SOURCES="$FPM_SOURCES fpm_trace.c fpm_trace_mach.c"
1058 +       fi
1059 +
1060 +])
1061 +
1062 +AC_DEFUN([AC_FPM_PRCTL],
1063 +[
1064 +       AC_MSG_CHECKING([for prctl])
1065 +
1066 +       AC_TRY_COMPILE([ #include <sys/prctl.h> ], [prctl(0, 0, 0, 0, 0);], [
1067 +               AC_DEFINE([HAVE_PRCTL], 1, [do we have prctl?])
1068 +               AC_MSG_RESULT([yes])
1069 +       ], [
1070 +               AC_MSG_RESULT([no])
1071 +       ])
1072 +])
1073 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/conf/php-fpm.conf.in php-5.2.17/sapi/cgi/fpm/conf/php-fpm.conf.in
1074 --- php-5.2.17.org/sapi/cgi/fpm/conf/php-fpm.conf.in    1970-01-01 01:00:00.000000000 +0100
1075 +++ php-5.2.17/sapi/cgi/fpm/conf/php-fpm.conf.in        2021-10-23 19:09:19.829791741 +0200
1076 @@ -0,0 +1,156 @@
1077 +<?xml version="1.0" ?>
1078 +<configuration>
1079 +
1080 +       All relative paths in this config are relative to php's install prefix
1081 +
1082 +       <section name="global_options">
1083 +
1084 +               Pid file
1085 +               <value name="pid_file">@php_fpm_pid_path@</value>
1086 +
1087 +               Error log file
1088 +               <value name="error_log">@php_fpm_log_path@</value>
1089 +
1090 +               Log level
1091 +               <value name="log_level">notice</value>
1092 +
1093 +               When this amount of php processes exited with SIGSEGV or SIGBUS ...
1094 +               <value name="emergency_restart_threshold">10</value>
1095 +
1096 +               ... in a less than this interval of time, a graceful restart will be initiated.
1097 +               Useful to work around accidental curruptions in accelerator's shared memory.
1098 +               <value name="emergency_restart_interval">1m</value>
1099 +
1100 +               Time limit on waiting child's reaction on signals from master
1101 +               <value name="process_control_timeout">5s</value>
1102 +
1103 +               Set to 'no' to debug fpm
1104 +               <value name="daemonize">yes</value>
1105 +
1106 +       </section>
1107 +
1108 +       <workers>
1109 +
1110 +               <section name="pool">
1111 +
1112 +                       Name of pool. Used in logs and stats.
1113 +                       <value name="name">default</value>
1114 +
1115 +                       Address to accept fastcgi requests on.
1116 +                       Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
1117 +                       <value name="listen_address">127.0.0.1:9000</value>
1118 +
1119 +                       <value name="listen_options">
1120 +
1121 +                               Set listen(2) backlog
1122 +                               <value name="backlog">-1</value>
1123 +
1124 +                               Set permissions for unix socket, if one used.
1125 +                               In Linux read/write permissions must be set in order to allow connections from web server.
1126 +                               Many BSD-derrived systems allow connections regardless of permissions.
1127 +                               <value name="owner"></value>
1128 +                               <value name="group"></value>
1129 +                               <value name="mode">0666</value>
1130 +                       </value>
1131 +
1132 +                       Additional php.ini defines, specific to this pool of workers.
1133 +                       <value name="php_defines">
1134 +               <!--            <value name="sendmail_path">/usr/sbin/sendmail -t -i</value>            -->
1135 +               <!--            <value name="display_errors">0</value>                                                          -->
1136 +                       </value>
1137 +
1138 +                       Unix user of processes
1139 +               <!--    <value name="user">nobody</value>                               -->
1140 +
1141 +                       Unix group of processes
1142 +               <!--    <value name="group">@php_fpm_group@</value>             -->
1143 +
1144 +                       Process manager settings
1145 +                       <value name="pm">
1146 +
1147 +                               Sets style of controling worker process count.
1148 +                               Valid values are 'static' and 'apache-like'
1149 +                               <value name="style">static</value>
1150 +
1151 +                               Sets the limit on the number of simultaneous requests that will be served.
1152 +                               Equivalent to Apache MaxClients directive.
1153 +                               Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
1154 +                               Used with any pm_style.
1155 +                               <value name="max_children">5</value>
1156 +
1157 +                               Settings group for 'apache-like' pm style
1158 +                               <value name="apache_like">
1159 +
1160 +                                       Sets the number of server processes created on startup.
1161 +                                       Used only when 'apache-like' pm_style is selected
1162 +                                       <value name="StartServers">20</value>
1163 +
1164 +                                       Sets the desired minimum number of idle server processes.
1165 +                                       Used only when 'apache-like' pm_style is selected
1166 +                                       <value name="MinSpareServers">5</value>
1167 +
1168 +                                       Sets the desired maximum number of idle server processes.
1169 +                                       Used only when 'apache-like' pm_style is selected
1170 +                                       <value name="MaxSpareServers">35</value>
1171 +
1172 +                               </value>
1173 +
1174 +                       </value>
1175 +
1176 +                       The timeout (in seconds) for serving a single request after which the worker process will be terminated
1177 +                       Should be used when 'max_execution_time' ini option does not stop script execution for some reason
1178 +                       '0s' means 'off'
1179 +                       <value name="request_terminate_timeout">0s</value>
1180 +
1181 +                       The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
1182 +                       '0s' means 'off'
1183 +                       <value name="request_slowlog_timeout">0s</value>
1184 +
1185 +                       The log file for slow requests
1186 +                       <value name="slowlog">logs/slow.log</value>
1187 +
1188 +                       Set open file desc rlimit
1189 +                       <value name="rlimit_files">1024</value>
1190 +
1191 +                       Set max core size rlimit
1192 +                       <value name="rlimit_core">0</value>
1193 +
1194 +                       Chroot to this directory at the start, absolute path
1195 +                       <value name="chroot"></value>
1196 +
1197 +                       Chdir to this directory at the start, absolute path
1198 +                       <value name="chdir"></value>
1199 +
1200 +                       Redirect workers' stdout and stderr into main error log.
1201 +                       If not set, they will be redirected to /dev/null, according to FastCGI specs
1202 +                       <value name="catch_workers_output">yes</value>
1203 +
1204 +                       How much requests each process should execute before respawn.
1205 +                       Useful to work around memory leaks in 3rd party libraries.
1206 +                       For endless request processing please specify 0
1207 +                       Equivalent to PHP_FCGI_MAX_REQUESTS
1208 +                       <value name="max_requests">500</value>
1209 +
1210 +                       Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
1211 +                       Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
1212 +                       Makes sense only with AF_INET listening socket.
1213 +                       <value name="allowed_clients">127.0.0.1</value>
1214 +
1215 +                       Pass environment variables like LD_LIBRARY_PATH
1216 +                       All $VARIABLEs are taken from current environment
1217 +                       <value name="environment">
1218 +                               <value name="HOSTNAME">$HOSTNAME</value>
1219 +                               <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
1220 +                               <value name="TMP">/tmp</value>
1221 +                               <value name="TMPDIR">/tmp</value>
1222 +                               <value name="TEMP">/tmp</value>
1223 +                               <value name="OSTYPE">$OSTYPE</value>
1224 +                               <value name="MACHTYPE">$MACHTYPE</value>
1225 +                               <value name="MALLOC_CHECK_">2</value>
1226 +                       </value>
1227 +
1228 +               </section>
1229 +
1230 +       </workers>
1231 +
1232 +</configuration>
1233 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/config.m4 php-5.2.17/sapi/cgi/fpm/config.m4
1234 --- php-5.2.17.org/sapi/cgi/fpm/config.m4       1970-01-01 01:00:00.000000000 +0100
1235 +++ php-5.2.17/sapi/cgi/fpm/config.m4   2021-10-23 19:09:19.829791741 +0200
1236 @@ -0,0 +1,117 @@
1237 +
1238 +FPM_VERSION="0.5.14"
1239 +
1240 +PHP_ARG_WITH(fpm-conf, for php-fpm config file path,
1241 +[  --with-fpm-conf=PATH        Set the path for php-fpm configuration file [PREFIX/etc/php-fpm.conf]], \$prefix/etc/php-fpm.conf, no)
1242 +
1243 +PHP_ARG_WITH(fpm-log, for php-fpm log file path,
1244 +[  --with-fpm-log=PATH         Set the path for php-fpm log file [PREFIX/logs/php-fpm.log]], \$prefix/logs/php-fpm.log, no)
1245 +
1246 +PHP_ARG_WITH(fpm-pid, for php-fpm pid file path,
1247 +[  --with-fpm-pid=PATH         Set the path for php-fpm pid file [PREFIX/logs/php-fpm.pid]], \$prefix/logs/php-fpm.pid, no)
1248 +
1249 +FPM_SOURCES="fpm.c \
1250 +       fpm_conf.c \
1251 +       fpm_signals.c \
1252 +       fpm_children.c \
1253 +       fpm_worker_pool.c \
1254 +       fpm_unix.c \
1255 +       fpm_cleanup.c \
1256 +       fpm_sockets.c \
1257 +       fpm_stdio.c \
1258 +       fpm_env.c \
1259 +       fpm_events.c \
1260 +       fpm_php.c \
1261 +       fpm_php_trace.c \
1262 +       fpm_process_ctl.c \
1263 +       fpm_request.c \
1264 +       fpm_clock.c \
1265 +       fpm_shm.c \
1266 +       fpm_shm_slots.c \
1267 +       xml_config.c \
1268 +       zlog.c"
1269 +
1270 +dnl AC_FPM_LIBEVENT
1271 +AC_FPM_LIBXML
1272 +AC_FPM_PRCTL
1273 +AC_FPM_CLOCK
1274 +AC_FPM_TRACE
1275 +dnl AC_FPM_JUDY
1276 +
1277 +LIBEVENT_CFLAGS=""
1278 +LIBEVENT_LIBS="-levent"
1279 +
1280 +SAPI_EXTRA_DEPS="$LIBEVENT_LIBS"
1281 +
1282 +FPM_CFLAGS="$LIBEVENT_CFLAGS $LIBXML_CFLAGS $JUDY_CFLAGS"
1283 +
1284 +dnl FPM_CFLAGS="$FPM_CFLAGS -DJUDYERROR_NOTEST" # for Judy
1285 +FPM_CFLAGS="$FPM_CFLAGS -I$abs_srcdir/sapi/cgi" # for fastcgi.h
1286 +
1287 +if test "$ICC" = "yes" ; then
1288 +       FPM_ADD_CFLAGS="-Wall -wd279,310,869,810,981"
1289 +elif test "$GCC" = "yes" ; then
1290 +       FPM_ADD_CFLAGS="-Wall -Wpointer-arith -Wno-unused-parameter -Wunused-variable -Wunused-value -fno-strict-aliasing"
1291 +fi
1292 +
1293 +if test -n "$FPM_WERROR" ; then
1294 +       FPM_ADD_CFLAGS="$FPM_ADD_CFLAGS -Werror"
1295 +fi
1296 +
1297 +FPM_CFLAGS="$FPM_ADD_CFLAGS $FPM_CFLAGS"
1298 +
1299 +PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/sapi/cgi/fpm/Makefile.frag)
1300 +
1301 +PHP_ADD_SOURCES(sapi/cgi/fpm, $FPM_SOURCES, $FPM_CFLAGS, sapi)
1302 +
1303 +PHP_ADD_BUILD_DIR(sapi/cgi/fpm)
1304 +
1305 +install_fpm="install-fpm"
1306 +
1307 +SAPI_EXTRA_LIBS="$LIBEVENT_LIBS $LIBXML_LIBS $JUDY_LIBS"
1308 +
1309 +
1310 +if test "$prefix" = "NONE" ; then
1311 +       fpm_prefix=/usr/local
1312 +else
1313 +       fpm_prefix="$prefix"
1314 +fi
1315 +
1316 +if test "$PHP_FPM_CONF" = "\$prefix/etc/php-fpm.conf" ; then
1317 +       php_fpm_conf_path="$fpm_prefix/etc/php-fpm.conf"
1318 +else
1319 +       php_fpm_conf_path="$PHP_FPM_CONF"
1320 +fi
1321 +
1322 +if test "$PHP_FPM_LOG" = "\$prefix/logs/php-fpm.log" ; then
1323 +       php_fpm_log_path="$fpm_prefix/logs/php-fpm.log"
1324 +else
1325 +       php_fpm_log_path="$PHP_FPM_LOG"
1326 +fi
1327 +
1328 +if test "$PHP_FPM_PID" = "\$prefix/logs/php-fpm.pid" ; then
1329 +       php_fpm_pid_path="$fpm_prefix/logs/php-fpm.pid"
1330 +else
1331 +       php_fpm_pid_path="$PHP_FPM_PID"
1332 +fi
1333 +
1334 +
1335 +if grep nobody /etc/group >/dev/null 2>&1; then
1336 +       php_fpm_group=nobody
1337 +else
1338 +       if grep nogroup /etc/group >/dev/null 2>&1; then
1339 +               php_fpm_group=nogroup
1340 +       else
1341 +               php_fpm_group=nobody
1342 +       fi
1343 +fi
1344 +
1345 +PHP_SUBST_OLD(php_fpm_conf_path)
1346 +PHP_SUBST_OLD(php_fpm_log_path)
1347 +PHP_SUBST_OLD(php_fpm_pid_path)
1348 +PHP_SUBST_OLD(php_fpm_group)
1349 +PHP_SUBST_OLD(FPM_VERSION)
1350 +
1351 +PHP_OUTPUT(sapi/cgi/fpm/fpm_autoconf.h)
1352 +PHP_OUTPUT(sapi/cgi/fpm/php-fpm.conf:sapi/cgi/fpm/conf/php-fpm.conf.in)
1353 +PHP_OUTPUT(sapi/cgi/fpm/php-fpm:sapi/cgi/fpm/init.d/php-fpm.in)
1354 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm.c php-5.2.17/sapi/cgi/fpm/fpm.c
1355 --- php-5.2.17.org/sapi/cgi/fpm/fpm.c   1970-01-01 01:00:00.000000000 +0100
1356 +++ php-5.2.17/sapi/cgi/fpm/fpm.c       2021-10-23 19:09:19.829791741 +0200
1357 @@ -0,0 +1,84 @@
1358 +
1359 +       /* $Id$ */
1360 +       /* (c) 2007,2008 Andrei Nigmatulin */
1361 +
1362 +#include "fpm_config.h"
1363 +
1364 +#include <stdlib.h> /* for exit */
1365 +
1366 +#include "fpm.h"
1367 +#include "fpm_children.h"
1368 +#include "fpm_signals.h"
1369 +#include "fpm_env.h"
1370 +#include "fpm_events.h"
1371 +#include "fpm_cleanup.h"
1372 +#include "fpm_php.h"
1373 +#include "fpm_sockets.h"
1374 +#include "fpm_unix.h"
1375 +#include "fpm_process_ctl.h"
1376 +#include "fpm_conf.h"
1377 +#include "fpm_worker_pool.h"
1378 +#include "fpm_stdio.h"
1379 +#include "zlog.h"
1380 +
1381 +int fpm;
1382 +
1383 +struct fpm_globals_s fpm_globals;
1384 +
1385 +int fpm_init(int argc, char **argv, char *config)
1386 +{
1387 +       fpm_globals.argc = argc;
1388 +       fpm_globals.argv = argv;
1389 +       fpm_globals.config = config;
1390 +
1391 +       if (0 > fpm_php_init_main()              ||
1392 +               0 > fpm_stdio_init_main()            ||
1393 +               0 > fpm_conf_init_main()             ||
1394 +               0 > fpm_unix_init_main()             ||
1395 +               0 > fpm_env_init_main()              ||
1396 +               0 > fpm_signals_init_main()          ||
1397 +               0 > fpm_pctl_init_main()             ||
1398 +               0 > fpm_children_init_main()         ||
1399 +               0 > fpm_sockets_init_main()          ||
1400 +               0 > fpm_worker_pool_init_main()      ||
1401 +               0 > fpm_event_init_main()) {
1402 +               return -1;
1403 +       }
1404 +
1405 +       if (0 > fpm_conf_write_pid()) {
1406 +               return -1;
1407 +       }
1408 +
1409 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "fpm is running, pid %d", (int) fpm_globals.parent_pid);
1410 +
1411 +       return 0;
1412 +}
1413 +
1414 +/*     children: return listening socket
1415 +       parent: never return */
1416 +int fpm_run(int *max_requests)
1417 +{
1418 +       struct fpm_worker_pool_s *wp;
1419 +
1420 +       /* create initial children in all pools */
1421 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1422 +               int is_parent;
1423 +
1424 +               is_parent = fpm_children_create_initial(wp);
1425 +
1426 +               if (!is_parent) {
1427 +                       goto run_child;
1428 +               }
1429 +       }
1430 +
1431 +       /* run event loop forever */
1432 +       fpm_event_loop();
1433 +
1434 +run_child: /* only workers reach this point */
1435 +
1436 +       fpm_cleanups_run(FPM_CLEANUP_CHILD);
1437 +
1438 +       *max_requests = fpm_globals.max_requests;
1439 +       return fpm_globals.listening_socket;
1440 +}
1441 +
1442 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm.h php-5.2.17/sapi/cgi/fpm/fpm.h
1443 --- php-5.2.17.org/sapi/cgi/fpm/fpm.h   1970-01-01 01:00:00.000000000 +0100
1444 +++ php-5.2.17/sapi/cgi/fpm/fpm.h       2021-10-23 19:09:19.829791741 +0200
1445 @@ -0,0 +1,30 @@
1446 +
1447 +       /* $Id$ */
1448 +       /* (c) 2007,2008 Andrei Nigmatulin */
1449 +
1450 +#ifndef FPM_H
1451 +#define FPM_H 1
1452 +
1453 +#include <unistd.h>
1454 +
1455 +int fpm_run(int *max_requests);
1456 +int fpm_init(int argc, char **argv, char *config);
1457 +
1458 +struct fpm_globals_s {
1459 +       pid_t parent_pid;
1460 +       int argc;
1461 +       char **argv;
1462 +       char *config;
1463 +       int running_children;
1464 +       int error_log_fd;
1465 +       int log_level;
1466 +       int listening_socket; /* for this child */
1467 +       int max_requests; /* for this child */
1468 +       int is_child;
1469 +};
1470 +
1471 +extern struct fpm_globals_s fpm_globals;
1472 +
1473 +extern int fpm;
1474 +
1475 +#endif
1476 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_arrays.h php-5.2.17/sapi/cgi/fpm/fpm_arrays.h
1477 --- php-5.2.17.org/sapi/cgi/fpm/fpm_arrays.h    1970-01-01 01:00:00.000000000 +0100
1478 +++ php-5.2.17/sapi/cgi/fpm/fpm_arrays.h        2021-10-23 19:09:19.829791741 +0200
1479 @@ -0,0 +1,110 @@
1480 +
1481 +       /* $Id$ */
1482 +       /* (c) 2007,2008 Andrei Nigmatulin */
1483 +
1484 +#ifndef FPM_ARRAYS_H
1485 +#define FPM_ARRAYS_H 1
1486 +
1487 +#include <stdlib.h>
1488 +#include <string.h>
1489 +
1490 +struct fpm_array_s {
1491 +       void *data;
1492 +       size_t sz;
1493 +       size_t used;
1494 +       size_t allocated;
1495 +};
1496 +
1497 +static inline struct fpm_array_s *fpm_array_init(struct fpm_array_s *a, unsigned int sz, unsigned int initial_num)
1498 +{
1499 +       void *allocated = 0;
1500 +
1501 +       if (!a) {
1502 +               a = malloc(sizeof(struct fpm_array_s));
1503 +
1504 +               if (!a) {
1505 +                       return 0;
1506 +               }
1507 +
1508 +               allocated = a;
1509 +       }
1510 +
1511 +       a->sz = sz;
1512 +
1513 +       a->data = calloc(sz, initial_num);
1514 +
1515 +       if (!a->data) {
1516 +               free(allocated);
1517 +               return 0;
1518 +       }
1519 +
1520 +       a->allocated = initial_num;
1521 +       a->used = 0;
1522 +
1523 +       return a;
1524 +}
1525 +
1526 +static inline void *fpm_array_item(struct fpm_array_s *a, unsigned int n)
1527 +{
1528 +       char *ret;
1529 +
1530 +       ret = (char *) a->data + a->sz * n;
1531 +
1532 +       return ret;
1533 +}
1534 +
1535 +static inline void *fpm_array_item_last(struct fpm_array_s *a)
1536 +{
1537 +       return fpm_array_item(a, a->used - 1);
1538 +}
1539 +
1540 +static inline int fpm_array_item_remove(struct fpm_array_s *a, unsigned int n)
1541 +{
1542 +       int ret = -1;
1543 +
1544 +       if (n < a->used - 1) {
1545 +               void *last = fpm_array_item(a, a->used - 1);
1546 +               void *to_remove = fpm_array_item(a, n);
1547 +
1548 +               memcpy(to_remove, last, a->sz);
1549 +
1550 +               ret = n;
1551 +       }
1552 +
1553 +       --a->used;
1554 +
1555 +       return ret;
1556 +}
1557 +
1558 +static inline void *fpm_array_push(struct fpm_array_s *a)
1559 +{
1560 +       void *ret;
1561 +
1562 +       if (a->used == a->allocated) {
1563 +               size_t new_allocated = a->allocated ? a->allocated * 2 : 20;
1564 +               void *new_ptr = realloc(a->data, a->sz * new_allocated);
1565 +
1566 +               if (!new_ptr) {
1567 +                       return 0;
1568 +               }
1569 +
1570 +               a->data = new_ptr;
1571 +               a->allocated = new_allocated;
1572 +       }
1573 +
1574 +       ret = fpm_array_item(a, a->used);
1575 +
1576 +       ++a->used;
1577 +
1578 +       return ret;
1579 +}
1580 +
1581 +static inline void fpm_array_free(struct fpm_array_s *a)
1582 +{
1583 +       free(a->data);
1584 +       a->data = 0;
1585 +       a->sz = 0;
1586 +       a->used = a->allocated = 0;
1587 +}
1588 +
1589 +#endif
1590 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_atomic.h php-5.2.17/sapi/cgi/fpm/fpm_atomic.h
1591 --- php-5.2.17.org/sapi/cgi/fpm/fpm_atomic.h    1970-01-01 01:00:00.000000000 +0100
1592 +++ php-5.2.17/sapi/cgi/fpm/fpm_atomic.h        2021-10-23 19:09:19.829791741 +0200
1593 @@ -0,0 +1,85 @@
1594 +
1595 +       /* $Id$ */
1596 +       /* (c) 2007,2008 Andrei Nigmatulin */
1597 +
1598 +#ifndef FPM_ATOMIC_H
1599 +#define FPM_ATOMIC_H 1
1600 +
1601 +#include <stdint.h>
1602 +#include <sched.h>
1603 +
1604 +#if ( __i386__ || __i386 )
1605 +
1606 +typedef int32_t                     atomic_int_t;
1607 +typedef uint32_t                    atomic_uint_t;
1608 +typedef volatile atomic_uint_t      atomic_t;
1609 +
1610 +
1611 +static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
1612 +{
1613 +       __asm__ volatile ( "lock;" "xaddl %0, %1;" :
1614 +               "+r" (add) : "m" (*value) : "memory");
1615 +
1616 +       return add;
1617 +}
1618 +
1619 +static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
1620 +{
1621 +       unsigned char res;
1622 +
1623 +       __asm__ volatile ( "lock;" "cmpxchgl %3, %1;" "sete %0;" :
1624 +               "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
1625 +
1626 +    return res;
1627 +}
1628 +
1629 +#elif ( __amd64__ || __amd64 )
1630 +
1631 +typedef int64_t                     atomic_int_t;
1632 +typedef uint64_t                    atomic_uint_t;
1633 +typedef volatile atomic_uint_t      atomic_t;
1634 +
1635 +static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
1636 +{
1637 +       __asm__ volatile ( "lock;" "xaddq %0, %1;" :
1638 +               "+r" (add) : "m" (*value) : "memory");
1639 +
1640 +       return add;
1641 +}
1642 +
1643 +static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
1644 +{
1645 +       unsigned char res;
1646 +
1647 +       __asm__ volatile ( "lock;" "cmpxchgq %3, %1;" "sete %0;" :
1648 +               "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
1649 +
1650 +       return res;
1651 +}
1652 +
1653 +#else
1654 +
1655 +#error unsupported processor. please write a patch and send it to me
1656 +
1657 +#endif
1658 +
1659 +static inline int fpm_spinlock(atomic_t *lock, int try_once)
1660 +{
1661 +       if (try_once) {
1662 +               return atomic_cmp_set(lock, 0, 1) ? 0 : -1;
1663 +       }
1664 +
1665 +       for (;;) {
1666 +
1667 +               if (atomic_cmp_set(lock, 0, 1)) {
1668 +                       break;
1669 +               }
1670 +
1671 +               sched_yield();
1672 +       }
1673 +
1674 +       return 0;
1675 +}
1676 +
1677 +#endif
1678 +
1679 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_autoconf.h.in php-5.2.17/sapi/cgi/fpm/fpm_autoconf.h.in
1680 --- php-5.2.17.org/sapi/cgi/fpm/fpm_autoconf.h.in       1970-01-01 01:00:00.000000000 +0100
1681 +++ php-5.2.17/sapi/cgi/fpm/fpm_autoconf.h.in   2021-10-23 19:09:19.829791741 +0200
1682 @@ -0,0 +1,9 @@
1683 +
1684 +       /* $Id$ */
1685 +       /* (c) 2007,2008 Andrei Nigmatulin */
1686 +
1687 +#define PHP_FPM_VERSION   "@FPM_VERSION@"
1688 +#define PHP_FPM_CONF_PATH "@php_fpm_conf_path@"
1689 +#define PHP_FPM_LOG_PATH  "@php_fpm_log_path@"
1690 +#define PHP_FPM_PID_PATH  "@php_fpm_pid_path@"
1691 +
1692 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_children.c php-5.2.17/sapi/cgi/fpm/fpm_children.c
1693 --- php-5.2.17.org/sapi/cgi/fpm/fpm_children.c  1970-01-01 01:00:00.000000000 +0100
1694 +++ php-5.2.17/sapi/cgi/fpm/fpm_children.c      2021-10-23 19:09:19.829791741 +0200
1695 @@ -0,0 +1,385 @@
1696 +
1697 +       /* $Id$ */
1698 +       /* (c) 2007,2008 Andrei Nigmatulin */
1699 +
1700 +#include "fpm_config.h"
1701 +
1702 +#include <sys/types.h>
1703 +#include <sys/wait.h>
1704 +#include <time.h>
1705 +#include <unistd.h>
1706 +#include <string.h>
1707 +#include <stdio.h>
1708 +
1709 +#include "fpm.h"
1710 +#include "fpm_children.h"
1711 +#include "fpm_signals.h"
1712 +#include "fpm_worker_pool.h"
1713 +#include "fpm_sockets.h"
1714 +#include "fpm_process_ctl.h"
1715 +#include "fpm_php.h"
1716 +#include "fpm_conf.h"
1717 +#include "fpm_cleanup.h"
1718 +#include "fpm_events.h"
1719 +#include "fpm_clock.h"
1720 +#include "fpm_stdio.h"
1721 +#include "fpm_unix.h"
1722 +#include "fpm_env.h"
1723 +#include "fpm_shm_slots.h"
1724 +
1725 +#include "zlog.h"
1726 +
1727 +static time_t *last_faults;
1728 +static int fault;
1729 +
1730 +static int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop);
1731 +
1732 +static void fpm_children_cleanup(int which, void *arg)
1733 +{
1734 +       free(last_faults);
1735 +}
1736 +
1737 +static struct fpm_child_s *fpm_child_alloc()
1738 +{
1739 +       struct fpm_child_s *ret;
1740 +
1741 +       ret = malloc(sizeof(struct fpm_child_s));
1742 +
1743 +       if (!ret) return 0;
1744 +
1745 +       memset(ret, 0, sizeof(*ret));
1746 +
1747 +       return ret;
1748 +}
1749 +
1750 +static void fpm_child_free(struct fpm_child_s *child)
1751 +{
1752 +       free(child);
1753 +}
1754 +
1755 +static void fpm_child_close(struct fpm_child_s *child, int in_event_loop)
1756 +{
1757 +       if (child->fd_stdout != -1) {
1758 +               if (in_event_loop) {
1759 +                       fpm_event_fire(&child->ev_stdout);
1760 +               }
1761 +               if (child->fd_stdout != -1) {
1762 +                       close(child->fd_stdout);
1763 +               }
1764 +       }
1765 +
1766 +       if (child->fd_stderr != -1) {
1767 +               if (in_event_loop) {
1768 +                       fpm_event_fire(&child->ev_stderr);
1769 +               }
1770 +               if (child->fd_stderr != -1) {
1771 +                       close(child->fd_stderr);
1772 +               }
1773 +       }
1774 +
1775 +       fpm_child_free(child);
1776 +}
1777 +
1778 +static void fpm_child_link(struct fpm_child_s *child)
1779 +{
1780 +       struct fpm_worker_pool_s *wp = child->wp;
1781 +
1782 +       ++wp->running_children;
1783 +       ++fpm_globals.running_children;
1784 +
1785 +       child->next = wp->children;
1786 +       if (child->next) child->next->prev = child;
1787 +       child->prev = 0;
1788 +       wp->children = child;
1789 +}
1790 +
1791 +static void fpm_child_unlink(struct fpm_child_s *child)
1792 +{
1793 +       --child->wp->running_children;
1794 +       --fpm_globals.running_children;
1795 +
1796 +       if (child->prev) child->prev->next = child->next;
1797 +       else child->wp->children = child->next;
1798 +       if (child->next) child->next->prev = child->prev;
1799 +
1800 +}
1801 +
1802 +static struct fpm_child_s *fpm_child_find(pid_t pid)
1803 +{
1804 +       struct fpm_worker_pool_s *wp;
1805 +       struct fpm_child_s *child = 0;
1806 +
1807 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1808 +
1809 +               for (child = wp->children; child; child = child->next) {
1810 +                       if (child->pid == pid) {
1811 +                               break;
1812 +                       }
1813 +               }
1814 +
1815 +               if (child) break;
1816 +       }
1817 +
1818 +       if (!child) {
1819 +               return 0;
1820 +       }
1821 +
1822 +       return child;
1823 +}
1824 +
1825 +static void fpm_child_init(struct fpm_worker_pool_s *wp)
1826 +{
1827 +       fpm_globals.max_requests = wp->config->max_requests;
1828 +
1829 +       if (0 > fpm_stdio_init_child(wp) ||
1830 +               0 > fpm_unix_init_child(wp) ||
1831 +               0 > fpm_signals_init_child() ||
1832 +               0 > fpm_env_init_child(wp) ||
1833 +               0 > fpm_php_init_child(wp)) {
1834 +
1835 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "child failed to initialize (pool %s)", wp->config->name);
1836 +               exit(255);
1837 +       }
1838 +}
1839 +
1840 +int fpm_children_free(struct fpm_child_s *child)
1841 +{
1842 +       struct fpm_child_s *next;
1843 +
1844 +       for (; child; child = next) {
1845 +               next = child->next;
1846 +               fpm_child_close(child, 0 /* in_event_loop */);
1847 +       }
1848 +
1849 +       return 0;
1850 +}
1851 +
1852 +void fpm_children_bury()
1853 +{
1854 +       int status;
1855 +       pid_t pid;
1856 +       struct fpm_child_s *child;
1857 +
1858 +       while ( (pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
1859 +               char buf[128];
1860 +               int severity = ZLOG_NOTICE;
1861 +
1862 +               child = fpm_child_find(pid);
1863 +
1864 +               if (WIFEXITED(status)) {
1865 +
1866 +                       snprintf(buf, sizeof(buf), "with code %d", WEXITSTATUS(status));
1867 +
1868 +                       if (WEXITSTATUS(status) != 0) {
1869 +                               severity = ZLOG_WARNING;
1870 +                       }
1871 +
1872 +               }
1873 +               else if (WIFSIGNALED(status)) {
1874 +                       const char *signame = fpm_signal_names[WTERMSIG(status)];
1875 +                       const char *have_core = WCOREDUMP(status) ? " (core dumped)" : "";
1876 +
1877 +                       if (signame == NULL) {
1878 +                               signame = "";
1879 +                       }
1880 +
1881 +                       snprintf(buf, sizeof(buf), "on signal %d %s%s", WTERMSIG(status), signame, have_core);
1882 +
1883 +                       if (WTERMSIG(status) != SIGQUIT) { /* possible request loss */
1884 +                               severity = ZLOG_WARNING;
1885 +                       }
1886 +               }
1887 +               else if (WIFSTOPPED(status)) {
1888 +
1889 +                       zlog(ZLOG_STUFF, ZLOG_NOTICE, "child %d stopped for tracing", (int) pid);
1890 +
1891 +                       if (child && child->tracer) {
1892 +                               child->tracer(child);
1893 +                       }
1894 +
1895 +                       continue;
1896 +               }
1897 +
1898 +               if (child) {
1899 +                       struct fpm_worker_pool_s *wp = child->wp;
1900 +                       struct timeval tv1, tv2;
1901 +
1902 +                       fpm_child_unlink(child);
1903 +
1904 +                       fpm_shm_slots_discard_slot(child);
1905 +
1906 +                       fpm_clock_get(&tv1);
1907 +
1908 +                       timersub(&tv1, &child->started, &tv2);
1909 +
1910 +                       zlog(ZLOG_STUFF, severity, "child %d (pool %s) exited %s after %ld.%06d seconds from start", (int) pid,
1911 +                                               child->wp->config->name, buf, tv2.tv_sec, (int) tv2.tv_usec);
1912 +
1913 +                       fpm_child_close(child, 1 /* in event_loop */);
1914 +
1915 +                       fpm_pctl_child_exited();
1916 +
1917 +                       if (last_faults && (WTERMSIG(status) == SIGSEGV || WTERMSIG(status) == SIGBUS)) {
1918 +                               time_t now = tv1.tv_sec;
1919 +                               int restart_condition = 1;
1920 +                               int i;
1921 +
1922 +                               last_faults[fault++] = now;
1923 +
1924 +                               if (fault == fpm_global_config.emergency_restart_threshold) {
1925 +                                       fault = 0;
1926 +                               }
1927 +
1928 +                               for (i = 0; i < fpm_global_config.emergency_restart_threshold; i++) {
1929 +                                       if (now - last_faults[i] > fpm_global_config.emergency_restart_interval) {
1930 +                                               restart_condition = 0;
1931 +                                               break;
1932 +                                       }
1933 +                               }
1934 +
1935 +                               if (restart_condition) {
1936 +
1937 +                                       zlog(ZLOG_STUFF, ZLOG_WARNING, "failed processes threshold (%d in %d sec) is reached, initiating reload",
1938 +                                               fpm_global_config.emergency_restart_threshold, fpm_global_config.emergency_restart_interval);
1939 +
1940 +                                       fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
1941 +                               }
1942 +                       }
1943 +
1944 +                       fpm_children_make(wp, 1 /* in event loop */);
1945 +
1946 +                       if (fpm_globals.is_child) {
1947 +                               break;
1948 +                       }
1949 +               }
1950 +               else {
1951 +                       zlog(ZLOG_STUFF, ZLOG_ALERT, "oops, unknown child exited %s", buf);
1952 +               }
1953 +       }
1954 +
1955 +}
1956 +
1957 +static struct fpm_child_s *fpm_resources_prepare(struct fpm_worker_pool_s *wp)
1958 +{
1959 +       struct fpm_child_s *c;
1960 +
1961 +       c = fpm_child_alloc();
1962 +
1963 +       if (!c) {
1964 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "malloc failed (pool %s)", wp->config->name);
1965 +               return 0;
1966 +       }
1967 +
1968 +       c->wp = wp;
1969 +       c->fd_stdout = -1; c->fd_stderr = -1;
1970 +
1971 +       if (0 > fpm_stdio_prepare_pipes(c)) {
1972 +               fpm_child_free(c);
1973 +               return 0;
1974 +       }
1975 +
1976 +       if (0 > fpm_shm_slots_prepare_slot(c)) {
1977 +               fpm_stdio_discard_pipes(c);
1978 +               fpm_child_free(c);
1979 +               return 0;
1980 +       }
1981 +
1982 +       return c;
1983 +}
1984 +
1985 +static void fpm_resources_discard(struct fpm_child_s *child)
1986 +{
1987 +       fpm_shm_slots_discard_slot(child);
1988 +       fpm_stdio_discard_pipes(child);
1989 +       fpm_child_free(child);
1990 +}
1991 +
1992 +static void fpm_child_resources_use(struct fpm_child_s *child)
1993 +{
1994 +       fpm_shm_slots_child_use_slot(child);
1995 +       fpm_stdio_child_use_pipes(child);
1996 +       fpm_child_free(child);
1997 +}
1998 +
1999 +static void fpm_parent_resources_use(struct fpm_child_s *child)
2000 +{
2001 +       fpm_shm_slots_parent_use_slot(child);
2002 +       fpm_stdio_parent_use_pipes(child);
2003 +       fpm_child_link(child);
2004 +}
2005 +
2006 +static int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop)
2007 +{
2008 +       int enough = 0;
2009 +       pid_t pid;
2010 +       struct fpm_child_s *child;
2011 +
2012 +       while (!enough && fpm_pctl_can_spawn_children() && wp->running_children < wp->config->pm->max_children) {
2013 +
2014 +               child = fpm_resources_prepare(wp);
2015 +
2016 +               if (!child) {
2017 +                       enough = 1;
2018 +                       break;
2019 +               }
2020 +
2021 +               pid = fork();
2022 +
2023 +               switch (pid) {
2024 +
2025 +                       case 0 :
2026 +                               fpm_child_resources_use(child);
2027 +                               fpm_globals.is_child = 1;
2028 +                               if (in_event_loop) {
2029 +                                       fpm_event_exit_loop();
2030 +                               }
2031 +                               fpm_child_init(wp);
2032 +                               return 0;
2033 +
2034 +                       case -1 :
2035 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fork() failed");
2036 +                               enough = 1;
2037 +
2038 +                               fpm_resources_discard(child);
2039 +
2040 +                               break; /* dont try any more on error */
2041 +
2042 +                       default :
2043 +                               child->pid = pid;
2044 +                               fpm_clock_get(&child->started);
2045 +                               fpm_parent_resources_use(child);
2046 +
2047 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "child %d (pool %s) started", (int) pid, wp->config->name);
2048 +               }
2049 +
2050 +       }
2051 +
2052 +       return 1; /* we are done */
2053 +}
2054 +
2055 +int fpm_children_create_initial(struct fpm_worker_pool_s *wp)
2056 +{
2057 +       return fpm_children_make(wp, 0 /* not in event loop yet */);
2058 +}
2059 +
2060 +int fpm_children_init_main()
2061 +{
2062 +       if (fpm_global_config.emergency_restart_threshold &&
2063 +               fpm_global_config.emergency_restart_interval) {
2064 +
2065 +               last_faults = malloc(sizeof(time_t) * fpm_global_config.emergency_restart_threshold);
2066 +
2067 +               if (!last_faults) {
2068 +                       return -1;
2069 +               }
2070 +
2071 +               memset(last_faults, 0, sizeof(time_t) * fpm_global_config.emergency_restart_threshold);
2072 +       }
2073 +
2074 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_children_cleanup, 0)) {
2075 +               return -1;
2076 +       }
2077 +
2078 +       return 0;
2079 +}
2080 +
2081 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_children.h php-5.2.17/sapi/cgi/fpm/fpm_children.h
2082 --- php-5.2.17.org/sapi/cgi/fpm/fpm_children.h  1970-01-01 01:00:00.000000000 +0100
2083 +++ php-5.2.17/sapi/cgi/fpm/fpm_children.h      2021-10-23 19:09:19.829791741 +0200
2084 @@ -0,0 +1,33 @@
2085 +
2086 +       /* $Id$ */
2087 +       /* (c) 2007,2008 Andrei Nigmatulin */
2088 +
2089 +#ifndef FPM_CHILDREN_H
2090 +#define FPM_CHILDREN_H 1
2091 +
2092 +#include <sys/time.h>
2093 +#include <sys/types.h>
2094 +#include <event.h>
2095 +
2096 +#include "fpm_worker_pool.h"
2097 +
2098 +int fpm_children_create_initial(struct fpm_worker_pool_s *wp);
2099 +int fpm_children_free(struct fpm_child_s *child);
2100 +void fpm_children_bury();
2101 +int fpm_children_init_main();
2102 +
2103 +struct fpm_child_s;
2104 +
2105 +struct fpm_child_s {
2106 +       struct fpm_child_s *prev, *next;
2107 +       struct timeval started;
2108 +       struct fpm_worker_pool_s *wp;
2109 +       struct event ev_stdout, ev_stderr;
2110 +       int shm_slot_i;
2111 +       int fd_stdout, fd_stderr;
2112 +       void (*tracer)(struct fpm_child_s *);
2113 +       struct timeval slow_logged;
2114 +       pid_t pid;
2115 +};
2116 +
2117 +#endif
2118 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_cleanup.c php-5.2.17/sapi/cgi/fpm/fpm_cleanup.c
2119 --- php-5.2.17.org/sapi/cgi/fpm/fpm_cleanup.c   1970-01-01 01:00:00.000000000 +0100
2120 +++ php-5.2.17/sapi/cgi/fpm/fpm_cleanup.c       2021-10-23 19:09:19.829791741 +0200
2121 @@ -0,0 +1,51 @@
2122 +
2123 +       /* $Id$ */
2124 +       /* (c) 2007,2008 Andrei Nigmatulin */
2125 +
2126 +#include "fpm_config.h"
2127 +
2128 +#include <stdlib.h>
2129 +
2130 +#include "fpm_arrays.h"
2131 +#include "fpm_cleanup.h"
2132 +#include "zlog.h"
2133 +
2134 +struct cleanup_s {
2135 +       int type;
2136 +       void (*cleanup)(int, void *);
2137 +       void *arg;
2138 +};
2139 +
2140 +static struct fpm_array_s cleanups = { .sz = sizeof(struct cleanup_s) };
2141 +
2142 +int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *arg)
2143 +{
2144 +       struct cleanup_s *c;
2145 +
2146 +       c = fpm_array_push(&cleanups);
2147 +
2148 +       if (!c) {
2149 +               return -1;
2150 +       }
2151 +
2152 +       c->type = type;
2153 +       c->cleanup = cleanup;
2154 +       c->arg = arg;
2155 +
2156 +       return 0;
2157 +}
2158 +
2159 +void fpm_cleanups_run(int type)
2160 +{
2161 +       struct cleanup_s *c = fpm_array_item_last(&cleanups);
2162 +       int cl = cleanups.used;
2163 +
2164 +       for ( ; cl--; c--) {
2165 +               if (c->type & type) {
2166 +                       c->cleanup(type, c->arg);
2167 +               }
2168 +       }
2169 +
2170 +       fpm_array_free(&cleanups);
2171 +}
2172 +
2173 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_cleanup.h php-5.2.17/sapi/cgi/fpm/fpm_cleanup.h
2174 --- php-5.2.17.org/sapi/cgi/fpm/fpm_cleanup.h   1970-01-01 01:00:00.000000000 +0100
2175 +++ php-5.2.17/sapi/cgi/fpm/fpm_cleanup.h       2021-10-23 19:09:19.829791741 +0200
2176 @@ -0,0 +1,21 @@
2177 +
2178 +       /* $Id$ */
2179 +       /* (c) 2007,2008 Andrei Nigmatulin */
2180 +
2181 +#ifndef FPM_CLEANUP_H
2182 +#define FPM_CLEANUP_H 1
2183 +
2184 +int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *);
2185 +void fpm_cleanups_run(int type);
2186 +
2187 +enum {
2188 +       FPM_CLEANUP_CHILD                                       = (1 << 0),
2189 +       FPM_CLEANUP_PARENT_EXIT                         = (1 << 1),
2190 +       FPM_CLEANUP_PARENT_EXIT_MAIN            = (1 << 2),
2191 +       FPM_CLEANUP_PARENT_EXEC                         = (1 << 3),
2192 +       FPM_CLEANUP_PARENT                                      = (1 << 1) | (1 << 2) | (1 << 3),
2193 +       FPM_CLEANUP_ALL                                         = ~0,
2194 +};
2195 +
2196 +#endif
2197 +
2198 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_clock.c php-5.2.17/sapi/cgi/fpm/fpm_clock.c
2199 --- php-5.2.17.org/sapi/cgi/fpm/fpm_clock.c     1970-01-01 01:00:00.000000000 +0100
2200 +++ php-5.2.17/sapi/cgi/fpm/fpm_clock.c 2021-10-23 19:09:19.829791741 +0200
2201 @@ -0,0 +1,115 @@
2202 +
2203 +       /* $Id$ */
2204 +       /* (c) 2007,2008 Andrei Nigmatulin */
2205 +
2206 +#include "fpm_config.h"
2207 +
2208 +#if defined(HAVE_CLOCK_GETTIME)
2209 +#include <time.h> /* for CLOCK_MONOTONIC */
2210 +#endif
2211 +
2212 +#include "fpm_clock.h"
2213 +#include "zlog.h"
2214 +
2215 +
2216 +/* posix monotonic clock - preferred source of time */
2217 +#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
2218 +
2219 +static int monotonic_works;
2220 +
2221 +int fpm_clock_init()
2222 +{
2223 +       struct timespec ts;
2224 +
2225 +       monotonic_works = 0;
2226 +
2227 +       if (0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
2228 +               monotonic_works = 1;
2229 +       }
2230 +
2231 +       return 0;
2232 +}
2233 +
2234 +int fpm_clock_get(struct timeval *tv)
2235 +{
2236 +       if (monotonic_works) {
2237 +               struct timespec ts;
2238 +
2239 +               if (0 > clock_gettime(CLOCK_MONOTONIC, &ts)) {
2240 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "clock_gettime() failed");
2241 +                       return -1;
2242 +               }
2243 +
2244 +               tv->tv_sec = ts.tv_sec;
2245 +               tv->tv_usec = ts.tv_nsec / 1000;
2246 +               return 0;
2247 +       }
2248 +
2249 +       return gettimeofday(tv, 0);
2250 +}
2251 +
2252 +/* macosx clock */
2253 +#elif defined(HAVE_CLOCK_GET_TIME)
2254 +
2255 +#include <mach/mach.h>
2256 +#include <mach/clock.h>
2257 +#include <mach/mach_error.h>
2258 +
2259 +static clock_serv_t mach_clock;
2260 +
2261 +/* this code borrowed from here: http://lists.apple.com/archives/Darwin-development/2002/Mar/msg00746.html */
2262 +/* mach_clock also should be re-initialized in child process after fork */
2263 +int fpm_clock_init()
2264 +{
2265 +       kern_return_t ret;
2266 +       mach_timespec_t aTime;
2267 +
2268 +       ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &mach_clock);
2269 +
2270 +       if (ret != KERN_SUCCESS) {
2271 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "host_get_clock_service() failed: %s", mach_error_string(ret));
2272 +               return -1;
2273 +       }
2274 +
2275 +       /* test if it works */
2276 +       ret = clock_get_time(mach_clock, &aTime);
2277 +
2278 +       if (ret != KERN_SUCCESS) {
2279 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
2280 +               return -1;
2281 +       }
2282 +
2283 +       return 0;
2284 +}
2285 +
2286 +int fpm_clock_get(struct timeval *tv)
2287 +{
2288 +       kern_return_t ret;
2289 +       mach_timespec_t aTime;
2290 +
2291 +       ret = clock_get_time(mach_clock, &aTime);
2292 +
2293 +       if (ret != KERN_SUCCESS) {
2294 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
2295 +               return -1;
2296 +       }
2297 +
2298 +       tv->tv_sec = aTime.tv_sec;
2299 +       tv->tv_usec = aTime.tv_nsec / 1000;
2300 +
2301 +       return 0;
2302 +}
2303 +
2304 +#else /* no clock */
2305 +
2306 +int fpm_clock_init()
2307 +{
2308 +       return 0;
2309 +}
2310 +
2311 +int fpm_clock_get(struct timeval *tv)
2312 +{
2313 +       return gettimeofday(tv, 0);
2314 +}
2315 +
2316 +#endif
2317 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_clock.h php-5.2.17/sapi/cgi/fpm/fpm_clock.h
2318 --- php-5.2.17.org/sapi/cgi/fpm/fpm_clock.h     1970-01-01 01:00:00.000000000 +0100
2319 +++ php-5.2.17/sapi/cgi/fpm/fpm_clock.h 2021-10-23 19:09:19.829791741 +0200
2320 @@ -0,0 +1,13 @@
2321 +
2322 +       /* $Id$ */
2323 +       /* (c) 2007,2008 Andrei Nigmatulin */
2324 +
2325 +#ifndef FPM_CLOCK_H
2326 +#define FPM_CLOCK_H 1
2327 +
2328 +#include <sys/time.h>
2329 +
2330 +int fpm_clock_init();
2331 +int fpm_clock_get(struct timeval *tv);
2332 +
2333 +#endif
2334 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_conf.c php-5.2.17/sapi/cgi/fpm/fpm_conf.c
2335 --- php-5.2.17.org/sapi/cgi/fpm/fpm_conf.c      1970-01-01 01:00:00.000000000 +0100
2336 +++ php-5.2.17/sapi/cgi/fpm/fpm_conf.c  2021-10-23 19:09:19.829791741 +0200
2337 @@ -0,0 +1,532 @@
2338 +
2339 +       /* $Id$ */
2340 +       /* (c) 2007,2008 Andrei Nigmatulin */
2341 +
2342 +#include "fpm_config.h"
2343 +
2344 +#include <sys/types.h>
2345 +#include <sys/stat.h>
2346 +#include <fcntl.h>
2347 +#include <string.h>
2348 +#include <stdlib.h>
2349 +#include <stddef.h>
2350 +#include <stdint.h>
2351 +#include <stdio.h>
2352 +#include <unistd.h>
2353 +
2354 +#include "fpm.h"
2355 +#include "fpm_conf.h"
2356 +#include "fpm_stdio.h"
2357 +#include "fpm_worker_pool.h"
2358 +#include "fpm_cleanup.h"
2359 +#include "fpm_php.h"
2360 +#include "fpm_sockets.h"
2361 +#include "xml_config.h"
2362 +#include "zlog.h"
2363 +
2364 +
2365 +struct fpm_global_config_s fpm_global_config;
2366 +
2367 +static void *fpm_global_config_ptr()
2368 +{
2369 +       return &fpm_global_config;
2370 +}
2371 +
2372 +static char *fpm_conf_set_log_level(void **conf, char *name, void *vv, intptr_t offset)
2373 +{
2374 +       char *value = vv;
2375 +
2376 +       if (!strcmp(value, "debug")) {
2377 +               fpm_globals.log_level = ZLOG_DEBUG;
2378 +       }
2379 +       else if (!strcmp(value, "notice")) {
2380 +               fpm_globals.log_level = ZLOG_NOTICE;
2381 +       }
2382 +       else if (!strcmp(value, "warn")) {
2383 +               fpm_globals.log_level = ZLOG_WARNING;
2384 +       }
2385 +       else if (!strcmp(value, "error")) {
2386 +               fpm_globals.log_level = ZLOG_ERROR;
2387 +       }
2388 +       else if (!strcmp(value, "alert")) {
2389 +               fpm_globals.log_level = ZLOG_ALERT;
2390 +       }
2391 +       else {
2392 +               return "invalid value for 'log_level'";
2393 +       }
2394 +
2395 +       return NULL;
2396 +}
2397 +
2398 +static struct xml_conf_section xml_section_fpm_global_options = {
2399 +       .conf = &fpm_global_config_ptr,
2400 +       .path = "/configuration/global_options",
2401 +       .parsers = (struct xml_value_parser []) {
2402 +               { XML_CONF_SCALAR,      "emergency_restart_threshold",          &xml_conf_set_slot_integer,             offsetof(struct fpm_global_config_s, emergency_restart_threshold) },
2403 +               { XML_CONF_SCALAR,      "emergency_restart_interval",           &xml_conf_set_slot_time,                offsetof(struct fpm_global_config_s, emergency_restart_interval) },
2404 +               { XML_CONF_SCALAR,      "process_control_timeout",                      &xml_conf_set_slot_time,                offsetof(struct fpm_global_config_s, process_control_timeout) },
2405 +               { XML_CONF_SCALAR,      "daemonize",                                            &xml_conf_set_slot_boolean,             offsetof(struct fpm_global_config_s, daemonize) },
2406 +               { XML_CONF_SCALAR,      "pid_file",                                                     &xml_conf_set_slot_string,              offsetof(struct fpm_global_config_s, pid_file) },
2407 +               { XML_CONF_SCALAR,      "error_log",                                            &xml_conf_set_slot_string,              offsetof(struct fpm_global_config_s, error_log) },
2408 +               { XML_CONF_SCALAR,  "log_level",                                                &fpm_conf_set_log_level,                0 },
2409 +               { 0, 0, 0, 0 }
2410 +       }
2411 +};
2412 +
2413 +static char *fpm_conf_set_pm_style(void **conf, char *name, void *vv, intptr_t offset)
2414 +{
2415 +       char *value = vv;
2416 +       struct fpm_pm_s *c = *conf;
2417 +
2418 +       if (!strcmp(value, "static")) {
2419 +               c->style = PM_STYLE_STATIC;
2420 +       }
2421 +       else if (!strcmp(value, "apache-like")) {
2422 +               c->style = PM_STYLE_APACHE_LIKE;
2423 +       }
2424 +       else {
2425 +               return "invalid value for 'style'";
2426 +       }
2427 +
2428 +       return NULL;
2429 +}
2430 +
2431 +static char *fpm_conf_set_rlimit_core(void **conf, char *name, void *vv, intptr_t offset)
2432 +{
2433 +       char *value = vv;
2434 +       struct fpm_worker_pool_config_s *c = *conf;
2435 +
2436 +       if (!strcmp(value, "unlimited")) {
2437 +               c->rlimit_core = -1;
2438 +       }
2439 +       else {
2440 +               int int_value;
2441 +               void *subconf = &int_value;
2442 +               char *error;
2443 +
2444 +               error = xml_conf_set_slot_integer(&subconf, name, vv, 0);
2445 +
2446 +               if (error) return error;
2447 +
2448 +               if (int_value < 0) return "invalid value for 'rlimit_core'";
2449 +
2450 +               c->rlimit_core = int_value;
2451 +       }
2452 +
2453 +       return NULL;
2454 +}
2455 +
2456 +static char *fpm_conf_set_catch_workers_output(void **conf, char *name, void *vv, intptr_t offset)
2457 +{
2458 +       struct fpm_worker_pool_config_s *c = *conf;
2459 +       int int_value;
2460 +       void *subconf = &int_value;
2461 +       char *error;
2462 +
2463 +       error = xml_conf_set_slot_boolean(&subconf, name, vv, 0);
2464 +
2465 +       if (error) return error;
2466 +
2467 +       c->catch_workers_output = int_value;
2468 +
2469 +       return NULL;
2470 +}
2471 +
2472 +static struct xml_conf_section fpm_conf_set_apache_like_subsection_conf = {
2473 +       .path = "apache_like somewhere", /* fixme */
2474 +       .parsers = (struct xml_value_parser []) {
2475 +               { XML_CONF_SCALAR, "StartServers",              &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.StartServers) },
2476 +               { XML_CONF_SCALAR, "MinSpareServers",   &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.MinSpareServers) },
2477 +               { XML_CONF_SCALAR, "MaxSpareServers",   &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.MaxSpareServers) },
2478 +               { 0, 0, 0, 0 }
2479 +       }
2480 +};
2481 +
2482 +static char *fpm_conf_set_apache_like_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2483 +{
2484 +       return xml_conf_parse_section(conf, &fpm_conf_set_apache_like_subsection_conf, xml_node);
2485 +}
2486 +
2487 +static struct xml_conf_section fpm_conf_set_listen_options_subsection_conf = {
2488 +       .path = "listen options somewhere", /* fixme */
2489 +       .parsers = (struct xml_value_parser []) {
2490 +               { XML_CONF_SCALAR,              "backlog",              &xml_conf_set_slot_integer,             offsetof(struct fpm_listen_options_s, backlog) },
2491 +               { XML_CONF_SCALAR,              "owner",                &xml_conf_set_slot_string,              offsetof(struct fpm_listen_options_s, owner) },
2492 +               { XML_CONF_SCALAR,              "group",                &xml_conf_set_slot_string,              offsetof(struct fpm_listen_options_s, group) },
2493 +               { XML_CONF_SCALAR,              "mode",                 &xml_conf_set_slot_string,              offsetof(struct fpm_listen_options_s, mode) },
2494 +               { 0, 0, 0, 0 }
2495 +       }
2496 +};
2497 +
2498 +static char *fpm_conf_set_listen_options_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2499 +{
2500 +       void *subconf = (char *) *conf + offset;
2501 +       struct fpm_listen_options_s *lo;
2502 +
2503 +       lo = malloc(sizeof(*lo));
2504 +
2505 +       if (!lo) {
2506 +               return "malloc() failed";
2507 +       }
2508 +
2509 +       memset(lo, 0, sizeof(*lo));
2510 +
2511 +       lo->backlog = -1;
2512 +
2513 +       * (struct fpm_listen_options_s **) subconf = lo;
2514 +
2515 +       subconf = lo;
2516 +
2517 +       return xml_conf_parse_section(&subconf, &fpm_conf_set_listen_options_subsection_conf, xml_node);
2518 +}
2519 +
2520 +static struct xml_conf_section fpm_conf_set_pm_subsection_conf = {
2521 +       .path = "pm settings somewhere", /* fixme */
2522 +       .parsers = (struct xml_value_parser []) {
2523 +               { XML_CONF_SCALAR,              "style",                                &fpm_conf_set_pm_style,                                         0 },
2524 +               { XML_CONF_SCALAR,              "max_children",                 &xml_conf_set_slot_integer,                                     offsetof(struct fpm_pm_s, max_children) },
2525 +               { XML_CONF_SUBSECTION,  "apache_like",                  &fpm_conf_set_apache_like_subsection,           offsetof(struct fpm_pm_s, options_apache_like) },
2526 +               { 0, 0, 0, 0 }
2527 +       }
2528 +};
2529 +
2530 +static char *fpm_conf_set_pm_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2531 +{
2532 +       void *subconf = (char *) *conf + offset;
2533 +       struct fpm_pm_s *pm;
2534 +
2535 +       pm = malloc(sizeof(*pm));
2536 +
2537 +       if (!pm) {
2538 +               return "fpm_conf_set_pm_subsection(): malloc failed";
2539 +       }
2540 +
2541 +       memset(pm, 0, sizeof(*pm));
2542 +
2543 +       * (struct fpm_pm_s **) subconf = pm;
2544 +
2545 +       subconf = pm;
2546 +
2547 +       return xml_conf_parse_section(&subconf, &fpm_conf_set_pm_subsection_conf, xml_node);
2548 +}
2549 +
2550 +static char *xml_conf_set_slot_key_value_pair(void **conf, char *name, void *vv, intptr_t offset)
2551 +{
2552 +       char *value = vv;
2553 +       struct key_value_s *kv;
2554 +       struct key_value_s ***parent = (struct key_value_s ***) conf;
2555 +
2556 +       kv = malloc(sizeof(*kv));
2557 +
2558 +       if (!kv) {
2559 +               return "malloc() failed";
2560 +       }
2561 +
2562 +       memset(kv, 0, sizeof(*kv));
2563 +
2564 +       kv->key = strdup(name);
2565 +       kv->value = strdup(value);
2566 +
2567 +       if (!kv->key || !kv->value) {
2568 +               return "xml_conf_set_slot_key_value_pair(): strdup() failed";
2569 +       }
2570 +
2571 +       **parent = kv;
2572 +
2573 +       *parent = &kv->next;
2574 +
2575 +       return NULL;
2576 +}
2577 +
2578 +static struct xml_conf_section fpm_conf_set_key_value_pairs_subsection_conf = {
2579 +       .path = "key_value_pairs somewhere", /* fixme */
2580 +       .parsers = (struct xml_value_parser []) {
2581 +               { XML_CONF_SCALAR, 0, &xml_conf_set_slot_key_value_pair, 0 },
2582 +               { 0, 0, 0, 0 }
2583 +       }
2584 +};
2585 +
2586 +static char *fpm_conf_set_key_value_pairs_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2587 +{
2588 +       void *next_kv = (char *) *conf + offset;
2589 +
2590 +       return xml_conf_parse_section(&next_kv, &fpm_conf_set_key_value_pairs_subsection_conf, xml_node);
2591 +}
2592 +
2593 +static void *fpm_worker_pool_config_alloc()
2594 +{
2595 +       static struct fpm_worker_pool_s *current_wp = 0;
2596 +       struct fpm_worker_pool_s *wp;
2597 +
2598 +       wp = fpm_worker_pool_alloc();
2599 +
2600 +       if (!wp) return 0;
2601 +
2602 +       wp->config = malloc(sizeof(struct fpm_worker_pool_config_s));
2603 +
2604 +       if (!wp->config) return 0;
2605 +
2606 +       memset(wp->config, 0, sizeof(struct fpm_worker_pool_config_s));
2607 +
2608 +       if (current_wp) current_wp->next = wp;
2609 +
2610 +       current_wp = wp;
2611 +
2612 +       return wp->config;
2613 +}
2614 +
2615 +int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc)
2616 +{
2617 +       struct key_value_s *kv, *kv_next;
2618 +
2619 +       free(wpc->name);
2620 +       free(wpc->listen_address);
2621 +       if (wpc->listen_options) {
2622 +               free(wpc->listen_options->owner);
2623 +               free(wpc->listen_options->group);
2624 +               free(wpc->listen_options->mode);
2625 +               free(wpc->listen_options);
2626 +       }
2627 +       for (kv = wpc->php_defines; kv; kv = kv_next) {
2628 +               kv_next = kv->next;
2629 +               free(kv->key);
2630 +               free(kv->value);
2631 +               free(kv);
2632 +       }
2633 +       for (kv = wpc->environment; kv; kv = kv_next) {
2634 +               kv_next = kv->next;
2635 +               free(kv->key);
2636 +               free(kv->value);
2637 +               free(kv);
2638 +       }
2639 +       free(wpc->pm);
2640 +       free(wpc->user);
2641 +       free(wpc->group);
2642 +       free(wpc->chroot);
2643 +       free(wpc->chdir);
2644 +       free(wpc->allowed_clients);
2645 +       free(wpc->slowlog);
2646 +
2647 +       return 0;
2648 +}
2649 +
2650 +static struct xml_conf_section xml_section_fpm_worker_pool_config = {
2651 +       .conf = &fpm_worker_pool_config_alloc,
2652 +       .path = "/configuration/workers/pool",
2653 +       .parsers = (struct xml_value_parser []) {
2654 +               { XML_CONF_SCALAR,              "name",                                                 &xml_conf_set_slot_string,                                      offsetof(struct fpm_worker_pool_config_s, name) },
2655 +               { XML_CONF_SCALAR,              "listen_address",                               &xml_conf_set_slot_string,                                      offsetof(struct fpm_worker_pool_config_s, listen_address) },
2656 +               { XML_CONF_SUBSECTION,  "listen_options",                               &fpm_conf_set_listen_options_subsection,        offsetof(struct fpm_worker_pool_config_s, listen_options) },
2657 +               { XML_CONF_SUBSECTION,  "php_defines",                                  &fpm_conf_set_key_value_pairs_subsection,       offsetof(struct fpm_worker_pool_config_s, php_defines) },
2658 +               { XML_CONF_SCALAR,              "user",                                                 &xml_conf_set_slot_string,                                      offsetof(struct fpm_worker_pool_config_s, user) },
2659 +               { XML_CONF_SCALAR,              "group",                                                &xml_conf_set_slot_string,                                      offsetof(struct fpm_worker_pool_config_s, group) },
2660 +               { XML_CONF_SCALAR,              "chroot",                                               &xml_conf_set_slot_string,                                      offsetof(struct fpm_worker_pool_config_s, chroot) },
2661 +               { XML_CONF_SCALAR,              "chdir",                                                &xml_conf_set_slot_string,                                      offsetof(struct fpm_worker_pool_config_s, chdir) },
2662 +               { XML_CONF_SCALAR,              "allowed_clients",                              &xml_conf_set_slot_string,                                      offsetof(struct fpm_worker_pool_config_s, allowed_clients) },
2663 +               { XML_CONF_SUBSECTION,  "environment",                                  &fpm_conf_set_key_value_pairs_subsection,       offsetof(struct fpm_worker_pool_config_s, environment) },
2664 +               { XML_CONF_SCALAR,              "request_terminate_timeout",    &xml_conf_set_slot_time,                                        offsetof(struct fpm_worker_pool_config_s, request_terminate_timeout) },
2665 +               { XML_CONF_SCALAR,              "request_slowlog_timeout",              &xml_conf_set_slot_time,                                        offsetof(struct fpm_worker_pool_config_s, request_slowlog_timeout) },
2666 +               { XML_CONF_SCALAR,              "slowlog",                                              &xml_conf_set_slot_string,                                      offsetof(struct fpm_worker_pool_config_s, slowlog) },
2667 +               { XML_CONF_SCALAR,              "rlimit_files",                                 &xml_conf_set_slot_integer,                                     offsetof(struct fpm_worker_pool_config_s, rlimit_files) },
2668 +               { XML_CONF_SCALAR,              "rlimit_core",                                  &fpm_conf_set_rlimit_core,                                      0 },
2669 +               { XML_CONF_SCALAR,              "max_requests",                                 &xml_conf_set_slot_integer,                                     offsetof(struct fpm_worker_pool_config_s, max_requests) },
2670 +               { XML_CONF_SCALAR,              "catch_workers_output",                 &fpm_conf_set_catch_workers_output,                     0 },
2671 +               { XML_CONF_SUBSECTION,  "pm",                                                   &fpm_conf_set_pm_subsection,                            offsetof(struct fpm_worker_pool_config_s, pm) },
2672 +               { 0, 0, 0, 0 }
2673 +       }
2674 +};
2675 +
2676 +static struct xml_conf_section *fpm_conf_all_sections[] = {
2677 +       &xml_section_fpm_global_options,
2678 +       &xml_section_fpm_worker_pool_config,
2679 +       0
2680 +};
2681 +
2682 +static int fpm_evaluate_full_path(char **path)
2683 +{
2684 +       if (**path != '/') {
2685 +               char *full_path;
2686 +
2687 +               full_path = malloc(sizeof(PHP_PREFIX) + strlen(*path) + 1);
2688 +
2689 +               if (!full_path) return -1;
2690 +
2691 +               sprintf(full_path, "%s/%s", PHP_PREFIX, *path);
2692 +
2693 +               free(*path);
2694 +
2695 +               *path = full_path;
2696 +       }
2697 +
2698 +       return 0;
2699 +}
2700 +
2701 +static int fpm_conf_process_all_pools()
2702 +{
2703 +       struct fpm_worker_pool_s *wp;
2704 +
2705 +       if (!fpm_worker_all_pools) {
2706 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "at least one pool section must be specified in config file");
2707 +               return -1;
2708 +       }
2709 +
2710 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
2711 +
2712 +               if (wp->config->listen_address && *wp->config->listen_address) {
2713 +
2714 +                       wp->listen_address_domain = fpm_sockets_domain_from_address(wp->config->listen_address);
2715 +
2716 +                       if (wp->listen_address_domain == FPM_AF_UNIX && *wp->config->listen_address != '/') {
2717 +                               fpm_evaluate_full_path(&wp->config->listen_address);
2718 +                       }
2719 +
2720 +               }
2721 +               else {
2722 +
2723 +                       wp->is_template = 1;
2724 +
2725 +               }
2726 +
2727 +               if (wp->config->request_slowlog_timeout) {
2728 +#if HAVE_FPM_TRACE
2729 +                       if (! (wp->config->slowlog && *wp->config->slowlog)) {
2730 +                               zlog(ZLOG_STUFF, ZLOG_ERROR, "pool %s: 'slowlog' must be specified for use with 'request_slowlog_timeout'",
2731 +                                       wp->config->name);
2732 +                               return -1;
2733 +                       }
2734 +#else
2735 +                       static int warned = 0;
2736 +
2737 +                       if (!warned) {
2738 +                               zlog(ZLOG_STUFF, ZLOG_WARNING, "pool %s: 'request_slowlog_timeout' is not supported on your system",
2739 +                                       wp->config->name);
2740 +                               warned = 1;
2741 +                       }
2742 +
2743 +                       wp->config->request_slowlog_timeout = 0;
2744 +#endif
2745 +               }
2746 +
2747 +               if (wp->config->request_slowlog_timeout && wp->config->slowlog && *wp->config->slowlog) {
2748 +                       int fd;
2749 +
2750 +                       fpm_evaluate_full_path(&wp->config->slowlog);
2751 +
2752 +                       if (wp->config->request_slowlog_timeout) {
2753 +                               fd = open(wp->config->slowlog, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
2754 +
2755 +                               if (0 > fd) {
2756 +                                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(%s) failed", wp->config->slowlog);
2757 +                                       return -1;
2758 +                               }
2759 +                               close(fd);
2760 +                       }
2761 +               }
2762 +       }
2763 +
2764 +       return 0;
2765 +}
2766 +
2767 +int fpm_conf_unlink_pid()
2768 +{
2769 +       if (fpm_global_config.pid_file) {
2770 +
2771 +               if (0 > unlink(fpm_global_config.pid_file)) {
2772 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "unlink(\"%s\") failed", fpm_global_config.pid_file);
2773 +                       return -1;
2774 +               }
2775 +
2776 +       }
2777 +
2778 +       return 0;
2779 +}
2780 +
2781 +int fpm_conf_write_pid()
2782 +{
2783 +       int fd;
2784 +
2785 +       if (fpm_global_config.pid_file) {
2786 +               char buf[64];
2787 +               int len;
2788 +
2789 +               unlink(fpm_global_config.pid_file);
2790 +
2791 +               fd = creat(fpm_global_config.pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
2792 +
2793 +               if (fd < 0) {
2794 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "creat(\"%s\") failed", fpm_global_config.pid_file);
2795 +                       return -1;
2796 +               }
2797 +
2798 +               len = sprintf(buf, "%d", (int) fpm_globals.parent_pid);
2799 +
2800 +               if (len != write(fd, buf, len)) {
2801 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "write() failed");
2802 +                       return -1;
2803 +               }
2804 +
2805 +               close(fd);
2806 +       }
2807 +
2808 +       return 0;
2809 +}
2810 +
2811 +static int fpm_conf_post_process()
2812 +{
2813 +       if (fpm_global_config.pid_file) {
2814 +               fpm_evaluate_full_path(&fpm_global_config.pid_file);
2815 +       }
2816 +
2817 +       if (!fpm_global_config.error_log) {
2818 +               fpm_global_config.error_log = strdup(PHP_FPM_LOG_PATH);
2819 +       }
2820 +
2821 +       fpm_evaluate_full_path(&fpm_global_config.error_log);
2822 +
2823 +       if (0 > fpm_stdio_open_error_log(0)) {
2824 +               return -1;
2825 +       }
2826 +
2827 +       return fpm_conf_process_all_pools();
2828 +}
2829 +
2830 +static void fpm_conf_cleanup(int which, void *arg)
2831 +{
2832 +       free(fpm_global_config.pid_file);
2833 +       free(fpm_global_config.error_log);
2834 +       fpm_global_config.pid_file = 0;
2835 +       fpm_global_config.error_log = 0;
2836 +}
2837 +
2838 +int fpm_conf_init_main()
2839 +{
2840 +       char *filename = fpm_globals.config;
2841 +       char *err;
2842 +
2843 +       if (0 > xml_conf_sections_register(fpm_conf_all_sections)) {
2844 +               return -1;
2845 +       }
2846 +
2847 +       if (filename == NULL) {
2848 +               filename = PHP_FPM_CONF_PATH;
2849 +       }
2850 +
2851 +       err = xml_conf_load_file(filename);
2852 +
2853 +       if (err) {
2854 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "failed to load configuration file: %s", err);
2855 +               return -1;
2856 +       }
2857 +
2858 +       if (0 > fpm_conf_post_process()) {
2859 +               return -1;
2860 +       }
2861 +
2862 +       xml_conf_clean();
2863 +
2864 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_conf_cleanup, 0)) {
2865 +               return -1;
2866 +       }
2867 +
2868 +       return 0;
2869 +}
2870 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_conf.h php-5.2.17/sapi/cgi/fpm/fpm_conf.h
2871 --- php-5.2.17.org/sapi/cgi/fpm/fpm_conf.h      1970-01-01 01:00:00.000000000 +0100
2872 +++ php-5.2.17/sapi/cgi/fpm/fpm_conf.h  2021-10-23 19:09:19.829791741 +0200
2873 @@ -0,0 +1,73 @@
2874 +
2875 +       /* $Id$ */
2876 +       /* (c) 2007,2008 Andrei Nigmatulin */
2877 +
2878 +#ifndef FPM_CONF_H
2879 +#define FPM_CONF_H 1
2880 +
2881 +struct key_value_s;
2882 +
2883 +struct key_value_s {
2884 +       struct key_value_s *next;
2885 +       char *key;
2886 +       char *value;
2887 +};
2888 +
2889 +struct fpm_global_config_s {
2890 +       int emergency_restart_threshold;
2891 +       int emergency_restart_interval;
2892 +       int process_control_timeout;
2893 +       int daemonize;
2894 +       char *pid_file;
2895 +       char *error_log;
2896 +};
2897 +
2898 +extern struct fpm_global_config_s fpm_global_config;
2899 +
2900 +struct fpm_pm_s {
2901 +       int style;
2902 +       int max_children;
2903 +       struct {
2904 +               int StartServers;
2905 +               int MinSpareServers;
2906 +               int MaxSpareServers;
2907 +       } options_apache_like;
2908 +};
2909 +
2910 +struct fpm_listen_options_s {
2911 +       int backlog;
2912 +       char *owner;
2913 +       char *group;
2914 +       char *mode;
2915 +};
2916 +
2917 +struct fpm_worker_pool_config_s {
2918 +       char *name;
2919 +       char *listen_address;
2920 +       struct fpm_listen_options_s *listen_options;
2921 +       struct key_value_s *php_defines;
2922 +       char *user;
2923 +       char *group;
2924 +       char *chroot;
2925 +       char *chdir;
2926 +       char *allowed_clients;
2927 +       struct key_value_s *environment;
2928 +       struct fpm_pm_s *pm;
2929 +       int request_terminate_timeout;
2930 +       int request_slowlog_timeout;
2931 +       char *slowlog;
2932 +       int max_requests;
2933 +       int rlimit_files;
2934 +       int rlimit_core;
2935 +       unsigned catch_workers_output:1;
2936 +};
2937 +
2938 +enum { PM_STYLE_STATIC = 1, PM_STYLE_APACHE_LIKE = 2 };
2939 +
2940 +int fpm_conf_init_main();
2941 +int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc);
2942 +int fpm_conf_write_pid();
2943 +int fpm_conf_unlink_pid();
2944 +
2945 +#endif
2946 +
2947 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_config.h php-5.2.17/sapi/cgi/fpm/fpm_config.h
2948 --- php-5.2.17.org/sapi/cgi/fpm/fpm_config.h    1970-01-01 01:00:00.000000000 +0100
2949 +++ php-5.2.17/sapi/cgi/fpm/fpm_config.h        2021-10-23 19:09:19.829791741 +0200
2950 @@ -0,0 +1,39 @@
2951 +
2952 +       /* $Id$ */
2953 +       /* (c) 2007,2008 Andrei Nigmatulin */
2954 +
2955 +#include "php_config.h"
2956 +#include "fpm_autoconf.h"
2957 +
2958 +
2959 +/* Solaris does not have it */
2960 +#ifndef INADDR_NONE
2961 +#define INADDR_NONE (-1)
2962 +#endif
2963 +
2964 +
2965 +/* If we're not using GNU C, elide __attribute__ */
2966 +#ifndef __GNUC__
2967 +#  define  __attribute__(x)  /*NOTHING*/
2968 +#endif
2969 +
2970 +
2971 +/* Solaris does not have it */
2972 +#ifndef timersub
2973 +#define        timersub(tvp, uvp, vvp)                                         \
2974 +       do {                                                            \
2975 +               (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
2976 +               (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
2977 +               if ((vvp)->tv_usec < 0) {                               \
2978 +                       (vvp)->tv_sec--;                                \
2979 +                       (vvp)->tv_usec += 1000000;                      \
2980 +               }                                                       \
2981 +       } while (0)
2982 +#endif
2983 +
2984 +#if defined(HAVE_PTRACE) || defined(PROC_MEM_FILE) || defined(HAVE_MACH_VM_READ)
2985 +#define HAVE_FPM_TRACE 1
2986 +#else
2987 +#define HAVE_FPM_TRACE 0
2988 +#endif
2989 +
2990 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_env.c php-5.2.17/sapi/cgi/fpm/fpm_env.c
2991 --- php-5.2.17.org/sapi/cgi/fpm/fpm_env.c       1970-01-01 01:00:00.000000000 +0100
2992 +++ php-5.2.17/sapi/cgi/fpm/fpm_env.c   2021-10-23 19:09:19.829791741 +0200
2993 @@ -0,0 +1,125 @@
2994 +
2995 +       /* $Id$ */
2996 +       /* (c) 2007,2008 Andrei Nigmatulin */
2997 +
2998 +#include "fpm_config.h"
2999 +
3000 +#ifdef HAVE_ALLOCA_H
3001 +#include <alloca.h>
3002 +#endif
3003 +#include <stdio.h>
3004 +#include <stdlib.h>
3005 +#include <string.h>
3006 +
3007 +#include "fpm_env.h"
3008 +#include "zlog.h"
3009 +
3010 +#ifndef HAVE_SETENV
3011 +int setenv(char *name, char *value, int overwrite)
3012 +{
3013 +       int name_len = strlen(name);
3014 +       int value_len = strlen(value);
3015 +       char *var = alloca(name_len + 1 + value_len + 1);
3016 +
3017 +       memcpy(var, name, name_len);
3018 +
3019 +       var[name_len] = '=';
3020 +
3021 +       memcpy(var + name_len + 1, value, value_len);
3022 +
3023 +       var[name_len + 1 + value_len] = '\0';
3024 +
3025 +       return putenv(var);
3026 +}
3027 +#endif
3028 +
3029 +#ifndef HAVE_CLEARENV
3030 +void clearenv()
3031 +{
3032 +       char **envp;
3033 +       char *s;
3034 +
3035 +       /* this algo is the only one known to me
3036 +               that works well on all systems */
3037 +       while (*(envp = environ)) {
3038 +               char *eq = strchr(*envp, '=');
3039 +
3040 +               s = strdup(*envp);
3041 +
3042 +               if (eq) s[eq - *envp] = '\0';
3043 +
3044 +               unsetenv(s);
3045 +               free(s);
3046 +       }
3047 +
3048 +}
3049 +#endif
3050 +
3051 +
3052 +int fpm_env_init_child(struct fpm_worker_pool_s *wp)
3053 +{
3054 +       struct key_value_s *kv;
3055 +
3056 +       clearenv();
3057 +
3058 +       for (kv = wp->config->environment; kv; kv = kv->next) {
3059 +               setenv(kv->key, kv->value, 1);
3060 +       }
3061 +
3062 +       if (wp->user) {
3063 +               setenv("USER", wp->user, 1);
3064 +       }
3065 +
3066 +       if (wp->home) {
3067 +               setenv("HOME", wp->home, 1);
3068 +       }
3069 +
3070 +       return 0;
3071 +}
3072 +
3073 +static int fpm_env_conf_wp(struct fpm_worker_pool_s *wp)
3074 +{
3075 +       struct key_value_s *kv;
3076 +
3077 +       kv = wp->config->environment;
3078 +
3079 +       for (kv = wp->config->environment; kv; kv = kv->next) {
3080 +               if (*kv->value == '$') {
3081 +                       char *value = getenv(kv->value + 1);
3082 +
3083 +                       if (!value) value = "";
3084 +
3085 +                       free(kv->value);
3086 +                       kv->value = strdup(value);
3087 +               }
3088 +
3089 +               /* autodetected values should be removed
3090 +                       if these vars specified in config */
3091 +               if (!strcmp(kv->key, "USER")) {
3092 +                       free(wp->user);
3093 +                       wp->user = 0;
3094 +               }
3095 +
3096 +               if (!strcmp(kv->key, "HOME")) {
3097 +                       free(wp->home);
3098 +                       wp->home = 0;
3099 +               }
3100 +       }
3101 +
3102 +       return 0;
3103 +}
3104 +
3105 +int fpm_env_init_main()
3106 +{
3107 +       struct fpm_worker_pool_s *wp;
3108 +
3109 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
3110 +
3111 +               if (0 > fpm_env_conf_wp(wp)) {
3112 +                       return -1;
3113 +               }
3114 +
3115 +       }
3116 +
3117 +       return 0;
3118 +}
3119 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_env.h php-5.2.17/sapi/cgi/fpm/fpm_env.h
3120 --- php-5.2.17.org/sapi/cgi/fpm/fpm_env.h       1970-01-01 01:00:00.000000000 +0100
3121 +++ php-5.2.17/sapi/cgi/fpm/fpm_env.h   2021-10-23 19:09:19.829791741 +0200
3122 @@ -0,0 +1,24 @@
3123 +
3124 +       /* $Id$ */
3125 +       /* (c) 2007,2008 Andrei Nigmatulin */
3126 +
3127 +#ifndef FPM_ENV_H
3128 +#define FPM_ENV_H 1
3129 +
3130 +#include "fpm_worker_pool.h"
3131 +
3132 +int fpm_env_init_child(struct fpm_worker_pool_s *wp);
3133 +int fpm_env_init_main();
3134 +
3135 +extern char **environ;
3136 +
3137 +#ifndef HAVE_SETENV
3138 +int setenv(char *name, char *value, int overwrite);
3139 +#endif
3140 +
3141 +#ifndef HAVE_CLEARENV
3142 +void clearenv();
3143 +#endif
3144 +
3145 +#endif
3146 +
3147 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_events.c php-5.2.17/sapi/cgi/fpm/fpm_events.c
3148 --- php-5.2.17.org/sapi/cgi/fpm/fpm_events.c    1970-01-01 01:00:00.000000000 +0100
3149 +++ php-5.2.17/sapi/cgi/fpm/fpm_events.c        2021-10-23 19:09:19.829791741 +0200
3150 @@ -0,0 +1,135 @@
3151 +
3152 +       /* $Id$ */
3153 +       /* (c) 2007,2008 Andrei Nigmatulin */
3154 +
3155 +#include "fpm_config.h"
3156 +
3157 +#include <unistd.h>
3158 +#include <errno.h>
3159 +#include <stdlib.h> /* for putenv */
3160 +#include <string.h>
3161 +#include <sys/types.h> /* for event.h below */
3162 +#include <event.h>
3163 +
3164 +#include "fpm.h"
3165 +#include "fpm_process_ctl.h"
3166 +#include "fpm_events.h"
3167 +#include "fpm_cleanup.h"
3168 +#include "fpm_stdio.h"
3169 +#include "fpm_signals.h"
3170 +#include "fpm_children.h"
3171 +#include "zlog.h"
3172 +
3173 +static void fpm_event_cleanup(int which, void *arg)
3174 +{
3175 +       event_base_free(0);
3176 +}
3177 +
3178 +static void fpm_got_signal(int fd, short ev, void *arg)
3179 +{
3180 +       char c;
3181 +       int res;
3182 +
3183 +       do {
3184 +
3185 +               do {
3186 +                       res = read(fd, &c, 1);
3187 +               } while (res == -1 && errno == EINTR);
3188 +
3189 +               if (res <= 0) {
3190 +                       if (res < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
3191 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
3192 +                       }
3193 +                       return;
3194 +               }
3195 +
3196 +               switch (c) {
3197 +                       case 'C' :                  /* SIGCHLD */
3198 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGCHLD");
3199 +                               fpm_children_bury();
3200 +                               break;
3201 +                       case 'I' :                  /* SIGINT  */
3202 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGINT");
3203 +                               fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
3204 +                               break;
3205 +                       case 'T' :                  /* SIGTERM */
3206 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGTERM");
3207 +                               fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
3208 +                               break;
3209 +                       case 'Q' :                  /* SIGQUIT */
3210 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGQUIT");
3211 +                               fpm_pctl(FPM_PCTL_STATE_FINISHING, FPM_PCTL_ACTION_SET);
3212 +                               break;
3213 +                       case '1' :                  /* SIGUSR1 */
3214 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGUSR1");
3215 +                               if (0 == fpm_stdio_open_error_log(1)) {
3216 +                                       zlog(ZLOG_STUFF, ZLOG_NOTICE, "log file re-opened");
3217 +                               }
3218 +                               break;
3219 +                       case '2' :                  /* SIGUSR2 */
3220 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGUSR2");
3221 +                               fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
3222 +                               break;
3223 +               }
3224 +
3225 +               if (fpm_globals.is_child) {
3226 +                       break;
3227 +               }
3228 +
3229 +       } while (1);
3230 +
3231 +       return;
3232 +}
3233 +
3234 +int fpm_event_init_main()
3235 +{
3236 +       event_init();
3237 +
3238 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: using %s", event_get_method());
3239 +
3240 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_event_cleanup, 0)) {
3241 +               return -1;
3242 +       }
3243 +
3244 +       return 0;
3245 +}
3246 +
3247 +int fpm_event_loop()
3248 +{
3249 +       static struct event signal_fd_event;
3250 +
3251 +       event_set(&signal_fd_event, fpm_signals_get_fd(), EV_PERSIST | EV_READ, &fpm_got_signal, 0);
3252 +
3253 +       event_add(&signal_fd_event, 0);
3254 +
3255 +       fpm_pctl_heartbeat(-1, 0, 0);
3256 +
3257 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: entering main loop");
3258 +
3259 +       event_loop(0);
3260 +
3261 +       return 0;
3262 +}
3263 +
3264 +int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg)
3265 +{
3266 +       event_set(ev, fd, EV_PERSIST | EV_READ, callback, arg);
3267 +
3268 +       return event_add(ev, 0);
3269 +}
3270 +
3271 +int fpm_event_del(struct event *ev)
3272 +{
3273 +       return event_del(ev);
3274 +}
3275 +
3276 +void fpm_event_exit_loop()
3277 +{
3278 +       event_loopbreak();
3279 +}
3280 +
3281 +void fpm_event_fire(struct event *ev)
3282 +{
3283 +       (*ev->ev_callback)( (int) ev->ev_fd, (short) ev->ev_res, ev->ev_arg);   
3284 +}
3285 +
3286 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_events.h php-5.2.17/sapi/cgi/fpm/fpm_events.h
3287 --- php-5.2.17.org/sapi/cgi/fpm/fpm_events.h    1970-01-01 01:00:00.000000000 +0100
3288 +++ php-5.2.17/sapi/cgi/fpm/fpm_events.h        2021-10-23 19:09:19.829791741 +0200
3289 @@ -0,0 +1,16 @@
3290 +
3291 +       /* $Id$ */
3292 +       /* (c) 2007,2008 Andrei Nigmatulin */
3293 +
3294 +#ifndef FPM_EVENTS_H
3295 +#define FPM_EVENTS_H 1
3296 +
3297 +void fpm_event_exit_loop();
3298 +int fpm_event_loop();
3299 +int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg);
3300 +int fpm_event_del(struct event *ev);
3301 +void fpm_event_fire(struct event *ev);
3302 +int fpm_event_init_main();
3303 +
3304 +
3305 +#endif
3306 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_php.c php-5.2.17/sapi/cgi/fpm/fpm_php.c
3307 --- php-5.2.17.org/sapi/cgi/fpm/fpm_php.c       1970-01-01 01:00:00.000000000 +0100
3308 +++ php-5.2.17/sapi/cgi/fpm/fpm_php.c   2021-10-23 19:09:19.829791741 +0200
3309 @@ -0,0 +1,190 @@
3310 +
3311 +       /* $Id$ */
3312 +       /* (c) 2007,2008 Andrei Nigmatulin */
3313 +
3314 +#include "fpm_config.h"
3315 +
3316 +#include <stdlib.h>
3317 +#include <string.h>
3318 +#include <stdio.h>
3319 +
3320 +#include "php.h"
3321 +#include "php_main.h"
3322 +#include "php_ini.h"
3323 +#include "ext/standard/dl.h"
3324 +
3325 +#include "fastcgi.h"
3326 +
3327 +#include "fpm.h"
3328 +#include "fpm_php.h"
3329 +#include "fpm_cleanup.h"
3330 +#include "fpm_worker_pool.h"
3331 +
3332 +static int zend_ini_alter_master(char *name, int name_length, char *new_value, int new_value_length, int stage TSRMLS_DC)
3333 +{
3334 +       zend_ini_entry *ini_entry;
3335 +       char *duplicate;
3336 +
3337 +       if (zend_hash_find(EG(ini_directives), name, name_length, (void **) &ini_entry) == FAILURE) {
3338 +               return FAILURE;
3339 +       }
3340 +
3341 +       duplicate = strdup(new_value);
3342 +
3343 +       if (!ini_entry->on_modify
3344 +               || ini_entry->on_modify(ini_entry, duplicate, new_value_length,
3345 +                       ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage TSRMLS_CC) == SUCCESS) {
3346 +               ini_entry->value = duplicate;
3347 +               ini_entry->value_length = new_value_length;
3348 +       } else {
3349 +               free(duplicate);
3350 +       }
3351 +
3352 +       return SUCCESS;
3353 +}
3354 +
3355 +static void fpm_php_disable(char *value, int (*zend_disable)(char *, uint TSRMLS_DC) TSRMLS_DC)
3356 +{
3357 +       char *s = 0, *e = value;
3358 +
3359 +       while (*e) {
3360 +               switch (*e) {
3361 +                       case ' ':
3362 +                       case ',':
3363 +                               if (s) {
3364 +                                       *e = '\0';
3365 +                                       zend_disable(s, e - s TSRMLS_CC);
3366 +                                       s = 0;
3367 +                               }
3368 +                               break;
3369 +                       default:
3370 +                               if (!s) {
3371 +                                       s = e;
3372 +                               }
3373 +                               break;
3374 +               }
3375 +               e++;
3376 +       }
3377 +
3378 +       if (s) {
3379 +               zend_disable(s, e - s TSRMLS_CC);
3380 +       }
3381 +}
3382 +
3383 +static int fpm_php_apply_defines(struct fpm_worker_pool_s *wp)
3384 +{
3385 +       TSRMLS_FETCH();
3386 +       struct key_value_s *kv;
3387 +
3388 +       for (kv = wp->config->php_defines; kv; kv = kv->next) {
3389 +               char *name = kv->key;
3390 +               char *value = kv->value;
3391 +               int name_len = strlen(name);
3392 +               int value_len = strlen(value);
3393 +
3394 +               if (!strcmp(name, "extension") && *value) {
3395 +                       zval zv;
3396 +
3397 +#if defined(PHP_VERSION_ID) && (PHP_VERSION_ID >= 50300)
3398 +                       php_dl(value, MODULE_PERSISTENT, &zv, 1 TSRMLS_CC);
3399 +#else
3400 +                       zval filename;
3401 +                       ZVAL_STRINGL(&filename, value, value_len, 0);
3402 +#if (PHP_MAJOR_VERSION >= 5)
3403 +                       php_dl(&filename, MODULE_PERSISTENT, &zv, 1 TSRMLS_CC);
3404 +#else
3405 +                       php_dl(&filename, MODULE_PERSISTENT, &zv TSRMLS_CC);
3406 +#endif
3407 +#endif
3408 +                       continue;
3409 +               }
3410 +
3411 +               zend_ini_alter_master(name, name_len + 1, value, value_len, PHP_INI_STAGE_ACTIVATE TSRMLS_CC);
3412 +
3413 +               if (!strcmp(name, "disable_functions") && *value) {
3414 +                       char *v = strdup(value);
3415 +#if (PHP_MAJOR_VERSION >= 5)
3416 +                       PG(disable_functions) = v;
3417 +#endif
3418 +                       fpm_php_disable(v, zend_disable_function TSRMLS_CC);
3419 +               }
3420 +               else if (!strcmp(name, "disable_classes") && *value) {
3421 +                       char *v = strdup(value);
3422 +#if (PHP_MAJOR_VERSION >= 5)
3423 +                       PG(disable_classes) = v;
3424 +#endif
3425 +                       fpm_php_disable(v, zend_disable_class TSRMLS_CC);
3426 +               }
3427 +       }
3428 +
3429 +       return 0;
3430 +}
3431 +
3432 +static int fpm_php_set_allowed_clients(struct fpm_worker_pool_s *wp)
3433 +{
3434 +       if (wp->listen_address_domain == FPM_AF_INET) {
3435 +               fcgi_set_allowed_clients(wp->config->allowed_clients);
3436 +       }
3437 +
3438 +       return 0;
3439 +}
3440 +
3441 +static int fpm_php_set_fcgi_mgmt_vars(struct fpm_worker_pool_s *wp)
3442 +{
3443 +       char max_workers[10 + 1]; /* 4294967295 */
3444 +       int len;
3445 +
3446 +       len = sprintf(max_workers, "%u", (unsigned int) wp->config->pm->max_children);
3447 +
3448 +       fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, max_workers, len);
3449 +       fcgi_set_mgmt_var("FCGI_MAX_REQS",  sizeof("FCGI_MAX_REQS")-1,  max_workers, len);
3450 +
3451 +       return 0;
3452 +}
3453 +
3454 +char *fpm_php_script_filename(TSRMLS_D)
3455 +{
3456 +       return SG(request_info).path_translated;
3457 +}
3458 +
3459 +char *fpm_php_request_method(TSRMLS_D)
3460 +{
3461 +       return (char *) SG(request_info).request_method;
3462 +}
3463 +
3464 +size_t fpm_php_content_length(TSRMLS_D)
3465 +{
3466 +       return SG(request_info).content_length;
3467 +}
3468 +
3469 +static void fpm_php_cleanup(int which, void *arg)
3470 +{
3471 +       TSRMLS_FETCH();
3472 +       php_module_shutdown(TSRMLS_C);
3473 +       sapi_shutdown();
3474 +}
3475 +
3476 +void fpm_php_soft_quit()
3477 +{
3478 +       fcgi_set_in_shutdown(1);
3479 +}
3480 +
3481 +int fpm_php_init_main()
3482 +{
3483 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_PARENT, fpm_php_cleanup, 0)) {
3484 +               return -1;
3485 +       }
3486 +
3487 +       return 0;
3488 +}
3489 +
3490 +int fpm_php_init_child(struct fpm_worker_pool_s *wp)
3491 +{
3492 +       if (0 > fpm_php_apply_defines(wp) ||
3493 +               0 > fpm_php_set_allowed_clients(wp) ||
3494 +               0 > fpm_php_set_fcgi_mgmt_vars(wp)) {
3495 +               return -1;
3496 +       }
3497 +
3498 +       return 0;
3499 +}
3500 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_php.h php-5.2.17/sapi/cgi/fpm/fpm_php.h
3501 --- php-5.2.17.org/sapi/cgi/fpm/fpm_php.h       1970-01-01 01:00:00.000000000 +0100
3502 +++ php-5.2.17/sapi/cgi/fpm/fpm_php.h   2021-10-23 19:09:19.829791741 +0200
3503 @@ -0,0 +1,22 @@
3504 +
3505 +       /* $Id$ */
3506 +       /* (c) 2007,2008 Andrei Nigmatulin */
3507 +
3508 +#ifndef FPM_PHP_H
3509 +#define FPM_PHP_H 1
3510 +
3511 +#include <TSRM.h>
3512 +
3513 +#include "build-defs.h" /* for PHP_ defines */
3514 +
3515 +struct fpm_worker_pool_s;
3516 +
3517 +int fpm_php_init_child(struct fpm_worker_pool_s *wp);
3518 +char *fpm_php_script_filename(TSRMLS_D);
3519 +char *fpm_php_request_method(TSRMLS_D);
3520 +size_t fpm_php_content_length(TSRMLS_D);
3521 +void fpm_php_soft_quit();
3522 +int fpm_php_init_main();
3523 +
3524 +#endif
3525 +
3526 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_php_trace.c php-5.2.17/sapi/cgi/fpm/fpm_php_trace.c
3527 --- php-5.2.17.org/sapi/cgi/fpm/fpm_php_trace.c 1970-01-01 01:00:00.000000000 +0100
3528 +++ php-5.2.17/sapi/cgi/fpm/fpm_php_trace.c     2021-10-23 19:09:19.829791741 +0200
3529 @@ -0,0 +1,171 @@
3530 +
3531 +       /* $Id$ */
3532 +       /* (c) 2007,2008 Andrei Nigmatulin */
3533 +
3534 +#include "fpm_config.h"
3535 +
3536 +#if HAVE_FPM_TRACE
3537 +
3538 +#include "php.h"
3539 +#include "php_main.h"
3540 +
3541 +#include <stdio.h>
3542 +#include <stddef.h>
3543 +#include <stdint.h>
3544 +#include <unistd.h>
3545 +#include <sys/time.h>
3546 +#include <sys/types.h>
3547 +#include <errno.h>
3548 +
3549 +#include "fpm_trace.h"
3550 +#include "fpm_php_trace.h"
3551 +#include "fpm_children.h"
3552 +#include "fpm_worker_pool.h"
3553 +#include "fpm_process_ctl.h"
3554 +
3555 +#include "zlog.h"
3556 +
3557 +
3558 +#define valid_ptr(p) ((p) && 0 == ((p) & (sizeof(long) - 1)))
3559 +
3560 +#if SIZEOF_LONG == 4
3561 +#define PTR_FMT "08"
3562 +#elif SIZEOF_LONG == 8
3563 +#define PTR_FMT "016"
3564 +#endif
3565 +
3566 +
3567 +static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog TSRMLS_DC)
3568 +{
3569 +       int callers_limit = 20;
3570 +       pid_t pid = child->pid;
3571 +       struct timeval tv;
3572 +       static const int buf_size = 1024;
3573 +       char buf[buf_size];
3574 +       long execute_data;
3575 +       long l;
3576 +
3577 +       gettimeofday(&tv, 0);
3578 +
3579 +       zlog_print_time(&tv, buf, buf_size);
3580 +
3581 +       fprintf(slowlog, "\n%s pid %d (pool %s)\n", buf, (int) pid, child->wp->config->name);
3582 +
3583 +       if (0 > fpm_trace_get_strz(buf, buf_size, (long) &SG(request_info).path_translated)) {
3584 +               return -1;
3585 +       }
3586 +
3587 +       fprintf(slowlog, "script_filename = %s\n", buf);
3588 +
3589 +       if (0 > fpm_trace_get_long((long) &EG(current_execute_data), &l)) {
3590 +               return -1;
3591 +       }
3592 +
3593 +       execute_data = l;
3594 +
3595 +       while (execute_data) {
3596 +               long function;
3597 +               uint lineno = 0;
3598 +
3599 +               fprintf(slowlog, "[0x%" PTR_FMT "lx] ", execute_data);
3600 +
3601 +               if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, function_state.function), &l)) {
3602 +                       return -1;
3603 +               }
3604 +
3605 +               function = l;
3606 +
3607 +               if (valid_ptr(function)) {
3608 +                       if (0 > fpm_trace_get_strz(buf, buf_size, function + offsetof(zend_function, common.function_name))) {
3609 +                               return -1;
3610 +                       }
3611 +
3612 +                       fprintf(slowlog, "%s()", buf);
3613 +               }
3614 +               else {
3615 +                       fprintf(slowlog, "???");
3616 +               }
3617 +
3618 +               if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, op_array), &l)) {
3619 +                       return -1;
3620 +               }
3621 +
3622 +               *buf = '\0';
3623 +
3624 +               if (valid_ptr(l)) {
3625 +                       long op_array = l;
3626 +
3627 +                       if (0 > fpm_trace_get_strz(buf, buf_size, op_array + offsetof(zend_op_array, filename))) {
3628 +                               return -1;
3629 +                       }
3630 +               }
3631 +
3632 +               if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, opline), &l)) {
3633 +                       return -1;
3634 +               }
3635 +
3636 +               if (valid_ptr(l)) {
3637 +                       long opline = l;
3638 +                       uint *lu = (uint *) &l;
3639 +
3640 +                       if (0 > fpm_trace_get_long(opline + offsetof(struct _zend_op, lineno), &l)) {
3641 +                               return -1;
3642 +                       }
3643 +
3644 +                       lineno = *lu;
3645 +               }
3646 +
3647 +               fprintf(slowlog, " %s:%u\n", *buf ? buf : "unknown", lineno);
3648 +
3649 +               if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, prev_execute_data), &l)) {
3650 +                       return -1;
3651 +               }
3652 +
3653 +               execute_data = l;
3654 +
3655 +               if (0 == --callers_limit) {
3656 +                       break;
3657 +               }
3658 +       }
3659 +
3660 +       return 0;
3661 +}
3662 +
3663 +void fpm_php_trace(struct fpm_child_s *child)
3664 +{
3665 +       TSRMLS_FETCH();
3666 +       FILE *slowlog;
3667 +
3668 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "about to trace %d", (int) child->pid);
3669 +
3670 +       slowlog = fopen(child->wp->config->slowlog, "a+");
3671 +
3672 +       if (!slowlog) {
3673 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fopen(%s) failed", child->wp->config->slowlog);
3674 +               goto done0;
3675 +       }
3676 +
3677 +       if (0 > fpm_trace_ready(child->pid)) {
3678 +               goto done1;
3679 +       }
3680 +
3681 +       if (0 > fpm_php_trace_dump(child, slowlog TSRMLS_CC)) {
3682 +               fprintf(slowlog, "+++ dump failed\n");
3683 +       }
3684 +
3685 +       if (0 > fpm_trace_close(child->pid)) {
3686 +               goto done1;
3687 +       }
3688 +
3689 +done1:
3690 +       fclose(slowlog);
3691 +
3692 +done0:
3693 +       fpm_pctl_kill(child->pid, FPM_PCTL_CONT);
3694 +       child->tracer = 0;
3695 +
3696 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "finished trace of %d", (int) child->pid);
3697 +}
3698 +
3699 +#endif
3700 +
3701 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_php_trace.h php-5.2.17/sapi/cgi/fpm/fpm_php_trace.h
3702 --- php-5.2.17.org/sapi/cgi/fpm/fpm_php_trace.h 1970-01-01 01:00:00.000000000 +0100
3703 +++ php-5.2.17/sapi/cgi/fpm/fpm_php_trace.h     2021-10-23 19:09:19.829791741 +0200
3704 @@ -0,0 +1,13 @@
3705 +
3706 +       /* $Id$ */
3707 +       /* (c) 2007,2008 Andrei Nigmatulin */
3708 +
3709 +#ifndef FPM_PHP_TRACE_H
3710 +#define FPM_PHP_TRACE_H 1
3711 +
3712 +struct fpm_child_s;
3713 +
3714 +void fpm_php_trace(struct fpm_child_s *);
3715 +
3716 +#endif
3717 +
3718 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_process_ctl.c php-5.2.17/sapi/cgi/fpm/fpm_process_ctl.c
3719 --- php-5.2.17.org/sapi/cgi/fpm/fpm_process_ctl.c       1970-01-01 01:00:00.000000000 +0100
3720 +++ php-5.2.17/sapi/cgi/fpm/fpm_process_ctl.c   2021-10-23 19:09:19.829791741 +0200
3721 @@ -0,0 +1,354 @@
3722 +
3723 +       /* $Id$ */
3724 +       /* (c) 2007,2008 Andrei Nigmatulin */
3725 +
3726 +#include "fpm_config.h"
3727 +
3728 +#include <sys/types.h>
3729 +#include <signal.h>
3730 +#include <unistd.h>
3731 +#include <stdlib.h>
3732 +
3733 +#include "fpm.h"
3734 +#include "fpm_clock.h"
3735 +#include "fpm_children.h"
3736 +#include "fpm_signals.h"
3737 +#include "fpm_events.h"
3738 +#include "fpm_process_ctl.h"
3739 +#include "fpm_cleanup.h"
3740 +#include "fpm_request.h"
3741 +#include "fpm_worker_pool.h"
3742 +#include "zlog.h"
3743 +
3744 +
3745 +static int fpm_state = FPM_PCTL_STATE_NORMAL;
3746 +static int fpm_signal_sent = 0;
3747 +
3748 +
3749 +static const char *fpm_state_names[] = {
3750 +       [FPM_PCTL_STATE_NORMAL] = "normal",
3751 +       [FPM_PCTL_STATE_RELOADING] = "reloading",
3752 +       [FPM_PCTL_STATE_TERMINATING] = "terminating",
3753 +       [FPM_PCTL_STATE_FINISHING] = "finishing"
3754 +};
3755 +
3756 +static int saved_argc;
3757 +static char **saved_argv;
3758 +
3759 +static void fpm_pctl_cleanup(int which, void *arg)
3760 +{
3761 +       int i;
3762 +
3763 +       if (which != FPM_CLEANUP_PARENT_EXEC) {
3764 +
3765 +               for (i = 0; i < saved_argc; i++) {
3766 +                       free(saved_argv[i]);
3767 +               }
3768 +
3769 +               free(saved_argv);
3770 +
3771 +       }
3772 +}
3773 +
3774 +static struct event pctl_event;
3775 +
3776 +static void fpm_pctl_action(int fd, short which, void *arg)
3777 +{
3778 +       evtimer_del(&pctl_event);
3779 +
3780 +       memset(&pctl_event, 0, sizeof(pctl_event));
3781 +
3782 +       fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_TIMEOUT);
3783 +}
3784 +
3785 +static int fpm_pctl_timeout_set(int sec)
3786 +{
3787 +       struct timeval tv = { .tv_sec = sec, .tv_usec = 0 };
3788 +
3789 +       if (evtimer_initialized(&pctl_event)) {
3790 +               evtimer_del(&pctl_event);
3791 +       }
3792 +
3793 +       evtimer_set(&pctl_event, &fpm_pctl_action, 0);
3794 +
3795 +       evtimer_add(&pctl_event, &tv);
3796 +
3797 +       return 0;
3798 +}
3799 +
3800 +static void fpm_pctl_exit()
3801 +{
3802 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "exiting, bye-bye!");
3803 +
3804 +       fpm_conf_unlink_pid();
3805 +
3806 +       fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT_MAIN);
3807 +
3808 +       exit(0);
3809 +}
3810 +
3811 +#define optional_arg(c) (saved_argc > c ? ", \"" : ""), (saved_argc > c ? saved_argv[c] : ""), (saved_argc > c ? "\"" : "")
3812 +
3813 +static void fpm_pctl_exec()
3814 +{
3815 +
3816 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "reloading: execvp(\"%s\", {\"%s\""
3817 +                       "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
3818 +                       "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
3819 +               "})",
3820 +               saved_argv[0], saved_argv[0],
3821 +               optional_arg(1),
3822 +               optional_arg(2),
3823 +               optional_arg(3),
3824 +               optional_arg(4),
3825 +               optional_arg(5),
3826 +               optional_arg(6),
3827 +               optional_arg(7),
3828 +               optional_arg(8),
3829 +               optional_arg(9),
3830 +               optional_arg(10)
3831 +       );
3832 +
3833 +       fpm_cleanups_run(FPM_CLEANUP_PARENT_EXEC);
3834 +
3835 +       execvp(saved_argv[0], saved_argv);
3836 +
3837 +       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "execvp() failed");
3838 +
3839 +       exit(1);
3840 +}
3841 +
3842 +static void fpm_pctl_action_last()
3843 +{
3844 +       switch (fpm_state) {
3845 +
3846 +               case FPM_PCTL_STATE_RELOADING :
3847 +
3848 +                       fpm_pctl_exec();
3849 +                       break;
3850 +
3851 +               case FPM_PCTL_STATE_FINISHING :
3852 +
3853 +               case FPM_PCTL_STATE_TERMINATING :
3854 +
3855 +                       fpm_pctl_exit();
3856 +                       break;
3857 +       }
3858 +}
3859 +
3860 +int fpm_pctl_kill(pid_t pid, int how)
3861 +{
3862 +       int s = 0;
3863 +
3864 +       switch (how) {
3865 +               case FPM_PCTL_TERM :
3866 +                       s = SIGTERM;
3867 +                       break;
3868 +               case FPM_PCTL_STOP :
3869 +                       s = SIGSTOP;
3870 +                       break;
3871 +               case FPM_PCTL_CONT :
3872 +                       s = SIGCONT;
3873 +                       break;
3874 +               default :
3875 +                       break;
3876 +       }
3877 +
3878 +       return kill(pid, s);
3879 +}
3880 +
3881 +static void fpm_pctl_kill_all(int signo)
3882 +{
3883 +       struct fpm_worker_pool_s *wp;
3884 +       int alive_children = 0;
3885 +
3886 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
3887 +               struct fpm_child_s *child;
3888 +
3889 +               for (child = wp->children; child; child = child->next) {
3890 +
3891 +                       int res = kill(child->pid, signo);
3892 +
3893 +                       zlog(ZLOG_STUFF, ZLOG_NOTICE, "sending signal %d %s to child %d (pool %s)", signo,
3894 +                               fpm_signal_names[signo] ? fpm_signal_names[signo] : "",
3895 +                               (int) child->pid, child->wp->config->name);
3896 +
3897 +                       if (res == 0) ++alive_children;
3898 +               }
3899 +       }
3900 +
3901 +       if (alive_children) {
3902 +               zlog(ZLOG_STUFF, ZLOG_NOTICE, "%d %s still alive", alive_children, alive_children == 1 ? "child is" : "children are");
3903 +       }
3904 +}
3905 +
3906 +static void fpm_pctl_action_next()
3907 +{
3908 +       int sig, timeout;
3909 +
3910 +       if (!fpm_globals.running_children) fpm_pctl_action_last();
3911 +
3912 +       if (fpm_signal_sent == 0) {
3913 +               if (fpm_state == FPM_PCTL_STATE_TERMINATING) {
3914 +                       sig = SIGTERM;
3915 +               }
3916 +               else {
3917 +                       sig = SIGQUIT;
3918 +               }
3919 +               timeout = fpm_global_config.process_control_timeout;
3920 +       }
3921 +       else {
3922 +               if (fpm_signal_sent == SIGQUIT) {
3923 +                       sig = SIGTERM;
3924 +               }
3925 +               else {
3926 +                       sig = SIGKILL;
3927 +               }
3928 +               timeout = 1;
3929 +       }
3930 +
3931 +       fpm_pctl_kill_all(sig);
3932 +
3933 +       fpm_signal_sent = sig;
3934 +
3935 +       fpm_pctl_timeout_set(timeout);
3936 +}
3937 +
3938 +void fpm_pctl(int new_state, int action)
3939 +{
3940 +       switch (action) {
3941 +
3942 +               case FPM_PCTL_ACTION_SET :
3943 +
3944 +                       if (fpm_state == new_state) { /* already in progress - just ignore duplicate signal */
3945 +                               return;
3946 +                       }
3947 +
3948 +                       switch (fpm_state) { /* check which states can be overridden */
3949 +
3950 +                               case FPM_PCTL_STATE_NORMAL :
3951 +
3952 +                                       /* 'normal' can be overridden by any other state */
3953 +                                       break;
3954 +
3955 +                               case FPM_PCTL_STATE_RELOADING :
3956 +
3957 +                                       /* 'reloading' can be overridden by 'finishing' */
3958 +                                       if (new_state == FPM_PCTL_STATE_FINISHING) break;
3959 +
3960 +                               case FPM_PCTL_STATE_FINISHING :
3961 +
3962 +                                       /* 'reloading' and 'finishing' can be overridden by 'terminating' */
3963 +                                       if (new_state == FPM_PCTL_STATE_TERMINATING) break;
3964 +
3965 +                               case FPM_PCTL_STATE_TERMINATING :
3966 +
3967 +                                       /* nothing can override 'terminating' state */
3968 +                                       zlog(ZLOG_STUFF, ZLOG_NOTICE, "not switching to '%s' state, because already in '%s' state",
3969 +                                               fpm_state_names[new_state], fpm_state_names[fpm_state]);
3970 +
3971 +                                       return;
3972 +                       }
3973 +
3974 +                       fpm_signal_sent = 0;
3975 +                       fpm_state = new_state;
3976 +
3977 +                       zlog(ZLOG_STUFF, ZLOG_NOTICE, "switching to '%s' state", fpm_state_names[fpm_state]);
3978 +
3979 +                       /* fall down */
3980 +
3981 +               case FPM_PCTL_ACTION_TIMEOUT :
3982 +
3983 +                       fpm_pctl_action_next();
3984 +
3985 +                       break;
3986 +
3987 +               case FPM_PCTL_ACTION_LAST_CHILD_EXITED :
3988 +
3989 +                       fpm_pctl_action_last();
3990 +
3991 +                       break;
3992 +
3993 +       }
3994 +}
3995 +
3996 +int fpm_pctl_can_spawn_children()
3997 +{
3998 +       return fpm_state == FPM_PCTL_STATE_NORMAL;
3999 +}
4000 +
4001 +int fpm_pctl_child_exited()
4002 +{
4003 +       if (fpm_state == FPM_PCTL_STATE_NORMAL) return 0;
4004 +
4005 +       if (!fpm_globals.running_children) {
4006 +               fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_LAST_CHILD_EXITED);
4007 +       }
4008 +
4009 +       return 0;
4010 +}
4011 +
4012 +int fpm_pctl_init_main()
4013 +{
4014 +       int i;
4015 +
4016 +       saved_argc = fpm_globals.argc;
4017 +
4018 +       saved_argv = malloc(sizeof(char *) * (saved_argc + 1));
4019 +
4020 +       if (!saved_argv) {
4021 +               return -1;
4022 +       }
4023 +
4024 +       for (i = 0; i < saved_argc; i++) {
4025 +               saved_argv[i] = strdup(fpm_globals.argv[i]);
4026 +
4027 +               if (!saved_argv[i]) {
4028 +                       return -1;
4029 +               }
4030 +       }
4031 +
4032 +       saved_argv[i] = 0;
4033 +
4034 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_pctl_cleanup, 0)) {
4035 +               return -1;
4036 +       }
4037 +
4038 +       return 0;
4039 +}
4040 +
4041 +static void fpm_pctl_check_request_timeout(struct timeval *now)
4042 +{
4043 +       struct fpm_worker_pool_s *wp;
4044 +
4045 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
4046 +               int terminate_timeout = wp->config->request_terminate_timeout;
4047 +               int slowlog_timeout = wp->config->request_slowlog_timeout;
4048 +               struct fpm_child_s *child;
4049 +
4050 +               if (terminate_timeout || slowlog_timeout) {
4051 +                       for (child = wp->children; child; child = child->next) {
4052 +                               fpm_request_check_timed_out(child, now, terminate_timeout, slowlog_timeout);
4053 +                       }
4054 +               }
4055 +       }
4056 +       
4057 +}
4058 +
4059 +void fpm_pctl_heartbeat(int fd, short which, void *arg)
4060 +{
4061 +       static struct event heartbeat;
4062 +       struct timeval tv = { .tv_sec = 0, .tv_usec = 130000 };
4063 +       struct timeval now;
4064 +
4065 +       if (which == EV_TIMEOUT) {
4066 +               evtimer_del(&heartbeat);
4067 +               fpm_clock_get(&now);
4068 +               fpm_pctl_check_request_timeout(&now);
4069 +       }
4070 +
4071 +       evtimer_set(&heartbeat, &fpm_pctl_heartbeat, 0);
4072 +
4073 +       evtimer_add(&heartbeat, &tv);
4074 +}
4075 +
4076 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_process_ctl.h php-5.2.17/sapi/cgi/fpm/fpm_process_ctl.h
4077 --- php-5.2.17.org/sapi/cgi/fpm/fpm_process_ctl.h       1970-01-01 01:00:00.000000000 +0100
4078 +++ php-5.2.17/sapi/cgi/fpm/fpm_process_ctl.h   2021-10-23 19:09:19.829791741 +0200
4079 @@ -0,0 +1,39 @@
4080 +
4081 +       /* $Id$ */
4082 +       /* (c) 2007,2008 Andrei Nigmatulin */
4083 +
4084 +#ifndef FPM_PROCESS_CTL_H
4085 +#define FPM_PROCESS_CTL_H 1
4086 +
4087 +struct fpm_child_s;
4088 +
4089 +void fpm_pctl(int new_state, int action);
4090 +int fpm_pctl_can_spawn_children();
4091 +int fpm_pctl_kill(pid_t pid, int how);
4092 +void fpm_pctl_heartbeat(int fd, short which, void *arg);
4093 +int fpm_pctl_child_exited();
4094 +int fpm_pctl_init_main();
4095 +
4096 +
4097 +enum {
4098 +       FPM_PCTL_STATE_UNSPECIFIED,
4099 +       FPM_PCTL_STATE_NORMAL,
4100 +       FPM_PCTL_STATE_RELOADING,
4101 +       FPM_PCTL_STATE_TERMINATING,
4102 +       FPM_PCTL_STATE_FINISHING
4103 +};
4104 +
4105 +enum {
4106 +       FPM_PCTL_ACTION_SET,
4107 +       FPM_PCTL_ACTION_TIMEOUT,
4108 +       FPM_PCTL_ACTION_LAST_CHILD_EXITED
4109 +};
4110 +
4111 +enum {
4112 +       FPM_PCTL_TERM,
4113 +       FPM_PCTL_STOP,
4114 +       FPM_PCTL_CONT
4115 +};
4116 +
4117 +#endif
4118 +
4119 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_request.c php-5.2.17/sapi/cgi/fpm/fpm_request.c
4120 --- php-5.2.17.org/sapi/cgi/fpm/fpm_request.c   1970-01-01 01:00:00.000000000 +0100
4121 +++ php-5.2.17/sapi/cgi/fpm/fpm_request.c       2021-10-23 19:09:19.829791741 +0200
4122 @@ -0,0 +1,164 @@
4123 +
4124 +       /* $Id$ */
4125 +       /* (c) 2007,2008 Andrei Nigmatulin */
4126 +
4127 +#include "fpm_config.h"
4128 +
4129 +#include "fpm_php.h"
4130 +#include "fpm_str.h"
4131 +#include "fpm_clock.h"
4132 +#include "fpm_conf.h"
4133 +#include "fpm_trace.h"
4134 +#include "fpm_php_trace.h"
4135 +#include "fpm_process_ctl.h"
4136 +#include "fpm_children.h"
4137 +#include "fpm_shm_slots.h"
4138 +#include "fpm_request.h"
4139 +
4140 +#include "zlog.h"
4141 +
4142 +void fpm_request_accepting()
4143 +{
4144 +       struct fpm_shm_slot_s *slot;
4145 +
4146 +       slot = fpm_shm_slots_acquire(0, 0);
4147 +
4148 +       slot->request_stage = FPM_REQUEST_ACCEPTING;
4149 +
4150 +       fpm_clock_get(&slot->tv);
4151 +       memset(slot->request_method, 0, sizeof(slot->request_method));
4152 +       slot->content_length = 0;
4153 +       memset(slot->script_filename, 0, sizeof(slot->script_filename));
4154 +
4155 +       fpm_shm_slots_release(slot);
4156 +}
4157 +
4158 +void fpm_request_reading_headers()
4159 +{
4160 +       struct fpm_shm_slot_s *slot;
4161 +
4162 +       slot = fpm_shm_slots_acquire(0, 0);
4163 +
4164 +       slot->request_stage = FPM_REQUEST_READING_HEADERS;
4165 +
4166 +       fpm_clock_get(&slot->tv);
4167 +       slot->accepted = slot->tv;
4168 +
4169 +       fpm_shm_slots_release(slot);
4170 +}
4171 +
4172 +void fpm_request_info()
4173 +{
4174 +       TSRMLS_FETCH();
4175 +       struct fpm_shm_slot_s *slot;
4176 +       char *request_method = fpm_php_request_method(TSRMLS_C);
4177 +       char *script_filename = fpm_php_script_filename(TSRMLS_C);
4178 +
4179 +       slot = fpm_shm_slots_acquire(0, 0);
4180 +
4181 +       slot->request_stage = FPM_REQUEST_INFO;
4182 +
4183 +       fpm_clock_get(&slot->tv);
4184 +
4185 +       if (request_method) {
4186 +               cpystrn(slot->request_method, request_method, sizeof(slot->request_method));
4187 +       }
4188 +
4189 +       slot->content_length = fpm_php_content_length(TSRMLS_C);
4190 +
4191 +       /* if cgi.fix_pathinfo is set to "1" and script cannot be found (404)
4192 +               the sapi_globals.request_info.path_translated is set to NULL */
4193 +       if (script_filename) {
4194 +               cpystrn(slot->script_filename, script_filename, sizeof(slot->script_filename));
4195 +       }
4196 +
4197 +       fpm_shm_slots_release(slot);
4198 +}
4199 +
4200 +void fpm_request_executing()
4201 +{
4202 +       struct fpm_shm_slot_s *slot;
4203 +
4204 +       slot = fpm_shm_slots_acquire(0, 0);
4205 +
4206 +       slot->request_stage = FPM_REQUEST_EXECUTING;
4207 +
4208 +       fpm_clock_get(&slot->tv);
4209 +
4210 +       fpm_shm_slots_release(slot);
4211 +}
4212 +
4213 +void fpm_request_finished()
4214 +{
4215 +       struct fpm_shm_slot_s *slot;
4216 +
4217 +       slot = fpm_shm_slots_acquire(0, 0);
4218 +
4219 +       slot->request_stage = FPM_REQUEST_FINISHED;
4220 +
4221 +       fpm_clock_get(&slot->tv);
4222 +       memset(&slot->accepted, 0, sizeof(slot->accepted));
4223 +
4224 +       fpm_shm_slots_release(slot);
4225 +}
4226 +
4227 +void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *now, int terminate_timeout, int slowlog_timeout)
4228 +{
4229 +       struct fpm_shm_slot_s *slot;
4230 +       struct fpm_shm_slot_s slot_c;
4231 +
4232 +       slot = fpm_shm_slot(child);
4233 +
4234 +       if (!fpm_shm_slots_acquire(slot, 1)) {
4235 +               return;
4236 +       }
4237 +
4238 +       slot_c = *slot;
4239 +
4240 +       fpm_shm_slots_release(slot);
4241 +
4242 +#if HAVE_FPM_TRACE
4243 +       if (child->slow_logged.tv_sec) {
4244 +               if (child->slow_logged.tv_sec != slot_c.accepted.tv_sec || child->slow_logged.tv_usec != slot_c.accepted.tv_usec) {
4245 +                       child->slow_logged.tv_sec = 0;
4246 +                       child->slow_logged.tv_usec = 0;
4247 +               }
4248 +       }
4249 +#endif
4250 +
4251 +       if (slot_c.request_stage > FPM_REQUEST_ACCEPTING && slot_c.request_stage < FPM_REQUEST_FINISHED) {
4252 +               char purified_script_filename[sizeof(slot_c.script_filename)];
4253 +               struct timeval tv;
4254 +
4255 +               timersub(now, &slot_c.accepted, &tv);
4256 +
4257 +#if HAVE_FPM_TRACE
4258 +               if (child->slow_logged.tv_sec == 0 && slowlog_timeout &&
4259 +                               slot_c.request_stage == FPM_REQUEST_EXECUTING && tv.tv_sec >= slowlog_timeout) {
4260 +                       
4261 +                       str_purify_filename(purified_script_filename, slot_c.script_filename, sizeof(slot_c.script_filename));
4262 +
4263 +                       child->slow_logged = slot_c.accepted;
4264 +                       child->tracer = fpm_php_trace;
4265 +
4266 +                       fpm_trace_signal(child->pid);
4267 +
4268 +                       zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d, script '%s' (pool %s) executing too slow (%d.%06d sec), logging",
4269 +                               (int) child->pid, purified_script_filename, child->wp->config->name, (int) tv.tv_sec, (int) tv.tv_usec);
4270 +               }
4271 +
4272 +               else
4273 +#endif
4274 +               if (terminate_timeout && tv.tv_sec >= terminate_timeout) {
4275 +
4276 +                       str_purify_filename(purified_script_filename, slot_c.script_filename, sizeof(slot_c.script_filename));
4277 +
4278 +                       fpm_pctl_kill(child->pid, FPM_PCTL_TERM);
4279 +
4280 +                       zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d, script '%s' (pool %s) execution timed out (%d.%06d sec), terminating",
4281 +                               (int) child->pid, purified_script_filename, child->wp->config->name, (int) tv.tv_sec, (int) tv.tv_usec);
4282 +               }
4283 +       }
4284 +
4285 +}
4286 +
4287 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_request.h php-5.2.17/sapi/cgi/fpm/fpm_request.h
4288 --- php-5.2.17.org/sapi/cgi/fpm/fpm_request.h   1970-01-01 01:00:00.000000000 +0100
4289 +++ php-5.2.17/sapi/cgi/fpm/fpm_request.h       2021-10-23 19:09:19.829791741 +0200
4290 @@ -0,0 +1,27 @@
4291 +
4292 +       /* $Id$ */
4293 +       /* (c) 2007,2008 Andrei Nigmatulin */
4294 +
4295 +#ifndef FPM_REQUEST_H
4296 +#define FPM_REQUEST_H 1
4297 +
4298 +void fpm_request_accepting();                          /* hanging in accept() */
4299 +void fpm_request_reading_headers();                    /* start reading fastcgi request from very first byte */
4300 +void fpm_request_info();                                       /* not a stage really but a point in the php code, where all request params have become known to sapi */
4301 +void fpm_request_executing();                          /* the script is executing */
4302 +void fpm_request_finished();                           /* request processed: script response have been sent to web server */
4303 +
4304 +struct fpm_child_s;
4305 +struct timeval;
4306 +
4307 +void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *tv, int terminate_timeout, int slowlog_timeout);
4308 +
4309 +enum fpm_request_stage_e {
4310 +       FPM_REQUEST_ACCEPTING = 1,
4311 +       FPM_REQUEST_READING_HEADERS,
4312 +       FPM_REQUEST_INFO,
4313 +       FPM_REQUEST_EXECUTING,
4314 +       FPM_REQUEST_FINISHED
4315 +};
4316 +
4317 +#endif
4318 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_shm.c php-5.2.17/sapi/cgi/fpm/fpm_shm.c
4319 --- php-5.2.17.org/sapi/cgi/fpm/fpm_shm.c       1970-01-01 01:00:00.000000000 +0100
4320 +++ php-5.2.17/sapi/cgi/fpm/fpm_shm.c   2021-10-23 19:09:19.829791741 +0200
4321 @@ -0,0 +1,100 @@
4322 +
4323 +       /* $Id$ */
4324 +       /* (c) 2007,2008 Andrei Nigmatulin */
4325 +
4326 +#include "fpm_config.h"
4327 +
4328 +#include <unistd.h>
4329 +#include <sys/mman.h>
4330 +#include <stdlib.h>
4331 +
4332 +#include "fpm_shm.h"
4333 +#include "zlog.h"
4334 +
4335 +
4336 +/* MAP_ANON is depricated, but not in macosx */
4337 +#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
4338 +#define MAP_ANONYMOUS MAP_ANON
4339 +#endif
4340 +
4341 +
4342 +struct fpm_shm_s *fpm_shm_alloc(size_t sz)
4343 +{
4344 +       struct fpm_shm_s *shm;
4345 +
4346 +       shm = malloc(sizeof(*shm));
4347 +
4348 +       if (!shm) {
4349 +               return 0;
4350 +       }
4351 +
4352 +       shm->mem = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
4353 +
4354 +       if (!shm->mem) {
4355 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "mmap(MAP_ANONYMOUS | MAP_SHARED) failed");
4356 +               free(shm);
4357 +               return 0;
4358 +       }
4359 +
4360 +       shm->used = 0;
4361 +       shm->sz = sz;
4362 +
4363 +       return shm;
4364 +}
4365 +
4366 +static void fpm_shm_free(struct fpm_shm_s *shm, int do_unmap)
4367 +{
4368 +       if (do_unmap) {
4369 +               munmap(shm->mem, shm->sz);
4370 +       }
4371 +
4372 +       free(shm);      
4373 +}
4374 +
4375 +void fpm_shm_free_list(struct fpm_shm_s *shm, void *mem)
4376 +{
4377 +       struct fpm_shm_s *next;
4378 +
4379 +       for (; shm; shm = next) {
4380 +               next = shm->next;
4381 +
4382 +               fpm_shm_free(shm, mem != shm->mem);
4383 +       }
4384 +}
4385 +
4386 +void *fpm_shm_alloc_chunk(struct fpm_shm_s **head, size_t sz, void **mem)
4387 +{
4388 +       size_t pagesize = getpagesize();
4389 +       static const size_t cache_line_size = 16;
4390 +       size_t aligned_sz;
4391 +       struct fpm_shm_s *shm;
4392 +       void *ret;
4393 +
4394 +       sz = (sz + cache_line_size - 1) & -cache_line_size;
4395 +
4396 +       shm = *head;
4397 +
4398 +       if (0 == shm || shm->sz - shm->used < sz) {
4399 +               /* allocate one more shm segment */
4400 +
4401 +               aligned_sz = (sz + pagesize - 1) & -pagesize;
4402 +
4403 +               shm = fpm_shm_alloc(aligned_sz);
4404 +
4405 +               if (!shm) {
4406 +                       return 0;
4407 +               }
4408 +
4409 +               shm->next = *head;
4410 +               if (shm->next) shm->next->prev = shm;
4411 +               shm->prev = 0;
4412 +               *head = shm;
4413 +       }
4414 +
4415 +       *mem = shm->mem;
4416 +       ret = (char *) shm->mem + shm->used;
4417 +       shm->used += sz;
4418 +
4419 +       return ret;
4420 +}
4421 +
4422 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_shm.h php-5.2.17/sapi/cgi/fpm/fpm_shm.h
4423 --- php-5.2.17.org/sapi/cgi/fpm/fpm_shm.h       1970-01-01 01:00:00.000000000 +0100
4424 +++ php-5.2.17/sapi/cgi/fpm/fpm_shm.h   2021-10-23 19:09:19.829791741 +0200
4425 @@ -0,0 +1,22 @@
4426 +
4427 +       /* $Id$ */
4428 +       /* (c) 2007,2008 Andrei Nigmatulin */
4429 +
4430 +#ifndef FPM_SHM_H
4431 +#define FPM_SHM_H 1
4432 +
4433 +struct fpm_shm_s;
4434 +
4435 +struct fpm_shm_s {
4436 +       struct fpm_shm_s *prev, *next;
4437 +       void *mem;
4438 +       size_t sz;
4439 +       size_t used;
4440 +};
4441 +
4442 +struct fpm_shm_s *fpm_shm_alloc(size_t sz);
4443 +void fpm_shm_free_list(struct fpm_shm_s *, void *);
4444 +void *fpm_shm_alloc_chunk(struct fpm_shm_s **head, size_t sz, void **mem);
4445 +
4446 +#endif
4447 +
4448 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_shm_slots.c php-5.2.17/sapi/cgi/fpm/fpm_shm_slots.c
4449 --- php-5.2.17.org/sapi/cgi/fpm/fpm_shm_slots.c 1970-01-01 01:00:00.000000000 +0100
4450 +++ php-5.2.17/sapi/cgi/fpm/fpm_shm_slots.c     2021-10-23 19:09:19.829791741 +0200
4451 @@ -0,0 +1,127 @@
4452 +
4453 +       /* $Id$ */
4454 +       /* (c) 2007,2008 Andrei Nigmatulin */
4455 +
4456 +#include "fpm_config.h"
4457 +
4458 +#include "fpm_atomic.h"
4459 +#include "fpm_worker_pool.h"
4460 +#include "fpm_children.h"
4461 +#include "fpm_shm.h"
4462 +#include "fpm_shm_slots.h"
4463 +#include "zlog.h"
4464 +
4465 +static void *shm_mem;
4466 +static struct fpm_shm_slot_s *shm_slot;
4467 +
4468 +int fpm_shm_slots_prepare_slot(struct fpm_child_s *child)
4469 +{
4470 +       struct fpm_worker_pool_s *wp = child->wp;
4471 +       struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4472 +
4473 +       child->shm_slot_i = wp->slots_used.used;
4474 +
4475 +       shm_slot_ptr = fpm_array_push(&wp->slots_used);
4476 +
4477 +       if (0 == shm_slot_ptr) {
4478 +               return -1;
4479 +       }
4480 +
4481 +       if (0 == wp->slots_free.used) {
4482 +               shm_slot_ptr->shm_slot = fpm_shm_alloc_chunk(&wp->shm_list, sizeof(struct fpm_shm_slot_s), &shm_slot_ptr->mem);
4483 +
4484 +               if (!shm_slot_ptr->shm_slot) {
4485 +                       return -1;
4486 +               }
4487 +       }
4488 +       else {
4489 +               *shm_slot_ptr = *(struct fpm_shm_slot_ptr_s *) fpm_array_item_last(&wp->slots_free);
4490 +
4491 +               --wp->slots_free.used;
4492 +       }
4493 +
4494 +       memset(shm_slot_ptr->shm_slot, 0, sizeof(struct fpm_shm_slot_s));
4495 +
4496 +       shm_slot_ptr->child = child;
4497 +
4498 +       return 0;
4499 +}
4500 +
4501 +void fpm_shm_slots_discard_slot(struct fpm_child_s *child)
4502 +{
4503 +       struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4504 +       struct fpm_worker_pool_s *wp = child->wp;
4505 +       int n;
4506 +
4507 +       shm_slot_ptr = fpm_array_push(&wp->slots_free);
4508 +
4509 +       if (shm_slot_ptr) {
4510 +
4511 +               struct fpm_shm_slot_ptr_s *shm_slot_ptr_used;
4512 +
4513 +               shm_slot_ptr_used = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4514 +
4515 +               *shm_slot_ptr = *shm_slot_ptr_used;
4516 +
4517 +               shm_slot_ptr->child = 0;
4518 +
4519 +       }
4520 +
4521 +       n = fpm_array_item_remove(&wp->slots_used, child->shm_slot_i);
4522 +
4523 +       if (n > -1) {
4524 +               shm_slot_ptr = fpm_array_item(&wp->slots_used, n);
4525 +
4526 +               shm_slot_ptr->child->shm_slot_i = n;
4527 +       }
4528 +}
4529 +
4530 +void fpm_shm_slots_child_use_slot(struct fpm_child_s *child)
4531 +{
4532 +       struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4533 +       struct fpm_worker_pool_s *wp = child->wp;
4534 +
4535 +       shm_slot_ptr = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4536 +
4537 +       shm_slot = shm_slot_ptr->shm_slot;
4538 +       shm_mem = shm_slot_ptr->mem;
4539 +}
4540 +
4541 +void fpm_shm_slots_parent_use_slot(struct fpm_child_s *child)
4542 +{
4543 +       /* nothing to do */
4544 +}
4545 +
4546 +void *fpm_shm_slots_mem()
4547 +{
4548 +       return shm_mem;
4549 +}
4550 +
4551 +struct fpm_shm_slot_s *fpm_shm_slot(struct fpm_child_s *child)
4552 +{
4553 +       struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4554 +       struct fpm_worker_pool_s *wp = child->wp;
4555 +
4556 +       shm_slot_ptr = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4557 +
4558 +       return shm_slot_ptr->shm_slot;
4559 +}
4560 +
4561 +struct fpm_shm_slot_s *fpm_shm_slots_acquire(struct fpm_shm_slot_s *s, int nohang)
4562 +{
4563 +       if (s == 0) {
4564 +               s = shm_slot;
4565 +       }
4566 +
4567 +       if (0 > fpm_spinlock(&s->lock, nohang)) {
4568 +               return 0;
4569 +       }
4570 +
4571 +       return s;
4572 +}
4573 +
4574 +void fpm_shm_slots_release(struct fpm_shm_slot_s *s)
4575 +{
4576 +       s->lock = 0;
4577 +}
4578 +
4579 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_shm_slots.h php-5.2.17/sapi/cgi/fpm/fpm_shm_slots.h
4580 --- php-5.2.17.org/sapi/cgi/fpm/fpm_shm_slots.h 1970-01-01 01:00:00.000000000 +0100
4581 +++ php-5.2.17/sapi/cgi/fpm/fpm_shm_slots.h     2021-10-23 19:09:19.833125073 +0200
4582 @@ -0,0 +1,43 @@
4583 +
4584 +       /* $Id$ */
4585 +       /* (c) 2007,2008 Andrei Nigmatulin */
4586 +
4587 +#ifndef FPM_SHM_SLOTS_H
4588 +#define FPM_SHM_SLOTS_H 1
4589 +
4590 +#include "fpm_atomic.h"
4591 +#include "fpm_worker_pool.h"
4592 +#include "fpm_request.h"
4593 +
4594 +struct fpm_child_s;
4595 +
4596 +struct fpm_shm_slot_s {
4597 +       union {
4598 +               atomic_t lock;
4599 +               char dummy[16];
4600 +       };
4601 +       enum fpm_request_stage_e request_stage;
4602 +       struct timeval accepted;
4603 +       struct timeval tv;
4604 +       char request_method[16];
4605 +       size_t content_length; /* used with POST only */
4606 +       char script_filename[256];
4607 +};
4608 +
4609 +struct fpm_shm_slot_ptr_s {
4610 +       void *mem;
4611 +       struct fpm_shm_slot_s *shm_slot;
4612 +       struct fpm_child_s *child;
4613 +};
4614 +
4615 +int fpm_shm_slots_prepare_slot(struct fpm_child_s *child);
4616 +void fpm_shm_slots_discard_slot(struct fpm_child_s *child);
4617 +void fpm_shm_slots_child_use_slot(struct fpm_child_s *child);
4618 +void fpm_shm_slots_parent_use_slot(struct fpm_child_s *child);
4619 +void *fpm_shm_slots_mem();
4620 +struct fpm_shm_slot_s *fpm_shm_slot(struct fpm_child_s *child);
4621 +struct fpm_shm_slot_s *fpm_shm_slots_acquire(struct fpm_shm_slot_s *, int nohang);
4622 +void fpm_shm_slots_release(struct fpm_shm_slot_s *);
4623 +
4624 +#endif
4625 +
4626 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_signals.c php-5.2.17/sapi/cgi/fpm/fpm_signals.c
4627 --- php-5.2.17.org/sapi/cgi/fpm/fpm_signals.c   1970-01-01 01:00:00.000000000 +0100
4628 +++ php-5.2.17/sapi/cgi/fpm/fpm_signals.c       2021-10-23 19:09:19.833125073 +0200
4629 @@ -0,0 +1,252 @@
4630 +
4631 +       /* $Id$ */
4632 +       /* (c) 2007,2008 Andrei Nigmatulin */
4633 +
4634 +#include "fpm_config.h"
4635 +
4636 +#include <signal.h>
4637 +#include <stdio.h>
4638 +#include <sys/types.h>
4639 +#include <sys/socket.h>
4640 +#include <stdlib.h>
4641 +#include <string.h>
4642 +#include <fcntl.h>
4643 +#include <unistd.h>
4644 +#include <errno.h>
4645 +
4646 +#include "fpm.h"
4647 +#include "fpm_signals.h"
4648 +#include "fpm_sockets.h"
4649 +#include "fpm_php.h"
4650 +#include "zlog.h"
4651 +
4652 +static int sp[2];
4653 +
4654 +const char *fpm_signal_names[NSIG + 1] = {
4655 +#ifdef SIGHUP
4656 +       [SIGHUP]                = "SIGHUP",
4657 +#endif
4658 +#ifdef SIGINT
4659 +       [SIGINT]                = "SIGINT",
4660 +#endif
4661 +#ifdef SIGQUIT
4662 +       [SIGQUIT]               = "SIGQUIT",
4663 +#endif
4664 +#ifdef SIGILL
4665 +       [SIGILL]                = "SIGILL",
4666 +#endif
4667 +#ifdef SIGTRAP
4668 +       [SIGTRAP]               = "SIGTRAP",
4669 +#endif
4670 +#ifdef SIGABRT
4671 +       [SIGABRT]               = "SIGABRT",
4672 +#endif
4673 +#ifdef SIGEMT
4674 +       [SIGEMT]                = "SIGEMT",
4675 +#endif
4676 +#ifdef SIGBUS
4677 +       [SIGBUS]                = "SIGBUS",
4678 +#endif
4679 +#ifdef SIGFPE
4680 +       [SIGFPE]                = "SIGFPE",
4681 +#endif
4682 +#ifdef SIGKILL
4683 +       [SIGKILL]               = "SIGKILL",
4684 +#endif
4685 +#ifdef SIGUSR1
4686 +       [SIGUSR1]               = "SIGUSR1",
4687 +#endif
4688 +#ifdef SIGSEGV
4689 +       [SIGSEGV]               = "SIGSEGV",
4690 +#endif
4691 +#ifdef SIGUSR2
4692 +       [SIGUSR2]               = "SIGUSR2",
4693 +#endif
4694 +#ifdef SIGPIPE
4695 +       [SIGPIPE]               = "SIGPIPE",
4696 +#endif
4697 +#ifdef SIGALRM
4698 +       [SIGALRM]               = "SIGALRM",
4699 +#endif
4700 +#ifdef SIGTERM
4701 +       [SIGTERM]               = "SIGTERM",
4702 +#endif
4703 +#ifdef SIGCHLD
4704 +       [SIGCHLD]               = "SIGCHLD",
4705 +#endif
4706 +#ifdef SIGCONT
4707 +       [SIGCONT]               = "SIGCONT",
4708 +#endif
4709 +#ifdef SIGSTOP
4710 +       [SIGSTOP]               = "SIGSTOP",
4711 +#endif
4712 +#ifdef SIGTSTP
4713 +       [SIGTSTP]               = "SIGTSTP",
4714 +#endif
4715 +#ifdef SIGTTIN
4716 +       [SIGTTIN]               = "SIGTTIN",
4717 +#endif
4718 +#ifdef SIGTTOU
4719 +       [SIGTTOU]               = "SIGTTOU",
4720 +#endif
4721 +#ifdef SIGURG
4722 +       [SIGURG]                = "SIGURG",
4723 +#endif
4724 +#ifdef SIGXCPU
4725 +       [SIGXCPU]               = "SIGXCPU",
4726 +#endif
4727 +#ifdef SIGXFSZ
4728 +       [SIGXFSZ]               = "SIGXFSZ",
4729 +#endif
4730 +#ifdef SIGVTALRM
4731 +       [SIGVTALRM]     = "SIGVTALRM",
4732 +#endif
4733 +#ifdef SIGPROF
4734 +       [SIGPROF]               = "SIGPROF",
4735 +#endif
4736 +#ifdef SIGWINCH
4737 +       [SIGWINCH]              = "SIGWINCH",
4738 +#endif
4739 +#ifdef SIGINFO
4740 +       [SIGINFO]               = "SIGINFO",
4741 +#endif
4742 +#ifdef SIGIO
4743 +       [SIGIO]                 = "SIGIO",
4744 +#endif
4745 +#ifdef SIGPWR
4746 +       [SIGPWR]                = "SIGPWR",
4747 +#endif
4748 +#ifdef SIGSYS
4749 +       [SIGSYS]                = "SIGSYS",
4750 +#endif
4751 +#ifdef SIGWAITING
4752 +       [SIGWAITING]    = "SIGWAITING",
4753 +#endif
4754 +#ifdef SIGLWP
4755 +       [SIGLWP]                = "SIGLWP",
4756 +#endif
4757 +#ifdef SIGFREEZE
4758 +       [SIGFREEZE]     = "SIGFREEZE",
4759 +#endif
4760 +#ifdef SIGTHAW
4761 +       [SIGTHAW]               = "SIGTHAW",
4762 +#endif
4763 +#ifdef SIGCANCEL
4764 +       [SIGCANCEL]     = "SIGCANCEL",
4765 +#endif
4766 +#ifdef SIGLOST
4767 +       [SIGLOST]               = "SIGLOST",
4768 +#endif
4769 +};
4770 +
4771 +static void sig_soft_quit(int signo)
4772 +{
4773 +       int saved_errno = errno;
4774 +
4775 +       /* closing fastcgi listening socket will force fcgi_accept() exit immediately */
4776 +       close(0);
4777 +       socket(AF_UNIX, SOCK_STREAM, 0);
4778 +
4779 +       fpm_php_soft_quit();
4780 +
4781 +       errno = saved_errno;
4782 +}
4783 +
4784 +static void sig_handler(int signo)
4785 +{
4786 +       static const char sig_chars[NSIG + 1] = {
4787 +               [SIGTERM] = 'T',
4788 +               [SIGINT]  = 'I',
4789 +               [SIGUSR1] = '1',
4790 +               [SIGUSR2] = '2',
4791 +               [SIGQUIT] = 'Q',
4792 +               [SIGCHLD] = 'C'
4793 +       };
4794 +       char s;
4795 +       int saved_errno;
4796 +
4797 +       if (fpm_globals.parent_pid != getpid()) {
4798 +               /* prevent a signal race condition when child process
4799 +                       have not set up it's own signal handler yet */
4800 +               return;
4801 +       }
4802 +
4803 +       saved_errno = errno;
4804 +
4805 +       s = sig_chars[signo];
4806 +
4807 +       write(sp[1], &s, sizeof(s));
4808 +
4809 +       errno = saved_errno;
4810 +}
4811 +
4812 +int fpm_signals_init_main()
4813 +{
4814 +       struct sigaction act;
4815 +
4816 +       if (0 > socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) {
4817 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "socketpair() failed");
4818 +               return -1;
4819 +       }
4820 +
4821 +       if (0 > fd_set_blocked(sp[0], 0) || 0 > fd_set_blocked(sp[1], 0)) {
4822 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fd_set_blocked() failed");
4823 +               return -1;
4824 +       }
4825 +
4826 +       if (0 > fcntl(sp[0], F_SETFD, FD_CLOEXEC) || 0 > fcntl(sp[1], F_SETFD, FD_CLOEXEC)) {
4827 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fcntl(F_SETFD, FD_CLOEXEC) failed");
4828 +               return -1;
4829 +       }
4830 +
4831 +       memset(&act, 0, sizeof(act));
4832 +       act.sa_handler = sig_handler;
4833 +       sigfillset(&act.sa_mask);
4834 +
4835 +       if (0 > sigaction(SIGTERM,  &act, 0) ||
4836 +               0 > sigaction(SIGINT,   &act, 0) ||
4837 +               0 > sigaction(SIGUSR1,  &act, 0) ||
4838 +               0 > sigaction(SIGUSR2,  &act, 0) ||
4839 +               0 > sigaction(SIGCHLD,  &act, 0) ||
4840 +               0 > sigaction(SIGQUIT,  &act, 0)) {
4841 +
4842 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "sigaction() failed");
4843 +               return -1;
4844 +       }
4845 +
4846 +       return 0;
4847 +}
4848 +
4849 +int fpm_signals_init_child()
4850 +{
4851 +       struct sigaction act, act_dfl;
4852 +
4853 +       memset(&act, 0, sizeof(act));
4854 +       memset(&act_dfl, 0, sizeof(act_dfl));
4855 +
4856 +       act.sa_handler = &sig_soft_quit;
4857 +       act.sa_flags |= SA_RESTART;
4858 +
4859 +       act_dfl.sa_handler = SIG_DFL;
4860 +
4861 +       close(sp[0]);
4862 +       close(sp[1]);
4863 +
4864 +       if (0 > sigaction(SIGTERM,  &act_dfl,  0) ||
4865 +               0 > sigaction(SIGINT,   &act_dfl,  0) ||
4866 +               0 > sigaction(SIGUSR1,  &act_dfl,  0) ||
4867 +               0 > sigaction(SIGUSR2,  &act_dfl,  0) ||
4868 +               0 > sigaction(SIGCHLD,  &act_dfl,  0) ||
4869 +               0 > sigaction(SIGQUIT,  &act,      0)) {
4870 +
4871 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "sigaction() failed");
4872 +               return -1;
4873 +       }
4874 +
4875 +       return 0;
4876 +}
4877 +
4878 +int fpm_signals_get_fd()
4879 +{
4880 +       return sp[0];
4881 +}
4882 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_signals.h php-5.2.17/sapi/cgi/fpm/fpm_signals.h
4883 --- php-5.2.17.org/sapi/cgi/fpm/fpm_signals.h   1970-01-01 01:00:00.000000000 +0100
4884 +++ php-5.2.17/sapi/cgi/fpm/fpm_signals.h       2021-10-23 19:09:19.833125073 +0200
4885 @@ -0,0 +1,16 @@
4886 +
4887 +       /* $Id$ */
4888 +       /* (c) 2007,2008 Andrei Nigmatulin */
4889 +
4890 +#ifndef FPM_SIGNALS_H
4891 +#define FPM_SIGNALS_H 1
4892 +
4893 +#include <signal.h>
4894 +
4895 +int fpm_signals_init_main();
4896 +int fpm_signals_init_child();
4897 +int fpm_signals_get_fd();
4898 +
4899 +extern const char *fpm_signal_names[NSIG + 1];
4900 +
4901 +#endif
4902 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_sockets.c php-5.2.17/sapi/cgi/fpm/fpm_sockets.c
4903 --- php-5.2.17.org/sapi/cgi/fpm/fpm_sockets.c   1970-01-01 01:00:00.000000000 +0100
4904 +++ php-5.2.17/sapi/cgi/fpm/fpm_sockets.c       2021-10-23 19:09:19.833125073 +0200
4905 @@ -0,0 +1,427 @@
4906 +
4907 +       /* $Id$ */
4908 +       /* (c) 2007,2008 Andrei Nigmatulin */
4909 +
4910 +#include "fpm_config.h"
4911 +
4912 +#ifdef HAVE_ALLOCA_H
4913 +#include <alloca.h>
4914 +#endif
4915 +#include <sys/types.h>
4916 +#include <sys/stat.h> /* for chmod(2) */
4917 +#include <sys/socket.h>
4918 +#include <netinet/in.h>
4919 +#include <arpa/inet.h>
4920 +#include <sys/un.h>
4921 +#include <netdb.h>
4922 +#include <stdio.h>
4923 +#include <stdlib.h>
4924 +#include <string.h>
4925 +#include <errno.h>
4926 +#include <unistd.h>
4927 +
4928 +#include "zlog.h"
4929 +#include "fpm_arrays.h"
4930 +#include "fpm_sockets.h"
4931 +#include "fpm_worker_pool.h"
4932 +#include "fpm_unix.h"
4933 +#include "fpm_str.h"
4934 +#include "fpm_env.h"
4935 +#include "fpm_cleanup.h"
4936 +
4937 +struct listening_socket_s {
4938 +       int refcount;
4939 +       int sock;
4940 +       int type;
4941 +       char *key;
4942 +};
4943 +
4944 +static struct fpm_array_s sockets_list;
4945 +
4946 +static int fpm_sockets_resolve_af_inet(char *node, char *service, struct sockaddr_in *addr)
4947 +{
4948 +       struct addrinfo *res;
4949 +       struct addrinfo hints;
4950 +       int ret;
4951 +
4952 +       memset(&hints, 0, sizeof(hints));
4953 +
4954 +       hints.ai_family = AF_INET;
4955 +
4956 +       ret = getaddrinfo(node, service, &hints, &res);
4957 +
4958 +       if (ret != 0) {
4959 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "can't resolve hostname '%s%s%s': getaddrinfo said: %s%s%s\n",
4960 +                                       node, service ? ":" : "", service ? service : "",
4961 +                                       gai_strerror(ret), ret == EAI_SYSTEM ? ", system error: " : "", ret == EAI_SYSTEM ? strerror(errno) : "");
4962 +               return -1;
4963 +       }
4964 +
4965 +       *addr = *(struct sockaddr_in *) res->ai_addr;
4966 +
4967 +       freeaddrinfo(res);
4968 +
4969 +       return 0;
4970 +}
4971 +
4972 +enum { FPM_GET_USE_SOCKET = 1, FPM_STORE_SOCKET = 2, FPM_STORE_USE_SOCKET = 3 };
4973 +
4974 +static void fpm_sockets_cleanup(int which, void *arg)
4975 +{
4976 +       int i;
4977 +       char *env_value = 0;
4978 +       int p = 0;
4979 +       struct listening_socket_s *ls = sockets_list.data;
4980 +
4981 +       for (i = 0; i < sockets_list.used; i++, ls++) {
4982 +
4983 +               if (which != FPM_CLEANUP_PARENT_EXEC) {
4984 +
4985 +                       close(ls->sock);
4986 +
4987 +               }
4988 +               else { /* on PARENT EXEC we want socket fds to be inherited through environment variable */
4989 +                       char fd[32];
4990 +                       sprintf(fd, "%d", ls->sock);
4991 +                       env_value = realloc(env_value, p + (p ? 1 : 0) + strlen(ls->key) + 1 + strlen(fd) + 1);
4992 +                       p += sprintf(env_value + p, "%s%s=%s", p ? "," : "", ls->key, fd);
4993 +               }
4994 +
4995 +               if (which == FPM_CLEANUP_PARENT_EXIT_MAIN) {
4996 +
4997 +                       if (ls->type == FPM_AF_UNIX) {
4998 +                               unlink(ls->key);
4999 +                       }
5000 +
5001 +               }
5002 +
5003 +               free(ls->key);
5004 +       }
5005 +
5006 +       if (env_value) {
5007 +               setenv("FPM_SOCKETS", env_value, 1);
5008 +               free(env_value);
5009 +       }
5010 +
5011 +       fpm_array_free(&sockets_list);
5012 +}
5013 +
5014 +static int fpm_sockets_hash_op(int sock, struct sockaddr *sa, char *key, int type, int op)
5015 +{
5016 +
5017 +       if (key == NULL) {
5018 +
5019 +               switch (type) {
5020 +
5021 +                       case FPM_AF_INET : {
5022 +                               struct sockaddr_in *sa_in = (struct sockaddr_in *) sa;
5023 +
5024 +                               key = alloca(sizeof("xxx.xxx.xxx.xxx:ppppp"));
5025 +
5026 +                               sprintf(key, "%u.%u.%u.%u:%u", IPQUAD(&sa_in->sin_addr), (unsigned int) ntohs(sa_in->sin_port));
5027 +
5028 +                               break;
5029 +                       }
5030 +
5031 +                       case FPM_AF_UNIX : {
5032 +                               struct sockaddr_un *sa_un = (struct sockaddr_un *) sa;
5033 +
5034 +                               key = alloca(strlen(sa_un->sun_path) + 1);
5035 +
5036 +                               strcpy(key, sa_un->sun_path);
5037 +
5038 +                               break;
5039 +                       }
5040 +
5041 +                       default :
5042 +
5043 +                               return -1;
5044 +               }
5045 +
5046 +       }
5047 +
5048 +       switch (op) {
5049 +
5050 +               case FPM_GET_USE_SOCKET :
5051 +               {
5052 +
5053 +                       int i;
5054 +                       struct listening_socket_s *ls = sockets_list.data;
5055 +
5056 +                       for (i = 0; i < sockets_list.used; i++, ls++) {
5057 +
5058 +                               if (!strcmp(ls->key, key)) {
5059 +                                       ++ls->refcount;
5060 +                                       return ls->sock;
5061 +                               }
5062 +                       }
5063 +
5064 +                       break;
5065 +               }
5066 +
5067 +               case FPM_STORE_SOCKET :                 /* inherited socket */
5068 +               case FPM_STORE_USE_SOCKET :             /* just created */
5069 +               {
5070 +
5071 +                       struct listening_socket_s *ls;
5072 +
5073 +                       ls = fpm_array_push(&sockets_list);
5074 +
5075 +                       if (!ls) {
5076 +                               break;
5077 +                       }
5078 +
5079 +                       if (op == FPM_STORE_SOCKET) {
5080 +                               ls->refcount = 0;
5081 +                       }
5082 +                       else {
5083 +                               ls->refcount = 1;
5084 +                       }
5085 +                       ls->type = type;
5086 +                       ls->sock = sock;
5087 +                       ls->key = strdup(key);
5088 +
5089 +                       return 0;
5090 +
5091 +               }
5092 +       }
5093 +
5094 +       return -1;
5095 +
5096 +}
5097 +
5098 +static int fpm_sockets_new_listening_socket(struct fpm_worker_pool_s *wp, struct sockaddr *sa, int socklen)
5099 +{
5100 +       int backlog = -1;
5101 +       int flags = 1;
5102 +       int sock;
5103 +       mode_t saved_umask;
5104 +
5105 +       /* we have custom backlog value */
5106 +       if (wp->config->listen_options) {
5107 +               backlog = wp->config->listen_options->backlog;
5108 +       }
5109 +
5110 +       sock = socket(sa->sa_family, SOCK_STREAM, 0);
5111 +
5112 +       if (0 > sock) {
5113 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "socket() failed");
5114 +               return -1;
5115 +       }
5116 +
5117 +       setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
5118 +
5119 +       if (wp->listen_address_domain == FPM_AF_UNIX) {
5120 +               unlink( ((struct sockaddr_un *) sa)->sun_path);
5121 +       }
5122 +
5123 +       saved_umask = umask(0777 ^ wp->socket_mode);
5124 +
5125 +       if (0 > bind(sock, sa, socklen)) {
5126 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "bind() for address '%s' failed", wp->config->listen_address);
5127 +               return -1;
5128 +       }
5129 +
5130 +       if (wp->listen_address_domain == FPM_AF_UNIX) {
5131 +
5132 +               char *path = ((struct sockaddr_un *) sa)->sun_path;
5133 +
5134 +               if (wp->socket_uid != -1 || wp->socket_gid != -1) {
5135 +
5136 +                       if (0 > chown(path, wp->socket_uid, wp->socket_gid)) {
5137 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chown() for address '%s' failed", wp->config->listen_address);
5138 +                               return -1;
5139 +                       }
5140 +
5141 +               }
5142 +
5143 +       }
5144 +
5145 +       umask(saved_umask);
5146 +
5147 +       if (0 > listen(sock, backlog)) {
5148 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "listen() for address '%s' failed", wp->config->listen_address);
5149 +               return -1;
5150 +       }
5151 +
5152 +       return sock;
5153 +}
5154 +
5155 +static int fpm_sockets_get_listening_socket(struct fpm_worker_pool_s *wp, struct sockaddr *sa, int socklen)
5156 +{
5157 +       int sock;
5158 +
5159 +       sock = fpm_sockets_hash_op(0, sa, 0, wp->listen_address_domain, FPM_GET_USE_SOCKET);
5160 +
5161 +       if (sock >= 0) return sock;
5162 +
5163 +       sock = fpm_sockets_new_listening_socket(wp, sa, socklen);
5164 +
5165 +       fpm_sockets_hash_op(sock, sa, 0, wp->listen_address_domain, FPM_STORE_USE_SOCKET);
5166 +
5167 +       return sock;
5168 +}
5169 +
5170 +enum fpm_address_domain fpm_sockets_domain_from_address(char *address)
5171 +{
5172 +       if (strchr(address, ':')) return FPM_AF_INET;
5173 +
5174 +       if (strlen(address) == strspn(address, "0123456789")) return FPM_AF_INET;
5175 +
5176 +       return FPM_AF_UNIX;
5177 +}
5178 +
5179 +static int fpm_socket_af_inet_listening_socket(struct fpm_worker_pool_s *wp)
5180 +{
5181 +       struct sockaddr_in sa_in;
5182 +       char *dup_address = strdup(wp->config->listen_address);
5183 +       char *port_str = strchr(dup_address, ':');
5184 +       char *addr = NULL;
5185 +       int port = 0;
5186 +
5187 +       if (port_str) { /* this is host:port pair */
5188 +               *port_str++ = '\0';
5189 +               port = atoi(port_str);
5190 +               addr = dup_address;
5191 +       }
5192 +       else if (strlen(dup_address) == strspn(dup_address, "0123456789")) { /* this is port */
5193 +               port = atoi(dup_address);
5194 +               port_str = dup_address;
5195 +       }
5196 +
5197 +       if (port == 0) {
5198 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "invalid port value '%s'", port_str);
5199 +               return -1;
5200 +       }
5201 +
5202 +       memset(&sa_in, 0, sizeof(sa_in));
5203 +
5204 +       if (addr) {
5205 +
5206 +               sa_in.sin_addr.s_addr = inet_addr(addr);
5207 +
5208 +               if (sa_in.sin_addr.s_addr == INADDR_NONE) { /* do resolve */
5209 +                       if (0 > fpm_sockets_resolve_af_inet(addr, NULL, &sa_in)) {
5210 +                               return -1;
5211 +                       }
5212 +                       zlog(ZLOG_STUFF, ZLOG_NOTICE, "address '%s' resolved as %u.%u.%u.%u", addr, IPQUAD(&sa_in.sin_addr));
5213 +               }
5214 +       }
5215 +       else {
5216 +
5217 +               sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
5218 +
5219 +       }
5220 +
5221 +       sa_in.sin_family = AF_INET;
5222 +       sa_in.sin_port = htons(port);
5223 +
5224 +       free(dup_address);
5225 +
5226 +       return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_in, sizeof(struct sockaddr_in));
5227 +}
5228 +
5229 +static int fpm_socket_af_unix_listening_socket(struct fpm_worker_pool_s *wp)
5230 +{
5231 +       struct sockaddr_un sa_un;
5232 +
5233 +       memset(&sa_un, 0, sizeof(sa_un));
5234 +
5235 +       cpystrn(sa_un.sun_path, wp->config->listen_address, sizeof(sa_un.sun_path));
5236 +       sa_un.sun_family = AF_UNIX;
5237 +
5238 +       return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_un, sizeof(struct sockaddr_un));
5239 +}
5240 +
5241 +int fpm_sockets_init_main()
5242 +{
5243 +       int i;
5244 +       struct fpm_worker_pool_s *wp;
5245 +       char *inherited = getenv("FPM_SOCKETS");
5246 +       struct listening_socket_s *ls;
5247 +
5248 +       if (0 == fpm_array_init(&sockets_list, sizeof(struct listening_socket_s), 10)) {
5249 +               return -1;
5250 +       }
5251 +
5252 +       /* import inherited sockets */
5253 +       while (inherited && *inherited) {
5254 +               char *comma = strchr(inherited, ',');
5255 +               int type, fd_no;
5256 +               char *eq;
5257 +
5258 +               if (comma) *comma = '\0';
5259 +
5260 +               eq = strchr(inherited, '=');
5261 +
5262 +               if (eq) {
5263 +                       *eq = '\0';
5264 +
5265 +                       fd_no = atoi(eq + 1);
5266 +
5267 +                       type = fpm_sockets_domain_from_address(inherited);
5268 +
5269 +                       zlog(ZLOG_STUFF, ZLOG_NOTICE, "using inherited socket fd=%d, \"%s\"", fd_no, inherited);
5270 +
5271 +                       fpm_sockets_hash_op(fd_no, 0, inherited, type, FPM_STORE_SOCKET);
5272 +               }
5273 +
5274 +               if (comma) inherited = comma + 1;
5275 +               else inherited = 0;
5276 +       }
5277 +
5278 +       /* create all required sockets */
5279 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
5280 +
5281 +               if (!wp->is_template) {
5282 +
5283 +                       switch (wp->listen_address_domain) {
5284 +
5285 +                               case FPM_AF_INET :
5286 +
5287 +                                       wp->listening_socket = fpm_socket_af_inet_listening_socket(wp);
5288 +                                       break;
5289 +
5290 +                               case FPM_AF_UNIX :
5291 +
5292 +                                       if (0 > fpm_unix_resolve_socket_premissions(wp)) {
5293 +                                               return -1;
5294 +                                       }
5295 +
5296 +                                       wp->listening_socket = fpm_socket_af_unix_listening_socket(wp);
5297 +                                       break;
5298 +
5299 +                       }
5300 +
5301 +                       if (wp->listening_socket == -1) {
5302 +                               return -1;
5303 +                       }
5304 +               }
5305 +
5306 +       }
5307 +
5308 +       /* close unused sockets that was inherited */
5309 +       ls = sockets_list.data;
5310 +
5311 +       for (i = 0; i < sockets_list.used; ) {
5312 +
5313 +               if (ls->refcount == 0) {
5314 +                       close(ls->sock);
5315 +                       if (ls->type == FPM_AF_UNIX) {
5316 +                               unlink(ls->key);
5317 +                       }
5318 +                       free(ls->key);
5319 +                       fpm_array_item_remove(&sockets_list, i);
5320 +               }
5321 +               else {
5322 +                       ++i;
5323 +                       ++ls;
5324 +               }
5325 +       }
5326 +
5327 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_sockets_cleanup, 0)) {
5328 +               return -1;
5329 +       }
5330 +
5331 +       return 0;
5332 +}
5333 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_sockets.h php-5.2.17/sapi/cgi/fpm/fpm_sockets.h
5334 --- php-5.2.17.org/sapi/cgi/fpm/fpm_sockets.h   1970-01-01 01:00:00.000000000 +0100
5335 +++ php-5.2.17/sapi/cgi/fpm/fpm_sockets.h       2021-10-23 19:09:19.833125073 +0200
5336 @@ -0,0 +1,37 @@
5337 +
5338 +       /* $Id$ */
5339 +       /* (c) 2007,2008 Andrei Nigmatulin */
5340 +
5341 +#ifndef FPM_MISC_H
5342 +#define FPM_MISC_H 1
5343 +
5344 +#include <unistd.h>
5345 +#include <fcntl.h>
5346 +
5347 +#include "fpm_worker_pool.h"
5348 +
5349 +enum fpm_address_domain fpm_sockets_domain_from_address(char *addr);
5350 +int fpm_sockets_init_main();
5351 +
5352 +
5353 +static inline int fd_set_blocked(int fd, int blocked)
5354 +{
5355 +       int flags = fcntl(fd, F_GETFL);
5356 +
5357 +       if (flags < 0) return -1;
5358 +
5359 +       if (blocked)
5360 +               flags &= ~O_NONBLOCK;
5361 +       else
5362 +               flags |= O_NONBLOCK;
5363 +
5364 +       return fcntl(fd, F_SETFL, flags);
5365 +}
5366 +
5367 +#define IPQUAD(sin_addr) \
5368 +                       (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[0], \
5369 +                       (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[1], \
5370 +                       (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[2], \
5371 +                       (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[3]
5372 +
5373 +#endif
5374 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_stdio.c php-5.2.17/sapi/cgi/fpm/fpm_stdio.c
5375 --- php-5.2.17.org/sapi/cgi/fpm/fpm_stdio.c     1970-01-01 01:00:00.000000000 +0100
5376 +++ php-5.2.17/sapi/cgi/fpm/fpm_stdio.c 2021-10-23 19:09:19.833125073 +0200
5377 @@ -0,0 +1,286 @@
5378 +
5379 +       /* $Id$ */
5380 +       /* (c) 2007,2008 Andrei Nigmatulin */
5381 +
5382 +#include "fpm_config.h"
5383 +
5384 +#include <sys/types.h>
5385 +#include <sys/stat.h>
5386 +#include <string.h>
5387 +#include <fcntl.h>
5388 +#include <unistd.h>
5389 +#include <errno.h>
5390 +
5391 +#include "fpm.h"
5392 +#include "fpm_children.h"
5393 +#include "fpm_events.h"
5394 +#include "fpm_sockets.h"
5395 +#include "fpm_stdio.h"
5396 +#include "zlog.h"
5397 +
5398 +static int fd_stdout[2];
5399 +static int fd_stderr[2];
5400 +
5401 +int fpm_stdio_init_main()
5402 +{
5403 +       int fd = open("/dev/null", O_RDWR);
5404 +
5405 +       if (0 > fd) {
5406 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"/dev/null\") failed");
5407 +               return -1;
5408 +       }
5409 +
5410 +       if (0 > dup2(fd, STDIN_FILENO) || 0 > dup2(fd, STDOUT_FILENO)) {
5411 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
5412 +               return -1;
5413 +       }
5414 +
5415 +       close(fd);
5416 +
5417 +       return 0;
5418 +}
5419 +
5420 +int fpm_stdio_init_final()
5421 +{
5422 +       if (fpm_global_config.daemonize) {
5423 +
5424 +               if (fpm_globals.error_log_fd != STDERR_FILENO) {
5425 +                       /* there might be messages to stderr from libevent, we need to log them all */
5426 +                       if (0 > dup2(fpm_globals.error_log_fd, STDERR_FILENO)) {
5427 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
5428 +                               return -1;
5429 +                       }
5430 +               }
5431 +
5432 +               zlog_set_level(fpm_globals.log_level);
5433 +
5434 +               zlog_set_fd(fpm_globals.error_log_fd);
5435 +       }
5436 +
5437 +       return 0;
5438 +}
5439 +
5440 +int fpm_stdio_init_child(struct fpm_worker_pool_s *wp)
5441 +{
5442 +       close(fpm_globals.error_log_fd);
5443 +       fpm_globals.error_log_fd = -1;
5444 +       zlog_set_fd(-1);
5445 +
5446 +       if (wp->listening_socket != STDIN_FILENO) {
5447 +               if (0 > dup2(wp->listening_socket, STDIN_FILENO)) {
5448 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
5449 +                       return -1;
5450 +               }
5451 +       }
5452 +
5453 +       return 0;
5454 +}
5455 +
5456 +static void fpm_stdio_child_said(int fd, short which, void *arg)
5457 +{
5458 +       static const int max_buf_size = 1024;
5459 +       char buf[max_buf_size];
5460 +       struct fpm_child_s *child = arg;
5461 +       int is_stdout = fd == child->fd_stdout;
5462 +       struct event *ev = is_stdout ? &child->ev_stdout : &child->ev_stderr;
5463 +       int fifo_in = 1, fifo_out = 1;
5464 +       int is_last_message = 0;
5465 +       int in_buf = 0;
5466 +       int res;
5467 +
5468 +#if 0
5469 +       zlog(ZLOG_STUFF, ZLOG_DEBUG, "child %d said %s", (int) child->pid, is_stdout ? "stdout" : "stderr");
5470 +#endif
5471 +
5472 +       while (fifo_in || fifo_out) {
5473 +
5474 +               if (fifo_in) {
5475 +
5476 +                       res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf);
5477 +
5478 +                       if (res <= 0) { /* no data */
5479 +                               fifo_in = 0;
5480 +
5481 +                               if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
5482 +                                       /* just no more data ready */
5483 +                               }
5484 +                               else { /* error or pipe is closed */
5485 +
5486 +                                       if (res < 0) { /* error */
5487 +                                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
5488 +                                       }
5489 +
5490 +                                       fpm_event_del(ev);
5491 +                                       is_last_message = 1;
5492 +
5493 +                                       if (is_stdout) {
5494 +                                               close(child->fd_stdout);
5495 +                                               child->fd_stdout = -1;
5496 +                                       }
5497 +                                       else {
5498 +                                               close(child->fd_stderr);
5499 +                                               child->fd_stderr = -1;
5500 +                                       }
5501 +
5502 +#if 0
5503 +                                       if (in_buf == 0 && !fpm_globals.is_child) {
5504 +                                               zlog(ZLOG_STUFF, ZLOG_DEBUG, "child %d (pool %s) %s pipe is closed", (int) child->pid,
5505 +                                                       child->wp->config->name, is_stdout ? "stdout" : "stderr");
5506 +                                       }
5507 +#endif
5508 +                               }
5509 +                       }
5510 +                       else {
5511 +                               in_buf += res;
5512 +                       }
5513 +               }
5514 +
5515 +               if (fifo_out) {
5516 +                       if (in_buf == 0) {
5517 +                               fifo_out = 0;
5518 +                       }
5519 +                       else {
5520 +                               char *nl;
5521 +                               int should_print = 0;
5522 +                               buf[in_buf] = '\0';
5523 +
5524 +                               /* FIXME: there might be binary data */
5525 +
5526 +                               /* we should print if no more space in the buffer */
5527 +                               if (in_buf == max_buf_size - 1) {
5528 +                                       should_print = 1;
5529 +                               }
5530 +
5531 +                               /* we should print if no more data to come */
5532 +                               if (!fifo_in) {
5533 +                                       should_print = 1;
5534 +                               }
5535 +
5536 +                               nl = strchr(buf, '\n');
5537 +
5538 +                               if (nl || should_print) {
5539 +
5540 +                                       if (nl) {
5541 +                                               *nl = '\0';
5542 +                                       }
5543 +
5544 +                                       zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d (pool %s) said into %s: \"%s\"%s", (int) child->pid,
5545 +                                               child->wp->config->name, is_stdout ? "stdout" : "stderr", buf, is_last_message ? ", pipe is closed" : "");
5546 +
5547 +                                       if (nl) {
5548 +                                               int out_buf = 1 + nl - buf;
5549 +                                               memmove(buf, buf + out_buf, in_buf - out_buf);
5550 +                                               in_buf -= out_buf;
5551 +                                       }
5552 +                                       else {
5553 +                                               in_buf = 0;
5554 +                                       }
5555 +                               }
5556 +                       }
5557 +               }
5558 +       }
5559 +
5560 +}
5561 +
5562 +int fpm_stdio_prepare_pipes(struct fpm_child_s *child)
5563 +{
5564 +       if (0 == child->wp->config->catch_workers_output) { /* not required */
5565 +               return 0;
5566 +       }
5567 +
5568 +       if (0 > pipe(fd_stdout)) {
5569 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
5570 +               return -1;
5571 +       }
5572 +
5573 +       if (0 > pipe(fd_stderr)) {
5574 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
5575 +               close(fd_stdout[0]); close(fd_stdout[1]);
5576 +               return -1;
5577 +       }
5578 +
5579 +       if (0 > fd_set_blocked(fd_stdout[0], 0) || 0 > fd_set_blocked(fd_stderr[0], 0)) {
5580 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fd_set_blocked() failed");
5581 +               close(fd_stdout[0]); close(fd_stdout[1]);
5582 +               close(fd_stderr[0]); close(fd_stderr[1]);
5583 +               return -1;
5584 +       }
5585 +
5586 +       return 0;
5587 +}
5588 +
5589 +int fpm_stdio_parent_use_pipes(struct fpm_child_s *child)
5590 +{
5591 +       if (0 == child->wp->config->catch_workers_output) { /* not required */
5592 +               return 0;
5593 +       }
5594 +
5595 +       close(fd_stdout[1]);
5596 +       close(fd_stderr[1]);
5597 +
5598 +       child->fd_stdout = fd_stdout[0];
5599 +       child->fd_stderr = fd_stderr[0];
5600 +
5601 +       fpm_event_add(child->fd_stdout, &child->ev_stdout, fpm_stdio_child_said, child);
5602 +       fpm_event_add(child->fd_stderr, &child->ev_stderr, fpm_stdio_child_said, child);
5603 +
5604 +       return 0;
5605 +}
5606 +
5607 +int fpm_stdio_discard_pipes(struct fpm_child_s *child)
5608 +{
5609 +       if (0 == child->wp->config->catch_workers_output) { /* not required */
5610 +               return 0;
5611 +       }
5612 +
5613 +       close(fd_stdout[1]);
5614 +       close(fd_stderr[1]);
5615 +
5616 +       close(fd_stdout[0]);
5617 +       close(fd_stderr[0]);
5618 +
5619 +       return 0;
5620 +}
5621 +
5622 +void fpm_stdio_child_use_pipes(struct fpm_child_s *child)
5623 +{
5624 +       if (child->wp->config->catch_workers_output) {
5625 +               dup2(fd_stdout[1], STDOUT_FILENO);
5626 +               dup2(fd_stderr[1], STDERR_FILENO);
5627 +               close(fd_stdout[0]); close(fd_stdout[1]);
5628 +               close(fd_stderr[0]); close(fd_stderr[1]);
5629 +       }
5630 +       else {
5631 +               /* stdout of parent is always /dev/null */
5632 +               dup2(STDOUT_FILENO, STDERR_FILENO);
5633 +       }
5634 +}      
5635 +
5636 +int fpm_stdio_open_error_log(int reopen)
5637 +{
5638 +       int fd;
5639 +
5640 +       fd = open(fpm_global_config.error_log, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
5641 +
5642 +       if (0 > fd) {
5643 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"%s\") failed", fpm_global_config.error_log);
5644 +               return -1;
5645 +       }
5646 +
5647 +       if (reopen) {
5648 +               if (fpm_global_config.daemonize) {
5649 +                       dup2(fd, STDERR_FILENO);
5650 +               }
5651 +
5652 +               dup2(fd, fpm_globals.error_log_fd);
5653 +               close(fd);
5654 +               fd = fpm_globals.error_log_fd; /* for FD_CLOSEXEC to work */
5655 +       }
5656 +       else {
5657 +               fpm_globals.error_log_fd = fd;
5658 +       }
5659 +
5660 +       fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
5661 +
5662 +       return 0;
5663 +}
5664 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_stdio.h php-5.2.17/sapi/cgi/fpm/fpm_stdio.h
5665 --- php-5.2.17.org/sapi/cgi/fpm/fpm_stdio.h     1970-01-01 01:00:00.000000000 +0100
5666 +++ php-5.2.17/sapi/cgi/fpm/fpm_stdio.h 2021-10-23 19:09:19.833125073 +0200
5667 @@ -0,0 +1,20 @@
5668 +
5669 +       /* $Id$ */
5670 +       /* (c) 2007,2008 Andrei Nigmatulin */
5671 +
5672 +#ifndef FPM_STDIO_H
5673 +#define FPM_STDIO_H 1
5674 +
5675 +#include "fpm_worker_pool.h"
5676 +
5677 +int fpm_stdio_init_main();
5678 +int fpm_stdio_init_final();
5679 +int fpm_stdio_init_child(struct fpm_worker_pool_s *wp);
5680 +int fpm_stdio_prepare_pipes(struct fpm_child_s *child);
5681 +void fpm_stdio_child_use_pipes(struct fpm_child_s *child);
5682 +int fpm_stdio_parent_use_pipes(struct fpm_child_s *child);
5683 +int fpm_stdio_discard_pipes(struct fpm_child_s *child);
5684 +int fpm_stdio_open_error_log(int reopen);
5685 +
5686 +#endif
5687 +
5688 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_str.h php-5.2.17/sapi/cgi/fpm/fpm_str.h
5689 --- php-5.2.17.org/sapi/cgi/fpm/fpm_str.h       1970-01-01 01:00:00.000000000 +0100
5690 +++ php-5.2.17/sapi/cgi/fpm/fpm_str.h   2021-10-23 19:09:19.833125073 +0200
5691 @@ -0,0 +1,49 @@
5692 +
5693 +       /* $Id$ */
5694 +       /* (c) 2007,2008 Andrei Nigmatulin */
5695 +
5696 +#ifndef FPM_STR_H
5697 +#define FPM_STR_H 1
5698 +
5699 +static inline char *cpystrn(char *dst, const char *src, size_t dst_size)
5700 +{
5701 +       char *d, *end;
5702 +       
5703 +       if (!dst_size) return dst;
5704 +       
5705 +       d = dst;
5706 +       end = dst + dst_size - 1;
5707 +       
5708 +       for (; d < end; ++d, ++src) {
5709 +               if (!(*d = *src)) {
5710 +                       return d;
5711 +               }
5712 +       }
5713 +
5714 +       *d = '\0';
5715 +
5716 +       return d;
5717 +}
5718 +
5719 +static inline char *str_purify_filename(char *dst, char *src, size_t size)
5720 +{
5721 +       char *d, *end;
5722 +
5723 +       d = dst;
5724 +       end = dst + size - 1;
5725 +
5726 +       for (; d < end && *src; ++d, ++src) {
5727 +               if (* (unsigned char *) src < ' ' || * (unsigned char *) src > '\x7f') {
5728 +                       *d = '.';
5729 +               }
5730 +               else {
5731 +                       *d = *src;
5732 +               }
5733 +       }
5734 +
5735 +       *d = '\0';
5736 +
5737 +       return d;
5738 +}
5739 +
5740 +#endif
5741 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_trace.c php-5.2.17/sapi/cgi/fpm/fpm_trace.c
5742 --- php-5.2.17.org/sapi/cgi/fpm/fpm_trace.c     1970-01-01 01:00:00.000000000 +0100
5743 +++ php-5.2.17/sapi/cgi/fpm/fpm_trace.c 2021-10-23 19:09:19.833125073 +0200
5744 @@ -0,0 +1,46 @@
5745 +
5746 +       /* $Id$ */
5747 +       /* (c) 2007,2008 Andrei Nigmatulin */
5748 +
5749 +#include "fpm_config.h"
5750 +
5751 +#include <sys/types.h>
5752 +
5753 +#include "fpm_trace.h"
5754 +
5755 +int fpm_trace_get_strz(char *buf, size_t sz, long addr)
5756 +{
5757 +       int i;
5758 +       long l;
5759 +       char *lc = (char *) &l;
5760 +
5761 +       if (0 > fpm_trace_get_long(addr, &l)) {
5762 +               return -1;
5763 +       }
5764 +
5765 +       i = l % SIZEOF_LONG;
5766 +
5767 +       l -= i;
5768 +
5769 +       for (addr = l; ; addr += SIZEOF_LONG) {
5770 +
5771 +               if (0 > fpm_trace_get_long(addr, &l)) {
5772 +                       return -1;
5773 +               }
5774 +
5775 +               for ( ; i < SIZEOF_LONG; i++) {
5776 +                       --sz;
5777 +
5778 +                       if (sz && lc[i]) {
5779 +                               *buf++ = lc[i];
5780 +                               continue;
5781 +                       }
5782 +
5783 +                       *buf = '\0';
5784 +                       return 0;
5785 +               }
5786 +
5787 +               i = 0;
5788 +       }
5789 +}
5790 +
5791 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_trace.h php-5.2.17/sapi/cgi/fpm/fpm_trace.h
5792 --- php-5.2.17.org/sapi/cgi/fpm/fpm_trace.h     1970-01-01 01:00:00.000000000 +0100
5793 +++ php-5.2.17/sapi/cgi/fpm/fpm_trace.h 2021-10-23 19:09:19.833125073 +0200
5794 @@ -0,0 +1,17 @@
5795 +
5796 +       /* $Id$ */
5797 +       /* (c) 2007,2008 Andrei Nigmatulin */
5798 +
5799 +#ifndef FPM_TRACE_H
5800 +#define FPM_TRACE_H 1
5801 +
5802 +#include <unistd.h>
5803 +
5804 +int fpm_trace_signal(pid_t pid);
5805 +int fpm_trace_ready(pid_t pid);
5806 +int fpm_trace_close(pid_t pid);
5807 +int fpm_trace_get_long(long addr, long *data);
5808 +int fpm_trace_get_strz(char *buf, size_t sz, long addr);
5809 +
5810 +#endif
5811 +
5812 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_trace_mach.c php-5.2.17/sapi/cgi/fpm/fpm_trace_mach.c
5813 --- php-5.2.17.org/sapi/cgi/fpm/fpm_trace_mach.c        1970-01-01 01:00:00.000000000 +0100
5814 +++ php-5.2.17/sapi/cgi/fpm/fpm_trace_mach.c    2021-10-23 19:09:19.833125073 +0200
5815 @@ -0,0 +1,102 @@
5816 +
5817 +       /* $Id$ */
5818 +       /* (c) 2007,2008 Andrei Nigmatulin */
5819 +
5820 +#include "fpm_config.h"
5821 +
5822 +#include <mach/mach.h>
5823 +#include <mach/mach_vm.h>
5824 +
5825 +#include <unistd.h>
5826 +
5827 +#include "fpm_trace.h"
5828 +#include "fpm_process_ctl.h"
5829 +#include "fpm_unix.h"
5830 +#include "zlog.h"
5831 +
5832 +
5833 +static mach_port_name_t target;
5834 +static vm_offset_t target_page_base;
5835 +static vm_offset_t local_page;
5836 +static mach_msg_type_number_t local_size;
5837 +
5838 +static void fpm_mach_vm_deallocate()
5839 +{
5840 +       if (local_page) {
5841 +               mach_vm_deallocate(mach_task_self(), local_page, local_size);
5842 +               target_page_base = 0;
5843 +               local_page = 0;
5844 +               local_size = 0;
5845 +       }
5846 +}
5847 +
5848 +static int fpm_mach_vm_read_page(vm_offset_t page)
5849 +{
5850 +       kern_return_t kr;
5851 +
5852 +       kr = mach_vm_read(target, page, fpm_pagesize, &local_page, &local_size);
5853 +
5854 +       if (kr != KERN_SUCCESS) {
5855 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "mach_vm_read() failed: %s (%d)", mach_error_string(kr), kr);
5856 +               return -1;
5857 +       }
5858 +
5859 +       return 0;
5860 +}
5861 +
5862 +int fpm_trace_signal(pid_t pid)
5863 +{
5864 +       if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
5865 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "kill(SIGSTOP) failed");
5866 +               return -1;
5867 +       }
5868 +
5869 +       return 0;
5870 +}
5871 +
5872 +int fpm_trace_ready(pid_t pid)
5873 +{
5874 +       kern_return_t kr;
5875 +
5876 +       kr = task_for_pid(mach_task_self(), pid, &target);
5877 +
5878 +       if (kr != KERN_SUCCESS) {
5879 +               char *msg = "";
5880 +
5881 +               if (kr == KERN_FAILURE) {
5882 +                       msg = " It seems that master process does not have enough privileges to trace processes.";
5883 +               }
5884 +
5885 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "task_for_pid() failed: %s (%d)%s", mach_error_string(kr), kr, msg);
5886 +               return -1;
5887 +       }
5888 +
5889 +       return 0;
5890 +}
5891 +
5892 +int fpm_trace_close(pid_t pid)
5893 +{
5894 +       fpm_mach_vm_deallocate();
5895 +
5896 +       target = 0;
5897 +
5898 +       return 0;
5899 +}
5900 +
5901 +int fpm_trace_get_long(long addr, long *data)
5902 +{
5903 +       size_t offset = ((uintptr_t) (addr) % fpm_pagesize);
5904 +       vm_offset_t base = (uintptr_t) (addr) - offset;
5905 +
5906 +       if (base != target_page_base) {
5907 +               fpm_mach_vm_deallocate();
5908 +               if (0 > fpm_mach_vm_read_page(base)) {
5909 +                       return -1;
5910 +               }
5911 +       }
5912 +
5913 +       *data = * (long *) (local_page + offset);
5914 +
5915 +       return 0;
5916 +}
5917 +
5918 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_trace_pread.c php-5.2.17/sapi/cgi/fpm/fpm_trace_pread.c
5919 --- php-5.2.17.org/sapi/cgi/fpm/fpm_trace_pread.c       1970-01-01 01:00:00.000000000 +0100
5920 +++ php-5.2.17/sapi/cgi/fpm/fpm_trace_pread.c   2021-10-23 19:09:19.833125073 +0200
5921 @@ -0,0 +1,67 @@
5922 +
5923 +       /* $Id$ */
5924 +       /* (c) 2007,2008 Andrei Nigmatulin */
5925 +
5926 +#define _GNU_SOURCE
5927 +#define _FILE_OFFSET_BITS 64
5928 +
5929 +#include "fpm_config.h"
5930 +
5931 +#include <unistd.h>
5932 +
5933 +#include <fcntl.h>
5934 +#include <stdio.h>
5935 +#include <stdint.h>
5936 +
5937 +#include "fpm_trace.h"
5938 +#include "fpm_process_ctl.h"
5939 +#include "zlog.h"
5940 +
5941 +
5942 +static int mem_file = -1;
5943 +
5944 +int fpm_trace_signal(pid_t pid)
5945 +{
5946 +       if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
5947 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "kill(SIGSTOP) failed");
5948 +               return -1;
5949 +       }
5950 +
5951 +       return 0;
5952 +}
5953 +
5954 +int fpm_trace_ready(pid_t pid)
5955 +{
5956 +       char buf[128];
5957 +
5958 +       sprintf(buf, "/proc/%d/" PROC_MEM_FILE, (int) pid);
5959 +
5960 +       mem_file = open(buf, O_RDONLY);
5961 +
5962 +       if (0 > mem_file) {
5963 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(%s) failed", buf);
5964 +               return -1;
5965 +       }
5966 +
5967 +       return 0;
5968 +}
5969 +
5970 +int fpm_trace_close(pid_t pid)
5971 +{
5972 +       close(mem_file);
5973 +
5974 +       mem_file = -1;
5975 +
5976 +       return 0;
5977 +}
5978 +
5979 +int fpm_trace_get_long(long addr, long *data)
5980 +{
5981 +       if (sizeof(*data) != pread(mem_file, (void *) data, sizeof(*data), (uintptr_t) addr)) {
5982 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pread() failed");
5983 +               return -1;
5984 +       }
5985 +
5986 +       return 0;
5987 +}
5988 +
5989 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_trace_ptrace.c php-5.2.17/sapi/cgi/fpm/fpm_trace_ptrace.c
5990 --- php-5.2.17.org/sapi/cgi/fpm/fpm_trace_ptrace.c      1970-01-01 01:00:00.000000000 +0100
5991 +++ php-5.2.17/sapi/cgi/fpm/fpm_trace_ptrace.c  2021-10-23 19:09:19.833125073 +0200
5992 @@ -0,0 +1,85 @@
5993 +
5994 +       /* $Id$ */
5995 +       /* (c) 2007,2008 Andrei Nigmatulin */
5996 +
5997 +#include "fpm_config.h"
5998 +
5999 +#include <sys/wait.h>
6000 +#include <sys/ptrace.h>
6001 +#include <unistd.h>
6002 +#include <errno.h>
6003 +
6004 +#if defined(PT_ATTACH) && !defined(PTRACE_ATTACH)
6005 +#define PTRACE_ATTACH PT_ATTACH
6006 +#endif
6007 +
6008 +#if defined(PT_DETACH) && !defined(PTRACE_DETACH)
6009 +#define PTRACE_DETACH PT_DETACH
6010 +#endif
6011 +
6012 +#if defined(PT_READ_D) && !defined(PTRACE_PEEKDATA)
6013 +#define PTRACE_PEEKDATA PT_READ_D
6014 +#endif
6015 +
6016 +#include "fpm_trace.h"
6017 +#include "zlog.h"
6018 +
6019 +static pid_t traced_pid;
6020 +
6021 +int fpm_trace_signal(pid_t pid)
6022 +{
6023 +       if (0 > ptrace(PTRACE_ATTACH, pid, 0, 0)) {
6024 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(ATTACH) failed");
6025 +               return -1;
6026 +       }
6027 +
6028 +       return 0;
6029 +}
6030 +
6031 +int fpm_trace_ready(pid_t pid)
6032 +{
6033 +       traced_pid = pid;
6034 +
6035 +       return 0;
6036 +}
6037 +
6038 +int fpm_trace_close(pid_t pid)
6039 +{
6040 +       if (0 > ptrace(PTRACE_DETACH, pid, (void *) 1, 0)) {
6041 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(DETACH) failed");
6042 +               return -1;
6043 +       }
6044 +
6045 +       traced_pid = 0;
6046 +
6047 +       return 0;
6048 +}
6049 +
6050 +int fpm_trace_get_long(long addr, long *data)
6051 +{
6052 +#ifdef PT_IO
6053 +       struct ptrace_io_desc ptio = {
6054 +               .piod_op = PIOD_READ_D,
6055 +               .piod_offs = (void *) addr,
6056 +               .piod_addr = (void *) data,
6057 +               .piod_len = sizeof(long)
6058 +       };
6059 +
6060 +       if (0 > ptrace(PT_IO, traced_pid, (void *) &ptio, 0)) {
6061 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(PT_IO) failed");
6062 +               return -1;
6063 +       }
6064 +#else
6065 +       errno = 0;
6066 +
6067 +       *data = ptrace(PTRACE_PEEKDATA, traced_pid, (void *) addr, 0);
6068 +
6069 +       if (errno) {
6070 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(PEEKDATA) failed");
6071 +               return -1;
6072 +       }
6073 +#endif
6074 +
6075 +       return 0;
6076 +}
6077 +
6078 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_unix.c php-5.2.17/sapi/cgi/fpm/fpm_unix.c
6079 --- php-5.2.17.org/sapi/cgi/fpm/fpm_unix.c      1970-01-01 01:00:00.000000000 +0100
6080 +++ php-5.2.17/sapi/cgi/fpm/fpm_unix.c  2021-10-23 19:09:19.833125073 +0200
6081 @@ -0,0 +1,289 @@
6082 +
6083 +       /* $Id$ */
6084 +       /* (c) 2007,2008 Andrei Nigmatulin */
6085 +
6086 +#include "fpm_config.h"
6087 +
6088 +#include <string.h>
6089 +#include <sys/time.h>
6090 +#include <sys/resource.h>
6091 +#include <stdlib.h>
6092 +#include <unistd.h>
6093 +#include <sys/types.h>
6094 +#include <pwd.h>
6095 +#include <grp.h>
6096 +
6097 +#ifdef HAVE_PRCTL
6098 +#include <sys/prctl.h>
6099 +#endif
6100 +
6101 +#include "fpm.h"
6102 +#include "fpm_conf.h"
6103 +#include "fpm_cleanup.h"
6104 +#include "fpm_clock.h"
6105 +#include "fpm_stdio.h"
6106 +#include "fpm_unix.h"
6107 +#include "zlog.h"
6108 +
6109 +size_t fpm_pagesize;
6110 +
6111 +int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp)
6112 +{
6113 +       struct fpm_listen_options_s *lo = wp->config->listen_options;
6114 +
6115 +       /* uninitialized */
6116 +       wp->socket_uid = -1;
6117 +       wp->socket_gid = -1;
6118 +       wp->socket_mode = 0666;
6119 +
6120 +       if (!lo) return 0;
6121 +
6122 +       if (lo->owner && *lo->owner) {
6123 +               struct passwd *pwd;
6124 +
6125 +               pwd = getpwnam(lo->owner);
6126 +
6127 +               if (!pwd) {
6128 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "cannot get uid for user '%s', pool '%s'", lo->owner, wp->config->name);
6129 +                       return -1;
6130 +               }
6131 +
6132 +               wp->socket_uid = pwd->pw_uid;
6133 +               wp->socket_gid = pwd->pw_gid;
6134 +       }
6135 +
6136 +       if (lo->group && *lo->group) {
6137 +               struct group *grp;
6138 +
6139 +               grp = getgrnam(lo->group);
6140 +
6141 +               if (!grp) {
6142 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "cannot get gid for group '%s', pool '%s'", lo->group, wp->config->name);
6143 +                       return -1;
6144 +               }
6145 +
6146 +               wp->socket_gid = grp->gr_gid;
6147 +       }
6148 +
6149 +       if (lo->mode && *lo->mode) {
6150 +               wp->socket_mode = strtoul(lo->mode, 0, 8);
6151 +       }
6152 +
6153 +       return 0;
6154 +}
6155 +
6156 +static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp)
6157 +{
6158 +       int is_root = !geteuid();
6159 +
6160 +       if (is_root) {
6161 +               if (wp->config->user && *wp->config->user) {
6162 +
6163 +                       if (strlen(wp->config->user) == strspn(wp->config->user, "0123456789")) {
6164 +                               wp->set_uid = strtoul(wp->config->user, 0, 10);
6165 +                       }
6166 +                       else {
6167 +                               struct passwd *pwd;
6168 +
6169 +                               pwd = getpwnam(wp->config->user);
6170 +
6171 +                               if (!pwd) {
6172 +                                       zlog(ZLOG_STUFF, ZLOG_ERROR, "cannot get uid for user '%s', pool '%s'", wp->config->user, wp->config->name);
6173 +                                       return -1;
6174 +                               }
6175 +
6176 +                               wp->set_uid = pwd->pw_uid;
6177 +                               wp->set_gid = pwd->pw_gid;
6178 +
6179 +                               wp->user = strdup(pwd->pw_name);
6180 +                               wp->home = strdup(pwd->pw_dir);
6181 +                       }
6182 +               }
6183 +
6184 +               if (wp->config->group && *wp->config->group) {
6185 +
6186 +                       if (strlen(wp->config->group) == strspn(wp->config->group, "0123456789")) {
6187 +                               wp->set_gid = strtoul(wp->config->group, 0, 10);
6188 +                       }
6189 +                       else {
6190 +                               struct group *grp;
6191 +
6192 +                               grp = getgrnam(wp->config->group);
6193 +
6194 +                               if (!grp) {
6195 +                                       zlog(ZLOG_STUFF, ZLOG_ERROR, "cannot get gid for group '%s', pool '%s'", wp->config->group, wp->config->name);
6196 +                                       return -1;
6197 +                               }
6198 +
6199 +                               wp->set_gid = grp->gr_gid;
6200 +                       }
6201 +               }
6202 +
6203 +#ifndef I_REALLY_WANT_ROOT_PHP
6204 +               if (wp->set_uid == 0 || wp->set_gid == 0) {
6205 +                       zlog(ZLOG_STUFF, ZLOG_ERROR, "please specify user and group other than root, pool '%s'", wp->config->name);
6206 +                       return -1;
6207 +               }
6208 +#endif
6209 +       }
6210 +       else { /* not root */
6211 +               if (wp->config->user && *wp->config->user) {
6212 +                       zlog(ZLOG_STUFF, ZLOG_WARNING, "'user' directive is ignored, pool '%s'", wp->config->name);
6213 +               }
6214 +               if (wp->config->group && *wp->config->group) {
6215 +                       zlog(ZLOG_STUFF, ZLOG_WARNING, "'group' directive is ignored, pool '%s'", wp->config->name);
6216 +               }
6217 +               if (wp->config->chroot && *wp->config->chroot) {
6218 +                       zlog(ZLOG_STUFF, ZLOG_WARNING, "'chroot' directive is ignored, pool '%s'", wp->config->name);
6219 +               }
6220 +
6221 +               { /* set up HOME and USER anyway */
6222 +                       struct passwd *pwd;
6223 +
6224 +                       pwd = getpwuid(getuid());
6225 +
6226 +                       if (pwd) {
6227 +                               wp->user = strdup(pwd->pw_name);
6228 +                               wp->home = strdup(pwd->pw_dir);
6229 +                       }
6230 +               }
6231 +       }
6232 +
6233 +       return 0;
6234 +}
6235 +
6236 +int fpm_unix_init_child(struct fpm_worker_pool_s *wp)
6237 +{
6238 +       int is_root = !geteuid();
6239 +       int made_chroot = 0;
6240 +
6241 +       if (wp->config->rlimit_files) {
6242 +               struct rlimit r;
6243 +
6244 +               getrlimit(RLIMIT_NOFILE, &r);
6245 +
6246 +               r.rlim_cur = (rlim_t) wp->config->rlimit_files;
6247 +       r.rlim_max = r.rlim_cur;    
6248 +               if (0 > setrlimit(RLIMIT_NOFILE, &r)) {
6249 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setrlimit(RLIMIT_NOFILE) failed");
6250 +               }
6251 +       }
6252 +
6253 +       if (wp->config->rlimit_core) {
6254 +               struct rlimit r;
6255 +
6256 +               getrlimit(RLIMIT_CORE, &r);
6257 +
6258 +               r.rlim_cur = wp->config->rlimit_core == -1 ? (rlim_t) RLIM_INFINITY : (rlim_t) wp->config->rlimit_core;
6259 +       r.rlim_max = r.rlim_cur;
6260 +               if (0 > setrlimit(RLIMIT_CORE, &r)) {
6261 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setrlimit(RLIMIT_CORE) failed");
6262 +               }
6263 +       }
6264 +
6265 +       if (is_root && wp->config->chroot && *wp->config->chroot) {
6266 +               if (0 > chroot(wp->config->chroot)) {
6267 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chroot(%s) failed", wp->config->chroot);
6268 +                       return -1;
6269 +               }
6270 +               made_chroot = 1;
6271 +       }
6272 +
6273 +       if (wp->config->chdir && *wp->config->chdir) {
6274 +               if (0 > chdir(wp->config->chdir)) {
6275 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chdir(%s) failed", wp->config->chdir);
6276 +                       return -1;
6277 +               }
6278 +       }
6279 +       else if (made_chroot) {
6280 +               chdir("/");
6281 +       }
6282 +
6283 +       if (is_root) {
6284 +               if (wp->set_gid) {
6285 +                       if (0 > setgid(wp->set_gid)) {
6286 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setgid(%d) failed", wp->set_gid);
6287 +                               return -1;
6288 +                       }
6289 +               }
6290 +               if (wp->set_uid) {
6291 +                       if (0 > initgroups(wp->config->user, wp->set_gid)) {
6292 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "initgroups(%s, %d) failed", wp->config->user, wp->set_gid);
6293 +                               return -1;
6294 +                       }
6295 +                       if (0 > setuid(wp->set_uid)) {
6296 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setuid(%d) failed", wp->set_uid);
6297 +                               return -1;
6298 +                       }
6299 +               }
6300 +       }
6301 +
6302 +#ifdef HAVE_PRCTL
6303 +       if (0 > prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)) {
6304 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "prctl(PR_SET_DUMPABLE) failed");
6305 +       }
6306 +#endif
6307 +
6308 +       if (0 > fpm_clock_init()) {
6309 +               return -1;
6310 +       }
6311 +
6312 +       return 0;
6313 +}
6314 +
6315 +int fpm_unix_init_main()
6316 +{
6317 +       struct fpm_worker_pool_s *wp;
6318 +
6319 +       fpm_pagesize = getpagesize();
6320 +
6321 +       if (fpm_global_config.daemonize) {
6322 +
6323 +               switch (fork()) {
6324 +
6325 +                       case -1 :
6326 +
6327 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fork() failed");
6328 +                               return -1;
6329 +
6330 +                       case 0 :
6331 +
6332 +                               break;
6333 +
6334 +                       default :
6335 +
6336 +                               fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT);
6337 +                               exit(0);
6338 +
6339 +               }
6340 +
6341 +       }
6342 +
6343 +       setsid();
6344 +
6345 +       if (0 > fpm_clock_init()) {
6346 +               return -1;
6347 +       }
6348 +
6349 +       fpm_globals.parent_pid = getpid();
6350 +
6351 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
6352 +
6353 +               if (0 > fpm_unix_conf_wp(wp)) {
6354 +                       return -1;
6355 +               }
6356 +
6357 +       }
6358 +
6359 +       fpm_stdio_init_final();
6360 +
6361 +       {
6362 +               struct rlimit r;
6363 +               getrlimit(RLIMIT_NOFILE, &r);
6364 +
6365 +               zlog(ZLOG_STUFF, ZLOG_NOTICE, "getrlimit(nofile): max:%lld, cur:%lld",
6366 +                       (long long) r.rlim_max, (long long) r.rlim_cur);
6367 +       }
6368 +
6369 +       return 0;
6370 +}
6371 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_unix.h php-5.2.17/sapi/cgi/fpm/fpm_unix.h
6372 --- php-5.2.17.org/sapi/cgi/fpm/fpm_unix.h      1970-01-01 01:00:00.000000000 +0100
6373 +++ php-5.2.17/sapi/cgi/fpm/fpm_unix.h  2021-10-23 19:09:19.833125073 +0200
6374 @@ -0,0 +1,17 @@
6375 +
6376 +       /* $Id$ */
6377 +       /* (c) 2007,2008 Andrei Nigmatulin */
6378 +
6379 +#ifndef FPM_UNIX_H
6380 +#define FPM_UNIX_H 1
6381 +
6382 +#include "fpm_worker_pool.h"
6383 +
6384 +int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp);
6385 +int fpm_unix_init_child(struct fpm_worker_pool_s *wp);
6386 +int fpm_unix_init_main();
6387 +
6388 +extern size_t fpm_pagesize;
6389 +
6390 +#endif
6391 +
6392 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_worker_pool.c php-5.2.17/sapi/cgi/fpm/fpm_worker_pool.c
6393 --- php-5.2.17.org/sapi/cgi/fpm/fpm_worker_pool.c       1970-01-01 01:00:00.000000000 +0100
6394 +++ php-5.2.17/sapi/cgi/fpm/fpm_worker_pool.c   2021-10-23 19:09:19.833125073 +0200
6395 @@ -0,0 +1,69 @@
6396 +
6397 +       /* $Id$ */
6398 +       /* (c) 2007,2008 Andrei Nigmatulin */
6399 +
6400 +#include "fpm_config.h"
6401 +
6402 +#include <string.h>
6403 +#include <stdlib.h>
6404 +#include <unistd.h>
6405 +
6406 +#include "fpm_worker_pool.h"
6407 +#include "fpm_cleanup.h"
6408 +#include "fpm_children.h"
6409 +#include "fpm_shm.h"
6410 +#include "fpm_shm_slots.h"
6411 +#include "fpm_conf.h"
6412 +
6413 +struct fpm_worker_pool_s *fpm_worker_all_pools;
6414 +
6415 +static void fpm_worker_pool_cleanup(int which, void *arg)
6416 +{
6417 +       struct fpm_worker_pool_s *wp, *wp_next;
6418 +
6419 +       for (wp = fpm_worker_all_pools; wp; wp = wp_next) {
6420 +               wp_next = wp->next;
6421 +               fpm_worker_pool_config_free(wp->config);
6422 +               fpm_children_free(wp->children);
6423 +               fpm_array_free(&wp->slots_used);
6424 +               fpm_array_free(&wp->slots_free);
6425 +               fpm_shm_free_list(wp->shm_list, which == FPM_CLEANUP_CHILD ? fpm_shm_slots_mem() : 0);
6426 +               free(wp->config);
6427 +               free(wp->user);
6428 +               free(wp->home);
6429 +               free(wp);
6430 +       }
6431 +
6432 +       fpm_worker_all_pools = 0;
6433 +}
6434 +
6435 +struct fpm_worker_pool_s *fpm_worker_pool_alloc()
6436 +{
6437 +       struct fpm_worker_pool_s *ret;
6438 +
6439 +       ret = malloc(sizeof(struct fpm_worker_pool_s));
6440 +
6441 +       if (!ret) {
6442 +               return 0;
6443 +       }
6444 +
6445 +       memset(ret, 0, sizeof(struct fpm_worker_pool_s));
6446 +
6447 +       if (!fpm_worker_all_pools) {
6448 +               fpm_worker_all_pools = ret;
6449 +       }
6450 +
6451 +       fpm_array_init(&ret->slots_used, sizeof(struct fpm_shm_slot_ptr_s), 50);
6452 +       fpm_array_init(&ret->slots_free, sizeof(struct fpm_shm_slot_ptr_s), 50);
6453 +
6454 +       return ret;
6455 +}
6456 +
6457 +int fpm_worker_pool_init_main()
6458 +{
6459 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_worker_pool_cleanup, 0)) {
6460 +               return -1;
6461 +       }
6462 +
6463 +       return 0;
6464 +}
6465 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/fpm_worker_pool.h php-5.2.17/sapi/cgi/fpm/fpm_worker_pool.h
6466 --- php-5.2.17.org/sapi/cgi/fpm/fpm_worker_pool.h       1970-01-01 01:00:00.000000000 +0100
6467 +++ php-5.2.17/sapi/cgi/fpm/fpm_worker_pool.h   2021-10-23 19:09:19.833125073 +0200
6468 @@ -0,0 +1,46 @@
6469 +
6470 +       /* $Id$ */
6471 +       /* (c) 2007,2008 Andrei Nigmatulin */
6472 +
6473 +#ifndef FPM_WORKER_POOL_H
6474 +#define FPM_WORKER_POOL_H 1
6475 +
6476 +#include "fpm_conf.h"
6477 +#include "fpm_arrays.h"
6478 +
6479 +struct fpm_worker_pool_s;
6480 +struct fpm_child_s;
6481 +struct fpm_child_stat_s;
6482 +struct fpm_shm_s;
6483 +
6484 +enum fpm_address_domain {
6485 +       FPM_AF_UNIX = 1,
6486 +       FPM_AF_INET = 2
6487 +};
6488 +
6489 +struct fpm_worker_pool_s {
6490 +       struct fpm_worker_pool_s *next;
6491 +       struct fpm_worker_pool_config_s *config;
6492 +       char *user, *home;                                                                      /* for setting env USER and HOME */
6493 +       enum fpm_address_domain listen_address_domain;
6494 +       int listening_socket;
6495 +       int set_uid, set_gid;                                                           /* config uid and gid */
6496 +       unsigned is_template:1;                                                                 /* just config template, no processes will be created */
6497 +       int socket_uid, socket_gid, socket_mode;
6498 +
6499 +       struct fpm_shm_s *shm_list;
6500 +       struct fpm_array_s slots_used;
6501 +       struct fpm_array_s slots_free;
6502 +
6503 +       /* runtime */
6504 +       struct fpm_child_s *children;
6505 +       int running_children;
6506 +};
6507 +
6508 +struct fpm_worker_pool_s *fpm_worker_pool_alloc();
6509 +int fpm_worker_pool_init_main();
6510 +
6511 +extern struct fpm_worker_pool_s *fpm_worker_all_pools;
6512 +
6513 +#endif
6514 +
6515 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/init.d/php-fpm.in php-5.2.17/sapi/cgi/fpm/init.d/php-fpm.in
6516 --- php-5.2.17.org/sapi/cgi/fpm/init.d/php-fpm.in       1970-01-01 01:00:00.000000000 +0100
6517 +++ php-5.2.17/sapi/cgi/fpm/init.d/php-fpm.in   2021-10-23 19:09:19.833125073 +0200
6518 @@ -0,0 +1,139 @@
6519 +#! /bin/sh
6520 +
6521 +php_fpm_BIN=@prefix@/bin/php-cgi
6522 +php_fpm_CONF=@php_fpm_conf_path@
6523 +php_fpm_PID=@php_fpm_pid_path@
6524 +
6525 +
6526 +php_opts="--fpm-config $php_fpm_CONF"
6527 +
6528 +
6529 +wait_for_pid () {
6530 +       try=0
6531 +
6532 +       while test $try -lt 35 ; do
6533 +
6534 +               case "$1" in
6535 +                       'created')
6536 +                       if [ -f "$2" ] ; then
6537 +                               try=''
6538 +                               break
6539 +                       fi
6540 +                       ;;
6541 +
6542 +                       'removed')
6543 +                       if [ ! -f "$2" ] ; then
6544 +                               try=''
6545 +                               break
6546 +                       fi
6547 +                       ;;
6548 +               esac
6549 +
6550 +               echo -n .
6551 +               try=`expr $try + 1`
6552 +               sleep 1
6553 +
6554 +       done
6555 +
6556 +}
6557 +
6558 +case "$1" in
6559 +       start)
6560 +               echo -n "Starting php_fpm "
6561 +
6562 +               $php_fpm_BIN --fpm $php_opts
6563 +
6564 +               if [ "$?" != 0 ] ; then
6565 +                       echo " failed"
6566 +                       exit 1
6567 +               fi
6568 +
6569 +               wait_for_pid created $php_fpm_PID
6570 +
6571 +               if [ -n "$try" ] ; then
6572 +                       echo " failed"
6573 +                       exit 1
6574 +               else
6575 +                       echo " done"
6576 +               fi
6577 +       ;;
6578 +
6579 +       stop)
6580 +               echo -n "Shutting down php_fpm "
6581 +
6582 +               if [ ! -r $php_fpm_PID ] ; then
6583 +                       echo "warning, no pid file found - php-fpm is not running ?"
6584 +                       exit 1
6585 +               fi
6586 +
6587 +               kill -TERM `cat $php_fpm_PID`
6588 +
6589 +               wait_for_pid removed $php_fpm_PID
6590 +
6591 +               if [ -n "$try" ] ; then
6592 +                       echo " failed"
6593 +                       exit 1
6594 +               else
6595 +                       echo " done"
6596 +               fi
6597 +       ;;
6598 +
6599 +       quit)
6600 +               echo -n "Gracefully shutting down php_fpm "
6601 +
6602 +               if [ ! -r $php_fpm_PID ] ; then
6603 +                       echo "warning, no pid file found - php-fpm is not running ?"
6604 +                       exit 1
6605 +               fi
6606 +
6607 +               kill -QUIT `cat $php_fpm_PID`
6608 +
6609 +               wait_for_pid removed $php_fpm_PID
6610 +
6611 +               if [ -n "$try" ] ; then
6612 +                       echo " failed"
6613 +                       exit 1
6614 +               else
6615 +                       echo " done"
6616 +               fi
6617 +       ;;
6618 +
6619 +       restart)
6620 +               $0 stop
6621 +               $0 start
6622 +       ;;
6623 +
6624 +       reload)
6625 +
6626 +               echo -n "Reload service php-fpm "
6627 +
6628 +               if [ ! -r $php_fpm_PID ] ; then
6629 +                       echo "warning, no pid file found - php-fpm is not running ?"
6630 +                       exit 1
6631 +               fi
6632 +
6633 +               kill -USR2 `cat $php_fpm_PID`
6634 +
6635 +               echo " done"
6636 +       ;;
6637 +
6638 +       logrotate)
6639 +
6640 +               echo -n "Re-opening php-fpm log file "
6641 +
6642 +               if [ ! -r $php_fpm_PID ] ; then
6643 +                       echo "warning, no pid file found - php-fpm is not running ?"
6644 +                       exit 1
6645 +               fi
6646 +
6647 +               kill -USR1 `cat $php_fpm_PID`
6648 +
6649 +               echo " done"
6650 +       ;;
6651 +
6652 +       *)
6653 +               echo "Usage: $0 {start|stop|quit|restart|reload|logrotate}"
6654 +               exit 1
6655 +       ;;
6656 +
6657 +esac
6658 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/xml_config.c php-5.2.17/sapi/cgi/fpm/xml_config.c
6659 --- php-5.2.17.org/sapi/cgi/fpm/xml_config.c    1970-01-01 01:00:00.000000000 +0100
6660 +++ php-5.2.17/sapi/cgi/fpm/xml_config.c        2021-10-23 19:09:19.833125073 +0200
6661 @@ -0,0 +1,278 @@
6662 +
6663 +       /* $Id$ */
6664 +       /* (c) 2004-2007 Andrei Nigmatulin */
6665 +
6666 +#include "fpm_config.h"
6667 +
6668 +#ifdef HAVE_ALLOCA_H
6669 +#include <alloca.h>
6670 +#endif
6671 +#include <string.h>
6672 +#include <stdio.h>
6673 +#include <stddef.h>
6674 +#include <stdlib.h>
6675 +
6676 +#include <libxml/parser.h>
6677 +#include <libxml/tree.h>
6678 +
6679 +#include "xml_config.h"
6680 +
6681 +static struct xml_conf_section **xml_conf_sections = 0;
6682 +static int xml_conf_sections_allocated = 0;
6683 +static int xml_conf_sections_used = 0;
6684 +
6685 +char *xml_conf_set_slot_boolean(void **conf, char *name, void *vv, intptr_t offset)
6686 +{
6687 +       char *value = vv;
6688 +       long value_y = !strcasecmp(value, "yes") || !strcmp(value,  "1") || !strcasecmp(value, "on");
6689 +       long value_n = !strcasecmp(value, "no")  || !strcmp(value,  "0") || !strcasecmp(value, "off");
6690 +
6691 +       if (!value_y && !value_n) {
6692 +               return "xml_conf_set_slot(): invalid boolean value";
6693 +       }
6694 +
6695 +#ifdef XML_CONF_DEBUG
6696 +       fprintf(stderr, "setting boolean '%s' => %s\n", name, value_y ? "TRUE" : "FALSE");
6697 +#endif
6698 +
6699 +       * (int *) ((char *) *conf + offset) = value_y ? 1 : 0;
6700 +
6701 +       return NULL;
6702 +}
6703 +
6704 +char *xml_conf_set_slot_string(void **conf, char *name, void *vv, intptr_t offset)
6705 +{
6706 +       char *value = vv;
6707 +       char *v = strdup(value);
6708 +
6709 +       if (!v) return "xml_conf_set_slot_string(): strdup() failed";
6710 +
6711 +#ifdef XML_CONF_DEBUG
6712 +       fprintf(stderr, "setting string '%s' => '%s'\n", name, v);
6713 +#endif
6714 +
6715 +       * (char **) ((char *) *conf + offset) = v;
6716 +
6717 +       return NULL;
6718 +}
6719 +
6720 +char *xml_conf_set_slot_integer(void **conf, char *name, void *vv, intptr_t offset)
6721 +{
6722 +       char *value = vv;
6723 +       int v = atoi(value);
6724 +
6725 +       * (int *) ((char *) *conf + offset) = v;
6726 +
6727 +#ifdef XML_CONF_DEBUG
6728 +       fprintf(stderr, "setting integer '%s' => %d\n", name, v);
6729 +#endif
6730 +
6731 +       return NULL;
6732 +}
6733 +
6734 +char *xml_conf_set_slot_time(void **conf, char *name, void *vv, intptr_t offset)
6735 +{
6736 +       char *value = vv;
6737 +       int len = strlen(value);
6738 +       char suffix;
6739 +       int seconds;
6740 +
6741 +       if (!len) return "xml_conf_set_slot_timeval(): invalid timeval value";
6742 +
6743 +       suffix = value[len-1];
6744 +
6745 +       value[len-1] = '\0';
6746 +
6747 +       switch (suffix) {
6748 +               case 's' :
6749 +                       seconds = atoi(value);
6750 +                       break;
6751 +               case 'm' :
6752 +                       seconds = 60 * atoi(value);
6753 +                       break;
6754 +               case 'h' :
6755 +                       seconds = 60 * 60 * atoi(value);
6756 +                       break;
6757 +               case 'd' :
6758 +                       seconds = 24 * 60 * 60 * atoi(value);
6759 +                       break;
6760 +               default :
6761 +                       return "xml_conf_set_slot_timeval(): unknown suffix used in timeval value";
6762 +       }
6763 +
6764 +       * (int *) ((char *) *conf + offset) = seconds;
6765 +
6766 +#ifdef XML_CONF_DEBUG
6767 +       fprintf(stderr, "setting time '%s' => %d:%02d:%02d:%02d\n", name, expand_dhms(seconds));
6768 +#endif
6769 +
6770 +       return NULL;
6771 +}
6772 +
6773 +char *xml_conf_parse_section(void **conf, struct xml_conf_section *section, void *xml_node)
6774 +{
6775 +       xmlNode *element = xml_node;
6776 +       char *ret = 0;
6777 +
6778 +#ifdef XML_CONF_DEBUG
6779 +       fprintf(stderr, "processing a section %s\n", section->path);
6780 +#endif
6781 +
6782 +       for ( ; element; element = element->next) {
6783 +               if (element->type == XML_ELEMENT_NODE && !strcmp((const char *) element->name, "value") && element->children) {
6784 +                       xmlChar *name = xmlGetProp(element, (unsigned char *) "name");
6785 +
6786 +                       if (name) {
6787 +                               int i;
6788 +
6789 +#ifdef XML_CONF_DEBUG
6790 +                               fprintf(stderr, "found a value: %s\n", name);
6791 +#endif
6792 +                               for (i = 0; section->parsers[i].parser; i++) {
6793 +                                       if (!section->parsers[i].name || !strcmp(section->parsers[i].name, (char *) name)) {
6794 +                                               break;
6795 +                                       }
6796 +                               }
6797 +
6798 +                               if (section->parsers[i].parser) {
6799 +                                       if (section->parsers[i].type == XML_CONF_SCALAR) {
6800 +                                               if (element->children->type == XML_TEXT_NODE) {
6801 +                                                       ret = section->parsers[i].parser(conf, (char *) name, element->children->content, section->parsers[i].offset);
6802 +                                               }
6803 +                                               else {
6804 +                                                       ret = "XML_TEXT_NODE is expected, something different is given";
6805 +                                               }
6806 +                                       }
6807 +                                       else {
6808 +                                               ret = section->parsers[i].parser(conf, (char *) name, element->children, section->parsers[i].offset);
6809 +                                       }
6810 +
6811 +                                       xmlFree(name);
6812 +                                       if (ret) return ret;
6813 +                                       else continue;
6814 +                               }
6815 +
6816 +                               fprintf(stderr, "Warning, unknown setting '%s' in section '%s'\n", (char *) name, section->path);
6817 +
6818 +                               xmlFree(name);
6819 +                       }
6820 +               }
6821 +       }
6822 +
6823 +       return NULL;
6824 +}
6825 +
6826 +static char *xml_conf_parse_file(xmlNode *element)
6827 +{
6828 +       char *ret = 0;
6829 +
6830 +       for ( ; element; element = element->next) {
6831 +
6832 +               if (element->parent && element->type == XML_ELEMENT_NODE && !strcmp((const char *) element->name, "section")) {
6833 +                       xmlChar *name = xmlGetProp(element, (unsigned char *) "name");
6834 +
6835 +                       if (name) {
6836 +                               char *parent_name = (char *) xmlGetNodePath(element->parent);
6837 +                               char *full_name;
6838 +                               int i;
6839 +                               struct xml_conf_section *section = NULL;
6840 +
6841 +#ifdef XML_CONF_DEBUG
6842 +                               fprintf(stderr, "got a section: %s/%s\n", parent_name, name);
6843 +#endif
6844 +                               full_name = alloca(strlen(parent_name) + strlen((char *) name) + 1 + 1);
6845 +
6846 +                               sprintf(full_name, "%s/%s", parent_name, (char *) name);
6847 +
6848 +                               xmlFree(parent_name);
6849 +                               xmlFree(name);
6850 +
6851 +                               for (i = 0; i < xml_conf_sections_used; i++) {
6852 +                                       if (!strcmp(xml_conf_sections[i]->path, full_name)) {
6853 +                                               section = xml_conf_sections[i];
6854 +                                       }
6855 +                               }
6856 +
6857 +                               if (section) { /* found a registered section */
6858 +                                       void *conf = section->conf();
6859 +                                       ret = xml_conf_parse_section(&conf, section, element->children);
6860 +                                       if (ret) break;
6861 +                               }
6862 +
6863 +                       }
6864 +               }
6865 +
6866 +               if (element->children) {
6867 +                       ret = xml_conf_parse_file(element->children);
6868 +                       if (ret) break;
6869 +               }
6870 +       }
6871 +
6872 +       return ret;
6873 +}
6874 +
6875 +char *xml_conf_load_file(char *file)
6876 +{
6877 +       char *ret = 0;
6878 +       xmlDoc *doc;
6879 +
6880 +       LIBXML_TEST_VERSION
6881 +
6882 +       doc = xmlParseFile(file);
6883 +
6884 +       if (doc) {
6885 +               ret = xml_conf_parse_file(doc->children);
6886 +               xmlFreeDoc(doc);
6887 +       }
6888 +       else {
6889 +               ret = "failed to parse conf file";
6890 +       }
6891 +
6892 +       xmlCleanupParser();
6893 +       return ret;
6894 +}
6895 +
6896 +int xml_conf_init()
6897 +{
6898 +       return 0;
6899 +}
6900 +
6901 +void xml_conf_clean()
6902 +{
6903 +       if (xml_conf_sections) {
6904 +               free(xml_conf_sections);
6905 +       }
6906 +}
6907 +
6908 +int xml_conf_section_register(struct xml_conf_section *section)
6909 +{
6910 +       if (xml_conf_sections_allocated == xml_conf_sections_used) {
6911 +               int new_size = xml_conf_sections_used + 10;
6912 +               void *new_ptr = realloc(xml_conf_sections, sizeof(struct xml_conf_section *) * new_size);
6913 +
6914 +               if (new_ptr) {
6915 +                       xml_conf_sections = new_ptr;
6916 +                       xml_conf_sections_allocated = new_size;
6917 +               }
6918 +               else {
6919 +                       fprintf(stderr, "xml_conf_section_register(): out of memory\n");
6920 +                       return -1;
6921 +               }
6922 +       }
6923 +
6924 +       xml_conf_sections[xml_conf_sections_used++] = section;
6925 +
6926 +       return 0;
6927 +}
6928 +
6929 +int xml_conf_sections_register(struct xml_conf_section *sections[])
6930 +{
6931 +       for ( ; sections && *sections; sections++) {
6932 +               if (0 > xml_conf_section_register(*sections)) {
6933 +                       return -1;
6934 +               }
6935 +       }
6936 +
6937 +       return 0;
6938 +}
6939 +
6940 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/xml_config.h php-5.2.17/sapi/cgi/fpm/xml_config.h
6941 --- php-5.2.17.org/sapi/cgi/fpm/xml_config.h    1970-01-01 01:00:00.000000000 +0100
6942 +++ php-5.2.17/sapi/cgi/fpm/xml_config.h        2021-10-23 19:09:19.833125073 +0200
6943 @@ -0,0 +1,43 @@
6944 +
6945 +       /* $Id$ */
6946 +       /* (c) 2004-2007 Andrei Nigmatulin */
6947 +
6948 +#ifndef XML_CONFIG_H
6949 +#define XML_CONFIG_H 1
6950 +
6951 +#include <stdint.h>
6952 +
6953 +struct xml_value_parser;
6954 +
6955 +struct xml_value_parser {
6956 +       int type;
6957 +       char *name;
6958 +       char *(*parser)(void **, char *, void *, intptr_t offset);
6959 +       intptr_t offset;
6960 +};
6961 +
6962 +struct xml_conf_section {
6963 +       void *(*conf)();
6964 +       char *path;
6965 +       struct xml_value_parser *parsers;
6966 +};
6967 +
6968 +char *xml_conf_set_slot_boolean(void **conf, char *name, void *value, intptr_t offset);
6969 +char *xml_conf_set_slot_string(void **conf, char *name, void *value, intptr_t offset);
6970 +char *xml_conf_set_slot_integer(void **conf, char *name, void *value, intptr_t offset);
6971 +char *xml_conf_set_slot_time(void **conf, char *name, void *value, intptr_t offset);
6972 +
6973 +int xml_conf_init();
6974 +void xml_conf_clean();
6975 +char *xml_conf_load_file(char *file);
6976 +char *xml_conf_parse_section(void **conf, struct xml_conf_section *section, void *ve);
6977 +int xml_conf_section_register(struct xml_conf_section *section);
6978 +int xml_conf_sections_register(struct xml_conf_section *sections[]);
6979 +
6980 +#define expand_hms(value) (value) / 3600, ((value) % 3600) / 60, (value) % 60
6981 +
6982 +#define expand_dhms(value) (value) / 86400, ((value) % 86400) / 3600, ((value) % 3600) / 60, (value) % 60
6983 +
6984 +enum { XML_CONF_SCALAR = 1, XML_CONF_SUBSECTION = 2 };
6985 +
6986 +#endif
6987 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/zlog.c php-5.2.17/sapi/cgi/fpm/zlog.c
6988 --- php-5.2.17.org/sapi/cgi/fpm/zlog.c  1970-01-01 01:00:00.000000000 +0100
6989 +++ php-5.2.17/sapi/cgi/fpm/zlog.c      2021-10-23 19:09:19.833125073 +0200
6990 @@ -0,0 +1,113 @@
6991 +
6992 +       /* $Id$ */
6993 +       /* (c) 2004-2007 Andrei Nigmatulin */
6994 +
6995 +#include "fpm_config.h"
6996 +
6997 +#include <stdio.h>
6998 +#include <unistd.h>
6999 +#include <time.h>
7000 +#include <string.h>
7001 +#include <stdarg.h>
7002 +#include <sys/time.h>
7003 +#include <errno.h>
7004 +
7005 +#include "zlog.h"
7006 +
7007 +#define MAX_LINE_LENGTH 1024
7008 +
7009 +static int zlog_fd = -1;
7010 +static int zlog_level = ZLOG_NOTICE;
7011 +
7012 +static const char *level_names[] = {
7013 +       [ZLOG_DEBUG]            = "DEBUG",
7014 +       [ZLOG_NOTICE]           = "NOTICE",
7015 +       [ZLOG_WARNING]          = "WARNING",
7016 +       [ZLOG_ERROR]            = "ERROR",
7017 +       [ZLOG_ALERT]            = "ALERT",
7018 +};
7019 +
7020 +size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len)
7021 +{
7022 +       struct tm t;
7023 +       size_t len;
7024 +
7025 +       len = strftime(timebuf, timebuf_len, "%b %d %H:%M:%S", localtime_r((const time_t *) &tv->tv_sec, &t));
7026 +       len += snprintf(timebuf + len, timebuf_len - len, ".%06d", (int) tv->tv_usec);
7027 +
7028 +       return len;
7029 +}
7030 +
7031 +int zlog_set_fd(int new_fd)
7032 +{
7033 +       int old_fd = zlog_fd;
7034 +       zlog_fd = new_fd;
7035 +
7036 +       return old_fd;
7037 +}
7038 +
7039 +int zlog_set_level(int new_value)
7040 +{
7041 +       int old_value = zlog_level;
7042 +
7043 +       zlog_level = new_value;
7044 +
7045 +       return old_value;
7046 +}
7047 +
7048 +void zlog(const char *function, int line, int flags, const char *fmt, ...)
7049 +{
7050 +       struct timeval tv;
7051 +       char buf[MAX_LINE_LENGTH];
7052 +       const size_t buf_size = MAX_LINE_LENGTH;
7053 +       va_list args;
7054 +       size_t len;
7055 +       int truncated = 0;
7056 +       int saved_errno;
7057 +
7058 +       if ((flags & ZLOG_LEVEL_MASK) < zlog_level) {
7059 +               return;
7060 +       }
7061 +
7062 +       saved_errno = errno;
7063 +
7064 +       gettimeofday(&tv, 0);
7065 +
7066 +       len = zlog_print_time(&tv, buf, buf_size);
7067 +
7068 +       len += snprintf(buf + len, buf_size - len, " [%s] %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
7069 +
7070 +       if (len > buf_size - 1) {
7071 +               truncated = 1;
7072 +       }
7073 +
7074 +       if (!truncated) {
7075 +               va_start(args, fmt);
7076 +
7077 +               len += vsnprintf(buf + len, buf_size - len, fmt, args);
7078 +
7079 +               va_end(args);
7080 +
7081 +               if (len >= buf_size) {
7082 +                       truncated = 1;
7083 +               }
7084 +       }
7085 +
7086 +       if (!truncated) {
7087 +               if (flags & ZLOG_HAVE_ERRNO) {
7088 +                       len += snprintf(buf + len, buf_size - len, ": %s (%d)", strerror(saved_errno), saved_errno);
7089 +                       if (len >= buf_size) {
7090 +                               truncated = 1;
7091 +                       }
7092 +               }
7093 +       }
7094 +
7095 +       if (truncated) {
7096 +               memcpy(buf + buf_size - sizeof("..."), "...", sizeof("...") - 1);
7097 +               len = buf_size - 1;
7098 +       }
7099 +
7100 +       buf[len++] = '\n';
7101 +
7102 +       write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, buf, len);
7103 +}
7104 diff -urNp -x '*.orig' php-5.2.17.org/sapi/cgi/fpm/zlog.h php-5.2.17/sapi/cgi/fpm/zlog.h
7105 --- php-5.2.17.org/sapi/cgi/fpm/zlog.h  1970-01-01 01:00:00.000000000 +0100
7106 +++ php-5.2.17/sapi/cgi/fpm/zlog.h      2021-10-23 19:09:19.833125073 +0200
7107 @@ -0,0 +1,34 @@
7108 +
7109 +       /* $Id$ */
7110 +       /* (c) 2004-2007 Andrei Nigmatulin */
7111 +
7112 +#ifndef ZLOG_H
7113 +#define ZLOG_H 1
7114 +
7115 +#define ZLOG_STUFF             __func__, __LINE__
7116 +
7117 +struct timeval;
7118 +
7119 +int zlog_set_fd(int new_fd);
7120 +int zlog_set_level(int new_value);
7121 +
7122 +size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len);
7123 +
7124 +void zlog(const char *function, int line, int flags, const char *fmt, ...)
7125 +               __attribute__ ((format(printf,4,5)));
7126 +
7127 +enum {
7128 +       ZLOG_DEBUG                      = 1,
7129 +       ZLOG_NOTICE                     = 2,
7130 +       ZLOG_WARNING            = 3,
7131 +       ZLOG_ERROR                      = 4,
7132 +       ZLOG_ALERT                      = 5,
7133 +};
7134 +
7135 +#define ZLOG_LEVEL_MASK 7
7136 +
7137 +#define ZLOG_HAVE_ERRNO 0x100
7138 +
7139 +#define ZLOG_SYSERROR (ZLOG_ERROR | ZLOG_HAVE_ERRNO)
7140 +
7141 +#endif
This page took 0.565522 seconds and 3 git commands to generate.