1 diff -Nru php-5.2.6.vanilla/configure php-5.2.6.fpm/configure
2 diff -Nru php-5.2.6.vanilla/configure.in php-5.2.6.fpm/configure.in
3 --- php-5.2.6.vanilla/configure.in 2008-04-30 22:27:55.000000000 +0400
4 +++ php-5.2.6.fpm/configure.in 2008-05-01 20:42:05.000000000 +0400
9 +if test "$PHP_FASTCGI" = "yes" -a "$PHP_FPM" = "yes"; then
10 + PHP_CONFIGURE_PART(Running FastCGI Process Manager checks)
11 + sinclude(sapi/cgi/fpm/acinclude.m4)
12 + sinclude(sapi/cgi/fpm/config.m4)
17 dnl ## In diversion 3 we check for compile-time options to the PHP
27 PHP_SUBST_OLD(EXTRA_LDFLAGS)
28 PHP_SUBST_OLD(EXTRA_LDFLAGS_PROGRAM)
29 PHP_SUBST_OLD(EXTRA_LIBS)
30 +PHP_SUBST_OLD(SAPI_EXTRA_LIBS)
31 +PHP_SUBST_OLD(SAPI_EXTRA_DEPS)
32 PHP_SUBST_OLD(ZEND_EXTRA_LIBS)
33 PHP_SUBST_OLD(INCLUDES)
34 PHP_SUBST_OLD(EXTRA_INCLUDES)
36 install_targets="$PHP_INSTALL_CLI_TARGET $install_targets"
39 - install_targets="install-sapi $PHP_INSTALL_CLI_TARGET $install_targets"
40 + install_targets="install-sapi $install_fpm $PHP_INSTALL_CLI_TARGET $install_targets"
44 diff -Nru php-5.2.6.vanilla/libevent/aclocal.m4 php-5.2.6.fpm/libevent/aclocal.m4
45 diff -Nru php-5.2.6.vanilla/libevent/autogen.sh php-5.2.6.fpm/libevent/autogen.sh
46 diff -Nru php-5.2.6.vanilla/libevent/ChangeLog php-5.2.6.fpm/libevent/ChangeLog
47 diff -Nru php-5.2.6.vanilla/libevent/compat/sys/queue.h php-5.2.6.fpm/libevent/compat/sys/queue.h
48 diff -Nru php-5.2.6.vanilla/libevent/compat/sys/_time.h php-5.2.6.fpm/libevent/compat/sys/_time.h
49 diff -Nru php-5.2.6.vanilla/libevent/config.guess php-5.2.6.fpm/libevent/config.guess
50 diff -Nru php-5.2.6.vanilla/libevent/config.h.in php-5.2.6.fpm/libevent/config.h.in
51 diff -Nru php-5.2.6.vanilla/libevent/config.sub php-5.2.6.fpm/libevent/config.sub
52 diff -Nru php-5.2.6.vanilla/libevent/configure php-5.2.6.fpm/libevent/configure
53 diff -Nru php-5.2.6.vanilla/libevent/configure.in php-5.2.6.fpm/libevent/configure.in
54 diff -Nru php-5.2.6.vanilla/libevent/depcomp php-5.2.6.fpm/libevent/depcomp
55 diff -Nru php-5.2.6.vanilla/libevent/devpoll.c php-5.2.6.fpm/libevent/devpoll.c
56 diff -Nru php-5.2.6.vanilla/libevent/epoll.c php-5.2.6.fpm/libevent/epoll.c
57 diff -Nru php-5.2.6.vanilla/libevent/epoll_sub.c php-5.2.6.fpm/libevent/epoll_sub.c
58 diff -Nru php-5.2.6.vanilla/libevent/event.3 php-5.2.6.fpm/libevent/event.3
59 diff -Nru php-5.2.6.vanilla/libevent/event.c php-5.2.6.fpm/libevent/event.c
60 diff -Nru php-5.2.6.vanilla/libevent/event-config.h php-5.2.6.fpm/libevent/event-config.h
61 diff -Nru php-5.2.6.vanilla/libevent/event-fpm.h php-5.2.6.fpm/libevent/event-fpm.h
62 diff -Nru php-5.2.6.vanilla/libevent/event.h php-5.2.6.fpm/libevent/event.h
63 diff -Nru php-5.2.6.vanilla/libevent/event-internal.h php-5.2.6.fpm/libevent/event-internal.h
64 diff -Nru php-5.2.6.vanilla/libevent/evport.c php-5.2.6.fpm/libevent/evport.c
65 diff -Nru php-5.2.6.vanilla/libevent/evsignal.h php-5.2.6.fpm/libevent/evsignal.h
66 diff -Nru php-5.2.6.vanilla/libevent/evutil.c php-5.2.6.fpm/libevent/evutil.c
67 diff -Nru php-5.2.6.vanilla/libevent/evutil.h php-5.2.6.fpm/libevent/evutil.h
68 diff -Nru php-5.2.6.vanilla/libevent/install-sh php-5.2.6.fpm/libevent/install-sh
69 diff -Nru php-5.2.6.vanilla/libevent/kqueue.c php-5.2.6.fpm/libevent/kqueue.c
70 diff -Nru php-5.2.6.vanilla/libevent/log.c php-5.2.6.fpm/libevent/log.c
71 diff -Nru php-5.2.6.vanilla/libevent/log.h php-5.2.6.fpm/libevent/log.h
72 diff -Nru php-5.2.6.vanilla/libevent/Makefile.am php-5.2.6.fpm/libevent/Makefile.am
73 diff -Nru php-5.2.6.vanilla/libevent/Makefile.in php-5.2.6.fpm/libevent/Makefile.in
74 diff -Nru php-5.2.6.vanilla/libevent/min_heap.h php-5.2.6.fpm/libevent/min_heap.h
75 diff -Nru php-5.2.6.vanilla/libevent/missing php-5.2.6.fpm/libevent/missing
76 diff -Nru php-5.2.6.vanilla/libevent/mkinstalldirs php-5.2.6.fpm/libevent/mkinstalldirs
77 diff -Nru php-5.2.6.vanilla/libevent/poll.c php-5.2.6.fpm/libevent/poll.c
78 diff -Nru php-5.2.6.vanilla/libevent/README php-5.2.6.fpm/libevent/README
79 diff -Nru php-5.2.6.vanilla/libevent/select.c php-5.2.6.fpm/libevent/select.c
80 diff -Nru php-5.2.6.vanilla/libevent/signal.c php-5.2.6.fpm/libevent/signal.c
81 diff -Nru php-5.2.6.vanilla/main/php_config.h.in php-5.2.6.fpm/main/php_config.h.in
82 --- php-5.2.6.vanilla/main/php_config.h.in 2008-04-30 22:37:39.000000000 +0400
83 +++ php-5.2.6.fpm/main/php_config.h.in 2008-09-21 17:37:45.000000000 +0400
85 /* Define if you have the chroot function. */
88 +/* Define if you have the clearenv function. */
91 /* Define if you have the crypt function. */
98 +/* Is experimental fastcgi process manager code activated */
99 +#undef PHP_FASTCGI_PM
102 #undef FORCE_CGI_REDIRECT
106 #undef ENABLE_PATHINFO_CHECK
108 +/* do we have libxml? */
111 +/* do we have prctl? */
114 +/* do we have clock_gettime? */
115 +#undef HAVE_CLOCK_GETTIME
117 +/* do we have clock_get_time? */
118 +#undef HAVE_CLOCK_GET_TIME
120 +/* do we have ptrace? */
123 +/* do we have mach_vm_read? */
124 +#undef HAVE_MACH_VM_READ
126 +/* /proc/pid/mem interface */
127 +#undef PROC_MEM_FILE
129 /* Define if system uses EBCDIC */
130 #undef CHARSET_EBCDIC
132 --- php-5.2.6.fpm/sapi/cgi/cgi_main.c 2008-07-22 01:50:58.000000000 +0400
133 +++ php-5.2.6/sapi/cgi/cgi_main.c 2008-11-03 20:21:40.431760373 +0200
136 #include <sys/wait.h>
142 #include "zend_extensions.h"
149 +#include "fpm/fpm.h"
150 +#include "fpm/fpm_request.h"
154 /* XXX this will need to change later when threaded fastcgi is
155 implemented. shane */
160 +static int request_body_fd;
164 +static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC);
166 #define PHP_MODE_STANDARD 1
167 #define PHP_MODE_HIGHLIGHT 2
168 #define PHP_MODE_INDENT 3
171 {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
175 + {'y', 1, "fpm-config"},
177 {'z', 1, "zend-extension"},
181 zend_bool impersonate;
184 + char *error_header;
185 } php_cgi_globals_struct;
190 if (fcgi_is_fastcgi()) {
191 fcgi_request *request = (fcgi_request*) SG(server_context);
192 - tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
194 + if (request_body_fd == -1) {
195 + char *request_body_filename = sapi_cgibin_getenv((char *) "REQUEST_BODY_FILE",
196 + sizeof("REQUEST_BODY_FILE")-1 TSRMLS_CC);
198 + if (request_body_filename && *request_body_filename) {
199 + request_body_fd = open(request_body_filename, O_RDONLY);
201 + if (0 > request_body_fd) {
202 + php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)",
203 + request_body_filename, strerror(errno), errno);
209 + /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */
210 + if (request_body_fd < 0) {
211 + tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
213 + tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes);
216 tmp_read_bytes = read(0, buffer + read_bytes, count_bytes - read_bytes);
219 " -s Display colour syntax highlighted source.\n"
220 " -v Version number\n"
221 " -w Display source with stripped comments and whitespace.\n"
222 - " -z <file> Load Zend extension <file>.\n"
224 + " -x, --fpm Run in FastCGI process manager mode.\n"
225 + " -y, --fpm-config <file>\n"
226 + " Specify alternative path to FastCGI process manager config file.\n"
228 + " -z <file> Load Zend extension <file>.\n"
230 " -T <count> Measure execution time of script repeated <count> times.\n"
232 @@ -1173,6 +1216,7 @@
234 STD_PHP_INI_ENTRY("fastcgi.impersonate", "0", PHP_INI_SYSTEM, OnUpdateBool, impersonate, php_cgi_globals_struct, php_cgi_globals)
236 + STD_PHP_INI_ENTRY("fastcgi.error_header", NULL, PHP_INI_SYSTEM, OnUpdateString, error_header, php_cgi_globals_struct, php_cgi_globals)
240 @@ -1195,6 +1239,7 @@
242 php_cgi_globals->impersonate = 0;
244 + php_cgi_globals->error_header = NULL;
248 @@ -1227,9 +1272,47 @@
249 static PHP_MINFO_FUNCTION(cgi)
251 DISPLAY_INI_ENTRIES();
255 +#include "fpm/fpm_autoconf.h"
257 + php_info_print_table_start();
258 + php_info_print_table_row(2, "php-fpm", fpm ? "active" : "inactive");
259 + php_info_print_table_row(2, "php-fpm version", PHP_FPM_VERSION);
260 + php_info_print_table_end();
267 +PHP_FUNCTION(fastcgi_finish_request)
269 + fcgi_request *request = (fcgi_request*) SG(server_context);
271 + if (fcgi_is_fastcgi() && request->fd >= 0) {
273 + php_end_ob_buffers(1 TSRMLS_CC);
274 + php_header(TSRMLS_C);
276 + fcgi_flush(request, 1);
277 + fcgi_close(request, 0, 0);
286 +function_entry cgi_fcgi_sapi_functions[] = {
288 + PHP_FE(fastcgi_finish_request, NULL)
293 static zend_module_entry cgi_module_entry = {
294 STANDARD_MODULE_HEADER,
296 @@ -1237,7 +1320,7 @@
301 + cgi_fcgi_sapi_functions,
305 @@ -1277,6 +1360,7 @@
306 char *bindpath = NULL;
308 fcgi_request request;
309 + char *fpm_config = NULL;
312 #if HAVE_GETTIMEOFDAY
313 @@ -1397,6 +1481,14 @@
314 case 's': /* generate highlighted HTML from source */
315 behavior = PHP_MODE_HIGHLIGHT;
319 + fpm_config = php_optarg;
328 @@ -1459,6 +1551,19 @@
329 #endif /* FORCE_CGI_REDIRECT */
334 + if (0 > fpm_init(argc, argv, fpm_config)) {
338 + fcgi_fd = fpm_run(&max_requests);
340 + fcgi_set_is_fastcgi(fastcgi = 1);
346 fcgi_fd = fcgi_listen(bindpath, 128);
348 @@ -1625,7 +1625,11 @@
351 /* How many times to run PHP scripts before dying */
352 - if (getenv("PHP_FCGI_MAX_REQUESTS")) {
357 + getenv("PHP_FCGI_MAX_REQUESTS")) {
358 max_requests = atoi(getenv("PHP_FCGI_MAX_REQUESTS"));
359 if (max_requests < 0) {
360 fprintf(stderr, "PHP_FCGI_MAX_REQUESTS is not valid\n");
361 @@ -1677,7 +1677,11 @@
364 /* Pre-fork, if required */
365 - if (getenv("PHP_FCGI_CHILDREN")) {
370 + getenv("PHP_FCGI_CHILDREN")) {
371 char * children_str = getenv("PHP_FCGI_CHILDREN");
372 children = atoi(children_str);
374 @@ -1616,6 +1729,8 @@
378 + request_body_fd = -1;
380 SG(server_context) = (void *) &request;
382 SG(server_context) = (void *) 1; /* avoid server_context==NULL checks */
383 @@ -1623,6 +1730,10 @@
384 init_request_info(TSRMLS_C);
388 + if (fpm) fpm_request_info();
394 @@ -1914,6 +2025,10 @@
399 + if (fpm) fpm_request_executing();
403 case PHP_MODE_STANDARD:
404 php_execute_script(&file_handle TSRMLS_CC);
405 @@ -1966,6 +2081,10 @@
408 fastcgi_request_done:
410 + if (request_body_fd != -1) close(request_body_fd);
412 + request_body_fd = -2;
415 char *path_translated;
416 @@ -1979,6 +2098,16 @@
417 SG(request_info).path_translated = path_translated;
420 + if (EG(exit_status) == 255) {
421 + if (CGIG(error_header) && *CGIG(error_header)) {
422 + sapi_header_line ctr = {0};
424 + ctr.line = CGIG(error_header);
425 + ctr.line_len = strlen(CGIG(error_header));
426 + sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
430 php_request_shutdown((void *) 0);
431 if (exit_status == 0) {
432 exit_status = EG(exit_status);
433 @@ -2016,15 +2145,20 @@
437 - if (max_requests != 1) {
438 - /* no need to return exit_status of the last request */
443 /* end of fastcgi loop */
447 + if (fcgi_in_shutdown() || /* graceful shutdown by a signal */
448 + (max_requests && (requests == max_requests)) /* we were told to process max_requests and we are done */
457 if (cgi_sapi_module.php_ini_path_override) {
458 diff -Nru php-5.2.6.vanilla/sapi/cgi/config9.m4 php-5.2.6.fpm/sapi/cgi/config9.m4
459 --- php-5.2.6.vanilla/sapi/cgi/config9.m4 2007-07-12 03:20:36.000000000 +0400
460 +++ php-5.2.6.fpm/sapi/cgi/config9.m4 2008-07-20 20:46:41.000000000 +0400
462 [ --disable-path-info-check CGI: If this is disabled, paths such as
463 /info.php/test?a=b will fail to work], yes, no)
465 +PHP_ARG_ENABLE(fpm,,
466 +[ --enable-fpm FastCGI: If this is enabled, the fastcgi support
467 + will include experimental process manager code], no, no)
473 AC_DEFINE_UNQUOTED(PHP_FASTCGI, $PHP_ENABLE_FASTCGI, [ ])
474 AC_MSG_RESULT($PHP_FASTCGI)
477 + if test "$PHP_FASTCGI" = "yes"; then
478 + AC_MSG_CHECKING(whether to enable FastCGI Process Manager)
479 + if test "$PHP_FPM" = "yes"; then
484 + AC_MSG_RESULT($PHP_FPM)
488 + AC_DEFINE_UNQUOTED(PHP_FASTCGI_PM, $PHP_FASTCGI_PM, [Is experimental fastcgi process manager code activated])
490 dnl --enable-force-cgi-redirect
491 AC_MSG_CHECKING(whether to force Apache CGI redirect)
492 if test "$PHP_FORCE_CGI_REDIRECT" = "yes"; then
493 @@ -108,13 +108,13 @@
497 - 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)"
498 + 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)"
501 - 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)"
502 + 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)"
505 - 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)"
506 + 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)"
510 diff -Nru php-5.2.6.vanilla/sapi/cgi/fastcgi.c php-5.2.6.fpm/sapi/cgi/fastcgi.c
511 --- php-5.2.6.vanilla/sapi/cgi/fastcgi.c 2008-04-03 14:24:44.000000000 +0400
512 +++ php-5.2.6.fpm/sapi/cgi/fastcgi.c 2008-05-15 23:37:01.000000000 +0400
518 +#include "fpm/fpm.h"
519 +#include "fpm/fpm_request.h"
527 return is_fastcgi = 0;
530 + fcgi_set_allowed_clients(getenv("FCGI_WEB_SERVER_ADDRS"));
534 @@ -256,11 +256,27 @@
538 +void fcgi_set_is_fastcgi(int new_value)
540 + is_fastcgi = new_value;
543 +void fcgi_set_in_shutdown(int new_value)
545 + in_shutdown = new_value;
548 void fcgi_shutdown(void)
550 if (is_initialized) {
551 zend_hash_destroy(&fcgi_mgmt_vars);
554 + if (allowed_clients) {
555 + free(allowed_clients);
556 + allowed_clients = 0;
566 +void fcgi_set_allowed_clients(char *ip)
576 + if (*cur == ',') n++;
579 + if (allowed_clients) free(allowed_clients);
580 + allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
584 + end = strchr(cur, ',');
589 + allowed_clients[n] = inet_addr(cur);
590 + if (allowed_clients[n] == INADDR_NONE) {
591 + fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
596 + allowed_clients[n] = INADDR_NONE;
601 static int is_port_number(const char *bindpath)
609 - char *ip = getenv("FCGI_WEB_SERVER_ADDRS");
618 - if (*cur == ',') n++;
621 - allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
625 - end = strchr(cur, ',');
630 - allowed_clients[n] = inet_addr(cur);
631 - if (allowed_clients[n] == INADDR_NONE) {
632 - fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
637 - allowed_clients[n] = INADDR_NONE;
642 if (!is_initialized) {
647 -static inline void fcgi_close(fcgi_request *req, int force, int destroy)
648 +void fcgi_close(fcgi_request *req, int force, int destroy)
651 zend_hash_destroy(&req->env);
658 + if (fpm) fpm_request_finished();
665 socklen_t len = sizeof(sa);
668 + if (fpm) fpm_request_accepting();
671 FCGI_LOCK(req->listen_socket);
672 req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
673 FCGI_UNLOCK(req->listen_socket);
680 + if (fpm) fpm_request_reading_headers();
683 #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
686 diff -Nru php-5.2.6.vanilla/sapi/cgi/fastcgi.h php-5.2.6.fpm/sapi/cgi/fastcgi.h
687 --- php-5.2.6.vanilla/sapi/cgi/fastcgi.h 2007-12-31 10:20:16.000000000 +0300
688 +++ php-5.2.6.fpm/sapi/cgi/fastcgi.h 2008-05-01 20:42:06.000000000 +0400
691 void fcgi_shutdown(void);
692 int fcgi_is_fastcgi(void);
693 +void fcgi_set_is_fastcgi(int);
694 +void fcgi_set_in_shutdown(int);
695 +void fcgi_set_allowed_clients(char *);
696 int fcgi_in_shutdown(void);
697 int fcgi_listen(const char *path, int backlog);
698 void fcgi_init_request(fcgi_request *req, int listen_socket);
700 int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len);
701 int fcgi_flush(fcgi_request *req, int close);
703 +void fcgi_close(fcgi_request *req, int force, int destroy);
706 void fcgi_impersonate(void);
708 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/acinclude.m4 php-5.2.6.fpm/sapi/cgi/fpm/acinclude.m4
709 --- php-5.2.6.vanilla/sapi/cgi/fpm/acinclude.m4 1970-01-01 03:00:00.000000000 +0300
710 +++ php-5.2.6.fpm/sapi/cgi/fpm/acinclude.m4 2008-07-21 06:39:38.000000000 +0400
713 +AC_DEFUN([AC_FPM_CHECK_FUNC],
715 + SAVED_CFLAGS="$CFLAGS"
716 + CFLAGS="$CFLAGS $2"
720 + AC_CHECK_FUNC([$1],[$4],[$5])
722 + CFLAGS="$SAVED_CFLAGS"
726 +AC_DEFUN([AC_FPM_LIBEVENT],
728 + AC_ARG_WITH([libevent],
729 + [ --with-libevent=DIR FPM: libevent install directory])
732 + LIBEVENT_LIBS="-levent"
733 + LIBEVENT_INCLUDE_PATH=""
735 + if test "$with_libevent" != "no" -a -n "$with_libevent"; then
736 + LIBEVENT_CFLAGS="-I$with_libevent/include"
737 + LIBEVENT_LIBS="-L$with_libevent/lib $LIBEVENT_LIBS"
738 + LIBEVENT_INCLUDE_PATH="$with_libevent/include"
741 + AC_MSG_CHECKING([for event.h])
745 + for dir in "$LIBEVENT_INCLUDE_PATH" /usr/include ; do
746 + if test -r "$dir/event.h" ; then
752 + AC_MSG_RESULT([$found])
754 + AC_FPM_CHECK_FUNC([event_set], [$LIBEVENT_CFLAGS], [$LIBEVENT_LIBS], ,
755 + [AC_MSG_ERROR([Failed to link with libevent. Perhaps --with-libevent=DIR option could help.])])
757 + AC_FPM_CHECK_FUNC([event_base_free], [$LIBEVENT_CFLAGS], [$LIBEVENT_LIBS], ,
758 + [AC_MSG_ERROR([You have too old version. libevent version >= 1.2 is required.])])
762 +AC_DEFUN([AC_FPM_LIBXML],
764 + AC_MSG_RESULT([checking for XML configuration])
766 + AC_ARG_WITH(xml-config,
767 + [ --with-xml-config=PATH FPM: use xml-config in PATH to find libxml],
768 + [XMLCONFIG="$withval"],
769 + [AC_PATH_PROGS(XMLCONFIG, [xml2-config xml-config], "")]
772 + if test "x$XMLCONFIG" = "x"; then
773 + AC_MSG_ERROR([XML configuration could not be found])
775 + AC_MSG_CHECKING([for libxml library])
777 + if test ! -x "$XMLCONFIG"; then
778 + AC_MSG_ERROR([$XMLCONFIG cannot be executed])
781 + LIBXML_LIBS="`$XMLCONFIG --libs`"
782 + LIBXML_CFLAGS="`$XMLCONFIG --cflags`"
783 + LIBXML_VERSION="`$XMLCONFIG --version`"
785 + AC_MSG_RESULT([yes, $LIBXML_VERSION])
787 + AC_FPM_CHECK_FUNC([xmlParseFile], [$LIBXML_CFLAGS], [$LIBXML_LIBS], ,
788 + [AC_MSG_ERROR([Failed to link with libxml])])
790 + AC_DEFINE(HAVE_LIBXML, 1, [do we have libxml?])
794 +AC_DEFUN([AC_FPM_JUDY],
796 + AC_ARG_WITH([Judy],
797 + [ --with-Judy=DIR FPM: Judy install directory])
801 + JUDY_INCLUDE_PATH=""
803 + if test "$with_Judy" != "no" -a -n "$with_Judy"; then
804 + JUDY_INCLUDE_PATH="$with_Judy/include"
805 + JUDY_CFLAGS="-I$with_Judy/include $JUDY_CFLAGS"
806 + JUDY_LIBS="-L$with_Judy/lib $JUDY_LIBS"
809 + AC_MSG_CHECKING([for Judy.h])
813 + for dir in "$JUDY_INCLUDE_PATH" /usr/include ; do
814 + if test -r "$dir/Judy.h" ; then
820 + AC_MSG_RESULT([$found])
822 + AC_FPM_CHECK_FUNC([JudyLCount], [$JUDY_CFLAGS], [$JUDY_LIBS], ,
823 + [AC_MSG_ERROR([Failed to link with Judy])])
827 +AC_DEFUN([AC_FPM_CLOCK],
829 + have_clock_gettime=no
831 + AC_MSG_CHECKING([for clock_gettime])
833 + AC_TRY_COMPILE([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
834 + have_clock_gettime=yes
835 + AC_MSG_RESULT([yes])
837 + AC_MSG_RESULT([no])
840 + if test "$have_clock_gettime" = "no"; then
841 + AC_MSG_CHECKING([for clock_gettime in -lrt])
846 + AC_TRY_COMPILE([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
847 + have_clock_gettime=yes
848 + AC_MSG_RESULT([yes])
851 + AC_MSG_RESULT([no])
855 + if test "$have_clock_gettime" = "yes"; then
856 + AC_DEFINE([HAVE_CLOCK_GETTIME], 1, [do we have clock_gettime?])
859 + have_clock_get_time=no
861 + if test "$have_clock_gettime" = "no"; then
862 + AC_MSG_CHECKING([for clock_get_time])
864 + AC_TRY_RUN([ #include <mach/mach.h>
865 + #include <mach/clock.h>
866 + #include <mach/mach_error.h>
870 + kern_return_t ret; clock_serv_t aClock; mach_timespec_t aTime;
871 + ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &aClock);
873 + if (ret != KERN_SUCCESS) {
877 + ret = clock_get_time(aClock, &aTime);
878 + if (ret != KERN_SUCCESS) {
885 + have_clock_get_time=yes
886 + AC_MSG_RESULT([yes])
888 + AC_MSG_RESULT([no])
892 + if test "$have_clock_get_time" = "yes"; then
893 + AC_DEFINE([HAVE_CLOCK_GET_TIME], 1, [do we have clock_get_time?])
897 +AC_DEFUN([AC_FPM_TRACE],
900 + have_broken_ptrace=no
902 + AC_MSG_CHECKING([for ptrace])
905 + #include <sys/types.h>
906 + #include <sys/ptrace.h> ], [ptrace(0, 0, (void *) 0, 0);], [
908 + AC_MSG_RESULT([yes])
910 + AC_MSG_RESULT([no])
913 + if test "$have_ptrace" = "yes"; then
914 + AC_MSG_CHECKING([whether ptrace works])
917 + #include <unistd.h>
918 + #include <signal.h>
919 + #include <sys/wait.h>
920 + #include <sys/types.h>
921 + #include <sys/ptrace.h>
924 + #if !defined(PTRACE_ATTACH) && defined(PT_ATTACH)
925 + #define PTRACE_ATTACH PT_ATTACH
928 + #if !defined(PTRACE_DETACH) && defined(PT_DETACH)
929 + #define PTRACE_DETACH PT_DETACH
932 + #if !defined(PTRACE_PEEKDATA) && defined(PT_READ_D)
933 + #define PTRACE_PEEKDATA PT_READ_D
938 + long v1 = (unsigned int) -1; /* copy will fail if sizeof(long) == 8 and we've got "int ptrace()" */
943 + if ( (child = fork()) ) { /* parent */
946 + if (0 > ptrace(PTRACE_ATTACH, child, 0, 0)) {
950 + waitpid(child, &status, 0);
953 + struct ptrace_io_desc ptio = {
954 + .piod_op = PIOD_READ_D,
957 + .piod_len = sizeof(v1)
960 + if (0 > ptrace(PT_IO, child, (void *) &ptio, 0)) {
966 + v2 = ptrace(PTRACE_PEEKDATA, child, (void *) &v1, 0);
972 + ptrace(PTRACE_DETACH, child, (void *) 1, 0);
974 + kill(child, SIGKILL);
976 + return ret ? ret : (v1 != v2);
984 + AC_MSG_RESULT([yes])
987 + have_broken_ptrace=yes
988 + AC_MSG_RESULT([no])
992 + if test "$have_ptrace" = "yes"; then
993 + AC_DEFINE([HAVE_PTRACE], 1, [do we have ptrace?])
996 + have_mach_vm_read=no
998 + if test "$have_broken_ptrace" = "yes"; then
999 + AC_MSG_CHECKING([for mach_vm_read])
1001 + AC_TRY_COMPILE([ #include <mach/mach.h>
1002 + #include <mach/mach_vm.h>
1004 + 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);
1006 + have_mach_vm_read=yes
1007 + AC_MSG_RESULT([yes])
1009 + AC_MSG_RESULT([no])
1013 + if test "$have_mach_vm_read" = "yes"; then
1014 + AC_DEFINE([HAVE_MACH_VM_READ], 1, [do we have mach_vm_read?])
1019 + if test -r /proc/$$/mem ; then
1020 + proc_mem_file="mem"
1022 + if test -r /proc/$$/as ; then
1023 + proc_mem_file="as"
1027 + if test -n "$proc_mem_file" ; then
1028 + AC_MSG_CHECKING([for proc mem file])
1031 + #define _GNU_SOURCE
1032 + #define _FILE_OFFSET_BITS 64
1033 + #include <stdint.h>
1034 + #include <unistd.h>
1035 + #include <sys/types.h>
1036 + #include <sys/stat.h>
1037 + #include <fcntl.h>
1038 + #include <stdio.h>
1041 + long v1 = (unsigned int) -1, v2 = 0;
1044 + sprintf(buf, "/proc/%d/$proc_mem_file", getpid());
1045 + fd = open(buf, O_RDONLY);
1049 + if (sizeof(long) != pread(fd, &v2, sizeof(long), (uintptr_t) &v1)) {
1057 + AC_MSG_RESULT([$proc_mem_file])
1060 + AC_MSG_RESULT([no])
1064 + if test -n "$proc_mem_file"; then
1065 + AC_DEFINE_UNQUOTED([PROC_MEM_FILE], "$proc_mem_file", [/proc/pid/mem interface])
1070 + if test "$have_ptrace" = "yes"; then
1071 + FPM_TRACE_SRCS="fpm_trace_ptrace.c"
1072 + elif test -n "$proc_mem_file"; then
1073 + FPM_TRACE_SRCS="fpm_trace_pread.c"
1074 + elif test "$have_mach_vm_read" = "yes" ; then
1075 + FPM_TRACE_SRCS="fpm_trace_mach.c"
1078 + if test -n "$FPM_TRACE_SRCS"; then
1079 + FPM_TRACE_SRCS="fpm_trace.c $FPM_TRACE_SRCS"
1084 +AC_DEFUN([AC_FPM_PRCTL],
1086 + AC_MSG_CHECKING([for prctl])
1088 + AC_TRY_COMPILE([ #include <sys/prctl.h> ], [prctl(0, 0, 0, 0, 0);], [
1089 + AC_DEFINE([HAVE_PRCTL], 1, [do we have prctl?])
1090 + AC_MSG_RESULT([yes])
1092 + AC_MSG_RESULT([no])
1095 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
1096 --- php-5.2.6.vanilla/sapi/cgi/fpm/conf/php-fpm.conf.in 1970-01-01 03:00:00.000000000 +0300
1097 +++ php-5.2.6.fpm/sapi/cgi/fpm/conf/php-fpm.conf.in 2008-05-23 00:53:15.000000000 +0400
1099 +<?xml version="1.0" ?>
1102 + All relative paths in this config are relative to php's install prefix
1104 + <section name="global_options">
1107 + <value name="pid_file">@php_fpm_pid_path@</value>
1110 + <value name="error_log">@php_fpm_log_path@</value>
1113 + <value name="log_level">notice</value>
1115 + When this amount of php processes exited with SIGSEGV or SIGBUS ...
1116 + <value name="emergency_restart_threshold">10</value>
1118 + ... in a less than this interval of time, a graceful restart will be initiated.
1119 + Useful to work around accidental curruptions in accelerator's shared memory.
1120 + <value name="emergency_restart_interval">1m</value>
1122 + Time limit on waiting child's reaction on signals from master
1123 + <value name="process_control_timeout">5s</value>
1125 + Set to 'no' to debug fpm
1126 + <value name="daemonize">yes</value>
1132 + <section name="pool">
1134 + Name of pool. Used in logs and stats.
1135 + <value name="name">default</value>
1137 + Address to accept fastcgi requests on.
1138 + Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
1139 + <value name="listen_address">127.0.0.1:9000</value>
1141 + <value name="listen_options">
1143 + Set listen(2) backlog
1144 + <value name="backlog">-1</value>
1146 + Set permissions for unix socket, if one used.
1147 + In Linux read/write permissions must be set in order to allow connections from web server.
1148 + Many BSD-derrived systems allow connections regardless of permissions.
1149 + <value name="owner"></value>
1150 + <value name="group"></value>
1151 + <value name="mode">0666</value>
1154 + Additional php.ini defines, specific to this pool of workers.
1155 + <value name="php_defines">
1156 + <!-- <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> -->
1157 + <!-- <value name="display_errors">0</value> -->
1160 + Unix user of processes
1161 + <!-- <value name="user">nobody</value> -->
1163 + Unix group of processes
1164 + <!-- <value name="group">@php_fpm_group@</value> -->
1166 + Process manager settings
1169 + Sets style of controling worker process count.
1170 + Valid values are 'static' and 'apache-like'
1171 + <value name="style">static</value>
1173 + Sets the limit on the number of simultaneous requests that will be served.
1174 + Equivalent to Apache MaxClients directive.
1175 + Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
1176 + Used with any pm_style.
1177 + <value name="max_children">5</value>
1179 + Settings group for 'apache-like' pm style
1180 + <value name="apache_like">
1182 + Sets the number of server processes created on startup.
1183 + Used only when 'apache-like' pm_style is selected
1184 + <value name="StartServers">20</value>
1186 + Sets the desired minimum number of idle server processes.
1187 + Used only when 'apache-like' pm_style is selected
1188 + <value name="MinSpareServers">5</value>
1190 + Sets the desired maximum number of idle server processes.
1191 + Used only when 'apache-like' pm_style is selected
1192 + <value name="MaxSpareServers">35</value>
1198 + The timeout (in seconds) for serving a single request after which the worker process will be terminated
1199 + Should be used when 'max_execution_time' ini option does not stop script execution for some reason
1201 + <value name="request_terminate_timeout">0s</value>
1203 + The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
1205 + <value name="request_slowlog_timeout">0s</value>
1207 + The log file for slow requests
1208 + <value name="slowlog">logs/slow.log</value>
1210 + Set open file desc rlimit
1211 + <value name="rlimit_files">1024</value>
1213 + Set max core size rlimit
1214 + <value name="rlimit_core">0</value>
1216 + Chroot to this directory at the start, absolute path
1217 + <value name="chroot"></value>
1219 + Chdir to this directory at the start, absolute path
1220 + <value name="chdir"></value>
1222 + Redirect workers' stdout and stderr into main error log.
1223 + If not set, they will be redirected to /dev/null, according to FastCGI specs
1224 + <value name="catch_workers_output">yes</value>
1226 + How much requests each process should execute before respawn.
1227 + Useful to work around memory leaks in 3rd party libraries.
1228 + For endless request processing please specify 0
1229 + Equivalent to PHP_FCGI_MAX_REQUESTS
1230 + <value name="max_requests">500</value>
1232 + Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
1233 + Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
1234 + Makes sense only with AF_INET listening socket.
1235 + <value name="allowed_clients">127.0.0.1</value>
1237 + Pass environment variables like LD_LIBRARY_PATH
1238 + All $VARIABLEs are taken from current environment
1239 + <value name="environment">
1240 + <value name="HOSTNAME">$HOSTNAME</value>
1241 + <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
1242 + <value name="TMP">/tmp</value>
1243 + <value name="TMPDIR">/tmp</value>
1244 + <value name="TEMP">/tmp</value>
1245 + <value name="OSTYPE">$OSTYPE</value>
1246 + <value name="MACHTYPE">$MACHTYPE</value>
1247 + <value name="MALLOC_CHECK_">2</value>
1255 --- php-5.2.6.fpm/sapi/cgi/fpm/config.m4 2008-09-19 05:22:37.000000000 +0400
1256 +++ php-5.2.6.fpm/sapi/cgi/fpm/config.m4 2008-09-19 05:22:37.000000000 +0400
1259 +FPM_VERSION="0.5.9"
1261 +PHP_ARG_WITH(fpm-conf, for php-fpm config file path,
1262 +[ --with-fpm-conf=PATH Set the path for php-fpm configuration file [PREFIX/etc/php-fpm.conf]], \$prefix/etc/php-fpm.conf, no)
1264 +PHP_ARG_WITH(fpm-log, for php-fpm log file path,
1265 +[ --with-fpm-log=PATH Set the path for php-fpm log file [PREFIX/logs/php-fpm.log]], \$prefix/logs/php-fpm.log, no)
1267 +PHP_ARG_WITH(fpm-pid, for php-fpm pid file path,
1268 +[ --with-fpm-pid=PATH Set the path for php-fpm pid file [PREFIX/logs/php-fpm.pid]], \$prefix/logs/php-fpm.pid, no)
1270 +dnl AC_FPM_LIBEVENT
1278 +LIBEVENT_LIBS="-levent"
1280 +SAPI_EXTRA_DEPS="$LIBEVENT_LIBS"
1282 +FPM_SOURCES="fpm.c \
1286 + fpm_worker_pool.c \
1296 + fpm_process_ctl.c \
1304 +FPM_CFLAGS="$LIBEVENT_CFLAGS $LIBXML_CFLAGS $JUDY_CFLAGS"
1306 +dnl FPM_CFLAGS="$FPM_CFLAGS -DJUDYERROR_NOTEST" # for Judy
1307 +FPM_CFLAGS="$FPM_CFLAGS -I$abs_srcdir/sapi/cgi" # for fastcgi.h
1309 +if test "$ICC" = "yes" ; then
1310 + FPM_ADD_CFLAGS="-Wall -wd279,310,869,810,981"
1311 +elif test "$GCC" = "yes" ; then
1312 + FPM_ADD_CFLAGS="-Wall -Wpointer-arith -Wno-unused-parameter -Wunused-variable -Wunused-value -fno-strict-aliasing"
1315 +if test -n "$FPM_WERROR" ; then
1316 + FPM_ADD_CFLAGS="$FPM_ADD_CFLAGS -Werror"
1319 +FPM_CFLAGS="$FPM_ADD_CFLAGS $FPM_CFLAGS"
1321 +PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/sapi/cgi/fpm/Makefile.frag)
1323 +PHP_ADD_SOURCES(sapi/cgi/fpm, $FPM_SOURCES, $FPM_CFLAGS, sapi)
1325 +PHP_ADD_BUILD_DIR(sapi/cgi/fpm)
1327 +install_fpm="install-fpm"
1329 +SAPI_EXTRA_LIBS="$LIBEVENT_LIBS $LIBXML_LIBS $JUDY_LIBS"
1332 +if test "$prefix" = "NONE" ; then
1333 + fpm_prefix=/usr/local
1335 + fpm_prefix="$prefix"
1338 +if test "$PHP_FPM_CONF" = "\$prefix/etc/php-fpm.conf" ; then
1339 + php_fpm_conf_path="$fpm_prefix/etc/php-fpm.conf"
1341 + php_fpm_conf_path="$PHP_FPM_CONF"
1344 +if test "$PHP_FPM_LOG" = "\$prefix/logs/php-fpm.log" ; then
1345 + php_fpm_log_path="$fpm_prefix/logs/php-fpm.log"
1347 + php_fpm_log_path="$PHP_FPM_LOG"
1350 +if test "$PHP_FPM_PID" = "\$prefix/logs/php-fpm.pid" ; then
1351 + php_fpm_pid_path="$fpm_prefix/logs/php-fpm.pid"
1353 + php_fpm_pid_path="$PHP_FPM_PID"
1357 +if grep nobody /etc/group >/dev/null 2>&1; then
1358 + php_fpm_group=nobody
1360 + if grep nogroup /etc/group >/dev/null 2>&1; then
1361 + php_fpm_group=nogroup
1363 + php_fpm_group=nobody
1367 +PHP_SUBST_OLD(php_fpm_conf_path)
1368 +PHP_SUBST_OLD(php_fpm_log_path)
1369 +PHP_SUBST_OLD(php_fpm_pid_path)
1370 +PHP_SUBST_OLD(php_fpm_group)
1371 +PHP_SUBST_OLD(FPM_VERSION)
1373 +PHP_OUTPUT(sapi/cgi/fpm/fpm_autoconf.h)
1374 +PHP_OUTPUT(sapi/cgi/fpm/php-fpm.conf:sapi/cgi/fpm/conf/php-fpm.conf.in)
1375 +PHP_OUTPUT(sapi/cgi/fpm/php-fpm:sapi/cgi/fpm/init.d/php-fpm.in)
1376 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_arrays.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_arrays.h
1377 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_arrays.h 1970-01-01 03:00:00.000000000 +0300
1378 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_arrays.h 2008-05-24 21:38:47.000000000 +0400
1382 + /* (c) 2007,2008 Andrei Nigmatulin */
1384 +#ifndef FPM_ARRAYS_H
1385 +#define FPM_ARRAYS_H 1
1387 +#include <stdlib.h>
1388 +#include <string.h>
1390 +struct fpm_array_s {
1397 +static inline struct fpm_array_s *fpm_array_init(struct fpm_array_s *a, unsigned int sz, unsigned int initial_num)
1399 + void *allocated = 0;
1402 + a = malloc(sizeof(struct fpm_array_s));
1413 + a->data = calloc(sz, initial_num);
1420 + a->allocated = initial_num;
1426 +static inline void *fpm_array_item(struct fpm_array_s *a, unsigned int n)
1430 + ret = (char *) a->data + a->sz * n;
1435 +static inline void *fpm_array_item_last(struct fpm_array_s *a)
1437 + return fpm_array_item(a, a->used - 1);
1440 +static inline int fpm_array_item_remove(struct fpm_array_s *a, unsigned int n)
1444 + if (n < a->used - 1) {
1445 + void *last = fpm_array_item(a, a->used - 1);
1446 + void *to_remove = fpm_array_item(a, n);
1448 + memcpy(to_remove, last, a->sz);
1458 +static inline void *fpm_array_push(struct fpm_array_s *a)
1462 + if (a->used == a->allocated) {
1463 + size_t new_allocated = a->allocated ? a->allocated * 2 : 20;
1464 + void *new_ptr = realloc(a->data, a->sz * new_allocated);
1470 + a->data = new_ptr;
1471 + a->allocated = new_allocated;
1474 + ret = fpm_array_item(a, a->used);
1481 +static inline void fpm_array_free(struct fpm_array_s *a)
1486 + a->used = a->allocated = 0;
1490 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_atomic.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_atomic.h
1491 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_atomic.h 1970-01-01 03:00:00.000000000 +0300
1492 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_atomic.h 2008-09-19 03:34:11.000000000 +0400
1496 + /* (c) 2007,2008 Andrei Nigmatulin */
1498 +#ifndef FPM_ATOMIC_H
1499 +#define FPM_ATOMIC_H 1
1501 +#include <stdint.h>
1504 +#if ( __i386__ || __i386 )
1506 +typedef int32_t atomic_int_t;
1507 +typedef uint32_t atomic_uint_t;
1508 +typedef volatile atomic_uint_t atomic_t;
1511 +static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
1513 + __asm__ volatile ( "lock;" "xaddl %0, %1;" :
1514 + "+r" (add) : "m" (*value) : "memory");
1519 +static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
1521 + unsigned char res;
1523 + __asm__ volatile ( "lock;" "cmpxchgl %3, %1;" "sete %0;" :
1524 + "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
1529 +#elif ( __amd64__ || __amd64 )
1531 +typedef int64_t atomic_int_t;
1532 +typedef uint64_t atomic_uint_t;
1533 +typedef volatile atomic_uint_t atomic_t;
1535 +static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
1537 + __asm__ volatile ( "lock;" "xaddq %0, %1;" :
1538 + "+r" (add) : "m" (*value) : "memory");
1543 +static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
1545 + unsigned char res;
1547 + __asm__ volatile ( "lock;" "cmpxchgq %3, %1;" "sete %0;" :
1548 + "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
1555 +#error unsupported processor. please write a patch and send it to me
1559 +static inline int fpm_spinlock(atomic_t *lock, int try_once)
1562 + return atomic_cmp_set(lock, 0, 1) ? 0 : -1;
1567 + if (atomic_cmp_set(lock, 0, 1)) {
1579 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
1580 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_autoconf.h.in 1970-01-01 03:00:00.000000000 +0300
1581 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_autoconf.h.in 2008-05-24 21:38:47.000000000 +0400
1585 + /* (c) 2007,2008 Andrei Nigmatulin */
1587 +#define PHP_FPM_VERSION "@FPM_VERSION@"
1588 +#define PHP_FPM_CONF_PATH "@php_fpm_conf_path@"
1589 +#define PHP_FPM_LOG_PATH "@php_fpm_log_path@"
1590 +#define PHP_FPM_PID_PATH "@php_fpm_pid_path@"
1592 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm.c php-5.2.6.fpm/sapi/cgi/fpm/fpm.c
1593 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm.c 1970-01-01 03:00:00.000000000 +0300
1594 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm.c 2008-07-20 20:38:31.000000000 +0400
1598 + /* (c) 2007,2008 Andrei Nigmatulin */
1600 +#include "fpm_config.h"
1602 +#include <stdlib.h> /* for exit */
1605 +#include "fpm_children.h"
1606 +#include "fpm_signals.h"
1607 +#include "fpm_env.h"
1608 +#include "fpm_events.h"
1609 +#include "fpm_cleanup.h"
1610 +#include "fpm_php.h"
1611 +#include "fpm_sockets.h"
1612 +#include "fpm_unix.h"
1613 +#include "fpm_process_ctl.h"
1614 +#include "fpm_conf.h"
1615 +#include "fpm_worker_pool.h"
1616 +#include "fpm_stdio.h"
1621 +struct fpm_globals_s fpm_globals;
1623 +int fpm_init(int argc, char **argv, char *config)
1625 + fpm_globals.argc = argc;
1626 + fpm_globals.argv = argv;
1627 + fpm_globals.config = config;
1629 + if (0 > fpm_php_init_main() ||
1630 + 0 > fpm_stdio_init_main() ||
1631 + 0 > fpm_conf_init_main() ||
1632 + 0 > fpm_unix_init_main() ||
1633 + 0 > fpm_env_init_main() ||
1634 + 0 > fpm_signals_init_main() ||
1635 + 0 > fpm_pctl_init_main() ||
1636 + 0 > fpm_children_init_main() ||
1637 + 0 > fpm_sockets_init_main() ||
1638 + 0 > fpm_worker_pool_init_main() ||
1639 + 0 > fpm_event_init_main()) {
1643 + if (0 > fpm_conf_write_pid()) {
1647 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "fpm is running, pid %d", (int) fpm_globals.parent_pid);
1652 +/* children: return listening socket
1653 + parent: never return */
1654 +int fpm_run(int *max_requests)
1656 + struct fpm_worker_pool_s *wp;
1658 + /* create initial children in all pools */
1659 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1662 + is_parent = fpm_children_create_initial(wp);
1669 + /* run event loop forever */
1672 +run_child: /* only workers reach this point */
1674 + fpm_cleanups_run(FPM_CLEANUP_CHILD);
1676 + *max_requests = fpm_globals.max_requests;
1677 + return fpm_globals.listening_socket;
1680 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_children.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_children.c
1681 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_children.c 1970-01-01 03:00:00.000000000 +0300
1682 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_children.c 2008-08-26 19:09:15.000000000 +0400
1686 + /* (c) 2007,2008 Andrei Nigmatulin */
1688 +#include "fpm_config.h"
1690 +#include <sys/types.h>
1691 +#include <sys/wait.h>
1693 +#include <unistd.h>
1694 +#include <string.h>
1698 +#include "fpm_children.h"
1699 +#include "fpm_signals.h"
1700 +#include "fpm_worker_pool.h"
1701 +#include "fpm_sockets.h"
1702 +#include "fpm_process_ctl.h"
1703 +#include "fpm_php.h"
1704 +#include "fpm_conf.h"
1705 +#include "fpm_cleanup.h"
1706 +#include "fpm_events.h"
1707 +#include "fpm_clock.h"
1708 +#include "fpm_stdio.h"
1709 +#include "fpm_unix.h"
1710 +#include "fpm_env.h"
1711 +#include "fpm_shm_slots.h"
1715 +static time_t *last_faults;
1718 +static int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop);
1720 +static void fpm_children_cleanup(int which, void *arg)
1722 + free(last_faults);
1725 +static struct fpm_child_s *fpm_child_alloc()
1727 + struct fpm_child_s *ret;
1729 + ret = malloc(sizeof(struct fpm_child_s));
1731 + if (!ret) return 0;
1733 + memset(ret, 0, sizeof(*ret));
1738 +static void fpm_child_free(struct fpm_child_s *child)
1743 +static void fpm_child_close(struct fpm_child_s *child, int in_event_loop)
1745 + if (child->fd_stdout != -1) {
1746 + if (in_event_loop) {
1747 + fpm_event_fire(&child->ev_stdout);
1749 + if (child->fd_stdout != -1) {
1750 + close(child->fd_stdout);
1754 + if (child->fd_stderr != -1) {
1755 + if (in_event_loop) {
1756 + fpm_event_fire(&child->ev_stderr);
1758 + if (child->fd_stderr != -1) {
1759 + close(child->fd_stderr);
1763 + fpm_child_free(child);
1766 +static void fpm_child_link(struct fpm_child_s *child)
1768 + struct fpm_worker_pool_s *wp = child->wp;
1770 + ++wp->running_children;
1771 + ++fpm_globals.running_children;
1773 + child->next = wp->children;
1774 + if (child->next) child->next->prev = child;
1776 + wp->children = child;
1779 +static void fpm_child_unlink(struct fpm_child_s *child)
1781 + --child->wp->running_children;
1782 + --fpm_globals.running_children;
1784 + if (child->prev) child->prev->next = child->next;
1785 + else child->wp->children = child->next;
1786 + if (child->next) child->next->prev = child->prev;
1790 +static struct fpm_child_s *fpm_child_find(pid_t pid)
1792 + struct fpm_worker_pool_s *wp;
1793 + struct fpm_child_s *child = 0;
1795 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1797 + for (child = wp->children; child; child = child->next) {
1798 + if (child->pid == pid) {
1813 +static void fpm_child_init(struct fpm_worker_pool_s *wp)
1815 + fpm_globals.max_requests = wp->config->max_requests;
1817 + if (0 > fpm_stdio_init_child(wp) ||
1818 + 0 > fpm_unix_init_child(wp) ||
1819 + 0 > fpm_signals_init_child() ||
1820 + 0 > fpm_env_init_child(wp) ||
1821 + 0 > fpm_php_init_child(wp)) {
1823 + zlog(ZLOG_STUFF, ZLOG_ERROR, "child failed to initialize (pool %s)", wp->config->name);
1828 +int fpm_children_free(struct fpm_child_s *child)
1830 + struct fpm_child_s *next;
1832 + for (; child; child = next) {
1833 + next = child->next;
1834 + fpm_child_close(child, 0 /* in_event_loop */);
1840 +void fpm_children_bury()
1844 + struct fpm_child_s *child;
1846 + while ( (pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
1848 + int severity = ZLOG_NOTICE;
1850 + child = fpm_child_find(pid);
1852 + if (WIFEXITED(status)) {
1854 + snprintf(buf, sizeof(buf), "with code %d", WEXITSTATUS(status));
1856 + if (WEXITSTATUS(status) != 0) {
1857 + severity = ZLOG_WARNING;
1861 + else if (WIFSIGNALED(status)) {
1862 + const char *signame = fpm_signal_names[WTERMSIG(status)];
1863 + const char *have_core = WCOREDUMP(status) ? " (core dumped)" : "";
1865 + if (signame == NULL) {
1869 + snprintf(buf, sizeof(buf), "on signal %d %s%s", WTERMSIG(status), signame, have_core);
1871 + if (WTERMSIG(status) != SIGQUIT) { /* possible request loss */
1872 + severity = ZLOG_WARNING;
1875 + else if (WIFSTOPPED(status)) {
1877 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "child %d stopped for tracing", (int) pid);
1879 + if (child && child->tracer) {
1880 + child->tracer(child);
1887 + struct fpm_worker_pool_s *wp = child->wp;
1888 + struct timeval tv1, tv2;
1890 + fpm_child_unlink(child);
1892 + fpm_shm_slots_discard_slot(child);
1894 + fpm_clock_get(&tv1);
1896 + timersub(&tv1, &child->started, &tv2);
1898 + zlog(ZLOG_STUFF, severity, "child %d (pool %s) exited %s after %ld.%06d seconds from start", (int) pid,
1899 + child->wp->config->name, buf, tv2.tv_sec, (int) tv2.tv_usec);
1901 + fpm_child_close(child, 1 /* in event_loop */);
1903 + fpm_pctl_child_exited();
1905 + if (last_faults && (WTERMSIG(status) == SIGSEGV || WTERMSIG(status) == SIGBUS)) {
1906 + time_t now = tv1.tv_sec;
1907 + int restart_condition = 1;
1910 + last_faults[fault++] = now;
1912 + if (fault == fpm_global_options.emergency_restart_threshold) {
1916 + for (i = 0; i < fpm_global_options.emergency_restart_threshold; i++) {
1917 + if (now - last_faults[i] > fpm_global_options.emergency_restart_interval) {
1918 + restart_condition = 0;
1923 + if (restart_condition) {
1925 + zlog(ZLOG_STUFF, ZLOG_WARNING, "failed processes threshold (%d in %d sec) is reached, initiating reload",
1926 + fpm_global_options.emergency_restart_threshold, fpm_global_options.emergency_restart_interval);
1928 + fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
1932 + fpm_children_make(wp, 1 /* in event loop */);
1934 + if (fpm_globals.is_child) {
1939 + zlog(ZLOG_STUFF, ZLOG_ALERT, "oops, unknown child exited %s", buf);
1945 +static struct fpm_child_s *fpm_resources_prepare(struct fpm_worker_pool_s *wp)
1947 + struct fpm_child_s *c;
1949 + c = fpm_child_alloc();
1952 + zlog(ZLOG_STUFF, ZLOG_ERROR, "malloc failed (pool %s)", wp->config->name);
1957 + c->fd_stdout = -1; c->fd_stderr = -1;
1959 + if (0 > fpm_stdio_prepare_pipes(c)) {
1960 + fpm_child_free(c);
1964 + if (0 > fpm_shm_slots_prepare_slot(c)) {
1965 + fpm_stdio_discard_pipes(c);
1966 + fpm_child_free(c);
1973 +static void fpm_resources_discard(struct fpm_child_s *child)
1975 + fpm_shm_slots_discard_slot(child);
1976 + fpm_stdio_discard_pipes(child);
1977 + fpm_child_free(child);
1980 +static void fpm_child_resources_use(struct fpm_child_s *child)
1982 + fpm_shm_slots_child_use_slot(child);
1983 + fpm_stdio_child_use_pipes(child);
1984 + fpm_child_free(child);
1987 +static void fpm_parent_resources_use(struct fpm_child_s *child)
1989 + fpm_shm_slots_parent_use_slot(child);
1990 + fpm_stdio_parent_use_pipes(child);
1991 + fpm_child_link(child);
1994 +static int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop)
1998 + struct fpm_child_s *child;
2000 + while (!enough && fpm_pctl_can_spawn_children() && wp->running_children < wp->config->pm->max_children) {
2002 + child = fpm_resources_prepare(wp);
2014 + fpm_child_resources_use(child);
2015 + fpm_globals.is_child = 1;
2016 + if (in_event_loop) {
2017 + fpm_event_exit_loop();
2019 + fpm_child_init(wp);
2023 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fork() failed");
2026 + fpm_resources_discard(child);
2028 + break; /* dont try any more on error */
2032 + fpm_clock_get(&child->started);
2033 + fpm_parent_resources_use(child);
2035 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "child %d (pool %s) started", (int) pid, wp->config->name);
2040 + return 1; /* we are done */
2043 +int fpm_children_create_initial(struct fpm_worker_pool_s *wp)
2045 + return fpm_children_make(wp, 0 /* not in event loop yet */);
2048 +int fpm_children_init_main()
2050 + if (fpm_global_options.emergency_restart_threshold &&
2051 + fpm_global_options.emergency_restart_interval) {
2053 + last_faults = malloc(sizeof(time_t) * fpm_global_options.emergency_restart_threshold);
2055 + if (!last_faults) {
2059 + memset(last_faults, 0, sizeof(time_t) * fpm_global_options.emergency_restart_threshold);
2062 + fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_children_cleanup, 0);
2067 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_children.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_children.h
2068 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_children.h 1970-01-01 03:00:00.000000000 +0300
2069 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_children.h 2008-05-24 21:38:47.000000000 +0400
2073 + /* (c) 2007,2008 Andrei Nigmatulin */
2075 +#ifndef FPM_CHILDREN_H
2076 +#define FPM_CHILDREN_H 1
2078 +#include <sys/time.h>
2079 +#include <sys/types.h>
2082 +#include "fpm_worker_pool.h"
2084 +int fpm_children_create_initial(struct fpm_worker_pool_s *wp);
2085 +int fpm_children_free(struct fpm_child_s *child);
2086 +void fpm_children_bury();
2087 +int fpm_children_init_main();
2089 +struct fpm_child_s;
2091 +struct fpm_child_s {
2092 + struct fpm_child_s *prev, *next;
2093 + struct timeval started;
2094 + struct fpm_worker_pool_s *wp;
2095 + struct event ev_stdout, ev_stderr;
2097 + int fd_stdout, fd_stderr;
2098 + void (*tracer)(struct fpm_child_s *);
2099 + struct timeval slow_logged;
2104 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_cleanup.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_cleanup.c
2105 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_cleanup.c 1970-01-01 03:00:00.000000000 +0300
2106 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_cleanup.c 2008-05-24 21:38:47.000000000 +0400
2110 + /* (c) 2007,2008 Andrei Nigmatulin */
2112 +#include "fpm_config.h"
2114 +#include <stdlib.h>
2116 +#include "fpm_arrays.h"
2117 +#include "fpm_cleanup.h"
2122 + void (*cleanup)(int, void *);
2126 +static struct fpm_array_s cleanups = { .sz = sizeof(struct cleanup_s) };
2128 +int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *arg)
2130 + struct cleanup_s *c;
2132 + c = fpm_array_push(&cleanups);
2139 + c->cleanup = cleanup;
2145 +void fpm_cleanups_run(int type)
2147 + struct cleanup_s *c = fpm_array_item_last(&cleanups);
2148 + int cl = cleanups.used;
2150 + for ( ; cl--; c--) {
2151 + if (c->type & type) {
2152 + c->cleanup(type, c->arg);
2156 + fpm_array_free(&cleanups);
2159 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_cleanup.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_cleanup.h
2160 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_cleanup.h 1970-01-01 03:00:00.000000000 +0300
2161 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_cleanup.h 2008-05-24 21:38:47.000000000 +0400
2165 + /* (c) 2007,2008 Andrei Nigmatulin */
2167 +#ifndef FPM_CLEANUP_H
2168 +#define FPM_CLEANUP_H 1
2170 +int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *);
2171 +void fpm_cleanups_run(int type);
2174 + FPM_CLEANUP_CHILD = (1 << 0),
2175 + FPM_CLEANUP_PARENT_EXIT = (1 << 1),
2176 + FPM_CLEANUP_PARENT_EXIT_MAIN = (1 << 2),
2177 + FPM_CLEANUP_PARENT_EXEC = (1 << 3),
2178 + FPM_CLEANUP_PARENT = (1 << 1) | (1 << 2) | (1 << 3),
2179 + FPM_CLEANUP_ALL = ~0,
2184 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_clock.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_clock.c
2185 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_clock.c 1970-01-01 03:00:00.000000000 +0300
2186 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_clock.c 2008-09-19 03:19:59.000000000 +0400
2190 + /* (c) 2007,2008 Andrei Nigmatulin */
2192 +#include "fpm_config.h"
2194 +#if defined(HAVE_CLOCK_GETTIME)
2195 +#include <time.h> /* for CLOCK_MONOTONIC */
2198 +#include "fpm_clock.h"
2202 +/* posix monotonic clock - preferred source of time */
2203 +#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
2205 +static int monotonic_works;
2207 +int fpm_clock_init()
2209 + struct timespec ts;
2211 + monotonic_works = 0;
2213 + if (0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
2214 + monotonic_works = 1;
2220 +int fpm_clock_get(struct timeval *tv)
2222 + if (monotonic_works) {
2223 + struct timespec ts;
2225 + if (0 > clock_gettime(CLOCK_MONOTONIC, &ts)) {
2226 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "clock_gettime() failed");
2230 + tv->tv_sec = ts.tv_sec;
2231 + tv->tv_usec = ts.tv_nsec / 1000;
2235 + return gettimeofday(tv, 0);
2239 +#elif defined(HAVE_CLOCK_GET_TIME)
2241 +#include <mach/mach.h>
2242 +#include <mach/clock.h>
2243 +#include <mach/mach_error.h>
2245 +static clock_serv_t mach_clock;
2247 +/* this code borrowed from here: http://lists.apple.com/archives/Darwin-development/2002/Mar/msg00746.html */
2248 +/* mach_clock also should be re-initialized in child process after fork */
2249 +int fpm_clock_init()
2251 + kern_return_t ret;
2252 + mach_timespec_t aTime;
2254 + ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &mach_clock);
2256 + if (ret != KERN_SUCCESS) {
2257 + zlog(ZLOG_STUFF, ZLOG_ERROR, "host_get_clock_service() failed: %s", mach_error_string(ret));
2261 + /* test if it works */
2262 + ret = clock_get_time(mach_clock, &aTime);
2264 + if (ret != KERN_SUCCESS) {
2265 + zlog(ZLOG_STUFF, ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
2272 +int fpm_clock_get(struct timeval *tv)
2274 + kern_return_t ret;
2275 + mach_timespec_t aTime;
2277 + ret = clock_get_time(mach_clock, &aTime);
2279 + if (ret != KERN_SUCCESS) {
2280 + zlog(ZLOG_STUFF, ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
2284 + tv->tv_sec = aTime.tv_sec;
2285 + tv->tv_usec = aTime.tv_nsec / 1000;
2290 +#else /* no clock */
2292 +int fpm_clock_init()
2297 +int fpm_clock_get(struct timeval *tv)
2299 + return gettimeofday(tv, 0);
2303 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_clock.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_clock.h
2304 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_clock.h 1970-01-01 03:00:00.000000000 +0300
2305 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_clock.h 2008-05-24 21:38:47.000000000 +0400
2309 + /* (c) 2007,2008 Andrei Nigmatulin */
2311 +#ifndef FPM_CLOCK_H
2312 +#define FPM_CLOCK_H 1
2314 +#include <sys/time.h>
2316 +int fpm_clock_init();
2317 +int fpm_clock_get(struct timeval *tv);
2320 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_conf.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_conf.c
2321 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_conf.c 1970-01-01 03:00:00.000000000 +0300
2322 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_conf.c 2008-09-19 04:54:15.000000000 +0400
2326 + /* (c) 2007,2008 Andrei Nigmatulin */
2328 +#include "fpm_config.h"
2330 +#include <sys/types.h>
2331 +#include <sys/stat.h>
2333 +#include <string.h>
2334 +#include <stdlib.h>
2335 +#include <stddef.h>
2336 +#include <stdint.h>
2338 +#include <unistd.h>
2341 +#include "fpm_conf.h"
2342 +#include "fpm_stdio.h"
2343 +#include "fpm_worker_pool.h"
2344 +#include "fpm_cleanup.h"
2345 +#include "fpm_php.h"
2346 +#include "fpm_sockets.h"
2347 +#include "xml_config.h"
2351 +struct fpm_options_s fpm_global_options;
2353 +static void *fpm_global_options_ptr()
2355 + return &fpm_global_options;
2358 +static char *fpm_conf_set_log_level(void **conf, char *name, void *vv, intptr_t offset)
2362 + if (!strcmp(value, "debug")) {
2363 + fpm_globals.log_level = ZLOG_DEBUG;
2365 + else if (!strcmp(value, "notice")) {
2366 + fpm_globals.log_level = ZLOG_NOTICE;
2368 + else if (!strcmp(value, "warn")) {
2369 + fpm_globals.log_level = ZLOG_WARNING;
2371 + else if (!strcmp(value, "error")) {
2372 + fpm_globals.log_level = ZLOG_ERROR;
2374 + else if (!strcmp(value, "alert")) {
2375 + fpm_globals.log_level = ZLOG_ALERT;
2378 + return "invalid value for 'log_level'";
2384 +static struct xml_conf_section xml_section_fpm_global_options = {
2385 + .conf = &fpm_global_options_ptr,
2386 + .path = "/configuration/global_options",
2387 + .parsers = (struct xml_value_parser []) {
2388 + { XML_CONF_SCALAR, "emergency_restart_threshold", &xml_conf_set_slot_integer, offsetof(struct fpm_options_s, emergency_restart_threshold) },
2389 + { XML_CONF_SCALAR, "emergency_restart_interval", &xml_conf_set_slot_time, offsetof(struct fpm_options_s, emergency_restart_interval) },
2390 + { XML_CONF_SCALAR, "process_control_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_options_s, process_control_timeout) },
2391 + { XML_CONF_SCALAR, "daemonize", &xml_conf_set_slot_boolean, offsetof(struct fpm_options_s, daemonize) },
2392 + { XML_CONF_SCALAR, "pid_file", &xml_conf_set_slot_string, offsetof(struct fpm_options_s, pid_file) },
2393 + { XML_CONF_SCALAR, "error_log", &xml_conf_set_slot_string, offsetof(struct fpm_options_s, error_log) },
2394 + { XML_CONF_SCALAR, "log_level", &fpm_conf_set_log_level, 0 },
2399 +static char *fpm_conf_set_pm_style(void **conf, char *name, void *vv, intptr_t offset)
2402 + struct fpm_pm_s *c = *conf;
2404 + if (!strcmp(value, "static")) {
2405 + c->style = PM_STYLE_STATIC;
2407 + else if (!strcmp(value, "apache-like")) {
2408 + c->style = PM_STYLE_APACHE_LIKE;
2411 + return "invalid value for 'style'";
2417 +static char *fpm_conf_set_rlimit_core(void **conf, char *name, void *vv, intptr_t offset)
2420 + struct fpm_worker_pool_config_s *c = *conf;
2422 + if (!strcmp(value, "unlimited")) {
2423 + c->rlimit_core = -1;
2427 + void *subconf = &int_value;
2430 + error = xml_conf_set_slot_integer(&subconf, name, vv, 0);
2432 + if (error) return error;
2434 + if (int_value < 0) return "invalid value for 'rlimit_core'";
2436 + c->rlimit_core = int_value;
2442 +static char *fpm_conf_set_catch_workers_output(void **conf, char *name, void *vv, intptr_t offset)
2444 + struct fpm_worker_pool_config_s *c = *conf;
2446 + void *subconf = &int_value;
2449 + error = xml_conf_set_slot_boolean(&subconf, name, vv, 0);
2451 + if (error) return error;
2453 + c->catch_workers_output = int_value;
2458 +static struct xml_conf_section fpm_conf_set_apache_like_subsection_conf = {
2459 + .path = "apache_like somewhere", /* fixme */
2460 + .parsers = (struct xml_value_parser []) {
2461 + { XML_CONF_SCALAR, "StartServers", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.StartServers) },
2462 + { XML_CONF_SCALAR, "MinSpareServers", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.MinSpareServers) },
2463 + { XML_CONF_SCALAR, "MaxSpareServers", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.MaxSpareServers) },
2468 +static char *fpm_conf_set_apache_like_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2470 + return xml_conf_parse_section(conf, &fpm_conf_set_apache_like_subsection_conf, xml_node);
2473 +static struct xml_conf_section fpm_conf_set_listen_options_subsection_conf = {
2474 + .path = "listen options somewhere", /* fixme */
2475 + .parsers = (struct xml_value_parser []) {
2476 + { XML_CONF_SCALAR, "backlog", &xml_conf_set_slot_integer, offsetof(struct fpm_listen_options_s, backlog) },
2477 + { XML_CONF_SCALAR, "owner", &xml_conf_set_slot_string, offsetof(struct fpm_listen_options_s, owner) },
2478 + { XML_CONF_SCALAR, "group", &xml_conf_set_slot_string, offsetof(struct fpm_listen_options_s, group) },
2479 + { XML_CONF_SCALAR, "mode", &xml_conf_set_slot_string, offsetof(struct fpm_listen_options_s, mode) },
2484 +static char *fpm_conf_set_listen_options_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2486 + void *subconf = (char *) *conf + offset;
2487 + struct fpm_listen_options_s *lo;
2489 + lo = malloc(sizeof(*lo));
2492 + return "malloc() failed";
2495 + memset(lo, 0, sizeof(*lo));
2499 + * (struct fpm_listen_options_s **) subconf = lo;
2503 + return xml_conf_parse_section(&subconf, &fpm_conf_set_listen_options_subsection_conf, xml_node);
2506 +static struct xml_conf_section fpm_conf_set_pm_subsection_conf = {
2507 + .path = "pm settings somewhere", /* fixme */
2508 + .parsers = (struct xml_value_parser []) {
2509 + { XML_CONF_SCALAR, "style", &fpm_conf_set_pm_style, 0 },
2510 + { XML_CONF_SCALAR, "max_children", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, max_children) },
2511 + { XML_CONF_SUBSECTION, "apache_like", &fpm_conf_set_apache_like_subsection, offsetof(struct fpm_pm_s, options_apache_like) },
2516 +static char *fpm_conf_set_pm_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2518 + void *subconf = (char *) *conf + offset;
2519 + struct fpm_pm_s *pm;
2521 + pm = malloc(sizeof(*pm));
2524 + return "fpm_conf_set_pm_subsection(): malloc failed";
2527 + memset(pm, 0, sizeof(*pm));
2529 + * (struct fpm_pm_s **) subconf = pm;
2533 + return xml_conf_parse_section(&subconf, &fpm_conf_set_pm_subsection_conf, xml_node);
2536 +static char *xml_conf_set_slot_key_value_pair(void **conf, char *name, void *vv, intptr_t offset)
2539 + struct key_value_s *kv;
2540 + struct key_value_s ***parent = (struct key_value_s ***) conf;
2542 + kv = malloc(sizeof(*kv));
2545 + return "malloc() failed";
2548 + memset(kv, 0, sizeof(*kv));
2550 + kv->key = strdup(name);
2551 + kv->value = strdup(value);
2553 + if (!kv->key || !kv->value) {
2554 + return "xml_conf_set_slot_key_value_pair(): strdup() failed";
2559 + *parent = &kv->next;
2564 +static struct xml_conf_section fpm_conf_set_key_value_pairs_subsection_conf = {
2565 + .path = "key_value_pairs somewhere", /* fixme */
2566 + .parsers = (struct xml_value_parser []) {
2567 + { XML_CONF_SCALAR, 0, &xml_conf_set_slot_key_value_pair, 0 },
2572 +static char *fpm_conf_set_key_value_pairs_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2574 + void *next_kv = (char *) *conf + offset;
2576 + return xml_conf_parse_section(&next_kv, &fpm_conf_set_key_value_pairs_subsection_conf, xml_node);
2579 +static void *fpm_worker_pool_config_alloc()
2581 + static struct fpm_worker_pool_s *current_wp = 0;
2582 + struct fpm_worker_pool_s *wp;
2584 + wp = fpm_worker_pool_alloc();
2586 + if (!wp) return 0;
2588 + wp->config = malloc(sizeof(struct fpm_worker_pool_config_s));
2590 + if (!wp->config) return 0;
2592 + memset(wp->config, 0, sizeof(struct fpm_worker_pool_config_s));
2594 + if (current_wp) current_wp->next = wp;
2598 + return wp->config;
2601 +int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc)
2603 + struct key_value_s *kv, *kv_next;
2606 + free(wpc->listen_address);
2607 + if (wpc->listen_options) {
2608 + free(wpc->listen_options->owner);
2609 + free(wpc->listen_options->group);
2610 + free(wpc->listen_options->mode);
2611 + free(wpc->listen_options);
2613 + for (kv = wpc->php_defines; kv; kv = kv_next) {
2614 + kv_next = kv->next;
2619 + for (kv = wpc->environment; kv; kv = kv_next) {
2620 + kv_next = kv->next;
2628 + free(wpc->chroot);
2630 + free(wpc->allowed_clients);
2631 + free(wpc->slowlog);
2636 +static struct xml_conf_section xml_section_fpm_worker_pool_config = {
2637 + .conf = &fpm_worker_pool_config_alloc,
2638 + .path = "/configuration/workers/pool",
2639 + .parsers = (struct xml_value_parser []) {
2640 + { XML_CONF_SCALAR, "name", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, name) },
2641 + { XML_CONF_SCALAR, "listen_address", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, listen_address) },
2642 + { XML_CONF_SUBSECTION, "listen_options", &fpm_conf_set_listen_options_subsection, offsetof(struct fpm_worker_pool_config_s, listen_options) },
2643 + { XML_CONF_SUBSECTION, "php_defines", &fpm_conf_set_key_value_pairs_subsection, offsetof(struct fpm_worker_pool_config_s, php_defines) },
2644 + { XML_CONF_SCALAR, "user", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, user) },
2645 + { XML_CONF_SCALAR, "group", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, group) },
2646 + { XML_CONF_SCALAR, "chroot", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, chroot) },
2647 + { XML_CONF_SCALAR, "chdir", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, chdir) },
2648 + { XML_CONF_SCALAR, "allowed_clients", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, allowed_clients) },
2649 + { XML_CONF_SUBSECTION, "environment", &fpm_conf_set_key_value_pairs_subsection, offsetof(struct fpm_worker_pool_config_s, environment) },
2650 + { XML_CONF_SCALAR, "request_terminate_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_worker_pool_config_s, request_terminate_timeout) },
2651 + { XML_CONF_SCALAR, "request_slowlog_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_worker_pool_config_s, request_slowlog_timeout) },
2652 + { XML_CONF_SCALAR, "slowlog", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, slowlog) },
2653 + { XML_CONF_SCALAR, "rlimit_files", &xml_conf_set_slot_integer, offsetof(struct fpm_worker_pool_config_s, rlimit_files) },
2654 + { XML_CONF_SCALAR, "rlimit_core", &fpm_conf_set_rlimit_core, 0 },
2655 + { XML_CONF_SCALAR, "max_requests", &xml_conf_set_slot_integer, offsetof(struct fpm_worker_pool_config_s, max_requests) },
2656 + { XML_CONF_SCALAR, "catch_workers_output", &fpm_conf_set_catch_workers_output, 0 },
2657 + { XML_CONF_SUBSECTION, "pm", &fpm_conf_set_pm_subsection, offsetof(struct fpm_worker_pool_config_s, pm) },
2662 +static struct xml_conf_section *fpm_conf_all_sections[] = {
2663 + &xml_section_fpm_global_options,
2664 + &xml_section_fpm_worker_pool_config,
2668 +static int fpm_evaluate_full_path(char **path)
2670 + if (**path != '/') {
2673 + full_path = malloc(sizeof(PHP_PREFIX) + strlen(*path) + 1);
2675 + if (!full_path) return -1;
2677 + sprintf(full_path, "%s/%s", PHP_PREFIX, *path);
2681 + *path = full_path;
2687 +static int fpm_conf_process_all_pools()
2689 + struct fpm_worker_pool_s *wp;
2691 + if (!fpm_worker_all_pools) {
2692 + zlog(ZLOG_STUFF, ZLOG_ERROR, "at least one pool section must be specified in config file");
2696 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
2698 + if (wp->config->listen_address && *wp->config->listen_address) {
2700 + wp->listen_address_domain = fpm_sockets_domain_from_address(wp->config->listen_address);
2702 + if (wp->listen_address_domain == FPM_AF_UNIX && *wp->config->listen_address != '/') {
2703 + fpm_evaluate_full_path(&wp->config->listen_address);
2709 + wp->is_template = 1;
2713 + if (wp->config->request_slowlog_timeout) {
2715 + if (! (wp->config->slowlog && *wp->config->slowlog)) {
2716 + zlog(ZLOG_STUFF, ZLOG_ERROR, "pool %s: 'slowlog' must be specified for use with 'request_slowlog_timeout'",
2717 + wp->config->name);
2721 + static int warned = 0;
2724 + zlog(ZLOG_STUFF, ZLOG_WARNING, "pool %s: 'request_slowlog_timeout' is not supported on your system",
2725 + wp->config->name);
2729 + wp->config->request_slowlog_timeout = 0;
2733 + if (wp->config->request_slowlog_timeout && wp->config->slowlog && *wp->config->slowlog) {
2736 + fpm_evaluate_full_path(&wp->config->slowlog);
2738 + if (wp->config->request_slowlog_timeout) {
2739 + fd = open(wp->config->slowlog, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
2742 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(%s) failed", wp->config->slowlog);
2753 +int fpm_conf_unlink_pid()
2755 + if (fpm_global_options.pid_file) {
2757 + if (0 > unlink(fpm_global_options.pid_file)) {
2758 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "unlink(\"%s\") failed", fpm_global_options.pid_file);
2767 +int fpm_conf_write_pid()
2771 + if (fpm_global_options.pid_file) {
2775 + unlink(fpm_global_options.pid_file);
2777 + fd = creat(fpm_global_options.pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
2780 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "creat(\"%s\") failed", fpm_global_options.pid_file);
2784 + len = sprintf(buf, "%d", (int) fpm_globals.parent_pid);
2786 + if (len != write(fd, buf, len)) {
2787 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "write() failed");
2797 +static int fpm_conf_post_process()
2799 + if (fpm_global_options.pid_file) {
2800 + fpm_evaluate_full_path(&fpm_global_options.pid_file);
2803 + if (!fpm_global_options.error_log) {
2804 + fpm_global_options.error_log = strdup(PHP_FPM_LOG_PATH);
2807 + fpm_evaluate_full_path(&fpm_global_options.error_log);
2809 + if (0 > fpm_stdio_open_error_log(0)) {
2813 + return fpm_conf_process_all_pools();
2816 +static void fpm_conf_cleanup(int which, void *arg)
2818 + free(fpm_global_options.pid_file);
2819 + free(fpm_global_options.error_log);
2820 + fpm_global_options.pid_file = 0;
2821 + fpm_global_options.error_log = 0;
2824 +int fpm_conf_init_main()
2826 + char *filename = fpm_globals.config;
2829 + if (0 > xml_conf_sections_register(fpm_conf_all_sections)) {
2833 + if (filename == NULL) {
2834 + filename = PHP_FPM_CONF_PATH;
2837 + err = xml_conf_load_file(filename);
2840 + zlog(ZLOG_STUFF, ZLOG_ERROR, "failed to load configuration file: %s", err);
2844 + if (0 > fpm_conf_post_process()) {
2850 + fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_conf_cleanup, 0);
2854 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_conf.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_conf.h
2855 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_conf.h 1970-01-01 03:00:00.000000000 +0300
2856 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_conf.h 2008-08-26 19:09:15.000000000 +0400
2860 + /* (c) 2007,2008 Andrei Nigmatulin */
2863 +#define FPM_CONF_H 1
2865 +struct key_value_s;
2867 +struct key_value_s {
2868 + struct key_value_s *next;
2873 +struct fpm_options_s {
2874 + int emergency_restart_threshold;
2875 + int emergency_restart_interval;
2876 + int process_control_timeout;
2882 +extern struct fpm_options_s fpm_global_options;
2889 + int MinSpareServers;
2890 + int MaxSpareServers;
2891 + } options_apache_like;
2894 +struct fpm_listen_options_s {
2901 +struct fpm_worker_pool_config_s {
2903 + char *listen_address;
2904 + struct fpm_listen_options_s *listen_options;
2905 + struct key_value_s *php_defines;
2910 + char *allowed_clients;
2911 + struct key_value_s *environment;
2912 + struct fpm_pm_s *pm;
2913 + int request_terminate_timeout;
2914 + int request_slowlog_timeout;
2919 + unsigned catch_workers_output:1;
2922 +enum { PM_STYLE_STATIC = 1, PM_STYLE_APACHE_LIKE = 2 };
2924 +int fpm_conf_init_main();
2925 +int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc);
2926 +int fpm_conf_write_pid();
2927 +int fpm_conf_unlink_pid();
2931 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_config.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_config.h
2932 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_config.h 1970-01-01 03:00:00.000000000 +0300
2933 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_config.h 2008-05-25 04:30:43.000000000 +0400
2937 + /* (c) 2007,2008 Andrei Nigmatulin */
2939 +#include "php_config.h"
2940 +#include "fpm_autoconf.h"
2943 +/* Solaris does not have it */
2944 +#ifndef INADDR_NONE
2945 +#define INADDR_NONE (-1)
2949 +/* If we're not using GNU C, elide __attribute__ */
2951 +# define __attribute__(x) /*NOTHING*/
2955 +/* Solaris does not have it */
2957 +#define timersub(tvp, uvp, vvp) \
2959 + (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
2960 + (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
2961 + if ((vvp)->tv_usec < 0) { \
2962 + (vvp)->tv_sec--; \
2963 + (vvp)->tv_usec += 1000000; \
2968 +#if defined(HAVE_PTRACE) || defined(PROC_MEM_FILE) || defined(HAVE_MACH_VM_READ)
2969 +#define HAVE_FPM_TRACE 1
2971 +#define HAVE_FPM_TRACE 0
2974 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_env.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_env.c
2975 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_env.c 1970-01-01 03:00:00.000000000 +0300
2976 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_env.c 2008-09-19 03:19:59.000000000 +0400
2980 + /* (c) 2007,2008 Andrei Nigmatulin */
2982 +#include "fpm_config.h"
2984 +#ifdef HAVE_ALLOCA_H
2985 +#include <alloca.h>
2988 +#include <stdlib.h>
2989 +#include <string.h>
2991 +#include "fpm_env.h"
2994 +#ifndef HAVE_SETENV
2995 +int setenv(char *name, char *value, int overwrite)
2997 + int name_len = strlen(name);
2998 + int value_len = strlen(value);
2999 + char *var = alloca(name_len + 1 + value_len + 1);
3001 + memcpy(var, name, name_len);
3003 + var[name_len] = '=';
3005 + memcpy(var + name_len + 1, value, value_len);
3007 + var[name_len + 1 + value_len] = '\0';
3009 + return putenv(var);
3013 +#ifndef HAVE_CLEARENV
3019 + /* this algo is the only one known to me
3020 + that works well on all systems */
3021 + while (*(envp = environ)) {
3022 + char *eq = strchr(*envp, '=');
3024 + s = strdup(*envp);
3026 + if (eq) s[eq - *envp] = '\0';
3036 +int fpm_env_init_child(struct fpm_worker_pool_s *wp)
3038 + struct key_value_s *kv;
3042 + for (kv = wp->config->environment; kv; kv = kv->next) {
3043 + setenv(kv->key, kv->value, 1);
3047 + setenv("USER", wp->user, 1);
3051 + setenv("HOME", wp->home, 1);
3057 +static int fpm_env_conf_wp(struct fpm_worker_pool_s *wp)
3059 + struct key_value_s *kv;
3061 + kv = wp->config->environment;
3063 + for (kv = wp->config->environment; kv; kv = kv->next) {
3064 + if (*kv->value == '$') {
3065 + char *value = getenv(kv->value + 1);
3067 + if (!value) value = "";
3070 + kv->value = strdup(value);
3073 + /* autodetected values should be removed
3074 + if these vars specified in config */
3075 + if (!strcmp(kv->key, "USER")) {
3080 + if (!strcmp(kv->key, "HOME")) {
3089 +int fpm_env_init_main()
3091 + struct fpm_worker_pool_s *wp;
3093 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
3095 + if (0 > fpm_env_conf_wp(wp)) {
3103 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_env.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_env.h
3104 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_env.h 1970-01-01 03:00:00.000000000 +0300
3105 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_env.h 2008-09-19 03:19:59.000000000 +0400
3109 + /* (c) 2007,2008 Andrei Nigmatulin */
3112 +#define FPM_ENV_H 1
3114 +#include "fpm_worker_pool.h"
3116 +int fpm_env_init_child(struct fpm_worker_pool_s *wp);
3117 +int fpm_env_init_main();
3119 +extern char **environ;
3121 +#ifndef HAVE_SETENV
3122 +int setenv(char *name, char *value, int overwrite);
3125 +#ifndef HAVE_CLEARENV
3131 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_events.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_events.c
3132 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_events.c 1970-01-01 03:00:00.000000000 +0300
3133 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_events.c 2008-08-26 19:09:15.000000000 +0400
3137 + /* (c) 2007,2008 Andrei Nigmatulin */
3139 +#include "fpm_config.h"
3141 +#include <unistd.h>
3143 +#include <stdlib.h> /* for putenv */
3144 +#include <string.h>
3145 +#include <sys/types.h> /* for event.h below */
3149 +#include "fpm_process_ctl.h"
3150 +#include "fpm_events.h"
3151 +#include "fpm_cleanup.h"
3152 +#include "fpm_stdio.h"
3153 +#include "fpm_signals.h"
3154 +#include "fpm_children.h"
3157 +static void fpm_event_cleanup(int which, void *arg)
3159 + event_base_free(0);
3162 +static void fpm_got_signal(int fd, short ev, void *arg)
3170 + res = read(fd, &c, 1);
3171 + } while (res == -1 && errno == EINTR);
3174 + if (res < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
3175 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
3181 + case 'C' : /* SIGCHLD */
3182 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGCHLD");
3183 + fpm_children_bury();
3185 + case 'I' : /* SIGINT */
3186 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGINT");
3187 + fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
3189 + case 'T' : /* SIGTERM */
3190 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGTERM");
3191 + fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
3193 + case 'Q' : /* SIGQUIT */
3194 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGQUIT");
3195 + fpm_pctl(FPM_PCTL_STATE_FINISHING, FPM_PCTL_ACTION_SET);
3197 + case '1' : /* SIGUSR1 */
3198 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGUSR1");
3199 + if (0 == fpm_stdio_open_error_log(1)) {
3200 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "log file re-opened");
3203 + case '2' : /* SIGUSR2 */
3204 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGUSR2");
3205 + fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
3209 + if (fpm_globals.is_child) {
3218 +int fpm_event_init_main()
3222 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: using %s", event_get_method());
3224 + fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_event_cleanup, 0);
3229 +int fpm_event_loop()
3231 + static struct event signal_fd_event;
3233 + event_set(&signal_fd_event, fpm_signals_get_fd(), EV_PERSIST | EV_READ, &fpm_got_signal, 0);
3235 + event_add(&signal_fd_event, 0);
3237 + fpm_pctl_heartbeat(-1, 0, 0);
3239 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: entering main loop");
3246 +int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg)
3248 + event_set(ev, fd, EV_PERSIST | EV_READ, callback, arg);
3250 + return event_add(ev, 0);
3253 +int fpm_event_del(struct event *ev)
3255 + return event_del(ev);
3258 +void fpm_event_exit_loop()
3260 + event_loopexit(0);
3263 +void fpm_event_fire(struct event *ev)
3265 + (*ev->ev_callback)( (int) ev->ev_fd, (short) ev->ev_res, ev->ev_arg);
3268 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_events.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_events.h
3269 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_events.h 1970-01-01 03:00:00.000000000 +0300
3270 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_events.h 2008-05-24 21:38:47.000000000 +0400
3274 + /* (c) 2007,2008 Andrei Nigmatulin */
3276 +#ifndef FPM_EVENTS_H
3277 +#define FPM_EVENTS_H 1
3279 +void fpm_event_exit_loop();
3280 +int fpm_event_loop();
3281 +int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg);
3282 +int fpm_event_del(struct event *ev);
3283 +void fpm_event_fire(struct event *ev);
3284 +int fpm_event_init_main();
3288 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm.h php-5.2.6.fpm/sapi/cgi/fpm/fpm.h
3289 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm.h 1970-01-01 03:00:00.000000000 +0300
3290 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm.h 2008-05-24 21:38:47.000000000 +0400
3294 + /* (c) 2007,2008 Andrei Nigmatulin */
3299 +#include <unistd.h>
3301 +int fpm_run(int *max_requests);
3302 +int fpm_init(int argc, char **argv, char *config);
3304 +struct fpm_globals_s {
3309 + int running_children;
3312 + int listening_socket; /* for this child */
3313 + int max_requests; /* for this child */
3317 +extern struct fpm_globals_s fpm_globals;
3322 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_php.c
3323 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php.c 1970-01-01 03:00:00.000000000 +0300
3324 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_php.c 2008-07-20 20:16:28.000000000 +0400
3328 + /* (c) 2007,2008 Andrei Nigmatulin */
3330 +#include "fpm_config.h"
3332 +#include <stdlib.h>
3333 +#include <string.h>
3336 +#include "php_main.h"
3337 +#include "php_ini.h"
3338 +#include "ext/standard/dl.h"
3340 +#include "fastcgi.h"
3343 +#include "fpm_php.h"
3344 +#include "fpm_cleanup.h"
3345 +#include "fpm_worker_pool.h"
3347 +static int zend_ini_alter_master(char *name, int name_length, char *new_value, int new_value_length, int stage)
3349 + zend_ini_entry *ini_entry;
3352 + if (zend_hash_find(EG(ini_directives), name, name_length, (void **) &ini_entry) == FAILURE) {
3356 + duplicate = strdup(new_value);
3358 + if (!ini_entry->on_modify
3359 + || ini_entry->on_modify(ini_entry, duplicate, new_value_length,
3360 + ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage) == SUCCESS) {
3361 + ini_entry->value = duplicate;
3362 + ini_entry->value_length = new_value_length;
3370 +static void fpm_php_disable(char *value, int (*zend_disable)(char *, uint))
3372 + char *s = 0, *e = value;
3380 + zend_disable(s, e - s);
3394 + zend_disable(s, e - s);
3398 +static int fpm_php_apply_defines(struct fpm_worker_pool_s *wp)
3400 + struct key_value_s *kv;
3402 + for (kv = wp->config->php_defines; kv; kv = kv->next) {
3403 + char *name = kv->key;
3404 + char *value = kv->value;
3405 + int name_len = strlen(name);
3406 + int value_len = strlen(value);
3408 + if (!strcmp(name, "extension") && *value) {
3411 +#if defined(PHP_VERSION_ID) && (PHP_VERSION_ID >= 50300)
3412 + php_dl(value, MODULE_PERSISTENT, &zv, 1);
3415 + ZVAL_STRINGL(&filename, value, value_len, 0);
3416 +#if (PHP_MAJOR_VERSION >= 5)
3417 + php_dl(&filename, MODULE_PERSISTENT, &zv, 1);
3419 + php_dl(&filename, MODULE_PERSISTENT, &zv);
3425 + zend_ini_alter_master(name, name_len + 1, value, value_len, PHP_INI_STAGE_ACTIVATE);
3427 + if (!strcmp(name, "disable_functions") && *value) {
3428 + char *v = strdup(value);
3429 +#if (PHP_MAJOR_VERSION >= 5)
3430 + PG(disable_functions) = v;
3432 + fpm_php_disable(v, zend_disable_function);
3434 + else if (!strcmp(name, "disable_classes") && *value) {
3435 + char *v = strdup(value);
3436 +#if (PHP_MAJOR_VERSION >= 5)
3437 + PG(disable_classes) = v;
3439 + fpm_php_disable(v, zend_disable_class);
3446 +static int fpm_php_set_allowed_clients(struct fpm_worker_pool_s *wp)
3448 + if (wp->listen_address_domain == FPM_AF_INET) {
3449 + fcgi_set_allowed_clients(wp->config->allowed_clients);
3455 +char *fpm_php_script_filename()
3457 + return SG(request_info).path_translated;
3460 +char *fpm_php_request_method()
3462 + return (char *) SG(request_info).request_method;
3465 +size_t fpm_php_content_length()
3467 + return SG(request_info).content_length;
3470 +static void fpm_php_cleanup(int which, void *arg)
3472 + php_module_shutdown();
3476 +void fpm_php_soft_quit()
3478 + fcgi_set_in_shutdown(1);
3481 +int fpm_php_init_main()
3483 + fpm_cleanup_add(FPM_CLEANUP_PARENT, fpm_php_cleanup, 0);
3488 +int fpm_php_init_child(struct fpm_worker_pool_s *wp)
3490 + if (0 > fpm_php_apply_defines(wp) ||
3491 + 0 > fpm_php_set_allowed_clients(wp)) {
3497 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_php.h
3498 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php.h 1970-01-01 03:00:00.000000000 +0300
3499 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_php.h 2008-05-24 21:38:47.000000000 +0400
3503 + /* (c) 2007,2008 Andrei Nigmatulin */
3506 +#define FPM_PHP_H 1
3508 +#include "fpm_worker_pool.h"
3510 +#include "build-defs.h" /* for PHP_ defines */
3512 +int fpm_php_init_child(struct fpm_worker_pool_s *wp);
3513 +char *fpm_php_script_filename();
3514 +char *fpm_php_request_method();
3515 +size_t fpm_php_content_length();
3516 +void fpm_php_soft_quit();
3517 +int fpm_php_init_main();
3521 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
3522 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php_trace.c 1970-01-01 03:00:00.000000000 +0300
3523 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_php_trace.c 2008-08-26 19:09:15.000000000 +0400
3527 + /* (c) 2007,2008 Andrei Nigmatulin */
3529 +#include "fpm_config.h"
3534 +#include "php_main.h"
3537 +#include <stddef.h>
3538 +#include <stdint.h>
3539 +#include <unistd.h>
3540 +#include <sys/time.h>
3541 +#include <sys/types.h>
3544 +#include "fpm_trace.h"
3545 +#include "fpm_php_trace.h"
3546 +#include "fpm_children.h"
3547 +#include "fpm_worker_pool.h"
3548 +#include "fpm_process_ctl.h"
3553 +#define valid_ptr(p) ((p) && 0 == ((p) & (sizeof(long) - 1)))
3555 +#if SIZEOF_LONG == 4
3556 +#define PTR_FMT "08"
3557 +#elif SIZEOF_LONG == 8
3558 +#define PTR_FMT "016"
3562 +static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog)
3564 + int callers_limit = 20;
3565 + pid_t pid = child->pid;
3566 + struct timeval tv;
3567 + static const int buf_size = 1024;
3568 + char buf[buf_size];
3569 + long execute_data;
3572 + gettimeofday(&tv, 0);
3574 + zlog_print_time(&tv, buf, buf_size);
3576 + fprintf(slowlog, "\n%s pid %d (pool %s)\n", buf, (int) pid, child->wp->config->name);
3578 + if (0 > fpm_trace_get_strz(buf, buf_size, (long) &SG(request_info).path_translated)) {
3582 + fprintf(slowlog, "script_filename = %s\n", buf);
3584 + if (0 > fpm_trace_get_long((long) &EG(current_execute_data), &l)) {
3590 + while (execute_data) {
3594 + fprintf(slowlog, "[0x%" PTR_FMT "lx] ", execute_data);
3596 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, function_state.function), &l)) {
3602 + if (valid_ptr(function)) {
3603 + if (0 > fpm_trace_get_strz(buf, buf_size, function + offsetof(zend_function, common.function_name))) {
3607 + fprintf(slowlog, "%s()", buf);
3610 + fprintf(slowlog, "???");
3613 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, op_array), &l)) {
3619 + if (valid_ptr(l)) {
3620 + long op_array = l;
3622 + if (0 > fpm_trace_get_strz(buf, buf_size, op_array + offsetof(zend_op_array, filename))) {
3627 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, opline), &l)) {
3631 + if (valid_ptr(l)) {
3633 + uint *lu = (uint *) &l;
3635 + if (0 > fpm_trace_get_long(opline + offsetof(struct _zend_op, lineno), &l)) {
3642 + fprintf(slowlog, " %s:%u\n", *buf ? buf : "unknown", lineno);
3644 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, prev_execute_data), &l)) {
3650 + if (0 == --callers_limit) {
3658 +void fpm_php_trace(struct fpm_child_s *child)
3662 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "about to trace %d", (int) child->pid);
3664 + slowlog = fopen(child->wp->config->slowlog, "a+");
3667 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fopen(%s) failed", child->wp->config->slowlog);
3671 + if (0 > fpm_trace_ready(child->pid)) {
3675 + if (0 > fpm_php_trace_dump(child, slowlog)) {
3676 + fprintf(slowlog, "+++ dump failed\n");
3679 + if (0 > fpm_trace_close(child->pid)) {
3687 + fpm_pctl_kill(child->pid, FPM_PCTL_CONT);
3688 + child->tracer = 0;
3690 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "finished trace of %d", (int) child->pid);
3695 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
3696 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_php_trace.h 1970-01-01 03:00:00.000000000 +0300
3697 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_php_trace.h 2008-05-24 21:38:47.000000000 +0400
3701 + /* (c) 2007,2008 Andrei Nigmatulin */
3703 +#ifndef FPM_PHP_TRACE_H
3704 +#define FPM_PHP_TRACE_H 1
3706 +struct fpm_child_s;
3708 +void fpm_php_trace(struct fpm_child_s *);
3712 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
3713 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_process_ctl.c 1970-01-01 03:00:00.000000000 +0300
3714 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_process_ctl.c 2008-07-21 17:13:44.000000000 +0400
3718 + /* (c) 2007,2008 Andrei Nigmatulin */
3720 +#include "fpm_config.h"
3722 +#include <sys/types.h>
3723 +#include <signal.h>
3724 +#include <unistd.h>
3725 +#include <stdlib.h>
3728 +#include "fpm_clock.h"
3729 +#include "fpm_children.h"
3730 +#include "fpm_signals.h"
3731 +#include "fpm_events.h"
3732 +#include "fpm_process_ctl.h"
3733 +#include "fpm_cleanup.h"
3734 +#include "fpm_request.h"
3735 +#include "fpm_worker_pool.h"
3739 +static int fpm_state = FPM_PCTL_STATE_NORMAL;
3740 +static int fpm_signal_sent = 0;
3743 +static const char *fpm_state_names[] = {
3744 + [FPM_PCTL_STATE_NORMAL] = "normal",
3745 + [FPM_PCTL_STATE_RELOADING] = "reloading",
3746 + [FPM_PCTL_STATE_TERMINATING] = "terminating",
3747 + [FPM_PCTL_STATE_FINISHING] = "finishing"
3750 +static int saved_argc;
3751 +static char **saved_argv;
3753 +static void fpm_pctl_cleanup(int which, void *arg)
3757 + if (which != FPM_CLEANUP_PARENT_EXEC) {
3759 + for (i = 0; i < saved_argc; i++) {
3760 + free(saved_argv[i]);
3768 +static struct event pctl_event;
3770 +static void fpm_pctl_action(int fd, short which, void *arg)
3772 + evtimer_del(&pctl_event);
3774 + memset(&pctl_event, 0, sizeof(pctl_event));
3776 + fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_TIMEOUT);
3779 +static int fpm_pctl_timeout_set(int sec)
3781 + struct timeval tv = { .tv_sec = sec, .tv_usec = 0 };
3783 + if (evtimer_initialized(&pctl_event)) {
3784 + evtimer_del(&pctl_event);
3787 + evtimer_set(&pctl_event, &fpm_pctl_action, 0);
3789 + evtimer_add(&pctl_event, &tv);
3794 +static void fpm_pctl_exit()
3796 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "exiting, bye-bye!");
3798 + fpm_conf_unlink_pid();
3800 + fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT_MAIN);
3805 +#define optional_arg(c) (saved_argc > c ? ", \"" : ""), (saved_argc > c ? saved_argv[c] : ""), (saved_argc > c ? "\"" : "")
3807 +static void fpm_pctl_exec()
3810 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "reloading: execvp(\"%s\", {\"%s\""
3811 + "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
3812 + "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
3814 + saved_argv[0], saved_argv[0],
3827 + fpm_cleanups_run(FPM_CLEANUP_PARENT_EXEC);
3829 + execvp(saved_argv[0], saved_argv);
3831 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "execvp() failed");
3836 +static void fpm_pctl_action_last()
3838 + switch (fpm_state) {
3840 + case FPM_PCTL_STATE_RELOADING :
3845 + case FPM_PCTL_STATE_FINISHING :
3847 + case FPM_PCTL_STATE_TERMINATING :
3854 +int fpm_pctl_kill(pid_t pid, int how)
3859 + case FPM_PCTL_TERM :
3862 + case FPM_PCTL_STOP :
3865 + case FPM_PCTL_CONT :
3872 + return kill(pid, s);
3875 +static void fpm_pctl_kill_all(int signo)
3877 + struct fpm_worker_pool_s *wp;
3878 + int alive_children = 0;
3880 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
3881 + struct fpm_child_s *child;
3883 + for (child = wp->children; child; child = child->next) {
3885 + int res = kill(child->pid, signo);
3887 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "sending signal %d %s to child %d (pool %s)", signo,
3888 + fpm_signal_names[signo] ? fpm_signal_names[signo] : "",
3889 + (int) child->pid, child->wp->config->name);
3891 + if (res == 0) ++alive_children;
3895 + if (alive_children) {
3896 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "%d %s still alive", alive_children, alive_children == 1 ? "child is" : "children are");
3900 +static void fpm_pctl_action_next()
3904 + if (!fpm_globals.running_children) fpm_pctl_action_last();
3906 + if (fpm_signal_sent == 0) {
3907 + if (fpm_state == FPM_PCTL_STATE_TERMINATING) {
3913 + timeout = fpm_global_options.process_control_timeout;
3916 + if (fpm_signal_sent == SIGQUIT) {
3925 + fpm_pctl_kill_all(sig);
3927 + fpm_signal_sent = sig;
3929 + fpm_pctl_timeout_set(timeout);
3932 +void fpm_pctl(int new_state, int action)
3936 + case FPM_PCTL_ACTION_SET :
3938 + if (fpm_state == new_state) { /* already in progress - just ignore duplicate signal */
3942 + switch (fpm_state) { /* check which states can be overridden */
3944 + case FPM_PCTL_STATE_NORMAL :
3946 + /* 'normal' can be overridden by any other state */
3949 + case FPM_PCTL_STATE_RELOADING :
3951 + /* 'reloading' can be overridden by 'finishing' */
3952 + if (new_state == FPM_PCTL_STATE_FINISHING) break;
3954 + case FPM_PCTL_STATE_FINISHING :
3956 + /* 'reloading' and 'finishing' can be overridden by 'terminating' */
3957 + if (new_state == FPM_PCTL_STATE_TERMINATING) break;
3959 + case FPM_PCTL_STATE_TERMINATING :
3961 + /* nothing can override 'terminating' state */
3962 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "not switching to '%s' state, because already in '%s' state",
3963 + fpm_state_names[new_state], fpm_state_names[fpm_state]);
3968 + fpm_signal_sent = 0;
3969 + fpm_state = new_state;
3971 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "switching to '%s' state", fpm_state_names[fpm_state]);
3975 + case FPM_PCTL_ACTION_TIMEOUT :
3977 + fpm_pctl_action_next();
3981 + case FPM_PCTL_ACTION_LAST_CHILD_EXITED :
3983 + fpm_pctl_action_last();
3990 +int fpm_pctl_can_spawn_children()
3992 + return fpm_state == FPM_PCTL_STATE_NORMAL;
3995 +int fpm_pctl_child_exited()
3997 + if (fpm_state == FPM_PCTL_STATE_NORMAL) return 0;
3999 + if (!fpm_globals.running_children) {
4000 + fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_LAST_CHILD_EXITED);
4006 +int fpm_pctl_init_main()
4010 + saved_argc = fpm_globals.argc;
4012 + saved_argv = malloc(sizeof(char *) * (saved_argc + 1));
4014 + if (!saved_argv) {
4018 + for (i = 0; i < saved_argc; i++) {
4019 + saved_argv[i] = strdup(fpm_globals.argv[i]);
4021 + if (!saved_argv[i]) {
4026 + saved_argv[i] = 0;
4028 + fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_pctl_cleanup, 0);
4033 +static void fpm_pctl_check_request_timeout(struct timeval *now)
4035 + struct fpm_worker_pool_s *wp;
4037 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
4038 + int terminate_timeout = wp->config->request_terminate_timeout;
4039 + int slowlog_timeout = wp->config->request_slowlog_timeout;
4040 + struct fpm_child_s *child;
4042 + if (terminate_timeout || slowlog_timeout) {
4043 + for (child = wp->children; child; child = child->next) {
4044 + fpm_request_check_timed_out(child, now, terminate_timeout, slowlog_timeout);
4051 +void fpm_pctl_heartbeat(int fd, short which, void *arg)
4053 + static struct event heartbeat;
4054 + struct timeval tv = { .tv_sec = 0, .tv_usec = 130000 };
4055 + struct timeval now;
4057 + if (which == EV_TIMEOUT) {
4058 + evtimer_del(&heartbeat);
4059 + fpm_clock_get(&now);
4060 + fpm_pctl_check_request_timeout(&now);
4063 + evtimer_set(&heartbeat, &fpm_pctl_heartbeat, 0);
4065 + evtimer_add(&heartbeat, &tv);
4068 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
4069 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_process_ctl.h 1970-01-01 03:00:00.000000000 +0300
4070 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_process_ctl.h 2008-07-21 01:33:10.000000000 +0400
4074 + /* (c) 2007,2008 Andrei Nigmatulin */
4076 +#ifndef FPM_PROCESS_CTL_H
4077 +#define FPM_PROCESS_CTL_H 1
4079 +struct fpm_child_s;
4081 +void fpm_pctl(int new_state, int action);
4082 +int fpm_pctl_can_spawn_children();
4083 +int fpm_pctl_kill(pid_t pid, int how);
4084 +void fpm_pctl_heartbeat(int fd, short which, void *arg);
4085 +int fpm_pctl_child_exited();
4086 +int fpm_pctl_init_main();
4090 + FPM_PCTL_STATE_UNSPECIFIED,
4091 + FPM_PCTL_STATE_NORMAL,
4092 + FPM_PCTL_STATE_RELOADING,
4093 + FPM_PCTL_STATE_TERMINATING,
4094 + FPM_PCTL_STATE_FINISHING
4098 + FPM_PCTL_ACTION_SET,
4099 + FPM_PCTL_ACTION_TIMEOUT,
4100 + FPM_PCTL_ACTION_LAST_CHILD_EXITED
4111 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_request.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_request.c
4112 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_request.c 1970-01-01 03:00:00.000000000 +0300
4113 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_request.c 2008-09-01 03:34:36.000000000 +0400
4117 + /* (c) 2007,2008 Andrei Nigmatulin */
4119 +#include "fpm_config.h"
4121 +#include "fpm_php.h"
4122 +#include "fpm_str.h"
4123 +#include "fpm_clock.h"
4124 +#include "fpm_conf.h"
4125 +#include "fpm_trace.h"
4126 +#include "fpm_php_trace.h"
4127 +#include "fpm_process_ctl.h"
4128 +#include "fpm_children.h"
4129 +#include "fpm_shm_slots.h"
4130 +#include "fpm_request.h"
4134 +void fpm_request_accepting()
4136 + struct fpm_shm_slot_s *slot;
4138 + slot = fpm_shm_slots_acquire(0, 0);
4140 + slot->request_stage = FPM_REQUEST_ACCEPTING;
4142 + fpm_clock_get(&slot->tv);
4143 + memset(slot->request_method, 0, sizeof(slot->request_method));
4144 + slot->content_length = 0;
4145 + memset(slot->script_filename, 0, sizeof(slot->script_filename));
4147 + fpm_shm_slots_release(slot);
4150 +void fpm_request_reading_headers()
4152 + struct fpm_shm_slot_s *slot;
4154 + slot = fpm_shm_slots_acquire(0, 0);
4156 + slot->request_stage = FPM_REQUEST_READING_HEADERS;
4158 + fpm_clock_get(&slot->tv);
4159 + slot->accepted = slot->tv;
4161 + fpm_shm_slots_release(slot);
4164 +void fpm_request_info()
4166 + struct fpm_shm_slot_s *slot;
4167 + char *request_method = fpm_php_request_method();
4168 + char *script_filename = fpm_php_script_filename();
4170 + slot = fpm_shm_slots_acquire(0, 0);
4172 + slot->request_stage = FPM_REQUEST_INFO;
4174 + fpm_clock_get(&slot->tv);
4176 + if (request_method) {
4177 + cpystrn(slot->request_method, request_method, sizeof(slot->request_method));
4180 + slot->content_length = fpm_php_content_length();
4182 + /* if cgi.fix_pathinfo is set to "1" and script cannot be found (404)
4183 + the sapi_globals.request_info.path_translated is set to NULL */
4184 + if (script_filename) {
4185 + cpystrn(slot->script_filename, script_filename, sizeof(slot->script_filename));
4188 + fpm_shm_slots_release(slot);
4191 +void fpm_request_executing()
4193 + struct fpm_shm_slot_s *slot;
4195 + slot = fpm_shm_slots_acquire(0, 0);
4197 + slot->request_stage = FPM_REQUEST_EXECUTING;
4199 + fpm_clock_get(&slot->tv);
4201 + fpm_shm_slots_release(slot);
4204 +void fpm_request_finished()
4206 + struct fpm_shm_slot_s *slot;
4208 + slot = fpm_shm_slots_acquire(0, 0);
4210 + slot->request_stage = FPM_REQUEST_FINISHED;
4212 + fpm_clock_get(&slot->tv);
4213 + memset(&slot->accepted, 0, sizeof(slot->accepted));
4215 + fpm_shm_slots_release(slot);
4218 +void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *now, int terminate_timeout, int slowlog_timeout)
4220 + struct fpm_shm_slot_s *slot;
4221 + struct fpm_shm_slot_s slot_c;
4223 + slot = fpm_shm_slot(child);
4225 + if (!fpm_shm_slots_acquire(slot, 1)) {
4231 + fpm_shm_slots_release(slot);
4234 + if (child->slow_logged.tv_sec) {
4235 + if (child->slow_logged.tv_sec != slot_c.accepted.tv_sec || child->slow_logged.tv_usec != slot_c.accepted.tv_usec) {
4236 + child->slow_logged.tv_sec = 0;
4237 + child->slow_logged.tv_usec = 0;
4242 + if (slot_c.request_stage > FPM_REQUEST_ACCEPTING && slot_c.request_stage < FPM_REQUEST_FINISHED) {
4243 + char purified_script_filename[sizeof(slot_c.script_filename)];
4244 + struct timeval tv;
4246 + timersub(now, &slot_c.accepted, &tv);
4249 + if (child->slow_logged.tv_sec == 0 && slowlog_timeout &&
4250 + slot_c.request_stage == FPM_REQUEST_EXECUTING && tv.tv_sec >= slowlog_timeout) {
4252 + str_purify_filename(purified_script_filename, slot_c.script_filename, sizeof(slot_c.script_filename));
4254 + child->slow_logged = slot_c.accepted;
4255 + child->tracer = fpm_php_trace;
4257 + fpm_trace_signal(child->pid);
4259 + zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d, script '%s' (pool %s) executing too slow (%d.%06d sec), logging",
4260 + (int) child->pid, purified_script_filename, child->wp->config->name, (int) tv.tv_sec, (int) tv.tv_usec);
4265 + if (terminate_timeout && tv.tv_sec >= terminate_timeout) {
4267 + str_purify_filename(purified_script_filename, slot_c.script_filename, sizeof(slot_c.script_filename));
4269 + fpm_pctl_kill(child->pid, FPM_PCTL_TERM);
4271 + zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d, script '%s' (pool %s) execution timed out (%d.%06d sec), terminating",
4272 + (int) child->pid, purified_script_filename, child->wp->config->name, (int) tv.tv_sec, (int) tv.tv_usec);
4278 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_request.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_request.h
4279 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_request.h 1970-01-01 03:00:00.000000000 +0300
4280 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_request.h 2008-07-20 05:47:16.000000000 +0400
4284 + /* (c) 2007,2008 Andrei Nigmatulin */
4286 +#ifndef FPM_REQUEST_H
4287 +#define FPM_REQUEST_H 1
4289 +void fpm_request_accepting(); /* hanging in accept() */
4290 +void fpm_request_reading_headers(); /* start reading fastcgi request from very first byte */
4291 +void fpm_request_info(); /* not a stage really but a point in the php code, where all request params have become known to sapi */
4292 +void fpm_request_executing(); /* the script is executing */
4293 +void fpm_request_finished(); /* request processed: script response have been sent to web server */
4295 +struct fpm_child_s;
4298 +void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *tv, int terminate_timeout, int slowlog_timeout);
4300 +enum fpm_request_stage_e {
4301 + FPM_REQUEST_ACCEPTING = 1,
4302 + FPM_REQUEST_READING_HEADERS,
4304 + FPM_REQUEST_EXECUTING,
4305 + FPM_REQUEST_FINISHED
4309 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm.c
4310 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm.c 1970-01-01 03:00:00.000000000 +0300
4311 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm.c 2008-05-24 21:38:47.000000000 +0400
4315 + /* (c) 2007,2008 Andrei Nigmatulin */
4317 +#include "fpm_config.h"
4319 +#include <unistd.h>
4320 +#include <sys/mman.h>
4321 +#include <stdlib.h>
4323 +#include "fpm_shm.h"
4327 +/* MAP_ANON is depricated, but not in macosx */
4328 +#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
4329 +#define MAP_ANONYMOUS MAP_ANON
4333 +struct fpm_shm_s *fpm_shm_alloc(size_t sz)
4335 + struct fpm_shm_s *shm;
4337 + shm = malloc(sizeof(*shm));
4343 + shm->mem = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
4346 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "mmap(MAP_ANONYMOUS | MAP_SHARED) failed");
4357 +static void fpm_shm_free(struct fpm_shm_s *shm, int do_unmap)
4360 + munmap(shm->mem, shm->sz);
4366 +void fpm_shm_free_list(struct fpm_shm_s *shm, void *mem)
4368 + struct fpm_shm_s *next;
4370 + for (; shm; shm = next) {
4373 + fpm_shm_free(shm, mem != shm->mem);
4377 +void *fpm_shm_alloc_chunk(struct fpm_shm_s **head, size_t sz, void **mem)
4379 + size_t pagesize = getpagesize();
4380 + static const size_t cache_line_size = 16;
4381 + size_t aligned_sz;
4382 + struct fpm_shm_s *shm;
4385 + sz = (sz + cache_line_size - 1) & -cache_line_size;
4389 + if (0 == shm || shm->sz - shm->used < sz) {
4390 + /* allocate one more shm segment */
4392 + aligned_sz = (sz + pagesize - 1) & -pagesize;
4394 + shm = fpm_shm_alloc(aligned_sz);
4400 + shm->next = *head;
4401 + if (shm->next) shm->next->prev = shm;
4407 + ret = (char *) shm->mem + shm->used;
4413 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm.h
4414 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm.h 1970-01-01 03:00:00.000000000 +0300
4415 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm.h 2008-05-24 21:38:47.000000000 +0400
4419 + /* (c) 2007,2008 Andrei Nigmatulin */
4422 +#define FPM_SHM_H 1
4427 + struct fpm_shm_s *prev, *next;
4433 +struct fpm_shm_s *fpm_shm_alloc(size_t sz);
4434 +void fpm_shm_free_list(struct fpm_shm_s *, void *);
4435 +void *fpm_shm_alloc_chunk(struct fpm_shm_s **head, size_t sz, void **mem);
4439 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
4440 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm_slots.c 1970-01-01 03:00:00.000000000 +0300
4441 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm_slots.c 2008-05-24 21:38:47.000000000 +0400
4445 + /* (c) 2007,2008 Andrei Nigmatulin */
4447 +#include "fpm_config.h"
4449 +#include "fpm_atomic.h"
4450 +#include "fpm_worker_pool.h"
4451 +#include "fpm_children.h"
4452 +#include "fpm_shm.h"
4453 +#include "fpm_shm_slots.h"
4456 +static void *shm_mem;
4457 +static struct fpm_shm_slot_s *shm_slot;
4459 +int fpm_shm_slots_prepare_slot(struct fpm_child_s *child)
4461 + struct fpm_worker_pool_s *wp = child->wp;
4462 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4464 + child->shm_slot_i = wp->slots_used.used;
4466 + shm_slot_ptr = fpm_array_push(&wp->slots_used);
4468 + if (0 == shm_slot_ptr) {
4472 + if (0 == wp->slots_free.used) {
4473 + shm_slot_ptr->shm_slot = fpm_shm_alloc_chunk(&wp->shm_list, sizeof(struct fpm_shm_slot_s), &shm_slot_ptr->mem);
4475 + if (!shm_slot_ptr->shm_slot) {
4480 + *shm_slot_ptr = *(struct fpm_shm_slot_ptr_s *) fpm_array_item_last(&wp->slots_free);
4482 + --wp->slots_free.used;
4485 + memset(shm_slot_ptr->shm_slot, 0, sizeof(struct fpm_shm_slot_s));
4487 + shm_slot_ptr->child = child;
4492 +void fpm_shm_slots_discard_slot(struct fpm_child_s *child)
4494 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4495 + struct fpm_worker_pool_s *wp = child->wp;
4498 + shm_slot_ptr = fpm_array_push(&wp->slots_free);
4500 + if (shm_slot_ptr) {
4502 + struct fpm_shm_slot_ptr_s *shm_slot_ptr_used;
4504 + shm_slot_ptr_used = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4506 + *shm_slot_ptr = *shm_slot_ptr_used;
4508 + shm_slot_ptr->child = 0;
4512 + n = fpm_array_item_remove(&wp->slots_used, child->shm_slot_i);
4515 + shm_slot_ptr = fpm_array_item(&wp->slots_used, n);
4517 + shm_slot_ptr->child->shm_slot_i = n;
4521 +void fpm_shm_slots_child_use_slot(struct fpm_child_s *child)
4523 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4524 + struct fpm_worker_pool_s *wp = child->wp;
4526 + shm_slot_ptr = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4528 + shm_slot = shm_slot_ptr->shm_slot;
4529 + shm_mem = shm_slot_ptr->mem;
4532 +void fpm_shm_slots_parent_use_slot(struct fpm_child_s *child)
4534 + /* nothing to do */
4537 +void *fpm_shm_slots_mem()
4542 +struct fpm_shm_slot_s *fpm_shm_slot(struct fpm_child_s *child)
4544 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4545 + struct fpm_worker_pool_s *wp = child->wp;
4547 + shm_slot_ptr = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4549 + return shm_slot_ptr->shm_slot;
4552 +struct fpm_shm_slot_s *fpm_shm_slots_acquire(struct fpm_shm_slot_s *s, int nohang)
4558 + if (0 > fpm_spinlock(&s->lock, nohang)) {
4565 +void fpm_shm_slots_release(struct fpm_shm_slot_s *s)
4570 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
4571 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_shm_slots.h 1970-01-01 03:00:00.000000000 +0300
4572 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_shm_slots.h 2008-05-24 21:38:47.000000000 +0400
4576 + /* (c) 2007,2008 Andrei Nigmatulin */
4578 +#ifndef FPM_SHM_SLOTS_H
4579 +#define FPM_SHM_SLOTS_H 1
4581 +#include "fpm_atomic.h"
4582 +#include "fpm_worker_pool.h"
4583 +#include "fpm_request.h"
4585 +struct fpm_child_s;
4587 +struct fpm_shm_slot_s {
4592 + enum fpm_request_stage_e request_stage;
4593 + struct timeval accepted;
4594 + struct timeval tv;
4595 + char request_method[16];
4596 + size_t content_length; /* used with POST only */
4597 + char script_filename[256];
4600 +struct fpm_shm_slot_ptr_s {
4602 + struct fpm_shm_slot_s *shm_slot;
4603 + struct fpm_child_s *child;
4606 +int fpm_shm_slots_prepare_slot(struct fpm_child_s *child);
4607 +void fpm_shm_slots_discard_slot(struct fpm_child_s *child);
4608 +void fpm_shm_slots_child_use_slot(struct fpm_child_s *child);
4609 +void fpm_shm_slots_parent_use_slot(struct fpm_child_s *child);
4610 +void *fpm_shm_slots_mem();
4611 +struct fpm_shm_slot_s *fpm_shm_slot(struct fpm_child_s *child);
4612 +struct fpm_shm_slot_s *fpm_shm_slots_acquire(struct fpm_shm_slot_s *, int nohang);
4613 +void fpm_shm_slots_release(struct fpm_shm_slot_s *);
4617 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_signals.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_signals.c
4618 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_signals.c 1970-01-01 03:00:00.000000000 +0300
4619 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_signals.c 2008-08-26 19:09:15.000000000 +0400
4623 + /* (c) 2007,2008 Andrei Nigmatulin */
4625 +#include "fpm_config.h"
4627 +#include <signal.h>
4629 +#include <sys/types.h>
4630 +#include <sys/socket.h>
4631 +#include <stdlib.h>
4632 +#include <string.h>
4634 +#include <unistd.h>
4638 +#include "fpm_signals.h"
4639 +#include "fpm_sockets.h"
4640 +#include "fpm_php.h"
4645 +const char *fpm_signal_names[NSIG + 1] = {
4647 + [SIGHUP] = "SIGHUP",
4650 + [SIGINT] = "SIGINT",
4653 + [SIGQUIT] = "SIGQUIT",
4656 + [SIGILL] = "SIGILL",
4659 + [SIGTRAP] = "SIGTRAP",
4662 + [SIGABRT] = "SIGABRT",
4665 + [SIGEMT] = "SIGEMT",
4668 + [SIGBUS] = "SIGBUS",
4671 + [SIGFPE] = "SIGFPE",
4674 + [SIGKILL] = "SIGKILL",
4677 + [SIGUSR1] = "SIGUSR1",
4680 + [SIGSEGV] = "SIGSEGV",
4683 + [SIGUSR2] = "SIGUSR2",
4686 + [SIGPIPE] = "SIGPIPE",
4689 + [SIGALRM] = "SIGALRM",
4692 + [SIGTERM] = "SIGTERM",
4695 + [SIGCHLD] = "SIGCHLD",
4698 + [SIGCONT] = "SIGCONT",
4701 + [SIGSTOP] = "SIGSTOP",
4704 + [SIGTSTP] = "SIGTSTP",
4707 + [SIGTTIN] = "SIGTTIN",
4710 + [SIGTTOU] = "SIGTTOU",
4713 + [SIGURG] = "SIGURG",
4716 + [SIGXCPU] = "SIGXCPU",
4719 + [SIGXFSZ] = "SIGXFSZ",
4722 + [SIGVTALRM] = "SIGVTALRM",
4725 + [SIGPROF] = "SIGPROF",
4728 + [SIGWINCH] = "SIGWINCH",
4731 + [SIGINFO] = "SIGINFO",
4734 + [SIGIO] = "SIGIO",
4737 + [SIGPWR] = "SIGPWR",
4740 + [SIGSYS] = "SIGSYS",
4743 + [SIGWAITING] = "SIGWAITING",
4746 + [SIGLWP] = "SIGLWP",
4749 + [SIGFREEZE] = "SIGFREEZE",
4752 + [SIGTHAW] = "SIGTHAW",
4755 + [SIGCANCEL] = "SIGCANCEL",
4758 + [SIGLOST] = "SIGLOST",
4762 +static void sig_soft_quit(int signo)
4764 + int saved_errno = errno;
4766 + /* closing fastcgi listening socket will force fcgi_accept() exit immediately */
4768 + socket(AF_UNIX, SOCK_STREAM, 0);
4770 + fpm_php_soft_quit();
4772 + errno = saved_errno;
4775 +static void sig_handler(int signo)
4777 + static const char sig_chars[NSIG + 1] = {
4788 + if (fpm_globals.parent_pid != getpid()) {
4789 + /* prevent a signal race condition when child process
4790 + have not set up it's own signal handler yet */
4794 + saved_errno = errno;
4796 + s = sig_chars[signo];
4798 + write(sp[1], &s, sizeof(s));
4800 + errno = saved_errno;
4803 +int fpm_signals_init_main()
4805 + struct sigaction act;
4807 + if (0 > socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) {
4808 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "socketpair() failed");
4812 + if (0 > fd_set_blocked(sp[0], 0) || 0 > fd_set_blocked(sp[1], 0)) {
4813 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fd_set_blocked() failed");
4817 + if (0 > fcntl(sp[0], F_SETFD, FD_CLOEXEC) || 0 > fcntl(sp[1], F_SETFD, FD_CLOEXEC)) {
4818 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fcntl(F_SETFD, FD_CLOEXEC) failed");
4822 + memset(&act, 0, sizeof(act));
4823 + act.sa_handler = sig_handler;
4824 + sigfillset(&act.sa_mask);
4826 + if (0 > sigaction(SIGTERM, &act, 0) ||
4827 + 0 > sigaction(SIGINT, &act, 0) ||
4828 + 0 > sigaction(SIGUSR1, &act, 0) ||
4829 + 0 > sigaction(SIGUSR2, &act, 0) ||
4830 + 0 > sigaction(SIGCHLD, &act, 0) ||
4831 + 0 > sigaction(SIGQUIT, &act, 0)) {
4833 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "sigaction() failed");
4840 +int fpm_signals_init_child()
4842 + struct sigaction act, act_dfl;
4844 + memset(&act, 0, sizeof(act));
4845 + memset(&act_dfl, 0, sizeof(act_dfl));
4847 + act.sa_handler = &sig_soft_quit;
4848 + act.sa_flags |= SA_RESTART;
4850 + act_dfl.sa_handler = SIG_DFL;
4855 + if (0 > sigaction(SIGTERM, &act_dfl, 0) ||
4856 + 0 > sigaction(SIGINT, &act_dfl, 0) ||
4857 + 0 > sigaction(SIGUSR1, &act_dfl, 0) ||
4858 + 0 > sigaction(SIGUSR2, &act_dfl, 0) ||
4859 + 0 > sigaction(SIGCHLD, &act_dfl, 0) ||
4860 + 0 > sigaction(SIGQUIT, &act, 0)) {
4862 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "sigaction() failed");
4869 +int fpm_signals_get_fd()
4873 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_signals.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_signals.h
4874 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_signals.h 1970-01-01 03:00:00.000000000 +0300
4875 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_signals.h 2008-05-24 21:38:47.000000000 +0400
4879 + /* (c) 2007,2008 Andrei Nigmatulin */
4881 +#ifndef FPM_SIGNALS_H
4882 +#define FPM_SIGNALS_H 1
4884 +#include <signal.h>
4886 +int fpm_signals_init_main();
4887 +int fpm_signals_init_child();
4888 +int fpm_signals_get_fd();
4890 +extern const char *fpm_signal_names[NSIG + 1];
4893 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_sockets.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_sockets.c
4894 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_sockets.c 1970-01-01 03:00:00.000000000 +0300
4895 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_sockets.c 2008-08-26 19:09:15.000000000 +0400
4899 + /* (c) 2007,2008 Andrei Nigmatulin */
4901 +#include "fpm_config.h"
4903 +#ifdef HAVE_ALLOCA_H
4904 +#include <alloca.h>
4906 +#include <sys/types.h>
4907 +#include <sys/stat.h> /* for chmod(2) */
4908 +#include <sys/socket.h>
4909 +#include <netinet/in.h>
4910 +#include <arpa/inet.h>
4911 +#include <sys/un.h>
4914 +#include <stdlib.h>
4915 +#include <string.h>
4917 +#include <unistd.h>
4920 +#include "fpm_arrays.h"
4921 +#include "fpm_sockets.h"
4922 +#include "fpm_worker_pool.h"
4923 +#include "fpm_unix.h"
4924 +#include "fpm_str.h"
4925 +#include "fpm_env.h"
4926 +#include "fpm_cleanup.h"
4928 +struct listening_socket_s {
4935 +static struct fpm_array_s sockets_list;
4937 +static int fpm_sockets_resolve_af_inet(char *node, char *service, struct sockaddr_in *addr)
4939 + struct addrinfo *res;
4940 + struct addrinfo hints;
4943 + memset(&hints, 0, sizeof(hints));
4945 + hints.ai_family = AF_INET;
4947 + ret = getaddrinfo(node, service, &hints, &res);
4950 + zlog(ZLOG_STUFF, ZLOG_ERROR, "can't resolve hostname '%s%s%s': getaddrinfo said: %s%s%s\n",
4951 + node, service ? ":" : "", service ? service : "",
4952 + gai_strerror(ret), ret == EAI_SYSTEM ? ", system error: " : "", ret == EAI_SYSTEM ? strerror(errno) : "");
4956 + *addr = *(struct sockaddr_in *) res->ai_addr;
4958 + freeaddrinfo(res);
4963 +enum { FPM_GET_USE_SOCKET = 1, FPM_STORE_SOCKET = 2, FPM_STORE_USE_SOCKET = 3 };
4965 +static void fpm_sockets_cleanup(int which, void *arg)
4968 + char *env_value = 0;
4970 + struct listening_socket_s *ls = sockets_list.data;
4972 + for (i = 0; i < sockets_list.used; i++, ls++) {
4974 + if (which != FPM_CLEANUP_PARENT_EXEC) {
4979 + else { /* on PARENT EXEC we want socket fds to be inherited through environment variable */
4981 + sprintf(fd, "%d", ls->sock);
4982 + env_value = realloc(env_value, p + (p ? 1 : 0) + strlen(ls->key) + 1 + strlen(fd) + 1);
4983 + p += sprintf(env_value + p, "%s%s=%s", p ? "," : "", ls->key, fd);
4986 + if (which == FPM_CLEANUP_PARENT_EXIT_MAIN) {
4988 + if (ls->type == FPM_AF_UNIX) {
4998 + setenv("FPM_SOCKETS", env_value, 1);
5002 + fpm_array_free(&sockets_list);
5005 +static int fpm_sockets_hash_op(int sock, struct sockaddr *sa, char *key, int type, int op)
5008 + if (key == NULL) {
5012 + case FPM_AF_INET : {
5013 + struct sockaddr_in *sa_in = (struct sockaddr_in *) sa;
5015 + key = alloca(sizeof("xxx.xxx.xxx.xxx:ppppp"));
5017 + sprintf(key, "%u.%u.%u.%u:%u", IPQUAD(&sa_in->sin_addr), (unsigned int) ntohs(sa_in->sin_port));
5022 + case FPM_AF_UNIX : {
5023 + struct sockaddr_un *sa_un = (struct sockaddr_un *) sa;
5025 + key = alloca(strlen(sa_un->sun_path) + 1);
5027 + strcpy(key, sa_un->sun_path);
5041 + case FPM_GET_USE_SOCKET :
5045 + struct listening_socket_s *ls = sockets_list.data;
5047 + for (i = 0; i < sockets_list.used; i++, ls++) {
5049 + if (!strcmp(ls->key, key)) {
5058 + case FPM_STORE_SOCKET : /* inherited socket */
5059 + case FPM_STORE_USE_SOCKET : /* just created */
5062 + struct listening_socket_s *ls;
5064 + ls = fpm_array_push(&sockets_list);
5070 + if (op == FPM_STORE_SOCKET) {
5078 + ls->key = strdup(key);
5089 +static int fpm_sockets_new_listening_socket(struct fpm_worker_pool_s *wp, struct sockaddr *sa, int socklen)
5094 + mode_t saved_umask;
5096 + /* we have custom backlog value */
5097 + if (wp->config->listen_options) {
5098 + backlog = wp->config->listen_options->backlog;
5101 + sock = socket(sa->sa_family, SOCK_STREAM, 0);
5104 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "socket() failed");
5108 + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
5110 + if (wp->listen_address_domain == FPM_AF_UNIX) {
5111 + unlink( ((struct sockaddr_un *) sa)->sun_path);
5114 + saved_umask = umask(0777 ^ wp->socket_mode);
5116 + if (0 > bind(sock, sa, socklen)) {
5117 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "bind() for address '%s' failed", wp->config->listen_address);
5121 + if (wp->listen_address_domain == FPM_AF_UNIX) {
5123 + char *path = ((struct sockaddr_un *) sa)->sun_path;
5125 + if (wp->socket_uid != -1 || wp->socket_gid != -1) {
5127 + if (0 > chown(path, wp->socket_uid, wp->socket_gid)) {
5128 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chown() for address '%s' failed", wp->config->listen_address);
5136 + umask(saved_umask);
5138 + if (0 > listen(sock, backlog)) {
5139 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "listen() for address '%s' failed", wp->config->listen_address);
5146 +static int fpm_sockets_get_listening_socket(struct fpm_worker_pool_s *wp, struct sockaddr *sa, int socklen)
5150 + sock = fpm_sockets_hash_op(0, sa, 0, wp->listen_address_domain, FPM_GET_USE_SOCKET);
5152 + if (sock >= 0) return sock;
5154 + sock = fpm_sockets_new_listening_socket(wp, sa, socklen);
5156 + fpm_sockets_hash_op(sock, sa, 0, wp->listen_address_domain, FPM_STORE_USE_SOCKET);
5161 +enum fpm_address_domain fpm_sockets_domain_from_address(char *address)
5163 + if (strchr(address, ':')) return FPM_AF_INET;
5165 + if (strlen(address) == strspn(address, "0123456789")) return FPM_AF_INET;
5167 + return FPM_AF_UNIX;
5170 +static int fpm_socket_af_inet_listening_socket(struct fpm_worker_pool_s *wp)
5172 + struct sockaddr_in sa_in;
5173 + char *dup_address = strdup(wp->config->listen_address);
5174 + char *port_str = strchr(dup_address, ':');
5175 + char *addr = NULL;
5178 + if (port_str) { /* this is host:port pair */
5179 + *port_str++ = '\0';
5180 + port = atoi(port_str);
5181 + addr = dup_address;
5183 + else if (strlen(dup_address) == strspn(dup_address, "0123456789")) { /* this is port */
5184 + port = atoi(dup_address);
5185 + port_str = dup_address;
5189 + zlog(ZLOG_STUFF, ZLOG_ERROR, "invalid port value '%s'", port_str);
5193 + memset(&sa_in, 0, sizeof(sa_in));
5197 + sa_in.sin_addr.s_addr = inet_addr(addr);
5199 + if (sa_in.sin_addr.s_addr == INADDR_NONE) { /* do resolve */
5200 + if (0 > fpm_sockets_resolve_af_inet(addr, NULL, &sa_in)) {
5203 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "address '%s' resolved as %u.%u.%u.%u", addr, IPQUAD(&sa_in.sin_addr));
5208 + sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
5212 + sa_in.sin_family = AF_INET;
5213 + sa_in.sin_port = htons(port);
5215 + free(dup_address);
5217 + return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_in, sizeof(struct sockaddr_in));
5220 +static int fpm_socket_af_unix_listening_socket(struct fpm_worker_pool_s *wp)
5222 + struct sockaddr_un sa_un;
5224 + memset(&sa_un, 0, sizeof(sa_un));
5226 + cpystrn(sa_un.sun_path, wp->config->listen_address, sizeof(sa_un.sun_path));
5227 + sa_un.sun_family = AF_UNIX;
5229 + return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_un, sizeof(struct sockaddr_un));
5232 +int fpm_sockets_init_main()
5235 + struct fpm_worker_pool_s *wp;
5236 + char *inherited = getenv("FPM_SOCKETS");
5237 + struct listening_socket_s *ls;
5239 + if (0 == fpm_array_init(&sockets_list, sizeof(struct listening_socket_s), 10)) {
5243 + /* import inherited sockets */
5244 + while (inherited && *inherited) {
5245 + char *comma = strchr(inherited, ',');
5249 + if (comma) *comma = '\0';
5251 + eq = strchr(inherited, '=');
5256 + fd_no = atoi(eq + 1);
5258 + type = fpm_sockets_domain_from_address(inherited);
5260 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "using inherited socket fd=%d, \"%s\"", fd_no, inherited);
5262 + fpm_sockets_hash_op(fd_no, 0, inherited, type, FPM_STORE_SOCKET);
5265 + if (comma) inherited = comma + 1;
5266 + else inherited = 0;
5269 + /* create all required sockets */
5270 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
5272 + if (!wp->is_template) {
5274 + switch (wp->listen_address_domain) {
5276 + case FPM_AF_INET :
5278 + wp->listening_socket = fpm_socket_af_inet_listening_socket(wp);
5281 + case FPM_AF_UNIX :
5283 + if (0 > fpm_unix_resolve_socket_premissions(wp)) {
5287 + wp->listening_socket = fpm_socket_af_unix_listening_socket(wp);
5292 + if (wp->listening_socket == -1) {
5299 + /* close unused sockets that was inherited */
5300 + ls = sockets_list.data;
5302 + for (i = 0; i < sockets_list.used; ) {
5304 + if (ls->refcount == 0) {
5306 + if (ls->type == FPM_AF_UNIX) {
5310 + fpm_array_item_remove(&sockets_list, i);
5318 + fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_sockets_cleanup, 0);
5322 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_sockets.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_sockets.h
5323 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_sockets.h 1970-01-01 03:00:00.000000000 +0300
5324 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_sockets.h 2008-08-26 19:09:15.000000000 +0400
5328 + /* (c) 2007,2008 Andrei Nigmatulin */
5331 +#define FPM_MISC_H 1
5333 +#include <unistd.h>
5336 +#include "fpm_worker_pool.h"
5338 +enum fpm_address_domain fpm_sockets_domain_from_address(char *addr);
5339 +int fpm_sockets_init_main();
5342 +static inline int fd_set_blocked(int fd, int blocked)
5344 + int flags = fcntl(fd, F_GETFL);
5346 + if (flags < 0) return -1;
5349 + flags &= ~O_NONBLOCK;
5351 + flags |= O_NONBLOCK;
5353 + return fcntl(fd, F_SETFL, flags);
5356 +#define IPQUAD(sin_addr) \
5357 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[0], \
5358 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[1], \
5359 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[2], \
5360 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[3]
5363 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_stdio.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_stdio.c
5364 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_stdio.c 1970-01-01 03:00:00.000000000 +0300
5365 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_stdio.c 2008-05-24 21:38:47.000000000 +0400
5369 + /* (c) 2007,2008 Andrei Nigmatulin */
5371 +#include "fpm_config.h"
5373 +#include <sys/types.h>
5374 +#include <sys/stat.h>
5375 +#include <string.h>
5377 +#include <unistd.h>
5381 +#include "fpm_children.h"
5382 +#include "fpm_events.h"
5383 +#include "fpm_sockets.h"
5384 +#include "fpm_stdio.h"
5387 +static int fd_stdout[2];
5388 +static int fd_stderr[2];
5390 +int fpm_stdio_init_main()
5392 + int fd = open("/dev/null", O_RDWR);
5395 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"/dev/null\") failed");
5399 + dup2(fd, STDIN_FILENO);
5400 + dup2(fd, STDOUT_FILENO);
5406 +int fpm_stdio_init_final()
5408 + if (fpm_global_options.daemonize) {
5410 + if (fpm_globals.error_log_fd != STDERR_FILENO) {
5411 + /* there might be messages to stderr from libevent, we need to log them all */
5412 + dup2(fpm_globals.error_log_fd, STDERR_FILENO);
5415 + zlog_set_level(fpm_globals.log_level);
5417 + zlog_set_fd(fpm_globals.error_log_fd);
5423 +int fpm_stdio_init_child(struct fpm_worker_pool_s *wp)
5425 + close(fpm_globals.error_log_fd);
5426 + fpm_globals.error_log_fd = -1;
5429 + if (wp->listening_socket != STDIN_FILENO) {
5430 + dup2(wp->listening_socket, STDIN_FILENO);
5436 +static void fpm_stdio_child_said(int fd, short which, void *arg)
5438 + static const int max_buf_size = 1024;
5439 + char buf[max_buf_size];
5440 + struct fpm_child_s *child = arg;
5441 + int is_stdout = fd == child->fd_stdout;
5442 + struct event *ev = is_stdout ? &child->ev_stdout : &child->ev_stderr;
5443 + int fifo_in = 1, fifo_out = 1;
5444 + int is_last_message = 0;
5449 + zlog(ZLOG_STUFF, ZLOG_DEBUG, "child %d said %s", (int) child->pid, is_stdout ? "stdout" : "stderr");
5452 + while (fifo_in || fifo_out) {
5456 + res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf);
5458 + if (res <= 0) { /* no data */
5461 + if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
5462 + /* just no more data ready */
5464 + else { /* error or pipe is closed */
5466 + if (res < 0) { /* error */
5467 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
5470 + fpm_event_del(ev);
5471 + is_last_message = 1;
5474 + close(child->fd_stdout);
5475 + child->fd_stdout = -1;
5478 + close(child->fd_stderr);
5479 + child->fd_stderr = -1;
5483 + if (in_buf == 0 && !fpm_globals.is_child) {
5484 + zlog(ZLOG_STUFF, ZLOG_DEBUG, "child %d (pool %s) %s pipe is closed", (int) child->pid,
5485 + child->wp->config->name, is_stdout ? "stdout" : "stderr");
5496 + if (in_buf == 0) {
5501 + int should_print = 0;
5502 + buf[in_buf] = '\0';
5504 + /* FIXME: there might be binary data */
5506 + /* we should print if no more space in the buffer */
5507 + if (in_buf == max_buf_size - 1) {
5511 + /* we should print if no more data to come */
5516 + nl = strchr(buf, '\n');
5518 + if (nl || should_print) {
5524 + zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d (pool %s) said into %s: \"%s\"%s", (int) child->pid,
5525 + child->wp->config->name, is_stdout ? "stdout" : "stderr", buf, is_last_message ? ", pipe is closed" : "");
5528 + int out_buf = 1 + nl - buf;
5529 + memmove(buf, buf + out_buf, in_buf - out_buf);
5530 + in_buf -= out_buf;
5542 +int fpm_stdio_prepare_pipes(struct fpm_child_s *child)
5544 + if (0 == child->wp->config->catch_workers_output) { /* not required */
5548 + if (0 > pipe(fd_stdout)) {
5549 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
5553 + if (0 > pipe(fd_stderr)) {
5554 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
5555 + close(fd_stdout[0]); close(fd_stdout[1]);
5559 + if (0 > fd_set_blocked(fd_stdout[0], 0) || 0 > fd_set_blocked(fd_stderr[0], 0)) {
5560 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fd_set_blocked() failed");
5561 + close(fd_stdout[0]); close(fd_stdout[1]);
5562 + close(fd_stderr[0]); close(fd_stderr[1]);
5569 +int fpm_stdio_parent_use_pipes(struct fpm_child_s *child)
5571 + if (0 == child->wp->config->catch_workers_output) { /* not required */
5575 + close(fd_stdout[1]);
5576 + close(fd_stderr[1]);
5578 + child->fd_stdout = fd_stdout[0];
5579 + child->fd_stderr = fd_stderr[0];
5581 + fpm_event_add(child->fd_stdout, &child->ev_stdout, fpm_stdio_child_said, child);
5582 + fpm_event_add(child->fd_stderr, &child->ev_stderr, fpm_stdio_child_said, child);
5587 +int fpm_stdio_discard_pipes(struct fpm_child_s *child)
5589 + if (0 == child->wp->config->catch_workers_output) { /* not required */
5593 + close(fd_stdout[1]);
5594 + close(fd_stderr[1]);
5596 + close(fd_stdout[0]);
5597 + close(fd_stderr[0]);
5602 +void fpm_stdio_child_use_pipes(struct fpm_child_s *child)
5604 + if (child->wp->config->catch_workers_output) {
5605 + dup2(fd_stdout[1], STDOUT_FILENO);
5606 + dup2(fd_stderr[1], STDERR_FILENO);
5607 + close(fd_stdout[0]); close(fd_stdout[1]);
5608 + close(fd_stderr[0]); close(fd_stderr[1]);
5611 + /* stdout of parent is always /dev/null */
5612 + dup2(STDOUT_FILENO, STDERR_FILENO);
5616 +int fpm_stdio_open_error_log(int reopen)
5620 + fd = open(fpm_global_options.error_log, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
5623 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"%s\") failed", fpm_global_options.error_log);
5628 + if (fpm_global_options.daemonize) {
5629 + dup2(fd, STDERR_FILENO);
5632 + dup2(fd, fpm_globals.error_log_fd);
5634 + fd = fpm_globals.error_log_fd; /* for FD_CLOSEXEC to work */
5637 + fpm_globals.error_log_fd = fd;
5640 + fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
5644 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_stdio.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_stdio.h
5645 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_stdio.h 1970-01-01 03:00:00.000000000 +0300
5646 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_stdio.h 2008-05-24 21:38:47.000000000 +0400
5650 + /* (c) 2007,2008 Andrei Nigmatulin */
5652 +#ifndef FPM_STDIO_H
5653 +#define FPM_STDIO_H 1
5655 +#include "fpm_worker_pool.h"
5657 +int fpm_stdio_init_main();
5658 +int fpm_stdio_init_final();
5659 +int fpm_stdio_init_child(struct fpm_worker_pool_s *wp);
5660 +int fpm_stdio_prepare_pipes(struct fpm_child_s *child);
5661 +void fpm_stdio_child_use_pipes(struct fpm_child_s *child);
5662 +int fpm_stdio_parent_use_pipes(struct fpm_child_s *child);
5663 +int fpm_stdio_discard_pipes(struct fpm_child_s *child);
5664 +int fpm_stdio_open_error_log(int reopen);
5668 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_str.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_str.h
5669 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_str.h 1970-01-01 03:00:00.000000000 +0300
5670 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_str.h 2008-05-24 21:38:47.000000000 +0400
5674 + /* (c) 2007,2008 Andrei Nigmatulin */
5677 +#define FPM_STR_H 1
5679 +static inline char *cpystrn(char *dst, const char *src, size_t dst_size)
5683 + if (!dst_size) return dst;
5686 + end = dst + dst_size - 1;
5688 + for (; d < end; ++d, ++src) {
5689 + if (!(*d = *src)) {
5699 +static inline char *str_purify_filename(char *dst, char *src, size_t size)
5704 + end = dst + size - 1;
5706 + for (; d < end && *src; ++d, ++src) {
5707 + if (* (unsigned char *) src < ' ' || * (unsigned char *) src > '\x7f') {
5721 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace.c
5722 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace.c 1970-01-01 03:00:00.000000000 +0300
5723 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace.c 2008-07-21 00:59:00.000000000 +0400
5727 + /* (c) 2007,2008 Andrei Nigmatulin */
5729 +#include "fpm_config.h"
5731 +#include <sys/types.h>
5733 +#include "fpm_trace.h"
5735 +int fpm_trace_get_strz(char *buf, size_t sz, long addr)
5739 + char *lc = (char *) &l;
5741 + if (0 > fpm_trace_get_long(addr, &l)) {
5745 + i = l % SIZEOF_LONG;
5749 + for (addr = l; ; addr += SIZEOF_LONG) {
5751 + if (0 > fpm_trace_get_long(addr, &l)) {
5755 + for ( ; i < SIZEOF_LONG; i++) {
5758 + if (sz && lc[i]) {
5771 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace.h
5772 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace.h 1970-01-01 03:00:00.000000000 +0300
5773 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace.h 2008-07-21 07:04:25.000000000 +0400
5777 + /* (c) 2007,2008 Andrei Nigmatulin */
5779 +#ifndef FPM_TRACE_H
5780 +#define FPM_TRACE_H 1
5782 +#include <unistd.h>
5784 +int fpm_trace_signal(pid_t pid);
5785 +int fpm_trace_ready(pid_t pid);
5786 +int fpm_trace_close(pid_t pid);
5787 +int fpm_trace_get_long(long addr, long *data);
5788 +int fpm_trace_get_strz(char *buf, size_t sz, long addr);
5792 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
5793 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace_mach.c 1970-01-01 03:00:00.000000000 +0300
5794 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace_mach.c 2008-08-26 19:09:15.000000000 +0400
5798 + /* (c) 2007,2008 Andrei Nigmatulin */
5800 +#include "fpm_config.h"
5802 +#include <mach/mach.h>
5803 +#include <mach/mach_vm.h>
5805 +#include <unistd.h>
5807 +#include "fpm_trace.h"
5808 +#include "fpm_process_ctl.h"
5809 +#include "fpm_unix.h"
5813 +static mach_port_name_t target;
5814 +static vm_offset_t target_page_base;
5815 +static vm_offset_t local_page;
5816 +static mach_msg_type_number_t local_size;
5818 +static void fpm_mach_vm_deallocate()
5821 + mach_vm_deallocate(mach_task_self(), local_page, local_size);
5822 + target_page_base = 0;
5828 +static int fpm_mach_vm_read_page(vm_offset_t page)
5832 + kr = mach_vm_read(target, page, fpm_pagesize, &local_page, &local_size);
5834 + if (kr != KERN_SUCCESS) {
5835 + zlog(ZLOG_STUFF, ZLOG_ERROR, "mach_vm_read() failed: %s (%d)", mach_error_string(kr), kr);
5842 +int fpm_trace_signal(pid_t pid)
5844 + if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
5845 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "kill(SIGSTOP) failed");
5852 +int fpm_trace_ready(pid_t pid)
5856 + kr = task_for_pid(mach_task_self(), pid, &target);
5858 + if (kr != KERN_SUCCESS) {
5861 + if (kr == KERN_FAILURE) {
5862 + msg = " It seems that master process does not have enough privileges to trace processes.";
5865 + zlog(ZLOG_STUFF, ZLOG_ERROR, "task_for_pid() failed: %s (%d)%s", mach_error_string(kr), kr, msg);
5872 +int fpm_trace_close(pid_t pid)
5874 + fpm_mach_vm_deallocate();
5881 +int fpm_trace_get_long(long addr, long *data)
5883 + size_t offset = ((uintptr_t) (addr) % fpm_pagesize);
5884 + vm_offset_t base = (uintptr_t) (addr) - offset;
5886 + if (base != target_page_base) {
5887 + fpm_mach_vm_deallocate();
5888 + if (0 > fpm_mach_vm_read_page(base)) {
5893 + *data = * (long *) (local_page + offset);
5898 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
5899 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace_pread.c 1970-01-01 03:00:00.000000000 +0300
5900 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace_pread.c 2008-08-26 19:09:15.000000000 +0400
5904 + /* (c) 2007,2008 Andrei Nigmatulin */
5906 +#define _GNU_SOURCE
5907 +#define _FILE_OFFSET_BITS 64
5909 +#include "fpm_config.h"
5911 +#include <unistd.h>
5915 +#include <stdint.h>
5917 +#include "fpm_trace.h"
5918 +#include "fpm_process_ctl.h"
5922 +static int mem_file = -1;
5924 +int fpm_trace_signal(pid_t pid)
5926 + if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
5927 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "kill(SIGSTOP) failed");
5934 +int fpm_trace_ready(pid_t pid)
5938 + sprintf(buf, "/proc/%d/" PROC_MEM_FILE, (int) pid);
5940 + mem_file = open(buf, O_RDONLY);
5942 + if (0 > mem_file) {
5943 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(%s) failed", buf);
5950 +int fpm_trace_close(pid_t pid)
5959 +int fpm_trace_get_long(long addr, long *data)
5961 + if (sizeof(*data) != pread(mem_file, (void *) data, sizeof(*data), (uintptr_t) addr)) {
5962 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pread() failed");
5969 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
5970 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_trace_ptrace.c 1970-01-01 03:00:00.000000000 +0300
5971 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_trace_ptrace.c 2008-09-19 03:34:11.000000000 +0400
5975 + /* (c) 2007,2008 Andrei Nigmatulin */
5977 +#include "fpm_config.h"
5979 +#include <sys/wait.h>
5980 +#include <sys/ptrace.h>
5981 +#include <unistd.h>
5984 +#if defined(PT_ATTACH) && !defined(PTRACE_ATTACH)
5985 +#define PTRACE_ATTACH PT_ATTACH
5988 +#if defined(PT_DETACH) && !defined(PTRACE_DETACH)
5989 +#define PTRACE_DETACH PT_DETACH
5992 +#if defined(PT_READ_D) && !defined(PTRACE_PEEKDATA)
5993 +#define PTRACE_PEEKDATA PT_READ_D
5996 +#include "fpm_trace.h"
5999 +static pid_t traced_pid;
6001 +int fpm_trace_signal(pid_t pid)
6003 + if (0 > ptrace(PTRACE_ATTACH, pid, 0, 0)) {
6004 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(ATTACH) failed");
6011 +int fpm_trace_ready(pid_t pid)
6018 +int fpm_trace_close(pid_t pid)
6020 + if (0 > ptrace(PTRACE_DETACH, pid, (void *) 1, 0)) {
6021 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(DETACH) failed");
6030 +int fpm_trace_get_long(long addr, long *data)
6033 + struct ptrace_io_desc ptio = {
6034 + .piod_op = PIOD_READ_D,
6035 + .piod_offs = (void *) addr,
6036 + .piod_addr = (void *) data,
6037 + .piod_len = sizeof(long)
6040 + if (0 > ptrace(PT_IO, traced_pid, (void *) &ptio, 0)) {
6041 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(PT_IO) failed");
6047 + *data = ptrace(PTRACE_PEEKDATA, traced_pid, (void *) addr, 0);
6050 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(PEEKDATA) failed");
6058 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_unix.c php-5.2.6.fpm/sapi/cgi/fpm/fpm_unix.c
6059 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_unix.c 1970-01-01 03:00:00.000000000 +0300
6060 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_unix.c 2008-09-19 03:19:59.000000000 +0400
6064 + /* (c) 2007,2008 Andrei Nigmatulin */
6066 +#include "fpm_config.h"
6068 +#include <string.h>
6069 +#include <sys/time.h>
6070 +#include <sys/resource.h>
6071 +#include <stdlib.h>
6072 +#include <unistd.h>
6073 +#include <sys/types.h>
6078 +#include <sys/prctl.h>
6082 +#include "fpm_conf.h"
6083 +#include "fpm_cleanup.h"
6084 +#include "fpm_clock.h"
6085 +#include "fpm_stdio.h"
6086 +#include "fpm_unix.h"
6089 +size_t fpm_pagesize;
6091 +int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp)
6093 + struct fpm_listen_options_s *lo = wp->config->listen_options;
6095 + /* uninitialized */
6096 + wp->socket_uid = -1;
6097 + wp->socket_gid = -1;
6098 + wp->socket_mode = 0666;
6100 + if (!lo) return 0;
6102 + if (lo->owner && *lo->owner) {
6103 + struct passwd *pwd;
6105 + pwd = getpwnam(lo->owner);
6108 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "cannot get uid for user '%s', pool '%s'", lo->owner, wp->config->name);
6112 + wp->socket_uid = pwd->pw_uid;
6113 + wp->socket_gid = pwd->pw_gid;
6116 + if (lo->group && *lo->group) {
6117 + struct group *grp;
6119 + grp = getgrnam(lo->group);
6122 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "cannot get gid for group '%s', pool '%s'", lo->group, wp->config->name);
6126 + wp->socket_gid = grp->gr_gid;
6129 + if (lo->mode && *lo->mode) {
6130 + wp->socket_mode = strtoul(lo->mode, 0, 8);
6136 +static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp)
6138 + int is_root = !geteuid();
6141 + if (wp->config->user && *wp->config->user) {
6143 + if (strlen(wp->config->user) == strspn(wp->config->user, "0123456789")) {
6144 + wp->set_uid = strtoul(wp->config->user, 0, 10);
6147 + struct passwd *pwd;
6149 + pwd = getpwnam(wp->config->user);
6152 + zlog(ZLOG_STUFF, ZLOG_ERROR, "cannot get uid for user '%s', pool '%s'", wp->config->user, wp->config->name);
6156 + wp->set_uid = pwd->pw_uid;
6157 + wp->set_gid = pwd->pw_gid;
6159 + wp->user = strdup(pwd->pw_name);
6160 + wp->home = strdup(pwd->pw_dir);
6164 + if (wp->config->group && *wp->config->group) {
6166 + if (strlen(wp->config->group) == strspn(wp->config->group, "0123456789")) {
6167 + wp->set_gid = strtoul(wp->config->group, 0, 10);
6170 + struct group *grp;
6172 + grp = getgrnam(wp->config->group);
6175 + zlog(ZLOG_STUFF, ZLOG_ERROR, "cannot get gid for group '%s', pool '%s'", wp->config->group, wp->config->name);
6179 + wp->set_gid = grp->gr_gid;
6183 +#ifndef I_REALLY_WANT_ROOT_PHP
6184 + if (wp->set_uid == 0 || wp->set_gid == 0) {
6185 + zlog(ZLOG_STUFF, ZLOG_ERROR, "please specify user and group other than root, pool '%s'", wp->config->name);
6190 + else { /* not root */
6191 + if (wp->config->user && *wp->config->user) {
6192 + zlog(ZLOG_STUFF, ZLOG_WARNING, "'user' directive is ignored, pool '%s'", wp->config->name);
6194 + if (wp->config->group && *wp->config->group) {
6195 + zlog(ZLOG_STUFF, ZLOG_WARNING, "'group' directive is ignored, pool '%s'", wp->config->name);
6197 + if (wp->config->chroot && *wp->config->chroot) {
6198 + zlog(ZLOG_STUFF, ZLOG_WARNING, "'chroot' directive is ignored, pool '%s'", wp->config->name);
6201 + { /* set up HOME and USER anyway */
6202 + struct passwd *pwd;
6204 + pwd = getpwuid(getuid());
6207 + wp->user = strdup(pwd->pw_name);
6208 + wp->home = strdup(pwd->pw_dir);
6216 +int fpm_unix_init_child(struct fpm_worker_pool_s *wp)
6218 + int is_root = !geteuid();
6219 + int made_chroot = 0;
6221 + if (wp->config->rlimit_files) {
6224 + getrlimit(RLIMIT_NOFILE, &r);
6226 + r.rlim_cur = (rlim_t) wp->config->rlimit_files;
6228 + if (0 > setrlimit(RLIMIT_NOFILE, &r)) {
6229 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setrlimit(RLIMIT_NOFILE) failed");
6233 + if (wp->config->rlimit_core) {
6236 + getrlimit(RLIMIT_CORE, &r);
6238 + r.rlim_cur = wp->config->rlimit_core == -1 ? (rlim_t) RLIM_INFINITY : (rlim_t) wp->config->rlimit_core;
6240 + if (0 > setrlimit(RLIMIT_CORE, &r)) {
6241 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setrlimit(RLIMIT_CORE) failed");
6245 + if (is_root && wp->config->chroot && *wp->config->chroot) {
6246 + if (0 > chroot(wp->config->chroot)) {
6247 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chroot(%s) failed", wp->config->chroot);
6253 + if (wp->config->chdir && *wp->config->chdir) {
6254 + if (0 > chdir(wp->config->chdir)) {
6255 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chdir(%s) failed", wp->config->chdir);
6259 + else if (made_chroot) {
6264 + if (wp->set_gid) {
6265 + if (0 > setgid(wp->set_gid)) {
6266 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setgid(%d) failed", wp->set_gid);
6270 + if (wp->set_uid) {
6271 + if (0 > initgroups(wp->config->user, wp->set_gid)) {
6272 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "initgroups(%s, %d) failed", wp->config->user, wp->set_gid);
6275 + if (0 > setuid(wp->set_uid)) {
6276 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setuid(%d) failed", wp->set_uid);
6283 + if (0 > prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)) {
6284 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "prctl(PR_SET_DUMPABLE) failed");
6288 + if (0 > fpm_clock_init()) {
6295 +int fpm_unix_init_main()
6297 + struct fpm_worker_pool_s *wp;
6299 + fpm_pagesize = getpagesize();
6301 + if (fpm_global_options.daemonize) {
6307 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fork() failed");
6316 + fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT);
6325 + if (0 > fpm_clock_init()) {
6329 + fpm_globals.parent_pid = getpid();
6331 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
6333 + if (0 > fpm_unix_conf_wp(wp)) {
6339 + fpm_stdio_init_final();
6343 + getrlimit(RLIMIT_NOFILE, &r);
6345 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "getrlimit(nofile): max:%lld, cur:%lld",
6346 + (long long) r.rlim_max, (long long) r.rlim_cur);
6351 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/fpm_unix.h php-5.2.6.fpm/sapi/cgi/fpm/fpm_unix.h
6352 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_unix.h 1970-01-01 03:00:00.000000000 +0300
6353 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_unix.h 2008-05-25 17:21:13.000000000 +0400
6357 + /* (c) 2007,2008 Andrei Nigmatulin */
6360 +#define FPM_UNIX_H 1
6362 +#include "fpm_worker_pool.h"
6364 +int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp);
6365 +int fpm_unix_init_child(struct fpm_worker_pool_s *wp);
6366 +int fpm_unix_init_main();
6368 +extern size_t fpm_pagesize;
6372 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
6373 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_worker_pool.c 1970-01-01 03:00:00.000000000 +0300
6374 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_worker_pool.c 2008-08-26 19:09:15.000000000 +0400
6378 + /* (c) 2007,2008 Andrei Nigmatulin */
6380 +#include "fpm_config.h"
6382 +#include <string.h>
6383 +#include <stdlib.h>
6384 +#include <unistd.h>
6386 +#include "fpm_worker_pool.h"
6387 +#include "fpm_cleanup.h"
6388 +#include "fpm_children.h"
6389 +#include "fpm_shm.h"
6390 +#include "fpm_shm_slots.h"
6391 +#include "fpm_conf.h"
6393 +struct fpm_worker_pool_s *fpm_worker_all_pools;
6395 +static void fpm_worker_pool_cleanup(int which, void *arg)
6397 + struct fpm_worker_pool_s *wp, *wp_next;
6399 + for (wp = fpm_worker_all_pools; wp; wp = wp_next) {
6400 + wp_next = wp->next;
6401 + fpm_worker_pool_config_free(wp->config);
6402 + fpm_children_free(wp->children);
6403 + fpm_array_free(&wp->slots_used);
6404 + fpm_array_free(&wp->slots_free);
6405 + fpm_shm_free_list(wp->shm_list, which == FPM_CLEANUP_CHILD ? fpm_shm_slots_mem() : 0);
6412 + fpm_worker_all_pools = 0;
6415 +struct fpm_worker_pool_s *fpm_worker_pool_alloc()
6417 + struct fpm_worker_pool_s *ret;
6419 + ret = malloc(sizeof(struct fpm_worker_pool_s));
6425 + memset(ret, 0, sizeof(struct fpm_worker_pool_s));
6427 + if (!fpm_worker_all_pools) {
6428 + fpm_worker_all_pools = ret;
6431 + fpm_array_init(&ret->slots_used, sizeof(struct fpm_shm_slot_ptr_s), 50);
6432 + fpm_array_init(&ret->slots_free, sizeof(struct fpm_shm_slot_ptr_s), 50);
6437 +int fpm_worker_pool_init_main()
6439 + fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_worker_pool_cleanup, 0);
6443 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
6444 --- php-5.2.6.vanilla/sapi/cgi/fpm/fpm_worker_pool.h 1970-01-01 03:00:00.000000000 +0300
6445 +++ php-5.2.6.fpm/sapi/cgi/fpm/fpm_worker_pool.h 2008-08-26 19:09:15.000000000 +0400
6449 + /* (c) 2007,2008 Andrei Nigmatulin */
6451 +#ifndef FPM_WORKER_POOL_H
6452 +#define FPM_WORKER_POOL_H 1
6454 +#include "fpm_conf.h"
6455 +#include "fpm_arrays.h"
6457 +struct fpm_worker_pool_s;
6458 +struct fpm_child_s;
6459 +struct fpm_child_stat_s;
6462 +enum fpm_address_domain {
6467 +struct fpm_worker_pool_s {
6468 + struct fpm_worker_pool_s *next;
6469 + struct fpm_worker_pool_config_s *config;
6470 + char *user, *home; /* for setting env USER and HOME */
6471 + enum fpm_address_domain listen_address_domain;
6472 + int listening_socket;
6473 + int set_uid, set_gid; /* config uid and gid */
6474 + unsigned is_template:1; /* just config template, no processes will be created */
6475 + int socket_uid, socket_gid, socket_mode;
6477 + struct fpm_shm_s *shm_list;
6478 + struct fpm_array_s slots_used;
6479 + struct fpm_array_s slots_free;
6482 + struct fpm_child_s *children;
6483 + int running_children;
6486 +struct fpm_worker_pool_s *fpm_worker_pool_alloc();
6487 +int fpm_worker_pool_init_main();
6489 +extern struct fpm_worker_pool_s *fpm_worker_all_pools;
6493 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
6494 --- php-5.2.6.vanilla/sapi/cgi/fpm/init.d/php-fpm.in 1970-01-01 03:00:00.000000000 +0300
6495 +++ php-5.2.6.fpm/sapi/cgi/fpm/init.d/php-fpm.in 2008-08-05 20:31:27.000000000 +0400
6499 +php_fpm_BIN=@prefix@/bin/php-cgi
6500 +php_fpm_CONF=@php_fpm_conf_path@
6501 +php_fpm_PID=@php_fpm_pid_path@
6504 +php_opts="--fpm-config $php_fpm_CONF"
6510 + while test $try -lt 35 ; do
6514 + if [ -f "$2" ] ; then
6521 + if [ ! -f "$2" ] ; then
6529 + try=`expr $try + 1`
6538 + echo -n "Starting php_fpm "
6540 + $php_fpm_BIN --fpm $php_opts
6542 + if [ "$?" != 0 ] ; then
6547 + wait_for_pid created $php_fpm_PID
6549 + if [ -n "$try" ] ; then
6558 + echo -n "Shutting down php_fpm "
6560 + if [ ! -r $php_fpm_PID ] ; then
6561 + echo "warning, no pid file found - php-fpm is not running ?"
6565 + kill -TERM `cat $php_fpm_PID`
6567 + wait_for_pid removed $php_fpm_PID
6569 + if [ -n "$try" ] ; then
6578 + echo -n "Gracefully shutting down php_fpm "
6580 + if [ ! -r $php_fpm_PID ] ; then
6581 + echo "warning, no pid file found - php-fpm is not running ?"
6585 + kill -QUIT `cat $php_fpm_PID`
6587 + wait_for_pid removed $php_fpm_PID
6589 + if [ -n "$try" ] ; then
6604 + echo -n "Reload service php-fpm "
6606 + if [ ! -r $php_fpm_PID ] ; then
6607 + echo "warning, no pid file found - php-fpm is not running ?"
6611 + kill -USR2 `cat $php_fpm_PID`
6618 + echo -n "Re-opening php-fpm log file "
6620 + if [ ! -r $php_fpm_PID ] ; then
6621 + echo "warning, no pid file found - php-fpm is not running ?"
6625 + kill -USR1 `cat $php_fpm_PID`
6631 + echo "Usage: $0 {start|stop|quit|restart|reload|logrotate}"
6636 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/Makefile.frag php-5.2.6.fpm/sapi/cgi/fpm/Makefile.frag
6637 --- php-5.2.6.vanilla/sapi/cgi/fpm/Makefile.frag 1970-01-01 03:00:00.000000000 +0300
6638 +++ php-5.2.6.fpm/sapi/cgi/fpm/Makefile.frag 2008-03-28 16:51:22.000000000 +0300
6641 +install-fpm: sapi/cgi/fpm/php-fpm.conf sapi/cgi/fpm/php-fpm
6642 + @echo "Installing FPM config: $(INSTALL_ROOT)$(php_fpm_conf_path)"
6643 + -@$(mkinstalldirs) \
6644 + $(INSTALL_ROOT)$(prefix)/sbin \
6645 + `dirname "$(INSTALL_ROOT)$(php_fpm_conf_path)"` \
6646 + `dirname "$(INSTALL_ROOT)$(php_fpm_log_path)"` \
6647 + `dirname "$(INSTALL_ROOT)$(php_fpm_pid_path)"`
6648 + -@if test -r "$(INSTALL_ROOT)$(php_fpm_conf_path)" ; then \
6649 + dest=`basename "$(php_fpm_conf_path)"`.default ; \
6650 + echo " (installing as $$dest)" ; \
6652 + dest=`basename "$(php_fpm_conf_path)"` ; \
6654 + $(INSTALL_DATA) $(top_builddir)/sapi/cgi/fpm/php-fpm.conf $(INSTALL_ROOT)`dirname "$(php_fpm_conf_path)"`/$$dest
6655 + @echo "Installing init.d script: $(INSTALL_ROOT)$(prefix)/sbin/php-fpm"
6656 + -@$(INSTALL) -m 0755 $(top_builddir)/sapi/cgi/fpm/php-fpm $(INSTALL_ROOT)$(prefix)/sbin/php-fpm
6658 +$(top_builddir)/libevent/libevent.a: $(top_builddir)/libevent/Makefile
6659 + cd $(top_builddir)/libevent && $(MAKE) libevent.a
6661 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/xml_config.c php-5.2.6.fpm/sapi/cgi/fpm/xml_config.c
6662 --- php-5.2.6.vanilla/sapi/cgi/fpm/xml_config.c 1970-01-01 03:00:00.000000000 +0300
6663 +++ php-5.2.6.fpm/sapi/cgi/fpm/xml_config.c 2008-08-26 19:09:15.000000000 +0400
6667 + /* (c) 2004-2007 Andrei Nigmatulin */
6669 +#include "fpm_config.h"
6671 +#ifdef HAVE_ALLOCA_H
6672 +#include <alloca.h>
6674 +#include <string.h>
6676 +#include <stddef.h>
6677 +#include <stdlib.h>
6679 +#include <libxml/parser.h>
6680 +#include <libxml/tree.h>
6682 +#include "xml_config.h"
6684 +static struct xml_conf_section **xml_conf_sections = 0;
6685 +static int xml_conf_sections_allocated = 0;
6686 +static int xml_conf_sections_used = 0;
6688 +char *xml_conf_set_slot_boolean(void **conf, char *name, void *vv, intptr_t offset)
6691 + long value_y = !strcasecmp(value, "yes") || !strcmp(value, "1") || !strcasecmp(value, "on");
6692 + long value_n = !strcasecmp(value, "no") || !strcmp(value, "0") || !strcasecmp(value, "off");
6694 + if (!value_y && !value_n) {
6695 + return "xml_conf_set_slot(): invalid boolean value";
6698 +#ifdef XML_CONF_DEBUG
6699 + fprintf(stderr, "setting boolean '%s' => %s\n", name, value_y ? "TRUE" : "FALSE");
6702 + * (int *) ((char *) *conf + offset) = value_y ? 1 : 0;
6707 +char *xml_conf_set_slot_string(void **conf, char *name, void *vv, intptr_t offset)
6710 + char *v = strdup(value);
6712 + if (!v) return "xml_conf_set_slot_string(): strdup() failed";
6714 +#ifdef XML_CONF_DEBUG
6715 + fprintf(stderr, "setting string '%s' => '%s'\n", name, v);
6718 + * (char **) ((char *) *conf + offset) = v;
6723 +char *xml_conf_set_slot_integer(void **conf, char *name, void *vv, intptr_t offset)
6726 + int v = atoi(value);
6728 + * (int *) ((char *) *conf + offset) = v;
6730 +#ifdef XML_CONF_DEBUG
6731 + fprintf(stderr, "setting integer '%s' => %d\n", name, v);
6737 +char *xml_conf_set_slot_time(void **conf, char *name, void *vv, intptr_t offset)
6740 + int len = strlen(value);
6744 + if (!len) return "xml_conf_set_slot_timeval(): invalid timeval value";
6746 + suffix = value[len-1];
6748 + value[len-1] = '\0';
6752 + seconds = atoi(value);
6755 + seconds = 60 * atoi(value);
6758 + seconds = 60 * 60 * atoi(value);
6761 + seconds = 24 * 60 * 60 * atoi(value);
6764 + return "xml_conf_set_slot_timeval(): unknown suffix used in timeval value";
6767 + * (int *) ((char *) *conf + offset) = seconds;
6769 +#ifdef XML_CONF_DEBUG
6770 + fprintf(stderr, "setting time '%s' => %d:%02d:%02d:%02d\n", name, expand_dhms(seconds));
6776 +char *xml_conf_parse_section(void **conf, struct xml_conf_section *section, void *xml_node)
6778 + xmlNode *element = xml_node;
6781 +#ifdef XML_CONF_DEBUG
6782 + fprintf(stderr, "processing a section %s\n", section->path);
6785 + for ( ; element; element = element->next) {
6786 + if (element->type == XML_ELEMENT_NODE && !strcmp((const char *) element->name, "value") && element->children) {
6787 + xmlChar *name = xmlGetProp(element, (unsigned char *) "name");
6792 +#ifdef XML_CONF_DEBUG
6793 + fprintf(stderr, "found a value: %s\n", name);
6795 + for (i = 0; section->parsers[i].parser; i++) {
6796 + if (!section->parsers[i].name || !strcmp(section->parsers[i].name, (char *) name)) {
6801 + if (section->parsers[i].parser) {
6802 + if (section->parsers[i].type == XML_CONF_SCALAR) {
6803 + if (element->children->type == XML_TEXT_NODE) {
6804 + ret = section->parsers[i].parser(conf, (char *) name, element->children->content, section->parsers[i].offset);
6807 + ret = "XML_TEXT_NODE is expected, something different is given";
6811 + ret = section->parsers[i].parser(conf, (char *) name, element->children, section->parsers[i].offset);
6815 + if (ret) return ret;
6819 + fprintf(stderr, "Warning, unknown setting '%s' in section '%s'\n", (char *) name, section->path);
6829 +static char *xml_conf_parse_file(xmlNode *element)
6833 + for ( ; element; element = element->next) {
6835 + if (element->parent && element->type == XML_ELEMENT_NODE && !strcmp((const char *) element->name, "section")) {
6836 + xmlChar *name = xmlGetProp(element, (unsigned char *) "name");
6839 + char *parent_name = (char *) xmlGetNodePath(element->parent);
6842 + struct xml_conf_section *section = NULL;
6844 +#ifdef XML_CONF_DEBUG
6845 + fprintf(stderr, "got a section: %s/%s\n", parent_name, name);
6847 + full_name = alloca(strlen(parent_name) + strlen((char *) name) + 1 + 1);
6849 + sprintf(full_name, "%s/%s", parent_name, (char *) name);
6851 + xmlFree(parent_name);
6854 + for (i = 0; i < xml_conf_sections_used; i++) {
6855 + if (!strcmp(xml_conf_sections[i]->path, full_name)) {
6856 + section = xml_conf_sections[i];
6860 + if (section) { /* found a registered section */
6861 + void *conf = section->conf();
6862 + ret = xml_conf_parse_section(&conf, section, element->children);
6869 + if (element->children) {
6870 + ret = xml_conf_parse_file(element->children);
6878 +char *xml_conf_load_file(char *file)
6883 + LIBXML_TEST_VERSION
6885 + doc = xmlParseFile(file);
6888 + ret = xml_conf_parse_file(doc->children);
6892 + ret = "failed to parse conf file";
6895 + xmlCleanupParser();
6899 +int xml_conf_init()
6904 +void xml_conf_clean()
6906 + if (xml_conf_sections) {
6907 + free(xml_conf_sections);
6911 +int xml_conf_section_register(struct xml_conf_section *section)
6913 + if (xml_conf_sections_allocated == xml_conf_sections_used) {
6914 + int new_size = xml_conf_sections_used + 10;
6915 + void *new_ptr = realloc(xml_conf_sections, sizeof(struct xml_conf_section *) * new_size);
6918 + xml_conf_sections = new_ptr;
6919 + xml_conf_sections_allocated = new_size;
6922 + fprintf(stderr, "xml_conf_section_register(): out of memory\n");
6927 + xml_conf_sections[xml_conf_sections_used++] = section;
6932 +int xml_conf_sections_register(struct xml_conf_section *sections[])
6934 + for ( ; sections && *sections; sections++) {
6935 + if (0 > xml_conf_section_register(*sections)) {
6943 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/xml_config.h php-5.2.6.fpm/sapi/cgi/fpm/xml_config.h
6944 --- php-5.2.6.vanilla/sapi/cgi/fpm/xml_config.h 1970-01-01 03:00:00.000000000 +0300
6945 +++ php-5.2.6.fpm/sapi/cgi/fpm/xml_config.h 2008-09-19 03:02:58.000000000 +0400
6949 + /* (c) 2004-2007 Andrei Nigmatulin */
6951 +#ifndef XML_CONFIG_H
6952 +#define XML_CONFIG_H 1
6954 +#include <stdint.h>
6956 +struct xml_value_parser;
6958 +struct xml_value_parser {
6961 + char *(*parser)(void **, char *, void *, intptr_t offset);
6965 +struct xml_conf_section {
6968 + struct xml_value_parser *parsers;
6971 +char *xml_conf_set_slot_boolean(void **conf, char *name, void *value, intptr_t offset);
6972 +char *xml_conf_set_slot_string(void **conf, char *name, void *value, intptr_t offset);
6973 +char *xml_conf_set_slot_integer(void **conf, char *name, void *value, intptr_t offset);
6974 +char *xml_conf_set_slot_time(void **conf, char *name, void *value, intptr_t offset);
6976 +int xml_conf_init();
6977 +void xml_conf_clean();
6978 +char *xml_conf_load_file(char *file);
6979 +char *xml_conf_parse_section(void **conf, struct xml_conf_section *section, void *ve);
6980 +int xml_conf_section_register(struct xml_conf_section *section);
6981 +int xml_conf_sections_register(struct xml_conf_section *sections[]);
6983 +#define expand_hms(value) (value) / 3600, ((value) % 3600) / 60, (value) % 60
6985 +#define expand_dhms(value) (value) / 86400, ((value) % 86400) / 3600, ((value) % 3600) / 60, (value) % 60
6987 +enum { XML_CONF_SCALAR = 1, XML_CONF_SUBSECTION = 2 };
6990 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/zlog.c php-5.2.6.fpm/sapi/cgi/fpm/zlog.c
6991 --- php-5.2.6.vanilla/sapi/cgi/fpm/zlog.c 1970-01-01 03:00:00.000000000 +0300
6992 +++ php-5.2.6.fpm/sapi/cgi/fpm/zlog.c 2008-05-23 01:08:32.000000000 +0400
6996 + /* (c) 2004-2007 Andrei Nigmatulin */
6998 +#include "fpm_config.h"
7001 +#include <unistd.h>
7003 +#include <string.h>
7004 +#include <stdarg.h>
7005 +#include <sys/time.h>
7010 +#define MAX_LINE_LENGTH 1024
7012 +static int zlog_fd = -1;
7013 +static int zlog_level = ZLOG_NOTICE;
7015 +static const char *level_names[] = {
7016 + [ZLOG_DEBUG] = "DEBUG",
7017 + [ZLOG_NOTICE] = "NOTICE",
7018 + [ZLOG_WARNING] = "WARNING",
7019 + [ZLOG_ERROR] = "ERROR",
7020 + [ZLOG_ALERT] = "ALERT",
7023 +size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len)
7028 + len = strftime(timebuf, timebuf_len, "%b %d %H:%M:%S", localtime_r((const time_t *) &tv->tv_sec, &t));
7029 + len += snprintf(timebuf + len, timebuf_len - len, ".%06d", (int) tv->tv_usec);
7034 +int zlog_set_fd(int new_fd)
7036 + int old_fd = zlog_fd;
7042 +int zlog_set_level(int new_value)
7044 + int old_value = zlog_level;
7046 + zlog_level = new_value;
7051 +void zlog(const char *function, int line, int flags, const char *fmt, ...)
7053 + struct timeval tv;
7054 + char buf[MAX_LINE_LENGTH];
7055 + const size_t buf_size = MAX_LINE_LENGTH;
7058 + int truncated = 0;
7061 + if ((flags & ZLOG_LEVEL_MASK) < zlog_level) {
7065 + saved_errno = errno;
7067 + gettimeofday(&tv, 0);
7069 + len = zlog_print_time(&tv, buf, buf_size);
7071 + len += snprintf(buf + len, buf_size - len, " [%s] %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
7073 + if (len > buf_size - 1) {
7078 + va_start(args, fmt);
7080 + len += vsnprintf(buf + len, buf_size - len, fmt, args);
7084 + if (len >= buf_size) {
7090 + if (flags & ZLOG_HAVE_ERRNO) {
7091 + len += snprintf(buf + len, buf_size - len, ": %s (%d)", strerror(saved_errno), saved_errno);
7092 + if (len >= buf_size) {
7099 + memcpy(buf + buf_size - sizeof("..."), "...", sizeof("...") - 1);
7100 + len = buf_size - 1;
7103 + buf[len++] = '\n';
7105 + write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, buf, len);
7107 diff -Nru php-5.2.6.vanilla/sapi/cgi/fpm/zlog.h php-5.2.6.fpm/sapi/cgi/fpm/zlog.h
7108 --- php-5.2.6.vanilla/sapi/cgi/fpm/zlog.h 1970-01-01 03:00:00.000000000 +0300
7109 +++ php-5.2.6.fpm/sapi/cgi/fpm/zlog.h 2008-05-23 01:08:32.000000000 +0400
7113 + /* (c) 2004-2007 Andrei Nigmatulin */
7118 +#define ZLOG_STUFF __func__, __LINE__
7122 +int zlog_set_fd(int new_fd);
7123 +int zlog_set_level(int new_value);
7125 +size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len);
7127 +void zlog(const char *function, int line, int flags, const char *fmt, ...)
7128 + __attribute__ ((format(printf,4,5)));
7138 +#define ZLOG_LEVEL_MASK 7
7140 +#define ZLOG_HAVE_ERRNO 0x100
7142 +#define ZLOG_SYSERROR (ZLOG_ERROR | ZLOG_HAVE_ERRNO)
7145 --- php-5.2.6/sapi/cgi/Makefile.frag~ 2008-11-03 19:29:12.000000000 +0200
7146 +++ php-5.2.6/sapi/cgi/Makefile.frag 2008-11-03 19:31:19.361921087 +0200
7148 -$(SAPI_CGI_PATH): libphp_common.la $(PHP_SAPI_OBJS)
7149 +$(SAPI_CGI_PATH): libphp_common.la $(PHP_SAPI_OBJS) $(SAPI_EXTRA_DEPS)