1 diff --git a/configure b/configure
2 index 2d88ed7..c490abf 100755
3 diff --git a/configure.in b/configure.in
4 index e181ac9..4424930 100644
7 @@ -295,6 +295,12 @@ if test "$enable_maintainer_zts" = "yes"; then
11 +if test "$PHP_FASTCGI" = "yes" -a "$PHP_FPM" = "yes"; then
12 + PHP_CONFIGURE_PART(Running FastCGI Process Manager checks)
13 + sinclude(sapi/cgi/fpm/acinclude.m4)
14 + sinclude(sapi/cgi/fpm/config.m4)
19 dnl ## In diversion 3 we check for compile-time options to the PHP
20 @@ -511,6 +517,7 @@ AC_CHECK_FUNCS(
28 @@ -1253,6 +1260,8 @@ PHP_SUBST_OLD(EXTENSION_DIR)
29 PHP_SUBST_OLD(EXTRA_LDFLAGS)
30 PHP_SUBST_OLD(EXTRA_LDFLAGS_PROGRAM)
31 PHP_SUBST_OLD(EXTRA_LIBS)
32 +PHP_SUBST_OLD(SAPI_EXTRA_LIBS)
33 +PHP_SUBST_OLD(SAPI_EXTRA_DEPS)
34 PHP_SUBST_OLD(ZEND_EXTRA_LIBS)
35 PHP_SUBST_OLD(INCLUDES)
36 PHP_SUBST_OLD(EXTRA_INCLUDES)
37 @@ -1364,7 +1373,7 @@ case $PHP_SAPI in
38 install_targets="$PHP_INSTALL_CLI_TARGET $install_targets"
41 - install_targets="install-sapi $PHP_INSTALL_CLI_TARGET $install_targets"
42 + install_targets="install-sapi $install_fpm $PHP_INSTALL_CLI_TARGET $install_targets"
46 diff --git a/libevent/ChangeLog b/libevent/ChangeLog
48 index 0000000..c592139
49 diff --git a/libevent/Makefile.am b/libevent/Makefile.am
51 index 0000000..5ccfd2c
52 diff --git a/libevent/Makefile.in b/libevent/Makefile.in
54 index 0000000..0600dcc
55 diff --git a/libevent/README b/libevent/README
57 index 0000000..b065039
58 diff --git a/libevent/aclocal.m4 b/libevent/aclocal.m4
60 index 0000000..74de4a1
61 diff --git a/libevent/autogen.sh b/libevent/autogen.sh
63 index 0000000..218dbf4
64 diff --git a/libevent/buffer.c b/libevent/buffer.c
66 index 0000000..e66080f
67 diff --git a/libevent/compat/sys/_time.h b/libevent/compat/sys/_time.h
69 index 0000000..8cabb0d
70 diff --git a/libevent/compat/sys/queue.h b/libevent/compat/sys/queue.h
72 index 0000000..c0956dd
73 diff --git a/libevent/config.h.in b/libevent/config.h.in
75 index 0000000..d151f87
76 diff --git a/libevent/configure b/libevent/configure
78 index 0000000..0a74aec
79 diff --git a/libevent/configure.in b/libevent/configure.in
81 index 0000000..852d3c5
82 diff --git a/libevent/depcomp b/libevent/depcomp
84 index 0000000..ffcd540
85 diff --git a/libevent/devpoll.c b/libevent/devpoll.c
87 index 0000000..cbd2730
88 diff --git a/libevent/epoll.c b/libevent/epoll.c
90 index 0000000..cf3c859
91 diff --git a/libevent/epoll_sub.c b/libevent/epoll_sub.c
93 index 0000000..431970c
94 diff --git a/libevent/evbuffer.c b/libevent/evbuffer.c
96 index 0000000..f2179a5
97 diff --git a/libevent/event-config.h b/libevent/event-config.h
99 index 0000000..0a278b3
100 diff --git a/libevent/event-fpm.h b/libevent/event-fpm.h
102 index 0000000..8625ca5
103 diff --git a/libevent/event-internal.h b/libevent/event-internal.h
105 index 0000000..7485f21
106 diff --git a/libevent/event.3 b/libevent/event.3
108 index 0000000..5b33ec6
109 diff --git a/libevent/event.c b/libevent/event.c
111 index 0000000..826b7db
112 diff --git a/libevent/event.h b/libevent/event.h
114 index 0000000..d67ecb5
115 diff --git a/libevent/evhttp.h b/libevent/evhttp.h
117 index 0000000..0d35f9e
118 diff --git a/libevent/evport.c b/libevent/evport.c
120 index 0000000..31523b4
121 diff --git a/libevent/evsignal.h b/libevent/evsignal.h
123 index 0000000..8be9cbd
124 diff --git a/libevent/evutil.c b/libevent/evutil.c
126 index 0000000..86205b2
127 diff --git a/libevent/evutil.h b/libevent/evutil.h
129 index 0000000..0a018b8
130 diff --git a/libevent/http-internal.h b/libevent/http-internal.h
132 index 0000000..bc9a1ed
133 diff --git a/libevent/http.c b/libevent/http.c
135 index 0000000..1d60fc5
136 diff --git a/libevent/install-sh b/libevent/install-sh
138 index 0000000..1a83534
139 diff --git a/libevent/kqueue.c b/libevent/kqueue.c
141 index 0000000..38a1819
142 diff --git a/libevent/log.c b/libevent/log.c
144 index 0000000..b62a619
145 diff --git a/libevent/log.h b/libevent/log.h
147 index 0000000..7bc6632
148 diff --git a/libevent/min_heap.h b/libevent/min_heap.h
150 index 0000000..d47e563
151 diff --git a/libevent/missing b/libevent/missing
153 index 0000000..09edd88
154 diff --git a/libevent/poll.c b/libevent/poll.c
156 index 0000000..b67c6ff
157 diff --git a/libevent/select.c b/libevent/select.c
159 index 0000000..7faafe4
160 diff --git a/libevent/signal.c b/libevent/signal.c
162 index 0000000..bcaa3f9
163 diff --git a/libevent/strlcpy-internal.h b/libevent/strlcpy-internal.h
165 index 0000000..22b5f61
166 diff --git a/libevent/strlcpy.c b/libevent/strlcpy.c
168 index 0000000..a1a413d
169 diff --git a/main/php_config.h.in b/main/php_config.h.in
170 --- a/main/php_config.h.in
171 +++ b/main/php_config.h.in
173 /* Define if you have the chroot function. */
176 +/* Define if you have the clearenv function. */
177 +#undef HAVE_CLEARENV
179 /* Define if you have the crypt function. */
186 +/* Is experimental fastcgi process manager code activated */
187 +#undef PHP_FASTCGI_PM
190 #undef FORCE_CGI_REDIRECT
194 #undef ENABLE_PATHINFO_CHECK
196 +/* do we have libxml? */
199 +/* do we have prctl? */
202 +/* do we have clock_gettime? */
203 +#undef HAVE_CLOCK_GETTIME
205 +/* do we have clock_get_time? */
206 +#undef HAVE_CLOCK_GET_TIME
208 +/* do we have ptrace? */
211 +/* do we have mach_vm_read? */
212 +#undef HAVE_MACH_VM_READ
214 +/* /proc/pid/mem interface */
215 +#undef PROC_MEM_FILE
217 /* Define if system uses EBCDIC */
218 #undef CHARSET_EBCDIC
220 --- php-5.2.17/sapi/cgi/Makefile.frag~ 2012-02-27 14:44:23.000000000 +0200
221 +++ php-5.2.17/sapi/cgi/Makefile.frag 2012-02-27 14:46:13.037731342 +0200
223 -$(SAPI_CGI_PATH): libphp_common.la $(PHP_SAPI_OBJS)
224 +$(SAPI_CGI_PATH): libphp_common.la $(PHP_SAPI_OBJS) $(SAPI_EXTRA_DEPS)
226 diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c
227 --- a/sapi/cgi/cgi_main.c
228 +++ b/sapi/cgi/cgi_main.c
231 #include <sys/wait.h>
237 #include "zend_extensions.h"
239 @@ -83,6 +86,11 @@ int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS;
244 +#include "fpm/fpm.h"
245 +#include "fpm/fpm_request.h"
249 /* XXX this will need to change later when threaded fastcgi is
250 implemented. shane */
251 @@ -115,8 +123,12 @@ static int parent_waiting = 0;
255 +static int request_body_fd;
259 +static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC);
261 #define PHP_MODE_STANDARD 1
262 #define PHP_MODE_HIGHLIGHT 2
263 #define PHP_MODE_INDENT 3
264 @@ -146,6 +158,10 @@ static const opt_struct OPTIONS[] = {
266 {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
270 + {'y', 1, "fpm-config"},
272 {'z', 1, "zend-extension"},
275 @@ -170,6 +186,7 @@ typedef struct _php_cgi_globals_struct {
276 zend_bool impersonate;
279 + char *error_header;
280 } php_cgi_globals_struct;
283 @@ -474,7 +491,28 @@ static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC)
285 if (fcgi_is_fastcgi()) {
286 fcgi_request *request = (fcgi_request*) SG(server_context);
287 - tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
289 + if (request_body_fd == -1) {
290 + char *request_body_filename = sapi_cgibin_getenv((char *) "REQUEST_BODY_FILE",
291 + sizeof("REQUEST_BODY_FILE")-1 TSRMLS_CC);
293 + if (request_body_filename && *request_body_filename) {
294 + request_body_fd = open(request_body_filename, O_RDONLY);
296 + if (0 > request_body_fd) {
297 + php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)",
298 + request_body_filename, strerror(errno), errno);
304 + /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */
305 + if (request_body_fd < 0) {
306 + tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
308 + tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes);
311 tmp_read_bytes = read(STDIN_FILENO, buffer + read_bytes, count_bytes - read_bytes);
313 @@ -786,7 +824,12 @@ static void php_cgi_usage(char *argv0)
314 " -s Display colour syntax highlighted source.\n"
315 " -v Version number\n"
316 " -w Display source with stripped comments and whitespace.\n"
317 - " -z <file> Load Zend extension <file>.\n"
319 + " -x, --fpm Run in FastCGI process manager mode.\n"
320 + " -y, --fpm-config <file>\n"
321 + " Specify alternative path to FastCGI process manager config file.\n"
323 + " -z <file> Load Zend extension <file>.\n"
325 " -T <count> Measure execution time of script repeated <count> times.\n"
327 @@ -1236,6 +1279,7 @@ PHP_INI_BEGIN()
329 STD_PHP_INI_ENTRY("fastcgi.impersonate", "0", PHP_INI_SYSTEM, OnUpdateBool, impersonate, php_cgi_globals_struct, php_cgi_globals)
331 + STD_PHP_INI_ENTRY("fastcgi.error_header", NULL, PHP_INI_SYSTEM, OnUpdateString, error_header, php_cgi_globals_struct, php_cgi_globals)
335 @@ -1258,6 +1302,7 @@ static void php_cgi_globals_ctor(php_cgi_globals_struct *php_cgi_globals TSRMLS_
337 php_cgi_globals->impersonate = 0;
339 + php_cgi_globals->error_header = NULL;
343 @@ -1290,9 +1335,47 @@ static PHP_MSHUTDOWN_FUNCTION(cgi)
344 static PHP_MINFO_FUNCTION(cgi)
346 DISPLAY_INI_ENTRIES();
350 +#include "fpm/fpm_autoconf.h"
352 + php_info_print_table_start();
353 + php_info_print_table_row(2, "php-fpm", fpm ? "active" : "inactive");
354 + php_info_print_table_row(2, "php-fpm version", PHP_FPM_VERSION);
355 + php_info_print_table_end();
362 +PHP_FUNCTION(fastcgi_finish_request)
364 + fcgi_request *request = (fcgi_request*) SG(server_context);
366 + if (fcgi_is_fastcgi() && request->fd >= 0) {
368 + php_end_ob_buffers(1 TSRMLS_CC);
369 + php_header(TSRMLS_C);
371 + fcgi_flush(request, 1);
372 + fcgi_close(request, 0, 0);
381 +function_entry cgi_fcgi_sapi_functions[] = {
383 + PHP_FE(fastcgi_finish_request, NULL)
388 static zend_module_entry cgi_module_entry = {
389 STANDARD_MODULE_HEADER,
391 @@ -1300,7 +1383,7 @@ static zend_module_entry cgi_module_entry = {
396 + cgi_fcgi_sapi_functions,
400 @@ -1340,6 +1423,7 @@ int main(int argc, char *argv[])
401 char *bindpath = NULL;
403 fcgi_request request;
404 + char *fpm_config = NULL;
407 #if HAVE_GETTIMEOFDAY
408 @@ -1460,6 +1544,14 @@ int main(int argc, char *argv[])
409 case 's': /* generate highlighted HTML from source */
410 behavior = PHP_MODE_HIGHLIGHT;
414 + fpm_config = php_optarg;
423 @@ -1524,6 +1616,19 @@ consult the installation file that came with this distribution, or visit \n\
424 #endif /* FORCE_CGI_REDIRECT */
429 + if (0 > fpm_init(argc, argv, fpm_config)) {
433 + fcgi_fd = fpm_run(&max_requests);
435 + fcgi_set_is_fastcgi(fastcgi = 1);
441 fcgi_fd = fcgi_listen(bindpath, 128);
443 @@ -1538,6 +1643,9 @@ consult the installation file that came with this distribution, or visit \n\
446 /* How many times to run PHP scripts before dying */
450 if (getenv("PHP_FCGI_MAX_REQUESTS")) {
451 max_requests = atoi(getenv("PHP_FCGI_MAX_REQUESTS"));
452 if (max_requests < 0) {
453 @@ -1555,6 +1663,9 @@ consult the installation file that came with this distribution, or visit \n\
456 /* Pre-fork, if required */
460 if (getenv("PHP_FCGI_CHILDREN")) {
461 char * children_str = getenv("PHP_FCGI_CHILDREN");
462 children = atoi(children_str);
463 @@ -1704,6 +1815,8 @@ consult the installation file that came with this distribution, or visit \n\
467 + request_body_fd = -1;
469 SG(server_context) = (void *) &request;
471 SG(server_context) = (void *) 1; /* avoid server_context==NULL checks */
472 @@ -1711,6 +1824,10 @@ consult the installation file that came with this distribution, or visit \n\
473 init_request_info(TSRMLS_C);
477 + if (fpm) fpm_request_info();
483 @@ -1994,6 +2111,10 @@ consult the installation file that came with this distribution, or visit \n\
488 + if (fpm) fpm_request_executing();
492 case PHP_MODE_STANDARD:
493 php_execute_script(&file_handle TSRMLS_CC);
494 @@ -2046,6 +2167,10 @@ consult the installation file that came with this distribution, or visit \n\
497 fastcgi_request_done:
499 + if (request_body_fd != -1) close(request_body_fd);
501 + request_body_fd = -2;
504 char *path_translated;
505 @@ -2059,6 +2184,16 @@ fastcgi_request_done:
506 SG(request_info).path_translated = path_translated;
509 + if (EG(exit_status) == 255) {
510 + if (CGIG(error_header) && *CGIG(error_header)) {
511 + sapi_header_line ctr = {0};
513 + ctr.line = CGIG(error_header);
514 + ctr.line_len = strlen(CGIG(error_header));
515 + sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
519 php_request_shutdown((void *) 0);
520 if (exit_status == 0) {
521 exit_status = EG(exit_status);
522 @@ -2096,15 +2231,20 @@ fastcgi_request_done:
526 - if (max_requests != 1) {
527 - /* no need to return exit_status of the last request */
532 /* end of fastcgi loop */
536 + if (fcgi_in_shutdown() || /* graceful shutdown by a signal */
537 + (max_requests && (requests == max_requests)) /* we were told to process max_requests and we are done */
546 if (cgi_sapi_module.php_ini_path_override) {
547 --- php-5.2.17/sapi/cgi/config9.m4~ 2012-02-27 14:44:23.000000000 +0200
548 +++ php-5.2.17/sapi/cgi/config9.m4 2012-02-27 14:48:07.279580606 +0200
549 @@ -22,6 +22,10 @@ PHP_ARG_ENABLE(path-info-check,,
550 [ --disable-path-info-check CGI: If this is disabled, paths such as
551 /info.php/test?a=b will fail to work], yes, no)
553 +PHP_ARG_ENABLE(fpm,,
554 +[ --enable-fpm FastCGI: If this is enabled, the fastcgi support
555 + will include experimental process manager code], no, no)
560 @@ -54,6 +58,20 @@ if test "$PHP_SAPI" = "default"; then
561 AC_DEFINE_UNQUOTED(PHP_FASTCGI, $PHP_ENABLE_FASTCGI, [ ])
562 AC_MSG_RESULT($PHP_FASTCGI)
565 + if test "$PHP_FASTCGI" = "yes"; then
566 + AC_MSG_CHECKING(whether to enable FastCGI Process Manager)
567 + if test "$PHP_FPM" = "yes"; then
572 + AC_MSG_RESULT($PHP_FPM)
576 + AC_DEFINE_UNQUOTED(PHP_FASTCGI_PM, $PHP_FASTCGI_PM, [Is experimental fastcgi process manager code activated])
578 dnl --enable-force-cgi-redirect
579 AC_MSG_CHECKING(whether to force Apache CGI redirect)
580 if test "$PHP_FORCE_CGI_REDIRECT" = "yes"; then
581 @@ -111,10 +111,10 @@
582 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)"
585 - BUILD_CGI="\$(CC) \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(NATIVE_RPATHS) \$(PHP_GLOBAL_OBJS:.lo=.o) \$(PHP_SAPI_OBJS:.lo=.o) \$(PHP_FRAMEWORKS) \$(EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
586 + BUILD_CGI="\$(CC) \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(NATIVE_RPATHS) \$(PHP_GLOBAL_OBJS:.lo=.o) \$(PHP_SAPI_OBJS:.lo=.o) \$(PHP_FRAMEWORKS) \$(EXTRA_LIBS) \$(SAPI_EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
589 - BUILD_CGI="\$(LIBTOOL) --mode=link \$(CC) -export-dynamic \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(PHP_RPATHS) libphp_common.la \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
590 + BUILD_CGI="\$(LIBTOOL) --mode=link \$(CC) -export-dynamic \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(PHP_RPATHS) libphp_common.la \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(SAPI_EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
594 diff --git a/sapi/cgi/fastcgi.c b/sapi/cgi/fastcgi.c
595 --- a/sapi/cgi/fastcgi.c
596 +++ b/sapi/cgi/fastcgi.c
602 +#include "fpm/fpm.h"
603 +#include "fpm/fpm_request.h"
609 @@ -234,6 +239,8 @@ int fcgi_init(void)
611 return is_fastcgi = 0;
614 + fcgi_set_allowed_clients(getenv("FCGI_WEB_SERVER_ADDRS"));
618 @@ -249,14 +256,26 @@ int fcgi_is_fastcgi(void)
622 +void fcgi_set_is_fastcgi(int new_value)
624 + is_fastcgi = new_value;
627 +void fcgi_set_in_shutdown(int new_value)
629 + in_shutdown = new_value;
632 void fcgi_shutdown(void)
634 if (is_initialized) {
635 zend_hash_destroy(&fcgi_mgmt_vars);
639 if (allowed_clients) {
640 free(allowed_clients);
641 + allowed_clients = 0;
645 @@ -330,6 +349,41 @@ out_fail:
649 +void fcgi_set_allowed_clients(char *ip)
659 + if (*cur == ',') n++;
662 + if (allowed_clients) free(allowed_clients);
663 + allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
667 + end = strchr(cur, ',');
672 + allowed_clients[n] = inet_addr(cur);
673 + if (allowed_clients[n] == INADDR_NONE) {
674 + fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
679 + allowed_clients[n] = INADDR_NONE;
684 static int is_port_number(const char *bindpath)
687 @@ -458,38 +512,6 @@ int fcgi_listen(const char *path, int backlog)
692 - char *ip = getenv("FCGI_WEB_SERVER_ADDRS");
701 - if (*cur == ',') n++;
704 - allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
708 - end = strchr(cur, ',');
713 - allowed_clients[n] = inet_addr(cur);
714 - if (allowed_clients[n] == INADDR_NONE) {
715 - fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
720 - allowed_clients[n] = INADDR_NONE;
725 if (!is_initialized) {
726 @@ -866,7 +888,7 @@ int fcgi_read(fcgi_request *req, char *str, int len)
730 -static inline void fcgi_close(fcgi_request *req, int force, int destroy)
731 +void fcgi_close(fcgi_request *req, int force, int destroy)
734 zend_hash_destroy(&req->env);
735 @@ -906,6 +928,10 @@ static inline void fcgi_close(fcgi_request *req, int force, int destroy)
741 + if (fpm) fpm_request_finished();
746 @@ -953,6 +979,10 @@ int fcgi_accept_request(fcgi_request *req)
748 socklen_t len = sizeof(sa);
751 + if (fpm) fpm_request_accepting();
754 FCGI_LOCK(req->listen_socket);
755 req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
756 FCGI_UNLOCK(req->listen_socket);
757 @@ -988,6 +1018,11 @@ int fcgi_accept_request(fcgi_request *req)
763 + if (fpm) fpm_request_reading_headers();
766 #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
769 diff --git a/sapi/cgi/fastcgi.h b/sapi/cgi/fastcgi.h
770 --- a/sapi/cgi/fastcgi.h
771 +++ b/sapi/cgi/fastcgi.h
772 @@ -114,6 +114,9 @@ typedef struct _fcgi_request {
774 void fcgi_shutdown(void);
775 int fcgi_is_fastcgi(void);
776 +void fcgi_set_is_fastcgi(int);
777 +void fcgi_set_in_shutdown(int);
778 +void fcgi_set_allowed_clients(char *);
779 int fcgi_in_shutdown(void);
780 int fcgi_listen(const char *path, int backlog);
781 void fcgi_init_request(fcgi_request *req, int listen_socket);
782 @@ -128,6 +131,8 @@ int fcgi_read(fcgi_request *req, char *str, int len);
783 int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len);
784 int fcgi_flush(fcgi_request *req, int close);
786 +void fcgi_close(fcgi_request *req, int force, int destroy);
789 void fcgi_impersonate(void);
791 diff --git a/sapi/cgi/fpm/Makefile.frag b/sapi/cgi/fpm/Makefile.frag
794 +++ b/sapi/cgi/fpm/Makefile.frag
797 +install-fpm: sapi/cgi/fpm/php-fpm.conf sapi/cgi/fpm/php-fpm
798 + @echo "Installing FPM config: $(INSTALL_ROOT)$(php_fpm_conf_path)"
799 + -@$(mkinstalldirs) \
800 + $(INSTALL_ROOT)$(prefix)/sbin \
801 + `dirname "$(INSTALL_ROOT)$(php_fpm_conf_path)"` \
802 + `dirname "$(INSTALL_ROOT)$(php_fpm_log_path)"` \
803 + `dirname "$(INSTALL_ROOT)$(php_fpm_pid_path)"`
804 + -@if test -r "$(INSTALL_ROOT)$(php_fpm_conf_path)" ; then \
805 + dest=`basename "$(php_fpm_conf_path)"`.default ; \
806 + echo " (installing as $$dest)" ; \
808 + dest=`basename "$(php_fpm_conf_path)"` ; \
810 + $(INSTALL_DATA) $(top_builddir)/sapi/cgi/fpm/php-fpm.conf $(INSTALL_ROOT)`dirname "$(php_fpm_conf_path)"`/$$dest
811 + @echo "Installing init.d script: $(INSTALL_ROOT)$(prefix)/sbin/php-fpm"
812 + -@$(INSTALL) -m 0755 $(top_builddir)/sapi/cgi/fpm/php-fpm $(INSTALL_ROOT)$(prefix)/sbin/php-fpm
814 +$(top_builddir)/libevent/libevent.a: $(top_builddir)/libevent/Makefile
815 + cd $(top_builddir)/libevent && $(MAKE) libevent.a
817 diff --git a/sapi/cgi/fpm/acinclude.m4 b/sapi/cgi/fpm/acinclude.m4
820 +++ b/sapi/cgi/fpm/acinclude.m4
823 +AC_DEFUN([AC_FPM_CHECK_FUNC],
825 + SAVED_CFLAGS="$CFLAGS"
826 + CFLAGS="$CFLAGS $2"
830 + AC_CHECK_FUNC([$1],[$4],[$5])
832 + CFLAGS="$SAVED_CFLAGS"
836 +AC_DEFUN([AC_FPM_LIBEVENT],
838 + AC_ARG_WITH([libevent],
839 + [ --with-libevent=DIR FPM: libevent install directory])
842 + LIBEVENT_LIBS="-levent"
843 + LIBEVENT_INCLUDE_PATH=""
845 + if test "$with_libevent" != "no" -a -n "$with_libevent"; then
846 + LIBEVENT_CFLAGS="-I$with_libevent/include"
847 + LIBEVENT_LIBS="-L$with_libevent/lib $LIBEVENT_LIBS"
848 + LIBEVENT_INCLUDE_PATH="$with_libevent/include"
851 + AC_MSG_CHECKING([for event.h])
855 + for dir in "$LIBEVENT_INCLUDE_PATH" /usr/include ; do
856 + if test -r "$dir/event.h" ; then
862 + AC_MSG_RESULT([$found])
864 + AC_FPM_CHECK_FUNC([event_set], [$LIBEVENT_CFLAGS], [$LIBEVENT_LIBS], ,
865 + [AC_MSG_ERROR([Failed to link with libevent. Perhaps --with-libevent=DIR option could help.])])
867 + AC_FPM_CHECK_FUNC([event_base_free], [$LIBEVENT_CFLAGS], [$LIBEVENT_LIBS], ,
868 + [AC_MSG_ERROR([You have too old version. libevent version >= 1.2 is required.])])
872 +AC_DEFUN([AC_FPM_LIBXML],
874 + AC_MSG_RESULT([checking for XML configuration])
876 + AC_ARG_WITH(xml-config,
877 + [ --with-xml-config=PATH FPM: use xml-config in PATH to find libxml],
878 + [XMLCONFIG="$withval"],
879 + [AC_PATH_PROGS(XMLCONFIG, [xml2-config xml-config], "")]
882 + if test "x$XMLCONFIG" = "x"; then
883 + AC_MSG_ERROR([XML configuration could not be found])
885 + AC_MSG_CHECKING([for libxml library])
887 + if test ! -x "$XMLCONFIG"; then
888 + AC_MSG_ERROR([$XMLCONFIG cannot be executed])
891 + LIBXML_LIBS="`$XMLCONFIG --libs`"
892 + LIBXML_CFLAGS="`$XMLCONFIG --cflags`"
893 + LIBXML_VERSION="`$XMLCONFIG --version`"
895 + AC_MSG_RESULT([yes, $LIBXML_VERSION])
897 + AC_FPM_CHECK_FUNC([xmlParseFile], [$LIBXML_CFLAGS], [$LIBXML_LIBS], ,
898 + [AC_MSG_ERROR([Failed to link with libxml])])
900 + AC_DEFINE(HAVE_LIBXML, 1, [do we have libxml?])
904 +AC_DEFUN([AC_FPM_JUDY],
906 + AC_ARG_WITH([Judy],
907 + [ --with-Judy=DIR FPM: Judy install directory])
911 + JUDY_INCLUDE_PATH=""
913 + if test "$with_Judy" != "no" -a -n "$with_Judy"; then
914 + JUDY_INCLUDE_PATH="$with_Judy/include"
915 + JUDY_CFLAGS="-I$with_Judy/include $JUDY_CFLAGS"
916 + JUDY_LIBS="-L$with_Judy/lib $JUDY_LIBS"
919 + AC_MSG_CHECKING([for Judy.h])
923 + for dir in "$JUDY_INCLUDE_PATH" /usr/include ; do
924 + if test -r "$dir/Judy.h" ; then
930 + AC_MSG_RESULT([$found])
932 + AC_FPM_CHECK_FUNC([JudyLCount], [$JUDY_CFLAGS], [$JUDY_LIBS], ,
933 + [AC_MSG_ERROR([Failed to link with Judy])])
937 +AC_DEFUN([AC_FPM_CLOCK],
939 + have_clock_gettime=no
941 + AC_MSG_CHECKING([for clock_gettime])
943 + AC_TRY_LINK([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
944 + have_clock_gettime=yes
945 + AC_MSG_RESULT([yes])
947 + AC_MSG_RESULT([no])
950 + if test "$have_clock_gettime" = "no"; then
951 + AC_MSG_CHECKING([for clock_gettime in -lrt])
956 + AC_TRY_LINK([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
957 + have_clock_gettime=yes
958 + AC_MSG_RESULT([yes])
961 + AC_MSG_RESULT([no])
965 + if test "$have_clock_gettime" = "yes"; then
966 + AC_DEFINE([HAVE_CLOCK_GETTIME], 1, [do we have clock_gettime?])
969 + have_clock_get_time=no
971 + if test "$have_clock_gettime" = "no"; then
972 + AC_MSG_CHECKING([for clock_get_time])
974 + AC_TRY_RUN([ #include <mach/mach.h>
975 + #include <mach/clock.h>
976 + #include <mach/mach_error.h>
980 + kern_return_t ret; clock_serv_t aClock; mach_timespec_t aTime;
981 + ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &aClock);
983 + if (ret != KERN_SUCCESS) {
987 + ret = clock_get_time(aClock, &aTime);
988 + if (ret != KERN_SUCCESS) {
995 + have_clock_get_time=yes
996 + AC_MSG_RESULT([yes])
998 + AC_MSG_RESULT([no])
1002 + if test "$have_clock_get_time" = "yes"; then
1003 + AC_DEFINE([HAVE_CLOCK_GET_TIME], 1, [do we have clock_get_time?])
1007 +AC_DEFUN([AC_FPM_TRACE],
1010 + have_broken_ptrace=no
1012 + AC_MSG_CHECKING([for ptrace])
1015 + #include <sys/types.h>
1016 + #include <sys/ptrace.h> ], [ptrace(0, 0, (void *) 0, 0);], [
1018 + AC_MSG_RESULT([yes])
1020 + AC_MSG_RESULT([no])
1023 + if test "$have_ptrace" = "yes"; then
1024 + AC_MSG_CHECKING([whether ptrace works])
1027 + #include <unistd.h>
1028 + #include <signal.h>
1029 + #include <sys/wait.h>
1030 + #include <sys/types.h>
1031 + #include <sys/ptrace.h>
1032 + #include <errno.h>
1034 + #if !defined(PTRACE_ATTACH) && defined(PT_ATTACH)
1035 + #define PTRACE_ATTACH PT_ATTACH
1038 + #if !defined(PTRACE_DETACH) && defined(PT_DETACH)
1039 + #define PTRACE_DETACH PT_DETACH
1042 + #if !defined(PTRACE_PEEKDATA) && defined(PT_READ_D)
1043 + #define PTRACE_PEEKDATA PT_READ_D
1048 + long v1 = (unsigned int) -1; /* copy will fail if sizeof(long) == 8 and we've got "int ptrace()" */
1053 + if ( (child = fork()) ) { /* parent */
1056 + if (0 > ptrace(PTRACE_ATTACH, child, 0, 0)) {
1060 + waitpid(child, &status, 0);
1063 + struct ptrace_io_desc ptio = {
1064 + .piod_op = PIOD_READ_D,
1067 + .piod_len = sizeof(v1)
1070 + if (0 > ptrace(PT_IO, child, (void *) &ptio, 0)) {
1076 + v2 = ptrace(PTRACE_PEEKDATA, child, (void *) &v1, 0);
1082 + ptrace(PTRACE_DETACH, child, (void *) 1, 0);
1084 + kill(child, SIGKILL);
1086 + return ret ? ret : (v1 != v2);
1088 + else { /* child */
1094 + AC_MSG_RESULT([yes])
1097 + have_broken_ptrace=yes
1098 + AC_MSG_RESULT([no])
1102 + if test "$have_ptrace" = "yes"; then
1103 + AC_DEFINE([HAVE_PTRACE], 1, [do we have ptrace?])
1106 + have_mach_vm_read=no
1108 + if test "$have_broken_ptrace" = "yes"; then
1109 + AC_MSG_CHECKING([for mach_vm_read])
1111 + AC_TRY_COMPILE([ #include <mach/mach.h>
1112 + #include <mach/mach_vm.h>
1114 + 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);
1116 + have_mach_vm_read=yes
1117 + AC_MSG_RESULT([yes])
1119 + AC_MSG_RESULT([no])
1123 + if test "$have_mach_vm_read" = "yes"; then
1124 + AC_DEFINE([HAVE_MACH_VM_READ], 1, [do we have mach_vm_read?])
1129 + if test -r /proc/$$/mem ; then
1130 + proc_mem_file="mem"
1132 + if test -r /proc/$$/as ; then
1133 + proc_mem_file="as"
1137 + if test -n "$proc_mem_file" ; then
1138 + AC_MSG_CHECKING([for proc mem file])
1141 + #define _GNU_SOURCE
1142 + #define _FILE_OFFSET_BITS 64
1143 + #include <stdint.h>
1144 + #include <unistd.h>
1145 + #include <sys/types.h>
1146 + #include <sys/stat.h>
1147 + #include <fcntl.h>
1148 + #include <stdio.h>
1151 + long v1 = (unsigned int) -1, v2 = 0;
1154 + sprintf(buf, "/proc/%d/$proc_mem_file", getpid());
1155 + fd = open(buf, O_RDONLY);
1159 + if (sizeof(long) != pread(fd, &v2, sizeof(long), (uintptr_t) &v1)) {
1167 + AC_MSG_RESULT([$proc_mem_file])
1170 + AC_MSG_RESULT([no])
1174 + if test -n "$proc_mem_file"; then
1175 + AC_DEFINE_UNQUOTED([PROC_MEM_FILE], "$proc_mem_file", [/proc/pid/mem interface])
1178 + if test "$have_ptrace" = "yes"; then
1179 + FPM_SOURCES="$FPM_SOURCES fpm_trace.c fpm_trace_ptrace.c"
1180 + elif test -n "$proc_mem_file"; then
1181 + FPM_SOURCES="$FPM_SOURCES fpm_trace.c fpm_trace_pread.c"
1182 + elif test "$have_mach_vm_read" = "yes" ; then
1183 + FPM_SOURCES="$FPM_SOURCES fpm_trace.c fpm_trace_mach.c"
1188 +AC_DEFUN([AC_FPM_PRCTL],
1190 + AC_MSG_CHECKING([for prctl])
1192 + AC_TRY_COMPILE([ #include <sys/prctl.h> ], [prctl(0, 0, 0, 0, 0);], [
1193 + AC_DEFINE([HAVE_PRCTL], 1, [do we have prctl?])
1194 + AC_MSG_RESULT([yes])
1196 + AC_MSG_RESULT([no])
1199 diff --git a/sapi/cgi/fpm/conf/php-fpm.conf.in b/sapi/cgi/fpm/conf/php-fpm.conf.in
1200 new file mode 100644
1202 +++ b/sapi/cgi/fpm/conf/php-fpm.conf.in
1204 +<?xml version="1.0" ?>
1207 + All relative paths in this config are relative to php's install prefix
1209 + <section name="global_options">
1212 + <value name="pid_file">@php_fpm_pid_path@</value>
1215 + <value name="error_log">@php_fpm_log_path@</value>
1218 + <value name="log_level">notice</value>
1220 + When this amount of php processes exited with SIGSEGV or SIGBUS ...
1221 + <value name="emergency_restart_threshold">10</value>
1223 + ... in a less than this interval of time, a graceful restart will be initiated.
1224 + Useful to work around accidental curruptions in accelerator's shared memory.
1225 + <value name="emergency_restart_interval">1m</value>
1227 + Time limit on waiting child's reaction on signals from master
1228 + <value name="process_control_timeout">5s</value>
1230 + Set to 'no' to debug fpm
1231 + <value name="daemonize">yes</value>
1237 + <section name="pool">
1239 + Name of pool. Used in logs and stats.
1240 + <value name="name">default</value>
1242 + Address to accept fastcgi requests on.
1243 + Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
1244 + <value name="listen_address">127.0.0.1:9000</value>
1246 + <value name="listen_options">
1248 + Set listen(2) backlog
1249 + <value name="backlog">-1</value>
1251 + Set permissions for unix socket, if one used.
1252 + In Linux read/write permissions must be set in order to allow connections from web server.
1253 + Many BSD-derrived systems allow connections regardless of permissions.
1254 + <value name="owner"></value>
1255 + <value name="group"></value>
1256 + <value name="mode">0666</value>
1259 + Additional php.ini defines, specific to this pool of workers.
1260 + <value name="php_defines">
1261 + <!-- <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> -->
1262 + <!-- <value name="display_errors">0</value> -->
1265 + Unix user of processes
1266 + <!-- <value name="user">nobody</value> -->
1268 + Unix group of processes
1269 + <!-- <value name="group">@php_fpm_group@</value> -->
1271 + Process manager settings
1274 + Sets style of controling worker process count.
1275 + Valid values are 'static' and 'apache-like'
1276 + <value name="style">static</value>
1278 + Sets the limit on the number of simultaneous requests that will be served.
1279 + Equivalent to Apache MaxClients directive.
1280 + Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
1281 + Used with any pm_style.
1282 + <value name="max_children">5</value>
1284 + Settings group for 'apache-like' pm style
1285 + <value name="apache_like">
1287 + Sets the number of server processes created on startup.
1288 + Used only when 'apache-like' pm_style is selected
1289 + <value name="StartServers">20</value>
1291 + Sets the desired minimum number of idle server processes.
1292 + Used only when 'apache-like' pm_style is selected
1293 + <value name="MinSpareServers">5</value>
1295 + Sets the desired maximum number of idle server processes.
1296 + Used only when 'apache-like' pm_style is selected
1297 + <value name="MaxSpareServers">35</value>
1303 + The timeout (in seconds) for serving a single request after which the worker process will be terminated
1304 + Should be used when 'max_execution_time' ini option does not stop script execution for some reason
1306 + <value name="request_terminate_timeout">0s</value>
1308 + The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
1310 + <value name="request_slowlog_timeout">0s</value>
1312 + The log file for slow requests
1313 + <value name="slowlog">logs/slow.log</value>
1315 + Set open file desc rlimit
1316 + <value name="rlimit_files">1024</value>
1318 + Set max core size rlimit
1319 + <value name="rlimit_core">0</value>
1321 + Chroot to this directory at the start, absolute path
1322 + <value name="chroot"></value>
1324 + Chdir to this directory at the start, absolute path
1325 + <value name="chdir"></value>
1327 + Redirect workers' stdout and stderr into main error log.
1328 + If not set, they will be redirected to /dev/null, according to FastCGI specs
1329 + <value name="catch_workers_output">yes</value>
1331 + How much requests each process should execute before respawn.
1332 + Useful to work around memory leaks in 3rd party libraries.
1333 + For endless request processing please specify 0
1334 + Equivalent to PHP_FCGI_MAX_REQUESTS
1335 + <value name="max_requests">500</value>
1337 + Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
1338 + Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
1339 + Makes sense only with AF_INET listening socket.
1340 + <value name="allowed_clients">127.0.0.1</value>
1342 + Pass environment variables like LD_LIBRARY_PATH
1343 + All $VARIABLEs are taken from current environment
1344 + <value name="environment">
1345 + <value name="HOSTNAME">$HOSTNAME</value>
1346 + <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
1347 + <value name="TMP">/tmp</value>
1348 + <value name="TMPDIR">/tmp</value>
1349 + <value name="TEMP">/tmp</value>
1350 + <value name="OSTYPE">$OSTYPE</value>
1351 + <value name="MACHTYPE">$MACHTYPE</value>
1352 + <value name="MALLOC_CHECK_">2</value>
1360 diff --git a/sapi/cgi/fpm/config.m4 b/sapi/cgi/fpm/config.m4
1361 new file mode 100644
1363 +++ b/sapi/cgi/fpm/config.m4
1366 +FPM_VERSION="0.5.14"
1368 +PHP_ARG_WITH(fpm-conf, for php-fpm config file path,
1369 +[ --with-fpm-conf=PATH Set the path for php-fpm configuration file [PREFIX/etc/php-fpm.conf]], \$prefix/etc/php-fpm.conf, no)
1371 +PHP_ARG_WITH(fpm-log, for php-fpm log file path,
1372 +[ --with-fpm-log=PATH Set the path for php-fpm log file [PREFIX/logs/php-fpm.log]], \$prefix/logs/php-fpm.log, no)
1374 +PHP_ARG_WITH(fpm-pid, for php-fpm pid file path,
1375 +[ --with-fpm-pid=PATH Set the path for php-fpm pid file [PREFIX/logs/php-fpm.pid]], \$prefix/logs/php-fpm.pid, no)
1377 +FPM_SOURCES="fpm.c \
1381 + fpm_worker_pool.c \
1390 + fpm_process_ctl.c \
1398 +dnl AC_FPM_LIBEVENT
1406 +LIBEVENT_LIBS="-levent"
1408 +SAPI_EXTRA_DEPS="$LIBEVENT_LIBS"
1410 +FPM_CFLAGS="$LIBEVENT_CFLAGS $LIBXML_CFLAGS $JUDY_CFLAGS"
1412 +dnl FPM_CFLAGS="$FPM_CFLAGS -DJUDYERROR_NOTEST" # for Judy
1413 +FPM_CFLAGS="$FPM_CFLAGS -I$abs_srcdir/sapi/cgi" # for fastcgi.h
1415 +if test "$ICC" = "yes" ; then
1416 + FPM_ADD_CFLAGS="-Wall -wd279,310,869,810,981"
1417 +elif test "$GCC" = "yes" ; then
1418 + FPM_ADD_CFLAGS="-Wall -Wpointer-arith -Wno-unused-parameter -Wunused-variable -Wunused-value -fno-strict-aliasing"
1421 +if test -n "$FPM_WERROR" ; then
1422 + FPM_ADD_CFLAGS="$FPM_ADD_CFLAGS -Werror"
1425 +FPM_CFLAGS="$FPM_ADD_CFLAGS $FPM_CFLAGS"
1427 +PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/sapi/cgi/fpm/Makefile.frag)
1429 +PHP_ADD_SOURCES(sapi/cgi/fpm, $FPM_SOURCES, $FPM_CFLAGS, sapi)
1431 +PHP_ADD_BUILD_DIR(sapi/cgi/fpm)
1433 +install_fpm="install-fpm"
1435 +SAPI_EXTRA_LIBS="$LIBEVENT_LIBS $LIBXML_LIBS $JUDY_LIBS"
1438 +if test "$prefix" = "NONE" ; then
1439 + fpm_prefix=/usr/local
1441 + fpm_prefix="$prefix"
1444 +if test "$PHP_FPM_CONF" = "\$prefix/etc/php-fpm.conf" ; then
1445 + php_fpm_conf_path="$fpm_prefix/etc/php-fpm.conf"
1447 + php_fpm_conf_path="$PHP_FPM_CONF"
1450 +if test "$PHP_FPM_LOG" = "\$prefix/logs/php-fpm.log" ; then
1451 + php_fpm_log_path="$fpm_prefix/logs/php-fpm.log"
1453 + php_fpm_log_path="$PHP_FPM_LOG"
1456 +if test "$PHP_FPM_PID" = "\$prefix/logs/php-fpm.pid" ; then
1457 + php_fpm_pid_path="$fpm_prefix/logs/php-fpm.pid"
1459 + php_fpm_pid_path="$PHP_FPM_PID"
1463 +if grep nobody /etc/group >/dev/null 2>&1; then
1464 + php_fpm_group=nobody
1466 + if grep nogroup /etc/group >/dev/null 2>&1; then
1467 + php_fpm_group=nogroup
1469 + php_fpm_group=nobody
1473 +PHP_SUBST_OLD(php_fpm_conf_path)
1474 +PHP_SUBST_OLD(php_fpm_log_path)
1475 +PHP_SUBST_OLD(php_fpm_pid_path)
1476 +PHP_SUBST_OLD(php_fpm_group)
1477 +PHP_SUBST_OLD(FPM_VERSION)
1479 +PHP_OUTPUT(sapi/cgi/fpm/fpm_autoconf.h)
1480 +PHP_OUTPUT(sapi/cgi/fpm/php-fpm.conf:sapi/cgi/fpm/conf/php-fpm.conf.in)
1481 +PHP_OUTPUT(sapi/cgi/fpm/php-fpm:sapi/cgi/fpm/init.d/php-fpm.in)
1482 diff --git a/sapi/cgi/fpm/fpm.c b/sapi/cgi/fpm/fpm.c
1483 new file mode 100644
1484 index 0000000..9db2c9b
1486 +++ b/sapi/cgi/fpm/fpm.c
1490 + /* (c) 2007,2008 Andrei Nigmatulin */
1492 +#include "fpm_config.h"
1494 +#include <stdlib.h> /* for exit */
1497 +#include "fpm_children.h"
1498 +#include "fpm_signals.h"
1499 +#include "fpm_env.h"
1500 +#include "fpm_events.h"
1501 +#include "fpm_cleanup.h"
1502 +#include "fpm_php.h"
1503 +#include "fpm_sockets.h"
1504 +#include "fpm_unix.h"
1505 +#include "fpm_process_ctl.h"
1506 +#include "fpm_conf.h"
1507 +#include "fpm_worker_pool.h"
1508 +#include "fpm_stdio.h"
1513 +struct fpm_globals_s fpm_globals;
1515 +int fpm_init(int argc, char **argv, char *config)
1517 + fpm_globals.argc = argc;
1518 + fpm_globals.argv = argv;
1519 + fpm_globals.config = config;
1521 + if (0 > fpm_php_init_main() ||
1522 + 0 > fpm_stdio_init_main() ||
1523 + 0 > fpm_conf_init_main() ||
1524 + 0 > fpm_unix_init_main() ||
1525 + 0 > fpm_env_init_main() ||
1526 + 0 > fpm_signals_init_main() ||
1527 + 0 > fpm_pctl_init_main() ||
1528 + 0 > fpm_children_init_main() ||
1529 + 0 > fpm_sockets_init_main() ||
1530 + 0 > fpm_worker_pool_init_main() ||
1531 + 0 > fpm_event_init_main()) {
1535 + if (0 > fpm_conf_write_pid()) {
1539 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "fpm is running, pid %d", (int) fpm_globals.parent_pid);
1544 +/* children: return listening socket
1545 + parent: never return */
1546 +int fpm_run(int *max_requests)
1548 + struct fpm_worker_pool_s *wp;
1550 + /* create initial children in all pools */
1551 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1554 + is_parent = fpm_children_create_initial(wp);
1561 + /* run event loop forever */
1564 +run_child: /* only workers reach this point */
1566 + fpm_cleanups_run(FPM_CLEANUP_CHILD);
1568 + *max_requests = fpm_globals.max_requests;
1569 + return fpm_globals.listening_socket;
1572 diff --git a/sapi/cgi/fpm/fpm.h b/sapi/cgi/fpm/fpm.h
1573 new file mode 100644
1575 +++ b/sapi/cgi/fpm/fpm.h
1579 + /* (c) 2007,2008 Andrei Nigmatulin */
1584 +#include <unistd.h>
1586 +int fpm_run(int *max_requests);
1587 +int fpm_init(int argc, char **argv, char *config);
1589 +struct fpm_globals_s {
1594 + int running_children;
1597 + int listening_socket; /* for this child */
1598 + int max_requests; /* for this child */
1602 +extern struct fpm_globals_s fpm_globals;
1607 diff --git a/sapi/cgi/fpm/fpm_arrays.h b/sapi/cgi/fpm/fpm_arrays.h
1608 new file mode 100644
1610 +++ b/sapi/cgi/fpm/fpm_arrays.h
1614 + /* (c) 2007,2008 Andrei Nigmatulin */
1616 +#ifndef FPM_ARRAYS_H
1617 +#define FPM_ARRAYS_H 1
1619 +#include <stdlib.h>
1620 +#include <string.h>
1622 +struct fpm_array_s {
1629 +static inline struct fpm_array_s *fpm_array_init(struct fpm_array_s *a, unsigned int sz, unsigned int initial_num)
1631 + void *allocated = 0;
1634 + a = malloc(sizeof(struct fpm_array_s));
1645 + a->data = calloc(sz, initial_num);
1652 + a->allocated = initial_num;
1658 +static inline void *fpm_array_item(struct fpm_array_s *a, unsigned int n)
1662 + ret = (char *) a->data + a->sz * n;
1667 +static inline void *fpm_array_item_last(struct fpm_array_s *a)
1669 + return fpm_array_item(a, a->used - 1);
1672 +static inline int fpm_array_item_remove(struct fpm_array_s *a, unsigned int n)
1676 + if (n < a->used - 1) {
1677 + void *last = fpm_array_item(a, a->used - 1);
1678 + void *to_remove = fpm_array_item(a, n);
1680 + memcpy(to_remove, last, a->sz);
1690 +static inline void *fpm_array_push(struct fpm_array_s *a)
1694 + if (a->used == a->allocated) {
1695 + size_t new_allocated = a->allocated ? a->allocated * 2 : 20;
1696 + void *new_ptr = realloc(a->data, a->sz * new_allocated);
1702 + a->data = new_ptr;
1703 + a->allocated = new_allocated;
1706 + ret = fpm_array_item(a, a->used);
1713 +static inline void fpm_array_free(struct fpm_array_s *a)
1718 + a->used = a->allocated = 0;
1722 diff --git a/sapi/cgi/fpm/fpm_atomic.h b/sapi/cgi/fpm/fpm_atomic.h
1723 new file mode 100644
1725 +++ b/sapi/cgi/fpm/fpm_atomic.h
1729 + /* (c) 2007,2008 Andrei Nigmatulin */
1731 +#ifndef FPM_ATOMIC_H
1732 +#define FPM_ATOMIC_H 1
1734 +#include <stdint.h>
1737 +#if ( __i386__ || __i386 )
1739 +typedef int32_t atomic_int_t;
1740 +typedef uint32_t atomic_uint_t;
1741 +typedef volatile atomic_uint_t atomic_t;
1744 +static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
1746 + __asm__ volatile ( "lock;" "xaddl %0, %1;" :
1747 + "+r" (add) : "m" (*value) : "memory");
1752 +static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
1754 + unsigned char res;
1756 + __asm__ volatile ( "lock;" "cmpxchgl %3, %1;" "sete %0;" :
1757 + "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
1762 +#elif ( __amd64__ || __amd64 )
1764 +typedef int64_t atomic_int_t;
1765 +typedef uint64_t atomic_uint_t;
1766 +typedef volatile atomic_uint_t atomic_t;
1768 +static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
1770 + __asm__ volatile ( "lock;" "xaddq %0, %1;" :
1771 + "+r" (add) : "m" (*value) : "memory");
1776 +static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
1778 + unsigned char res;
1780 + __asm__ volatile ( "lock;" "cmpxchgq %3, %1;" "sete %0;" :
1781 + "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
1788 +#error unsupported processor. please write a patch and send it to me
1792 +static inline int fpm_spinlock(atomic_t *lock, int try_once)
1795 + return atomic_cmp_set(lock, 0, 1) ? 0 : -1;
1800 + if (atomic_cmp_set(lock, 0, 1)) {
1812 diff --git a/sapi/cgi/fpm/fpm_autoconf.h.in b/sapi/cgi/fpm/fpm_autoconf.h.in
1813 new file mode 100644
1815 +++ b/sapi/cgi/fpm/fpm_autoconf.h.in
1819 + /* (c) 2007,2008 Andrei Nigmatulin */
1821 +#define PHP_FPM_VERSION "@FPM_VERSION@"
1822 +#define PHP_FPM_CONF_PATH "@php_fpm_conf_path@"
1823 +#define PHP_FPM_LOG_PATH "@php_fpm_log_path@"
1824 +#define PHP_FPM_PID_PATH "@php_fpm_pid_path@"
1826 diff --git a/sapi/cgi/fpm/fpm_children.c b/sapi/cgi/fpm/fpm_children.c
1827 new file mode 100644
1829 +++ b/sapi/cgi/fpm/fpm_children.c
1833 + /* (c) 2007,2008 Andrei Nigmatulin */
1835 +#include "fpm_config.h"
1837 +#include <sys/types.h>
1838 +#include <sys/wait.h>
1840 +#include <unistd.h>
1841 +#include <string.h>
1845 +#include "fpm_children.h"
1846 +#include "fpm_signals.h"
1847 +#include "fpm_worker_pool.h"
1848 +#include "fpm_sockets.h"
1849 +#include "fpm_process_ctl.h"
1850 +#include "fpm_php.h"
1851 +#include "fpm_conf.h"
1852 +#include "fpm_cleanup.h"
1853 +#include "fpm_events.h"
1854 +#include "fpm_clock.h"
1855 +#include "fpm_stdio.h"
1856 +#include "fpm_unix.h"
1857 +#include "fpm_env.h"
1858 +#include "fpm_shm_slots.h"
1862 +static time_t *last_faults;
1865 +static int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop);
1867 +static void fpm_children_cleanup(int which, void *arg)
1869 + free(last_faults);
1872 +static struct fpm_child_s *fpm_child_alloc()
1874 + struct fpm_child_s *ret;
1876 + ret = malloc(sizeof(struct fpm_child_s));
1878 + if (!ret) return 0;
1880 + memset(ret, 0, sizeof(*ret));
1885 +static void fpm_child_free(struct fpm_child_s *child)
1890 +static void fpm_child_close(struct fpm_child_s *child, int in_event_loop)
1892 + if (child->fd_stdout != -1) {
1893 + if (in_event_loop) {
1894 + fpm_event_fire(&child->ev_stdout);
1896 + if (child->fd_stdout != -1) {
1897 + close(child->fd_stdout);
1901 + if (child->fd_stderr != -1) {
1902 + if (in_event_loop) {
1903 + fpm_event_fire(&child->ev_stderr);
1905 + if (child->fd_stderr != -1) {
1906 + close(child->fd_stderr);
1910 + fpm_child_free(child);
1913 +static void fpm_child_link(struct fpm_child_s *child)
1915 + struct fpm_worker_pool_s *wp = child->wp;
1917 + ++wp->running_children;
1918 + ++fpm_globals.running_children;
1920 + child->next = wp->children;
1921 + if (child->next) child->next->prev = child;
1923 + wp->children = child;
1926 +static void fpm_child_unlink(struct fpm_child_s *child)
1928 + --child->wp->running_children;
1929 + --fpm_globals.running_children;
1931 + if (child->prev) child->prev->next = child->next;
1932 + else child->wp->children = child->next;
1933 + if (child->next) child->next->prev = child->prev;
1937 +static struct fpm_child_s *fpm_child_find(pid_t pid)
1939 + struct fpm_worker_pool_s *wp;
1940 + struct fpm_child_s *child = 0;
1942 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1944 + for (child = wp->children; child; child = child->next) {
1945 + if (child->pid == pid) {
1960 +static void fpm_child_init(struct fpm_worker_pool_s *wp)
1962 + fpm_globals.max_requests = wp->config->max_requests;
1964 + if (0 > fpm_stdio_init_child(wp) ||
1965 + 0 > fpm_unix_init_child(wp) ||
1966 + 0 > fpm_signals_init_child() ||
1967 + 0 > fpm_env_init_child(wp) ||
1968 + 0 > fpm_php_init_child(wp)) {
1970 + zlog(ZLOG_STUFF, ZLOG_ERROR, "child failed to initialize (pool %s)", wp->config->name);
1975 +int fpm_children_free(struct fpm_child_s *child)
1977 + struct fpm_child_s *next;
1979 + for (; child; child = next) {
1980 + next = child->next;
1981 + fpm_child_close(child, 0 /* in_event_loop */);
1987 +void fpm_children_bury()
1991 + struct fpm_child_s *child;
1993 + while ( (pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
1995 + int severity = ZLOG_NOTICE;
1997 + child = fpm_child_find(pid);
1999 + if (WIFEXITED(status)) {
2001 + snprintf(buf, sizeof(buf), "with code %d", WEXITSTATUS(status));
2003 + if (WEXITSTATUS(status) != 0) {
2004 + severity = ZLOG_WARNING;
2008 + else if (WIFSIGNALED(status)) {
2009 + const char *signame = fpm_signal_names[WTERMSIG(status)];
2010 + const char *have_core = WCOREDUMP(status) ? " (core dumped)" : "";
2012 + if (signame == NULL) {
2016 + snprintf(buf, sizeof(buf), "on signal %d %s%s", WTERMSIG(status), signame, have_core);
2018 + if (WTERMSIG(status) != SIGQUIT) { /* possible request loss */
2019 + severity = ZLOG_WARNING;
2022 + else if (WIFSTOPPED(status)) {
2024 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "child %d stopped for tracing", (int) pid);
2026 + if (child && child->tracer) {
2027 + child->tracer(child);
2034 + struct fpm_worker_pool_s *wp = child->wp;
2035 + struct timeval tv1, tv2;
2037 + fpm_child_unlink(child);
2039 + fpm_shm_slots_discard_slot(child);
2041 + fpm_clock_get(&tv1);
2043 + timersub(&tv1, &child->started, &tv2);
2045 + zlog(ZLOG_STUFF, severity, "child %d (pool %s) exited %s after %ld.%06d seconds from start", (int) pid,
2046 + child->wp->config->name, buf, tv2.tv_sec, (int) tv2.tv_usec);
2048 + fpm_child_close(child, 1 /* in event_loop */);
2050 + fpm_pctl_child_exited();
2052 + if (last_faults && (WTERMSIG(status) == SIGSEGV || WTERMSIG(status) == SIGBUS)) {
2053 + time_t now = tv1.tv_sec;
2054 + int restart_condition = 1;
2057 + last_faults[fault++] = now;
2059 + if (fault == fpm_global_config.emergency_restart_threshold) {
2063 + for (i = 0; i < fpm_global_config.emergency_restart_threshold; i++) {
2064 + if (now - last_faults[i] > fpm_global_config.emergency_restart_interval) {
2065 + restart_condition = 0;
2070 + if (restart_condition) {
2072 + zlog(ZLOG_STUFF, ZLOG_WARNING, "failed processes threshold (%d in %d sec) is reached, initiating reload",
2073 + fpm_global_config.emergency_restart_threshold, fpm_global_config.emergency_restart_interval);
2075 + fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
2079 + fpm_children_make(wp, 1 /* in event loop */);
2081 + if (fpm_globals.is_child) {
2086 + zlog(ZLOG_STUFF, ZLOG_ALERT, "oops, unknown child exited %s", buf);
2092 +static struct fpm_child_s *fpm_resources_prepare(struct fpm_worker_pool_s *wp)
2094 + struct fpm_child_s *c;
2096 + c = fpm_child_alloc();
2099 + zlog(ZLOG_STUFF, ZLOG_ERROR, "malloc failed (pool %s)", wp->config->name);
2104 + c->fd_stdout = -1; c->fd_stderr = -1;
2106 + if (0 > fpm_stdio_prepare_pipes(c)) {
2107 + fpm_child_free(c);
2111 + if (0 > fpm_shm_slots_prepare_slot(c)) {
2112 + fpm_stdio_discard_pipes(c);
2113 + fpm_child_free(c);
2120 +static void fpm_resources_discard(struct fpm_child_s *child)
2122 + fpm_shm_slots_discard_slot(child);
2123 + fpm_stdio_discard_pipes(child);
2124 + fpm_child_free(child);
2127 +static void fpm_child_resources_use(struct fpm_child_s *child)
2129 + fpm_shm_slots_child_use_slot(child);
2130 + fpm_stdio_child_use_pipes(child);
2131 + fpm_child_free(child);
2134 +static void fpm_parent_resources_use(struct fpm_child_s *child)
2136 + fpm_shm_slots_parent_use_slot(child);
2137 + fpm_stdio_parent_use_pipes(child);
2138 + fpm_child_link(child);
2141 +static int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop)
2145 + struct fpm_child_s *child;
2147 + while (!enough && fpm_pctl_can_spawn_children() && wp->running_children < wp->config->pm->max_children) {
2149 + child = fpm_resources_prepare(wp);
2161 + fpm_child_resources_use(child);
2162 + fpm_globals.is_child = 1;
2163 + if (in_event_loop) {
2164 + fpm_event_exit_loop();
2166 + fpm_child_init(wp);
2170 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fork() failed");
2173 + fpm_resources_discard(child);
2175 + break; /* dont try any more on error */
2179 + fpm_clock_get(&child->started);
2180 + fpm_parent_resources_use(child);
2182 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "child %d (pool %s) started", (int) pid, wp->config->name);
2187 + return 1; /* we are done */
2190 +int fpm_children_create_initial(struct fpm_worker_pool_s *wp)
2192 + return fpm_children_make(wp, 0 /* not in event loop yet */);
2195 +int fpm_children_init_main()
2197 + if (fpm_global_config.emergency_restart_threshold &&
2198 + fpm_global_config.emergency_restart_interval) {
2200 + last_faults = malloc(sizeof(time_t) * fpm_global_config.emergency_restart_threshold);
2202 + if (!last_faults) {
2206 + memset(last_faults, 0, sizeof(time_t) * fpm_global_config.emergency_restart_threshold);
2209 + if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_children_cleanup, 0)) {
2216 diff --git a/sapi/cgi/fpm/fpm_children.h b/sapi/cgi/fpm/fpm_children.h
2217 new file mode 100644
2219 +++ b/sapi/cgi/fpm/fpm_children.h
2223 + /* (c) 2007,2008 Andrei Nigmatulin */
2225 +#ifndef FPM_CHILDREN_H
2226 +#define FPM_CHILDREN_H 1
2228 +#include <sys/time.h>
2229 +#include <sys/types.h>
2232 +#include "fpm_worker_pool.h"
2234 +int fpm_children_create_initial(struct fpm_worker_pool_s *wp);
2235 +int fpm_children_free(struct fpm_child_s *child);
2236 +void fpm_children_bury();
2237 +int fpm_children_init_main();
2239 +struct fpm_child_s;
2241 +struct fpm_child_s {
2242 + struct fpm_child_s *prev, *next;
2243 + struct timeval started;
2244 + struct fpm_worker_pool_s *wp;
2245 + struct event ev_stdout, ev_stderr;
2247 + int fd_stdout, fd_stderr;
2248 + void (*tracer)(struct fpm_child_s *);
2249 + struct timeval slow_logged;
2254 diff --git a/sapi/cgi/fpm/fpm_cleanup.c b/sapi/cgi/fpm/fpm_cleanup.c
2255 new file mode 100644
2257 +++ b/sapi/cgi/fpm/fpm_cleanup.c
2261 + /* (c) 2007,2008 Andrei Nigmatulin */
2263 +#include "fpm_config.h"
2265 +#include <stdlib.h>
2267 +#include "fpm_arrays.h"
2268 +#include "fpm_cleanup.h"
2273 + void (*cleanup)(int, void *);
2277 +static struct fpm_array_s cleanups = { .sz = sizeof(struct cleanup_s) };
2279 +int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *arg)
2281 + struct cleanup_s *c;
2283 + c = fpm_array_push(&cleanups);
2290 + c->cleanup = cleanup;
2296 +void fpm_cleanups_run(int type)
2298 + struct cleanup_s *c = fpm_array_item_last(&cleanups);
2299 + int cl = cleanups.used;
2301 + for ( ; cl--; c--) {
2302 + if (c->type & type) {
2303 + c->cleanup(type, c->arg);
2307 + fpm_array_free(&cleanups);
2310 diff --git a/sapi/cgi/fpm/fpm_cleanup.h b/sapi/cgi/fpm/fpm_cleanup.h
2311 new file mode 100644
2313 +++ b/sapi/cgi/fpm/fpm_cleanup.h
2317 + /* (c) 2007,2008 Andrei Nigmatulin */
2319 +#ifndef FPM_CLEANUP_H
2320 +#define FPM_CLEANUP_H 1
2322 +int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *);
2323 +void fpm_cleanups_run(int type);
2326 + FPM_CLEANUP_CHILD = (1 << 0),
2327 + FPM_CLEANUP_PARENT_EXIT = (1 << 1),
2328 + FPM_CLEANUP_PARENT_EXIT_MAIN = (1 << 2),
2329 + FPM_CLEANUP_PARENT_EXEC = (1 << 3),
2330 + FPM_CLEANUP_PARENT = (1 << 1) | (1 << 2) | (1 << 3),
2331 + FPM_CLEANUP_ALL = ~0,
2336 diff --git a/sapi/cgi/fpm/fpm_clock.c b/sapi/cgi/fpm/fpm_clock.c
2337 new file mode 100644
2339 +++ b/sapi/cgi/fpm/fpm_clock.c
2343 + /* (c) 2007,2008 Andrei Nigmatulin */
2345 +#include "fpm_config.h"
2347 +#if defined(HAVE_CLOCK_GETTIME)
2348 +#include <time.h> /* for CLOCK_MONOTONIC */
2351 +#include "fpm_clock.h"
2355 +/* posix monotonic clock - preferred source of time */
2356 +#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
2358 +static int monotonic_works;
2360 +int fpm_clock_init()
2362 + struct timespec ts;
2364 + monotonic_works = 0;
2366 + if (0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
2367 + monotonic_works = 1;
2373 +int fpm_clock_get(struct timeval *tv)
2375 + if (monotonic_works) {
2376 + struct timespec ts;
2378 + if (0 > clock_gettime(CLOCK_MONOTONIC, &ts)) {
2379 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "clock_gettime() failed");
2383 + tv->tv_sec = ts.tv_sec;
2384 + tv->tv_usec = ts.tv_nsec / 1000;
2388 + return gettimeofday(tv, 0);
2392 +#elif defined(HAVE_CLOCK_GET_TIME)
2394 +#include <mach/mach.h>
2395 +#include <mach/clock.h>
2396 +#include <mach/mach_error.h>
2398 +static clock_serv_t mach_clock;
2400 +/* this code borrowed from here: http://lists.apple.com/archives/Darwin-development/2002/Mar/msg00746.html */
2401 +/* mach_clock also should be re-initialized in child process after fork */
2402 +int fpm_clock_init()
2404 + kern_return_t ret;
2405 + mach_timespec_t aTime;
2407 + ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &mach_clock);
2409 + if (ret != KERN_SUCCESS) {
2410 + zlog(ZLOG_STUFF, ZLOG_ERROR, "host_get_clock_service() failed: %s", mach_error_string(ret));
2414 + /* test if it works */
2415 + ret = clock_get_time(mach_clock, &aTime);
2417 + if (ret != KERN_SUCCESS) {
2418 + zlog(ZLOG_STUFF, ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
2425 +int fpm_clock_get(struct timeval *tv)
2427 + kern_return_t ret;
2428 + mach_timespec_t aTime;
2430 + ret = clock_get_time(mach_clock, &aTime);
2432 + if (ret != KERN_SUCCESS) {
2433 + zlog(ZLOG_STUFF, ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
2437 + tv->tv_sec = aTime.tv_sec;
2438 + tv->tv_usec = aTime.tv_nsec / 1000;
2443 +#else /* no clock */
2445 +int fpm_clock_init()
2450 +int fpm_clock_get(struct timeval *tv)
2452 + return gettimeofday(tv, 0);
2456 diff --git a/sapi/cgi/fpm/fpm_clock.h b/sapi/cgi/fpm/fpm_clock.h
2457 new file mode 100644
2459 +++ b/sapi/cgi/fpm/fpm_clock.h
2463 + /* (c) 2007,2008 Andrei Nigmatulin */
2465 +#ifndef FPM_CLOCK_H
2466 +#define FPM_CLOCK_H 1
2468 +#include <sys/time.h>
2470 +int fpm_clock_init();
2471 +int fpm_clock_get(struct timeval *tv);
2474 diff --git a/sapi/cgi/fpm/fpm_conf.c b/sapi/cgi/fpm/fpm_conf.c
2475 new file mode 100644
2477 +++ b/sapi/cgi/fpm/fpm_conf.c
2481 + /* (c) 2007,2008 Andrei Nigmatulin */
2483 +#include "fpm_config.h"
2485 +#include <sys/types.h>
2486 +#include <sys/stat.h>
2488 +#include <string.h>
2489 +#include <stdlib.h>
2490 +#include <stddef.h>
2491 +#include <stdint.h>
2493 +#include <unistd.h>
2496 +#include "fpm_conf.h"
2497 +#include "fpm_stdio.h"
2498 +#include "fpm_worker_pool.h"
2499 +#include "fpm_cleanup.h"
2500 +#include "fpm_php.h"
2501 +#include "fpm_sockets.h"
2502 +#include "xml_config.h"
2506 +struct fpm_global_config_s fpm_global_config;
2508 +static void *fpm_global_config_ptr()
2510 + return &fpm_global_config;
2513 +static char *fpm_conf_set_log_level(void **conf, char *name, void *vv, intptr_t offset)
2517 + if (!strcmp(value, "debug")) {
2518 + fpm_globals.log_level = ZLOG_DEBUG;
2520 + else if (!strcmp(value, "notice")) {
2521 + fpm_globals.log_level = ZLOG_NOTICE;
2523 + else if (!strcmp(value, "warn")) {
2524 + fpm_globals.log_level = ZLOG_WARNING;
2526 + else if (!strcmp(value, "error")) {
2527 + fpm_globals.log_level = ZLOG_ERROR;
2529 + else if (!strcmp(value, "alert")) {
2530 + fpm_globals.log_level = ZLOG_ALERT;
2533 + return "invalid value for 'log_level'";
2539 +static struct xml_conf_section xml_section_fpm_global_options = {
2540 + .conf = &fpm_global_config_ptr,
2541 + .path = "/configuration/global_options",
2542 + .parsers = (struct xml_value_parser []) {
2543 + { XML_CONF_SCALAR, "emergency_restart_threshold", &xml_conf_set_slot_integer, offsetof(struct fpm_global_config_s, emergency_restart_threshold) },
2544 + { XML_CONF_SCALAR, "emergency_restart_interval", &xml_conf_set_slot_time, offsetof(struct fpm_global_config_s, emergency_restart_interval) },
2545 + { XML_CONF_SCALAR, "process_control_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_global_config_s, process_control_timeout) },
2546 + { XML_CONF_SCALAR, "daemonize", &xml_conf_set_slot_boolean, offsetof(struct fpm_global_config_s, daemonize) },
2547 + { XML_CONF_SCALAR, "pid_file", &xml_conf_set_slot_string, offsetof(struct fpm_global_config_s, pid_file) },
2548 + { XML_CONF_SCALAR, "error_log", &xml_conf_set_slot_string, offsetof(struct fpm_global_config_s, error_log) },
2549 + { XML_CONF_SCALAR, "log_level", &fpm_conf_set_log_level, 0 },
2554 +static char *fpm_conf_set_pm_style(void **conf, char *name, void *vv, intptr_t offset)
2557 + struct fpm_pm_s *c = *conf;
2559 + if (!strcmp(value, "static")) {
2560 + c->style = PM_STYLE_STATIC;
2562 + else if (!strcmp(value, "apache-like")) {
2563 + c->style = PM_STYLE_APACHE_LIKE;
2566 + return "invalid value for 'style'";
2572 +static char *fpm_conf_set_rlimit_core(void **conf, char *name, void *vv, intptr_t offset)
2575 + struct fpm_worker_pool_config_s *c = *conf;
2577 + if (!strcmp(value, "unlimited")) {
2578 + c->rlimit_core = -1;
2582 + void *subconf = &int_value;
2585 + error = xml_conf_set_slot_integer(&subconf, name, vv, 0);
2587 + if (error) return error;
2589 + if (int_value < 0) return "invalid value for 'rlimit_core'";
2591 + c->rlimit_core = int_value;
2597 +static char *fpm_conf_set_catch_workers_output(void **conf, char *name, void *vv, intptr_t offset)
2599 + struct fpm_worker_pool_config_s *c = *conf;
2601 + void *subconf = &int_value;
2604 + error = xml_conf_set_slot_boolean(&subconf, name, vv, 0);
2606 + if (error) return error;
2608 + c->catch_workers_output = int_value;
2613 +static struct xml_conf_section fpm_conf_set_apache_like_subsection_conf = {
2614 + .path = "apache_like somewhere", /* fixme */
2615 + .parsers = (struct xml_value_parser []) {
2616 + { XML_CONF_SCALAR, "StartServers", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.StartServers) },
2617 + { XML_CONF_SCALAR, "MinSpareServers", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.MinSpareServers) },
2618 + { XML_CONF_SCALAR, "MaxSpareServers", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.MaxSpareServers) },
2623 +static char *fpm_conf_set_apache_like_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2625 + return xml_conf_parse_section(conf, &fpm_conf_set_apache_like_subsection_conf, xml_node);
2628 +static struct xml_conf_section fpm_conf_set_listen_options_subsection_conf = {
2629 + .path = "listen options somewhere", /* fixme */
2630 + .parsers = (struct xml_value_parser []) {
2631 + { XML_CONF_SCALAR, "backlog", &xml_conf_set_slot_integer, offsetof(struct fpm_listen_options_s, backlog) },
2632 + { XML_CONF_SCALAR, "owner", &xml_conf_set_slot_string, offsetof(struct fpm_listen_options_s, owner) },
2633 + { XML_CONF_SCALAR, "group", &xml_conf_set_slot_string, offsetof(struct fpm_listen_options_s, group) },
2634 + { XML_CONF_SCALAR, "mode", &xml_conf_set_slot_string, offsetof(struct fpm_listen_options_s, mode) },
2639 +static char *fpm_conf_set_listen_options_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2641 + void *subconf = (char *) *conf + offset;
2642 + struct fpm_listen_options_s *lo;
2644 + lo = malloc(sizeof(*lo));
2647 + return "malloc() failed";
2650 + memset(lo, 0, sizeof(*lo));
2654 + * (struct fpm_listen_options_s **) subconf = lo;
2658 + return xml_conf_parse_section(&subconf, &fpm_conf_set_listen_options_subsection_conf, xml_node);
2661 +static struct xml_conf_section fpm_conf_set_pm_subsection_conf = {
2662 + .path = "pm settings somewhere", /* fixme */
2663 + .parsers = (struct xml_value_parser []) {
2664 + { XML_CONF_SCALAR, "style", &fpm_conf_set_pm_style, 0 },
2665 + { XML_CONF_SCALAR, "max_children", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, max_children) },
2666 + { XML_CONF_SUBSECTION, "apache_like", &fpm_conf_set_apache_like_subsection, offsetof(struct fpm_pm_s, options_apache_like) },
2671 +static char *fpm_conf_set_pm_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2673 + void *subconf = (char *) *conf + offset;
2674 + struct fpm_pm_s *pm;
2676 + pm = malloc(sizeof(*pm));
2679 + return "fpm_conf_set_pm_subsection(): malloc failed";
2682 + memset(pm, 0, sizeof(*pm));
2684 + * (struct fpm_pm_s **) subconf = pm;
2688 + return xml_conf_parse_section(&subconf, &fpm_conf_set_pm_subsection_conf, xml_node);
2691 +static char *xml_conf_set_slot_key_value_pair(void **conf, char *name, void *vv, intptr_t offset)
2694 + struct key_value_s *kv;
2695 + struct key_value_s ***parent = (struct key_value_s ***) conf;
2697 + kv = malloc(sizeof(*kv));
2700 + return "malloc() failed";
2703 + memset(kv, 0, sizeof(*kv));
2705 + kv->key = strdup(name);
2706 + kv->value = strdup(value);
2708 + if (!kv->key || !kv->value) {
2709 + return "xml_conf_set_slot_key_value_pair(): strdup() failed";
2714 + *parent = &kv->next;
2719 +static struct xml_conf_section fpm_conf_set_key_value_pairs_subsection_conf = {
2720 + .path = "key_value_pairs somewhere", /* fixme */
2721 + .parsers = (struct xml_value_parser []) {
2722 + { XML_CONF_SCALAR, 0, &xml_conf_set_slot_key_value_pair, 0 },
2727 +static char *fpm_conf_set_key_value_pairs_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2729 + void *next_kv = (char *) *conf + offset;
2731 + return xml_conf_parse_section(&next_kv, &fpm_conf_set_key_value_pairs_subsection_conf, xml_node);
2734 +static void *fpm_worker_pool_config_alloc()
2736 + static struct fpm_worker_pool_s *current_wp = 0;
2737 + struct fpm_worker_pool_s *wp;
2739 + wp = fpm_worker_pool_alloc();
2741 + if (!wp) return 0;
2743 + wp->config = malloc(sizeof(struct fpm_worker_pool_config_s));
2745 + if (!wp->config) return 0;
2747 + memset(wp->config, 0, sizeof(struct fpm_worker_pool_config_s));
2749 + if (current_wp) current_wp->next = wp;
2753 + return wp->config;
2756 +int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc)
2758 + struct key_value_s *kv, *kv_next;
2761 + free(wpc->listen_address);
2762 + if (wpc->listen_options) {
2763 + free(wpc->listen_options->owner);
2764 + free(wpc->listen_options->group);
2765 + free(wpc->listen_options->mode);
2766 + free(wpc->listen_options);
2768 + for (kv = wpc->php_defines; kv; kv = kv_next) {
2769 + kv_next = kv->next;
2774 + for (kv = wpc->environment; kv; kv = kv_next) {
2775 + kv_next = kv->next;
2783 + free(wpc->chroot);
2785 + free(wpc->allowed_clients);
2786 + free(wpc->slowlog);
2791 +static struct xml_conf_section xml_section_fpm_worker_pool_config = {
2792 + .conf = &fpm_worker_pool_config_alloc,
2793 + .path = "/configuration/workers/pool",
2794 + .parsers = (struct xml_value_parser []) {
2795 + { XML_CONF_SCALAR, "name", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, name) },
2796 + { XML_CONF_SCALAR, "listen_address", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, listen_address) },
2797 + { XML_CONF_SUBSECTION, "listen_options", &fpm_conf_set_listen_options_subsection, offsetof(struct fpm_worker_pool_config_s, listen_options) },
2798 + { XML_CONF_SUBSECTION, "php_defines", &fpm_conf_set_key_value_pairs_subsection, offsetof(struct fpm_worker_pool_config_s, php_defines) },
2799 + { XML_CONF_SCALAR, "user", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, user) },
2800 + { XML_CONF_SCALAR, "group", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, group) },
2801 + { XML_CONF_SCALAR, "chroot", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, chroot) },
2802 + { XML_CONF_SCALAR, "chdir", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, chdir) },
2803 + { XML_CONF_SCALAR, "allowed_clients", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, allowed_clients) },
2804 + { XML_CONF_SUBSECTION, "environment", &fpm_conf_set_key_value_pairs_subsection, offsetof(struct fpm_worker_pool_config_s, environment) },
2805 + { XML_CONF_SCALAR, "request_terminate_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_worker_pool_config_s, request_terminate_timeout) },
2806 + { XML_CONF_SCALAR, "request_slowlog_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_worker_pool_config_s, request_slowlog_timeout) },
2807 + { XML_CONF_SCALAR, "slowlog", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, slowlog) },
2808 + { XML_CONF_SCALAR, "rlimit_files", &xml_conf_set_slot_integer, offsetof(struct fpm_worker_pool_config_s, rlimit_files) },
2809 + { XML_CONF_SCALAR, "rlimit_core", &fpm_conf_set_rlimit_core, 0 },
2810 + { XML_CONF_SCALAR, "max_requests", &xml_conf_set_slot_integer, offsetof(struct fpm_worker_pool_config_s, max_requests) },
2811 + { XML_CONF_SCALAR, "catch_workers_output", &fpm_conf_set_catch_workers_output, 0 },
2812 + { XML_CONF_SUBSECTION, "pm", &fpm_conf_set_pm_subsection, offsetof(struct fpm_worker_pool_config_s, pm) },
2817 +static struct xml_conf_section *fpm_conf_all_sections[] = {
2818 + &xml_section_fpm_global_options,
2819 + &xml_section_fpm_worker_pool_config,
2823 +static int fpm_evaluate_full_path(char **path)
2825 + if (**path != '/') {
2828 + full_path = malloc(sizeof(PHP_PREFIX) + strlen(*path) + 1);
2830 + if (!full_path) return -1;
2832 + sprintf(full_path, "%s/%s", PHP_PREFIX, *path);
2836 + *path = full_path;
2842 +static int fpm_conf_process_all_pools()
2844 + struct fpm_worker_pool_s *wp;
2846 + if (!fpm_worker_all_pools) {
2847 + zlog(ZLOG_STUFF, ZLOG_ERROR, "at least one pool section must be specified in config file");
2851 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
2853 + if (wp->config->listen_address && *wp->config->listen_address) {
2855 + wp->listen_address_domain = fpm_sockets_domain_from_address(wp->config->listen_address);
2857 + if (wp->listen_address_domain == FPM_AF_UNIX && *wp->config->listen_address != '/') {
2858 + fpm_evaluate_full_path(&wp->config->listen_address);
2864 + wp->is_template = 1;
2868 + if (wp->config->request_slowlog_timeout) {
2870 + if (! (wp->config->slowlog && *wp->config->slowlog)) {
2871 + zlog(ZLOG_STUFF, ZLOG_ERROR, "pool %s: 'slowlog' must be specified for use with 'request_slowlog_timeout'",
2872 + wp->config->name);
2876 + static int warned = 0;
2879 + zlog(ZLOG_STUFF, ZLOG_WARNING, "pool %s: 'request_slowlog_timeout' is not supported on your system",
2880 + wp->config->name);
2884 + wp->config->request_slowlog_timeout = 0;
2888 + if (wp->config->request_slowlog_timeout && wp->config->slowlog && *wp->config->slowlog) {
2891 + fpm_evaluate_full_path(&wp->config->slowlog);
2893 + if (wp->config->request_slowlog_timeout) {
2894 + fd = open(wp->config->slowlog, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
2897 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(%s) failed", wp->config->slowlog);
2908 +int fpm_conf_unlink_pid()
2910 + if (fpm_global_config.pid_file) {
2912 + if (0 > unlink(fpm_global_config.pid_file)) {
2913 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "unlink(\"%s\") failed", fpm_global_config.pid_file);
2922 +int fpm_conf_write_pid()
2926 + if (fpm_global_config.pid_file) {
2930 + unlink(fpm_global_config.pid_file);
2932 + fd = creat(fpm_global_config.pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
2935 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "creat(\"%s\") failed", fpm_global_config.pid_file);
2939 + len = sprintf(buf, "%d", (int) fpm_globals.parent_pid);
2941 + if (len != write(fd, buf, len)) {
2942 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "write() failed");
2952 +static int fpm_conf_post_process()
2954 + if (fpm_global_config.pid_file) {
2955 + fpm_evaluate_full_path(&fpm_global_config.pid_file);
2958 + if (!fpm_global_config.error_log) {
2959 + fpm_global_config.error_log = strdup(PHP_FPM_LOG_PATH);
2962 + fpm_evaluate_full_path(&fpm_global_config.error_log);
2964 + if (0 > fpm_stdio_open_error_log(0)) {
2968 + return fpm_conf_process_all_pools();
2971 +static void fpm_conf_cleanup(int which, void *arg)
2973 + free(fpm_global_config.pid_file);
2974 + free(fpm_global_config.error_log);
2975 + fpm_global_config.pid_file = 0;
2976 + fpm_global_config.error_log = 0;
2979 +int fpm_conf_init_main()
2981 + char *filename = fpm_globals.config;
2984 + if (0 > xml_conf_sections_register(fpm_conf_all_sections)) {
2988 + if (filename == NULL) {
2989 + filename = PHP_FPM_CONF_PATH;
2992 + err = xml_conf_load_file(filename);
2995 + zlog(ZLOG_STUFF, ZLOG_ERROR, "failed to load configuration file: %s", err);
2999 + if (0 > fpm_conf_post_process()) {
3005 + if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_conf_cleanup, 0)) {
3011 diff --git a/sapi/cgi/fpm/fpm_conf.h b/sapi/cgi/fpm/fpm_conf.h
3012 new file mode 100644
3014 +++ b/sapi/cgi/fpm/fpm_conf.h
3018 + /* (c) 2007,2008 Andrei Nigmatulin */
3021 +#define FPM_CONF_H 1
3023 +struct key_value_s;
3025 +struct key_value_s {
3026 + struct key_value_s *next;
3031 +struct fpm_global_config_s {
3032 + int emergency_restart_threshold;
3033 + int emergency_restart_interval;
3034 + int process_control_timeout;
3040 +extern struct fpm_global_config_s fpm_global_config;
3047 + int MinSpareServers;
3048 + int MaxSpareServers;
3049 + } options_apache_like;
3052 +struct fpm_listen_options_s {
3059 +struct fpm_worker_pool_config_s {
3061 + char *listen_address;
3062 + struct fpm_listen_options_s *listen_options;
3063 + struct key_value_s *php_defines;
3068 + char *allowed_clients;
3069 + struct key_value_s *environment;
3070 + struct fpm_pm_s *pm;
3071 + int request_terminate_timeout;
3072 + int request_slowlog_timeout;
3077 + unsigned catch_workers_output:1;
3080 +enum { PM_STYLE_STATIC = 1, PM_STYLE_APACHE_LIKE = 2 };
3082 +int fpm_conf_init_main();
3083 +int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc);
3084 +int fpm_conf_write_pid();
3085 +int fpm_conf_unlink_pid();
3089 diff --git a/sapi/cgi/fpm/fpm_config.h b/sapi/cgi/fpm/fpm_config.h
3090 new file mode 100644
3092 +++ b/sapi/cgi/fpm/fpm_config.h
3096 + /* (c) 2007,2008 Andrei Nigmatulin */
3098 +#include "php_config.h"
3099 +#include "fpm_autoconf.h"
3102 +/* Solaris does not have it */
3103 +#ifndef INADDR_NONE
3104 +#define INADDR_NONE (-1)
3108 +/* If we're not using GNU C, elide __attribute__ */
3110 +# define __attribute__(x) /*NOTHING*/
3114 +/* Solaris does not have it */
3116 +#define timersub(tvp, uvp, vvp) \
3118 + (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
3119 + (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
3120 + if ((vvp)->tv_usec < 0) { \
3121 + (vvp)->tv_sec--; \
3122 + (vvp)->tv_usec += 1000000; \
3127 +#if defined(HAVE_PTRACE) || defined(PROC_MEM_FILE) || defined(HAVE_MACH_VM_READ)
3128 +#define HAVE_FPM_TRACE 1
3130 +#define HAVE_FPM_TRACE 0
3133 diff --git a/sapi/cgi/fpm/fpm_env.c b/sapi/cgi/fpm/fpm_env.c
3134 new file mode 100644
3136 +++ b/sapi/cgi/fpm/fpm_env.c
3140 + /* (c) 2007,2008 Andrei Nigmatulin */
3142 +#include "fpm_config.h"
3144 +#ifdef HAVE_ALLOCA_H
3145 +#include <alloca.h>
3148 +#include <stdlib.h>
3149 +#include <string.h>
3151 +#include "fpm_env.h"
3154 +#ifndef HAVE_SETENV
3155 +int setenv(char *name, char *value, int overwrite)
3157 + int name_len = strlen(name);
3158 + int value_len = strlen(value);
3159 + char *var = alloca(name_len + 1 + value_len + 1);
3161 + memcpy(var, name, name_len);
3163 + var[name_len] = '=';
3165 + memcpy(var + name_len + 1, value, value_len);
3167 + var[name_len + 1 + value_len] = '\0';
3169 + return putenv(var);
3173 +#ifndef HAVE_CLEARENV
3179 + /* this algo is the only one known to me
3180 + that works well on all systems */
3181 + while (*(envp = environ)) {
3182 + char *eq = strchr(*envp, '=');
3184 + s = strdup(*envp);
3186 + if (eq) s[eq - *envp] = '\0';
3196 +int fpm_env_init_child(struct fpm_worker_pool_s *wp)
3198 + struct key_value_s *kv;
3202 + for (kv = wp->config->environment; kv; kv = kv->next) {
3203 + setenv(kv->key, kv->value, 1);
3207 + setenv("USER", wp->user, 1);
3211 + setenv("HOME", wp->home, 1);
3217 +static int fpm_env_conf_wp(struct fpm_worker_pool_s *wp)
3219 + struct key_value_s *kv;
3221 + kv = wp->config->environment;
3223 + for (kv = wp->config->environment; kv; kv = kv->next) {
3224 + if (*kv->value == '$') {
3225 + char *value = getenv(kv->value + 1);
3227 + if (!value) value = "";
3230 + kv->value = strdup(value);
3233 + /* autodetected values should be removed
3234 + if these vars specified in config */
3235 + if (!strcmp(kv->key, "USER")) {
3240 + if (!strcmp(kv->key, "HOME")) {
3249 +int fpm_env_init_main()
3251 + struct fpm_worker_pool_s *wp;
3253 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
3255 + if (0 > fpm_env_conf_wp(wp)) {
3263 diff --git a/sapi/cgi/fpm/fpm_env.h b/sapi/cgi/fpm/fpm_env.h
3264 new file mode 100644
3266 +++ b/sapi/cgi/fpm/fpm_env.h
3270 + /* (c) 2007,2008 Andrei Nigmatulin */
3273 +#define FPM_ENV_H 1
3275 +#include "fpm_worker_pool.h"
3277 +int fpm_env_init_child(struct fpm_worker_pool_s *wp);
3278 +int fpm_env_init_main();
3280 +extern char **environ;
3282 +#ifndef HAVE_SETENV
3283 +int setenv(char *name, char *value, int overwrite);
3286 +#ifndef HAVE_CLEARENV
3292 diff --git a/sapi/cgi/fpm/fpm_events.c b/sapi/cgi/fpm/fpm_events.c
3293 new file mode 100644
3295 +++ b/sapi/cgi/fpm/fpm_events.c
3299 + /* (c) 2007,2008 Andrei Nigmatulin */
3301 +#include "fpm_config.h"
3303 +#include <unistd.h>
3305 +#include <stdlib.h> /* for putenv */
3306 +#include <string.h>
3307 +#include <sys/types.h> /* for event.h below */
3311 +#include "fpm_process_ctl.h"
3312 +#include "fpm_events.h"
3313 +#include "fpm_cleanup.h"
3314 +#include "fpm_stdio.h"
3315 +#include "fpm_signals.h"
3316 +#include "fpm_children.h"
3319 +static void fpm_event_cleanup(int which, void *arg)
3321 + event_base_free(0);
3324 +static void fpm_got_signal(int fd, short ev, void *arg)
3332 + res = read(fd, &c, 1);
3333 + } while (res == -1 && errno == EINTR);
3336 + if (res < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
3337 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
3343 + case 'C' : /* SIGCHLD */
3344 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGCHLD");
3345 + fpm_children_bury();
3347 + case 'I' : /* SIGINT */
3348 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGINT");
3349 + fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
3351 + case 'T' : /* SIGTERM */
3352 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGTERM");
3353 + fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
3355 + case 'Q' : /* SIGQUIT */
3356 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGQUIT");
3357 + fpm_pctl(FPM_PCTL_STATE_FINISHING, FPM_PCTL_ACTION_SET);
3359 + case '1' : /* SIGUSR1 */
3360 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGUSR1");
3361 + if (0 == fpm_stdio_open_error_log(1)) {
3362 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "log file re-opened");
3365 + case '2' : /* SIGUSR2 */
3366 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGUSR2");
3367 + fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
3371 + if (fpm_globals.is_child) {
3380 +int fpm_event_init_main()
3384 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: using %s", event_get_method());
3386 + if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_event_cleanup, 0)) {
3393 +int fpm_event_loop()
3395 + static struct event signal_fd_event;
3397 + event_set(&signal_fd_event, fpm_signals_get_fd(), EV_PERSIST | EV_READ, &fpm_got_signal, 0);
3399 + event_add(&signal_fd_event, 0);
3401 + fpm_pctl_heartbeat(-1, 0, 0);
3403 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: entering main loop");
3410 +int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg)
3412 + event_set(ev, fd, EV_PERSIST | EV_READ, callback, arg);
3414 + return event_add(ev, 0);
3417 +int fpm_event_del(struct event *ev)
3419 + return event_del(ev);
3422 +void fpm_event_exit_loop()
3424 + event_loopbreak();
3427 +void fpm_event_fire(struct event *ev)
3429 + (*ev->ev_callback)( (int) ev->ev_fd, (short) ev->ev_res, ev->ev_arg);
3432 diff --git a/sapi/cgi/fpm/fpm_events.h b/sapi/cgi/fpm/fpm_events.h
3433 new file mode 100644
3435 +++ b/sapi/cgi/fpm/fpm_events.h
3439 + /* (c) 2007,2008 Andrei Nigmatulin */
3441 +#ifndef FPM_EVENTS_H
3442 +#define FPM_EVENTS_H 1
3444 +void fpm_event_exit_loop();
3445 +int fpm_event_loop();
3446 +int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg);
3447 +int fpm_event_del(struct event *ev);
3448 +void fpm_event_fire(struct event *ev);
3449 +int fpm_event_init_main();
3453 diff --git a/sapi/cgi/fpm/fpm_php.c b/sapi/cgi/fpm/fpm_php.c
3454 new file mode 100644
3456 +++ b/sapi/cgi/fpm/fpm_php.c
3460 + /* (c) 2007,2008 Andrei Nigmatulin */
3462 +#include "fpm_config.h"
3464 +#include <stdlib.h>
3465 +#include <string.h>
3469 +#include "php_main.h"
3470 +#include "php_ini.h"
3471 +#include "ext/standard/dl.h"
3473 +#include "fastcgi.h"
3476 +#include "fpm_php.h"
3477 +#include "fpm_cleanup.h"
3478 +#include "fpm_worker_pool.h"
3480 +static int zend_ini_alter_master(char *name, int name_length, char *new_value, int new_value_length, int stage TSRMLS_DC)
3482 + zend_ini_entry *ini_entry;
3485 + if (zend_hash_find(EG(ini_directives), name, name_length, (void **) &ini_entry) == FAILURE) {
3489 + duplicate = strdup(new_value);
3491 + if (!ini_entry->on_modify
3492 + || ini_entry->on_modify(ini_entry, duplicate, new_value_length,
3493 + ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage TSRMLS_CC) == SUCCESS) {
3494 + ini_entry->value = duplicate;
3495 + ini_entry->value_length = new_value_length;
3503 +static void fpm_php_disable(char *value, int (*zend_disable)(char *, uint TSRMLS_DC) TSRMLS_DC)
3505 + char *s = 0, *e = value;
3513 + zend_disable(s, e - s TSRMLS_CC);
3527 + zend_disable(s, e - s TSRMLS_CC);
3531 +static int fpm_php_apply_defines(struct fpm_worker_pool_s *wp)
3534 + struct key_value_s *kv;
3536 + for (kv = wp->config->php_defines; kv; kv = kv->next) {
3537 + char *name = kv->key;
3538 + char *value = kv->value;
3539 + int name_len = strlen(name);
3540 + int value_len = strlen(value);
3542 + if (!strcmp(name, "extension") && *value) {
3545 +#if defined(PHP_VERSION_ID) && (PHP_VERSION_ID >= 50300)
3546 + php_dl(value, MODULE_PERSISTENT, &zv, 1 TSRMLS_CC);
3549 + ZVAL_STRINGL(&filename, value, value_len, 0);
3550 +#if (PHP_MAJOR_VERSION >= 5)
3551 + php_dl(&filename, MODULE_PERSISTENT, &zv, 1 TSRMLS_CC);
3553 + php_dl(&filename, MODULE_PERSISTENT, &zv TSRMLS_CC);
3559 + zend_ini_alter_master(name, name_len + 1, value, value_len, PHP_INI_STAGE_ACTIVATE TSRMLS_CC);
3561 + if (!strcmp(name, "disable_functions") && *value) {
3562 + char *v = strdup(value);
3563 +#if (PHP_MAJOR_VERSION >= 5)
3564 + PG(disable_functions) = v;
3566 + fpm_php_disable(v, zend_disable_function TSRMLS_CC);
3568 + else if (!strcmp(name, "disable_classes") && *value) {
3569 + char *v = strdup(value);
3570 +#if (PHP_MAJOR_VERSION >= 5)
3571 + PG(disable_classes) = v;
3573 + fpm_php_disable(v, zend_disable_class TSRMLS_CC);
3580 +static int fpm_php_set_allowed_clients(struct fpm_worker_pool_s *wp)
3582 + if (wp->listen_address_domain == FPM_AF_INET) {
3583 + fcgi_set_allowed_clients(wp->config->allowed_clients);
3589 +static int fpm_php_set_fcgi_mgmt_vars(struct fpm_worker_pool_s *wp)
3591 + char max_workers[10 + 1]; /* 4294967295 */
3594 + len = sprintf(max_workers, "%u", (unsigned int) wp->config->pm->max_children);
3596 + fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, max_workers, len);
3597 + fcgi_set_mgmt_var("FCGI_MAX_REQS", sizeof("FCGI_MAX_REQS")-1, max_workers, len);
3602 +char *fpm_php_script_filename(TSRMLS_D)
3604 + return SG(request_info).path_translated;
3607 +char *fpm_php_request_method(TSRMLS_D)
3609 + return (char *) SG(request_info).request_method;
3612 +size_t fpm_php_content_length(TSRMLS_D)
3614 + return SG(request_info).content_length;
3617 +static void fpm_php_cleanup(int which, void *arg)
3620 + php_module_shutdown(TSRMLS_C);
3624 +void fpm_php_soft_quit()
3626 + fcgi_set_in_shutdown(1);
3629 +int fpm_php_init_main()
3631 + if (0 > fpm_cleanup_add(FPM_CLEANUP_PARENT, fpm_php_cleanup, 0)) {
3638 +int fpm_php_init_child(struct fpm_worker_pool_s *wp)
3640 + if (0 > fpm_php_apply_defines(wp) ||
3641 + 0 > fpm_php_set_allowed_clients(wp) ||
3642 + 0 > fpm_php_set_fcgi_mgmt_vars(wp)) {
3648 diff --git a/sapi/cgi/fpm/fpm_php.h b/sapi/cgi/fpm/fpm_php.h
3649 new file mode 100644
3651 +++ b/sapi/cgi/fpm/fpm_php.h
3655 + /* (c) 2007,2008 Andrei Nigmatulin */
3658 +#define FPM_PHP_H 1
3662 +#include "build-defs.h" /* for PHP_ defines */
3664 +struct fpm_worker_pool_s;
3666 +int fpm_php_init_child(struct fpm_worker_pool_s *wp);
3667 +char *fpm_php_script_filename(TSRMLS_D);
3668 +char *fpm_php_request_method(TSRMLS_D);
3669 +size_t fpm_php_content_length(TSRMLS_D);
3670 +void fpm_php_soft_quit();
3671 +int fpm_php_init_main();
3675 diff --git a/sapi/cgi/fpm/fpm_php_trace.c b/sapi/cgi/fpm/fpm_php_trace.c
3676 new file mode 100644
3678 +++ b/sapi/cgi/fpm/fpm_php_trace.c
3682 + /* (c) 2007,2008 Andrei Nigmatulin */
3684 +#include "fpm_config.h"
3689 +#include "php_main.h"
3692 +#include <stddef.h>
3693 +#include <stdint.h>
3694 +#include <unistd.h>
3695 +#include <sys/time.h>
3696 +#include <sys/types.h>
3699 +#include "fpm_trace.h"
3700 +#include "fpm_php_trace.h"
3701 +#include "fpm_children.h"
3702 +#include "fpm_worker_pool.h"
3703 +#include "fpm_process_ctl.h"
3708 +#define valid_ptr(p) ((p) && 0 == ((p) & (sizeof(long) - 1)))
3710 +#if SIZEOF_LONG == 4
3711 +#define PTR_FMT "08"
3712 +#elif SIZEOF_LONG == 8
3713 +#define PTR_FMT "016"
3717 +static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog TSRMLS_DC)
3719 + int callers_limit = 20;
3720 + pid_t pid = child->pid;
3721 + struct timeval tv;
3722 + static const int buf_size = 1024;
3723 + char buf[buf_size];
3724 + long execute_data;
3727 + gettimeofday(&tv, 0);
3729 + zlog_print_time(&tv, buf, buf_size);
3731 + fprintf(slowlog, "\n%s pid %d (pool %s)\n", buf, (int) pid, child->wp->config->name);
3733 + if (0 > fpm_trace_get_strz(buf, buf_size, (long) &SG(request_info).path_translated)) {
3737 + fprintf(slowlog, "script_filename = %s\n", buf);
3739 + if (0 > fpm_trace_get_long((long) &EG(current_execute_data), &l)) {
3745 + while (execute_data) {
3749 + fprintf(slowlog, "[0x%" PTR_FMT "lx] ", execute_data);
3751 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, function_state.function), &l)) {
3757 + if (valid_ptr(function)) {
3758 + if (0 > fpm_trace_get_strz(buf, buf_size, function + offsetof(zend_function, common.function_name))) {
3762 + fprintf(slowlog, "%s()", buf);
3765 + fprintf(slowlog, "???");
3768 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, op_array), &l)) {
3774 + if (valid_ptr(l)) {
3775 + long op_array = l;
3777 + if (0 > fpm_trace_get_strz(buf, buf_size, op_array + offsetof(zend_op_array, filename))) {
3782 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, opline), &l)) {
3786 + if (valid_ptr(l)) {
3788 + uint *lu = (uint *) &l;
3790 + if (0 > fpm_trace_get_long(opline + offsetof(struct _zend_op, lineno), &l)) {
3797 + fprintf(slowlog, " %s:%u\n", *buf ? buf : "unknown", lineno);
3799 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, prev_execute_data), &l)) {
3805 + if (0 == --callers_limit) {
3813 +void fpm_php_trace(struct fpm_child_s *child)
3818 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "about to trace %d", (int) child->pid);
3820 + slowlog = fopen(child->wp->config->slowlog, "a+");
3823 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fopen(%s) failed", child->wp->config->slowlog);
3827 + if (0 > fpm_trace_ready(child->pid)) {
3831 + if (0 > fpm_php_trace_dump(child, slowlog TSRMLS_CC)) {
3832 + fprintf(slowlog, "+++ dump failed\n");
3835 + if (0 > fpm_trace_close(child->pid)) {
3843 + fpm_pctl_kill(child->pid, FPM_PCTL_CONT);
3844 + child->tracer = 0;
3846 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "finished trace of %d", (int) child->pid);
3851 diff --git a/sapi/cgi/fpm/fpm_php_trace.h b/sapi/cgi/fpm/fpm_php_trace.h
3852 new file mode 100644
3854 +++ b/sapi/cgi/fpm/fpm_php_trace.h
3858 + /* (c) 2007,2008 Andrei Nigmatulin */
3860 +#ifndef FPM_PHP_TRACE_H
3861 +#define FPM_PHP_TRACE_H 1
3863 +struct fpm_child_s;
3865 +void fpm_php_trace(struct fpm_child_s *);
3869 diff --git a/sapi/cgi/fpm/fpm_process_ctl.c b/sapi/cgi/fpm/fpm_process_ctl.c
3870 new file mode 100644
3872 +++ b/sapi/cgi/fpm/fpm_process_ctl.c
3876 + /* (c) 2007,2008 Andrei Nigmatulin */
3878 +#include "fpm_config.h"
3880 +#include <sys/types.h>
3881 +#include <signal.h>
3882 +#include <unistd.h>
3883 +#include <stdlib.h>
3886 +#include "fpm_clock.h"
3887 +#include "fpm_children.h"
3888 +#include "fpm_signals.h"
3889 +#include "fpm_events.h"
3890 +#include "fpm_process_ctl.h"
3891 +#include "fpm_cleanup.h"
3892 +#include "fpm_request.h"
3893 +#include "fpm_worker_pool.h"
3897 +static int fpm_state = FPM_PCTL_STATE_NORMAL;
3898 +static int fpm_signal_sent = 0;
3901 +static const char *fpm_state_names[] = {
3902 + [FPM_PCTL_STATE_NORMAL] = "normal",
3903 + [FPM_PCTL_STATE_RELOADING] = "reloading",
3904 + [FPM_PCTL_STATE_TERMINATING] = "terminating",
3905 + [FPM_PCTL_STATE_FINISHING] = "finishing"
3908 +static int saved_argc;
3909 +static char **saved_argv;
3911 +static void fpm_pctl_cleanup(int which, void *arg)
3915 + if (which != FPM_CLEANUP_PARENT_EXEC) {
3917 + for (i = 0; i < saved_argc; i++) {
3918 + free(saved_argv[i]);
3926 +static struct event pctl_event;
3928 +static void fpm_pctl_action(int fd, short which, void *arg)
3930 + evtimer_del(&pctl_event);
3932 + memset(&pctl_event, 0, sizeof(pctl_event));
3934 + fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_TIMEOUT);
3937 +static int fpm_pctl_timeout_set(int sec)
3939 + struct timeval tv = { .tv_sec = sec, .tv_usec = 0 };
3941 + if (evtimer_initialized(&pctl_event)) {
3942 + evtimer_del(&pctl_event);
3945 + evtimer_set(&pctl_event, &fpm_pctl_action, 0);
3947 + evtimer_add(&pctl_event, &tv);
3952 +static void fpm_pctl_exit()
3954 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "exiting, bye-bye!");
3956 + fpm_conf_unlink_pid();
3958 + fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT_MAIN);
3963 +#define optional_arg(c) (saved_argc > c ? ", \"" : ""), (saved_argc > c ? saved_argv[c] : ""), (saved_argc > c ? "\"" : "")
3965 +static void fpm_pctl_exec()
3968 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "reloading: execvp(\"%s\", {\"%s\""
3969 + "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
3970 + "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
3972 + saved_argv[0], saved_argv[0],
3985 + fpm_cleanups_run(FPM_CLEANUP_PARENT_EXEC);
3987 + execvp(saved_argv[0], saved_argv);
3989 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "execvp() failed");
3994 +static void fpm_pctl_action_last()
3996 + switch (fpm_state) {
3998 + case FPM_PCTL_STATE_RELOADING :
4003 + case FPM_PCTL_STATE_FINISHING :
4005 + case FPM_PCTL_STATE_TERMINATING :
4012 +int fpm_pctl_kill(pid_t pid, int how)
4017 + case FPM_PCTL_TERM :
4020 + case FPM_PCTL_STOP :
4023 + case FPM_PCTL_CONT :
4030 + return kill(pid, s);
4033 +static void fpm_pctl_kill_all(int signo)
4035 + struct fpm_worker_pool_s *wp;
4036 + int alive_children = 0;
4038 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
4039 + struct fpm_child_s *child;
4041 + for (child = wp->children; child; child = child->next) {
4043 + int res = kill(child->pid, signo);
4045 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "sending signal %d %s to child %d (pool %s)", signo,
4046 + fpm_signal_names[signo] ? fpm_signal_names[signo] : "",
4047 + (int) child->pid, child->wp->config->name);
4049 + if (res == 0) ++alive_children;
4053 + if (alive_children) {
4054 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "%d %s still alive", alive_children, alive_children == 1 ? "child is" : "children are");
4058 +static void fpm_pctl_action_next()
4062 + if (!fpm_globals.running_children) fpm_pctl_action_last();
4064 + if (fpm_signal_sent == 0) {
4065 + if (fpm_state == FPM_PCTL_STATE_TERMINATING) {
4071 + timeout = fpm_global_config.process_control_timeout;
4074 + if (fpm_signal_sent == SIGQUIT) {
4083 + fpm_pctl_kill_all(sig);
4085 + fpm_signal_sent = sig;
4087 + fpm_pctl_timeout_set(timeout);
4090 +void fpm_pctl(int new_state, int action)
4094 + case FPM_PCTL_ACTION_SET :
4096 + if (fpm_state == new_state) { /* already in progress - just ignore duplicate signal */
4100 + switch (fpm_state) { /* check which states can be overridden */
4102 + case FPM_PCTL_STATE_NORMAL :
4104 + /* 'normal' can be overridden by any other state */
4107 + case FPM_PCTL_STATE_RELOADING :
4109 + /* 'reloading' can be overridden by 'finishing' */
4110 + if (new_state == FPM_PCTL_STATE_FINISHING) break;
4112 + case FPM_PCTL_STATE_FINISHING :
4114 + /* 'reloading' and 'finishing' can be overridden by 'terminating' */
4115 + if (new_state == FPM_PCTL_STATE_TERMINATING) break;
4117 + case FPM_PCTL_STATE_TERMINATING :
4119 + /* nothing can override 'terminating' state */
4120 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "not switching to '%s' state, because already in '%s' state",
4121 + fpm_state_names[new_state], fpm_state_names[fpm_state]);
4126 + fpm_signal_sent = 0;
4127 + fpm_state = new_state;
4129 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "switching to '%s' state", fpm_state_names[fpm_state]);
4133 + case FPM_PCTL_ACTION_TIMEOUT :
4135 + fpm_pctl_action_next();
4139 + case FPM_PCTL_ACTION_LAST_CHILD_EXITED :
4141 + fpm_pctl_action_last();
4148 +int fpm_pctl_can_spawn_children()
4150 + return fpm_state == FPM_PCTL_STATE_NORMAL;
4153 +int fpm_pctl_child_exited()
4155 + if (fpm_state == FPM_PCTL_STATE_NORMAL) return 0;
4157 + if (!fpm_globals.running_children) {
4158 + fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_LAST_CHILD_EXITED);
4164 +int fpm_pctl_init_main()
4168 + saved_argc = fpm_globals.argc;
4170 + saved_argv = malloc(sizeof(char *) * (saved_argc + 1));
4172 + if (!saved_argv) {
4176 + for (i = 0; i < saved_argc; i++) {
4177 + saved_argv[i] = strdup(fpm_globals.argv[i]);
4179 + if (!saved_argv[i]) {
4184 + saved_argv[i] = 0;
4186 + if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_pctl_cleanup, 0)) {
4193 +static void fpm_pctl_check_request_timeout(struct timeval *now)
4195 + struct fpm_worker_pool_s *wp;
4197 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
4198 + int terminate_timeout = wp->config->request_terminate_timeout;
4199 + int slowlog_timeout = wp->config->request_slowlog_timeout;
4200 + struct fpm_child_s *child;
4202 + if (terminate_timeout || slowlog_timeout) {
4203 + for (child = wp->children; child; child = child->next) {
4204 + fpm_request_check_timed_out(child, now, terminate_timeout, slowlog_timeout);
4211 +void fpm_pctl_heartbeat(int fd, short which, void *arg)
4213 + static struct event heartbeat;
4214 + struct timeval tv = { .tv_sec = 0, .tv_usec = 130000 };
4215 + struct timeval now;
4217 + if (which == EV_TIMEOUT) {
4218 + evtimer_del(&heartbeat);
4219 + fpm_clock_get(&now);
4220 + fpm_pctl_check_request_timeout(&now);
4223 + evtimer_set(&heartbeat, &fpm_pctl_heartbeat, 0);
4225 + evtimer_add(&heartbeat, &tv);
4228 diff --git a/sapi/cgi/fpm/fpm_process_ctl.h b/sapi/cgi/fpm/fpm_process_ctl.h
4229 new file mode 100644
4231 +++ b/sapi/cgi/fpm/fpm_process_ctl.h
4235 + /* (c) 2007,2008 Andrei Nigmatulin */
4237 +#ifndef FPM_PROCESS_CTL_H
4238 +#define FPM_PROCESS_CTL_H 1
4240 +struct fpm_child_s;
4242 +void fpm_pctl(int new_state, int action);
4243 +int fpm_pctl_can_spawn_children();
4244 +int fpm_pctl_kill(pid_t pid, int how);
4245 +void fpm_pctl_heartbeat(int fd, short which, void *arg);
4246 +int fpm_pctl_child_exited();
4247 +int fpm_pctl_init_main();
4251 + FPM_PCTL_STATE_UNSPECIFIED,
4252 + FPM_PCTL_STATE_NORMAL,
4253 + FPM_PCTL_STATE_RELOADING,
4254 + FPM_PCTL_STATE_TERMINATING,
4255 + FPM_PCTL_STATE_FINISHING
4259 + FPM_PCTL_ACTION_SET,
4260 + FPM_PCTL_ACTION_TIMEOUT,
4261 + FPM_PCTL_ACTION_LAST_CHILD_EXITED
4272 diff --git a/sapi/cgi/fpm/fpm_request.c b/sapi/cgi/fpm/fpm_request.c
4273 new file mode 100644
4275 +++ b/sapi/cgi/fpm/fpm_request.c
4279 + /* (c) 2007,2008 Andrei Nigmatulin */
4281 +#include "fpm_config.h"
4283 +#include "fpm_php.h"
4284 +#include "fpm_str.h"
4285 +#include "fpm_clock.h"
4286 +#include "fpm_conf.h"
4287 +#include "fpm_trace.h"
4288 +#include "fpm_php_trace.h"
4289 +#include "fpm_process_ctl.h"
4290 +#include "fpm_children.h"
4291 +#include "fpm_shm_slots.h"
4292 +#include "fpm_request.h"
4296 +void fpm_request_accepting()
4298 + struct fpm_shm_slot_s *slot;
4300 + slot = fpm_shm_slots_acquire(0, 0);
4302 + slot->request_stage = FPM_REQUEST_ACCEPTING;
4304 + fpm_clock_get(&slot->tv);
4305 + memset(slot->request_method, 0, sizeof(slot->request_method));
4306 + slot->content_length = 0;
4307 + memset(slot->script_filename, 0, sizeof(slot->script_filename));
4309 + fpm_shm_slots_release(slot);
4312 +void fpm_request_reading_headers()
4314 + struct fpm_shm_slot_s *slot;
4316 + slot = fpm_shm_slots_acquire(0, 0);
4318 + slot->request_stage = FPM_REQUEST_READING_HEADERS;
4320 + fpm_clock_get(&slot->tv);
4321 + slot->accepted = slot->tv;
4323 + fpm_shm_slots_release(slot);
4326 +void fpm_request_info()
4329 + struct fpm_shm_slot_s *slot;
4330 + char *request_method = fpm_php_request_method(TSRMLS_C);
4331 + char *script_filename = fpm_php_script_filename(TSRMLS_C);
4333 + slot = fpm_shm_slots_acquire(0, 0);
4335 + slot->request_stage = FPM_REQUEST_INFO;
4337 + fpm_clock_get(&slot->tv);
4339 + if (request_method) {
4340 + cpystrn(slot->request_method, request_method, sizeof(slot->request_method));
4343 + slot->content_length = fpm_php_content_length(TSRMLS_C);
4345 + /* if cgi.fix_pathinfo is set to "1" and script cannot be found (404)
4346 + the sapi_globals.request_info.path_translated is set to NULL */
4347 + if (script_filename) {
4348 + cpystrn(slot->script_filename, script_filename, sizeof(slot->script_filename));
4351 + fpm_shm_slots_release(slot);
4354 +void fpm_request_executing()
4356 + struct fpm_shm_slot_s *slot;
4358 + slot = fpm_shm_slots_acquire(0, 0);
4360 + slot->request_stage = FPM_REQUEST_EXECUTING;
4362 + fpm_clock_get(&slot->tv);
4364 + fpm_shm_slots_release(slot);
4367 +void fpm_request_finished()
4369 + struct fpm_shm_slot_s *slot;
4371 + slot = fpm_shm_slots_acquire(0, 0);
4373 + slot->request_stage = FPM_REQUEST_FINISHED;
4375 + fpm_clock_get(&slot->tv);
4376 + memset(&slot->accepted, 0, sizeof(slot->accepted));
4378 + fpm_shm_slots_release(slot);
4381 +void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *now, int terminate_timeout, int slowlog_timeout)
4383 + struct fpm_shm_slot_s *slot;
4384 + struct fpm_shm_slot_s slot_c;
4386 + slot = fpm_shm_slot(child);
4388 + if (!fpm_shm_slots_acquire(slot, 1)) {
4394 + fpm_shm_slots_release(slot);
4397 + if (child->slow_logged.tv_sec) {
4398 + if (child->slow_logged.tv_sec != slot_c.accepted.tv_sec || child->slow_logged.tv_usec != slot_c.accepted.tv_usec) {
4399 + child->slow_logged.tv_sec = 0;
4400 + child->slow_logged.tv_usec = 0;
4405 + if (slot_c.request_stage > FPM_REQUEST_ACCEPTING && slot_c.request_stage < FPM_REQUEST_FINISHED) {
4406 + char purified_script_filename[sizeof(slot_c.script_filename)];
4407 + struct timeval tv;
4409 + timersub(now, &slot_c.accepted, &tv);
4412 + if (child->slow_logged.tv_sec == 0 && slowlog_timeout &&
4413 + slot_c.request_stage == FPM_REQUEST_EXECUTING && tv.tv_sec >= slowlog_timeout) {
4415 + str_purify_filename(purified_script_filename, slot_c.script_filename, sizeof(slot_c.script_filename));
4417 + child->slow_logged = slot_c.accepted;
4418 + child->tracer = fpm_php_trace;
4420 + fpm_trace_signal(child->pid);
4422 + zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d, script '%s' (pool %s) executing too slow (%d.%06d sec), logging",
4423 + (int) child->pid, purified_script_filename, child->wp->config->name, (int) tv.tv_sec, (int) tv.tv_usec);
4428 + if (terminate_timeout && tv.tv_sec >= terminate_timeout) {
4430 + str_purify_filename(purified_script_filename, slot_c.script_filename, sizeof(slot_c.script_filename));
4432 + fpm_pctl_kill(child->pid, FPM_PCTL_TERM);
4434 + zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d, script '%s' (pool %s) execution timed out (%d.%06d sec), terminating",
4435 + (int) child->pid, purified_script_filename, child->wp->config->name, (int) tv.tv_sec, (int) tv.tv_usec);
4441 diff --git a/sapi/cgi/fpm/fpm_request.h b/sapi/cgi/fpm/fpm_request.h
4442 new file mode 100644
4444 +++ b/sapi/cgi/fpm/fpm_request.h
4448 + /* (c) 2007,2008 Andrei Nigmatulin */
4450 +#ifndef FPM_REQUEST_H
4451 +#define FPM_REQUEST_H 1
4453 +void fpm_request_accepting(); /* hanging in accept() */
4454 +void fpm_request_reading_headers(); /* start reading fastcgi request from very first byte */
4455 +void fpm_request_info(); /* not a stage really but a point in the php code, where all request params have become known to sapi */
4456 +void fpm_request_executing(); /* the script is executing */
4457 +void fpm_request_finished(); /* request processed: script response have been sent to web server */
4459 +struct fpm_child_s;
4462 +void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *tv, int terminate_timeout, int slowlog_timeout);
4464 +enum fpm_request_stage_e {
4465 + FPM_REQUEST_ACCEPTING = 1,
4466 + FPM_REQUEST_READING_HEADERS,
4468 + FPM_REQUEST_EXECUTING,
4469 + FPM_REQUEST_FINISHED
4473 diff --git a/sapi/cgi/fpm/fpm_shm.c b/sapi/cgi/fpm/fpm_shm.c
4474 new file mode 100644
4476 +++ b/sapi/cgi/fpm/fpm_shm.c
4480 + /* (c) 2007,2008 Andrei Nigmatulin */
4482 +#include "fpm_config.h"
4484 +#include <unistd.h>
4485 +#include <sys/mman.h>
4486 +#include <stdlib.h>
4488 +#include "fpm_shm.h"
4492 +/* MAP_ANON is depricated, but not in macosx */
4493 +#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
4494 +#define MAP_ANONYMOUS MAP_ANON
4498 +struct fpm_shm_s *fpm_shm_alloc(size_t sz)
4500 + struct fpm_shm_s *shm;
4502 + shm = malloc(sizeof(*shm));
4508 + shm->mem = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
4511 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "mmap(MAP_ANONYMOUS | MAP_SHARED) failed");
4522 +static void fpm_shm_free(struct fpm_shm_s *shm, int do_unmap)
4525 + munmap(shm->mem, shm->sz);
4531 +void fpm_shm_free_list(struct fpm_shm_s *shm, void *mem)
4533 + struct fpm_shm_s *next;
4535 + for (; shm; shm = next) {
4538 + fpm_shm_free(shm, mem != shm->mem);
4542 +void *fpm_shm_alloc_chunk(struct fpm_shm_s **head, size_t sz, void **mem)
4544 + size_t pagesize = getpagesize();
4545 + static const size_t cache_line_size = 16;
4546 + size_t aligned_sz;
4547 + struct fpm_shm_s *shm;
4550 + sz = (sz + cache_line_size - 1) & -cache_line_size;
4554 + if (0 == shm || shm->sz - shm->used < sz) {
4555 + /* allocate one more shm segment */
4557 + aligned_sz = (sz + pagesize - 1) & -pagesize;
4559 + shm = fpm_shm_alloc(aligned_sz);
4565 + shm->next = *head;
4566 + if (shm->next) shm->next->prev = shm;
4572 + ret = (char *) shm->mem + shm->used;
4578 diff --git a/sapi/cgi/fpm/fpm_shm.h b/sapi/cgi/fpm/fpm_shm.h
4579 new file mode 100644
4581 +++ b/sapi/cgi/fpm/fpm_shm.h
4585 + /* (c) 2007,2008 Andrei Nigmatulin */
4588 +#define FPM_SHM_H 1
4593 + struct fpm_shm_s *prev, *next;
4599 +struct fpm_shm_s *fpm_shm_alloc(size_t sz);
4600 +void fpm_shm_free_list(struct fpm_shm_s *, void *);
4601 +void *fpm_shm_alloc_chunk(struct fpm_shm_s **head, size_t sz, void **mem);
4605 diff --git a/sapi/cgi/fpm/fpm_shm_slots.c b/sapi/cgi/fpm/fpm_shm_slots.c
4606 new file mode 100644
4608 +++ b/sapi/cgi/fpm/fpm_shm_slots.c
4612 + /* (c) 2007,2008 Andrei Nigmatulin */
4614 +#include "fpm_config.h"
4616 +#include "fpm_atomic.h"
4617 +#include "fpm_worker_pool.h"
4618 +#include "fpm_children.h"
4619 +#include "fpm_shm.h"
4620 +#include "fpm_shm_slots.h"
4623 +static void *shm_mem;
4624 +static struct fpm_shm_slot_s *shm_slot;
4626 +int fpm_shm_slots_prepare_slot(struct fpm_child_s *child)
4628 + struct fpm_worker_pool_s *wp = child->wp;
4629 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4631 + child->shm_slot_i = wp->slots_used.used;
4633 + shm_slot_ptr = fpm_array_push(&wp->slots_used);
4635 + if (0 == shm_slot_ptr) {
4639 + if (0 == wp->slots_free.used) {
4640 + shm_slot_ptr->shm_slot = fpm_shm_alloc_chunk(&wp->shm_list, sizeof(struct fpm_shm_slot_s), &shm_slot_ptr->mem);
4642 + if (!shm_slot_ptr->shm_slot) {
4647 + *shm_slot_ptr = *(struct fpm_shm_slot_ptr_s *) fpm_array_item_last(&wp->slots_free);
4649 + --wp->slots_free.used;
4652 + memset(shm_slot_ptr->shm_slot, 0, sizeof(struct fpm_shm_slot_s));
4654 + shm_slot_ptr->child = child;
4659 +void fpm_shm_slots_discard_slot(struct fpm_child_s *child)
4661 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4662 + struct fpm_worker_pool_s *wp = child->wp;
4665 + shm_slot_ptr = fpm_array_push(&wp->slots_free);
4667 + if (shm_slot_ptr) {
4669 + struct fpm_shm_slot_ptr_s *shm_slot_ptr_used;
4671 + shm_slot_ptr_used = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4673 + *shm_slot_ptr = *shm_slot_ptr_used;
4675 + shm_slot_ptr->child = 0;
4679 + n = fpm_array_item_remove(&wp->slots_used, child->shm_slot_i);
4682 + shm_slot_ptr = fpm_array_item(&wp->slots_used, n);
4684 + shm_slot_ptr->child->shm_slot_i = n;
4688 +void fpm_shm_slots_child_use_slot(struct fpm_child_s *child)
4690 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4691 + struct fpm_worker_pool_s *wp = child->wp;
4693 + shm_slot_ptr = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4695 + shm_slot = shm_slot_ptr->shm_slot;
4696 + shm_mem = shm_slot_ptr->mem;
4699 +void fpm_shm_slots_parent_use_slot(struct fpm_child_s *child)
4701 + /* nothing to do */
4704 +void *fpm_shm_slots_mem()
4709 +struct fpm_shm_slot_s *fpm_shm_slot(struct fpm_child_s *child)
4711 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4712 + struct fpm_worker_pool_s *wp = child->wp;
4714 + shm_slot_ptr = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4716 + return shm_slot_ptr->shm_slot;
4719 +struct fpm_shm_slot_s *fpm_shm_slots_acquire(struct fpm_shm_slot_s *s, int nohang)
4725 + if (0 > fpm_spinlock(&s->lock, nohang)) {
4732 +void fpm_shm_slots_release(struct fpm_shm_slot_s *s)
4737 diff --git a/sapi/cgi/fpm/fpm_shm_slots.h b/sapi/cgi/fpm/fpm_shm_slots.h
4738 new file mode 100644
4740 +++ b/sapi/cgi/fpm/fpm_shm_slots.h
4744 + /* (c) 2007,2008 Andrei Nigmatulin */
4746 +#ifndef FPM_SHM_SLOTS_H
4747 +#define FPM_SHM_SLOTS_H 1
4749 +#include "fpm_atomic.h"
4750 +#include "fpm_worker_pool.h"
4751 +#include "fpm_request.h"
4753 +struct fpm_child_s;
4755 +struct fpm_shm_slot_s {
4760 + enum fpm_request_stage_e request_stage;
4761 + struct timeval accepted;
4762 + struct timeval tv;
4763 + char request_method[16];
4764 + size_t content_length; /* used with POST only */
4765 + char script_filename[256];
4768 +struct fpm_shm_slot_ptr_s {
4770 + struct fpm_shm_slot_s *shm_slot;
4771 + struct fpm_child_s *child;
4774 +int fpm_shm_slots_prepare_slot(struct fpm_child_s *child);
4775 +void fpm_shm_slots_discard_slot(struct fpm_child_s *child);
4776 +void fpm_shm_slots_child_use_slot(struct fpm_child_s *child);
4777 +void fpm_shm_slots_parent_use_slot(struct fpm_child_s *child);
4778 +void *fpm_shm_slots_mem();
4779 +struct fpm_shm_slot_s *fpm_shm_slot(struct fpm_child_s *child);
4780 +struct fpm_shm_slot_s *fpm_shm_slots_acquire(struct fpm_shm_slot_s *, int nohang);
4781 +void fpm_shm_slots_release(struct fpm_shm_slot_s *);
4785 diff --git a/sapi/cgi/fpm/fpm_signals.c b/sapi/cgi/fpm/fpm_signals.c
4786 new file mode 100644
4788 +++ b/sapi/cgi/fpm/fpm_signals.c
4792 + /* (c) 2007,2008 Andrei Nigmatulin */
4794 +#include "fpm_config.h"
4796 +#include <signal.h>
4798 +#include <sys/types.h>
4799 +#include <sys/socket.h>
4800 +#include <stdlib.h>
4801 +#include <string.h>
4803 +#include <unistd.h>
4807 +#include "fpm_signals.h"
4808 +#include "fpm_sockets.h"
4809 +#include "fpm_php.h"
4814 +const char *fpm_signal_names[NSIG + 1] = {
4816 + [SIGHUP] = "SIGHUP",
4819 + [SIGINT] = "SIGINT",
4822 + [SIGQUIT] = "SIGQUIT",
4825 + [SIGILL] = "SIGILL",
4828 + [SIGTRAP] = "SIGTRAP",
4831 + [SIGABRT] = "SIGABRT",
4834 + [SIGEMT] = "SIGEMT",
4837 + [SIGBUS] = "SIGBUS",
4840 + [SIGFPE] = "SIGFPE",
4843 + [SIGKILL] = "SIGKILL",
4846 + [SIGUSR1] = "SIGUSR1",
4849 + [SIGSEGV] = "SIGSEGV",
4852 + [SIGUSR2] = "SIGUSR2",
4855 + [SIGPIPE] = "SIGPIPE",
4858 + [SIGALRM] = "SIGALRM",
4861 + [SIGTERM] = "SIGTERM",
4864 + [SIGCHLD] = "SIGCHLD",
4867 + [SIGCONT] = "SIGCONT",
4870 + [SIGSTOP] = "SIGSTOP",
4873 + [SIGTSTP] = "SIGTSTP",
4876 + [SIGTTIN] = "SIGTTIN",
4879 + [SIGTTOU] = "SIGTTOU",
4882 + [SIGURG] = "SIGURG",
4885 + [SIGXCPU] = "SIGXCPU",
4888 + [SIGXFSZ] = "SIGXFSZ",
4891 + [SIGVTALRM] = "SIGVTALRM",
4894 + [SIGPROF] = "SIGPROF",
4897 + [SIGWINCH] = "SIGWINCH",
4900 + [SIGINFO] = "SIGINFO",
4903 + [SIGIO] = "SIGIO",
4906 + [SIGPWR] = "SIGPWR",
4909 + [SIGSYS] = "SIGSYS",
4912 + [SIGWAITING] = "SIGWAITING",
4915 + [SIGLWP] = "SIGLWP",
4918 + [SIGFREEZE] = "SIGFREEZE",
4921 + [SIGTHAW] = "SIGTHAW",
4924 + [SIGCANCEL] = "SIGCANCEL",
4927 + [SIGLOST] = "SIGLOST",
4931 +static void sig_soft_quit(int signo)
4933 + int saved_errno = errno;
4935 + /* closing fastcgi listening socket will force fcgi_accept() exit immediately */
4937 + socket(AF_UNIX, SOCK_STREAM, 0);
4939 + fpm_php_soft_quit();
4941 + errno = saved_errno;
4944 +static void sig_handler(int signo)
4946 + static const char sig_chars[NSIG + 1] = {
4957 + if (fpm_globals.parent_pid != getpid()) {
4958 + /* prevent a signal race condition when child process
4959 + have not set up it's own signal handler yet */
4963 + saved_errno = errno;
4965 + s = sig_chars[signo];
4967 + write(sp[1], &s, sizeof(s));
4969 + errno = saved_errno;
4972 +int fpm_signals_init_main()
4974 + struct sigaction act;
4976 + if (0 > socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) {
4977 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "socketpair() failed");
4981 + if (0 > fd_set_blocked(sp[0], 0) || 0 > fd_set_blocked(sp[1], 0)) {
4982 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fd_set_blocked() failed");
4986 + if (0 > fcntl(sp[0], F_SETFD, FD_CLOEXEC) || 0 > fcntl(sp[1], F_SETFD, FD_CLOEXEC)) {
4987 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fcntl(F_SETFD, FD_CLOEXEC) failed");
4991 + memset(&act, 0, sizeof(act));
4992 + act.sa_handler = sig_handler;
4993 + sigfillset(&act.sa_mask);
4995 + if (0 > sigaction(SIGTERM, &act, 0) ||
4996 + 0 > sigaction(SIGINT, &act, 0) ||
4997 + 0 > sigaction(SIGUSR1, &act, 0) ||
4998 + 0 > sigaction(SIGUSR2, &act, 0) ||
4999 + 0 > sigaction(SIGCHLD, &act, 0) ||
5000 + 0 > sigaction(SIGQUIT, &act, 0)) {
5002 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "sigaction() failed");
5009 +int fpm_signals_init_child()
5011 + struct sigaction act, act_dfl;
5013 + memset(&act, 0, sizeof(act));
5014 + memset(&act_dfl, 0, sizeof(act_dfl));
5016 + act.sa_handler = &sig_soft_quit;
5017 + act.sa_flags |= SA_RESTART;
5019 + act_dfl.sa_handler = SIG_DFL;
5024 + if (0 > sigaction(SIGTERM, &act_dfl, 0) ||
5025 + 0 > sigaction(SIGINT, &act_dfl, 0) ||
5026 + 0 > sigaction(SIGUSR1, &act_dfl, 0) ||
5027 + 0 > sigaction(SIGUSR2, &act_dfl, 0) ||
5028 + 0 > sigaction(SIGCHLD, &act_dfl, 0) ||
5029 + 0 > sigaction(SIGQUIT, &act, 0)) {
5031 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "sigaction() failed");
5038 +int fpm_signals_get_fd()
5042 diff --git a/sapi/cgi/fpm/fpm_signals.h b/sapi/cgi/fpm/fpm_signals.h
5043 new file mode 100644
5045 +++ b/sapi/cgi/fpm/fpm_signals.h
5049 + /* (c) 2007,2008 Andrei Nigmatulin */
5051 +#ifndef FPM_SIGNALS_H
5052 +#define FPM_SIGNALS_H 1
5054 +#include <signal.h>
5056 +int fpm_signals_init_main();
5057 +int fpm_signals_init_child();
5058 +int fpm_signals_get_fd();
5060 +extern const char *fpm_signal_names[NSIG + 1];
5063 diff --git a/sapi/cgi/fpm/fpm_sockets.c b/sapi/cgi/fpm/fpm_sockets.c
5064 new file mode 100644
5066 +++ b/sapi/cgi/fpm/fpm_sockets.c
5070 + /* (c) 2007,2008 Andrei Nigmatulin */
5072 +#include "fpm_config.h"
5074 +#ifdef HAVE_ALLOCA_H
5075 +#include <alloca.h>
5077 +#include <sys/types.h>
5078 +#include <sys/stat.h> /* for chmod(2) */
5079 +#include <sys/socket.h>
5080 +#include <netinet/in.h>
5081 +#include <arpa/inet.h>
5082 +#include <sys/un.h>
5085 +#include <stdlib.h>
5086 +#include <string.h>
5088 +#include <unistd.h>
5091 +#include "fpm_arrays.h"
5092 +#include "fpm_sockets.h"
5093 +#include "fpm_worker_pool.h"
5094 +#include "fpm_unix.h"
5095 +#include "fpm_str.h"
5096 +#include "fpm_env.h"
5097 +#include "fpm_cleanup.h"
5099 +struct listening_socket_s {
5106 +static struct fpm_array_s sockets_list;
5108 +static int fpm_sockets_resolve_af_inet(char *node, char *service, struct sockaddr_in *addr)
5110 + struct addrinfo *res;
5111 + struct addrinfo hints;
5114 + memset(&hints, 0, sizeof(hints));
5116 + hints.ai_family = AF_INET;
5118 + ret = getaddrinfo(node, service, &hints, &res);
5121 + zlog(ZLOG_STUFF, ZLOG_ERROR, "can't resolve hostname '%s%s%s': getaddrinfo said: %s%s%s\n",
5122 + node, service ? ":" : "", service ? service : "",
5123 + gai_strerror(ret), ret == EAI_SYSTEM ? ", system error: " : "", ret == EAI_SYSTEM ? strerror(errno) : "");
5127 + *addr = *(struct sockaddr_in *) res->ai_addr;
5129 + freeaddrinfo(res);
5134 +enum { FPM_GET_USE_SOCKET = 1, FPM_STORE_SOCKET = 2, FPM_STORE_USE_SOCKET = 3 };
5136 +static void fpm_sockets_cleanup(int which, void *arg)
5139 + char *env_value = 0;
5141 + struct listening_socket_s *ls = sockets_list.data;
5143 + for (i = 0; i < sockets_list.used; i++, ls++) {
5145 + if (which != FPM_CLEANUP_PARENT_EXEC) {
5150 + else { /* on PARENT EXEC we want socket fds to be inherited through environment variable */
5152 + sprintf(fd, "%d", ls->sock);
5153 + env_value = realloc(env_value, p + (p ? 1 : 0) + strlen(ls->key) + 1 + strlen(fd) + 1);
5154 + p += sprintf(env_value + p, "%s%s=%s", p ? "," : "", ls->key, fd);
5157 + if (which == FPM_CLEANUP_PARENT_EXIT_MAIN) {
5159 + if (ls->type == FPM_AF_UNIX) {
5169 + setenv("FPM_SOCKETS", env_value, 1);
5173 + fpm_array_free(&sockets_list);
5176 +static int fpm_sockets_hash_op(int sock, struct sockaddr *sa, char *key, int type, int op)
5179 + if (key == NULL) {
5183 + case FPM_AF_INET : {
5184 + struct sockaddr_in *sa_in = (struct sockaddr_in *) sa;
5186 + key = alloca(sizeof("xxx.xxx.xxx.xxx:ppppp"));
5188 + sprintf(key, "%u.%u.%u.%u:%u", IPQUAD(&sa_in->sin_addr), (unsigned int) ntohs(sa_in->sin_port));
5193 + case FPM_AF_UNIX : {
5194 + struct sockaddr_un *sa_un = (struct sockaddr_un *) sa;
5196 + key = alloca(strlen(sa_un->sun_path) + 1);
5198 + strcpy(key, sa_un->sun_path);
5212 + case FPM_GET_USE_SOCKET :
5216 + struct listening_socket_s *ls = sockets_list.data;
5218 + for (i = 0; i < sockets_list.used; i++, ls++) {
5220 + if (!strcmp(ls->key, key)) {
5229 + case FPM_STORE_SOCKET : /* inherited socket */
5230 + case FPM_STORE_USE_SOCKET : /* just created */
5233 + struct listening_socket_s *ls;
5235 + ls = fpm_array_push(&sockets_list);
5241 + if (op == FPM_STORE_SOCKET) {
5249 + ls->key = strdup(key);
5260 +static int fpm_sockets_new_listening_socket(struct fpm_worker_pool_s *wp, struct sockaddr *sa, int socklen)
5265 + mode_t saved_umask;
5267 + /* we have custom backlog value */
5268 + if (wp->config->listen_options) {
5269 + backlog = wp->config->listen_options->backlog;
5272 + sock = socket(sa->sa_family, SOCK_STREAM, 0);
5275 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "socket() failed");
5279 + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
5281 + if (wp->listen_address_domain == FPM_AF_UNIX) {
5282 + unlink( ((struct sockaddr_un *) sa)->sun_path);
5285 + saved_umask = umask(0777 ^ wp->socket_mode);
5287 + if (0 > bind(sock, sa, socklen)) {
5288 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "bind() for address '%s' failed", wp->config->listen_address);
5292 + if (wp->listen_address_domain == FPM_AF_UNIX) {
5294 + char *path = ((struct sockaddr_un *) sa)->sun_path;
5296 + if (wp->socket_uid != -1 || wp->socket_gid != -1) {
5298 + if (0 > chown(path, wp->socket_uid, wp->socket_gid)) {
5299 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chown() for address '%s' failed", wp->config->listen_address);
5307 + umask(saved_umask);
5309 + if (0 > listen(sock, backlog)) {
5310 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "listen() for address '%s' failed", wp->config->listen_address);
5317 +static int fpm_sockets_get_listening_socket(struct fpm_worker_pool_s *wp, struct sockaddr *sa, int socklen)
5321 + sock = fpm_sockets_hash_op(0, sa, 0, wp->listen_address_domain, FPM_GET_USE_SOCKET);
5323 + if (sock >= 0) return sock;
5325 + sock = fpm_sockets_new_listening_socket(wp, sa, socklen);
5327 + fpm_sockets_hash_op(sock, sa, 0, wp->listen_address_domain, FPM_STORE_USE_SOCKET);
5332 +enum fpm_address_domain fpm_sockets_domain_from_address(char *address)
5334 + if (strchr(address, ':')) return FPM_AF_INET;
5336 + if (strlen(address) == strspn(address, "0123456789")) return FPM_AF_INET;
5338 + return FPM_AF_UNIX;
5341 +static int fpm_socket_af_inet_listening_socket(struct fpm_worker_pool_s *wp)
5343 + struct sockaddr_in sa_in;
5344 + char *dup_address = strdup(wp->config->listen_address);
5345 + char *port_str = strchr(dup_address, ':');
5346 + char *addr = NULL;
5349 + if (port_str) { /* this is host:port pair */
5350 + *port_str++ = '\0';
5351 + port = atoi(port_str);
5352 + addr = dup_address;
5354 + else if (strlen(dup_address) == strspn(dup_address, "0123456789")) { /* this is port */
5355 + port = atoi(dup_address);
5356 + port_str = dup_address;
5360 + zlog(ZLOG_STUFF, ZLOG_ERROR, "invalid port value '%s'", port_str);
5364 + memset(&sa_in, 0, sizeof(sa_in));
5368 + sa_in.sin_addr.s_addr = inet_addr(addr);
5370 + if (sa_in.sin_addr.s_addr == INADDR_NONE) { /* do resolve */
5371 + if (0 > fpm_sockets_resolve_af_inet(addr, NULL, &sa_in)) {
5374 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "address '%s' resolved as %u.%u.%u.%u", addr, IPQUAD(&sa_in.sin_addr));
5379 + sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
5383 + sa_in.sin_family = AF_INET;
5384 + sa_in.sin_port = htons(port);
5386 + free(dup_address);
5388 + return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_in, sizeof(struct sockaddr_in));
5391 +static int fpm_socket_af_unix_listening_socket(struct fpm_worker_pool_s *wp)
5393 + struct sockaddr_un sa_un;
5395 + memset(&sa_un, 0, sizeof(sa_un));
5397 + cpystrn(sa_un.sun_path, wp->config->listen_address, sizeof(sa_un.sun_path));
5398 + sa_un.sun_family = AF_UNIX;
5400 + return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_un, sizeof(struct sockaddr_un));
5403 +int fpm_sockets_init_main()
5406 + struct fpm_worker_pool_s *wp;
5407 + char *inherited = getenv("FPM_SOCKETS");
5408 + struct listening_socket_s *ls;
5410 + if (0 == fpm_array_init(&sockets_list, sizeof(struct listening_socket_s), 10)) {
5414 + /* import inherited sockets */
5415 + while (inherited && *inherited) {
5416 + char *comma = strchr(inherited, ',');
5420 + if (comma) *comma = '\0';
5422 + eq = strchr(inherited, '=');
5427 + fd_no = atoi(eq + 1);
5429 + type = fpm_sockets_domain_from_address(inherited);
5431 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "using inherited socket fd=%d, \"%s\"", fd_no, inherited);
5433 + fpm_sockets_hash_op(fd_no, 0, inherited, type, FPM_STORE_SOCKET);
5436 + if (comma) inherited = comma + 1;
5437 + else inherited = 0;
5440 + /* create all required sockets */
5441 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
5443 + if (!wp->is_template) {
5445 + switch (wp->listen_address_domain) {
5447 + case FPM_AF_INET :
5449 + wp->listening_socket = fpm_socket_af_inet_listening_socket(wp);
5452 + case FPM_AF_UNIX :
5454 + if (0 > fpm_unix_resolve_socket_premissions(wp)) {
5458 + wp->listening_socket = fpm_socket_af_unix_listening_socket(wp);
5463 + if (wp->listening_socket == -1) {
5470 + /* close unused sockets that was inherited */
5471 + ls = sockets_list.data;
5473 + for (i = 0; i < sockets_list.used; ) {
5475 + if (ls->refcount == 0) {
5477 + if (ls->type == FPM_AF_UNIX) {
5481 + fpm_array_item_remove(&sockets_list, i);
5489 + if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_sockets_cleanup, 0)) {
5495 diff --git a/sapi/cgi/fpm/fpm_sockets.h b/sapi/cgi/fpm/fpm_sockets.h
5496 new file mode 100644
5498 +++ b/sapi/cgi/fpm/fpm_sockets.h
5502 + /* (c) 2007,2008 Andrei Nigmatulin */
5505 +#define FPM_MISC_H 1
5507 +#include <unistd.h>
5510 +#include "fpm_worker_pool.h"
5512 +enum fpm_address_domain fpm_sockets_domain_from_address(char *addr);
5513 +int fpm_sockets_init_main();
5516 +static inline int fd_set_blocked(int fd, int blocked)
5518 + int flags = fcntl(fd, F_GETFL);
5520 + if (flags < 0) return -1;
5523 + flags &= ~O_NONBLOCK;
5525 + flags |= O_NONBLOCK;
5527 + return fcntl(fd, F_SETFL, flags);
5530 +#define IPQUAD(sin_addr) \
5531 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[0], \
5532 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[1], \
5533 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[2], \
5534 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[3]
5537 diff --git a/sapi/cgi/fpm/fpm_stdio.c b/sapi/cgi/fpm/fpm_stdio.c
5538 new file mode 100644
5540 +++ b/sapi/cgi/fpm/fpm_stdio.c
5544 + /* (c) 2007,2008 Andrei Nigmatulin */
5546 +#include "fpm_config.h"
5548 +#include <sys/types.h>
5549 +#include <sys/stat.h>
5550 +#include <string.h>
5552 +#include <unistd.h>
5556 +#include "fpm_children.h"
5557 +#include "fpm_events.h"
5558 +#include "fpm_sockets.h"
5559 +#include "fpm_stdio.h"
5562 +static int fd_stdout[2];
5563 +static int fd_stderr[2];
5565 +int fpm_stdio_init_main()
5567 + int fd = open("/dev/null", O_RDWR);
5570 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"/dev/null\") failed");
5574 + if (0 > dup2(fd, STDIN_FILENO) || 0 > dup2(fd, STDOUT_FILENO)) {
5575 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
5584 +int fpm_stdio_init_final()
5586 + if (fpm_global_config.daemonize) {
5588 + if (fpm_globals.error_log_fd != STDERR_FILENO) {
5589 + /* there might be messages to stderr from libevent, we need to log them all */
5590 + if (0 > dup2(fpm_globals.error_log_fd, STDERR_FILENO)) {
5591 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
5596 + zlog_set_level(fpm_globals.log_level);
5598 + zlog_set_fd(fpm_globals.error_log_fd);
5604 +int fpm_stdio_init_child(struct fpm_worker_pool_s *wp)
5606 + close(fpm_globals.error_log_fd);
5607 + fpm_globals.error_log_fd = -1;
5610 + if (wp->listening_socket != STDIN_FILENO) {
5611 + if (0 > dup2(wp->listening_socket, STDIN_FILENO)) {
5612 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
5620 +static void fpm_stdio_child_said(int fd, short which, void *arg)
5622 + static const int max_buf_size = 1024;
5623 + char buf[max_buf_size];
5624 + struct fpm_child_s *child = arg;
5625 + int is_stdout = fd == child->fd_stdout;
5626 + struct event *ev = is_stdout ? &child->ev_stdout : &child->ev_stderr;
5627 + int fifo_in = 1, fifo_out = 1;
5628 + int is_last_message = 0;
5633 + zlog(ZLOG_STUFF, ZLOG_DEBUG, "child %d said %s", (int) child->pid, is_stdout ? "stdout" : "stderr");
5636 + while (fifo_in || fifo_out) {
5640 + res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf);
5642 + if (res <= 0) { /* no data */
5645 + if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
5646 + /* just no more data ready */
5648 + else { /* error or pipe is closed */
5650 + if (res < 0) { /* error */
5651 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
5654 + fpm_event_del(ev);
5655 + is_last_message = 1;
5658 + close(child->fd_stdout);
5659 + child->fd_stdout = -1;
5662 + close(child->fd_stderr);
5663 + child->fd_stderr = -1;
5667 + if (in_buf == 0 && !fpm_globals.is_child) {
5668 + zlog(ZLOG_STUFF, ZLOG_DEBUG, "child %d (pool %s) %s pipe is closed", (int) child->pid,
5669 + child->wp->config->name, is_stdout ? "stdout" : "stderr");
5680 + if (in_buf == 0) {
5685 + int should_print = 0;
5686 + buf[in_buf] = '\0';
5688 + /* FIXME: there might be binary data */
5690 + /* we should print if no more space in the buffer */
5691 + if (in_buf == max_buf_size - 1) {
5695 + /* we should print if no more data to come */
5700 + nl = strchr(buf, '\n');
5702 + if (nl || should_print) {
5708 + zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d (pool %s) said into %s: \"%s\"%s", (int) child->pid,
5709 + child->wp->config->name, is_stdout ? "stdout" : "stderr", buf, is_last_message ? ", pipe is closed" : "");
5712 + int out_buf = 1 + nl - buf;
5713 + memmove(buf, buf + out_buf, in_buf - out_buf);
5714 + in_buf -= out_buf;
5726 +int fpm_stdio_prepare_pipes(struct fpm_child_s *child)
5728 + if (0 == child->wp->config->catch_workers_output) { /* not required */
5732 + if (0 > pipe(fd_stdout)) {
5733 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
5737 + if (0 > pipe(fd_stderr)) {
5738 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
5739 + close(fd_stdout[0]); close(fd_stdout[1]);
5743 + if (0 > fd_set_blocked(fd_stdout[0], 0) || 0 > fd_set_blocked(fd_stderr[0], 0)) {
5744 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fd_set_blocked() failed");
5745 + close(fd_stdout[0]); close(fd_stdout[1]);
5746 + close(fd_stderr[0]); close(fd_stderr[1]);
5753 +int fpm_stdio_parent_use_pipes(struct fpm_child_s *child)
5755 + if (0 == child->wp->config->catch_workers_output) { /* not required */
5759 + close(fd_stdout[1]);
5760 + close(fd_stderr[1]);
5762 + child->fd_stdout = fd_stdout[0];
5763 + child->fd_stderr = fd_stderr[0];
5765 + fpm_event_add(child->fd_stdout, &child->ev_stdout, fpm_stdio_child_said, child);
5766 + fpm_event_add(child->fd_stderr, &child->ev_stderr, fpm_stdio_child_said, child);
5771 +int fpm_stdio_discard_pipes(struct fpm_child_s *child)
5773 + if (0 == child->wp->config->catch_workers_output) { /* not required */
5777 + close(fd_stdout[1]);
5778 + close(fd_stderr[1]);
5780 + close(fd_stdout[0]);
5781 + close(fd_stderr[0]);
5786 +void fpm_stdio_child_use_pipes(struct fpm_child_s *child)
5788 + if (child->wp->config->catch_workers_output) {
5789 + dup2(fd_stdout[1], STDOUT_FILENO);
5790 + dup2(fd_stderr[1], STDERR_FILENO);
5791 + close(fd_stdout[0]); close(fd_stdout[1]);
5792 + close(fd_stderr[0]); close(fd_stderr[1]);
5795 + /* stdout of parent is always /dev/null */
5796 + dup2(STDOUT_FILENO, STDERR_FILENO);
5800 +int fpm_stdio_open_error_log(int reopen)
5804 + fd = open(fpm_global_config.error_log, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
5807 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"%s\") failed", fpm_global_config.error_log);
5812 + if (fpm_global_config.daemonize) {
5813 + dup2(fd, STDERR_FILENO);
5816 + dup2(fd, fpm_globals.error_log_fd);
5818 + fd = fpm_globals.error_log_fd; /* for FD_CLOSEXEC to work */
5821 + fpm_globals.error_log_fd = fd;
5824 + fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
5828 diff --git a/sapi/cgi/fpm/fpm_stdio.h b/sapi/cgi/fpm/fpm_stdio.h
5829 new file mode 100644
5831 +++ b/sapi/cgi/fpm/fpm_stdio.h
5835 + /* (c) 2007,2008 Andrei Nigmatulin */
5837 +#ifndef FPM_STDIO_H
5838 +#define FPM_STDIO_H 1
5840 +#include "fpm_worker_pool.h"
5842 +int fpm_stdio_init_main();
5843 +int fpm_stdio_init_final();
5844 +int fpm_stdio_init_child(struct fpm_worker_pool_s *wp);
5845 +int fpm_stdio_prepare_pipes(struct fpm_child_s *child);
5846 +void fpm_stdio_child_use_pipes(struct fpm_child_s *child);
5847 +int fpm_stdio_parent_use_pipes(struct fpm_child_s *child);
5848 +int fpm_stdio_discard_pipes(struct fpm_child_s *child);
5849 +int fpm_stdio_open_error_log(int reopen);
5853 diff --git a/sapi/cgi/fpm/fpm_str.h b/sapi/cgi/fpm/fpm_str.h
5854 new file mode 100644
5856 +++ b/sapi/cgi/fpm/fpm_str.h
5860 + /* (c) 2007,2008 Andrei Nigmatulin */
5863 +#define FPM_STR_H 1
5865 +static inline char *cpystrn(char *dst, const char *src, size_t dst_size)
5869 + if (!dst_size) return dst;
5872 + end = dst + dst_size - 1;
5874 + for (; d < end; ++d, ++src) {
5875 + if (!(*d = *src)) {
5885 +static inline char *str_purify_filename(char *dst, char *src, size_t size)
5890 + end = dst + size - 1;
5892 + for (; d < end && *src; ++d, ++src) {
5893 + if (* (unsigned char *) src < ' ' || * (unsigned char *) src > '\x7f') {
5907 diff --git a/sapi/cgi/fpm/fpm_trace.c b/sapi/cgi/fpm/fpm_trace.c
5908 new file mode 100644
5910 +++ b/sapi/cgi/fpm/fpm_trace.c
5914 + /* (c) 2007,2008 Andrei Nigmatulin */
5916 +#include "fpm_config.h"
5918 +#include <sys/types.h>
5920 +#include "fpm_trace.h"
5922 +int fpm_trace_get_strz(char *buf, size_t sz, long addr)
5926 + char *lc = (char *) &l;
5928 + if (0 > fpm_trace_get_long(addr, &l)) {
5932 + i = l % SIZEOF_LONG;
5936 + for (addr = l; ; addr += SIZEOF_LONG) {
5938 + if (0 > fpm_trace_get_long(addr, &l)) {
5942 + for ( ; i < SIZEOF_LONG; i++) {
5945 + if (sz && lc[i]) {
5958 diff --git a/sapi/cgi/fpm/fpm_trace.h b/sapi/cgi/fpm/fpm_trace.h
5959 new file mode 100644
5961 +++ b/sapi/cgi/fpm/fpm_trace.h
5965 + /* (c) 2007,2008 Andrei Nigmatulin */
5967 +#ifndef FPM_TRACE_H
5968 +#define FPM_TRACE_H 1
5970 +#include <unistd.h>
5972 +int fpm_trace_signal(pid_t pid);
5973 +int fpm_trace_ready(pid_t pid);
5974 +int fpm_trace_close(pid_t pid);
5975 +int fpm_trace_get_long(long addr, long *data);
5976 +int fpm_trace_get_strz(char *buf, size_t sz, long addr);
5980 diff --git a/sapi/cgi/fpm/fpm_trace_mach.c b/sapi/cgi/fpm/fpm_trace_mach.c
5981 new file mode 100644
5983 +++ b/sapi/cgi/fpm/fpm_trace_mach.c
5987 + /* (c) 2007,2008 Andrei Nigmatulin */
5989 +#include "fpm_config.h"
5991 +#include <mach/mach.h>
5992 +#include <mach/mach_vm.h>
5994 +#include <unistd.h>
5996 +#include "fpm_trace.h"
5997 +#include "fpm_process_ctl.h"
5998 +#include "fpm_unix.h"
6002 +static mach_port_name_t target;
6003 +static vm_offset_t target_page_base;
6004 +static vm_offset_t local_page;
6005 +static mach_msg_type_number_t local_size;
6007 +static void fpm_mach_vm_deallocate()
6010 + mach_vm_deallocate(mach_task_self(), local_page, local_size);
6011 + target_page_base = 0;
6017 +static int fpm_mach_vm_read_page(vm_offset_t page)
6021 + kr = mach_vm_read(target, page, fpm_pagesize, &local_page, &local_size);
6023 + if (kr != KERN_SUCCESS) {
6024 + zlog(ZLOG_STUFF, ZLOG_ERROR, "mach_vm_read() failed: %s (%d)", mach_error_string(kr), kr);
6031 +int fpm_trace_signal(pid_t pid)
6033 + if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
6034 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "kill(SIGSTOP) failed");
6041 +int fpm_trace_ready(pid_t pid)
6045 + kr = task_for_pid(mach_task_self(), pid, &target);
6047 + if (kr != KERN_SUCCESS) {
6050 + if (kr == KERN_FAILURE) {
6051 + msg = " It seems that master process does not have enough privileges to trace processes.";
6054 + zlog(ZLOG_STUFF, ZLOG_ERROR, "task_for_pid() failed: %s (%d)%s", mach_error_string(kr), kr, msg);
6061 +int fpm_trace_close(pid_t pid)
6063 + fpm_mach_vm_deallocate();
6070 +int fpm_trace_get_long(long addr, long *data)
6072 + size_t offset = ((uintptr_t) (addr) % fpm_pagesize);
6073 + vm_offset_t base = (uintptr_t) (addr) - offset;
6075 + if (base != target_page_base) {
6076 + fpm_mach_vm_deallocate();
6077 + if (0 > fpm_mach_vm_read_page(base)) {
6082 + *data = * (long *) (local_page + offset);
6087 diff --git a/sapi/cgi/fpm/fpm_trace_pread.c b/sapi/cgi/fpm/fpm_trace_pread.c
6088 new file mode 100644
6090 +++ b/sapi/cgi/fpm/fpm_trace_pread.c
6094 + /* (c) 2007,2008 Andrei Nigmatulin */
6096 +#define _GNU_SOURCE
6097 +#define _FILE_OFFSET_BITS 64
6099 +#include "fpm_config.h"
6101 +#include <unistd.h>
6105 +#include <stdint.h>
6107 +#include "fpm_trace.h"
6108 +#include "fpm_process_ctl.h"
6112 +static int mem_file = -1;
6114 +int fpm_trace_signal(pid_t pid)
6116 + if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
6117 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "kill(SIGSTOP) failed");
6124 +int fpm_trace_ready(pid_t pid)
6128 + sprintf(buf, "/proc/%d/" PROC_MEM_FILE, (int) pid);
6130 + mem_file = open(buf, O_RDONLY);
6132 + if (0 > mem_file) {
6133 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(%s) failed", buf);
6140 +int fpm_trace_close(pid_t pid)
6149 +int fpm_trace_get_long(long addr, long *data)
6151 + if (sizeof(*data) != pread(mem_file, (void *) data, sizeof(*data), (uintptr_t) addr)) {
6152 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pread() failed");
6159 diff --git a/sapi/cgi/fpm/fpm_trace_ptrace.c b/sapi/cgi/fpm/fpm_trace_ptrace.c
6160 new file mode 100644
6162 +++ b/sapi/cgi/fpm/fpm_trace_ptrace.c
6166 + /* (c) 2007,2008 Andrei Nigmatulin */
6168 +#include "fpm_config.h"
6170 +#include <sys/wait.h>
6171 +#include <sys/ptrace.h>
6172 +#include <unistd.h>
6175 +#if defined(PT_ATTACH) && !defined(PTRACE_ATTACH)
6176 +#define PTRACE_ATTACH PT_ATTACH
6179 +#if defined(PT_DETACH) && !defined(PTRACE_DETACH)
6180 +#define PTRACE_DETACH PT_DETACH
6183 +#if defined(PT_READ_D) && !defined(PTRACE_PEEKDATA)
6184 +#define PTRACE_PEEKDATA PT_READ_D
6187 +#include "fpm_trace.h"
6190 +static pid_t traced_pid;
6192 +int fpm_trace_signal(pid_t pid)
6194 + if (0 > ptrace(PTRACE_ATTACH, pid, 0, 0)) {
6195 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(ATTACH) failed");
6202 +int fpm_trace_ready(pid_t pid)
6209 +int fpm_trace_close(pid_t pid)
6211 + if (0 > ptrace(PTRACE_DETACH, pid, (void *) 1, 0)) {
6212 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(DETACH) failed");
6221 +int fpm_trace_get_long(long addr, long *data)
6224 + struct ptrace_io_desc ptio = {
6225 + .piod_op = PIOD_READ_D,
6226 + .piod_offs = (void *) addr,
6227 + .piod_addr = (void *) data,
6228 + .piod_len = sizeof(long)
6231 + if (0 > ptrace(PT_IO, traced_pid, (void *) &ptio, 0)) {
6232 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(PT_IO) failed");
6238 + *data = ptrace(PTRACE_PEEKDATA, traced_pid, (void *) addr, 0);
6241 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(PEEKDATA) failed");
6249 diff --git a/sapi/cgi/fpm/fpm_unix.c b/sapi/cgi/fpm/fpm_unix.c
6250 new file mode 100644
6252 +++ b/sapi/cgi/fpm/fpm_unix.c
6256 + /* (c) 2007,2008 Andrei Nigmatulin */
6258 +#include "fpm_config.h"
6260 +#include <string.h>
6261 +#include <sys/time.h>
6262 +#include <sys/resource.h>
6263 +#include <stdlib.h>
6264 +#include <unistd.h>
6265 +#include <sys/types.h>
6270 +#include <sys/prctl.h>
6274 +#include "fpm_conf.h"
6275 +#include "fpm_cleanup.h"
6276 +#include "fpm_clock.h"
6277 +#include "fpm_stdio.h"
6278 +#include "fpm_unix.h"
6281 +size_t fpm_pagesize;
6283 +int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp)
6285 + struct fpm_listen_options_s *lo = wp->config->listen_options;
6287 + /* uninitialized */
6288 + wp->socket_uid = -1;
6289 + wp->socket_gid = -1;
6290 + wp->socket_mode = 0666;
6292 + if (!lo) return 0;
6294 + if (lo->owner && *lo->owner) {
6295 + struct passwd *pwd;
6297 + pwd = getpwnam(lo->owner);
6300 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "cannot get uid for user '%s', pool '%s'", lo->owner, wp->config->name);
6304 + wp->socket_uid = pwd->pw_uid;
6305 + wp->socket_gid = pwd->pw_gid;
6308 + if (lo->group && *lo->group) {
6309 + struct group *grp;
6311 + grp = getgrnam(lo->group);
6314 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "cannot get gid for group '%s', pool '%s'", lo->group, wp->config->name);
6318 + wp->socket_gid = grp->gr_gid;
6321 + if (lo->mode && *lo->mode) {
6322 + wp->socket_mode = strtoul(lo->mode, 0, 8);
6328 +static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp)
6330 + int is_root = !geteuid();
6333 + if (wp->config->user && *wp->config->user) {
6335 + if (strlen(wp->config->user) == strspn(wp->config->user, "0123456789")) {
6336 + wp->set_uid = strtoul(wp->config->user, 0, 10);
6339 + struct passwd *pwd;
6341 + pwd = getpwnam(wp->config->user);
6344 + zlog(ZLOG_STUFF, ZLOG_ERROR, "cannot get uid for user '%s', pool '%s'", wp->config->user, wp->config->name);
6348 + wp->set_uid = pwd->pw_uid;
6349 + wp->set_gid = pwd->pw_gid;
6351 + wp->user = strdup(pwd->pw_name);
6352 + wp->home = strdup(pwd->pw_dir);
6356 + if (wp->config->group && *wp->config->group) {
6358 + if (strlen(wp->config->group) == strspn(wp->config->group, "0123456789")) {
6359 + wp->set_gid = strtoul(wp->config->group, 0, 10);
6362 + struct group *grp;
6364 + grp = getgrnam(wp->config->group);
6367 + zlog(ZLOG_STUFF, ZLOG_ERROR, "cannot get gid for group '%s', pool '%s'", wp->config->group, wp->config->name);
6371 + wp->set_gid = grp->gr_gid;
6375 +#ifndef I_REALLY_WANT_ROOT_PHP
6376 + if (wp->set_uid == 0 || wp->set_gid == 0) {
6377 + zlog(ZLOG_STUFF, ZLOG_ERROR, "please specify user and group other than root, pool '%s'", wp->config->name);
6382 + else { /* not root */
6383 + if (wp->config->user && *wp->config->user) {
6384 + zlog(ZLOG_STUFF, ZLOG_WARNING, "'user' directive is ignored, pool '%s'", wp->config->name);
6386 + if (wp->config->group && *wp->config->group) {
6387 + zlog(ZLOG_STUFF, ZLOG_WARNING, "'group' directive is ignored, pool '%s'", wp->config->name);
6389 + if (wp->config->chroot && *wp->config->chroot) {
6390 + zlog(ZLOG_STUFF, ZLOG_WARNING, "'chroot' directive is ignored, pool '%s'", wp->config->name);
6393 + { /* set up HOME and USER anyway */
6394 + struct passwd *pwd;
6396 + pwd = getpwuid(getuid());
6399 + wp->user = strdup(pwd->pw_name);
6400 + wp->home = strdup(pwd->pw_dir);
6408 +int fpm_unix_init_child(struct fpm_worker_pool_s *wp)
6410 + int is_root = !geteuid();
6411 + int made_chroot = 0;
6413 + if (wp->config->rlimit_files) {
6416 + getrlimit(RLIMIT_NOFILE, &r);
6418 + r.rlim_cur = (rlim_t) wp->config->rlimit_files;
6419 + r.rlim_max = r.rlim_cur;
6420 + if (0 > setrlimit(RLIMIT_NOFILE, &r)) {
6421 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setrlimit(RLIMIT_NOFILE) failed");
6425 + if (wp->config->rlimit_core) {
6428 + getrlimit(RLIMIT_CORE, &r);
6430 + r.rlim_cur = wp->config->rlimit_core == -1 ? (rlim_t) RLIM_INFINITY : (rlim_t) wp->config->rlimit_core;
6431 + r.rlim_max = r.rlim_cur;
6432 + if (0 > setrlimit(RLIMIT_CORE, &r)) {
6433 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setrlimit(RLIMIT_CORE) failed");
6437 + if (is_root && wp->config->chroot && *wp->config->chroot) {
6438 + if (0 > chroot(wp->config->chroot)) {
6439 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chroot(%s) failed", wp->config->chroot);
6445 + if (wp->config->chdir && *wp->config->chdir) {
6446 + if (0 > chdir(wp->config->chdir)) {
6447 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chdir(%s) failed", wp->config->chdir);
6451 + else if (made_chroot) {
6456 + if (wp->set_gid) {
6457 + if (0 > setgid(wp->set_gid)) {
6458 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setgid(%d) failed", wp->set_gid);
6462 + if (wp->set_uid) {
6463 + if (0 > initgroups(wp->config->user, wp->set_gid)) {
6464 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "initgroups(%s, %d) failed", wp->config->user, wp->set_gid);
6467 + if (0 > setuid(wp->set_uid)) {
6468 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setuid(%d) failed", wp->set_uid);
6475 + if (0 > prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)) {
6476 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "prctl(PR_SET_DUMPABLE) failed");
6480 + if (0 > fpm_clock_init()) {
6487 +int fpm_unix_init_main()
6489 + struct fpm_worker_pool_s *wp;
6491 + fpm_pagesize = getpagesize();
6493 + if (fpm_global_config.daemonize) {
6499 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fork() failed");
6508 + fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT);
6517 + if (0 > fpm_clock_init()) {
6521 + fpm_globals.parent_pid = getpid();
6523 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
6525 + if (0 > fpm_unix_conf_wp(wp)) {
6531 + fpm_stdio_init_final();
6535 + getrlimit(RLIMIT_NOFILE, &r);
6537 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "getrlimit(nofile): max:%lld, cur:%lld",
6538 + (long long) r.rlim_max, (long long) r.rlim_cur);
6543 diff --git a/sapi/cgi/fpm/fpm_unix.h b/sapi/cgi/fpm/fpm_unix.h
6544 new file mode 100644
6546 +++ b/sapi/cgi/fpm/fpm_unix.h
6550 + /* (c) 2007,2008 Andrei Nigmatulin */
6553 +#define FPM_UNIX_H 1
6555 +#include "fpm_worker_pool.h"
6557 +int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp);
6558 +int fpm_unix_init_child(struct fpm_worker_pool_s *wp);
6559 +int fpm_unix_init_main();
6561 +extern size_t fpm_pagesize;
6565 diff --git a/sapi/cgi/fpm/fpm_worker_pool.c b/sapi/cgi/fpm/fpm_worker_pool.c
6566 new file mode 100644
6568 +++ b/sapi/cgi/fpm/fpm_worker_pool.c
6572 + /* (c) 2007,2008 Andrei Nigmatulin */
6574 +#include "fpm_config.h"
6576 +#include <string.h>
6577 +#include <stdlib.h>
6578 +#include <unistd.h>
6580 +#include "fpm_worker_pool.h"
6581 +#include "fpm_cleanup.h"
6582 +#include "fpm_children.h"
6583 +#include "fpm_shm.h"
6584 +#include "fpm_shm_slots.h"
6585 +#include "fpm_conf.h"
6587 +struct fpm_worker_pool_s *fpm_worker_all_pools;
6589 +static void fpm_worker_pool_cleanup(int which, void *arg)
6591 + struct fpm_worker_pool_s *wp, *wp_next;
6593 + for (wp = fpm_worker_all_pools; wp; wp = wp_next) {
6594 + wp_next = wp->next;
6595 + fpm_worker_pool_config_free(wp->config);
6596 + fpm_children_free(wp->children);
6597 + fpm_array_free(&wp->slots_used);
6598 + fpm_array_free(&wp->slots_free);
6599 + fpm_shm_free_list(wp->shm_list, which == FPM_CLEANUP_CHILD ? fpm_shm_slots_mem() : 0);
6606 + fpm_worker_all_pools = 0;
6609 +struct fpm_worker_pool_s *fpm_worker_pool_alloc()
6611 + struct fpm_worker_pool_s *ret;
6613 + ret = malloc(sizeof(struct fpm_worker_pool_s));
6619 + memset(ret, 0, sizeof(struct fpm_worker_pool_s));
6621 + if (!fpm_worker_all_pools) {
6622 + fpm_worker_all_pools = ret;
6625 + fpm_array_init(&ret->slots_used, sizeof(struct fpm_shm_slot_ptr_s), 50);
6626 + fpm_array_init(&ret->slots_free, sizeof(struct fpm_shm_slot_ptr_s), 50);
6631 +int fpm_worker_pool_init_main()
6633 + if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_worker_pool_cleanup, 0)) {
6639 diff --git a/sapi/cgi/fpm/fpm_worker_pool.h b/sapi/cgi/fpm/fpm_worker_pool.h
6640 new file mode 100644
6642 +++ b/sapi/cgi/fpm/fpm_worker_pool.h
6646 + /* (c) 2007,2008 Andrei Nigmatulin */
6648 +#ifndef FPM_WORKER_POOL_H
6649 +#define FPM_WORKER_POOL_H 1
6651 +#include "fpm_conf.h"
6652 +#include "fpm_arrays.h"
6654 +struct fpm_worker_pool_s;
6655 +struct fpm_child_s;
6656 +struct fpm_child_stat_s;
6659 +enum fpm_address_domain {
6664 +struct fpm_worker_pool_s {
6665 + struct fpm_worker_pool_s *next;
6666 + struct fpm_worker_pool_config_s *config;
6667 + char *user, *home; /* for setting env USER and HOME */
6668 + enum fpm_address_domain listen_address_domain;
6669 + int listening_socket;
6670 + int set_uid, set_gid; /* config uid and gid */
6671 + unsigned is_template:1; /* just config template, no processes will be created */
6672 + int socket_uid, socket_gid, socket_mode;
6674 + struct fpm_shm_s *shm_list;
6675 + struct fpm_array_s slots_used;
6676 + struct fpm_array_s slots_free;
6679 + struct fpm_child_s *children;
6680 + int running_children;
6683 +struct fpm_worker_pool_s *fpm_worker_pool_alloc();
6684 +int fpm_worker_pool_init_main();
6686 +extern struct fpm_worker_pool_s *fpm_worker_all_pools;
6690 diff --git a/sapi/cgi/fpm/init.d/php-fpm.in b/sapi/cgi/fpm/init.d/php-fpm.in
6691 new file mode 100644
6693 +++ b/sapi/cgi/fpm/init.d/php-fpm.in
6697 +php_fpm_BIN=@prefix@/bin/php-cgi
6698 +php_fpm_CONF=@php_fpm_conf_path@
6699 +php_fpm_PID=@php_fpm_pid_path@
6702 +php_opts="--fpm-config $php_fpm_CONF"
6708 + while test $try -lt 35 ; do
6712 + if [ -f "$2" ] ; then
6719 + if [ ! -f "$2" ] ; then
6727 + try=`expr $try + 1`
6736 + echo -n "Starting php_fpm "
6738 + $php_fpm_BIN --fpm $php_opts
6740 + if [ "$?" != 0 ] ; then
6745 + wait_for_pid created $php_fpm_PID
6747 + if [ -n "$try" ] ; then
6756 + echo -n "Shutting down php_fpm "
6758 + if [ ! -r $php_fpm_PID ] ; then
6759 + echo "warning, no pid file found - php-fpm is not running ?"
6763 + kill -TERM `cat $php_fpm_PID`
6765 + wait_for_pid removed $php_fpm_PID
6767 + if [ -n "$try" ] ; then
6776 + echo -n "Gracefully shutting down php_fpm "
6778 + if [ ! -r $php_fpm_PID ] ; then
6779 + echo "warning, no pid file found - php-fpm is not running ?"
6783 + kill -QUIT `cat $php_fpm_PID`
6785 + wait_for_pid removed $php_fpm_PID
6787 + if [ -n "$try" ] ; then
6802 + echo -n "Reload service php-fpm "
6804 + if [ ! -r $php_fpm_PID ] ; then
6805 + echo "warning, no pid file found - php-fpm is not running ?"
6809 + kill -USR2 `cat $php_fpm_PID`
6816 + echo -n "Re-opening php-fpm log file "
6818 + if [ ! -r $php_fpm_PID ] ; then
6819 + echo "warning, no pid file found - php-fpm is not running ?"
6823 + kill -USR1 `cat $php_fpm_PID`
6829 + echo "Usage: $0 {start|stop|quit|restart|reload|logrotate}"
6834 diff --git a/sapi/cgi/fpm/xml_config.c b/sapi/cgi/fpm/xml_config.c
6835 new file mode 100644
6837 +++ b/sapi/cgi/fpm/xml_config.c
6841 + /* (c) 2004-2007 Andrei Nigmatulin */
6843 +#include "fpm_config.h"
6845 +#ifdef HAVE_ALLOCA_H
6846 +#include <alloca.h>
6848 +#include <string.h>
6850 +#include <stddef.h>
6851 +#include <stdlib.h>
6853 +#include <libxml/parser.h>
6854 +#include <libxml/tree.h>
6856 +#include "xml_config.h"
6858 +static struct xml_conf_section **xml_conf_sections = 0;
6859 +static int xml_conf_sections_allocated = 0;
6860 +static int xml_conf_sections_used = 0;
6862 +char *xml_conf_set_slot_boolean(void **conf, char *name, void *vv, intptr_t offset)
6865 + long value_y = !strcasecmp(value, "yes") || !strcmp(value, "1") || !strcasecmp(value, "on");
6866 + long value_n = !strcasecmp(value, "no") || !strcmp(value, "0") || !strcasecmp(value, "off");
6868 + if (!value_y && !value_n) {
6869 + return "xml_conf_set_slot(): invalid boolean value";
6872 +#ifdef XML_CONF_DEBUG
6873 + fprintf(stderr, "setting boolean '%s' => %s\n", name, value_y ? "TRUE" : "FALSE");
6876 + * (int *) ((char *) *conf + offset) = value_y ? 1 : 0;
6881 +char *xml_conf_set_slot_string(void **conf, char *name, void *vv, intptr_t offset)
6884 + char *v = strdup(value);
6886 + if (!v) return "xml_conf_set_slot_string(): strdup() failed";
6888 +#ifdef XML_CONF_DEBUG
6889 + fprintf(stderr, "setting string '%s' => '%s'\n", name, v);
6892 + * (char **) ((char *) *conf + offset) = v;
6897 +char *xml_conf_set_slot_integer(void **conf, char *name, void *vv, intptr_t offset)
6900 + int v = atoi(value);
6902 + * (int *) ((char *) *conf + offset) = v;
6904 +#ifdef XML_CONF_DEBUG
6905 + fprintf(stderr, "setting integer '%s' => %d\n", name, v);
6911 +char *xml_conf_set_slot_time(void **conf, char *name, void *vv, intptr_t offset)
6914 + int len = strlen(value);
6918 + if (!len) return "xml_conf_set_slot_timeval(): invalid timeval value";
6920 + suffix = value[len-1];
6922 + value[len-1] = '\0';
6926 + seconds = atoi(value);
6929 + seconds = 60 * atoi(value);
6932 + seconds = 60 * 60 * atoi(value);
6935 + seconds = 24 * 60 * 60 * atoi(value);
6938 + return "xml_conf_set_slot_timeval(): unknown suffix used in timeval value";
6941 + * (int *) ((char *) *conf + offset) = seconds;
6943 +#ifdef XML_CONF_DEBUG
6944 + fprintf(stderr, "setting time '%s' => %d:%02d:%02d:%02d\n", name, expand_dhms(seconds));
6950 +char *xml_conf_parse_section(void **conf, struct xml_conf_section *section, void *xml_node)
6952 + xmlNode *element = xml_node;
6955 +#ifdef XML_CONF_DEBUG
6956 + fprintf(stderr, "processing a section %s\n", section->path);
6959 + for ( ; element; element = element->next) {
6960 + if (element->type == XML_ELEMENT_NODE && !strcmp((const char *) element->name, "value") && element->children) {
6961 + xmlChar *name = xmlGetProp(element, (unsigned char *) "name");
6966 +#ifdef XML_CONF_DEBUG
6967 + fprintf(stderr, "found a value: %s\n", name);
6969 + for (i = 0; section->parsers[i].parser; i++) {
6970 + if (!section->parsers[i].name || !strcmp(section->parsers[i].name, (char *) name)) {
6975 + if (section->parsers[i].parser) {
6976 + if (section->parsers[i].type == XML_CONF_SCALAR) {
6977 + if (element->children->type == XML_TEXT_NODE) {
6978 + ret = section->parsers[i].parser(conf, (char *) name, element->children->content, section->parsers[i].offset);
6981 + ret = "XML_TEXT_NODE is expected, something different is given";
6985 + ret = section->parsers[i].parser(conf, (char *) name, element->children, section->parsers[i].offset);
6989 + if (ret) return ret;
6993 + fprintf(stderr, "Warning, unknown setting '%s' in section '%s'\n", (char *) name, section->path);
7003 +static char *xml_conf_parse_file(xmlNode *element)
7007 + for ( ; element; element = element->next) {
7009 + if (element->parent && element->type == XML_ELEMENT_NODE && !strcmp((const char *) element->name, "section")) {
7010 + xmlChar *name = xmlGetProp(element, (unsigned char *) "name");
7013 + char *parent_name = (char *) xmlGetNodePath(element->parent);
7016 + struct xml_conf_section *section = NULL;
7018 +#ifdef XML_CONF_DEBUG
7019 + fprintf(stderr, "got a section: %s/%s\n", parent_name, name);
7021 + full_name = alloca(strlen(parent_name) + strlen((char *) name) + 1 + 1);
7023 + sprintf(full_name, "%s/%s", parent_name, (char *) name);
7025 + xmlFree(parent_name);
7028 + for (i = 0; i < xml_conf_sections_used; i++) {
7029 + if (!strcmp(xml_conf_sections[i]->path, full_name)) {
7030 + section = xml_conf_sections[i];
7034 + if (section) { /* found a registered section */
7035 + void *conf = section->conf();
7036 + ret = xml_conf_parse_section(&conf, section, element->children);
7043 + if (element->children) {
7044 + ret = xml_conf_parse_file(element->children);
7052 +char *xml_conf_load_file(char *file)
7057 + LIBXML_TEST_VERSION
7059 + doc = xmlParseFile(file);
7062 + ret = xml_conf_parse_file(doc->children);
7066 + ret = "failed to parse conf file";
7069 + xmlCleanupParser();
7073 +int xml_conf_init()
7078 +void xml_conf_clean()
7080 + if (xml_conf_sections) {
7081 + free(xml_conf_sections);
7085 +int xml_conf_section_register(struct xml_conf_section *section)
7087 + if (xml_conf_sections_allocated == xml_conf_sections_used) {
7088 + int new_size = xml_conf_sections_used + 10;
7089 + void *new_ptr = realloc(xml_conf_sections, sizeof(struct xml_conf_section *) * new_size);
7092 + xml_conf_sections = new_ptr;
7093 + xml_conf_sections_allocated = new_size;
7096 + fprintf(stderr, "xml_conf_section_register(): out of memory\n");
7101 + xml_conf_sections[xml_conf_sections_used++] = section;
7106 +int xml_conf_sections_register(struct xml_conf_section *sections[])
7108 + for ( ; sections && *sections; sections++) {
7109 + if (0 > xml_conf_section_register(*sections)) {
7117 diff --git a/sapi/cgi/fpm/xml_config.h b/sapi/cgi/fpm/xml_config.h
7118 new file mode 100644
7120 +++ b/sapi/cgi/fpm/xml_config.h
7124 + /* (c) 2004-2007 Andrei Nigmatulin */
7126 +#ifndef XML_CONFIG_H
7127 +#define XML_CONFIG_H 1
7129 +#include <stdint.h>
7131 +struct xml_value_parser;
7133 +struct xml_value_parser {
7136 + char *(*parser)(void **, char *, void *, intptr_t offset);
7140 +struct xml_conf_section {
7143 + struct xml_value_parser *parsers;
7146 +char *xml_conf_set_slot_boolean(void **conf, char *name, void *value, intptr_t offset);
7147 +char *xml_conf_set_slot_string(void **conf, char *name, void *value, intptr_t offset);
7148 +char *xml_conf_set_slot_integer(void **conf, char *name, void *value, intptr_t offset);
7149 +char *xml_conf_set_slot_time(void **conf, char *name, void *value, intptr_t offset);
7151 +int xml_conf_init();
7152 +void xml_conf_clean();
7153 +char *xml_conf_load_file(char *file);
7154 +char *xml_conf_parse_section(void **conf, struct xml_conf_section *section, void *ve);
7155 +int xml_conf_section_register(struct xml_conf_section *section);
7156 +int xml_conf_sections_register(struct xml_conf_section *sections[]);
7158 +#define expand_hms(value) (value) / 3600, ((value) % 3600) / 60, (value) % 60
7160 +#define expand_dhms(value) (value) / 86400, ((value) % 86400) / 3600, ((value) % 3600) / 60, (value) % 60
7162 +enum { XML_CONF_SCALAR = 1, XML_CONF_SUBSECTION = 2 };
7165 diff --git a/sapi/cgi/fpm/zlog.c b/sapi/cgi/fpm/zlog.c
7166 new file mode 100644
7168 +++ b/sapi/cgi/fpm/zlog.c
7172 + /* (c) 2004-2007 Andrei Nigmatulin */
7174 +#include "fpm_config.h"
7177 +#include <unistd.h>
7179 +#include <string.h>
7180 +#include <stdarg.h>
7181 +#include <sys/time.h>
7186 +#define MAX_LINE_LENGTH 1024
7188 +static int zlog_fd = -1;
7189 +static int zlog_level = ZLOG_NOTICE;
7191 +static const char *level_names[] = {
7192 + [ZLOG_DEBUG] = "DEBUG",
7193 + [ZLOG_NOTICE] = "NOTICE",
7194 + [ZLOG_WARNING] = "WARNING",
7195 + [ZLOG_ERROR] = "ERROR",
7196 + [ZLOG_ALERT] = "ALERT",
7199 +size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len)
7204 + len = strftime(timebuf, timebuf_len, "%b %d %H:%M:%S", localtime_r((const time_t *) &tv->tv_sec, &t));
7205 + len += snprintf(timebuf + len, timebuf_len - len, ".%06d", (int) tv->tv_usec);
7210 +int zlog_set_fd(int new_fd)
7212 + int old_fd = zlog_fd;
7218 +int zlog_set_level(int new_value)
7220 + int old_value = zlog_level;
7222 + zlog_level = new_value;
7227 +void zlog(const char *function, int line, int flags, const char *fmt, ...)
7229 + struct timeval tv;
7230 + char buf[MAX_LINE_LENGTH];
7231 + const size_t buf_size = MAX_LINE_LENGTH;
7234 + int truncated = 0;
7237 + if ((flags & ZLOG_LEVEL_MASK) < zlog_level) {
7241 + saved_errno = errno;
7243 + gettimeofday(&tv, 0);
7245 + len = zlog_print_time(&tv, buf, buf_size);
7247 + len += snprintf(buf + len, buf_size - len, " [%s] %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
7249 + if (len > buf_size - 1) {
7254 + va_start(args, fmt);
7256 + len += vsnprintf(buf + len, buf_size - len, fmt, args);
7260 + if (len >= buf_size) {
7266 + if (flags & ZLOG_HAVE_ERRNO) {
7267 + len += snprintf(buf + len, buf_size - len, ": %s (%d)", strerror(saved_errno), saved_errno);
7268 + if (len >= buf_size) {
7275 + memcpy(buf + buf_size - sizeof("..."), "...", sizeof("...") - 1);
7276 + len = buf_size - 1;
7279 + buf[len++] = '\n';
7281 + write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, buf, len);
7283 diff --git a/sapi/cgi/fpm/zlog.h b/sapi/cgi/fpm/zlog.h
7284 new file mode 100644
7286 +++ b/sapi/cgi/fpm/zlog.h
7290 + /* (c) 2004-2007 Andrei Nigmatulin */
7295 +#define ZLOG_STUFF __func__, __LINE__
7299 +int zlog_set_fd(int new_fd);
7300 +int zlog_set_level(int new_value);
7302 +size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len);
7304 +void zlog(const char *function, int line, int flags, const char *fmt, ...)
7305 + __attribute__ ((format(printf,4,5)));
7315 +#define ZLOG_LEVEL_MASK 7
7317 +#define ZLOG_HAVE_ERRNO 0x100
7319 +#define ZLOG_SYSERROR (ZLOG_ERROR | ZLOG_HAVE_ERRNO)