1 diff -Nru php-5.2.6.vanilla/configure php-5.2.6.fpm/configure
2 diff -Nru php-5.2.6.vanilla/configure.in php-5.2.6.fpm/configure.in
3 --- php-5.2.6.vanilla/configure.in 2008-04-30 22:27:55.000000000 +0400
4 +++ php-5.2.6.fpm/configure.in 2008-05-01 20:42:05.000000000 +0400
9 +if test "$PHP_FASTCGI" = "yes" -a "$PHP_FPM" = "yes"; then
10 + PHP_CONFIGURE_PART(Running FastCGI Process Manager checks)
11 + sinclude(sapi/cgi/fpm/acinclude.m4)
12 + sinclude(sapi/cgi/fpm/config.m4)
17 dnl ## In diversion 3 we check for compile-time options to the PHP
27 PHP_SUBST_OLD(EXTRA_LDFLAGS)
28 PHP_SUBST_OLD(EXTRA_LDFLAGS_PROGRAM)
29 PHP_SUBST_OLD(EXTRA_LIBS)
30 +PHP_SUBST_OLD(SAPI_EXTRA_LIBS)
31 +PHP_SUBST_OLD(SAPI_EXTRA_DEPS)
32 PHP_SUBST_OLD(ZEND_EXTRA_LIBS)
33 PHP_SUBST_OLD(INCLUDES)
34 PHP_SUBST_OLD(EXTRA_INCLUDES)
36 install_targets="$PHP_INSTALL_CLI_TARGET $install_targets"
39 - install_targets="install-sapi $PHP_INSTALL_CLI_TARGET $install_targets"
40 + install_targets="install-sapi $install_fpm $PHP_INSTALL_CLI_TARGET $install_targets"
44 diff -Nru php-5.2.6.vanilla/libevent/aclocal.m4 php-5.2.6.fpm/libevent/aclocal.m4
45 diff -Nru php-5.2.6.vanilla/libevent/autogen.sh php-5.2.6.fpm/libevent/autogen.sh
46 diff -Nru php-5.2.6.vanilla/libevent/ChangeLog php-5.2.6.fpm/libevent/ChangeLog
47 diff -Nru php-5.2.6.vanilla/libevent/compat/sys/queue.h php-5.2.6.fpm/libevent/compat/sys/queue.h
48 diff -Nru php-5.2.6.vanilla/libevent/compat/sys/_time.h php-5.2.6.fpm/libevent/compat/sys/_time.h
49 diff -Nru php-5.2.6.vanilla/libevent/config.guess php-5.2.6.fpm/libevent/config.guess
50 diff -Nru php-5.2.6.vanilla/libevent/config.h.in php-5.2.6.fpm/libevent/config.h.in
51 diff -Nru php-5.2.6.vanilla/libevent/config.sub php-5.2.6.fpm/libevent/config.sub
52 diff -Nru php-5.2.6.vanilla/libevent/configure php-5.2.6.fpm/libevent/configure
53 diff -Nru php-5.2.6.vanilla/libevent/configure.in php-5.2.6.fpm/libevent/configure.in
54 diff -Nru php-5.2.6.vanilla/libevent/depcomp php-5.2.6.fpm/libevent/depcomp
55 diff -Nru php-5.2.6.vanilla/libevent/devpoll.c php-5.2.6.fpm/libevent/devpoll.c
56 diff -Nru php-5.2.6.vanilla/libevent/epoll.c php-5.2.6.fpm/libevent/epoll.c
57 diff -Nru php-5.2.6.vanilla/libevent/epoll_sub.c php-5.2.6.fpm/libevent/epoll_sub.c
58 diff -Nru php-5.2.6.vanilla/libevent/event.3 php-5.2.6.fpm/libevent/event.3
59 diff -Nru php-5.2.6.vanilla/libevent/event.c php-5.2.6.fpm/libevent/event.c
60 diff -Nru php-5.2.6.vanilla/libevent/event-config.h php-5.2.6.fpm/libevent/event-config.h
61 diff -Nru php-5.2.6.vanilla/libevent/event-fpm.h php-5.2.6.fpm/libevent/event-fpm.h
62 diff -Nru php-5.2.6.vanilla/libevent/event.h php-5.2.6.fpm/libevent/event.h
63 diff -Nru php-5.2.6.vanilla/libevent/event-internal.h php-5.2.6.fpm/libevent/event-internal.h
64 diff -Nru php-5.2.6.vanilla/libevent/evport.c php-5.2.6.fpm/libevent/evport.c
65 diff -Nru php-5.2.6.vanilla/libevent/evsignal.h php-5.2.6.fpm/libevent/evsignal.h
66 diff -Nru php-5.2.6.vanilla/libevent/evutil.c php-5.2.6.fpm/libevent/evutil.c
67 diff -Nru php-5.2.6.vanilla/libevent/evutil.h php-5.2.6.fpm/libevent/evutil.h
68 diff -Nru php-5.2.6.vanilla/libevent/install-sh php-5.2.6.fpm/libevent/install-sh
69 diff -Nru php-5.2.6.vanilla/libevent/kqueue.c php-5.2.6.fpm/libevent/kqueue.c
70 diff -Nru php-5.2.6.vanilla/libevent/log.c php-5.2.6.fpm/libevent/log.c
71 diff -Nru php-5.2.6.vanilla/libevent/log.h php-5.2.6.fpm/libevent/log.h
72 diff -Nru php-5.2.6.vanilla/libevent/Makefile.am php-5.2.6.fpm/libevent/Makefile.am
73 diff -Nru php-5.2.6.vanilla/libevent/Makefile.in php-5.2.6.fpm/libevent/Makefile.in
74 diff -Nru php-5.2.6.vanilla/libevent/min_heap.h php-5.2.6.fpm/libevent/min_heap.h
75 diff -Nru php-5.2.6.vanilla/libevent/missing php-5.2.6.fpm/libevent/missing
76 diff -Nru php-5.2.6.vanilla/libevent/mkinstalldirs php-5.2.6.fpm/libevent/mkinstalldirs
77 diff -Nru php-5.2.6.vanilla/libevent/poll.c php-5.2.6.fpm/libevent/poll.c
78 diff -Nru php-5.2.6.vanilla/libevent/README php-5.2.6.fpm/libevent/README
79 diff -Nru php-5.2.6.vanilla/libevent/select.c php-5.2.6.fpm/libevent/select.c
80 diff -Nru php-5.2.6.vanilla/libevent/signal.c php-5.2.6.fpm/libevent/signal.c
81 diff -Nru php-5.2.6.vanilla/main/php_config.h.in php-5.2.6.fpm/main/php_config.h.in
82 --- php-5.2.6.vanilla/main/php_config.h.in 2008-04-30 22:37:39.000000000 +0400
83 +++ php-5.2.6.fpm/main/php_config.h.in 2008-09-21 17:37:45.000000000 +0400
85 /* Define if you have the chroot function. */
88 +/* Define if you have the clearenv function. */
91 /* Define if you have the crypt function. */
98 +/* Is experimental fastcgi process manager code activated */
99 +#undef PHP_FASTCGI_PM
102 #undef FORCE_CGI_REDIRECT
106 #undef ENABLE_PATHINFO_CHECK
108 +/* do we have libxml? */
111 +/* do we have prctl? */
114 +/* do we have clock_gettime? */
115 +#undef HAVE_CLOCK_GETTIME
117 +/* do we have clock_get_time? */
118 +#undef HAVE_CLOCK_GET_TIME
120 +/* do we have ptrace? */
123 +/* do we have mach_vm_read? */
124 +#undef HAVE_MACH_VM_READ
126 +/* /proc/pid/mem interface */
127 +#undef PROC_MEM_FILE
129 /* Define if system uses EBCDIC */
130 #undef CHARSET_EBCDIC
132 diff -Nru php-5.2.6.vanilla/sapi/cgi/cgi_main.c php-5.2.6.fpm/sapi/cgi/cgi_main.c
133 --- php-5.2.6.vanilla/sapi/cgi/cgi_main.c 2008-04-09 13:16:40.000000000 +0400
134 +++ php-5.2.6.fpm/sapi/cgi/cgi_main.c 2008-07-22 01:50:58.000000000 +0400
137 #include <sys/wait.h>
143 #include "zend_extensions.h"
150 +#include "fpm/fpm.h"
151 +#include "fpm/fpm_request.h"
155 /* XXX this will need to change later when threaded fastcgi is
156 implemented. shane */
161 +static int request_body_fd;
165 +static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC);
167 #define PHP_MODE_STANDARD 1
168 #define PHP_MODE_HIGHLIGHT 2
169 #define PHP_MODE_INDENT 3
172 {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
176 + {'y', 1, "fpm-config"},
178 {'z', 1, "zend-extension"},
182 zend_bool impersonate;
185 + char *error_header;
186 } php_cgi_globals_struct;
191 if (fcgi_is_fastcgi()) {
192 fcgi_request *request = (fcgi_request*) SG(server_context);
193 - tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
195 + if (request_body_fd == -1) {
196 + char *request_body_filename = sapi_cgibin_getenv((char *) "REQUEST_BODY_FILE",
197 + sizeof("REQUEST_BODY_FILE")-1 TSRMLS_CC);
199 + if (request_body_filename && *request_body_filename) {
200 + request_body_fd = open(request_body_filename, O_RDONLY);
202 + if (0 > request_body_fd) {
203 + php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)",
204 + request_body_filename, strerror(errno), errno);
210 + /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */
211 + if (request_body_fd < 0) {
212 + tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
214 + tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes);
217 tmp_read_bytes = read(0, buffer + read_bytes, count_bytes - read_bytes);
220 " -s Display colour syntax highlighted source.\n"
221 " -v Version number\n"
222 " -w Display source with stripped comments and whitespace.\n"
223 - " -z <file> Load Zend extension <file>.\n"
225 + " -x, --fpm Run in FastCGI process manager mode.\n"
226 + " -y, --fpm-config <file>\n"
227 + " Specify alternative path to FastCGI process manager config file.\n"
229 + " -z <file> Load Zend extension <file>.\n"
231 " -T <count> Measure execution time of script repeated <count> times.\n"
233 @@ -1173,6 +1216,7 @@
235 STD_PHP_INI_ENTRY("fastcgi.impersonate", "0", PHP_INI_SYSTEM, OnUpdateBool, impersonate, php_cgi_globals_struct, php_cgi_globals)
237 + STD_PHP_INI_ENTRY("fastcgi.error_header", NULL, PHP_INI_SYSTEM, OnUpdateString, error_header, php_cgi_globals_struct, php_cgi_globals)
241 @@ -1195,6 +1239,7 @@
243 php_cgi_globals->impersonate = 0;
245 + php_cgi_globals->error_header = NULL;
249 @@ -1227,9 +1272,47 @@
250 static PHP_MINFO_FUNCTION(cgi)
252 DISPLAY_INI_ENTRIES();
256 +#include "fpm/fpm_autoconf.h"
258 + php_info_print_table_start();
259 + php_info_print_table_row(2, "php-fpm", fpm ? "active" : "inactive");
260 + php_info_print_table_row(2, "php-fpm version", PHP_FPM_VERSION);
261 + php_info_print_table_end();
268 +PHP_FUNCTION(fastcgi_finish_request)
270 + fcgi_request *request = (fcgi_request*) SG(server_context);
272 + if (fcgi_is_fastcgi() && request->fd >= 0) {
274 + php_end_ob_buffers(1 TSRMLS_CC);
275 + php_header(TSRMLS_C);
277 + fcgi_flush(request, 1);
278 + fcgi_close(request, 0, 0);
287 +function_entry cgi_fcgi_sapi_functions[] = {
289 + PHP_FE(fastcgi_finish_request, NULL)
294 static zend_module_entry cgi_module_entry = {
295 STANDARD_MODULE_HEADER,
297 @@ -1237,7 +1320,7 @@
302 + cgi_fcgi_sapi_functions,
306 @@ -1277,6 +1360,7 @@
307 char *bindpath = NULL;
309 fcgi_request request;
310 + char *fpm_config = NULL;
313 #if HAVE_GETTIMEOFDAY
314 @@ -1397,6 +1481,14 @@
315 case 's': /* generate highlighted HTML from source */
316 behavior = PHP_MODE_HIGHLIGHT;
320 + fpm_config = php_optarg;
329 @@ -1459,6 +1551,19 @@
330 #endif /* FORCE_CGI_REDIRECT */
335 + if (0 > fpm_init(argc, argv, fpm_config)) {
339 + fcgi_fd = fpm_run(&max_requests);
341 + fcgi_set_is_fastcgi(fastcgi = 1);
347 fcgi_fd = fcgi_listen(bindpath, 128);
349 @@ -1473,7 +1578,7 @@
352 /* How many times to run PHP scripts before dying */
353 - if (getenv("PHP_FCGI_MAX_REQUESTS")) {
354 + if (!fpm && getenv("PHP_FCGI_MAX_REQUESTS")) {
355 max_requests = atoi(getenv("PHP_FCGI_MAX_REQUESTS"));
356 if (max_requests < 0) {
357 fprintf(stderr, "PHP_FCGI_MAX_REQUESTS is not valid\n");
358 @@ -1490,7 +1595,7 @@
361 /* Pre-fork, if required */
362 - if (getenv("PHP_FCGI_CHILDREN")) {
363 + if (!fpm && getenv("PHP_FCGI_CHILDREN")) {
364 children = atoi(getenv("PHP_FCGI_CHILDREN"));
366 fprintf(stderr, "PHP_FCGI_CHILDREN is not valid\n");
367 @@ -1616,6 +1721,8 @@
371 + request_body_fd = -1;
373 SG(server_context) = (void *) &request;
375 SG(server_context) = (void *) 1; /* avoid server_context==NULL checks */
376 @@ -1623,6 +1730,10 @@
377 init_request_info(TSRMLS_C);
381 + if (fpm) fpm_request_info();
387 @@ -1914,6 +2025,10 @@
392 + if (fpm) fpm_request_executing();
396 case PHP_MODE_STANDARD:
397 php_execute_script(&file_handle TSRMLS_CC);
398 @@ -1966,6 +2081,10 @@
401 fastcgi_request_done:
403 + if (request_body_fd != -1) close(request_body_fd);
405 + request_body_fd = -2;
408 char *path_translated;
409 @@ -1979,6 +2098,16 @@
410 SG(request_info).path_translated = path_translated;
413 + if (EG(exit_status) == 255) {
414 + if (CGIG(error_header) && *CGIG(error_header)) {
415 + sapi_header_line ctr = {0};
417 + ctr.line = CGIG(error_header);
418 + ctr.line_len = strlen(CGIG(error_header));
419 + sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
423 php_request_shutdown((void *) 0);
424 if (exit_status == 0) {
425 exit_status = EG(exit_status);
426 @@ -2016,15 +2145,20 @@
430 - if (max_requests != 1) {
431 - /* no need to return exit_status of the last request */
436 /* end of fastcgi loop */
440 + if (fcgi_in_shutdown() || /* graceful shutdown by a signal */
441 + (max_requests && (requests == max_requests)) /* we were told to process max_requests and we are done */
450 if (cgi_sapi_module.php_ini_path_override) {
451 diff -Nru php-5.2.6.vanilla/sapi/cgi/config9.m4 php-5.2.6.fpm/sapi/cgi/config9.m4
452 --- php-5.2.6.vanilla/sapi/cgi/config9.m4 2007-07-12 03:20:36.000000000 +0400
453 +++ php-5.2.6.fpm/sapi/cgi/config9.m4 2008-07-20 20:46:41.000000000 +0400
455 [ --disable-path-info-check CGI: If this is disabled, paths such as
456 /info.php/test?a=b will fail to work], yes, no)
458 +PHP_ARG_ENABLE(fpm,,
459 +[ --enable-fpm FastCGI: If this is enabled, the fastcgi support
460 + will include experimental process manager code], no, no)
466 AC_DEFINE_UNQUOTED(PHP_FASTCGI, $PHP_ENABLE_FASTCGI, [ ])
467 AC_MSG_RESULT($PHP_FASTCGI)
470 + if test "$PHP_FASTCGI" = "yes"; then
471 + AC_MSG_CHECKING(whether to enable FastCGI Process Manager)
472 + if test "$PHP_FPM" = "yes"; then
477 + AC_MSG_RESULT($PHP_FPM)
481 + AC_DEFINE_UNQUOTED(PHP_FASTCGI_PM, $PHP_FASTCGI_PM, [Is experimental fastcgi process manager code activated])
483 dnl --enable-force-cgi-redirect
484 AC_MSG_CHECKING(whether to force Apache CGI redirect)
485 if test "$PHP_FORCE_CGI_REDIRECT" = "yes"; then
486 @@ -108,13 +108,13 @@
490 - 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)"
491 + 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) \$(SAPI_EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
494 - BUILD_CGI="\$(CC) \$(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)"
495 + BUILD_CGI="\$(CC) \$(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)"
498 - BUILD_CGI="\$(LIBTOOL) --mode=link \$(CC) -export-dynamic \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(PHP_RPATHS) libphp_common.la \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
499 + BUILD_CGI="\$(LIBTOOL) --mode=link \$(CC) -export-dynamic \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(PHP_RPATHS) libphp_common.la \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(SAPI_EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
503 diff -Nru php-5.2.6.vanilla/sapi/cgi/fastcgi.c php-5.2.6.fpm/sapi/cgi/fastcgi.c
504 --- php-5.2.6.vanilla/sapi/cgi/fastcgi.c 2008-04-03 14:24:44.000000000 +0400
505 +++ php-5.2.6.fpm/sapi/cgi/fastcgi.c 2008-05-15 23:37:01.000000000 +0400
511 +#include "fpm/fpm.h"
512 +#include "fpm/fpm_request.h"
520 return is_fastcgi = 0;
523 + fcgi_set_allowed_clients(getenv("FCGI_WEB_SERVER_ADDRS"));
531 +void fcgi_set_is_fastcgi(int new_value)
533 + is_fastcgi = new_value;
536 +void fcgi_set_in_shutdown(int new_value)
538 + in_shutdown = new_value;
541 void fcgi_shutdown(void)
545 + if (allowed_clients) {
546 + free(allowed_clients);
547 + allowed_clients = 0;
556 +void fcgi_set_allowed_clients(char *ip)
566 + if (*cur == ',') n++;
569 + if (allowed_clients) free(allowed_clients);
570 + allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
574 + end = strchr(cur, ',');
579 + allowed_clients[n] = inet_addr(cur);
580 + if (allowed_clients[n] == INADDR_NONE) {
581 + fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
586 + allowed_clients[n] = INADDR_NONE;
591 static int is_port_number(const char *bindpath)
599 - char *ip = getenv("FCGI_WEB_SERVER_ADDRS");
608 - if (*cur == ',') n++;
611 - allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
615 - end = strchr(cur, ',');
620 - allowed_clients[n] = inet_addr(cur);
621 - if (allowed_clients[n] == INADDR_NONE) {
622 - fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
627 - allowed_clients[n] = INADDR_NONE;
632 if (!is_initialized) {
637 -static inline void fcgi_close(fcgi_request *req, int force, int destroy)
638 +void fcgi_close(fcgi_request *req, int force, int destroy)
641 zend_hash_destroy(&req->env);
648 + if (fpm) fpm_request_finished();
655 socklen_t len = sizeof(sa);
658 + if (fpm) fpm_request_accepting();
661 FCGI_LOCK(req->listen_socket);
662 req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
663 FCGI_UNLOCK(req->listen_socket);
670 + if (fpm) fpm_request_reading_headers();
673 #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
676 diff -Nru php-5.2.6.vanilla/sapi/cgi/fastcgi.h php-5.2.6.fpm/sapi/cgi/fastcgi.h
677 --- php-5.2.6.vanilla/sapi/cgi/fastcgi.h 2007-12-31 10:20:16.000000000 +0300
678 +++ php-5.2.6.fpm/sapi/cgi/fastcgi.h 2008-05-01 20:42:06.000000000 +0400
681 void fcgi_shutdown(void);
682 int fcgi_is_fastcgi(void);
683 +void fcgi_set_is_fastcgi(int);
684 +void fcgi_set_in_shutdown(int);
685 +void fcgi_set_allowed_clients(char *);
686 int fcgi_in_shutdown(void);
687 int fcgi_listen(const char *path, int backlog);
688 void fcgi_init_request(fcgi_request *req, int listen_socket);
690 int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len);
691 int fcgi_flush(fcgi_request *req, int close);
693 +void fcgi_close(fcgi_request *req, int force, int destroy);
696 void fcgi_impersonate(void);
698 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/acinclude.m4 php-5.2.6.fpm/sapi/cgi/fpm/acinclude.m4
699 --- php-5.2.6.vanilla/sapi/cgi/fpm/acinclude.m4 1970-01-01 03:00:00.000000000 +0300
700 +++ php-5.2.6.fpm/sapi/cgi/fpm/acinclude.m4 2008-07-21 06:39:38.000000000 +0400
703 +AC_DEFUN([AC_FPM_CHECK_FUNC],
705 + SAVED_CFLAGS="$CFLAGS"
706 + CFLAGS="$CFLAGS $2"
710 + AC_CHECK_FUNC([$1],[$4],[$5])
712 + CFLAGS="$SAVED_CFLAGS"
716 +AC_DEFUN([AC_FPM_LIBEVENT],
718 + AC_ARG_WITH([libevent],
719 + [ --with-libevent=DIR FPM: libevent install directory])
722 + LIBEVENT_LIBS="-levent"
723 + LIBEVENT_INCLUDE_PATH=""
725 + if test "$with_libevent" != "no" -a -n "$with_libevent"; then
726 + LIBEVENT_CFLAGS="-I$with_libevent/include"
727 + LIBEVENT_LIBS="-L$with_libevent/lib $LIBEVENT_LIBS"
728 + LIBEVENT_INCLUDE_PATH="$with_libevent/include"
731 + AC_MSG_CHECKING([for event.h])
735 + for dir in "$LIBEVENT_INCLUDE_PATH" /usr/include ; do
736 + if test -r "$dir/event.h" ; then
742 + AC_MSG_RESULT([$found])
744 + AC_FPM_CHECK_FUNC([event_set], [$LIBEVENT_CFLAGS], [$LIBEVENT_LIBS], ,
745 + [AC_MSG_ERROR([Failed to link with libevent. Perhaps --with-libevent=DIR option could help.])])
747 + AC_FPM_CHECK_FUNC([event_base_free], [$LIBEVENT_CFLAGS], [$LIBEVENT_LIBS], ,
748 + [AC_MSG_ERROR([You have too old version. libevent version >= 1.2 is required.])])
752 +AC_DEFUN([AC_FPM_LIBXML],
754 + AC_MSG_RESULT([checking for XML configuration])
756 + AC_ARG_WITH(xml-config,
757 + [ --with-xml-config=PATH FPM: use xml-config in PATH to find libxml],
758 + [XMLCONFIG="$withval"],
759 + [AC_PATH_PROGS(XMLCONFIG, [xml2-config xml-config], "")]
762 + if test "x$XMLCONFIG" = "x"; then
763 + AC_MSG_ERROR([XML configuration could not be found])
765 + AC_MSG_CHECKING([for libxml library])
767 + if test ! -x "$XMLCONFIG"; then
768 + AC_MSG_ERROR([$XMLCONFIG cannot be executed])
771 + LIBXML_LIBS="`$XMLCONFIG --libs`"
772 + LIBXML_CFLAGS="`$XMLCONFIG --cflags`"
773 + LIBXML_VERSION="`$XMLCONFIG --version`"
775 + AC_MSG_RESULT([yes, $LIBXML_VERSION])
777 + AC_FPM_CHECK_FUNC([xmlParseFile], [$LIBXML_CFLAGS], [$LIBXML_LIBS], ,
778 + [AC_MSG_ERROR([Failed to link with libxml])])
780 + AC_DEFINE(HAVE_LIBXML, 1, [do we have libxml?])
784 +AC_DEFUN([AC_FPM_JUDY],
786 + AC_ARG_WITH([Judy],
787 + [ --with-Judy=DIR FPM: Judy install directory])
791 + JUDY_INCLUDE_PATH=""
793 + if test "$with_Judy" != "no" -a -n "$with_Judy"; then
794 + JUDY_INCLUDE_PATH="$with_Judy/include"
795 + JUDY_CFLAGS="-I$with_Judy/include $JUDY_CFLAGS"
796 + JUDY_LIBS="-L$with_Judy/lib $JUDY_LIBS"
799 + AC_MSG_CHECKING([for Judy.h])
803 + for dir in "$JUDY_INCLUDE_PATH" /usr/include ; do
804 + if test -r "$dir/Judy.h" ; then
810 + AC_MSG_RESULT([$found])
812 + AC_FPM_CHECK_FUNC([JudyLCount], [$JUDY_CFLAGS], [$JUDY_LIBS], ,
813 + [AC_MSG_ERROR([Failed to link with Judy])])
817 +AC_DEFUN([AC_FPM_CLOCK],
819 + have_clock_gettime=no
821 + AC_MSG_CHECKING([for clock_gettime])
823 + AC_TRY_COMPILE([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
824 + have_clock_gettime=yes
825 + AC_MSG_RESULT([yes])
827 + AC_MSG_RESULT([no])
830 + if test "$have_clock_gettime" = "no"; then
831 + AC_MSG_CHECKING([for clock_gettime in -lrt])
836 + AC_TRY_COMPILE([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
837 + have_clock_gettime=yes
838 + AC_MSG_RESULT([yes])
841 + AC_MSG_RESULT([no])
845 + if test "$have_clock_gettime" = "yes"; then
846 + AC_DEFINE([HAVE_CLOCK_GETTIME], 1, [do we have clock_gettime?])
849 + have_clock_get_time=no
851 + if test "$have_clock_gettime" = "no"; then
852 + AC_MSG_CHECKING([for clock_get_time])
854 + AC_TRY_RUN([ #include <mach/mach.h>
855 + #include <mach/clock.h>
856 + #include <mach/mach_error.h>
860 + kern_return_t ret; clock_serv_t aClock; mach_timespec_t aTime;
861 + ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &aClock);
863 + if (ret != KERN_SUCCESS) {
867 + ret = clock_get_time(aClock, &aTime);
868 + if (ret != KERN_SUCCESS) {
875 + have_clock_get_time=yes
876 + AC_MSG_RESULT([yes])
878 + AC_MSG_RESULT([no])
882 + if test "$have_clock_get_time" = "yes"; then
883 + AC_DEFINE([HAVE_CLOCK_GET_TIME], 1, [do we have clock_get_time?])
887 +AC_DEFUN([AC_FPM_TRACE],
890 + have_broken_ptrace=no
892 + AC_MSG_CHECKING([for ptrace])
895 + #include <sys/types.h>
896 + #include <sys/ptrace.h> ], [ptrace(0, 0, (void *) 0, 0);], [
898 + AC_MSG_RESULT([yes])
900 + AC_MSG_RESULT([no])
903 + if test "$have_ptrace" = "yes"; then
904 + AC_MSG_CHECKING([whether ptrace works])
907 + #include <unistd.h>
908 + #include <signal.h>
909 + #include <sys/wait.h>
910 + #include <sys/types.h>
911 + #include <sys/ptrace.h>
914 + #if !defined(PTRACE_ATTACH) && defined(PT_ATTACH)
915 + #define PTRACE_ATTACH PT_ATTACH
918 + #if !defined(PTRACE_DETACH) && defined(PT_DETACH)
919 + #define PTRACE_DETACH PT_DETACH
922 + #if !defined(PTRACE_PEEKDATA) && defined(PT_READ_D)
923 + #define PTRACE_PEEKDATA PT_READ_D
928 + long v1 = (unsigned int) -1; /* copy will fail if sizeof(long) == 8 and we've got "int ptrace()" */
933 + if ( (child = fork()) ) { /* parent */
936 + if (0 > ptrace(PTRACE_ATTACH, child, 0, 0)) {
940 + waitpid(child, &status, 0);
943 + struct ptrace_io_desc ptio = {
944 + .piod_op = PIOD_READ_D,
947 + .piod_len = sizeof(v1)
950 + if (0 > ptrace(PT_IO, child, (void *) &ptio, 0)) {
956 + v2 = ptrace(PTRACE_PEEKDATA, child, (void *) &v1, 0);
962 + ptrace(PTRACE_DETACH, child, (void *) 1, 0);
964 + kill(child, SIGKILL);
966 + return ret ? ret : (v1 != v2);
974 + AC_MSG_RESULT([yes])
977 + have_broken_ptrace=yes
978 + AC_MSG_RESULT([no])
982 + if test "$have_ptrace" = "yes"; then
983 + AC_DEFINE([HAVE_PTRACE], 1, [do we have ptrace?])
986 + have_mach_vm_read=no
988 + if test "$have_broken_ptrace" = "yes"; then
989 + AC_MSG_CHECKING([for mach_vm_read])
991 + AC_TRY_COMPILE([ #include <mach/mach.h>
992 + #include <mach/mach_vm.h>
994 + 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);
996 + have_mach_vm_read=yes
997 + AC_MSG_RESULT([yes])
999 + AC_MSG_RESULT([no])
1003 + if test "$have_mach_vm_read" = "yes"; then
1004 + AC_DEFINE([HAVE_MACH_VM_READ], 1, [do we have mach_vm_read?])
1009 + if test -r /proc/$$/mem ; then
1010 + proc_mem_file="mem"
1012 + if test -r /proc/$$/as ; then
1013 + proc_mem_file="as"
1017 + if test -n "$proc_mem_file" ; then
1018 + AC_MSG_CHECKING([for proc mem file])
1021 + #define _GNU_SOURCE
1022 + #define _FILE_OFFSET_BITS 64
1023 + #include <stdint.h>
1024 + #include <unistd.h>
1025 + #include <sys/types.h>
1026 + #include <sys/stat.h>
1027 + #include <fcntl.h>
1028 + #include <stdio.h>
1031 + long v1 = (unsigned int) -1, v2 = 0;
1034 + sprintf(buf, "/proc/%d/$proc_mem_file", getpid());
1035 + fd = open(buf, O_RDONLY);
1039 + if (sizeof(long) != pread(fd, &v2, sizeof(long), (uintptr_t) &v1)) {
1047 + AC_MSG_RESULT([$proc_mem_file])
1050 + AC_MSG_RESULT([no])
1054 + if test -n "$proc_mem_file"; then
1055 + AC_DEFINE_UNQUOTED([PROC_MEM_FILE], "$proc_mem_file", [/proc/pid/mem interface])
1060 + if test "$have_ptrace" = "yes"; then
1061 + FPM_TRACE_SRCS="fpm_trace_ptrace.c"
1062 + elif test -n "$proc_mem_file"; then
1063 + FPM_TRACE_SRCS="fpm_trace_pread.c"
1064 + elif test "$have_mach_vm_read" = "yes" ; then
1065 + FPM_TRACE_SRCS="fpm_trace_mach.c"
1068 + if test -n "$FPM_TRACE_SRCS"; then
1069 + FPM_TRACE_SRCS="fpm_trace.c $FPM_TRACE_SRCS"
1074 +AC_DEFUN([AC_FPM_PRCTL],
1076 + AC_MSG_CHECKING([for prctl])
1078 + AC_TRY_COMPILE([ #include <sys/prctl.h> ], [prctl(0, 0, 0, 0, 0);], [
1079 + AC_DEFINE([HAVE_PRCTL], 1, [do we have prctl?])
1080 + AC_MSG_RESULT([yes])
1082 + AC_MSG_RESULT([no])
1085 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/conf/php-fpm.conf.in php-5.2.6.fpm/sapi/cgi/fpm/conf/php-fpm.conf.in
1086 --- php-5.2.6.vanilla/sapi/cgi/fpm/conf/php-fpm.conf.in 1970-01-01 03:00:00.000000000 +0300
1087 +++ php-5.2.6.fpm/sapi/cgi/fpm/conf/php-fpm.conf.in 2008-05-23 00:53:15.000000000 +0400
1089 +<?xml version="1.0" ?>
1092 + All relative paths in this config are relative to php's install prefix
1094 + <section name="global_options">
1097 + <value name="pid_file">@php_fpm_pid_path@</value>
1100 + <value name="error_log">@php_fpm_log_path@</value>
1103 + <value name="log_level">notice</value>
1105 + When this amount of php processes exited with SIGSEGV or SIGBUS ...
1106 + <value name="emergency_restart_threshold">10</value>
1108 + ... in a less than this interval of time, a graceful restart will be initiated.
1109 + Useful to work around accidental curruptions in accelerator's shared memory.
1110 + <value name="emergency_restart_interval">1m</value>
1112 + Time limit on waiting child's reaction on signals from master
1113 + <value name="process_control_timeout">5s</value>
1115 + Set to 'no' to debug fpm
1116 + <value name="daemonize">yes</value>
1122 + <section name="pool">
1124 + Name of pool. Used in logs and stats.
1125 + <value name="name">default</value>
1127 + Address to accept fastcgi requests on.
1128 + Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
1129 + <value name="listen_address">127.0.0.1:9000</value>
1131 + <value name="listen_options">
1133 + Set listen(2) backlog
1134 + <value name="backlog">-1</value>
1136 + Set permissions for unix socket, if one used.
1137 + In Linux read/write permissions must be set in order to allow connections from web server.
1138 + Many BSD-derrived systems allow connections regardless of permissions.
1139 + <value name="owner"></value>
1140 + <value name="group"></value>
1141 + <value name="mode">0666</value>
1144 + Additional php.ini defines, specific to this pool of workers.
1145 + <value name="php_defines">
1146 + <!-- <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> -->
1147 + <!-- <value name="display_errors">0</value> -->
1150 + Unix user of processes
1151 + <!-- <value name="user">nobody</value> -->
1153 + Unix group of processes
1154 + <!-- <value name="group">@php_fpm_group@</value> -->
1156 + Process manager settings
1159 + Sets style of controling worker process count.
1160 + Valid values are 'static' and 'apache-like'
1161 + <value name="style">static</value>
1163 + Sets the limit on the number of simultaneous requests that will be served.
1164 + Equivalent to Apache MaxClients directive.
1165 + Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
1166 + Used with any pm_style.
1167 + <value name="max_children">5</value>
1169 + Settings group for 'apache-like' pm style
1170 + <value name="apache_like">
1172 + Sets the number of server processes created on startup.
1173 + Used only when 'apache-like' pm_style is selected
1174 + <value name="StartServers">20</value>
1176 + Sets the desired minimum number of idle server processes.
1177 + Used only when 'apache-like' pm_style is selected
1178 + <value name="MinSpareServers">5</value>
1180 + Sets the desired maximum number of idle server processes.
1181 + Used only when 'apache-like' pm_style is selected
1182 + <value name="MaxSpareServers">35</value>
1188 + The timeout (in seconds) for serving a single request after which the worker process will be terminated
1189 + Should be used when 'max_execution_time' ini option does not stop script execution for some reason
1191 + <value name="request_terminate_timeout">0s</value>
1193 + The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
1195 + <value name="request_slowlog_timeout">0s</value>
1197 + The log file for slow requests
1198 + <value name="slowlog">logs/slow.log</value>
1200 + Set open file desc rlimit
1201 + <value name="rlimit_files">1024</value>
1203 + Set max core size rlimit
1204 + <value name="rlimit_core">0</value>
1206 + Chroot to this directory at the start, absolute path
1207 + <value name="chroot"></value>
1209 + Chdir to this directory at the start, absolute path
1210 + <value name="chdir"></value>
1212 + Redirect workers' stdout and stderr into main error log.
1213 + If not set, they will be redirected to /dev/null, according to FastCGI specs
1214 + <value name="catch_workers_output">yes</value>
1216 + How much requests each process should execute before respawn.
1217 + Useful to work around memory leaks in 3rd party libraries.
1218 + For endless request processing please specify 0
1219 + Equivalent to PHP_FCGI_MAX_REQUESTS
1220 + <value name="max_requests">500</value>
1222 + Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
1223 + Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
1224 + Makes sense only with AF_INET listening socket.
1225 + <value name="allowed_clients">127.0.0.1</value>
1227 + Pass environment variables like LD_LIBRARY_PATH
1228 + All $VARIABLEs are taken from current environment
1229 + <value name="environment">
1230 + <value name="HOSTNAME">$HOSTNAME</value>
1231 + <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
1232 + <value name="TMP">/tmp</value>
1233 + <value name="TMPDIR">/tmp</value>
1234 + <value name="TEMP">/tmp</value>
1235 + <value name="OSTYPE">$OSTYPE</value>
1236 + <value name="MACHTYPE">$MACHTYPE</value>
1237 + <value name="MALLOC_CHECK_">2</value>
1245 --- php-5.2.6.fpm/sapi/cgi/fpm/config.m4 2008-09-19 05:22:37.000000000 +0400
1246 +++ php-5.2.6.fpm/sapi/cgi/fpm/config.m4 2008-09-19 05:22:37.000000000 +0400
1249 +FPM_VERSION="0.5.9"
1251 +PHP_ARG_WITH(fpm-conf, for php-fpm config file path,
1252 +[ --with-fpm-conf=PATH Set the path for php-fpm configuration file [PREFIX/etc/php-fpm.conf]], \$prefix/etc/php-fpm.conf, no)
1254 +PHP_ARG_WITH(fpm-log, for php-fpm log file path,
1255 +[ --with-fpm-log=PATH Set the path for php-fpm log file [PREFIX/logs/php-fpm.log]], \$prefix/logs/php-fpm.log, no)
1257 +PHP_ARG_WITH(fpm-pid, for php-fpm pid file path,
1258 +[ --with-fpm-pid=PATH Set the path for php-fpm pid file [PREFIX/logs/php-fpm.pid]], \$prefix/logs/php-fpm.pid, no)
1260 +dnl AC_FPM_LIBEVENT
1268 +LIBEVENT_LIBS="-levent"
1270 +SAPI_EXTRA_DEPS="$LIBEVENT_LIBS"
1272 +FPM_SOURCES="fpm.c \
1276 + fpm_worker_pool.c \
1286 + fpm_process_ctl.c \
1294 +FPM_CFLAGS="$LIBEVENT_CFLAGS $LIBXML_CFLAGS $JUDY_CFLAGS"
1296 +dnl FPM_CFLAGS="$FPM_CFLAGS -DJUDYERROR_NOTEST" # for Judy
1297 +FPM_CFLAGS="$FPM_CFLAGS -I$abs_srcdir/sapi/cgi" # for fastcgi.h
1299 +if test "$ICC" = "yes" ; then
1300 + FPM_ADD_CFLAGS="-Wall -wd279,310,869,810,981"
1301 +elif test "$GCC" = "yes" ; then
1302 + FPM_ADD_CFLAGS="-Wall -Wpointer-arith -Wno-unused-parameter -Wunused-variable -Wunused-value -fno-strict-aliasing"
1305 +if test -n "$FPM_WERROR" ; then
1306 + FPM_ADD_CFLAGS="$FPM_ADD_CFLAGS -Werror"
1309 +FPM_CFLAGS="$FPM_ADD_CFLAGS $FPM_CFLAGS"
1311 +PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/sapi/cgi/fpm/Makefile.frag)
1313 +PHP_ADD_SOURCES(sapi/cgi/fpm, $FPM_SOURCES, $FPM_CFLAGS, sapi)
1315 +PHP_ADD_BUILD_DIR(sapi/cgi/fpm)
1317 +install_fpm="install-fpm"
1319 +SAPI_EXTRA_LIBS="$LIBEVENT_LIBS $LIBXML_LIBS $JUDY_LIBS"
1322 +if test "$prefix" = "NONE" ; then
1323 + fpm_prefix=/usr/local
1325 + fpm_prefix="$prefix"
1328 +if test "$PHP_FPM_CONF" = "\$prefix/etc/php-fpm.conf" ; then
1329 + php_fpm_conf_path="$fpm_prefix/etc/php-fpm.conf"
1331 + php_fpm_conf_path="$PHP_FPM_CONF"
1334 +if test "$PHP_FPM_LOG" = "\$prefix/logs/php-fpm.log" ; then
1335 + php_fpm_log_path="$fpm_prefix/logs/php-fpm.log"
1337 + php_fpm_log_path="$PHP_FPM_LOG"
1340 +if test "$PHP_FPM_PID" = "\$prefix/logs/php-fpm.pid" ; then
1341 + php_fpm_pid_path="$fpm_prefix/logs/php-fpm.pid"
1343 + php_fpm_pid_path="$PHP_FPM_PID"
1347 +if grep nobody /etc/group >/dev/null 2>&1; then
1348 + php_fpm_group=nobody
1350 + if grep nogroup /etc/group >/dev/null 2>&1; then
1351 + php_fpm_group=nogroup
1353 + php_fpm_group=nobody
1357 +PHP_SUBST_OLD(php_fpm_conf_path)
1358 +PHP_SUBST_OLD(php_fpm_log_path)
1359 +PHP_SUBST_OLD(php_fpm_pid_path)
1360 +PHP_SUBST_OLD(php_fpm_group)
1361 +PHP_SUBST_OLD(FPM_VERSION)
1363 +PHP_OUTPUT(sapi/cgi/fpm/fpm_autoconf.h)
1364 +PHP_OUTPUT(sapi/cgi/fpm/php-fpm.conf:sapi/cgi/fpm/conf/php-fpm.conf.in)
1365 +PHP_OUTPUT(sapi/cgi/fpm/php-fpm:sapi/cgi/fpm/init.d/php-fpm.in)
1366 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_arrays.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_arrays.h
1367 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_arrays.h 1970-01-01 03:00:00.000000000 +0300
1368 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_arrays.h 2008-05-24 21:38:47.000000000 +0400
1372 + /* (c) 2007,2008 Andrei Nigmatulin */
1374 +#ifndef FPM_ARRAYS_H
1375 +#define FPM_ARRAYS_H 1
1377 +#include <stdlib.h>
1378 +#include <string.h>
1380 +struct fpm_array_s {
1387 +static inline struct fpm_array_s *fpm_array_init(struct fpm_array_s *a, unsigned int sz, unsigned int initial_num)
1389 + void *allocated = 0;
1392 + a = malloc(sizeof(struct fpm_array_s));
1403 + a->data = calloc(sz, initial_num);
1410 + a->allocated = initial_num;
1416 +static inline void *fpm_array_item(struct fpm_array_s *a, unsigned int n)
1420 + ret = (char *) a->data + a->sz * n;
1425 +static inline void *fpm_array_item_last(struct fpm_array_s *a)
1427 + return fpm_array_item(a, a->used - 1);
1430 +static inline int fpm_array_item_remove(struct fpm_array_s *a, unsigned int n)
1434 + if (n < a->used - 1) {
1435 + void *last = fpm_array_item(a, a->used - 1);
1436 + void *to_remove = fpm_array_item(a, n);
1438 + memcpy(to_remove, last, a->sz);
1448 +static inline void *fpm_array_push(struct fpm_array_s *a)
1452 + if (a->used == a->allocated) {
1453 + size_t new_allocated = a->allocated ? a->allocated * 2 : 20;
1454 + void *new_ptr = realloc(a->data, a->sz * new_allocated);
1460 + a->data = new_ptr;
1461 + a->allocated = new_allocated;
1464 + ret = fpm_array_item(a, a->used);
1471 +static inline void fpm_array_free(struct fpm_array_s *a)
1476 + a->used = a->allocated = 0;
1480 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_atomic.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_atomic.h
1481 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_atomic.h 1970-01-01 03:00:00.000000000 +0300
1482 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_atomic.h 2008-09-19 03:34:11.000000000 +0400
1486 + /* (c) 2007,2008 Andrei Nigmatulin */
1488 +#ifndef FPM_ATOMIC_H
1489 +#define FPM_ATOMIC_H 1
1491 +#include <stdint.h>
1494 +#if ( __i386__ || __i386 )
1496 +typedef int32_t atomic_int_t;
1497 +typedef uint32_t atomic_uint_t;
1498 +typedef volatile atomic_uint_t atomic_t;
1501 +static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
1503 + __asm__ volatile ( "lock;" "xaddl %0, %1;" :
1504 + "+r" (add) : "m" (*value) : "memory");
1509 +static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
1511 + unsigned char res;
1513 + __asm__ volatile ( "lock;" "cmpxchgl %3, %1;" "sete %0;" :
1514 + "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
1519 +#elif ( __amd64__ || __amd64 )
1521 +typedef int64_t atomic_int_t;
1522 +typedef uint64_t atomic_uint_t;
1523 +typedef volatile atomic_uint_t atomic_t;
1525 +static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
1527 + __asm__ volatile ( "lock;" "xaddq %0, %1;" :
1528 + "+r" (add) : "m" (*value) : "memory");
1533 +static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
1535 + unsigned char res;
1537 + __asm__ volatile ( "lock;" "cmpxchgq %3, %1;" "sete %0;" :
1538 + "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
1545 +#error unsupported processor. please write a patch and send it to me
1549 +static inline int fpm_spinlock(atomic_t *lock, int try_once)
1552 + return atomic_cmp_set(lock, 0, 1) ? 0 : -1;
1557 + if (atomic_cmp_set(lock, 0, 1)) {
1569 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_autoconf.h.in php-5.2.6.fpm/sapi/cgi/fpm/fpm_autoconf.h.in
1570 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_autoconf.h.in 1970-01-01 03:00:00.000000000 +0300
1571 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_autoconf.h.in 2008-05-24 21:38:47.000000000 +0400
1575 + /* (c) 2007,2008 Andrei Nigmatulin */
1577 +#define PHP_FPM_VERSION "@FPM_VERSION@"
1578 +#define PHP_FPM_CONF_PATH "@php_fpm_conf_path@"
1579 +#define PHP_FPM_LOG_PATH "@php_fpm_log_path@"
1580 +#define PHP_FPM_PID_PATH "@php_fpm_pid_path@"
1582 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm.c php-5.2.6.fpm/sapi/cgi/fpm/fpm.c
1583 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm.c 1970-01-01 03:00:00.000000000 +0300
1584 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm.c 2008-07-20 20:38:31.000000000 +0400
1588 + /* (c) 2007,2008 Andrei Nigmatulin */
1590 +#include "fpm_config.h"
1592 +#include <stdlib.h> /* for exit */
1595 +#include "fpm_children.h"
1596 +#include "fpm_signals.h"
1597 +#include "fpm_env.h"
1598 +#include "fpm_events.h"
1599 +#include "fpm_cleanup.h"
1600 +#include "fpm_php.h"
1601 +#include "fpm_sockets.h"
1602 +#include "fpm_unix.h"
1603 +#include "fpm_process_ctl.h"
1604 +#include "fpm_conf.h"
1605 +#include "fpm_worker_pool.h"
1606 +#include "fpm_stdio.h"
1611 +struct fpm_globals_s fpm_globals;
1613 +int fpm_init(int argc, char **argv, char *config)
1615 + fpm_globals.argc = argc;
1616 + fpm_globals.argv = argv;
1617 + fpm_globals.config = config;
1619 + if (0 > fpm_php_init_main() ||
1620 + 0 > fpm_stdio_init_main() ||
1621 + 0 > fpm_conf_init_main() ||
1622 + 0 > fpm_unix_init_main() ||
1623 + 0 > fpm_env_init_main() ||
1624 + 0 > fpm_signals_init_main() ||
1625 + 0 > fpm_pctl_init_main() ||
1626 + 0 > fpm_children_init_main() ||
1627 + 0 > fpm_sockets_init_main() ||
1628 + 0 > fpm_worker_pool_init_main() ||
1629 + 0 > fpm_event_init_main()) {
1633 + if (0 > fpm_conf_write_pid()) {
1637 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "fpm is running, pid %d", (int) fpm_globals.parent_pid);
1642 +/* children: return listening socket
1643 + parent: never return */
1644 +int fpm_run(int *max_requests)
1646 + struct fpm_worker_pool_s *wp;
1648 + /* create initial children in all pools */
1649 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1652 + is_parent = fpm_children_create_initial(wp);
1659 + /* run event loop forever */
1662 +run_child: /* only workers reach this point */
1664 + fpm_cleanups_run(FPM_CLEANUP_CHILD);
1666 + *max_requests = fpm_globals.max_requests;
1667 + return fpm_globals.listening_socket;
1670 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_children.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_children.c
1671 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_children.c 1970-01-01 03:00:00.000000000 +0300
1672 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_children.c 2008-08-26 19:09:15.000000000 +0400
1676 + /* (c) 2007,2008 Andrei Nigmatulin */
1678 +#include "fpm_config.h"
1680 +#include <sys/types.h>
1681 +#include <sys/wait.h>
1683 +#include <unistd.h>
1684 +#include <string.h>
1688 +#include "fpm_children.h"
1689 +#include "fpm_signals.h"
1690 +#include "fpm_worker_pool.h"
1691 +#include "fpm_sockets.h"
1692 +#include "fpm_process_ctl.h"
1693 +#include "fpm_php.h"
1694 +#include "fpm_conf.h"
1695 +#include "fpm_cleanup.h"
1696 +#include "fpm_events.h"
1697 +#include "fpm_clock.h"
1698 +#include "fpm_stdio.h"
1699 +#include "fpm_unix.h"
1700 +#include "fpm_env.h"
1701 +#include "fpm_shm_slots.h"
1705 +static time_t *last_faults;
1708 +static int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop);
1710 +static void fpm_children_cleanup(int which, void *arg)
1712 + free(last_faults);
1715 +static struct fpm_child_s *fpm_child_alloc()
1717 + struct fpm_child_s *ret;
1719 + ret = malloc(sizeof(struct fpm_child_s));
1721 + if (!ret) return 0;
1723 + memset(ret, 0, sizeof(*ret));
1728 +static void fpm_child_free(struct fpm_child_s *child)
1733 +static void fpm_child_close(struct fpm_child_s *child, int in_event_loop)
1735 + if (child->fd_stdout != -1) {
1736 + if (in_event_loop) {
1737 + fpm_event_fire(&child->ev_stdout);
1739 + if (child->fd_stdout != -1) {
1740 + close(child->fd_stdout);
1744 + if (child->fd_stderr != -1) {
1745 + if (in_event_loop) {
1746 + fpm_event_fire(&child->ev_stderr);
1748 + if (child->fd_stderr != -1) {
1749 + close(child->fd_stderr);
1753 + fpm_child_free(child);
1756 +static void fpm_child_link(struct fpm_child_s *child)
1758 + struct fpm_worker_pool_s *wp = child->wp;
1760 + ++wp->running_children;
1761 + ++fpm_globals.running_children;
1763 + child->next = wp->children;
1764 + if (child->next) child->next->prev = child;
1766 + wp->children = child;
1769 +static void fpm_child_unlink(struct fpm_child_s *child)
1771 + --child->wp->running_children;
1772 + --fpm_globals.running_children;
1774 + if (child->prev) child->prev->next = child->next;
1775 + else child->wp->children = child->next;
1776 + if (child->next) child->next->prev = child->prev;
1780 +static struct fpm_child_s *fpm_child_find(pid_t pid)
1782 + struct fpm_worker_pool_s *wp;
1783 + struct fpm_child_s *child = 0;
1785 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1787 + for (child = wp->children; child; child = child->next) {
1788 + if (child->pid == pid) {
1803 +static void fpm_child_init(struct fpm_worker_pool_s *wp)
1805 + fpm_globals.max_requests = wp->config->max_requests;
1807 + if (0 > fpm_stdio_init_child(wp) ||
1808 + 0 > fpm_unix_init_child(wp) ||
1809 + 0 > fpm_signals_init_child() ||
1810 + 0 > fpm_env_init_child(wp) ||
1811 + 0 > fpm_php_init_child(wp)) {
1813 + zlog(ZLOG_STUFF, ZLOG_ERROR, "child failed to initialize (pool %s)", wp->config->name);
1818 +int fpm_children_free(struct fpm_child_s *child)
1820 + struct fpm_child_s *next;
1822 + for (; child; child = next) {
1823 + next = child->next;
1824 + fpm_child_close(child, 0 /* in_event_loop */);
1830 +void fpm_children_bury()
1834 + struct fpm_child_s *child;
1836 + while ( (pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
1838 + int severity = ZLOG_NOTICE;
1840 + child = fpm_child_find(pid);
1842 + if (WIFEXITED(status)) {
1844 + snprintf(buf, sizeof(buf), "with code %d", WEXITSTATUS(status));
1846 + if (WEXITSTATUS(status) != 0) {
1847 + severity = ZLOG_WARNING;
1851 + else if (WIFSIGNALED(status)) {
1852 + const char *signame = fpm_signal_names[WTERMSIG(status)];
1853 + const char *have_core = WCOREDUMP(status) ? " (core dumped)" : "";
1855 + if (signame == NULL) {
1859 + snprintf(buf, sizeof(buf), "on signal %d %s%s", WTERMSIG(status), signame, have_core);
1861 + if (WTERMSIG(status) != SIGQUIT) { /* possible request loss */
1862 + severity = ZLOG_WARNING;
1865 + else if (WIFSTOPPED(status)) {
1867 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "child %d stopped for tracing", (int) pid);
1869 + if (child && child->tracer) {
1870 + child->tracer(child);
1877 + struct fpm_worker_pool_s *wp = child->wp;
1878 + struct timeval tv1, tv2;
1880 + fpm_child_unlink(child);
1882 + fpm_shm_slots_discard_slot(child);
1884 + fpm_clock_get(&tv1);
1886 + timersub(&tv1, &child->started, &tv2);
1888 + zlog(ZLOG_STUFF, severity, "child %d (pool %s) exited %s after %ld.%06d seconds from start", (int) pid,
1889 + child->wp->config->name, buf, tv2.tv_sec, (int) tv2.tv_usec);
1891 + fpm_child_close(child, 1 /* in event_loop */);
1893 + fpm_pctl_child_exited();
1895 + if (last_faults && (WTERMSIG(status) == SIGSEGV || WTERMSIG(status) == SIGBUS)) {
1896 + time_t now = tv1.tv_sec;
1897 + int restart_condition = 1;
1900 + last_faults[fault++] = now;
1902 + if (fault == fpm_global_options.emergency_restart_threshold) {
1906 + for (i = 0; i < fpm_global_options.emergency_restart_threshold; i++) {
1907 + if (now - last_faults[i] > fpm_global_options.emergency_restart_interval) {
1908 + restart_condition = 0;
1913 + if (restart_condition) {
1915 + zlog(ZLOG_STUFF, ZLOG_WARNING, "failed processes threshold (%d in %d sec) is reached, initiating reload",
1916 + fpm_global_options.emergency_restart_threshold, fpm_global_options.emergency_restart_interval);
1918 + fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
1922 + fpm_children_make(wp, 1 /* in event loop */);
1924 + if (fpm_globals.is_child) {
1929 + zlog(ZLOG_STUFF, ZLOG_ALERT, "oops, unknown child exited %s", buf);
1935 +static struct fpm_child_s *fpm_resources_prepare(struct fpm_worker_pool_s *wp)
1937 + struct fpm_child_s *c;
1939 + c = fpm_child_alloc();
1942 + zlog(ZLOG_STUFF, ZLOG_ERROR, "malloc failed (pool %s)", wp->config->name);
1947 + c->fd_stdout = -1; c->fd_stderr = -1;
1949 + if (0 > fpm_stdio_prepare_pipes(c)) {
1950 + fpm_child_free(c);
1954 + if (0 > fpm_shm_slots_prepare_slot(c)) {
1955 + fpm_stdio_discard_pipes(c);
1956 + fpm_child_free(c);
1963 +static void fpm_resources_discard(struct fpm_child_s *child)
1965 + fpm_shm_slots_discard_slot(child);
1966 + fpm_stdio_discard_pipes(child);
1967 + fpm_child_free(child);
1970 +static void fpm_child_resources_use(struct fpm_child_s *child)
1972 + fpm_shm_slots_child_use_slot(child);
1973 + fpm_stdio_child_use_pipes(child);
1974 + fpm_child_free(child);
1977 +static void fpm_parent_resources_use(struct fpm_child_s *child)
1979 + fpm_shm_slots_parent_use_slot(child);
1980 + fpm_stdio_parent_use_pipes(child);
1981 + fpm_child_link(child);
1984 +static int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop)
1988 + struct fpm_child_s *child;
1990 + while (!enough && fpm_pctl_can_spawn_children() && wp->running_children < wp->config->pm->max_children) {
1992 + child = fpm_resources_prepare(wp);
2004 + fpm_child_resources_use(child);
2005 + fpm_globals.is_child = 1;
2006 + if (in_event_loop) {
2007 + fpm_event_exit_loop();
2009 + fpm_child_init(wp);
2013 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fork() failed");
2016 + fpm_resources_discard(child);
2018 + break; /* dont try any more on error */
2022 + fpm_clock_get(&child->started);
2023 + fpm_parent_resources_use(child);
2025 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "child %d (pool %s) started", (int) pid, wp->config->name);
2030 + return 1; /* we are done */
2033 +int fpm_children_create_initial(struct fpm_worker_pool_s *wp)
2035 + return fpm_children_make(wp, 0 /* not in event loop yet */);
2038 +int fpm_children_init_main()
2040 + if (fpm_global_options.emergency_restart_threshold &&
2041 + fpm_global_options.emergency_restart_interval) {
2043 + last_faults = malloc(sizeof(time_t) * fpm_global_options.emergency_restart_threshold);
2045 + if (!last_faults) {
2049 + memset(last_faults, 0, sizeof(time_t) * fpm_global_options.emergency_restart_threshold);
2052 + fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_children_cleanup, 0);
2057 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_children.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_children.h
2058 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_children.h 1970-01-01 03:00:00.000000000 +0300
2059 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_children.h 2008-05-24 21:38:47.000000000 +0400
2063 + /* (c) 2007,2008 Andrei Nigmatulin */
2065 +#ifndef FPM_CHILDREN_H
2066 +#define FPM_CHILDREN_H 1
2068 +#include <sys/time.h>
2069 +#include <sys/types.h>
2072 +#include "fpm_worker_pool.h"
2074 +int fpm_children_create_initial(struct fpm_worker_pool_s *wp);
2075 +int fpm_children_free(struct fpm_child_s *child);
2076 +void fpm_children_bury();
2077 +int fpm_children_init_main();
2079 +struct fpm_child_s;
2081 +struct fpm_child_s {
2082 + struct fpm_child_s *prev, *next;
2083 + struct timeval started;
2084 + struct fpm_worker_pool_s *wp;
2085 + struct event ev_stdout, ev_stderr;
2087 + int fd_stdout, fd_stderr;
2088 + void (*tracer)(struct fpm_child_s *);
2089 + struct timeval slow_logged;
2094 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_cleanup.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_cleanup.c
2095 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_cleanup.c 1970-01-01 03:00:00.000000000 +0300
2096 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_cleanup.c 2008-05-24 21:38:47.000000000 +0400
2100 + /* (c) 2007,2008 Andrei Nigmatulin */
2102 +#include "fpm_config.h"
2104 +#include <stdlib.h>
2106 +#include "fpm_arrays.h"
2107 +#include "fpm_cleanup.h"
2112 + void (*cleanup)(int, void *);
2116 +static struct fpm_array_s cleanups = { .sz = sizeof(struct cleanup_s) };
2118 +int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *arg)
2120 + struct cleanup_s *c;
2122 + c = fpm_array_push(&cleanups);
2129 + c->cleanup = cleanup;
2135 +void fpm_cleanups_run(int type)
2137 + struct cleanup_s *c = fpm_array_item_last(&cleanups);
2138 + int cl = cleanups.used;
2140 + for ( ; cl--; c--) {
2141 + if (c->type & type) {
2142 + c->cleanup(type, c->arg);
2146 + fpm_array_free(&cleanups);
2149 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_cleanup.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_cleanup.h
2150 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_cleanup.h 1970-01-01 03:00:00.000000000 +0300
2151 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_cleanup.h 2008-05-24 21:38:47.000000000 +0400
2155 + /* (c) 2007,2008 Andrei Nigmatulin */
2157 +#ifndef FPM_CLEANUP_H
2158 +#define FPM_CLEANUP_H 1
2160 +int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *);
2161 +void fpm_cleanups_run(int type);
2164 + FPM_CLEANUP_CHILD = (1 << 0),
2165 + FPM_CLEANUP_PARENT_EXIT = (1 << 1),
2166 + FPM_CLEANUP_PARENT_EXIT_MAIN = (1 << 2),
2167 + FPM_CLEANUP_PARENT_EXEC = (1 << 3),
2168 + FPM_CLEANUP_PARENT = (1 << 1) | (1 << 2) | (1 << 3),
2169 + FPM_CLEANUP_ALL = ~0,
2174 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_clock.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_clock.c
2175 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_clock.c 1970-01-01 03:00:00.000000000 +0300
2176 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_clock.c 2008-09-19 03:19:59.000000000 +0400
2180 + /* (c) 2007,2008 Andrei Nigmatulin */
2182 +#include "fpm_config.h"
2184 +#if defined(HAVE_CLOCK_GETTIME)
2185 +#include <time.h> /* for CLOCK_MONOTONIC */
2188 +#include "fpm_clock.h"
2192 +/* posix monotonic clock - preferred source of time */
2193 +#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
2195 +static int monotonic_works;
2197 +int fpm_clock_init()
2199 + struct timespec ts;
2201 + monotonic_works = 0;
2203 + if (0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
2204 + monotonic_works = 1;
2210 +int fpm_clock_get(struct timeval *tv)
2212 + if (monotonic_works) {
2213 + struct timespec ts;
2215 + if (0 > clock_gettime(CLOCK_MONOTONIC, &ts)) {
2216 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "clock_gettime() failed");
2220 + tv->tv_sec = ts.tv_sec;
2221 + tv->tv_usec = ts.tv_nsec / 1000;
2225 + return gettimeofday(tv, 0);
2229 +#elif defined(HAVE_CLOCK_GET_TIME)
2231 +#include <mach/mach.h>
2232 +#include <mach/clock.h>
2233 +#include <mach/mach_error.h>
2235 +static clock_serv_t mach_clock;
2237 +/* this code borrowed from here: http://lists.apple.com/archives/Darwin-development/2002/Mar/msg00746.html */
2238 +/* mach_clock also should be re-initialized in child process after fork */
2239 +int fpm_clock_init()
2241 + kern_return_t ret;
2242 + mach_timespec_t aTime;
2244 + ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &mach_clock);
2246 + if (ret != KERN_SUCCESS) {
2247 + zlog(ZLOG_STUFF, ZLOG_ERROR, "host_get_clock_service() failed: %s", mach_error_string(ret));
2251 + /* test if it works */
2252 + ret = clock_get_time(mach_clock, &aTime);
2254 + if (ret != KERN_SUCCESS) {
2255 + zlog(ZLOG_STUFF, ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
2262 +int fpm_clock_get(struct timeval *tv)
2264 + kern_return_t ret;
2265 + mach_timespec_t aTime;
2267 + ret = clock_get_time(mach_clock, &aTime);
2269 + if (ret != KERN_SUCCESS) {
2270 + zlog(ZLOG_STUFF, ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
2274 + tv->tv_sec = aTime.tv_sec;
2275 + tv->tv_usec = aTime.tv_nsec / 1000;
2280 +#else /* no clock */
2282 +int fpm_clock_init()
2287 +int fpm_clock_get(struct timeval *tv)
2289 + return gettimeofday(tv, 0);
2293 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_clock.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_clock.h
2294 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_clock.h 1970-01-01 03:00:00.000000000 +0300
2295 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_clock.h 2008-05-24 21:38:47.000000000 +0400
2299 + /* (c) 2007,2008 Andrei Nigmatulin */
2301 +#ifndef FPM_CLOCK_H
2302 +#define FPM_CLOCK_H 1
2304 +#include <sys/time.h>
2306 +int fpm_clock_init();
2307 +int fpm_clock_get(struct timeval *tv);
2310 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_conf.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_conf.c
2311 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_conf.c 1970-01-01 03:00:00.000000000 +0300
2312 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_conf.c 2008-09-19 04:54:15.000000000 +0400
2316 + /* (c) 2007,2008 Andrei Nigmatulin */
2318 +#include "fpm_config.h"
2320 +#include <sys/types.h>
2321 +#include <sys/stat.h>
2323 +#include <string.h>
2324 +#include <stdlib.h>
2325 +#include <stddef.h>
2326 +#include <stdint.h>
2328 +#include <unistd.h>
2331 +#include "fpm_conf.h"
2332 +#include "fpm_stdio.h"
2333 +#include "fpm_worker_pool.h"
2334 +#include "fpm_cleanup.h"
2335 +#include "fpm_php.h"
2336 +#include "fpm_sockets.h"
2337 +#include "xml_config.h"
2341 +struct fpm_options_s fpm_global_options;
2343 +static void *fpm_global_options_ptr()
2345 + return &fpm_global_options;
2348 +static char *fpm_conf_set_log_level(void **conf, char *name, void *vv, intptr_t offset)
2352 + if (!strcmp(value, "debug")) {
2353 + fpm_globals.log_level = ZLOG_DEBUG;
2355 + else if (!strcmp(value, "notice")) {
2356 + fpm_globals.log_level = ZLOG_NOTICE;
2358 + else if (!strcmp(value, "warn")) {
2359 + fpm_globals.log_level = ZLOG_WARNING;
2361 + else if (!strcmp(value, "error")) {
2362 + fpm_globals.log_level = ZLOG_ERROR;
2364 + else if (!strcmp(value, "alert")) {
2365 + fpm_globals.log_level = ZLOG_ALERT;
2368 + return "invalid value for 'log_level'";
2374 +static struct xml_conf_section xml_section_fpm_global_options = {
2375 + .conf = &fpm_global_options_ptr,
2376 + .path = "/configuration/global_options",
2377 + .parsers = (struct xml_value_parser []) {
2378 + { XML_CONF_SCALAR, "emergency_restart_threshold", &xml_conf_set_slot_integer, offsetof(struct fpm_options_s, emergency_restart_threshold) },
2379 + { XML_CONF_SCALAR, "emergency_restart_interval", &xml_conf_set_slot_time, offsetof(struct fpm_options_s, emergency_restart_interval) },
2380 + { XML_CONF_SCALAR, "process_control_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_options_s, process_control_timeout) },
2381 + { XML_CONF_SCALAR, "daemonize", &xml_conf_set_slot_boolean, offsetof(struct fpm_options_s, daemonize) },
2382 + { XML_CONF_SCALAR, "pid_file", &xml_conf_set_slot_string, offsetof(struct fpm_options_s, pid_file) },
2383 + { XML_CONF_SCALAR, "error_log", &xml_conf_set_slot_string, offsetof(struct fpm_options_s, error_log) },
2384 + { XML_CONF_SCALAR, "log_level", &fpm_conf_set_log_level, 0 },
2389 +static char *fpm_conf_set_pm_style(void **conf, char *name, void *vv, intptr_t offset)
2392 + struct fpm_pm_s *c = *conf;
2394 + if (!strcmp(value, "static")) {
2395 + c->style = PM_STYLE_STATIC;
2397 + else if (!strcmp(value, "apache-like")) {
2398 + c->style = PM_STYLE_APACHE_LIKE;
2401 + return "invalid value for 'style'";
2407 +static char *fpm_conf_set_rlimit_core(void **conf, char *name, void *vv, intptr_t offset)
2410 + struct fpm_worker_pool_config_s *c = *conf;
2412 + if (!strcmp(value, "unlimited")) {
2413 + c->rlimit_core = -1;
2417 + void *subconf = &int_value;
2420 + error = xml_conf_set_slot_integer(&subconf, name, vv, 0);
2422 + if (error) return error;
2424 + if (int_value < 0) return "invalid value for 'rlimit_core'";
2426 + c->rlimit_core = int_value;
2432 +static char *fpm_conf_set_catch_workers_output(void **conf, char *name, void *vv, intptr_t offset)
2434 + struct fpm_worker_pool_config_s *c = *conf;
2436 + void *subconf = &int_value;
2439 + error = xml_conf_set_slot_boolean(&subconf, name, vv, 0);
2441 + if (error) return error;
2443 + c->catch_workers_output = int_value;
2448 +static struct xml_conf_section fpm_conf_set_apache_like_subsection_conf = {
2449 + .path = "apache_like somewhere", /* fixme */
2450 + .parsers = (struct xml_value_parser []) {
2451 + { XML_CONF_SCALAR, "StartServers", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.StartServers) },
2452 + { XML_CONF_SCALAR, "MinSpareServers", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.MinSpareServers) },
2453 + { XML_CONF_SCALAR, "MaxSpareServers", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.MaxSpareServers) },
2458 +static char *fpm_conf_set_apache_like_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2460 + return xml_conf_parse_section(conf, &fpm_conf_set_apache_like_subsection_conf, xml_node);
2463 +static struct xml_conf_section fpm_conf_set_listen_options_subsection_conf = {
2464 + .path = "listen options somewhere", /* fixme */
2465 + .parsers = (struct xml_value_parser []) {
2466 + { XML_CONF_SCALAR, "backlog", &xml_conf_set_slot_integer, offsetof(struct fpm_listen_options_s, backlog) },
2467 + { XML_CONF_SCALAR, "owner", &xml_conf_set_slot_string, offsetof(struct fpm_listen_options_s, owner) },
2468 + { XML_CONF_SCALAR, "group", &xml_conf_set_slot_string, offsetof(struct fpm_listen_options_s, group) },
2469 + { XML_CONF_SCALAR, "mode", &xml_conf_set_slot_string, offsetof(struct fpm_listen_options_s, mode) },
2474 +static char *fpm_conf_set_listen_options_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2476 + void *subconf = (char *) *conf + offset;
2477 + struct fpm_listen_options_s *lo;
2479 + lo = malloc(sizeof(*lo));
2482 + return "malloc() failed";
2485 + memset(lo, 0, sizeof(*lo));
2489 + * (struct fpm_listen_options_s **) subconf = lo;
2493 + return xml_conf_parse_section(&subconf, &fpm_conf_set_listen_options_subsection_conf, xml_node);
2496 +static struct xml_conf_section fpm_conf_set_pm_subsection_conf = {
2497 + .path = "pm settings somewhere", /* fixme */
2498 + .parsers = (struct xml_value_parser []) {
2499 + { XML_CONF_SCALAR, "style", &fpm_conf_set_pm_style, 0 },
2500 + { XML_CONF_SCALAR, "max_children", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, max_children) },
2501 + { XML_CONF_SUBSECTION, "apache_like", &fpm_conf_set_apache_like_subsection, offsetof(struct fpm_pm_s, options_apache_like) },
2506 +static char *fpm_conf_set_pm_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2508 + void *subconf = (char *) *conf + offset;
2509 + struct fpm_pm_s *pm;
2511 + pm = malloc(sizeof(*pm));
2514 + return "fpm_conf_set_pm_subsection(): malloc failed";
2517 + memset(pm, 0, sizeof(*pm));
2519 + * (struct fpm_pm_s **) subconf = pm;
2523 + return xml_conf_parse_section(&subconf, &fpm_conf_set_pm_subsection_conf, xml_node);
2526 +static char *xml_conf_set_slot_key_value_pair(void **conf, char *name, void *vv, intptr_t offset)
2529 + struct key_value_s *kv;
2530 + struct key_value_s ***parent = (struct key_value_s ***) conf;
2532 + kv = malloc(sizeof(*kv));
2535 + return "malloc() failed";
2538 + memset(kv, 0, sizeof(*kv));
2540 + kv->key = strdup(name);
2541 + kv->value = strdup(value);
2543 + if (!kv->key || !kv->value) {
2544 + return "xml_conf_set_slot_key_value_pair(): strdup() failed";
2549 + *parent = &kv->next;
2554 +static struct xml_conf_section fpm_conf_set_key_value_pairs_subsection_conf = {
2555 + .path = "key_value_pairs somewhere", /* fixme */
2556 + .parsers = (struct xml_value_parser []) {
2557 + { XML_CONF_SCALAR, 0, &xml_conf_set_slot_key_value_pair, 0 },
2562 +static char *fpm_conf_set_key_value_pairs_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2564 + void *next_kv = (char *) *conf + offset;
2566 + return xml_conf_parse_section(&next_kv, &fpm_conf_set_key_value_pairs_subsection_conf, xml_node);
2569 +static void *fpm_worker_pool_config_alloc()
2571 + static struct fpm_worker_pool_s *current_wp = 0;
2572 + struct fpm_worker_pool_s *wp;
2574 + wp = fpm_worker_pool_alloc();
2576 + if (!wp) return 0;
2578 + wp->config = malloc(sizeof(struct fpm_worker_pool_config_s));
2580 + if (!wp->config) return 0;
2582 + memset(wp->config, 0, sizeof(struct fpm_worker_pool_config_s));
2584 + if (current_wp) current_wp->next = wp;
2588 + return wp->config;
2591 +int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc)
2593 + struct key_value_s *kv, *kv_next;
2596 + free(wpc->listen_address);
2597 + if (wpc->listen_options) {
2598 + free(wpc->listen_options->owner);
2599 + free(wpc->listen_options->group);
2600 + free(wpc->listen_options->mode);
2601 + free(wpc->listen_options);
2603 + for (kv = wpc->php_defines; kv; kv = kv_next) {
2604 + kv_next = kv->next;
2609 + for (kv = wpc->environment; kv; kv = kv_next) {
2610 + kv_next = kv->next;
2618 + free(wpc->chroot);
2620 + free(wpc->allowed_clients);
2621 + free(wpc->slowlog);
2626 +static struct xml_conf_section xml_section_fpm_worker_pool_config = {
2627 + .conf = &fpm_worker_pool_config_alloc,
2628 + .path = "/configuration/workers/pool",
2629 + .parsers = (struct xml_value_parser []) {
2630 + { XML_CONF_SCALAR, "name", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, name) },
2631 + { XML_CONF_SCALAR, "listen_address", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, listen_address) },
2632 + { XML_CONF_SUBSECTION, "listen_options", &fpm_conf_set_listen_options_subsection, offsetof(struct fpm_worker_pool_config_s, listen_options) },
2633 + { XML_CONF_SUBSECTION, "php_defines", &fpm_conf_set_key_value_pairs_subsection, offsetof(struct fpm_worker_pool_config_s, php_defines) },
2634 + { XML_CONF_SCALAR, "user", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, user) },
2635 + { XML_CONF_SCALAR, "group", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, group) },
2636 + { XML_CONF_SCALAR, "chroot", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, chroot) },
2637 + { XML_CONF_SCALAR, "chdir", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, chdir) },
2638 + { XML_CONF_SCALAR, "allowed_clients", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, allowed_clients) },
2639 + { XML_CONF_SUBSECTION, "environment", &fpm_conf_set_key_value_pairs_subsection, offsetof(struct fpm_worker_pool_config_s, environment) },
2640 + { XML_CONF_SCALAR, "request_terminate_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_worker_pool_config_s, request_terminate_timeout) },
2641 + { XML_CONF_SCALAR, "request_slowlog_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_worker_pool_config_s, request_slowlog_timeout) },
2642 + { XML_CONF_SCALAR, "slowlog", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, slowlog) },
2643 + { XML_CONF_SCALAR, "rlimit_files", &xml_conf_set_slot_integer, offsetof(struct fpm_worker_pool_config_s, rlimit_files) },
2644 + { XML_CONF_SCALAR, "rlimit_core", &fpm_conf_set_rlimit_core, 0 },
2645 + { XML_CONF_SCALAR, "max_requests", &xml_conf_set_slot_integer, offsetof(struct fpm_worker_pool_config_s, max_requests) },
2646 + { XML_CONF_SCALAR, "catch_workers_output", &fpm_conf_set_catch_workers_output, 0 },
2647 + { XML_CONF_SUBSECTION, "pm", &fpm_conf_set_pm_subsection, offsetof(struct fpm_worker_pool_config_s, pm) },
2652 +static struct xml_conf_section *fpm_conf_all_sections[] = {
2653 + &xml_section_fpm_global_options,
2654 + &xml_section_fpm_worker_pool_config,
2658 +static int fpm_evaluate_full_path(char **path)
2660 + if (**path != '/') {
2663 + full_path = malloc(sizeof(PHP_PREFIX) + strlen(*path) + 1);
2665 + if (!full_path) return -1;
2667 + sprintf(full_path, "%s/%s", PHP_PREFIX, *path);
2671 + *path = full_path;
2677 +static int fpm_conf_process_all_pools()
2679 + struct fpm_worker_pool_s *wp;
2681 + if (!fpm_worker_all_pools) {
2682 + zlog(ZLOG_STUFF, ZLOG_ERROR, "at least one pool section must be specified in config file");
2686 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
2688 + if (wp->config->listen_address && *wp->config->listen_address) {
2690 + wp->listen_address_domain = fpm_sockets_domain_from_address(wp->config->listen_address);
2692 + if (wp->listen_address_domain == FPM_AF_UNIX && *wp->config->listen_address != '/') {
2693 + fpm_evaluate_full_path(&wp->config->listen_address);
2699 + wp->is_template = 1;
2703 + if (wp->config->request_slowlog_timeout) {
2705 + if (! (wp->config->slowlog && *wp->config->slowlog)) {
2706 + zlog(ZLOG_STUFF, ZLOG_ERROR, "pool %s: 'slowlog' must be specified for use with 'request_slowlog_timeout'",
2707 + wp->config->name);
2711 + static int warned = 0;
2714 + zlog(ZLOG_STUFF, ZLOG_WARNING, "pool %s: 'request_slowlog_timeout' is not supported on your system",
2715 + wp->config->name);
2719 + wp->config->request_slowlog_timeout = 0;
2723 + if (wp->config->request_slowlog_timeout && wp->config->slowlog && *wp->config->slowlog) {
2726 + fpm_evaluate_full_path(&wp->config->slowlog);
2728 + if (wp->config->request_slowlog_timeout) {
2729 + fd = open(wp->config->slowlog, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
2732 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(%s) failed", wp->config->slowlog);
2743 +int fpm_conf_unlink_pid()
2745 + if (fpm_global_options.pid_file) {
2747 + if (0 > unlink(fpm_global_options.pid_file)) {
2748 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "unlink(\"%s\") failed", fpm_global_options.pid_file);
2757 +int fpm_conf_write_pid()
2761 + if (fpm_global_options.pid_file) {
2765 + unlink(fpm_global_options.pid_file);
2767 + fd = creat(fpm_global_options.pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
2770 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "creat(\"%s\") failed", fpm_global_options.pid_file);
2774 + len = sprintf(buf, "%d", (int) fpm_globals.parent_pid);
2776 + if (len != write(fd, buf, len)) {
2777 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "write() failed");
2787 +static int fpm_conf_post_process()
2789 + if (fpm_global_options.pid_file) {
2790 + fpm_evaluate_full_path(&fpm_global_options.pid_file);
2793 + if (!fpm_global_options.error_log) {
2794 + fpm_global_options.error_log = strdup(PHP_FPM_LOG_PATH);
2797 + fpm_evaluate_full_path(&fpm_global_options.error_log);
2799 + if (0 > fpm_stdio_open_error_log(0)) {
2803 + return fpm_conf_process_all_pools();
2806 +static void fpm_conf_cleanup(int which, void *arg)
2808 + free(fpm_global_options.pid_file);
2809 + free(fpm_global_options.error_log);
2810 + fpm_global_options.pid_file = 0;
2811 + fpm_global_options.error_log = 0;
2814 +int fpm_conf_init_main()
2816 + char *filename = fpm_globals.config;
2819 + if (0 > xml_conf_sections_register(fpm_conf_all_sections)) {
2823 + if (filename == NULL) {
2824 + filename = PHP_FPM_CONF_PATH;
2827 + err = xml_conf_load_file(filename);
2830 + zlog(ZLOG_STUFF, ZLOG_ERROR, "failed to load configuration file: %s", err);
2834 + if (0 > fpm_conf_post_process()) {
2840 + fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_conf_cleanup, 0);
2844 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_conf.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_conf.h
2845 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_conf.h 1970-01-01 03:00:00.000000000 +0300
2846 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_conf.h 2008-08-26 19:09:15.000000000 +0400
2850 + /* (c) 2007,2008 Andrei Nigmatulin */
2853 +#define FPM_CONF_H 1
2855 +struct key_value_s;
2857 +struct key_value_s {
2858 + struct key_value_s *next;
2863 +struct fpm_options_s {
2864 + int emergency_restart_threshold;
2865 + int emergency_restart_interval;
2866 + int process_control_timeout;
2872 +extern struct fpm_options_s fpm_global_options;
2879 + int MinSpareServers;
2880 + int MaxSpareServers;
2881 + } options_apache_like;
2884 +struct fpm_listen_options_s {
2891 +struct fpm_worker_pool_config_s {
2893 + char *listen_address;
2894 + struct fpm_listen_options_s *listen_options;
2895 + struct key_value_s *php_defines;
2900 + char *allowed_clients;
2901 + struct key_value_s *environment;
2902 + struct fpm_pm_s *pm;
2903 + int request_terminate_timeout;
2904 + int request_slowlog_timeout;
2909 + unsigned catch_workers_output:1;
2912 +enum { PM_STYLE_STATIC = 1, PM_STYLE_APACHE_LIKE = 2 };
2914 +int fpm_conf_init_main();
2915 +int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc);
2916 +int fpm_conf_write_pid();
2917 +int fpm_conf_unlink_pid();
2921 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_config.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_config.h
2922 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_config.h 1970-01-01 03:00:00.000000000 +0300
2923 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_config.h 2008-05-25 04:30:43.000000000 +0400
2927 + /* (c) 2007,2008 Andrei Nigmatulin */
2929 +#include "php_config.h"
2930 +#include "fpm_autoconf.h"
2933 +/* Solaris does not have it */
2934 +#ifndef INADDR_NONE
2935 +#define INADDR_NONE (-1)
2939 +/* If we're not using GNU C, elide __attribute__ */
2941 +# define __attribute__(x) /*NOTHING*/
2945 +/* Solaris does not have it */
2947 +#define timersub(tvp, uvp, vvp) \
2949 + (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2950 + (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2951 + if ((vvp)->tv_usec < 0) { \
2952 + (vvp)->tv_sec--; \
2953 + (vvp)->tv_usec += 1000000; \
2958 +#if defined(HAVE_PTRACE) || defined(PROC_MEM_FILE) || defined(HAVE_MACH_VM_READ)
2959 +#define HAVE_FPM_TRACE 1
2961 +#define HAVE_FPM_TRACE 0
2964 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_env.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_env.c
2965 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_env.c 1970-01-01 03:00:00.000000000 +0300
2966 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_env.c 2008-09-19 03:19:59.000000000 +0400
2970 + /* (c) 2007,2008 Andrei Nigmatulin */
2972 +#include "fpm_config.h"
2974 +#ifdef HAVE_ALLOCA_H
2975 +#include <alloca.h>
2978 +#include <stdlib.h>
2979 +#include <string.h>
2981 +#include "fpm_env.h"
2984 +#ifndef HAVE_SETENV
2985 +int setenv(char *name, char *value, int overwrite)
2987 + int name_len = strlen(name);
2988 + int value_len = strlen(value);
2989 + char *var = alloca(name_len + 1 + value_len + 1);
2991 + memcpy(var, name, name_len);
2993 + var[name_len] = '=';
2995 + memcpy(var + name_len + 1, value, value_len);
2997 + var[name_len + 1 + value_len] = '\0';
2999 + return putenv(var);
3003 +#ifndef HAVE_CLEARENV
3009 + /* this algo is the only one known to me
3010 + that works well on all systems */
3011 + while (*(envp = environ)) {
3012 + char *eq = strchr(*envp, '=');
3014 + s = strdup(*envp);
3016 + if (eq) s[eq - *envp] = '\0';
3026 +int fpm_env_init_child(struct fpm_worker_pool_s *wp)
3028 + struct key_value_s *kv;
3032 + for (kv = wp->config->environment; kv; kv = kv->next) {
3033 + setenv(kv->key, kv->value, 1);
3037 + setenv("USER", wp->user, 1);
3041 + setenv("HOME", wp->home, 1);
3047 +static int fpm_env_conf_wp(struct fpm_worker_pool_s *wp)
3049 + struct key_value_s *kv;
3051 + kv = wp->config->environment;
3053 + for (kv = wp->config->environment; kv; kv = kv->next) {
3054 + if (*kv->value == '$') {
3055 + char *value = getenv(kv->value + 1);
3057 + if (!value) value = "";
3060 + kv->value = strdup(value);
3063 + /* autodetected values should be removed
3064 + if these vars specified in config */
3065 + if (!strcmp(kv->key, "USER")) {
3070 + if (!strcmp(kv->key, "HOME")) {
3079 +int fpm_env_init_main()
3081 + struct fpm_worker_pool_s *wp;
3083 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
3085 + if (0 > fpm_env_conf_wp(wp)) {
3093 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_env.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_env.h
3094 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_env.h 1970-01-01 03:00:00.000000000 +0300
3095 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_env.h 2008-09-19 03:19:59.000000000 +0400
3099 + /* (c) 2007,2008 Andrei Nigmatulin */
3102 +#define FPM_ENV_H 1
3104 +#include "fpm_worker_pool.h"
3106 +int fpm_env_init_child(struct fpm_worker_pool_s *wp);
3107 +int fpm_env_init_main();
3109 +extern char **environ;
3111 +#ifndef HAVE_SETENV
3112 +int setenv(char *name, char *value, int overwrite);
3115 +#ifndef HAVE_CLEARENV
3121 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_events.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_events.c
3122 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_events.c 1970-01-01 03:00:00.000000000 +0300
3123 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_events.c 2008-08-26 19:09:15.000000000 +0400
3127 + /* (c) 2007,2008 Andrei Nigmatulin */
3129 +#include "fpm_config.h"
3131 +#include <unistd.h>
3133 +#include <stdlib.h> /* for putenv */
3134 +#include <string.h>
3135 +#include <sys/types.h> /* for event.h below */
3139 +#include "fpm_process_ctl.h"
3140 +#include "fpm_events.h"
3141 +#include "fpm_cleanup.h"
3142 +#include "fpm_stdio.h"
3143 +#include "fpm_signals.h"
3144 +#include "fpm_children.h"
3147 +static void fpm_event_cleanup(int which, void *arg)
3149 + event_base_free(0);
3152 +static void fpm_got_signal(int fd, short ev, void *arg)
3160 + res = read(fd, &c, 1);
3161 + } while (res == -1 && errno == EINTR);
3164 + if (res < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
3165 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
3171 + case 'C' : /* SIGCHLD */
3172 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGCHLD");
3173 + fpm_children_bury();
3175 + case 'I' : /* SIGINT */
3176 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGINT");
3177 + fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
3179 + case 'T' : /* SIGTERM */
3180 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGTERM");
3181 + fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
3183 + case 'Q' : /* SIGQUIT */
3184 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGQUIT");
3185 + fpm_pctl(FPM_PCTL_STATE_FINISHING, FPM_PCTL_ACTION_SET);
3187 + case '1' : /* SIGUSR1 */
3188 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGUSR1");
3189 + if (0 == fpm_stdio_open_error_log(1)) {
3190 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "log file re-opened");
3193 + case '2' : /* SIGUSR2 */
3194 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGUSR2");
3195 + fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
3199 + if (fpm_globals.is_child) {
3208 +int fpm_event_init_main()
3212 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: using %s", event_get_method());
3214 + fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_event_cleanup, 0);
3219 +int fpm_event_loop()
3221 + static struct event signal_fd_event;
3223 + event_set(&signal_fd_event, fpm_signals_get_fd(), EV_PERSIST | EV_READ, &fpm_got_signal, 0);
3225 + event_add(&signal_fd_event, 0);
3227 + fpm_pctl_heartbeat(-1, 0, 0);
3229 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: entering main loop");
3236 +int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg)
3238 + event_set(ev, fd, EV_PERSIST | EV_READ, callback, arg);
3240 + return event_add(ev, 0);
3243 +int fpm_event_del(struct event *ev)
3245 + return event_del(ev);
3248 +void fpm_event_exit_loop()
3250 + event_loopexit(0);
3253 +void fpm_event_fire(struct event *ev)
3255 + (*ev->ev_callback)( (int) ev->ev_fd, (short) ev->ev_res, ev->ev_arg);
3258 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_events.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_events.h
3259 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_events.h 1970-01-01 03:00:00.000000000 +0300
3260 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_events.h 2008-05-24 21:38:47.000000000 +0400
3264 + /* (c) 2007,2008 Andrei Nigmatulin */
3266 +#ifndef FPM_EVENTS_H
3267 +#define FPM_EVENTS_H 1
3269 +void fpm_event_exit_loop();
3270 +int fpm_event_loop();
3271 +int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg);
3272 +int fpm_event_del(struct event *ev);
3273 +void fpm_event_fire(struct event *ev);
3274 +int fpm_event_init_main();
3278 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm.h php-5.2.6.fpm/sapi/cgi/fpm/fpm.h
3279 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm.h 1970-01-01 03:00:00.000000000 +0300
3280 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm.h 2008-05-24 21:38:47.000000000 +0400
3284 + /* (c) 2007,2008 Andrei Nigmatulin */
3289 +#include <unistd.h>
3291 +int fpm_run(int *max_requests);
3292 +int fpm_init(int argc, char **argv, char *config);
3294 +struct fpm_globals_s {
3299 + int running_children;
3302 + int listening_socket; /* for this child */
3303 + int max_requests; /* for this child */
3307 +extern struct fpm_globals_s fpm_globals;
3312 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_php.c
3313 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php.c 1970-01-01 03:00:00.000000000 +0300
3314 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_php.c 2008-07-20 20:16:28.000000000 +0400
3318 + /* (c) 2007,2008 Andrei Nigmatulin */
3320 +#include "fpm_config.h"
3322 +#include <stdlib.h>
3323 +#include <string.h>
3326 +#include "php_main.h"
3327 +#include "php_ini.h"
3328 +#include "ext/standard/dl.h"
3330 +#include "fastcgi.h"
3333 +#include "fpm_php.h"
3334 +#include "fpm_cleanup.h"
3335 +#include "fpm_worker_pool.h"
3337 +static int zend_ini_alter_master(char *name, int name_length, char *new_value, int new_value_length, int stage)
3339 + zend_ini_entry *ini_entry;
3342 + if (zend_hash_find(EG(ini_directives), name, name_length, (void **) &ini_entry) == FAILURE) {
3346 + duplicate = strdup(new_value);
3348 + if (!ini_entry->on_modify
3349 + || ini_entry->on_modify(ini_entry, duplicate, new_value_length,
3350 + ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage) == SUCCESS) {
3351 + ini_entry->value = duplicate;
3352 + ini_entry->value_length = new_value_length;
3360 +static void fpm_php_disable(char *value, int (*zend_disable)(char *, uint))
3362 + char *s = 0, *e = value;
3370 + zend_disable(s, e - s);
3384 + zend_disable(s, e - s);
3388 +static int fpm_php_apply_defines(struct fpm_worker_pool_s *wp)
3390 + struct key_value_s *kv;
3392 + for (kv = wp->config->php_defines; kv; kv = kv->next) {
3393 + char *name = kv->key;
3394 + char *value = kv->value;
3395 + int name_len = strlen(name);
3396 + int value_len = strlen(value);
3398 + if (!strcmp(name, "extension") && *value) {
3401 +#if defined(PHP_VERSION_ID) && (PHP_VERSION_ID >= 50300)
3402 + php_dl(value, MODULE_PERSISTENT, &zv, 1);
3405 + ZVAL_STRINGL(&filename, value, value_len, 0);
3406 +#if (PHP_MAJOR_VERSION >= 5)
3407 + php_dl(&filename, MODULE_PERSISTENT, &zv, 1);
3409 + php_dl(&filename, MODULE_PERSISTENT, &zv);
3415 + zend_ini_alter_master(name, name_len + 1, value, value_len, PHP_INI_STAGE_ACTIVATE);
3417 + if (!strcmp(name, "disable_functions") && *value) {
3418 + char *v = strdup(value);
3419 +#if (PHP_MAJOR_VERSION >= 5)
3420 + PG(disable_functions) = v;
3422 + fpm_php_disable(v, zend_disable_function);
3424 + else if (!strcmp(name, "disable_classes") && *value) {
3425 + char *v = strdup(value);
3426 +#if (PHP_MAJOR_VERSION >= 5)
3427 + PG(disable_classes) = v;
3429 + fpm_php_disable(v, zend_disable_class);
3436 +static int fpm_php_set_allowed_clients(struct fpm_worker_pool_s *wp)
3438 + if (wp->listen_address_domain == FPM_AF_INET) {
3439 + fcgi_set_allowed_clients(wp->config->allowed_clients);
3445 +char *fpm_php_script_filename()
3447 + return SG(request_info).path_translated;
3450 +char *fpm_php_request_method()
3452 + return (char *) SG(request_info).request_method;
3455 +size_t fpm_php_content_length()
3457 + return SG(request_info).content_length;
3460 +static void fpm_php_cleanup(int which, void *arg)
3462 + php_module_shutdown();
3466 +void fpm_php_soft_quit()
3468 + fcgi_set_in_shutdown(1);
3471 +int fpm_php_init_main()
3473 + fpm_cleanup_add(FPM_CLEANUP_PARENT, fpm_php_cleanup, 0);
3478 +int fpm_php_init_child(struct fpm_worker_pool_s *wp)
3480 + if (0 > fpm_php_apply_defines(wp) ||
3481 + 0 > fpm_php_set_allowed_clients(wp)) {
3487 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_php.h
3488 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php.h 1970-01-01 03:00:00.000000000 +0300
3489 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_php.h 2008-05-24 21:38:47.000000000 +0400
3493 + /* (c) 2007,2008 Andrei Nigmatulin */
3496 +#define FPM_PHP_H 1
3498 +#include "fpm_worker_pool.h"
3500 +#include "build-defs.h" /* for PHP_ defines */
3502 +int fpm_php_init_child(struct fpm_worker_pool_s *wp);
3503 +char *fpm_php_script_filename();
3504 +char *fpm_php_request_method();
3505 +size_t fpm_php_content_length();
3506 +void fpm_php_soft_quit();
3507 +int fpm_php_init_main();
3511 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php_trace.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_php_trace.c
3512 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php_trace.c 1970-01-01 03:00:00.000000000 +0300
3513 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_php_trace.c 2008-08-26 19:09:15.000000000 +0400
3517 + /* (c) 2007,2008 Andrei Nigmatulin */
3519 +#include "fpm_config.h"
3524 +#include "php_main.h"
3527 +#include <stddef.h>
3528 +#include <stdint.h>
3529 +#include <unistd.h>
3530 +#include <sys/time.h>
3531 +#include <sys/types.h>
3534 +#include "fpm_trace.h"
3535 +#include "fpm_php_trace.h"
3536 +#include "fpm_children.h"
3537 +#include "fpm_worker_pool.h"
3538 +#include "fpm_process_ctl.h"
3543 +#define valid_ptr(p) ((p) && 0 == ((p) & (sizeof(long) - 1)))
3545 +#if SIZEOF_LONG == 4
3546 +#define PTR_FMT "08"
3547 +#elif SIZEOF_LONG == 8
3548 +#define PTR_FMT "016"
3552 +static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog)
3554 + int callers_limit = 20;
3555 + pid_t pid = child->pid;
3556 + struct timeval tv;
3557 + static const int buf_size = 1024;
3558 + char buf[buf_size];
3559 + long execute_data;
3562 + gettimeofday(&tv, 0);
3564 + zlog_print_time(&tv, buf, buf_size);
3566 + fprintf(slowlog, "\n%s pid %d (pool %s)\n", buf, (int) pid, child->wp->config->name);
3568 + if (0 > fpm_trace_get_strz(buf, buf_size, (long) &SG(request_info).path_translated)) {
3572 + fprintf(slowlog, "script_filename = %s\n", buf);
3574 + if (0 > fpm_trace_get_long((long) &EG(current_execute_data), &l)) {
3580 + while (execute_data) {
3584 + fprintf(slowlog, "[0x%" PTR_FMT "lx] ", execute_data);
3586 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, function_state.function), &l)) {
3592 + if (valid_ptr(function)) {
3593 + if (0 > fpm_trace_get_strz(buf, buf_size, function + offsetof(zend_function, common.function_name))) {
3597 + fprintf(slowlog, "%s()", buf);
3600 + fprintf(slowlog, "???");
3603 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, op_array), &l)) {
3609 + if (valid_ptr(l)) {
3610 + long op_array = l;
3612 + if (0 > fpm_trace_get_strz(buf, buf_size, op_array + offsetof(zend_op_array, filename))) {
3617 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, opline), &l)) {
3621 + if (valid_ptr(l)) {
3623 + uint *lu = (uint *) &l;
3625 + if (0 > fpm_trace_get_long(opline + offsetof(struct _zend_op, lineno), &l)) {
3632 + fprintf(slowlog, " %s:%u\n", *buf ? buf : "unknown", lineno);
3634 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, prev_execute_data), &l)) {
3640 + if (0 == --callers_limit) {
3648 +void fpm_php_trace(struct fpm_child_s *child)
3652 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "about to trace %d", (int) child->pid);
3654 + slowlog = fopen(child->wp->config->slowlog, "a+");
3657 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fopen(%s) failed", child->wp->config->slowlog);
3661 + if (0 > fpm_trace_ready(child->pid)) {
3665 + if (0 > fpm_php_trace_dump(child, slowlog)) {
3666 + fprintf(slowlog, "+++ dump failed\n");
3669 + if (0 > fpm_trace_close(child->pid)) {
3677 + fpm_pctl_kill(child->pid, FPM_PCTL_CONT);
3678 + child->tracer = 0;
3680 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "finished trace of %d", (int) child->pid);
3685 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php_trace.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_php_trace.h
3686 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php_trace.h 1970-01-01 03:00:00.000000000 +0300
3687 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_php_trace.h 2008-05-24 21:38:47.000000000 +0400
3691 + /* (c) 2007,2008 Andrei Nigmatulin */
3693 +#ifndef FPM_PHP_TRACE_H
3694 +#define FPM_PHP_TRACE_H 1
3696 +struct fpm_child_s;
3698 +void fpm_php_trace(struct fpm_child_s *);
3702 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_process_ctl.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_process_ctl.c
3703 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_process_ctl.c 1970-01-01 03:00:00.000000000 +0300
3704 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_process_ctl.c 2008-07-21 17:13:44.000000000 +0400
3708 + /* (c) 2007,2008 Andrei Nigmatulin */
3710 +#include "fpm_config.h"
3712 +#include <sys/types.h>
3713 +#include <signal.h>
3714 +#include <unistd.h>
3715 +#include <stdlib.h>
3718 +#include "fpm_clock.h"
3719 +#include "fpm_children.h"
3720 +#include "fpm_signals.h"
3721 +#include "fpm_events.h"
3722 +#include "fpm_process_ctl.h"
3723 +#include "fpm_cleanup.h"
3724 +#include "fpm_request.h"
3725 +#include "fpm_worker_pool.h"
3729 +static int fpm_state = FPM_PCTL_STATE_NORMAL;
3730 +static int fpm_signal_sent = 0;
3733 +static const char *fpm_state_names[] = {
3734 + [FPM_PCTL_STATE_NORMAL] = "normal",
3735 + [FPM_PCTL_STATE_RELOADING] = "reloading",
3736 + [FPM_PCTL_STATE_TERMINATING] = "terminating",
3737 + [FPM_PCTL_STATE_FINISHING] = "finishing"
3740 +static int saved_argc;
3741 +static char **saved_argv;
3743 +static void fpm_pctl_cleanup(int which, void *arg)
3747 + if (which != FPM_CLEANUP_PARENT_EXEC) {
3749 + for (i = 0; i < saved_argc; i++) {
3750 + free(saved_argv[i]);
3758 +static struct event pctl_event;
3760 +static void fpm_pctl_action(int fd, short which, void *arg)
3762 + evtimer_del(&pctl_event);
3764 + memset(&pctl_event, 0, sizeof(pctl_event));
3766 + fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_TIMEOUT);
3769 +static int fpm_pctl_timeout_set(int sec)
3771 + struct timeval tv = { .tv_sec = sec, .tv_usec = 0 };
3773 + if (evtimer_initialized(&pctl_event)) {
3774 + evtimer_del(&pctl_event);
3777 + evtimer_set(&pctl_event, &fpm_pctl_action, 0);
3779 + evtimer_add(&pctl_event, &tv);
3784 +static void fpm_pctl_exit()
3786 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "exiting, bye-bye!");
3788 + fpm_conf_unlink_pid();
3790 + fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT_MAIN);
3795 +#define optional_arg(c) (saved_argc > c ? ", \"" : ""), (saved_argc > c ? saved_argv[c] : ""), (saved_argc > c ? "\"" : "")
3797 +static void fpm_pctl_exec()
3800 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "reloading: execvp(\"%s\", {\"%s\""
3801 + "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
3802 + "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
3804 + saved_argv[0], saved_argv[0],
3817 + fpm_cleanups_run(FPM_CLEANUP_PARENT_EXEC);
3819 + execvp(saved_argv[0], saved_argv);
3821 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "execvp() failed");
3826 +static void fpm_pctl_action_last()
3828 + switch (fpm_state) {
3830 + case FPM_PCTL_STATE_RELOADING :
3835 + case FPM_PCTL_STATE_FINISHING :
3837 + case FPM_PCTL_STATE_TERMINATING :
3844 +int fpm_pctl_kill(pid_t pid, int how)
3849 + case FPM_PCTL_TERM :
3852 + case FPM_PCTL_STOP :
3855 + case FPM_PCTL_CONT :
3862 + return kill(pid, s);
3865 +static void fpm_pctl_kill_all(int signo)
3867 + struct fpm_worker_pool_s *wp;
3868 + int alive_children = 0;
3870 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
3871 + struct fpm_child_s *child;
3873 + for (child = wp->children; child; child = child->next) {
3875 + int res = kill(child->pid, signo);
3877 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "sending signal %d %s to child %d (pool %s)", signo,
3878 + fpm_signal_names[signo] ? fpm_signal_names[signo] : "",
3879 + (int) child->pid, child->wp->config->name);
3881 + if (res == 0) ++alive_children;
3885 + if (alive_children) {
3886 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "%d %s still alive", alive_children, alive_children == 1 ? "child is" : "children are");
3890 +static void fpm_pctl_action_next()
3894 + if (!fpm_globals.running_children) fpm_pctl_action_last();
3896 + if (fpm_signal_sent == 0) {
3897 + if (fpm_state == FPM_PCTL_STATE_TERMINATING) {
3903 + timeout = fpm_global_options.process_control_timeout;
3906 + if (fpm_signal_sent == SIGQUIT) {
3915 + fpm_pctl_kill_all(sig);
3917 + fpm_signal_sent = sig;
3919 + fpm_pctl_timeout_set(timeout);
3922 +void fpm_pctl(int new_state, int action)
3926 + case FPM_PCTL_ACTION_SET :
3928 + if (fpm_state == new_state) { /* already in progress - just ignore duplicate signal */
3932 + switch (fpm_state) { /* check which states can be overridden */
3934 + case FPM_PCTL_STATE_NORMAL :
3936 + /* 'normal' can be overridden by any other state */
3939 + case FPM_PCTL_STATE_RELOADING :
3941 + /* 'reloading' can be overridden by 'finishing' */
3942 + if (new_state == FPM_PCTL_STATE_FINISHING) break;
3944 + case FPM_PCTL_STATE_FINISHING :
3946 + /* 'reloading' and 'finishing' can be overridden by 'terminating' */
3947 + if (new_state == FPM_PCTL_STATE_TERMINATING) break;
3949 + case FPM_PCTL_STATE_TERMINATING :
3951 + /* nothing can override 'terminating' state */
3952 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "not switching to '%s' state, because already in '%s' state",
3953 + fpm_state_names[new_state], fpm_state_names[fpm_state]);
3958 + fpm_signal_sent = 0;
3959 + fpm_state = new_state;
3961 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "switching to '%s' state", fpm_state_names[fpm_state]);
3965 + case FPM_PCTL_ACTION_TIMEOUT :
3967 + fpm_pctl_action_next();
3971 + case FPM_PCTL_ACTION_LAST_CHILD_EXITED :
3973 + fpm_pctl_action_last();
3980 +int fpm_pctl_can_spawn_children()
3982 + return fpm_state == FPM_PCTL_STATE_NORMAL;
3985 +int fpm_pctl_child_exited()
3987 + if (fpm_state == FPM_PCTL_STATE_NORMAL) return 0;
3989 + if (!fpm_globals.running_children) {
3990 + fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_LAST_CHILD_EXITED);
3996 +int fpm_pctl_init_main()
4000 + saved_argc = fpm_globals.argc;
4002 + saved_argv = malloc(sizeof(char *) * (saved_argc + 1));
4004 + if (!saved_argv) {
4008 + for (i = 0; i < saved_argc; i++) {
4009 + saved_argv[i] = strdup(fpm_globals.argv[i]);
4011 + if (!saved_argv[i]) {
4016 + saved_argv[i] = 0;
4018 + fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_pctl_cleanup, 0);
4023 +static void fpm_pctl_check_request_timeout(struct timeval *now)
4025 + struct fpm_worker_pool_s *wp;
4027 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
4028 + int terminate_timeout = wp->config->request_terminate_timeout;
4029 + int slowlog_timeout = wp->config->request_slowlog_timeout;
4030 + struct fpm_child_s *child;
4032 + if (terminate_timeout || slowlog_timeout) {
4033 + for (child = wp->children; child; child = child->next) {
4034 + fpm_request_check_timed_out(child, now, terminate_timeout, slowlog_timeout);
4041 +void fpm_pctl_heartbeat(int fd, short which, void *arg)
4043 + static struct event heartbeat;
4044 + struct timeval tv = { .tv_sec = 0, .tv_usec = 130000 };
4045 + struct timeval now;
4047 + if (which == EV_TIMEOUT) {
4048 + evtimer_del(&heartbeat);
4049 + fpm_clock_get(&now);
4050 + fpm_pctl_check_request_timeout(&now);
4053 + evtimer_set(&heartbeat, &fpm_pctl_heartbeat, 0);
4055 + evtimer_add(&heartbeat, &tv);
4058 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_process_ctl.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_process_ctl.h
4059 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_process_ctl.h 1970-01-01 03:00:00.000000000 +0300
4060 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_process_ctl.h 2008-07-21 01:33:10.000000000 +0400
4064 + /* (c) 2007,2008 Andrei Nigmatulin */
4066 +#ifndef FPM_PROCESS_CTL_H
4067 +#define FPM_PROCESS_CTL_H 1
4069 +struct fpm_child_s;
4071 +void fpm_pctl(int new_state, int action);
4072 +int fpm_pctl_can_spawn_children();
4073 +int fpm_pctl_kill(pid_t pid, int how);
4074 +void fpm_pctl_heartbeat(int fd, short which, void *arg);
4075 +int fpm_pctl_child_exited();
4076 +int fpm_pctl_init_main();
4080 + FPM_PCTL_STATE_UNSPECIFIED,
4081 + FPM_PCTL_STATE_NORMAL,
4082 + FPM_PCTL_STATE_RELOADING,
4083 + FPM_PCTL_STATE_TERMINATING,
4084 + FPM_PCTL_STATE_FINISHING
4088 + FPM_PCTL_ACTION_SET,
4089 + FPM_PCTL_ACTION_TIMEOUT,
4090 + FPM_PCTL_ACTION_LAST_CHILD_EXITED
4101 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_request.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_request.c
4102 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_request.c 1970-01-01 03:00:00.000000000 +0300
4103 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_request.c 2008-09-01 03:34:36.000000000 +0400
4107 + /* (c) 2007,2008 Andrei Nigmatulin */
4109 +#include "fpm_config.h"
4111 +#include "fpm_php.h"
4112 +#include "fpm_str.h"
4113 +#include "fpm_clock.h"
4114 +#include "fpm_conf.h"
4115 +#include "fpm_trace.h"
4116 +#include "fpm_php_trace.h"
4117 +#include "fpm_process_ctl.h"
4118 +#include "fpm_children.h"
4119 +#include "fpm_shm_slots.h"
4120 +#include "fpm_request.h"
4124 +void fpm_request_accepting()
4126 + struct fpm_shm_slot_s *slot;
4128 + slot = fpm_shm_slots_acquire(0, 0);
4130 + slot->request_stage = FPM_REQUEST_ACCEPTING;
4132 + fpm_clock_get(&slot->tv);
4133 + memset(slot->request_method, 0, sizeof(slot->request_method));
4134 + slot->content_length = 0;
4135 + memset(slot->script_filename, 0, sizeof(slot->script_filename));
4137 + fpm_shm_slots_release(slot);
4140 +void fpm_request_reading_headers()
4142 + struct fpm_shm_slot_s *slot;
4144 + slot = fpm_shm_slots_acquire(0, 0);
4146 + slot->request_stage = FPM_REQUEST_READING_HEADERS;
4148 + fpm_clock_get(&slot->tv);
4149 + slot->accepted = slot->tv;
4151 + fpm_shm_slots_release(slot);
4154 +void fpm_request_info()
4156 + struct fpm_shm_slot_s *slot;
4157 + char *request_method = fpm_php_request_method();
4158 + char *script_filename = fpm_php_script_filename();
4160 + slot = fpm_shm_slots_acquire(0, 0);
4162 + slot->request_stage = FPM_REQUEST_INFO;
4164 + fpm_clock_get(&slot->tv);
4166 + if (request_method) {
4167 + cpystrn(slot->request_method, request_method, sizeof(slot->request_method));
4170 + slot->content_length = fpm_php_content_length();
4172 + /* if cgi.fix_pathinfo is set to "1" and script cannot be found (404)
4173 + the sapi_globals.request_info.path_translated is set to NULL */
4174 + if (script_filename) {
4175 + cpystrn(slot->script_filename, script_filename, sizeof(slot->script_filename));
4178 + fpm_shm_slots_release(slot);
4181 +void fpm_request_executing()
4183 + struct fpm_shm_slot_s *slot;
4185 + slot = fpm_shm_slots_acquire(0, 0);
4187 + slot->request_stage = FPM_REQUEST_EXECUTING;
4189 + fpm_clock_get(&slot->tv);
4191 + fpm_shm_slots_release(slot);
4194 +void fpm_request_finished()
4196 + struct fpm_shm_slot_s *slot;
4198 + slot = fpm_shm_slots_acquire(0, 0);
4200 + slot->request_stage = FPM_REQUEST_FINISHED;
4202 + fpm_clock_get(&slot->tv);
4203 + memset(&slot->accepted, 0, sizeof(slot->accepted));
4205 + fpm_shm_slots_release(slot);
4208 +void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *now, int terminate_timeout, int slowlog_timeout)
4210 + struct fpm_shm_slot_s *slot;
4211 + struct fpm_shm_slot_s slot_c;
4213 + slot = fpm_shm_slot(child);
4215 + if (!fpm_shm_slots_acquire(slot, 1)) {
4221 + fpm_shm_slots_release(slot);
4224 + if (child->slow_logged.tv_sec) {
4225 + if (child->slow_logged.tv_sec != slot_c.accepted.tv_sec || child->slow_logged.tv_usec != slot_c.accepted.tv_usec) {
4226 + child->slow_logged.tv_sec = 0;
4227 + child->slow_logged.tv_usec = 0;
4232 + if (slot_c.request_stage > FPM_REQUEST_ACCEPTING && slot_c.request_stage < FPM_REQUEST_FINISHED) {
4233 + char purified_script_filename[sizeof(slot_c.script_filename)];
4234 + struct timeval tv;
4236 + timersub(now, &slot_c.accepted, &tv);
4239 + if (child->slow_logged.tv_sec == 0 && slowlog_timeout &&
4240 + slot_c.request_stage == FPM_REQUEST_EXECUTING && tv.tv_sec >= slowlog_timeout) {
4242 + str_purify_filename(purified_script_filename, slot_c.script_filename, sizeof(slot_c.script_filename));
4244 + child->slow_logged = slot_c.accepted;
4245 + child->tracer = fpm_php_trace;
4247 + fpm_trace_signal(child->pid);
4249 + zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d, script '%s' (pool %s) executing too slow (%d.%06d sec), logging",
4250 + (int) child->pid, purified_script_filename, child->wp->config->name, (int) tv.tv_sec, (int) tv.tv_usec);
4255 + if (terminate_timeout && tv.tv_sec >= terminate_timeout) {
4257 + str_purify_filename(purified_script_filename, slot_c.script_filename, sizeof(slot_c.script_filename));
4259 + fpm_pctl_kill(child->pid, FPM_PCTL_TERM);
4261 + zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d, script '%s' (pool %s) execution timed out (%d.%06d sec), terminating",
4262 + (int) child->pid, purified_script_filename, child->wp->config->name, (int) tv.tv_sec, (int) tv.tv_usec);
4268 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_request.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_request.h
4269 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_request.h 1970-01-01 03:00:00.000000000 +0300
4270 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_request.h 2008-07-20 05:47:16.000000000 +0400
4274 + /* (c) 2007,2008 Andrei Nigmatulin */
4276 +#ifndef FPM_REQUEST_H
4277 +#define FPM_REQUEST_H 1
4279 +void fpm_request_accepting(); /* hanging in accept() */
4280 +void fpm_request_reading_headers(); /* start reading fastcgi request from very first byte */
4281 +void fpm_request_info(); /* not a stage really but a point in the php code, where all request params have become known to sapi */
4282 +void fpm_request_executing(); /* the script is executing */
4283 +void fpm_request_finished(); /* request processed: script response have been sent to web server */
4285 +struct fpm_child_s;
4288 +void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *tv, int terminate_timeout, int slowlog_timeout);
4290 +enum fpm_request_stage_e {
4291 + FPM_REQUEST_ACCEPTING = 1,
4292 + FPM_REQUEST_READING_HEADERS,
4294 + FPM_REQUEST_EXECUTING,
4295 + FPM_REQUEST_FINISHED
4299 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm.c
4300 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm.c 1970-01-01 03:00:00.000000000 +0300
4301 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm.c 2008-05-24 21:38:47.000000000 +0400
4305 + /* (c) 2007,2008 Andrei Nigmatulin */
4307 +#include "fpm_config.h"
4309 +#include <unistd.h>
4310 +#include <sys/mman.h>
4311 +#include <stdlib.h>
4313 +#include "fpm_shm.h"
4317 +/* MAP_ANON is depricated, but not in macosx */
4318 +#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
4319 +#define MAP_ANONYMOUS MAP_ANON
4323 +struct fpm_shm_s *fpm_shm_alloc(size_t sz)
4325 + struct fpm_shm_s *shm;
4327 + shm = malloc(sizeof(*shm));
4333 + shm->mem = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
4336 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "mmap(MAP_ANONYMOUS | MAP_SHARED) failed");
4347 +static void fpm_shm_free(struct fpm_shm_s *shm, int do_unmap)
4350 + munmap(shm->mem, shm->sz);
4356 +void fpm_shm_free_list(struct fpm_shm_s *shm, void *mem)
4358 + struct fpm_shm_s *next;
4360 + for (; shm; shm = next) {
4363 + fpm_shm_free(shm, mem != shm->mem);
4367 +void *fpm_shm_alloc_chunk(struct fpm_shm_s **head, size_t sz, void **mem)
4369 + size_t pagesize = getpagesize();
4370 + static const size_t cache_line_size = 16;
4371 + size_t aligned_sz;
4372 + struct fpm_shm_s *shm;
4375 + sz = (sz + cache_line_size - 1) & -cache_line_size;
4379 + if (0 == shm || shm->sz - shm->used < sz) {
4380 + /* allocate one more shm segment */
4382 + aligned_sz = (sz + pagesize - 1) & -pagesize;
4384 + shm = fpm_shm_alloc(aligned_sz);
4390 + shm->next = *head;
4391 + if (shm->next) shm->next->prev = shm;
4397 + ret = (char *) shm->mem + shm->used;
4403 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm.h
4404 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm.h 1970-01-01 03:00:00.000000000 +0300
4405 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm.h 2008-05-24 21:38:47.000000000 +0400
4409 + /* (c) 2007,2008 Andrei Nigmatulin */
4412 +#define FPM_SHM_H 1
4417 + struct fpm_shm_s *prev, *next;
4423 +struct fpm_shm_s *fpm_shm_alloc(size_t sz);
4424 +void fpm_shm_free_list(struct fpm_shm_s *, void *);
4425 +void *fpm_shm_alloc_chunk(struct fpm_shm_s **head, size_t sz, void **mem);
4429 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm_slots.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm_slots.c
4430 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm_slots.c 1970-01-01 03:00:00.000000000 +0300
4431 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm_slots.c 2008-05-24 21:38:47.000000000 +0400
4435 + /* (c) 2007,2008 Andrei Nigmatulin */
4437 +#include "fpm_config.h"
4439 +#include "fpm_atomic.h"
4440 +#include "fpm_worker_pool.h"
4441 +#include "fpm_children.h"
4442 +#include "fpm_shm.h"
4443 +#include "fpm_shm_slots.h"
4446 +static void *shm_mem;
4447 +static struct fpm_shm_slot_s *shm_slot;
4449 +int fpm_shm_slots_prepare_slot(struct fpm_child_s *child)
4451 + struct fpm_worker_pool_s *wp = child->wp;
4452 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4454 + child->shm_slot_i = wp->slots_used.used;
4456 + shm_slot_ptr = fpm_array_push(&wp->slots_used);
4458 + if (0 == shm_slot_ptr) {
4462 + if (0 == wp->slots_free.used) {
4463 + shm_slot_ptr->shm_slot = fpm_shm_alloc_chunk(&wp->shm_list, sizeof(struct fpm_shm_slot_s), &shm_slot_ptr->mem);
4465 + if (!shm_slot_ptr->shm_slot) {
4470 + *shm_slot_ptr = *(struct fpm_shm_slot_ptr_s *) fpm_array_item_last(&wp->slots_free);
4472 + --wp->slots_free.used;
4475 + memset(shm_slot_ptr->shm_slot, 0, sizeof(struct fpm_shm_slot_s));
4477 + shm_slot_ptr->child = child;
4482 +void fpm_shm_slots_discard_slot(struct fpm_child_s *child)
4484 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4485 + struct fpm_worker_pool_s *wp = child->wp;
4488 + shm_slot_ptr = fpm_array_push(&wp->slots_free);
4490 + if (shm_slot_ptr) {
4492 + struct fpm_shm_slot_ptr_s *shm_slot_ptr_used;
4494 + shm_slot_ptr_used = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4496 + *shm_slot_ptr = *shm_slot_ptr_used;
4498 + shm_slot_ptr->child = 0;
4502 + n = fpm_array_item_remove(&wp->slots_used, child->shm_slot_i);
4505 + shm_slot_ptr = fpm_array_item(&wp->slots_used, n);
4507 + shm_slot_ptr->child->shm_slot_i = n;
4511 +void fpm_shm_slots_child_use_slot(struct fpm_child_s *child)
4513 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4514 + struct fpm_worker_pool_s *wp = child->wp;
4516 + shm_slot_ptr = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4518 + shm_slot = shm_slot_ptr->shm_slot;
4519 + shm_mem = shm_slot_ptr->mem;
4522 +void fpm_shm_slots_parent_use_slot(struct fpm_child_s *child)
4524 + /* nothing to do */
4527 +void *fpm_shm_slots_mem()
4532 +struct fpm_shm_slot_s *fpm_shm_slot(struct fpm_child_s *child)
4534 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4535 + struct fpm_worker_pool_s *wp = child->wp;
4537 + shm_slot_ptr = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4539 + return shm_slot_ptr->shm_slot;
4542 +struct fpm_shm_slot_s *fpm_shm_slots_acquire(struct fpm_shm_slot_s *s, int nohang)
4548 + if (0 > fpm_spinlock(&s->lock, nohang)) {
4555 +void fpm_shm_slots_release(struct fpm_shm_slot_s *s)
4560 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm_slots.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm_slots.h
4561 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm_slots.h 1970-01-01 03:00:00.000000000 +0300
4562 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm_slots.h 2008-05-24 21:38:47.000000000 +0400
4566 + /* (c) 2007,2008 Andrei Nigmatulin */
4568 +#ifndef FPM_SHM_SLOTS_H
4569 +#define FPM_SHM_SLOTS_H 1
4571 +#include "fpm_atomic.h"
4572 +#include "fpm_worker_pool.h"
4573 +#include "fpm_request.h"
4575 +struct fpm_child_s;
4577 +struct fpm_shm_slot_s {
4582 + enum fpm_request_stage_e request_stage;
4583 + struct timeval accepted;
4584 + struct timeval tv;
4585 + char request_method[16];
4586 + size_t content_length; /* used with POST only */
4587 + char script_filename[256];
4590 +struct fpm_shm_slot_ptr_s {
4592 + struct fpm_shm_slot_s *shm_slot;
4593 + struct fpm_child_s *child;
4596 +int fpm_shm_slots_prepare_slot(struct fpm_child_s *child);
4597 +void fpm_shm_slots_discard_slot(struct fpm_child_s *child);
4598 +void fpm_shm_slots_child_use_slot(struct fpm_child_s *child);
4599 +void fpm_shm_slots_parent_use_slot(struct fpm_child_s *child);
4600 +void *fpm_shm_slots_mem();
4601 +struct fpm_shm_slot_s *fpm_shm_slot(struct fpm_child_s *child);
4602 +struct fpm_shm_slot_s *fpm_shm_slots_acquire(struct fpm_shm_slot_s *, int nohang);
4603 +void fpm_shm_slots_release(struct fpm_shm_slot_s *);
4607 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_signals.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_signals.c
4608 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_signals.c 1970-01-01 03:00:00.000000000 +0300
4609 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_signals.c 2008-08-26 19:09:15.000000000 +0400
4613 + /* (c) 2007,2008 Andrei Nigmatulin */
4615 +#include "fpm_config.h"
4617 +#include <signal.h>
4619 +#include <sys/types.h>
4620 +#include <sys/socket.h>
4621 +#include <stdlib.h>
4622 +#include <string.h>
4624 +#include <unistd.h>
4628 +#include "fpm_signals.h"
4629 +#include "fpm_sockets.h"
4630 +#include "fpm_php.h"
4635 +const char *fpm_signal_names[NSIG + 1] = {
4637 + [SIGHUP] = "SIGHUP",
4640 + [SIGINT] = "SIGINT",
4643 + [SIGQUIT] = "SIGQUIT",
4646 + [SIGILL] = "SIGILL",
4649 + [SIGTRAP] = "SIGTRAP",
4652 + [SIGABRT] = "SIGABRT",
4655 + [SIGEMT] = "SIGEMT",
4658 + [SIGBUS] = "SIGBUS",
4661 + [SIGFPE] = "SIGFPE",
4664 + [SIGKILL] = "SIGKILL",
4667 + [SIGUSR1] = "SIGUSR1",
4670 + [SIGSEGV] = "SIGSEGV",
4673 + [SIGUSR2] = "SIGUSR2",
4676 + [SIGPIPE] = "SIGPIPE",
4679 + [SIGALRM] = "SIGALRM",
4682 + [SIGTERM] = "SIGTERM",
4685 + [SIGCHLD] = "SIGCHLD",
4688 + [SIGCONT] = "SIGCONT",
4691 + [SIGSTOP] = "SIGSTOP",
4694 + [SIGTSTP] = "SIGTSTP",
4697 + [SIGTTIN] = "SIGTTIN",
4700 + [SIGTTOU] = "SIGTTOU",
4703 + [SIGURG] = "SIGURG",
4706 + [SIGXCPU] = "SIGXCPU",
4709 + [SIGXFSZ] = "SIGXFSZ",
4712 + [SIGVTALRM] = "SIGVTALRM",
4715 + [SIGPROF] = "SIGPROF",
4718 + [SIGWINCH] = "SIGWINCH",
4721 + [SIGINFO] = "SIGINFO",
4724 + [SIGIO] = "SIGIO",
4727 + [SIGPWR] = "SIGPWR",
4730 + [SIGSYS] = "SIGSYS",
4733 + [SIGWAITING] = "SIGWAITING",
4736 + [SIGLWP] = "SIGLWP",
4739 + [SIGFREEZE] = "SIGFREEZE",
4742 + [SIGTHAW] = "SIGTHAW",
4745 + [SIGCANCEL] = "SIGCANCEL",
4748 + [SIGLOST] = "SIGLOST",
4752 +static void sig_soft_quit(int signo)
4754 + int saved_errno = errno;
4756 + /* closing fastcgi listening socket will force fcgi_accept() exit immediately */
4758 + socket(AF_UNIX, SOCK_STREAM, 0);
4760 + fpm_php_soft_quit();
4762 + errno = saved_errno;
4765 +static void sig_handler(int signo)
4767 + static const char sig_chars[NSIG + 1] = {
4778 + if (fpm_globals.parent_pid != getpid()) {
4779 + /* prevent a signal race condition when child process
4780 + have not set up it's own signal handler yet */
4784 + saved_errno = errno;
4786 + s = sig_chars[signo];
4788 + write(sp[1], &s, sizeof(s));
4790 + errno = saved_errno;
4793 +int fpm_signals_init_main()
4795 + struct sigaction act;
4797 + if (0 > socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) {
4798 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "socketpair() failed");
4802 + if (0 > fd_set_blocked(sp[0], 0) || 0 > fd_set_blocked(sp[1], 0)) {
4803 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fd_set_blocked() failed");
4807 + if (0 > fcntl(sp[0], F_SETFD, FD_CLOEXEC) || 0 > fcntl(sp[1], F_SETFD, FD_CLOEXEC)) {
4808 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fcntl(F_SETFD, FD_CLOEXEC) failed");
4812 + memset(&act, 0, sizeof(act));
4813 + act.sa_handler = sig_handler;
4814 + sigfillset(&act.sa_mask);
4816 + if (0 > sigaction(SIGTERM, &act, 0) ||
4817 + 0 > sigaction(SIGINT, &act, 0) ||
4818 + 0 > sigaction(SIGUSR1, &act, 0) ||
4819 + 0 > sigaction(SIGUSR2, &act, 0) ||
4820 + 0 > sigaction(SIGCHLD, &act, 0) ||
4821 + 0 > sigaction(SIGQUIT, &act, 0)) {
4823 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "sigaction() failed");
4830 +int fpm_signals_init_child()
4832 + struct sigaction act, act_dfl;
4834 + memset(&act, 0, sizeof(act));
4835 + memset(&act_dfl, 0, sizeof(act_dfl));
4837 + act.sa_handler = &sig_soft_quit;
4838 + act.sa_flags |= SA_RESTART;
4840 + act_dfl.sa_handler = SIG_DFL;
4845 + if (0 > sigaction(SIGTERM, &act_dfl, 0) ||
4846 + 0 > sigaction(SIGINT, &act_dfl, 0) ||
4847 + 0 > sigaction(SIGUSR1, &act_dfl, 0) ||
4848 + 0 > sigaction(SIGUSR2, &act_dfl, 0) ||
4849 + 0 > sigaction(SIGCHLD, &act_dfl, 0) ||
4850 + 0 > sigaction(SIGQUIT, &act, 0)) {
4852 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "sigaction() failed");
4859 +int fpm_signals_get_fd()
4863 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_signals.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_signals.h
4864 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_signals.h 1970-01-01 03:00:00.000000000 +0300
4865 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_signals.h 2008-05-24 21:38:47.000000000 +0400
4869 + /* (c) 2007,2008 Andrei Nigmatulin */
4871 +#ifndef FPM_SIGNALS_H
4872 +#define FPM_SIGNALS_H 1
4874 +#include <signal.h>
4876 +int fpm_signals_init_main();
4877 +int fpm_signals_init_child();
4878 +int fpm_signals_get_fd();
4880 +extern const char *fpm_signal_names[NSIG + 1];
4883 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_sockets.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_sockets.c
4884 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_sockets.c 1970-01-01 03:00:00.000000000 +0300
4885 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_sockets.c 2008-08-26 19:09:15.000000000 +0400
4889 + /* (c) 2007,2008 Andrei Nigmatulin */
4891 +#include "fpm_config.h"
4893 +#ifdef HAVE_ALLOCA_H
4894 +#include <alloca.h>
4896 +#include <sys/types.h>
4897 +#include <sys/stat.h> /* for chmod(2) */
4898 +#include <sys/socket.h>
4899 +#include <netinet/in.h>
4900 +#include <arpa/inet.h>
4901 +#include <sys/un.h>
4904 +#include <stdlib.h>
4905 +#include <string.h>
4907 +#include <unistd.h>
4910 +#include "fpm_arrays.h"
4911 +#include "fpm_sockets.h"
4912 +#include "fpm_worker_pool.h"
4913 +#include "fpm_unix.h"
4914 +#include "fpm_str.h"
4915 +#include "fpm_env.h"
4916 +#include "fpm_cleanup.h"
4918 +struct listening_socket_s {
4925 +static struct fpm_array_s sockets_list;
4927 +static int fpm_sockets_resolve_af_inet(char *node, char *service, struct sockaddr_in *addr)
4929 + struct addrinfo *res;
4930 + struct addrinfo hints;
4933 + memset(&hints, 0, sizeof(hints));
4935 + hints.ai_family = AF_INET;
4937 + ret = getaddrinfo(node, service, &hints, &res);
4940 + zlog(ZLOG_STUFF, ZLOG_ERROR, "can't resolve hostname '%s%s%s': getaddrinfo said: %s%s%s\n",
4941 + node, service ? ":" : "", service ? service : "",
4942 + gai_strerror(ret), ret == EAI_SYSTEM ? ", system error: " : "", ret == EAI_SYSTEM ? strerror(errno) : "");
4946 + *addr = *(struct sockaddr_in *) res->ai_addr;
4948 + freeaddrinfo(res);
4953 +enum { FPM_GET_USE_SOCKET = 1, FPM_STORE_SOCKET = 2, FPM_STORE_USE_SOCKET = 3 };
4955 +static void fpm_sockets_cleanup(int which, void *arg)
4958 + char *env_value = 0;
4960 + struct listening_socket_s *ls = sockets_list.data;
4962 + for (i = 0; i < sockets_list.used; i++, ls++) {
4964 + if (which != FPM_CLEANUP_PARENT_EXEC) {
4969 + else { /* on PARENT EXEC we want socket fds to be inherited through environment variable */
4971 + sprintf(fd, "%d", ls->sock);
4972 + env_value = realloc(env_value, p + (p ? 1 : 0) + strlen(ls->key) + 1 + strlen(fd) + 1);
4973 + p += sprintf(env_value + p, "%s%s=%s", p ? "," : "", ls->key, fd);
4976 + if (which == FPM_CLEANUP_PARENT_EXIT_MAIN) {
4978 + if (ls->type == FPM_AF_UNIX) {
4988 + setenv("FPM_SOCKETS", env_value, 1);
4992 + fpm_array_free(&sockets_list);
4995 +static int fpm_sockets_hash_op(int sock, struct sockaddr *sa, char *key, int type, int op)
4998 + if (key == NULL) {
5002 + case FPM_AF_INET : {
5003 + struct sockaddr_in *sa_in = (struct sockaddr_in *) sa;
5005 + key = alloca(sizeof("xxx.xxx.xxx.xxx:ppppp"));
5007 + sprintf(key, "%u.%u.%u.%u:%u", IPQUAD(&sa_in->sin_addr), (unsigned int) ntohs(sa_in->sin_port));
5012 + case FPM_AF_UNIX : {
5013 + struct sockaddr_un *sa_un = (struct sockaddr_un *) sa;
5015 + key = alloca(strlen(sa_un->sun_path) + 1);
5017 + strcpy(key, sa_un->sun_path);
5031 + case FPM_GET_USE_SOCKET :
5035 + struct listening_socket_s *ls = sockets_list.data;
5037 + for (i = 0; i < sockets_list.used; i++, ls++) {
5039 + if (!strcmp(ls->key, key)) {
5048 + case FPM_STORE_SOCKET : /* inherited socket */
5049 + case FPM_STORE_USE_SOCKET : /* just created */
5052 + struct listening_socket_s *ls;
5054 + ls = fpm_array_push(&sockets_list);
5060 + if (op == FPM_STORE_SOCKET) {
5068 + ls->key = strdup(key);
5079 +static int fpm_sockets_new_listening_socket(struct fpm_worker_pool_s *wp, struct sockaddr *sa, int socklen)
5084 + mode_t saved_umask;
5086 + /* we have custom backlog value */
5087 + if (wp->config->listen_options) {
5088 + backlog = wp->config->listen_options->backlog;
5091 + sock = socket(sa->sa_family, SOCK_STREAM, 0);
5094 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "socket() failed");
5098 + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
5100 + if (wp->listen_address_domain == FPM_AF_UNIX) {
5101 + unlink( ((struct sockaddr_un *) sa)->sun_path);
5104 + saved_umask = umask(0777 ^ wp->socket_mode);
5106 + if (0 > bind(sock, sa, socklen)) {
5107 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "bind() for address '%s' failed", wp->config->listen_address);
5111 + if (wp->listen_address_domain == FPM_AF_UNIX) {
5113 + char *path = ((struct sockaddr_un *) sa)->sun_path;
5115 + if (wp->socket_uid != -1 || wp->socket_gid != -1) {
5117 + if (0 > chown(path, wp->socket_uid, wp->socket_gid)) {
5118 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chown() for address '%s' failed", wp->config->listen_address);
5126 + umask(saved_umask);
5128 + if (0 > listen(sock, backlog)) {
5129 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "listen() for address '%s' failed", wp->config->listen_address);
5136 +static int fpm_sockets_get_listening_socket(struct fpm_worker_pool_s *wp, struct sockaddr *sa, int socklen)
5140 + sock = fpm_sockets_hash_op(0, sa, 0, wp->listen_address_domain, FPM_GET_USE_SOCKET);
5142 + if (sock >= 0) return sock;
5144 + sock = fpm_sockets_new_listening_socket(wp, sa, socklen);
5146 + fpm_sockets_hash_op(sock, sa, 0, wp->listen_address_domain, FPM_STORE_USE_SOCKET);
5151 +enum fpm_address_domain fpm_sockets_domain_from_address(char *address)
5153 + if (strchr(address, ':')) return FPM_AF_INET;
5155 + if (strlen(address) == strspn(address, "0123456789")) return FPM_AF_INET;
5157 + return FPM_AF_UNIX;
5160 +static int fpm_socket_af_inet_listening_socket(struct fpm_worker_pool_s *wp)
5162 + struct sockaddr_in sa_in;
5163 + char *dup_address = strdup(wp->config->listen_address);
5164 + char *port_str = strchr(dup_address, ':');
5165 + char *addr = NULL;
5168 + if (port_str) { /* this is host:port pair */
5169 + *port_str++ = '\0';
5170 + port = atoi(port_str);
5171 + addr = dup_address;
5173 + else if (strlen(dup_address) == strspn(dup_address, "0123456789")) { /* this is port */
5174 + port = atoi(dup_address);
5175 + port_str = dup_address;
5179 + zlog(ZLOG_STUFF, ZLOG_ERROR, "invalid port value '%s'", port_str);
5183 + memset(&sa_in, 0, sizeof(sa_in));
5187 + sa_in.sin_addr.s_addr = inet_addr(addr);
5189 + if (sa_in.sin_addr.s_addr == INADDR_NONE) { /* do resolve */
5190 + if (0 > fpm_sockets_resolve_af_inet(addr, NULL, &sa_in)) {
5193 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "address '%s' resolved as %u.%u.%u.%u", addr, IPQUAD(&sa_in.sin_addr));
5198 + sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
5202 + sa_in.sin_family = AF_INET;
5203 + sa_in.sin_port = htons(port);
5205 + free(dup_address);
5207 + return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_in, sizeof(struct sockaddr_in));
5210 +static int fpm_socket_af_unix_listening_socket(struct fpm_worker_pool_s *wp)
5212 + struct sockaddr_un sa_un;
5214 + memset(&sa_un, 0, sizeof(sa_un));
5216 + cpystrn(sa_un.sun_path, wp->config->listen_address, sizeof(sa_un.sun_path));
5217 + sa_un.sun_family = AF_UNIX;
5219 + return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_un, sizeof(struct sockaddr_un));
5222 +int fpm_sockets_init_main()
5225 + struct fpm_worker_pool_s *wp;
5226 + char *inherited = getenv("FPM_SOCKETS");
5227 + struct listening_socket_s *ls;
5229 + if (0 == fpm_array_init(&sockets_list, sizeof(struct listening_socket_s), 10)) {
5233 + /* import inherited sockets */
5234 + while (inherited && *inherited) {
5235 + char *comma = strchr(inherited, ',');
5239 + if (comma) *comma = '\0';
5241 + eq = strchr(inherited, '=');
5246 + fd_no = atoi(eq + 1);
5248 + type = fpm_sockets_domain_from_address(inherited);
5250 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "using inherited socket fd=%d, \"%s\"", fd_no, inherited);
5252 + fpm_sockets_hash_op(fd_no, 0, inherited, type, FPM_STORE_SOCKET);
5255 + if (comma) inherited = comma + 1;
5256 + else inherited = 0;
5259 + /* create all required sockets */
5260 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
5262 + if (!wp->is_template) {
5264 + switch (wp->listen_address_domain) {
5266 + case FPM_AF_INET :
5268 + wp->listening_socket = fpm_socket_af_inet_listening_socket(wp);
5271 + case FPM_AF_UNIX :
5273 + if (0 > fpm_unix_resolve_socket_premissions(wp)) {
5277 + wp->listening_socket = fpm_socket_af_unix_listening_socket(wp);
5282 + if (wp->listening_socket == -1) {
5289 + /* close unused sockets that was inherited */
5290 + ls = sockets_list.data;
5292 + for (i = 0; i < sockets_list.used; ) {
5294 + if (ls->refcount == 0) {
5296 + if (ls->type == FPM_AF_UNIX) {
5300 + fpm_array_item_remove(&sockets_list, i);
5308 + fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_sockets_cleanup, 0);
5312 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_sockets.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_sockets.h
5313 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_sockets.h 1970-01-01 03:00:00.000000000 +0300
5314 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_sockets.h 2008-08-26 19:09:15.000000000 +0400
5318 + /* (c) 2007,2008 Andrei Nigmatulin */
5321 +#define FPM_MISC_H 1
5323 +#include <unistd.h>
5326 +#include "fpm_worker_pool.h"
5328 +enum fpm_address_domain fpm_sockets_domain_from_address(char *addr);
5329 +int fpm_sockets_init_main();
5332 +static inline int fd_set_blocked(int fd, int blocked)
5334 + int flags = fcntl(fd, F_GETFL);
5336 + if (flags < 0) return -1;
5339 + flags &= ~O_NONBLOCK;
5341 + flags |= O_NONBLOCK;
5343 + return fcntl(fd, F_SETFL, flags);
5346 +#define IPQUAD(sin_addr) \
5347 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[0], \
5348 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[1], \
5349 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[2], \
5350 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[3]
5353 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_stdio.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_stdio.c
5354 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_stdio.c 1970-01-01 03:00:00.000000000 +0300
5355 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_stdio.c 2008-05-24 21:38:47.000000000 +0400
5359 + /* (c) 2007,2008 Andrei Nigmatulin */
5361 +#include "fpm_config.h"
5363 +#include <sys/types.h>
5364 +#include <sys/stat.h>
5365 +#include <string.h>
5367 +#include <unistd.h>
5371 +#include "fpm_children.h"
5372 +#include "fpm_events.h"
5373 +#include "fpm_sockets.h"
5374 +#include "fpm_stdio.h"
5377 +static int fd_stdout[2];
5378 +static int fd_stderr[2];
5380 +int fpm_stdio_init_main()
5382 + int fd = open("/dev/null", O_RDWR);
5385 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"/dev/null\") failed");
5389 + dup2(fd, STDIN_FILENO);
5390 + dup2(fd, STDOUT_FILENO);
5396 +int fpm_stdio_init_final()
5398 + if (fpm_global_options.daemonize) {
5400 + if (fpm_globals.error_log_fd != STDERR_FILENO) {
5401 + /* there might be messages to stderr from libevent, we need to log them all */
5402 + dup2(fpm_globals.error_log_fd, STDERR_FILENO);
5405 + zlog_set_level(fpm_globals.log_level);
5407 + zlog_set_fd(fpm_globals.error_log_fd);
5413 +int fpm_stdio_init_child(struct fpm_worker_pool_s *wp)
5415 + close(fpm_globals.error_log_fd);
5416 + fpm_globals.error_log_fd = -1;
5419 + if (wp->listening_socket != STDIN_FILENO) {
5420 + dup2(wp->listening_socket, STDIN_FILENO);
5426 +static void fpm_stdio_child_said(int fd, short which, void *arg)
5428 + static const int max_buf_size = 1024;
5429 + char buf[max_buf_size];
5430 + struct fpm_child_s *child = arg;
5431 + int is_stdout = fd == child->fd_stdout;
5432 + struct event *ev = is_stdout ? &child->ev_stdout : &child->ev_stderr;
5433 + int fifo_in = 1, fifo_out = 1;
5434 + int is_last_message = 0;
5439 + zlog(ZLOG_STUFF, ZLOG_DEBUG, "child %d said %s", (int) child->pid, is_stdout ? "stdout" : "stderr");
5442 + while (fifo_in || fifo_out) {
5446 + res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf);
5448 + if (res <= 0) { /* no data */
5451 + if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
5452 + /* just no more data ready */
5454 + else { /* error or pipe is closed */
5456 + if (res < 0) { /* error */
5457 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
5460 + fpm_event_del(ev);
5461 + is_last_message = 1;
5464 + close(child->fd_stdout);
5465 + child->fd_stdout = -1;
5468 + close(child->fd_stderr);
5469 + child->fd_stderr = -1;
5473 + if (in_buf == 0 && !fpm_globals.is_child) {
5474 + zlog(ZLOG_STUFF, ZLOG_DEBUG, "child %d (pool %s) %s pipe is closed", (int) child->pid,
5475 + child->wp->config->name, is_stdout ? "stdout" : "stderr");
5486 + if (in_buf == 0) {
5491 + int should_print = 0;
5492 + buf[in_buf] = '\0';
5494 + /* FIXME: there might be binary data */
5496 + /* we should print if no more space in the buffer */
5497 + if (in_buf == max_buf_size - 1) {
5501 + /* we should print if no more data to come */
5506 + nl = strchr(buf, '\n');
5508 + if (nl || should_print) {
5514 + zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d (pool %s) said into %s: \"%s\"%s", (int) child->pid,
5515 + child->wp->config->name, is_stdout ? "stdout" : "stderr", buf, is_last_message ? ", pipe is closed" : "");
5518 + int out_buf = 1 + nl - buf;
5519 + memmove(buf, buf + out_buf, in_buf - out_buf);
5520 + in_buf -= out_buf;
5532 +int fpm_stdio_prepare_pipes(struct fpm_child_s *child)
5534 + if (0 == child->wp->config->catch_workers_output) { /* not required */
5538 + if (0 > pipe(fd_stdout)) {
5539 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
5543 + if (0 > pipe(fd_stderr)) {
5544 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
5545 + close(fd_stdout[0]); close(fd_stdout[1]);
5549 + if (0 > fd_set_blocked(fd_stdout[0], 0) || 0 > fd_set_blocked(fd_stderr[0], 0)) {
5550 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fd_set_blocked() failed");
5551 + close(fd_stdout[0]); close(fd_stdout[1]);
5552 + close(fd_stderr[0]); close(fd_stderr[1]);
5559 +int fpm_stdio_parent_use_pipes(struct fpm_child_s *child)
5561 + if (0 == child->wp->config->catch_workers_output) { /* not required */
5565 + close(fd_stdout[1]);
5566 + close(fd_stderr[1]);
5568 + child->fd_stdout = fd_stdout[0];
5569 + child->fd_stderr = fd_stderr[0];
5571 + fpm_event_add(child->fd_stdout, &child->ev_stdout, fpm_stdio_child_said, child);
5572 + fpm_event_add(child->fd_stderr, &child->ev_stderr, fpm_stdio_child_said, child);
5577 +int fpm_stdio_discard_pipes(struct fpm_child_s *child)
5579 + if (0 == child->wp->config->catch_workers_output) { /* not required */
5583 + close(fd_stdout[1]);
5584 + close(fd_stderr[1]);
5586 + close(fd_stdout[0]);
5587 + close(fd_stderr[0]);
5592 +void fpm_stdio_child_use_pipes(struct fpm_child_s *child)
5594 + if (child->wp->config->catch_workers_output) {
5595 + dup2(fd_stdout[1], STDOUT_FILENO);
5596 + dup2(fd_stderr[1], STDERR_FILENO);
5597 + close(fd_stdout[0]); close(fd_stdout[1]);
5598 + close(fd_stderr[0]); close(fd_stderr[1]);
5601 + /* stdout of parent is always /dev/null */
5602 + dup2(STDOUT_FILENO, STDERR_FILENO);
5606 +int fpm_stdio_open_error_log(int reopen)
5610 + fd = open(fpm_global_options.error_log, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
5613 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"%s\") failed", fpm_global_options.error_log);
5618 + if (fpm_global_options.daemonize) {
5619 + dup2(fd, STDERR_FILENO);
5622 + dup2(fd, fpm_globals.error_log_fd);
5624 + fd = fpm_globals.error_log_fd; /* for FD_CLOSEXEC to work */
5627 + fpm_globals.error_log_fd = fd;
5630 + fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
5634 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_stdio.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_stdio.h
5635 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_stdio.h 1970-01-01 03:00:00.000000000 +0300
5636 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_stdio.h 2008-05-24 21:38:47.000000000 +0400
5640 + /* (c) 2007,2008 Andrei Nigmatulin */
5642 +#ifndef FPM_STDIO_H
5643 +#define FPM_STDIO_H 1
5645 +#include "fpm_worker_pool.h"
5647 +int fpm_stdio_init_main();
5648 +int fpm_stdio_init_final();
5649 +int fpm_stdio_init_child(struct fpm_worker_pool_s *wp);
5650 +int fpm_stdio_prepare_pipes(struct fpm_child_s *child);
5651 +void fpm_stdio_child_use_pipes(struct fpm_child_s *child);
5652 +int fpm_stdio_parent_use_pipes(struct fpm_child_s *child);
5653 +int fpm_stdio_discard_pipes(struct fpm_child_s *child);
5654 +int fpm_stdio_open_error_log(int reopen);
5658 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_str.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_str.h
5659 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_str.h 1970-01-01 03:00:00.000000000 +0300
5660 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_str.h 2008-05-24 21:38:47.000000000 +0400
5664 + /* (c) 2007,2008 Andrei Nigmatulin */
5667 +#define FPM_STR_H 1
5669 +static inline char *cpystrn(char *dst, const char *src, size_t dst_size)
5673 + if (!dst_size) return dst;
5676 + end = dst + dst_size - 1;
5678 + for (; d < end; ++d, ++src) {
5679 + if (!(*d = *src)) {
5689 +static inline char *str_purify_filename(char *dst, char *src, size_t size)
5694 + end = dst + size - 1;
5696 + for (; d < end && *src; ++d, ++src) {
5697 + if (* (unsigned char *) src < ' ' || * (unsigned char *) src > '\x7f') {
5711 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace.c
5712 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace.c 1970-01-01 03:00:00.000000000 +0300
5713 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace.c 2008-07-21 00:59:00.000000000 +0400
5717 + /* (c) 2007,2008 Andrei Nigmatulin */
5719 +#include "fpm_config.h"
5721 +#include <sys/types.h>
5723 +#include "fpm_trace.h"
5725 +int fpm_trace_get_strz(char *buf, size_t sz, long addr)
5729 + char *lc = (char *) &l;
5731 + if (0 > fpm_trace_get_long(addr, &l)) {
5735 + i = l % SIZEOF_LONG;
5739 + for (addr = l; ; addr += SIZEOF_LONG) {
5741 + if (0 > fpm_trace_get_long(addr, &l)) {
5745 + for ( ; i < SIZEOF_LONG; i++) {
5748 + if (sz && lc[i]) {
5761 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace.h
5762 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace.h 1970-01-01 03:00:00.000000000 +0300
5763 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace.h 2008-07-21 07:04:25.000000000 +0400
5767 + /* (c) 2007,2008 Andrei Nigmatulin */
5769 +#ifndef FPM_TRACE_H
5770 +#define FPM_TRACE_H 1
5772 +#include <unistd.h>
5774 +int fpm_trace_signal(pid_t pid);
5775 +int fpm_trace_ready(pid_t pid);
5776 +int fpm_trace_close(pid_t pid);
5777 +int fpm_trace_get_long(long addr, long *data);
5778 +int fpm_trace_get_strz(char *buf, size_t sz, long addr);
5782 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace_mach.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace_mach.c
5783 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace_mach.c 1970-01-01 03:00:00.000000000 +0300
5784 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace_mach.c 2008-08-26 19:09:15.000000000 +0400
5788 + /* (c) 2007,2008 Andrei Nigmatulin */
5790 +#include "fpm_config.h"
5792 +#include <mach/mach.h>
5793 +#include <mach/mach_vm.h>
5795 +#include <unistd.h>
5797 +#include "fpm_trace.h"
5798 +#include "fpm_process_ctl.h"
5799 +#include "fpm_unix.h"
5803 +static mach_port_name_t target;
5804 +static vm_offset_t target_page_base;
5805 +static vm_offset_t local_page;
5806 +static mach_msg_type_number_t local_size;
5808 +static void fpm_mach_vm_deallocate()
5811 + mach_vm_deallocate(mach_task_self(), local_page, local_size);
5812 + target_page_base = 0;
5818 +static int fpm_mach_vm_read_page(vm_offset_t page)
5822 + kr = mach_vm_read(target, page, fpm_pagesize, &local_page, &local_size);
5824 + if (kr != KERN_SUCCESS) {
5825 + zlog(ZLOG_STUFF, ZLOG_ERROR, "mach_vm_read() failed: %s (%d)", mach_error_string(kr), kr);
5832 +int fpm_trace_signal(pid_t pid)
5834 + if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
5835 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "kill(SIGSTOP) failed");
5842 +int fpm_trace_ready(pid_t pid)
5846 + kr = task_for_pid(mach_task_self(), pid, &target);
5848 + if (kr != KERN_SUCCESS) {
5851 + if (kr == KERN_FAILURE) {
5852 + msg = " It seems that master process does not have enough privileges to trace processes.";
5855 + zlog(ZLOG_STUFF, ZLOG_ERROR, "task_for_pid() failed: %s (%d)%s", mach_error_string(kr), kr, msg);
5862 +int fpm_trace_close(pid_t pid)
5864 + fpm_mach_vm_deallocate();
5871 +int fpm_trace_get_long(long addr, long *data)
5873 + size_t offset = ((uintptr_t) (addr) % fpm_pagesize);
5874 + vm_offset_t base = (uintptr_t) (addr) - offset;
5876 + if (base != target_page_base) {
5877 + fpm_mach_vm_deallocate();
5878 + if (0 > fpm_mach_vm_read_page(base)) {
5883 + *data = * (long *) (local_page + offset);
5888 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace_pread.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace_pread.c
5889 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace_pread.c 1970-01-01 03:00:00.000000000 +0300
5890 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace_pread.c 2008-08-26 19:09:15.000000000 +0400
5894 + /* (c) 2007,2008 Andrei Nigmatulin */
5896 +#define _GNU_SOURCE
5897 +#define _FILE_OFFSET_BITS 64
5899 +#include "fpm_config.h"
5901 +#include <unistd.h>
5905 +#include <stdint.h>
5907 +#include "fpm_trace.h"
5908 +#include "fpm_process_ctl.h"
5912 +static int mem_file = -1;
5914 +int fpm_trace_signal(pid_t pid)
5916 + if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
5917 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "kill(SIGSTOP) failed");
5924 +int fpm_trace_ready(pid_t pid)
5928 + sprintf(buf, "/proc/%d/" PROC_MEM_FILE, (int) pid);
5930 + mem_file = open(buf, O_RDONLY);
5932 + if (0 > mem_file) {
5933 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(%s) failed", buf);
5940 +int fpm_trace_close(pid_t pid)
5949 +int fpm_trace_get_long(long addr, long *data)
5951 + if (sizeof(*data) != pread(mem_file, (void *) data, sizeof(*data), (uintptr_t) addr)) {
5952 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pread() failed");
5959 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace_ptrace.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace_ptrace.c
5960 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace_ptrace.c 1970-01-01 03:00:00.000000000 +0300
5961 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace_ptrace.c 2008-09-19 03:34:11.000000000 +0400
5965 + /* (c) 2007,2008 Andrei Nigmatulin */
5967 +#include "fpm_config.h"
5969 +#include <sys/wait.h>
5970 +#include <sys/ptrace.h>
5971 +#include <unistd.h>
5974 +#if defined(PT_ATTACH) && !defined(PTRACE_ATTACH)
5975 +#define PTRACE_ATTACH PT_ATTACH
5978 +#if defined(PT_DETACH) && !defined(PTRACE_DETACH)
5979 +#define PTRACE_DETACH PT_DETACH
5982 +#if defined(PT_READ_D) && !defined(PTRACE_PEEKDATA)
5983 +#define PTRACE_PEEKDATA PT_READ_D
5986 +#include "fpm_trace.h"
5989 +static pid_t traced_pid;
5991 +int fpm_trace_signal(pid_t pid)
5993 + if (0 > ptrace(PTRACE_ATTACH, pid, 0, 0)) {
5994 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(ATTACH) failed");
6001 +int fpm_trace_ready(pid_t pid)
6008 +int fpm_trace_close(pid_t pid)
6010 + if (0 > ptrace(PTRACE_DETACH, pid, (void *) 1, 0)) {
6011 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(DETACH) failed");
6020 +int fpm_trace_get_long(long addr, long *data)
6023 + struct ptrace_io_desc ptio = {
6024 + .piod_op = PIOD_READ_D,
6025 + .piod_offs = (void *) addr,
6026 + .piod_addr = (void *) data,
6027 + .piod_len = sizeof(long)
6030 + if (0 > ptrace(PT_IO, traced_pid, (void *) &ptio, 0)) {
6031 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(PT_IO) failed");
6037 + *data = ptrace(PTRACE_PEEKDATA, traced_pid, (void *) addr, 0);
6040 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(PEEKDATA) failed");
6048 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_unix.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_unix.c
6049 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_unix.c 1970-01-01 03:00:00.000000000 +0300
6050 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_unix.c 2008-09-19 03:19:59.000000000 +0400
6054 + /* (c) 2007,2008 Andrei Nigmatulin */
6056 +#include "fpm_config.h"
6058 +#include <string.h>
6059 +#include <sys/time.h>
6060 +#include <sys/resource.h>
6061 +#include <stdlib.h>
6062 +#include <unistd.h>
6063 +#include <sys/types.h>
6068 +#include <sys/prctl.h>
6072 +#include "fpm_conf.h"
6073 +#include "fpm_cleanup.h"
6074 +#include "fpm_clock.h"
6075 +#include "fpm_stdio.h"
6076 +#include "fpm_unix.h"
6079 +size_t fpm_pagesize;
6081 +int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp)
6083 + struct fpm_listen_options_s *lo = wp->config->listen_options;
6085 + /* uninitialized */
6086 + wp->socket_uid = -1;
6087 + wp->socket_gid = -1;
6088 + wp->socket_mode = 0666;
6090 + if (!lo) return 0;
6092 + if (lo->owner && *lo->owner) {
6093 + struct passwd *pwd;
6095 + pwd = getpwnam(lo->owner);
6098 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "cannot get uid for user '%s', pool '%s'", lo->owner, wp->config->name);
6102 + wp->socket_uid = pwd->pw_uid;
6103 + wp->socket_gid = pwd->pw_gid;
6106 + if (lo->group && *lo->group) {
6107 + struct group *grp;
6109 + grp = getgrnam(lo->group);
6112 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "cannot get gid for group '%s', pool '%s'", lo->group, wp->config->name);
6116 + wp->socket_gid = grp->gr_gid;
6119 + if (lo->mode && *lo->mode) {
6120 + wp->socket_mode = strtoul(lo->mode, 0, 8);
6126 +static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp)
6128 + int is_root = !geteuid();
6131 + if (wp->config->user && *wp->config->user) {
6133 + if (strlen(wp->config->user) == strspn(wp->config->user, "0123456789")) {
6134 + wp->set_uid = strtoul(wp->config->user, 0, 10);
6137 + struct passwd *pwd;
6139 + pwd = getpwnam(wp->config->user);
6142 + zlog(ZLOG_STUFF, ZLOG_ERROR, "cannot get uid for user '%s', pool '%s'", wp->config->user, wp->config->name);
6146 + wp->set_uid = pwd->pw_uid;
6147 + wp->set_gid = pwd->pw_gid;
6149 + wp->user = strdup(pwd->pw_name);
6150 + wp->home = strdup(pwd->pw_dir);
6154 + if (wp->config->group && *wp->config->group) {
6156 + if (strlen(wp->config->group) == strspn(wp->config->group, "0123456789")) {
6157 + wp->set_gid = strtoul(wp->config->group, 0, 10);
6160 + struct group *grp;
6162 + grp = getgrnam(wp->config->group);
6165 + zlog(ZLOG_STUFF, ZLOG_ERROR, "cannot get gid for group '%s', pool '%s'", wp->config->group, wp->config->name);
6169 + wp->set_gid = grp->gr_gid;
6173 +#ifndef I_REALLY_WANT_ROOT_PHP
6174 + if (wp->set_uid == 0 || wp->set_gid == 0) {
6175 + zlog(ZLOG_STUFF, ZLOG_ERROR, "please specify user and group other than root, pool '%s'", wp->config->name);
6180 + else { /* not root */
6181 + if (wp->config->user && *wp->config->user) {
6182 + zlog(ZLOG_STUFF, ZLOG_WARNING, "'user' directive is ignored, pool '%s'", wp->config->name);
6184 + if (wp->config->group && *wp->config->group) {
6185 + zlog(ZLOG_STUFF, ZLOG_WARNING, "'group' directive is ignored, pool '%s'", wp->config->name);
6187 + if (wp->config->chroot && *wp->config->chroot) {
6188 + zlog(ZLOG_STUFF, ZLOG_WARNING, "'chroot' directive is ignored, pool '%s'", wp->config->name);
6191 + { /* set up HOME and USER anyway */
6192 + struct passwd *pwd;
6194 + pwd = getpwuid(getuid());
6197 + wp->user = strdup(pwd->pw_name);
6198 + wp->home = strdup(pwd->pw_dir);
6206 +int fpm_unix_init_child(struct fpm_worker_pool_s *wp)
6208 + int is_root = !geteuid();
6209 + int made_chroot = 0;
6211 + if (wp->config->rlimit_files) {
6214 + getrlimit(RLIMIT_NOFILE, &r);
6216 + r.rlim_cur = (rlim_t) wp->config->rlimit_files;
6218 + if (0 > setrlimit(RLIMIT_NOFILE, &r)) {
6219 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setrlimit(RLIMIT_NOFILE) failed");
6223 + if (wp->config->rlimit_core) {
6226 + getrlimit(RLIMIT_CORE, &r);
6228 + r.rlim_cur = wp->config->rlimit_core == -1 ? (rlim_t) RLIM_INFINITY : (rlim_t) wp->config->rlimit_core;
6230 + if (0 > setrlimit(RLIMIT_CORE, &r)) {
6231 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setrlimit(RLIMIT_CORE) failed");
6235 + if (is_root && wp->config->chroot && *wp->config->chroot) {
6236 + if (0 > chroot(wp->config->chroot)) {
6237 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chroot(%s) failed", wp->config->chroot);
6243 + if (wp->config->chdir && *wp->config->chdir) {
6244 + if (0 > chdir(wp->config->chdir)) {
6245 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chdir(%s) failed", wp->config->chdir);
6249 + else if (made_chroot) {
6254 + if (wp->set_gid) {
6255 + if (0 > setgid(wp->set_gid)) {
6256 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setgid(%d) failed", wp->set_gid);
6260 + if (wp->set_uid) {
6261 + if (0 > initgroups(wp->config->user, wp->set_gid)) {
6262 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "initgroups(%s, %d) failed", wp->config->user, wp->set_gid);
6265 + if (0 > setuid(wp->set_uid)) {
6266 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setuid(%d) failed", wp->set_uid);
6273 + if (0 > prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)) {
6274 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "prctl(PR_SET_DUMPABLE) failed");
6278 + if (0 > fpm_clock_init()) {
6285 +int fpm_unix_init_main()
6287 + struct fpm_worker_pool_s *wp;
6289 + fpm_pagesize = getpagesize();
6291 + if (fpm_global_options.daemonize) {
6297 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fork() failed");
6306 + fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT);
6315 + if (0 > fpm_clock_init()) {
6319 + fpm_globals.parent_pid = getpid();
6321 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
6323 + if (0 > fpm_unix_conf_wp(wp)) {
6329 + fpm_stdio_init_final();
6333 + getrlimit(RLIMIT_NOFILE, &r);
6335 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "getrlimit(nofile): max:%lld, cur:%lld",
6336 + (long long) r.rlim_max, (long long) r.rlim_cur);
6341 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_unix.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_unix.h
6342 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_unix.h 1970-01-01 03:00:00.000000000 +0300
6343 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_unix.h 2008-05-25 17:21:13.000000000 +0400
6347 + /* (c) 2007,2008 Andrei Nigmatulin */
6350 +#define FPM_UNIX_H 1
6352 +#include "fpm_worker_pool.h"
6354 +int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp);
6355 +int fpm_unix_init_child(struct fpm_worker_pool_s *wp);
6356 +int fpm_unix_init_main();
6358 +extern size_t fpm_pagesize;
6362 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_worker_pool.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_worker_pool.c
6363 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_worker_pool.c 1970-01-01 03:00:00.000000000 +0300
6364 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_worker_pool.c 2008-08-26 19:09:15.000000000 +0400
6368 + /* (c) 2007,2008 Andrei Nigmatulin */
6370 +#include "fpm_config.h"
6372 +#include <string.h>
6373 +#include <stdlib.h>
6374 +#include <unistd.h>
6376 +#include "fpm_worker_pool.h"
6377 +#include "fpm_cleanup.h"
6378 +#include "fpm_children.h"
6379 +#include "fpm_shm.h"
6380 +#include "fpm_shm_slots.h"
6381 +#include "fpm_conf.h"
6383 +struct fpm_worker_pool_s *fpm_worker_all_pools;
6385 +static void fpm_worker_pool_cleanup(int which, void *arg)
6387 + struct fpm_worker_pool_s *wp, *wp_next;
6389 + for (wp = fpm_worker_all_pools; wp; wp = wp_next) {
6390 + wp_next = wp->next;
6391 + fpm_worker_pool_config_free(wp->config);
6392 + fpm_children_free(wp->children);
6393 + fpm_array_free(&wp->slots_used);
6394 + fpm_array_free(&wp->slots_free);
6395 + fpm_shm_free_list(wp->shm_list, which == FPM_CLEANUP_CHILD ? fpm_shm_slots_mem() : 0);
6402 + fpm_worker_all_pools = 0;
6405 +struct fpm_worker_pool_s *fpm_worker_pool_alloc()
6407 + struct fpm_worker_pool_s *ret;
6409 + ret = malloc(sizeof(struct fpm_worker_pool_s));
6415 + memset(ret, 0, sizeof(struct fpm_worker_pool_s));
6417 + if (!fpm_worker_all_pools) {
6418 + fpm_worker_all_pools = ret;
6421 + fpm_array_init(&ret->slots_used, sizeof(struct fpm_shm_slot_ptr_s), 50);
6422 + fpm_array_init(&ret->slots_free, sizeof(struct fpm_shm_slot_ptr_s), 50);
6427 +int fpm_worker_pool_init_main()
6429 + fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_worker_pool_cleanup, 0);
6433 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_worker_pool.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_worker_pool.h
6434 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_worker_pool.h 1970-01-01 03:00:00.000000000 +0300
6435 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_worker_pool.h 2008-08-26 19:09:15.000000000 +0400
6439 + /* (c) 2007,2008 Andrei Nigmatulin */
6441 +#ifndef FPM_WORKER_POOL_H
6442 +#define FPM_WORKER_POOL_H 1
6444 +#include "fpm_conf.h"
6445 +#include "fpm_arrays.h"
6447 +struct fpm_worker_pool_s;
6448 +struct fpm_child_s;
6449 +struct fpm_child_stat_s;
6452 +enum fpm_address_domain {
6457 +struct fpm_worker_pool_s {
6458 + struct fpm_worker_pool_s *next;
6459 + struct fpm_worker_pool_config_s *config;
6460 + char *user, *home; /* for setting env USER and HOME */
6461 + enum fpm_address_domain listen_address_domain;
6462 + int listening_socket;
6463 + int set_uid, set_gid; /* config uid and gid */
6464 + unsigned is_template:1; /* just config template, no processes will be created */
6465 + int socket_uid, socket_gid, socket_mode;
6467 + struct fpm_shm_s *shm_list;
6468 + struct fpm_array_s slots_used;
6469 + struct fpm_array_s slots_free;
6472 + struct fpm_child_s *children;
6473 + int running_children;
6476 +struct fpm_worker_pool_s *fpm_worker_pool_alloc();
6477 +int fpm_worker_pool_init_main();
6479 +extern struct fpm_worker_pool_s *fpm_worker_all_pools;
6483 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/init.d/php-fpm.in php-5.2.6.fpm/sapi/cgi/fpm/init.d/php-fpm.in
6484 --- php-5.2.6.vanilla/sapi/cgi/fpm/init.d/php-fpm.in 1970-01-01 03:00:00.000000000 +0300
6485 +++ php-5.2.6.fpm/sapi/cgi/fpm/init.d/php-fpm.in 2008-08-05 20:31:27.000000000 +0400
6489 +php_fpm_BIN=@prefix@/bin/php-cgi
6490 +php_fpm_CONF=@php_fpm_conf_path@
6491 +php_fpm_PID=@php_fpm_pid_path@
6494 +php_opts="--fpm-config $php_fpm_CONF"
6500 + while test $try -lt 35 ; do
6504 + if [ -f "$2" ] ; then
6511 + if [ ! -f "$2" ] ; then
6519 + try=`expr $try + 1`
6528 + echo -n "Starting php_fpm "
6530 + $php_fpm_BIN --fpm $php_opts
6532 + if [ "$?" != 0 ] ; then
6537 + wait_for_pid created $php_fpm_PID
6539 + if [ -n "$try" ] ; then
6548 + echo -n "Shutting down php_fpm "
6550 + if [ ! -r $php_fpm_PID ] ; then
6551 + echo "warning, no pid file found - php-fpm is not running ?"
6555 + kill -TERM `cat $php_fpm_PID`
6557 + wait_for_pid removed $php_fpm_PID
6559 + if [ -n "$try" ] ; then
6568 + echo -n "Gracefully shutting down php_fpm "
6570 + if [ ! -r $php_fpm_PID ] ; then
6571 + echo "warning, no pid file found - php-fpm is not running ?"
6575 + kill -QUIT `cat $php_fpm_PID`
6577 + wait_for_pid removed $php_fpm_PID
6579 + if [ -n "$try" ] ; then
6594 + echo -n "Reload service php-fpm "
6596 + if [ ! -r $php_fpm_PID ] ; then
6597 + echo "warning, no pid file found - php-fpm is not running ?"
6601 + kill -USR2 `cat $php_fpm_PID`
6608 + echo -n "Re-opening php-fpm log file "
6610 + if [ ! -r $php_fpm_PID ] ; then
6611 + echo "warning, no pid file found - php-fpm is not running ?"
6615 + kill -USR1 `cat $php_fpm_PID`
6621 + echo "Usage: $0 {start|stop|quit|restart|reload|logrotate}"
6626 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/Makefile.frag php-5.2.6.fpm/sapi/cgi/fpm/Makefile.frag
6627 --- php-5.2.6.vanilla/sapi/cgi/fpm/Makefile.frag 1970-01-01 03:00:00.000000000 +0300
6628 +++ php-5.2.6.fpm/sapi/cgi/fpm/Makefile.frag 2008-03-28 16:51:22.000000000 +0300
6631 +install-fpm: sapi/cgi/fpm/php-fpm.conf sapi/cgi/fpm/php-fpm
6632 + @echo "Installing FPM config: $(INSTALL_ROOT)$(php_fpm_conf_path)"
6633 + -@$(mkinstalldirs) \
6634 + $(INSTALL_ROOT)$(prefix)/sbin \
6635 + `dirname "$(INSTALL_ROOT)$(php_fpm_conf_path)"` \
6636 + `dirname "$(INSTALL_ROOT)$(php_fpm_log_path)"` \
6637 + `dirname "$(INSTALL_ROOT)$(php_fpm_pid_path)"`
6638 + -@if test -r "$(INSTALL_ROOT)$(php_fpm_conf_path)" ; then \
6639 + dest=`basename "$(php_fpm_conf_path)"`.default ; \
6640 + echo " (installing as $$dest)" ; \
6642 + dest=`basename "$(php_fpm_conf_path)"` ; \
6644 + $(INSTALL_DATA) $(top_builddir)/sapi/cgi/fpm/php-fpm.conf $(INSTALL_ROOT)`dirname "$(php_fpm_conf_path)"`/$$dest
6645 + @echo "Installing init.d script: $(INSTALL_ROOT)$(prefix)/sbin/php-fpm"
6646 + -@$(INSTALL) -m 0755 $(top_builddir)/sapi/cgi/fpm/php-fpm $(INSTALL_ROOT)$(prefix)/sbin/php-fpm
6648 +$(top_builddir)/libevent/libevent.a: $(top_builddir)/libevent/Makefile
6649 + cd $(top_builddir)/libevent && $(MAKE) libevent.a
6651 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/xml_config.c php-5.2.6.fpm/sapi/cgi/fpm/xml_config.c
6652 --- php-5.2.6.vanilla/sapi/cgi/fpm/xml_config.c 1970-01-01 03:00:00.000000000 +0300
6653 +++ php-5.2.6.fpm/sapi/cgi/fpm/xml_config.c 2008-08-26 19:09:15.000000000 +0400
6657 + /* (c) 2004-2007 Andrei Nigmatulin */
6659 +#include "fpm_config.h"
6661 +#ifdef HAVE_ALLOCA_H
6662 +#include <alloca.h>
6664 +#include <string.h>
6666 +#include <stddef.h>
6667 +#include <stdlib.h>
6669 +#include <libxml/parser.h>
6670 +#include <libxml/tree.h>
6672 +#include "xml_config.h"
6674 +static struct xml_conf_section **xml_conf_sections = 0;
6675 +static int xml_conf_sections_allocated = 0;
6676 +static int xml_conf_sections_used = 0;
6678 +char *xml_conf_set_slot_boolean(void **conf, char *name, void *vv, intptr_t offset)
6681 + long value_y = !strcasecmp(value, "yes") || !strcmp(value, "1") || !strcasecmp(value, "on");
6682 + long value_n = !strcasecmp(value, "no") || !strcmp(value, "0") || !strcasecmp(value, "off");
6684 + if (!value_y && !value_n) {
6685 + return "xml_conf_set_slot(): invalid boolean value";
6688 +#ifdef XML_CONF_DEBUG
6689 + fprintf(stderr, "setting boolean '%s' => %s\n", name, value_y ? "TRUE" : "FALSE");
6692 + * (int *) ((char *) *conf + offset) = value_y ? 1 : 0;
6697 +char *xml_conf_set_slot_string(void **conf, char *name, void *vv, intptr_t offset)
6700 + char *v = strdup(value);
6702 + if (!v) return "xml_conf_set_slot_string(): strdup() failed";
6704 +#ifdef XML_CONF_DEBUG
6705 + fprintf(stderr, "setting string '%s' => '%s'\n", name, v);
6708 + * (char **) ((char *) *conf + offset) = v;
6713 +char *xml_conf_set_slot_integer(void **conf, char *name, void *vv, intptr_t offset)
6716 + int v = atoi(value);
6718 + * (int *) ((char *) *conf + offset) = v;
6720 +#ifdef XML_CONF_DEBUG
6721 + fprintf(stderr, "setting integer '%s' => %d\n", name, v);
6727 +char *xml_conf_set_slot_time(void **conf, char *name, void *vv, intptr_t offset)
6730 + int len = strlen(value);
6734 + if (!len) return "xml_conf_set_slot_timeval(): invalid timeval value";
6736 + suffix = value[len-1];
6738 + value[len-1] = '\0';
6742 + seconds = atoi(value);
6745 + seconds = 60 * atoi(value);
6748 + seconds = 60 * 60 * atoi(value);
6751 + seconds = 24 * 60 * 60 * atoi(value);
6754 + return "xml_conf_set_slot_timeval(): unknown suffix used in timeval value";
6757 + * (int *) ((char *) *conf + offset) = seconds;
6759 +#ifdef XML_CONF_DEBUG
6760 + fprintf(stderr, "setting time '%s' => %d:%02d:%02d:%02d\n", name, expand_dhms(seconds));
6766 +char *xml_conf_parse_section(void **conf, struct xml_conf_section *section, void *xml_node)
6768 + xmlNode *element = xml_node;
6771 +#ifdef XML_CONF_DEBUG
6772 + fprintf(stderr, "processing a section %s\n", section->path);
6775 + for ( ; element; element = element->next) {
6776 + if (element->type == XML_ELEMENT_NODE && !strcmp((const char *) element->name, "value") && element->children) {
6777 + xmlChar *name = xmlGetProp(element, (unsigned char *) "name");
6782 +#ifdef XML_CONF_DEBUG
6783 + fprintf(stderr, "found a value: %s\n", name);
6785 + for (i = 0; section->parsers[i].parser; i++) {
6786 + if (!section->parsers[i].name || !strcmp(section->parsers[i].name, (char *) name)) {
6791 + if (section->parsers[i].parser) {
6792 + if (section->parsers[i].type == XML_CONF_SCALAR) {
6793 + if (element->children->type == XML_TEXT_NODE) {
6794 + ret = section->parsers[i].parser(conf, (char *) name, element->children->content, section->parsers[i].offset);
6797 + ret = "XML_TEXT_NODE is expected, something different is given";
6801 + ret = section->parsers[i].parser(conf, (char *) name, element->children, section->parsers[i].offset);
6805 + if (ret) return ret;
6809 + fprintf(stderr, "Warning, unknown setting '%s' in section '%s'\n", (char *) name, section->path);
6819 +static char *xml_conf_parse_file(xmlNode *element)
6823 + for ( ; element; element = element->next) {
6825 + if (element->parent && element->type == XML_ELEMENT_NODE && !strcmp((const char *) element->name, "section")) {
6826 + xmlChar *name = xmlGetProp(element, (unsigned char *) "name");
6829 + char *parent_name = (char *) xmlGetNodePath(element->parent);
6832 + struct xml_conf_section *section = NULL;
6834 +#ifdef XML_CONF_DEBUG
6835 + fprintf(stderr, "got a section: %s/%s\n", parent_name, name);
6837 + full_name = alloca(strlen(parent_name) + strlen((char *) name) + 1 + 1);
6839 + sprintf(full_name, "%s/%s", parent_name, (char *) name);
6841 + xmlFree(parent_name);
6844 + for (i = 0; i < xml_conf_sections_used; i++) {
6845 + if (!strcmp(xml_conf_sections[i]->path, full_name)) {
6846 + section = xml_conf_sections[i];
6850 + if (section) { /* found a registered section */
6851 + void *conf = section->conf();
6852 + ret = xml_conf_parse_section(&conf, section, element->children);
6859 + if (element->children) {
6860 + ret = xml_conf_parse_file(element->children);
6868 +char *xml_conf_load_file(char *file)
6873 + LIBXML_TEST_VERSION
6875 + doc = xmlParseFile(file);
6878 + ret = xml_conf_parse_file(doc->children);
6882 + ret = "failed to parse conf file";
6885 + xmlCleanupParser();
6889 +int xml_conf_init()
6894 +void xml_conf_clean()
6896 + if (xml_conf_sections) {
6897 + free(xml_conf_sections);
6901 +int xml_conf_section_register(struct xml_conf_section *section)
6903 + if (xml_conf_sections_allocated == xml_conf_sections_used) {
6904 + int new_size = xml_conf_sections_used + 10;
6905 + void *new_ptr = realloc(xml_conf_sections, sizeof(struct xml_conf_section *) * new_size);
6908 + xml_conf_sections = new_ptr;
6909 + xml_conf_sections_allocated = new_size;
6912 + fprintf(stderr, "xml_conf_section_register(): out of memory\n");
6917 + xml_conf_sections[xml_conf_sections_used++] = section;
6922 +int xml_conf_sections_register(struct xml_conf_section *sections[])
6924 + for ( ; sections && *sections; sections++) {
6925 + if (0 > xml_conf_section_register(*sections)) {
6933 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/xml_config.h php-5.2.6.fpm/sapi/cgi/fpm/xml_config.h
6934 --- php-5.2.6.vanilla/sapi/cgi/fpm/xml_config.h 1970-01-01 03:00:00.000000000 +0300
6935 +++ php-5.2.6.fpm/sapi/cgi/fpm/xml_config.h 2008-09-19 03:02:58.000000000 +0400
6939 + /* (c) 2004-2007 Andrei Nigmatulin */
6941 +#ifndef XML_CONFIG_H
6942 +#define XML_CONFIG_H 1
6944 +#include <stdint.h>
6946 +struct xml_value_parser;
6948 +struct xml_value_parser {
6951 + char *(*parser)(void **, char *, void *, intptr_t offset);
6955 +struct xml_conf_section {
6958 + struct xml_value_parser *parsers;
6961 +char *xml_conf_set_slot_boolean(void **conf, char *name, void *value, intptr_t offset);
6962 +char *xml_conf_set_slot_string(void **conf, char *name, void *value, intptr_t offset);
6963 +char *xml_conf_set_slot_integer(void **conf, char *name, void *value, intptr_t offset);
6964 +char *xml_conf_set_slot_time(void **conf, char *name, void *value, intptr_t offset);
6966 +int xml_conf_init();
6967 +void xml_conf_clean();
6968 +char *xml_conf_load_file(char *file);
6969 +char *xml_conf_parse_section(void **conf, struct xml_conf_section *section, void *ve);
6970 +int xml_conf_section_register(struct xml_conf_section *section);
6971 +int xml_conf_sections_register(struct xml_conf_section *sections[]);
6973 +#define expand_hms(value) (value) / 3600, ((value) % 3600) / 60, (value) % 60
6975 +#define expand_dhms(value) (value) / 86400, ((value) % 86400) / 3600, ((value) % 3600) / 60, (value) % 60
6977 +enum { XML_CONF_SCALAR = 1, XML_CONF_SUBSECTION = 2 };
6980 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/zlog.c php-5.2.6.fpm/sapi/cgi/fpm/zlog.c
6981 --- php-5.2.6.vanilla/sapi/cgi/fpm/zlog.c 1970-01-01 03:00:00.000000000 +0300
6982 +++ php-5.2.6.fpm/sapi/cgi/fpm/zlog.c 2008-05-23 01:08:32.000000000 +0400
6986 + /* (c) 2004-2007 Andrei Nigmatulin */
6988 +#include "fpm_config.h"
6991 +#include <unistd.h>
6993 +#include <string.h>
6994 +#include <stdarg.h>
6995 +#include <sys/time.h>
7000 +#define MAX_LINE_LENGTH 1024
7002 +static int zlog_fd = -1;
7003 +static int zlog_level = ZLOG_NOTICE;
7005 +static const char *level_names[] = {
7006 + [ZLOG_DEBUG] = "DEBUG",
7007 + [ZLOG_NOTICE] = "NOTICE",
7008 + [ZLOG_WARNING] = "WARNING",
7009 + [ZLOG_ERROR] = "ERROR",
7010 + [ZLOG_ALERT] = "ALERT",
7013 +size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len)
7018 + len = strftime(timebuf, timebuf_len, "%b %d %H:%M:%S", localtime_r((const time_t *) &tv->tv_sec, &t));
7019 + len += snprintf(timebuf + len, timebuf_len - len, ".%06d", (int) tv->tv_usec);
7024 +int zlog_set_fd(int new_fd)
7026 + int old_fd = zlog_fd;
7032 +int zlog_set_level(int new_value)
7034 + int old_value = zlog_level;
7036 + zlog_level = new_value;
7041 +void zlog(const char *function, int line, int flags, const char *fmt, ...)
7043 + struct timeval tv;
7044 + char buf[MAX_LINE_LENGTH];
7045 + const size_t buf_size = MAX_LINE_LENGTH;
7048 + int truncated = 0;
7051 + if ((flags & ZLOG_LEVEL_MASK) < zlog_level) {
7055 + saved_errno = errno;
7057 + gettimeofday(&tv, 0);
7059 + len = zlog_print_time(&tv, buf, buf_size);
7061 + len += snprintf(buf + len, buf_size - len, " [%s] %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
7063 + if (len > buf_size - 1) {
7068 + va_start(args, fmt);
7070 + len += vsnprintf(buf + len, buf_size - len, fmt, args);
7074 + if (len >= buf_size) {
7080 + if (flags & ZLOG_HAVE_ERRNO) {
7081 + len += snprintf(buf + len, buf_size - len, ": %s (%d)", strerror(saved_errno), saved_errno);
7082 + if (len >= buf_size) {
7089 + memcpy(buf + buf_size - sizeof("..."), "...", sizeof("...") - 1);
7090 + len = buf_size - 1;
7093 + buf[len++] = '\n';
7095 + write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, buf, len);
7097 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/zlog.h php-5.2.6.fpm/sapi/cgi/fpm/zlog.h
7098 --- php-5.2.6.vanilla/sapi/cgi/fpm/zlog.h 1970-01-01 03:00:00.000000000 +0300
7099 +++ php-5.2.6.fpm/sapi/cgi/fpm/zlog.h 2008-05-23 01:08:32.000000000 +0400
7103 + /* (c) 2004-2007 Andrei Nigmatulin */
7108 +#define ZLOG_STUFF __func__, __LINE__
7112 +int zlog_set_fd(int new_fd);
7113 +int zlog_set_level(int new_value);
7115 +size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len);
7117 +void zlog(const char *function, int line, int flags, const char *fmt, ...)
7118 + __attribute__ ((format(printf,4,5)));
7128 +#define ZLOG_LEVEL_MASK 7
7130 +#define ZLOG_HAVE_ERRNO 0x100
7132 +#define ZLOG_SYSERROR (ZLOG_ERROR | ZLOG_HAVE_ERRNO)
7135 --- php-5.2.6/sapi/cgi/Makefile.frag~ 2008-11-03 19:29:12.000000000 +0200
7136 +++ php-5.2.6/sapi/cgi/Makefile.frag 2008-11-03 19:31:19.361921087 +0200
7138 -$(SAPI_CGI_PATH): libphp_common.la $(PHP_SAPI_OBJS)
7139 +$(SAPI_CGI_PATH): libphp_common.la $(PHP_SAPI_OBJS) $(SAPI_EXTRA_DEPS)