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 index 6df7d68..f155934 100644
171 --- a/main/php_config.h.in
172 +++ b/main/php_config.h.in
174 /* Define if you have the chroot function. */
177 +/* Define if you have the clearenv function. */
178 +#undef HAVE_CLEARENV
180 /* Define if you have the crypt function. */
187 +/* Is experimental fastcgi process manager code activated */
188 +#undef PHP_FASTCGI_PM
191 #undef FORCE_CGI_REDIRECT
195 #undef ENABLE_PATHINFO_CHECK
197 +/* do we have libxml? */
200 +/* do we have prctl? */
203 +/* do we have clock_gettime? */
204 +#undef HAVE_CLOCK_GETTIME
206 +/* do we have clock_get_time? */
207 +#undef HAVE_CLOCK_GET_TIME
209 +/* do we have ptrace? */
212 +/* do we have mach_vm_read? */
213 +#undef HAVE_MACH_VM_READ
215 +/* /proc/pid/mem interface */
216 +#undef PROC_MEM_FILE
218 /* Define if system uses EBCDIC */
219 #undef CHARSET_EBCDIC
221 diff --git a/sapi/cgi/Makefile.frag b/sapi/cgi/Makefile.frag
222 index 57a3b29..6fa6c9f 100644
223 --- a/sapi/cgi/Makefile.frag
224 +++ b/sapi/cgi/Makefile.frag
226 -$(SAPI_CGI_PATH): $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS)
227 +$(SAPI_CGI_PATH): $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS) $(SAPI_EXTRA_DEPS)
229 diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c
230 index a4a19c2..2c19083 100644
231 --- a/sapi/cgi/cgi_main.c
232 +++ b/sapi/cgi/cgi_main.c
235 #include <sys/wait.h>
241 #include "zend_extensions.h"
243 @@ -83,6 +86,11 @@ int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS;
248 +#include "fpm/fpm.h"
249 +#include "fpm/fpm_request.h"
253 /* XXX this will need to change later when threaded fastcgi is
254 implemented. shane */
255 @@ -115,8 +123,12 @@ static int parent_waiting = 0;
259 +static int request_body_fd;
263 +static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC);
265 #define PHP_MODE_STANDARD 1
266 #define PHP_MODE_HIGHLIGHT 2
267 #define PHP_MODE_INDENT 3
268 @@ -146,6 +158,10 @@ static const opt_struct OPTIONS[] = {
270 {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
274 + {'y', 1, "fpm-config"},
276 {'z', 1, "zend-extension"},
279 @@ -170,6 +186,7 @@ typedef struct _php_cgi_globals_struct {
280 zend_bool impersonate;
283 + char *error_header;
284 } php_cgi_globals_struct;
287 @@ -474,7 +491,28 @@ static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC)
289 if (fcgi_is_fastcgi()) {
290 fcgi_request *request = (fcgi_request*) SG(server_context);
291 - tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
293 + if (request_body_fd == -1) {
294 + char *request_body_filename = sapi_cgibin_getenv((char *) "REQUEST_BODY_FILE",
295 + sizeof("REQUEST_BODY_FILE")-1 TSRMLS_CC);
297 + if (request_body_filename && *request_body_filename) {
298 + request_body_fd = open(request_body_filename, O_RDONLY);
300 + if (0 > request_body_fd) {
301 + php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)",
302 + request_body_filename, strerror(errno), errno);
308 + /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */
309 + if (request_body_fd < 0) {
310 + tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
312 + tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes);
315 tmp_read_bytes = read(STDIN_FILENO, buffer + read_bytes, count_bytes - read_bytes);
317 @@ -786,7 +824,12 @@ static void php_cgi_usage(char *argv0)
318 " -s Display colour syntax highlighted source.\n"
319 " -v Version number\n"
320 " -w Display source with stripped comments and whitespace.\n"
321 - " -z <file> Load Zend extension <file>.\n"
323 + " -x, --fpm Run in FastCGI process manager mode.\n"
324 + " -y, --fpm-config <file>\n"
325 + " Specify alternative path to FastCGI process manager config file.\n"
327 + " -z <file> Load Zend extension <file>.\n"
329 " -T <count> Measure execution time of script repeated <count> times.\n"
331 @@ -1236,6 +1279,7 @@ PHP_INI_BEGIN()
333 STD_PHP_INI_ENTRY("fastcgi.impersonate", "0", PHP_INI_SYSTEM, OnUpdateBool, impersonate, php_cgi_globals_struct, php_cgi_globals)
335 + STD_PHP_INI_ENTRY("fastcgi.error_header", NULL, PHP_INI_SYSTEM, OnUpdateString, error_header, php_cgi_globals_struct, php_cgi_globals)
339 @@ -1258,6 +1302,7 @@ static void php_cgi_globals_ctor(php_cgi_globals_struct *php_cgi_globals TSRMLS_
341 php_cgi_globals->impersonate = 0;
343 + php_cgi_globals->error_header = NULL;
347 @@ -1290,9 +1335,47 @@ static PHP_MSHUTDOWN_FUNCTION(cgi)
348 static PHP_MINFO_FUNCTION(cgi)
350 DISPLAY_INI_ENTRIES();
354 +#include "fpm/fpm_autoconf.h"
356 + php_info_print_table_start();
357 + php_info_print_table_row(2, "php-fpm", fpm ? "active" : "inactive");
358 + php_info_print_table_row(2, "php-fpm version", PHP_FPM_VERSION);
359 + php_info_print_table_end();
366 +PHP_FUNCTION(fastcgi_finish_request)
368 + fcgi_request *request = (fcgi_request*) SG(server_context);
370 + if (fcgi_is_fastcgi() && request->fd >= 0) {
372 + php_end_ob_buffers(1 TSRMLS_CC);
373 + php_header(TSRMLS_C);
375 + fcgi_flush(request, 1);
376 + fcgi_close(request, 0, 0);
385 +function_entry cgi_fcgi_sapi_functions[] = {
387 + PHP_FE(fastcgi_finish_request, NULL)
392 static zend_module_entry cgi_module_entry = {
393 STANDARD_MODULE_HEADER,
395 @@ -1300,7 +1383,7 @@ static zend_module_entry cgi_module_entry = {
400 + cgi_fcgi_sapi_functions,
404 @@ -1340,6 +1423,7 @@ int main(int argc, char *argv[])
405 char *bindpath = NULL;
407 fcgi_request request;
408 + char *fpm_config = NULL;
411 #if HAVE_GETTIMEOFDAY
412 @@ -1460,6 +1544,14 @@ int main(int argc, char *argv[])
413 case 's': /* generate highlighted HTML from source */
414 behavior = PHP_MODE_HIGHLIGHT;
418 + fpm_config = php_optarg;
427 @@ -1524,6 +1616,19 @@ consult the installation file that came with this distribution, or visit \n\
428 #endif /* FORCE_CGI_REDIRECT */
433 + if (0 > fpm_init(argc, argv, fpm_config)) {
437 + fcgi_fd = fpm_run(&max_requests);
439 + fcgi_set_is_fastcgi(fastcgi = 1);
445 fcgi_fd = fcgi_listen(bindpath, 128);
447 @@ -1538,6 +1643,9 @@ consult the installation file that came with this distribution, or visit \n\
450 /* How many times to run PHP scripts before dying */
454 if (getenv("PHP_FCGI_MAX_REQUESTS")) {
455 max_requests = atoi(getenv("PHP_FCGI_MAX_REQUESTS"));
456 if (max_requests < 0) {
457 @@ -1555,6 +1663,9 @@ consult the installation file that came with this distribution, or visit \n\
460 /* Pre-fork, if required */
464 if (getenv("PHP_FCGI_CHILDREN")) {
465 char * children_str = getenv("PHP_FCGI_CHILDREN");
466 children = atoi(children_str);
467 @@ -1704,6 +1815,8 @@ consult the installation file that came with this distribution, or visit \n\
471 + request_body_fd = -1;
473 SG(server_context) = (void *) &request;
475 SG(server_context) = (void *) 1; /* avoid server_context==NULL checks */
476 @@ -1711,6 +1824,10 @@ consult the installation file that came with this distribution, or visit \n\
477 init_request_info(TSRMLS_C);
481 + if (fpm) fpm_request_info();
487 @@ -1994,6 +2111,10 @@ consult the installation file that came with this distribution, or visit \n\
492 + if (fpm) fpm_request_executing();
496 case PHP_MODE_STANDARD:
497 php_execute_script(&file_handle TSRMLS_CC);
498 @@ -2046,6 +2167,10 @@ consult the installation file that came with this distribution, or visit \n\
501 fastcgi_request_done:
503 + if (request_body_fd != -1) close(request_body_fd);
505 + request_body_fd = -2;
508 char *path_translated;
509 @@ -2059,6 +2184,16 @@ fastcgi_request_done:
510 SG(request_info).path_translated = path_translated;
513 + if (EG(exit_status) == 255) {
514 + if (CGIG(error_header) && *CGIG(error_header)) {
515 + sapi_header_line ctr = {0};
517 + ctr.line = CGIG(error_header);
518 + ctr.line_len = strlen(CGIG(error_header));
519 + sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
523 php_request_shutdown((void *) 0);
524 if (exit_status == 0) {
525 exit_status = EG(exit_status);
526 @@ -2096,15 +2231,20 @@ fastcgi_request_done:
530 - if (max_requests != 1) {
531 - /* no need to return exit_status of the last request */
536 /* end of fastcgi loop */
540 + if (fcgi_in_shutdown() || /* graceful shutdown by a signal */
541 + (max_requests && (requests == max_requests)) /* we were told to process max_requests and we are done */
550 if (cgi_sapi_module.php_ini_path_override) {
551 diff --git a/sapi/cgi/config9.m4 b/sapi/cgi/config9.m4
552 index 4ff90fd..fdf409b 100644
553 --- a/sapi/cgi/config9.m4
554 +++ b/sapi/cgi/config9.m4
555 @@ -22,6 +22,10 @@ PHP_ARG_ENABLE(path-info-check,,
556 [ --disable-path-info-check CGI: If this is disabled, paths such as
557 /info.php/test?a=b will fail to work], yes, no)
559 +PHP_ARG_ENABLE(fpm,,
560 +[ --enable-fpm FastCGI: If this is enabled, the fastcgi support
561 + will include experimental process manager code], no, no)
566 @@ -54,6 +58,20 @@ if test "$PHP_SAPI" = "default"; then
567 AC_DEFINE_UNQUOTED(PHP_FASTCGI, $PHP_ENABLE_FASTCGI, [ ])
568 AC_MSG_RESULT($PHP_FASTCGI)
571 + if test "$PHP_FASTCGI" = "yes"; then
572 + AC_MSG_CHECKING(whether to enable FastCGI Process Manager)
573 + if test "$PHP_FPM" = "yes"; then
578 + AC_MSG_RESULT($PHP_FPM)
582 + AC_DEFINE_UNQUOTED(PHP_FASTCGI_PM, $PHP_FASTCGI_PM, [Is experimental fastcgi process manager code activated])
584 dnl --enable-force-cgi-redirect
585 AC_MSG_CHECKING(whether to force Apache CGI redirect)
586 if test "$PHP_FORCE_CGI_REDIRECT" = "yes"; then
587 @@ -93,10 +111,10 @@ if test "$PHP_SAPI" = "default"; then
588 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)"
591 - 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)"
592 + 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)"
595 - BUILD_CGI="\$(LIBTOOL) --mode=link \$(CC) -export-dynamic \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(PHP_RPATHS) \$(PHP_GLOBAL_OBJS) \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
596 + BUILD_CGI="\$(LIBTOOL) --mode=link \$(CC) -export-dynamic \$(CFLAGS_CLEAN) \$(EXTRA_CFLAGS) \$(EXTRA_LDFLAGS_PROGRAM) \$(LDFLAGS) \$(PHP_RPATHS) \$(PHP_GLOBAL_OBJS) \$(PHP_SAPI_OBJS) \$(EXTRA_LIBS) \$(SAPI_EXTRA_LIBS) \$(ZEND_EXTRA_LIBS) -o \$(SAPI_CGI_PATH)"
600 diff --git a/sapi/cgi/fastcgi.c b/sapi/cgi/fastcgi.c
601 index 68b7e21..bcc0e7e 100644
602 --- a/sapi/cgi/fastcgi.c
603 +++ b/sapi/cgi/fastcgi.c
609 +#include "fpm/fpm.h"
610 +#include "fpm/fpm_request.h"
616 @@ -234,6 +239,8 @@ int fcgi_init(void)
618 return is_fastcgi = 0;
621 + fcgi_set_allowed_clients(getenv("FCGI_WEB_SERVER_ADDRS"));
625 @@ -249,14 +256,26 @@ int fcgi_is_fastcgi(void)
629 +void fcgi_set_is_fastcgi(int new_value)
631 + is_fastcgi = new_value;
634 +void fcgi_set_in_shutdown(int new_value)
636 + in_shutdown = new_value;
639 void fcgi_shutdown(void)
641 if (is_initialized) {
642 zend_hash_destroy(&fcgi_mgmt_vars);
646 if (allowed_clients) {
647 free(allowed_clients);
648 + allowed_clients = 0;
652 @@ -330,6 +349,41 @@ out_fail:
656 +void fcgi_set_allowed_clients(char *ip)
666 + if (*cur == ',') n++;
669 + if (allowed_clients) free(allowed_clients);
670 + allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
674 + end = strchr(cur, ',');
679 + allowed_clients[n] = inet_addr(cur);
680 + if (allowed_clients[n] == INADDR_NONE) {
681 + fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
686 + allowed_clients[n] = INADDR_NONE;
691 static int is_port_number(const char *bindpath)
694 @@ -458,38 +512,6 @@ int fcgi_listen(const char *path, int backlog)
699 - char *ip = getenv("FCGI_WEB_SERVER_ADDRS");
708 - if (*cur == ',') n++;
711 - allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
715 - end = strchr(cur, ',');
720 - allowed_clients[n] = inet_addr(cur);
721 - if (allowed_clients[n] == INADDR_NONE) {
722 - fprintf(stderr, "Wrong IP address '%s' in FCGI_WEB_SERVER_ADDRS\n", cur);
727 - allowed_clients[n] = INADDR_NONE;
732 if (!is_initialized) {
733 @@ -866,7 +888,7 @@ int fcgi_read(fcgi_request *req, char *str, int len)
737 -static inline void fcgi_close(fcgi_request *req, int force, int destroy)
738 +void fcgi_close(fcgi_request *req, int force, int destroy)
741 zend_hash_destroy(&req->env);
742 @@ -906,6 +928,10 @@ static inline void fcgi_close(fcgi_request *req, int force, int destroy)
748 + if (fpm) fpm_request_finished();
753 @@ -953,6 +979,10 @@ int fcgi_accept_request(fcgi_request *req)
755 socklen_t len = sizeof(sa);
758 + if (fpm) fpm_request_accepting();
761 FCGI_LOCK(req->listen_socket);
762 req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
763 FCGI_UNLOCK(req->listen_socket);
764 @@ -988,6 +1018,11 @@ int fcgi_accept_request(fcgi_request *req)
770 + if (fpm) fpm_request_reading_headers();
773 #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
776 diff --git a/sapi/cgi/fastcgi.h b/sapi/cgi/fastcgi.h
777 index c095c49..a3b3832 100644
778 --- a/sapi/cgi/fastcgi.h
779 +++ b/sapi/cgi/fastcgi.h
780 @@ -114,6 +114,9 @@ typedef struct _fcgi_request {
782 void fcgi_shutdown(void);
783 int fcgi_is_fastcgi(void);
784 +void fcgi_set_is_fastcgi(int);
785 +void fcgi_set_in_shutdown(int);
786 +void fcgi_set_allowed_clients(char *);
787 int fcgi_in_shutdown(void);
788 int fcgi_listen(const char *path, int backlog);
789 void fcgi_init_request(fcgi_request *req, int listen_socket);
790 @@ -128,6 +131,8 @@ int fcgi_read(fcgi_request *req, char *str, int len);
791 int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len);
792 int fcgi_flush(fcgi_request *req, int close);
794 +void fcgi_close(fcgi_request *req, int force, int destroy);
797 void fcgi_impersonate(void);
799 diff --git a/sapi/cgi/fpm/Makefile.frag b/sapi/cgi/fpm/Makefile.frag
801 index 0000000..15e6637
803 +++ b/sapi/cgi/fpm/Makefile.frag
806 +install-fpm: sapi/cgi/fpm/php-fpm.conf sapi/cgi/fpm/php-fpm
807 + @echo "Installing FPM config: $(INSTALL_ROOT)$(php_fpm_conf_path)"
808 + -@$(mkinstalldirs) \
809 + $(INSTALL_ROOT)$(prefix)/sbin \
810 + `dirname "$(INSTALL_ROOT)$(php_fpm_conf_path)"` \
811 + `dirname "$(INSTALL_ROOT)$(php_fpm_log_path)"` \
812 + `dirname "$(INSTALL_ROOT)$(php_fpm_pid_path)"`
813 + -@if test -r "$(INSTALL_ROOT)$(php_fpm_conf_path)" ; then \
814 + dest=`basename "$(php_fpm_conf_path)"`.default ; \
815 + echo " (installing as $$dest)" ; \
817 + dest=`basename "$(php_fpm_conf_path)"` ; \
819 + $(INSTALL_DATA) $(top_builddir)/sapi/cgi/fpm/php-fpm.conf $(INSTALL_ROOT)`dirname "$(php_fpm_conf_path)"`/$$dest
820 + @echo "Installing init.d script: $(INSTALL_ROOT)$(prefix)/sbin/php-fpm"
821 + -@$(INSTALL) -m 0755 $(top_builddir)/sapi/cgi/fpm/php-fpm $(INSTALL_ROOT)$(prefix)/sbin/php-fpm
823 +$(top_builddir)/libevent/libevent.a: $(top_builddir)/libevent/Makefile
824 + cd $(top_builddir)/libevent && $(MAKE) libevent.a
826 diff --git a/sapi/cgi/fpm/acinclude.m4 b/sapi/cgi/fpm/acinclude.m4
828 index 0000000..e4d1f3e
830 +++ b/sapi/cgi/fpm/acinclude.m4
833 +AC_DEFUN([AC_FPM_CHECK_FUNC],
835 + SAVED_CFLAGS="$CFLAGS"
836 + CFLAGS="$CFLAGS $2"
840 + AC_CHECK_FUNC([$1],[$4],[$5])
842 + CFLAGS="$SAVED_CFLAGS"
846 +AC_DEFUN([AC_FPM_LIBEVENT],
848 + AC_ARG_WITH([libevent],
849 + [ --with-libevent=DIR FPM: libevent install directory])
852 + LIBEVENT_LIBS="-levent"
853 + LIBEVENT_INCLUDE_PATH=""
855 + if test "$with_libevent" != "no" -a -n "$with_libevent"; then
856 + LIBEVENT_CFLAGS="-I$with_libevent/include"
857 + LIBEVENT_LIBS="-L$with_libevent/lib $LIBEVENT_LIBS"
858 + LIBEVENT_INCLUDE_PATH="$with_libevent/include"
861 + AC_MSG_CHECKING([for event.h])
865 + for dir in "$LIBEVENT_INCLUDE_PATH" /usr/include ; do
866 + if test -r "$dir/event.h" ; then
872 + AC_MSG_RESULT([$found])
874 + AC_FPM_CHECK_FUNC([event_set], [$LIBEVENT_CFLAGS], [$LIBEVENT_LIBS], ,
875 + [AC_MSG_ERROR([Failed to link with libevent. Perhaps --with-libevent=DIR option could help.])])
877 + AC_FPM_CHECK_FUNC([event_base_free], [$LIBEVENT_CFLAGS], [$LIBEVENT_LIBS], ,
878 + [AC_MSG_ERROR([You have too old version. libevent version >= 1.2 is required.])])
882 +AC_DEFUN([AC_FPM_LIBXML],
884 + AC_MSG_RESULT([checking for XML configuration])
886 + AC_ARG_WITH(xml-config,
887 + [ --with-xml-config=PATH FPM: use xml-config in PATH to find libxml],
888 + [XMLCONFIG="$withval"],
889 + [AC_PATH_PROGS(XMLCONFIG, [xml2-config xml-config], "")]
892 + if test "x$XMLCONFIG" = "x"; then
893 + AC_MSG_ERROR([XML configuration could not be found])
895 + AC_MSG_CHECKING([for libxml library])
897 + if test ! -x "$XMLCONFIG"; then
898 + AC_MSG_ERROR([$XMLCONFIG cannot be executed])
901 + LIBXML_LIBS="`$XMLCONFIG --libs`"
902 + LIBXML_CFLAGS="`$XMLCONFIG --cflags`"
903 + LIBXML_VERSION="`$XMLCONFIG --version`"
905 + AC_MSG_RESULT([yes, $LIBXML_VERSION])
907 + AC_FPM_CHECK_FUNC([xmlParseFile], [$LIBXML_CFLAGS], [$LIBXML_LIBS], ,
908 + [AC_MSG_ERROR([Failed to link with libxml])])
910 + AC_DEFINE(HAVE_LIBXML, 1, [do we have libxml?])
914 +AC_DEFUN([AC_FPM_JUDY],
916 + AC_ARG_WITH([Judy],
917 + [ --with-Judy=DIR FPM: Judy install directory])
921 + JUDY_INCLUDE_PATH=""
923 + if test "$with_Judy" != "no" -a -n "$with_Judy"; then
924 + JUDY_INCLUDE_PATH="$with_Judy/include"
925 + JUDY_CFLAGS="-I$with_Judy/include $JUDY_CFLAGS"
926 + JUDY_LIBS="-L$with_Judy/lib $JUDY_LIBS"
929 + AC_MSG_CHECKING([for Judy.h])
933 + for dir in "$JUDY_INCLUDE_PATH" /usr/include ; do
934 + if test -r "$dir/Judy.h" ; then
940 + AC_MSG_RESULT([$found])
942 + AC_FPM_CHECK_FUNC([JudyLCount], [$JUDY_CFLAGS], [$JUDY_LIBS], ,
943 + [AC_MSG_ERROR([Failed to link with Judy])])
947 +AC_DEFUN([AC_FPM_CLOCK],
949 + have_clock_gettime=no
951 + AC_MSG_CHECKING([for clock_gettime])
953 + AC_TRY_LINK([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
954 + have_clock_gettime=yes
955 + AC_MSG_RESULT([yes])
957 + AC_MSG_RESULT([no])
960 + if test "$have_clock_gettime" = "no"; then
961 + AC_MSG_CHECKING([for clock_gettime in -lrt])
966 + AC_TRY_LINK([ #include <time.h> ], [struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);], [
967 + have_clock_gettime=yes
968 + AC_MSG_RESULT([yes])
971 + AC_MSG_RESULT([no])
975 + if test "$have_clock_gettime" = "yes"; then
976 + AC_DEFINE([HAVE_CLOCK_GETTIME], 1, [do we have clock_gettime?])
979 + have_clock_get_time=no
981 + if test "$have_clock_gettime" = "no"; then
982 + AC_MSG_CHECKING([for clock_get_time])
984 + AC_TRY_RUN([ #include <mach/mach.h>
985 + #include <mach/clock.h>
986 + #include <mach/mach_error.h>
990 + kern_return_t ret; clock_serv_t aClock; mach_timespec_t aTime;
991 + ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &aClock);
993 + if (ret != KERN_SUCCESS) {
997 + ret = clock_get_time(aClock, &aTime);
998 + if (ret != KERN_SUCCESS) {
1005 + have_clock_get_time=yes
1006 + AC_MSG_RESULT([yes])
1008 + AC_MSG_RESULT([no])
1012 + if test "$have_clock_get_time" = "yes"; then
1013 + AC_DEFINE([HAVE_CLOCK_GET_TIME], 1, [do we have clock_get_time?])
1017 +AC_DEFUN([AC_FPM_TRACE],
1020 + have_broken_ptrace=no
1022 + AC_MSG_CHECKING([for ptrace])
1025 + #include <sys/types.h>
1026 + #include <sys/ptrace.h> ], [ptrace(0, 0, (void *) 0, 0);], [
1028 + AC_MSG_RESULT([yes])
1030 + AC_MSG_RESULT([no])
1033 + if test "$have_ptrace" = "yes"; then
1034 + AC_MSG_CHECKING([whether ptrace works])
1037 + #include <unistd.h>
1038 + #include <signal.h>
1039 + #include <sys/wait.h>
1040 + #include <sys/types.h>
1041 + #include <sys/ptrace.h>
1042 + #include <errno.h>
1044 + #if !defined(PTRACE_ATTACH) && defined(PT_ATTACH)
1045 + #define PTRACE_ATTACH PT_ATTACH
1048 + #if !defined(PTRACE_DETACH) && defined(PT_DETACH)
1049 + #define PTRACE_DETACH PT_DETACH
1052 + #if !defined(PTRACE_PEEKDATA) && defined(PT_READ_D)
1053 + #define PTRACE_PEEKDATA PT_READ_D
1058 + long v1 = (unsigned int) -1; /* copy will fail if sizeof(long) == 8 and we've got "int ptrace()" */
1063 + if ( (child = fork()) ) { /* parent */
1066 + if (0 > ptrace(PTRACE_ATTACH, child, 0, 0)) {
1070 + waitpid(child, &status, 0);
1073 + struct ptrace_io_desc ptio = {
1074 + .piod_op = PIOD_READ_D,
1077 + .piod_len = sizeof(v1)
1080 + if (0 > ptrace(PT_IO, child, (void *) &ptio, 0)) {
1086 + v2 = ptrace(PTRACE_PEEKDATA, child, (void *) &v1, 0);
1092 + ptrace(PTRACE_DETACH, child, (void *) 1, 0);
1094 + kill(child, SIGKILL);
1096 + return ret ? ret : (v1 != v2);
1098 + else { /* child */
1104 + AC_MSG_RESULT([yes])
1107 + have_broken_ptrace=yes
1108 + AC_MSG_RESULT([no])
1112 + if test "$have_ptrace" = "yes"; then
1113 + AC_DEFINE([HAVE_PTRACE], 1, [do we have ptrace?])
1116 + have_mach_vm_read=no
1118 + if test "$have_broken_ptrace" = "yes"; then
1119 + AC_MSG_CHECKING([for mach_vm_read])
1121 + AC_TRY_COMPILE([ #include <mach/mach.h>
1122 + #include <mach/mach_vm.h>
1124 + 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);
1126 + have_mach_vm_read=yes
1127 + AC_MSG_RESULT([yes])
1129 + AC_MSG_RESULT([no])
1133 + if test "$have_mach_vm_read" = "yes"; then
1134 + AC_DEFINE([HAVE_MACH_VM_READ], 1, [do we have mach_vm_read?])
1139 + if test -r /proc/$$/mem ; then
1140 + proc_mem_file="mem"
1142 + if test -r /proc/$$/as ; then
1143 + proc_mem_file="as"
1147 + if test -n "$proc_mem_file" ; then
1148 + AC_MSG_CHECKING([for proc mem file])
1151 + #define _GNU_SOURCE
1152 + #define _FILE_OFFSET_BITS 64
1153 + #include <stdint.h>
1154 + #include <unistd.h>
1155 + #include <sys/types.h>
1156 + #include <sys/stat.h>
1157 + #include <fcntl.h>
1158 + #include <stdio.h>
1161 + long v1 = (unsigned int) -1, v2 = 0;
1164 + sprintf(buf, "/proc/%d/$proc_mem_file", getpid());
1165 + fd = open(buf, O_RDONLY);
1169 + if (sizeof(long) != pread(fd, &v2, sizeof(long), (uintptr_t) &v1)) {
1177 + AC_MSG_RESULT([$proc_mem_file])
1180 + AC_MSG_RESULT([no])
1184 + if test -n "$proc_mem_file"; then
1185 + AC_DEFINE_UNQUOTED([PROC_MEM_FILE], "$proc_mem_file", [/proc/pid/mem interface])
1188 + if test "$have_ptrace" = "yes"; then
1189 + FPM_SOURCES="$FPM_SOURCES fpm_trace.c fpm_trace_ptrace.c"
1190 + elif test -n "$proc_mem_file"; then
1191 + FPM_SOURCES="$FPM_SOURCES fpm_trace.c fpm_trace_pread.c"
1192 + elif test "$have_mach_vm_read" = "yes" ; then
1193 + FPM_SOURCES="$FPM_SOURCES fpm_trace.c fpm_trace_mach.c"
1198 +AC_DEFUN([AC_FPM_PRCTL],
1200 + AC_MSG_CHECKING([for prctl])
1202 + AC_TRY_COMPILE([ #include <sys/prctl.h> ], [prctl(0, 0, 0, 0, 0);], [
1203 + AC_DEFINE([HAVE_PRCTL], 1, [do we have prctl?])
1204 + AC_MSG_RESULT([yes])
1206 + AC_MSG_RESULT([no])
1209 diff --git a/sapi/cgi/fpm/conf/php-fpm.conf.in b/sapi/cgi/fpm/conf/php-fpm.conf.in
1210 new file mode 100644
1211 index 0000000..a54b0c2
1213 +++ b/sapi/cgi/fpm/conf/php-fpm.conf.in
1215 +<?xml version="1.0" ?>
1218 + All relative paths in this config are relative to php's install prefix
1220 + <section name="global_options">
1223 + <value name="pid_file">@php_fpm_pid_path@</value>
1226 + <value name="error_log">@php_fpm_log_path@</value>
1229 + <value name="log_level">notice</value>
1231 + When this amount of php processes exited with SIGSEGV or SIGBUS ...
1232 + <value name="emergency_restart_threshold">10</value>
1234 + ... in a less than this interval of time, a graceful restart will be initiated.
1235 + Useful to work around accidental curruptions in accelerator's shared memory.
1236 + <value name="emergency_restart_interval">1m</value>
1238 + Time limit on waiting child's reaction on signals from master
1239 + <value name="process_control_timeout">5s</value>
1241 + Set to 'no' to debug fpm
1242 + <value name="daemonize">yes</value>
1248 + <section name="pool">
1250 + Name of pool. Used in logs and stats.
1251 + <value name="name">default</value>
1253 + Address to accept fastcgi requests on.
1254 + Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
1255 + <value name="listen_address">127.0.0.1:9000</value>
1257 + <value name="listen_options">
1259 + Set listen(2) backlog
1260 + <value name="backlog">-1</value>
1262 + Set permissions for unix socket, if one used.
1263 + In Linux read/write permissions must be set in order to allow connections from web server.
1264 + Many BSD-derrived systems allow connections regardless of permissions.
1265 + <value name="owner"></value>
1266 + <value name="group"></value>
1267 + <value name="mode">0666</value>
1270 + Additional php.ini defines, specific to this pool of workers.
1271 + <value name="php_defines">
1272 + <!-- <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> -->
1273 + <!-- <value name="display_errors">0</value> -->
1276 + Unix user of processes
1277 + <!-- <value name="user">nobody</value> -->
1279 + Unix group of processes
1280 + <!-- <value name="group">@php_fpm_group@</value> -->
1282 + Process manager settings
1285 + Sets style of controling worker process count.
1286 + Valid values are 'static' and 'apache-like'
1287 + <value name="style">static</value>
1289 + Sets the limit on the number of simultaneous requests that will be served.
1290 + Equivalent to Apache MaxClients directive.
1291 + Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
1292 + Used with any pm_style.
1293 + <value name="max_children">5</value>
1295 + Settings group for 'apache-like' pm style
1296 + <value name="apache_like">
1298 + Sets the number of server processes created on startup.
1299 + Used only when 'apache-like' pm_style is selected
1300 + <value name="StartServers">20</value>
1302 + Sets the desired minimum number of idle server processes.
1303 + Used only when 'apache-like' pm_style is selected
1304 + <value name="MinSpareServers">5</value>
1306 + Sets the desired maximum number of idle server processes.
1307 + Used only when 'apache-like' pm_style is selected
1308 + <value name="MaxSpareServers">35</value>
1314 + The timeout (in seconds) for serving a single request after which the worker process will be terminated
1315 + Should be used when 'max_execution_time' ini option does not stop script execution for some reason
1317 + <value name="request_terminate_timeout">0s</value>
1319 + The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
1321 + <value name="request_slowlog_timeout">0s</value>
1323 + The log file for slow requests
1324 + <value name="slowlog">logs/slow.log</value>
1326 + Set open file desc rlimit
1327 + <value name="rlimit_files">1024</value>
1329 + Set max core size rlimit
1330 + <value name="rlimit_core">0</value>
1332 + Chroot to this directory at the start, absolute path
1333 + <value name="chroot"></value>
1335 + Chdir to this directory at the start, absolute path
1336 + <value name="chdir"></value>
1338 + Redirect workers' stdout and stderr into main error log.
1339 + If not set, they will be redirected to /dev/null, according to FastCGI specs
1340 + <value name="catch_workers_output">yes</value>
1342 + How much requests each process should execute before respawn.
1343 + Useful to work around memory leaks in 3rd party libraries.
1344 + For endless request processing please specify 0
1345 + Equivalent to PHP_FCGI_MAX_REQUESTS
1346 + <value name="max_requests">500</value>
1348 + Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
1349 + Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
1350 + Makes sense only with AF_INET listening socket.
1351 + <value name="allowed_clients">127.0.0.1</value>
1353 + Pass environment variables like LD_LIBRARY_PATH
1354 + All $VARIABLEs are taken from current environment
1355 + <value name="environment">
1356 + <value name="HOSTNAME">$HOSTNAME</value>
1357 + <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
1358 + <value name="TMP">/tmp</value>
1359 + <value name="TMPDIR">/tmp</value>
1360 + <value name="TEMP">/tmp</value>
1361 + <value name="OSTYPE">$OSTYPE</value>
1362 + <value name="MACHTYPE">$MACHTYPE</value>
1363 + <value name="MALLOC_CHECK_">2</value>
1371 diff --git a/sapi/cgi/fpm/config.m4 b/sapi/cgi/fpm/config.m4
1372 new file mode 100644
1373 index 0000000..65ea5e2
1375 +++ b/sapi/cgi/fpm/config.m4
1378 +FPM_VERSION="0.5.14"
1380 +PHP_ARG_WITH(fpm-conf, for php-fpm config file path,
1381 +[ --with-fpm-conf=PATH Set the path for php-fpm configuration file [PREFIX/etc/php-fpm.conf]], \$prefix/etc/php-fpm.conf, no)
1383 +PHP_ARG_WITH(fpm-log, for php-fpm log file path,
1384 +[ --with-fpm-log=PATH Set the path for php-fpm log file [PREFIX/logs/php-fpm.log]], \$prefix/logs/php-fpm.log, no)
1386 +PHP_ARG_WITH(fpm-pid, for php-fpm pid file path,
1387 +[ --with-fpm-pid=PATH Set the path for php-fpm pid file [PREFIX/logs/php-fpm.pid]], \$prefix/logs/php-fpm.pid, no)
1389 +FPM_SOURCES="fpm.c \
1393 + fpm_worker_pool.c \
1402 + fpm_process_ctl.c \
1410 +dnl AC_FPM_LIBEVENT
1417 +LIBEVENT_CFLAGS="-I$abs_srcdir/libevent"
1418 +LIBEVENT_LIBS="$abs_builddir/libevent/libevent.a"
1420 +SAPI_EXTRA_DEPS="$LIBEVENT_LIBS"
1422 +FPM_CFLAGS="$LIBEVENT_CFLAGS $LIBXML_CFLAGS $JUDY_CFLAGS"
1424 +dnl FPM_CFLAGS="$FPM_CFLAGS -DJUDYERROR_NOTEST" # for Judy
1425 +FPM_CFLAGS="$FPM_CFLAGS -I$abs_srcdir/sapi/cgi" # for fastcgi.h
1427 +if test "$ICC" = "yes" ; then
1428 + FPM_ADD_CFLAGS="-Wall -wd279,310,869,810,981"
1429 +elif test "$GCC" = "yes" ; then
1430 + FPM_ADD_CFLAGS="-Wall -Wpointer-arith -Wno-unused-parameter -Wunused-variable -Wunused-value -fno-strict-aliasing"
1433 +if test -n "$FPM_WERROR" ; then
1434 + FPM_ADD_CFLAGS="$FPM_ADD_CFLAGS -Werror"
1437 +FPM_CFLAGS="$FPM_ADD_CFLAGS $FPM_CFLAGS"
1439 +PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/sapi/cgi/fpm/Makefile.frag)
1441 +PHP_ADD_SOURCES(sapi/cgi/fpm, $FPM_SOURCES, $FPM_CFLAGS, sapi)
1443 +PHP_ADD_BUILD_DIR(sapi/cgi/fpm)
1445 +install_fpm="install-fpm"
1447 +PHP_CONFIGURE_PART(Configuring libevent)
1449 +test -d "$abs_builddir/libevent" || mkdir -p $abs_builddir/libevent
1451 +dnl this is a bad hack
1453 +chmod +x "$abs_srcdir/libevent/configure" \
1454 + "$abs_srcdir/libevent/depcomp" \
1455 + "$abs_srcdir/libevent/install-sh" \
1456 + "$abs_srcdir/libevent/missing"
1458 +libevent_configure="cd $abs_builddir/libevent ; CFLAGS=\"$CFLAGS $GCC_CFLAGS\" $abs_srcdir/libevent/configure --disable-shared"
1460 +(eval $libevent_configure)
1462 +if test ! -f "$abs_builddir/libevent/Makefile" ; then
1463 + echo "Failed to configure libevent" >&2
1467 +dnl another hack for stealing libevent dependant library list
1469 +LIBEVENT_LIBS="$LIBEVENT_LIBS `echo "@LIBS@" | $abs_builddir/libevent/config.status --file=-:-`"
1471 +SAPI_EXTRA_LIBS="$LIBEVENT_LIBS $LIBXML_LIBS $JUDY_LIBS"
1474 +if test "$prefix" = "NONE" ; then
1475 + fpm_prefix=/usr/local
1477 + fpm_prefix="$prefix"
1480 +if test "$PHP_FPM_CONF" = "\$prefix/etc/php-fpm.conf" ; then
1481 + php_fpm_conf_path="$fpm_prefix/etc/php-fpm.conf"
1483 + php_fpm_conf_path="$PHP_FPM_CONF"
1486 +if test "$PHP_FPM_LOG" = "\$prefix/logs/php-fpm.log" ; then
1487 + php_fpm_log_path="$fpm_prefix/logs/php-fpm.log"
1489 + php_fpm_log_path="$PHP_FPM_LOG"
1492 +if test "$PHP_FPM_PID" = "\$prefix/logs/php-fpm.pid" ; then
1493 + php_fpm_pid_path="$fpm_prefix/logs/php-fpm.pid"
1495 + php_fpm_pid_path="$PHP_FPM_PID"
1499 +if grep nobody /etc/group >/dev/null 2>&1; then
1500 + php_fpm_group=nobody
1502 + if grep nogroup /etc/group >/dev/null 2>&1; then
1503 + php_fpm_group=nogroup
1505 + php_fpm_group=nobody
1509 +PHP_SUBST_OLD(php_fpm_conf_path)
1510 +PHP_SUBST_OLD(php_fpm_log_path)
1511 +PHP_SUBST_OLD(php_fpm_pid_path)
1512 +PHP_SUBST_OLD(php_fpm_group)
1513 +PHP_SUBST_OLD(FPM_VERSION)
1515 +PHP_OUTPUT(sapi/cgi/fpm/fpm_autoconf.h)
1516 +PHP_OUTPUT(sapi/cgi/fpm/php-fpm.conf:sapi/cgi/fpm/conf/php-fpm.conf.in)
1517 +PHP_OUTPUT(sapi/cgi/fpm/php-fpm:sapi/cgi/fpm/init.d/php-fpm.in)
1518 diff --git a/sapi/cgi/fpm/fpm.c b/sapi/cgi/fpm/fpm.c
1519 new file mode 100644
1520 index 0000000..9db2c9b
1522 +++ b/sapi/cgi/fpm/fpm.c
1526 + /* (c) 2007,2008 Andrei Nigmatulin */
1528 +#include "fpm_config.h"
1530 +#include <stdlib.h> /* for exit */
1533 +#include "fpm_children.h"
1534 +#include "fpm_signals.h"
1535 +#include "fpm_env.h"
1536 +#include "fpm_events.h"
1537 +#include "fpm_cleanup.h"
1538 +#include "fpm_php.h"
1539 +#include "fpm_sockets.h"
1540 +#include "fpm_unix.h"
1541 +#include "fpm_process_ctl.h"
1542 +#include "fpm_conf.h"
1543 +#include "fpm_worker_pool.h"
1544 +#include "fpm_stdio.h"
1549 +struct fpm_globals_s fpm_globals;
1551 +int fpm_init(int argc, char **argv, char *config)
1553 + fpm_globals.argc = argc;
1554 + fpm_globals.argv = argv;
1555 + fpm_globals.config = config;
1557 + if (0 > fpm_php_init_main() ||
1558 + 0 > fpm_stdio_init_main() ||
1559 + 0 > fpm_conf_init_main() ||
1560 + 0 > fpm_unix_init_main() ||
1561 + 0 > fpm_env_init_main() ||
1562 + 0 > fpm_signals_init_main() ||
1563 + 0 > fpm_pctl_init_main() ||
1564 + 0 > fpm_children_init_main() ||
1565 + 0 > fpm_sockets_init_main() ||
1566 + 0 > fpm_worker_pool_init_main() ||
1567 + 0 > fpm_event_init_main()) {
1571 + if (0 > fpm_conf_write_pid()) {
1575 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "fpm is running, pid %d", (int) fpm_globals.parent_pid);
1580 +/* children: return listening socket
1581 + parent: never return */
1582 +int fpm_run(int *max_requests)
1584 + struct fpm_worker_pool_s *wp;
1586 + /* create initial children in all pools */
1587 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1590 + is_parent = fpm_children_create_initial(wp);
1597 + /* run event loop forever */
1600 +run_child: /* only workers reach this point */
1602 + fpm_cleanups_run(FPM_CLEANUP_CHILD);
1604 + *max_requests = fpm_globals.max_requests;
1605 + return fpm_globals.listening_socket;
1608 diff --git a/sapi/cgi/fpm/fpm.h b/sapi/cgi/fpm/fpm.h
1609 new file mode 100644
1610 index 0000000..eebd2dc
1612 +++ b/sapi/cgi/fpm/fpm.h
1616 + /* (c) 2007,2008 Andrei Nigmatulin */
1621 +#include <unistd.h>
1623 +int fpm_run(int *max_requests);
1624 +int fpm_init(int argc, char **argv, char *config);
1626 +struct fpm_globals_s {
1631 + int running_children;
1634 + int listening_socket; /* for this child */
1635 + int max_requests; /* for this child */
1639 +extern struct fpm_globals_s fpm_globals;
1644 diff --git a/sapi/cgi/fpm/fpm_arrays.h b/sapi/cgi/fpm/fpm_arrays.h
1645 new file mode 100644
1646 index 0000000..fee6661
1648 +++ b/sapi/cgi/fpm/fpm_arrays.h
1652 + /* (c) 2007,2008 Andrei Nigmatulin */
1654 +#ifndef FPM_ARRAYS_H
1655 +#define FPM_ARRAYS_H 1
1657 +#include <stdlib.h>
1658 +#include <string.h>
1660 +struct fpm_array_s {
1667 +static inline struct fpm_array_s *fpm_array_init(struct fpm_array_s *a, unsigned int sz, unsigned int initial_num)
1669 + void *allocated = 0;
1672 + a = malloc(sizeof(struct fpm_array_s));
1683 + a->data = calloc(sz, initial_num);
1690 + a->allocated = initial_num;
1696 +static inline void *fpm_array_item(struct fpm_array_s *a, unsigned int n)
1700 + ret = (char *) a->data + a->sz * n;
1705 +static inline void *fpm_array_item_last(struct fpm_array_s *a)
1707 + return fpm_array_item(a, a->used - 1);
1710 +static inline int fpm_array_item_remove(struct fpm_array_s *a, unsigned int n)
1714 + if (n < a->used - 1) {
1715 + void *last = fpm_array_item(a, a->used - 1);
1716 + void *to_remove = fpm_array_item(a, n);
1718 + memcpy(to_remove, last, a->sz);
1728 +static inline void *fpm_array_push(struct fpm_array_s *a)
1732 + if (a->used == a->allocated) {
1733 + size_t new_allocated = a->allocated ? a->allocated * 2 : 20;
1734 + void *new_ptr = realloc(a->data, a->sz * new_allocated);
1740 + a->data = new_ptr;
1741 + a->allocated = new_allocated;
1744 + ret = fpm_array_item(a, a->used);
1751 +static inline void fpm_array_free(struct fpm_array_s *a)
1756 + a->used = a->allocated = 0;
1760 diff --git a/sapi/cgi/fpm/fpm_atomic.h b/sapi/cgi/fpm/fpm_atomic.h
1761 new file mode 100644
1762 index 0000000..3334ae0
1764 +++ b/sapi/cgi/fpm/fpm_atomic.h
1768 + /* (c) 2007,2008 Andrei Nigmatulin */
1770 +#ifndef FPM_ATOMIC_H
1771 +#define FPM_ATOMIC_H 1
1773 +#include <stdint.h>
1776 +#if ( __i386__ || __i386 )
1778 +typedef int32_t atomic_int_t;
1779 +typedef uint32_t atomic_uint_t;
1780 +typedef volatile atomic_uint_t atomic_t;
1783 +static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
1785 + __asm__ volatile ( "lock;" "xaddl %0, %1;" :
1786 + "+r" (add) : "m" (*value) : "memory");
1791 +static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
1793 + unsigned char res;
1795 + __asm__ volatile ( "lock;" "cmpxchgl %3, %1;" "sete %0;" :
1796 + "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
1801 +#elif ( __amd64__ || __amd64 )
1803 +typedef int64_t atomic_int_t;
1804 +typedef uint64_t atomic_uint_t;
1805 +typedef volatile atomic_uint_t atomic_t;
1807 +static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
1809 + __asm__ volatile ( "lock;" "xaddq %0, %1;" :
1810 + "+r" (add) : "m" (*value) : "memory");
1815 +static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
1817 + unsigned char res;
1819 + __asm__ volatile ( "lock;" "cmpxchgq %3, %1;" "sete %0;" :
1820 + "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
1827 +#error unsupported processor. please write a patch and send it to me
1831 +static inline int fpm_spinlock(atomic_t *lock, int try_once)
1834 + return atomic_cmp_set(lock, 0, 1) ? 0 : -1;
1839 + if (atomic_cmp_set(lock, 0, 1)) {
1851 diff --git a/sapi/cgi/fpm/fpm_autoconf.h.in b/sapi/cgi/fpm/fpm_autoconf.h.in
1852 new file mode 100644
1853 index 0000000..d00c165
1855 +++ b/sapi/cgi/fpm/fpm_autoconf.h.in
1859 + /* (c) 2007,2008 Andrei Nigmatulin */
1861 +#define PHP_FPM_VERSION "@FPM_VERSION@"
1862 +#define PHP_FPM_CONF_PATH "@php_fpm_conf_path@"
1863 +#define PHP_FPM_LOG_PATH "@php_fpm_log_path@"
1864 +#define PHP_FPM_PID_PATH "@php_fpm_pid_path@"
1866 diff --git a/sapi/cgi/fpm/fpm_children.c b/sapi/cgi/fpm/fpm_children.c
1867 new file mode 100644
1868 index 0000000..f586405
1870 +++ b/sapi/cgi/fpm/fpm_children.c
1874 + /* (c) 2007,2008 Andrei Nigmatulin */
1876 +#include "fpm_config.h"
1878 +#include <sys/types.h>
1879 +#include <sys/wait.h>
1881 +#include <unistd.h>
1882 +#include <string.h>
1886 +#include "fpm_children.h"
1887 +#include "fpm_signals.h"
1888 +#include "fpm_worker_pool.h"
1889 +#include "fpm_sockets.h"
1890 +#include "fpm_process_ctl.h"
1891 +#include "fpm_php.h"
1892 +#include "fpm_conf.h"
1893 +#include "fpm_cleanup.h"
1894 +#include "fpm_events.h"
1895 +#include "fpm_clock.h"
1896 +#include "fpm_stdio.h"
1897 +#include "fpm_unix.h"
1898 +#include "fpm_env.h"
1899 +#include "fpm_shm_slots.h"
1903 +static time_t *last_faults;
1906 +static int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop);
1908 +static void fpm_children_cleanup(int which, void *arg)
1910 + free(last_faults);
1913 +static struct fpm_child_s *fpm_child_alloc()
1915 + struct fpm_child_s *ret;
1917 + ret = malloc(sizeof(struct fpm_child_s));
1919 + if (!ret) return 0;
1921 + memset(ret, 0, sizeof(*ret));
1926 +static void fpm_child_free(struct fpm_child_s *child)
1931 +static void fpm_child_close(struct fpm_child_s *child, int in_event_loop)
1933 + if (child->fd_stdout != -1) {
1934 + if (in_event_loop) {
1935 + fpm_event_fire(&child->ev_stdout);
1937 + if (child->fd_stdout != -1) {
1938 + close(child->fd_stdout);
1942 + if (child->fd_stderr != -1) {
1943 + if (in_event_loop) {
1944 + fpm_event_fire(&child->ev_stderr);
1946 + if (child->fd_stderr != -1) {
1947 + close(child->fd_stderr);
1951 + fpm_child_free(child);
1954 +static void fpm_child_link(struct fpm_child_s *child)
1956 + struct fpm_worker_pool_s *wp = child->wp;
1958 + ++wp->running_children;
1959 + ++fpm_globals.running_children;
1961 + child->next = wp->children;
1962 + if (child->next) child->next->prev = child;
1964 + wp->children = child;
1967 +static void fpm_child_unlink(struct fpm_child_s *child)
1969 + --child->wp->running_children;
1970 + --fpm_globals.running_children;
1972 + if (child->prev) child->prev->next = child->next;
1973 + else child->wp->children = child->next;
1974 + if (child->next) child->next->prev = child->prev;
1978 +static struct fpm_child_s *fpm_child_find(pid_t pid)
1980 + struct fpm_worker_pool_s *wp;
1981 + struct fpm_child_s *child = 0;
1983 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1985 + for (child = wp->children; child; child = child->next) {
1986 + if (child->pid == pid) {
2001 +static void fpm_child_init(struct fpm_worker_pool_s *wp)
2003 + fpm_globals.max_requests = wp->config->max_requests;
2005 + if (0 > fpm_stdio_init_child(wp) ||
2006 + 0 > fpm_unix_init_child(wp) ||
2007 + 0 > fpm_signals_init_child() ||
2008 + 0 > fpm_env_init_child(wp) ||
2009 + 0 > fpm_php_init_child(wp)) {
2011 + zlog(ZLOG_STUFF, ZLOG_ERROR, "child failed to initialize (pool %s)", wp->config->name);
2016 +int fpm_children_free(struct fpm_child_s *child)
2018 + struct fpm_child_s *next;
2020 + for (; child; child = next) {
2021 + next = child->next;
2022 + fpm_child_close(child, 0 /* in_event_loop */);
2028 +void fpm_children_bury()
2032 + struct fpm_child_s *child;
2034 + while ( (pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
2036 + int severity = ZLOG_NOTICE;
2038 + child = fpm_child_find(pid);
2040 + if (WIFEXITED(status)) {
2042 + snprintf(buf, sizeof(buf), "with code %d", WEXITSTATUS(status));
2044 + if (WEXITSTATUS(status) != 0) {
2045 + severity = ZLOG_WARNING;
2049 + else if (WIFSIGNALED(status)) {
2050 + const char *signame = fpm_signal_names[WTERMSIG(status)];
2051 + const char *have_core = WCOREDUMP(status) ? " (core dumped)" : "";
2053 + if (signame == NULL) {
2057 + snprintf(buf, sizeof(buf), "on signal %d %s%s", WTERMSIG(status), signame, have_core);
2059 + if (WTERMSIG(status) != SIGQUIT) { /* possible request loss */
2060 + severity = ZLOG_WARNING;
2063 + else if (WIFSTOPPED(status)) {
2065 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "child %d stopped for tracing", (int) pid);
2067 + if (child && child->tracer) {
2068 + child->tracer(child);
2075 + struct fpm_worker_pool_s *wp = child->wp;
2076 + struct timeval tv1, tv2;
2078 + fpm_child_unlink(child);
2080 + fpm_shm_slots_discard_slot(child);
2082 + fpm_clock_get(&tv1);
2084 + timersub(&tv1, &child->started, &tv2);
2086 + zlog(ZLOG_STUFF, severity, "child %d (pool %s) exited %s after %ld.%06d seconds from start", (int) pid,
2087 + child->wp->config->name, buf, tv2.tv_sec, (int) tv2.tv_usec);
2089 + fpm_child_close(child, 1 /* in event_loop */);
2091 + fpm_pctl_child_exited();
2093 + if (last_faults && (WTERMSIG(status) == SIGSEGV || WTERMSIG(status) == SIGBUS)) {
2094 + time_t now = tv1.tv_sec;
2095 + int restart_condition = 1;
2098 + last_faults[fault++] = now;
2100 + if (fault == fpm_global_config.emergency_restart_threshold) {
2104 + for (i = 0; i < fpm_global_config.emergency_restart_threshold; i++) {
2105 + if (now - last_faults[i] > fpm_global_config.emergency_restart_interval) {
2106 + restart_condition = 0;
2111 + if (restart_condition) {
2113 + zlog(ZLOG_STUFF, ZLOG_WARNING, "failed processes threshold (%d in %d sec) is reached, initiating reload",
2114 + fpm_global_config.emergency_restart_threshold, fpm_global_config.emergency_restart_interval);
2116 + fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
2120 + fpm_children_make(wp, 1 /* in event loop */);
2122 + if (fpm_globals.is_child) {
2127 + zlog(ZLOG_STUFF, ZLOG_ALERT, "oops, unknown child exited %s", buf);
2133 +static struct fpm_child_s *fpm_resources_prepare(struct fpm_worker_pool_s *wp)
2135 + struct fpm_child_s *c;
2137 + c = fpm_child_alloc();
2140 + zlog(ZLOG_STUFF, ZLOG_ERROR, "malloc failed (pool %s)", wp->config->name);
2145 + c->fd_stdout = -1; c->fd_stderr = -1;
2147 + if (0 > fpm_stdio_prepare_pipes(c)) {
2148 + fpm_child_free(c);
2152 + if (0 > fpm_shm_slots_prepare_slot(c)) {
2153 + fpm_stdio_discard_pipes(c);
2154 + fpm_child_free(c);
2161 +static void fpm_resources_discard(struct fpm_child_s *child)
2163 + fpm_shm_slots_discard_slot(child);
2164 + fpm_stdio_discard_pipes(child);
2165 + fpm_child_free(child);
2168 +static void fpm_child_resources_use(struct fpm_child_s *child)
2170 + fpm_shm_slots_child_use_slot(child);
2171 + fpm_stdio_child_use_pipes(child);
2172 + fpm_child_free(child);
2175 +static void fpm_parent_resources_use(struct fpm_child_s *child)
2177 + fpm_shm_slots_parent_use_slot(child);
2178 + fpm_stdio_parent_use_pipes(child);
2179 + fpm_child_link(child);
2182 +static int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop)
2186 + struct fpm_child_s *child;
2188 + while (!enough && fpm_pctl_can_spawn_children() && wp->running_children < wp->config->pm->max_children) {
2190 + child = fpm_resources_prepare(wp);
2202 + fpm_child_resources_use(child);
2203 + fpm_globals.is_child = 1;
2204 + if (in_event_loop) {
2205 + fpm_event_exit_loop();
2207 + fpm_child_init(wp);
2211 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fork() failed");
2214 + fpm_resources_discard(child);
2216 + break; /* dont try any more on error */
2220 + fpm_clock_get(&child->started);
2221 + fpm_parent_resources_use(child);
2223 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "child %d (pool %s) started", (int) pid, wp->config->name);
2228 + return 1; /* we are done */
2231 +int fpm_children_create_initial(struct fpm_worker_pool_s *wp)
2233 + return fpm_children_make(wp, 0 /* not in event loop yet */);
2236 +int fpm_children_init_main()
2238 + if (fpm_global_config.emergency_restart_threshold &&
2239 + fpm_global_config.emergency_restart_interval) {
2241 + last_faults = malloc(sizeof(time_t) * fpm_global_config.emergency_restart_threshold);
2243 + if (!last_faults) {
2247 + memset(last_faults, 0, sizeof(time_t) * fpm_global_config.emergency_restart_threshold);
2250 + if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_children_cleanup, 0)) {
2257 diff --git a/sapi/cgi/fpm/fpm_children.h b/sapi/cgi/fpm/fpm_children.h
2258 new file mode 100644
2259 index 0000000..c85adf9
2261 +++ b/sapi/cgi/fpm/fpm_children.h
2265 + /* (c) 2007,2008 Andrei Nigmatulin */
2267 +#ifndef FPM_CHILDREN_H
2268 +#define FPM_CHILDREN_H 1
2270 +#include <sys/time.h>
2271 +#include <sys/types.h>
2274 +#include "fpm_worker_pool.h"
2276 +int fpm_children_create_initial(struct fpm_worker_pool_s *wp);
2277 +int fpm_children_free(struct fpm_child_s *child);
2278 +void fpm_children_bury();
2279 +int fpm_children_init_main();
2281 +struct fpm_child_s;
2283 +struct fpm_child_s {
2284 + struct fpm_child_s *prev, *next;
2285 + struct timeval started;
2286 + struct fpm_worker_pool_s *wp;
2287 + struct event ev_stdout, ev_stderr;
2289 + int fd_stdout, fd_stderr;
2290 + void (*tracer)(struct fpm_child_s *);
2291 + struct timeval slow_logged;
2296 diff --git a/sapi/cgi/fpm/fpm_cleanup.c b/sapi/cgi/fpm/fpm_cleanup.c
2297 new file mode 100644
2298 index 0000000..b4ef7bb
2300 +++ b/sapi/cgi/fpm/fpm_cleanup.c
2304 + /* (c) 2007,2008 Andrei Nigmatulin */
2306 +#include "fpm_config.h"
2308 +#include <stdlib.h>
2310 +#include "fpm_arrays.h"
2311 +#include "fpm_cleanup.h"
2316 + void (*cleanup)(int, void *);
2320 +static struct fpm_array_s cleanups = { .sz = sizeof(struct cleanup_s) };
2322 +int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *arg)
2324 + struct cleanup_s *c;
2326 + c = fpm_array_push(&cleanups);
2333 + c->cleanup = cleanup;
2339 +void fpm_cleanups_run(int type)
2341 + struct cleanup_s *c = fpm_array_item_last(&cleanups);
2342 + int cl = cleanups.used;
2344 + for ( ; cl--; c--) {
2345 + if (c->type & type) {
2346 + c->cleanup(type, c->arg);
2350 + fpm_array_free(&cleanups);
2353 diff --git a/sapi/cgi/fpm/fpm_cleanup.h b/sapi/cgi/fpm/fpm_cleanup.h
2354 new file mode 100644
2355 index 0000000..4d7cf39
2357 +++ b/sapi/cgi/fpm/fpm_cleanup.h
2361 + /* (c) 2007,2008 Andrei Nigmatulin */
2363 +#ifndef FPM_CLEANUP_H
2364 +#define FPM_CLEANUP_H 1
2366 +int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *);
2367 +void fpm_cleanups_run(int type);
2370 + FPM_CLEANUP_CHILD = (1 << 0),
2371 + FPM_CLEANUP_PARENT_EXIT = (1 << 1),
2372 + FPM_CLEANUP_PARENT_EXIT_MAIN = (1 << 2),
2373 + FPM_CLEANUP_PARENT_EXEC = (1 << 3),
2374 + FPM_CLEANUP_PARENT = (1 << 1) | (1 << 2) | (1 << 3),
2375 + FPM_CLEANUP_ALL = ~0,
2380 diff --git a/sapi/cgi/fpm/fpm_clock.c b/sapi/cgi/fpm/fpm_clock.c
2381 new file mode 100644
2382 index 0000000..2abbce8
2384 +++ b/sapi/cgi/fpm/fpm_clock.c
2388 + /* (c) 2007,2008 Andrei Nigmatulin */
2390 +#include "fpm_config.h"
2392 +#if defined(HAVE_CLOCK_GETTIME)
2393 +#include <time.h> /* for CLOCK_MONOTONIC */
2396 +#include "fpm_clock.h"
2400 +/* posix monotonic clock - preferred source of time */
2401 +#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
2403 +static int monotonic_works;
2405 +int fpm_clock_init()
2407 + struct timespec ts;
2409 + monotonic_works = 0;
2411 + if (0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
2412 + monotonic_works = 1;
2418 +int fpm_clock_get(struct timeval *tv)
2420 + if (monotonic_works) {
2421 + struct timespec ts;
2423 + if (0 > clock_gettime(CLOCK_MONOTONIC, &ts)) {
2424 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "clock_gettime() failed");
2428 + tv->tv_sec = ts.tv_sec;
2429 + tv->tv_usec = ts.tv_nsec / 1000;
2433 + return gettimeofday(tv, 0);
2437 +#elif defined(HAVE_CLOCK_GET_TIME)
2439 +#include <mach/mach.h>
2440 +#include <mach/clock.h>
2441 +#include <mach/mach_error.h>
2443 +static clock_serv_t mach_clock;
2445 +/* this code borrowed from here: http://lists.apple.com/archives/Darwin-development/2002/Mar/msg00746.html */
2446 +/* mach_clock also should be re-initialized in child process after fork */
2447 +int fpm_clock_init()
2449 + kern_return_t ret;
2450 + mach_timespec_t aTime;
2452 + ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &mach_clock);
2454 + if (ret != KERN_SUCCESS) {
2455 + zlog(ZLOG_STUFF, ZLOG_ERROR, "host_get_clock_service() failed: %s", mach_error_string(ret));
2459 + /* test if it works */
2460 + ret = clock_get_time(mach_clock, &aTime);
2462 + if (ret != KERN_SUCCESS) {
2463 + zlog(ZLOG_STUFF, ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
2470 +int fpm_clock_get(struct timeval *tv)
2472 + kern_return_t ret;
2473 + mach_timespec_t aTime;
2475 + ret = clock_get_time(mach_clock, &aTime);
2477 + if (ret != KERN_SUCCESS) {
2478 + zlog(ZLOG_STUFF, ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
2482 + tv->tv_sec = aTime.tv_sec;
2483 + tv->tv_usec = aTime.tv_nsec / 1000;
2488 +#else /* no clock */
2490 +int fpm_clock_init()
2495 +int fpm_clock_get(struct timeval *tv)
2497 + return gettimeofday(tv, 0);
2501 diff --git a/sapi/cgi/fpm/fpm_clock.h b/sapi/cgi/fpm/fpm_clock.h
2502 new file mode 100644
2503 index 0000000..6aab959
2505 +++ b/sapi/cgi/fpm/fpm_clock.h
2509 + /* (c) 2007,2008 Andrei Nigmatulin */
2511 +#ifndef FPM_CLOCK_H
2512 +#define FPM_CLOCK_H 1
2514 +#include <sys/time.h>
2516 +int fpm_clock_init();
2517 +int fpm_clock_get(struct timeval *tv);
2520 diff --git a/sapi/cgi/fpm/fpm_conf.c b/sapi/cgi/fpm/fpm_conf.c
2521 new file mode 100644
2522 index 0000000..adf4f1a
2524 +++ b/sapi/cgi/fpm/fpm_conf.c
2528 + /* (c) 2007,2008 Andrei Nigmatulin */
2530 +#include "fpm_config.h"
2532 +#include <sys/types.h>
2533 +#include <sys/stat.h>
2535 +#include <string.h>
2536 +#include <stdlib.h>
2537 +#include <stddef.h>
2538 +#include <stdint.h>
2540 +#include <unistd.h>
2543 +#include "fpm_conf.h"
2544 +#include "fpm_stdio.h"
2545 +#include "fpm_worker_pool.h"
2546 +#include "fpm_cleanup.h"
2547 +#include "fpm_php.h"
2548 +#include "fpm_sockets.h"
2549 +#include "xml_config.h"
2553 +struct fpm_global_config_s fpm_global_config;
2555 +static void *fpm_global_config_ptr()
2557 + return &fpm_global_config;
2560 +static char *fpm_conf_set_log_level(void **conf, char *name, void *vv, intptr_t offset)
2564 + if (!strcmp(value, "debug")) {
2565 + fpm_globals.log_level = ZLOG_DEBUG;
2567 + else if (!strcmp(value, "notice")) {
2568 + fpm_globals.log_level = ZLOG_NOTICE;
2570 + else if (!strcmp(value, "warn")) {
2571 + fpm_globals.log_level = ZLOG_WARNING;
2573 + else if (!strcmp(value, "error")) {
2574 + fpm_globals.log_level = ZLOG_ERROR;
2576 + else if (!strcmp(value, "alert")) {
2577 + fpm_globals.log_level = ZLOG_ALERT;
2580 + return "invalid value for 'log_level'";
2586 +static struct xml_conf_section xml_section_fpm_global_options = {
2587 + .conf = &fpm_global_config_ptr,
2588 + .path = "/configuration/global_options",
2589 + .parsers = (struct xml_value_parser []) {
2590 + { XML_CONF_SCALAR, "emergency_restart_threshold", &xml_conf_set_slot_integer, offsetof(struct fpm_global_config_s, emergency_restart_threshold) },
2591 + { XML_CONF_SCALAR, "emergency_restart_interval", &xml_conf_set_slot_time, offsetof(struct fpm_global_config_s, emergency_restart_interval) },
2592 + { XML_CONF_SCALAR, "process_control_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_global_config_s, process_control_timeout) },
2593 + { XML_CONF_SCALAR, "daemonize", &xml_conf_set_slot_boolean, offsetof(struct fpm_global_config_s, daemonize) },
2594 + { XML_CONF_SCALAR, "pid_file", &xml_conf_set_slot_string, offsetof(struct fpm_global_config_s, pid_file) },
2595 + { XML_CONF_SCALAR, "error_log", &xml_conf_set_slot_string, offsetof(struct fpm_global_config_s, error_log) },
2596 + { XML_CONF_SCALAR, "log_level", &fpm_conf_set_log_level, 0 },
2601 +static char *fpm_conf_set_pm_style(void **conf, char *name, void *vv, intptr_t offset)
2604 + struct fpm_pm_s *c = *conf;
2606 + if (!strcmp(value, "static")) {
2607 + c->style = PM_STYLE_STATIC;
2609 + else if (!strcmp(value, "apache-like")) {
2610 + c->style = PM_STYLE_APACHE_LIKE;
2613 + return "invalid value for 'style'";
2619 +static char *fpm_conf_set_rlimit_core(void **conf, char *name, void *vv, intptr_t offset)
2622 + struct fpm_worker_pool_config_s *c = *conf;
2624 + if (!strcmp(value, "unlimited")) {
2625 + c->rlimit_core = -1;
2629 + void *subconf = &int_value;
2632 + error = xml_conf_set_slot_integer(&subconf, name, vv, 0);
2634 + if (error) return error;
2636 + if (int_value < 0) return "invalid value for 'rlimit_core'";
2638 + c->rlimit_core = int_value;
2644 +static char *fpm_conf_set_catch_workers_output(void **conf, char *name, void *vv, intptr_t offset)
2646 + struct fpm_worker_pool_config_s *c = *conf;
2648 + void *subconf = &int_value;
2651 + error = xml_conf_set_slot_boolean(&subconf, name, vv, 0);
2653 + if (error) return error;
2655 + c->catch_workers_output = int_value;
2660 +static struct xml_conf_section fpm_conf_set_apache_like_subsection_conf = {
2661 + .path = "apache_like somewhere", /* fixme */
2662 + .parsers = (struct xml_value_parser []) {
2663 + { XML_CONF_SCALAR, "StartServers", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.StartServers) },
2664 + { XML_CONF_SCALAR, "MinSpareServers", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.MinSpareServers) },
2665 + { XML_CONF_SCALAR, "MaxSpareServers", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, options_apache_like.MaxSpareServers) },
2670 +static char *fpm_conf_set_apache_like_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2672 + return xml_conf_parse_section(conf, &fpm_conf_set_apache_like_subsection_conf, xml_node);
2675 +static struct xml_conf_section fpm_conf_set_listen_options_subsection_conf = {
2676 + .path = "listen options somewhere", /* fixme */
2677 + .parsers = (struct xml_value_parser []) {
2678 + { XML_CONF_SCALAR, "backlog", &xml_conf_set_slot_integer, offsetof(struct fpm_listen_options_s, backlog) },
2679 + { XML_CONF_SCALAR, "owner", &xml_conf_set_slot_string, offsetof(struct fpm_listen_options_s, owner) },
2680 + { XML_CONF_SCALAR, "group", &xml_conf_set_slot_string, offsetof(struct fpm_listen_options_s, group) },
2681 + { XML_CONF_SCALAR, "mode", &xml_conf_set_slot_string, offsetof(struct fpm_listen_options_s, mode) },
2686 +static char *fpm_conf_set_listen_options_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2688 + void *subconf = (char *) *conf + offset;
2689 + struct fpm_listen_options_s *lo;
2691 + lo = malloc(sizeof(*lo));
2694 + return "malloc() failed";
2697 + memset(lo, 0, sizeof(*lo));
2701 + * (struct fpm_listen_options_s **) subconf = lo;
2705 + return xml_conf_parse_section(&subconf, &fpm_conf_set_listen_options_subsection_conf, xml_node);
2708 +static struct xml_conf_section fpm_conf_set_pm_subsection_conf = {
2709 + .path = "pm settings somewhere", /* fixme */
2710 + .parsers = (struct xml_value_parser []) {
2711 + { XML_CONF_SCALAR, "style", &fpm_conf_set_pm_style, 0 },
2712 + { XML_CONF_SCALAR, "max_children", &xml_conf_set_slot_integer, offsetof(struct fpm_pm_s, max_children) },
2713 + { XML_CONF_SUBSECTION, "apache_like", &fpm_conf_set_apache_like_subsection, offsetof(struct fpm_pm_s, options_apache_like) },
2718 +static char *fpm_conf_set_pm_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2720 + void *subconf = (char *) *conf + offset;
2721 + struct fpm_pm_s *pm;
2723 + pm = malloc(sizeof(*pm));
2726 + return "fpm_conf_set_pm_subsection(): malloc failed";
2729 + memset(pm, 0, sizeof(*pm));
2731 + * (struct fpm_pm_s **) subconf = pm;
2735 + return xml_conf_parse_section(&subconf, &fpm_conf_set_pm_subsection_conf, xml_node);
2738 +static char *xml_conf_set_slot_key_value_pair(void **conf, char *name, void *vv, intptr_t offset)
2741 + struct key_value_s *kv;
2742 + struct key_value_s ***parent = (struct key_value_s ***) conf;
2744 + kv = malloc(sizeof(*kv));
2747 + return "malloc() failed";
2750 + memset(kv, 0, sizeof(*kv));
2752 + kv->key = strdup(name);
2753 + kv->value = strdup(value);
2755 + if (!kv->key || !kv->value) {
2756 + return "xml_conf_set_slot_key_value_pair(): strdup() failed";
2761 + *parent = &kv->next;
2766 +static struct xml_conf_section fpm_conf_set_key_value_pairs_subsection_conf = {
2767 + .path = "key_value_pairs somewhere", /* fixme */
2768 + .parsers = (struct xml_value_parser []) {
2769 + { XML_CONF_SCALAR, 0, &xml_conf_set_slot_key_value_pair, 0 },
2774 +static char *fpm_conf_set_key_value_pairs_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2776 + void *next_kv = (char *) *conf + offset;
2778 + return xml_conf_parse_section(&next_kv, &fpm_conf_set_key_value_pairs_subsection_conf, xml_node);
2781 +static void *fpm_worker_pool_config_alloc()
2783 + static struct fpm_worker_pool_s *current_wp = 0;
2784 + struct fpm_worker_pool_s *wp;
2786 + wp = fpm_worker_pool_alloc();
2788 + if (!wp) return 0;
2790 + wp->config = malloc(sizeof(struct fpm_worker_pool_config_s));
2792 + if (!wp->config) return 0;
2794 + memset(wp->config, 0, sizeof(struct fpm_worker_pool_config_s));
2796 + if (current_wp) current_wp->next = wp;
2800 + return wp->config;
2803 +int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc)
2805 + struct key_value_s *kv, *kv_next;
2808 + free(wpc->listen_address);
2809 + if (wpc->listen_options) {
2810 + free(wpc->listen_options->owner);
2811 + free(wpc->listen_options->group);
2812 + free(wpc->listen_options->mode);
2813 + free(wpc->listen_options);
2815 + for (kv = wpc->php_defines; kv; kv = kv_next) {
2816 + kv_next = kv->next;
2821 + for (kv = wpc->environment; kv; kv = kv_next) {
2822 + kv_next = kv->next;
2830 + free(wpc->chroot);
2832 + free(wpc->allowed_clients);
2833 + free(wpc->slowlog);
2838 +static struct xml_conf_section xml_section_fpm_worker_pool_config = {
2839 + .conf = &fpm_worker_pool_config_alloc,
2840 + .path = "/configuration/workers/pool",
2841 + .parsers = (struct xml_value_parser []) {
2842 + { XML_CONF_SCALAR, "name", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, name) },
2843 + { XML_CONF_SCALAR, "listen_address", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, listen_address) },
2844 + { XML_CONF_SUBSECTION, "listen_options", &fpm_conf_set_listen_options_subsection, offsetof(struct fpm_worker_pool_config_s, listen_options) },
2845 + { XML_CONF_SUBSECTION, "php_defines", &fpm_conf_set_key_value_pairs_subsection, offsetof(struct fpm_worker_pool_config_s, php_defines) },
2846 + { XML_CONF_SCALAR, "user", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, user) },
2847 + { XML_CONF_SCALAR, "group", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, group) },
2848 + { XML_CONF_SCALAR, "chroot", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, chroot) },
2849 + { XML_CONF_SCALAR, "chdir", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, chdir) },
2850 + { XML_CONF_SCALAR, "allowed_clients", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, allowed_clients) },
2851 + { XML_CONF_SUBSECTION, "environment", &fpm_conf_set_key_value_pairs_subsection, offsetof(struct fpm_worker_pool_config_s, environment) },
2852 + { XML_CONF_SCALAR, "request_terminate_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_worker_pool_config_s, request_terminate_timeout) },
2853 + { XML_CONF_SCALAR, "request_slowlog_timeout", &xml_conf_set_slot_time, offsetof(struct fpm_worker_pool_config_s, request_slowlog_timeout) },
2854 + { XML_CONF_SCALAR, "slowlog", &xml_conf_set_slot_string, offsetof(struct fpm_worker_pool_config_s, slowlog) },
2855 + { XML_CONF_SCALAR, "rlimit_files", &xml_conf_set_slot_integer, offsetof(struct fpm_worker_pool_config_s, rlimit_files) },
2856 + { XML_CONF_SCALAR, "rlimit_core", &fpm_conf_set_rlimit_core, 0 },
2857 + { XML_CONF_SCALAR, "max_requests", &xml_conf_set_slot_integer, offsetof(struct fpm_worker_pool_config_s, max_requests) },
2858 + { XML_CONF_SCALAR, "catch_workers_output", &fpm_conf_set_catch_workers_output, 0 },
2859 + { XML_CONF_SUBSECTION, "pm", &fpm_conf_set_pm_subsection, offsetof(struct fpm_worker_pool_config_s, pm) },
2864 +static struct xml_conf_section *fpm_conf_all_sections[] = {
2865 + &xml_section_fpm_global_options,
2866 + &xml_section_fpm_worker_pool_config,
2870 +static int fpm_evaluate_full_path(char **path)
2872 + if (**path != '/') {
2875 + full_path = malloc(sizeof(PHP_PREFIX) + strlen(*path) + 1);
2877 + if (!full_path) return -1;
2879 + sprintf(full_path, "%s/%s", PHP_PREFIX, *path);
2883 + *path = full_path;
2889 +static int fpm_conf_process_all_pools()
2891 + struct fpm_worker_pool_s *wp;
2893 + if (!fpm_worker_all_pools) {
2894 + zlog(ZLOG_STUFF, ZLOG_ERROR, "at least one pool section must be specified in config file");
2898 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
2900 + if (wp->config->listen_address && *wp->config->listen_address) {
2902 + wp->listen_address_domain = fpm_sockets_domain_from_address(wp->config->listen_address);
2904 + if (wp->listen_address_domain == FPM_AF_UNIX && *wp->config->listen_address != '/') {
2905 + fpm_evaluate_full_path(&wp->config->listen_address);
2911 + wp->is_template = 1;
2915 + if (wp->config->request_slowlog_timeout) {
2917 + if (! (wp->config->slowlog && *wp->config->slowlog)) {
2918 + zlog(ZLOG_STUFF, ZLOG_ERROR, "pool %s: 'slowlog' must be specified for use with 'request_slowlog_timeout'",
2919 + wp->config->name);
2923 + static int warned = 0;
2926 + zlog(ZLOG_STUFF, ZLOG_WARNING, "pool %s: 'request_slowlog_timeout' is not supported on your system",
2927 + wp->config->name);
2931 + wp->config->request_slowlog_timeout = 0;
2935 + if (wp->config->request_slowlog_timeout && wp->config->slowlog && *wp->config->slowlog) {
2938 + fpm_evaluate_full_path(&wp->config->slowlog);
2940 + if (wp->config->request_slowlog_timeout) {
2941 + fd = open(wp->config->slowlog, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
2944 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(%s) failed", wp->config->slowlog);
2955 +int fpm_conf_unlink_pid()
2957 + if (fpm_global_config.pid_file) {
2959 + if (0 > unlink(fpm_global_config.pid_file)) {
2960 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "unlink(\"%s\") failed", fpm_global_config.pid_file);
2969 +int fpm_conf_write_pid()
2973 + if (fpm_global_config.pid_file) {
2977 + unlink(fpm_global_config.pid_file);
2979 + fd = creat(fpm_global_config.pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
2982 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "creat(\"%s\") failed", fpm_global_config.pid_file);
2986 + len = sprintf(buf, "%d", (int) fpm_globals.parent_pid);
2988 + if (len != write(fd, buf, len)) {
2989 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "write() failed");
2999 +static int fpm_conf_post_process()
3001 + if (fpm_global_config.pid_file) {
3002 + fpm_evaluate_full_path(&fpm_global_config.pid_file);
3005 + if (!fpm_global_config.error_log) {
3006 + fpm_global_config.error_log = strdup(PHP_FPM_LOG_PATH);
3009 + fpm_evaluate_full_path(&fpm_global_config.error_log);
3011 + if (0 > fpm_stdio_open_error_log(0)) {
3015 + return fpm_conf_process_all_pools();
3018 +static void fpm_conf_cleanup(int which, void *arg)
3020 + free(fpm_global_config.pid_file);
3021 + free(fpm_global_config.error_log);
3022 + fpm_global_config.pid_file = 0;
3023 + fpm_global_config.error_log = 0;
3026 +int fpm_conf_init_main()
3028 + char *filename = fpm_globals.config;
3031 + if (0 > xml_conf_sections_register(fpm_conf_all_sections)) {
3035 + if (filename == NULL) {
3036 + filename = PHP_FPM_CONF_PATH;
3039 + err = xml_conf_load_file(filename);
3042 + zlog(ZLOG_STUFF, ZLOG_ERROR, "failed to load configuration file: %s", err);
3046 + if (0 > fpm_conf_post_process()) {
3052 + if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_conf_cleanup, 0)) {
3058 diff --git a/sapi/cgi/fpm/fpm_conf.h b/sapi/cgi/fpm/fpm_conf.h
3059 new file mode 100644
3060 index 0000000..4dd011e
3062 +++ b/sapi/cgi/fpm/fpm_conf.h
3066 + /* (c) 2007,2008 Andrei Nigmatulin */
3069 +#define FPM_CONF_H 1
3071 +struct key_value_s;
3073 +struct key_value_s {
3074 + struct key_value_s *next;
3079 +struct fpm_global_config_s {
3080 + int emergency_restart_threshold;
3081 + int emergency_restart_interval;
3082 + int process_control_timeout;
3088 +extern struct fpm_global_config_s fpm_global_config;
3095 + int MinSpareServers;
3096 + int MaxSpareServers;
3097 + } options_apache_like;
3100 +struct fpm_listen_options_s {
3107 +struct fpm_worker_pool_config_s {
3109 + char *listen_address;
3110 + struct fpm_listen_options_s *listen_options;
3111 + struct key_value_s *php_defines;
3116 + char *allowed_clients;
3117 + struct key_value_s *environment;
3118 + struct fpm_pm_s *pm;
3119 + int request_terminate_timeout;
3120 + int request_slowlog_timeout;
3125 + unsigned catch_workers_output:1;
3128 +enum { PM_STYLE_STATIC = 1, PM_STYLE_APACHE_LIKE = 2 };
3130 +int fpm_conf_init_main();
3131 +int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc);
3132 +int fpm_conf_write_pid();
3133 +int fpm_conf_unlink_pid();
3137 diff --git a/sapi/cgi/fpm/fpm_config.h b/sapi/cgi/fpm/fpm_config.h
3138 new file mode 100644
3139 index 0000000..319b200
3141 +++ b/sapi/cgi/fpm/fpm_config.h
3145 + /* (c) 2007,2008 Andrei Nigmatulin */
3147 +#include "php_config.h"
3148 +#include "fpm_autoconf.h"
3151 +/* Solaris does not have it */
3152 +#ifndef INADDR_NONE
3153 +#define INADDR_NONE (-1)
3157 +/* If we're not using GNU C, elide __attribute__ */
3159 +# define __attribute__(x) /*NOTHING*/
3163 +/* Solaris does not have it */
3165 +#define timersub(tvp, uvp, vvp) \
3167 + (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
3168 + (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
3169 + if ((vvp)->tv_usec < 0) { \
3170 + (vvp)->tv_sec--; \
3171 + (vvp)->tv_usec += 1000000; \
3176 +#if defined(HAVE_PTRACE) || defined(PROC_MEM_FILE) || defined(HAVE_MACH_VM_READ)
3177 +#define HAVE_FPM_TRACE 1
3179 +#define HAVE_FPM_TRACE 0
3182 diff --git a/sapi/cgi/fpm/fpm_env.c b/sapi/cgi/fpm/fpm_env.c
3183 new file mode 100644
3184 index 0000000..624a9fe
3186 +++ b/sapi/cgi/fpm/fpm_env.c
3190 + /* (c) 2007,2008 Andrei Nigmatulin */
3192 +#include "fpm_config.h"
3194 +#ifdef HAVE_ALLOCA_H
3195 +#include <alloca.h>
3198 +#include <stdlib.h>
3199 +#include <string.h>
3201 +#include "fpm_env.h"
3204 +#ifndef HAVE_SETENV
3205 +int setenv(char *name, char *value, int overwrite)
3207 + int name_len = strlen(name);
3208 + int value_len = strlen(value);
3209 + char *var = alloca(name_len + 1 + value_len + 1);
3211 + memcpy(var, name, name_len);
3213 + var[name_len] = '=';
3215 + memcpy(var + name_len + 1, value, value_len);
3217 + var[name_len + 1 + value_len] = '\0';
3219 + return putenv(var);
3223 +#ifndef HAVE_CLEARENV
3229 + /* this algo is the only one known to me
3230 + that works well on all systems */
3231 + while (*(envp = environ)) {
3232 + char *eq = strchr(*envp, '=');
3234 + s = strdup(*envp);
3236 + if (eq) s[eq - *envp] = '\0';
3246 +int fpm_env_init_child(struct fpm_worker_pool_s *wp)
3248 + struct key_value_s *kv;
3252 + for (kv = wp->config->environment; kv; kv = kv->next) {
3253 + setenv(kv->key, kv->value, 1);
3257 + setenv("USER", wp->user, 1);
3261 + setenv("HOME", wp->home, 1);
3267 +static int fpm_env_conf_wp(struct fpm_worker_pool_s *wp)
3269 + struct key_value_s *kv;
3271 + kv = wp->config->environment;
3273 + for (kv = wp->config->environment; kv; kv = kv->next) {
3274 + if (*kv->value == '$') {
3275 + char *value = getenv(kv->value + 1);
3277 + if (!value) value = "";
3280 + kv->value = strdup(value);
3283 + /* autodetected values should be removed
3284 + if these vars specified in config */
3285 + if (!strcmp(kv->key, "USER")) {
3290 + if (!strcmp(kv->key, "HOME")) {
3299 +int fpm_env_init_main()
3301 + struct fpm_worker_pool_s *wp;
3303 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
3305 + if (0 > fpm_env_conf_wp(wp)) {
3313 diff --git a/sapi/cgi/fpm/fpm_env.h b/sapi/cgi/fpm/fpm_env.h
3314 new file mode 100644
3315 index 0000000..0f79ed7
3317 +++ b/sapi/cgi/fpm/fpm_env.h
3321 + /* (c) 2007,2008 Andrei Nigmatulin */
3324 +#define FPM_ENV_H 1
3326 +#include "fpm_worker_pool.h"
3328 +int fpm_env_init_child(struct fpm_worker_pool_s *wp);
3329 +int fpm_env_init_main();
3331 +extern char **environ;
3333 +#ifndef HAVE_SETENV
3334 +int setenv(char *name, char *value, int overwrite);
3337 +#ifndef HAVE_CLEARENV
3343 diff --git a/sapi/cgi/fpm/fpm_events.c b/sapi/cgi/fpm/fpm_events.c
3344 new file mode 100644
3345 index 0000000..654e9c8
3347 +++ b/sapi/cgi/fpm/fpm_events.c
3351 + /* (c) 2007,2008 Andrei Nigmatulin */
3353 +#include "fpm_config.h"
3355 +#include <unistd.h>
3357 +#include <stdlib.h> /* for putenv */
3358 +#include <string.h>
3359 +#include <sys/types.h> /* for event.h below */
3363 +#include "fpm_process_ctl.h"
3364 +#include "fpm_events.h"
3365 +#include "fpm_cleanup.h"
3366 +#include "fpm_stdio.h"
3367 +#include "fpm_signals.h"
3368 +#include "fpm_children.h"
3371 +static void fpm_event_cleanup(int which, void *arg)
3373 + event_base_free(0);
3376 +static void fpm_got_signal(int fd, short ev, void *arg)
3384 + res = read(fd, &c, 1);
3385 + } while (res == -1 && errno == EINTR);
3388 + if (res < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
3389 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
3395 + case 'C' : /* SIGCHLD */
3396 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGCHLD");
3397 + fpm_children_bury();
3399 + case 'I' : /* SIGINT */
3400 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGINT");
3401 + fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
3403 + case 'T' : /* SIGTERM */
3404 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGTERM");
3405 + fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
3407 + case 'Q' : /* SIGQUIT */
3408 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGQUIT");
3409 + fpm_pctl(FPM_PCTL_STATE_FINISHING, FPM_PCTL_ACTION_SET);
3411 + case '1' : /* SIGUSR1 */
3412 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGUSR1");
3413 + if (0 == fpm_stdio_open_error_log(1)) {
3414 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "log file re-opened");
3417 + case '2' : /* SIGUSR2 */
3418 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGUSR2");
3419 + fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
3423 + if (fpm_globals.is_child) {
3432 +int fpm_event_init_main()
3436 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: using %s", event_get_method());
3438 + if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_event_cleanup, 0)) {
3445 +int fpm_event_loop()
3447 + static struct event signal_fd_event;
3449 + event_set(&signal_fd_event, fpm_signals_get_fd(), EV_PERSIST | EV_READ, &fpm_got_signal, 0);
3451 + event_add(&signal_fd_event, 0);
3453 + fpm_pctl_heartbeat(-1, 0, 0);
3455 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: entering main loop");
3462 +int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg)
3464 + event_set(ev, fd, EV_PERSIST | EV_READ, callback, arg);
3466 + return event_add(ev, 0);
3469 +int fpm_event_del(struct event *ev)
3471 + return event_del(ev);
3474 +void fpm_event_exit_loop()
3476 + event_loopbreak();
3479 +void fpm_event_fire(struct event *ev)
3481 + (*ev->ev_callback)( (int) ev->ev_fd, (short) ev->ev_res, ev->ev_arg);
3484 diff --git a/sapi/cgi/fpm/fpm_events.h b/sapi/cgi/fpm/fpm_events.h
3485 new file mode 100644
3486 index 0000000..d5a45ce
3488 +++ b/sapi/cgi/fpm/fpm_events.h
3492 + /* (c) 2007,2008 Andrei Nigmatulin */
3494 +#ifndef FPM_EVENTS_H
3495 +#define FPM_EVENTS_H 1
3497 +void fpm_event_exit_loop();
3498 +int fpm_event_loop();
3499 +int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg);
3500 +int fpm_event_del(struct event *ev);
3501 +void fpm_event_fire(struct event *ev);
3502 +int fpm_event_init_main();
3506 diff --git a/sapi/cgi/fpm/fpm_php.c b/sapi/cgi/fpm/fpm_php.c
3507 new file mode 100644
3508 index 0000000..fb46b9b
3510 +++ b/sapi/cgi/fpm/fpm_php.c
3514 + /* (c) 2007,2008 Andrei Nigmatulin */
3516 +#include "fpm_config.h"
3518 +#include <stdlib.h>
3519 +#include <string.h>
3523 +#include "php_main.h"
3524 +#include "php_ini.h"
3525 +#include "ext/standard/dl.h"
3527 +#include "fastcgi.h"
3530 +#include "fpm_php.h"
3531 +#include "fpm_cleanup.h"
3532 +#include "fpm_worker_pool.h"
3534 +static int zend_ini_alter_master(char *name, int name_length, char *new_value, int new_value_length, int stage TSRMLS_DC)
3536 + zend_ini_entry *ini_entry;
3539 + if (zend_hash_find(EG(ini_directives), name, name_length, (void **) &ini_entry) == FAILURE) {
3543 + duplicate = strdup(new_value);
3545 + if (!ini_entry->on_modify
3546 + || ini_entry->on_modify(ini_entry, duplicate, new_value_length,
3547 + ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, stage TSRMLS_CC) == SUCCESS) {
3548 + ini_entry->value = duplicate;
3549 + ini_entry->value_length = new_value_length;
3557 +static void fpm_php_disable(char *value, int (*zend_disable)(char *, uint TSRMLS_DC) TSRMLS_DC)
3559 + char *s = 0, *e = value;
3567 + zend_disable(s, e - s TSRMLS_CC);
3581 + zend_disable(s, e - s TSRMLS_CC);
3585 +static int fpm_php_apply_defines(struct fpm_worker_pool_s *wp)
3588 + struct key_value_s *kv;
3590 + for (kv = wp->config->php_defines; kv; kv = kv->next) {
3591 + char *name = kv->key;
3592 + char *value = kv->value;
3593 + int name_len = strlen(name);
3594 + int value_len = strlen(value);
3596 + if (!strcmp(name, "extension") && *value) {
3599 +#if defined(PHP_VERSION_ID) && (PHP_VERSION_ID >= 50300)
3600 + php_dl(value, MODULE_PERSISTENT, &zv, 1 TSRMLS_CC);
3603 + ZVAL_STRINGL(&filename, value, value_len, 0);
3604 +#if (PHP_MAJOR_VERSION >= 5)
3605 + php_dl(&filename, MODULE_PERSISTENT, &zv, 1 TSRMLS_CC);
3607 + php_dl(&filename, MODULE_PERSISTENT, &zv TSRMLS_CC);
3613 + zend_ini_alter_master(name, name_len + 1, value, value_len, PHP_INI_STAGE_ACTIVATE TSRMLS_CC);
3615 + if (!strcmp(name, "disable_functions") && *value) {
3616 + char *v = strdup(value);
3617 +#if (PHP_MAJOR_VERSION >= 5)
3618 + PG(disable_functions) = v;
3620 + fpm_php_disable(v, zend_disable_function TSRMLS_CC);
3622 + else if (!strcmp(name, "disable_classes") && *value) {
3623 + char *v = strdup(value);
3624 +#if (PHP_MAJOR_VERSION >= 5)
3625 + PG(disable_classes) = v;
3627 + fpm_php_disable(v, zend_disable_class TSRMLS_CC);
3634 +static int fpm_php_set_allowed_clients(struct fpm_worker_pool_s *wp)
3636 + if (wp->listen_address_domain == FPM_AF_INET) {
3637 + fcgi_set_allowed_clients(wp->config->allowed_clients);
3643 +static int fpm_php_set_fcgi_mgmt_vars(struct fpm_worker_pool_s *wp)
3645 + char max_workers[10 + 1]; /* 4294967295 */
3648 + len = sprintf(max_workers, "%u", (unsigned int) wp->config->pm->max_children);
3650 + fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, max_workers, len);
3651 + fcgi_set_mgmt_var("FCGI_MAX_REQS", sizeof("FCGI_MAX_REQS")-1, max_workers, len);
3656 +char *fpm_php_script_filename(TSRMLS_D)
3658 + return SG(request_info).path_translated;
3661 +char *fpm_php_request_method(TSRMLS_D)
3663 + return (char *) SG(request_info).request_method;
3666 +size_t fpm_php_content_length(TSRMLS_D)
3668 + return SG(request_info).content_length;
3671 +static void fpm_php_cleanup(int which, void *arg)
3674 + php_module_shutdown(TSRMLS_C);
3678 +void fpm_php_soft_quit()
3680 + fcgi_set_in_shutdown(1);
3683 +int fpm_php_init_main()
3685 + if (0 > fpm_cleanup_add(FPM_CLEANUP_PARENT, fpm_php_cleanup, 0)) {
3692 +int fpm_php_init_child(struct fpm_worker_pool_s *wp)
3694 + if (0 > fpm_php_apply_defines(wp) ||
3695 + 0 > fpm_php_set_allowed_clients(wp) ||
3696 + 0 > fpm_php_set_fcgi_mgmt_vars(wp)) {
3702 diff --git a/sapi/cgi/fpm/fpm_php.h b/sapi/cgi/fpm/fpm_php.h
3703 new file mode 100644
3704 index 0000000..a05464f
3706 +++ b/sapi/cgi/fpm/fpm_php.h
3710 + /* (c) 2007,2008 Andrei Nigmatulin */
3713 +#define FPM_PHP_H 1
3717 +#include "build-defs.h" /* for PHP_ defines */
3719 +struct fpm_worker_pool_s;
3721 +int fpm_php_init_child(struct fpm_worker_pool_s *wp);
3722 +char *fpm_php_script_filename(TSRMLS_D);
3723 +char *fpm_php_request_method(TSRMLS_D);
3724 +size_t fpm_php_content_length(TSRMLS_D);
3725 +void fpm_php_soft_quit();
3726 +int fpm_php_init_main();
3730 diff --git a/sapi/cgi/fpm/fpm_php_trace.c b/sapi/cgi/fpm/fpm_php_trace.c
3731 new file mode 100644
3732 index 0000000..25a0d71
3734 +++ b/sapi/cgi/fpm/fpm_php_trace.c
3738 + /* (c) 2007,2008 Andrei Nigmatulin */
3740 +#include "fpm_config.h"
3745 +#include "php_main.h"
3748 +#include <stddef.h>
3749 +#include <stdint.h>
3750 +#include <unistd.h>
3751 +#include <sys/time.h>
3752 +#include <sys/types.h>
3755 +#include "fpm_trace.h"
3756 +#include "fpm_php_trace.h"
3757 +#include "fpm_children.h"
3758 +#include "fpm_worker_pool.h"
3759 +#include "fpm_process_ctl.h"
3764 +#define valid_ptr(p) ((p) && 0 == ((p) & (sizeof(long) - 1)))
3766 +#if SIZEOF_LONG == 4
3767 +#define PTR_FMT "08"
3768 +#elif SIZEOF_LONG == 8
3769 +#define PTR_FMT "016"
3773 +static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog TSRMLS_DC)
3775 + int callers_limit = 20;
3776 + pid_t pid = child->pid;
3777 + struct timeval tv;
3778 + static const int buf_size = 1024;
3779 + char buf[buf_size];
3780 + long execute_data;
3783 + gettimeofday(&tv, 0);
3785 + zlog_print_time(&tv, buf, buf_size);
3787 + fprintf(slowlog, "\n%s pid %d (pool %s)\n", buf, (int) pid, child->wp->config->name);
3789 + if (0 > fpm_trace_get_strz(buf, buf_size, (long) &SG(request_info).path_translated)) {
3793 + fprintf(slowlog, "script_filename = %s\n", buf);
3795 + if (0 > fpm_trace_get_long((long) &EG(current_execute_data), &l)) {
3801 + while (execute_data) {
3805 + fprintf(slowlog, "[0x%" PTR_FMT "lx] ", execute_data);
3807 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, function_state.function), &l)) {
3813 + if (valid_ptr(function)) {
3814 + if (0 > fpm_trace_get_strz(buf, buf_size, function + offsetof(zend_function, common.function_name))) {
3818 + fprintf(slowlog, "%s()", buf);
3821 + fprintf(slowlog, "???");
3824 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, op_array), &l)) {
3830 + if (valid_ptr(l)) {
3831 + long op_array = l;
3833 + if (0 > fpm_trace_get_strz(buf, buf_size, op_array + offsetof(zend_op_array, filename))) {
3838 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, opline), &l)) {
3842 + if (valid_ptr(l)) {
3844 + uint *lu = (uint *) &l;
3846 + if (0 > fpm_trace_get_long(opline + offsetof(struct _zend_op, lineno), &l)) {
3853 + fprintf(slowlog, " %s:%u\n", *buf ? buf : "unknown", lineno);
3855 + if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, prev_execute_data), &l)) {
3861 + if (0 == --callers_limit) {
3869 +void fpm_php_trace(struct fpm_child_s *child)
3874 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "about to trace %d", (int) child->pid);
3876 + slowlog = fopen(child->wp->config->slowlog, "a+");
3879 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fopen(%s) failed", child->wp->config->slowlog);
3883 + if (0 > fpm_trace_ready(child->pid)) {
3887 + if (0 > fpm_php_trace_dump(child, slowlog TSRMLS_CC)) {
3888 + fprintf(slowlog, "+++ dump failed\n");
3891 + if (0 > fpm_trace_close(child->pid)) {
3899 + fpm_pctl_kill(child->pid, FPM_PCTL_CONT);
3900 + child->tracer = 0;
3902 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "finished trace of %d", (int) child->pid);
3907 diff --git a/sapi/cgi/fpm/fpm_php_trace.h b/sapi/cgi/fpm/fpm_php_trace.h
3908 new file mode 100644
3909 index 0000000..af5e456
3911 +++ b/sapi/cgi/fpm/fpm_php_trace.h
3915 + /* (c) 2007,2008 Andrei Nigmatulin */
3917 +#ifndef FPM_PHP_TRACE_H
3918 +#define FPM_PHP_TRACE_H 1
3920 +struct fpm_child_s;
3922 +void fpm_php_trace(struct fpm_child_s *);
3926 diff --git a/sapi/cgi/fpm/fpm_process_ctl.c b/sapi/cgi/fpm/fpm_process_ctl.c
3927 new file mode 100644
3928 index 0000000..c9b69eb
3930 +++ b/sapi/cgi/fpm/fpm_process_ctl.c
3934 + /* (c) 2007,2008 Andrei Nigmatulin */
3936 +#include "fpm_config.h"
3938 +#include <sys/types.h>
3939 +#include <signal.h>
3940 +#include <unistd.h>
3941 +#include <stdlib.h>
3944 +#include "fpm_clock.h"
3945 +#include "fpm_children.h"
3946 +#include "fpm_signals.h"
3947 +#include "fpm_events.h"
3948 +#include "fpm_process_ctl.h"
3949 +#include "fpm_cleanup.h"
3950 +#include "fpm_request.h"
3951 +#include "fpm_worker_pool.h"
3955 +static int fpm_state = FPM_PCTL_STATE_NORMAL;
3956 +static int fpm_signal_sent = 0;
3959 +static const char *fpm_state_names[] = {
3960 + [FPM_PCTL_STATE_NORMAL] = "normal",
3961 + [FPM_PCTL_STATE_RELOADING] = "reloading",
3962 + [FPM_PCTL_STATE_TERMINATING] = "terminating",
3963 + [FPM_PCTL_STATE_FINISHING] = "finishing"
3966 +static int saved_argc;
3967 +static char **saved_argv;
3969 +static void fpm_pctl_cleanup(int which, void *arg)
3973 + if (which != FPM_CLEANUP_PARENT_EXEC) {
3975 + for (i = 0; i < saved_argc; i++) {
3976 + free(saved_argv[i]);
3984 +static struct event pctl_event;
3986 +static void fpm_pctl_action(int fd, short which, void *arg)
3988 + evtimer_del(&pctl_event);
3990 + memset(&pctl_event, 0, sizeof(pctl_event));
3992 + fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_TIMEOUT);
3995 +static int fpm_pctl_timeout_set(int sec)
3997 + struct timeval tv = { .tv_sec = sec, .tv_usec = 0 };
3999 + if (evtimer_initialized(&pctl_event)) {
4000 + evtimer_del(&pctl_event);
4003 + evtimer_set(&pctl_event, &fpm_pctl_action, 0);
4005 + evtimer_add(&pctl_event, &tv);
4010 +static void fpm_pctl_exit()
4012 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "exiting, bye-bye!");
4014 + fpm_conf_unlink_pid();
4016 + fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT_MAIN);
4021 +#define optional_arg(c) (saved_argc > c ? ", \"" : ""), (saved_argc > c ? saved_argv[c] : ""), (saved_argc > c ? "\"" : "")
4023 +static void fpm_pctl_exec()
4026 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "reloading: execvp(\"%s\", {\"%s\""
4027 + "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
4028 + "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
4030 + saved_argv[0], saved_argv[0],
4043 + fpm_cleanups_run(FPM_CLEANUP_PARENT_EXEC);
4045 + execvp(saved_argv[0], saved_argv);
4047 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "execvp() failed");
4052 +static void fpm_pctl_action_last()
4054 + switch (fpm_state) {
4056 + case FPM_PCTL_STATE_RELOADING :
4061 + case FPM_PCTL_STATE_FINISHING :
4063 + case FPM_PCTL_STATE_TERMINATING :
4070 +int fpm_pctl_kill(pid_t pid, int how)
4075 + case FPM_PCTL_TERM :
4078 + case FPM_PCTL_STOP :
4081 + case FPM_PCTL_CONT :
4088 + return kill(pid, s);
4091 +static void fpm_pctl_kill_all(int signo)
4093 + struct fpm_worker_pool_s *wp;
4094 + int alive_children = 0;
4096 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
4097 + struct fpm_child_s *child;
4099 + for (child = wp->children; child; child = child->next) {
4101 + int res = kill(child->pid, signo);
4103 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "sending signal %d %s to child %d (pool %s)", signo,
4104 + fpm_signal_names[signo] ? fpm_signal_names[signo] : "",
4105 + (int) child->pid, child->wp->config->name);
4107 + if (res == 0) ++alive_children;
4111 + if (alive_children) {
4112 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "%d %s still alive", alive_children, alive_children == 1 ? "child is" : "children are");
4116 +static void fpm_pctl_action_next()
4120 + if (!fpm_globals.running_children) fpm_pctl_action_last();
4122 + if (fpm_signal_sent == 0) {
4123 + if (fpm_state == FPM_PCTL_STATE_TERMINATING) {
4129 + timeout = fpm_global_config.process_control_timeout;
4132 + if (fpm_signal_sent == SIGQUIT) {
4141 + fpm_pctl_kill_all(sig);
4143 + fpm_signal_sent = sig;
4145 + fpm_pctl_timeout_set(timeout);
4148 +void fpm_pctl(int new_state, int action)
4152 + case FPM_PCTL_ACTION_SET :
4154 + if (fpm_state == new_state) { /* already in progress - just ignore duplicate signal */
4158 + switch (fpm_state) { /* check which states can be overridden */
4160 + case FPM_PCTL_STATE_NORMAL :
4162 + /* 'normal' can be overridden by any other state */
4165 + case FPM_PCTL_STATE_RELOADING :
4167 + /* 'reloading' can be overridden by 'finishing' */
4168 + if (new_state == FPM_PCTL_STATE_FINISHING) break;
4170 + case FPM_PCTL_STATE_FINISHING :
4172 + /* 'reloading' and 'finishing' can be overridden by 'terminating' */
4173 + if (new_state == FPM_PCTL_STATE_TERMINATING) break;
4175 + case FPM_PCTL_STATE_TERMINATING :
4177 + /* nothing can override 'terminating' state */
4178 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "not switching to '%s' state, because already in '%s' state",
4179 + fpm_state_names[new_state], fpm_state_names[fpm_state]);
4184 + fpm_signal_sent = 0;
4185 + fpm_state = new_state;
4187 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "switching to '%s' state", fpm_state_names[fpm_state]);
4191 + case FPM_PCTL_ACTION_TIMEOUT :
4193 + fpm_pctl_action_next();
4197 + case FPM_PCTL_ACTION_LAST_CHILD_EXITED :
4199 + fpm_pctl_action_last();
4206 +int fpm_pctl_can_spawn_children()
4208 + return fpm_state == FPM_PCTL_STATE_NORMAL;
4211 +int fpm_pctl_child_exited()
4213 + if (fpm_state == FPM_PCTL_STATE_NORMAL) return 0;
4215 + if (!fpm_globals.running_children) {
4216 + fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_LAST_CHILD_EXITED);
4222 +int fpm_pctl_init_main()
4226 + saved_argc = fpm_globals.argc;
4228 + saved_argv = malloc(sizeof(char *) * (saved_argc + 1));
4230 + if (!saved_argv) {
4234 + for (i = 0; i < saved_argc; i++) {
4235 + saved_argv[i] = strdup(fpm_globals.argv[i]);
4237 + if (!saved_argv[i]) {
4242 + saved_argv[i] = 0;
4244 + if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_pctl_cleanup, 0)) {
4251 +static void fpm_pctl_check_request_timeout(struct timeval *now)
4253 + struct fpm_worker_pool_s *wp;
4255 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
4256 + int terminate_timeout = wp->config->request_terminate_timeout;
4257 + int slowlog_timeout = wp->config->request_slowlog_timeout;
4258 + struct fpm_child_s *child;
4260 + if (terminate_timeout || slowlog_timeout) {
4261 + for (child = wp->children; child; child = child->next) {
4262 + fpm_request_check_timed_out(child, now, terminate_timeout, slowlog_timeout);
4269 +void fpm_pctl_heartbeat(int fd, short which, void *arg)
4271 + static struct event heartbeat;
4272 + struct timeval tv = { .tv_sec = 0, .tv_usec = 130000 };
4273 + struct timeval now;
4275 + if (which == EV_TIMEOUT) {
4276 + evtimer_del(&heartbeat);
4277 + fpm_clock_get(&now);
4278 + fpm_pctl_check_request_timeout(&now);
4281 + evtimer_set(&heartbeat, &fpm_pctl_heartbeat, 0);
4283 + evtimer_add(&heartbeat, &tv);
4286 diff --git a/sapi/cgi/fpm/fpm_process_ctl.h b/sapi/cgi/fpm/fpm_process_ctl.h
4287 new file mode 100644
4288 index 0000000..8424f10
4290 +++ b/sapi/cgi/fpm/fpm_process_ctl.h
4294 + /* (c) 2007,2008 Andrei Nigmatulin */
4296 +#ifndef FPM_PROCESS_CTL_H
4297 +#define FPM_PROCESS_CTL_H 1
4299 +struct fpm_child_s;
4301 +void fpm_pctl(int new_state, int action);
4302 +int fpm_pctl_can_spawn_children();
4303 +int fpm_pctl_kill(pid_t pid, int how);
4304 +void fpm_pctl_heartbeat(int fd, short which, void *arg);
4305 +int fpm_pctl_child_exited();
4306 +int fpm_pctl_init_main();
4310 + FPM_PCTL_STATE_UNSPECIFIED,
4311 + FPM_PCTL_STATE_NORMAL,
4312 + FPM_PCTL_STATE_RELOADING,
4313 + FPM_PCTL_STATE_TERMINATING,
4314 + FPM_PCTL_STATE_FINISHING
4318 + FPM_PCTL_ACTION_SET,
4319 + FPM_PCTL_ACTION_TIMEOUT,
4320 + FPM_PCTL_ACTION_LAST_CHILD_EXITED
4331 diff --git a/sapi/cgi/fpm/fpm_request.c b/sapi/cgi/fpm/fpm_request.c
4332 new file mode 100644
4333 index 0000000..3faf77d
4335 +++ b/sapi/cgi/fpm/fpm_request.c
4339 + /* (c) 2007,2008 Andrei Nigmatulin */
4341 +#include "fpm_config.h"
4343 +#include "fpm_php.h"
4344 +#include "fpm_str.h"
4345 +#include "fpm_clock.h"
4346 +#include "fpm_conf.h"
4347 +#include "fpm_trace.h"
4348 +#include "fpm_php_trace.h"
4349 +#include "fpm_process_ctl.h"
4350 +#include "fpm_children.h"
4351 +#include "fpm_shm_slots.h"
4352 +#include "fpm_request.h"
4356 +void fpm_request_accepting()
4358 + struct fpm_shm_slot_s *slot;
4360 + slot = fpm_shm_slots_acquire(0, 0);
4362 + slot->request_stage = FPM_REQUEST_ACCEPTING;
4364 + fpm_clock_get(&slot->tv);
4365 + memset(slot->request_method, 0, sizeof(slot->request_method));
4366 + slot->content_length = 0;
4367 + memset(slot->script_filename, 0, sizeof(slot->script_filename));
4369 + fpm_shm_slots_release(slot);
4372 +void fpm_request_reading_headers()
4374 + struct fpm_shm_slot_s *slot;
4376 + slot = fpm_shm_slots_acquire(0, 0);
4378 + slot->request_stage = FPM_REQUEST_READING_HEADERS;
4380 + fpm_clock_get(&slot->tv);
4381 + slot->accepted = slot->tv;
4383 + fpm_shm_slots_release(slot);
4386 +void fpm_request_info()
4389 + struct fpm_shm_slot_s *slot;
4390 + char *request_method = fpm_php_request_method(TSRMLS_C);
4391 + char *script_filename = fpm_php_script_filename(TSRMLS_C);
4393 + slot = fpm_shm_slots_acquire(0, 0);
4395 + slot->request_stage = FPM_REQUEST_INFO;
4397 + fpm_clock_get(&slot->tv);
4399 + if (request_method) {
4400 + cpystrn(slot->request_method, request_method, sizeof(slot->request_method));
4403 + slot->content_length = fpm_php_content_length(TSRMLS_C);
4405 + /* if cgi.fix_pathinfo is set to "1" and script cannot be found (404)
4406 + the sapi_globals.request_info.path_translated is set to NULL */
4407 + if (script_filename) {
4408 + cpystrn(slot->script_filename, script_filename, sizeof(slot->script_filename));
4411 + fpm_shm_slots_release(slot);
4414 +void fpm_request_executing()
4416 + struct fpm_shm_slot_s *slot;
4418 + slot = fpm_shm_slots_acquire(0, 0);
4420 + slot->request_stage = FPM_REQUEST_EXECUTING;
4422 + fpm_clock_get(&slot->tv);
4424 + fpm_shm_slots_release(slot);
4427 +void fpm_request_finished()
4429 + struct fpm_shm_slot_s *slot;
4431 + slot = fpm_shm_slots_acquire(0, 0);
4433 + slot->request_stage = FPM_REQUEST_FINISHED;
4435 + fpm_clock_get(&slot->tv);
4436 + memset(&slot->accepted, 0, sizeof(slot->accepted));
4438 + fpm_shm_slots_release(slot);
4441 +void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *now, int terminate_timeout, int slowlog_timeout)
4443 + struct fpm_shm_slot_s *slot;
4444 + struct fpm_shm_slot_s slot_c;
4446 + slot = fpm_shm_slot(child);
4448 + if (!fpm_shm_slots_acquire(slot, 1)) {
4454 + fpm_shm_slots_release(slot);
4457 + if (child->slow_logged.tv_sec) {
4458 + if (child->slow_logged.tv_sec != slot_c.accepted.tv_sec || child->slow_logged.tv_usec != slot_c.accepted.tv_usec) {
4459 + child->slow_logged.tv_sec = 0;
4460 + child->slow_logged.tv_usec = 0;
4465 + if (slot_c.request_stage > FPM_REQUEST_ACCEPTING && slot_c.request_stage < FPM_REQUEST_FINISHED) {
4466 + char purified_script_filename[sizeof(slot_c.script_filename)];
4467 + struct timeval tv;
4469 + timersub(now, &slot_c.accepted, &tv);
4472 + if (child->slow_logged.tv_sec == 0 && slowlog_timeout &&
4473 + slot_c.request_stage == FPM_REQUEST_EXECUTING && tv.tv_sec >= slowlog_timeout) {
4475 + str_purify_filename(purified_script_filename, slot_c.script_filename, sizeof(slot_c.script_filename));
4477 + child->slow_logged = slot_c.accepted;
4478 + child->tracer = fpm_php_trace;
4480 + fpm_trace_signal(child->pid);
4482 + zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d, script '%s' (pool %s) executing too slow (%d.%06d sec), logging",
4483 + (int) child->pid, purified_script_filename, child->wp->config->name, (int) tv.tv_sec, (int) tv.tv_usec);
4488 + if (terminate_timeout && tv.tv_sec >= terminate_timeout) {
4490 + str_purify_filename(purified_script_filename, slot_c.script_filename, sizeof(slot_c.script_filename));
4492 + fpm_pctl_kill(child->pid, FPM_PCTL_TERM);
4494 + zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d, script '%s' (pool %s) execution timed out (%d.%06d sec), terminating",
4495 + (int) child->pid, purified_script_filename, child->wp->config->name, (int) tv.tv_sec, (int) tv.tv_usec);
4501 diff --git a/sapi/cgi/fpm/fpm_request.h b/sapi/cgi/fpm/fpm_request.h
4502 new file mode 100644
4503 index 0000000..d768db6
4505 +++ b/sapi/cgi/fpm/fpm_request.h
4509 + /* (c) 2007,2008 Andrei Nigmatulin */
4511 +#ifndef FPM_REQUEST_H
4512 +#define FPM_REQUEST_H 1
4514 +void fpm_request_accepting(); /* hanging in accept() */
4515 +void fpm_request_reading_headers(); /* start reading fastcgi request from very first byte */
4516 +void fpm_request_info(); /* not a stage really but a point in the php code, where all request params have become known to sapi */
4517 +void fpm_request_executing(); /* the script is executing */
4518 +void fpm_request_finished(); /* request processed: script response have been sent to web server */
4520 +struct fpm_child_s;
4523 +void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *tv, int terminate_timeout, int slowlog_timeout);
4525 +enum fpm_request_stage_e {
4526 + FPM_REQUEST_ACCEPTING = 1,
4527 + FPM_REQUEST_READING_HEADERS,
4529 + FPM_REQUEST_EXECUTING,
4530 + FPM_REQUEST_FINISHED
4534 diff --git a/sapi/cgi/fpm/fpm_shm.c b/sapi/cgi/fpm/fpm_shm.c
4535 new file mode 100644
4536 index 0000000..21c3d75
4538 +++ b/sapi/cgi/fpm/fpm_shm.c
4542 + /* (c) 2007,2008 Andrei Nigmatulin */
4544 +#include "fpm_config.h"
4546 +#include <unistd.h>
4547 +#include <sys/mman.h>
4548 +#include <stdlib.h>
4550 +#include "fpm_shm.h"
4554 +/* MAP_ANON is depricated, but not in macosx */
4555 +#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
4556 +#define MAP_ANONYMOUS MAP_ANON
4560 +struct fpm_shm_s *fpm_shm_alloc(size_t sz)
4562 + struct fpm_shm_s *shm;
4564 + shm = malloc(sizeof(*shm));
4570 + shm->mem = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
4573 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "mmap(MAP_ANONYMOUS | MAP_SHARED) failed");
4584 +static void fpm_shm_free(struct fpm_shm_s *shm, int do_unmap)
4587 + munmap(shm->mem, shm->sz);
4593 +void fpm_shm_free_list(struct fpm_shm_s *shm, void *mem)
4595 + struct fpm_shm_s *next;
4597 + for (; shm; shm = next) {
4600 + fpm_shm_free(shm, mem != shm->mem);
4604 +void *fpm_shm_alloc_chunk(struct fpm_shm_s **head, size_t sz, void **mem)
4606 + size_t pagesize = getpagesize();
4607 + static const size_t cache_line_size = 16;
4608 + size_t aligned_sz;
4609 + struct fpm_shm_s *shm;
4612 + sz = (sz + cache_line_size - 1) & -cache_line_size;
4616 + if (0 == shm || shm->sz - shm->used < sz) {
4617 + /* allocate one more shm segment */
4619 + aligned_sz = (sz + pagesize - 1) & -pagesize;
4621 + shm = fpm_shm_alloc(aligned_sz);
4627 + shm->next = *head;
4628 + if (shm->next) shm->next->prev = shm;
4634 + ret = (char *) shm->mem + shm->used;
4640 diff --git a/sapi/cgi/fpm/fpm_shm.h b/sapi/cgi/fpm/fpm_shm.h
4641 new file mode 100644
4642 index 0000000..f3f0be0
4644 +++ b/sapi/cgi/fpm/fpm_shm.h
4648 + /* (c) 2007,2008 Andrei Nigmatulin */
4651 +#define FPM_SHM_H 1
4656 + struct fpm_shm_s *prev, *next;
4662 +struct fpm_shm_s *fpm_shm_alloc(size_t sz);
4663 +void fpm_shm_free_list(struct fpm_shm_s *, void *);
4664 +void *fpm_shm_alloc_chunk(struct fpm_shm_s **head, size_t sz, void **mem);
4668 diff --git a/sapi/cgi/fpm/fpm_shm_slots.c b/sapi/cgi/fpm/fpm_shm_slots.c
4669 new file mode 100644
4670 index 0000000..efa3707
4672 +++ b/sapi/cgi/fpm/fpm_shm_slots.c
4676 + /* (c) 2007,2008 Andrei Nigmatulin */
4678 +#include "fpm_config.h"
4680 +#include "fpm_atomic.h"
4681 +#include "fpm_worker_pool.h"
4682 +#include "fpm_children.h"
4683 +#include "fpm_shm.h"
4684 +#include "fpm_shm_slots.h"
4687 +static void *shm_mem;
4688 +static struct fpm_shm_slot_s *shm_slot;
4690 +int fpm_shm_slots_prepare_slot(struct fpm_child_s *child)
4692 + struct fpm_worker_pool_s *wp = child->wp;
4693 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4695 + child->shm_slot_i = wp->slots_used.used;
4697 + shm_slot_ptr = fpm_array_push(&wp->slots_used);
4699 + if (0 == shm_slot_ptr) {
4703 + if (0 == wp->slots_free.used) {
4704 + shm_slot_ptr->shm_slot = fpm_shm_alloc_chunk(&wp->shm_list, sizeof(struct fpm_shm_slot_s), &shm_slot_ptr->mem);
4706 + if (!shm_slot_ptr->shm_slot) {
4711 + *shm_slot_ptr = *(struct fpm_shm_slot_ptr_s *) fpm_array_item_last(&wp->slots_free);
4713 + --wp->slots_free.used;
4716 + memset(shm_slot_ptr->shm_slot, 0, sizeof(struct fpm_shm_slot_s));
4718 + shm_slot_ptr->child = child;
4723 +void fpm_shm_slots_discard_slot(struct fpm_child_s *child)
4725 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4726 + struct fpm_worker_pool_s *wp = child->wp;
4729 + shm_slot_ptr = fpm_array_push(&wp->slots_free);
4731 + if (shm_slot_ptr) {
4733 + struct fpm_shm_slot_ptr_s *shm_slot_ptr_used;
4735 + shm_slot_ptr_used = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4737 + *shm_slot_ptr = *shm_slot_ptr_used;
4739 + shm_slot_ptr->child = 0;
4743 + n = fpm_array_item_remove(&wp->slots_used, child->shm_slot_i);
4746 + shm_slot_ptr = fpm_array_item(&wp->slots_used, n);
4748 + shm_slot_ptr->child->shm_slot_i = n;
4752 +void fpm_shm_slots_child_use_slot(struct fpm_child_s *child)
4754 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4755 + struct fpm_worker_pool_s *wp = child->wp;
4757 + shm_slot_ptr = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4759 + shm_slot = shm_slot_ptr->shm_slot;
4760 + shm_mem = shm_slot_ptr->mem;
4763 +void fpm_shm_slots_parent_use_slot(struct fpm_child_s *child)
4765 + /* nothing to do */
4768 +void *fpm_shm_slots_mem()
4773 +struct fpm_shm_slot_s *fpm_shm_slot(struct fpm_child_s *child)
4775 + struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4776 + struct fpm_worker_pool_s *wp = child->wp;
4778 + shm_slot_ptr = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4780 + return shm_slot_ptr->shm_slot;
4783 +struct fpm_shm_slot_s *fpm_shm_slots_acquire(struct fpm_shm_slot_s *s, int nohang)
4789 + if (0 > fpm_spinlock(&s->lock, nohang)) {
4796 +void fpm_shm_slots_release(struct fpm_shm_slot_s *s)
4801 diff --git a/sapi/cgi/fpm/fpm_shm_slots.h b/sapi/cgi/fpm/fpm_shm_slots.h
4802 new file mode 100644
4803 index 0000000..4596c6f
4805 +++ b/sapi/cgi/fpm/fpm_shm_slots.h
4809 + /* (c) 2007,2008 Andrei Nigmatulin */
4811 +#ifndef FPM_SHM_SLOTS_H
4812 +#define FPM_SHM_SLOTS_H 1
4814 +#include "fpm_atomic.h"
4815 +#include "fpm_worker_pool.h"
4816 +#include "fpm_request.h"
4818 +struct fpm_child_s;
4820 +struct fpm_shm_slot_s {
4825 + enum fpm_request_stage_e request_stage;
4826 + struct timeval accepted;
4827 + struct timeval tv;
4828 + char request_method[16];
4829 + size_t content_length; /* used with POST only */
4830 + char script_filename[256];
4833 +struct fpm_shm_slot_ptr_s {
4835 + struct fpm_shm_slot_s *shm_slot;
4836 + struct fpm_child_s *child;
4839 +int fpm_shm_slots_prepare_slot(struct fpm_child_s *child);
4840 +void fpm_shm_slots_discard_slot(struct fpm_child_s *child);
4841 +void fpm_shm_slots_child_use_slot(struct fpm_child_s *child);
4842 +void fpm_shm_slots_parent_use_slot(struct fpm_child_s *child);
4843 +void *fpm_shm_slots_mem();
4844 +struct fpm_shm_slot_s *fpm_shm_slot(struct fpm_child_s *child);
4845 +struct fpm_shm_slot_s *fpm_shm_slots_acquire(struct fpm_shm_slot_s *, int nohang);
4846 +void fpm_shm_slots_release(struct fpm_shm_slot_s *);
4850 diff --git a/sapi/cgi/fpm/fpm_signals.c b/sapi/cgi/fpm/fpm_signals.c
4851 new file mode 100644
4852 index 0000000..2cf878d
4854 +++ b/sapi/cgi/fpm/fpm_signals.c
4858 + /* (c) 2007,2008 Andrei Nigmatulin */
4860 +#include "fpm_config.h"
4862 +#include <signal.h>
4864 +#include <sys/types.h>
4865 +#include <sys/socket.h>
4866 +#include <stdlib.h>
4867 +#include <string.h>
4869 +#include <unistd.h>
4873 +#include "fpm_signals.h"
4874 +#include "fpm_sockets.h"
4875 +#include "fpm_php.h"
4880 +const char *fpm_signal_names[NSIG + 1] = {
4882 + [SIGHUP] = "SIGHUP",
4885 + [SIGINT] = "SIGINT",
4888 + [SIGQUIT] = "SIGQUIT",
4891 + [SIGILL] = "SIGILL",
4894 + [SIGTRAP] = "SIGTRAP",
4897 + [SIGABRT] = "SIGABRT",
4900 + [SIGEMT] = "SIGEMT",
4903 + [SIGBUS] = "SIGBUS",
4906 + [SIGFPE] = "SIGFPE",
4909 + [SIGKILL] = "SIGKILL",
4912 + [SIGUSR1] = "SIGUSR1",
4915 + [SIGSEGV] = "SIGSEGV",
4918 + [SIGUSR2] = "SIGUSR2",
4921 + [SIGPIPE] = "SIGPIPE",
4924 + [SIGALRM] = "SIGALRM",
4927 + [SIGTERM] = "SIGTERM",
4930 + [SIGCHLD] = "SIGCHLD",
4933 + [SIGCONT] = "SIGCONT",
4936 + [SIGSTOP] = "SIGSTOP",
4939 + [SIGTSTP] = "SIGTSTP",
4942 + [SIGTTIN] = "SIGTTIN",
4945 + [SIGTTOU] = "SIGTTOU",
4948 + [SIGURG] = "SIGURG",
4951 + [SIGXCPU] = "SIGXCPU",
4954 + [SIGXFSZ] = "SIGXFSZ",
4957 + [SIGVTALRM] = "SIGVTALRM",
4960 + [SIGPROF] = "SIGPROF",
4963 + [SIGWINCH] = "SIGWINCH",
4966 + [SIGINFO] = "SIGINFO",
4969 + [SIGIO] = "SIGIO",
4972 + [SIGPWR] = "SIGPWR",
4975 + [SIGSYS] = "SIGSYS",
4978 + [SIGWAITING] = "SIGWAITING",
4981 + [SIGLWP] = "SIGLWP",
4984 + [SIGFREEZE] = "SIGFREEZE",
4987 + [SIGTHAW] = "SIGTHAW",
4990 + [SIGCANCEL] = "SIGCANCEL",
4993 + [SIGLOST] = "SIGLOST",
4997 +static void sig_soft_quit(int signo)
4999 + int saved_errno = errno;
5001 + /* closing fastcgi listening socket will force fcgi_accept() exit immediately */
5003 + socket(AF_UNIX, SOCK_STREAM, 0);
5005 + fpm_php_soft_quit();
5007 + errno = saved_errno;
5010 +static void sig_handler(int signo)
5012 + static const char sig_chars[NSIG + 1] = {
5023 + if (fpm_globals.parent_pid != getpid()) {
5024 + /* prevent a signal race condition when child process
5025 + have not set up it's own signal handler yet */
5029 + saved_errno = errno;
5031 + s = sig_chars[signo];
5033 + write(sp[1], &s, sizeof(s));
5035 + errno = saved_errno;
5038 +int fpm_signals_init_main()
5040 + struct sigaction act;
5042 + if (0 > socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) {
5043 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "socketpair() failed");
5047 + if (0 > fd_set_blocked(sp[0], 0) || 0 > fd_set_blocked(sp[1], 0)) {
5048 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fd_set_blocked() failed");
5052 + if (0 > fcntl(sp[0], F_SETFD, FD_CLOEXEC) || 0 > fcntl(sp[1], F_SETFD, FD_CLOEXEC)) {
5053 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fcntl(F_SETFD, FD_CLOEXEC) failed");
5057 + memset(&act, 0, sizeof(act));
5058 + act.sa_handler = sig_handler;
5059 + sigfillset(&act.sa_mask);
5061 + if (0 > sigaction(SIGTERM, &act, 0) ||
5062 + 0 > sigaction(SIGINT, &act, 0) ||
5063 + 0 > sigaction(SIGUSR1, &act, 0) ||
5064 + 0 > sigaction(SIGUSR2, &act, 0) ||
5065 + 0 > sigaction(SIGCHLD, &act, 0) ||
5066 + 0 > sigaction(SIGQUIT, &act, 0)) {
5068 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "sigaction() failed");
5075 +int fpm_signals_init_child()
5077 + struct sigaction act, act_dfl;
5079 + memset(&act, 0, sizeof(act));
5080 + memset(&act_dfl, 0, sizeof(act_dfl));
5082 + act.sa_handler = &sig_soft_quit;
5083 + act.sa_flags |= SA_RESTART;
5085 + act_dfl.sa_handler = SIG_DFL;
5090 + if (0 > sigaction(SIGTERM, &act_dfl, 0) ||
5091 + 0 > sigaction(SIGINT, &act_dfl, 0) ||
5092 + 0 > sigaction(SIGUSR1, &act_dfl, 0) ||
5093 + 0 > sigaction(SIGUSR2, &act_dfl, 0) ||
5094 + 0 > sigaction(SIGCHLD, &act_dfl, 0) ||
5095 + 0 > sigaction(SIGQUIT, &act, 0)) {
5097 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "sigaction() failed");
5104 +int fpm_signals_get_fd()
5108 diff --git a/sapi/cgi/fpm/fpm_signals.h b/sapi/cgi/fpm/fpm_signals.h
5109 new file mode 100644
5110 index 0000000..eb80fae
5112 +++ b/sapi/cgi/fpm/fpm_signals.h
5116 + /* (c) 2007,2008 Andrei Nigmatulin */
5118 +#ifndef FPM_SIGNALS_H
5119 +#define FPM_SIGNALS_H 1
5121 +#include <signal.h>
5123 +int fpm_signals_init_main();
5124 +int fpm_signals_init_child();
5125 +int fpm_signals_get_fd();
5127 +extern const char *fpm_signal_names[NSIG + 1];
5130 diff --git a/sapi/cgi/fpm/fpm_sockets.c b/sapi/cgi/fpm/fpm_sockets.c
5131 new file mode 100644
5132 index 0000000..5acb559
5134 +++ b/sapi/cgi/fpm/fpm_sockets.c
5138 + /* (c) 2007,2008 Andrei Nigmatulin */
5140 +#include "fpm_config.h"
5142 +#ifdef HAVE_ALLOCA_H
5143 +#include <alloca.h>
5145 +#include <sys/types.h>
5146 +#include <sys/stat.h> /* for chmod(2) */
5147 +#include <sys/socket.h>
5148 +#include <netinet/in.h>
5149 +#include <arpa/inet.h>
5150 +#include <sys/un.h>
5153 +#include <stdlib.h>
5154 +#include <string.h>
5156 +#include <unistd.h>
5159 +#include "fpm_arrays.h"
5160 +#include "fpm_sockets.h"
5161 +#include "fpm_worker_pool.h"
5162 +#include "fpm_unix.h"
5163 +#include "fpm_str.h"
5164 +#include "fpm_env.h"
5165 +#include "fpm_cleanup.h"
5167 +struct listening_socket_s {
5174 +static struct fpm_array_s sockets_list;
5176 +static int fpm_sockets_resolve_af_inet(char *node, char *service, struct sockaddr_in *addr)
5178 + struct addrinfo *res;
5179 + struct addrinfo hints;
5182 + memset(&hints, 0, sizeof(hints));
5184 + hints.ai_family = AF_INET;
5186 + ret = getaddrinfo(node, service, &hints, &res);
5189 + zlog(ZLOG_STUFF, ZLOG_ERROR, "can't resolve hostname '%s%s%s': getaddrinfo said: %s%s%s\n",
5190 + node, service ? ":" : "", service ? service : "",
5191 + gai_strerror(ret), ret == EAI_SYSTEM ? ", system error: " : "", ret == EAI_SYSTEM ? strerror(errno) : "");
5195 + *addr = *(struct sockaddr_in *) res->ai_addr;
5197 + freeaddrinfo(res);
5202 +enum { FPM_GET_USE_SOCKET = 1, FPM_STORE_SOCKET = 2, FPM_STORE_USE_SOCKET = 3 };
5204 +static void fpm_sockets_cleanup(int which, void *arg)
5207 + char *env_value = 0;
5209 + struct listening_socket_s *ls = sockets_list.data;
5211 + for (i = 0; i < sockets_list.used; i++, ls++) {
5213 + if (which != FPM_CLEANUP_PARENT_EXEC) {
5218 + else { /* on PARENT EXEC we want socket fds to be inherited through environment variable */
5220 + sprintf(fd, "%d", ls->sock);
5221 + env_value = realloc(env_value, p + (p ? 1 : 0) + strlen(ls->key) + 1 + strlen(fd) + 1);
5222 + p += sprintf(env_value + p, "%s%s=%s", p ? "," : "", ls->key, fd);
5225 + if (which == FPM_CLEANUP_PARENT_EXIT_MAIN) {
5227 + if (ls->type == FPM_AF_UNIX) {
5237 + setenv("FPM_SOCKETS", env_value, 1);
5241 + fpm_array_free(&sockets_list);
5244 +static int fpm_sockets_hash_op(int sock, struct sockaddr *sa, char *key, int type, int op)
5247 + if (key == NULL) {
5251 + case FPM_AF_INET : {
5252 + struct sockaddr_in *sa_in = (struct sockaddr_in *) sa;
5254 + key = alloca(sizeof("xxx.xxx.xxx.xxx:ppppp"));
5256 + sprintf(key, "%u.%u.%u.%u:%u", IPQUAD(&sa_in->sin_addr), (unsigned int) ntohs(sa_in->sin_port));
5261 + case FPM_AF_UNIX : {
5262 + struct sockaddr_un *sa_un = (struct sockaddr_un *) sa;
5264 + key = alloca(strlen(sa_un->sun_path) + 1);
5266 + strcpy(key, sa_un->sun_path);
5280 + case FPM_GET_USE_SOCKET :
5284 + struct listening_socket_s *ls = sockets_list.data;
5286 + for (i = 0; i < sockets_list.used; i++, ls++) {
5288 + if (!strcmp(ls->key, key)) {
5297 + case FPM_STORE_SOCKET : /* inherited socket */
5298 + case FPM_STORE_USE_SOCKET : /* just created */
5301 + struct listening_socket_s *ls;
5303 + ls = fpm_array_push(&sockets_list);
5309 + if (op == FPM_STORE_SOCKET) {
5317 + ls->key = strdup(key);
5328 +static int fpm_sockets_new_listening_socket(struct fpm_worker_pool_s *wp, struct sockaddr *sa, int socklen)
5333 + mode_t saved_umask;
5335 + /* we have custom backlog value */
5336 + if (wp->config->listen_options) {
5337 + backlog = wp->config->listen_options->backlog;
5340 + sock = socket(sa->sa_family, SOCK_STREAM, 0);
5343 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "socket() failed");
5347 + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
5349 + if (wp->listen_address_domain == FPM_AF_UNIX) {
5350 + unlink( ((struct sockaddr_un *) sa)->sun_path);
5353 + saved_umask = umask(0777 ^ wp->socket_mode);
5355 + if (0 > bind(sock, sa, socklen)) {
5356 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "bind() for address '%s' failed", wp->config->listen_address);
5360 + if (wp->listen_address_domain == FPM_AF_UNIX) {
5362 + char *path = ((struct sockaddr_un *) sa)->sun_path;
5364 + if (wp->socket_uid != -1 || wp->socket_gid != -1) {
5366 + if (0 > chown(path, wp->socket_uid, wp->socket_gid)) {
5367 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chown() for address '%s' failed", wp->config->listen_address);
5375 + umask(saved_umask);
5377 + if (0 > listen(sock, backlog)) {
5378 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "listen() for address '%s' failed", wp->config->listen_address);
5385 +static int fpm_sockets_get_listening_socket(struct fpm_worker_pool_s *wp, struct sockaddr *sa, int socklen)
5389 + sock = fpm_sockets_hash_op(0, sa, 0, wp->listen_address_domain, FPM_GET_USE_SOCKET);
5391 + if (sock >= 0) return sock;
5393 + sock = fpm_sockets_new_listening_socket(wp, sa, socklen);
5395 + fpm_sockets_hash_op(sock, sa, 0, wp->listen_address_domain, FPM_STORE_USE_SOCKET);
5400 +enum fpm_address_domain fpm_sockets_domain_from_address(char *address)
5402 + if (strchr(address, ':')) return FPM_AF_INET;
5404 + if (strlen(address) == strspn(address, "0123456789")) return FPM_AF_INET;
5406 + return FPM_AF_UNIX;
5409 +static int fpm_socket_af_inet_listening_socket(struct fpm_worker_pool_s *wp)
5411 + struct sockaddr_in sa_in;
5412 + char *dup_address = strdup(wp->config->listen_address);
5413 + char *port_str = strchr(dup_address, ':');
5414 + char *addr = NULL;
5417 + if (port_str) { /* this is host:port pair */
5418 + *port_str++ = '\0';
5419 + port = atoi(port_str);
5420 + addr = dup_address;
5422 + else if (strlen(dup_address) == strspn(dup_address, "0123456789")) { /* this is port */
5423 + port = atoi(dup_address);
5424 + port_str = dup_address;
5428 + zlog(ZLOG_STUFF, ZLOG_ERROR, "invalid port value '%s'", port_str);
5432 + memset(&sa_in, 0, sizeof(sa_in));
5436 + sa_in.sin_addr.s_addr = inet_addr(addr);
5438 + if (sa_in.sin_addr.s_addr == INADDR_NONE) { /* do resolve */
5439 + if (0 > fpm_sockets_resolve_af_inet(addr, NULL, &sa_in)) {
5442 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "address '%s' resolved as %u.%u.%u.%u", addr, IPQUAD(&sa_in.sin_addr));
5447 + sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
5451 + sa_in.sin_family = AF_INET;
5452 + sa_in.sin_port = htons(port);
5454 + free(dup_address);
5456 + return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_in, sizeof(struct sockaddr_in));
5459 +static int fpm_socket_af_unix_listening_socket(struct fpm_worker_pool_s *wp)
5461 + struct sockaddr_un sa_un;
5463 + memset(&sa_un, 0, sizeof(sa_un));
5465 + cpystrn(sa_un.sun_path, wp->config->listen_address, sizeof(sa_un.sun_path));
5466 + sa_un.sun_family = AF_UNIX;
5468 + return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_un, sizeof(struct sockaddr_un));
5471 +int fpm_sockets_init_main()
5474 + struct fpm_worker_pool_s *wp;
5475 + char *inherited = getenv("FPM_SOCKETS");
5476 + struct listening_socket_s *ls;
5478 + if (0 == fpm_array_init(&sockets_list, sizeof(struct listening_socket_s), 10)) {
5482 + /* import inherited sockets */
5483 + while (inherited && *inherited) {
5484 + char *comma = strchr(inherited, ',');
5488 + if (comma) *comma = '\0';
5490 + eq = strchr(inherited, '=');
5495 + fd_no = atoi(eq + 1);
5497 + type = fpm_sockets_domain_from_address(inherited);
5499 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "using inherited socket fd=%d, \"%s\"", fd_no, inherited);
5501 + fpm_sockets_hash_op(fd_no, 0, inherited, type, FPM_STORE_SOCKET);
5504 + if (comma) inherited = comma + 1;
5505 + else inherited = 0;
5508 + /* create all required sockets */
5509 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
5511 + if (!wp->is_template) {
5513 + switch (wp->listen_address_domain) {
5515 + case FPM_AF_INET :
5517 + wp->listening_socket = fpm_socket_af_inet_listening_socket(wp);
5520 + case FPM_AF_UNIX :
5522 + if (0 > fpm_unix_resolve_socket_premissions(wp)) {
5526 + wp->listening_socket = fpm_socket_af_unix_listening_socket(wp);
5531 + if (wp->listening_socket == -1) {
5538 + /* close unused sockets that was inherited */
5539 + ls = sockets_list.data;
5541 + for (i = 0; i < sockets_list.used; ) {
5543 + if (ls->refcount == 0) {
5545 + if (ls->type == FPM_AF_UNIX) {
5549 + fpm_array_item_remove(&sockets_list, i);
5557 + if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_sockets_cleanup, 0)) {
5563 diff --git a/sapi/cgi/fpm/fpm_sockets.h b/sapi/cgi/fpm/fpm_sockets.h
5564 new file mode 100644
5565 index 0000000..d5433e3
5567 +++ b/sapi/cgi/fpm/fpm_sockets.h
5571 + /* (c) 2007,2008 Andrei Nigmatulin */
5574 +#define FPM_MISC_H 1
5576 +#include <unistd.h>
5579 +#include "fpm_worker_pool.h"
5581 +enum fpm_address_domain fpm_sockets_domain_from_address(char *addr);
5582 +int fpm_sockets_init_main();
5585 +static inline int fd_set_blocked(int fd, int blocked)
5587 + int flags = fcntl(fd, F_GETFL);
5589 + if (flags < 0) return -1;
5592 + flags &= ~O_NONBLOCK;
5594 + flags |= O_NONBLOCK;
5596 + return fcntl(fd, F_SETFL, flags);
5599 +#define IPQUAD(sin_addr) \
5600 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[0], \
5601 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[1], \
5602 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[2], \
5603 + (unsigned int) ((unsigned char *) &(sin_addr)->s_addr)[3]
5606 diff --git a/sapi/cgi/fpm/fpm_stdio.c b/sapi/cgi/fpm/fpm_stdio.c
5607 new file mode 100644
5608 index 0000000..a6818d7
5610 +++ b/sapi/cgi/fpm/fpm_stdio.c
5614 + /* (c) 2007,2008 Andrei Nigmatulin */
5616 +#include "fpm_config.h"
5618 +#include <sys/types.h>
5619 +#include <sys/stat.h>
5620 +#include <string.h>
5622 +#include <unistd.h>
5626 +#include "fpm_children.h"
5627 +#include "fpm_events.h"
5628 +#include "fpm_sockets.h"
5629 +#include "fpm_stdio.h"
5632 +static int fd_stdout[2];
5633 +static int fd_stderr[2];
5635 +int fpm_stdio_init_main()
5637 + int fd = open("/dev/null", O_RDWR);
5640 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"/dev/null\") failed");
5644 + if (0 > dup2(fd, STDIN_FILENO) || 0 > dup2(fd, STDOUT_FILENO)) {
5645 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
5654 +int fpm_stdio_init_final()
5656 + if (fpm_global_config.daemonize) {
5658 + if (fpm_globals.error_log_fd != STDERR_FILENO) {
5659 + /* there might be messages to stderr from libevent, we need to log them all */
5660 + if (0 > dup2(fpm_globals.error_log_fd, STDERR_FILENO)) {
5661 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
5666 + zlog_set_level(fpm_globals.log_level);
5668 + zlog_set_fd(fpm_globals.error_log_fd);
5674 +int fpm_stdio_init_child(struct fpm_worker_pool_s *wp)
5676 + close(fpm_globals.error_log_fd);
5677 + fpm_globals.error_log_fd = -1;
5680 + if (wp->listening_socket != STDIN_FILENO) {
5681 + if (0 > dup2(wp->listening_socket, STDIN_FILENO)) {
5682 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
5690 +static void fpm_stdio_child_said(int fd, short which, void *arg)
5692 + static const int max_buf_size = 1024;
5693 + char buf[max_buf_size];
5694 + struct fpm_child_s *child = arg;
5695 + int is_stdout = fd == child->fd_stdout;
5696 + struct event *ev = is_stdout ? &child->ev_stdout : &child->ev_stderr;
5697 + int fifo_in = 1, fifo_out = 1;
5698 + int is_last_message = 0;
5703 + zlog(ZLOG_STUFF, ZLOG_DEBUG, "child %d said %s", (int) child->pid, is_stdout ? "stdout" : "stderr");
5706 + while (fifo_in || fifo_out) {
5710 + res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf);
5712 + if (res <= 0) { /* no data */
5715 + if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
5716 + /* just no more data ready */
5718 + else { /* error or pipe is closed */
5720 + if (res < 0) { /* error */
5721 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
5724 + fpm_event_del(ev);
5725 + is_last_message = 1;
5728 + close(child->fd_stdout);
5729 + child->fd_stdout = -1;
5732 + close(child->fd_stderr);
5733 + child->fd_stderr = -1;
5737 + if (in_buf == 0 && !fpm_globals.is_child) {
5738 + zlog(ZLOG_STUFF, ZLOG_DEBUG, "child %d (pool %s) %s pipe is closed", (int) child->pid,
5739 + child->wp->config->name, is_stdout ? "stdout" : "stderr");
5750 + if (in_buf == 0) {
5755 + int should_print = 0;
5756 + buf[in_buf] = '\0';
5758 + /* FIXME: there might be binary data */
5760 + /* we should print if no more space in the buffer */
5761 + if (in_buf == max_buf_size - 1) {
5765 + /* we should print if no more data to come */
5770 + nl = strchr(buf, '\n');
5772 + if (nl || should_print) {
5778 + zlog(ZLOG_STUFF, ZLOG_WARNING, "child %d (pool %s) said into %s: \"%s\"%s", (int) child->pid,
5779 + child->wp->config->name, is_stdout ? "stdout" : "stderr", buf, is_last_message ? ", pipe is closed" : "");
5782 + int out_buf = 1 + nl - buf;
5783 + memmove(buf, buf + out_buf, in_buf - out_buf);
5784 + in_buf -= out_buf;
5796 +int fpm_stdio_prepare_pipes(struct fpm_child_s *child)
5798 + if (0 == child->wp->config->catch_workers_output) { /* not required */
5802 + if (0 > pipe(fd_stdout)) {
5803 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
5807 + if (0 > pipe(fd_stderr)) {
5808 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
5809 + close(fd_stdout[0]); close(fd_stdout[1]);
5813 + if (0 > fd_set_blocked(fd_stdout[0], 0) || 0 > fd_set_blocked(fd_stderr[0], 0)) {
5814 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fd_set_blocked() failed");
5815 + close(fd_stdout[0]); close(fd_stdout[1]);
5816 + close(fd_stderr[0]); close(fd_stderr[1]);
5823 +int fpm_stdio_parent_use_pipes(struct fpm_child_s *child)
5825 + if (0 == child->wp->config->catch_workers_output) { /* not required */
5829 + close(fd_stdout[1]);
5830 + close(fd_stderr[1]);
5832 + child->fd_stdout = fd_stdout[0];
5833 + child->fd_stderr = fd_stderr[0];
5835 + fpm_event_add(child->fd_stdout, &child->ev_stdout, fpm_stdio_child_said, child);
5836 + fpm_event_add(child->fd_stderr, &child->ev_stderr, fpm_stdio_child_said, child);
5841 +int fpm_stdio_discard_pipes(struct fpm_child_s *child)
5843 + if (0 == child->wp->config->catch_workers_output) { /* not required */
5847 + close(fd_stdout[1]);
5848 + close(fd_stderr[1]);
5850 + close(fd_stdout[0]);
5851 + close(fd_stderr[0]);
5856 +void fpm_stdio_child_use_pipes(struct fpm_child_s *child)
5858 + if (child->wp->config->catch_workers_output) {
5859 + dup2(fd_stdout[1], STDOUT_FILENO);
5860 + dup2(fd_stderr[1], STDERR_FILENO);
5861 + close(fd_stdout[0]); close(fd_stdout[1]);
5862 + close(fd_stderr[0]); close(fd_stderr[1]);
5865 + /* stdout of parent is always /dev/null */
5866 + dup2(STDOUT_FILENO, STDERR_FILENO);
5870 +int fpm_stdio_open_error_log(int reopen)
5874 + fd = open(fpm_global_config.error_log, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
5877 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"%s\") failed", fpm_global_config.error_log);
5882 + if (fpm_global_config.daemonize) {
5883 + dup2(fd, STDERR_FILENO);
5886 + dup2(fd, fpm_globals.error_log_fd);
5888 + fd = fpm_globals.error_log_fd; /* for FD_CLOSEXEC to work */
5891 + fpm_globals.error_log_fd = fd;
5894 + fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
5898 diff --git a/sapi/cgi/fpm/fpm_stdio.h b/sapi/cgi/fpm/fpm_stdio.h
5899 new file mode 100644
5900 index 0000000..d3d61e4
5902 +++ b/sapi/cgi/fpm/fpm_stdio.h
5906 + /* (c) 2007,2008 Andrei Nigmatulin */
5908 +#ifndef FPM_STDIO_H
5909 +#define FPM_STDIO_H 1
5911 +#include "fpm_worker_pool.h"
5913 +int fpm_stdio_init_main();
5914 +int fpm_stdio_init_final();
5915 +int fpm_stdio_init_child(struct fpm_worker_pool_s *wp);
5916 +int fpm_stdio_prepare_pipes(struct fpm_child_s *child);
5917 +void fpm_stdio_child_use_pipes(struct fpm_child_s *child);
5918 +int fpm_stdio_parent_use_pipes(struct fpm_child_s *child);
5919 +int fpm_stdio_discard_pipes(struct fpm_child_s *child);
5920 +int fpm_stdio_open_error_log(int reopen);
5924 diff --git a/sapi/cgi/fpm/fpm_str.h b/sapi/cgi/fpm/fpm_str.h
5925 new file mode 100644
5926 index 0000000..19e2055
5928 +++ b/sapi/cgi/fpm/fpm_str.h
5932 + /* (c) 2007,2008 Andrei Nigmatulin */
5935 +#define FPM_STR_H 1
5937 +static inline char *cpystrn(char *dst, const char *src, size_t dst_size)
5941 + if (!dst_size) return dst;
5944 + end = dst + dst_size - 1;
5946 + for (; d < end; ++d, ++src) {
5947 + if (!(*d = *src)) {
5957 +static inline char *str_purify_filename(char *dst, char *src, size_t size)
5962 + end = dst + size - 1;
5964 + for (; d < end && *src; ++d, ++src) {
5965 + if (* (unsigned char *) src < ' ' || * (unsigned char *) src > '\x7f') {
5979 diff --git a/sapi/cgi/fpm/fpm_trace.c b/sapi/cgi/fpm/fpm_trace.c
5980 new file mode 100644
5981 index 0000000..7996355
5983 +++ b/sapi/cgi/fpm/fpm_trace.c
5987 + /* (c) 2007,2008 Andrei Nigmatulin */
5989 +#include "fpm_config.h"
5991 +#include <sys/types.h>
5993 +#include "fpm_trace.h"
5995 +int fpm_trace_get_strz(char *buf, size_t sz, long addr)
5999 + char *lc = (char *) &l;
6001 + if (0 > fpm_trace_get_long(addr, &l)) {
6005 + i = l % SIZEOF_LONG;
6009 + for (addr = l; ; addr += SIZEOF_LONG) {
6011 + if (0 > fpm_trace_get_long(addr, &l)) {
6015 + for ( ; i < SIZEOF_LONG; i++) {
6018 + if (sz && lc[i]) {
6031 diff --git a/sapi/cgi/fpm/fpm_trace.h b/sapi/cgi/fpm/fpm_trace.h
6032 new file mode 100644
6033 index 0000000..b421172
6035 +++ b/sapi/cgi/fpm/fpm_trace.h
6039 + /* (c) 2007,2008 Andrei Nigmatulin */
6041 +#ifndef FPM_TRACE_H
6042 +#define FPM_TRACE_H 1
6044 +#include <unistd.h>
6046 +int fpm_trace_signal(pid_t pid);
6047 +int fpm_trace_ready(pid_t pid);
6048 +int fpm_trace_close(pid_t pid);
6049 +int fpm_trace_get_long(long addr, long *data);
6050 +int fpm_trace_get_strz(char *buf, size_t sz, long addr);
6054 diff --git a/sapi/cgi/fpm/fpm_trace_mach.c b/sapi/cgi/fpm/fpm_trace_mach.c
6055 new file mode 100644
6056 index 0000000..11cb9cf
6058 +++ b/sapi/cgi/fpm/fpm_trace_mach.c
6062 + /* (c) 2007,2008 Andrei Nigmatulin */
6064 +#include "fpm_config.h"
6066 +#include <mach/mach.h>
6067 +#include <mach/mach_vm.h>
6069 +#include <unistd.h>
6071 +#include "fpm_trace.h"
6072 +#include "fpm_process_ctl.h"
6073 +#include "fpm_unix.h"
6077 +static mach_port_name_t target;
6078 +static vm_offset_t target_page_base;
6079 +static vm_offset_t local_page;
6080 +static mach_msg_type_number_t local_size;
6082 +static void fpm_mach_vm_deallocate()
6085 + mach_vm_deallocate(mach_task_self(), local_page, local_size);
6086 + target_page_base = 0;
6092 +static int fpm_mach_vm_read_page(vm_offset_t page)
6096 + kr = mach_vm_read(target, page, fpm_pagesize, &local_page, &local_size);
6098 + if (kr != KERN_SUCCESS) {
6099 + zlog(ZLOG_STUFF, ZLOG_ERROR, "mach_vm_read() failed: %s (%d)", mach_error_string(kr), kr);
6106 +int fpm_trace_signal(pid_t pid)
6108 + if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
6109 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "kill(SIGSTOP) failed");
6116 +int fpm_trace_ready(pid_t pid)
6120 + kr = task_for_pid(mach_task_self(), pid, &target);
6122 + if (kr != KERN_SUCCESS) {
6125 + if (kr == KERN_FAILURE) {
6126 + msg = " It seems that master process does not have enough privileges to trace processes.";
6129 + zlog(ZLOG_STUFF, ZLOG_ERROR, "task_for_pid() failed: %s (%d)%s", mach_error_string(kr), kr, msg);
6136 +int fpm_trace_close(pid_t pid)
6138 + fpm_mach_vm_deallocate();
6145 +int fpm_trace_get_long(long addr, long *data)
6147 + size_t offset = ((uintptr_t) (addr) % fpm_pagesize);
6148 + vm_offset_t base = (uintptr_t) (addr) - offset;
6150 + if (base != target_page_base) {
6151 + fpm_mach_vm_deallocate();
6152 + if (0 > fpm_mach_vm_read_page(base)) {
6157 + *data = * (long *) (local_page + offset);
6162 diff --git a/sapi/cgi/fpm/fpm_trace_pread.c b/sapi/cgi/fpm/fpm_trace_pread.c
6163 new file mode 100644
6164 index 0000000..f41bb91
6166 +++ b/sapi/cgi/fpm/fpm_trace_pread.c
6170 + /* (c) 2007,2008 Andrei Nigmatulin */
6172 +#define _GNU_SOURCE
6173 +#define _FILE_OFFSET_BITS 64
6175 +#include "fpm_config.h"
6177 +#include <unistd.h>
6181 +#include <stdint.h>
6183 +#include "fpm_trace.h"
6184 +#include "fpm_process_ctl.h"
6188 +static int mem_file = -1;
6190 +int fpm_trace_signal(pid_t pid)
6192 + if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
6193 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "kill(SIGSTOP) failed");
6200 +int fpm_trace_ready(pid_t pid)
6204 + sprintf(buf, "/proc/%d/" PROC_MEM_FILE, (int) pid);
6206 + mem_file = open(buf, O_RDONLY);
6208 + if (0 > mem_file) {
6209 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(%s) failed", buf);
6216 +int fpm_trace_close(pid_t pid)
6225 +int fpm_trace_get_long(long addr, long *data)
6227 + if (sizeof(*data) != pread(mem_file, (void *) data, sizeof(*data), (uintptr_t) addr)) {
6228 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pread() failed");
6235 diff --git a/sapi/cgi/fpm/fpm_trace_ptrace.c b/sapi/cgi/fpm/fpm_trace_ptrace.c
6236 new file mode 100644
6237 index 0000000..11e2081
6239 +++ b/sapi/cgi/fpm/fpm_trace_ptrace.c
6243 + /* (c) 2007,2008 Andrei Nigmatulin */
6245 +#include "fpm_config.h"
6247 +#include <sys/wait.h>
6248 +#include <sys/ptrace.h>
6249 +#include <unistd.h>
6252 +#if defined(PT_ATTACH) && !defined(PTRACE_ATTACH)
6253 +#define PTRACE_ATTACH PT_ATTACH
6256 +#if defined(PT_DETACH) && !defined(PTRACE_DETACH)
6257 +#define PTRACE_DETACH PT_DETACH
6260 +#if defined(PT_READ_D) && !defined(PTRACE_PEEKDATA)
6261 +#define PTRACE_PEEKDATA PT_READ_D
6264 +#include "fpm_trace.h"
6267 +static pid_t traced_pid;
6269 +int fpm_trace_signal(pid_t pid)
6271 + if (0 > ptrace(PTRACE_ATTACH, pid, 0, 0)) {
6272 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(ATTACH) failed");
6279 +int fpm_trace_ready(pid_t pid)
6286 +int fpm_trace_close(pid_t pid)
6288 + if (0 > ptrace(PTRACE_DETACH, pid, (void *) 1, 0)) {
6289 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(DETACH) failed");
6298 +int fpm_trace_get_long(long addr, long *data)
6301 + struct ptrace_io_desc ptio = {
6302 + .piod_op = PIOD_READ_D,
6303 + .piod_offs = (void *) addr,
6304 + .piod_addr = (void *) data,
6305 + .piod_len = sizeof(long)
6308 + if (0 > ptrace(PT_IO, traced_pid, (void *) &ptio, 0)) {
6309 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(PT_IO) failed");
6315 + *data = ptrace(PTRACE_PEEKDATA, traced_pid, (void *) addr, 0);
6318 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(PEEKDATA) failed");
6326 diff --git a/sapi/cgi/fpm/fpm_unix.c b/sapi/cgi/fpm/fpm_unix.c
6327 new file mode 100644
6328 index 0000000..4d5eecc
6330 +++ b/sapi/cgi/fpm/fpm_unix.c
6334 + /* (c) 2007,2008 Andrei Nigmatulin */
6336 +#include "fpm_config.h"
6338 +#include <string.h>
6339 +#include <sys/time.h>
6340 +#include <sys/resource.h>
6341 +#include <stdlib.h>
6342 +#include <unistd.h>
6343 +#include <sys/types.h>
6348 +#include <sys/prctl.h>
6352 +#include "fpm_conf.h"
6353 +#include "fpm_cleanup.h"
6354 +#include "fpm_clock.h"
6355 +#include "fpm_stdio.h"
6356 +#include "fpm_unix.h"
6359 +size_t fpm_pagesize;
6361 +int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp)
6363 + struct fpm_listen_options_s *lo = wp->config->listen_options;
6365 + /* uninitialized */
6366 + wp->socket_uid = -1;
6367 + wp->socket_gid = -1;
6368 + wp->socket_mode = 0666;
6370 + if (!lo) return 0;
6372 + if (lo->owner && *lo->owner) {
6373 + struct passwd *pwd;
6375 + pwd = getpwnam(lo->owner);
6378 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "cannot get uid for user '%s', pool '%s'", lo->owner, wp->config->name);
6382 + wp->socket_uid = pwd->pw_uid;
6383 + wp->socket_gid = pwd->pw_gid;
6386 + if (lo->group && *lo->group) {
6387 + struct group *grp;
6389 + grp = getgrnam(lo->group);
6392 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "cannot get gid for group '%s', pool '%s'", lo->group, wp->config->name);
6396 + wp->socket_gid = grp->gr_gid;
6399 + if (lo->mode && *lo->mode) {
6400 + wp->socket_mode = strtoul(lo->mode, 0, 8);
6406 +static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp)
6408 + int is_root = !geteuid();
6411 + if (wp->config->user && *wp->config->user) {
6413 + if (strlen(wp->config->user) == strspn(wp->config->user, "0123456789")) {
6414 + wp->set_uid = strtoul(wp->config->user, 0, 10);
6417 + struct passwd *pwd;
6419 + pwd = getpwnam(wp->config->user);
6422 + zlog(ZLOG_STUFF, ZLOG_ERROR, "cannot get uid for user '%s', pool '%s'", wp->config->user, wp->config->name);
6426 + wp->set_uid = pwd->pw_uid;
6427 + wp->set_gid = pwd->pw_gid;
6429 + wp->user = strdup(pwd->pw_name);
6430 + wp->home = strdup(pwd->pw_dir);
6434 + if (wp->config->group && *wp->config->group) {
6436 + if (strlen(wp->config->group) == strspn(wp->config->group, "0123456789")) {
6437 + wp->set_gid = strtoul(wp->config->group, 0, 10);
6440 + struct group *grp;
6442 + grp = getgrnam(wp->config->group);
6445 + zlog(ZLOG_STUFF, ZLOG_ERROR, "cannot get gid for group '%s', pool '%s'", wp->config->group, wp->config->name);
6449 + wp->set_gid = grp->gr_gid;
6453 +#ifndef I_REALLY_WANT_ROOT_PHP
6454 + if (wp->set_uid == 0 || wp->set_gid == 0) {
6455 + zlog(ZLOG_STUFF, ZLOG_ERROR, "please specify user and group other than root, pool '%s'", wp->config->name);
6460 + else { /* not root */
6461 + if (wp->config->user && *wp->config->user) {
6462 + zlog(ZLOG_STUFF, ZLOG_WARNING, "'user' directive is ignored, pool '%s'", wp->config->name);
6464 + if (wp->config->group && *wp->config->group) {
6465 + zlog(ZLOG_STUFF, ZLOG_WARNING, "'group' directive is ignored, pool '%s'", wp->config->name);
6467 + if (wp->config->chroot && *wp->config->chroot) {
6468 + zlog(ZLOG_STUFF, ZLOG_WARNING, "'chroot' directive is ignored, pool '%s'", wp->config->name);
6471 + { /* set up HOME and USER anyway */
6472 + struct passwd *pwd;
6474 + pwd = getpwuid(getuid());
6477 + wp->user = strdup(pwd->pw_name);
6478 + wp->home = strdup(pwd->pw_dir);
6486 +int fpm_unix_init_child(struct fpm_worker_pool_s *wp)
6488 + int is_root = !geteuid();
6489 + int made_chroot = 0;
6491 + if (wp->config->rlimit_files) {
6494 + getrlimit(RLIMIT_NOFILE, &r);
6496 + r.rlim_cur = (rlim_t) wp->config->rlimit_files;
6497 + r.rlim_max = r.rlim_cur;
6498 + if (0 > setrlimit(RLIMIT_NOFILE, &r)) {
6499 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setrlimit(RLIMIT_NOFILE) failed");
6503 + if (wp->config->rlimit_core) {
6506 + getrlimit(RLIMIT_CORE, &r);
6508 + r.rlim_cur = wp->config->rlimit_core == -1 ? (rlim_t) RLIM_INFINITY : (rlim_t) wp->config->rlimit_core;
6509 + r.rlim_max = r.rlim_cur;
6510 + if (0 > setrlimit(RLIMIT_CORE, &r)) {
6511 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setrlimit(RLIMIT_CORE) failed");
6515 + if (is_root && wp->config->chroot && *wp->config->chroot) {
6516 + if (0 > chroot(wp->config->chroot)) {
6517 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chroot(%s) failed", wp->config->chroot);
6523 + if (wp->config->chdir && *wp->config->chdir) {
6524 + if (0 > chdir(wp->config->chdir)) {
6525 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "chdir(%s) failed", wp->config->chdir);
6529 + else if (made_chroot) {
6534 + if (wp->set_gid) {
6535 + if (0 > setgid(wp->set_gid)) {
6536 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setgid(%d) failed", wp->set_gid);
6540 + if (wp->set_uid) {
6541 + if (0 > initgroups(wp->config->user, wp->set_gid)) {
6542 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "initgroups(%s, %d) failed", wp->config->user, wp->set_gid);
6545 + if (0 > setuid(wp->set_uid)) {
6546 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setuid(%d) failed", wp->set_uid);
6553 + if (0 > prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)) {
6554 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "prctl(PR_SET_DUMPABLE) failed");
6558 + if (0 > fpm_clock_init()) {
6565 +int fpm_unix_init_main()
6567 + struct fpm_worker_pool_s *wp;
6569 + fpm_pagesize = getpagesize();
6571 + if (fpm_global_config.daemonize) {
6577 + zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fork() failed");
6586 + fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT);
6595 + if (0 > fpm_clock_init()) {
6599 + fpm_globals.parent_pid = getpid();
6601 + for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
6603 + if (0 > fpm_unix_conf_wp(wp)) {
6609 + fpm_stdio_init_final();
6613 + getrlimit(RLIMIT_NOFILE, &r);
6615 + zlog(ZLOG_STUFF, ZLOG_NOTICE, "getrlimit(nofile): max:%lld, cur:%lld",
6616 + (long long) r.rlim_max, (long long) r.rlim_cur);
6621 diff --git a/sapi/cgi/fpm/fpm_unix.h b/sapi/cgi/fpm/fpm_unix.h
6622 new file mode 100644
6623 index 0000000..3451db1
6625 +++ b/sapi/cgi/fpm/fpm_unix.h
6629 + /* (c) 2007,2008 Andrei Nigmatulin */
6632 +#define FPM_UNIX_H 1
6634 +#include "fpm_worker_pool.h"
6636 +int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp);
6637 +int fpm_unix_init_child(struct fpm_worker_pool_s *wp);
6638 +int fpm_unix_init_main();
6640 +extern size_t fpm_pagesize;
6644 diff --git a/sapi/cgi/fpm/fpm_worker_pool.c b/sapi/cgi/fpm/fpm_worker_pool.c
6645 new file mode 100644
6646 index 0000000..49dd5a8
6648 +++ b/sapi/cgi/fpm/fpm_worker_pool.c
6652 + /* (c) 2007,2008 Andrei Nigmatulin */
6654 +#include "fpm_config.h"
6656 +#include <string.h>
6657 +#include <stdlib.h>
6658 +#include <unistd.h>
6660 +#include "fpm_worker_pool.h"
6661 +#include "fpm_cleanup.h"
6662 +#include "fpm_children.h"
6663 +#include "fpm_shm.h"
6664 +#include "fpm_shm_slots.h"
6665 +#include "fpm_conf.h"
6667 +struct fpm_worker_pool_s *fpm_worker_all_pools;
6669 +static void fpm_worker_pool_cleanup(int which, void *arg)
6671 + struct fpm_worker_pool_s *wp, *wp_next;
6673 + for (wp = fpm_worker_all_pools; wp; wp = wp_next) {
6674 + wp_next = wp->next;
6675 + fpm_worker_pool_config_free(wp->config);
6676 + fpm_children_free(wp->children);
6677 + fpm_array_free(&wp->slots_used);
6678 + fpm_array_free(&wp->slots_free);
6679 + fpm_shm_free_list(wp->shm_list, which == FPM_CLEANUP_CHILD ? fpm_shm_slots_mem() : 0);
6686 + fpm_worker_all_pools = 0;
6689 +struct fpm_worker_pool_s *fpm_worker_pool_alloc()
6691 + struct fpm_worker_pool_s *ret;
6693 + ret = malloc(sizeof(struct fpm_worker_pool_s));
6699 + memset(ret, 0, sizeof(struct fpm_worker_pool_s));
6701 + if (!fpm_worker_all_pools) {
6702 + fpm_worker_all_pools = ret;
6705 + fpm_array_init(&ret->slots_used, sizeof(struct fpm_shm_slot_ptr_s), 50);
6706 + fpm_array_init(&ret->slots_free, sizeof(struct fpm_shm_slot_ptr_s), 50);
6711 +int fpm_worker_pool_init_main()
6713 + if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_worker_pool_cleanup, 0)) {
6719 diff --git a/sapi/cgi/fpm/fpm_worker_pool.h b/sapi/cgi/fpm/fpm_worker_pool.h
6720 new file mode 100644
6721 index 0000000..cc0dbbd
6723 +++ b/sapi/cgi/fpm/fpm_worker_pool.h
6727 + /* (c) 2007,2008 Andrei Nigmatulin */
6729 +#ifndef FPM_WORKER_POOL_H
6730 +#define FPM_WORKER_POOL_H 1
6732 +#include "fpm_conf.h"
6733 +#include "fpm_arrays.h"
6735 +struct fpm_worker_pool_s;
6736 +struct fpm_child_s;
6737 +struct fpm_child_stat_s;
6740 +enum fpm_address_domain {
6745 +struct fpm_worker_pool_s {
6746 + struct fpm_worker_pool_s *next;
6747 + struct fpm_worker_pool_config_s *config;
6748 + char *user, *home; /* for setting env USER and HOME */
6749 + enum fpm_address_domain listen_address_domain;
6750 + int listening_socket;
6751 + int set_uid, set_gid; /* config uid and gid */
6752 + unsigned is_template:1; /* just config template, no processes will be created */
6753 + int socket_uid, socket_gid, socket_mode;
6755 + struct fpm_shm_s *shm_list;
6756 + struct fpm_array_s slots_used;
6757 + struct fpm_array_s slots_free;
6760 + struct fpm_child_s *children;
6761 + int running_children;
6764 +struct fpm_worker_pool_s *fpm_worker_pool_alloc();
6765 +int fpm_worker_pool_init_main();
6767 +extern struct fpm_worker_pool_s *fpm_worker_all_pools;
6771 diff --git a/sapi/cgi/fpm/init.d/php-fpm.in b/sapi/cgi/fpm/init.d/php-fpm.in
6772 new file mode 100644
6773 index 0000000..0befa52
6775 +++ b/sapi/cgi/fpm/init.d/php-fpm.in
6779 +php_fpm_BIN=@prefix@/bin/php-cgi
6780 +php_fpm_CONF=@php_fpm_conf_path@
6781 +php_fpm_PID=@php_fpm_pid_path@
6784 +php_opts="--fpm-config $php_fpm_CONF"
6790 + while test $try -lt 35 ; do
6794 + if [ -f "$2" ] ; then
6801 + if [ ! -f "$2" ] ; then
6809 + try=`expr $try + 1`
6818 + echo -n "Starting php_fpm "
6820 + $php_fpm_BIN --fpm $php_opts
6822 + if [ "$?" != 0 ] ; then
6827 + wait_for_pid created $php_fpm_PID
6829 + if [ -n "$try" ] ; then
6838 + echo -n "Shutting down php_fpm "
6840 + if [ ! -r $php_fpm_PID ] ; then
6841 + echo "warning, no pid file found - php-fpm is not running ?"
6845 + kill -TERM `cat $php_fpm_PID`
6847 + wait_for_pid removed $php_fpm_PID
6849 + if [ -n "$try" ] ; then
6858 + echo -n "Gracefully shutting down php_fpm "
6860 + if [ ! -r $php_fpm_PID ] ; then
6861 + echo "warning, no pid file found - php-fpm is not running ?"
6865 + kill -QUIT `cat $php_fpm_PID`
6867 + wait_for_pid removed $php_fpm_PID
6869 + if [ -n "$try" ] ; then
6884 + echo -n "Reload service php-fpm "
6886 + if [ ! -r $php_fpm_PID ] ; then
6887 + echo "warning, no pid file found - php-fpm is not running ?"
6891 + kill -USR2 `cat $php_fpm_PID`
6898 + echo -n "Re-opening php-fpm log file "
6900 + if [ ! -r $php_fpm_PID ] ; then
6901 + echo "warning, no pid file found - php-fpm is not running ?"
6905 + kill -USR1 `cat $php_fpm_PID`
6911 + echo "Usage: $0 {start|stop|quit|restart|reload|logrotate}"
6916 diff --git a/sapi/cgi/fpm/xml_config.c b/sapi/cgi/fpm/xml_config.c
6917 new file mode 100644
6918 index 0000000..10eb77e
6920 +++ b/sapi/cgi/fpm/xml_config.c
6924 + /* (c) 2004-2007 Andrei Nigmatulin */
6926 +#include "fpm_config.h"
6928 +#ifdef HAVE_ALLOCA_H
6929 +#include <alloca.h>
6931 +#include <string.h>
6933 +#include <stddef.h>
6934 +#include <stdlib.h>
6936 +#include <libxml/parser.h>
6937 +#include <libxml/tree.h>
6939 +#include "xml_config.h"
6941 +static struct xml_conf_section **xml_conf_sections = 0;
6942 +static int xml_conf_sections_allocated = 0;
6943 +static int xml_conf_sections_used = 0;
6945 +char *xml_conf_set_slot_boolean(void **conf, char *name, void *vv, intptr_t offset)
6948 + long value_y = !strcasecmp(value, "yes") || !strcmp(value, "1") || !strcasecmp(value, "on");
6949 + long value_n = !strcasecmp(value, "no") || !strcmp(value, "0") || !strcasecmp(value, "off");
6951 + if (!value_y && !value_n) {
6952 + return "xml_conf_set_slot(): invalid boolean value";
6955 +#ifdef XML_CONF_DEBUG
6956 + fprintf(stderr, "setting boolean '%s' => %s\n", name, value_y ? "TRUE" : "FALSE");
6959 + * (int *) ((char *) *conf + offset) = value_y ? 1 : 0;
6964 +char *xml_conf_set_slot_string(void **conf, char *name, void *vv, intptr_t offset)
6967 + char *v = strdup(value);
6969 + if (!v) return "xml_conf_set_slot_string(): strdup() failed";
6971 +#ifdef XML_CONF_DEBUG
6972 + fprintf(stderr, "setting string '%s' => '%s'\n", name, v);
6975 + * (char **) ((char *) *conf + offset) = v;
6980 +char *xml_conf_set_slot_integer(void **conf, char *name, void *vv, intptr_t offset)
6983 + int v = atoi(value);
6985 + * (int *) ((char *) *conf + offset) = v;
6987 +#ifdef XML_CONF_DEBUG
6988 + fprintf(stderr, "setting integer '%s' => %d\n", name, v);
6994 +char *xml_conf_set_slot_time(void **conf, char *name, void *vv, intptr_t offset)
6997 + int len = strlen(value);
7001 + if (!len) return "xml_conf_set_slot_timeval(): invalid timeval value";
7003 + suffix = value[len-1];
7005 + value[len-1] = '\0';
7009 + seconds = atoi(value);
7012 + seconds = 60 * atoi(value);
7015 + seconds = 60 * 60 * atoi(value);
7018 + seconds = 24 * 60 * 60 * atoi(value);
7021 + return "xml_conf_set_slot_timeval(): unknown suffix used in timeval value";
7024 + * (int *) ((char *) *conf + offset) = seconds;
7026 +#ifdef XML_CONF_DEBUG
7027 + fprintf(stderr, "setting time '%s' => %d:%02d:%02d:%02d\n", name, expand_dhms(seconds));
7033 +char *xml_conf_parse_section(void **conf, struct xml_conf_section *section, void *xml_node)
7035 + xmlNode *element = xml_node;
7038 +#ifdef XML_CONF_DEBUG
7039 + fprintf(stderr, "processing a section %s\n", section->path);
7042 + for ( ; element; element = element->next) {
7043 + if (element->type == XML_ELEMENT_NODE && !strcmp((const char *) element->name, "value") && element->children) {
7044 + xmlChar *name = xmlGetProp(element, (unsigned char *) "name");
7049 +#ifdef XML_CONF_DEBUG
7050 + fprintf(stderr, "found a value: %s\n", name);
7052 + for (i = 0; section->parsers[i].parser; i++) {
7053 + if (!section->parsers[i].name || !strcmp(section->parsers[i].name, (char *) name)) {
7058 + if (section->parsers[i].parser) {
7059 + if (section->parsers[i].type == XML_CONF_SCALAR) {
7060 + if (element->children->type == XML_TEXT_NODE) {
7061 + ret = section->parsers[i].parser(conf, (char *) name, element->children->content, section->parsers[i].offset);
7064 + ret = "XML_TEXT_NODE is expected, something different is given";
7068 + ret = section->parsers[i].parser(conf, (char *) name, element->children, section->parsers[i].offset);
7072 + if (ret) return ret;
7076 + fprintf(stderr, "Warning, unknown setting '%s' in section '%s'\n", (char *) name, section->path);
7086 +static char *xml_conf_parse_file(xmlNode *element)
7090 + for ( ; element; element = element->next) {
7092 + if (element->parent && element->type == XML_ELEMENT_NODE && !strcmp((const char *) element->name, "section")) {
7093 + xmlChar *name = xmlGetProp(element, (unsigned char *) "name");
7096 + char *parent_name = (char *) xmlGetNodePath(element->parent);
7099 + struct xml_conf_section *section = NULL;
7101 +#ifdef XML_CONF_DEBUG
7102 + fprintf(stderr, "got a section: %s/%s\n", parent_name, name);
7104 + full_name = alloca(strlen(parent_name) + strlen((char *) name) + 1 + 1);
7106 + sprintf(full_name, "%s/%s", parent_name, (char *) name);
7108 + xmlFree(parent_name);
7111 + for (i = 0; i < xml_conf_sections_used; i++) {
7112 + if (!strcmp(xml_conf_sections[i]->path, full_name)) {
7113 + section = xml_conf_sections[i];
7117 + if (section) { /* found a registered section */
7118 + void *conf = section->conf();
7119 + ret = xml_conf_parse_section(&conf, section, element->children);
7126 + if (element->children) {
7127 + ret = xml_conf_parse_file(element->children);
7135 +char *xml_conf_load_file(char *file)
7140 + LIBXML_TEST_VERSION
7142 + doc = xmlParseFile(file);
7145 + ret = xml_conf_parse_file(doc->children);
7149 + ret = "failed to parse conf file";
7152 + xmlCleanupParser();
7156 +int xml_conf_init()
7161 +void xml_conf_clean()
7163 + if (xml_conf_sections) {
7164 + free(xml_conf_sections);
7168 +int xml_conf_section_register(struct xml_conf_section *section)
7170 + if (xml_conf_sections_allocated == xml_conf_sections_used) {
7171 + int new_size = xml_conf_sections_used + 10;
7172 + void *new_ptr = realloc(xml_conf_sections, sizeof(struct xml_conf_section *) * new_size);
7175 + xml_conf_sections = new_ptr;
7176 + xml_conf_sections_allocated = new_size;
7179 + fprintf(stderr, "xml_conf_section_register(): out of memory\n");
7184 + xml_conf_sections[xml_conf_sections_used++] = section;
7189 +int xml_conf_sections_register(struct xml_conf_section *sections[])
7191 + for ( ; sections && *sections; sections++) {
7192 + if (0 > xml_conf_section_register(*sections)) {
7200 diff --git a/sapi/cgi/fpm/xml_config.h b/sapi/cgi/fpm/xml_config.h
7201 new file mode 100644
7202 index 0000000..b6169cd
7204 +++ b/sapi/cgi/fpm/xml_config.h
7208 + /* (c) 2004-2007 Andrei Nigmatulin */
7210 +#ifndef XML_CONFIG_H
7211 +#define XML_CONFIG_H 1
7213 +#include <stdint.h>
7215 +struct xml_value_parser;
7217 +struct xml_value_parser {
7220 + char *(*parser)(void **, char *, void *, intptr_t offset);
7224 +struct xml_conf_section {
7227 + struct xml_value_parser *parsers;
7230 +char *xml_conf_set_slot_boolean(void **conf, char *name, void *value, intptr_t offset);
7231 +char *xml_conf_set_slot_string(void **conf, char *name, void *value, intptr_t offset);
7232 +char *xml_conf_set_slot_integer(void **conf, char *name, void *value, intptr_t offset);
7233 +char *xml_conf_set_slot_time(void **conf, char *name, void *value, intptr_t offset);
7235 +int xml_conf_init();
7236 +void xml_conf_clean();
7237 +char *xml_conf_load_file(char *file);
7238 +char *xml_conf_parse_section(void **conf, struct xml_conf_section *section, void *ve);
7239 +int xml_conf_section_register(struct xml_conf_section *section);
7240 +int xml_conf_sections_register(struct xml_conf_section *sections[]);
7242 +#define expand_hms(value) (value) / 3600, ((value) % 3600) / 60, (value) % 60
7244 +#define expand_dhms(value) (value) / 86400, ((value) % 86400) / 3600, ((value) % 3600) / 60, (value) % 60
7246 +enum { XML_CONF_SCALAR = 1, XML_CONF_SUBSECTION = 2 };
7249 diff --git a/sapi/cgi/fpm/zlog.c b/sapi/cgi/fpm/zlog.c
7250 new file mode 100644
7251 index 0000000..2fb6c45
7253 +++ b/sapi/cgi/fpm/zlog.c
7257 + /* (c) 2004-2007 Andrei Nigmatulin */
7259 +#include "fpm_config.h"
7262 +#include <unistd.h>
7264 +#include <string.h>
7265 +#include <stdarg.h>
7266 +#include <sys/time.h>
7271 +#define MAX_LINE_LENGTH 1024
7273 +static int zlog_fd = -1;
7274 +static int zlog_level = ZLOG_NOTICE;
7276 +static const char *level_names[] = {
7277 + [ZLOG_DEBUG] = "DEBUG",
7278 + [ZLOG_NOTICE] = "NOTICE",
7279 + [ZLOG_WARNING] = "WARNING",
7280 + [ZLOG_ERROR] = "ERROR",
7281 + [ZLOG_ALERT] = "ALERT",
7284 +size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len)
7289 + len = strftime(timebuf, timebuf_len, "%b %d %H:%M:%S", localtime_r((const time_t *) &tv->tv_sec, &t));
7290 + len += snprintf(timebuf + len, timebuf_len - len, ".%06d", (int) tv->tv_usec);
7295 +int zlog_set_fd(int new_fd)
7297 + int old_fd = zlog_fd;
7303 +int zlog_set_level(int new_value)
7305 + int old_value = zlog_level;
7307 + zlog_level = new_value;
7312 +void zlog(const char *function, int line, int flags, const char *fmt, ...)
7314 + struct timeval tv;
7315 + char buf[MAX_LINE_LENGTH];
7316 + const size_t buf_size = MAX_LINE_LENGTH;
7319 + int truncated = 0;
7322 + if ((flags & ZLOG_LEVEL_MASK) < zlog_level) {
7326 + saved_errno = errno;
7328 + gettimeofday(&tv, 0);
7330 + len = zlog_print_time(&tv, buf, buf_size);
7332 + len += snprintf(buf + len, buf_size - len, " [%s] %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
7334 + if (len > buf_size - 1) {
7339 + va_start(args, fmt);
7341 + len += vsnprintf(buf + len, buf_size - len, fmt, args);
7345 + if (len >= buf_size) {
7351 + if (flags & ZLOG_HAVE_ERRNO) {
7352 + len += snprintf(buf + len, buf_size - len, ": %s (%d)", strerror(saved_errno), saved_errno);
7353 + if (len >= buf_size) {
7360 + memcpy(buf + buf_size - sizeof("..."), "...", sizeof("...") - 1);
7361 + len = buf_size - 1;
7364 + buf[len++] = '\n';
7366 + write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, buf, len);
7368 diff --git a/sapi/cgi/fpm/zlog.h b/sapi/cgi/fpm/zlog.h
7369 new file mode 100644
7370 index 0000000..b5ac3d9
7372 +++ b/sapi/cgi/fpm/zlog.h
7376 + /* (c) 2004-2007 Andrei Nigmatulin */
7381 +#define ZLOG_STUFF __func__, __LINE__
7385 +int zlog_set_fd(int new_fd);
7386 +int zlog_set_level(int new_value);
7388 +size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len);
7390 +void zlog(const char *function, int line, int flags, const char *fmt, ...)
7391 + __attribute__ ((format(printf,4,5)));
7401 +#define ZLOG_LEVEL_MASK 7
7403 +#define ZLOG_HAVE_ERRNO 0x100
7405 +#define ZLOG_SYSERROR (ZLOG_ERROR | ZLOG_HAVE_ERRNO)