From e2d05bfcb2b788932bacf53bcd4cab8ec27a8dd4 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 16 Sep 2021 16:54:07 +0200 Subject: [PATCH] Allow get_request_time() hook to fail In particular, this allows using the hook without server_context. The apache2handler implementation now checks that server_context is available itself, as that's the implementation that cares about it. --- main/SAPI.c | 5 ++--- main/SAPI.h | 2 +- sapi/apache2handler/sapi_apache2.c | 9 +++++++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/main/SAPI.c b/main/SAPI.c index 039ba068270..d1bd3134b6d 100644 --- a/main/SAPI.c +++ b/main/SAPI.c @@ -1073,9 +1073,8 @@ SAPI_API double sapi_get_request_time(void) { if(SG(global_request_time)) return SG(global_request_time); - if (sapi_module.get_request_time && SG(server_context)) { - SG(global_request_time) = sapi_module.get_request_time(); - } else { + if (!sapi_module.get_request_time + || sapi_module.get_request_time(&SG(global_request_time)) == FAILURE) { struct timeval tp = {0}; if (!gettimeofday(&tp, NULL)) { SG(global_request_time) = (double)(tp.tv_sec + tp.tv_usec / 1000000.00); diff --git a/main/SAPI.h b/main/SAPI.h index 97c52c41ecc..77ab84806fe 100644 --- a/main/SAPI.h +++ b/main/SAPI.h @@ -236,7 +236,7 @@ struct _sapi_module_struct { void (*register_server_variables)(zval *track_vars_array); void (*log_message)(const char *message, int syslog_type_int); - double (*get_request_time)(void); + zend_result (*get_request_time)(double *request_time); void (*terminate_process)(void); char *php_ini_path_override; diff --git a/sapi/apache2handler/sapi_apache2.c b/sapi/apache2handler/sapi_apache2.c index 2eeeaf174d8..178b6f3de47 100644 --- a/sapi/apache2handler/sapi_apache2.c +++ b/sapi/apache2handler/sapi_apache2.c @@ -363,10 +363,15 @@ static void php_apache_sapi_log_message_ex(const char *msg, request_rec *r) } } -static double php_apache_sapi_get_request_time(void) +static zend_result php_apache_sapi_get_request_time(double *request_time) { php_struct *ctx = SG(server_context); - return ((double) ctx->r->request_time) / 1000000.0; + if (!ctx) { + return FAILURE; + } + + *request_time = ((double) ctx->r->request_time) / 1000000.0; + return SUCCESS; } extern zend_module_entry php_apache_module;