-diff -Nru php-5.2.6.vanilla/configure php-5.2.6.fpm/configure
-diff -Nru php-5.2.6.vanilla/configure.in php-5.2.6.fpm/configure.in
---- php-5.2.6.vanilla/configure.in 2008-04-30 22:27:55.000000000 +0400
-+++ php-5.2.6.fpm/configure.in 2008-05-01 20:42:05.000000000 +0400
-@@ -267,6 +267,12 @@
- PTHREADS_FLAGS
- fi
-
-+if test "$PHP_FASTCGI" = "yes" -a "$PHP_FPM" = "yes"; then
-+ PHP_CONFIGURE_PART(Running FastCGI Process Manager checks)
-+ sinclude(sapi/cgi/fpm/acinclude.m4)
-+ sinclude(sapi/cgi/fpm/config.m4)
-+fi
-+
- divert(3)
-
- dnl ## In diversion 3 we check for compile-time options to the PHP
-@@ -476,6 +482,7 @@
- alphasort \
- asctime_r \
- chroot \
-+clearenv \
- ctime_r \
- cuserid \
- crypt \
-@@ -1197,6 +1204,8 @@
- PHP_SUBST_OLD(EXTRA_LDFLAGS)
- PHP_SUBST_OLD(EXTRA_LDFLAGS_PROGRAM)
- PHP_SUBST_OLD(EXTRA_LIBS)
-+PHP_SUBST_OLD(SAPI_EXTRA_LIBS)
-+PHP_SUBST_OLD(SAPI_EXTRA_DEPS)
- PHP_SUBST_OLD(ZEND_EXTRA_LIBS)
- PHP_SUBST_OLD(INCLUDES)
- PHP_SUBST_OLD(EXTRA_INCLUDES)
-@@ -1285,7 +1294,7 @@
- install_targets="$PHP_INSTALL_CLI_TARGET $install_targets"
- ;;
- *)
-- install_targets="install-sapi $PHP_INSTALL_CLI_TARGET $install_targets"
-+ install_targets="install-sapi $install_fpm $PHP_INSTALL_CLI_TARGET $install_targets"
- ;;
- esac
-
-diff -Nru php-5.2.6.vanilla/libevent/aclocal.m4 php-5.2.6.fpm/libevent/aclocal.m4
-diff -Nru php-5.2.6.vanilla/libevent/autogen.sh php-5.2.6.fpm/libevent/autogen.sh
-diff -Nru php-5.2.6.vanilla/libevent/ChangeLog php-5.2.6.fpm/libevent/ChangeLog
-diff -Nru php-5.2.6.vanilla/libevent/compat/sys/queue.h php-5.2.6.fpm/libevent/compat/sys/queue.h
-diff -Nru php-5.2.6.vanilla/libevent/compat/sys/_time.h php-5.2.6.fpm/libevent/compat/sys/_time.h
-diff -Nru php-5.2.6.vanilla/libevent/config.guess php-5.2.6.fpm/libevent/config.guess
-diff -Nru php-5.2.6.vanilla/libevent/config.h.in php-5.2.6.fpm/libevent/config.h.in
-diff -Nru php-5.2.6.vanilla/libevent/config.sub php-5.2.6.fpm/libevent/config.sub
-diff -Nru php-5.2.6.vanilla/libevent/configure php-5.2.6.fpm/libevent/configure
-diff -Nru php-5.2.6.vanilla/libevent/configure.in php-5.2.6.fpm/libevent/configure.in
-diff -Nru php-5.2.6.vanilla/libevent/depcomp php-5.2.6.fpm/libevent/depcomp
-diff -Nru php-5.2.6.vanilla/libevent/devpoll.c php-5.2.6.fpm/libevent/devpoll.c
-diff -Nru php-5.2.6.vanilla/libevent/epoll.c php-5.2.6.fpm/libevent/epoll.c
-diff -Nru php-5.2.6.vanilla/libevent/epoll_sub.c php-5.2.6.fpm/libevent/epoll_sub.c
-diff -Nru php-5.2.6.vanilla/libevent/event.3 php-5.2.6.fpm/libevent/event.3
-diff -Nru php-5.2.6.vanilla/libevent/event.c php-5.2.6.fpm/libevent/event.c
-diff -Nru php-5.2.6.vanilla/libevent/event-config.h php-5.2.6.fpm/libevent/event-config.h
-diff -Nru php-5.2.6.vanilla/libevent/event-fpm.h php-5.2.6.fpm/libevent/event-fpm.h
-diff -Nru php-5.2.6.vanilla/libevent/event.h php-5.2.6.fpm/libevent/event.h
-diff -Nru php-5.2.6.vanilla/libevent/event-internal.h php-5.2.6.fpm/libevent/event-internal.h
-diff -Nru php-5.2.6.vanilla/libevent/evport.c php-5.2.6.fpm/libevent/evport.c
-diff -Nru php-5.2.6.vanilla/libevent/evsignal.h php-5.2.6.fpm/libevent/evsignal.h
-diff -Nru php-5.2.6.vanilla/libevent/evutil.c php-5.2.6.fpm/libevent/evutil.c
-diff -Nru php-5.2.6.vanilla/libevent/evutil.h php-5.2.6.fpm/libevent/evutil.h
-diff -Nru php-5.2.6.vanilla/libevent/install-sh php-5.2.6.fpm/libevent/install-sh
-diff -Nru php-5.2.6.vanilla/libevent/kqueue.c php-5.2.6.fpm/libevent/kqueue.c
-diff -Nru php-5.2.6.vanilla/libevent/log.c php-5.2.6.fpm/libevent/log.c
-diff -Nru php-5.2.6.vanilla/libevent/log.h php-5.2.6.fpm/libevent/log.h
-diff -Nru php-5.2.6.vanilla/libevent/Makefile.am php-5.2.6.fpm/libevent/Makefile.am
-diff -Nru php-5.2.6.vanilla/libevent/Makefile.in php-5.2.6.fpm/libevent/Makefile.in
-diff -Nru php-5.2.6.vanilla/libevent/min_heap.h php-5.2.6.fpm/libevent/min_heap.h
-diff -Nru php-5.2.6.vanilla/libevent/missing php-5.2.6.fpm/libevent/missing
-diff -Nru php-5.2.6.vanilla/libevent/mkinstalldirs php-5.2.6.fpm/libevent/mkinstalldirs
-diff -Nru php-5.2.6.vanilla/libevent/poll.c php-5.2.6.fpm/libevent/poll.c
-diff -Nru php-5.2.6.vanilla/libevent/README php-5.2.6.fpm/libevent/README
-diff -Nru php-5.2.6.vanilla/libevent/select.c php-5.2.6.fpm/libevent/select.c
-diff -Nru php-5.2.6.vanilla/libevent/signal.c php-5.2.6.fpm/libevent/signal.c
-diff -Nru php-5.2.6.vanilla/main/php_config.h.in php-5.2.6.fpm/main/php_config.h.in
---- php-5.2.6.vanilla/main/php_config.h.in 2008-04-30 22:37:39.000000000 +0400
-+++ php-5.2.6.fpm/main/php_config.h.in 2008-09-21 17:37:45.000000000 +0400
-@@ -170,6 +170,9 @@
- /* Define if you have the chroot function. */
- #undef HAVE_CHROOT
-
-+/* Define if you have the clearenv function. */
-+#undef HAVE_CLEARENV
-+
- /* Define if you have the crypt function. */
- #undef HAVE_CRYPT
-
-@@ -929,6 +932,9 @@
- /* */
- #undef PHP_FASTCGI
-
-+/* Is experimental fastcgi process manager code activated */
-+#undef PHP_FASTCGI_PM
-+
- /* */
- #undef FORCE_CGI_REDIRECT
-
-@@ -938,6 +944,27 @@
- /* */
- #undef ENABLE_PATHINFO_CHECK
-
-+/* do we have libxml? */
-+#undef HAVE_LIBXML
-+
-+/* do we have prctl? */
-+#undef HAVE_PRCTL
-+
-+/* do we have clock_gettime? */
-+#undef HAVE_CLOCK_GETTIME
-+
-+/* do we have clock_get_time? */
-+#undef HAVE_CLOCK_GET_TIME
-+
-+/* do we have ptrace? */
-+#undef HAVE_PTRACE
-+
-+/* do we have mach_vm_read? */
-+#undef HAVE_MACH_VM_READ
-+
-+/* /proc/pid/mem interface */
-+#undef PROC_MEM_FILE
-+
- /* Define if system uses EBCDIC */
- #undef CHARSET_EBCDIC
-
---- php-5.2.6.fpm/sapi/cgi/cgi_main.c 2008-07-22 01:50:58.000000000 +0400
-+++ php-5.2.6/sapi/cgi/cgi_main.c 2008-11-03 20:21:40.431760373 +0200
-@@ -55,6 +55,9 @@
- #if HAVE_SYS_WAIT_H
- #include <sys/wait.h>
- #endif
-+#if HAVE_FCNTL_H
-+#include <fcntl.h>
-+#endif
- #include "zend.h"
- #include "zend_extensions.h"
- #include "php_ini.h"
-@@ -83,6 +86,11 @@
- #if PHP_FASTCGI
- #include "fastcgi.h"
-
-+#if PHP_FASTCGI_PM
-+#include "fpm/fpm.h"
-+#include "fpm/fpm_request.h"
-+#endif
-+
- #ifndef PHP_WIN32
- /* XXX this will need to change later when threaded fastcgi is
- implemented. shane */
-@@ -109,8 +117,12 @@
- static pid_t pgroup;
- #endif
-
-+static int request_body_fd;
-+
- #endif
-
-+static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC);
-+
- #define PHP_MODE_STANDARD 1
- #define PHP_MODE_HIGHLIGHT 2
- #define PHP_MODE_INDENT 3
-@@ -140,6 +152,10 @@
- {'w', 0, "strip"},
- {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
- {'v', 0, "version"},
-+#if PHP_FASTCGI_PM
-+ {'x', 0, "fpm"},
-+ {'y', 1, "fpm-config"},
-+#endif
- {'z', 1, "zend-extension"},
- #if PHP_FASTCGI
- {'T', 1, "timing"},
-@@ -164,6 +180,7 @@
- zend_bool impersonate;
- # endif
- #endif
-+ char *error_header;
- } php_cgi_globals_struct;
-
- #ifdef ZTS
-@@ -444,7 +461,28 @@
- #if PHP_FASTCGI
- if (fcgi_is_fastcgi()) {
- fcgi_request *request = (fcgi_request*) SG(server_context);
-- tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
-+
-+ if (request_body_fd == -1) {
-+ char *request_body_filename = sapi_cgibin_getenv((char *) "REQUEST_BODY_FILE",
-+ sizeof("REQUEST_BODY_FILE")-1 TSRMLS_CC);
-+
-+ if (request_body_filename && *request_body_filename) {
-+ request_body_fd = open(request_body_filename, O_RDONLY);
-+
-+ if (0 > request_body_fd) {
-+ php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)",
-+ request_body_filename, strerror(errno), errno);
-+ return 0;
-+ }
-+ }
-+ }
-+
-+ /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */
-+ if (request_body_fd < 0) {
-+ tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
-+ } else {
-+ tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes);
-+ }
- } else {
- tmp_read_bytes = read(0, buffer + read_bytes, count_bytes - read_bytes);
- }
-@@ -756,7 +794,12 @@
- " -s Display colour syntax highlighted source.\n"
- " -v Version number\n"
- " -w Display source with stripped comments and whitespace.\n"
-- " -z <file> Load Zend extension <file>.\n"
-+#if PHP_FASTCGI_PM
-+ " -x, --fpm Run in FastCGI process manager mode.\n"
-+ " -y, --fpm-config <file>\n"
-+ " Specify alternative path to FastCGI process manager config file.\n"
-+#endif
-+ " -z <file> Load Zend extension <file>.\n"
- #if PHP_FASTCGI
- " -T <count> Measure execution time of script repeated <count> times.\n"
- #endif
-@@ -1173,6 +1216,7 @@
- # ifdef PHP_WIN32
- STD_PHP_INI_ENTRY("fastcgi.impersonate", "0", PHP_INI_SYSTEM, OnUpdateBool, impersonate, php_cgi_globals_struct, php_cgi_globals)
- # endif
-+ STD_PHP_INI_ENTRY("fastcgi.error_header", NULL, PHP_INI_SYSTEM, OnUpdateString, error_header, php_cgi_globals_struct, php_cgi_globals)
- #endif
- PHP_INI_END()
-
-@@ -1195,6 +1239,7 @@
- # ifdef PHP_WIN32
- php_cgi_globals->impersonate = 0;
- # endif
-+ php_cgi_globals->error_header = NULL;
- #endif
- }
- /* }}} */
-@@ -1227,9 +1272,47 @@
- static PHP_MINFO_FUNCTION(cgi)
- {
- DISPLAY_INI_ENTRIES();
-+
-+#if PHP_FASTCGI_PM
-+
-+#include "fpm/fpm_autoconf.h"
-+
-+ php_info_print_table_start();
-+ php_info_print_table_row(2, "php-fpm", fpm ? "active" : "inactive");
-+ php_info_print_table_row(2, "php-fpm version", PHP_FPM_VERSION);
-+ php_info_print_table_end();
-+#endif
-+
- }
- /* }}} */
-
-+#if PHP_FASTCGI
-+PHP_FUNCTION(fastcgi_finish_request)
-+{
-+ fcgi_request *request = (fcgi_request*) SG(server_context);
-+
-+ if (fcgi_is_fastcgi() && request->fd >= 0) {
-+
-+ php_end_ob_buffers(1 TSRMLS_CC);
-+ php_header(TSRMLS_C);
-+
-+ fcgi_flush(request, 1);
-+ fcgi_close(request, 0, 0);
-+ RETURN_TRUE;
-+ }
-+
-+ RETURN_FALSE;
+diff -Naur php-src-vanilla/sapi/fpm/ac/fpm_build.m4 php-src/sapi/fpm/ac/fpm_build.m4
+--- php-src-vanilla/sapi/fpm/ac/fpm_build.m4 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/ac/fpm_build.m4 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,47 @@
+
-+}
-+#endif
-+
-+function_entry cgi_fcgi_sapi_functions[] = {
-+#if PHP_FASTCGI
-+ PHP_FE(fastcgi_finish_request, NULL)
-+#endif
-+ {NULL, NULL, NULL}
-+};
-+
- static zend_module_entry cgi_module_entry = {
- STANDARD_MODULE_HEADER,
- #if PHP_FASTCGI
-@@ -1237,7 +1320,7 @@
- #else
- "cgi",
- #endif
-- NULL,
-+ cgi_fcgi_sapi_functions,
- PHP_MINIT(cgi),
- PHP_MSHUTDOWN(cgi),
- NULL,
-@@ -1277,6 +1360,7 @@
- char *bindpath = NULL;
- int fcgi_fd = 0;
- fcgi_request request;
-+ char *fpm_config = NULL;
- int repeats = 1;
- int benchmark = 0;
- #if HAVE_GETTIMEOFDAY
-@@ -1397,6 +1481,14 @@
- case 's': /* generate highlighted HTML from source */
- behavior = PHP_MODE_HIGHLIGHT;
- break;
-+#if PHP_FASTCGI_PM
-+ case 'y':
-+ fpm_config = php_optarg;
-+ break;
-+ case 'x':
-+ fpm = 1;
-+ break;
-+#endif
-
- }
-
-@@ -1459,6 +1551,19 @@
- #endif /* FORCE_CGI_REDIRECT */
-
- #if PHP_FASTCGI
-+#if PHP_FASTCGI_PM
-+ if (fpm) {
-+ if (0 > fpm_init(argc, argv, fpm_config)) {
-+ return FAILURE;
-+ }
-+
-+ fcgi_fd = fpm_run(&max_requests);
-+
-+ fcgi_set_is_fastcgi(fastcgi = 1);
-+ }
-+ else
-+#endif
-+
- if (bindpath) {
- fcgi_fd = fcgi_listen(bindpath, 128);
- if (fcgi_fd < 0) {
-@@ -1625,7 +1625,11 @@
-
- if (fastcgi) {
- /* How many times to run PHP scripts before dying */
-- if (getenv("PHP_FCGI_MAX_REQUESTS")) {
-+ if (
-+#if PHP_FASTCGI_PM
-+ !fpm &&
-+#endif
-+ getenv("PHP_FCGI_MAX_REQUESTS")) {
- max_requests = atoi(getenv("PHP_FCGI_MAX_REQUESTS"));
- if (max_requests < 0) {
- fprintf(stderr, "PHP_FCGI_MAX_REQUESTS is not valid\n");
-@@ -1677,7 +1677,11 @@
-
- #ifndef PHP_WIN32
- /* Pre-fork, if required */
-- if (getenv("PHP_FCGI_CHILDREN")) {
-+ if (
-+#if PHP_FASTCGI_PM
-+ !fpm &&
-+#endif
-+ getenv("PHP_FCGI_CHILDREN")) {
- char * children_str = getenv("PHP_FCGI_CHILDREN");
- children = atoi(children_str);
- if (children < 0) {
-@@ -1616,6 +1729,8 @@
- #endif
-
- #if PHP_FASTCGI
-+ request_body_fd = -1;
-+
- SG(server_context) = (void *) &request;
- #else
- SG(server_context) = (void *) 1; /* avoid server_context==NULL checks */
-@@ -1623,6 +1730,10 @@
- init_request_info(TSRMLS_C);
- CG(interactive) = 0;
-
-+#if PHP_FASTCGI_PM
-+ if (fpm) fpm_request_info();
-+#endif
-+
- if (!cgi
- #if PHP_FASTCGI
- && !fastcgi
-@@ -1914,6 +2025,10 @@
- }
- }
-
-+#if PHP_FASTCGI_PM
-+ if (fpm) fpm_request_executing();
-+#endif
-+
- switch (behavior) {
- case PHP_MODE_STANDARD:
- php_execute_script(&file_handle TSRMLS_CC);
-@@ -1966,6 +2081,10 @@
-
- #if PHP_FASTCGI
- fastcgi_request_done:
-+
-+ if (request_body_fd != -1) close(request_body_fd);
-+
-+ request_body_fd = -2;
- #endif
- {
- char *path_translated;
-@@ -1979,6 +2098,16 @@
- SG(request_info).path_translated = path_translated;
- }
-
-+ if (EG(exit_status) == 255) {
-+ if (CGIG(error_header) && *CGIG(error_header)) {
-+ sapi_header_line ctr = {0};
-+
-+ ctr.line = CGIG(error_header);
-+ ctr.line_len = strlen(CGIG(error_header));
-+ sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
-+ }
-+ }
-+
- php_request_shutdown((void *) 0);
- if (exit_status == 0) {
- exit_status = EG(exit_status);
-@@ -2016,15 +2145,20 @@
- if (bindpath) {
- free(bindpath);
- }
-- if (max_requests != 1) {
-- /* no need to return exit_status of the last request */
-- exit_status = 0;
-- }
- break;
- }
- /* end of fastcgi loop */
- }
- fcgi_shutdown();
-+
-+ if (fcgi_in_shutdown() || /* graceful shutdown by a signal */
-+ (max_requests && (requests == max_requests)) /* we were told to process max_requests and we are done */
-+ ) {
-+ exit_status = 0;
-+ }
-+ else {
-+ exit_status = 255;
-+ }
- #endif
-
- if (cgi_sapi_module.php_ini_path_override) {
-diff -Nru php-5.2.6.vanilla/sapi/cgi/config9.m4 php-5.2.6.fpm/sapi/cgi/config9.m4
---- php-5.2.6.vanilla/sapi/cgi/config9.m4 2007-07-12 03:20:36.000000000 +0400
-+++ php-5.2.6.fpm/sapi/cgi/config9.m4 2008-07-20 20:46:41.000000000 +0400
-@@ -22,6 +22,10 @@
- [ --disable-path-info-check CGI: If this is disabled, paths such as
- /info.php/test?a=b will fail to work], yes, no)
-
-+PHP_ARG_ENABLE(fpm,,
-+[ --enable-fpm FastCGI: If this is enabled, the fastcgi support
-+ will include experimental process manager code], no, no)
-+
- dnl
- dnl CGI setup
- dnl
-@@ -54,6 +58,20 @@
- AC_DEFINE_UNQUOTED(PHP_FASTCGI, $PHP_ENABLE_FASTCGI, [ ])
- AC_MSG_RESULT($PHP_FASTCGI)
-
-+ dnl --enable-fpm
-+ if test "$PHP_FASTCGI" = "yes"; then
-+ AC_MSG_CHECKING(whether to enable FastCGI Process Manager)
-+ if test "$PHP_FPM" = "yes"; then
-+ PHP_FASTCGI_PM=1
-+ else
-+ PHP_FASTCGI_PM=0
-+ fi
-+ AC_MSG_RESULT($PHP_FPM)
-+ else
-+ PHP_FASTCGI_PM=0
-+ fi
-+ AC_DEFINE_UNQUOTED(PHP_FASTCGI_PM, $PHP_FASTCGI_PM, [Is experimental fastcgi process manager code activated])
-+
- dnl --enable-force-cgi-redirect
- AC_MSG_CHECKING(whether to force Apache CGI redirect)
- if test "$PHP_FORCE_CGI_REDIRECT" = "yes"; then
-@@ -108,13 +108,13 @@
-
- case $host_alias in
- *aix*)
-- 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)"
-+ 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)"
- ;;
- *darwin*)
-- 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)"
-+ 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)"
- ;;
- *)
-- 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)"
-+ 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)"
- ;;
- esac
-
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fastcgi.c php-5.2.6.fpm/sapi/cgi/fastcgi.c
---- php-5.2.6.vanilla/sapi/cgi/fastcgi.c 2008-04-03 14:24:44.000000000 +0400
-+++ php-5.2.6.fpm/sapi/cgi/fastcgi.c 2008-05-15 23:37:01.000000000 +0400
-@@ -27,6 +27,11 @@
- #include <stdarg.h>
- #include <errno.h>
-
-+#if PHP_FASTCGI_PM
-+#include "fpm/fpm.h"
-+#include "fpm/fpm_request.h"
-+#endif
-+
- #ifdef _WIN32
-
- #include <windows.h>
-@@ -240,6 +245,8 @@
- } else {
- return is_fastcgi = 0;
- }
-+
-+ fcgi_set_allowed_clients(getenv("FCGI_WEB_SERVER_ADDRS"));
- #endif
- }
- return is_fastcgi;
-@@ -256,11 +256,27 @@
- }
- }
-
-+void fcgi_set_is_fastcgi(int new_value)
-+{
-+ is_fastcgi = new_value;
-+}
-+
-+void fcgi_set_in_shutdown(int new_value)
-+{
-+ in_shutdown = new_value;
-+}
++AC_DEFUN([AC_FPM_BUILD_SAPI],
++[
++ PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/sapi/fpm/ac/Makefile.frag,$abs_srcdir/sapi/fpm,sapi/fpm)
+
- void fcgi_shutdown(void)
- {
- if (is_initialized) {
- zend_hash_destroy(&fcgi_mgmt_vars);
- }
++ SAPI_FPM_PATH=sapi/fpm/$php_fpm_bin
++ PHP_SUBST(SAPI_FPM_PATH)
+
-+ if (allowed_clients) {
-+ free(allowed_clients);
-+ allowed_clients = 0;
-+ }
-+
- is_fastcgi = 0;
- }
-
-@@ -330,6 +352,41 @@
- }
- #endif
-
-+void fcgi_set_allowed_clients(char *ip)
-+{
-+ char *cur, *end;
-+ int n;
-+
-+ if (ip) {
-+ ip = strdup(ip);
-+ cur = ip;
-+ n = 0;
-+ while (*cur) {
-+ if (*cur == ',') n++;
-+ cur++;
-+ }
-+ if (allowed_clients) free(allowed_clients);
-+ allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
-+ n = 0;
-+ cur = ip;
-+ while (cur) {
-+ end = strchr(cur, ',');
-+ if (end) {
-+ *end = 0;
-+ end++;
-+ }
-+ allowed_clients[n] = inet_addr(cur);
-+ if (allowed_clients[n] == INADDR_NONE) {
-+ fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
-+ }
-+ n++;
-+ cur = end;
-+ }
-+ allowed_clients[n] = INADDR_NONE;
-+ free(ip);
-+ }
-+}
-+
- static int is_port_number(const char *bindpath)
- {
- while (*bindpath) {
-@@ -458,38 +515,6 @@
-
- if (!tcp) {
- chmod(path, 0777);
-- } else {
-- char *ip = getenv("FCGI_WEB_SERVER_ADDRS");
-- char *cur, *end;
-- int n;
--
-- if (ip) {
-- ip = strdup(ip);
-- cur = ip;
-- n = 0;
-- while (*cur) {
-- if (*cur == ',') n++;
-- cur++;
-- }
-- allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
-- n = 0;
-- cur = ip;
-- while (cur) {
-- end = strchr(cur, ',');
-- if (end) {
-- *end = 0;
-- end++;
-- }
-- allowed_clients[n] = inet_addr(cur);
-- if (allowed_clients[n] == INADDR_NONE) {
-- fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
-- }
-- n++;
-- cur = end;
-- }
-- allowed_clients[n] = INADDR_NONE;
-- free(ip);
-- }
- }
-
- if (!is_initialized) {
-@@ -829,7 +854,7 @@
- return n;
- }
-
--static inline void fcgi_close(fcgi_request *req, int force, int destroy)
-+void fcgi_close(fcgi_request *req, int force, int destroy)
- {
- if (destroy) {
- zend_hash_destroy(&req->env);
-@@ -869,6 +894,10 @@
- close(req->fd);
- #endif
- req->fd = -1;
-+
-+#if PHP_FASTCGI_PM
-+ if (fpm) fpm_request_finished();
-+#endif
- }
- }
-
-@@ -916,6 +945,10 @@
- sa_t sa;
- socklen_t len = sizeof(sa);
-
-+#if PHP_FASTCGI_PM
-+ if (fpm) fpm_request_accepting();
-+#endif
-+
- FCGI_LOCK(req->listen_socket);
- req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
- FCGI_UNLOCK(req->listen_socket);
-@@ -951,6 +984,11 @@
- break;
- #else
- if (req->fd >= 0) {
-+
-+#if PHP_FASTCGI_PM
-+ if (fpm) fpm_request_reading_headers();
-+#endif
-+
- #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
- struct pollfd fds;
- int ret;
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fastcgi.h php-5.2.6.fpm/sapi/cgi/fastcgi.h
---- php-5.2.6.vanilla/sapi/cgi/fastcgi.h 2007-12-31 10:20:16.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fastcgi.h 2008-05-01 20:42:06.000000000 +0400
-@@ -114,6 +114,9 @@
- int fcgi_init(void);
- void fcgi_shutdown(void);
- int fcgi_is_fastcgi(void);
-+void fcgi_set_is_fastcgi(int);
-+void fcgi_set_in_shutdown(int);
-+void fcgi_set_allowed_clients(char *);
- int fcgi_in_shutdown(void);
- int fcgi_listen(const char *path, int backlog);
- void fcgi_init_request(fcgi_request *req, int listen_socket);
-@@ -128,6 +131,8 @@
- int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len);
- int fcgi_flush(fcgi_request *req, int close);
-
-+void fcgi_close(fcgi_request *req, int force, int destroy);
++ mkdir -p sapi/fpm/cgi
++ PHP_FPM_SAPI_FILES=`cd $abs_srcdir/sapi/fpm && find cgi/ \( -name *.c \) -exec printf "{} " \;`
++ # PHP_FPM_SAPI_FILES="cgi/cgi_main.c cgi/fastcgi.c"
+
- #ifdef PHP_WIN32
- void fcgi_impersonate(void);
- #endif
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/acinclude.m4 php-5.2.6.fpm/sapi/cgi/fpm/acinclude.m4
---- php-5.2.6.vanilla/sapi/cgi/fpm/acinclude.m4 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/acinclude.m4 2008-07-21 06:39:38.000000000 +0400
-@@ -0,0 +1,383 @@
++ mkdir -p sapi/fpm/fpm
++ PHP_FPM_CORE_FILES=`cd $abs_srcdir/sapi/fpm && find fpm/ \( -name *.c -not -name fpm_trace*.c \) -exec printf "{} " \;`
++ # PHP_FPM_CORE_FILES="fpm/fpm_process_ctl.c fpm/fpm_signals.c fpm/fpm_shm.c fpm/fpm.c fpm/fpm_worker_pool.c fpm/fpm_clock.c fpm/fpm_env.c fpm/fpm_shm_slots.c fpm/fpm_children.c fpm/fpm_events.c fpm/fpm_php.c fpm/fpm_unix.c fpm/fpm_request.c fpm/fpm_sockets.c fpm/fpm_php_trace.c fpm/zlog.c fpm/fpm_cleanup.c fpm/fpm_conf.c fpm/xml_config.c fpm/fpm_stdio.c"
+
-+AC_DEFUN([AC_FPM_CHECK_FUNC],
-+[
-+ SAVED_CFLAGS="$CFLAGS"
-+ CFLAGS="$CFLAGS $2"
-+ SAVED_LIBS="$LIBS"
-+ LIBS="$LIBS $3"
++ if test "$fpm_trace_type" ; then
++ PHP_FPM_TRACE_FILES=`cd $abs_srcdir/sapi/fpm && find fpm/ \( -name fpm_trace.c -or -name fpm_trace_$fpm_trace_type.c \) -exec printf "{} " \;`
++ fi
++
++ PHP_FPM_CFLAGS="$LIBEVENT_CFLAGS -I$abs_srcdir/sapi/fpm"
+
-+ AC_CHECK_FUNC([$1],[$4],[$5])
++ SAPI_EXTRA_LIBS="$LIBEVENT_LIBS"
++ PHP_SUBST(SAPI_EXTRA_LIBS)
++
++ dnl Set install target and select SAPI
++ INSTALL_IT=":"
++
++ PHP_SELECT_SAPI(fpm, program, $PHP_FPM_SAPI_FILES $PHP_FPM_CORE_FILES $PHP_FPM_TRACE_FILES, $PHP_FPM_CFLAGS, '$(SAPI_FPM_PATH)')
++
++ case $host_alias in
++ *aix*)
++ BUILD_FPM="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_FPM_PATH)"
++ ;;
++ *darwin*)
++ BUILD_FPM="\$(CC) \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(NATIVE_RPATHS) \$(PHP_GLOBAL_OBJS:.lo=.o) \$(PHP_SAPI_OBJS:.lo=.o) \$(PHP_FRAMEWORKS) \$(EXTRA_LIBS) \$(SAPI_EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_FPM_PATH)"
++ ;;
++ *)
++ BUILD_FPM="\$(LIBTOOL) --mode=link \$(CC) -export-dynamic \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(PHP_RPATHS) \$(PHP_GLOBAL_OBJS) \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(SAPI_EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_FPM_PATH)"
++ ;;
++ esac
++
++ ENDIF=endif
++ PHP_SUBST(ENDIF)
++ PHP_SUBST(BUILD_FPM)
+
-+ CFLAGS="$SAVED_CFLAGS"
-+ LIBS="$SAVED_LIBS"
+])
-+
-+AC_DEFUN([AC_FPM_LIBEVENT],
+diff -Naur php-src-vanilla/sapi/fpm/ac/fpm_checks.m4 php-src/sapi/fpm/ac/fpm_checks.m4
+--- php-src-vanilla/sapi/fpm/ac/fpm_checks.m4 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/ac/fpm_checks.m4 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,299 @@
++dnl
++dnl $Id$
++dnl
++
++AC_DEFUN([AC_FPM_CHECKS],
+[
-+ AC_ARG_WITH([libevent],
-+ [ --with-libevent=DIR FPM: libevent install directory])
-+
-+ LIBEVENT_CFLAGS=""
-+ LIBEVENT_LIBS="-levent"
-+ LIBEVENT_INCLUDE_PATH=""
-+
-+ if test "$with_libevent" != "no" -a -n "$with_libevent"; then
-+ LIBEVENT_CFLAGS="-I$with_libevent/include"
-+ LIBEVENT_LIBS="-L$with_libevent/lib $LIBEVENT_LIBS"
-+ LIBEVENT_INCLUDE_PATH="$with_libevent/include"
-+ fi
-+
-+ AC_MSG_CHECKING([for event.h])
-+
-+ found=no
-+
-+ for dir in "$LIBEVENT_INCLUDE_PATH" /usr/include ; do
-+ if test -r "$dir/event.h" ; then
-+ found=yes
-+ break
-+ fi
-+ done
-+
-+ AC_MSG_RESULT([$found])
-+
-+ AC_FPM_CHECK_FUNC([event_set], [$LIBEVENT_CFLAGS], [$LIBEVENT_LIBS], ,
-+ [AC_MSG_ERROR([Failed to link with libevent. Perhaps --with-libevent=DIR option could help.])])
-+
-+ AC_FPM_CHECK_FUNC([event_base_free], [$LIBEVENT_CFLAGS], [$LIBEVENT_LIBS], ,
-+ [AC_MSG_ERROR([You have too old version. libevent version >= 1.2 is required.])])
-+
++ AC_FPM_STDLIBS
++ AC_FPM_PRCTL
++ AC_FPM_CLOCK
++ AC_FPM_TRACE
+])
+
-+AC_DEFUN([AC_FPM_LIBXML],
++AC_DEFUN([AC_FPM_STDLIBS],
+[
-+ AC_MSG_RESULT([checking for XML configuration])
-+
-+ AC_ARG_WITH(xml-config,
-+ [ --with-xml-config=PATH FPM: use xml-config in PATH to find libxml],
-+ [XMLCONFIG="$withval"],
-+ [AC_PATH_PROGS(XMLCONFIG, [xml2-config xml-config], "")]
-+ )
-+
-+ if test "x$XMLCONFIG" = "x"; then
-+ AC_MSG_ERROR([XML configuration could not be found])
-+ else
-+ AC_MSG_CHECKING([for libxml library])
-+
-+ if test ! -x "$XMLCONFIG"; then
-+ AC_MSG_ERROR([$XMLCONFIG cannot be executed])
-+ fi
-+
-+ LIBXML_LIBS="`$XMLCONFIG --libs`"
-+ LIBXML_CFLAGS="`$XMLCONFIG --cflags`"
-+ LIBXML_VERSION="`$XMLCONFIG --version`"
++ AC_CHECK_FUNCS(setenv clearenv)
+
-+ AC_MSG_RESULT([yes, $LIBXML_VERSION])
++ AC_SEARCH_LIBS(socket, socket)
++ AC_SEARCH_LIBS(inet_addr, nsl)
+
-+ AC_FPM_CHECK_FUNC([xmlParseFile], [$LIBXML_CFLAGS], [$LIBXML_LIBS], ,
-+ [AC_MSG_ERROR([Failed to link with libxml])])
-+
-+ AC_DEFINE(HAVE_LIBXML, 1, [do we have libxml?])
-+ fi
++ AC_CHECK_HEADERS([errno.h fcntl.h stdio.h stdlib.h unistd.h sys/uio.h])
++ AC_CHECK_HEADERS([sys/select.h sys/socket.h sys/time.h])
++ AC_CHECK_HEADERS([arpa/inet.h netinet/in.h])
+])
+
-+AC_DEFUN([AC_FPM_JUDY],
++AC_DEFUN([AC_FPM_PRCTL],
+[
-+ AC_ARG_WITH([Judy],
-+ [ --with-Judy=DIR FPM: Judy install directory])
-+
-+ JUDY_CFLAGS=""
-+ JUDY_LIBS="-lJudy"
-+ JUDY_INCLUDE_PATH=""
-+
-+ if test "$with_Judy" != "no" -a -n "$with_Judy"; then
-+ JUDY_INCLUDE_PATH="$with_Judy/include"
-+ JUDY_CFLAGS="-I$with_Judy/include $JUDY_CFLAGS"
-+ JUDY_LIBS="-L$with_Judy/lib $JUDY_LIBS"
-+ fi
-+
-+ AC_MSG_CHECKING([for Judy.h])
-+
-+ found=no
-+
-+ for dir in "$JUDY_INCLUDE_PATH" /usr/include ; do
-+ if test -r "$dir/Judy.h" ; then
-+ found=yes
-+ break
-+ fi
-+ done
-+
-+ AC_MSG_RESULT([$found])
-+
-+ AC_FPM_CHECK_FUNC([JudyLCount], [$JUDY_CFLAGS], [$JUDY_LIBS], ,
-+ [AC_MSG_ERROR([Failed to link with Judy])])
++ AC_MSG_CHECKING([for prctl])
+
++ AC_TRY_COMPILE([ #include <sys/prctl.h> ], [prctl(0, 0, 0, 0, 0);], [
++ AC_DEFINE([HAVE_PRCTL], 1, [do we have prctl?])
++ AC_MSG_RESULT([yes])
++ ], [
++ AC_MSG_RESULT([no])
++ ])
+])
+
+AC_DEFUN([AC_FPM_CLOCK],
+
+ AC_MSG_CHECKING([for clock_gettime])
+
-+ AC_TRY_COMPILE([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
++ AC_TRY_LINK([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
+ have_clock_gettime=yes
+ AC_MSG_RESULT([yes])
+ ], [
+ SAVED_LIBS="$LIBS"
+ LIBS="$LIBS -lrt"
+
-+ AC_TRY_COMPILE([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
++ AC_TRY_LINK([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
+ have_clock_gettime=yes
+ AC_MSG_RESULT([yes])
+ ], [
+ #define PTRACE_DETACH PT_DETACH
+ #endif
+
-+ #if !defined(PTRACE_PEEKDATA) && defined(PT_READ_D)
-+ #define PTRACE_PEEKDATA PT_READ_D
-+ #endif
++ #if !defined(PTRACE_PEEKDATA) && defined(PT_READ_D)
++ #define PTRACE_PEEKDATA PT_READ_D
++ #endif
++
++ int main()
++ {
++ long v1 = (unsigned int) -1; /* copy will fail if sizeof(long) == 8 and we've got "int ptrace()" */
++ long v2;
++ pid_t child;
++ int status;
++
++ if ( (child = fork()) ) { /* parent */
++ int ret = 0;
++
++ if (0 > ptrace(PTRACE_ATTACH, child, 0, 0)) {
++ return 1;
++ }
++
++ waitpid(child, &status, 0);
++
++ #ifdef PT_IO
++ struct ptrace_io_desc ptio = {
++ .piod_op = PIOD_READ_D,
++ .piod_offs = &v1,
++ .piod_addr = &v2,
++ .piod_len = sizeof(v1)
++ };
++
++ if (0 > ptrace(PT_IO, child, (void *) &ptio, 0)) {
++ ret = 1;
++ }
++ #else
++ errno = 0;
++
++ v2 = ptrace(PTRACE_PEEKDATA, child, (void *) &v1, 0);
++
++ if (errno) {
++ ret = 1;
++ }
++ #endif
++ ptrace(PTRACE_DETACH, child, (void *) 1, 0);
++
++ kill(child, SIGKILL);
++
++ return ret ? ret : (v1 != v2);
++ }
++ else { /* child */
++ sleep(10);
++ return 0;
++ }
++ }
++ ], [
++ AC_MSG_RESULT([yes])
++ ], [
++ have_ptrace=no
++ have_broken_ptrace=yes
++ AC_MSG_RESULT([no])
++ ])
++ fi
++
++ if test "$have_ptrace" = "yes"; then
++ AC_DEFINE([HAVE_PTRACE], 1, [do we have ptrace?])
++ fi
++
++ have_mach_vm_read=no
++
++ if test "$have_broken_ptrace" = "yes"; then
++ AC_MSG_CHECKING([for mach_vm_read])
++
++ AC_TRY_COMPILE([ #include <mach/mach.h>
++ #include <mach/mach_vm.h>
++ ], [
++ 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);
++ ], [
++ have_mach_vm_read=yes
++ AC_MSG_RESULT([yes])
++ ], [
++ AC_MSG_RESULT([no])
++ ])
++ fi
++
++ if test "$have_mach_vm_read" = "yes"; then
++ AC_DEFINE([HAVE_MACH_VM_READ], 1, [do we have mach_vm_read?])
++ fi
++
++ proc_mem_file=""
++
++ if test -r /proc/$$/mem ; then
++ proc_mem_file="mem"
++ else
++ if test -r /proc/$$/as ; then
++ proc_mem_file="as"
++ fi
++ fi
++
++ if test -n "$proc_mem_file" ; then
++ AC_MSG_CHECKING([for proc mem file])
++
++ AC_TRY_RUN([
++ #define _GNU_SOURCE
++ #define _FILE_OFFSET_BITS 64
++ #if HAVE_INTTYPES_H
++ #include <inttypes.h>
++ #else
++ #include <stdint.h>
++ #endif
++
++ #include <unistd.h>
++ #include <sys/types.h>
++ #include <sys/stat.h>
++ #include <fcntl.h>
++ #include <stdio.h>
++ int main()
++ {
++ long v1 = (unsigned int) -1, v2 = 0;
++ char buf[128];
++ int fd;
++ sprintf(buf, "/proc/%d/$proc_mem_file", getpid());
++ fd = open(buf, O_RDONLY);
++ if (0 > fd) {
++ return 1;
++ }
++ if (sizeof(long) != pread(fd, &v2, sizeof(long), (uintptr_t) &v1)) {
++ close(fd);
++ return 1;
++ }
++ close(fd);
++ return v1 != v2;
++ }
++ ], [
++ AC_MSG_RESULT([$proc_mem_file])
++ ], [
++ proc_mem_file=""
++ AC_MSG_RESULT([no])
++ ])
++ fi
++
++ if test -n "$proc_mem_file"; then
++ AC_DEFINE_UNQUOTED([PROC_MEM_FILE], "$proc_mem_file", [/proc/pid/mem interface])
++ fi
++
++ fpm_trace_type=""
++
++ if test "$have_ptrace" = "yes"; then
++ fpm_trace_type=ptrace
++
++ elif test -n "$proc_mem_file"; then
++ fpm_trace_type=pread
++
++ elif test "$have_mach_vm_read" = "yes" ; then
++ fpm_trace_type=mach
++
++ else
++ AC_MSG_ERROR([FPM Trace - ptrace, pread, or mach: could not be found])
++ fi
++
++])
++
+diff -Naur php-src-vanilla/sapi/fpm/ac/fpm_conf.m4 php-src/sapi/fpm/ac/fpm_conf.m4
+--- php-src-vanilla/sapi/fpm/ac/fpm_conf.m4 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/ac/fpm_conf.m4 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,188 @@
++
++AC_DEFUN([AC_FPM_ARGS],
++[
++ PHP_ARG_WITH(fpm-bin,,
++ [ --with-fpm-bin[=PATH] Set the path for the php-fpm binary [/usr/local/bin/php-fpm]], yes, no)
++
++ PHP_ARG_WITH(fpm-port,,
++ [ --with-fpm-port[=PORT] Set the tcp port number to listen for cgi requests [9000]], yes, no)
++
++ PHP_ARG_WITH(fpm-conf,,
++ [ --with-fpm-conf[=PATH] Set the path for php-fpm configuration file [/etc/php-fpm.conf]], yes, no)
++
++ PHP_ARG_WITH(fpm-init,,
++ [ --with-fpm-init[=PATH] Set the path for php-fpm init file [/etc/init.d/php-fpm]], yes, no)
++
++ PHP_ARG_WITH(fpm-log,,
++ [ --with-fpm-log[=PATH] Set the path for php-fpm log file [/var/log/php-fpm.log]], yes, no)
++
++ PHP_ARG_WITH(fpm-pid,,
++ [ --with-fpm-pid[=PATH] Set the path for php-fpm pid file [/var/run/php-fpm.pid]], yes, no)
++
++ PHP_ARG_WITH(fpm-user,,
++ [ --with-fpm-user[=USER] Set the user for php-fpm to run as [nobody]], yes, no)
++
++ PHP_ARG_WITH(fpm-group,,
++ [ --with-fpm-group[=GRP] Set the group for php-fpm to run as. For a system user,
++ this should be set to match the fpm username [nobody]], yes, no)
++])
++
++AC_DEFUN([AC_FPM_VARS],
++[
++ fpm_prefix=$ac_default_prefix
++ if test $prefix != "NONE" -a $prefix != "" -a $prefix != "no" ; then
++ fpm_prefix=$prefix
++ else
++ prefix=$fpm_prefix
++ fi
++
++ if test $exec_prefix = "NONE" -o $exec_prefix = "" -o $exec_prefix = "no" ; then
++ exec_prefix=$fpm_prefix
++ fi
++
++ if test `echo "$bindir" | grep "exec_prefix"` ; then
++ bindir=$exec_prefix/bin
++ fi
++
++ fpm_bin_prefix=$fpm_prefix/bin
++ if test $bindir != "NONE" -a $bindir != "" -a $bindir != "no" ; then
++ fpm_bin_prefix=$bindir
++ fi
++
++ if test -z "$PHP_FPM_BIN" -o "$PHP_FPM_BIN" = "yes" -o "$PHP_FPM_BIN" = "no"; then
++ php_fpm_bin_path="$fpm_bin_prefix/php-fpm"
++ else
++ php_fpm_bin_path="$PHP_FPM_BIN"
++ fi
++ php_fpm_bin=`basename $php_fpm_bin_path`
++ php_fpm_bin_dir=`dirname $php_fpm_bin_path`
++
++ if test -z "$PHP_FPM_PORT" -o "$PHP_FPM_PORT" = "yes" -o "$PHP_FPM_PORT" = "no"; then
++ php_fpm_port="9000"
++ else
++ php_fpm_port="$PHP_FPM_PORT"
++ fi
++
++ if test -z "$PHP_FPM_CONF" -o "$PHP_FPM_CONF" = "yes"; then
++ case $host_os in
++ freebsd*|dragonfly*) php_fpm_conf_path="/usr/local/etc/php-fpm.conf" ;;
++ *) php_fpm_conf_path="/etc/php-fpm.conf" ;;
++ esac
++ elif test "$PHP_FPM_CONF" = "no"; then
++ php_fpm_conf_path=""
++ else
++ php_fpm_conf_path="$PHP_FPM_CONF"
++ fi
++ if test -z "$php_fpm_conf_path"; then
++ php_fpm_conf=""
++ php_fpm_conf_dir=""
++ else
++ php_fpm_conf=`basename $php_fpm_conf_path`
++ php_fpm_conf_dir=`dirname $php_fpm_conf_path`
++ fi
++
++ if test -z "$PHP_FPM_INIT" -o "$PHP_FPM_INIT" = "yes"; then
++ case $host_os in
++ openbsd*) php_fpm_init_path="" ;;
++ netbsd*) php_fpm_init_path="/etc/rc.d/php-fpm" ;;
++ *bsd*|dragonfly*) php_fpm_init_path="/usr/local/etc/rc.d/php-fpm" ;;
++ *) php_fpm_init_path="/etc/init.d/php-fpm" ;;
++ esac
++ test -f /etc/arch-release && php_fpm_init_path="/etc/rc.d/php-fpm" # arch linux
++
++ elif test "$PHP_FPM_INIT" = "no"; then
++ php_fpm_init_path=""
++ else
++ php_fpm_init_path="$PHP_FPM_INIT"
++ fi
++ if test -z "$php_fpm_init_path"; then
++ php_fpm_init=""
++ php_fpm_init_dir=""
++ else
++ php_fpm_init=`basename $php_fpm_init_path`
++ php_fpm_init_dir=`dirname $php_fpm_init_path`
++ fi
++
++ if test -z "$PHP_FPM_LOG" -o "$PHP_FPM_LOG" = "yes" -o "$PHP_FPM_LOG" = "no"; then
++ php_fpm_log_path="/var/log/php-fpm.log"
++ else
++ php_fpm_log_path="$PHP_FPM_LOG"
++ fi
++ php_fpm_log_dir=`dirname $php_fpm_log_path`
++
++ if test -z "$PHP_FPM_PID" -o "$PHP_FPM_PID" = "yes" -o "$PHP_FPM_PID" = "no"; then
++ php_fpm_pid_path="/var/run/php-fpm.pid"
++ else
++ php_fpm_pid_path="$PHP_FPM_PID"
++ fi
++ php_fpm_pid_dir=`dirname $php_fpm_pid_path`
++
++ if test -z "$PHP_FPM_USER" -o "$PHP_FPM_USER" = "yes" -o "$PHP_FPM_USER" = "no"; then
++ php_fpm_user="nobody"
++ else
++ php_fpm_user="$PHP_FPM_USER"
++ fi
++
++ if test -z "$PHP_FPM_GROUP" -o "$PHP_FPM_GROUP" = "yes" -o "$PHP_FPM_GROUP" = "no"; then
++ php_fpm_group="nobody"
++ else
++ php_fpm_group="$PHP_FPM_GROUP"
++ fi
++
++
++ PHP_SUBST_OLD(fpm_version)
++ PHP_SUBST_OLD(php_fpm_bin)
++ PHP_SUBST_OLD(php_fpm_bin_dir)
++ PHP_SUBST_OLD(php_fpm_bin_path)
++ PHP_SUBST_OLD(php_fpm_port)
++ PHP_SUBST_OLD(php_fpm_conf)
++ PHP_SUBST_OLD(php_fpm_conf_dir)
++ PHP_SUBST_OLD(php_fpm_conf_path)
++ PHP_SUBST_OLD(php_fpm_init)
++ PHP_SUBST_OLD(php_fpm_init_dir)
++ PHP_SUBST_OLD(php_fpm_init_path)
++ PHP_SUBST_OLD(php_fpm_log_dir)
++ PHP_SUBST_OLD(php_fpm_log_path)
++ PHP_SUBST_OLD(php_fpm_pid_dir)
++ PHP_SUBST_OLD(php_fpm_pid_path)
++ PHP_SUBST_OLD(php_fpm_user)
++ PHP_SUBST_OLD(php_fpm_group)
++
++
++ AC_DEFINE_UNQUOTED(PHP_FPM_VERSION, "$fpm_version", [fpm version])
++ AC_DEFINE_UNQUOTED(PHP_FPM_BIN, "$php_fpm_bin", [fpm binary executable])
++ AC_DEFINE_UNQUOTED(PHP_FPM_BIN_DIR, "$php_fpm_bin_dir", [fpm binary dir])
++ AC_DEFINE_UNQUOTED(PHP_FPM_BIN_PATH, "$php_fpm_bin_path", [fpm bin file path])
++ AC_DEFINE_UNQUOTED(PHP_FPM_PORT, "$php_fpm_port", [tcp port])
++ AC_DEFINE_UNQUOTED(PHP_FPM_CONF, "$php_fpm_conf", [fpm conf file])
++ AC_DEFINE_UNQUOTED(PHP_FPM_CONF_DIR, "$php_fpm_conf_dir", [fpm conf dir])
++ AC_DEFINE_UNQUOTED(PHP_FPM_CONF_PATH, "$php_fpm_conf_path", [fpm conf file path])
++ AC_DEFINE_UNQUOTED(PHP_FPM_INIT, "$php_fpm_init", [fpm init file])
++ AC_DEFINE_UNQUOTED(PHP_FPM_INIT_DIR, "$php_fpm_init_dir", [fpm init dir])
++ AC_DEFINE_UNQUOTED(PHP_FPM_INIT_PATH, "$php_fpm_init_path", [fpm init file path])
++ AC_DEFINE_UNQUOTED(PHP_FPM_LOG_DIR, "$php_fpm_log_dir", [fpm log dir])
++ AC_DEFINE_UNQUOTED(PHP_FPM_LOG_PATH, "$php_fpm_log_path", [fpm log file path])
++ AC_DEFINE_UNQUOTED(PHP_FPM_PID_DIR, "$php_fpm_pid_dir", [fpm pid dir])
++ AC_DEFINE_UNQUOTED(PHP_FPM_PID_PATH, "$php_fpm_pid_path", [fpm pid file path])
++ AC_DEFINE_UNQUOTED(PHP_FPM_USER, "$php_fpm_user", [fpm user name])
++ AC_DEFINE_UNQUOTED(PHP_FPM_GROUP, "$php_fpm_group", [fpm group name])
++
++])
++
++
++AC_DEFUN([AC_FPM_OUTPUT],
++[
++ PHP_OUTPUT(sapi/fpm/php_fpm.conf:sapi/fpm/conf/php-fpm.conf.in)
++ PHP_OUTPUT(sapi/fpm/init.d.php_fpm:sapi/fpm/conf/init.d.php-fpm.in)
++ PHP_OUTPUT(sapi/fpm/nginx-site-conf.sample:sapi/fpm/conf/nginx-site-conf.sample.in)
++ PHP_OUTPUT(sapi/fpm/$php_fpm_bin.1:sapi/fpm/man/php-fpm.1.in)
++])
++
++
++AC_DEFUN([AC_FPM_CONF],
++[
++ AC_FPM_ARGS
++ AC_FPM_VARS
++ AC_FPM_OUTPUT
++])
++
+diff -Naur php-src-vanilla/sapi/fpm/ac/fpm_libevent.m4 php-src/sapi/fpm/ac/fpm_libevent.m4
+--- php-src-vanilla/sapi/fpm/ac/fpm_libevent.m4 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/ac/fpm_libevent.m4 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,250 @@
++dnl @synopsis AC_LIB_EVENT([MINIMUM-VERSION],[REQUIRED-VERSION])
++dnl
++dnl Test for the libevent library of a particular version (or newer).
++dnl Source: http://svn.apache.org/repos/asf/incubator/thrift/trunk/aclocal/ax_lib_event.m4
++dnl Modified: This file was modified for autoconf-2.13 and the PHP_ARG_WITH macro.
++dnl
++dnl If no path to the installed libevent is given, the macro will first try
++dnl using no -I or -L flags, then searches under /usr, /usr/local, /opt,
++dnl and /opt/libevent.
++dnl If these all fail, it will try the $LIBEVENT_ROOT environment variable.
++dnl
++dnl This macro requires that #include <sys/types.h> works and defines u_char.
++dnl
++dnl This macro calls:
++dnl AC_SUBST(LIBEVENT_CFLAGS)
++dnl AC_SUBST(LIBEVENT_LIBS)
++dnl
++dnl And (if libevent is found):
++dnl AC_DEFINE(HAVE_LIBEVENT)
++dnl
++dnl It also leaves the shell variables "success" and "ac_have_libevent"
++dnl set to "yes" or "no".
++dnl
++dnl NOTE: This macro does not currently work for cross-compiling,
++dnl but it can be easily modified to allow it. (grep "cross").
++dnl
++dnl @category InstalledPackages
++dnl @category C
++dnl @version 2007-09-12
++dnl @license AllPermissive
++dnl
++dnl Copyright (C) 2009 David Reiss
++dnl Copying and distribution of this file, with or without modification,
++dnl are permitted in any medium without royalty provided the copyright
++dnl notice and this notice are preserved.
++
++AC_DEFUN([AC_LIB_EVENT_DO_CHECK],
++[
++# Save our flags.
++CPPFLAGS_SAVED="$CPPFLAGS"
++LDFLAGS_SAVED="$LDFLAGS"
++LIBS_SAVED="$LIBS"
++LD_LIBRARY_PATH_SAVED="$LD_LIBRARY_PATH"
++
++# Set our flags if we are checking a specific directory.
++if test -n "$ac_libevent_path" ; then
++ LIBEVENT_CPPFLAGS="-I$ac_libevent_path/include"
++ LIBEVENT_LDFLAGS="-L$ac_libevent_path/lib"
++ LD_LIBRARY_PATH="$ac_libevent_path/lib:$LD_LIBRARY_PATH"
++else
++ LIBEVENT_CPPFLAGS=""
++ LIBEVENT_LDFLAGS=""
++fi
++
++# Required flag for libevent.
++LIBEVENT_LIBS="-levent"
++
++# Prepare the environment for compilation.
++CPPFLAGS="$CPPFLAGS $LIBEVENT_CPPFLAGS"
++LDFLAGS="$LDFLAGS $LIBEVENT_LDFLAGS"
++LIBS="$LIBS $LIBEVENT_LIBS"
++export CPPFLAGS
++export LDFLAGS
++export LIBS
++export LD_LIBRARY_PATH
++
++success=no
++
++# Compile, link, and run the program. This checks:
++# - event.h is available for including.
++# - event_get_version() is available for linking.
++# - The event version string is lexicographically greater
++# than the required version.
++AC_TRY_RUN([
++#include <sys/types.h>
++#include <event.h>
++
++int main(int argc, char *argv[])
++{
++ const char* lib_version = event_get_version();
++ const char* wnt_version = "$WANT_LIBEVENT_VERSION";
++ for (;;) {
++ /* If we reached the end of the want version. We have it. */
++ if (*wnt_version == '\0' || *wnt_version == '-') {
++ return 0;
++ }
++ /* If the want version continues but the lib version does not, */
++ /* we are missing a letter. We don't have it. */
++ if (*lib_version == '\0' || *lib_version == '-') {
++ return 1;
++ }
++
++ /* In the 1.4 version numbering style, if there are more digits */
++ /* in one version than the other, that one is higher. */
++ int lib_digits;
++ for (lib_digits = 0;
++ lib_version[lib_digits] >= '0' &&
++ lib_version[lib_digits] <= '9';
++ lib_digits++)
++ ;
++ int wnt_digits;
++ for (wnt_digits = 0;
++ wnt_version[wnt_digits] >= '0' &&
++ wnt_version[wnt_digits] <= '9';
++ wnt_digits++)
++ ;
++ if (lib_digits > wnt_digits) {
++ return 0;
++ }
++ if (lib_digits < wnt_digits) {
++ return 1;
++ }
++ /* If we have greater than what we want. We have it. */
++ if (*lib_version > *wnt_version) {
++ return 0;
++ }
++ /* If we have less, we don't. */
++ if (*lib_version < *wnt_version) {
++ return 1;
++ }
++ lib_version++;
++ wnt_version++;
++ }
++ return 0;
++}
++],[
++success=yes
++])
++
++# Restore flags.
++LIBEVENT_LIBS=""
++CPPFLAGS="$CPPFLAGS_SAVED"
++LDFLAGS="$LDFLAGS_SAVED"
++LIBS="$LIBS_SAVED"
++LD_LIBRARY_PATH="$LD_LIBRARY_PATH_SAVED"
++])
++
++AC_DEFUN([AC_LIB_EVENT],
++[
++
++PHP_ARG_WITH(libevent,,
++[ --with-libevent[=PATH] Path to the libevent, needed for fpm SAPI [/usr/local]], yes, yes)
++
++if test "$PHP_LIBEVENT" != "no"; then
++ LIBEVENT_MIN_VERSION=ifelse([$1], ,1.4.3,$1)
++ LIBEVENT_REQ_VERSION=ifelse([$2], ,1.4.11,$2)
++
++ # Default library search paths ($sys_lib_search_path_spec)
++ AC_LIBTOOL_SYS_DYNAMIC_LINKER
++
++ libevent_prefix=$ac_default_prefix
++ if test $prefix != "NONE" -a $prefix != "" -a $prefix != "no" ; then
++ libevent_prefix=$prefix
++ fi
++
++ if test "$PHP_LIBEVENT" = "yes"; then
++ PHP_LIBEVENT=$libevent_prefix
++ fi
++
++ AC_MSG_CHECKING(for libevent >= $LIBEVENT_REQ_VERSION)
++ for ac_libevent_path in "" $PHP_LIBEVENT /usr /usr/local /opt /opt/local /opt/libevent ; do
++ WANT_LIBEVENT_VERSION="$LIBEVENT_REQ_VERSION"
++ AC_LIB_EVENT_DO_CHECK
++ if test "$success" = "yes"; then
++ break;
++ fi
++ done
++ if test "$success" = "no"; then
++
++ AC_MSG_RESULT(no)
++ AC_MSG_WARN([Could not find libevent $LIBEVENT_REQ_VERSION.])
++ AC_MSG_WARN([The use of earlier versions of libevent is not recommended])
++ AC_MSG_WARN([and can result in unspecified or unsupported behaviour.])
++
++ AC_MSG_CHECKING(for minimum libevent version >= $LIBEVENT_MIN_VERSION)
++ for ac_libevent_path in "" $PHP_LIBEVENT /usr /usr/local /opt /opt/local /opt/libevent ; do
++ WANT_LIBEVENT_VERSION="$LIBEVENT_MIN_VERSION"
++ AC_LIB_EVENT_DO_CHECK
++ if test "$success" = "yes"; then
++ break;
++ fi
++ done
++ if test "$success" = "no"; then
++ AC_MSG_RESULT(no)
++ LIBEVENT_LIBS=""
++ ac_have_libevent=no
++ AC_MSG_WARN([Syntax:])
++ AC_MSG_WARN([--with-libevent=yes|[path] - link to libevent.a (static library)])
++ AC_MSG_WARN([--with-libevent=shared[,path] - link to libevent.so (shared library)])
++ AC_MSG_ERROR([Libevent minimum version >= $LIBEVENT_MIN_VERSION could not be found.])
++ fi
++ fi
++
++ if test "$ext_shared" = "yes"; then
++ if test -n "$ac_libevent_path"; then
++ LIBEVENT_LIBS="-L$ac_libevent_path/lib -levent"
++ else
++ LIBEVENT_LIBS="-levent"
++ fi
++ else
++ libevent_a="libevent.a"
++ if test -n "$ac_libevent_path"; then
++ if test -f "$ac_libevent_path/lib/$libevent_a" ; then
++ LIBEVENT_LIBS="$ac_libevent_path/lib/$libevent_a"
++ fi
++ if test -z "$LIBEVENT_LIBS"; then
++ AC_MSG_RESULT(no)
++ AC_MSG_WARN([libevent.a could not be found. We looked in:])
++ AC_MSG_WARN([\"$ac_libevent_path\"])
++ fi
++ else
++ for search_path in $sys_lib_search_path_spec ; do
++ if test -f "$search_path$libevent_a" ; then
++ LIBEVENT_LIBS="$search_path$libevent_a"
++ break;
++ fi
++ done
++ if test -z "$LIBEVENT_LIBS"; then
++ AC_MSG_RESULT(no)
++ AC_MSG_WARN([libevent.a could not be found. We looked in:])
++ AC_MSG_WARN([\"$sys_lib_search_path_spec\"])
++ fi
++ fi
++ if test -z "$LIBEVENT_LIBS"; then
++ AC_MSG_WARN([Install libevent system-wide (make install)])
++ AC_MSG_WARN([Syntax:])
++ AC_MSG_WARN([--with-libevent=yes|[path] - link to libevent.a (static library)])
++ AC_MSG_WARN([--with-libevent=shared[,path] - link to libevent.so (shared library)])
++ AC_MSG_ERROR([libevent.a could not be found. Stop.])
++ fi
++ fi
++
++ if test "$success" = "yes" ; then
++ AC_MSG_RESULT(yes)
++ ac_have_libevent=yes
++ AC_DEFINE(HAVE_LIBEVENT, 1, [define if libevent is available])
++ fi
++
++ if test -n "$ac_libevent_path"; then
++ LIBEVENT_CFLAGS="-I$ac_libevent_path/include"
++ fi
++
++ AC_SUBST(LIBEVENT_CFLAGS)
++ AC_SUBST(LIBEVENT_LIBS)
++
++else
++ AC_MSG_ERROR([FPM Requires Libevent. You must build this target --with-libevent. Stop.])
++fi
++
++])
+diff -Naur php-src-vanilla/sapi/fpm/ac/Makefile.frag php-src/sapi/fpm/ac/Makefile.frag
+--- php-src-vanilla/sapi/fpm/ac/Makefile.frag 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/ac/Makefile.frag 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,61 @@
++fpm: $(SAPI_FPM_PATH)
++
++$(SAPI_FPM_PATH): $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS) $(SAPI_EXTRA_DEPS)
++ $(BUILD_FPM)
++
++install: install-fpm
++
++install-fpm: all
++ @echo "Installing PHP FPM binary: $(INSTALL_ROOT)$(php_fpm_bin_path)"
++ @$(mkinstalldirs) $(INSTALL_ROOT)$(php_fpm_bin_dir)
++ @$(mkinstalldirs) $(INSTALL_ROOT)$(php_fpm_pid_dir)
++ @$(mkinstalldirs) $(INSTALL_ROOT)$(php_fpm_log_dir)
++ @$(INSTALL) -m 0755 $(SAPI_FPM_PATH) $(INSTALL_ROOT)$(php_fpm_bin_path)$(program_suffix)$(EXEEXT)
++
++ @test "$(php_fpm_conf)" && \
++ echo "Installing PHP FPM config: $(INSTALL_ROOT)$(php_fpm_conf_path)" && \
++ $(mkinstalldirs) $(INSTALL_ROOT)$(php_fpm_conf_dir) || :
++
++ @test "$(php_fpm_conf)" && \
++ test -f "$(INSTALL_ROOT)$(php_fpm_conf_path)" && \
++ $(INSTALL_DATA) --backup=numbered $(INSTALL_ROOT)$(php_fpm_conf_path) $(INSTALL_ROOT)$(php_fpm_conf_path).old || :
++
++ @test "$(php_fpm_conf)" && \
++ $(INSTALL_DATA) sapi/fpm/php_fpm.conf $(INSTALL_ROOT)$(php_fpm_conf_path).default && \
++ ln -sf $(INSTALL_ROOT)$(php_fpm_conf_path).default $(INSTALL_ROOT)$(php_fpm_conf_path) || :
++
++ @echo "Installing PHP FPM man page: $(INSTALL_ROOT)$(mandir)/man1/$(php_fpm_bin)$(program_suffix).1"
++ @$(mkinstalldirs) $(INSTALL_ROOT)$(mandir)/man1
++ @$(INSTALL_DATA) sapi/fpm/$(php_fpm_bin).1 $(INSTALL_ROOT)$(mandir)/man1/$(php_fpm_bin)$(program_suffix).1
++
++ @test "$(php_fpm_init)" && \
++ echo "Installing PHP FPM init script: $(INSTALL_ROOT)$(php_fpm_init_path)" && \
++ $(mkinstalldirs) $(INSTALL_ROOT)$(php_fpm_init_dir) && \
++ $(INSTALL) -m 0755 sapi/fpm/init.d.php_fpm $(INSTALL_ROOT)$(php_fpm_init_path) || :
++
++ @test -d /etc/nginx/ && \
++ echo "Installing NGINX sample config: /etc/nginx/nginx-site-conf.sample" && \
++ $(mkinstalldirs) $(INSTALL_ROOT)/etc/nginx && \
++ $(INSTALL_DATA) -b sapi/fpm/nginx-site-conf.sample $(INSTALL_ROOT)/etc/nginx/nginx-site-conf.sample || :
++
++ @test -d /usr/local/etc/nginx/ && \
++ echo "Installing NGINX sample config: /usr/local/etc/nginx/nginx-site-conf.sample" && \
++ $(mkinstalldirs) $(INSTALL_ROOT)/usr/local/etc/nginx && \
++ $(INSTALL_DATA) -b sapi/fpm/nginx-site-conf.sample $(INSTALL_ROOT)/usr/local/etc/nginx/nginx-site-conf.sample || :
++
++ @test -d /usr/local/nginx/conf/ && \
++ echo "Installing NGINX sample config: /usr/local/nginx/conf/nginx-site-conf.sample" && \
++ $(mkinstalldirs) $(INSTALL_ROOT)/usr/local/nginx/conf && \
++ $(INSTALL_DATA) -b sapi/fpm/nginx-site-conf.sample $(INSTALL_ROOT)/usr/local/nginx/conf/nginx-site-conf.sample || :
++
++ @echo ""
++ @echo "*** FPM Installation complete. ***"
++ @echo ""
++
++ @test "$(php_fpm_init)" && \
++ echo "run:" && \
++ echo "\`update-rc.d $(php_fpm_init) defaults; invoke-rc.d $(php_fpm_init) start\`" && \
++ echo "" && \
++ echo "or system equivalent to start the $(php_fpm_init) service." && \
++ echo "" || :
++
+diff -Naur php-src-vanilla/sapi/fpm/cgi/cgi_main.c php-src/sapi/fpm/cgi/cgi_main.c
+--- php-src-vanilla/sapi/fpm/cgi/cgi_main.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/cgi/cgi_main.c 2009-10-18 21:05:39.302497288 +0100
+@@ -0,0 +1,1660 @@
++/*
++ +----------------------------------------------------------------------+
++ | PHP Version 5 |
++ +----------------------------------------------------------------------+
++ | Copyright (c) 1997-2008 The PHP Group |
++ +----------------------------------------------------------------------+
++ | This source file is subject to version 3.01 of the PHP license, |
++ | that is bundled with this package in the file LICENSE, and is |
++ | available through the world-wide-web at the following url: |
++ | http://www.php.net/license/3_01.txt |
++ | If you did not receive a copy of the PHP license and are unable to |
++ | obtain it through the world-wide-web, please send a note to |
++ | license@php.net so we can mail you a copy immediately. |
++ +----------------------------------------------------------------------+
++ | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
++ | Stig Bakken <ssb@php.net> |
++ | Zeev Suraski <zeev@zend.com> |
++ | FastCGI: Ben Mansell <php@slimyhorror.com> |
++ | Shane Caraveo <shane@caraveo.com> |
++ | Dmitry Stogov <dmitry@zend.com> |
++ +----------------------------------------------------------------------+
++*/
++
++/* $Id$ */
++
++#include <php.h>
++#include <php_globals.h>
++#include <php_variables.h>
++#include <zend_modules.h>
++
++#include <SAPI.h>
++
++#include <stdio.h>
++
++#ifdef PHP_WIN32
++#include "win32/time.h"
++#include "win32/signal.h"
++#include <process.h>
++#endif
++#if HAVE_SYS_TIME_H
++#include <sys/time.h>
++#endif
++#if HAVE_UNISTD_H
++#include <unistd.h>
++#endif
++#if HAVE_SIGNAL_H
++#include <signal.h>
++#endif
++#if HAVE_SETLOCALE
++#include <locale.h>
++#endif
++#if HAVE_SYS_TYPES_H
++#include <sys/types.h>
++#endif
++#if HAVE_SYS_WAIT_H
++#include <sys/wait.h>
++#endif
++#if HAVE_FCNTL_H
++#include <fcntl.h>
++#endif
++#include <zend.h>
++#include <zend_extensions.h>
++#include <php_ini.h>
++#include <php_main.h>
++#include <fopen_wrappers.h>
++#include <ext/standard/php_standard.h>
++#ifdef PHP_WIN32
++#include <io.h>
++#include <fcntl.h>
++#include "win32/php_registry.h"
++#endif
++
++#ifdef __riscos__
++#include <unixlib/local.h>
++int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS;
++#endif
++
++#include "zend_compile.h"
++#include "zend_execute.h"
++#include "zend_highlight.h"
++#include "zend_indent.h"
++
++#include "php_getopt.h"
++
++#include "fastcgi.h"
++
++#ifdef FPM_AUTOCONFIG_H
++#include <fpm_autoconfig.h>
++#else
++#include <php_config.h>
++#endif
++#include <fpm/fpm.h>
++#include <fpm/fpm_request.h>
++
++
++static void (*php_php_import_environment_variables)(zval *array_ptr TSRMLS_DC);
++
++static int parent = 1;
++
++static int request_body_fd;
++
++static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC);
++
++static char *php_optarg = NULL;
++static int php_optind = 1;
++static zend_module_entry cgi_module_entry;
++
++static const opt_struct OPTIONS[] = {
++ {'a', 0, "interactive"},
++ {'b', 1, "bindpath"},
++ {'C', 0, "no-chdir"},
++ {'c', 1, "php-ini"},
++ {'d', 1, "define"},
++ {'e', 0, "profile-info"},
++ {'f', 1, "file"},
++ {'h', 0, "help"},
++ {'i', 0, "info"},
++ {'l', 0, "syntax-check"},
++ {'m', 0, "modules"},
++ {'n', 0, "no-php-ini"},
++ {'q', 0, "no-header"},
++ {'s', 0, "syntax-highlight"},
++ {'s', 0, "syntax-highlighting"},
++ {'w', 0, "strip"},
++ {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
++ {'v', 0, "version"},
++ {'x', 0, "fpm"},
++ {'y', 1, "fpm-config"},
++ {'z', 1, "zend-extension"},
++ {'-', 0, NULL} /* end of args */
++};
++
++typedef struct _php_cgi_globals_struct {
++ zend_bool rfc2616_headers;
++ zend_bool nph;
++ zend_bool check_shebang_line;
++#if ENABLE_PATHINFO_CHECK
++ zend_bool fix_pathinfo;
++#endif
++ zend_bool fcgi_logging;
++# ifdef PHP_WIN32
++ zend_bool impersonate;
++# endif
++ char *error_header;
++} php_cgi_globals_struct;
++
++#ifdef ZTS
++static int php_cgi_globals_id;
++#define CGIG(v) TSRMG(php_cgi_globals_id, php_cgi_globals_struct *, v)
++#else
++static php_cgi_globals_struct php_cgi_globals;
++#define CGIG(v) (php_cgi_globals.v)
++#endif
++
++#ifdef PHP_WIN32
++#define TRANSLATE_SLASHES(path) \
++ { \
++ char *tmp = path; \
++ while (*tmp) { \
++ if (*tmp == '\\') *tmp = '/'; \
++ tmp++; \
++ } \
++ }
++#else
++#define TRANSLATE_SLASHES(path)
++#endif
++
++static int print_module_info(zend_module_entry *module, void *arg TSRMLS_DC)
++{
++ php_printf("%s\n", module->name);
++ return 0;
++}
++
++static int module_name_cmp(const void *a, const void *b TSRMLS_DC)
++{
++ Bucket *f = *((Bucket **) a);
++ Bucket *s = *((Bucket **) b);
++
++ return strcasecmp(((zend_module_entry *)f->pData)->name,
++ ((zend_module_entry *)s->pData)->name);
++}
++
++static void print_modules(TSRMLS_D)
++{
++ HashTable sorted_registry;
++ zend_module_entry tmp;
++
++ zend_hash_init(&sorted_registry, 50, NULL, NULL, 1);
++ zend_hash_copy(&sorted_registry, &module_registry, NULL, &tmp, sizeof(zend_module_entry));
++ zend_hash_sort(&sorted_registry, zend_qsort, module_name_cmp, 0 TSRMLS_CC);
++ zend_hash_apply_with_argument(&sorted_registry, (apply_func_arg_t) print_module_info, NULL TSRMLS_CC);
++ zend_hash_destroy(&sorted_registry);
++}
++
++static int print_extension_info(zend_extension *ext, void *arg TSRMLS_DC)
++{
++ php_printf("%s\n", ext->name);
++ return 0;
++}
++
++static int extension_name_cmp(const zend_llist_element **f,
++ const zend_llist_element **s TSRMLS_DC)
++{
++ return strcmp(((zend_extension *)(*f)->data)->name,
++ ((zend_extension *)(*s)->data)->name);
++}
++
++static void print_extensions(TSRMLS_D)
++{
++ zend_llist sorted_exts;
++
++ zend_llist_copy(&sorted_exts, &zend_extensions);
++ sorted_exts.dtor = NULL;
++ zend_llist_sort(&sorted_exts, extension_name_cmp TSRMLS_CC);
++ zend_llist_apply_with_argument(&sorted_exts, (llist_apply_with_arg_func_t) print_extension_info, NULL TSRMLS_CC);
++ zend_llist_destroy(&sorted_exts);
++}
++
++#ifndef STDOUT_FILENO
++#define STDOUT_FILENO 1
++#endif
++
++static inline size_t sapi_cgibin_single_write(const char *str, uint str_length TSRMLS_DC)
++{
++ long ret;
++
++ if (fcgi_is_fastcgi()) {
++ fcgi_request *request = (fcgi_request*) SG(server_context);
++ long ret = fcgi_write(request, FCGI_STDOUT, str, str_length);
++ if (ret <= 0) {
++ return 0;
++ }
++ return ret;
++ }
++ ret = write(STDOUT_FILENO, str, str_length);
++ if (ret <= 0) return 0;
++ return ret;
++}
++
++static int sapi_cgibin_ub_write(const char *str, uint str_length TSRMLS_DC)
++{
++ const char *ptr = str;
++ uint remaining = str_length;
++ size_t ret;
++
++ while (remaining > 0) {
++ ret = sapi_cgibin_single_write(ptr, remaining TSRMLS_CC);
++ if (!ret) {
++ php_handle_aborted_connection();
++ return str_length - remaining;
++ }
++ ptr += ret;
++ remaining -= ret;
++ }
++
++ return str_length;
++}
++
++
++static void sapi_cgibin_flush(void *server_context)
++{
++ if (fcgi_is_fastcgi()) {
++ fcgi_request *request = (fcgi_request*) server_context;
++ if (
++#ifndef PHP_WIN32
++ !parent &&
++#endif
++ request && !fcgi_flush(request, 0)) {
++ php_handle_aborted_connection();
++ }
++ return;
++ }
++ if (fflush(stdout) == EOF) {
++ php_handle_aborted_connection();
++ }
++}
++
++#define SAPI_CGI_MAX_HEADER_LENGTH 1024
++
++typedef struct _http_error {
++ int code;
++ const char* msg;
++} http_error;
++
++static const http_error http_error_codes[] = {
++ {100, "Continue"},
++ {101, "Switching Protocols"},
++ {200, "OK"},
++ {201, "Created"},
++ {202, "Accepted"},
++ {203, "Non-Authoritative Information"},
++ {204, "No Content"},
++ {205, "Reset Content"},
++ {206, "Partial Content"},
++ {300, "Multiple Choices"},
++ {301, "Moved Permanently"},
++ {302, "Moved Temporarily"},
++ {303, "See Other"},
++ {304, "Not Modified"},
++ {305, "Use Proxy"},
++ {400, "Bad Request"},
++ {401, "Unauthorized"},
++ {402, "Payment Required"},
++ {403, "Forbidden"},
++ {404, "Not Found"},
++ {405, "Method Not Allowed"},
++ {406, "Not Acceptable"},
++ {407, "Proxy Authentication Required"},
++ {408, "Request Time-out"},
++ {409, "Conflict"},
++ {410, "Gone"},
++ {411, "Length Required"},
++ {412, "Precondition Failed"},
++ {413, "Request Entity Too Large"},
++ {414, "Request-URI Too Large"},
++ {415, "Unsupported Media Type"},
++ {500, "Internal Server Error"},
++ {501, "Not Implemented"},
++ {502, "Bad Gateway"},
++ {503, "Service Unavailable"},
++ {504, "Gateway Time-out"},
++ {505, "HTTP Version not supported"},
++ {0, NULL}
++};
++
++static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
++{
++ char buf[SAPI_CGI_MAX_HEADER_LENGTH];
++ sapi_header_struct *h;
++ zend_llist_position pos;
++ zend_bool ignore_status = 0;
++ int response_status = SG(sapi_headers).http_response_code;
++
++ if (SG(request_info).no_headers == 1) {
++ return SAPI_HEADER_SENT_SUCCESSFULLY;
++ }
++
++ if (CGIG(nph) || SG(sapi_headers).http_response_code != 200)
++ {
++ int len;
++ zend_bool has_status = 0;
++
++ if (CGIG(rfc2616_headers) && SG(sapi_headers).http_status_line) {
++ char *s;
++ len = slprintf(buf, SAPI_CGI_MAX_HEADER_LENGTH, "%s\r\n", SG(sapi_headers).http_status_line);
++ if ((s = strchr(SG(sapi_headers).http_status_line, ' '))) {
++ response_status = atoi((s + 1));
++ }
++
++ if (len > SAPI_CGI_MAX_HEADER_LENGTH) {
++ len = SAPI_CGI_MAX_HEADER_LENGTH;
++ }
++
++ } else {
++ char *s;
++
++ if (SG(sapi_headers).http_status_line &&
++ (s = strchr(SG(sapi_headers).http_status_line, ' ')) != 0 &&
++ (s - SG(sapi_headers).http_status_line) >= 5 &&
++ strncasecmp(SG(sapi_headers).http_status_line, "HTTP/", 5) == 0) {
++ len = slprintf(buf, sizeof(buf), "Status:%s\r\n", s);
++ response_status = atoi((s + 1));
++ } else {
++ h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
++ while (h) {
++ if (h->header_len > sizeof("Status:")-1 &&
++ strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0) {
++ has_status = 1;
++ break;
++ }
++ h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
++ }
++ if (!has_status) {
++ http_error *err = (http_error*)http_error_codes;
++
++ while (err->code != 0) {
++ if (err->code == SG(sapi_headers).http_response_code) {
++ break;
++ }
++ err++;
++ }
++ if (err->msg) {
++ len = slprintf(buf, sizeof(buf), "Status: %d %s\r\n", SG(sapi_headers).http_response_code, err->msg);
++ } else {
++ len = slprintf(buf, sizeof(buf), "Status: %d\r\n", SG(sapi_headers).http_response_code);
++ }
++ }
++ }
++ }
++ if (!has_status) {
++ PHPWRITE_H(buf, len);
++ ignore_status = 1;
++ }
++ }
++
++ h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
++ while (h) {
++ /* prevent CRLFCRLF */
++ if (h->header_len) {
++ if (h->header_len > sizeof("Status:")-1 &&
++ strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0) {
++ if (!ignore_status) {
++ ignore_status = 1;
++ PHPWRITE_H(h->header, h->header_len);
++ PHPWRITE_H("\r\n", 2);
++ }
++ } else if (response_status == 304 && h->header_len > sizeof("Content-Type:")-1 &&
++ strncasecmp(h->header, "Content-Type:", sizeof("Content-Type:")-1) == 0) {
++ h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
++ continue;
++ } else {
++ PHPWRITE_H(h->header, h->header_len);
++ PHPWRITE_H("\r\n", 2);
++ }
++ }
++ h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
++ }
++ PHPWRITE_H("\r\n", 2);
++
++ return SAPI_HEADER_SENT_SUCCESSFULLY;
++}
++
++
++static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC)
++{
++ int read_bytes=0, tmp_read_bytes;
++
++ count_bytes = MIN(count_bytes, (uint) SG(request_info).content_length - SG(read_post_bytes));
++ while (read_bytes < count_bytes) {
++ if (fcgi_is_fastcgi()) {
++ fcgi_request *request = (fcgi_request*) SG(server_context);
++
++ if (request_body_fd == -1) {
++ char *request_body_filename = sapi_cgibin_getenv((char *) "REQUEST_BODY_FILE",
++ sizeof("REQUEST_BODY_FILE")-1 TSRMLS_CC);
++
++ if (request_body_filename && *request_body_filename) {
++ request_body_fd = open(request_body_filename, O_RDONLY);
++
++ if (0 > request_body_fd) {
++ php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)",
++ request_body_filename, strerror(errno), errno);
++ return 0;
++ }
++ }
++ }
++
++ /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */
++ if (request_body_fd < 0) {
++ tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
++ } else {
++ tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes);
++ }
++ } else {
++ tmp_read_bytes = read(0, buffer + read_bytes, count_bytes - read_bytes);
++ }
++
++ if (tmp_read_bytes <= 0) {
++ break;
++ }
++ read_bytes += tmp_read_bytes;
++ }
++ return read_bytes;
++}
++
++static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC)
++{
++ /* when php is started by mod_fastcgi, no regular environment
++ is provided to PHP. It is always sent to PHP at the start
++ of a request. So we have to do our own lookup to get env
++ vars. This could probably be faster somehow. */
++ if (fcgi_is_fastcgi()) {
++ fcgi_request *request = (fcgi_request*) SG(server_context);
++ return fcgi_getenv(request, name, name_len);
++ }
++ /* if cgi, or fastcgi and not found in fcgi env
++ check the regular environment */
++ return getenv(name);
++}
++
++static char *_sapi_cgibin_putenv(char *name, char *value TSRMLS_DC)
++{
++ int name_len;
++#if !HAVE_SETENV || !HAVE_UNSETENV
++ int len;
++ char *buf;
++#endif
++
++ if (!name) {
++ return NULL;
++ }
++ name_len = strlen(name);
++
++ /* when php is started by mod_fastcgi, no regular environment
++ is provided to PHP. It is always sent to PHP at the start
++ of a request. So we have to do our own lookup to get env
++ vars. This could probably be faster somehow. */
++ if (fcgi_is_fastcgi()) {
++ fcgi_request *request = (fcgi_request*) SG(server_context);
++ return fcgi_putenv(request, name, name_len, value);
++ }
++#if HAVE_SETENV
++ if (value) {
++ setenv(name, value, 1);
++ }
++#endif
++#if HAVE_UNSETENV
++ if (!value) {
++ unsetenv(name);
++ }
++#endif
++
++#if !HAVE_SETENV || !HAVE_UNSETENV
++ /* if cgi, or fastcgi and not found in fcgi env
++ check the regular environment
++ this leaks, but it's only cgi anyway, we'll fix
++ it for 5.0
++ */
++ len = name_len + (value ? strlen(value) : 0) + sizeof("=") + 2;
++ buf = (char *) malloc(len);
++ if (buf == NULL) {
++ return getenv(name);
++ }
++#endif
++#if !HAVE_SETENV
++ if (value) {
++ len = slprintf(buf, len - 1, "%s=%s", name, value);
++ putenv(buf);
++ }
++#endif
++#if !HAVE_UNSETENV
++ if (!value) {
++ len = slprintf(buf, len - 1, "%s=", name);
++ putenv(buf);
++ }
++#endif
++ return getenv(name);
++}
++
++static char *sapi_cgi_read_cookies(TSRMLS_D)
++{
++ return sapi_cgibin_getenv((char *) "HTTP_COOKIE", sizeof("HTTP_COOKIE")-1 TSRMLS_CC);
++}
++
++void cgi_php_import_environment_variables(zval *array_ptr TSRMLS_DC)
++{
++ if (PG(http_globals)[TRACK_VARS_ENV] &&
++ array_ptr != PG(http_globals)[TRACK_VARS_ENV] &&
++ Z_TYPE_P(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
++ zend_hash_num_elements(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_ENV])) > 0) {
++ zval_dtor(array_ptr);
++ *array_ptr = *PG(http_globals)[TRACK_VARS_ENV];
++ INIT_PZVAL(array_ptr);
++ zval_copy_ctor(array_ptr);
++ return;
++ } else if (PG(http_globals)[TRACK_VARS_SERVER] &&
++ array_ptr != PG(http_globals)[TRACK_VARS_SERVER] &&
++ Z_TYPE_P(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
++ zend_hash_num_elements(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER])) > 0) {
++ zval_dtor(array_ptr);
++ *array_ptr = *PG(http_globals)[TRACK_VARS_SERVER];
++ INIT_PZVAL(array_ptr);
++ zval_copy_ctor(array_ptr);
++ return;
++ }
++
++ /* call php's original import as a catch-all */
++ php_php_import_environment_variables(array_ptr TSRMLS_CC);
++
++ if (fcgi_is_fastcgi()) {
++ fcgi_request *request = (fcgi_request*) SG(server_context);
++ HashPosition pos;
++ int magic_quotes_gpc = PG(magic_quotes_gpc);
++ char *var, **val;
++ uint var_len;
++ ulong idx;
++ int filter_arg = (array_ptr == PG(http_globals)[TRACK_VARS_ENV])?PARSE_ENV:PARSE_SERVER;
++
++ /* turn off magic_quotes while importing environment variables */
++ PG(magic_quotes_gpc) = 0;
++ for (zend_hash_internal_pointer_reset_ex(&request->env, &pos);
++ zend_hash_get_current_key_ex(&request->env, &var, &var_len, &idx, 0, &pos) == HASH_KEY_IS_STRING &&
++ zend_hash_get_current_data_ex(&request->env, (void **) &val, &pos) == SUCCESS;
++ zend_hash_move_forward_ex(&request->env, &pos)) {
++ unsigned int new_val_len;
++ if (sapi_module.input_filter(filter_arg, var, val, strlen(*val), &new_val_len TSRMLS_CC)) {
++ php_register_variable_safe(var, *val, new_val_len, array_ptr TSRMLS_CC);
++ }
++ }
++ PG(magic_quotes_gpc) = magic_quotes_gpc;
++ }
++}
++
++static void sapi_cgi_register_variables(zval *track_vars_array TSRMLS_DC)
++{
++ unsigned int php_self_len;
++ char *php_self;
++
++ /* In CGI mode, we consider the environment to be a part of the server
++ * variables
++ */
++ php_import_environment_variables(track_vars_array TSRMLS_CC);
++
++#if ENABLE_PATHINFO_CHECK
++ if (CGIG(fix_pathinfo)) {
++ char *script_name = SG(request_info).request_uri;
++ unsigned int script_name_len = script_name ? strlen(script_name) : 0;
++ char *path_info = sapi_cgibin_getenv("PATH_INFO", sizeof("PATH_INFO")-1 TSRMLS_CC);
++ unsigned int path_info_len = path_info ? strlen(path_info) : 0;
++
++ php_self_len = script_name_len + path_info_len;
++ php_self = emalloc(php_self_len + 1);
++ if (script_name) {
++ memcpy(php_self, script_name, script_name_len + 1);
++ }
++ if (path_info) {
++ memcpy(php_self + script_name_len, path_info, path_info_len + 1);
++ }
++
++ /* Build the special-case PHP_SELF variable for the CGI version */
++ if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len TSRMLS_CC)) {
++ php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array TSRMLS_CC);
++ }
++ efree(php_self);
++ return;
++ }
++#endif
++
++ php_self = SG(request_info).request_uri ? SG(request_info).request_uri : "";
++ php_self_len = strlen(php_self);
++ if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len TSRMLS_CC)) {
++ php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array TSRMLS_CC);
++ }
++}
++
++static void sapi_cgi_log_message(char *message)
++{
++ TSRMLS_FETCH();
++
++ if (fcgi_is_fastcgi() && CGIG(fcgi_logging)) {
++ fcgi_request *request;
++
++ request = (fcgi_request*) SG(server_context);
++ if (request) {
++ int len = strlen(message);
++ char *buf = malloc(len+2);
++
++ memcpy(buf, message, len);
++ memcpy(buf + len, "\n", sizeof("\n"));
++ fcgi_write(request, FCGI_STDERR, buf, len+1);
++ free(buf);
++ } else {
++ fprintf(stderr, "%s\n", message);
++ }
++ /* ignore return code */
++ } else
++ fprintf(stderr, "%s\n", message);
++}
++
++static int sapi_cgi_deactivate(TSRMLS_D)
++{
++ /* flush only when SAPI was started. The reasons are:
++ 1. SAPI Deactivate is called from two places: module init and request shutdown
++ 2. When the first call occurs and the request is not set up, flush fails on
++ FastCGI.
++ */
++ if (SG(sapi_started)) {
++ sapi_cgibin_flush(SG(server_context));
++ }
++ return SUCCESS;
++}
++
++static int php_cgi_startup(sapi_module_struct *sapi_module)
++{
++ if (php_module_startup(sapi_module, &cgi_module_entry, 1) == FAILURE) {
++ return FAILURE;
++ }
++ return SUCCESS;
++}
++
++
++/* {{{ sapi_module_struct cgi_sapi_module
++ */
++static sapi_module_struct cgi_sapi_module = {
++ "cgi-fcgi", /* name */
++ "CGI/FastCGI", /* pretty name */
++
++ php_cgi_startup, /* startup */
++ php_module_shutdown_wrapper, /* shutdown */
++
++ NULL, /* activate */
++ sapi_cgi_deactivate, /* deactivate */
++
++ sapi_cgibin_ub_write, /* unbuffered write */
++ sapi_cgibin_flush, /* flush */
++ NULL, /* get uid */
++ sapi_cgibin_getenv, /* getenv */
++
++ php_error, /* error handler */
++
++ NULL, /* header handler */
++ sapi_cgi_send_headers, /* send headers handler */
++ NULL, /* send header handler */
++
++ sapi_cgi_read_post, /* read POST data */
++ sapi_cgi_read_cookies, /* read Cookies */
++
++ sapi_cgi_register_variables, /* register server variables */
++ sapi_cgi_log_message, /* Log message */
++ NULL, /* Get request time */
++
++ STANDARD_SAPI_MODULE_PROPERTIES
++};
++/* }}} */
++
++/* {{{ php_cgi_usage
++ */
++static void php_cgi_usage(char *argv0)
++{
++ char *prog;
++
++ prog = strrchr(argv0, '/');
++ if (prog) {
++ prog++;
++ } else {
++ prog = "php";
++ }
++
++ php_printf("Usage: %s [options]\n"
++ "\n"
++ " -C Do not chdir to the script's directory\n"
++ " -c <path>|<file> Look for php.ini file in this directory\n"
++ " -n No php.ini file will be used\n"
++ " -d foo[=bar] Define INI entry foo with value 'bar'\n"
++ " -e Generate extended information for debugger/profiler\n"
++ " -h This help\n"
++ " -i PHP information\n"
++ " -m Show compiled in modules\n"
++ " -v Version number\n"
++ " -y, --fpm-config <file>\n"
++ " Specify alternative path to FastCGI process manager config file.\n"
++ " -z <file> Load Zend extension <file>.\n"
++ ,
++ prog);
++}
++/* }}} */
++
++/* {{{ is_valid_path
++ *
++ * some server configurations allow '..' to slip through in the
++ * translated path. We'll just refuse to handle such a path.
++ */
++static int is_valid_path(const char *path)
++{
++ const char *p;
++
++ if (!path) {
++ return 0;
++ }
++ p = strstr(path, "..");
++ if (p) {
++ if ((p == path || IS_SLASH(*(p-1))) &&
++ (*(p+2) == 0 || IS_SLASH(*(p+2)))) {
++ return 0;
++ }
++ while (1) {
++ p = strstr(p+1, "..");
++ if (!p) {
++ break;
++ }
++ if (IS_SLASH(*(p-1)) &&
++ (*(p+2) == 0 || IS_SLASH(*(p+2)))) {
++ return 0;
++ }
++ }
++ }
++ return 1;
++}
++/* }}} */
++
++/* {{{ init_request_info
++
++ initializes request_info structure
++
++ specificly in this section we handle proper translations
++ for:
++
++ PATH_INFO
++ derived from the portion of the URI path following
++ the script name but preceding any query data
++ may be empty
++
++ PATH_TRANSLATED
++ derived by taking any path-info component of the
++ request URI and performing any virtual-to-physical
++ translation appropriate to map it onto the server's
++ document repository structure
++
++ empty if PATH_INFO is empty
++
++ The env var PATH_TRANSLATED **IS DIFFERENT** than the
++ request_info.path_translated variable, the latter should
++ match SCRIPT_FILENAME instead.
++
++ SCRIPT_NAME
++ set to a URL path that could identify the CGI script
++ rather than the interpreter. PHP_SELF is set to this.
++
++ REQUEST_URI
++ uri section following the domain:port part of a URI
++
++ SCRIPT_FILENAME
++ The virtual-to-physical translation of SCRIPT_NAME (as per
++ PATH_TRANSLATED)
++
++ These settings are documented at
++ http://cgi-spec.golux.com/
++
++
++ Based on the following URL request:
++
++ http://localhost/info.php/test?a=b
++
++ should produce, which btw is the same as if
++ we were running under mod_cgi on apache (ie. not
++ using ScriptAlias directives):
++
++ PATH_INFO=/test
++ PATH_TRANSLATED=/docroot/test
++ SCRIPT_NAME=/info.php
++ REQUEST_URI=/info.php/test?a=b
++ SCRIPT_FILENAME=/docroot/info.php
++ QUERY_STRING=a=b
++
++ but what we get is (cgi/mod_fastcgi under apache):
++
++ PATH_INFO=/info.php/test
++ PATH_TRANSLATED=/docroot/info.php/test
++ SCRIPT_NAME=/php/php-cgi (from the Action setting I suppose)
++ REQUEST_URI=/info.php/test?a=b
++ SCRIPT_FILENAME=/path/to/php/bin/php-cgi (Action setting translated)
++ QUERY_STRING=a=b
++
++ Comments in the code below refer to using the above URL in a request
++
++ */
++static void init_request_info(TSRMLS_D)
++{
++ char *env_script_filename = sapi_cgibin_getenv("SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1 TSRMLS_CC);
++ char *env_path_translated = sapi_cgibin_getenv("PATH_TRANSLATED", sizeof("PATH_TRANSLATED")-1 TSRMLS_CC);
++ char *script_path_translated = env_script_filename;
++
++#if !DISCARD_PATH
++ /* some broken servers do not have script_filename or argv0
++ an example, IIS configured in some ways. then they do more
++ broken stuff and set path_translated to the cgi script location */
++ if (!script_path_translated && env_path_translated) {
++ script_path_translated = env_path_translated;
++ }
++#endif
++
++ /* initialize the defaults */
++ SG(request_info).path_translated = NULL;
++ SG(request_info).request_method = NULL;
++ SG(request_info).proto_num = 1000;
++ SG(request_info).query_string = NULL;
++ SG(request_info).request_uri = NULL;
++ SG(request_info).content_type = NULL;
++ SG(request_info).content_length = 0;
++ SG(sapi_headers).http_response_code = 200;
++
++ /* script_path_translated being set is a good indication that
++ we are running in a cgi environment, since it is always
++ null otherwise. otherwise, the filename
++ of the script will be retreived later via argc/argv */
++ if (script_path_translated) {
++ const char *auth;
++ char *content_length = sapi_cgibin_getenv("CONTENT_LENGTH", sizeof("CONTENT_LENGTH")-1 TSRMLS_CC);
++ char *content_type = sapi_cgibin_getenv("CONTENT_TYPE", sizeof("CONTENT_TYPE")-1 TSRMLS_CC);
++ char *env_path_info = sapi_cgibin_getenv("PATH_INFO", sizeof("PATH_INFO")-1 TSRMLS_CC);
++ char *env_script_name = sapi_cgibin_getenv("SCRIPT_NAME", sizeof("SCRIPT_NAME")-1 TSRMLS_CC);
++#if ENABLE_PATHINFO_CHECK
++ struct stat st;
++ char *env_redirect_url = sapi_cgibin_getenv("REDIRECT_URL", sizeof("REDIRECT_URL")-1 TSRMLS_CC);
++ char *env_document_root = sapi_cgibin_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT")-1 TSRMLS_CC);
++ int script_path_translated_len;
++
++ /* Hack for buggy IIS that sets incorrect PATH_INFO */
++ char *env_server_software = sapi_cgibin_getenv("SERVER_SOFTWARE", sizeof("SERVER_SOFTWARE")-1 TSRMLS_CC);
++ if (env_server_software &&
++ env_script_name &&
++ env_path_info &&
++ strncmp(env_server_software, "Microsoft-IIS", sizeof("Microsoft-IIS")-1) == 0 &&
++ strncmp(env_path_info, env_script_name, strlen(env_script_name)) == 0) {
++ env_path_info = _sapi_cgibin_putenv("ORIG_PATH_INFO", env_path_info TSRMLS_CC);
++ env_path_info += strlen(env_script_name);
++ if (*env_path_info == 0) {
++ env_path_info = NULL;
++ }
++ env_path_info = _sapi_cgibin_putenv("PATH_INFO", env_path_info TSRMLS_CC);
++ }
++
++ if (CGIG(fix_pathinfo)) {
++ char *real_path = NULL;
++ char *orig_path_translated = env_path_translated;
++ char *orig_path_info = env_path_info;
++ char *orig_script_name = env_script_name;
++ char *orig_script_filename = env_script_filename;
++
++ if (!env_document_root && PG(doc_root)) {
++ env_document_root = _sapi_cgibin_putenv("DOCUMENT_ROOT", PG(doc_root) TSRMLS_CC);
++ /* fix docroot */
++ TRANSLATE_SLASHES(env_document_root);
++ }
++
++ if (env_path_translated != NULL && env_redirect_url != NULL) {
++ /*
++ pretty much apache specific. If we have a redirect_url
++ then our script_filename and script_name point to the
++ php executable
++ */
++ script_path_translated = env_path_translated;
++ /* we correct SCRIPT_NAME now in case we don't have PATH_INFO */
++ env_script_name = env_redirect_url;
++ }
++
++#ifdef __riscos__
++ /* Convert path to unix format*/
++ __riscosify_control |= __RISCOSIFY_DONT_CHECK_DIR;
++ script_path_translated = __unixify(script_path_translated, 0, NULL, 1, 0);
++#endif
++
++ /*
++ * if the file doesn't exist, try to extract PATH_INFO out
++ * of it by stat'ing back through the '/'
++ * this fixes url's like /info.php/test
++ */
++ if (script_path_translated &&
++ (script_path_translated_len = strlen(script_path_translated)) > 0 &&
++ (script_path_translated[script_path_translated_len-1] == '/' ||
++#ifdef PHP_WIN32
++ script_path_translated[script_path_translated_len-1] == '\\' ||
++#endif
++ (real_path = tsrm_realpath(script_path_translated, NULL TSRMLS_CC)) == NULL)) {
++ char *pt = estrndup(script_path_translated, script_path_translated_len);
++ int len = script_path_translated_len;
++ char *ptr;
++
++ while ((ptr = strrchr(pt, '/')) || (ptr = strrchr(pt, '\\'))) {
++ *ptr = 0;
++ if (stat(pt, &st) == 0 && S_ISREG(st.st_mode)) {
++ /*
++ * okay, we found the base script!
++ * work out how many chars we had to strip off;
++ * then we can modify PATH_INFO
++ * accordingly
++ *
++ * we now have the makings of
++ * PATH_INFO=/test
++ * SCRIPT_FILENAME=/docroot/info.php
++ *
++ * we now need to figure out what docroot is.
++ * if DOCUMENT_ROOT is set, this is easy, otherwise,
++ * we have to play the game of hide and seek to figure
++ * out what SCRIPT_NAME should be
++ */
++ int slen = len - strlen(pt);
++ int pilen = env_path_info ? strlen(env_path_info) : 0;
++ char *path_info = env_path_info ? env_path_info + pilen - slen : NULL;
++
++ if (orig_path_info != path_info) {
++ if (orig_path_info) {
++ char old;
++
++ _sapi_cgibin_putenv("ORIG_PATH_INFO", orig_path_info TSRMLS_CC);
++ old = path_info[0];
++ path_info[0] = 0;
++ if (!orig_script_name ||
++ strcmp(orig_script_name, env_path_info) != 0) {
++ if (orig_script_name) {
++ _sapi_cgibin_putenv("ORIG_SCRIPT_NAME", orig_script_name TSRMLS_CC);
++ }
++ SG(request_info).request_uri = _sapi_cgibin_putenv("SCRIPT_NAME", env_path_info TSRMLS_CC);
++ } else {
++ SG(request_info).request_uri = orig_script_name;
++ }
++ path_info[0] = old;
++ }
++ env_path_info = _sapi_cgibin_putenv("PATH_INFO", path_info TSRMLS_CC);
++ }
++ if (!orig_script_filename ||
++ strcmp(orig_script_filename, pt) != 0) {
++ if (orig_script_filename) {
++ _sapi_cgibin_putenv("ORIG_SCRIPT_FILENAME", orig_script_filename TSRMLS_CC);
++ }
++ script_path_translated = _sapi_cgibin_putenv("SCRIPT_FILENAME", pt TSRMLS_CC);
++ }
++ TRANSLATE_SLASHES(pt);
++
++ /* figure out docroot
++ SCRIPT_FILENAME minus SCRIPT_NAME
++ */
++
++ if (env_document_root) {
++ int l = strlen(env_document_root);
++ int path_translated_len = 0;
++ char *path_translated = NULL;
++
++ if (l && env_document_root[l - 1] == '/') {
++ --l;
++ }
++
++ /* we have docroot, so we should have:
++ * DOCUMENT_ROOT=/docroot
++ * SCRIPT_FILENAME=/docroot/info.php
++ */
++
++ /* PATH_TRANSLATED = DOCUMENT_ROOT + PATH_INFO */
++ path_translated_len = l + (env_path_info ? strlen(env_path_info) : 0);
++ path_translated = (char *) emalloc(path_translated_len + 1);
++ memcpy(path_translated, env_document_root, l);
++ if (env_path_info) {
++ memcpy(path_translated + l, env_path_info, (path_translated_len - l));
++ }
++ path_translated[path_translated_len] = '\0';
++ if (orig_path_translated) {
++ _sapi_cgibin_putenv("ORIG_PATH_TRANSLATED", orig_path_translated TSRMLS_CC);
++ }
++ env_path_translated = _sapi_cgibin_putenv("PATH_TRANSLATED", path_translated TSRMLS_CC);
++ efree(path_translated);
++ } else if (env_script_name &&
++ strstr(pt, env_script_name)
++ ) {
++ /* PATH_TRANSLATED = PATH_TRANSLATED - SCRIPT_NAME + PATH_INFO */
++ int ptlen = strlen(pt) - strlen(env_script_name);
++ int path_translated_len = ptlen + (env_path_info ? strlen(env_path_info) : 0);
++ char *path_translated = NULL;
++
++ path_translated = (char *) emalloc(path_translated_len + 1);
++ memcpy(path_translated, pt, ptlen);
++ if (env_path_info) {
++ memcpy(path_translated + ptlen, env_path_info, path_translated_len - ptlen);
++ }
++ path_translated[path_translated_len] = '\0';
++ if (orig_path_translated) {
++ _sapi_cgibin_putenv("ORIG_PATH_TRANSLATED", orig_path_translated TSRMLS_CC);
++ }
++ env_path_translated = _sapi_cgibin_putenv("PATH_TRANSLATED", path_translated TSRMLS_CC);
++ efree(path_translated);
++ }
++ break;
++ }
++ }
++ if (!ptr) {
++ /*
++ * if we stripped out all the '/' and still didn't find
++ * a valid path... we will fail, badly. of course we would
++ * have failed anyway... we output 'no input file' now.
++ */
++ if (orig_script_filename) {
++ _sapi_cgibin_putenv("ORIG_SCRIPT_FILENAME", orig_script_filename TSRMLS_CC);
++ }
++ script_path_translated = _sapi_cgibin_putenv("SCRIPT_FILENAME", NULL TSRMLS_CC);
++ SG(sapi_headers).http_response_code = 404;
++ }
++ if (!SG(request_info).request_uri) {
++ if (!orig_script_name ||
++ strcmp(orig_script_name, env_script_name) != 0) {
++ if (orig_script_name) {
++ _sapi_cgibin_putenv("ORIG_SCRIPT_NAME", orig_script_name TSRMLS_CC);
++ }
++ SG(request_info).request_uri = _sapi_cgibin_putenv("SCRIPT_NAME", env_script_name TSRMLS_CC);
++ } else {
++ SG(request_info).request_uri = orig_script_name;
++ }
++ }
++ if (pt) {
++ efree(pt);
++ }
++ if (is_valid_path(script_path_translated)) {
++ SG(request_info).path_translated = estrdup(script_path_translated);
++ }
++ } else {
++ /* make sure path_info/translated are empty */
++ if (!orig_script_filename ||
++ (script_path_translated != orig_script_filename &&
++ strcmp(script_path_translated, orig_script_filename) != 0)) {
++ if (orig_script_filename) {
++ _sapi_cgibin_putenv("ORIG_SCRIPT_FILENAME", orig_script_filename TSRMLS_CC);
++ }
++ script_path_translated = _sapi_cgibin_putenv("SCRIPT_FILENAME", script_path_translated TSRMLS_CC);
++ }
++ if (env_redirect_url) {
++ if (orig_path_info) {
++ _sapi_cgibin_putenv("ORIG_PATH_INFO", orig_path_info TSRMLS_CC);
++ _sapi_cgibin_putenv("PATH_INFO", NULL TSRMLS_CC);
++ }
++ if (orig_path_translated) {
++ _sapi_cgibin_putenv("ORIG_PATH_TRANSLATED", orig_path_translated TSRMLS_CC);
++ _sapi_cgibin_putenv("PATH_TRANSLATED", NULL TSRMLS_CC);
++ }
++ }
++ if (env_script_name != orig_script_name) {
++ if (orig_script_name) {
++ _sapi_cgibin_putenv("ORIG_SCRIPT_NAME", orig_script_name TSRMLS_CC);
++ }
++ SG(request_info).request_uri = _sapi_cgibin_putenv("SCRIPT_NAME", env_script_name TSRMLS_CC);
++ } else {
++ SG(request_info).request_uri = env_script_name;
++ }
++ if (is_valid_path(script_path_translated)) {
++ SG(request_info).path_translated = estrdup(script_path_translated);
++ }
++ free(real_path);
++ }
++ } else {
++#endif
++ /* pre 4.3 behaviour, shouldn't be used but provides BC */
++ if (env_path_info) {
++ SG(request_info).request_uri = env_path_info;
++ } else {
++ SG(request_info).request_uri = env_script_name;
++ }
++#if !DISCARD_PATH
++ if (env_path_translated) {
++ script_path_translated = env_path_translated;
++ }
++#endif
++ if (is_valid_path(script_path_translated)) {
++ SG(request_info).path_translated = estrdup(script_path_translated);
++ }
++#if ENABLE_PATHINFO_CHECK
++ }
++#endif
++ SG(request_info).request_method = sapi_cgibin_getenv("REQUEST_METHOD", sizeof("REQUEST_METHOD")-1 TSRMLS_CC);
++ /* FIXME - Work out proto_num here */
++ SG(request_info).query_string = sapi_cgibin_getenv("QUERY_STRING", sizeof("QUERY_STRING")-1 TSRMLS_CC);
++ SG(request_info).content_type = (content_type ? content_type : "" );
++ SG(request_info).content_length = (content_length ? atoi(content_length) : 0);
++
++ /* The CGI RFC allows servers to pass on unvalidated Authorization data */
++ auth = sapi_cgibin_getenv("HTTP_AUTHORIZATION", sizeof("HTTP_AUTHORIZATION")-1 TSRMLS_CC);
++ php_handle_auth_data(auth TSRMLS_CC);
++ }
++}
++/* }}} */
++
++
++PHP_INI_BEGIN()
++ STD_PHP_INI_ENTRY("cgi.rfc2616_headers", "0", PHP_INI_ALL, OnUpdateBool, rfc2616_headers, php_cgi_globals_struct, php_cgi_globals)
++ STD_PHP_INI_ENTRY("cgi.nph", "0", PHP_INI_ALL, OnUpdateBool, nph, php_cgi_globals_struct, php_cgi_globals)
++ STD_PHP_INI_ENTRY("cgi.check_shebang_line", "1", PHP_INI_SYSTEM, OnUpdateBool, check_shebang_line, php_cgi_globals_struct, php_cgi_globals)
++#if ENABLE_PATHINFO_CHECK
++ STD_PHP_INI_ENTRY("cgi.fix_pathinfo", "1", PHP_INI_SYSTEM, OnUpdateBool, fix_pathinfo, php_cgi_globals_struct, php_cgi_globals)
++#endif
++ STD_PHP_INI_ENTRY("fastcgi.logging", "1", PHP_INI_SYSTEM, OnUpdateBool, fcgi_logging, php_cgi_globals_struct, php_cgi_globals)
++# ifdef PHP_WIN32
++ STD_PHP_INI_ENTRY("fastcgi.impersonate", "0", PHP_INI_SYSTEM, OnUpdateBool, impersonate, php_cgi_globals_struct, php_cgi_globals)
++# endif
++ STD_PHP_INI_ENTRY("fastcgi.error_header", NULL, PHP_INI_SYSTEM, OnUpdateString, error_header, php_cgi_globals_struct, php_cgi_globals)
++PHP_INI_END()
++
++/* {{{ php_cgi_globals_ctor
++ */
++static void php_cgi_globals_ctor(php_cgi_globals_struct *php_cgi_globals TSRMLS_DC)
++{
++ php_cgi_globals->rfc2616_headers = 0;
++ php_cgi_globals->nph = 0;
++ php_cgi_globals->check_shebang_line = 1;
++#if ENABLE_PATHINFO_CHECK
++ php_cgi_globals->fix_pathinfo = 1;
++#endif
++ php_cgi_globals->fcgi_logging = 1;
++# ifdef PHP_WIN32
++ php_cgi_globals->impersonate = 0;
++# endif
++ php_cgi_globals->error_header = NULL;
++}
++/* }}} */
++
++/* {{{ PHP_MINIT_FUNCTION
++ */
++static PHP_MINIT_FUNCTION(cgi)
++{
++#ifdef ZTS
++ ts_allocate_id(&php_cgi_globals_id, sizeof(php_cgi_globals_struct), (ts_allocate_ctor) php_cgi_globals_ctor, NULL);
++#else
++ php_cgi_globals_ctor(&php_cgi_globals TSRMLS_CC);
++#endif
++ REGISTER_INI_ENTRIES();
++ return SUCCESS;
++}
++/* }}} */
++
++/* {{{ PHP_MSHUTDOWN_FUNCTION
++ */
++static PHP_MSHUTDOWN_FUNCTION(cgi)
++{
++ UNREGISTER_INI_ENTRIES();
++ return SUCCESS;
++}
++/* }}} */
++
++/* {{{ PHP_MINFO_FUNCTION
++ */
++static PHP_MINFO_FUNCTION(cgi)
++{
++ DISPLAY_INI_ENTRIES();
++
++ php_info_print_table_start();
++ php_info_print_table_row(2, "php-fpm", "active");
++ php_info_print_table_row(2, "php-fpm version", PHP_FPM_VERSION);
++ php_info_print_table_end();
++
++}
++/* }}} */
++
++PHP_FUNCTION(fastcgi_finish_request)
++{
++ fcgi_request *request = (fcgi_request*) SG(server_context);
++
++ if (fcgi_is_fastcgi() && request->fd >= 0) {
++
++ php_end_ob_buffers(1 TSRMLS_CC);
++ php_header(TSRMLS_C);
++
++ fcgi_flush(request, 1);
++ fcgi_close(request, 0, 0);
++ RETURN_TRUE;
++ }
++
++ RETURN_FALSE;
++
++}
++
++function_entry cgi_fcgi_sapi_functions[] = {
++ PHP_FE(fastcgi_finish_request, NULL)
++ {NULL, NULL, NULL}
++};
++
++static zend_module_entry cgi_module_entry = {
++ STANDARD_MODULE_HEADER,
++ "cgi-fcgi",
++ cgi_fcgi_sapi_functions,
++ PHP_MINIT(cgi),
++ PHP_MSHUTDOWN(cgi),
++ NULL,
++ NULL,
++ PHP_MINFO(cgi),
++ NO_VERSION_YET,
++ STANDARD_MODULE_PROPERTIES
++};
++
++/* {{{ main
++ */
++int main(int argc, char *argv[])
++{
++ int free_query_string = 0;
++ int exit_status = SUCCESS;
++ int c;
++ zend_file_handle file_handle = {};
++ int retval;
++/* temporary locals */
++ int orig_optind = php_optind;
++ char *orig_optarg = php_optarg;
++ int ini_entries_len = 0;
++
++/* end of temporary locals */
++#ifdef ZTS
++ void ***tsrm_ls;
++#endif
++
++ int max_requests = 500;
++ int requests = 0;
++ int fcgi_fd = 0;
++ fcgi_request request;
++ char *fpm_config = NULL;
++
++#ifdef HAVE_SIGNAL_H
++#if defined(SIGPIPE) && defined(SIG_IGN)
++ signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
++ that sockets created via fsockopen()
++ don't kill PHP if the remote site
++ closes it. in apache|apxs mode apache
++ does that for us! thies@thieso.net
++ 20000419 */
++#endif
++#endif
++
++#ifdef ZTS
++ tsrm_startup(1, 1, 0, NULL);
++ tsrm_ls = ts_resource(0);
++#endif
++
++ sapi_startup(&cgi_sapi_module);
++ cgi_sapi_module.php_ini_path_override = NULL;
++
++#ifdef PHP_WIN32
++ _fmode = _O_BINARY; /* sets default for file streams to binary */
++ setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
++ setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
++ setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
++#endif
++
++ while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0)) != -1) {
++ switch (c) {
++
++ case 'c':
++ if (cgi_sapi_module.php_ini_path_override) {
++ free(cgi_sapi_module.php_ini_path_override);
++ }
++ cgi_sapi_module.php_ini_path_override = strdup(php_optarg);
++ break;
++
++ case 'n':
++ cgi_sapi_module.php_ini_ignore = 1;
++ break;
++
++ case 'C': /* don't chdir to the script directory */
++ SG(options) |= SAPI_OPTION_NO_CHDIR;
++ break;
++
++ case 'd': {
++ /* define ini entries on command line */
++ int len = strlen(php_optarg);
++ char *val;
++
++ if ((val = strchr(php_optarg, '='))) {
++ val++;
++ if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
++ cgi_sapi_module.ini_entries = realloc(cgi_sapi_module.ini_entries, ini_entries_len + len + sizeof("\"\"\n\0"));
++ memcpy(cgi_sapi_module.ini_entries + ini_entries_len, php_optarg, (val - php_optarg));
++ ini_entries_len += (val - php_optarg);
++ memcpy(cgi_sapi_module.ini_entries + ini_entries_len, "\"", 1);
++ ini_entries_len++;
++ memcpy(cgi_sapi_module.ini_entries + ini_entries_len, val, len - (val - php_optarg));
++ ini_entries_len += len - (val - php_optarg);
++ memcpy(cgi_sapi_module.ini_entries + ini_entries_len, "\"\n\0", sizeof("\"\n\0"));
++ ini_entries_len += sizeof("\n\0\"") - 2;
++ } else {
++ cgi_sapi_module.ini_entries = realloc(cgi_sapi_module.ini_entries, ini_entries_len + len + sizeof("\n\0"));
++ memcpy(cgi_sapi_module.ini_entries + ini_entries_len, php_optarg, len);
++ memcpy(cgi_sapi_module.ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
++ ini_entries_len += len + sizeof("\n\0") - 2;
++ }
++ } else {
++ cgi_sapi_module.ini_entries = realloc(cgi_sapi_module.ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
++ memcpy(cgi_sapi_module.ini_entries + ini_entries_len, php_optarg, len);
++ memcpy(cgi_sapi_module.ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
++ ini_entries_len += len + sizeof("=1\n\0") - 2;
++ }
++ break;
++ }
++
++ case 'y':
++ fpm_config = php_optarg;
++ break;
++
++ case 'e': /* enable extended info output */
++ /* CG(extended_info) = 1; */ /* 5_2 */
++ CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO; /* 5_3 */
++ break;
++
++ case 'm': /* list compiled in modules */
++ cgi_sapi_module.startup(&cgi_sapi_module);
++ php_output_startup();
++ php_output_activate(TSRMLS_C);
++ SG(headers_sent) = 1;
++ php_printf("[PHP Modules]\n");
++ print_modules(TSRMLS_C);
++ php_printf("\n[Zend Modules]\n");
++ print_extensions(TSRMLS_C);
++ php_printf("\n");
++ php_end_ob_buffers(1 TSRMLS_CC);
++ exit_status = 0;
++ goto out;
++
++ case 'i': /* php info & quit */
++ cgi_sapi_module.startup(&cgi_sapi_module);
++ if (php_request_startup(TSRMLS_C) == FAILURE) {
++ SG(server_context) = NULL;
++ php_module_shutdown(TSRMLS_C);
++ return FAILURE;
++ }
++ SG(headers_sent) = 1;
++ SG(request_info).no_headers = 1;
++ php_print_info(0xFFFFFFFF TSRMLS_CC);
++ php_request_shutdown((void *) 0);
++ exit_status = 0;
++ goto out;
++
++ case 'h':
++ case '?':
++ cgi_sapi_module.startup(&cgi_sapi_module);
++ php_output_startup();
++ php_output_activate(TSRMLS_C);
++ SG(headers_sent) = 1;
++ php_cgi_usage(argv[0]);
++ php_end_ob_buffers(1 TSRMLS_CC);
++ exit_status = 0;
++ goto out;
++
++ case 'v': /* show php version & quit */
++ cgi_sapi_module.startup(&cgi_sapi_module);
++ if (php_request_startup(TSRMLS_C) == FAILURE) {
++ SG(server_context) = NULL;
++ php_module_shutdown(TSRMLS_C);
++ return FAILURE;
++ }
++ SG(headers_sent) = 1;
++ SG(request_info).no_headers = 1;
++
++#if SUHOSIN_PATCH
++#if ZEND_DEBUG
++ php_printf("PHP %s with Suhosin-Patch %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, SUHOSIN_PATCH_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
++#else
++ php_printf("PHP %s with Suhosin-Patch %s (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, SUHOSIN_PATCH_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
++#endif
++#else
++#if ZEND_DEBUG
++ php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
++#else
++ php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
++#endif
++#endif
++ php_request_shutdown((void *) 0);
++ exit_status = 0;
++ goto out;
++
++ }
++
++ }
++ php_optind = orig_optind;
++ php_optarg = orig_optarg;
++
++#ifdef ZTS
++ SG(request_info).path_translated = NULL;
++#endif
++
++ cgi_sapi_module.executable_location = argv[0];
++
++ /* startup after we get the above ini override se we get things right */
++ if (cgi_sapi_module.startup(&cgi_sapi_module) == FAILURE) {
++#ifdef ZTS
++ tsrm_shutdown();
++#endif
++ return FAILURE;
++ }
++
++ if (0 > fpm_init(argc, argv, fpm_config)) {
++ return FAILURE;
++ }
++
++ fcgi_fd = fpm_run(&max_requests);
++
++ parent = 0;
++
++ fcgi_set_is_fastcgi(1);
++
++ /* make php call us to get _ENV vars */
++ php_php_import_environment_variables = php_import_environment_variables;
++ php_import_environment_variables = cgi_php_import_environment_variables;
++
++ /* library is already initialized, now init our request */
++ fcgi_init_request(&request, fcgi_fd);
++
++ zend_first_try {
++
++ /* start of FAST CGI loop */
++ /* Initialise FastCGI request structure */
++#ifdef PHP_WIN32
++ /* attempt to set security impersonation for fastcgi
++ will only happen on NT based OS, others will ignore it. */
++ if (fastcgi && CGIG(impersonate)) {
++ fcgi_impersonate();
++ }
++#endif
++ while (fcgi_accept_request(&request) >= 0) {
++
++ request_body_fd = -1;
++
++ SG(server_context) = (void *) &request;
++
++ init_request_info(TSRMLS_C);
++
++ CG(interactive) = 0;
++
++ fpm_request_info();
++
++ /*
++ we never take stdin if we're (f)cgi, always
++ rely on the web server giving us the info
++ we need in the environment.
++ */
++ if (SG(request_info).path_translated) {
++ file_handle.type = ZEND_HANDLE_FILENAME;
++ file_handle.filename = SG(request_info).path_translated;
++ file_handle.handle.fp = NULL;
++ }
++ file_handle.opened_path = NULL;
++ file_handle.free_filename = 0;
++
++ /* request startup only after we've done all we can to
++ get path_translated */
++ if (php_request_startup(TSRMLS_C) == FAILURE) {
++ fcgi_finish_request(&request);
++ SG(server_context) = NULL;
++ php_module_shutdown(TSRMLS_C);
++ return FAILURE;
++ }
++
++ /*
++ at this point path_translated will be set if:
++ 1. we are running from shell and got filename was there
++ 2. we are running as cgi or fastcgi
++ */
++ retval = FAILURE;
++ if (SG(request_info).path_translated) {
++ if (!php_check_open_basedir(SG(request_info).path_translated TSRMLS_CC)) {
++ retval = php_fopen_primary_script(&file_handle TSRMLS_CC);
++ }
++ }
++ /*
++ if we are unable to open path_translated and we are not
++ running from shell (so fp == NULL), then fail.
++ */
++ if (retval == FAILURE && file_handle.handle.fp == NULL) {
++ if (errno == EACCES) {
++ SG(sapi_headers).http_response_code = 403;
++ PUTS("Access denied.\n");
++ } else {
++ SG(sapi_headers).http_response_code = 404;
++ PUTS("No input file specified.\n");
++ }
++ /* we want to serve more requests if this is fastcgi
++ so cleanup and continue, request shutdown is
++ handled later */
++ goto fastcgi_request_done;
++
++ STR_FREE(SG(request_info).path_translated);
++
++ if (free_query_string && SG(request_info).query_string) {
++ free(SG(request_info).query_string);
++ SG(request_info).query_string = NULL;
++ }
++
++ php_request_shutdown((void *) 0);
++ SG(server_context) = NULL;
++ php_module_shutdown(TSRMLS_C);
++ sapi_shutdown();
++#ifdef ZTS
++ tsrm_shutdown();
++#endif
++ return FAILURE;
++ }
++
++ fpm_request_executing();
++
++ php_execute_script(&file_handle TSRMLS_CC);
++
++fastcgi_request_done:
++
++ if (request_body_fd != -1) close(request_body_fd);
++
++ request_body_fd = -2;
++
++ {
++ char *path_translated;
++
++ /* Go through this trouble so that the memory manager doesn't warn
++ * about SG(request_info).path_translated leaking
++ */
++ if (SG(request_info).path_translated) {
++ path_translated = strdup(SG(request_info).path_translated);
++ STR_FREE(SG(request_info).path_translated);
++ SG(request_info).path_translated = path_translated;
++ }
++
++ if (EG(exit_status) == 255) {
++ if (CGIG(error_header) && *CGIG(error_header)) {
++ sapi_header_line ctr = {0};
++
++ ctr.line = CGIG(error_header);
++ ctr.line_len = strlen(CGIG(error_header));
++ sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
++ }
++ }
++
++ php_request_shutdown((void *) 0);
++ if (exit_status == 0) {
++ exit_status = EG(exit_status);
++ }
++
++ if (SG(request_info).path_translated) {
++ free(SG(request_info).path_translated);
++ SG(request_info).path_translated = NULL;
++ }
++ if (free_query_string && SG(request_info).query_string) {
++ free(SG(request_info).query_string);
++ SG(request_info).query_string = NULL;
++ }
++
++ }
++
++ requests++;
++ if (max_requests && (requests == max_requests)) {
++ fcgi_finish_request(&request);
++ break;
++ }
++
++ /* end of fastcgi loop */
++ }
++
++ fcgi_shutdown();
++
++ if (fcgi_in_shutdown() || /* graceful shutdown by a signal */
++ (max_requests && (requests == max_requests)) /* we were told to process max_requests and we are done */
++ ) {
++ exit_status = 0;
++ }
++ else {
++ exit_status = 255;
++ }
++
++ if (cgi_sapi_module.php_ini_path_override) {
++ free(cgi_sapi_module.php_ini_path_override);
++ }
++ if (cgi_sapi_module.ini_entries) {
++ free(cgi_sapi_module.ini_entries);
++ }
++ } zend_catch {
++ exit_status = 255;
++ } zend_end_try();
++
++out:
++
++ SG(server_context) = NULL;
++ php_module_shutdown(TSRMLS_C);
++ sapi_shutdown();
++
++#ifdef ZTS
++ /*tsrm_shutdown();*/
++#endif
++
++#if defined(PHP_WIN32) && ZEND_DEBUG && 0
++ _CrtDumpMemoryLeaks();
++#endif
++
++ return exit_status;
++}
++/* }}} */
++
++/*
++ * Local variables:
++ * tab-width: 4
++ * c-basic-offset: 4
++ * End:
++ * vim600: sw=4 ts=4 fdm=marker
++ * vim<600: sw=4 ts=4
++ */
+diff -Naur php-src-vanilla/sapi/fpm/cgi/CREDITS php-src/sapi/fpm/cgi/CREDITS
+--- php-src-vanilla/sapi/fpm/cgi/CREDITS 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/cgi/CREDITS 2009-10-18 21:05:39.302497288 +0100
+@@ -0,0 +1,2 @@
++CGI / FastCGI
++Rasmus Lerdorf, Stig Bakken, Shane Caraveo, Dmitry Stogov
+diff -Naur php-src-vanilla/sapi/fpm/cgi/fastcgi.c php-src/sapi/fpm/cgi/fastcgi.c
+--- php-src-vanilla/sapi/fpm/cgi/fastcgi.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/cgi/fastcgi.c 2009-10-18 21:05:39.302497288 +0100
+@@ -0,0 +1,1319 @@
++/*
++ +----------------------------------------------------------------------+
++ | PHP Version 5 |
++ +----------------------------------------------------------------------+
++ | Copyright (c) 1997-2008 The PHP Group |
++ +----------------------------------------------------------------------+
++ | This source file is subject to version 3.01 of the PHP license, |
++ | that is bundled with this package in the file LICENSE, and is |
++ | available through the world-wide-web at the following url: |
++ | http://www.php.net/license/3_01.txt |
++ | If you did not receive a copy of the PHP license and are unable to |
++ | obtain it through the world-wide-web, please send a note to |
++ | license@php.net so we can mail you a copy immediately. |
++ +----------------------------------------------------------------------+
++ | Authors: Dmitry Stogov <dmitry@zend.com> |
++ +----------------------------------------------------------------------+
++*/
++
++/* $Id$ */
++
++#include <php.h>
++#include "fastcgi.h"
++
++#include <string.h>
++#include <stdlib.h>
++#include <stdio.h>
++#include <stdarg.h>
++#include <errno.h>
++
++#ifdef FPM_AUTOCONFIG_H
++#include <fpm_autoconfig.h>
++#else
++#include <php_config.h>
++#endif
++#include <fpm/fpm.h>
++#include <fpm/fpm_request.h>
++
++#ifdef _WIN32
++
++#include <windows.h>
++
++ typedef unsigned int in_addr_t;
++
++ struct sockaddr_un {
++ short sun_family;
++ char sun_path[MAXPATHLEN];
++ };
++
++ static HANDLE fcgi_accept_mutex = INVALID_HANDLE_VALUE;
++ static int is_impersonate = 0;
++
++#define FCGI_LOCK(fd) \
++ if (fcgi_accept_mutex != INVALID_HANDLE_VALUE) { \
++ DWORD ret; \
++ while ((ret = WaitForSingleObject(fcgi_accept_mutex, 1000)) == WAIT_TIMEOUT) { \
++ if (in_shutdown) return -1; \
++ } \
++ if (ret == WAIT_FAILED) { \
++ fprintf(stderr, "WaitForSingleObject() failed\n"); \
++ return -1; \
++ } \
++ }
++
++#define FCGI_UNLOCK(fd) \
++ if (fcgi_accept_mutex != INVALID_HANDLE_VALUE) { \
++ ReleaseMutex(fcgi_accept_mutex); \
++ }
++
++#else
++
++# include <sys/types.h>
++# include <sys/stat.h>
++# include <unistd.h>
++# include <fcntl.h>
++# include <sys/socket.h>
++# include <sys/un.h>
++# include <netinet/in.h>
++# include <arpa/inet.h>
++# include <netdb.h>
++# include <signal.h>
++
++# define closesocket(s) close(s)
++
++# if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
++# include <sys/poll.h>
++# endif
++# if defined(HAVE_SYS_SELECT_H)
++# include <sys/select.h>
++# endif
++
++#ifndef INADDR_NONE
++#define INADDR_NONE ((unsigned long) -1)
++#endif
++
++# ifndef HAVE_SOCKLEN_T
++ typedef unsigned int socklen_t;
++# endif
++
++# ifdef USE_LOCKING
++# define FCGI_LOCK(fd) \
++ do { \
++ struct flock lock; \
++ lock.l_type = F_WRLCK; \
++ lock.l_start = 0; \
++ lock.l_whence = SEEK_SET; \
++ lock.l_len = 0; \
++ if (fcntl(fd, F_SETLKW, &lock) != -1) { \
++ break; \
++ } else if (errno != EINTR || in_shutdown) { \
++ return -1; \
++ } \
++ } while (1)
++
++# define FCGI_UNLOCK(fd) \
++ do { \
++ int orig_errno = errno; \
++ while (1) { \
++ struct flock lock; \
++ lock.l_type = F_UNLCK; \
++ lock.l_start = 0; \
++ lock.l_whence = SEEK_SET; \
++ lock.l_len = 0; \
++ if (fcntl(fd, F_SETLK, &lock) != -1) { \
++ break; \
++ } else if (errno != EINTR) { \
++ return -1; \
++ } \
++ } \
++ errno = orig_errno; \
++ } while (0)
++# else
++# define FCGI_LOCK(fd)
++# define FCGI_UNLOCK(fd)
++# endif
++
++#endif
++
++typedef union _sa_t {
++ struct sockaddr sa;
++ struct sockaddr_un sa_unix;
++ struct sockaddr_in sa_inet;
++} sa_t;
++
++static HashTable *fcgi_mgmt_vars;
++
++static int is_initialized = 0;
++static int is_fastcgi = 0;
++static int in_shutdown = 0;
++static in_addr_t *allowed_clients = NULL;
++
++#ifdef _WIN32
++
++static DWORD WINAPI fcgi_shutdown_thread(LPVOID arg)
++{
++ HANDLE shutdown_event = (HANDLE) arg;
++ WaitForSingleObject(shutdown_event, INFINITE);
++ in_shutdown = 1;
++ return 0;
++}
++
++#else
++
++static void fcgi_signal_handler(int signo)
++{
++ if (signo == SIGUSR1 || signo == SIGTERM) {
++ in_shutdown = 1;
++ }
++}
++
++static void fcgi_setup_signals(void)
++{
++ struct sigaction new_sa, old_sa;
++
++ sigemptyset(&new_sa.sa_mask);
++ new_sa.sa_flags = 0;
++ new_sa.sa_handler = fcgi_signal_handler;
++ sigaction(SIGUSR1, &new_sa, NULL);
++ sigaction(SIGTERM, &new_sa, NULL);
++ sigaction(SIGPIPE, NULL, &old_sa);
++ if (old_sa.sa_handler == SIG_DFL) {
++ sigaction(SIGPIPE, &new_sa, NULL);
++ }
++}
++#endif
++
++int fcgi_in_shutdown(void)
++{
++ return in_shutdown;
++}
++
++int fcgi_init(void)
++{
++ if (!is_initialized) {
++ fcgi_mgmt_vars = pemalloc(sizeof(HashTable), 1);
++ zend_hash_init(fcgi_mgmt_vars, 3, NULL, fcgi_free_mgmt_var_cb, 1);
++ fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, "1", sizeof("1")-1);
++ fcgi_set_mgmt_var("FCGI_MAX_REQS", sizeof("FCGI_MAX_REQS")-1, "1", sizeof("1")-1);
++ fcgi_set_mgmt_var("FCGI_MPXS_CONNS", sizeof("FCGI_MPXS_CONNS")-1, "0", sizeof("0")-1);
++#ifdef _WIN32
++# if 0
++ /* TODO: Support for TCP sockets */
++ WSADATA wsaData;
++
++ if (WSAStartup(MAKEWORD(2,0), &wsaData)) {
++ fprintf(stderr, "Error starting Windows Sockets. Error: %d", WSAGetLastError());
++ return 0;
++ }
++# endif
++ is_initialized = 1;
++
++ if ((GetStdHandle(STD_OUTPUT_HANDLE) == INVALID_HANDLE_VALUE) &&
++ (GetStdHandle(STD_ERROR_HANDLE) == INVALID_HANDLE_VALUE) &&
++ (GetStdHandle(STD_INPUT_HANDLE) != INVALID_HANDLE_VALUE)) {
++ char *str;
++ DWORD pipe_mode = PIPE_READMODE_BYTE | PIPE_WAIT;
++ HANDLE pipe = GetStdHandle(STD_INPUT_HANDLE);
++
++ SetNamedPipeHandleState(pipe, &pipe_mode, NULL, NULL);
++
++ str = getenv("_FCGI_SHUTDOWN_EVENT_");
++ if (str != NULL) {
++ HANDLE shutdown_event = (HANDLE) atoi(str);
++ if (!CreateThread(NULL, 0, fcgi_shutdown_thread,
++ shutdown_event, 0, NULL)) {
++ return -1;
++ }
++ }
++ str = getenv("_FCGI_MUTEX_");
++ if (str != NULL) {
++ fcgi_accept_mutex = (HANDLE) atoi(str);
++ }
++ return is_fastcgi = 1;
++ } else {
++ return is_fastcgi = 0;
++ }
++#else
++ sa_t sa;
++ socklen_t len = sizeof(sa);
++
++ is_initialized = 1;
++ errno = 0;
++ if (getpeername(0, (struct sockaddr *)&sa, &len) != 0 && errno == ENOTCONN) {
++ fcgi_setup_signals();
++ return is_fastcgi = 1;
++ } else {
++ return is_fastcgi = 0;
++ }
++
++ fcgi_set_allowed_clients(getenv("FCGI_WEB_SERVER_ADDRS"));
++#endif
++ }
++ return is_fastcgi;
++}
++
++
++int fcgi_is_fastcgi(void)
++{
++ if (!is_initialized) {
++ return fcgi_init();
++ } else {
++ return is_fastcgi;
++ }
++}
++
++void fcgi_set_is_fastcgi(int new_value)
++{
++ is_fastcgi = new_value;
++}
++
++void fcgi_set_in_shutdown(int new_value)
++{
++ in_shutdown = new_value;
++}
++
++void fcgi_shutdown(void)
++{
++ if (is_initialized) {
++ zend_hash_destroy(fcgi_mgmt_vars);
++ pefree(fcgi_mgmt_vars, 1);
++ }
++ is_fastcgi = 0;
++
++ if (allowed_clients) {
++ free(allowed_clients);
++ allowed_clients = 0;
++ }
++}
++
++#ifdef _WIN32
++/* Do some black magic with the NT security API.
++ * We prepare a DACL (Discretionary Access Control List) so that
++ * we, the creator, are allowed all access, while "Everyone Else"
++ * is only allowed to read and write to the pipe.
++ * This avoids security issues on shared hosts where a luser messes
++ * with the lower-level pipe settings and screws up the FastCGI service.
++ */
++static PACL prepare_named_pipe_acl(PSECURITY_DESCRIPTOR sd, LPSECURITY_ATTRIBUTES sa)
++{
++ DWORD req_acl_size;
++ char everyone_buf[32], owner_buf[32];
++ PSID sid_everyone, sid_owner;
++ SID_IDENTIFIER_AUTHORITY
++ siaWorld = SECURITY_WORLD_SID_AUTHORITY,
++ siaCreator = SECURITY_CREATOR_SID_AUTHORITY;
++ PACL acl;
++
++ sid_everyone = (PSID)&everyone_buf;
++ sid_owner = (PSID)&owner_buf;
++
++ req_acl_size = sizeof(ACL) +
++ (2 * ((sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) + GetSidLengthRequired(1)));
++
++ acl = malloc(req_acl_size);
++
++ if (acl == NULL) {
++ return NULL;
++ }
++
++ if (!InitializeSid(sid_everyone, &siaWorld, 1)) {
++ goto out_fail;
++ }
++ *GetSidSubAuthority(sid_everyone, 0) = SECURITY_WORLD_RID;
++
++ if (!InitializeSid(sid_owner, &siaCreator, 1)) {
++ goto out_fail;
++ }
++ *GetSidSubAuthority(sid_owner, 0) = SECURITY_CREATOR_OWNER_RID;
++
++ if (!InitializeAcl(acl, req_acl_size, ACL_REVISION)) {
++ goto out_fail;
++ }
++
++ if (!AddAccessAllowedAce(acl, ACL_REVISION, FILE_GENERIC_READ | FILE_GENERIC_WRITE, sid_everyone)) {
++ goto out_fail;
++ }
++
++ if (!AddAccessAllowedAce(acl, ACL_REVISION, FILE_ALL_ACCESS, sid_owner)) {
++ goto out_fail;
++ }
++
++ if (!InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION)) {
++ goto out_fail;
++ }
++
++ if (!SetSecurityDescriptorDacl(sd, TRUE, acl, FALSE)) {
++ goto out_fail;
++ }
++
++ sa->lpSecurityDescriptor = sd;
++
++ return acl;
++
++out_fail:
++ free(acl);
++ return NULL;
++}
++#endif
++
++void fcgi_set_allowed_clients(char *ip)
++{
++ char *cur, *end;
++ int n;
++
++ if (ip) {
++ ip = strdup(ip);
++ cur = ip;
++ n = 0;
++ while (*cur) {
++ if (*cur == ',') n++;
++ cur++;
++ }
++ if (allowed_clients) free(allowed_clients);
++ allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
++ n = 0;
++ cur = ip;
++ while (cur) {
++ end = strchr(cur, ',');
++ if (end) {
++ *end = 0;
++ end++;
++ }
++ allowed_clients[n] = inet_addr(cur);
++ if (allowed_clients[n] == INADDR_NONE) {
++ fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
++ }
++ n++;
++ cur = end;
++ }
++ allowed_clients[n] = INADDR_NONE;
++ free(ip);
++ }
++}
++
++static int is_port_number(const char *bindpath)
++{
++ while (*bindpath) {
++ if (*bindpath < '0' || *bindpath > '9') {
++ return 0;
++ }
++ bindpath++;
++ }
++ return 1;
++}
++
++int fcgi_listen(const char *path, int backlog)
++{
++ char *s;
++ int tcp = 0;
++ char host[MAXPATHLEN];
++ short port = 0;
++ int listen_socket;
++ sa_t sa;
++ socklen_t sock_len;
++#ifdef SO_REUSEADDR
++# ifdef _WIN32
++ BOOL reuse = 1;
++# else
++ int reuse = 1;
++# endif
++#endif
++
++ if ((s = strchr(path, ':'))) {
++ port = atoi(s+1);
++ if (port != 0 && (s-path) < MAXPATHLEN) {
++ strncpy(host, path, s-path);
++ host[s-path] = '\0';
++ tcp = 1;
++ }
++ } else if (is_port_number(path)) {
++ port = atoi(path);
++ if (port != 0) {
++ host[0] = '\0';
++ tcp = 1;
++ }
++ }
++
++ /* Prepare socket address */
++ if (tcp) {
++ memset(&sa.sa_inet, 0, sizeof(sa.sa_inet));
++ sa.sa_inet.sin_family = AF_INET;
++ sa.sa_inet.sin_port = htons(port);
++ sock_len = sizeof(sa.sa_inet);
++
++ if (!*host || !strncmp(host, "*", sizeof("*")-1)) {
++ sa.sa_inet.sin_addr.s_addr = htonl(INADDR_ANY);
++ } else {
++ sa.sa_inet.sin_addr.s_addr = inet_addr(host);
++ if (sa.sa_inet.sin_addr.s_addr == INADDR_NONE) {
++ struct hostent *hep;
++
++ hep = gethostbyname(host);
++ if (!hep || hep->h_addrtype != AF_INET || !hep->h_addr_list[0]) {
++ fprintf(stderr, "Cannot resolve host name '%s'!\n", host);
++ return -1;
++ } else if (hep->h_addr_list[1]) {
++ fprintf(stderr, "Host '%s' has multiple addresses. You must choose one explicitly!\n", host);
++ return -1;
++ }
++ sa.sa_inet.sin_addr.s_addr = ((struct in_addr*)hep->h_addr_list[0])->s_addr;
++ }
++ }
++ } else {
++#ifdef _WIN32
++ SECURITY_DESCRIPTOR sd;
++ SECURITY_ATTRIBUTES sa;
++ PACL acl;
++ HANDLE namedPipe;
++
++ memset(&sa, 0, sizeof(sa));
++ sa.nLength = sizeof(sa);
++ sa.bInheritHandle = FALSE;
++ acl = prepare_named_pipe_acl(&sd, &sa);
++
++ namedPipe = CreateNamedPipe(path,
++ PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
++ PIPE_TYPE_BYTE | PIPE_WAIT | PIPE_READMODE_BYTE,
++ PIPE_UNLIMITED_INSTANCES,
++ 8192, 8192, 0, &sa);
++ if (namedPipe == INVALID_HANDLE_VALUE) {
++ return -1;
++ }
++ listen_socket = _open_osfhandle((long)namedPipe, 0);
++ if (!is_initialized) {
++ fcgi_init();
++ }
++ is_fastcgi = 1;
++ return listen_socket;
++
++#else
++ int path_len = strlen(path);
++
++ if (path_len >= sizeof(sa.sa_unix.sun_path)) {
++ fprintf(stderr, "Listening socket's path name is too long.\n");
++ return -1;
++ }
++
++ memset(&sa.sa_unix, 0, sizeof(sa.sa_unix));
++ sa.sa_unix.sun_family = AF_UNIX;
++ memcpy(sa.sa_unix.sun_path, path, path_len + 1);
++ sock_len = (size_t)(((struct sockaddr_un *)0)->sun_path) + path_len;
++#ifdef HAVE_SOCKADDR_UN_SUN_LEN
++ sa.sa_unix.sun_len = sock_len;
++#endif
++ unlink(path);
++#endif
++ }
++
++ /* Create, bind socket and start listen on it */
++ if ((listen_socket = socket(sa.sa.sa_family, SOCK_STREAM, 0)) < 0 ||
++#ifdef SO_REUSEADDR
++ setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse)) < 0 ||
++#endif
++ bind(listen_socket, (struct sockaddr *) &sa, sock_len) < 0 ||
++ listen(listen_socket, backlog) < 0) {
++
++ fprintf(stderr, "Cannot bind/listen socket - [%d] %s.\n",errno, strerror(errno));
++ return -1;
++ }
++
++ if (!tcp) {
++ chmod(path, 0777);
++ }
++
++ if (!is_initialized) {
++ fcgi_init();
++ }
++ is_fastcgi = 1;
++
++#ifdef _WIN32
++ if (tcp) {
++ listen_socket = _open_osfhandle((long)listen_socket, 0);
++ }
++#else
++ fcgi_setup_signals();
++#endif
++ return listen_socket;
++}
++
++void fcgi_init_request(fcgi_request *req, int listen_socket)
++{
++ memset(req, 0, sizeof(fcgi_request));
++ req->listen_socket = listen_socket;
++ req->fd = -1;
++ req->id = -1;
++
++ req->in_len = 0;
++ req->in_pad = 0;
++
++ req->out_hdr = NULL;
++ req->out_pos = req->out_buf;
++
++#ifdef _WIN32
++ req->tcp = !GetNamedPipeInfo((HANDLE)_get_osfhandle(req->listen_socket), NULL, NULL, NULL, NULL);
++#endif
++}
++
++static inline ssize_t safe_write(fcgi_request *req, const void *buf, size_t count)
++{
++ int ret;
++ size_t n = 0;
++
++ do {
++ errno = 0;
++#ifdef _WIN32
++ if (!req->tcp) {
++ ret = write(req->fd, ((char*)buf)+n, count-n);
++ } else {
++ ret = send(req->fd, ((char*)buf)+n, count-n, 0);
++ if (ret <= 0) {
++ errno = WSAGetLastError();
++ }
++ }
++#else
++ ret = write(req->fd, ((char*)buf)+n, count-n);
++#endif
++ if (ret > 0) {
++ n += ret;
++ } else if (ret <= 0 && errno != 0 && errno != EINTR) {
++ return ret;
++ }
++ } while (n != count);
++ return n;
++}
++
++static inline ssize_t safe_read(fcgi_request *req, const void *buf, size_t count)
++{
++ int ret;
++ size_t n = 0;
++
++ do {
++ errno = 0;
++#ifdef _WIN32
++ if (!req->tcp) {
++ ret = read(req->fd, ((char*)buf)+n, count-n);
++ } else {
++ ret = recv(req->fd, ((char*)buf)+n, count-n, 0);
++ if (ret <= 0) {
++ errno = WSAGetLastError();
++ }
++ }
++#else
++ ret = read(req->fd, ((char*)buf)+n, count-n);
++#endif
++ if (ret > 0) {
++ n += ret;
++ } else if (ret == 0 && errno == 0) {
++ return n;
++ } else if (ret <= 0 && errno != 0 && errno != EINTR) {
++ return ret;
++ }
++ } while (n != count);
++ return n;
++}
++
++static inline int fcgi_make_header(fcgi_header *hdr, fcgi_request_type type, int req_id, int len)
++{
++ int pad = ((len + 7) & ~7) - len;
++
++ hdr->contentLengthB0 = (unsigned char)(len & 0xff);
++ hdr->contentLengthB1 = (unsigned char)((len >> 8) & 0xff);
++ hdr->paddingLength = (unsigned char)pad;
++ hdr->requestIdB0 = (unsigned char)(req_id & 0xff);
++ hdr->requestIdB1 = (unsigned char)((req_id >> 8) & 0xff);
++ hdr->reserved = 0;
++ hdr->type = type;
++ hdr->version = FCGI_VERSION_1;
++ if (pad) {
++ memset(((unsigned char*)hdr) + sizeof(fcgi_header) + len, 0, pad);
++ }
++ return pad;
++}
++
++static int fcgi_get_params(fcgi_request *req, unsigned char *p, unsigned char *end)
++{
++ char buf[128];
++ char *tmp = buf;
++ int buf_size = sizeof(buf);
++ int name_len, val_len;
++ char *s;
++ int ret = 1;
++
++ while (p < end) {
++ name_len = *p++;
++ if (name_len >= 128) {
++ name_len = ((name_len & 0x7f) << 24);
++ name_len |= (*p++ << 16);
++ name_len |= (*p++ << 8);
++ name_len |= *p++;
++ }
++ val_len = *p++;
++ if (val_len >= 128) {
++ val_len = ((val_len & 0x7f) << 24);
++ val_len |= (*p++ << 16);
++ val_len |= (*p++ << 8);
++ val_len |= *p++;
++ }
++ if (name_len + val_len < 0 ||
++ name_len + val_len > end - p) {
++ /* Malformated request */
++ ret = 0;
++ break;
++ }
++ if (name_len+1 >= buf_size) {
++ buf_size = name_len + 64;
++ tmp = (tmp == buf ? emalloc(buf_size): erealloc(tmp, buf_size));
++ }
++ memcpy(tmp, p, name_len);
++ tmp[name_len] = 0;
++ s = zend_strndup((char*)p + name_len, val_len);
++ zend_hash_update(&req->env, tmp, name_len+1, &s, sizeof(char*), NULL);
++ p += name_len + val_len;
++ }
++ if (tmp != buf && tmp != NULL) {
++ efree(tmp);
++ }
++ return ret;
++}
++
++static void fcgi_free_var(char **s)
++{
++ free(*s);
++}
++
++static int fcgi_read_request(fcgi_request *req)
++{
++ fcgi_header hdr;
++ int len, padding;
++ unsigned char buf[FCGI_MAX_LENGTH+8];
++
++ req->keep = 0;
++ req->in_len = 0;
++ req->out_hdr = NULL;
++ req->out_pos = req->out_buf;
++ zend_hash_init(&req->env, 0, NULL, (void (*)(void *)) fcgi_free_var, 1);
++
++ if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) ||
++ hdr.version < FCGI_VERSION_1) {
++ return 0;
++ }
++
++ len = (hdr.contentLengthB1 << 8) | hdr.contentLengthB0;
++ padding = hdr.paddingLength;
++
++ while (hdr.type == FCGI_STDIN && len == 0) {
++ if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) ||
++ hdr.version < FCGI_VERSION_1) {
++ return 0;
++ }
++
++ len = (hdr.contentLengthB1 << 8) | hdr.contentLengthB0;
++ padding = hdr.paddingLength;
++ }
++
++ if (len + padding > FCGI_MAX_LENGTH) {
++ return 0;
++ }
++
++ req->id = (hdr.requestIdB1 << 8) + hdr.requestIdB0;
++
++ if (hdr.type == FCGI_BEGIN_REQUEST && len == sizeof(fcgi_begin_request)) {
++ char *val;
++
++ if (safe_read(req, buf, len+padding) != len+padding) {
++ return 0;
++ }
++
++ req->keep = (((fcgi_begin_request*)buf)->flags & FCGI_KEEP_CONN);
++ switch ((((fcgi_begin_request*)buf)->roleB1 << 8) + ((fcgi_begin_request*)buf)->roleB0) {
++ case FCGI_RESPONDER:
++ val = strdup("RESPONDER");
++ zend_hash_update(&req->env, "FCGI_ROLE", sizeof("FCGI_ROLE"), &val, sizeof(char*), NULL);
++ break;
++ case FCGI_AUTHORIZER:
++ val = strdup("AUTHORIZER");
++ zend_hash_update(&req->env, "FCGI_ROLE", sizeof("FCGI_ROLE"), &val, sizeof(char*), NULL);
++ break;
++ case FCGI_FILTER:
++ val = strdup("FILTER");
++ zend_hash_update(&req->env, "FCGI_ROLE", sizeof("FCGI_ROLE"), &val, sizeof(char*), NULL);
++ break;
++ default:
++ return 0;
++ }
++
++ if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) ||
++ hdr.version < FCGI_VERSION_1) {
++ return 0;
++ }
++
++ len = (hdr.contentLengthB1 << 8) | hdr.contentLengthB0;
++ padding = hdr.paddingLength;
++
++ while (hdr.type == FCGI_PARAMS && len > 0) {
++ if (len + padding > FCGI_MAX_LENGTH) {
++ return 0;
++ }
++
++ if (safe_read(req, buf, len+padding) != len+padding) {
++ req->keep = 0;
++ return 0;
++ }
++
++ if (!fcgi_get_params(req, buf, buf+len)) {
++ req->keep = 0;
++ return 0;
++ }
++
++ if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) ||
++ hdr.version < FCGI_VERSION_1) {
++ req->keep = 0;
++ return 0;
++ }
++ len = (hdr.contentLengthB1 << 8) | hdr.contentLengthB0;
++ padding = hdr.paddingLength;
++ }
++ } else if (hdr.type == FCGI_GET_VALUES) {
++ unsigned char *p = buf + sizeof(fcgi_header);
++ HashPosition pos;
++ char * str_index;
++ uint str_length;
++ ulong num_index;
++ int key_type;
++ zval ** value;
++
++ if (safe_read(req, buf, len+padding) != len+padding) {
++ req->keep = 0;
++ return 0;
++ }
++
++ if (!fcgi_get_params(req, buf, buf+len)) {
++ req->keep = 0;
++ return 0;
++ }
++
++ zend_hash_internal_pointer_reset_ex(&req->env, &pos);
++ while ((key_type = zend_hash_get_current_key_ex(&req->env, &str_index, &str_length, &num_index, 0, &pos)) != HASH_KEY_NON_EXISTANT) {
++ int zlen;
++ zend_hash_move_forward_ex(&req->env, &pos);
++ if (key_type != HASH_KEY_IS_STRING) {
++ continue;
++ }
++ if (zend_hash_find(fcgi_mgmt_vars, str_index, str_length, (void**) &value) != SUCCESS) {
++ continue;
++ }
++ --str_length;
++ zlen = Z_STRLEN_PP(value);
++ if ((p + 4 + 4 + str_length + zlen) >= (buf + sizeof(buf))) {
++ break;
++ }
++ if (str_length < 0x80) {
++ *p++ = str_length;
++ } else {
++ *p++ = ((str_length >> 24) & 0xff) | 0x80;
++ *p++ = (str_length >> 16) & 0xff;
++ *p++ = (str_length >> 8) & 0xff;
++ *p++ = str_length & 0xff;
++ }
++ if (zlen < 0x80) {
++ *p++ = zlen;
++ } else {
++ *p++ = ((zlen >> 24) & 0xff) | 0x80;
++ *p++ = (zlen >> 16) & 0xff;
++ *p++ = (zlen >> 8) & 0xff;
++ *p++ = zlen & 0xff;
++ }
++ memcpy(p, str_index, str_length);
++ p += str_length;
++ memcpy(p, Z_STRVAL_PP(value), zlen);
++ p += zlen;
++ }
++ len = p - buf - sizeof(fcgi_header);
++ len += fcgi_make_header((fcgi_header*)buf, FCGI_GET_VALUES_RESULT, 0, len);
++ if (safe_write(req, buf, sizeof(fcgi_header)+len) != (int)sizeof(fcgi_header)+len) {
++ req->keep = 0;
++ return 0;
++ }
++ return 0;
++ } else {
++ return 0;
++ }
++
++ return 1;
++}
++
++int fcgi_read(fcgi_request *req, char *str, int len)
++{
++ int ret, n, rest;
++ fcgi_header hdr;
++ unsigned char buf[255];
++
++ n = 0;
++ rest = len;
++ while (rest > 0) {
++ if (req->in_len == 0) {
++ if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) ||
++ hdr.version < FCGI_VERSION_1 ||
++ hdr.type != FCGI_STDIN) {
++ req->keep = 0;
++ return 0;
++ }
++ req->in_len = (hdr.contentLengthB1 << 8) | hdr.contentLengthB0;
++ req->in_pad = hdr.paddingLength;
++ if (req->in_len == 0) {
++ return n;
++ }
++ }
++
++ if (req->in_len >= rest) {
++ ret = safe_read(req, str, rest);
++ } else {
++ ret = safe_read(req, str, req->in_len);
++ }
++ if (ret < 0) {
++ req->keep = 0;
++ return ret;
++ } else if (ret > 0) {
++ req->in_len -= ret;
++ rest -= ret;
++ n += ret;
++ str += ret;
++ if (req->in_len == 0) {
++ if (req->in_pad) {
++ if (safe_read(req, buf, req->in_pad) != req->in_pad) {
++ req->keep = 0;
++ return ret;
++ }
++ }
++ } else {
++ return n;
++ }
++ } else {
++ return n;
++ }
++ }
++ return n;
++}
++
++void fcgi_close(fcgi_request *req, int force, int destroy)
++{
++ if (destroy) {
++ zend_hash_destroy(&req->env);
++ }
++
++#ifdef _WIN32
++ if (is_impersonate && !req->tcp) {
++ RevertToSelf();
++ }
++#endif
++
++ if ((force || !req->keep) && req->fd >= 0) {
++#ifdef _WIN32
++ if (!req->tcp) {
++ HANDLE pipe = (HANDLE)_get_osfhandle(req->fd);
++
++ if (!force) {
++ FlushFileBuffers(pipe);
++ }
++ DisconnectNamedPipe(pipe);
++ } else {
++ if (!force) {
++ char buf[8];
++
++ shutdown(req->fd, 1);
++ while (recv(req->fd, buf, sizeof(buf), 0) > 0) {}
++ }
++ closesocket(req->fd);
++ }
++#else
++ if (!force) {
++ char buf[8];
++
++ shutdown(req->fd, 1);
++ while (recv(req->fd, buf, sizeof(buf), 0) > 0) {}
++ }
++ close(req->fd);
++#endif
++ req->fd = -1;
++
++ fpm_request_finished();
++ }
++}
++
++int fcgi_accept_request(fcgi_request *req)
++{
++#ifdef _WIN32
++ HANDLE pipe;
++ OVERLAPPED ov;
++#endif
++ fcgi_finish_request(req);
++
++ while (1) {
++ if (req->fd < 0) {
++ while (1) {
++ if (in_shutdown) {
++ return -1;
++ }
++#ifdef _WIN32
++ if (!req->tcp) {
++ pipe = (HANDLE)_get_osfhandle(req->listen_socket);
++ FCGI_LOCK(req->listen_socket);
++ ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
++ if (!ConnectNamedPipe(pipe, &ov)) {
++ errno = GetLastError();
++ if (errno == ERROR_IO_PENDING) {
++ while (WaitForSingleObject(ov.hEvent, 1000) == WAIT_TIMEOUT) {
++ if (in_shutdown) {
++ CloseHandle(ov.hEvent);
++ FCGI_UNLOCK(req->listen_socket);
++ return -1;
++ }
++ }
++ } else if (errno != ERROR_PIPE_CONNECTED) {
++ }
++ }
++ CloseHandle(ov.hEvent);
++ req->fd = req->listen_socket;
++ FCGI_UNLOCK(req->listen_socket);
++ } else {
++ SOCKET listen_socket = (SOCKET)_get_osfhandle(req->listen_socket);
++#else
++ {
++ int listen_socket = req->listen_socket;
++#endif
++ sa_t sa;
++ socklen_t len = sizeof(sa);
++
++ fpm_request_accepting();
++
++ FCGI_LOCK(req->listen_socket);
++ req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
++ FCGI_UNLOCK(req->listen_socket);
++ if (req->fd >= 0 && allowed_clients) {
++ int n = 0;
++ int allowed = 0;
++
++ while (allowed_clients[n] != INADDR_NONE) {
++ if (allowed_clients[n] == sa.sa_inet.sin_addr.s_addr) {
++ allowed = 1;
++ break;
++ }
++ n++;
++ }
++ if (!allowed) {
++ fprintf(stderr, "Connection from disallowed IP address '%s' is dropped.\n", inet_ntoa(sa.sa_inet.sin_addr));
++ closesocket(req->fd);
++ req->fd = -1;
++ continue;
++ }
++ }
++ }
++
++#ifdef _WIN32
++ if (req->fd < 0 && (in_shutdown || errno != EINTR)) {
++#else
++ if (req->fd < 0 && (in_shutdown || (errno != EINTR && errno != ECONNABORTED))) {
++#endif
++ return -1;
++ }
++
++#ifdef _WIN32
++ break;
++#else
++ if (req->fd >= 0) {
++
++ fpm_request_reading_headers();
++
++#if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
++ struct pollfd fds;
++ int ret;
++
++ fds.fd = req->fd;
++ fds.events = POLLIN;
++ fds.revents = 0;
++ do {
++ errno = 0;
++ ret = poll(&fds, 1, 5000);
++ } while (ret < 0 && errno == EINTR);
++ if (ret > 0 && (fds.revents & POLLIN)) {
++ break;
++ }
++ fcgi_close(req, 1, 0);
++#else
++ if (req->fd < FD_SETSIZE) {
++ struct timeval tv = {5,0};
++ fd_set set;
++ int ret;
++
++ FD_ZERO(&set);
++ FD_SET(req->fd, &set);
++ do {
++ errno = 0;
++ ret = select(req->fd + 1, &set, NULL, NULL, &tv) >= 0;
++ } while (ret < 0 && errno == EINTR);
++ if (ret > 0 && FD_ISSET(req->fd, &set)) {
++ break;
++ }
++ fcgi_close(req, 1, 0);
++ } else {
++ fprintf(stderr, "Too many open file descriptors. FD_SETSIZE limit exceeded.");
++ fcgi_close(req, 1, 0);
++ }
++#endif
++ }
++#endif
++ }
++ } else if (in_shutdown) {
++ return -1;
++ }
++ if (fcgi_read_request(req)) {
++#ifdef _WIN32
++ if (is_impersonate && !req->tcp) {
++ pipe = (HANDLE)_get_osfhandle(req->fd);
++ if (!ImpersonateNamedPipeClient(pipe)) {
++ fcgi_close(req, 1, 1);
++ continue;
++ }
++ }
++#endif
++ return req->fd;
++ } else {
++ fcgi_close(req, 1, 1);
++ }
++ }
++}
++
++static inline fcgi_header* open_packet(fcgi_request *req, fcgi_request_type type)
++{
++ req->out_hdr = (fcgi_header*) req->out_pos;
++ req->out_hdr->type = type;
++ req->out_pos += sizeof(fcgi_header);
++ return req->out_hdr;
++}
++
++static inline void close_packet(fcgi_request *req)
++{
++ if (req->out_hdr) {
++ int len = req->out_pos - ((unsigned char*)req->out_hdr + sizeof(fcgi_header));
++
++ req->out_pos += fcgi_make_header(req->out_hdr, (fcgi_request_type)req->out_hdr->type, req->id, len);
++ req->out_hdr = NULL;
++ }
++}
++
++int fcgi_flush(fcgi_request *req, int close)
++{
++ int len;
++
++ close_packet(req);
++
++ len = req->out_pos - req->out_buf;
++
++ if (close) {
++ fcgi_end_request_rec *rec = (fcgi_end_request_rec*)(req->out_pos);
++
++ fcgi_make_header(&rec->hdr, FCGI_END_REQUEST, req->id, sizeof(fcgi_end_request));
++ rec->body.appStatusB3 = 0;
++ rec->body.appStatusB2 = 0;
++ rec->body.appStatusB1 = 0;
++ rec->body.appStatusB0 = 0;
++ rec->body.protocolStatus = FCGI_REQUEST_COMPLETE;
++ len += sizeof(fcgi_end_request_rec);
++ }
++
++ if (safe_write(req, req->out_buf, len) != len) {
++ req->keep = 0;
++ return 0;
++ }
++
++ req->out_pos = req->out_buf;
++ return 1;
++}
++
++int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len)
++{
++ int limit, rest;
++
++ if (len <= 0) {
++ return 0;
++ }
++
++ if (req->out_hdr && req->out_hdr->type != type) {
++ close_packet(req);
++ }
++#if 0
++ /* Unoptimized, but clear version */
++ rest = len;
++ while (rest > 0) {
++ limit = sizeof(req->out_buf) - (req->out_pos - req->out_buf);
++
++ if (!req->out_hdr) {
++ if (limit < sizeof(fcgi_header)) {
++ if (!fcgi_flush(req, 0)) {
++ return -1;
++ }
++ }
++ open_packet(req, type);
++ }
++ limit = sizeof(req->out_buf) - (req->out_pos - req->out_buf);
++ if (rest < limit) {
++ memcpy(req->out_pos, str, rest);
++ req->out_pos += rest;
++ return len;
++ } else {
++ memcpy(req->out_pos, str, limit);
++ req->out_pos += limit;
++ rest -= limit;
++ str += limit;
++ if (!fcgi_flush(req, 0)) {
++ return -1;
++ }
++ }
++ }
++#else
++ /* Optimized version */
++ limit = sizeof(req->out_buf) - (req->out_pos - req->out_buf);
++ if (!req->out_hdr) {
++ limit -= sizeof(fcgi_header);
++ if (limit < 0) limit = 0;
++ }
++
++ if (len < limit) {
++ if (!req->out_hdr) {
++ open_packet(req, type);
++ }
++ memcpy(req->out_pos, str, len);
++ req->out_pos += len;
++ } else if (len - limit < sizeof(req->out_buf) - sizeof(fcgi_header)) {
++ if (!req->out_hdr) {
++ open_packet(req, type);
++ }
++ if (limit > 0) {
++ memcpy(req->out_pos, str, limit);
++ req->out_pos += limit;
++ }
++ if (!fcgi_flush(req, 0)) {
++ return -1;
++ }
++ if (len > limit) {
++ open_packet(req, type);
++ memcpy(req->out_pos, str + limit, len - limit);
++ req->out_pos += len - limit;
++ }
++ } else {
++ int pos = 0;
++ int pad;
++
++ close_packet(req);
++ while ((len - pos) > 0xffff) {
++ open_packet(req, type);
++ fcgi_make_header(req->out_hdr, type, req->id, 0xfff8);
++ req->out_hdr = NULL;
++ if (!fcgi_flush(req, 0)) {
++ return -1;
++ }
++ if (safe_write(req, str + pos, 0xfff8) != 0xfff8) {
++ req->keep = 0;
++ return -1;
++ }
++ pos += 0xfff8;
++ }
++
++ pad = (((len - pos) + 7) & ~7) - (len - pos);
++ rest = pad ? 8 - pad : 0;
++
++ open_packet(req, type);
++ fcgi_make_header(req->out_hdr, type, req->id, (len - pos) - rest);
++ req->out_hdr = NULL;
++ if (!fcgi_flush(req, 0)) {
++ return -1;
++ }
++ if (safe_write(req, str + pos, (len - pos) - rest) != (len - pos) - rest) {
++ req->keep = 0;
++ return -1;
++ }
++ if (pad) {
++ open_packet(req, type);
++ memcpy(req->out_pos, str + len - rest, rest);
++ req->out_pos += rest;
++ }
++ }
++#endif
++ return len;
++}
++
++int fcgi_finish_request(fcgi_request *req)
++{
++ if (req->fd >= 0) {
++ fcgi_flush(req, 1);
++ fcgi_close(req, 0, 1);
++ }
++ return 1;
++}
++
++char* fcgi_getenv(fcgi_request *req, const char* var, int var_len)
++{
++ char **val;
++
++ if (!req) return NULL;
++
++ if (zend_hash_find(&req->env, (char*)var, var_len+1, (void**)&val) == SUCCESS) {
++ return *val;
++ }
++ return NULL;
++}
++
++char* fcgi_putenv(fcgi_request *req, char* var, int var_len, char* val)
++{
++ if (var && req) {
++ if (val == NULL) {
++ zend_hash_del(&req->env, var, var_len+1);
++ } else {
++ char **ret;
++
++ val = strdup(val);
++ if (zend_hash_update(&req->env, var, var_len+1, &val, sizeof(char*), (void**)&ret) == SUCCESS) {
++ return *ret;
++ }
++ }
++ }
++ return NULL;
++}
++
++#ifdef _WIN32
++void fcgi_impersonate(void)
++{
++ char *os_name;
++
++ os_name = getenv("OS");
++ if (os_name && stricmp(os_name, "Windows_NT") == 0) {
++ is_impersonate = 1;
++ }
++}
++#endif
++
++void fcgi_set_mgmt_var(char * name, size_t name_len, const char * value, size_t value_len)
++{
++ zval * zvalue;
++ zvalue = pemalloc(sizeof(*zvalue), 1);
++ Z_TYPE_P(zvalue) = IS_STRING;
++ Z_STRVAL_P(zvalue) = pestrndup(value, value_len, 1);
++ Z_STRLEN_P(zvalue) = value_len;
++ zend_hash_update(fcgi_mgmt_vars, name, name_len + 1, &zvalue, sizeof(zvalue), NULL);
++}
++
++void fcgi_free_mgmt_var_cb(void * ptr)
++{
++ zval ** var = (zval **)ptr;
++ pefree(Z_STRVAL_PP(var), 1);
++ pefree(*var, 1);
++}
++
++/*
++ * Local variables:
++ * tab-width: 4
++ * c-basic-offset: 4
++ * End:
++ * vim600: sw=4 ts=4 fdm=marker
++ * vim<600: sw=4 ts=4
++ */
+diff -Naur php-src-vanilla/sapi/fpm/cgi/fastcgi.h php-src/sapi/fpm/cgi/fastcgi.h
+--- php-src-vanilla/sapi/fpm/cgi/fastcgi.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/cgi/fastcgi.h 2009-10-18 21:05:39.302497288 +0100
+@@ -0,0 +1,150 @@
++/*
++ +----------------------------------------------------------------------+
++ | PHP Version 5 |
++ +----------------------------------------------------------------------+
++ | Copyright (c) 1997-2008 The PHP Group |
++ +----------------------------------------------------------------------+
++ | This source file is subject to version 3.01 of the PHP license, |
++ | that is bundled with this package in the file LICENSE, and is |
++ | available through the world-wide-web at the following url: |
++ | http://www.php.net/license/3_01.txt |
++ | If you did not receive a copy of the PHP license and are unable to |
++ | obtain it through the world-wide-web, please send a note to |
++ | license@php.net so we can mail you a copy immediately. |
++ +----------------------------------------------------------------------+
++ | Authors: Dmitry Stogov <dmitry@zend.com> |
++ +----------------------------------------------------------------------+
++*/
++
++/* $Id$ */
++
++/* FastCGI protocol */
++
++#define FCGI_VERSION_1 1
++
++#define FCGI_MAX_LENGTH 0xffff
++
++#define FCGI_KEEP_CONN 1
++
++typedef enum _fcgi_role {
++ FCGI_RESPONDER = 1,
++ FCGI_AUTHORIZER = 2,
++ FCGI_FILTER = 3
++} fcgi_role;
++
++typedef enum _fcgi_request_type {
++ FCGI_BEGIN_REQUEST = 1, /* [in] */
++ FCGI_ABORT_REQUEST = 2, /* [in] (not supported) */
++ FCGI_END_REQUEST = 3, /* [out] */
++ FCGI_PARAMS = 4, /* [in] environment variables */
++ FCGI_STDIN = 5, /* [in] post data */
++ FCGI_STDOUT = 6, /* [out] response */
++ FCGI_STDERR = 7, /* [out] errors */
++ FCGI_DATA = 8, /* [in] filter data (not supported) */
++ FCGI_GET_VALUES = 9, /* [in] */
++ FCGI_GET_VALUES_RESULT = 10 /* [out] */
++} fcgi_request_type;
++
++typedef enum _fcgi_protocol_status {
++ FCGI_REQUEST_COMPLETE = 0,
++ FCGI_CANT_MPX_CONN = 1,
++ FCGI_OVERLOADED = 2,
++ FCGI_UNKNOWN_ROLE = 3
++} dcgi_protocol_status;
++
++typedef struct _fcgi_header {
++ unsigned char version;
++ unsigned char type;
++ unsigned char requestIdB1;
++ unsigned char requestIdB0;
++ unsigned char contentLengthB1;
++ unsigned char contentLengthB0;
++ unsigned char paddingLength;
++ unsigned char reserved;
++} fcgi_header;
++
++typedef struct _fcgi_begin_request {
++ unsigned char roleB1;
++ unsigned char roleB0;
++ unsigned char flags;
++ unsigned char reserved[5];
++} fcgi_begin_request;
++
++typedef struct _fcgi_begin_request_rec {
++ fcgi_header hdr;
++ fcgi_begin_request body;
++} fcgi_begin_request_rec;
++
++typedef struct _fcgi_end_request {
++ unsigned char appStatusB3;
++ unsigned char appStatusB2;
++ unsigned char appStatusB1;
++ unsigned char appStatusB0;
++ unsigned char protocolStatus;
++ unsigned char reserved[3];
++} fcgi_end_request;
++
++typedef struct _fcgi_end_request_rec {
++ fcgi_header hdr;
++ fcgi_end_request body;
++} fcgi_end_request_rec;
++
++/* FastCGI client API */
++
++typedef struct _fcgi_request {
++ int listen_socket;
++#ifdef _WIN32
++ int tcp;
++#endif
++ int fd;
++ int id;
++ int keep;
++
++ int in_len;
++ int in_pad;
++
++ fcgi_header *out_hdr;
++ unsigned char *out_pos;
++ unsigned char out_buf[1024*8];
++ unsigned char reserved[sizeof(fcgi_end_request_rec)];
++
++ HashTable env;
++} fcgi_request;
++
++int fcgi_init(void);
++void fcgi_shutdown(void);
++int fcgi_is_fastcgi(void);
++void fcgi_set_is_fastcgi(int);
++void fcgi_set_in_shutdown(int);
++void fcgi_set_allowed_clients(char *);
++int fcgi_in_shutdown(void);
++int fcgi_listen(const char *path, int backlog);
++void fcgi_init_request(fcgi_request *req, int listen_socket);
++int fcgi_accept_request(fcgi_request *req);
++int fcgi_finish_request(fcgi_request *req);
++
++char* fcgi_getenv(fcgi_request *req, const char* var, int var_len);
++char* fcgi_putenv(fcgi_request *req, char* var, int var_len, char* val);
++
++int fcgi_read(fcgi_request *req, char *str, int len);
++
++int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len);
++int fcgi_flush(fcgi_request *req, int close);
++
++void fcgi_close(fcgi_request *req, int force, int destroy);
++
++#ifdef PHP_WIN32
++void fcgi_impersonate(void);
++#endif
++
++void fcgi_set_mgmt_var(char * name, size_t name_len, const char * value, size_t value_len);
++void fcgi_free_mgmt_var_cb(void * ptr);
++
++/*
++ * Local variables:
++ * tab-width: 4
++ * c-basic-offset: 4
++ * End:
++ * vim600: sw=4 ts=4 fdm=marker
++ * vim<600: sw=4 ts=4
++ */
+diff -Naur php-src-vanilla/sapi/fpm/cgi/php_getopt.h php-src/sapi/fpm/cgi/php_getopt.h
+--- php-src-vanilla/sapi/fpm/cgi/php_getopt.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/cgi/php_getopt.h 2009-10-18 21:05:39.302497288 +0100
+@@ -0,0 +1,39 @@
++/*
++ +----------------------------------------------------------------------+
++ | PHP Version 5 |
++ +----------------------------------------------------------------------+
++ | Copyright (c) 1997-2008 The PHP Group |
++ +----------------------------------------------------------------------+
++ | This source file is subject to version 3.01 of the PHP license, |
++ | that is bundled with this package in the file LICENSE, and is |
++ | available through the world-wide-web at the following url: |
++ | http://www.php.net/license/3_01.txt |
++ | If you did not receive a copy of the PHP license and are unable to |
++ | obtain it through the world-wide-web, please send a note to |
++ | license@php.net so we can mail you a copy immediately. |
++ +----------------------------------------------------------------------+
++ | Author: Marcus Boerger <helly@php.net> |
++ +----------------------------------------------------------------------+
++*/
++
++/* $Id$ */
++
++#include <php.h>
++
++#ifdef NETWARE
++/*
++As NetWare LibC has optind and optarg macros defined in unistd.h our local variables were getting mistakenly preprocessed so undeffing optind and optarg
++*/
++#undef optarg
++#undef optind
++#endif
++/* Define structure for one recognized option (both single char and long name).
++ * If short_open is '-' this is the last option.
++ */
++typedef struct _opt_struct {
++ const char opt_char;
++ const int need_param;
++ const char * opt_name;
++} opt_struct;
++
++int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err);
+diff -Naur php-src-vanilla/sapi/fpm/conf/init.d.php-fpm.in php-src/sapi/fpm/conf/init.d.php-fpm.in
+--- php-src-vanilla/sapi/fpm/conf/init.d.php-fpm.in 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/conf/init.d.php-fpm.in 2009-10-18 21:05:39.298456068 +0100
+@@ -0,0 +1,135 @@
++#! /bin/sh
++
++### BEGIN INIT INFO
++# Provides: @php_fpm_bin@
++# Required-Start: $all
++# Required-Stop: $all
++# Default-Start: 2 3 4 5
++# Default-Stop: 0 1 6
++# Short-Description: starts @php_fpm_bin@
++# Description: starts the PHP FastCGI Process Manager daemon
++### END INIT INFO
++
++php_fpm_BIN=@php_fpm_bin_path@
++php_fpm_CONF=@php_fpm_conf_path@
++php_fpm_PID=@php_fpm_pid_path@
++
++
++php_opts="--fpm-config $php_fpm_CONF"
++
++
++wait_for_pid () {
++ try=0
++
++ while test $try -lt 35 ; do
++
++ case "$1" in
++ 'created')
++ if [ -f "$2" ] ; then
++ try=''
++ break
++ fi
++ ;;
++
++ 'removed')
++ if [ ! -f "$2" ] ; then
++ try=''
++ break
++ fi
++ ;;
++ esac
++
++ echo -n .
++ try=`expr $try + 1`
++ sleep 1
++
++ done
++
++}
++
++case "$1" in
++ start)
++ echo -n "Starting @php_fpm_bin@ "
++
++ $php_fpm_BIN $php_opts
++
++ if [ "$?" != 0 ] ; then
++ echo " failed"
++ exit 1
++ fi
++
++ wait_for_pid created $php_fpm_PID
++
++ if [ -n "$try" ] ; then
++ echo " failed"
++ exit 1
++ else
++ echo " done"
++ fi
++ ;;
++
++ stop)
++ echo -n "Gracefully shutting down @php_fpm_bin@ "
+
-+ int main()
-+ {
-+ long v1 = (unsigned int) -1; /* copy will fail if sizeof(long) == 8 and we've got "int ptrace()" */
-+ long v2;
-+ pid_t child;
-+ int status;
++ if [ ! -r $php_fpm_PID ] ; then
++ echo "warning, no pid file found - php-fpm is not running ?"
++ exit 1
++ fi
+
-+ if ( (child = fork()) ) { /* parent */
-+ int ret = 0;
++ kill -QUIT `cat $php_fpm_PID`
+
-+ if (0 > ptrace(PTRACE_ATTACH, child, 0, 0)) {
-+ return 1;
-+ }
++ wait_for_pid removed $php_fpm_PID
+
-+ waitpid(child, &status, 0);
++ if [ -n "$try" ] ; then
++ echo " failed. Use force-exit"
++ exit 1
++ else
++ echo " done"
++ fi
++ ;;
+
-+ #ifdef PT_IO
-+ struct ptrace_io_desc ptio = {
-+ .piod_op = PIOD_READ_D,
-+ .piod_offs = &v1,
-+ .piod_addr = &v2,
-+ .piod_len = sizeof(v1)
-+ };
++ force-quit)
++ echo -n "Terminating @php_fpm_bin@ "
+
-+ if (0 > ptrace(PT_IO, child, (void *) &ptio, 0)) {
-+ ret = 1;
-+ }
-+ #else
-+ errno = 0;
++ if [ ! -r $php_fpm_PID ] ; then
++ echo "warning, no pid file found - php-fpm is not running ?"
++ exit 1
++ fi
+
-+ v2 = ptrace(PTRACE_PEEKDATA, child, (void *) &v1, 0);
++ kill -TERM `cat $php_fpm_PID`
+
-+ if (errno) {
-+ ret = 1;
-+ }
-+ #endif
-+ ptrace(PTRACE_DETACH, child, (void *) 1, 0);
++ wait_for_pid removed $php_fpm_PID
+
-+ kill(child, SIGKILL);
++ if [ -n "$try" ] ; then
++ echo " failed"
++ exit 1
++ else
++ echo " done"
++ fi
++ ;;
+
-+ return ret ? ret : (v1 != v2);
-+ }
-+ else { /* child */
-+ sleep(10);
-+ return 0;
-+ }
-+ }
-+ ], [
-+ AC_MSG_RESULT([yes])
-+ ], [
-+ have_ptrace=no
-+ have_broken_ptrace=yes
-+ AC_MSG_RESULT([no])
-+ ])
-+ fi
++ restart)
++ $0 stop
++ $0 start
++ ;;
+
-+ if test "$have_ptrace" = "yes"; then
-+ AC_DEFINE([HAVE_PTRACE], 1, [do we have ptrace?])
-+ fi
++ reload)
+
-+ have_mach_vm_read=no
++ echo -n "Reload service @php_fpm_bin@ "
+
-+ if test "$have_broken_ptrace" = "yes"; then
-+ AC_MSG_CHECKING([for mach_vm_read])
++ if [ ! -r $php_fpm_PID ] ; then
++ echo "warning, no pid file found - @php_fpm_bin@ is not running ?"
++ exit 1
++ fi
+
-+ AC_TRY_COMPILE([ #include <mach/mach.h>
-+ #include <mach/mach_vm.h>
-+ ], [
-+ 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);
-+ ], [
-+ have_mach_vm_read=yes
-+ AC_MSG_RESULT([yes])
-+ ], [
-+ AC_MSG_RESULT([no])
-+ ])
-+ fi
++ kill -USR2 `cat $php_fpm_PID`
+
-+ if test "$have_mach_vm_read" = "yes"; then
-+ AC_DEFINE([HAVE_MACH_VM_READ], 1, [do we have mach_vm_read?])
-+ fi
++ echo " done"
++ ;;
+
-+ proc_mem_file=""
++ *)
++ echo "Usage: $0 {start|stop|force-quit|restart|reload}"
++ exit 1
++ ;;
+
-+ if test -r /proc/$$/mem ; then
-+ proc_mem_file="mem"
-+ else
-+ if test -r /proc/$$/as ; then
-+ proc_mem_file="as"
-+ fi
-+ fi
++esac
+diff -Naur php-src-vanilla/sapi/fpm/conf/nginx-site-conf.sample.in php-src/sapi/fpm/conf/nginx-site-conf.sample.in
+--- php-src-vanilla/sapi/fpm/conf/nginx-site-conf.sample.in 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/conf/nginx-site-conf.sample.in 2009-10-18 21:05:39.298456068 +0100
+@@ -0,0 +1,46 @@
++# @php_fpm_bin@ - PHP FastCGI Process Manager 'PHP-FPM'
++#
++# nginx-site-conf.sample:
++# Php Site configuration for nginx webserver
++#
++# 1. set server root /path/to/your/website;
++# 2. Rename this file. Copy it to /etc/nginx/sites-available, /etc/nginx/sites-enabled
++# or otherwise ensure that this file is included by the nginx.conf
++# 3. Restart nginx webserver, and @php_fpm_bin@ service.
++#
+
-+ if test -n "$proc_mem_file" ; then
-+ AC_MSG_CHECKING([for proc mem file])
++server {
+
-+ AC_TRY_RUN([
-+ #define _GNU_SOURCE
-+ #define _FILE_OFFSET_BITS 64
-+ #include <stdint.h>
-+ #include <unistd.h>
-+ #include <sys/types.h>
-+ #include <sys/stat.h>
-+ #include <fcntl.h>
-+ #include <stdio.h>
-+ int main()
-+ {
-+ long v1 = (unsigned int) -1, v2 = 0;
-+ char buf[128];
-+ int fd;
-+ sprintf(buf, "/proc/%d/$proc_mem_file", getpid());
-+ fd = open(buf, O_RDONLY);
-+ if (0 > fd) {
-+ return 1;
-+ }
-+ if (sizeof(long) != pread(fd, &v2, sizeof(long), (uintptr_t) &v1)) {
-+ close(fd);
-+ return 1;
-+ }
-+ close(fd);
-+ return v1 != v2;
-+ }
-+ ], [
-+ AC_MSG_RESULT([$proc_mem_file])
-+ ], [
-+ proc_mem_file=""
-+ AC_MSG_RESULT([no])
-+ ])
-+ fi
++ root /var/www/nginx-site;
+
-+ if test -n "$proc_mem_file"; then
-+ AC_DEFINE_UNQUOTED([PROC_MEM_FILE], "$proc_mem_file", [/proc/pid/mem interface])
-+ fi
++ server_name localhost;
++ listen 80;
+
-+ FPM_TRACE_SRCS=""
++ access_log /var/log/nginx/localhost.access.log;
+
-+ if test "$have_ptrace" = "yes"; then
-+ FPM_TRACE_SRCS="fpm_trace_ptrace.c"
-+ elif test -n "$proc_mem_file"; then
-+ FPM_TRACE_SRCS="fpm_trace_pread.c"
-+ elif test "$have_mach_vm_read" = "yes" ; then
-+ FPM_TRACE_SRCS="fpm_trace_mach.c"
-+ fi
++ location / {
++ index index.html index.htm;
++ }
+
-+ if test -n "$FPM_TRACE_SRCS"; then
-+ FPM_TRACE_SRCS="fpm_trace.c $FPM_TRACE_SRCS"
-+ fi
++ #error_page 404 /404.html;
+
-+])
++ # redirect server error pages to the static page /50x.html
++ #
++ error_page 500 502 503 504 /50x.html;
++ location = /50x.html {
++ root /var/www/nginx-default;
++ }
+
-+AC_DEFUN([AC_FPM_PRCTL],
-+[
-+ AC_MSG_CHECKING([for prctl])
++ # pass the *.php scripts to @php_fpm_bin@ listening on tcp port @php_fpm_port@
++ #
++ location ~ \.php$ {
+
-+ AC_TRY_COMPILE([ #include <sys/prctl.h> ], [prctl(0, 0, 0, 0, 0);], [
-+ AC_DEFINE([HAVE_PRCTL], 1, [do we have prctl?])
-+ AC_MSG_RESULT([yes])
-+ ], [
-+ AC_MSG_RESULT([no])
-+ ])
-+])
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/conf/php-fpm.conf.in 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/conf/php-fpm.conf.in 2008-05-23 00:53:15.000000000 +0400
-@@ -0,0 +1,156 @@
++ fastcgi_pass 127.0.0.1:@php_fpm_port@;
++ fastcgi_index index.php;
++
++ include fastcgi_params;
++ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
++ fastcgi_param SERVER_NAME $http_host;
++ fastcgi_ignore_client_abort on;
++ }
++}
+diff -Naur php-src-vanilla/sapi/fpm/conf/php-fpm.conf.in php-src/sapi/fpm/conf/php-fpm.conf.in
+--- php-src-vanilla/sapi/fpm/conf/php-fpm.conf.in 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/conf/php-fpm.conf.in 2009-10-18 21:05:39.298456068 +0100
+@@ -0,0 +1,159 @@
+<?xml version="1.0" ?>
+<configuration>
+
+
+ Address to accept fastcgi requests on.
+ Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
-+ <value name="listen_address">127.0.0.1:9000</value>
++ <value name="listen_address">127.0.0.1:@php_fpm_port@</value>
+
+ <value name="listen_options">
+
+ Set permissions for unix socket, if one used.
+ In Linux read/write permissions must be set in order to allow connections from web server.
+ Many BSD-derrived systems allow connections regardless of permissions.
-+ <value name="owner"></value>
-+ <value name="group"></value>
++ <value name="owner">@php_fpm_user@</value>
++ <value name="group">@php_fpm_group@</value>
+ <value name="mode">0666</value>
+ </value>
+
+ Additional php.ini defines, specific to this pool of workers.
++ These settings overwrite the values previously defined in the php.ini.
+ <value name="php_defines">
-+ <!-- <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> -->
-+ <!-- <value name="display_errors">0</value> -->
++ <!-- <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> -->
++ <!-- <value name="display_errors">0</value> -->
++ <!-- <value name="error_log">/var/log/php-error.log</value> -->
++ <!-- <value name="log_errors">true</value> -->
+ </value>
+
+ Unix user of processes
-+ <!-- <value name="user">nobody</value> -->
++ <value name="user">@php_fpm_user@</value>
+
+ Unix group of processes
-+ <!-- <value name="group">@php_fpm_group@</value> -->
++ <value name="group">@php_fpm_group@</value>
+
+ Process manager settings
+ <value name="pm">
+ <value name="request_slowlog_timeout">0s</value>
+
+ The log file for slow requests
-+ <value name="slowlog">logs/slow.log</value>
++ <value name="slowlog">@php_fpm_log_path@.slow</value>
+
+ Set open file desc rlimit
+ <value name="rlimit_files">1024</value>
+ </workers>
+
+</configuration>
---- php-5.2.6.fpm/sapi/cgi/fpm/config.m4 2008-09-19 05:22:37.000000000 +0400
-+++ php-5.2.6.fpm/sapi/cgi/fpm/config.m4 2008-09-19 05:22:37.000000000 +0400
-@@ -0,0 +1,118 @@
-+
-+FPM_VERSION="0.5.9"
-+
-+PHP_ARG_WITH(fpm-conf, for php-fpm config file path,
-+[ --with-fpm-conf=PATH Set the path for php-fpm configuration file [PREFIX/etc/php-fpm.conf]], \$prefix/etc/php-fpm.conf, no)
-+
-+PHP_ARG_WITH(fpm-log, for php-fpm log file path,
-+[ --with-fpm-log=PATH Set the path for php-fpm log file [PREFIX/logs/php-fpm.log]], \$prefix/logs/php-fpm.log, no)
-+
-+PHP_ARG_WITH(fpm-pid, for php-fpm pid file path,
-+[ --with-fpm-pid=PATH Set the path for php-fpm pid file [PREFIX/logs/php-fpm.pid]], \$prefix/logs/php-fpm.pid, no)
-+
-+dnl AC_FPM_LIBEVENT
-+AC_FPM_LIBXML
-+AC_FPM_PRCTL
-+AC_FPM_CLOCK
-+AC_FPM_TRACE
-+dnl AC_FPM_JUDY
-+
-+LIBEVENT_CFLAGS=""
-+LIBEVENT_LIBS="-levent"
-+
-+SAPI_EXTRA_DEPS="$LIBEVENT_LIBS"
-+
-+FPM_SOURCES="fpm.c \
-+ fpm_conf.c \
-+ fpm_signals.c \
-+ fpm_children.c \
-+ fpm_worker_pool.c \
-+ fpm_unix.c \
-+ fpm_cleanup.c \
-+ fpm_sockets.c \
-+ fpm_stdio.c \
-+ fpm_env.c \
-+ fpm_events.c \
-+ fpm_php.c \
-+ fpm_php_trace.c \
-+ $FPM_TRACE_SRCS \
-+ fpm_process_ctl.c \
-+ fpm_request.c \
-+ fpm_clock.c \
-+ fpm_shm.c \
-+ fpm_shm_slots.c \
-+ xml_config.c \
-+ zlog.c"
-+
-+FPM_CFLAGS="$LIBEVENT_CFLAGS $LIBXML_CFLAGS $JUDY_CFLAGS"
-+
-+dnl FPM_CFLAGS="$FPM_CFLAGS -DJUDYERROR_NOTEST" # for Judy
-+FPM_CFLAGS="$FPM_CFLAGS -I$abs_srcdir/sapi/cgi" # for fastcgi.h
-+
-+if test "$ICC" = "yes" ; then
-+ FPM_ADD_CFLAGS="-Wall -wd279,310,869,810,981"
-+elif test "$GCC" = "yes" ; then
-+ FPM_ADD_CFLAGS="-Wall -Wpointer-arith -Wno-unused-parameter -Wunused-variable -Wunused-value -fno-strict-aliasing"
-+fi
-+
-+if test -n "$FPM_WERROR" ; then
-+ FPM_ADD_CFLAGS="$FPM_ADD_CFLAGS -Werror"
-+fi
-+
-+FPM_CFLAGS="$FPM_ADD_CFLAGS $FPM_CFLAGS"
-+
-+PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/sapi/cgi/fpm/Makefile.frag)
+diff -Naur php-src-vanilla/sapi/fpm/config.m4 php-src/sapi/fpm/config.m4
+--- php-src-vanilla/sapi/fpm/config.m4 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/config.m4 2009-10-18 21:05:39.302497288 +0100
+@@ -0,0 +1,26 @@
++dnl
++dnl $Id$
++dnl
+
-+PHP_ADD_SOURCES(sapi/cgi/fpm, $FPM_SOURCES, $FPM_CFLAGS, sapi)
++PHP_ARG_WITH(fpm,,
++[ --with-fpm Build PHP FastCGI - FPM executable], no)
+
-+PHP_ADD_BUILD_DIR(sapi/cgi/fpm)
++if test "$PHP_FPM" != "no"; then
+
-+install_fpm="install-fpm"
-+
-+SAPI_EXTRA_LIBS="$LIBEVENT_LIBS $LIBXML_LIBS $JUDY_LIBS"
-+
-+
-+if test "$prefix" = "NONE" ; then
-+ fpm_prefix=/usr/local
-+else
-+ fpm_prefix="$prefix"
-+fi
-+
-+if test "$PHP_FPM_CONF" = "\$prefix/etc/php-fpm.conf" ; then
-+ php_fpm_conf_path="$fpm_prefix/etc/php-fpm.conf"
-+else
-+ php_fpm_conf_path="$PHP_FPM_CONF"
-+fi
++ PHP_CONFIGURE_PART(Configuring fpm)
++
++ sinclude(sapi/fpm/ac/fpm_libevent.m4)
++ AC_LIB_EVENT([1.4.3],[1.4.11])
+
-+if test "$PHP_FPM_LOG" = "\$prefix/logs/php-fpm.log" ; then
-+ php_fpm_log_path="$fpm_prefix/logs/php-fpm.log"
-+else
-+ php_fpm_log_path="$PHP_FPM_LOG"
-+fi
++ sinclude(sapi/fpm/ac/fpm_checks.m4)
++ AC_FPM_CHECKS
+
-+if test "$PHP_FPM_PID" = "\$prefix/logs/php-fpm.pid" ; then
-+ php_fpm_pid_path="$fpm_prefix/logs/php-fpm.pid"
-+else
-+ php_fpm_pid_path="$PHP_FPM_PID"
-+fi
++ sinclude(sapi/fpm/ac/fpm_conf.m4)
++ fpm_version="0.6"
++ AC_FPM_CONF
+
++ sinclude(sapi/fpm/ac/fpm_build.m4)
++ AC_FPM_BUILD_SAPI
+
-+if grep nobody /etc/group >/dev/null 2>&1; then
-+ php_fpm_group=nobody
-+else
-+ if grep nogroup /etc/group >/dev/null 2>&1; then
-+ php_fpm_group=nogroup
-+ else
-+ php_fpm_group=nobody
-+ fi
++ AC_MSG_RESULT()
+fi
-+
-+PHP_SUBST_OLD(php_fpm_conf_path)
-+PHP_SUBST_OLD(php_fpm_log_path)
-+PHP_SUBST_OLD(php_fpm_pid_path)
-+PHP_SUBST_OLD(php_fpm_group)
-+PHP_SUBST_OLD(FPM_VERSION)
-+
-+PHP_OUTPUT(sapi/cgi/fpm/fpm_autoconf.h)
-+PHP_OUTPUT(sapi/cgi/fpm/php-fpm.conf:sapi/cgi/fpm/conf/php-fpm.conf.in)
-+PHP_OUTPUT(sapi/cgi/fpm/php-fpm:sapi/cgi/fpm/init.d/php-fpm.in)
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_arrays.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_arrays.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_arrays.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_arrays.h 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_arrays.h php-src/sapi/fpm/fpm/fpm_arrays.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_arrays.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_arrays.h 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,110 @@
+
+ /* $Id$ */
+}
+
+#endif
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_atomic.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_atomic.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_atomic.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_atomic.h 2008-09-19 03:34:11.000000000 +0400
-@@ -0,0 +1,85 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_atomic.h php-src/sapi/fpm/fpm/fpm_atomic.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_atomic.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_atomic.h 2009-10-18 21:05:39.308376784 +0100
+@@ -0,0 +1,139 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+#ifndef FPM_ATOMIC_H
+#define FPM_ATOMIC_H 1
+
++#if HAVE_INTTYPES_H
++#include <inttypes.h>
++#else
+#include <stdint.h>
++#endif
+#include <sched.h>
+
+#if ( __i386__ || __i386 )
+
+static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
+{
-+ __asm__ volatile ( "lock;" "xaddq %0, %1;" :
-+ "+r" (add) : "m" (*value) : "memory");
++ __asm__ volatile ( "lock;" "xaddq %0, %1;" :
++ "+r" (add) : "m" (*value) : "memory");
++
++ return add;
++}
++
++static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
++{
++ unsigned char res;
++
++ __asm__ volatile ( "lock;" "cmpxchgq %3, %1;" "sete %0;" :
++ "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
++
++ return res;
++}
++
++#if (__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 2))
++
++#elif ( __arm__ || __arm ) /* W-Mark Kubacki */
++
++#if (__arch64__ || __arch64)
++typedef int64_t atomic_int_t;
++typedef uint64_t atomic_uint_t;
++#else
++typedef int32_t atomic_int_t;
++typedef uint32_t atomic_uint_t;
++#endif
++
++#define atomic_cmp_set(a,b,c) __sync_bool_compare_and_swap(a,b,c)
++
++#endif /* defined (__GNUC__) &&... */
++
++#elif ( __sparc__ || __sparc ) /* Marcin Ochab */
++
++#if (__arch64__ || __arch64)
++typedef uint64_t atomic_uint_t;
++typedef volatile atomic_uint_t atomic_t;
++
++static inline int atomic_cas_64(atomic_t *lock, atomic_uint_t old, atomic_uint_t new)
++{
++ __asm__ __volatile__("casx [%2], %3, %0 " : "=&r"(new) : "0"(new), "r"(lock), "r"(old): "memory");
++
++ return new;
++}
++
++static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
++{
++ return (atomic_cas_64(lock, old, set)==old);
++}
++#else
++typedef uint32_t atomic_uint_t;
++typedef volatile atomic_uint_t atomic_t;
++
++static inline int atomic_cas_32(atomic_t *lock, atomic_uint_t old, atomic_uint_t new)
++{
++ __asm__ __volatile__("cas [%2], %3, %0 " : "=&r"(new) : "0"(new), "r"(lock), "r"(old): "memory");
+
-+ return add;
++ return new;
+}
+
+static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
+{
-+ unsigned char res;
-+
-+ __asm__ volatile ( "lock;" "cmpxchgq %3, %1;" "sete %0;" :
-+ "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
-+
-+ return res;
++ return (atomic_cas_32(lock, old, set)==old);
+}
++#endif
+
+#else
+
-+#error unsupported processor. please write a patch and send it to me
++#error unsupported architecture. please write a patch and send it in
+
+#endif
+
+
+#endif
+
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_autoconf.h.in 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_autoconf.h.in 2008-05-24 21:38:47.000000000 +0400
-@@ -0,0 +1,9 @@
-+
-+ /* $Id$ */
-+ /* (c) 2007,2008 Andrei Nigmatulin */
-+
-+#define PHP_FPM_VERSION "@FPM_VERSION@"
-+#define PHP_FPM_CONF_PATH "@php_fpm_conf_path@"
-+#define PHP_FPM_LOG_PATH "@php_fpm_log_path@"
-+#define PHP_FPM_PID_PATH "@php_fpm_pid_path@"
-+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm.c php-5.2.6.fpm/sapi/cgi/fpm/fpm.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm.c 2008-07-20 20:38:31.000000000 +0400
-@@ -0,0 +1,84 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm.c php-src/sapi/fpm/fpm/fpm.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm.c 2009-10-18 21:05:39.308376784 +0100
+@@ -0,0 +1,82 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+#include "fpm_stdio.h"
+#include "zlog.h"
+
-+int fpm;
-+
+struct fpm_globals_s fpm_globals;
+
+int fpm_init(int argc, char **argv, char *config)
+ return fpm_globals.listening_socket;
+}
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_children.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_children.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_children.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_children.c 2008-08-26 19:09:15.000000000 +0400
-@@ -0,0 +1,383 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_children.c php-src/sapi/fpm/fpm/fpm_children.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_children.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_children.c 2009-10-18 21:05:39.308376784 +0100
+@@ -0,0 +1,387 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+
+ ret = malloc(sizeof(struct fpm_child_s));
+
-+ if (!ret) return 0;
++ if (!ret) { return 0; }
+
+ memset(ret, 0, sizeof(*ret));
+
+ ++fpm_globals.running_children;
+
+ child->next = wp->children;
-+ if (child->next) child->next->prev = child;
++ if (child->next) { child->next->prev = child; }
+ child->prev = 0;
+ wp->children = child;
+}
+ --child->wp->running_children;
+ --fpm_globals.running_children;
+
-+ if (child->prev) child->prev->next = child->next;
-+ else child->wp->children = child->next;
-+ if (child->next) child->next->prev = child->prev;
++ if (child->prev) { child->prev->next = child->next; }
++ else { child->wp->children = child->next; }
++ if (child->next) { child->next->prev = child->prev; }
+
+}
+
+ }
+ }
+
-+ if (child) break;
++ if (child) {
++ break;
++ }
+ }
+
+ if (!child) {
+
+ last_faults[fault++] = now;
+
-+ if (fault == fpm_global_options.emergency_restart_threshold) {
++ if (fault == fpm_global_config.emergency_restart_threshold) {
+ fault = 0;
+ }
+
-+ for (i = 0; i < fpm_global_options.emergency_restart_threshold; i++) {
-+ if (now - last_faults[i] > fpm_global_options.emergency_restart_interval) {
++ for (i = 0; i < fpm_global_config.emergency_restart_threshold; i++) {
++ if (now - last_faults[i] > fpm_global_config.emergency_restart_interval) {
+ restart_condition = 0;
+ break;
+ }
+ if (restart_condition) {
+
+ zlog(ZLOG_STUFF, ZLOG_WARNING, "failed processes threshold (%d in %d sec) is reached, initiating reload",
-+ fpm_global_options.emergency_restart_threshold, fpm_global_options.emergency_restart_interval);
++ fpm_global_config.emergency_restart_threshold, fpm_global_config.emergency_restart_interval);
+
+ fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
+ }
+
+int fpm_children_init_main()
+{
-+ if (fpm_global_options.emergency_restart_threshold &&
-+ fpm_global_options.emergency_restart_interval) {
++ if (fpm_global_config.emergency_restart_threshold &&
++ fpm_global_config.emergency_restart_interval) {
+
-+ last_faults = malloc(sizeof(time_t) * fpm_global_options.emergency_restart_threshold);
++ last_faults = malloc(sizeof(time_t) * fpm_global_config.emergency_restart_threshold);
+
+ if (!last_faults) {
+ return -1;
+ }
+
-+ memset(last_faults, 0, sizeof(time_t) * fpm_global_options.emergency_restart_threshold);
++ memset(last_faults, 0, sizeof(time_t) * fpm_global_config.emergency_restart_threshold);
+ }
+
-+ fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_children_cleanup, 0);
++ if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_children_cleanup, 0)) {
++ return -1;
++ }
+
+ return 0;
+}
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_children.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_children.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_children.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_children.h 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_children.h php-src/sapi/fpm/fpm/fpm_children.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_children.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_children.h 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,33 @@
+
+ /* $Id$ */
+};
+
+#endif
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_cleanup.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_cleanup.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_cleanup.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_cleanup.c 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_cleanup.c php-src/sapi/fpm/fpm/fpm_cleanup.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_cleanup.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_cleanup.c 2009-10-18 21:05:39.310440424 +0100
@@ -0,0 +1,51 @@
+
+ /* $Id$ */
+ fpm_array_free(&cleanups);
+}
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_cleanup.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_cleanup.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_cleanup.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_cleanup.h 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_cleanup.h php-src/sapi/fpm/fpm/fpm_cleanup.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_cleanup.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_cleanup.h 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,21 @@
+
+ /* $Id$ */
+
+#endif
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_clock.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_clock.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_clock.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_clock.c 2008-09-19 03:19:59.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_clock.c php-src/sapi/fpm/fpm/fpm_clock.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_clock.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_clock.c 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,115 @@
+
+ /* $Id$ */
+}
+
+#endif
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_clock.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_clock.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_clock.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_clock.h 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_clock.h php-src/sapi/fpm/fpm/fpm_clock.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_clock.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_clock.h 2009-10-18 21:05:39.310440424 +0100
@@ -0,0 +1,13 @@
+
+ /* $Id$ */
+int fpm_clock_get(struct timeval *tv);
+
+#endif
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_conf.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_conf.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_conf.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_conf.c 2008-09-19 04:54:15.000000000 +0400
-@@ -0,0 +1,530 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_conf.c php-src/sapi/fpm/fpm/fpm_conf.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_conf.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_conf.c 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,537 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+#include <string.h>
+#include <stdlib.h>
+#include <stddef.h>
++#if HAVE_INTTYPES_H
++#include <inttypes.h>
++#else
+#include <stdint.h>
++#endif
++
+#include <stdio.h>
+#include <unistd.h>
+
+#include "zlog.h"
+
+
-+struct fpm_options_s fpm_global_options;
++struct fpm_global_config_s fpm_global_config;
+
-+static void *fpm_global_options_ptr()
++static void *fpm_global_config_ptr()
+{
-+ return &fpm_global_options;
++ return &fpm_global_config;
+}
+
+static char *fpm_conf_set_log_level(void **conf, char *name, void *vv, intptr_t offset)
+}
+
+static struct xml_conf_section xml_section_fpm_global_options = {
-+ .conf = &fpm_global_options_ptr,
++ .conf = &fpm_global_config_ptr,
+ .path = "/configuration/global_options",
+ .parsers = (struct xml_value_parser []) {
-+ { XML_CONF_SCALAR, "emergency_restart_threshold", &xml_conf_set_slot_integer, offsetof(struct fpm_options_s, emergency_restart_threshold) },
-+ { XML_CONF_SCALAR, "emergency_restart_interval", &xml_conf_set_slot_time, offsetof(struct fpm_options_s, emergency_restart_interval) },
-+ { XML_CONF_SCALAR, "process_control_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_options_s, process_control_timeout) },
-+ { XML_CONF_SCALAR, "daemonize", &xml_conf_set_slot_boolean, offsetof(struct fpm_options_s, daemonize) },
-+ { XML_CONF_SCALAR, "pid_file", &xml_conf_set_slot_string, offsetof(struct fpm_options_s, pid_file) },
-+ { XML_CONF_SCALAR, "error_log", &xml_conf_set_slot_string, offsetof(struct fpm_options_s, error_log) },
++ { XML_CONF_SCALAR, "emergency_restart_threshold", &xml_conf_set_slot_integer, offsetof(struct fpm_global_config_s, emergency_restart_threshold) },
++ { XML_CONF_SCALAR, "emergency_restart_interval", &xml_conf_set_slot_time, offsetof(struct fpm_global_config_s, emergency_restart_interval) },
++ { XML_CONF_SCALAR, "process_control_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_global_config_s, process_control_timeout) },
++ { XML_CONF_SCALAR, "daemonize", &xml_conf_set_slot_boolean, offsetof(struct fpm_global_config_s, daemonize) },
++ { XML_CONF_SCALAR, "pid_file", &xml_conf_set_slot_string, offsetof(struct fpm_global_config_s, pid_file) },
++ { XML_CONF_SCALAR, "error_log", &xml_conf_set_slot_string, offsetof(struct fpm_global_config_s, error_log) },
+ { XML_CONF_SCALAR, "log_level", &fpm_conf_set_log_level, 0 },
+ { 0, 0, 0, 0 }
+ }
+
+ error = xml_conf_set_slot_integer(&subconf, name, vv, 0);
+
-+ if (error) return error;
++ if (error) { return error; }
+
-+ if (int_value < 0) return "invalid value for 'rlimit_core'";
++ if (int_value < 0) { return "invalid value for 'rlimit_core'"; }
+
+ c->rlimit_core = int_value;
+ }
+
+ error = xml_conf_set_slot_boolean(&subconf, name, vv, 0);
+
-+ if (error) return error;
++ if (error) { return error; }
+
+ c->catch_workers_output = int_value;
+
+
+ wp = fpm_worker_pool_alloc();
+
-+ if (!wp) return 0;
++ if (!wp) { return 0; }
+
+ wp->config = malloc(sizeof(struct fpm_worker_pool_config_s));
+
-+ if (!wp->config) return 0;
++ if (!wp->config) { return 0; }
+
+ memset(wp->config, 0, sizeof(struct fpm_worker_pool_config_s));
+
-+ if (current_wp) current_wp->next = wp;
++ if (current_wp) { current_wp->next = wp; }
+
+ current_wp = wp;
+
+
+ full_path = malloc(sizeof(PHP_PREFIX) + strlen(*path) + 1);
+
-+ if (!full_path) return -1;
++ if (!full_path) { return -1; }
+
+ sprintf(full_path, "%s/%s", PHP_PREFIX, *path);
+
+
+int fpm_conf_unlink_pid()
+{
-+ if (fpm_global_options.pid_file) {
++ if (fpm_global_config.pid_file) {
+
-+ if (0 > unlink(fpm_global_options.pid_file)) {
-+ zlog(ZLOG_STUFF, ZLOG_SYSERROR, "unlink(\"%s\") failed", fpm_global_options.pid_file);
++ if (0 > unlink(fpm_global_config.pid_file)) {
++ zlog(ZLOG_STUFF, ZLOG_SYSERROR, "unlink(\"%s\") failed", fpm_global_config.pid_file);
+ return -1;
+ }
+
+{
+ int fd;
+
-+ if (fpm_global_options.pid_file) {
++ if (fpm_global_config.pid_file) {
+ char buf[64];
+ int len;
+
-+ unlink(fpm_global_options.pid_file);
++ unlink(fpm_global_config.pid_file);
+
-+ fd = creat(fpm_global_options.pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
++ fd = creat(fpm_global_config.pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+
+ if (fd < 0) {
-+ zlog(ZLOG_STUFF, ZLOG_SYSERROR, "creat(\"%s\") failed", fpm_global_options.pid_file);
++ zlog(ZLOG_STUFF, ZLOG_SYSERROR, "creat(\"%s\") failed", fpm_global_config.pid_file);
+ return -1;
+ }
+
+
+static int fpm_conf_post_process()
+{
-+ if (fpm_global_options.pid_file) {
-+ fpm_evaluate_full_path(&fpm_global_options.pid_file);
++ if (fpm_global_config.pid_file) {
++ fpm_evaluate_full_path(&fpm_global_config.pid_file);
+ }
+
-+ if (!fpm_global_options.error_log) {
-+ fpm_global_options.error_log = strdup(PHP_FPM_LOG_PATH);
++ if (!fpm_global_config.error_log) {
++ fpm_global_config.error_log = strdup(PHP_FPM_LOG_PATH);
+ }
+
-+ fpm_evaluate_full_path(&fpm_global_options.error_log);
++ fpm_evaluate_full_path(&fpm_global_config.error_log);
+
+ if (0 > fpm_stdio_open_error_log(0)) {
+ return -1;
+
+static void fpm_conf_cleanup(int which, void *arg)
+{
-+ free(fpm_global_options.pid_file);
-+ free(fpm_global_options.error_log);
-+ fpm_global_options.pid_file = 0;
-+ fpm_global_options.error_log = 0;
++ free(fpm_global_config.pid_file);
++ free(fpm_global_config.error_log);
++ fpm_global_config.pid_file = 0;
++ fpm_global_config.error_log = 0;
+}
+
+int fpm_conf_init_main()
+
+ xml_conf_clean();
+
-+ fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_conf_cleanup, 0);
++ if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_conf_cleanup, 0)) {
++ return -1;
++ }
+
+ return 0;
+}
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_conf.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_conf.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_conf.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_conf.h 2008-08-26 19:09:15.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_conf.h php-src/sapi/fpm/fpm/fpm_conf.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_conf.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_conf.h 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,73 @@
+
+ /* $Id$ */
+ char *value;
+};
+
-+struct fpm_options_s {
++struct fpm_global_config_s {
+ int emergency_restart_threshold;
+ int emergency_restart_interval;
+ int process_control_timeout;
+ char *error_log;
+};
+
-+extern struct fpm_options_s fpm_global_options;
++extern struct fpm_global_config_s fpm_global_config;
+
+struct fpm_pm_s {
+ int style;
+
+#endif
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_config.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_config.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_config.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_config.h 2008-05-25 04:30:43.000000000 +0400
-@@ -0,0 +1,39 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_config.h php-src/sapi/fpm/fpm/fpm_config.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_config.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_config.h 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,40 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+
-+#include "php_config.h"
-+#include "fpm_autoconf.h"
-+
++#include <php_config.h>
++#ifdef FPM_AUTOCONFIG_H
++#include <fpm_autoconfig.h>
++#endif
+
+/* Solaris does not have it */
+#ifndef INADDR_NONE
+#define HAVE_FPM_TRACE 0
+#endif
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_env.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_env.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_env.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_env.c 2008-09-19 03:19:59.000000000 +0400
-@@ -0,0 +1,125 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_env.c php-src/sapi/fpm/fpm/fpm_env.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_env.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_env.c 2009-10-18 21:05:39.308376784 +0100
+@@ -0,0 +1,175 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+#include "zlog.h"
+
+#ifndef HAVE_SETENV
++#ifdef (__sparc__ || __sparc)
++int setenv(name, value, clobber)
++char *name;
++char *value;
++int clobber;
++{
++ char *malloc();
++ char *getenv();
++ char *cp;
++
++ if (clobber == 0 && getenv(name) != 0)
++ { return (0); }
++ if ((cp = malloc(strlen(name) + strlen(value) + 2)) == 0)
++ { return (1); }
++ sprintf(cp, "%s=%s", name, value);
++ return (putenv(cp));
++}
++#else
+int setenv(char *name, char *value, int overwrite)
+{
+ int name_len = strlen(name);
+ return putenv(var);
+}
+#endif
++#endif
+
+#ifndef HAVE_CLEARENV
+void clearenv()
+
+ s = strdup(*envp);
+
-+ if (eq) s[eq - *envp] = '\0';
++ if (eq) { s[eq - *envp] = '\0'; }
+
+ unsetenv(s);
+ free(s);
+}
+#endif
+
++#ifndef HAVE_UNSETENV
++void unsetenv(const char *name)
++{
++ if(getenv(name)!=NULL)
++ {
++ int ct=0;
++ int del=0;
++
++ while(environ[ct] != NULL)
++ {
++ if (nvmatch(name, environ[ct]) != 0) del=ct;
++ { ct++; }
++ }
++ /* isn't needed free here?? */
++ environ[del]=environ[ct-1];
++ environ[ct-1]=NULL;
++ }
++}
++static char * nvmatch(s1, s2)
++register char *s1, *s2;
++{
++ while(*s1 == *s2++)
++ {
++ if(*s1++ == '=')
++ { return(s2); }
++ }
++ if(*s1 == '\0' && *(s2-1) == '=')
++ { return(s2); }
++ return(NULL);
++}
++#endif
+
+int fpm_env_init_child(struct fpm_worker_pool_s *wp)
+{
+ if (*kv->value == '$') {
+ char *value = getenv(kv->value + 1);
+
-+ if (!value) value = "";
++ if (!value) { value = ""; }
+
+ free(kv->value);
+ kv->value = strdup(value);
+
+ return 0;
+}
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_env.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_env.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_env.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_env.h 2008-09-19 03:19:59.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_env.h php-src/sapi/fpm/fpm/fpm_env.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_env.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_env.h 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,24 @@
+
+ /* $Id$ */
+
+#endif
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_events.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_events.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_events.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_events.c 2008-08-26 19:09:15.000000000 +0400
-@@ -0,0 +1,133 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_events.c php-src/sapi/fpm/fpm/fpm_events.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_events.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_events.c 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,135 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+
+ zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: using %s", event_get_method());
+
-+ fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_event_cleanup, 0);
++ if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_event_cleanup, 0)) {
++ return -1;
++ }
+
+ return 0;
+}
+
+void fpm_event_exit_loop()
+{
-+ event_loopexit(0);
++ event_loopbreak();
+}
+
+void fpm_event_fire(struct event *ev)
+ (*ev->ev_callback)( (int) ev->ev_fd, (short) ev->ev_res, ev->ev_arg);
+}
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_events.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_events.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_events.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_events.h 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_events.h php-src/sapi/fpm/fpm/fpm_events.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_events.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_events.h 2009-10-18 21:05:39.310440424 +0100
@@ -0,0 +1,16 @@
+
+ /* $Id$ */
+
+
+#endif
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm.h php-5.2.6.fpm/sapi/cgi/fpm/fpm.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm.h 2008-05-24 21:38:47.000000000 +0400
-@@ -0,0 +1,30 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm.h php-src/sapi/fpm/fpm/fpm.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm.h 2009-10-18 21:05:39.308376784 +0100
+@@ -0,0 +1,28 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+
+extern struct fpm_globals_s fpm_globals;
+
-+extern int fpm;
-+
+#endif
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_php.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_php.c 2008-07-20 20:16:28.000000000 +0400
-@@ -0,0 +1,171 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_php.c php-src/sapi/fpm/fpm/fpm_php.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_php.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_php.c 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,189 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+
+#include <stdlib.h>
+#include <string.h>
++#include <stdio.h>
+
+#include "php.h"
+#include "php_main.h"
+#include "php_ini.h"
+#include "ext/standard/dl.h"
+
-+#include "fastcgi.h"
++#include "cgi/fastcgi.h"
+
+#include "fpm.h"
+#include "fpm_php.h"
+#include "fpm_cleanup.h"
+#include "fpm_worker_pool.h"
+
-+static int zend_ini_alter_master(char *name, int name_length, char *new_value, int new_value_length, int stage)
++static int zend_ini_alter_master(char *name, int name_length, char *new_value, int new_value_length, int stage TSRMLS_DC)
+{
+ zend_ini_entry *ini_entry;
+ char *duplicate;
+
+ if (!ini_entry->on_modify
+ || ini_entry->on_modify(ini_entry, duplicate, new_value_length,
-+ ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage) == SUCCESS) {
++ ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage TSRMLS_CC) == SUCCESS) {
+ ini_entry->value = duplicate;
+ ini_entry->value_length = new_value_length;
+ } else {
+ return SUCCESS;
+}
+
-+static void fpm_php_disable(char *value, int (*zend_disable)(char *, uint))
++static void fpm_php_disable(char *value, int (*zend_disable)(char *, uint TSRMLS_DC) TSRMLS_DC)
+{
+ char *s = 0, *e = value;
+
+ case ',':
+ if (s) {
+ *e = '\0';
-+ zend_disable(s, e - s);
++ zend_disable(s, e - s TSRMLS_CC);
+ s = 0;
+ }
+ break;
+ }
+
+ if (s) {
-+ zend_disable(s, e - s);
++ zend_disable(s, e - s TSRMLS_CC);
+ }
+}
+
+static int fpm_php_apply_defines(struct fpm_worker_pool_s *wp)
+{
++ TSRMLS_FETCH();
+ struct key_value_s *kv;
+
+ for (kv = wp->config->php_defines; kv; kv = kv->next) {
+ zval zv;
+
+#if defined(PHP_VERSION_ID) && (PHP_VERSION_ID >= 50300)
-+ php_dl(value, MODULE_PERSISTENT, &zv, 1);
++ php_dl(value, MODULE_PERSISTENT, &zv, 1 TSRMLS_CC);
+#else
+ zval filename;
+ ZVAL_STRINGL(&filename, value, value_len, 0);
+#if (PHP_MAJOR_VERSION >= 5)
-+ php_dl(&filename, MODULE_PERSISTENT, &zv, 1);
++ php_dl(&filename, MODULE_PERSISTENT, &zv, 1 TSRMLS_CC);
+#else
-+ php_dl(&filename, MODULE_PERSISTENT, &zv);
++ php_dl(&filename, MODULE_PERSISTENT, &zv TSRMLS_CC);
+#endif
+#endif
+ continue;
+ }
+
-+ zend_ini_alter_master(name, name_len + 1, value, value_len, PHP_INI_STAGE_ACTIVATE);
++ zend_ini_alter_master(name, name_len + 1, value, value_len, PHP_INI_STAGE_ACTIVATE TSRMLS_CC);
+
+ if (!strcmp(name, "disable_functions") && *value) {
+ char *v = strdup(value);
+#if (PHP_MAJOR_VERSION >= 5)
+ PG(disable_functions) = v;
+#endif
-+ fpm_php_disable(v, zend_disable_function);
++ fpm_php_disable(v, zend_disable_function TSRMLS_CC);
+ }
+ else if (!strcmp(name, "disable_classes") && *value) {
+ char *v = strdup(value);
+#if (PHP_MAJOR_VERSION >= 5)
+ PG(disable_classes) = v;
+#endif
-+ fpm_php_disable(v, zend_disable_class);
++ fpm_php_disable(v, zend_disable_class TSRMLS_CC);
+ }
+ }
+
+ return 0;
+}
+
-+char *fpm_php_script_filename()
++static int fpm_php_set_fcgi_mgmt_vars(struct fpm_worker_pool_s *wp)
++{
++ char max_workers[10 + 1]; /* 4294967295 */
++ int len;
++
++ len = sprintf(max_workers, "%u", (unsigned int) wp->config->pm->max_children);
++
++ fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, max_workers, len);
++ fcgi_set_mgmt_var("FCGI_MAX_REQS", sizeof("FCGI_MAX_REQS")-1, max_workers, len);
++
++ return 0;
++}
++
++char *fpm_php_script_filename(TSRMLS_D)
+{
+ return SG(request_info).path_translated;
+}
+
-+char *fpm_php_request_method()
++char *fpm_php_request_method(TSRMLS_D)
+{
+ return (char *) SG(request_info).request_method;
+}
+
-+size_t fpm_php_content_length()
++size_t fpm_php_content_length(TSRMLS_D)
+{
+ return SG(request_info).content_length;
+}
+
+static void fpm_php_cleanup(int which, void *arg)
+{
-+ php_module_shutdown();
++ TSRMLS_FETCH();
++ php_module_shutdown(TSRMLS_C);
+ sapi_shutdown();
+}
+
+
+int fpm_php_init_main()
+{
-+ fpm_cleanup_add(FPM_CLEANUP_PARENT, fpm_php_cleanup, 0);
++ if (0 > fpm_cleanup_add(FPM_CLEANUP_PARENT, fpm_php_cleanup, 0)) {
++ return -1;
++ }
+
+ return 0;
+}
+
+ return 0;
+}
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_php.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_php.h 2008-05-24 21:38:47.000000000 +0400
-@@ -0,0 +1,20 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_php.h php-src/sapi/fpm/fpm/fpm_php.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_php.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_php.h 2009-10-18 21:05:39.302497288 +0100
+@@ -0,0 +1,22 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+#ifndef FPM_PHP_H
+#define FPM_PHP_H 1
+
-+#include "fpm_worker_pool.h"
++#include <TSRM.h>
+
+#include "build-defs.h" /* for PHP_ defines */
+
++struct fpm_worker_pool_s;
++
+int fpm_php_init_child(struct fpm_worker_pool_s *wp);
-+char *fpm_php_script_filename();
-+char *fpm_php_request_method();
-+size_t fpm_php_content_length();
++char *fpm_php_script_filename(TSRMLS_D);
++char *fpm_php_request_method(TSRMLS_D);
++size_t fpm_php_content_length(TSRMLS_D);
+void fpm_php_soft_quit();
+int fpm_php_init_main();
+
+#endif
+
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php_trace.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_php_trace.c 2008-08-26 19:09:15.000000000 +0400
-@@ -0,0 +1,170 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_php_trace.c php-src/sapi/fpm/fpm/fpm_php_trace.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_php_trace.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_php_trace.c 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,176 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+
+#include <stdio.h>
+#include <stddef.h>
++#if HAVE_INTTYPES_H
++#include <inttypes.h>
++#else
+#include <stdint.h>
++#endif
++
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#endif
+
+
-+static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog)
++static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog TSRMLS_DC)
+{
+ int callers_limit = 20;
+ pid_t pid = child->pid;
+
+void fpm_php_trace(struct fpm_child_s *child)
+{
++ TSRMLS_FETCH();
+ FILE *slowlog;
+
+ zlog(ZLOG_STUFF, ZLOG_NOTICE, "about to trace %d", (int) child->pid);
+ goto done1;
+ }
+
-+ if (0 > fpm_php_trace_dump(child, slowlog)) {
++ if (0 > fpm_php_trace_dump(child, slowlog TSRMLS_CC)) {
+ fprintf(slowlog, "+++ dump failed\n");
+ }
+
+
+#endif
+
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php_trace.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_php_trace.h 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_php_trace.h php-src/sapi/fpm/fpm/fpm_php_trace.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_php_trace.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_php_trace.h 2009-10-18 21:05:39.310440424 +0100
@@ -0,0 +1,13 @@
+
+ /* $Id$ */
+
+#endif
+
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_process_ctl.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_process_ctl.c 2008-07-21 17:13:44.000000000 +0400
-@@ -0,0 +1,352 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_process_ctl.c php-src/sapi/fpm/fpm/fpm_process_ctl.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_process_ctl.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_process_ctl.c 2009-10-18 21:05:39.308376784 +0100
+@@ -0,0 +1,354 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+ fpm_signal_names[signo] ? fpm_signal_names[signo] : "",
+ (int) child->pid, child->wp->config->name);
+
-+ if (res == 0) ++alive_children;
++ if (res == 0) { ++alive_children; }
+ }
+ }
+
+{
+ int sig, timeout;
+
-+ if (!fpm_globals.running_children) fpm_pctl_action_last();
++ if (!fpm_globals.running_children) { fpm_pctl_action_last(); }
+
+ if (fpm_signal_sent == 0) {
+ if (fpm_state == FPM_PCTL_STATE_TERMINATING) {
+ else {
+ sig = SIGQUIT;
+ }
-+ timeout = fpm_global_options.process_control_timeout;
++ timeout = fpm_global_config.process_control_timeout;
+ }
+ else {
+ if (fpm_signal_sent == SIGQUIT) {
+ case FPM_PCTL_STATE_RELOADING :
+
+ /* 'reloading' can be overridden by 'finishing' */
-+ if (new_state == FPM_PCTL_STATE_FINISHING) break;
++ if (new_state == FPM_PCTL_STATE_FINISHING) { break; }
+
+ case FPM_PCTL_STATE_FINISHING :
+
+ /* 'reloading' and 'finishing' can be overridden by 'terminating' */
-+ if (new_state == FPM_PCTL_STATE_TERMINATING) break;
++ if (new_state == FPM_PCTL_STATE_TERMINATING) { break; }
+
+ case FPM_PCTL_STATE_TERMINATING :
+
+
+int fpm_pctl_child_exited()
+{
-+ if (fpm_state == FPM_PCTL_STATE_NORMAL) return 0;
++ if (fpm_state == FPM_PCTL_STATE_NORMAL) { return 0; }
+
+ if (!fpm_globals.running_children) {
+ fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_LAST_CHILD_EXITED);
+
+ saved_argv[i] = 0;
+
-+ fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_pctl_cleanup, 0);
++ if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_pctl_cleanup, 0)) {
++ return -1;
++ }
+
+ return 0;
+}
+ evtimer_add(&heartbeat, &tv);
+}
+
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_process_ctl.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_process_ctl.h 2008-07-21 01:33:10.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_process_ctl.h php-src/sapi/fpm/fpm/fpm_process_ctl.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_process_ctl.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_process_ctl.h 2009-10-18 21:05:39.310440424 +0100
@@ -0,0 +1,39 @@
+
+ /* $Id$ */
+
+#endif
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_request.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_request.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_request.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_request.c 2008-09-01 03:34:36.000000000 +0400
-@@ -0,0 +1,163 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_request.c php-src/sapi/fpm/fpm/fpm_request.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_request.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_request.c 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,164 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+
+void fpm_request_info()
+{
++ TSRMLS_FETCH();
+ struct fpm_shm_slot_s *slot;
-+ char *request_method = fpm_php_request_method();
-+ char *script_filename = fpm_php_script_filename();
++ char *request_method = fpm_php_request_method(TSRMLS_C);
++ char *script_filename = fpm_php_script_filename(TSRMLS_C);
+
+ slot = fpm_shm_slots_acquire(0, 0);
+
+ cpystrn(slot->request_method, request_method, sizeof(slot->request_method));
+ }
+
-+ slot->content_length = fpm_php_content_length();
++ slot->content_length = fpm_php_content_length(TSRMLS_C);
+
+ /* if cgi.fix_pathinfo is set to "1" and script cannot be found (404)
+ the sapi_globals.request_info.path_translated is set to NULL */
+
+}
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_request.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_request.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_request.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_request.h 2008-07-20 05:47:16.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_request.h php-src/sapi/fpm/fpm/fpm_request.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_request.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_request.h 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,27 @@
+
+ /* $Id$ */
+};
+
+#endif
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm.c 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_shm.c php-src/sapi/fpm/fpm/fpm_shm.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_shm.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_shm.c 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,100 @@
+
+ /* $Id$ */
+ }
+
+ shm->next = *head;
-+ if (shm->next) shm->next->prev = shm;
++ if (shm->next) { shm->next->prev = shm; }
+ shm->prev = 0;
+ *head = shm;
+ }
+ return ret;
+}
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm.h 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_shm.h php-src/sapi/fpm/fpm/fpm_shm.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_shm.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_shm.h 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,22 @@
+
+ /* $Id$ */
+
+#endif
+
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm_slots.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm_slots.c 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_shm_slots.c php-src/sapi/fpm/fpm/fpm_shm_slots.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_shm_slots.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_shm_slots.c 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,127 @@
+
+ /* $Id$ */
+ s->lock = 0;
+}
+
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm_slots.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm_slots.h 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_shm_slots.h php-src/sapi/fpm/fpm/fpm_shm_slots.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_shm_slots.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_shm_slots.h 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,43 @@
+
+ /* $Id$ */
+
+#endif
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_signals.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_signals.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_signals.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_signals.c 2008-08-26 19:09:15.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_signals.c php-src/sapi/fpm/fpm/fpm_signals.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_signals.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_signals.c 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,252 @@
+
+ /* $Id$ */
+{
+ return sp[0];
+}
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_signals.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_signals.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_signals.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_signals.h 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_signals.h php-src/sapi/fpm/fpm/fpm_signals.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_signals.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_signals.h 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,16 @@
+
+ /* $Id$ */
+extern const char *fpm_signal_names[NSIG + 1];
+
+#endif
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_sockets.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_sockets.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_sockets.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_sockets.c 2008-08-26 19:09:15.000000000 +0400
-@@ -0,0 +1,425 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_sockets.c php-src/sapi/fpm/fpm/fpm_sockets.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_sockets.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_sockets.c 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,427 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+
+ sock = fpm_sockets_hash_op(0, sa, 0, wp->listen_address_domain, FPM_GET_USE_SOCKET);
+
-+ if (sock >= 0) return sock;
++ if (sock >= 0) { return sock; }
+
+ sock = fpm_sockets_new_listening_socket(wp, sa, socklen);
+
+
+enum fpm_address_domain fpm_sockets_domain_from_address(char *address)
+{
-+ if (strchr(address, ':')) return FPM_AF_INET;
++ if (strchr(address, ':')) { return FPM_AF_INET; }
+
-+ if (strlen(address) == strspn(address, "0123456789")) return FPM_AF_INET;
++ if (strlen(address) == strspn(address, "0123456789")) { return FPM_AF_INET; }
+
+ return FPM_AF_UNIX;
+}
+ int type, fd_no;
+ char *eq;
+
-+ if (comma) *comma = '\0';
++ if (comma) { *comma = '\0'; }
+
+ eq = strchr(inherited, '=');
+
+ fpm_sockets_hash_op(fd_no, 0, inherited, type, FPM_STORE_SOCKET);
+ }
+
-+ if (comma) inherited = comma + 1;
-+ else inherited = 0;
++ if (comma) { inherited = comma + 1; }
++ else { inherited = 0; }
+ }
+
+ /* create all required sockets */
+ }
+ }
+
-+ fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_sockets_cleanup, 0);
++ if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_sockets_cleanup, 0)) {
++ return -1;
++ }
+
+ return 0;
+}
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_sockets.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_sockets.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_sockets.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_sockets.h 2008-08-26 19:09:15.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_sockets.h php-src/sapi/fpm/fpm/fpm_sockets.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_sockets.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_sockets.h 2009-10-18 21:05:39.310440424 +0100
@@ -0,0 +1,37 @@
+
+ /* $Id$ */
+{
+ int flags = fcntl(fd, F_GETFL);
+
-+ if (flags < 0) return -1;
++ if (flags < 0) { return -1; }
+
+ if (blocked)
-+ flags &= ~O_NONBLOCK;
++ { flags &= ~O_NONBLOCK; }
+ else
-+ flags |= O_NONBLOCK;
++ { flags |= O_NONBLOCK; }
+
+ return fcntl(fd, F_SETFL, flags);
+}
+ (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[3]
+
+#endif
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_stdio.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_stdio.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_stdio.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_stdio.c 2008-05-24 21:38:47.000000000 +0400
-@@ -0,0 +1,277 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_stdio.c php-src/sapi/fpm/fpm/fpm_stdio.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_stdio.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_stdio.c 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,286 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+ return -1;
+ }
+
-+ dup2(fd, STDIN_FILENO);
-+ dup2(fd, STDOUT_FILENO);
++ if (0 > dup2(fd, STDIN_FILENO) || 0 > dup2(fd, STDOUT_FILENO)) {
++ zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
++ return -1;
++ }
++
+ close(fd);
+
+ return 0;
+
+int fpm_stdio_init_final()
+{
-+ if (fpm_global_options.daemonize) {
++ if (fpm_global_config.daemonize) {
+
+ if (fpm_globals.error_log_fd != STDERR_FILENO) {
+ /* there might be messages to stderr from libevent, we need to log them all */
-+ dup2(fpm_globals.error_log_fd, STDERR_FILENO);
++ if (0 > dup2(fpm_globals.error_log_fd, STDERR_FILENO)) {
++ zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
++ return -1;
++ }
+ }
+
+ zlog_set_level(fpm_globals.log_level);
+ zlog_set_fd(-1);
+
+ if (wp->listening_socket != STDIN_FILENO) {
-+ dup2(wp->listening_socket, STDIN_FILENO);
++ if (0 > dup2(wp->listening_socket, STDIN_FILENO)) {
++ zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
++ return -1;
++ }
+ }
+
+ return 0;
+{
+ int fd;
+
-+ fd = open(fpm_global_options.error_log, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
++ fd = open(fpm_global_config.error_log, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
+
+ if (0 > fd) {
-+ zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"%s\") failed", fpm_global_options.error_log);
++ zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"%s\") failed", fpm_global_config.error_log);
+ return -1;
+ }
+
+ if (reopen) {
-+ if (fpm_global_options.daemonize) {
++ if (fpm_global_config.daemonize) {
+ dup2(fd, STDERR_FILENO);
+ }
+
+
+ return 0;
+}
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_stdio.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_stdio.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_stdio.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_stdio.h 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_stdio.h php-src/sapi/fpm/fpm/fpm_stdio.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_stdio.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_stdio.h 2009-10-18 21:05:39.310440424 +0100
@@ -0,0 +1,20 @@
+
+ /* $Id$ */
+
+#endif
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_str.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_str.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_str.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_str.h 2008-05-24 21:38:47.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_str.h php-src/sapi/fpm/fpm/fpm_str.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_str.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_str.h 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,49 @@
+
+ /* $Id$ */
+{
+ char *d, *end;
+
-+ if (!dst_size) return dst;
++ if (!dst_size) { return dst; }
+
+ d = dst;
+ end = dst + dst_size - 1;
+}
+
+#endif
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace.c 2008-07-21 00:59:00.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_trace.c php-src/sapi/fpm/fpm/fpm_trace.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_trace.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_trace.c 2009-10-18 21:05:39.310440424 +0100
@@ -0,0 +1,46 @@
+
+ /* $Id$ */
+ }
+}
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace.h 2008-07-21 07:04:25.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_trace.h php-src/sapi/fpm/fpm/fpm_trace.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_trace.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_trace.h 2009-10-18 21:05:39.302497288 +0100
@@ -0,0 +1,17 @@
+
+ /* $Id$ */
+
+#endif
+
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace_mach.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace_mach.c 2008-08-26 19:09:15.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_trace_mach.c php-src/sapi/fpm/fpm/fpm_trace_mach.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_trace_mach.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_trace_mach.c 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,102 @@
+
+ /* $Id$ */
+ return 0;
+}
+
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace_pread.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace_pread.c 2008-08-26 19:09:15.000000000 +0400
-@@ -0,0 +1,67 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_trace_pread.c php-src/sapi/fpm/fpm/fpm_trace_pread.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_trace_pread.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_trace_pread.c 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,72 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+
+#include <fcntl.h>
+#include <stdio.h>
++#if HAVE_INTTYPES_H
++#include <inttypes.h>
++#else
+#include <stdint.h>
++#endif
++
+
+#include "fpm_trace.h"
+#include "fpm_process_ctl.h"
+ return 0;
+}
+
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace_ptrace.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace_ptrace.c 2008-09-19 03:34:11.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_trace_ptrace.c php-src/sapi/fpm/fpm/fpm_trace_ptrace.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_trace_ptrace.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_trace_ptrace.c 2009-10-18 21:05:39.302497288 +0100
@@ -0,0 +1,85 @@
+
+ /* $Id$ */
+ return 0;
+}
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_unix.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_unix.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_unix.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_unix.c 2008-09-19 03:19:59.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_unix.c php-src/sapi/fpm/fpm/fpm_unix.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_unix.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_unix.c 2009-10-18 21:05:39.310440424 +0100
@@ -0,0 +1,289 @@
+
+ /* $Id$ */
+ wp->socket_gid = -1;
+ wp->socket_mode = 0666;
+
-+ if (!lo) return 0;
++ if (!lo) { return 0; }
+
+ if (lo->owner && *lo->owner) {
+ struct passwd *pwd;
+
+ fpm_pagesize = getpagesize();
+
-+ if (fpm_global_options.daemonize) {
++ if (fpm_global_config.daemonize) {
+
+ switch (fork()) {
+
+
+ return 0;
+}
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_unix.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_unix.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_unix.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_unix.h 2008-05-25 17:21:13.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_unix.h php-src/sapi/fpm/fpm/fpm_unix.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_unix.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_unix.h 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,17 @@
+
+ /* $Id$ */
+
+#endif
+
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_worker_pool.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_worker_pool.c 2008-08-26 19:09:15.000000000 +0400
-@@ -0,0 +1,67 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_worker_pool.c php-src/sapi/fpm/fpm/fpm_worker_pool.c
+--- php-src-vanilla/sapi/fpm/fpm/fpm_worker_pool.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_worker_pool.c 2009-10-18 21:05:39.308376784 +0100
+@@ -0,0 +1,69 @@
+
+ /* $Id$ */
+ /* (c) 2007,2008 Andrei Nigmatulin */
+
+int fpm_worker_pool_init_main()
+{
-+ fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_worker_pool_cleanup, 0);
++ if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_worker_pool_cleanup, 0)) {
++ return -1;
++ }
+
+ return 0;
+}
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_worker_pool.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_worker_pool.h 2008-08-26 19:09:15.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/fpm_worker_pool.h php-src/sapi/fpm/fpm/fpm_worker_pool.h
+--- php-src-vanilla/sapi/fpm/fpm/fpm_worker_pool.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/fpm_worker_pool.h 2009-10-18 21:05:39.310440424 +0100
@@ -0,0 +1,46 @@
+
+ /* $Id$ */
+
+#endif
+
-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
---- php-5.2.6.vanilla/sapi/cgi/fpm/init.d/php-fpm.in 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/init.d/php-fpm.in 2008-08-05 20:31:27.000000000 +0400
-@@ -0,0 +1,139 @@
-+#! /bin/sh
-+
-+php_fpm_BIN=@prefix@/bin/php-cgi
-+php_fpm_CONF=@php_fpm_conf_path@
-+php_fpm_PID=@php_fpm_pid_path@
-+
-+
-+php_opts="--fpm-config $php_fpm_CONF"
-+
-+
-+wait_for_pid () {
-+ try=0
-+
-+ while test $try -lt 35 ; do
-+
-+ case "$1" in
-+ 'created')
-+ if [ -f "$2" ] ; then
-+ try=''
-+ break
-+ fi
-+ ;;
-+
-+ 'removed')
-+ if [ ! -f "$2" ] ; then
-+ try=''
-+ break
-+ fi
-+ ;;
-+ esac
-+
-+ echo -n .
-+ try=`expr $try + 1`
-+ sleep 1
-+
-+ done
-+
-+}
-+
-+case "$1" in
-+ start)
-+ echo -n "Starting php_fpm "
-+
-+ $php_fpm_BIN --fpm $php_opts
-+
-+ if [ "$?" != 0 ] ; then
-+ echo " failed"
-+ exit 1
-+ fi
-+
-+ wait_for_pid created $php_fpm_PID
-+
-+ if [ -n "$try" ] ; then
-+ echo " failed"
-+ exit 1
-+ else
-+ echo " done"
-+ fi
-+ ;;
-+
-+ stop)
-+ echo -n "Shutting down php_fpm "
-+
-+ if [ ! -r $php_fpm_PID ] ; then
-+ echo "warning, no pid file found - php-fpm is not running ?"
-+ exit 1
-+ fi
-+
-+ kill -TERM `cat $php_fpm_PID`
-+
-+ wait_for_pid removed $php_fpm_PID
-+
-+ if [ -n "$try" ] ; then
-+ echo " failed"
-+ exit 1
-+ else
-+ echo " done"
-+ fi
-+ ;;
-+
-+ quit)
-+ echo -n "Gracefully shutting down php_fpm "
-+
-+ if [ ! -r $php_fpm_PID ] ; then
-+ echo "warning, no pid file found - php-fpm is not running ?"
-+ exit 1
-+ fi
-+
-+ kill -QUIT `cat $php_fpm_PID`
-+
-+ wait_for_pid removed $php_fpm_PID
-+
-+ if [ -n "$try" ] ; then
-+ echo " failed"
-+ exit 1
-+ else
-+ echo " done"
-+ fi
-+ ;;
-+
-+ restart)
-+ $0 stop
-+ $0 start
-+ ;;
-+
-+ reload)
-+
-+ echo -n "Reload service php-fpm "
-+
-+ if [ ! -r $php_fpm_PID ] ; then
-+ echo "warning, no pid file found - php-fpm is not running ?"
-+ exit 1
-+ fi
-+
-+ kill -USR2 `cat $php_fpm_PID`
-+
-+ echo " done"
-+ ;;
-+
-+ logrotate)
-+
-+ echo -n "Re-opening php-fpm log file "
-+
-+ if [ ! -r $php_fpm_PID ] ; then
-+ echo "warning, no pid file found - php-fpm is not running ?"
-+ exit 1
-+ fi
-+
-+ kill -USR1 `cat $php_fpm_PID`
-+
-+ echo " done"
-+ ;;
-+
-+ *)
-+ echo "Usage: $0 {start|stop|quit|restart|reload|logrotate}"
-+ exit 1
-+ ;;
-+
-+esac
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/Makefile.frag php-5.2.6.fpm/sapi/cgi/fpm/Makefile.frag
---- php-5.2.6.vanilla/sapi/cgi/fpm/Makefile.frag 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/Makefile.frag 2008-03-28 16:51:22.000000000 +0300
-@@ -0,0 +1,21 @@
-+
-+install-fpm: sapi/cgi/fpm/php-fpm.conf sapi/cgi/fpm/php-fpm
-+ @echo "Installing FPM config: $(INSTALL_ROOT)$(php_fpm_conf_path)"
-+ -@$(mkinstalldirs) \
-+ $(INSTALL_ROOT)$(prefix)/sbin \
-+ `dirname "$(INSTALL_ROOT)$(php_fpm_conf_path)"` \
-+ `dirname "$(INSTALL_ROOT)$(php_fpm_log_path)"` \
-+ `dirname "$(INSTALL_ROOT)$(php_fpm_pid_path)"`
-+ -@if test -r "$(INSTALL_ROOT)$(php_fpm_conf_path)" ; then \
-+ dest=`basename "$(php_fpm_conf_path)"`.default ; \
-+ echo " (installing as $$dest)" ; \
-+ else \
-+ dest=`basename "$(php_fpm_conf_path)"` ; \
-+ fi ; \
-+ $(INSTALL_DATA) $(top_builddir)/sapi/cgi/fpm/php-fpm.conf $(INSTALL_ROOT)`dirname "$(php_fpm_conf_path)"`/$$dest
-+ @echo "Installing init.d script: $(INSTALL_ROOT)$(prefix)/sbin/php-fpm"
-+ -@$(INSTALL) -m 0755 $(top_builddir)/sapi/cgi/fpm/php-fpm $(INSTALL_ROOT)$(prefix)/sbin/php-fpm
-+
-+$(top_builddir)/libevent/libevent.a: $(top_builddir)/libevent/Makefile
-+ cd $(top_builddir)/libevent && $(MAKE) libevent.a
-+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/xml_config.c php-5.2.6.fpm/sapi/cgi/fpm/xml_config.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/xml_config.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/xml_config.c 2008-08-26 19:09:15.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/xml_config.c php-src/sapi/fpm/fpm/xml_config.c
+--- php-src-vanilla/sapi/fpm/fpm/xml_config.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/xml_config.c 2009-10-18 21:05:39.310440424 +0100
@@ -0,0 +1,278 @@
+
+ /* $Id$ */
+ char *value = vv;
+ char *v = strdup(value);
+
-+ if (!v) return "xml_conf_set_slot_string(): strdup() failed";
++ if (!v) { return "xml_conf_set_slot_string(): strdup() failed"; }
+
+#ifdef XML_CONF_DEBUG
+ fprintf(stderr, "setting string '%s' => '%s'\n", name, v);
+ char suffix;
+ int seconds;
+
-+ if (!len) return "xml_conf_set_slot_timeval(): invalid timeval value";
++ if (!len) { return "xml_conf_set_slot_timeval(): invalid timeval value"; }
+
+ suffix = value[len-1];
+
+ }
+
+ xmlFree(name);
-+ if (ret) return ret;
-+ else continue;
++ if (ret) { return ret; }
++ else { continue; }
+ }
+
+ fprintf(stderr, "Warning, unknown setting '%s' in section '%s'\n", (char *) name, section->path);
+ if (section) { /* found a registered section */
+ void *conf = section->conf();
+ ret = xml_conf_parse_section(&conf, section, element->children);
-+ if (ret) break;
++ if (ret) { break; }
+ }
+
+ }
+
+ if (element->children) {
+ ret = xml_conf_parse_file(element->children);
-+ if (ret) break;
++ if (ret) { break; }
+ }
+ }
+
+ return 0;
+}
+
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/xml_config.h php-5.2.6.fpm/sapi/cgi/fpm/xml_config.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/xml_config.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/xml_config.h 2008-09-19 03:02:58.000000000 +0400
-@@ -0,0 +1,43 @@
+diff -Naur php-src-vanilla/sapi/fpm/fpm/xml_config.h php-src/sapi/fpm/fpm/xml_config.h
+--- php-src-vanilla/sapi/fpm/fpm/xml_config.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/xml_config.h 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,48 @@
+
+ /* $Id$ */
+ /* (c) 2004-2007 Andrei Nigmatulin */
+#ifndef XML_CONFIG_H
+#define XML_CONFIG_H 1
+
++#if HAVE_INTTYPES_H
++#include <inttypes.h>
++#else
+#include <stdint.h>
++#endif
++
+
+struct xml_value_parser;
+
+enum { XML_CONF_SCALAR = 1, XML_CONF_SUBSECTION = 2 };
+
+#endif
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/zlog.c php-5.2.6.fpm/sapi/cgi/fpm/zlog.c
---- php-5.2.6.vanilla/sapi/cgi/fpm/zlog.c 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/zlog.c 2008-05-23 01:08:32.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/zlog.c php-src/sapi/fpm/fpm/zlog.c
+--- php-src-vanilla/sapi/fpm/fpm/zlog.c 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/zlog.c 2009-10-18 21:05:39.310440424 +0100
@@ -0,0 +1,113 @@
+
+ /* $Id$ */
+
+ write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, buf, len);
+}
-diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/zlog.h php-5.2.6.fpm/sapi/cgi/fpm/zlog.h
---- php-5.2.6.vanilla/sapi/cgi/fpm/zlog.h 1970-01-01 03:00:00.000000000 +0300
-+++ php-5.2.6.fpm/sapi/cgi/fpm/zlog.h 2008-05-23 01:08:32.000000000 +0400
+diff -Naur php-src-vanilla/sapi/fpm/fpm/zlog.h php-src/sapi/fpm/fpm/zlog.h
+--- php-src-vanilla/sapi/fpm/fpm/zlog.h 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/fpm/zlog.h 2009-10-18 21:05:39.308376784 +0100
@@ -0,0 +1,34 @@
+
+ /* $Id$ */
+#define ZLOG_SYSERROR (ZLOG_ERROR | ZLOG_HAVE_ERRNO)
+
+#endif
---- php-5.2.6/sapi/cgi/Makefile.frag~ 2008-11-03 19:29:12.000000000 +0200
-+++ php-5.2.6/sapi/cgi/Makefile.frag 2008-11-03 19:31:19.361921087 +0200
-@@ -1,2 +1,2 @@
--$(SAPI_CGI_PATH): libphp_common.la $(PHP_SAPI_OBJS)
-+$(SAPI_CGI_PATH): libphp_common.la $(PHP_SAPI_OBJS) $(SAPI_EXTRA_DEPS)
- $(BUILD_CGI)
+diff -Naur php-src-vanilla/sapi/fpm/LICENSE php-src/sapi/fpm/LICENSE
+--- php-src-vanilla/sapi/fpm/LICENSE 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/LICENSE 2009-10-18 21:05:39.302497288 +0100
+@@ -0,0 +1,23 @@
++Copyright (c) 2007-2009, Andrei Nigmatulin
++All rights reserved.
++
++Redistribution and use in source and binary forms, with or without
++modification, are permitted provided that the following conditions
++are met:
++1. Redistributions of source code must retain the above copyright
++ notice, this list of conditions and the following disclaimer.
++2. Redistributions in binary form must reproduce the above copyright
++ notice, this list of conditions and the following disclaimer in the
++ documentation and/or other materials provided with the distribution.
++
++THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
++ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
++IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
++ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
++FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
++DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
++OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
++HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
++LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
++OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
++SUCH DAMAGE.
+diff -Naur php-src-vanilla/sapi/fpm/man/php-fpm.1.in php-src/sapi/fpm/man/php-fpm.1.in
+--- php-src-vanilla/sapi/fpm/man/php-fpm.1.in 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/man/php-fpm.1.in 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,186 @@
++.TH PHP-FPM 1 "2009" "The PHP Group" "Scripting Language"
++.SH NAME
++.TP 15
++@php_fpm_bin@ \- PHP FastCGI Process Manager 'PHP-FPM'
++.SH SYNOPSIS
++.B @php_fpm_bin@
++[options]
++.LP
++.SH DESCRIPTION
++\fBPHP\fP is a widely\-used general\-purpose scripting language that is especially suited for
++Web development and can be embedded into HTML. This is a variant of PHP that will run in the background as a daemon, listening for CGI requests. Output is logged to @php_fpm_log_path@.
++.LP
++Most options are set in the xml configuration file @php_fpm_conf_path@. Unless configured otherwise, @php_fpm_bin@ will respond to CGI requests listening on http localhost port 9000 by default. Therefore @php_fpm_bin@ expects your webserver to forward all requests for '.php' files to port 9000 and you should edit your webserver configuration file appropriately.
++.SH OPTIONS
++.TP 15
++.B \-C
++Do not chdir to the script's directory
++.TP
++.PD 0
++.B \-\-php\-ini \fIpath\fP|\fIfile\fP
++.TP
++.PD 1
++.B \-c \fIpath\fP|\fIfile\fP
++Look for
++.B php.ini
++file in the directory
++.IR path
++or use the specified
++.IR file
++.TP
++.PD 0
++.B \-\-no\-php\-ini
++.TP
++.PD 1
++.B \-n
++No
++.B php.ini
++file will be used
++.TP
++.PD 0
++.B \-\-define \fIfoo\fP[=\fIbar\fP]
++.TP
++.PD 1
++.B \-d \fIfoo\fP[=\fIbar\fP]
++Define INI entry
++.IR foo
++with value
++.IR bar
++.TP
++.B \-e
++Generate extended information for debugger/profiler
++.TP
++.PD 0
++.B \-\-help
++.TP
++.PD 1
++.B \-h
++This help
++.TP
++.PD 0
++.B \-\-info
++.TP
++.PD 1
++.B \-i
++PHP information and configuration
++.TP
++.PD 0
++.B \-\-modules
++.TP
++.PD 1
++.B \-m
++Show compiled in modules
++.TP
++.PD 0
++.B \-\-version
++.TP
++.PD 1
++.B \-v
++Version number
++.TP
++.PD 0
++.B \-\-fpm\-config \fIfile\fP
++.TP
++.PD 1
++.B \-\-y
++Specify alternative path to FastCGI process manager configuration file (the default is @php_fpm_conf_path@)
++.TP
++.PD 0
++.B \-\-zend\-extension \fIfile\fP
++.TP
++.PD 1
++.B \-z \fIfile\fP
++Load Zend extension
++.IR file
++.SH FILES
++.TP 15
++.B @php_fpm_bin@.conf
++The configuration file for the @php_fpm_bin@ daemon.
++.TP
++.B php.ini
++The standard php configuration file.
++.SH EXAMPLES
++You should use the init script provided to start and stop the @php_fpm_bin@ daemon. This situation applies for any unix systems which use init.d for their main process manager.
++.P
++.PD 1
++.RS
++sudo /etc/init.d/@php_fpm_bin@ start
++.RE
++.TP
++If your installation has no appropriate init script, launch @php_fpm_bin_path@ with no arguments. It will launch as a daemon (background process) by default. The file @php_fpm_pid_path@ determines whether @php_fpm_bin@ is already up and running. Once started, @php_fpm_bin@ then responds to several POSIX signals:
++.P
++.PD 0
++.RS
++.B SIGINT,SIGTERM \fPimmediate termination
++.TP
++.B SIGQUIT \fPgraceful stop
++.TP
++.B SIGUSR1 \fPre-open log file
++.TP
++.B SIGUSR2 \fPgraceful reload of all workers + reload of fpm conf/binary
++.RE
++.PD 1
++.P
++.SH TIPS
++The PHP-FPM CGI daemon will work well with most popular webservers, including Apache2 and light-httpd. For best efficiency and performance improvements its also worthwhile to consider the engine-x webserver ('nginx'), and php opcode-cacher ('php5-xcache').
++.PD 1
++.P
++.SH SEE ALSO
++The PHP-FPM website:
++.PD 0
++.P
++.B http://php-fpm.org
++.PD 1
++.P
++For a more or less complete description of PHP look here:
++.PD 0
++.P
++.B http://www.php.net/manual/
++.PD 1
++.P
++A nice introduction to PHP by Stig Bakken can be found here:
++.PD 0
++.P
++.B http://www.zend.com/zend/art/intro.php
++.PD 1
++.SH BUGS
++You can view the list of known bugs or report any new bug you
++found at:
++.PD 0
++.P
++.B http://bugs.php.net
++.PD 1
++.SH AUTHORS
++PHP-FPM Sapi was written by Andrei Nigmatulin. The mailing-lists are highload-php-en (English) and highload-php-ru (Russion).
++.P
++The PHP Group: Thies C. Arntzen, Stig Bakken, Andi Gutmans, Rasmus Lerdorf, Sam Ruby, Sascha Schumann, Zeev Suraski, Jim Winstead, Andrei Zmievski.
++.P
++A List of active developers can be found here:
++.PD 0
++.P
++.B http://www.php.net/credits.php
++.PD 1
++.P
++And last but not least PHP was developed with the help of a huge amount of
++contributors all around the world.
++.SH VERSION INFORMATION
++This manpage describes \fBphp\fP, version @PHP_VERSION@, \fBfpm\fP, version @fpm_version@.
++.SH COPYRIGHT
++Copyright \(co 1997\-2009 The PHP Group
++.PD 0
++.P
++Copyright (c) 2007-2009, Andrei Nigmatulin
++.PD 1
++.LP
++This source file is subject to version 3.01 of the PHP license,
++that is bundled with this package in the file LICENSE, and is
++available through the world-wide-web at the following url:
++.PD 0
++.P
++.B http://www.php.net/license/3_01.txt
++.PD 1
++.P
++If you did not receive a copy of the PHP license and are unable to
++obtain it through the world-wide-web, please send a note to
++.B license@php.net
++so we can mail you a copy immediately.
+diff -Naur php-src-vanilla/sapi/fpm/readme-ru.markdown php-src/sapi/fpm/readme-ru.markdown
+--- php-src-vanilla/sapi/fpm/readme-ru.markdown 1970-01-01 01:00:00.000000000 +0100
++++ php-src/sapi/fpm/readme-ru.markdown 2009-10-18 21:05:39.310440424 +0100
+@@ -0,0 +1,127 @@
++# PHP FastCGI Менеджер процессов (PHP-FPM)
++
++PHP-FPM это Fast-CGI фронтэнд для php и расширение php-cgi. Проект находится на [Launchpad](https://launchpad.net/php-fpm)
++
++## Быстрый старт:
++
++Выберите один из 2 путей сборки fpm: Или `встроенный`, или `отдельный`. Если вы не разработчик или не системный администратор, то мы рекомендуем `встроенный` вариант компиляции. Для дополнительной информации смотрите файл `readme.markdown`.
++
++## Зависимости
++Если вы до этого не устанавливали php, то вам придётся установить пакет `libxml2-dev`. FPM также необходим `libevent-dev`. Debian / ubuntu:
++
++ sudo aptitude install -y libxml2-dev libevent-dev
++
++Рекомендуется использовать libevent 1.4.12-stable или позднее, но необходим, как минимум, libevent 1.4.3-stable. Если нет подходящей версии, скайте и скомпилируйте с [сайта Libevent](http://www.monkey.org/~provos/libevent/).
++
++ export LE_VER=1.4.12-stable
++ wget "http://www.monkey.org/~provos/libevent-$LE_VER.tar.gz"
++ tar -zxvf "libevent-$LE_VER.tar.gz"
++ cd "libevent-$LE_VER"
++ ./configure && make
++ DESTDIR=$PWD make install
++ export LIBEVENT_SEARCH_PATH="$PWD/usr/local"
++
++## Встроенная сборка
++
++Скачайте fpm и сгенерируйте патч
++
++ export PHP_VER=5.3.0
++ wget "http://launchpad.net/php-fpm/master/0.6/+download/php-fpm-0.6-$PHP_VER.tar.gz"
++ tar -zxvf "php-fpm-0.6-$PHP_VER.tar.gz"
++ "php-fpm-0.6-$PHP_VER/generate-fpm-patch"
++
++Скачайте и распакуйте исходный код PHP
++
++ wget "http://ru2.php.net/get/php-$PHP_VER.tar.gz/from/ru2.php.net/mirror"
++ tar xvfz "php-$PHP_VER.tar.gz"
++ cd "php-$PHP_VER"
++
++Примените патч и компилируйте
++
++ patch -p1 < ../fpm.patch
++ ./buildconf --force
++ mkdir fpm-build && cd fpm-build
++ ../configure --with-fpm \
++ --with-libevent="$LIBEVENT_SEARCH_PATH" && make
++
++## Отдельная сборка
++
++Скачайте и распакуйте исходный код PHP
++
++ export PHP_VER=5.3.0
++ wget "http://ru2.php.net/get/php-$PHP_VER.tar.gz/from/ru2.php.net/mirror"
++ tar xvfz "php-$PHP_VER.tar.gz"
++ cd "php-$PHP_VER"
++ mkdir php-build && cd php-build
++ ../configure && make
++
++Теперь можете скачать, конфигурировать и компилировать FPM фронтэнд
++
++ wget "http://launchpad.net/php-fpm/master/0.6/+download/php-fpm-0.6-$PHP_VER.tar.gz"
++ tar -zxvf "php-fpm-0.6-$PHP_VER.tar.gz"
++ cd "php-fpm-0.6-$PHP_VER"
++ mkdir fpm-build && cd fpm-build
++ ../configure --srcdir=../ \
++ --with-php-src="../../php-$PHP_VER" \
++ --with-php-build="../../php-$PHP_VER/php-build" \
++ --with-libevent="$LIBEVENT_SEARCH_PATH" && make
++
++## Флаги конфигурирования
++
++ --with-libevent[=PATH] Путь до libevent, для fpm SAPI [/usr/local]
++ --with-fpm-bin[=PATH] Путь для откомпилированного php-fpm [/usr/local/bin/php-fpm]
++ --with-fpm-port[=PORT] TCP порт для cgi запросов [9000]
++ --with[out]-fpm-conf[=PATH] Путь до файла конфигурации php-fpm [/etc/php-fpm.conf]
++ --with[out]-fpm-init[=PATH] Путь до init-файла php-fpm [/etc/init.d/php-fpm]
++ --with-fpm-log[=PATH] Путь до лог-файла php-fpm [/var/log/php-fpm.log]
++ --with-fpm-pid[=PATH] Путь до pid-файла php-fpm [/var/run/php-fpm.pid]
++ --with-fpm-user[=USER] Пользователь, под которым запускать php-fpm [nobody]
++ --with-fpm-group[=GRP] Группа, под которой запускать php-fpm. Для системных
++ пользователей задайте имя пользователя [nobody]
++
++## Установка
++
++Если вы делали `встроенную` сборку, то вы получите полный php, включая исполнитель коммандной строки `php-cli` и библиотеку PEAR. `Отдельная` или `независимая` сборка установит только демон `php-fpm` и минимум файлов, необходимых для его запуска.
++
++ # Посмотреть, какие файлы будут установлены
++ make install --dry-run
++
++ # Установить в '/'
++ sudo make install
++
++ # Установить в '/opt'
++ sudo INSTALL_ROOT=/opt make install
++
++Notes:
++
++* (Upgrade) When overwriting existing FPM installation files: A previous configuration file `php-fpm.conf` will be moved to `php-fpm.conf.old`. Then a newer (default) configuration file will be installed in it's place. If you have any custom XML settings which you wish to keep, its recommended to copy these back over manually.
++
++* (BSD) the default init.d path is `/usr/local/etc/rc.d/php-fpm` or disable: `--without-fpm-init`
++
++* (Nginx) An example nginx configuration file is generated. The file `nginx-site-conf.sample` may be installed into your nginx configuration directory, if exists: `/etc/nginx/`, `/usr/local/etc/nginx/`, or `/usr/local/nginx/conf`
++
++## Больше о процессе сборки PHP-FPM
++
++Процесс сборки можно описать так:
++
++ 1) Компилируются исходники php в объектные файлы
++ 2) Компилируются исходники fpm в объектные файлы
++ 3) Линковка php и fpm объектных файлов
++ 4) Результат: исполняемый php5, в основе которого php и fast-CGI от fpm как фронтэнд
++
++Fpm подмешивается в php при линковке (link-level). Андрей разделил исходный код fpm, сделав SAPI чем-то менее чуствительным к изменениям в остальном коде php. Код cgi-main.c из PHP-FPM - конктроллер запросов - вырезан из оригинального fcgi-sapi. Мы отправляем билд 0.6 в PHP Group. Мы будем отслеживать развитие PHP и периодически синхронизировать изменения с проектами встроенной / отдельной сборки.
++
++## Buildconf
++
++Для сборки fpm отдельно, конфигурирование (`./configure`) требует некоторой версии набора инструментов autoconf. Buildconf запустит `./generate-autotools` и попробует установить эти инструменты самостоятельно. Если `./buildconf` не работает, смотрите лог ошибок.
++
++
++## Обсуждение
++
++Есть 2 группы для обсуждения php-fpm,
++
++- [highload-php-ru](http://groups.google.com/group/highload-php-en) (english)
++
++- [highload-php-ru](http://groups.google.com/group/highload-php-ru) (русская)
++
++Translated by Anatoly Pashin