]> git.pld-linux.org Git - packages/php.git/blob - php-fpm.patch
412e074a636e903151e6dc77247d649eb0142aec
[packages/php.git] / php-fpm.patch
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
5 --- a/configure.in
6 +++ b/configure.in
7 @@ -295,6 +295,12 @@ if test "$enable_maintainer_zts" = "yes"; then
8    PTHREADS_FLAGS
9  fi
10  
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)
15 +fi
16 +
17  divert(3)
18  
19  dnl ## In diversion 3 we check for compile-time options to the PHP
20 @@ -511,6 +517,7 @@ AC_CHECK_FUNCS(
21  alphasort \
22  asctime_r \
23  chroot \
24 +clearenv \
25  ctime_r \
26  cuserid \
27  crypt \
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"
39      ;;
40    *)
41 -    install_targets="install-sapi $PHP_INSTALL_CLI_TARGET $install_targets"
42 +    install_targets="install-sapi $install_fpm $PHP_INSTALL_CLI_TARGET $install_targets"
43      ;;
44  esac
45  
46 diff --git a/libevent/ChangeLog b/libevent/ChangeLog
47 new file mode 100644
48 index 0000000..c592139
49 diff --git a/libevent/Makefile.am b/libevent/Makefile.am
50 new file mode 100644
51 index 0000000..5ccfd2c
52 diff --git a/libevent/Makefile.in b/libevent/Makefile.in
53 new file mode 100644
54 index 0000000..0600dcc
55 diff --git a/libevent/README b/libevent/README
56 new file mode 100644
57 index 0000000..b065039
58 diff --git a/libevent/aclocal.m4 b/libevent/aclocal.m4
59 new file mode 100644
60 index 0000000..74de4a1
61 diff --git a/libevent/autogen.sh b/libevent/autogen.sh
62 new file mode 100644
63 index 0000000..218dbf4
64 diff --git a/libevent/buffer.c b/libevent/buffer.c
65 new file mode 100644
66 index 0000000..e66080f
67 diff --git a/libevent/compat/sys/_time.h b/libevent/compat/sys/_time.h
68 new file mode 100644
69 index 0000000..8cabb0d
70 diff --git a/libevent/compat/sys/queue.h b/libevent/compat/sys/queue.h
71 new file mode 100644
72 index 0000000..c0956dd
73 diff --git a/libevent/config.h.in b/libevent/config.h.in
74 new file mode 100644
75 index 0000000..d151f87
76 diff --git a/libevent/configure b/libevent/configure
77 new file mode 100644
78 index 0000000..0a74aec
79 diff --git a/libevent/configure.in b/libevent/configure.in
80 new file mode 100644
81 index 0000000..852d3c5
82 diff --git a/libevent/depcomp b/libevent/depcomp
83 new file mode 100644
84 index 0000000..ffcd540
85 diff --git a/libevent/devpoll.c b/libevent/devpoll.c
86 new file mode 100644
87 index 0000000..cbd2730
88 diff --git a/libevent/epoll.c b/libevent/epoll.c
89 new file mode 100644
90 index 0000000..cf3c859
91 diff --git a/libevent/epoll_sub.c b/libevent/epoll_sub.c
92 new file mode 100644
93 index 0000000..431970c
94 diff --git a/libevent/evbuffer.c b/libevent/evbuffer.c
95 new file mode 100644
96 index 0000000..f2179a5
97 diff --git a/libevent/event-config.h b/libevent/event-config.h
98 new file mode 100644
99 index 0000000..0a278b3
100 diff --git a/libevent/event-fpm.h b/libevent/event-fpm.h
101 new file mode 100644
102 index 0000000..8625ca5
103 diff --git a/libevent/event-internal.h b/libevent/event-internal.h
104 new file mode 100644
105 index 0000000..7485f21
106 diff --git a/libevent/event.3 b/libevent/event.3
107 new file mode 100644
108 index 0000000..5b33ec6
109 diff --git a/libevent/event.c b/libevent/event.c
110 new file mode 100644
111 index 0000000..826b7db
112 diff --git a/libevent/event.h b/libevent/event.h
113 new file mode 100644
114 index 0000000..d67ecb5
115 diff --git a/libevent/evhttp.h b/libevent/evhttp.h
116 new file mode 100644
117 index 0000000..0d35f9e
118 diff --git a/libevent/evport.c b/libevent/evport.c
119 new file mode 100644
120 index 0000000..31523b4
121 diff --git a/libevent/evsignal.h b/libevent/evsignal.h
122 new file mode 100644
123 index 0000000..8be9cbd
124 diff --git a/libevent/evutil.c b/libevent/evutil.c
125 new file mode 100644
126 index 0000000..86205b2
127 diff --git a/libevent/evutil.h b/libevent/evutil.h
128 new file mode 100644
129 index 0000000..0a018b8
130 diff --git a/libevent/http-internal.h b/libevent/http-internal.h
131 new file mode 100644
132 index 0000000..bc9a1ed
133 diff --git a/libevent/http.c b/libevent/http.c
134 new file mode 100644
135 index 0000000..1d60fc5
136 diff --git a/libevent/install-sh b/libevent/install-sh
137 new file mode 100644
138 index 0000000..1a83534
139 diff --git a/libevent/kqueue.c b/libevent/kqueue.c
140 new file mode 100644
141 index 0000000..38a1819
142 diff --git a/libevent/log.c b/libevent/log.c
143 new file mode 100644
144 index 0000000..b62a619
145 diff --git a/libevent/log.h b/libevent/log.h
146 new file mode 100644
147 index 0000000..7bc6632
148 diff --git a/libevent/min_heap.h b/libevent/min_heap.h
149 new file mode 100644
150 index 0000000..d47e563
151 diff --git a/libevent/missing b/libevent/missing
152 new file mode 100644
153 index 0000000..09edd88
154 diff --git a/libevent/poll.c b/libevent/poll.c
155 new file mode 100644
156 index 0000000..b67c6ff
157 diff --git a/libevent/select.c b/libevent/select.c
158 new file mode 100644
159 index 0000000..7faafe4
160 diff --git a/libevent/signal.c b/libevent/signal.c
161 new file mode 100644
162 index 0000000..bcaa3f9
163 diff --git a/libevent/strlcpy-internal.h b/libevent/strlcpy-internal.h
164 new file mode 100644
165 index 0000000..22b5f61
166 diff --git a/libevent/strlcpy.c b/libevent/strlcpy.c
167 new file mode 100644
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
173 @@ -170,6 +170,9 @@
174  /* Define if you have the chroot function.  */
175  #undef HAVE_CHROOT
176  
177 +/* Define if you have the clearenv function.  */
178 +#undef HAVE_CLEARENV
179 +
180  /* Define if you have the crypt function.  */
181  #undef HAVE_CRYPT
182  
183 @@ -935,6 +938,9 @@
184  /*   */
185  #undef PHP_FASTCGI
186  
187 +/* Is experimental fastcgi process manager code activated */
188 +#undef PHP_FASTCGI_PM
189 +
190  /*   */
191  #undef FORCE_CGI_REDIRECT
192  
193 @@ -944,6 +950,27 @@
194  /*   */
195  #undef ENABLE_PATHINFO_CHECK
196  
197 +/* do we have libxml? */
198 +#undef HAVE_LIBXML
199 +
200 +/* do we have prctl? */
201 +#undef HAVE_PRCTL
202 +
203 +/* do we have clock_gettime? */
204 +#undef HAVE_CLOCK_GETTIME
205 +
206 +/* do we have clock_get_time? */
207 +#undef HAVE_CLOCK_GET_TIME
208 +
209 +/* do we have ptrace? */
210 +#undef HAVE_PTRACE
211 +
212 +/* do we have mach_vm_read? */
213 +#undef HAVE_MACH_VM_READ
214 +
215 +/* /proc/pid/mem interface */
216 +#undef PROC_MEM_FILE
217 +
218  /* Define if system uses EBCDIC */
219  #undef CHARSET_EBCDIC
220  
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
225 @@ -1,2 +1,2 @@
226 -$(SAPI_CGI_PATH): $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS)
227 +$(SAPI_CGI_PATH): $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS) $(SAPI_EXTRA_DEPS)
228         $(BUILD_CGI)
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
233 @@ -55,6 +55,9 @@
234  #if HAVE_SYS_WAIT_H
235  #include <sys/wait.h>
236  #endif
237 +#if HAVE_FCNTL_H
238 +#include <fcntl.h>
239 +#endif
240  #include "zend.h"
241  #include "zend_extensions.h"
242  #include "php_ini.h"
243 @@ -83,6 +86,11 @@ int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS;
244  #if PHP_FASTCGI
245  #include "fastcgi.h"
246  
247 +#if PHP_FASTCGI_PM
248 +#include "fpm/fpm.h"
249 +#include "fpm/fpm_request.h"
250 +#endif
251 +
252  #ifndef PHP_WIN32
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;
256  static pid_t pgroup;
257  #endif
258  
259 +static int request_body_fd;
260 +
261  #endif
262  
263 +static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC);
264 +
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[] = {
269         {'w', 0, "strip"},
270         {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
271         {'v', 0, "version"},
272 +#if PHP_FASTCGI_PM
273 +       {'x', 0, "fpm"},
274 +       {'y', 1, "fpm-config"},
275 +#endif
276         {'z', 1, "zend-extension"},
277  #if PHP_FASTCGI
278         {'T', 1, "timing"},
279 @@ -170,6 +186,7 @@ typedef struct _php_cgi_globals_struct {
280         zend_bool impersonate;
281  # endif
282  #endif
283 +       char *error_header;
284  } php_cgi_globals_struct;
285  
286  #ifdef ZTS
287 @@ -474,7 +491,28 @@ static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC)
288  #if PHP_FASTCGI
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);
292 +
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);
296 +
297 +                               if (request_body_filename && *request_body_filename) {
298 +                                       request_body_fd = open(request_body_filename, O_RDONLY);
299 +
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);
303 +                                               return 0;
304 +                                       }
305 +                               }
306 +                       }
307 +
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);
311 +                       } else {
312 +                               tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes);
313 +                       }
314                 } else {
315                         tmp_read_bytes = read(STDIN_FILENO, buffer + read_bytes, count_bytes - read_bytes);
316                 }
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"
322 +#if PHP_FASTCGI_PM
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"
326 +#endif
327 +                          "  -z <file>        Load Zend extension <file>.\n"
328  #if PHP_FASTCGI
329                            "  -T <count>       Measure execution time of script repeated <count> times.\n"
330  #endif
331 @@ -1236,6 +1279,7 @@ PHP_INI_BEGIN()
332  # ifdef PHP_WIN32
333         STD_PHP_INI_ENTRY("fastcgi.impersonate",     "0",  PHP_INI_SYSTEM, OnUpdateBool,   impersonate, php_cgi_globals_struct, php_cgi_globals)
334  # endif
335 +       STD_PHP_INI_ENTRY("fastcgi.error_header",    NULL, PHP_INI_SYSTEM, OnUpdateString, error_header, php_cgi_globals_struct, php_cgi_globals)
336  #endif
337  PHP_INI_END()
338  
339 @@ -1258,6 +1302,7 @@ static void php_cgi_globals_ctor(php_cgi_globals_struct *php_cgi_globals TSRMLS_
340  # ifdef PHP_WIN32
341         php_cgi_globals->impersonate = 0;
342  # endif
343 +       php_cgi_globals->error_header = NULL;
344  #endif
345  }
346  /* }}} */
347 @@ -1290,9 +1335,47 @@ static PHP_MSHUTDOWN_FUNCTION(cgi)
348  static PHP_MINFO_FUNCTION(cgi)
349  {
350         DISPLAY_INI_ENTRIES();
351 +
352 +#if PHP_FASTCGI_PM
353 +
354 +#include "fpm/fpm_autoconf.h"
355 +
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();
360 +#endif
361 +
362  }
363  /* }}} */
364  
365 +#if PHP_FASTCGI
366 +PHP_FUNCTION(fastcgi_finish_request)
367 +{
368 +       fcgi_request *request = (fcgi_request*) SG(server_context);
369 +
370 +       if (fcgi_is_fastcgi() && request->fd >= 0) {
371 +
372 +               php_end_ob_buffers(1 TSRMLS_CC);
373 +               php_header(TSRMLS_C);
374 +
375 +               fcgi_flush(request, 1);
376 +               fcgi_close(request, 0, 0);
377 +               RETURN_TRUE;
378 +       }
379 +
380 +       RETURN_FALSE;
381 +
382 +}
383 +#endif
384 +
385 +function_entry cgi_fcgi_sapi_functions[] = {
386 +#if PHP_FASTCGI
387 +       PHP_FE(fastcgi_finish_request,                          NULL)
388 +#endif
389 +       {NULL, NULL, NULL}
390 +};
391 +
392  static zend_module_entry cgi_module_entry = {
393         STANDARD_MODULE_HEADER,
394  #if PHP_FASTCGI
395 @@ -1300,7 +1383,7 @@ static zend_module_entry cgi_module_entry = {
396  #else
397         "cgi",
398  #endif
399 -       NULL, 
400 +       cgi_fcgi_sapi_functions, 
401         PHP_MINIT(cgi), 
402         PHP_MSHUTDOWN(cgi), 
403         NULL, 
404 @@ -1340,6 +1423,7 @@ int main(int argc, char *argv[])
405         char *bindpath = NULL;
406         int fcgi_fd = 0;
407         fcgi_request request;
408 +       char *fpm_config = NULL;
409         int repeats = 1;
410         int benchmark = 0;
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;
415                                 break;
416 +#if PHP_FASTCGI_PM
417 +                       case 'y':
418 +                               fpm_config = php_optarg;
419 +                               break;
420 +                       case 'x':
421 +                               fpm = 1;
422 +                               break;
423 +#endif
424  
425                 }
426  
427 @@ -1524,6 +1616,19 @@ consult the installation file that came with this distribution, or visit \n\
428  #endif /* FORCE_CGI_REDIRECT */
429  
430  #if PHP_FASTCGI
431 +#if PHP_FASTCGI_PM
432 +       if (fpm) {
433 +               if (0 > fpm_init(argc, argv, fpm_config)) {
434 +                       return FAILURE;
435 +               }
436 +
437 +               fcgi_fd = fpm_run(&max_requests);
438 +
439 +               fcgi_set_is_fastcgi(fastcgi = 1);
440 +       }
441 +       else
442 +#endif
443 +
444         if (bindpath) {
445                 fcgi_fd = fcgi_listen(bindpath, 128);
446                 if (fcgi_fd < 0) {
447 @@ -1538,6 +1643,9 @@ consult the installation file that came with this distribution, or visit \n\
448         
449         if (fastcgi) {
450                 /* How many times to run PHP scripts before dying */
451 +#if PHP_FASTCGI_PM
452 +               if (!fpm)
453 +#endif
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\
458  
459  #ifndef PHP_WIN32
460         /* Pre-fork, if required */
461 +#if PHP_FASTCGI_PM
462 +       if (!fpm)
463 +#endif
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\
468  #endif
469  
470  #if PHP_FASTCGI
471 +               request_body_fd = -1;
472 +
473                 SG(server_context) = (void *) &request;
474  #else
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);
478                 CG(interactive) = 0;
479  
480 +#if PHP_FASTCGI_PM
481 +               if (fpm) fpm_request_info();
482 +#endif
483 +
484                 if (!cgi
485  #if PHP_FASTCGI
486                         && !fastcgi
487 @@ -1994,6 +2111,10 @@ consult the installation file that came with this distribution, or visit \n\
488                         }
489                 }
490  
491 +#if PHP_FASTCGI_PM
492 +               if (fpm) fpm_request_executing();
493 +#endif
494 +
495                 switch (behavior) {
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\
499  
500  #if PHP_FASTCGI
501  fastcgi_request_done:
502 +
503 +               if (request_body_fd != -1) close(request_body_fd);
504 +
505 +               request_body_fd = -2;
506  #endif
507                 {
508                         char *path_translated;
509 @@ -2059,6 +2184,16 @@ fastcgi_request_done:
510                                 SG(request_info).path_translated = path_translated;
511                         }
512  
513 +                       if (EG(exit_status) == 255) {
514 +                               if (CGIG(error_header) && *CGIG(error_header)) {
515 +                                       sapi_header_line ctr = {0};
516 +
517 +                                       ctr.line = CGIG(error_header);
518 +                                       ctr.line_len = strlen(CGIG(error_header));
519 +                                       sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
520 +                               }
521 +                       }
522 +                       
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:
527                                 if (bindpath) {
528                                         free(bindpath);
529                                 }
530 -                               if (max_requests != 1) {
531 -                                       /* no need to return exit_status of the last request */
532 -                                       exit_status = 0;
533 -                               }
534                                 break;
535                         }
536                         /* end of fastcgi loop */
537                 }
538                 fcgi_shutdown();
539 +
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 */
542 +                       ) {
543 +                       exit_status = 0;
544 +               }
545 +               else {
546 +                       exit_status = 255;
547 +               }
548  #endif
549  
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)
558  
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)
562 +
563  dnl
564  dnl CGI setup
565  dnl
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)
569  
570 +    dnl --enable-fpm
571 +    if test "$PHP_FASTCGI" = "yes"; then
572 +      AC_MSG_CHECKING(whether to enable FastCGI Process Manager)
573 +      if test "$PHP_FPM" = "yes"; then
574 +        PHP_FASTCGI_PM=1
575 +      else
576 +        PHP_FASTCGI_PM=0
577 +      fi
578 +      AC_MSG_RESULT($PHP_FPM)
579 +    else
580 +      PHP_FASTCGI_PM=0
581 +    fi
582 +    AC_DEFINE_UNQUOTED(PHP_FASTCGI_PM, $PHP_FASTCGI_PM, [Is experimental fastcgi process manager code activated])
583 +
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)"
589          ;;
590        *darwin*)
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)"
593        ;;
594        *)
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)"
597        ;;
598      esac
599  
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
604 @@ -27,6 +27,11 @@
605  #include <stdarg.h>
606  #include <errno.h>
607  
608 +#if PHP_FASTCGI_PM
609 +#include "fpm/fpm.h"
610 +#include "fpm/fpm_request.h"
611 +#endif
612 +
613  #ifdef _WIN32
614  
615  #include <windows.h>
616 @@ -234,6 +239,8 @@ int fcgi_init(void)
617                 } else {
618                         return is_fastcgi = 0;
619                 }
620 +
621 +               fcgi_set_allowed_clients(getenv("FCGI_WEB_SERVER_ADDRS"));
622  #endif
623         }
624         return is_fastcgi;
625 @@ -249,14 +256,26 @@ int fcgi_is_fastcgi(void)
626         }
627  }
628  
629 +void fcgi_set_is_fastcgi(int new_value)
630 +{
631 +       is_fastcgi = new_value;
632 +}
633 +
634 +void fcgi_set_in_shutdown(int new_value)
635 +{
636 +       in_shutdown = new_value;
637 +}
638 +
639  void fcgi_shutdown(void)
640  {
641         if (is_initialized) {
642                 zend_hash_destroy(&fcgi_mgmt_vars);
643         }
644         is_fastcgi = 0;
645 +
646         if (allowed_clients) {
647                 free(allowed_clients);
648 +               allowed_clients = 0;
649         }
650  }
651  
652 @@ -330,6 +349,41 @@ out_fail:
653  }
654  #endif
655  
656 +void fcgi_set_allowed_clients(char *ip)
657 +{
658 +    char *cur, *end;
659 +    int n;
660 +           
661 +    if (ip) {
662 +       ip = strdup(ip);
663 +       cur = ip;
664 +       n = 0;
665 +       while (*cur) {
666 +               if (*cur == ',') n++;
667 +               cur++;
668 +       }
669 +               if (allowed_clients) free(allowed_clients);
670 +       allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
671 +       n = 0;
672 +       cur = ip;
673 +       while (cur) {
674 +               end = strchr(cur, ',');
675 +               if (end) {
676 +                       *end = 0;
677 +                       end++;
678 +               }
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);
682 +               }
683 +               n++;
684 +               cur = end;
685 +       }
686 +       allowed_clients[n] = INADDR_NONE;
687 +               free(ip);
688 +       }
689 +}
690 +
691  static int is_port_number(const char *bindpath)
692  {
693         while (*bindpath) {
694 @@ -458,38 +512,6 @@ int fcgi_listen(const char *path, int backlog)
695  
696         if (!tcp) {
697                 chmod(path, 0777);
698 -       } else {
699 -                       char *ip = getenv("FCGI_WEB_SERVER_ADDRS");
700 -                       char *cur, *end;
701 -                       int n;
702 -                       
703 -                       if (ip) {
704 -                               ip = strdup(ip);
705 -                               cur = ip;
706 -                               n = 0;
707 -                               while (*cur) {
708 -                                       if (*cur == ',') n++;
709 -                                       cur++;
710 -                               }
711 -                               allowed_clients = malloc(sizeof(in_addr_t) * (n+2));
712 -                               n = 0;
713 -                               cur = ip;
714 -                               while (cur) {
715 -                                       end = strchr(cur, ',');
716 -                                       if (end) {
717 -                                               *end = 0;
718 -                                               end++;
719 -                                       }
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);
723 -                                       }
724 -                                       n++;
725 -                                       cur = end;
726 -                               }
727 -                               allowed_clients[n] = INADDR_NONE;
728 -                       free(ip);
729 -               }
730         }
731  
732         if (!is_initialized) {
733 @@ -866,7 +888,7 @@ int fcgi_read(fcgi_request *req, char *str, int len)
734         return n;
735  }
736  
737 -static inline void fcgi_close(fcgi_request *req, int force, int destroy)
738 +void fcgi_close(fcgi_request *req, int force, int destroy)
739  {
740         if (destroy) {
741                 zend_hash_destroy(&req->env);
742 @@ -906,6 +928,10 @@ static inline void fcgi_close(fcgi_request *req, int force, int destroy)
743                 close(req->fd);
744  #endif
745                 req->fd = -1;
746 +
747 +#if PHP_FASTCGI_PM
748 +               if (fpm) fpm_request_finished();
749 +#endif
750         }
751  }
752  
753 @@ -953,6 +979,10 @@ int fcgi_accept_request(fcgi_request *req)
754                                         sa_t sa;
755                                         socklen_t len = sizeof(sa);
756  
757 +#if PHP_FASTCGI_PM
758 +                                       if (fpm) fpm_request_accepting();
759 +#endif
760 +
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)
765                                 break;
766  #else
767                                 if (req->fd >= 0) {
768 +
769 +#if PHP_FASTCGI_PM
770 +                                       if (fpm) fpm_request_reading_headers();
771 +#endif
772 +
773  #if defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL)
774                                         struct pollfd fds;
775                                         int ret;
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 {
781  int fcgi_init(void);
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);
793  
794 +void fcgi_close(fcgi_request *req, int force, int destroy);
795 +
796  #ifdef PHP_WIN32
797  void fcgi_impersonate(void);
798  #endif
799 diff --git a/sapi/cgi/fpm/Makefile.frag b/sapi/cgi/fpm/Makefile.frag
800 new file mode 100644
801 index 0000000..15e6637
802 --- /dev/null
803 +++ b/sapi/cgi/fpm/Makefile.frag
804 @@ -0,0 +1,21 @@
805 +
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)" ; \
816 +       else \
817 +               dest=`basename "$(php_fpm_conf_path)"` ; \
818 +       fi ; \
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
822 +
823 +$(top_builddir)/libevent/libevent.a: $(top_builddir)/libevent/Makefile
824 +       cd $(top_builddir)/libevent && $(MAKE) libevent.a
825 +
826 diff --git a/sapi/cgi/fpm/acinclude.m4 b/sapi/cgi/fpm/acinclude.m4
827 new file mode 100644
828 index 0000000..e4d1f3e
829 --- /dev/null
830 +++ b/sapi/cgi/fpm/acinclude.m4
831 @@ -0,0 +1,377 @@
832 +
833 +AC_DEFUN([AC_FPM_CHECK_FUNC],
834 +[
835 +       SAVED_CFLAGS="$CFLAGS"
836 +       CFLAGS="$CFLAGS $2"
837 +       SAVED_LIBS="$LIBS"
838 +       LIBS="$LIBS $3"
839 +
840 +       AC_CHECK_FUNC([$1],[$4],[$5])
841 +
842 +       CFLAGS="$SAVED_CFLAGS"
843 +       LIBS="$SAVED_LIBS"
844 +])
845 +
846 +AC_DEFUN([AC_FPM_LIBEVENT],
847 +[
848 +       AC_ARG_WITH([libevent],
849 +       [  --with-libevent=DIR         FPM: libevent install directory])
850 +
851 +       LIBEVENT_CFLAGS=""
852 +       LIBEVENT_LIBS="-levent"
853 +       LIBEVENT_INCLUDE_PATH=""
854 +
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"
859 +       fi
860 +
861 +       AC_MSG_CHECKING([for event.h])
862 +
863 +       found=no
864 +
865 +       for dir in "$LIBEVENT_INCLUDE_PATH" /usr/include ; do
866 +               if test -r "$dir/event.h" ; then
867 +                       found=yes
868 +                       break
869 +               fi
870 +       done
871 +
872 +       AC_MSG_RESULT([$found])
873 +
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.])])
876 +
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.])])
879 +
880 +])
881 +
882 +AC_DEFUN([AC_FPM_LIBXML],
883 +[
884 +       AC_MSG_RESULT([checking for XML configuration])
885 +
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], "")]
890 +       )
891 +
892 +       if test "x$XMLCONFIG" = "x"; then
893 +               AC_MSG_ERROR([XML configuration could not be found])
894 +       else
895 +        AC_MSG_CHECKING([for libxml library])
896 +
897 +               if test ! -x "$XMLCONFIG"; then
898 +                       AC_MSG_ERROR([$XMLCONFIG cannot be executed])
899 +               fi
900 +
901 +               LIBXML_LIBS="`$XMLCONFIG --libs`"
902 +               LIBXML_CFLAGS="`$XMLCONFIG --cflags`"
903 +               LIBXML_VERSION="`$XMLCONFIG --version`"
904 +
905 +        AC_MSG_RESULT([yes, $LIBXML_VERSION])
906 +
907 +               AC_FPM_CHECK_FUNC([xmlParseFile], [$LIBXML_CFLAGS], [$LIBXML_LIBS], ,
908 +                       [AC_MSG_ERROR([Failed to link with libxml])])
909 +
910 +               AC_DEFINE(HAVE_LIBXML, 1, [do we have libxml?])
911 +       fi
912 +])
913 +
914 +AC_DEFUN([AC_FPM_JUDY],
915 +[
916 +       AC_ARG_WITH([Judy],
917 +       [  --with-Judy=DIR             FPM: Judy install directory])
918 +
919 +       JUDY_CFLAGS=""
920 +       JUDY_LIBS="-lJudy"
921 +       JUDY_INCLUDE_PATH=""
922 +
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"
927 +       fi
928 +
929 +       AC_MSG_CHECKING([for Judy.h])
930 +
931 +       found=no
932 +
933 +       for dir in "$JUDY_INCLUDE_PATH" /usr/include ; do
934 +               if test -r "$dir/Judy.h" ; then
935 +                       found=yes
936 +                       break
937 +               fi
938 +       done
939 +
940 +       AC_MSG_RESULT([$found])
941 +
942 +       AC_FPM_CHECK_FUNC([JudyLCount], [$JUDY_CFLAGS], [$JUDY_LIBS], ,
943 +               [AC_MSG_ERROR([Failed to link with Judy])])
944 +
945 +])
946 +
947 +AC_DEFUN([AC_FPM_CLOCK],
948 +[
949 +       have_clock_gettime=no
950 +
951 +       AC_MSG_CHECKING([for clock_gettime])
952 +
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])
956 +       ], [
957 +               AC_MSG_RESULT([no])
958 +       ])
959 +
960 +       if test "$have_clock_gettime" = "no"; then
961 +               AC_MSG_CHECKING([for clock_gettime in -lrt])
962 +
963 +               SAVED_LIBS="$LIBS"
964 +               LIBS="$LIBS -lrt"
965 +
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])
969 +               ], [
970 +                       LIBS="$SAVED_LIBS"
971 +                       AC_MSG_RESULT([no])
972 +               ])
973 +       fi
974 +
975 +       if test "$have_clock_gettime" = "yes"; then
976 +               AC_DEFINE([HAVE_CLOCK_GETTIME], 1, [do we have clock_gettime?])
977 +       fi
978 +
979 +       have_clock_get_time=no
980 +
981 +       if test "$have_clock_gettime" = "no"; then
982 +               AC_MSG_CHECKING([for clock_get_time])
983 +
984 +               AC_TRY_RUN([ #include <mach/mach.h>
985 +                       #include <mach/clock.h>
986 +                       #include <mach/mach_error.h>
987 +
988 +                       int main()
989 +                       {
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);
992 +
993 +                               if (ret != KERN_SUCCESS) {
994 +                                       return 1;
995 +                               }
996 +
997 +                               ret = clock_get_time(aClock, &aTime);
998 +                               if (ret != KERN_SUCCESS) {
999 +                                       return 2;
1000 +                               }
1001 +
1002 +                               return 0;
1003 +                       }
1004 +               ], [
1005 +                       have_clock_get_time=yes
1006 +                       AC_MSG_RESULT([yes])
1007 +               ], [
1008 +                       AC_MSG_RESULT([no])
1009 +               ])
1010 +       fi
1011 +
1012 +       if test "$have_clock_get_time" = "yes"; then
1013 +               AC_DEFINE([HAVE_CLOCK_GET_TIME], 1, [do we have clock_get_time?])
1014 +       fi
1015 +])
1016 +
1017 +AC_DEFUN([AC_FPM_TRACE],
1018 +[
1019 +       have_ptrace=no
1020 +       have_broken_ptrace=no
1021 +
1022 +       AC_MSG_CHECKING([for ptrace])
1023 +
1024 +       AC_TRY_COMPILE([
1025 +               #include <sys/types.h>
1026 +               #include <sys/ptrace.h> ], [ptrace(0, 0, (void *) 0, 0);], [
1027 +               have_ptrace=yes
1028 +               AC_MSG_RESULT([yes])
1029 +       ], [
1030 +               AC_MSG_RESULT([no])
1031 +       ])
1032 +
1033 +       if test "$have_ptrace" = "yes"; then
1034 +               AC_MSG_CHECKING([whether ptrace works])
1035 +
1036 +               AC_TRY_RUN([
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>
1043 +
1044 +                       #if !defined(PTRACE_ATTACH) && defined(PT_ATTACH)
1045 +                       #define PTRACE_ATTACH PT_ATTACH
1046 +                       #endif
1047 +
1048 +                       #if !defined(PTRACE_DETACH) && defined(PT_DETACH)
1049 +                       #define PTRACE_DETACH PT_DETACH
1050 +                       #endif
1051 +
1052 +                       #if !defined(PTRACE_PEEKDATA) && defined(PT_READ_D)
1053 +                       #define PTRACE_PEEKDATA PT_READ_D
1054 +                       #endif
1055 +
1056 +                       int main()
1057 +                       {
1058 +                               long v1 = (unsigned int) -1; /* copy will fail if sizeof(long) == 8 and we've got "int ptrace()" */
1059 +                               long v2;
1060 +                               pid_t child;
1061 +                               int status;
1062 +
1063 +                               if ( (child = fork()) ) { /* parent */
1064 +                                       int ret = 0;
1065 +
1066 +                                       if (0 > ptrace(PTRACE_ATTACH, child, 0, 0)) {
1067 +                                               return 1;
1068 +                                       }
1069 +
1070 +                                       waitpid(child, &status, 0);
1071 +
1072 +                       #ifdef PT_IO
1073 +                                       struct ptrace_io_desc ptio = {
1074 +                                               .piod_op = PIOD_READ_D,
1075 +                                               .piod_offs = &v1,
1076 +                                               .piod_addr = &v2,
1077 +                                               .piod_len = sizeof(v1)
1078 +                                       };
1079 +
1080 +                                       if (0 > ptrace(PT_IO, child, (void *) &ptio, 0)) {
1081 +                                               ret = 1;
1082 +                                       }
1083 +                       #else
1084 +                                       errno = 0;
1085 +
1086 +                                       v2 = ptrace(PTRACE_PEEKDATA, child, (void *) &v1, 0);
1087 +
1088 +                                       if (errno) {
1089 +                                               ret = 1;
1090 +                                       }
1091 +                       #endif
1092 +                                       ptrace(PTRACE_DETACH, child, (void *) 1, 0);
1093 +
1094 +                                       kill(child, SIGKILL);
1095 +
1096 +                                       return ret ? ret : (v1 != v2);
1097 +                               }
1098 +                               else { /* child */
1099 +                                       sleep(10);
1100 +                                       return 0;
1101 +                               }
1102 +                       }
1103 +               ], [
1104 +                       AC_MSG_RESULT([yes])
1105 +               ], [
1106 +                       have_ptrace=no
1107 +                       have_broken_ptrace=yes
1108 +                       AC_MSG_RESULT([no])
1109 +               ])
1110 +       fi
1111 +
1112 +       if test "$have_ptrace" = "yes"; then
1113 +               AC_DEFINE([HAVE_PTRACE], 1, [do we have ptrace?])
1114 +       fi
1115 +
1116 +       have_mach_vm_read=no
1117 +
1118 +       if test "$have_broken_ptrace" = "yes"; then
1119 +               AC_MSG_CHECKING([for mach_vm_read])
1120 +
1121 +               AC_TRY_COMPILE([ #include <mach/mach.h>
1122 +                       #include <mach/mach_vm.h>
1123 +               ], [
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);
1125 +               ], [
1126 +                       have_mach_vm_read=yes
1127 +                       AC_MSG_RESULT([yes])
1128 +               ], [
1129 +                       AC_MSG_RESULT([no])
1130 +               ])
1131 +       fi
1132 +
1133 +       if test "$have_mach_vm_read" = "yes"; then
1134 +               AC_DEFINE([HAVE_MACH_VM_READ], 1, [do we have mach_vm_read?])
1135 +       fi
1136 +
1137 +       proc_mem_file=""
1138 +
1139 +       if test -r /proc/$$/mem ; then
1140 +               proc_mem_file="mem"
1141 +       else
1142 +               if test -r /proc/$$/as ; then
1143 +                       proc_mem_file="as"
1144 +               fi
1145 +       fi
1146 +
1147 +       if test -n "$proc_mem_file" ; then
1148 +               AC_MSG_CHECKING([for proc mem file])
1149 +
1150 +               AC_TRY_RUN([
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>
1159 +                       int main()
1160 +                       {
1161 +                               long v1 = (unsigned int) -1, v2 = 0;
1162 +                               char buf[128];
1163 +                               int fd;
1164 +                               sprintf(buf, "/proc/%d/$proc_mem_file", getpid());
1165 +                               fd = open(buf, O_RDONLY);
1166 +                               if (0 > fd) {
1167 +                                       return 1;
1168 +                               }
1169 +                               if (sizeof(long) != pread(fd, &v2, sizeof(long), (uintptr_t) &v1)) {
1170 +                                       close(fd);
1171 +                                       return 1;
1172 +                               }
1173 +                               close(fd);
1174 +                               return v1 != v2;
1175 +                       }
1176 +               ], [
1177 +                       AC_MSG_RESULT([$proc_mem_file])
1178 +               ], [
1179 +                       proc_mem_file=""
1180 +                       AC_MSG_RESULT([no])
1181 +               ])
1182 +       fi
1183 +
1184 +       if test -n "$proc_mem_file"; then
1185 +               AC_DEFINE_UNQUOTED([PROC_MEM_FILE], "$proc_mem_file", [/proc/pid/mem interface])
1186 +       fi
1187 +
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"
1194 +       fi
1195 +
1196 +])
1197 +
1198 +AC_DEFUN([AC_FPM_PRCTL],
1199 +[
1200 +       AC_MSG_CHECKING([for prctl])
1201 +
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])
1205 +       ], [
1206 +               AC_MSG_RESULT([no])
1207 +       ])
1208 +])
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
1212 --- /dev/null
1213 +++ b/sapi/cgi/fpm/conf/php-fpm.conf.in
1214 @@ -0,0 +1,156 @@
1215 +<?xml version="1.0" ?>
1216 +<configuration>
1217 +
1218 +       All relative paths in this config are relative to php's install prefix
1219 +
1220 +       <section name="global_options">
1221 +
1222 +               Pid file
1223 +               <value name="pid_file">@php_fpm_pid_path@</value>
1224 +
1225 +               Error log file
1226 +               <value name="error_log">@php_fpm_log_path@</value>
1227 +
1228 +               Log level
1229 +               <value name="log_level">notice</value>
1230 +
1231 +               When this amount of php processes exited with SIGSEGV or SIGBUS ...
1232 +               <value name="emergency_restart_threshold">10</value>
1233 +
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>
1237 +
1238 +               Time limit on waiting child's reaction on signals from master
1239 +               <value name="process_control_timeout">5s</value>
1240 +
1241 +               Set to 'no' to debug fpm
1242 +               <value name="daemonize">yes</value>
1243 +
1244 +       </section>
1245 +
1246 +       <workers>
1247 +
1248 +               <section name="pool">
1249 +
1250 +                       Name of pool. Used in logs and stats.
1251 +                       <value name="name">default</value>
1252 +
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>
1256 +
1257 +                       <value name="listen_options">
1258 +
1259 +                               Set listen(2) backlog
1260 +                               <value name="backlog">-1</value>
1261 +
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>
1268 +                       </value>
1269 +
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>                                                          -->
1274 +                       </value>
1275 +
1276 +                       Unix user of processes
1277 +               <!--    <value name="user">nobody</value>                               -->
1278 +
1279 +                       Unix group of processes
1280 +               <!--    <value name="group">@php_fpm_group@</value>             -->
1281 +
1282 +                       Process manager settings
1283 +                       <value name="pm">
1284 +
1285 +                               Sets style of controling worker process count.
1286 +                               Valid values are 'static' and 'apache-like'
1287 +                               <value name="style">static</value>
1288 +
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>
1294 +
1295 +                               Settings group for 'apache-like' pm style
1296 +                               <value name="apache_like">
1297 +
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>
1301 +
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>
1305 +
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>
1309 +
1310 +                               </value>
1311 +
1312 +                       </value>
1313 +
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
1316 +                       '0s' means 'off'
1317 +                       <value name="request_terminate_timeout">0s</value>
1318 +
1319 +                       The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
1320 +                       '0s' means 'off'
1321 +                       <value name="request_slowlog_timeout">0s</value>
1322 +
1323 +                       The log file for slow requests
1324 +                       <value name="slowlog">logs/slow.log</value>
1325 +
1326 +                       Set open file desc rlimit
1327 +                       <value name="rlimit_files">1024</value>
1328 +
1329 +                       Set max core size rlimit
1330 +                       <value name="rlimit_core">0</value>
1331 +
1332 +                       Chroot to this directory at the start, absolute path
1333 +                       <value name="chroot"></value>
1334 +
1335 +                       Chdir to this directory at the start, absolute path
1336 +                       <value name="chdir"></value>
1337 +
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>
1341 +
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>
1347 +
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>
1352 +
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>
1364 +                       </value>
1365 +
1366 +               </section>
1367 +
1368 +       </workers>
1369 +
1370 +</configuration>
1371 diff --git a/sapi/cgi/fpm/config.m4 b/sapi/cgi/fpm/config.m4
1372 new file mode 100644
1373 index 0000000..65ea5e2
1374 --- /dev/null
1375 +++ b/sapi/cgi/fpm/config.m4
1376 @@ -0,0 +1,141 @@
1377 +
1378 +FPM_VERSION="0.5.14"
1379 +
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)
1382 +
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)
1385 +
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)
1388 +
1389 +FPM_SOURCES="fpm.c \
1390 +       fpm_conf.c \
1391 +       fpm_signals.c \
1392 +       fpm_children.c \
1393 +       fpm_worker_pool.c \
1394 +       fpm_unix.c \
1395 +       fpm_cleanup.c \
1396 +       fpm_sockets.c \
1397 +       fpm_stdio.c \
1398 +       fpm_env.c \
1399 +       fpm_events.c \
1400 +       fpm_php.c \
1401 +       fpm_php_trace.c \
1402 +       fpm_process_ctl.c \
1403 +       fpm_request.c \
1404 +       fpm_clock.c \
1405 +       fpm_shm.c \
1406 +       fpm_shm_slots.c \
1407 +       xml_config.c \
1408 +       zlog.c"
1409 +
1410 +dnl AC_FPM_LIBEVENT
1411 +AC_FPM_LIBXML
1412 +AC_FPM_PRCTL
1413 +AC_FPM_CLOCK
1414 +AC_FPM_TRACE
1415 +dnl AC_FPM_JUDY
1416 +
1417 +LIBEVENT_CFLAGS="-I$abs_srcdir/libevent"
1418 +LIBEVENT_LIBS="$abs_builddir/libevent/libevent.a"
1419 +
1420 +SAPI_EXTRA_DEPS="$LIBEVENT_LIBS"
1421 +
1422 +FPM_CFLAGS="$LIBEVENT_CFLAGS $LIBXML_CFLAGS $JUDY_CFLAGS"
1423 +
1424 +dnl FPM_CFLAGS="$FPM_CFLAGS -DJUDYERROR_NOTEST" # for Judy
1425 +FPM_CFLAGS="$FPM_CFLAGS -I$abs_srcdir/sapi/cgi" # for fastcgi.h
1426 +
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"
1431 +fi
1432 +
1433 +if test -n "$FPM_WERROR" ; then
1434 +       FPM_ADD_CFLAGS="$FPM_ADD_CFLAGS -Werror"
1435 +fi
1436 +
1437 +FPM_CFLAGS="$FPM_ADD_CFLAGS $FPM_CFLAGS"
1438 +
1439 +PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/sapi/cgi/fpm/Makefile.frag)
1440 +
1441 +PHP_ADD_SOURCES(sapi/cgi/fpm, $FPM_SOURCES, $FPM_CFLAGS, sapi)
1442 +
1443 +PHP_ADD_BUILD_DIR(sapi/cgi/fpm)
1444 +
1445 +install_fpm="install-fpm"
1446 +
1447 +PHP_CONFIGURE_PART(Configuring libevent)
1448 +
1449 +test -d "$abs_builddir/libevent" || mkdir -p $abs_builddir/libevent
1450 +
1451 +dnl this is a bad hack
1452 +
1453 +chmod +x "$abs_srcdir/libevent/configure" \
1454 +               "$abs_srcdir/libevent/depcomp" \
1455 +               "$abs_srcdir/libevent/install-sh" \
1456 +               "$abs_srcdir/libevent/missing"
1457 +
1458 +libevent_configure="cd $abs_builddir/libevent ; CFLAGS=\"$CFLAGS $GCC_CFLAGS\" $abs_srcdir/libevent/configure --disable-shared"
1459 +
1460 +(eval $libevent_configure)
1461 +
1462 +if test ! -f "$abs_builddir/libevent/Makefile" ; then
1463 +       echo "Failed to configure libevent" >&2
1464 +       exit 1
1465 +fi
1466 +
1467 +dnl another hack for stealing libevent dependant library list
1468 +
1469 +LIBEVENT_LIBS="$LIBEVENT_LIBS `echo "@LIBS@" | $abs_builddir/libevent/config.status --file=-:-`"
1470 +
1471 +SAPI_EXTRA_LIBS="$LIBEVENT_LIBS $LIBXML_LIBS $JUDY_LIBS"
1472 +
1473 +
1474 +if test "$prefix" = "NONE" ; then
1475 +       fpm_prefix=/usr/local
1476 +else
1477 +       fpm_prefix="$prefix"
1478 +fi
1479 +
1480 +if test "$PHP_FPM_CONF" = "\$prefix/etc/php-fpm.conf" ; then
1481 +       php_fpm_conf_path="$fpm_prefix/etc/php-fpm.conf"
1482 +else
1483 +       php_fpm_conf_path="$PHP_FPM_CONF"
1484 +fi
1485 +
1486 +if test "$PHP_FPM_LOG" = "\$prefix/logs/php-fpm.log" ; then
1487 +       php_fpm_log_path="$fpm_prefix/logs/php-fpm.log"
1488 +else
1489 +       php_fpm_log_path="$PHP_FPM_LOG"
1490 +fi
1491 +
1492 +if test "$PHP_FPM_PID" = "\$prefix/logs/php-fpm.pid" ; then
1493 +       php_fpm_pid_path="$fpm_prefix/logs/php-fpm.pid"
1494 +else
1495 +       php_fpm_pid_path="$PHP_FPM_PID"
1496 +fi
1497 +
1498 +
1499 +if grep nobody /etc/group >/dev/null 2>&1; then
1500 +       php_fpm_group=nobody
1501 +else
1502 +       if grep nogroup /etc/group >/dev/null 2>&1; then
1503 +               php_fpm_group=nogroup
1504 +       else
1505 +               php_fpm_group=nobody
1506 +       fi
1507 +fi
1508 +
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)
1514 +
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
1521 --- /dev/null
1522 +++ b/sapi/cgi/fpm/fpm.c
1523 @@ -0,0 +1,84 @@
1524 +
1525 +       /* $Id$ */
1526 +       /* (c) 2007,2008 Andrei Nigmatulin */
1527 +
1528 +#include "fpm_config.h"
1529 +
1530 +#include <stdlib.h> /* for exit */
1531 +
1532 +#include "fpm.h"
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"
1545 +#include "zlog.h"
1546 +
1547 +int fpm;
1548 +
1549 +struct fpm_globals_s fpm_globals;
1550 +
1551 +int fpm_init(int argc, char **argv, char *config)
1552 +{
1553 +       fpm_globals.argc = argc;
1554 +       fpm_globals.argv = argv;
1555 +       fpm_globals.config = config;
1556 +
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()) {
1568 +               return -1;
1569 +       }
1570 +
1571 +       if (0 > fpm_conf_write_pid()) {
1572 +               return -1;
1573 +       }
1574 +
1575 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "fpm is running, pid %d", (int) fpm_globals.parent_pid);
1576 +
1577 +       return 0;
1578 +}
1579 +
1580 +/*     children: return listening socket
1581 +       parent: never return */
1582 +int fpm_run(int *max_requests)
1583 +{
1584 +       struct fpm_worker_pool_s *wp;
1585 +
1586 +       /* create initial children in all pools */
1587 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1588 +               int is_parent;
1589 +
1590 +               is_parent = fpm_children_create_initial(wp);
1591 +
1592 +               if (!is_parent) {
1593 +                       goto run_child;
1594 +               }
1595 +       }
1596 +
1597 +       /* run event loop forever */
1598 +       fpm_event_loop();
1599 +
1600 +run_child: /* only workers reach this point */
1601 +
1602 +       fpm_cleanups_run(FPM_CLEANUP_CHILD);
1603 +
1604 +       *max_requests = fpm_globals.max_requests;
1605 +       return fpm_globals.listening_socket;
1606 +}
1607 +
1608 diff --git a/sapi/cgi/fpm/fpm.h b/sapi/cgi/fpm/fpm.h
1609 new file mode 100644
1610 index 0000000..eebd2dc
1611 --- /dev/null
1612 +++ b/sapi/cgi/fpm/fpm.h
1613 @@ -0,0 +1,30 @@
1614 +
1615 +       /* $Id$ */
1616 +       /* (c) 2007,2008 Andrei Nigmatulin */
1617 +
1618 +#ifndef FPM_H
1619 +#define FPM_H 1
1620 +
1621 +#include <unistd.h>
1622 +
1623 +int fpm_run(int *max_requests);
1624 +int fpm_init(int argc, char **argv, char *config);
1625 +
1626 +struct fpm_globals_s {
1627 +       pid_t parent_pid;
1628 +       int argc;
1629 +       char **argv;
1630 +       char *config;
1631 +       int running_children;
1632 +       int error_log_fd;
1633 +       int log_level;
1634 +       int listening_socket; /* for this child */
1635 +       int max_requests; /* for this child */
1636 +       int is_child;
1637 +};
1638 +
1639 +extern struct fpm_globals_s fpm_globals;
1640 +
1641 +extern int fpm;
1642 +
1643 +#endif
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
1647 --- /dev/null
1648 +++ b/sapi/cgi/fpm/fpm_arrays.h
1649 @@ -0,0 +1,110 @@
1650 +
1651 +       /* $Id$ */
1652 +       /* (c) 2007,2008 Andrei Nigmatulin */
1653 +
1654 +#ifndef FPM_ARRAYS_H
1655 +#define FPM_ARRAYS_H 1
1656 +
1657 +#include <stdlib.h>
1658 +#include <string.h>
1659 +
1660 +struct fpm_array_s {
1661 +       void *data;
1662 +       size_t sz;
1663 +       size_t used;
1664 +       size_t allocated;
1665 +};
1666 +
1667 +static inline struct fpm_array_s *fpm_array_init(struct fpm_array_s *a, unsigned int sz, unsigned int initial_num)
1668 +{
1669 +       void *allocated = 0;
1670 +
1671 +       if (!a) {
1672 +               a = malloc(sizeof(struct fpm_array_s));
1673 +
1674 +               if (!a) {
1675 +                       return 0;
1676 +               }
1677 +
1678 +               allocated = a;
1679 +       }
1680 +
1681 +       a->sz = sz;
1682 +
1683 +       a->data = calloc(sz, initial_num);
1684 +
1685 +       if (!a->data) {
1686 +               free(allocated);
1687 +               return 0;
1688 +       }
1689 +
1690 +       a->allocated = initial_num;
1691 +       a->used = 0;
1692 +
1693 +       return a;
1694 +}
1695 +
1696 +static inline void *fpm_array_item(struct fpm_array_s *a, unsigned int n)
1697 +{
1698 +       char *ret;
1699 +
1700 +       ret = (char *) a->data + a->sz * n;
1701 +
1702 +       return ret;
1703 +}
1704 +
1705 +static inline void *fpm_array_item_last(struct fpm_array_s *a)
1706 +{
1707 +       return fpm_array_item(a, a->used - 1);
1708 +}
1709 +
1710 +static inline int fpm_array_item_remove(struct fpm_array_s *a, unsigned int n)
1711 +{
1712 +       int ret = -1;
1713 +
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);
1717 +
1718 +               memcpy(to_remove, last, a->sz);
1719 +
1720 +               ret = n;
1721 +       }
1722 +
1723 +       --a->used;
1724 +
1725 +       return ret;
1726 +}
1727 +
1728 +static inline void *fpm_array_push(struct fpm_array_s *a)
1729 +{
1730 +       void *ret;
1731 +
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);
1735 +
1736 +               if (!new_ptr) {
1737 +                       return 0;
1738 +               }
1739 +
1740 +               a->data = new_ptr;
1741 +               a->allocated = new_allocated;
1742 +       }
1743 +
1744 +       ret = fpm_array_item(a, a->used);
1745 +
1746 +       ++a->used;
1747 +
1748 +       return ret;
1749 +}
1750 +
1751 +static inline void fpm_array_free(struct fpm_array_s *a)
1752 +{
1753 +       free(a->data);
1754 +       a->data = 0;
1755 +       a->sz = 0;
1756 +       a->used = a->allocated = 0;
1757 +}
1758 +
1759 +#endif
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
1763 --- /dev/null
1764 +++ b/sapi/cgi/fpm/fpm_atomic.h
1765 @@ -0,0 +1,85 @@
1766 +
1767 +       /* $Id$ */
1768 +       /* (c) 2007,2008 Andrei Nigmatulin */
1769 +
1770 +#ifndef FPM_ATOMIC_H
1771 +#define FPM_ATOMIC_H 1
1772 +
1773 +#include <stdint.h>
1774 +#include <sched.h>
1775 +
1776 +#if ( __i386__ || __i386 )
1777 +
1778 +typedef int32_t                     atomic_int_t;
1779 +typedef uint32_t                    atomic_uint_t;
1780 +typedef volatile atomic_uint_t      atomic_t;
1781 +
1782 +
1783 +static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
1784 +{
1785 +       __asm__ volatile ( "lock;" "xaddl %0, %1;" :
1786 +               "+r" (add) : "m" (*value) : "memory");
1787 +
1788 +       return add;
1789 +}
1790 +
1791 +static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
1792 +{
1793 +       unsigned char res;
1794 +
1795 +       __asm__ volatile ( "lock;" "cmpxchgl %3, %1;" "sete %0;" :
1796 +               "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
1797 +
1798 +    return res;
1799 +}
1800 +
1801 +#elif ( __amd64__ || __amd64 )
1802 +
1803 +typedef int64_t                     atomic_int_t;
1804 +typedef uint64_t                    atomic_uint_t;
1805 +typedef volatile atomic_uint_t      atomic_t;
1806 +
1807 +static inline atomic_int_t atomic_fetch_add(atomic_t *value, atomic_int_t add)
1808 +{
1809 +       __asm__ volatile ( "lock;" "xaddq %0, %1;" :
1810 +               "+r" (add) : "m" (*value) : "memory");
1811 +
1812 +       return add;
1813 +}
1814 +
1815 +static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set)
1816 +{
1817 +       unsigned char res;
1818 +
1819 +       __asm__ volatile ( "lock;" "cmpxchgq %3, %1;" "sete %0;" :
1820 +               "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "memory");
1821 +
1822 +       return res;
1823 +}
1824 +
1825 +#else
1826 +
1827 +#error unsupported processor. please write a patch and send it to me
1828 +
1829 +#endif
1830 +
1831 +static inline int fpm_spinlock(atomic_t *lock, int try_once)
1832 +{
1833 +       if (try_once) {
1834 +               return atomic_cmp_set(lock, 0, 1) ? 0 : -1;
1835 +       }
1836 +
1837 +       for (;;) {
1838 +
1839 +               if (atomic_cmp_set(lock, 0, 1)) {
1840 +                       break;
1841 +               }
1842 +
1843 +               sched_yield();
1844 +       }
1845 +
1846 +       return 0;
1847 +}
1848 +
1849 +#endif
1850 +
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
1854 --- /dev/null
1855 +++ b/sapi/cgi/fpm/fpm_autoconf.h.in
1856 @@ -0,0 +1,9 @@
1857 +
1858 +       /* $Id$ */
1859 +       /* (c) 2007,2008 Andrei Nigmatulin */
1860 +
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@"
1865 +
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
1869 --- /dev/null
1870 +++ b/sapi/cgi/fpm/fpm_children.c
1871 @@ -0,0 +1,385 @@
1872 +
1873 +       /* $Id$ */
1874 +       /* (c) 2007,2008 Andrei Nigmatulin */
1875 +
1876 +#include "fpm_config.h"
1877 +
1878 +#include <sys/types.h>
1879 +#include <sys/wait.h>
1880 +#include <time.h>
1881 +#include <unistd.h>
1882 +#include <string.h>
1883 +#include <stdio.h>
1884 +
1885 +#include "fpm.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"
1900 +
1901 +#include "zlog.h"
1902 +
1903 +static time_t *last_faults;
1904 +static int fault;
1905 +
1906 +static int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop);
1907 +
1908 +static void fpm_children_cleanup(int which, void *arg)
1909 +{
1910 +       free(last_faults);
1911 +}
1912 +
1913 +static struct fpm_child_s *fpm_child_alloc()
1914 +{
1915 +       struct fpm_child_s *ret;
1916 +
1917 +       ret = malloc(sizeof(struct fpm_child_s));
1918 +
1919 +       if (!ret) return 0;
1920 +
1921 +       memset(ret, 0, sizeof(*ret));
1922 +
1923 +       return ret;
1924 +}
1925 +
1926 +static void fpm_child_free(struct fpm_child_s *child)
1927 +{
1928 +       free(child);
1929 +}
1930 +
1931 +static void fpm_child_close(struct fpm_child_s *child, int in_event_loop)
1932 +{
1933 +       if (child->fd_stdout != -1) {
1934 +               if (in_event_loop) {
1935 +                       fpm_event_fire(&child->ev_stdout);
1936 +               }
1937 +               if (child->fd_stdout != -1) {
1938 +                       close(child->fd_stdout);
1939 +               }
1940 +       }
1941 +
1942 +       if (child->fd_stderr != -1) {
1943 +               if (in_event_loop) {
1944 +                       fpm_event_fire(&child->ev_stderr);
1945 +               }
1946 +               if (child->fd_stderr != -1) {
1947 +                       close(child->fd_stderr);
1948 +               }
1949 +       }
1950 +
1951 +       fpm_child_free(child);
1952 +}
1953 +
1954 +static void fpm_child_link(struct fpm_child_s *child)
1955 +{
1956 +       struct fpm_worker_pool_s *wp = child->wp;
1957 +
1958 +       ++wp->running_children;
1959 +       ++fpm_globals.running_children;
1960 +
1961 +       child->next = wp->children;
1962 +       if (child->next) child->next->prev = child;
1963 +       child->prev = 0;
1964 +       wp->children = child;
1965 +}
1966 +
1967 +static void fpm_child_unlink(struct fpm_child_s *child)
1968 +{
1969 +       --child->wp->running_children;
1970 +       --fpm_globals.running_children;
1971 +
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;
1975 +
1976 +}
1977 +
1978 +static struct fpm_child_s *fpm_child_find(pid_t pid)
1979 +{
1980 +       struct fpm_worker_pool_s *wp;
1981 +       struct fpm_child_s *child = 0;
1982 +
1983 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1984 +
1985 +               for (child = wp->children; child; child = child->next) {
1986 +                       if (child->pid == pid) {
1987 +                               break;
1988 +                       }
1989 +               }
1990 +
1991 +               if (child) break;
1992 +       }
1993 +
1994 +       if (!child) {
1995 +               return 0;
1996 +       }
1997 +
1998 +       return child;
1999 +}
2000 +
2001 +static void fpm_child_init(struct fpm_worker_pool_s *wp)
2002 +{
2003 +       fpm_globals.max_requests = wp->config->max_requests;
2004 +
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)) {
2010 +
2011 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "child failed to initialize (pool %s)", wp->config->name);
2012 +               exit(255);
2013 +       }
2014 +}
2015 +
2016 +int fpm_children_free(struct fpm_child_s *child)
2017 +{
2018 +       struct fpm_child_s *next;
2019 +
2020 +       for (; child; child = next) {
2021 +               next = child->next;
2022 +               fpm_child_close(child, 0 /* in_event_loop */);
2023 +       }
2024 +
2025 +       return 0;
2026 +}
2027 +
2028 +void fpm_children_bury()
2029 +{
2030 +       int status;
2031 +       pid_t pid;
2032 +       struct fpm_child_s *child;
2033 +
2034 +       while ( (pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
2035 +               char buf[128];
2036 +               int severity = ZLOG_NOTICE;
2037 +
2038 +               child = fpm_child_find(pid);
2039 +
2040 +               if (WIFEXITED(status)) {
2041 +
2042 +                       snprintf(buf, sizeof(buf), "with code %d", WEXITSTATUS(status));
2043 +
2044 +                       if (WEXITSTATUS(status) != 0) {
2045 +                               severity = ZLOG_WARNING;
2046 +                       }
2047 +
2048 +               }
2049 +               else if (WIFSIGNALED(status)) {
2050 +                       const char *signame = fpm_signal_names[WTERMSIG(status)];
2051 +                       const char *have_core = WCOREDUMP(status) ? " (core dumped)" : "";
2052 +
2053 +                       if (signame == NULL) {
2054 +                               signame = "";
2055 +                       }
2056 +
2057 +                       snprintf(buf, sizeof(buf), "on signal %d %s%s", WTERMSIG(status), signame, have_core);
2058 +
2059 +                       if (WTERMSIG(status) != SIGQUIT) { /* possible request loss */
2060 +                               severity = ZLOG_WARNING;
2061 +                       }
2062 +               }
2063 +               else if (WIFSTOPPED(status)) {
2064 +
2065 +                       zlog(ZLOG_STUFF, ZLOG_NOTICE, "child %d stopped for tracing", (int) pid);
2066 +
2067 +                       if (child && child->tracer) {
2068 +                               child->tracer(child);
2069 +                       }
2070 +
2071 +                       continue;
2072 +               }
2073 +
2074 +               if (child) {
2075 +                       struct fpm_worker_pool_s *wp = child->wp;
2076 +                       struct timeval tv1, tv2;
2077 +
2078 +                       fpm_child_unlink(child);
2079 +
2080 +                       fpm_shm_slots_discard_slot(child);
2081 +
2082 +                       fpm_clock_get(&tv1);
2083 +
2084 +                       timersub(&tv1, &child->started, &tv2);
2085 +
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);
2088 +
2089 +                       fpm_child_close(child, 1 /* in event_loop */);
2090 +
2091 +                       fpm_pctl_child_exited();
2092 +
2093 +                       if (last_faults && (WTERMSIG(status) == SIGSEGV || WTERMSIG(status) == SIGBUS)) {
2094 +                               time_t now = tv1.tv_sec;
2095 +                               int restart_condition = 1;
2096 +                               int i;
2097 +
2098 +                               last_faults[fault++] = now;
2099 +
2100 +                               if (fault == fpm_global_config.emergency_restart_threshold) {
2101 +                                       fault = 0;
2102 +                               }
2103 +
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;
2107 +                                               break;
2108 +                                       }
2109 +                               }
2110 +
2111 +                               if (restart_condition) {
2112 +
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);
2115 +
2116 +                                       fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
2117 +                               }
2118 +                       }
2119 +
2120 +                       fpm_children_make(wp, 1 /* in event loop */);
2121 +
2122 +                       if (fpm_globals.is_child) {
2123 +                               break;
2124 +                       }
2125 +               }
2126 +               else {
2127 +                       zlog(ZLOG_STUFF, ZLOG_ALERT, "oops, unknown child exited %s", buf);
2128 +               }
2129 +       }
2130 +
2131 +}
2132 +
2133 +static struct fpm_child_s *fpm_resources_prepare(struct fpm_worker_pool_s *wp)
2134 +{
2135 +       struct fpm_child_s *c;
2136 +
2137 +       c = fpm_child_alloc();
2138 +
2139 +       if (!c) {
2140 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "malloc failed (pool %s)", wp->config->name);
2141 +               return 0;
2142 +       }
2143 +
2144 +       c->wp = wp;
2145 +       c->fd_stdout = -1; c->fd_stderr = -1;
2146 +
2147 +       if (0 > fpm_stdio_prepare_pipes(c)) {
2148 +               fpm_child_free(c);
2149 +               return 0;
2150 +       }
2151 +
2152 +       if (0 > fpm_shm_slots_prepare_slot(c)) {
2153 +               fpm_stdio_discard_pipes(c);
2154 +               fpm_child_free(c);
2155 +               return 0;
2156 +       }
2157 +
2158 +       return c;
2159 +}
2160 +
2161 +static void fpm_resources_discard(struct fpm_child_s *child)
2162 +{
2163 +       fpm_shm_slots_discard_slot(child);
2164 +       fpm_stdio_discard_pipes(child);
2165 +       fpm_child_free(child);
2166 +}
2167 +
2168 +static void fpm_child_resources_use(struct fpm_child_s *child)
2169 +{
2170 +       fpm_shm_slots_child_use_slot(child);
2171 +       fpm_stdio_child_use_pipes(child);
2172 +       fpm_child_free(child);
2173 +}
2174 +
2175 +static void fpm_parent_resources_use(struct fpm_child_s *child)
2176 +{
2177 +       fpm_shm_slots_parent_use_slot(child);
2178 +       fpm_stdio_parent_use_pipes(child);
2179 +       fpm_child_link(child);
2180 +}
2181 +
2182 +static int fpm_children_make(struct fpm_worker_pool_s *wp, int in_event_loop)
2183 +{
2184 +       int enough = 0;
2185 +       pid_t pid;
2186 +       struct fpm_child_s *child;
2187 +
2188 +       while (!enough && fpm_pctl_can_spawn_children() && wp->running_children < wp->config->pm->max_children) {
2189 +
2190 +               child = fpm_resources_prepare(wp);
2191 +
2192 +               if (!child) {
2193 +                       enough = 1;
2194 +                       break;
2195 +               }
2196 +
2197 +               pid = fork();
2198 +
2199 +               switch (pid) {
2200 +
2201 +                       case 0 :
2202 +                               fpm_child_resources_use(child);
2203 +                               fpm_globals.is_child = 1;
2204 +                               if (in_event_loop) {
2205 +                                       fpm_event_exit_loop();
2206 +                               }
2207 +                               fpm_child_init(wp);
2208 +                               return 0;
2209 +
2210 +                       case -1 :
2211 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fork() failed");
2212 +                               enough = 1;
2213 +
2214 +                               fpm_resources_discard(child);
2215 +
2216 +                               break; /* dont try any more on error */
2217 +
2218 +                       default :
2219 +                               child->pid = pid;
2220 +                               fpm_clock_get(&child->started);
2221 +                               fpm_parent_resources_use(child);
2222 +
2223 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "child %d (pool %s) started", (int) pid, wp->config->name);
2224 +               }
2225 +
2226 +       }
2227 +
2228 +       return 1; /* we are done */
2229 +}
2230 +
2231 +int fpm_children_create_initial(struct fpm_worker_pool_s *wp)
2232 +{
2233 +       return fpm_children_make(wp, 0 /* not in event loop yet */);
2234 +}
2235 +
2236 +int fpm_children_init_main()
2237 +{
2238 +       if (fpm_global_config.emergency_restart_threshold &&
2239 +               fpm_global_config.emergency_restart_interval) {
2240 +
2241 +               last_faults = malloc(sizeof(time_t) * fpm_global_config.emergency_restart_threshold);
2242 +
2243 +               if (!last_faults) {
2244 +                       return -1;
2245 +               }
2246 +
2247 +               memset(last_faults, 0, sizeof(time_t) * fpm_global_config.emergency_restart_threshold);
2248 +       }
2249 +
2250 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_children_cleanup, 0)) {
2251 +               return -1;
2252 +       }
2253 +
2254 +       return 0;
2255 +}
2256 +
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
2260 --- /dev/null
2261 +++ b/sapi/cgi/fpm/fpm_children.h
2262 @@ -0,0 +1,33 @@
2263 +
2264 +       /* $Id$ */
2265 +       /* (c) 2007,2008 Andrei Nigmatulin */
2266 +
2267 +#ifndef FPM_CHILDREN_H
2268 +#define FPM_CHILDREN_H 1
2269 +
2270 +#include <sys/time.h>
2271 +#include <sys/types.h>
2272 +#include <event.h>
2273 +
2274 +#include "fpm_worker_pool.h"
2275 +
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();
2280 +
2281 +struct fpm_child_s;
2282 +
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;
2288 +       int shm_slot_i;
2289 +       int fd_stdout, fd_stderr;
2290 +       void (*tracer)(struct fpm_child_s *);
2291 +       struct timeval slow_logged;
2292 +       pid_t pid;
2293 +};
2294 +
2295 +#endif
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
2299 --- /dev/null
2300 +++ b/sapi/cgi/fpm/fpm_cleanup.c
2301 @@ -0,0 +1,51 @@
2302 +
2303 +       /* $Id$ */
2304 +       /* (c) 2007,2008 Andrei Nigmatulin */
2305 +
2306 +#include "fpm_config.h"
2307 +
2308 +#include <stdlib.h>
2309 +
2310 +#include "fpm_arrays.h"
2311 +#include "fpm_cleanup.h"
2312 +#include "zlog.h"
2313 +
2314 +struct cleanup_s {
2315 +       int type;
2316 +       void (*cleanup)(int, void *);
2317 +       void *arg;
2318 +};
2319 +
2320 +static struct fpm_array_s cleanups = { .sz = sizeof(struct cleanup_s) };
2321 +
2322 +int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *arg)
2323 +{
2324 +       struct cleanup_s *c;
2325 +
2326 +       c = fpm_array_push(&cleanups);
2327 +
2328 +       if (!c) {
2329 +               return -1;
2330 +       }
2331 +
2332 +       c->type = type;
2333 +       c->cleanup = cleanup;
2334 +       c->arg = arg;
2335 +
2336 +       return 0;
2337 +}
2338 +
2339 +void fpm_cleanups_run(int type)
2340 +{
2341 +       struct cleanup_s *c = fpm_array_item_last(&cleanups);
2342 +       int cl = cleanups.used;
2343 +
2344 +       for ( ; cl--; c--) {
2345 +               if (c->type & type) {
2346 +                       c->cleanup(type, c->arg);
2347 +               }
2348 +       }
2349 +
2350 +       fpm_array_free(&cleanups);
2351 +}
2352 +
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
2356 --- /dev/null
2357 +++ b/sapi/cgi/fpm/fpm_cleanup.h
2358 @@ -0,0 +1,21 @@
2359 +
2360 +       /* $Id$ */
2361 +       /* (c) 2007,2008 Andrei Nigmatulin */
2362 +
2363 +#ifndef FPM_CLEANUP_H
2364 +#define FPM_CLEANUP_H 1
2365 +
2366 +int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *);
2367 +void fpm_cleanups_run(int type);
2368 +
2369 +enum {
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,
2376 +};
2377 +
2378 +#endif
2379 +
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
2383 --- /dev/null
2384 +++ b/sapi/cgi/fpm/fpm_clock.c
2385 @@ -0,0 +1,115 @@
2386 +
2387 +       /* $Id$ */
2388 +       /* (c) 2007,2008 Andrei Nigmatulin */
2389 +
2390 +#include "fpm_config.h"
2391 +
2392 +#if defined(HAVE_CLOCK_GETTIME)
2393 +#include <time.h> /* for CLOCK_MONOTONIC */
2394 +#endif
2395 +
2396 +#include "fpm_clock.h"
2397 +#include "zlog.h"
2398 +
2399 +
2400 +/* posix monotonic clock - preferred source of time */
2401 +#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
2402 +
2403 +static int monotonic_works;
2404 +
2405 +int fpm_clock_init()
2406 +{
2407 +       struct timespec ts;
2408 +
2409 +       monotonic_works = 0;
2410 +
2411 +       if (0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
2412 +               monotonic_works = 1;
2413 +       }
2414 +
2415 +       return 0;
2416 +}
2417 +
2418 +int fpm_clock_get(struct timeval *tv)
2419 +{
2420 +       if (monotonic_works) {
2421 +               struct timespec ts;
2422 +
2423 +               if (0 > clock_gettime(CLOCK_MONOTONIC, &ts)) {
2424 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "clock_gettime() failed");
2425 +                       return -1;
2426 +               }
2427 +
2428 +               tv->tv_sec = ts.tv_sec;
2429 +               tv->tv_usec = ts.tv_nsec / 1000;
2430 +               return 0;
2431 +       }
2432 +
2433 +       return gettimeofday(tv, 0);
2434 +}
2435 +
2436 +/* macosx clock */
2437 +#elif defined(HAVE_CLOCK_GET_TIME)
2438 +
2439 +#include <mach/mach.h>
2440 +#include <mach/clock.h>
2441 +#include <mach/mach_error.h>
2442 +
2443 +static clock_serv_t mach_clock;
2444 +
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()
2448 +{
2449 +       kern_return_t ret;
2450 +       mach_timespec_t aTime;
2451 +
2452 +       ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &mach_clock);
2453 +
2454 +       if (ret != KERN_SUCCESS) {
2455 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "host_get_clock_service() failed: %s", mach_error_string(ret));
2456 +               return -1;
2457 +       }
2458 +
2459 +       /* test if it works */
2460 +       ret = clock_get_time(mach_clock, &aTime);
2461 +
2462 +       if (ret != KERN_SUCCESS) {
2463 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
2464 +               return -1;
2465 +       }
2466 +
2467 +       return 0;
2468 +}
2469 +
2470 +int fpm_clock_get(struct timeval *tv)
2471 +{
2472 +       kern_return_t ret;
2473 +       mach_timespec_t aTime;
2474 +
2475 +       ret = clock_get_time(mach_clock, &aTime);
2476 +
2477 +       if (ret != KERN_SUCCESS) {
2478 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
2479 +               return -1;
2480 +       }
2481 +
2482 +       tv->tv_sec = aTime.tv_sec;
2483 +       tv->tv_usec = aTime.tv_nsec / 1000;
2484 +
2485 +       return 0;
2486 +}
2487 +
2488 +#else /* no clock */
2489 +
2490 +int fpm_clock_init()
2491 +{
2492 +       return 0;
2493 +}
2494 +
2495 +int fpm_clock_get(struct timeval *tv)
2496 +{
2497 +       return gettimeofday(tv, 0);
2498 +}
2499 +
2500 +#endif
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
2504 --- /dev/null
2505 +++ b/sapi/cgi/fpm/fpm_clock.h
2506 @@ -0,0 +1,13 @@
2507 +
2508 +       /* $Id$ */
2509 +       /* (c) 2007,2008 Andrei Nigmatulin */
2510 +
2511 +#ifndef FPM_CLOCK_H
2512 +#define FPM_CLOCK_H 1
2513 +
2514 +#include <sys/time.h>
2515 +
2516 +int fpm_clock_init();
2517 +int fpm_clock_get(struct timeval *tv);
2518 +
2519 +#endif
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
2523 --- /dev/null
2524 +++ b/sapi/cgi/fpm/fpm_conf.c
2525 @@ -0,0 +1,532 @@
2526 +
2527 +       /* $Id$ */
2528 +       /* (c) 2007,2008 Andrei Nigmatulin */
2529 +
2530 +#include "fpm_config.h"
2531 +
2532 +#include <sys/types.h>
2533 +#include <sys/stat.h>
2534 +#include <fcntl.h>
2535 +#include <string.h>
2536 +#include <stdlib.h>
2537 +#include <stddef.h>
2538 +#include <stdint.h>
2539 +#include <stdio.h>
2540 +#include <unistd.h>
2541 +
2542 +#include "fpm.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"
2550 +#include "zlog.h"
2551 +
2552 +
2553 +struct fpm_global_config_s fpm_global_config;
2554 +
2555 +static void *fpm_global_config_ptr()
2556 +{
2557 +       return &fpm_global_config;
2558 +}
2559 +
2560 +static char *fpm_conf_set_log_level(void **conf, char *name, void *vv, intptr_t offset)
2561 +{
2562 +       char *value = vv;
2563 +
2564 +       if (!strcmp(value, "debug")) {
2565 +               fpm_globals.log_level = ZLOG_DEBUG;
2566 +       }
2567 +       else if (!strcmp(value, "notice")) {
2568 +               fpm_globals.log_level = ZLOG_NOTICE;
2569 +       }
2570 +       else if (!strcmp(value, "warn")) {
2571 +               fpm_globals.log_level = ZLOG_WARNING;
2572 +       }
2573 +       else if (!strcmp(value, "error")) {
2574 +               fpm_globals.log_level = ZLOG_ERROR;
2575 +       }
2576 +       else if (!strcmp(value, "alert")) {
2577 +               fpm_globals.log_level = ZLOG_ALERT;
2578 +       }
2579 +       else {
2580 +               return "invalid value for 'log_level'";
2581 +       }
2582 +
2583 +       return NULL;
2584 +}
2585 +
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 },
2597 +               { 0, 0, 0, 0 }
2598 +       }
2599 +};
2600 +
2601 +static char *fpm_conf_set_pm_style(void **conf, char *name, void *vv, intptr_t offset)
2602 +{
2603 +       char *value = vv;
2604 +       struct fpm_pm_s *c = *conf;
2605 +
2606 +       if (!strcmp(value, "static")) {
2607 +               c->style = PM_STYLE_STATIC;
2608 +       }
2609 +       else if (!strcmp(value, "apache-like")) {
2610 +               c->style = PM_STYLE_APACHE_LIKE;
2611 +       }
2612 +       else {
2613 +               return "invalid value for 'style'";
2614 +       }
2615 +
2616 +       return NULL;
2617 +}
2618 +
2619 +static char *fpm_conf_set_rlimit_core(void **conf, char *name, void *vv, intptr_t offset)
2620 +{
2621 +       char *value = vv;
2622 +       struct fpm_worker_pool_config_s *c = *conf;
2623 +
2624 +       if (!strcmp(value, "unlimited")) {
2625 +               c->rlimit_core = -1;
2626 +       }
2627 +       else {
2628 +               int int_value;
2629 +               void *subconf = &int_value;
2630 +               char *error;
2631 +
2632 +               error = xml_conf_set_slot_integer(&subconf, name, vv, 0);
2633 +
2634 +               if (error) return error;
2635 +
2636 +               if (int_value < 0) return "invalid value for 'rlimit_core'";
2637 +
2638 +               c->rlimit_core = int_value;
2639 +       }
2640 +
2641 +       return NULL;
2642 +}
2643 +
2644 +static char *fpm_conf_set_catch_workers_output(void **conf, char *name, void *vv, intptr_t offset)
2645 +{
2646 +       struct fpm_worker_pool_config_s *c = *conf;
2647 +       int int_value;
2648 +       void *subconf = &int_value;
2649 +       char *error;
2650 +
2651 +       error = xml_conf_set_slot_boolean(&subconf, name, vv, 0);
2652 +
2653 +       if (error) return error;
2654 +
2655 +       c->catch_workers_output = int_value;
2656 +
2657 +       return NULL;
2658 +}
2659 +
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) },
2666 +               { 0, 0, 0, 0 }
2667 +       }
2668 +};
2669 +
2670 +static char *fpm_conf_set_apache_like_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2671 +{
2672 +       return xml_conf_parse_section(conf, &fpm_conf_set_apache_like_subsection_conf, xml_node);
2673 +}
2674 +
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) },
2682 +               { 0, 0, 0, 0 }
2683 +       }
2684 +};
2685 +
2686 +static char *fpm_conf_set_listen_options_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2687 +{
2688 +       void *subconf = (char *) *conf + offset;
2689 +       struct fpm_listen_options_s *lo;
2690 +
2691 +       lo = malloc(sizeof(*lo));
2692 +
2693 +       if (!lo) {
2694 +               return "malloc() failed";
2695 +       }
2696 +
2697 +       memset(lo, 0, sizeof(*lo));
2698 +
2699 +       lo->backlog = -1;
2700 +
2701 +       * (struct fpm_listen_options_s **) subconf = lo;
2702 +
2703 +       subconf = lo;
2704 +
2705 +       return xml_conf_parse_section(&subconf, &fpm_conf_set_listen_options_subsection_conf, xml_node);
2706 +}
2707 +
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) },
2714 +               { 0, 0, 0, 0 }
2715 +       }
2716 +};
2717 +
2718 +static char *fpm_conf_set_pm_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2719 +{
2720 +       void *subconf = (char *) *conf + offset;
2721 +       struct fpm_pm_s *pm;
2722 +
2723 +       pm = malloc(sizeof(*pm));
2724 +
2725 +       if (!pm) {
2726 +               return "fpm_conf_set_pm_subsection(): malloc failed";
2727 +       }
2728 +
2729 +       memset(pm, 0, sizeof(*pm));
2730 +
2731 +       * (struct fpm_pm_s **) subconf = pm;
2732 +
2733 +       subconf = pm;
2734 +
2735 +       return xml_conf_parse_section(&subconf, &fpm_conf_set_pm_subsection_conf, xml_node);
2736 +}
2737 +
2738 +static char *xml_conf_set_slot_key_value_pair(void **conf, char *name, void *vv, intptr_t offset)
2739 +{
2740 +       char *value = vv;
2741 +       struct key_value_s *kv;
2742 +       struct key_value_s ***parent = (struct key_value_s ***) conf;
2743 +
2744 +       kv = malloc(sizeof(*kv));
2745 +
2746 +       if (!kv) {
2747 +               return "malloc() failed";
2748 +       }
2749 +
2750 +       memset(kv, 0, sizeof(*kv));
2751 +
2752 +       kv->key = strdup(name);
2753 +       kv->value = strdup(value);
2754 +
2755 +       if (!kv->key || !kv->value) {
2756 +               return "xml_conf_set_slot_key_value_pair(): strdup() failed";
2757 +       }
2758 +
2759 +       **parent = kv;
2760 +
2761 +       *parent = &kv->next;
2762 +
2763 +       return NULL;
2764 +}
2765 +
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 },
2770 +               { 0, 0, 0, 0 }
2771 +       }
2772 +};
2773 +
2774 +static char *fpm_conf_set_key_value_pairs_subsection(void **conf, char *name, void *xml_node, intptr_t offset)
2775 +{
2776 +       void *next_kv = (char *) *conf + offset;
2777 +
2778 +       return xml_conf_parse_section(&next_kv, &fpm_conf_set_key_value_pairs_subsection_conf, xml_node);
2779 +}
2780 +
2781 +static void *fpm_worker_pool_config_alloc()
2782 +{
2783 +       static struct fpm_worker_pool_s *current_wp = 0;
2784 +       struct fpm_worker_pool_s *wp;
2785 +
2786 +       wp = fpm_worker_pool_alloc();
2787 +
2788 +       if (!wp) return 0;
2789 +
2790 +       wp->config = malloc(sizeof(struct fpm_worker_pool_config_s));
2791 +
2792 +       if (!wp->config) return 0;
2793 +
2794 +       memset(wp->config, 0, sizeof(struct fpm_worker_pool_config_s));
2795 +
2796 +       if (current_wp) current_wp->next = wp;
2797 +
2798 +       current_wp = wp;
2799 +
2800 +       return wp->config;
2801 +}
2802 +
2803 +int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc)
2804 +{
2805 +       struct key_value_s *kv, *kv_next;
2806 +
2807 +       free(wpc->name);
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);
2814 +       }
2815 +       for (kv = wpc->php_defines; kv; kv = kv_next) {
2816 +               kv_next = kv->next;
2817 +               free(kv->key);
2818 +               free(kv->value);
2819 +               free(kv);
2820 +       }
2821 +       for (kv = wpc->environment; kv; kv = kv_next) {
2822 +               kv_next = kv->next;
2823 +               free(kv->key);
2824 +               free(kv->value);
2825 +               free(kv);
2826 +       }
2827 +       free(wpc->pm);
2828 +       free(wpc->user);
2829 +       free(wpc->group);
2830 +       free(wpc->chroot);
2831 +       free(wpc->chdir);
2832 +       free(wpc->allowed_clients);
2833 +       free(wpc->slowlog);
2834 +
2835 +       return 0;
2836 +}
2837 +
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) },
2860 +               { 0, 0, 0, 0 }
2861 +       }
2862 +};
2863 +
2864 +static struct xml_conf_section *fpm_conf_all_sections[] = {
2865 +       &xml_section_fpm_global_options,
2866 +       &xml_section_fpm_worker_pool_config,
2867 +       0
2868 +};
2869 +
2870 +static int fpm_evaluate_full_path(char **path)
2871 +{
2872 +       if (**path != '/') {
2873 +               char *full_path;
2874 +
2875 +               full_path = malloc(sizeof(PHP_PREFIX) + strlen(*path) + 1);
2876 +
2877 +               if (!full_path) return -1;
2878 +
2879 +               sprintf(full_path, "%s/%s", PHP_PREFIX, *path);
2880 +
2881 +               free(*path);
2882 +
2883 +               *path = full_path;
2884 +       }
2885 +
2886 +       return 0;
2887 +}
2888 +
2889 +static int fpm_conf_process_all_pools()
2890 +{
2891 +       struct fpm_worker_pool_s *wp;
2892 +
2893 +       if (!fpm_worker_all_pools) {
2894 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "at least one pool section must be specified in config file");
2895 +               return -1;
2896 +       }
2897 +
2898 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
2899 +
2900 +               if (wp->config->listen_address && *wp->config->listen_address) {
2901 +
2902 +                       wp->listen_address_domain = fpm_sockets_domain_from_address(wp->config->listen_address);
2903 +
2904 +                       if (wp->listen_address_domain == FPM_AF_UNIX && *wp->config->listen_address != '/') {
2905 +                               fpm_evaluate_full_path(&wp->config->listen_address);
2906 +                       }
2907 +
2908 +               }
2909 +               else {
2910 +
2911 +                       wp->is_template = 1;
2912 +
2913 +               }
2914 +
2915 +               if (wp->config->request_slowlog_timeout) {
2916 +#if HAVE_FPM_TRACE
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);
2920 +                               return -1;
2921 +                       }
2922 +#else
2923 +                       static int warned = 0;
2924 +
2925 +                       if (!warned) {
2926 +                               zlog(ZLOG_STUFF, ZLOG_WARNING, "pool %s: 'request_slowlog_timeout' is not supported on your system",
2927 +                                       wp->config->name);
2928 +                               warned = 1;
2929 +                       }
2930 +
2931 +                       wp->config->request_slowlog_timeout = 0;
2932 +#endif
2933 +               }
2934 +
2935 +               if (wp->config->request_slowlog_timeout && wp->config->slowlog && *wp->config->slowlog) {
2936 +                       int fd;
2937 +
2938 +                       fpm_evaluate_full_path(&wp->config->slowlog);
2939 +
2940 +                       if (wp->config->request_slowlog_timeout) {
2941 +                               fd = open(wp->config->slowlog, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
2942 +
2943 +                               if (0 > fd) {
2944 +                                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(%s) failed", wp->config->slowlog);
2945 +                                       return -1;
2946 +                               }
2947 +                               close(fd);
2948 +                       }
2949 +               }
2950 +       }
2951 +
2952 +       return 0;
2953 +}
2954 +
2955 +int fpm_conf_unlink_pid()
2956 +{
2957 +       if (fpm_global_config.pid_file) {
2958 +
2959 +               if (0 > unlink(fpm_global_config.pid_file)) {
2960 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "unlink(\"%s\") failed", fpm_global_config.pid_file);
2961 +                       return -1;
2962 +               }
2963 +
2964 +       }
2965 +
2966 +       return 0;
2967 +}
2968 +
2969 +int fpm_conf_write_pid()
2970 +{
2971 +       int fd;
2972 +
2973 +       if (fpm_global_config.pid_file) {
2974 +               char buf[64];
2975 +               int len;
2976 +
2977 +               unlink(fpm_global_config.pid_file);
2978 +
2979 +               fd = creat(fpm_global_config.pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
2980 +
2981 +               if (fd < 0) {
2982 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "creat(\"%s\") failed", fpm_global_config.pid_file);
2983 +                       return -1;
2984 +               }
2985 +
2986 +               len = sprintf(buf, "%d", (int) fpm_globals.parent_pid);
2987 +
2988 +               if (len != write(fd, buf, len)) {
2989 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "write() failed");
2990 +                       return -1;
2991 +               }
2992 +
2993 +               close(fd);
2994 +       }
2995 +
2996 +       return 0;
2997 +}
2998 +
2999 +static int fpm_conf_post_process()
3000 +{
3001 +       if (fpm_global_config.pid_file) {
3002 +               fpm_evaluate_full_path(&fpm_global_config.pid_file);
3003 +       }
3004 +
3005 +       if (!fpm_global_config.error_log) {
3006 +               fpm_global_config.error_log = strdup(PHP_FPM_LOG_PATH);
3007 +       }
3008 +
3009 +       fpm_evaluate_full_path(&fpm_global_config.error_log);
3010 +
3011 +       if (0 > fpm_stdio_open_error_log(0)) {
3012 +               return -1;
3013 +       }
3014 +
3015 +       return fpm_conf_process_all_pools();
3016 +}
3017 +
3018 +static void fpm_conf_cleanup(int which, void *arg)
3019 +{
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;
3024 +}
3025 +
3026 +int fpm_conf_init_main()
3027 +{
3028 +       char *filename = fpm_globals.config;
3029 +       char *err;
3030 +
3031 +       if (0 > xml_conf_sections_register(fpm_conf_all_sections)) {
3032 +               return -1;
3033 +       }
3034 +
3035 +       if (filename == NULL) {
3036 +               filename = PHP_FPM_CONF_PATH;
3037 +       }
3038 +
3039 +       err = xml_conf_load_file(filename);
3040 +
3041 +       if (err) {
3042 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "failed to load configuration file: %s", err);
3043 +               return -1;
3044 +       }
3045 +
3046 +       if (0 > fpm_conf_post_process()) {
3047 +               return -1;
3048 +       }
3049 +
3050 +       xml_conf_clean();
3051 +
3052 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_conf_cleanup, 0)) {
3053 +               return -1;
3054 +       }
3055 +
3056 +       return 0;
3057 +}
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
3061 --- /dev/null
3062 +++ b/sapi/cgi/fpm/fpm_conf.h
3063 @@ -0,0 +1,73 @@
3064 +
3065 +       /* $Id$ */
3066 +       /* (c) 2007,2008 Andrei Nigmatulin */
3067 +
3068 +#ifndef FPM_CONF_H
3069 +#define FPM_CONF_H 1
3070 +
3071 +struct key_value_s;
3072 +
3073 +struct key_value_s {
3074 +       struct key_value_s *next;
3075 +       char *key;
3076 +       char *value;
3077 +};
3078 +
3079 +struct fpm_global_config_s {
3080 +       int emergency_restart_threshold;
3081 +       int emergency_restart_interval;
3082 +       int process_control_timeout;
3083 +       int daemonize;
3084 +       char *pid_file;
3085 +       char *error_log;
3086 +};
3087 +
3088 +extern struct fpm_global_config_s fpm_global_config;
3089 +
3090 +struct fpm_pm_s {
3091 +       int style;
3092 +       int max_children;
3093 +       struct {
3094 +               int StartServers;
3095 +               int MinSpareServers;
3096 +               int MaxSpareServers;
3097 +       } options_apache_like;
3098 +};
3099 +
3100 +struct fpm_listen_options_s {
3101 +       int backlog;
3102 +       char *owner;
3103 +       char *group;
3104 +       char *mode;
3105 +};
3106 +
3107 +struct fpm_worker_pool_config_s {
3108 +       char *name;
3109 +       char *listen_address;
3110 +       struct fpm_listen_options_s *listen_options;
3111 +       struct key_value_s *php_defines;
3112 +       char *user;
3113 +       char *group;
3114 +       char *chroot;
3115 +       char *chdir;
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;
3121 +       char *slowlog;
3122 +       int max_requests;
3123 +       int rlimit_files;
3124 +       int rlimit_core;
3125 +       unsigned catch_workers_output:1;
3126 +};
3127 +
3128 +enum { PM_STYLE_STATIC = 1, PM_STYLE_APACHE_LIKE = 2 };
3129 +
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();
3134 +
3135 +#endif
3136 +
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
3140 --- /dev/null
3141 +++ b/sapi/cgi/fpm/fpm_config.h
3142 @@ -0,0 +1,39 @@
3143 +
3144 +       /* $Id$ */
3145 +       /* (c) 2007,2008 Andrei Nigmatulin */
3146 +
3147 +#include "php_config.h"
3148 +#include "fpm_autoconf.h"
3149 +
3150 +
3151 +/* Solaris does not have it */
3152 +#ifndef INADDR_NONE
3153 +#define INADDR_NONE (-1)
3154 +#endif
3155 +
3156 +
3157 +/* If we're not using GNU C, elide __attribute__ */
3158 +#ifndef __GNUC__
3159 +#  define  __attribute__(x)  /*NOTHING*/
3160 +#endif
3161 +
3162 +
3163 +/* Solaris does not have it */
3164 +#ifndef timersub
3165 +#define        timersub(tvp, uvp, vvp)                                         \
3166 +       do {                                                            \
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;                      \
3172 +               }                                                       \
3173 +       } while (0)
3174 +#endif
3175 +
3176 +#if defined(HAVE_PTRACE) || defined(PROC_MEM_FILE) || defined(HAVE_MACH_VM_READ)
3177 +#define HAVE_FPM_TRACE 1
3178 +#else
3179 +#define HAVE_FPM_TRACE 0
3180 +#endif
3181 +
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
3185 --- /dev/null
3186 +++ b/sapi/cgi/fpm/fpm_env.c
3187 @@ -0,0 +1,125 @@
3188 +
3189 +       /* $Id$ */
3190 +       /* (c) 2007,2008 Andrei Nigmatulin */
3191 +
3192 +#include "fpm_config.h"
3193 +
3194 +#ifdef HAVE_ALLOCA_H
3195 +#include <alloca.h>
3196 +#endif
3197 +#include <stdio.h>
3198 +#include <stdlib.h>
3199 +#include <string.h>
3200 +
3201 +#include "fpm_env.h"
3202 +#include "zlog.h"
3203 +
3204 +#ifndef HAVE_SETENV
3205 +int setenv(char *name, char *value, int overwrite)
3206 +{
3207 +       int name_len = strlen(name);
3208 +       int value_len = strlen(value);
3209 +       char *var = alloca(name_len + 1 + value_len + 1);
3210 +
3211 +       memcpy(var, name, name_len);
3212 +
3213 +       var[name_len] = '=';
3214 +
3215 +       memcpy(var + name_len + 1, value, value_len);
3216 +
3217 +       var[name_len + 1 + value_len] = '\0';
3218 +
3219 +       return putenv(var);
3220 +}
3221 +#endif
3222 +
3223 +#ifndef HAVE_CLEARENV
3224 +void clearenv()
3225 +{
3226 +       char **envp;
3227 +       char *s;
3228 +
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, '=');
3233 +
3234 +               s = strdup(*envp);
3235 +
3236 +               if (eq) s[eq - *envp] = '\0';
3237 +
3238 +               unsetenv(s);
3239 +               free(s);
3240 +       }
3241 +
3242 +}
3243 +#endif
3244 +
3245 +
3246 +int fpm_env_init_child(struct fpm_worker_pool_s *wp)
3247 +{
3248 +       struct key_value_s *kv;
3249 +
3250 +       clearenv();
3251 +
3252 +       for (kv = wp->config->environment; kv; kv = kv->next) {
3253 +               setenv(kv->key, kv->value, 1);
3254 +       }
3255 +
3256 +       if (wp->user) {
3257 +               setenv("USER", wp->user, 1);
3258 +       }
3259 +
3260 +       if (wp->home) {
3261 +               setenv("HOME", wp->home, 1);
3262 +       }
3263 +
3264 +       return 0;
3265 +}
3266 +
3267 +static int fpm_env_conf_wp(struct fpm_worker_pool_s *wp)
3268 +{
3269 +       struct key_value_s *kv;
3270 +
3271 +       kv = wp->config->environment;
3272 +
3273 +       for (kv = wp->config->environment; kv; kv = kv->next) {
3274 +               if (*kv->value == '$') {
3275 +                       char *value = getenv(kv->value + 1);
3276 +
3277 +                       if (!value) value = "";
3278 +
3279 +                       free(kv->value);
3280 +                       kv->value = strdup(value);
3281 +               }
3282 +
3283 +               /* autodetected values should be removed
3284 +                       if these vars specified in config */
3285 +               if (!strcmp(kv->key, "USER")) {
3286 +                       free(wp->user);
3287 +                       wp->user = 0;
3288 +               }
3289 +
3290 +               if (!strcmp(kv->key, "HOME")) {
3291 +                       free(wp->home);
3292 +                       wp->home = 0;
3293 +               }
3294 +       }
3295 +
3296 +       return 0;
3297 +}
3298 +
3299 +int fpm_env_init_main()
3300 +{
3301 +       struct fpm_worker_pool_s *wp;
3302 +
3303 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
3304 +
3305 +               if (0 > fpm_env_conf_wp(wp)) {
3306 +                       return -1;
3307 +               }
3308 +
3309 +       }
3310 +
3311 +       return 0;
3312 +}
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
3316 --- /dev/null
3317 +++ b/sapi/cgi/fpm/fpm_env.h
3318 @@ -0,0 +1,24 @@
3319 +
3320 +       /* $Id$ */
3321 +       /* (c) 2007,2008 Andrei Nigmatulin */
3322 +
3323 +#ifndef FPM_ENV_H
3324 +#define FPM_ENV_H 1
3325 +
3326 +#include "fpm_worker_pool.h"
3327 +
3328 +int fpm_env_init_child(struct fpm_worker_pool_s *wp);
3329 +int fpm_env_init_main();
3330 +
3331 +extern char **environ;
3332 +
3333 +#ifndef HAVE_SETENV
3334 +int setenv(char *name, char *value, int overwrite);
3335 +#endif
3336 +
3337 +#ifndef HAVE_CLEARENV
3338 +void clearenv();
3339 +#endif
3340 +
3341 +#endif
3342 +
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
3346 --- /dev/null
3347 +++ b/sapi/cgi/fpm/fpm_events.c
3348 @@ -0,0 +1,135 @@
3349 +
3350 +       /* $Id$ */
3351 +       /* (c) 2007,2008 Andrei Nigmatulin */
3352 +
3353 +#include "fpm_config.h"
3354 +
3355 +#include <unistd.h>
3356 +#include <errno.h>
3357 +#include <stdlib.h> /* for putenv */
3358 +#include <string.h>
3359 +#include <sys/types.h> /* for event.h below */
3360 +#include <event.h>
3361 +
3362 +#include "fpm.h"
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"
3369 +#include "zlog.h"
3370 +
3371 +static void fpm_event_cleanup(int which, void *arg)
3372 +{
3373 +       event_base_free(0);
3374 +}
3375 +
3376 +static void fpm_got_signal(int fd, short ev, void *arg)
3377 +{
3378 +       char c;
3379 +       int res;
3380 +
3381 +       do {
3382 +
3383 +               do {
3384 +                       res = read(fd, &c, 1);
3385 +               } while (res == -1 && errno == EINTR);
3386 +
3387 +               if (res <= 0) {
3388 +                       if (res < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
3389 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
3390 +                       }
3391 +                       return;
3392 +               }
3393 +
3394 +               switch (c) {
3395 +                       case 'C' :                  /* SIGCHLD */
3396 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGCHLD");
3397 +                               fpm_children_bury();
3398 +                               break;
3399 +                       case 'I' :                  /* SIGINT  */
3400 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGINT");
3401 +                               fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
3402 +                               break;
3403 +                       case 'T' :                  /* SIGTERM */
3404 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGTERM");
3405 +                               fpm_pctl(FPM_PCTL_STATE_TERMINATING, FPM_PCTL_ACTION_SET);
3406 +                               break;
3407 +                       case 'Q' :                  /* SIGQUIT */
3408 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGQUIT");
3409 +                               fpm_pctl(FPM_PCTL_STATE_FINISHING, FPM_PCTL_ACTION_SET);
3410 +                               break;
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");
3415 +                               }
3416 +                               break;
3417 +                       case '2' :                  /* SIGUSR2 */
3418 +                               zlog(ZLOG_STUFF, ZLOG_NOTICE, "received SIGUSR2");
3419 +                               fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
3420 +                               break;
3421 +               }
3422 +
3423 +               if (fpm_globals.is_child) {
3424 +                       break;
3425 +               }
3426 +
3427 +       } while (1);
3428 +
3429 +       return;
3430 +}
3431 +
3432 +int fpm_event_init_main()
3433 +{
3434 +       event_init();
3435 +
3436 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: using %s", event_get_method());
3437 +
3438 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_event_cleanup, 0)) {
3439 +               return -1;
3440 +       }
3441 +
3442 +       return 0;
3443 +}
3444 +
3445 +int fpm_event_loop()
3446 +{
3447 +       static struct event signal_fd_event;
3448 +
3449 +       event_set(&signal_fd_event, fpm_signals_get_fd(), EV_PERSIST | EV_READ, &fpm_got_signal, 0);
3450 +
3451 +       event_add(&signal_fd_event, 0);
3452 +
3453 +       fpm_pctl_heartbeat(-1, 0, 0);
3454 +
3455 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "libevent: entering main loop");
3456 +
3457 +       event_loop(0);
3458 +
3459 +       return 0;
3460 +}
3461 +
3462 +int fpm_event_add(int fd, struct event *ev, void (*callback)(int, short, void *), void *arg)
3463 +{
3464 +       event_set(ev, fd, EV_PERSIST | EV_READ, callback, arg);
3465 +
3466 +       return event_add(ev, 0);
3467 +}
3468 +
3469 +int fpm_event_del(struct event *ev)
3470 +{
3471 +       return event_del(ev);
3472 +}
3473 +
3474 +void fpm_event_exit_loop()
3475 +{
3476 +       event_loopbreak();
3477 +}
3478 +
3479 +void fpm_event_fire(struct event *ev)
3480 +{
3481 +       (*ev->ev_callback)( (int) ev->ev_fd, (short) ev->ev_res, ev->ev_arg);   
3482 +}
3483 +
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
3487 --- /dev/null
3488 +++ b/sapi/cgi/fpm/fpm_events.h
3489 @@ -0,0 +1,16 @@
3490 +
3491 +       /* $Id$ */
3492 +       /* (c) 2007,2008 Andrei Nigmatulin */
3493 +
3494 +#ifndef FPM_EVENTS_H
3495 +#define FPM_EVENTS_H 1
3496 +
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();
3503 +
3504 +
3505 +#endif
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
3509 --- /dev/null
3510 +++ b/sapi/cgi/fpm/fpm_php.c
3511 @@ -0,0 +1,190 @@
3512 +
3513 +       /* $Id$ */
3514 +       /* (c) 2007,2008 Andrei Nigmatulin */
3515 +
3516 +#include "fpm_config.h"
3517 +
3518 +#include <stdlib.h>
3519 +#include <string.h>
3520 +#include <stdio.h>
3521 +
3522 +#include "php.h"
3523 +#include "php_main.h"
3524 +#include "php_ini.h"
3525 +#include "ext/standard/dl.h"
3526 +
3527 +#include "fastcgi.h"
3528 +
3529 +#include "fpm.h"
3530 +#include "fpm_php.h"
3531 +#include "fpm_cleanup.h"
3532 +#include "fpm_worker_pool.h"
3533 +
3534 +static int zend_ini_alter_master(char *name, int name_length, char *new_value, int new_value_length, int stage TSRMLS_DC)
3535 +{
3536 +       zend_ini_entry *ini_entry;
3537 +       char *duplicate;
3538 +
3539 +       if (zend_hash_find(EG(ini_directives), name, name_length, (void **) &ini_entry) == FAILURE) {
3540 +               return FAILURE;
3541 +       }
3542 +
3543 +       duplicate = strdup(new_value);
3544 +
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;
3550 +       } else {
3551 +               free(duplicate);
3552 +       }
3553 +
3554 +       return SUCCESS;
3555 +}
3556 +
3557 +static void fpm_php_disable(char *value, int (*zend_disable)(char *, uint TSRMLS_DC) TSRMLS_DC)
3558 +{
3559 +       char *s = 0, *e = value;
3560 +
3561 +       while (*e) {
3562 +               switch (*e) {
3563 +                       case ' ':
3564 +                       case ',':
3565 +                               if (s) {
3566 +                                       *e = '\0';
3567 +                                       zend_disable(s, e - s TSRMLS_CC);
3568 +                                       s = 0;
3569 +                               }
3570 +                               break;
3571 +                       default:
3572 +                               if (!s) {
3573 +                                       s = e;
3574 +                               }
3575 +                               break;
3576 +               }
3577 +               e++;
3578 +       }
3579 +
3580 +       if (s) {
3581 +               zend_disable(s, e - s TSRMLS_CC);
3582 +       }
3583 +}
3584 +
3585 +static int fpm_php_apply_defines(struct fpm_worker_pool_s *wp)
3586 +{
3587 +       TSRMLS_FETCH();
3588 +       struct key_value_s *kv;
3589 +
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);
3595 +
3596 +               if (!strcmp(name, "extension") && *value) {
3597 +                       zval zv;
3598 +
3599 +#if defined(PHP_VERSION_ID) && (PHP_VERSION_ID >= 50300)
3600 +                       php_dl(value, MODULE_PERSISTENT, &zv, 1 TSRMLS_CC);
3601 +#else
3602 +                       zval filename;
3603 +                       ZVAL_STRINGL(&filename, value, value_len, 0);
3604 +#if (PHP_MAJOR_VERSION >= 5)
3605 +                       php_dl(&filename, MODULE_PERSISTENT, &zv, 1 TSRMLS_CC);
3606 +#else
3607 +                       php_dl(&filename, MODULE_PERSISTENT, &zv TSRMLS_CC);
3608 +#endif
3609 +#endif
3610 +                       continue;
3611 +               }
3612 +
3613 +               zend_ini_alter_master(name, name_len + 1, value, value_len, PHP_INI_STAGE_ACTIVATE TSRMLS_CC);
3614 +
3615 +               if (!strcmp(name, "disable_functions") && *value) {
3616 +                       char *v = strdup(value);
3617 +#if (PHP_MAJOR_VERSION >= 5)
3618 +                       PG(disable_functions) = v;
3619 +#endif
3620 +                       fpm_php_disable(v, zend_disable_function TSRMLS_CC);
3621 +               }
3622 +               else if (!strcmp(name, "disable_classes") && *value) {
3623 +                       char *v = strdup(value);
3624 +#if (PHP_MAJOR_VERSION >= 5)
3625 +                       PG(disable_classes) = v;
3626 +#endif
3627 +                       fpm_php_disable(v, zend_disable_class TSRMLS_CC);
3628 +               }
3629 +       }
3630 +
3631 +       return 0;
3632 +}
3633 +
3634 +static int fpm_php_set_allowed_clients(struct fpm_worker_pool_s *wp)
3635 +{
3636 +       if (wp->listen_address_domain == FPM_AF_INET) {
3637 +               fcgi_set_allowed_clients(wp->config->allowed_clients);
3638 +       }
3639 +
3640 +       return 0;
3641 +}
3642 +
3643 +static int fpm_php_set_fcgi_mgmt_vars(struct fpm_worker_pool_s *wp)
3644 +{
3645 +       char max_workers[10 + 1]; /* 4294967295 */
3646 +       int len;
3647 +
3648 +       len = sprintf(max_workers, "%u", (unsigned int) wp->config->pm->max_children);
3649 +
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);
3652 +
3653 +       return 0;
3654 +}
3655 +
3656 +char *fpm_php_script_filename(TSRMLS_D)
3657 +{
3658 +       return SG(request_info).path_translated;
3659 +}
3660 +
3661 +char *fpm_php_request_method(TSRMLS_D)
3662 +{
3663 +       return (char *) SG(request_info).request_method;
3664 +}
3665 +
3666 +size_t fpm_php_content_length(TSRMLS_D)
3667 +{
3668 +       return SG(request_info).content_length;
3669 +}
3670 +
3671 +static void fpm_php_cleanup(int which, void *arg)
3672 +{
3673 +       TSRMLS_FETCH();
3674 +       php_module_shutdown(TSRMLS_C);
3675 +       sapi_shutdown();
3676 +}
3677 +
3678 +void fpm_php_soft_quit()
3679 +{
3680 +       fcgi_set_in_shutdown(1);
3681 +}
3682 +
3683 +int fpm_php_init_main()
3684 +{
3685 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_PARENT, fpm_php_cleanup, 0)) {
3686 +               return -1;
3687 +       }
3688 +
3689 +       return 0;
3690 +}
3691 +
3692 +int fpm_php_init_child(struct fpm_worker_pool_s *wp)
3693 +{
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)) {
3697 +               return -1;
3698 +       }
3699 +
3700 +       return 0;
3701 +}
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
3705 --- /dev/null
3706 +++ b/sapi/cgi/fpm/fpm_php.h
3707 @@ -0,0 +1,22 @@
3708 +
3709 +       /* $Id$ */
3710 +       /* (c) 2007,2008 Andrei Nigmatulin */
3711 +
3712 +#ifndef FPM_PHP_H
3713 +#define FPM_PHP_H 1
3714 +
3715 +#include <TSRM.h>
3716 +
3717 +#include "build-defs.h" /* for PHP_ defines */
3718 +
3719 +struct fpm_worker_pool_s;
3720 +
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();
3727 +
3728 +#endif
3729 +
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
3733 --- /dev/null
3734 +++ b/sapi/cgi/fpm/fpm_php_trace.c
3735 @@ -0,0 +1,171 @@
3736 +
3737 +       /* $Id$ */
3738 +       /* (c) 2007,2008 Andrei Nigmatulin */
3739 +
3740 +#include "fpm_config.h"
3741 +
3742 +#if HAVE_FPM_TRACE
3743 +
3744 +#include "php.h"
3745 +#include "php_main.h"
3746 +
3747 +#include <stdio.h>
3748 +#include <stddef.h>
3749 +#include <stdint.h>
3750 +#include <unistd.h>
3751 +#include <sys/time.h>
3752 +#include <sys/types.h>
3753 +#include <errno.h>
3754 +
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"
3760 +
3761 +#include "zlog.h"
3762 +
3763 +
3764 +#define valid_ptr(p) ((p) && 0 == ((p) & (sizeof(long) - 1)))
3765 +
3766 +#if SIZEOF_LONG == 4
3767 +#define PTR_FMT "08"
3768 +#elif SIZEOF_LONG == 8
3769 +#define PTR_FMT "016"
3770 +#endif
3771 +
3772 +
3773 +static int fpm_php_trace_dump(struct fpm_child_s *child, FILE *slowlog TSRMLS_DC)
3774 +{
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;
3781 +       long l;
3782 +
3783 +       gettimeofday(&tv, 0);
3784 +
3785 +       zlog_print_time(&tv, buf, buf_size);
3786 +
3787 +       fprintf(slowlog, "\n%s pid %d (pool %s)\n", buf, (int) pid, child->wp->config->name);
3788 +
3789 +       if (0 > fpm_trace_get_strz(buf, buf_size, (long) &SG(request_info).path_translated)) {
3790 +               return -1;
3791 +       }
3792 +
3793 +       fprintf(slowlog, "script_filename = %s\n", buf);
3794 +
3795 +       if (0 > fpm_trace_get_long((long) &EG(current_execute_data), &l)) {
3796 +               return -1;
3797 +       }
3798 +
3799 +       execute_data = l;
3800 +
3801 +       while (execute_data) {
3802 +               long function;
3803 +               uint lineno = 0;
3804 +
3805 +               fprintf(slowlog, "[0x%" PTR_FMT "lx] ", execute_data);
3806 +
3807 +               if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, function_state.function), &l)) {
3808 +                       return -1;
3809 +               }
3810 +
3811 +               function = l;
3812 +
3813 +               if (valid_ptr(function)) {
3814 +                       if (0 > fpm_trace_get_strz(buf, buf_size, function + offsetof(zend_function, common.function_name))) {
3815 +                               return -1;
3816 +                       }
3817 +
3818 +                       fprintf(slowlog, "%s()", buf);
3819 +               }
3820 +               else {
3821 +                       fprintf(slowlog, "???");
3822 +               }
3823 +
3824 +               if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, op_array), &l)) {
3825 +                       return -1;
3826 +               }
3827 +
3828 +               *buf = '\0';
3829 +
3830 +               if (valid_ptr(l)) {
3831 +                       long op_array = l;
3832 +
3833 +                       if (0 > fpm_trace_get_strz(buf, buf_size, op_array + offsetof(zend_op_array, filename))) {
3834 +                               return -1;
3835 +                       }
3836 +               }
3837 +
3838 +               if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, opline), &l)) {
3839 +                       return -1;
3840 +               }
3841 +
3842 +               if (valid_ptr(l)) {
3843 +                       long opline = l;
3844 +                       uint *lu = (uint *) &l;
3845 +
3846 +                       if (0 > fpm_trace_get_long(opline + offsetof(struct _zend_op, lineno), &l)) {
3847 +                               return -1;
3848 +                       }
3849 +
3850 +                       lineno = *lu;
3851 +               }
3852 +
3853 +               fprintf(slowlog, " %s:%u\n", *buf ? buf : "unknown", lineno);
3854 +
3855 +               if (0 > fpm_trace_get_long(execute_data + offsetof(zend_execute_data, prev_execute_data), &l)) {
3856 +                       return -1;
3857 +               }
3858 +
3859 +               execute_data = l;
3860 +
3861 +               if (0 == --callers_limit) {
3862 +                       break;
3863 +               }
3864 +       }
3865 +
3866 +       return 0;
3867 +}
3868 +
3869 +void fpm_php_trace(struct fpm_child_s *child)
3870 +{
3871 +       TSRMLS_FETCH();
3872 +       FILE *slowlog;
3873 +
3874 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "about to trace %d", (int) child->pid);
3875 +
3876 +       slowlog = fopen(child->wp->config->slowlog, "a+");
3877 +
3878 +       if (!slowlog) {
3879 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fopen(%s) failed", child->wp->config->slowlog);
3880 +               goto done0;
3881 +       }
3882 +
3883 +       if (0 > fpm_trace_ready(child->pid)) {
3884 +               goto done1;
3885 +       }
3886 +
3887 +       if (0 > fpm_php_trace_dump(child, slowlog TSRMLS_CC)) {
3888 +               fprintf(slowlog, "+++ dump failed\n");
3889 +       }
3890 +
3891 +       if (0 > fpm_trace_close(child->pid)) {
3892 +               goto done1;
3893 +       }
3894 +
3895 +done1:
3896 +       fclose(slowlog);
3897 +
3898 +done0:
3899 +       fpm_pctl_kill(child->pid, FPM_PCTL_CONT);
3900 +       child->tracer = 0;
3901 +
3902 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "finished trace of %d", (int) child->pid);
3903 +}
3904 +
3905 +#endif
3906 +
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
3910 --- /dev/null
3911 +++ b/sapi/cgi/fpm/fpm_php_trace.h
3912 @@ -0,0 +1,13 @@
3913 +
3914 +       /* $Id$ */
3915 +       /* (c) 2007,2008 Andrei Nigmatulin */
3916 +
3917 +#ifndef FPM_PHP_TRACE_H
3918 +#define FPM_PHP_TRACE_H 1
3919 +
3920 +struct fpm_child_s;
3921 +
3922 +void fpm_php_trace(struct fpm_child_s *);
3923 +
3924 +#endif
3925 +
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
3929 --- /dev/null
3930 +++ b/sapi/cgi/fpm/fpm_process_ctl.c
3931 @@ -0,0 +1,354 @@
3932 +
3933 +       /* $Id$ */
3934 +       /* (c) 2007,2008 Andrei Nigmatulin */
3935 +
3936 +#include "fpm_config.h"
3937 +
3938 +#include <sys/types.h>
3939 +#include <signal.h>
3940 +#include <unistd.h>
3941 +#include <stdlib.h>
3942 +
3943 +#include "fpm.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"
3952 +#include "zlog.h"
3953 +
3954 +
3955 +static int fpm_state = FPM_PCTL_STATE_NORMAL;
3956 +static int fpm_signal_sent = 0;
3957 +
3958 +
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"
3964 +};
3965 +
3966 +static int saved_argc;
3967 +static char **saved_argv;
3968 +
3969 +static void fpm_pctl_cleanup(int which, void *arg)
3970 +{
3971 +       int i;
3972 +
3973 +       if (which != FPM_CLEANUP_PARENT_EXEC) {
3974 +
3975 +               for (i = 0; i < saved_argc; i++) {
3976 +                       free(saved_argv[i]);
3977 +               }
3978 +
3979 +               free(saved_argv);
3980 +
3981 +       }
3982 +}
3983 +
3984 +static struct event pctl_event;
3985 +
3986 +static void fpm_pctl_action(int fd, short which, void *arg)
3987 +{
3988 +       evtimer_del(&pctl_event);
3989 +
3990 +       memset(&pctl_event, 0, sizeof(pctl_event));
3991 +
3992 +       fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_TIMEOUT);
3993 +}
3994 +
3995 +static int fpm_pctl_timeout_set(int sec)
3996 +{
3997 +       struct timeval tv = { .tv_sec = sec, .tv_usec = 0 };
3998 +
3999 +       if (evtimer_initialized(&pctl_event)) {
4000 +               evtimer_del(&pctl_event);
4001 +       }
4002 +
4003 +       evtimer_set(&pctl_event, &fpm_pctl_action, 0);
4004 +
4005 +       evtimer_add(&pctl_event, &tv);
4006 +
4007 +       return 0;
4008 +}
4009 +
4010 +static void fpm_pctl_exit()
4011 +{
4012 +       zlog(ZLOG_STUFF, ZLOG_NOTICE, "exiting, bye-bye!");
4013 +
4014 +       fpm_conf_unlink_pid();
4015 +
4016 +       fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT_MAIN);
4017 +
4018 +       exit(0);
4019 +}
4020 +
4021 +#define optional_arg(c) (saved_argc > c ? ", \"" : ""), (saved_argc > c ? saved_argv[c] : ""), (saved_argc > c ? "\"" : "")
4022 +
4023 +static void fpm_pctl_exec()
4024 +{
4025 +
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"
4029 +               "})",
4030 +               saved_argv[0], saved_argv[0],
4031 +               optional_arg(1),
4032 +               optional_arg(2),
4033 +               optional_arg(3),
4034 +               optional_arg(4),
4035 +               optional_arg(5),
4036 +               optional_arg(6),
4037 +               optional_arg(7),
4038 +               optional_arg(8),
4039 +               optional_arg(9),
4040 +               optional_arg(10)
4041 +       );
4042 +
4043 +       fpm_cleanups_run(FPM_CLEANUP_PARENT_EXEC);
4044 +
4045 +       execvp(saved_argv[0], saved_argv);
4046 +
4047 +       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "execvp() failed");
4048 +
4049 +       exit(1);
4050 +}
4051 +
4052 +static void fpm_pctl_action_last()
4053 +{
4054 +       switch (fpm_state) {
4055 +
4056 +               case FPM_PCTL_STATE_RELOADING :
4057 +
4058 +                       fpm_pctl_exec();
4059 +                       break;
4060 +
4061 +               case FPM_PCTL_STATE_FINISHING :
4062 +
4063 +               case FPM_PCTL_STATE_TERMINATING :
4064 +
4065 +                       fpm_pctl_exit();
4066 +                       break;
4067 +       }
4068 +}
4069 +
4070 +int fpm_pctl_kill(pid_t pid, int how)
4071 +{
4072 +       int s = 0;
4073 +
4074 +       switch (how) {
4075 +               case FPM_PCTL_TERM :
4076 +                       s = SIGTERM;
4077 +                       break;
4078 +               case FPM_PCTL_STOP :
4079 +                       s = SIGSTOP;
4080 +                       break;
4081 +               case FPM_PCTL_CONT :
4082 +                       s = SIGCONT;
4083 +                       break;
4084 +               default :
4085 +                       break;
4086 +       }
4087 +
4088 +       return kill(pid, s);
4089 +}
4090 +
4091 +static void fpm_pctl_kill_all(int signo)
4092 +{
4093 +       struct fpm_worker_pool_s *wp;
4094 +       int alive_children = 0;
4095 +
4096 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
4097 +               struct fpm_child_s *child;
4098 +
4099 +               for (child = wp->children; child; child = child->next) {
4100 +
4101 +                       int res = kill(child->pid, signo);
4102 +
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);
4106 +
4107 +                       if (res == 0) ++alive_children;
4108 +               }
4109 +       }
4110 +
4111 +       if (alive_children) {
4112 +               zlog(ZLOG_STUFF, ZLOG_NOTICE, "%d %s still alive", alive_children, alive_children == 1 ? "child is" : "children are");
4113 +       }
4114 +}
4115 +
4116 +static void fpm_pctl_action_next()
4117 +{
4118 +       int sig, timeout;
4119 +
4120 +       if (!fpm_globals.running_children) fpm_pctl_action_last();
4121 +
4122 +       if (fpm_signal_sent == 0) {
4123 +               if (fpm_state == FPM_PCTL_STATE_TERMINATING) {
4124 +                       sig = SIGTERM;
4125 +               }
4126 +               else {
4127 +                       sig = SIGQUIT;
4128 +               }
4129 +               timeout = fpm_global_config.process_control_timeout;
4130 +       }
4131 +       else {
4132 +               if (fpm_signal_sent == SIGQUIT) {
4133 +                       sig = SIGTERM;
4134 +               }
4135 +               else {
4136 +                       sig = SIGKILL;
4137 +               }
4138 +               timeout = 1;
4139 +       }
4140 +
4141 +       fpm_pctl_kill_all(sig);
4142 +
4143 +       fpm_signal_sent = sig;
4144 +
4145 +       fpm_pctl_timeout_set(timeout);
4146 +}
4147 +
4148 +void fpm_pctl(int new_state, int action)
4149 +{
4150 +       switch (action) {
4151 +
4152 +               case FPM_PCTL_ACTION_SET :
4153 +
4154 +                       if (fpm_state == new_state) { /* already in progress - just ignore duplicate signal */
4155 +                               return;
4156 +                       }
4157 +
4158 +                       switch (fpm_state) { /* check which states can be overridden */
4159 +
4160 +                               case FPM_PCTL_STATE_NORMAL :
4161 +
4162 +                                       /* 'normal' can be overridden by any other state */
4163 +                                       break;
4164 +
4165 +                               case FPM_PCTL_STATE_RELOADING :
4166 +
4167 +                                       /* 'reloading' can be overridden by 'finishing' */
4168 +                                       if (new_state == FPM_PCTL_STATE_FINISHING) break;
4169 +
4170 +                               case FPM_PCTL_STATE_FINISHING :
4171 +
4172 +                                       /* 'reloading' and 'finishing' can be overridden by 'terminating' */
4173 +                                       if (new_state == FPM_PCTL_STATE_TERMINATING) break;
4174 +
4175 +                               case FPM_PCTL_STATE_TERMINATING :
4176 +
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]);
4180 +
4181 +                                       return;
4182 +                       }
4183 +
4184 +                       fpm_signal_sent = 0;
4185 +                       fpm_state = new_state;
4186 +
4187 +                       zlog(ZLOG_STUFF, ZLOG_NOTICE, "switching to '%s' state", fpm_state_names[fpm_state]);
4188 +
4189 +                       /* fall down */
4190 +
4191 +               case FPM_PCTL_ACTION_TIMEOUT :
4192 +
4193 +                       fpm_pctl_action_next();
4194 +
4195 +                       break;
4196 +
4197 +               case FPM_PCTL_ACTION_LAST_CHILD_EXITED :
4198 +
4199 +                       fpm_pctl_action_last();
4200 +
4201 +                       break;
4202 +
4203 +       }
4204 +}
4205 +
4206 +int fpm_pctl_can_spawn_children()
4207 +{
4208 +       return fpm_state == FPM_PCTL_STATE_NORMAL;
4209 +}
4210 +
4211 +int fpm_pctl_child_exited()
4212 +{
4213 +       if (fpm_state == FPM_PCTL_STATE_NORMAL) return 0;
4214 +
4215 +       if (!fpm_globals.running_children) {
4216 +               fpm_pctl(FPM_PCTL_STATE_UNSPECIFIED, FPM_PCTL_ACTION_LAST_CHILD_EXITED);
4217 +       }
4218 +
4219 +       return 0;
4220 +}
4221 +
4222 +int fpm_pctl_init_main()
4223 +{
4224 +       int i;
4225 +
4226 +       saved_argc = fpm_globals.argc;
4227 +
4228 +       saved_argv = malloc(sizeof(char *) * (saved_argc + 1));
4229 +
4230 +       if (!saved_argv) {
4231 +               return -1;
4232 +       }
4233 +
4234 +       for (i = 0; i < saved_argc; i++) {
4235 +               saved_argv[i] = strdup(fpm_globals.argv[i]);
4236 +
4237 +               if (!saved_argv[i]) {
4238 +                       return -1;
4239 +               }
4240 +       }
4241 +
4242 +       saved_argv[i] = 0;
4243 +
4244 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_pctl_cleanup, 0)) {
4245 +               return -1;
4246 +       }
4247 +
4248 +       return 0;
4249 +}
4250 +
4251 +static void fpm_pctl_check_request_timeout(struct timeval *now)
4252 +{
4253 +       struct fpm_worker_pool_s *wp;
4254 +
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;
4259 +
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);
4263 +                       }
4264 +               }
4265 +       }
4266 +       
4267 +}
4268 +
4269 +void fpm_pctl_heartbeat(int fd, short which, void *arg)
4270 +{
4271 +       static struct event heartbeat;
4272 +       struct timeval tv = { .tv_sec = 0, .tv_usec = 130000 };
4273 +       struct timeval now;
4274 +
4275 +       if (which == EV_TIMEOUT) {
4276 +               evtimer_del(&heartbeat);
4277 +               fpm_clock_get(&now);
4278 +               fpm_pctl_check_request_timeout(&now);
4279 +       }
4280 +
4281 +       evtimer_set(&heartbeat, &fpm_pctl_heartbeat, 0);
4282 +
4283 +       evtimer_add(&heartbeat, &tv);
4284 +}
4285 +
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
4289 --- /dev/null
4290 +++ b/sapi/cgi/fpm/fpm_process_ctl.h
4291 @@ -0,0 +1,39 @@
4292 +
4293 +       /* $Id$ */
4294 +       /* (c) 2007,2008 Andrei Nigmatulin */
4295 +
4296 +#ifndef FPM_PROCESS_CTL_H
4297 +#define FPM_PROCESS_CTL_H 1
4298 +
4299 +struct fpm_child_s;
4300 +
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();
4307 +
4308 +
4309 +enum {
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
4315 +};
4316 +
4317 +enum {
4318 +       FPM_PCTL_ACTION_SET,
4319 +       FPM_PCTL_ACTION_TIMEOUT,
4320 +       FPM_PCTL_ACTION_LAST_CHILD_EXITED
4321 +};
4322 +
4323 +enum {
4324 +       FPM_PCTL_TERM,
4325 +       FPM_PCTL_STOP,
4326 +       FPM_PCTL_CONT
4327 +};
4328 +
4329 +#endif
4330 +
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
4334 --- /dev/null
4335 +++ b/sapi/cgi/fpm/fpm_request.c
4336 @@ -0,0 +1,164 @@
4337 +
4338 +       /* $Id$ */
4339 +       /* (c) 2007,2008 Andrei Nigmatulin */
4340 +
4341 +#include "fpm_config.h"
4342 +
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"
4353 +
4354 +#include "zlog.h"
4355 +
4356 +void fpm_request_accepting()
4357 +{
4358 +       struct fpm_shm_slot_s *slot;
4359 +
4360 +       slot = fpm_shm_slots_acquire(0, 0);
4361 +
4362 +       slot->request_stage = FPM_REQUEST_ACCEPTING;
4363 +
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));
4368 +
4369 +       fpm_shm_slots_release(slot);
4370 +}
4371 +
4372 +void fpm_request_reading_headers()
4373 +{
4374 +       struct fpm_shm_slot_s *slot;
4375 +
4376 +       slot = fpm_shm_slots_acquire(0, 0);
4377 +
4378 +       slot->request_stage = FPM_REQUEST_READING_HEADERS;
4379 +
4380 +       fpm_clock_get(&slot->tv);
4381 +       slot->accepted = slot->tv;
4382 +
4383 +       fpm_shm_slots_release(slot);
4384 +}
4385 +
4386 +void fpm_request_info()
4387 +{
4388 +       TSRMLS_FETCH();
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);
4392 +
4393 +       slot = fpm_shm_slots_acquire(0, 0);
4394 +
4395 +       slot->request_stage = FPM_REQUEST_INFO;
4396 +
4397 +       fpm_clock_get(&slot->tv);
4398 +
4399 +       if (request_method) {
4400 +               cpystrn(slot->request_method, request_method, sizeof(slot->request_method));
4401 +       }
4402 +
4403 +       slot->content_length = fpm_php_content_length(TSRMLS_C);
4404 +
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));
4409 +       }
4410 +
4411 +       fpm_shm_slots_release(slot);
4412 +}
4413 +
4414 +void fpm_request_executing()
4415 +{
4416 +       struct fpm_shm_slot_s *slot;
4417 +
4418 +       slot = fpm_shm_slots_acquire(0, 0);
4419 +
4420 +       slot->request_stage = FPM_REQUEST_EXECUTING;
4421 +
4422 +       fpm_clock_get(&slot->tv);
4423 +
4424 +       fpm_shm_slots_release(slot);
4425 +}
4426 +
4427 +void fpm_request_finished()
4428 +{
4429 +       struct fpm_shm_slot_s *slot;
4430 +
4431 +       slot = fpm_shm_slots_acquire(0, 0);
4432 +
4433 +       slot->request_stage = FPM_REQUEST_FINISHED;
4434 +
4435 +       fpm_clock_get(&slot->tv);
4436 +       memset(&slot->accepted, 0, sizeof(slot->accepted));
4437 +
4438 +       fpm_shm_slots_release(slot);
4439 +}
4440 +
4441 +void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *now, int terminate_timeout, int slowlog_timeout)
4442 +{
4443 +       struct fpm_shm_slot_s *slot;
4444 +       struct fpm_shm_slot_s slot_c;
4445 +
4446 +       slot = fpm_shm_slot(child);
4447 +
4448 +       if (!fpm_shm_slots_acquire(slot, 1)) {
4449 +               return;
4450 +       }
4451 +
4452 +       slot_c = *slot;
4453 +
4454 +       fpm_shm_slots_release(slot);
4455 +
4456 +#if HAVE_FPM_TRACE
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;
4461 +               }
4462 +       }
4463 +#endif
4464 +
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;
4468 +
4469 +               timersub(now, &slot_c.accepted, &tv);
4470 +
4471 +#if HAVE_FPM_TRACE
4472 +               if (child->slow_logged.tv_sec == 0 && slowlog_timeout &&
4473 +                               slot_c.request_stage == FPM_REQUEST_EXECUTING && tv.tv_sec >= slowlog_timeout) {
4474 +                       
4475 +                       str_purify_filename(purified_script_filename, slot_c.script_filename, sizeof(slot_c.script_filename));
4476 +
4477 +                       child->slow_logged = slot_c.accepted;
4478 +                       child->tracer = fpm_php_trace;
4479 +
4480 +                       fpm_trace_signal(child->pid);
4481 +
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);
4484 +               }
4485 +
4486 +               else
4487 +#endif
4488 +               if (terminate_timeout && tv.tv_sec >= terminate_timeout) {
4489 +
4490 +                       str_purify_filename(purified_script_filename, slot_c.script_filename, sizeof(slot_c.script_filename));
4491 +
4492 +                       fpm_pctl_kill(child->pid, FPM_PCTL_TERM);
4493 +
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);
4496 +               }
4497 +       }
4498 +
4499 +}
4500 +
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
4504 --- /dev/null
4505 +++ b/sapi/cgi/fpm/fpm_request.h
4506 @@ -0,0 +1,27 @@
4507 +
4508 +       /* $Id$ */
4509 +       /* (c) 2007,2008 Andrei Nigmatulin */
4510 +
4511 +#ifndef FPM_REQUEST_H
4512 +#define FPM_REQUEST_H 1
4513 +
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 */
4519 +
4520 +struct fpm_child_s;
4521 +struct timeval;
4522 +
4523 +void fpm_request_check_timed_out(struct fpm_child_s *child, struct timeval *tv, int terminate_timeout, int slowlog_timeout);
4524 +
4525 +enum fpm_request_stage_e {
4526 +       FPM_REQUEST_ACCEPTING = 1,
4527 +       FPM_REQUEST_READING_HEADERS,
4528 +       FPM_REQUEST_INFO,
4529 +       FPM_REQUEST_EXECUTING,
4530 +       FPM_REQUEST_FINISHED
4531 +};
4532 +
4533 +#endif
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
4537 --- /dev/null
4538 +++ b/sapi/cgi/fpm/fpm_shm.c
4539 @@ -0,0 +1,100 @@
4540 +
4541 +       /* $Id$ */
4542 +       /* (c) 2007,2008 Andrei Nigmatulin */
4543 +
4544 +#include "fpm_config.h"
4545 +
4546 +#include <unistd.h>
4547 +#include <sys/mman.h>
4548 +#include <stdlib.h>
4549 +
4550 +#include "fpm_shm.h"
4551 +#include "zlog.h"
4552 +
4553 +
4554 +/* MAP_ANON is depricated, but not in macosx */
4555 +#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
4556 +#define MAP_ANONYMOUS MAP_ANON
4557 +#endif
4558 +
4559 +
4560 +struct fpm_shm_s *fpm_shm_alloc(size_t sz)
4561 +{
4562 +       struct fpm_shm_s *shm;
4563 +
4564 +       shm = malloc(sizeof(*shm));
4565 +
4566 +       if (!shm) {
4567 +               return 0;
4568 +       }
4569 +
4570 +       shm->mem = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
4571 +
4572 +       if (!shm->mem) {
4573 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "mmap(MAP_ANONYMOUS | MAP_SHARED) failed");
4574 +               free(shm);
4575 +               return 0;
4576 +       }
4577 +
4578 +       shm->used = 0;
4579 +       shm->sz = sz;
4580 +
4581 +       return shm;
4582 +}
4583 +
4584 +static void fpm_shm_free(struct fpm_shm_s *shm, int do_unmap)
4585 +{
4586 +       if (do_unmap) {
4587 +               munmap(shm->mem, shm->sz);
4588 +       }
4589 +
4590 +       free(shm);      
4591 +}
4592 +
4593 +void fpm_shm_free_list(struct fpm_shm_s *shm, void *mem)
4594 +{
4595 +       struct fpm_shm_s *next;
4596 +
4597 +       for (; shm; shm = next) {
4598 +               next = shm->next;
4599 +
4600 +               fpm_shm_free(shm, mem != shm->mem);
4601 +       }
4602 +}
4603 +
4604 +void *fpm_shm_alloc_chunk(struct fpm_shm_s **head, size_t sz, void **mem)
4605 +{
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;
4610 +       void *ret;
4611 +
4612 +       sz = (sz + cache_line_size - 1) & -cache_line_size;
4613 +
4614 +       shm = *head;
4615 +
4616 +       if (0 == shm || shm->sz - shm->used < sz) {
4617 +               /* allocate one more shm segment */
4618 +
4619 +               aligned_sz = (sz + pagesize - 1) & -pagesize;
4620 +
4621 +               shm = fpm_shm_alloc(aligned_sz);
4622 +
4623 +               if (!shm) {
4624 +                       return 0;
4625 +               }
4626 +
4627 +               shm->next = *head;
4628 +               if (shm->next) shm->next->prev = shm;
4629 +               shm->prev = 0;
4630 +               *head = shm;
4631 +       }
4632 +
4633 +       *mem = shm->mem;
4634 +       ret = (char *) shm->mem + shm->used;
4635 +       shm->used += sz;
4636 +
4637 +       return ret;
4638 +}
4639 +
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
4643 --- /dev/null
4644 +++ b/sapi/cgi/fpm/fpm_shm.h
4645 @@ -0,0 +1,22 @@
4646 +
4647 +       /* $Id$ */
4648 +       /* (c) 2007,2008 Andrei Nigmatulin */
4649 +
4650 +#ifndef FPM_SHM_H
4651 +#define FPM_SHM_H 1
4652 +
4653 +struct fpm_shm_s;
4654 +
4655 +struct fpm_shm_s {
4656 +       struct fpm_shm_s *prev, *next;
4657 +       void *mem;
4658 +       size_t sz;
4659 +       size_t used;
4660 +};
4661 +
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);
4665 +
4666 +#endif
4667 +
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
4671 --- /dev/null
4672 +++ b/sapi/cgi/fpm/fpm_shm_slots.c
4673 @@ -0,0 +1,127 @@
4674 +
4675 +       /* $Id$ */
4676 +       /* (c) 2007,2008 Andrei Nigmatulin */
4677 +
4678 +#include "fpm_config.h"
4679 +
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"
4685 +#include "zlog.h"
4686 +
4687 +static void *shm_mem;
4688 +static struct fpm_shm_slot_s *shm_slot;
4689 +
4690 +int fpm_shm_slots_prepare_slot(struct fpm_child_s *child)
4691 +{
4692 +       struct fpm_worker_pool_s *wp = child->wp;
4693 +       struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4694 +
4695 +       child->shm_slot_i = wp->slots_used.used;
4696 +
4697 +       shm_slot_ptr = fpm_array_push(&wp->slots_used);
4698 +
4699 +       if (0 == shm_slot_ptr) {
4700 +               return -1;
4701 +       }
4702 +
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);
4705 +
4706 +               if (!shm_slot_ptr->shm_slot) {
4707 +                       return -1;
4708 +               }
4709 +       }
4710 +       else {
4711 +               *shm_slot_ptr = *(struct fpm_shm_slot_ptr_s *) fpm_array_item_last(&wp->slots_free);
4712 +
4713 +               --wp->slots_free.used;
4714 +       }
4715 +
4716 +       memset(shm_slot_ptr->shm_slot, 0, sizeof(struct fpm_shm_slot_s));
4717 +
4718 +       shm_slot_ptr->child = child;
4719 +
4720 +       return 0;
4721 +}
4722 +
4723 +void fpm_shm_slots_discard_slot(struct fpm_child_s *child)
4724 +{
4725 +       struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4726 +       struct fpm_worker_pool_s *wp = child->wp;
4727 +       int n;
4728 +
4729 +       shm_slot_ptr = fpm_array_push(&wp->slots_free);
4730 +
4731 +       if (shm_slot_ptr) {
4732 +
4733 +               struct fpm_shm_slot_ptr_s *shm_slot_ptr_used;
4734 +
4735 +               shm_slot_ptr_used = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4736 +
4737 +               *shm_slot_ptr = *shm_slot_ptr_used;
4738 +
4739 +               shm_slot_ptr->child = 0;
4740 +
4741 +       }
4742 +
4743 +       n = fpm_array_item_remove(&wp->slots_used, child->shm_slot_i);
4744 +
4745 +       if (n > -1) {
4746 +               shm_slot_ptr = fpm_array_item(&wp->slots_used, n);
4747 +
4748 +               shm_slot_ptr->child->shm_slot_i = n;
4749 +       }
4750 +}
4751 +
4752 +void fpm_shm_slots_child_use_slot(struct fpm_child_s *child)
4753 +{
4754 +       struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4755 +       struct fpm_worker_pool_s *wp = child->wp;
4756 +
4757 +       shm_slot_ptr = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4758 +
4759 +       shm_slot = shm_slot_ptr->shm_slot;
4760 +       shm_mem = shm_slot_ptr->mem;
4761 +}
4762 +
4763 +void fpm_shm_slots_parent_use_slot(struct fpm_child_s *child)
4764 +{
4765 +       /* nothing to do */
4766 +}
4767 +
4768 +void *fpm_shm_slots_mem()
4769 +{
4770 +       return shm_mem;
4771 +}
4772 +
4773 +struct fpm_shm_slot_s *fpm_shm_slot(struct fpm_child_s *child)
4774 +{
4775 +       struct fpm_shm_slot_ptr_s *shm_slot_ptr;
4776 +       struct fpm_worker_pool_s *wp = child->wp;
4777 +
4778 +       shm_slot_ptr = fpm_array_item(&wp->slots_used, child->shm_slot_i);
4779 +
4780 +       return shm_slot_ptr->shm_slot;
4781 +}
4782 +
4783 +struct fpm_shm_slot_s *fpm_shm_slots_acquire(struct fpm_shm_slot_s *s, int nohang)
4784 +{
4785 +       if (s == 0) {
4786 +               s = shm_slot;
4787 +       }
4788 +
4789 +       if (0 > fpm_spinlock(&s->lock, nohang)) {
4790 +               return 0;
4791 +       }
4792 +
4793 +       return s;
4794 +}
4795 +
4796 +void fpm_shm_slots_release(struct fpm_shm_slot_s *s)
4797 +{
4798 +       s->lock = 0;
4799 +}
4800 +
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
4804 --- /dev/null
4805 +++ b/sapi/cgi/fpm/fpm_shm_slots.h
4806 @@ -0,0 +1,43 @@
4807 +
4808 +       /* $Id$ */
4809 +       /* (c) 2007,2008 Andrei Nigmatulin */
4810 +
4811 +#ifndef FPM_SHM_SLOTS_H
4812 +#define FPM_SHM_SLOTS_H 1
4813 +
4814 +#include "fpm_atomic.h"
4815 +#include "fpm_worker_pool.h"
4816 +#include "fpm_request.h"
4817 +
4818 +struct fpm_child_s;
4819 +
4820 +struct fpm_shm_slot_s {
4821 +       union {
4822 +               atomic_t lock;
4823 +               char dummy[16];
4824 +       };
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];
4831 +};
4832 +
4833 +struct fpm_shm_slot_ptr_s {
4834 +       void *mem;
4835 +       struct fpm_shm_slot_s *shm_slot;
4836 +       struct fpm_child_s *child;
4837 +};
4838 +
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 *);
4847 +
4848 +#endif
4849 +
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
4853 --- /dev/null
4854 +++ b/sapi/cgi/fpm/fpm_signals.c
4855 @@ -0,0 +1,252 @@
4856 +
4857 +       /* $Id$ */
4858 +       /* (c) 2007,2008 Andrei Nigmatulin */
4859 +
4860 +#include "fpm_config.h"
4861 +
4862 +#include <signal.h>
4863 +#include <stdio.h>
4864 +#include <sys/types.h>
4865 +#include <sys/socket.h>
4866 +#include <stdlib.h>
4867 +#include <string.h>
4868 +#include <fcntl.h>
4869 +#include <unistd.h>
4870 +#include <errno.h>
4871 +
4872 +#include "fpm.h"
4873 +#include "fpm_signals.h"
4874 +#include "fpm_sockets.h"
4875 +#include "fpm_php.h"
4876 +#include "zlog.h"
4877 +
4878 +static int sp[2];
4879 +
4880 +const char *fpm_signal_names[NSIG + 1] = {
4881 +#ifdef SIGHUP
4882 +       [SIGHUP]                = "SIGHUP",
4883 +#endif
4884 +#ifdef SIGINT
4885 +       [SIGINT]                = "SIGINT",
4886 +#endif
4887 +#ifdef SIGQUIT
4888 +       [SIGQUIT]               = "SIGQUIT",
4889 +#endif
4890 +#ifdef SIGILL
4891 +       [SIGILL]                = "SIGILL",
4892 +#endif
4893 +#ifdef SIGTRAP
4894 +       [SIGTRAP]               = "SIGTRAP",
4895 +#endif
4896 +#ifdef SIGABRT
4897 +       [SIGABRT]               = "SIGABRT",
4898 +#endif
4899 +#ifdef SIGEMT
4900 +       [SIGEMT]                = "SIGEMT",
4901 +#endif
4902 +#ifdef SIGBUS
4903 +       [SIGBUS]                = "SIGBUS",
4904 +#endif
4905 +#ifdef SIGFPE
4906 +       [SIGFPE]                = "SIGFPE",
4907 +#endif
4908 +#ifdef SIGKILL
4909 +       [SIGKILL]               = "SIGKILL",
4910 +#endif
4911 +#ifdef SIGUSR1
4912 +       [SIGUSR1]               = "SIGUSR1",
4913 +#endif
4914 +#ifdef SIGSEGV
4915 +       [SIGSEGV]               = "SIGSEGV",
4916 +#endif
4917 +#ifdef SIGUSR2
4918 +       [SIGUSR2]               = "SIGUSR2",
4919 +#endif
4920 +#ifdef SIGPIPE
4921 +       [SIGPIPE]               = "SIGPIPE",
4922 +#endif
4923 +#ifdef SIGALRM
4924 +       [SIGALRM]               = "SIGALRM",
4925 +#endif
4926 +#ifdef SIGTERM
4927 +       [SIGTERM]               = "SIGTERM",
4928 +#endif
4929 +#ifdef SIGCHLD
4930 +       [SIGCHLD]               = "SIGCHLD",
4931 +#endif
4932 +#ifdef SIGCONT
4933 +       [SIGCONT]               = "SIGCONT",
4934 +#endif
4935 +#ifdef SIGSTOP
4936 +       [SIGSTOP]               = "SIGSTOP",
4937 +#endif
4938 +#ifdef SIGTSTP
4939 +       [SIGTSTP]               = "SIGTSTP",
4940 +#endif
4941 +#ifdef SIGTTIN
4942 +       [SIGTTIN]               = "SIGTTIN",
4943 +#endif
4944 +#ifdef SIGTTOU
4945 +       [SIGTTOU]               = "SIGTTOU",
4946 +#endif
4947 +#ifdef SIGURG
4948 +       [SIGURG]                = "SIGURG",
4949 +#endif
4950 +#ifdef SIGXCPU
4951 +       [SIGXCPU]               = "SIGXCPU",
4952 +#endif
4953 +#ifdef SIGXFSZ
4954 +       [SIGXFSZ]               = "SIGXFSZ",
4955 +#endif
4956 +#ifdef SIGVTALRM
4957 +       [SIGVTALRM]     = "SIGVTALRM",
4958 +#endif
4959 +#ifdef SIGPROF
4960 +       [SIGPROF]               = "SIGPROF",
4961 +#endif
4962 +#ifdef SIGWINCH
4963 +       [SIGWINCH]              = "SIGWINCH",
4964 +#endif
4965 +#ifdef SIGINFO
4966 +       [SIGINFO]               = "SIGINFO",
4967 +#endif
4968 +#ifdef SIGIO
4969 +       [SIGIO]                 = "SIGIO",
4970 +#endif
4971 +#ifdef SIGPWR
4972 +       [SIGPWR]                = "SIGPWR",
4973 +#endif
4974 +#ifdef SIGSYS
4975 +       [SIGSYS]                = "SIGSYS",
4976 +#endif
4977 +#ifdef SIGWAITING
4978 +       [SIGWAITING]    = "SIGWAITING",
4979 +#endif
4980 +#ifdef SIGLWP
4981 +       [SIGLWP]                = "SIGLWP",
4982 +#endif
4983 +#ifdef SIGFREEZE
4984 +       [SIGFREEZE]     = "SIGFREEZE",
4985 +#endif
4986 +#ifdef SIGTHAW
4987 +       [SIGTHAW]               = "SIGTHAW",
4988 +#endif
4989 +#ifdef SIGCANCEL
4990 +       [SIGCANCEL]     = "SIGCANCEL",
4991 +#endif
4992 +#ifdef SIGLOST
4993 +       [SIGLOST]               = "SIGLOST",
4994 +#endif
4995 +};
4996 +
4997 +static void sig_soft_quit(int signo)
4998 +{
4999 +       int saved_errno = errno;
5000 +
5001 +       /* closing fastcgi listening socket will force fcgi_accept() exit immediately */
5002 +       close(0);
5003 +       socket(AF_UNIX, SOCK_STREAM, 0);
5004 +
5005 +       fpm_php_soft_quit();
5006 +
5007 +       errno = saved_errno;
5008 +}
5009 +
5010 +static void sig_handler(int signo)
5011 +{
5012 +       static const char sig_chars[NSIG + 1] = {
5013 +               [SIGTERM] = 'T',
5014 +               [SIGINT]  = 'I',
5015 +               [SIGUSR1] = '1',
5016 +               [SIGUSR2] = '2',
5017 +               [SIGQUIT] = 'Q',
5018 +               [SIGCHLD] = 'C'
5019 +       };
5020 +       char s;
5021 +       int saved_errno;
5022 +
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 */
5026 +               return;
5027 +       }
5028 +
5029 +       saved_errno = errno;
5030 +
5031 +       s = sig_chars[signo];
5032 +
5033 +       write(sp[1], &s, sizeof(s));
5034 +
5035 +       errno = saved_errno;
5036 +}
5037 +
5038 +int fpm_signals_init_main()
5039 +{
5040 +       struct sigaction act;
5041 +
5042 +       if (0 > socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) {
5043 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "socketpair() failed");
5044 +               return -1;
5045 +       }
5046 +
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");
5049 +               return -1;
5050 +       }
5051 +
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");
5054 +               return -1;
5055 +       }
5056 +
5057 +       memset(&act, 0, sizeof(act));
5058 +       act.sa_handler = sig_handler;
5059 +       sigfillset(&act.sa_mask);
5060 +
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)) {
5067 +
5068 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "sigaction() failed");
5069 +               return -1;
5070 +       }
5071 +
5072 +       return 0;
5073 +}
5074 +
5075 +int fpm_signals_init_child()
5076 +{
5077 +       struct sigaction act, act_dfl;
5078 +
5079 +       memset(&act, 0, sizeof(act));
5080 +       memset(&act_dfl, 0, sizeof(act_dfl));
5081 +
5082 +       act.sa_handler = &sig_soft_quit;
5083 +       act.sa_flags |= SA_RESTART;
5084 +
5085 +       act_dfl.sa_handler = SIG_DFL;
5086 +
5087 +       close(sp[0]);
5088 +       close(sp[1]);
5089 +
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)) {
5096 +
5097 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "sigaction() failed");
5098 +               return -1;
5099 +       }
5100 +
5101 +       return 0;
5102 +}
5103 +
5104 +int fpm_signals_get_fd()
5105 +{
5106 +       return sp[0];
5107 +}
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
5111 --- /dev/null
5112 +++ b/sapi/cgi/fpm/fpm_signals.h
5113 @@ -0,0 +1,16 @@
5114 +
5115 +       /* $Id$ */
5116 +       /* (c) 2007,2008 Andrei Nigmatulin */
5117 +
5118 +#ifndef FPM_SIGNALS_H
5119 +#define FPM_SIGNALS_H 1
5120 +
5121 +#include <signal.h>
5122 +
5123 +int fpm_signals_init_main();
5124 +int fpm_signals_init_child();
5125 +int fpm_signals_get_fd();
5126 +
5127 +extern const char *fpm_signal_names[NSIG + 1];
5128 +
5129 +#endif
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
5133 --- /dev/null
5134 +++ b/sapi/cgi/fpm/fpm_sockets.c
5135 @@ -0,0 +1,427 @@
5136 +
5137 +       /* $Id$ */
5138 +       /* (c) 2007,2008 Andrei Nigmatulin */
5139 +
5140 +#include "fpm_config.h"
5141 +
5142 +#ifdef HAVE_ALLOCA_H
5143 +#include <alloca.h>
5144 +#endif
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>
5151 +#include <netdb.h>
5152 +#include <stdio.h>
5153 +#include <stdlib.h>
5154 +#include <string.h>
5155 +#include <errno.h>
5156 +#include <unistd.h>
5157 +
5158 +#include "zlog.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"
5166 +
5167 +struct listening_socket_s {
5168 +       int refcount;
5169 +       int sock;
5170 +       int type;
5171 +       char *key;
5172 +};
5173 +
5174 +static struct fpm_array_s sockets_list;
5175 +
5176 +static int fpm_sockets_resolve_af_inet(char *node, char *service, struct sockaddr_in *addr)
5177 +{
5178 +       struct addrinfo *res;
5179 +       struct addrinfo hints;
5180 +       int ret;
5181 +
5182 +       memset(&hints, 0, sizeof(hints));
5183 +
5184 +       hints.ai_family = AF_INET;
5185 +
5186 +       ret = getaddrinfo(node, service, &hints, &res);
5187 +
5188 +       if (ret != 0) {
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) : "");
5192 +               return -1;
5193 +       }
5194 +
5195 +       *addr = *(struct sockaddr_in *) res->ai_addr;
5196 +
5197 +       freeaddrinfo(res);
5198 +
5199 +       return 0;
5200 +}
5201 +
5202 +enum { FPM_GET_USE_SOCKET = 1, FPM_STORE_SOCKET = 2, FPM_STORE_USE_SOCKET = 3 };
5203 +
5204 +static void fpm_sockets_cleanup(int which, void *arg)
5205 +{
5206 +       int i;
5207 +       char *env_value = 0;
5208 +       int p = 0;
5209 +       struct listening_socket_s *ls = sockets_list.data;
5210 +
5211 +       for (i = 0; i < sockets_list.used; i++, ls++) {
5212 +
5213 +               if (which != FPM_CLEANUP_PARENT_EXEC) {
5214 +
5215 +                       close(ls->sock);
5216 +
5217 +               }
5218 +               else { /* on PARENT EXEC we want socket fds to be inherited through environment variable */
5219 +                       char fd[32];
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);
5223 +               }
5224 +
5225 +               if (which == FPM_CLEANUP_PARENT_EXIT_MAIN) {
5226 +
5227 +                       if (ls->type == FPM_AF_UNIX) {
5228 +                               unlink(ls->key);
5229 +                       }
5230 +
5231 +               }
5232 +
5233 +               free(ls->key);
5234 +       }
5235 +
5236 +       if (env_value) {
5237 +               setenv("FPM_SOCKETS", env_value, 1);
5238 +               free(env_value);
5239 +       }
5240 +
5241 +       fpm_array_free(&sockets_list);
5242 +}
5243 +
5244 +static int fpm_sockets_hash_op(int sock, struct sockaddr *sa, char *key, int type, int op)
5245 +{
5246 +
5247 +       if (key == NULL) {
5248 +
5249 +               switch (type) {
5250 +
5251 +                       case FPM_AF_INET : {
5252 +                               struct sockaddr_in *sa_in = (struct sockaddr_in *) sa;
5253 +
5254 +                               key = alloca(sizeof("xxx.xxx.xxx.xxx:ppppp"));
5255 +
5256 +                               sprintf(key, "%u.%u.%u.%u:%u", IPQUAD(&sa_in->sin_addr), (unsigned int) ntohs(sa_in->sin_port));
5257 +
5258 +                               break;
5259 +                       }
5260 +
5261 +                       case FPM_AF_UNIX : {
5262 +                               struct sockaddr_un *sa_un = (struct sockaddr_un *) sa;
5263 +
5264 +                               key = alloca(strlen(sa_un->sun_path) + 1);
5265 +
5266 +                               strcpy(key, sa_un->sun_path);
5267 +
5268 +                               break;
5269 +                       }
5270 +
5271 +                       default :
5272 +
5273 +                               return -1;
5274 +               }
5275 +
5276 +       }
5277 +
5278 +       switch (op) {
5279 +
5280 +               case FPM_GET_USE_SOCKET :
5281 +               {
5282 +
5283 +                       int i;
5284 +                       struct listening_socket_s *ls = sockets_list.data;
5285 +
5286 +                       for (i = 0; i < sockets_list.used; i++, ls++) {
5287 +
5288 +                               if (!strcmp(ls->key, key)) {
5289 +                                       ++ls->refcount;
5290 +                                       return ls->sock;
5291 +                               }
5292 +                       }
5293 +
5294 +                       break;
5295 +               }
5296 +
5297 +               case FPM_STORE_SOCKET :                 /* inherited socket */
5298 +               case FPM_STORE_USE_SOCKET :             /* just created */
5299 +               {
5300 +
5301 +                       struct listening_socket_s *ls;
5302 +
5303 +                       ls = fpm_array_push(&sockets_list);
5304 +
5305 +                       if (!ls) {
5306 +                               break;
5307 +                       }
5308 +
5309 +                       if (op == FPM_STORE_SOCKET) {
5310 +                               ls->refcount = 0;
5311 +                       }
5312 +                       else {
5313 +                               ls->refcount = 1;
5314 +                       }
5315 +                       ls->type = type;
5316 +                       ls->sock = sock;
5317 +                       ls->key = strdup(key);
5318 +
5319 +                       return 0;
5320 +
5321 +               }
5322 +       }
5323 +
5324 +       return -1;
5325 +
5326 +}
5327 +
5328 +static int fpm_sockets_new_listening_socket(struct fpm_worker_pool_s *wp, struct sockaddr *sa, int socklen)
5329 +{
5330 +       int backlog = -1;
5331 +       int flags = 1;
5332 +       int sock;
5333 +       mode_t saved_umask;
5334 +
5335 +       /* we have custom backlog value */
5336 +       if (wp->config->listen_options) {
5337 +               backlog = wp->config->listen_options->backlog;
5338 +       }
5339 +
5340 +       sock = socket(sa->sa_family, SOCK_STREAM, 0);
5341 +
5342 +       if (0 > sock) {
5343 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "socket() failed");
5344 +               return -1;
5345 +       }
5346 +
5347 +       setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
5348 +
5349 +       if (wp->listen_address_domain == FPM_AF_UNIX) {
5350 +               unlink( ((struct sockaddr_un *) sa)->sun_path);
5351 +       }
5352 +
5353 +       saved_umask = umask(0777 ^ wp->socket_mode);
5354 +
5355 +       if (0 > bind(sock, sa, socklen)) {
5356 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "bind() for address '%s' failed", wp->config->listen_address);
5357 +               return -1;
5358 +       }
5359 +
5360 +       if (wp->listen_address_domain == FPM_AF_UNIX) {
5361 +
5362 +               char *path = ((struct sockaddr_un *) sa)->sun_path;
5363 +
5364 +               if (wp->socket_uid != -1 || wp->socket_gid != -1) {
5365 +
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);
5368 +                               return -1;
5369 +                       }
5370 +
5371 +               }
5372 +
5373 +       }
5374 +
5375 +       umask(saved_umask);
5376 +
5377 +       if (0 > listen(sock, backlog)) {
5378 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "listen() for address '%s' failed", wp->config->listen_address);
5379 +               return -1;
5380 +       }
5381 +
5382 +       return sock;
5383 +}
5384 +
5385 +static int fpm_sockets_get_listening_socket(struct fpm_worker_pool_s *wp, struct sockaddr *sa, int socklen)
5386 +{
5387 +       int sock;
5388 +
5389 +       sock = fpm_sockets_hash_op(0, sa, 0, wp->listen_address_domain, FPM_GET_USE_SOCKET);
5390 +
5391 +       if (sock >= 0) return sock;
5392 +
5393 +       sock = fpm_sockets_new_listening_socket(wp, sa, socklen);
5394 +
5395 +       fpm_sockets_hash_op(sock, sa, 0, wp->listen_address_domain, FPM_STORE_USE_SOCKET);
5396 +
5397 +       return sock;
5398 +}
5399 +
5400 +enum fpm_address_domain fpm_sockets_domain_from_address(char *address)
5401 +{
5402 +       if (strchr(address, ':')) return FPM_AF_INET;
5403 +
5404 +       if (strlen(address) == strspn(address, "0123456789")) return FPM_AF_INET;
5405 +
5406 +       return FPM_AF_UNIX;
5407 +}
5408 +
5409 +static int fpm_socket_af_inet_listening_socket(struct fpm_worker_pool_s *wp)
5410 +{
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;
5415 +       int port = 0;
5416 +
5417 +       if (port_str) { /* this is host:port pair */
5418 +               *port_str++ = '\0';
5419 +               port = atoi(port_str);
5420 +               addr = dup_address;
5421 +       }
5422 +       else if (strlen(dup_address) == strspn(dup_address, "0123456789")) { /* this is port */
5423 +               port = atoi(dup_address);
5424 +               port_str = dup_address;
5425 +       }
5426 +
5427 +       if (port == 0) {
5428 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "invalid port value '%s'", port_str);
5429 +               return -1;
5430 +       }
5431 +
5432 +       memset(&sa_in, 0, sizeof(sa_in));
5433 +
5434 +       if (addr) {
5435 +
5436 +               sa_in.sin_addr.s_addr = inet_addr(addr);
5437 +
5438 +               if (sa_in.sin_addr.s_addr == INADDR_NONE) { /* do resolve */
5439 +                       if (0 > fpm_sockets_resolve_af_inet(addr, NULL, &sa_in)) {
5440 +                               return -1;
5441 +                       }
5442 +                       zlog(ZLOG_STUFF, ZLOG_NOTICE, "address '%s' resolved as %u.%u.%u.%u", addr, IPQUAD(&sa_in.sin_addr));
5443 +               }
5444 +       }
5445 +       else {
5446 +
5447 +               sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
5448 +
5449 +       }
5450 +
5451 +       sa_in.sin_family = AF_INET;
5452 +       sa_in.sin_port = htons(port);
5453 +
5454 +       free(dup_address);
5455 +
5456 +       return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_in, sizeof(struct sockaddr_in));
5457 +}
5458 +
5459 +static int fpm_socket_af_unix_listening_socket(struct fpm_worker_pool_s *wp)
5460 +{
5461 +       struct sockaddr_un sa_un;
5462 +
5463 +       memset(&sa_un, 0, sizeof(sa_un));
5464 +
5465 +       cpystrn(sa_un.sun_path, wp->config->listen_address, sizeof(sa_un.sun_path));
5466 +       sa_un.sun_family = AF_UNIX;
5467 +
5468 +       return fpm_sockets_get_listening_socket(wp, (struct sockaddr *) &sa_un, sizeof(struct sockaddr_un));
5469 +}
5470 +
5471 +int fpm_sockets_init_main()
5472 +{
5473 +       int i;
5474 +       struct fpm_worker_pool_s *wp;
5475 +       char *inherited = getenv("FPM_SOCKETS");
5476 +       struct listening_socket_s *ls;
5477 +
5478 +       if (0 == fpm_array_init(&sockets_list, sizeof(struct listening_socket_s), 10)) {
5479 +               return -1;
5480 +       }
5481 +
5482 +       /* import inherited sockets */
5483 +       while (inherited && *inherited) {
5484 +               char *comma = strchr(inherited, ',');
5485 +               int type, fd_no;
5486 +               char *eq;
5487 +
5488 +               if (comma) *comma = '\0';
5489 +
5490 +               eq = strchr(inherited, '=');
5491 +
5492 +               if (eq) {
5493 +                       *eq = '\0';
5494 +
5495 +                       fd_no = atoi(eq + 1);
5496 +
5497 +                       type = fpm_sockets_domain_from_address(inherited);
5498 +
5499 +                       zlog(ZLOG_STUFF, ZLOG_NOTICE, "using inherited socket fd=%d, \"%s\"", fd_no, inherited);
5500 +
5501 +                       fpm_sockets_hash_op(fd_no, 0, inherited, type, FPM_STORE_SOCKET);
5502 +               }
5503 +
5504 +               if (comma) inherited = comma + 1;
5505 +               else inherited = 0;
5506 +       }
5507 +
5508 +       /* create all required sockets */
5509 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
5510 +
5511 +               if (!wp->is_template) {
5512 +
5513 +                       switch (wp->listen_address_domain) {
5514 +
5515 +                               case FPM_AF_INET :
5516 +
5517 +                                       wp->listening_socket = fpm_socket_af_inet_listening_socket(wp);
5518 +                                       break;
5519 +
5520 +                               case FPM_AF_UNIX :
5521 +
5522 +                                       if (0 > fpm_unix_resolve_socket_premissions(wp)) {
5523 +                                               return -1;
5524 +                                       }
5525 +
5526 +                                       wp->listening_socket = fpm_socket_af_unix_listening_socket(wp);
5527 +                                       break;
5528 +
5529 +                       }
5530 +
5531 +                       if (wp->listening_socket == -1) {
5532 +                               return -1;
5533 +                       }
5534 +               }
5535 +
5536 +       }
5537 +
5538 +       /* close unused sockets that was inherited */
5539 +       ls = sockets_list.data;
5540 +
5541 +       for (i = 0; i < sockets_list.used; ) {
5542 +
5543 +               if (ls->refcount == 0) {
5544 +                       close(ls->sock);
5545 +                       if (ls->type == FPM_AF_UNIX) {
5546 +                               unlink(ls->key);
5547 +                       }
5548 +                       free(ls->key);
5549 +                       fpm_array_item_remove(&sockets_list, i);
5550 +               }
5551 +               else {
5552 +                       ++i;
5553 +                       ++ls;
5554 +               }
5555 +       }
5556 +
5557 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_sockets_cleanup, 0)) {
5558 +               return -1;
5559 +       }
5560 +
5561 +       return 0;
5562 +}
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
5566 --- /dev/null
5567 +++ b/sapi/cgi/fpm/fpm_sockets.h
5568 @@ -0,0 +1,37 @@
5569 +
5570 +       /* $Id$ */
5571 +       /* (c) 2007,2008 Andrei Nigmatulin */
5572 +
5573 +#ifndef FPM_MISC_H
5574 +#define FPM_MISC_H 1
5575 +
5576 +#include <unistd.h>
5577 +#include <fcntl.h>
5578 +
5579 +#include "fpm_worker_pool.h"
5580 +
5581 +enum fpm_address_domain fpm_sockets_domain_from_address(char *addr);
5582 +int fpm_sockets_init_main();
5583 +
5584 +
5585 +static inline int fd_set_blocked(int fd, int blocked)
5586 +{
5587 +       int flags = fcntl(fd, F_GETFL);
5588 +
5589 +       if (flags < 0) return -1;
5590 +
5591 +       if (blocked)
5592 +               flags &= ~O_NONBLOCK;
5593 +       else
5594 +               flags |= O_NONBLOCK;
5595 +
5596 +       return fcntl(fd, F_SETFL, flags);
5597 +}
5598 +
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]
5604 +
5605 +#endif
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
5609 --- /dev/null
5610 +++ b/sapi/cgi/fpm/fpm_stdio.c
5611 @@ -0,0 +1,286 @@
5612 +
5613 +       /* $Id$ */
5614 +       /* (c) 2007,2008 Andrei Nigmatulin */
5615 +
5616 +#include "fpm_config.h"
5617 +
5618 +#include <sys/types.h>
5619 +#include <sys/stat.h>
5620 +#include <string.h>
5621 +#include <fcntl.h>
5622 +#include <unistd.h>
5623 +#include <errno.h>
5624 +
5625 +#include "fpm.h"
5626 +#include "fpm_children.h"
5627 +#include "fpm_events.h"
5628 +#include "fpm_sockets.h"
5629 +#include "fpm_stdio.h"
5630 +#include "zlog.h"
5631 +
5632 +static int fd_stdout[2];
5633 +static int fd_stderr[2];
5634 +
5635 +int fpm_stdio_init_main()
5636 +{
5637 +       int fd = open("/dev/null", O_RDWR);
5638 +
5639 +       if (0 > fd) {
5640 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"/dev/null\") failed");
5641 +               return -1;
5642 +       }
5643 +
5644 +       if (0 > dup2(fd, STDIN_FILENO) || 0 > dup2(fd, STDOUT_FILENO)) {
5645 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
5646 +               return -1;
5647 +       }
5648 +
5649 +       close(fd);
5650 +
5651 +       return 0;
5652 +}
5653 +
5654 +int fpm_stdio_init_final()
5655 +{
5656 +       if (fpm_global_config.daemonize) {
5657 +
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");
5662 +                               return -1;
5663 +                       }
5664 +               }
5665 +
5666 +               zlog_set_level(fpm_globals.log_level);
5667 +
5668 +               zlog_set_fd(fpm_globals.error_log_fd);
5669 +       }
5670 +
5671 +       return 0;
5672 +}
5673 +
5674 +int fpm_stdio_init_child(struct fpm_worker_pool_s *wp)
5675 +{
5676 +       close(fpm_globals.error_log_fd);
5677 +       fpm_globals.error_log_fd = -1;
5678 +       zlog_set_fd(-1);
5679 +
5680 +       if (wp->listening_socket != STDIN_FILENO) {
5681 +               if (0 > dup2(wp->listening_socket, STDIN_FILENO)) {
5682 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
5683 +                       return -1;
5684 +               }
5685 +       }
5686 +
5687 +       return 0;
5688 +}
5689 +
5690 +static void fpm_stdio_child_said(int fd, short which, void *arg)
5691 +{
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;
5699 +       int in_buf = 0;
5700 +       int res;
5701 +
5702 +#if 0
5703 +       zlog(ZLOG_STUFF, ZLOG_DEBUG, "child %d said %s", (int) child->pid, is_stdout ? "stdout" : "stderr");
5704 +#endif
5705 +
5706 +       while (fifo_in || fifo_out) {
5707 +
5708 +               if (fifo_in) {
5709 +
5710 +                       res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf);
5711 +
5712 +                       if (res <= 0) { /* no data */
5713 +                               fifo_in = 0;
5714 +
5715 +                               if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
5716 +                                       /* just no more data ready */
5717 +                               }
5718 +                               else { /* error or pipe is closed */
5719 +
5720 +                                       if (res < 0) { /* error */
5721 +                                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
5722 +                                       }
5723 +
5724 +                                       fpm_event_del(ev);
5725 +                                       is_last_message = 1;
5726 +
5727 +                                       if (is_stdout) {
5728 +                                               close(child->fd_stdout);
5729 +                                               child->fd_stdout = -1;
5730 +                                       }
5731 +                                       else {
5732 +                                               close(child->fd_stderr);
5733 +                                               child->fd_stderr = -1;
5734 +                                       }
5735 +
5736 +#if 0
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");
5740 +                                       }
5741 +#endif
5742 +                               }
5743 +                       }
5744 +                       else {
5745 +                               in_buf += res;
5746 +                       }
5747 +               }
5748 +
5749 +               if (fifo_out) {
5750 +                       if (in_buf == 0) {
5751 +                               fifo_out = 0;
5752 +                       }
5753 +                       else {
5754 +                               char *nl;
5755 +                               int should_print = 0;
5756 +                               buf[in_buf] = '\0';
5757 +
5758 +                               /* FIXME: there might be binary data */
5759 +
5760 +                               /* we should print if no more space in the buffer */
5761 +                               if (in_buf == max_buf_size - 1) {
5762 +                                       should_print = 1;
5763 +                               }
5764 +
5765 +                               /* we should print if no more data to come */
5766 +                               if (!fifo_in) {
5767 +                                       should_print = 1;
5768 +                               }
5769 +
5770 +                               nl = strchr(buf, '\n');
5771 +
5772 +                               if (nl || should_print) {
5773 +
5774 +                                       if (nl) {
5775 +                                               *nl = '\0';
5776 +                                       }
5777 +
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" : "");
5780 +
5781 +                                       if (nl) {
5782 +                                               int out_buf = 1 + nl - buf;
5783 +                                               memmove(buf, buf + out_buf, in_buf - out_buf);
5784 +                                               in_buf -= out_buf;
5785 +                                       }
5786 +                                       else {
5787 +                                               in_buf = 0;
5788 +                                       }
5789 +                               }
5790 +                       }
5791 +               }
5792 +       }
5793 +
5794 +}
5795 +
5796 +int fpm_stdio_prepare_pipes(struct fpm_child_s *child)
5797 +{
5798 +       if (0 == child->wp->config->catch_workers_output) { /* not required */
5799 +               return 0;
5800 +       }
5801 +
5802 +       if (0 > pipe(fd_stdout)) {
5803 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
5804 +               return -1;
5805 +       }
5806 +
5807 +       if (0 > pipe(fd_stderr)) {
5808 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
5809 +               close(fd_stdout[0]); close(fd_stdout[1]);
5810 +               return -1;
5811 +       }
5812 +
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]);
5817 +               return -1;
5818 +       }
5819 +
5820 +       return 0;
5821 +}
5822 +
5823 +int fpm_stdio_parent_use_pipes(struct fpm_child_s *child)
5824 +{
5825 +       if (0 == child->wp->config->catch_workers_output) { /* not required */
5826 +               return 0;
5827 +       }
5828 +
5829 +       close(fd_stdout[1]);
5830 +       close(fd_stderr[1]);
5831 +
5832 +       child->fd_stdout = fd_stdout[0];
5833 +       child->fd_stderr = fd_stderr[0];
5834 +
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);
5837 +
5838 +       return 0;
5839 +}
5840 +
5841 +int fpm_stdio_discard_pipes(struct fpm_child_s *child)
5842 +{
5843 +       if (0 == child->wp->config->catch_workers_output) { /* not required */
5844 +               return 0;
5845 +       }
5846 +
5847 +       close(fd_stdout[1]);
5848 +       close(fd_stderr[1]);
5849 +
5850 +       close(fd_stdout[0]);
5851 +       close(fd_stderr[0]);
5852 +
5853 +       return 0;
5854 +}
5855 +
5856 +void fpm_stdio_child_use_pipes(struct fpm_child_s *child)
5857 +{
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]);
5863 +       }
5864 +       else {
5865 +               /* stdout of parent is always /dev/null */
5866 +               dup2(STDOUT_FILENO, STDERR_FILENO);
5867 +       }
5868 +}      
5869 +
5870 +int fpm_stdio_open_error_log(int reopen)
5871 +{
5872 +       int fd;
5873 +
5874 +       fd = open(fpm_global_config.error_log, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
5875 +
5876 +       if (0 > fd) {
5877 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"%s\") failed", fpm_global_config.error_log);
5878 +               return -1;
5879 +       }
5880 +
5881 +       if (reopen) {
5882 +               if (fpm_global_config.daemonize) {
5883 +                       dup2(fd, STDERR_FILENO);
5884 +               }
5885 +
5886 +               dup2(fd, fpm_globals.error_log_fd);
5887 +               close(fd);
5888 +               fd = fpm_globals.error_log_fd; /* for FD_CLOSEXEC to work */
5889 +       }
5890 +       else {
5891 +               fpm_globals.error_log_fd = fd;
5892 +       }
5893 +
5894 +       fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
5895 +
5896 +       return 0;
5897 +}
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
5901 --- /dev/null
5902 +++ b/sapi/cgi/fpm/fpm_stdio.h
5903 @@ -0,0 +1,20 @@
5904 +
5905 +       /* $Id$ */
5906 +       /* (c) 2007,2008 Andrei Nigmatulin */
5907 +
5908 +#ifndef FPM_STDIO_H
5909 +#define FPM_STDIO_H 1
5910 +
5911 +#include "fpm_worker_pool.h"
5912 +
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);
5921 +
5922 +#endif
5923 +
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
5927 --- /dev/null
5928 +++ b/sapi/cgi/fpm/fpm_str.h
5929 @@ -0,0 +1,49 @@
5930 +
5931 +       /* $Id$ */
5932 +       /* (c) 2007,2008 Andrei Nigmatulin */
5933 +
5934 +#ifndef FPM_STR_H
5935 +#define FPM_STR_H 1
5936 +
5937 +static inline char *cpystrn(char *dst, const char *src, size_t dst_size)
5938 +{
5939 +       char *d, *end;
5940 +       
5941 +       if (!dst_size) return dst;
5942 +       
5943 +       d = dst;
5944 +       end = dst + dst_size - 1;
5945 +       
5946 +       for (; d < end; ++d, ++src) {
5947 +               if (!(*d = *src)) {
5948 +                       return d;
5949 +               }
5950 +       }
5951 +
5952 +       *d = '\0';
5953 +
5954 +       return d;
5955 +}
5956 +
5957 +static inline char *str_purify_filename(char *dst, char *src, size_t size)
5958 +{
5959 +       char *d, *end;
5960 +
5961 +       d = dst;
5962 +       end = dst + size - 1;
5963 +
5964 +       for (; d < end && *src; ++d, ++src) {
5965 +               if (* (unsigned char *) src < ' ' || * (unsigned char *) src > '\x7f') {
5966 +                       *d = '.';
5967 +               }
5968 +               else {
5969 +                       *d = *src;
5970 +               }
5971 +       }
5972 +
5973 +       *d = '\0';
5974 +
5975 +       return d;
5976 +}
5977 +
5978 +#endif
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
5982 --- /dev/null
5983 +++ b/sapi/cgi/fpm/fpm_trace.c
5984 @@ -0,0 +1,46 @@
5985 +
5986 +       /* $Id$ */
5987 +       /* (c) 2007,2008 Andrei Nigmatulin */
5988 +
5989 +#include "fpm_config.h"
5990 +
5991 +#include <sys/types.h>
5992 +
5993 +#include "fpm_trace.h"
5994 +
5995 +int fpm_trace_get_strz(char *buf, size_t sz, long addr)
5996 +{
5997 +       int i;
5998 +       long l;
5999 +       char *lc = (char *) &l;
6000 +
6001 +       if (0 > fpm_trace_get_long(addr, &l)) {
6002 +               return -1;
6003 +       }
6004 +
6005 +       i = l % SIZEOF_LONG;
6006 +
6007 +       l -= i;
6008 +
6009 +       for (addr = l; ; addr += SIZEOF_LONG) {
6010 +
6011 +               if (0 > fpm_trace_get_long(addr, &l)) {
6012 +                       return -1;
6013 +               }
6014 +
6015 +               for ( ; i < SIZEOF_LONG; i++) {
6016 +                       --sz;
6017 +
6018 +                       if (sz && lc[i]) {
6019 +                               *buf++ = lc[i];
6020 +                               continue;
6021 +                       }
6022 +
6023 +                       *buf = '\0';
6024 +                       return 0;
6025 +               }
6026 +
6027 +               i = 0;
6028 +       }
6029 +}
6030 +
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
6034 --- /dev/null
6035 +++ b/sapi/cgi/fpm/fpm_trace.h
6036 @@ -0,0 +1,17 @@
6037 +
6038 +       /* $Id$ */
6039 +       /* (c) 2007,2008 Andrei Nigmatulin */
6040 +
6041 +#ifndef FPM_TRACE_H
6042 +#define FPM_TRACE_H 1
6043 +
6044 +#include <unistd.h>
6045 +
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);
6051 +
6052 +#endif
6053 +
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
6057 --- /dev/null
6058 +++ b/sapi/cgi/fpm/fpm_trace_mach.c
6059 @@ -0,0 +1,102 @@
6060 +
6061 +       /* $Id$ */
6062 +       /* (c) 2007,2008 Andrei Nigmatulin */
6063 +
6064 +#include "fpm_config.h"
6065 +
6066 +#include <mach/mach.h>
6067 +#include <mach/mach_vm.h>
6068 +
6069 +#include <unistd.h>
6070 +
6071 +#include "fpm_trace.h"
6072 +#include "fpm_process_ctl.h"
6073 +#include "fpm_unix.h"
6074 +#include "zlog.h"
6075 +
6076 +
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;
6081 +
6082 +static void fpm_mach_vm_deallocate()
6083 +{
6084 +       if (local_page) {
6085 +               mach_vm_deallocate(mach_task_self(), local_page, local_size);
6086 +               target_page_base = 0;
6087 +               local_page = 0;
6088 +               local_size = 0;
6089 +       }
6090 +}
6091 +
6092 +static int fpm_mach_vm_read_page(vm_offset_t page)
6093 +{
6094 +       kern_return_t kr;
6095 +
6096 +       kr = mach_vm_read(target, page, fpm_pagesize, &local_page, &local_size);
6097 +
6098 +       if (kr != KERN_SUCCESS) {
6099 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "mach_vm_read() failed: %s (%d)", mach_error_string(kr), kr);
6100 +               return -1;
6101 +       }
6102 +
6103 +       return 0;
6104 +}
6105 +
6106 +int fpm_trace_signal(pid_t pid)
6107 +{
6108 +       if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
6109 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "kill(SIGSTOP) failed");
6110 +               return -1;
6111 +       }
6112 +
6113 +       return 0;
6114 +}
6115 +
6116 +int fpm_trace_ready(pid_t pid)
6117 +{
6118 +       kern_return_t kr;
6119 +
6120 +       kr = task_for_pid(mach_task_self(), pid, &target);
6121 +
6122 +       if (kr != KERN_SUCCESS) {
6123 +               char *msg = "";
6124 +
6125 +               if (kr == KERN_FAILURE) {
6126 +                       msg = " It seems that master process does not have enough privileges to trace processes.";
6127 +               }
6128 +
6129 +               zlog(ZLOG_STUFF, ZLOG_ERROR, "task_for_pid() failed: %s (%d)%s", mach_error_string(kr), kr, msg);
6130 +               return -1;
6131 +       }
6132 +
6133 +       return 0;
6134 +}
6135 +
6136 +int fpm_trace_close(pid_t pid)
6137 +{
6138 +       fpm_mach_vm_deallocate();
6139 +
6140 +       target = 0;
6141 +
6142 +       return 0;
6143 +}
6144 +
6145 +int fpm_trace_get_long(long addr, long *data)
6146 +{
6147 +       size_t offset = ((uintptr_t) (addr) % fpm_pagesize);
6148 +       vm_offset_t base = (uintptr_t) (addr) - offset;
6149 +
6150 +       if (base != target_page_base) {
6151 +               fpm_mach_vm_deallocate();
6152 +               if (0 > fpm_mach_vm_read_page(base)) {
6153 +                       return -1;
6154 +               }
6155 +       }
6156 +
6157 +       *data = * (long *) (local_page + offset);
6158 +
6159 +       return 0;
6160 +}
6161 +
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
6165 --- /dev/null
6166 +++ b/sapi/cgi/fpm/fpm_trace_pread.c
6167 @@ -0,0 +1,67 @@
6168 +
6169 +       /* $Id$ */
6170 +       /* (c) 2007,2008 Andrei Nigmatulin */
6171 +
6172 +#define _GNU_SOURCE
6173 +#define _FILE_OFFSET_BITS 64
6174 +
6175 +#include "fpm_config.h"
6176 +
6177 +#include <unistd.h>
6178 +
6179 +#include <fcntl.h>
6180 +#include <stdio.h>
6181 +#include <stdint.h>
6182 +
6183 +#include "fpm_trace.h"
6184 +#include "fpm_process_ctl.h"
6185 +#include "zlog.h"
6186 +
6187 +
6188 +static int mem_file = -1;
6189 +
6190 +int fpm_trace_signal(pid_t pid)
6191 +{
6192 +       if (0 > fpm_pctl_kill(pid, FPM_PCTL_STOP)) {
6193 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "kill(SIGSTOP) failed");
6194 +               return -1;
6195 +       }
6196 +
6197 +       return 0;
6198 +}
6199 +
6200 +int fpm_trace_ready(pid_t pid)
6201 +{
6202 +       char buf[128];
6203 +
6204 +       sprintf(buf, "/proc/%d/" PROC_MEM_FILE, (int) pid);
6205 +
6206 +       mem_file = open(buf, O_RDONLY);
6207 +
6208 +       if (0 > mem_file) {
6209 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(%s) failed", buf);
6210 +               return -1;
6211 +       }
6212 +
6213 +       return 0;
6214 +}
6215 +
6216 +int fpm_trace_close(pid_t pid)
6217 +{
6218 +       close(mem_file);
6219 +
6220 +       mem_file = -1;
6221 +
6222 +       return 0;
6223 +}
6224 +
6225 +int fpm_trace_get_long(long addr, long *data)
6226 +{
6227 +       if (sizeof(*data) != pread(mem_file, (void *) data, sizeof(*data), (uintptr_t) addr)) {
6228 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pread() failed");
6229 +               return -1;
6230 +       }
6231 +
6232 +       return 0;
6233 +}
6234 +
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
6238 --- /dev/null
6239 +++ b/sapi/cgi/fpm/fpm_trace_ptrace.c
6240 @@ -0,0 +1,85 @@
6241 +
6242 +       /* $Id$ */
6243 +       /* (c) 2007,2008 Andrei Nigmatulin */
6244 +
6245 +#include "fpm_config.h"
6246 +
6247 +#include <sys/wait.h>
6248 +#include <sys/ptrace.h>
6249 +#include <unistd.h>
6250 +#include <errno.h>
6251 +
6252 +#if defined(PT_ATTACH) && !defined(PTRACE_ATTACH)
6253 +#define PTRACE_ATTACH PT_ATTACH
6254 +#endif
6255 +
6256 +#if defined(PT_DETACH) && !defined(PTRACE_DETACH)
6257 +#define PTRACE_DETACH PT_DETACH
6258 +#endif
6259 +
6260 +#if defined(PT_READ_D) && !defined(PTRACE_PEEKDATA)
6261 +#define PTRACE_PEEKDATA PT_READ_D
6262 +#endif
6263 +
6264 +#include "fpm_trace.h"
6265 +#include "zlog.h"
6266 +
6267 +static pid_t traced_pid;
6268 +
6269 +int fpm_trace_signal(pid_t pid)
6270 +{
6271 +       if (0 > ptrace(PTRACE_ATTACH, pid, 0, 0)) {
6272 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(ATTACH) failed");
6273 +               return -1;
6274 +       }
6275 +
6276 +       return 0;
6277 +}
6278 +
6279 +int fpm_trace_ready(pid_t pid)
6280 +{
6281 +       traced_pid = pid;
6282 +
6283 +       return 0;
6284 +}
6285 +
6286 +int fpm_trace_close(pid_t pid)
6287 +{
6288 +       if (0 > ptrace(PTRACE_DETACH, pid, (void *) 1, 0)) {
6289 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(DETACH) failed");
6290 +               return -1;
6291 +       }
6292 +
6293 +       traced_pid = 0;
6294 +
6295 +       return 0;
6296 +}
6297 +
6298 +int fpm_trace_get_long(long addr, long *data)
6299 +{
6300 +#ifdef PT_IO
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)
6306 +       };
6307 +
6308 +       if (0 > ptrace(PT_IO, traced_pid, (void *) &ptio, 0)) {
6309 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(PT_IO) failed");
6310 +               return -1;
6311 +       }
6312 +#else
6313 +       errno = 0;
6314 +
6315 +       *data = ptrace(PTRACE_PEEKDATA, traced_pid, (void *) addr, 0);
6316 +
6317 +       if (errno) {
6318 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "ptrace(PEEKDATA) failed");
6319 +               return -1;
6320 +       }
6321 +#endif
6322 +
6323 +       return 0;
6324 +}
6325 +
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
6329 --- /dev/null
6330 +++ b/sapi/cgi/fpm/fpm_unix.c
6331 @@ -0,0 +1,289 @@
6332 +
6333 +       /* $Id$ */
6334 +       /* (c) 2007,2008 Andrei Nigmatulin */
6335 +
6336 +#include "fpm_config.h"
6337 +
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>
6344 +#include <pwd.h>
6345 +#include <grp.h>
6346 +
6347 +#ifdef HAVE_PRCTL
6348 +#include <sys/prctl.h>
6349 +#endif
6350 +
6351 +#include "fpm.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"
6357 +#include "zlog.h"
6358 +
6359 +size_t fpm_pagesize;
6360 +
6361 +int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp)
6362 +{
6363 +       struct fpm_listen_options_s *lo = wp->config->listen_options;
6364 +
6365 +       /* uninitialized */
6366 +       wp->socket_uid = -1;
6367 +       wp->socket_gid = -1;
6368 +       wp->socket_mode = 0666;
6369 +
6370 +       if (!lo) return 0;
6371 +
6372 +       if (lo->owner && *lo->owner) {
6373 +               struct passwd *pwd;
6374 +
6375 +               pwd = getpwnam(lo->owner);
6376 +
6377 +               if (!pwd) {
6378 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "cannot get uid for user '%s', pool '%s'", lo->owner, wp->config->name);
6379 +                       return -1;
6380 +               }
6381 +
6382 +               wp->socket_uid = pwd->pw_uid;
6383 +               wp->socket_gid = pwd->pw_gid;
6384 +       }
6385 +
6386 +       if (lo->group && *lo->group) {
6387 +               struct group *grp;
6388 +
6389 +               grp = getgrnam(lo->group);
6390 +
6391 +               if (!grp) {
6392 +                       zlog(ZLOG_STUFF, ZLOG_SYSERROR, "cannot get gid for group '%s', pool '%s'", lo->group, wp->config->name);
6393 +                       return -1;
6394 +               }
6395 +
6396 +               wp->socket_gid = grp->gr_gid;
6397 +       }
6398 +
6399 +       if (lo->mode && *lo->mode) {
6400 +               wp->socket_mode = strtoul(lo->mode, 0, 8);
6401 +       }
6402 +
6403 +       return 0;
6404 +}
6405 +
6406 +static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp)
6407 +{
6408 +       int is_root = !geteuid();
6409 +
6410 +       if (is_root) {
6411 +               if (wp->config->user && *wp->config->user) {
6412 +
6413 +                       if (strlen(wp->config->user) == strspn(wp->config->user, "0123456789")) {
6414 +                               wp->set_uid = strtoul(wp->config->user, 0, 10);
6415 +                       }
6416 +                       else {
6417 +                               struct passwd *pwd;
6418 +
6419 +                               pwd = getpwnam(wp->config->user);
6420 +
6421 +                               if (!pwd) {
6422 +                                       zlog(ZLOG_STUFF, ZLOG_ERROR, "cannot get uid for user '%s', pool '%s'", wp->config->user, wp->config->name);
6423 +                                       return -1;
6424 +                               }
6425 +
6426 +                               wp->set_uid = pwd->pw_uid;
6427 +                               wp->set_gid = pwd->pw_gid;
6428 +
6429 +                               wp->user = strdup(pwd->pw_name);
6430 +                               wp->home = strdup(pwd->pw_dir);
6431 +                       }
6432 +               }
6433 +
6434 +               if (wp->config->group && *wp->config->group) {
6435 +
6436 +                       if (strlen(wp->config->group) == strspn(wp->config->group, "0123456789")) {
6437 +                               wp->set_gid = strtoul(wp->config->group, 0, 10);
6438 +                       }
6439 +                       else {
6440 +                               struct group *grp;
6441 +
6442 +                               grp = getgrnam(wp->config->group);
6443 +
6444 +                               if (!grp) {
6445 +                                       zlog(ZLOG_STUFF, ZLOG_ERROR, "cannot get gid for group '%s', pool '%s'", wp->config->group, wp->config->name);
6446 +                                       return -1;
6447 +                               }
6448 +
6449 +                               wp->set_gid = grp->gr_gid;
6450 +                       }
6451 +               }
6452 +
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);
6456 +                       return -1;
6457 +               }
6458 +#endif
6459 +       }
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);
6463 +               }
6464 +               if (wp->config->group && *wp->config->group) {
6465 +                       zlog(ZLOG_STUFF, ZLOG_WARNING, "'group' directive is ignored, pool '%s'", wp->config->name);
6466 +               }
6467 +               if (wp->config->chroot && *wp->config->chroot) {
6468 +                       zlog(ZLOG_STUFF, ZLOG_WARNING, "'chroot' directive is ignored, pool '%s'", wp->config->name);
6469 +               }
6470 +
6471 +               { /* set up HOME and USER anyway */
6472 +                       struct passwd *pwd;
6473 +
6474 +                       pwd = getpwuid(getuid());
6475 +
6476 +                       if (pwd) {
6477 +                               wp->user = strdup(pwd->pw_name);
6478 +                               wp->home = strdup(pwd->pw_dir);
6479 +                       }
6480 +               }
6481 +       }
6482 +
6483 +       return 0;
6484 +}
6485 +
6486 +int fpm_unix_init_child(struct fpm_worker_pool_s *wp)
6487 +{
6488 +       int is_root = !geteuid();
6489 +       int made_chroot = 0;
6490 +
6491 +       if (wp->config->rlimit_files) {
6492 +               struct rlimit r;
6493 +
6494 +               getrlimit(RLIMIT_NOFILE, &r);
6495 +
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");
6500 +               }
6501 +       }
6502 +
6503 +       if (wp->config->rlimit_core) {
6504 +               struct rlimit r;
6505 +
6506 +               getrlimit(RLIMIT_CORE, &r);
6507 +
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");
6512 +               }
6513 +       }
6514 +
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);
6518 +                       return -1;
6519 +               }
6520 +               made_chroot = 1;
6521 +       }
6522 +
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);
6526 +                       return -1;
6527 +               }
6528 +       }
6529 +       else if (made_chroot) {
6530 +               chdir("/");
6531 +       }
6532 +
6533 +       if (is_root) {
6534 +               if (wp->set_gid) {
6535 +                       if (0 > setgid(wp->set_gid)) {
6536 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setgid(%d) failed", wp->set_gid);
6537 +                               return -1;
6538 +                       }
6539 +               }
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);
6543 +                               return -1;
6544 +                       }
6545 +                       if (0 > setuid(wp->set_uid)) {
6546 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "setuid(%d) failed", wp->set_uid);
6547 +                               return -1;
6548 +                       }
6549 +               }
6550 +       }
6551 +
6552 +#ifdef HAVE_PRCTL
6553 +       if (0 > prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)) {
6554 +               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "prctl(PR_SET_DUMPABLE) failed");
6555 +       }
6556 +#endif
6557 +
6558 +       if (0 > fpm_clock_init()) {
6559 +               return -1;
6560 +       }
6561 +
6562 +       return 0;
6563 +}
6564 +
6565 +int fpm_unix_init_main()
6566 +{
6567 +       struct fpm_worker_pool_s *wp;
6568 +
6569 +       fpm_pagesize = getpagesize();
6570 +
6571 +       if (fpm_global_config.daemonize) {
6572 +
6573 +               switch (fork()) {
6574 +
6575 +                       case -1 :
6576 +
6577 +                               zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fork() failed");
6578 +                               return -1;
6579 +
6580 +                       case 0 :
6581 +
6582 +                               break;
6583 +
6584 +                       default :
6585 +
6586 +                               fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT);
6587 +                               exit(0);
6588 +
6589 +               }
6590 +
6591 +       }
6592 +
6593 +       setsid();
6594 +
6595 +       if (0 > fpm_clock_init()) {
6596 +               return -1;
6597 +       }
6598 +
6599 +       fpm_globals.parent_pid = getpid();
6600 +
6601 +       for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
6602 +
6603 +               if (0 > fpm_unix_conf_wp(wp)) {
6604 +                       return -1;
6605 +               }
6606 +
6607 +       }
6608 +
6609 +       fpm_stdio_init_final();
6610 +
6611 +       {
6612 +               struct rlimit r;
6613 +               getrlimit(RLIMIT_NOFILE, &r);
6614 +
6615 +               zlog(ZLOG_STUFF, ZLOG_NOTICE, "getrlimit(nofile): max:%lld, cur:%lld",
6616 +                       (long long) r.rlim_max, (long long) r.rlim_cur);
6617 +       }
6618 +
6619 +       return 0;
6620 +}
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
6624 --- /dev/null
6625 +++ b/sapi/cgi/fpm/fpm_unix.h
6626 @@ -0,0 +1,17 @@
6627 +
6628 +       /* $Id$ */
6629 +       /* (c) 2007,2008 Andrei Nigmatulin */
6630 +
6631 +#ifndef FPM_UNIX_H
6632 +#define FPM_UNIX_H 1
6633 +
6634 +#include "fpm_worker_pool.h"
6635 +
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();
6639 +
6640 +extern size_t fpm_pagesize;
6641 +
6642 +#endif
6643 +
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
6647 --- /dev/null
6648 +++ b/sapi/cgi/fpm/fpm_worker_pool.c
6649 @@ -0,0 +1,69 @@
6650 +
6651 +       /* $Id$ */
6652 +       /* (c) 2007,2008 Andrei Nigmatulin */
6653 +
6654 +#include "fpm_config.h"
6655 +
6656 +#include <string.h>
6657 +#include <stdlib.h>
6658 +#include <unistd.h>
6659 +
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"
6666 +
6667 +struct fpm_worker_pool_s *fpm_worker_all_pools;
6668 +
6669 +static void fpm_worker_pool_cleanup(int which, void *arg)
6670 +{
6671 +       struct fpm_worker_pool_s *wp, *wp_next;
6672 +
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);
6680 +               free(wp->config);
6681 +               free(wp->user);
6682 +               free(wp->home);
6683 +               free(wp);
6684 +       }
6685 +
6686 +       fpm_worker_all_pools = 0;
6687 +}
6688 +
6689 +struct fpm_worker_pool_s *fpm_worker_pool_alloc()
6690 +{
6691 +       struct fpm_worker_pool_s *ret;
6692 +
6693 +       ret = malloc(sizeof(struct fpm_worker_pool_s));
6694 +
6695 +       if (!ret) {
6696 +               return 0;
6697 +       }
6698 +
6699 +       memset(ret, 0, sizeof(struct fpm_worker_pool_s));
6700 +
6701 +       if (!fpm_worker_all_pools) {
6702 +               fpm_worker_all_pools = ret;
6703 +       }
6704 +
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);
6707 +
6708 +       return ret;
6709 +}
6710 +
6711 +int fpm_worker_pool_init_main()
6712 +{
6713 +       if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_worker_pool_cleanup, 0)) {
6714 +               return -1;
6715 +       }
6716 +
6717 +       return 0;
6718 +}
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
6722 --- /dev/null
6723 +++ b/sapi/cgi/fpm/fpm_worker_pool.h
6724 @@ -0,0 +1,46 @@
6725 +
6726 +       /* $Id$ */
6727 +       /* (c) 2007,2008 Andrei Nigmatulin */
6728 +
6729 +#ifndef FPM_WORKER_POOL_H
6730 +#define FPM_WORKER_POOL_H 1
6731 +
6732 +#include "fpm_conf.h"
6733 +#include "fpm_arrays.h"
6734 +
6735 +struct fpm_worker_pool_s;
6736 +struct fpm_child_s;
6737 +struct fpm_child_stat_s;
6738 +struct fpm_shm_s;
6739 +
6740 +enum fpm_address_domain {
6741 +       FPM_AF_UNIX = 1,
6742 +       FPM_AF_INET = 2
6743 +};
6744 +
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;
6754 +
6755 +       struct fpm_shm_s *shm_list;
6756 +       struct fpm_array_s slots_used;
6757 +       struct fpm_array_s slots_free;
6758 +
6759 +       /* runtime */
6760 +       struct fpm_child_s *children;
6761 +       int running_children;
6762 +};
6763 +
6764 +struct fpm_worker_pool_s *fpm_worker_pool_alloc();
6765 +int fpm_worker_pool_init_main();
6766 +
6767 +extern struct fpm_worker_pool_s *fpm_worker_all_pools;
6768 +
6769 +#endif
6770 +
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
6774 --- /dev/null
6775 +++ b/sapi/cgi/fpm/init.d/php-fpm.in
6776 @@ -0,0 +1,139 @@
6777 +#! /bin/sh
6778 +
6779 +php_fpm_BIN=@prefix@/bin/php-cgi
6780 +php_fpm_CONF=@php_fpm_conf_path@
6781 +php_fpm_PID=@php_fpm_pid_path@
6782 +
6783 +
6784 +php_opts="--fpm-config $php_fpm_CONF"
6785 +
6786 +
6787 +wait_for_pid () {
6788 +       try=0
6789 +
6790 +       while test $try -lt 35 ; do
6791 +
6792 +               case "$1" in
6793 +                       'created')
6794 +                       if [ -f "$2" ] ; then
6795 +                               try=''
6796 +                               break
6797 +                       fi
6798 +                       ;;
6799 +
6800 +                       'removed')
6801 +                       if [ ! -f "$2" ] ; then
6802 +                               try=''
6803 +                               break
6804 +                       fi
6805 +                       ;;
6806 +               esac
6807 +
6808 +               echo -n .
6809 +               try=`expr $try + 1`
6810 +               sleep 1
6811 +
6812 +       done
6813 +
6814 +}
6815 +
6816 +case "$1" in
6817 +       start)
6818 +               echo -n "Starting php_fpm "
6819 +
6820 +               $php_fpm_BIN --fpm $php_opts
6821 +
6822 +               if [ "$?" != 0 ] ; then
6823 +                       echo " failed"
6824 +                       exit 1
6825 +               fi
6826 +
6827 +               wait_for_pid created $php_fpm_PID
6828 +
6829 +               if [ -n "$try" ] ; then
6830 +                       echo " failed"
6831 +                       exit 1
6832 +               else
6833 +                       echo " done"
6834 +               fi
6835 +       ;;
6836 +
6837 +       stop)
6838 +               echo -n "Shutting down php_fpm "
6839 +
6840 +               if [ ! -r $php_fpm_PID ] ; then
6841 +                       echo "warning, no pid file found - php-fpm is not running ?"
6842 +                       exit 1
6843 +               fi
6844 +
6845 +               kill -TERM `cat $php_fpm_PID`
6846 +
6847 +               wait_for_pid removed $php_fpm_PID
6848 +
6849 +               if [ -n "$try" ] ; then
6850 +                       echo " failed"
6851 +                       exit 1
6852 +               else
6853 +                       echo " done"
6854 +               fi
6855 +       ;;
6856 +
6857 +       quit)
6858 +               echo -n "Gracefully shutting down php_fpm "
6859 +
6860 +               if [ ! -r $php_fpm_PID ] ; then
6861 +                       echo "warning, no pid file found - php-fpm is not running ?"
6862 +                       exit 1
6863 +               fi
6864 +
6865 +               kill -QUIT `cat $php_fpm_PID`
6866 +
6867 +               wait_for_pid removed $php_fpm_PID
6868 +
6869 +               if [ -n "$try" ] ; then
6870 +                       echo " failed"
6871 +                       exit 1
6872 +               else
6873 +                       echo " done"
6874 +               fi
6875 +       ;;
6876 +
6877 +       restart)
6878 +               $0 stop
6879 +               $0 start
6880 +       ;;
6881 +
6882 +       reload)
6883 +
6884 +               echo -n "Reload service php-fpm "
6885 +
6886 +               if [ ! -r $php_fpm_PID ] ; then
6887 +                       echo "warning, no pid file found - php-fpm is not running ?"
6888 +                       exit 1
6889 +               fi
6890 +
6891 +               kill -USR2 `cat $php_fpm_PID`
6892 +
6893 +               echo " done"
6894 +       ;;
6895 +
6896 +       logrotate)
6897 +
6898 +               echo -n "Re-opening php-fpm log file "
6899 +
6900 +               if [ ! -r $php_fpm_PID ] ; then
6901 +                       echo "warning, no pid file found - php-fpm is not running ?"
6902 +                       exit 1
6903 +               fi
6904 +
6905 +               kill -USR1 `cat $php_fpm_PID`
6906 +
6907 +               echo " done"
6908 +       ;;
6909 +
6910 +       *)
6911 +               echo "Usage: $0 {start|stop|quit|restart|reload|logrotate}"
6912 +               exit 1
6913 +       ;;
6914 +
6915 +esac
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
6919 --- /dev/null
6920 +++ b/sapi/cgi/fpm/xml_config.c
6921 @@ -0,0 +1,278 @@
6922 +
6923 +       /* $Id$ */
6924 +       /* (c) 2004-2007 Andrei Nigmatulin */
6925 +
6926 +#include "fpm_config.h"
6927 +
6928 +#ifdef HAVE_ALLOCA_H
6929 +#include <alloca.h>
6930 +#endif
6931 +#include <string.h>
6932 +#include <stdio.h>
6933 +#include <stddef.h>
6934 +#include <stdlib.h>
6935 +
6936 +#include <libxml/parser.h>
6937 +#include <libxml/tree.h>
6938 +
6939 +#include "xml_config.h"
6940 +
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;
6944 +
6945 +char *xml_conf_set_slot_boolean(void **conf, char *name, void *vv, intptr_t offset)
6946 +{
6947 +       char *value = vv;
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");
6950 +
6951 +       if (!value_y && !value_n) {
6952 +               return "xml_conf_set_slot(): invalid boolean value";
6953 +       }
6954 +
6955 +#ifdef XML_CONF_DEBUG
6956 +       fprintf(stderr, "setting boolean '%s' => %s\n", name, value_y ? "TRUE" : "FALSE");
6957 +#endif
6958 +
6959 +       * (int *) ((char *) *conf + offset) = value_y ? 1 : 0;
6960 +
6961 +       return NULL;
6962 +}
6963 +
6964 +char *xml_conf_set_slot_string(void **conf, char *name, void *vv, intptr_t offset)
6965 +{
6966 +       char *value = vv;
6967 +       char *v = strdup(value);
6968 +
6969 +       if (!v) return "xml_conf_set_slot_string(): strdup() failed";
6970 +
6971 +#ifdef XML_CONF_DEBUG
6972 +       fprintf(stderr, "setting string '%s' => '%s'\n", name, v);
6973 +#endif
6974 +
6975 +       * (char **) ((char *) *conf + offset) = v;
6976 +
6977 +       return NULL;
6978 +}
6979 +
6980 +char *xml_conf_set_slot_integer(void **conf, char *name, void *vv, intptr_t offset)
6981 +{
6982 +       char *value = vv;
6983 +       int v = atoi(value);
6984 +
6985 +       * (int *) ((char *) *conf + offset) = v;
6986 +
6987 +#ifdef XML_CONF_DEBUG
6988 +       fprintf(stderr, "setting integer '%s' => %d\n", name, v);
6989 +#endif
6990 +
6991 +       return NULL;
6992 +}
6993 +
6994 +char *xml_conf_set_slot_time(void **conf, char *name, void *vv, intptr_t offset)
6995 +{
6996 +       char *value = vv;
6997 +       int len = strlen(value);
6998 +       char suffix;
6999 +       int seconds;
7000 +
7001 +       if (!len) return "xml_conf_set_slot_timeval(): invalid timeval value";
7002 +
7003 +       suffix = value[len-1];
7004 +
7005 +       value[len-1] = '\0';
7006 +
7007 +       switch (suffix) {
7008 +               case 's' :
7009 +                       seconds = atoi(value);
7010 +                       break;
7011 +               case 'm' :
7012 +                       seconds = 60 * atoi(value);
7013 +                       break;
7014 +               case 'h' :
7015 +                       seconds = 60 * 60 * atoi(value);
7016 +                       break;
7017 +               case 'd' :
7018 +                       seconds = 24 * 60 * 60 * atoi(value);
7019 +                       break;
7020 +               default :
7021 +                       return "xml_conf_set_slot_timeval(): unknown suffix used in timeval value";
7022 +       }
7023 +
7024 +       * (int *) ((char *) *conf + offset) = seconds;
7025 +
7026 +#ifdef XML_CONF_DEBUG
7027 +       fprintf(stderr, "setting time '%s' => %d:%02d:%02d:%02d\n", name, expand_dhms(seconds));
7028 +#endif
7029 +
7030 +       return NULL;
7031 +}
7032 +
7033 +char *xml_conf_parse_section(void **conf, struct xml_conf_section *section, void *xml_node)
7034 +{
7035 +       xmlNode *element = xml_node;
7036 +       char *ret = 0;
7037 +
7038 +#ifdef XML_CONF_DEBUG
7039 +       fprintf(stderr, "processing a section %s\n", section->path);
7040 +#endif
7041 +
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");
7045 +
7046 +                       if (name) {
7047 +                               int i;
7048 +
7049 +#ifdef XML_CONF_DEBUG
7050 +                               fprintf(stderr, "found a value: %s\n", name);
7051 +#endif
7052 +                               for (i = 0; section->parsers[i].parser; i++) {
7053 +                                       if (!section->parsers[i].name || !strcmp(section->parsers[i].name, (char *) name)) {
7054 +                                               break;
7055 +                                       }
7056 +                               }
7057 +
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);
7062 +                                               }
7063 +                                               else {
7064 +                                                       ret = "XML_TEXT_NODE is expected, something different is given";
7065 +                                               }
7066 +                                       }
7067 +                                       else {
7068 +                                               ret = section->parsers[i].parser(conf, (char *) name, element->children, section->parsers[i].offset);
7069 +                                       }
7070 +
7071 +                                       xmlFree(name);
7072 +                                       if (ret) return ret;
7073 +                                       else continue;
7074 +                               }
7075 +
7076 +                               fprintf(stderr, "Warning, unknown setting '%s' in section '%s'\n", (char *) name, section->path);
7077 +
7078 +                               xmlFree(name);
7079 +                       }
7080 +               }
7081 +       }
7082 +
7083 +       return NULL;
7084 +}
7085 +
7086 +static char *xml_conf_parse_file(xmlNode *element)
7087 +{
7088 +       char *ret = 0;
7089 +
7090 +       for ( ; element; element = element->next) {
7091 +
7092 +               if (element->parent && element->type == XML_ELEMENT_NODE && !strcmp((const char *) element->name, "section")) {
7093 +                       xmlChar *name = xmlGetProp(element, (unsigned char *) "name");
7094 +
7095 +                       if (name) {
7096 +                               char *parent_name = (char *) xmlGetNodePath(element->parent);
7097 +                               char *full_name;
7098 +                               int i;
7099 +                               struct xml_conf_section *section = NULL;
7100 +
7101 +#ifdef XML_CONF_DEBUG
7102 +                               fprintf(stderr, "got a section: %s/%s\n", parent_name, name);
7103 +#endif
7104 +                               full_name = alloca(strlen(parent_name) + strlen((char *) name) + 1 + 1);
7105 +
7106 +                               sprintf(full_name, "%s/%s", parent_name, (char *) name);
7107 +
7108 +                               xmlFree(parent_name);
7109 +                               xmlFree(name);
7110 +
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];
7114 +                                       }
7115 +                               }
7116 +
7117 +                               if (section) { /* found a registered section */
7118 +                                       void *conf = section->conf();
7119 +                                       ret = xml_conf_parse_section(&conf, section, element->children);
7120 +                                       if (ret) break;
7121 +                               }
7122 +
7123 +                       }
7124 +               }
7125 +
7126 +               if (element->children) {
7127 +                       ret = xml_conf_parse_file(element->children);
7128 +                       if (ret) break;
7129 +               }
7130 +       }
7131 +
7132 +       return ret;
7133 +}
7134 +
7135 +char *xml_conf_load_file(char *file)
7136 +{
7137 +       char *ret = 0;
7138 +       xmlDoc *doc;
7139 +
7140 +       LIBXML_TEST_VERSION
7141 +
7142 +       doc = xmlParseFile(file);
7143 +
7144 +       if (doc) {
7145 +               ret = xml_conf_parse_file(doc->children);
7146 +               xmlFreeDoc(doc);
7147 +       }
7148 +       else {
7149 +               ret = "failed to parse conf file";
7150 +       }
7151 +
7152 +       xmlCleanupParser();
7153 +       return ret;
7154 +}
7155 +
7156 +int xml_conf_init()
7157 +{
7158 +       return 0;
7159 +}
7160 +
7161 +void xml_conf_clean()
7162 +{
7163 +       if (xml_conf_sections) {
7164 +               free(xml_conf_sections);
7165 +       }
7166 +}
7167 +
7168 +int xml_conf_section_register(struct xml_conf_section *section)
7169 +{
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);
7173 +
7174 +               if (new_ptr) {
7175 +                       xml_conf_sections = new_ptr;
7176 +                       xml_conf_sections_allocated = new_size;
7177 +               }
7178 +               else {
7179 +                       fprintf(stderr, "xml_conf_section_register(): out of memory\n");
7180 +                       return -1;
7181 +               }
7182 +       }
7183 +
7184 +       xml_conf_sections[xml_conf_sections_used++] = section;
7185 +
7186 +       return 0;
7187 +}
7188 +
7189 +int xml_conf_sections_register(struct xml_conf_section *sections[])
7190 +{
7191 +       for ( ; sections && *sections; sections++) {
7192 +               if (0 > xml_conf_section_register(*sections)) {
7193 +                       return -1;
7194 +               }
7195 +       }
7196 +
7197 +       return 0;
7198 +}
7199 +
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
7203 --- /dev/null
7204 +++ b/sapi/cgi/fpm/xml_config.h
7205 @@ -0,0 +1,43 @@
7206 +
7207 +       /* $Id$ */
7208 +       /* (c) 2004-2007 Andrei Nigmatulin */
7209 +
7210 +#ifndef XML_CONFIG_H
7211 +#define XML_CONFIG_H 1
7212 +
7213 +#include <stdint.h>
7214 +
7215 +struct xml_value_parser;
7216 +
7217 +struct xml_value_parser {
7218 +       int type;
7219 +       char *name;
7220 +       char *(*parser)(void **, char *, void *, intptr_t offset);
7221 +       intptr_t offset;
7222 +};
7223 +
7224 +struct xml_conf_section {
7225 +       void *(*conf)();
7226 +       char *path;
7227 +       struct xml_value_parser *parsers;
7228 +};
7229 +
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);
7234 +
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[]);
7241 +
7242 +#define expand_hms(value) (value) / 3600, ((value) % 3600) / 60, (value) % 60
7243 +
7244 +#define expand_dhms(value) (value) / 86400, ((value) % 86400) / 3600, ((value) % 3600) / 60, (value) % 60
7245 +
7246 +enum { XML_CONF_SCALAR = 1, XML_CONF_SUBSECTION = 2 };
7247 +
7248 +#endif
7249 diff --git a/sapi/cgi/fpm/zlog.c b/sapi/cgi/fpm/zlog.c
7250 new file mode 100644
7251 index 0000000..2fb6c45
7252 --- /dev/null
7253 +++ b/sapi/cgi/fpm/zlog.c
7254 @@ -0,0 +1,113 @@
7255 +
7256 +       /* $Id$ */
7257 +       /* (c) 2004-2007 Andrei Nigmatulin */
7258 +
7259 +#include "fpm_config.h"
7260 +
7261 +#include <stdio.h>
7262 +#include <unistd.h>
7263 +#include <time.h>
7264 +#include <string.h>
7265 +#include <stdarg.h>
7266 +#include <sys/time.h>
7267 +#include <errno.h>
7268 +
7269 +#include "zlog.h"
7270 +
7271 +#define MAX_LINE_LENGTH 1024
7272 +
7273 +static int zlog_fd = -1;
7274 +static int zlog_level = ZLOG_NOTICE;
7275 +
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",
7282 +};
7283 +
7284 +size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len)
7285 +{
7286 +       struct tm t;
7287 +       size_t len;
7288 +
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);
7291 +
7292 +       return len;
7293 +}
7294 +
7295 +int zlog_set_fd(int new_fd)
7296 +{
7297 +       int old_fd = zlog_fd;
7298 +       zlog_fd = new_fd;
7299 +
7300 +       return old_fd;
7301 +}
7302 +
7303 +int zlog_set_level(int new_value)
7304 +{
7305 +       int old_value = zlog_level;
7306 +
7307 +       zlog_level = new_value;
7308 +
7309 +       return old_value;
7310 +}
7311 +
7312 +void zlog(const char *function, int line, int flags, const char *fmt, ...)
7313 +{
7314 +       struct timeval tv;
7315 +       char buf[MAX_LINE_LENGTH];
7316 +       const size_t buf_size = MAX_LINE_LENGTH;
7317 +       va_list args;
7318 +       size_t len;
7319 +       int truncated = 0;
7320 +       int saved_errno;
7321 +
7322 +       if ((flags & ZLOG_LEVEL_MASK) < zlog_level) {
7323 +               return;
7324 +       }
7325 +
7326 +       saved_errno = errno;
7327 +
7328 +       gettimeofday(&tv, 0);
7329 +
7330 +       len = zlog_print_time(&tv, buf, buf_size);
7331 +
7332 +       len += snprintf(buf + len, buf_size - len, " [%s] %s(), line %d: ", level_names[flags & ZLOG_LEVEL_MASK], function, line);
7333 +
7334 +       if (len > buf_size - 1) {
7335 +               truncated = 1;
7336 +       }
7337 +
7338 +       if (!truncated) {
7339 +               va_start(args, fmt);
7340 +
7341 +               len += vsnprintf(buf + len, buf_size - len, fmt, args);
7342 +
7343 +               va_end(args);
7344 +
7345 +               if (len >= buf_size) {
7346 +                       truncated = 1;
7347 +               }
7348 +       }
7349 +
7350 +       if (!truncated) {
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) {
7354 +                               truncated = 1;
7355 +                       }
7356 +               }
7357 +       }
7358 +
7359 +       if (truncated) {
7360 +               memcpy(buf + buf_size - sizeof("..."), "...", sizeof("...") - 1);
7361 +               len = buf_size - 1;
7362 +       }
7363 +
7364 +       buf[len++] = '\n';
7365 +
7366 +       write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, buf, len);
7367 +}
7368 diff --git a/sapi/cgi/fpm/zlog.h b/sapi/cgi/fpm/zlog.h
7369 new file mode 100644
7370 index 0000000..b5ac3d9
7371 --- /dev/null
7372 +++ b/sapi/cgi/fpm/zlog.h
7373 @@ -0,0 +1,34 @@
7374 +
7375 +       /* $Id$ */
7376 +       /* (c) 2004-2007 Andrei Nigmatulin */
7377 +
7378 +#ifndef ZLOG_H
7379 +#define ZLOG_H 1
7380 +
7381 +#define ZLOG_STUFF             __func__, __LINE__
7382 +
7383 +struct timeval;
7384 +
7385 +int zlog_set_fd(int new_fd);
7386 +int zlog_set_level(int new_value);
7387 +
7388 +size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len);
7389 +
7390 +void zlog(const char *function, int line, int flags, const char *fmt, ...)
7391 +               __attribute__ ((format(printf,4,5)));
7392 +
7393 +enum {
7394 +       ZLOG_DEBUG                      = 1,
7395 +       ZLOG_NOTICE                     = 2,
7396 +       ZLOG_WARNING            = 3,
7397 +       ZLOG_ERROR                      = 4,
7398 +       ZLOG_ALERT                      = 5,
7399 +};
7400 +
7401 +#define ZLOG_LEVEL_MASK 7
7402 +
7403 +#define ZLOG_HAVE_ERRNO 0x100
7404 +
7405 +#define ZLOG_SYSERROR (ZLOG_ERROR | ZLOG_HAVE_ERRNO)
7406 +
7407 +#endif
This page took 0.978237 seconds and 2 git commands to generate.