diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index bd6b323871bcc..7c3abcbce5fba 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -76,7 +76,12 @@ typedef int gid_t; #endif #include #include + +#ifndef PHP_WIN32 #include +#else +#include "win32/time.h" +#endif #ifndef ZEND_WIN32 # include @@ -1162,13 +1167,32 @@ zend_result validate_timestamp_and_record(zend_persistent_script *persistent_scr { if (persistent_script->timestamp == 0) { return SUCCESS; /* Don't check timestamps of preloaded scripts */ - } else if (ZCG(accel_directives).revalidate_freq && - persistent_script->dynamic_members.revalidate >= ZCG(request_time)) { + } + + double revalidate_reference_time = 0.0; + + if (ZCG(cli_mode)) { +#if HAVE_GETTIMEOFDAY + struct timeval tp = {0}; + + if (UNEXPECTED(gettimeofday(&tp, NULL) != 0)) { + revalidate_reference_time = (double)time(NULL); + } else { + revalidate_reference_time = (double)(tp.tv_sec + tp.tv_usec / 1000000.00); + } +#else + revalidate_reference_time = (double)time(NULL); +#endif + } else { + revalidate_reference_time = (double)ZCG(request_time); + } + + if (ZCG(accel_directives).revalidate_freq && persistent_script->dynamic_members.revalidate >= revalidate_reference_time) { return SUCCESS; } else if (do_validate_timestamps(persistent_script, file_handle) == FAILURE) { return FAILURE; } else { - persistent_script->dynamic_members.revalidate = ZCG(request_time) + ZCG(accel_directives).revalidate_freq; + persistent_script->dynamic_members.revalidate = revalidate_reference_time + ZCG(accel_directives).revalidate_freq; return SUCCESS; } } @@ -2832,6 +2856,7 @@ static inline zend_result accel_find_sapi(void) if (ZCG(accel_directives).enable_cli && ( strcmp(sapi_module.name, "cli") == 0 || strcmp(sapi_module.name, "phpdbg") == 0)) { + ZCG(cli_mode) = true; return SUCCESS; } } diff --git a/ext/opcache/ZendAccelerator.h b/ext/opcache/ZendAccelerator.h index a25f9a1bdf037..bcd3548ec1e50 100644 --- a/ext/opcache/ZendAccelerator.h +++ b/ext/opcache/ZendAccelerator.h @@ -224,6 +224,7 @@ typedef struct _zend_accel_globals { zend_persistent_script *cache_persistent_script; /* preallocated buffer for keys */ zend_string *key; + bool cli_mode; } zend_accel_globals; typedef struct _zend_string_table { diff --git a/ext/opcache/tests/opcache_revalidation_in_cli.phpt b/ext/opcache/tests/opcache_revalidation_in_cli.phpt new file mode 100644 index 0000000000000..3cd9a29e74346 --- /dev/null +++ b/ext/opcache/tests/opcache_revalidation_in_cli.phpt @@ -0,0 +1,42 @@ +--TEST-- +opcache revalidation should work properly in CLI mode +--EXTENSIONS-- +opcache +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.validate_timestamps=1 +opcache.revalidate_freq=1 +--FILE-- + +--EXPECT-- +bool(true) +int(42) +int(42) +int(1234)