Skip to content

Commit 88ec473

Browse files
committed
Replace time() in favor of zend_realtime_get()
1 parent 3c341ff commit 88ec473

File tree

24 files changed

+55
-70
lines changed

24 files changed

+55
-70
lines changed

Zend/zend_time.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ZEND_API void zend_realtime_spec(struct timespec *ts) {
3333

3434
#else
3535

36-
ts->tv_sec = time(NULL);
36+
ts->tv_sec = zend_realtime_get();
3737
ts->tv_nsec = 0;
3838

3939
#endif

Zend/zend_virtual_cwd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
#include <errno.h>
2525
#include <stdlib.h>
2626
#include <fcntl.h>
27-
#include <time.h>
2827

2928
#include "zend.h"
3029
#include "zend_virtual_cwd.h"
30+
#include "zend_time.h"
3131

3232
#ifdef ZEND_WIN32
3333
#include <io.h>
@@ -577,7 +577,7 @@ static size_t tsrm_realpath_r(char *path, size_t start, size_t len, int *ll, tim
577577
if (start && save && CWDG(realpath_cache_size_limit)) {
578578
/* cache lookup for absolute path */
579579
if (!*t) {
580-
*t = time(0);
580+
*t = zend_realtime_get();
581581
}
582582
if ((bucket = realpath_cache_find(path, len, *t)) != NULL) {
583583
if (is_dir && !bucket->is_dir) {

ext/calendar/cal_unix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "php.h"
2020
#include "php_calendar.h"
2121
#include "sdncal.h"
22-
#include <time.h>
22+
#include "zend_time.h"
2323

2424
#define SECS_PER_DAY (24 * 3600)
2525

@@ -36,7 +36,7 @@ PHP_FUNCTION(unixtojd)
3636
}
3737

3838
if (tl_is_null) {
39-
ts = time(NULL);
39+
ts = zend_realtime_get();
4040
} else if (tl >= 0) {
4141
ts = (time_t) tl;
4242
} else {

ext/calendar/easter.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "php.h"
2121
#include "php_calendar.h"
2222
#include "sdncal.h"
23-
#include <time.h>
23+
#include "zend_time.h"
2424

2525
/**
2626
* If `gm` is true this will return the timestamp at midnight on Easter of the given year. If it is false this
@@ -43,9 +43,8 @@ static void _cal_easter(INTERNAL_FUNCTION_PARAMETERS, bool gm)
4343

4444
/* Default to the current year if year parameter is not given */
4545
if (year_is_null) {
46-
time_t a;
46+
time_t a = zend_realtime_get();
4747
struct tm b, *res;
48-
time(&a);
4948
res = php_localtime_r(&a, &b);
5049
if (!res) {
5150
year = 1900;

ext/ftp/ftp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ time_t ftp_mdtm(ftpbuf_t *ftp, const char *path, const size_t path_len)
11081108
tm.tm_isdst = -1;
11091109

11101110
/* figure out the GMT offset */
1111-
stamp = time(NULL);
1111+
stamp = zend_realtime_get();
11121112
gmt = php_gmtime_r(&stamp, &tmbuf);
11131113
if (!gmt) {
11141114
return -1;

ext/opcache/ZendAccelerator.c

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "zend_accelerator_hash.h"
4949
#include "zend_file_cache.h"
5050
#include "zend_system_id.h"
51+
#include "zend_time.h"
5152
#include "ext/pcre/php_pcre.h"
5253
#include "ext/standard/basic_functions.h"
5354

@@ -73,9 +74,7 @@ typedef int gid_t;
7374
#include <lmcons.h>
7475
#endif
7576

76-
#ifndef ZEND_WIN32
77-
# include <sys/time.h>
78-
#else
77+
#ifdef ZEND_WIN32
7978
# include <process.h>
8079
#endif
8180

@@ -84,7 +83,6 @@ typedef int gid_t;
8483
#endif
8584
#include <fcntl.h>
8685
#include <signal.h>
87-
#include <time.h>
8886

8987
#ifndef ZEND_WIN32
9088
# include <sys/types.h>
@@ -191,18 +189,6 @@ static void bzero_aligned(void *mem, size_t size)
191189
#endif
192190
}
193191

194-
#ifdef ZEND_WIN32
195-
static time_t zend_accel_get_time(void)
196-
{
197-
FILETIME now;
198-
GetSystemTimeAsFileTime(&now);
199-
200-
return (time_t) ((((((__int64)now.dwHighDateTime) << 32)|now.dwLowDateTime) - 116444736000000000L)/10000000);
201-
}
202-
#else
203-
# define zend_accel_get_time() time(NULL)
204-
#endif
205-
206192
static inline bool is_cacheable_stream_path(const char *filename)
207193
{
208194
return memcmp(filename, "file://", sizeof("file://") - 1) == 0 ||
@@ -878,7 +864,7 @@ static inline void kill_all_lockers(struct flock *mem_usage_check)
878864
}
879865
if (!success) {
880866
/* errno is not ESRCH or we ran out of tries to kill the locker */
881-
ZCSG(force_restart_time) = time(NULL); /* restore forced restart request */
867+
ZCSG(force_restart_time) = zend_realtime_get(); /* restore forced restart request */
882868
/* cannot kill the locker, bail out with error */
883869
zend_accel_error_noreturn(ACCEL_LOG_ERROR, "Cannot kill process %d!", mem_usage_check->l_pid);
884870
}
@@ -915,6 +901,7 @@ static inline bool accel_is_inactive(void)
915901
}
916902
#else
917903
struct flock mem_usage_check;
904+
time_t now;
918905

919906
mem_usage_check.l_type = F_WRLCK;
920907
mem_usage_check.l_whence = SEEK_SET;
@@ -931,8 +918,8 @@ static inline bool accel_is_inactive(void)
931918

932919
if (ZCG(accel_directives).force_restart_timeout
933920
&& ZCSG(force_restart_time)
934-
&& time(NULL) >= ZCSG(force_restart_time)) {
935-
zend_accel_error(ACCEL_LOG_WARNING, "Forced restart at %ld (after " ZEND_LONG_FMT " seconds), locked by %d", (long)time(NULL), ZCG(accel_directives).force_restart_timeout, mem_usage_check.l_pid);
921+
&& (now = zend_realtime_get()) >= ZCSG(force_restart_time)) {
922+
zend_accel_error(ACCEL_LOG_WARNING, "Forced restart at %lld (after " ZEND_LONG_FMT " seconds), locked by %d", (long long)now, ZCG(accel_directives).force_restart_timeout, mem_usage_check.l_pid);
936923
kill_all_lockers(&mem_usage_check);
937924

938925
return false; /* next request should be able to restart it */
@@ -2936,7 +2923,7 @@ static zend_result zend_accel_init_shm(void)
29362923
ZCSG(manual_restarts) = 0;
29372924

29382925
ZCSG(accelerator_enabled) = true;
2939-
ZCSG(start_time) = zend_accel_get_time();
2926+
ZCSG(start_time) = zend_realtime_get();
29402927
ZCSG(last_restart_time) = 0;
29412928
ZCSG(restart_in_progress) = false;
29422929

@@ -3552,7 +3539,7 @@ void zend_accel_schedule_restart(zend_accel_restart_reason reason)
35523539
ZCSG(accelerator_enabled) = false;
35533540

35543541
if (ZCG(accel_directives).force_restart_timeout) {
3555-
ZCSG(force_restart_time) = zend_accel_get_time() + ZCG(accel_directives).force_restart_timeout;
3542+
ZCSG(force_restart_time) = zend_realtime_get() + ZCG(accel_directives).force_restart_timeout;
35563543
} else {
35573544
ZCSG(force_restart_time) = 0;
35583545
}

ext/opcache/zend_accelerator_debug.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
#include <stdio.h>
2323
#include <stdlib.h>
2424
#include <stdarg.h>
25-
#include <time.h>
2625
#ifdef ZEND_WIN32
2726
# include <process.h>
2827
#endif
28+
#include "zend_time.h"
2929
#include "ZendAccelerator.h"
3030

3131
static void zend_accel_error_va_args(int type, const char *format, va_list args)
@@ -36,7 +36,7 @@ static void zend_accel_error_va_args(int type, const char *format, va_list args)
3636

3737
if (type <= ZCG(accel_directives).log_verbosity_level) {
3838

39-
timestamp = time(NULL);
39+
timestamp = zend_realtime_get();
4040
time_string = asctime(localtime(&timestamp));
4141
time_string[24] = 0;
4242

ext/pgsql/pgsql.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@
3131
#include "ext/standard/info.h"
3232
#include "zend_smart_str.h"
3333
#include "ext/pcre/php_pcre.h"
34-
#ifdef PHP_WIN32
35-
# include "win32/time.h"
36-
#endif
3734
#include "php_pgsql.h"
3835
#include "php_globals.h"
3936
#include "zend_exceptions.h"
4037
#include "zend_attributes.h"
4138
#include "zend_interfaces.h"
39+
#include "zend_time.h"
4240
#include "php_network.h"
4341

4442
#ifdef HAVE_PGSQL
@@ -472,7 +470,7 @@ static int PQsocketPoll(int socket, int read, int write, time_t timeout)
472470
}
473471

474472
if (timeout != (time_t)ts) {
475-
time_t cur = time(NULL);
473+
time_t cur = zend_realtime_get();
476474

477475
if (timeout > cur) {
478476
ts = (timeout - cur) * 1000;

ext/phar/phar.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "ext/standard/php_string.h" /* For php_stristr() */
2828
#include "ext/standard/info.h"
2929
#include "zend_smart_str.h"
30+
#include "zend_time.h"
3031

3132
static void destroy_phar_data(zval *zv);
3233

@@ -2973,7 +2974,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa
29732974
4: metadata-len
29742975
+: metadata
29752976
*/
2976-
mytime = time(NULL);
2977+
mytime = zend_realtime_get();
29772978
phar_set_32(entry_buffer, entry->uncompressed_filesize);
29782979
phar_set_32(entry_buffer+4, mytime);
29792980
phar_set_32(entry_buffer+8, entry->compressed_filesize);

ext/phar/stream.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "phar_internal.h"
2222
#include "stream.h"
2323
#include "dirstream.h"
24+
#include "zend_time.h"
2425

2526
static const php_stream_ops phar_ops = {
2627
phar_stream_write, /* write */
@@ -470,7 +471,7 @@ static int phar_stream_flush(php_stream *stream) /* {{{ */
470471
phar_entry_data *data = (phar_entry_data *) stream->abstract;
471472

472473
if (data->internal_file->is_modified) {
473-
data->internal_file->timestamp = time(0);
474+
data->internal_file->timestamp = zend_realtime_get();
474475
phar_flush(data->phar, &error);
475476
if (error) {
476477
php_stream_wrapper_log_error(stream->wrapper, REPORT_ERRORS, "%s", error);

0 commit comments

Comments
 (0)