From b911467d1d8375b86d355906f4b4021813ebca6d Mon Sep 17 00:00:00 2001 From: Rasmus Lerdorf Date: Wed, 19 Mar 2008 16:37:49 +0000 Subject: [PATCH] MFB Here are the signal changes from the 5.3 branch that optimizes signal handler registration and switches from longjmp to siglongjmp in order to make signal mask handling consistent across different UNIX operating systems. --- Zend/Zend.m4 | 2 +- Zend/zend.c | 2 +- Zend/zend.h | 16 ++++++++++--- Zend/zend_execute.h | 2 +- Zend/zend_execute_API.c | 36 ++++++++++++++++++------------ Zend/zend_globals.h | 2 +- main/SAPI.c | 6 +++++ main/SAPI.h | 2 ++ main/main.c | 14 +++++++----- main/php_globals.h | 1 + sapi/aolserver/aolserver.c | 1 + sapi/apache/mod_php.c | 9 ++++++++ sapi/apache2filter/sapi_apache2.c | 1 + sapi/apache2handler/sapi_apache2.c | 1 + sapi/apache_hooks/mod_php5.c | 1 + sapi/caudium/caudium.c | 1 + sapi/cgi/cgi_main.c | 1 + sapi/cli/php_cli.c | 1 + sapi/continuity/capi.c | 3 ++- sapi/embed/php_embed.c | 1 + sapi/isapi/php5isapi.c | 1 + sapi/milter/php_milter.c | 1 + sapi/nsapi/nsapi.c | 1 + sapi/phttpd/phttpd.c | 1 + sapi/pi3web/pi3web_sapi.c | 1 + sapi/roxen/roxen.c | 1 + sapi/thttpd/thttpd.c | 1 + sapi/tux/php_tux.c | 1 + sapi/webjames/webjames.c | 1 + win32/build/config.w32.h.in | 1 + 30 files changed, 85 insertions(+), 28 deletions(-) diff --git a/Zend/Zend.m4 b/Zend/Zend.m4 index 9c95754f33..fe0110e341 100644 --- a/Zend/Zend.m4 +++ b/Zend/Zend.m4 @@ -110,7 +110,7 @@ dnl Checks for library functions. AC_FUNC_VPRINTF AC_FUNC_MEMCMP AC_FUNC_ALLOCA -AC_CHECK_FUNCS(memcpy strdup getpid kill strtod strtol finite fpclass) +AC_CHECK_FUNCS(memcpy strdup getpid kill strtod strtol finite fpclass sigsetjmp) AC_ZEND_BROKEN_SPRINTF AC_CHECK_FUNCS(finite isfinite isinf isnan) diff --git a/Zend/zend.c b/Zend/zend.c index 2c5c9a78c6..19007cf778 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -1273,7 +1273,7 @@ ZEND_API void _zend_bailout(char *filename, uint lineno) /* {{{ */ CG(unclean_shutdown) = 1; CG(in_compilation) = EG(in_execution) = 0; EG(current_execute_data) = NULL; - longjmp(*EG(bailout), FAILURE); + LONGJMP(*EG(bailout), FAILURE); } /* }}} */ END_EXTERN_C() diff --git a/Zend/zend.h b/Zend/zend.h index ea79ea9966..7c9eb2e310 100644 --- a/Zend/zend.h +++ b/Zend/zend.h @@ -566,13 +566,23 @@ END_EXTERN_C() #define zend_bailout() _zend_bailout(__FILE__, __LINE__) +#ifdef HAVE_SIGSETJMP +# define SETJMP(a) sigsetjmp(a, 0) +# define LONGJMP(a,b) siglongjmp(a, b) +# define JMP_BUF sigjmp_buf +#else +# define SETJMP(a) setjmp(a) +# define LONGJMP(a,b) longjmp(a, b) +# define JMP_BUF jmp_buf +#endif + #define zend_try \ { \ - jmp_buf *__orig_bailout = EG(bailout); \ - jmp_buf __bailout; \ + JMP_BUF *__orig_bailout = EG(bailout); \ + JMP_BUF __bailout; \ \ EG(bailout) = &__bailout; \ - if (setjmp(__bailout)==0) { + if (SETJMP(__bailout)==0) { #define zend_catch \ } else { \ EG(bailout) = __orig_bailout; diff --git a/Zend/zend_execute.h b/Zend/zend_execute.h index ad92cb2e13..18bc0b6e9f 100644 --- a/Zend/zend_execute.h +++ b/Zend/zend_execute.h @@ -317,7 +317,7 @@ ZEND_API char *zend_get_executed_filename(TSRMLS_D); ZEND_API uint zend_get_executed_lineno(TSRMLS_D); ZEND_API zend_bool zend_is_executing(TSRMLS_D); -ZEND_API void zend_set_timeout(long seconds); +ZEND_API void zend_set_timeout(long seconds, int reset_signals); ZEND_API void zend_unset_timeout(TSRMLS_D); ZEND_API void zend_timeout(int dummy); ZEND_API zend_class_entry *zend_fetch_class(char *class_name, uint class_name_len, int fetch_type TSRMLS_DC); diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index 09d4b1cf7c..549209b655 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -1710,15 +1710,15 @@ void zend_shutdown_timeout_thread(void) /* {{{ */ #define SIGPROF 27 #endif -void zend_set_timeout(long seconds) /* {{{ */ +void zend_set_timeout(long seconds, int reset_signals) /* {{{ */ { TSRMLS_FETCH(); EG(timeout_seconds) = seconds; +#ifdef ZEND_WIN32 if(!seconds) { return; } -#ifdef ZEND_WIN32 if (timeout_thread_initialized == 0 && InterlockedIncrement(&timeout_thread_initialized) == 1) { /* We start up this process-wide thread here and not in zend_startup(), because if Zend * is initialized inside a DllMain(), you're not supposed to start threads from it. @@ -1731,22 +1731,30 @@ void zend_set_timeout(long seconds) /* {{{ */ { struct itimerval t_r; /* timeout requested */ sigset_t sigset; - - t_r.it_value.tv_sec = seconds; - t_r.it_value.tv_usec = t_r.it_interval.tv_sec = t_r.it_interval.tv_usec = 0; + if(seconds) { + t_r.it_value.tv_sec = seconds; + t_r.it_value.tv_usec = t_r.it_interval.tv_sec = t_r.it_interval.tv_usec = 0; # ifdef __CYGWIN__ - setitimer(ITIMER_REAL, &t_r, NULL); - signal(SIGALRM, zend_timeout); - sigemptyset(&sigset); - sigaddset(&sigset, SIGALRM); + setitimer(ITIMER_REAL, &t_r, NULL); + } + if(reset_signals) { + signal(SIGALRM, zend_timeout); + sigemptyset(&sigset); + sigaddset(&sigset, SIGALRM); + } # else - setitimer(ITIMER_PROF, &t_r, NULL); - signal(SIGPROF, zend_timeout); - sigemptyset(&sigset); - sigaddset(&sigset, SIGPROF); + setitimer(ITIMER_PROF, &t_r, NULL); + } + if(reset_signals) { + signal(SIGPROF, zend_timeout); + sigemptyset(&sigset); + sigaddset(&sigset, SIGPROF); + } # endif - sigprocmask(SIG_UNBLOCK, &sigset, NULL); + if(reset_signals) { + sigprocmask(SIG_UNBLOCK, &sigset, NULL); + } } # endif #endif diff --git a/Zend/zend_globals.h b/Zend/zend_globals.h index 3feb64dd52..e2f21e4284 100644 --- a/Zend/zend_globals.h +++ b/Zend/zend_globals.h @@ -170,7 +170,7 @@ struct _zend_executor_globals { HashTable included_files; /* files already included */ - jmp_buf *bailout; + JMP_BUF *bailout; int error_reporting; int orig_error_reporting; diff --git a/main/SAPI.c b/main/SAPI.c index f4c8d7525e..1ee7971903 100644 --- a/main/SAPI.c +++ b/main/SAPI.c @@ -933,6 +933,12 @@ SAPI_API time_t sapi_get_request_time(TSRMLS_D) return SG(global_request_time); } +SAPI_API void sapi_terminate_process(TSRMLS_D) { + if (sapi_module.terminate_process) { + sapi_module.terminate_process(TSRMLS_C); + } +} + /* * Local variables: * tab-width: 4 diff --git a/main/SAPI.h b/main/SAPI.h index b7e1366eb3..ff89c31a0c 100644 --- a/main/SAPI.h +++ b/main/SAPI.h @@ -208,6 +208,7 @@ SAPI_API int sapi_force_http_10(TSRMLS_D); SAPI_API int sapi_get_target_uid(uid_t * TSRMLS_DC); SAPI_API int sapi_get_target_gid(gid_t * TSRMLS_DC); SAPI_API time_t sapi_get_request_time(TSRMLS_D); +SAPI_API void sapi_terminate_process(TSRMLS_D); END_EXTERN_C() struct _sapi_module_struct { @@ -237,6 +238,7 @@ struct _sapi_module_struct { void (*register_server_variables)(zval *track_vars_array TSRMLS_DC); void (*log_message)(char *message); time_t (*get_request_time)(TSRMLS_D); + void (*terminate_process)(TSRMLS_D); char *php_ini_path_override; diff --git a/main/main.c b/main/main.c index 18b03fde9e..e375464d40 100644 --- a/main/main.c +++ b/main/main.c @@ -208,7 +208,7 @@ static PHP_INI_MH(OnUpdateTimeout) return SUCCESS; } zend_unset_timeout(TSRMLS_C); - zend_set_timeout(EG(timeout_seconds)); + zend_set_timeout(EG(timeout_seconds), 0); return SUCCESS; } /* }}} */ @@ -554,6 +554,7 @@ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("user_ini.filename", ".user.ini", PHP_INI_SYSTEM, OnUpdateString, user_ini_filename, php_core_globals, core_globals) STD_PHP_INI_ENTRY("user_ini.cache_ttl", "300", PHP_INI_SYSTEM, OnUpdateLong, user_ini_cache_ttl, php_core_globals, core_globals) + STD_PHP_INI_BOOLEAN("exit_on_timeout", "0", PHP_INI_ALL, OnUpdateBool, exit_on_timeout, php_core_globals, core_globals) PHP_INI_END() /* }}} */ @@ -1309,7 +1310,8 @@ static void php_message_handler_for_zend(long message, void *data) void php_on_timeout(int seconds TSRMLS_DC) /* {{{ */ { PG(connection_status) |= PHP_CONNECTION_TIMEOUT; - zend_set_timeout(EG(timeout_seconds)); + zend_set_timeout(EG(timeout_seconds), 0); + if(PG(exit_on_timeout)) sapi_terminate_process(TSRMLS_C); } /* }}} */ @@ -1340,7 +1342,7 @@ static int php_start_sapi(TSRMLS_D) PG(connection_status) = PHP_CONNECTION_NORMAL; zend_activate(TSRMLS_C); - zend_set_timeout(EG(timeout_seconds)); + zend_set_timeout(EG(timeout_seconds), 1); zend_activate_modules(TSRMLS_C); PG(modules_activated)=1; } zend_catch { @@ -1384,9 +1386,9 @@ int php_request_startup(TSRMLS_D) sapi_activate(TSRMLS_C); if (PG(max_input_time) == -1) { - zend_set_timeout(EG(timeout_seconds)); + zend_set_timeout(EG(timeout_seconds), 1); } else { - zend_set_timeout(PG(max_input_time)); + zend_set_timeout(PG(max_input_time), 1); } /* Disable realpath cache if safe_mode or open_basedir are set */ @@ -2191,7 +2193,7 @@ PHPAPI int php_execute_script(zend_file_handle *primary_file TSRMLS_DC) #ifdef PHP_WIN32 zend_unset_timeout(TSRMLS_C); #endif - zend_set_timeout(INI_INT("max_execution_time")); + zend_set_timeout(EG(timeout_seconds), 0); } retval = (zend_execute_scripts(ZEND_REQUIRE TSRMLS_CC, NULL, 3, prepend_file_p, primary_file, append_file_p) == SUCCESS); diff --git a/main/php_globals.h b/main/php_globals.h index b37ea5f3ab..5d7916a3fb 100644 --- a/main/php_globals.h +++ b/main/php_globals.h @@ -140,6 +140,7 @@ struct _php_core_globals { char *disable_functions; char *disable_classes; + zend_bool exit_on_timeout; #ifdef PHP_WIN32 zend_bool com_initialized; #endif diff --git a/sapi/aolserver/aolserver.c b/sapi/aolserver/aolserver.c index a3eeed3522..d0b84d3f86 100644 --- a/sapi/aolserver/aolserver.c +++ b/sapi/aolserver/aolserver.c @@ -386,6 +386,7 @@ static sapi_module_struct aolserver_sapi_module = { php_ns_sapi_register_variables, NULL, /* Log message */ NULL, /* Get request time */ + NULL, /* child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/sapi/apache/mod_php.c b/sapi/apache/mod_php.c index 7947c8127a..937cac3159 100644 --- a/sapi/apache/mod_php.c +++ b/sapi/apache/mod_php.c @@ -431,6 +431,14 @@ static time_t php_apache_get_request_time(TSRMLS_D) } /* }}} */ +/* {{{ sapi_apache_child_terminate + */ +static void sapi_apache_child_terminate(TSRMLS_D) +{ + ap_child_terminate((request_rec *)SG(server_context)); +} +/* }}} */ + /* {{{ sapi_module_struct apache_sapi_module */ static sapi_module_struct apache_sapi_module = { @@ -460,6 +468,7 @@ static sapi_module_struct apache_sapi_module = { sapi_apache_register_server_variables, /* register server variables */ php_apache_log_message, /* Log message */ php_apache_get_request_time, /* Get request time */ + sapi_apache_child_terminate, NULL, /* php.ini path override */ diff --git a/sapi/apache2filter/sapi_apache2.c b/sapi/apache2filter/sapi_apache2.c index 4427f1e334..10dc21555c 100644 --- a/sapi/apache2filter/sapi_apache2.c +++ b/sapi/apache2filter/sapi_apache2.c @@ -343,6 +343,7 @@ static sapi_module_struct apache2_sapi_module = { php_apache_sapi_register_variables, php_apache_sapi_log_message, /* Log message */ php_apache_sapi_get_request_time, /* Get Request Time */ + NULL, /* Child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/sapi/apache2handler/sapi_apache2.c b/sapi/apache2handler/sapi_apache2.c index 171101250d..9773c581f3 100644 --- a/sapi/apache2handler/sapi_apache2.c +++ b/sapi/apache2handler/sapi_apache2.c @@ -343,6 +343,7 @@ static sapi_module_struct apache2_sapi_module = { php_apache_sapi_register_variables, php_apache_sapi_log_message, /* Log message */ php_apache_sapi_get_request_time, /* Request Time */ + NULL, /* Child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/sapi/apache_hooks/mod_php5.c b/sapi/apache_hooks/mod_php5.c index bbbe70769b..3b180ef040 100644 --- a/sapi/apache_hooks/mod_php5.c +++ b/sapi/apache_hooks/mod_php5.c @@ -521,6 +521,7 @@ static sapi_module_struct apache_sapi_module = { sapi_apache_register_server_variables, /* register server variables */ php_apache_log_message, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ NULL, /* php.ini path override */ diff --git a/sapi/caudium/caudium.c b/sapi/caudium/caudium.c index 197448a433..9bbd419050 100644 --- a/sapi/caudium/caudium.c +++ b/sapi/caudium/caudium.c @@ -551,6 +551,7 @@ static sapi_module_struct caudium_sapi_module = { sapi_caudium_register_variables, /* register server variables */ NULL, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c index 75638d41e1..ad061490fd 100644 --- a/sapi/cgi/cgi_main.c +++ b/sapi/cgi/cgi_main.c @@ -806,6 +806,7 @@ static sapi_module_struct cgi_sapi_module = { sapi_cgi_register_variables, /* register server variables */ sapi_cgi_log_message, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index 05ee0cc05c..8aad625450 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -412,6 +412,7 @@ static sapi_module_struct cli_sapi_module = { sapi_cli_register_variables, /* register server variables */ sapi_cli_log_message, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/sapi/continuity/capi.c b/sapi/continuity/capi.c index 3908499b27..a419e1b896 100644 --- a/sapi/continuity/capi.c +++ b/sapi/continuity/capi.c @@ -378,7 +378,8 @@ sapi_module_struct capi_sapi_module = { sapi_capi_register_server_variables, /* register server variables */ capi_log_message, /* Log message */ - NULL, /* Get request time */ + NULL, /* Get request time */ + NULL, /* Child terminate */ NULL, /* Block interruptions */ NULL, /* Unblock interruptions */ diff --git a/sapi/embed/php_embed.c b/sapi/embed/php_embed.c index dfd069e557..7e8eccbe40 100644 --- a/sapi/embed/php_embed.c +++ b/sapi/embed/php_embed.c @@ -135,6 +135,7 @@ sapi_module_struct php_embed_module = { php_embed_register_variables, /* register server variables */ php_embed_log_message, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/sapi/isapi/php5isapi.c b/sapi/isapi/php5isapi.c index 0bfa51b4cb..959d85f48d 100644 --- a/sapi/isapi/php5isapi.c +++ b/sapi/isapi/php5isapi.c @@ -688,6 +688,7 @@ static sapi_module_struct isapi_sapi_module = { sapi_isapi_register_server_variables, /* register server variables */ NULL, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/sapi/milter/php_milter.c b/sapi/milter/php_milter.c index 966651e772..088922cdf5 100644 --- a/sapi/milter/php_milter.c +++ b/sapi/milter/php_milter.c @@ -869,6 +869,7 @@ static sapi_module_struct milter_sapi_module = { sapi_milter_register_variables, /* register server variables */ NULL, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ NULL, /* Block interruptions */ NULL, /* Unblock interruptions */ diff --git a/sapi/nsapi/nsapi.c b/sapi/nsapi/nsapi.c index fa116a72f5..47ca41080c 100644 --- a/sapi/nsapi/nsapi.c +++ b/sapi/nsapi/nsapi.c @@ -753,6 +753,7 @@ static sapi_module_struct nsapi_sapi_module = { sapi_nsapi_register_server_variables, /* register server variables */ nsapi_log_message, /* Log message */ sapi_nsapi_get_request_time, /* Get request time */ + NULL, /* Child terminate */ NULL, /* Block interruptions */ NULL, /* Unblock interruptions */ diff --git a/sapi/phttpd/phttpd.c b/sapi/phttpd/phttpd.c index ed4b3fd57f..c2a4166f2d 100644 --- a/sapi/phttpd/phttpd.c +++ b/sapi/phttpd/phttpd.c @@ -180,6 +180,7 @@ static sapi_module_struct phttpd_sapi_module = { NULL, /* register server variables */ NULL, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/sapi/pi3web/pi3web_sapi.c b/sapi/pi3web/pi3web_sapi.c index 6365f5ee07..5c1f4ff99a 100644 --- a/sapi/pi3web/pi3web_sapi.c +++ b/sapi/pi3web/pi3web_sapi.c @@ -337,6 +337,7 @@ static sapi_module_struct pi3web_sapi_module = { sapi_pi3web_register_variables, /* register server variables */ NULL, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/sapi/roxen/roxen.c b/sapi/roxen/roxen.c index 13155d49f4..695262556e 100644 --- a/sapi/roxen/roxen.c +++ b/sapi/roxen/roxen.c @@ -502,6 +502,7 @@ static sapi_module_struct roxen_sapi_module = { NULL, /* register server variables */ NULL, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/sapi/thttpd/thttpd.c b/sapi/thttpd/thttpd.c index faa8f4f43f..32f81ab522 100644 --- a/sapi/thttpd/thttpd.c +++ b/sapi/thttpd/thttpd.c @@ -394,6 +394,7 @@ static sapi_module_struct thttpd_sapi_module = { sapi_thttpd_register_variables, NULL, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ NULL, /* php.ini path override */ NULL, /* Block interruptions */ diff --git a/sapi/tux/php_tux.c b/sapi/tux/php_tux.c index 7d24367da6..68f0ce5d68 100644 --- a/sapi/tux/php_tux.c +++ b/sapi/tux/php_tux.c @@ -288,6 +288,7 @@ static sapi_module_struct tux_sapi_module = { sapi_tux_register_variables, NULL, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/sapi/webjames/webjames.c b/sapi/webjames/webjames.c index f166eddfb8..2fb52f5661 100644 --- a/sapi/webjames/webjames.c +++ b/sapi/webjames/webjames.c @@ -301,6 +301,7 @@ static sapi_module_struct sapi_module = { sapi_webjames_register_variables, /* register server variables */ NULL, /* Log message */ NULL, /* Get request time */ + NULL, /* Child terminate */ STANDARD_SAPI_MODULE_PROPERTIES }; diff --git a/win32/build/config.w32.h.in b/win32/build/config.w32.h.in index 91a4927321..8139d68dd0 100644 --- a/win32/build/config.w32.h.in +++ b/win32/build/config.w32.h.in @@ -53,6 +53,7 @@ #define NEED_ISBLANK 1 #define DISCARD_PATH 0 #undef HAVE_SETITIMER +#undef HAVE_SIGSETJMP #undef HAVE_IODBC #define HAVE_LIBDL 1 #define HAVE_GETTIMEOFDAY 1 -- 2.40.0