From fff49b23fca69721dca669ff4fb3289bc7c24660 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Wed, 4 May 2022 14:51:35 +0200 Subject: Add FAIL_PRE_INIT_CALLS define This commit adds a new define FAIL_PRE_INIT_CALLS. When that define is set, calls to clock_gettime() that occur before ftpl_init() was called (due to being marked with __attribute__((constructor))) will just fail and return -1. After this commit, the test case added in the previous commit no longer hangs. To make this actually work, this new define is enabled by default. Fixes: https://github.com/wolfcw/libfaketime/issues/365 Signed-off-by: Uli Schlachter --- src/libfaketime.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/libfaketime.c') diff --git a/src/libfaketime.c b/src/libfaketime.c index f92ecf8..ec80ec8 100644 --- a/src/libfaketime.c +++ b/src/libfaketime.c @@ -2282,6 +2282,16 @@ int clock_gettime(clockid_t clk_id, struct timespec *tp) if (!initialized) { recursion_depth++; +#ifdef FAIL_PRE_INIT_CALLS + fprintf(stderr, "libfaketime: clock_gettime() was called before initialization.\n"); + fprintf(stderr, "libfaketime: Returning -1 on clock_gettime().\n"); + if (tp != NULL) + { + tp->tv_sec = 0; + tp->tv_nsec = 0; + } + return -1; +#else if (recursion_depth == 2) { fprintf(stderr, "libfaketime: Unexpected recursive calls to clock_gettime() without proper initialization. Trying alternative.\n"); @@ -2302,6 +2312,7 @@ int clock_gettime(clockid_t clk_id, struct timespec *tp) else { ftpl_init(); } +#endif recursion_depth--; } /* sanity check */ -- cgit v1.2.1