From aa02ce716e2220eb046c6a815023c75a284b417d Mon Sep 17 00:00:00 2001 From: Florian Engelhardt Date: Mon, 26 Aug 2024 16:04:54 +0200 Subject: [PATCH 1/4] Add OPcache restart hook --- ext/opcache/ZendAccelerator.c | 7 +++++++ ext/opcache/ZendAccelerator.h | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index f9514d4807c4e..28749f038b719 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -26,6 +26,7 @@ #include "zend_compile.h" #include "ZendAccelerator.h" #include "zend_persist.h" +#include "zend_portability.h" #include "zend_shared_alloc.h" #include "zend_accelerator_module.h" #include "zend_accelerator_blacklist.h" @@ -3410,6 +3411,8 @@ void accel_shutdown(void) } } +ZEND_EXT_API void (*zend_accel_schedule_restart_hook)(zend_accel_restart_reason reason); + void zend_accel_schedule_restart(zend_accel_restart_reason reason) { const char *zend_accel_restart_reason_text[ACCEL_RESTART_USER + 1] = { @@ -3418,6 +3421,10 @@ void zend_accel_schedule_restart(zend_accel_restart_reason reason) "user", }; + if (UNEXPECTED(zend_accel_schedule_restart_hook)) { + zend_accel_schedule_restart_hook(reason); + } + if (ZCSG(restart_pending)) { /* don't schedule twice */ return; diff --git a/ext/opcache/ZendAccelerator.h b/ext/opcache/ZendAccelerator.h index 682677441e413..190fcc3eac9a7 100644 --- a/ext/opcache/ZendAccelerator.h +++ b/ext/opcache/ZendAccelerator.h @@ -311,8 +311,9 @@ extern const char *zps_api_failure_reason; BEGIN_EXTERN_C() void accel_shutdown(void); -zend_result accel_activate(INIT_FUNC_ARGS); +zend_result accel_activate(INIT_FUNC_ARGS); zend_result accel_post_deactivate(void); +extern ZEND_EXT_API void (*zend_accel_schedule_restart_hook)(zend_accel_restart_reason reason); void zend_accel_schedule_restart(zend_accel_restart_reason reason); void zend_accel_schedule_restart_if_necessary(zend_accel_restart_reason reason); accel_time_t zend_get_file_handle_timestamp(zend_file_handle *file_handle, size_t *size); From b99f3efd9313b9fdf6383b99e1e1abc550e0ae44 Mon Sep 17 00:00:00 2001 From: Florian Engelhardt Date: Tue, 27 Aug 2024 17:52:38 +0200 Subject: [PATCH 2/4] initialize with `NULL` --- ext/opcache/ZendAccelerator.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index 28749f038b719..7b03361d9b120 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -3411,7 +3411,7 @@ void accel_shutdown(void) } } -ZEND_EXT_API void (*zend_accel_schedule_restart_hook)(zend_accel_restart_reason reason); +ZEND_EXT_API void (*zend_accel_schedule_restart_hook)(zend_accel_restart_reason reason) = NULL; void zend_accel_schedule_restart(zend_accel_restart_reason reason) { From 7feba9a0bf4986d4c1451668b20f6e33c7aebeb7 Mon Sep 17 00:00:00 2001 From: Florian Engelhardt Date: Tue, 24 Sep 2024 13:47:24 +0200 Subject: [PATCH 3/4] make sure hook is called once per restart --- ext/opcache/ZendAccelerator.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index 7b03361d9b120..546c03cf454f7 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -3421,14 +3421,15 @@ void zend_accel_schedule_restart(zend_accel_restart_reason reason) "user", }; - if (UNEXPECTED(zend_accel_schedule_restart_hook)) { - zend_accel_schedule_restart_hook(reason); - } - if (ZCSG(restart_pending)) { /* don't schedule twice */ return; } + + if (UNEXPECTED(zend_accel_schedule_restart_hook)) { + zend_accel_schedule_restart_hook(reason); + } + zend_accel_error(ACCEL_LOG_DEBUG, "Restart Scheduled! Reason: %s", zend_accel_restart_reason_text[reason]); From a262875f9e95cdc7d5a2fe246ee8ff9f3402765a Mon Sep 17 00:00:00 2001 From: Florian Engelhardt Date: Tue, 24 Sep 2024 14:59:28 +0200 Subject: [PATCH 4/4] move hook to zend.c/h --- Zend/zend.c | 1 + Zend/zend.h | 2 ++ ext/opcache/ZendAccelerator.c | 3 --- ext/opcache/ZendAccelerator.h | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Zend/zend.c b/Zend/zend.c index ef5ea8cebc78b..63a6183078eb5 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -94,6 +94,7 @@ ZEND_API char *(*zend_getenv)(const char *name, size_t name_len); ZEND_API zend_string *(*zend_resolve_path)(zend_string *filename); ZEND_API zend_result (*zend_post_startup_cb)(void) = NULL; ZEND_API void (*zend_post_shutdown_cb)(void) = NULL; +ZEND_API void (*zend_accel_schedule_restart_hook)(int reason) = NULL; ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_random_bytes)(void *bytes, size_t size, char *errstr, size_t errstr_size) = NULL; ZEND_ATTRIBUTE_NONNULL ZEND_API void (*zend_random_bytes_insecure)(zend_random_bytes_insecure_state *state, void *bytes, size_t size) = NULL; diff --git a/Zend/zend.h b/Zend/zend.h index 4fe0703d42f69..b4f7a02988046 100644 --- a/Zend/zend.h +++ b/Zend/zend.h @@ -362,6 +362,8 @@ extern ZEND_ATTRIBUTE_NONNULL ZEND_API void (*zend_random_bytes_insecure)( extern ZEND_API zend_result (*zend_post_startup_cb)(void); extern ZEND_API void (*zend_post_shutdown_cb)(void); +extern ZEND_API void (*zend_accel_schedule_restart_hook)(int reason); + ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3); ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3); ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn_unchecked(int type, const char *format, ...); diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index 546c03cf454f7..80df219448b31 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -26,7 +26,6 @@ #include "zend_compile.h" #include "ZendAccelerator.h" #include "zend_persist.h" -#include "zend_portability.h" #include "zend_shared_alloc.h" #include "zend_accelerator_module.h" #include "zend_accelerator_blacklist.h" @@ -3411,8 +3410,6 @@ void accel_shutdown(void) } } -ZEND_EXT_API void (*zend_accel_schedule_restart_hook)(zend_accel_restart_reason reason) = NULL; - void zend_accel_schedule_restart(zend_accel_restart_reason reason) { const char *zend_accel_restart_reason_text[ACCEL_RESTART_USER + 1] = { diff --git a/ext/opcache/ZendAccelerator.h b/ext/opcache/ZendAccelerator.h index 190fcc3eac9a7..162892bf2c279 100644 --- a/ext/opcache/ZendAccelerator.h +++ b/ext/opcache/ZendAccelerator.h @@ -313,7 +313,6 @@ BEGIN_EXTERN_C() void accel_shutdown(void); zend_result accel_activate(INIT_FUNC_ARGS); zend_result accel_post_deactivate(void); -extern ZEND_EXT_API void (*zend_accel_schedule_restart_hook)(zend_accel_restart_reason reason); void zend_accel_schedule_restart(zend_accel_restart_reason reason); void zend_accel_schedule_restart_if_necessary(zend_accel_restart_reason reason); accel_time_t zend_get_file_handle_timestamp(zend_file_handle *file_handle, size_t *size);