2013-08-19 11:47:49 +02:00
|
|
|
/*
|
2025-06-09 14:31:15 +02:00
|
|
|
* This file is part of libfaketime, version 0.9.12
|
2013-08-19 11:47:49 +02:00
|
|
|
*
|
2013-09-03 13:23:42 +02:00
|
|
|
* libfaketime is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License v2 as published by the
|
|
|
|
|
* Free Software Foundation.
|
2013-08-19 11:47:49 +02:00
|
|
|
*
|
2013-09-03 13:23:42 +02:00
|
|
|
* libfaketime is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
2013-08-19 11:47:49 +02:00
|
|
|
*
|
2013-09-03 13:23:42 +02:00
|
|
|
* You should have received a copy of the GNU General Public License v2 along
|
|
|
|
|
* with the libfaketime; if not, write to the Free Software Foundation,
|
|
|
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2013-08-19 11:47:49 +02:00
|
|
|
*/
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:23:42 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
* Global settings, includes, and macros === HEAD
|
|
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
2013-08-19 11:47:49 +02:00
|
|
|
|
|
|
|
|
#define _GNU_SOURCE /* required to get RTLD_NEXT defined */
|
|
|
|
|
|
2024-01-18 12:46:06 +09:00
|
|
|
#define _LARGEFILE64_SOURCE 1 /* required for stat64 on musl */
|
|
|
|
|
|
2013-08-19 11:47:49 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2013-08-19 14:08:51 +02:00
|
|
|
#include <stdint.h>
|
2013-08-23 02:41:21 +02:00
|
|
|
#include <stdbool.h>
|
2013-08-27 12:30:56 +02:00
|
|
|
#include <unistd.h>
|
2013-08-19 14:08:51 +02:00
|
|
|
#include <fcntl.h>
|
2013-08-28 19:32:26 +02:00
|
|
|
#include <poll.h>
|
2019-08-22 23:29:48 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
|
#include <sys/epoll.h>
|
2022-02-28 15:19:08 +01:00
|
|
|
#endif
|
|
|
|
|
#ifdef __GLIBC__
|
2022-02-26 11:07:38 +01:00
|
|
|
#ifndef __APPLE__
|
2022-02-25 21:25:58 +01:00
|
|
|
#include <gnu/libc-version.h>
|
2019-08-22 23:29:48 +02:00
|
|
|
#endif
|
2022-02-28 15:21:45 +01:00
|
|
|
#endif
|
2013-08-19 11:47:49 +02:00
|
|
|
#include <time.h>
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
2025-01-28 21:30:17 +01:00
|
|
|
#include <pthread.h>
|
2022-02-06 21:39:29 +01:00
|
|
|
#include <sys/time.h>
|
2022-02-20 17:39:17 +01:00
|
|
|
#include <utime.h>
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
#include <errno.h>
|
2013-08-19 11:47:49 +02:00
|
|
|
#include <string.h>
|
2013-08-19 14:08:51 +02:00
|
|
|
#include <semaphore.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <sys/stat.h>
|
2013-09-01 16:04:21 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <netinet/in.h>
|
2013-10-04 11:45:55 +02:00
|
|
|
#include <limits.h>
|
2021-02-24 12:36:38 -05:00
|
|
|
#ifdef INTERCEPT_SYSCALL
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <sys/syscall.h>
|
2025-05-13 15:01:47 +02:00
|
|
|
#ifdef INTERCEPT_FUTEX
|
|
|
|
|
#include <linux/futex.h>
|
|
|
|
|
#endif
|
2021-02-24 12:36:38 -05:00
|
|
|
#else
|
|
|
|
|
#error INTERCEPT_SYSCALL should only be defined on GNU/Linux systems.
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
2021-03-30 12:34:26 -04:00
|
|
|
#ifdef __linux__
|
|
|
|
|
#include <sys/timerfd.h>
|
|
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
|
2017-12-31 02:23:56 +01:00
|
|
|
#include "uthash.h"
|
|
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
#include "time_ops.h"
|
|
|
|
|
#include "faketime_common.h"
|
2026-01-26 14:43:43 +01:00
|
|
|
#include "ft_sem.h"
|
2013-08-24 18:59:20 +02:00
|
|
|
|
2021-08-01 08:41:17 -04:00
|
|
|
#if defined PTHREAD_SINGLETHREADED_TIME && defined FAKE_STATELESS
|
|
|
|
|
#undef PTHREAD_SINGLETHREADED_TIME
|
|
|
|
|
#endif
|
2017-12-31 02:23:56 +01:00
|
|
|
|
2013-08-19 11:47:49 +02:00
|
|
|
/* pthread-handling contributed by David North, TDI in version 0.7 */
|
2017-12-31 02:23:56 +01:00
|
|
|
#if defined PTHREAD_SINGLETHREADED_TIME || defined FAKE_PTHREAD
|
2013-08-19 11:47:49 +02:00
|
|
|
#include <pthread.h>
|
2022-07-25 09:53:06 -07:00
|
|
|
#ifdef __aarch64__
|
|
|
|
|
#define _SYS_TIME_H 1
|
|
|
|
|
#endif
|
2020-03-03 16:39:42 +01:00
|
|
|
#include <signal.h>
|
2013-08-19 11:47:49 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <sys/timeb.h>
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
|
|
#define BUFFERLEN 256
|
|
|
|
|
|
2013-10-04 22:26:54 +02:00
|
|
|
#ifndef __APPLE__
|
2013-10-28 22:58:03 +01:00
|
|
|
extern char *__progname;
|
2014-08-05 16:43:34 -07:00
|
|
|
#ifdef __sun
|
|
|
|
|
#include "sunos_endian.h"
|
|
|
|
|
#else
|
2013-10-04 22:26:54 +02:00
|
|
|
#include <endian.h>
|
2014-08-05 16:43:34 -07:00
|
|
|
#endif
|
2013-10-04 22:26:54 +02:00
|
|
|
#else
|
|
|
|
|
/* endianness related macros */
|
2017-05-19 18:55:11 +02:00
|
|
|
#ifndef OSSwapHostToBigInt64
|
2016-10-30 13:25:38 +01:00
|
|
|
#define OSSwapHostToBigInt64(x) ((uint64_t)(x))
|
2017-05-19 18:55:11 +02:00
|
|
|
#endif
|
2013-10-04 22:26:54 +02:00
|
|
|
#define htobe64(x) OSSwapHostToBigInt64(x)
|
2017-05-19 18:55:11 +02:00
|
|
|
#ifndef OSSwapHostToLittleInt64
|
2016-10-30 13:25:38 +01:00
|
|
|
#define OSSwapHostToLittleInt64(x) OSSwapInt64(x)
|
2017-05-19 18:55:11 +02:00
|
|
|
#endif
|
2013-10-04 22:26:54 +02:00
|
|
|
#define htole64(x) OSSwapHostToLittleInt64(x)
|
2017-05-19 18:55:11 +02:00
|
|
|
#ifndef OSSwapBigToHostInt64
|
2016-10-30 13:25:38 +01:00
|
|
|
#define OSSwapBigToHostInt64(x) ((uint64_t)(x))
|
2017-05-19 18:55:11 +02:00
|
|
|
#endif
|
2013-10-04 22:26:54 +02:00
|
|
|
#define be64toh(x) OSSwapBigToHostInt64(x)
|
2017-05-19 18:55:11 +02:00
|
|
|
#ifndef OSSwapLittleToHostInt64
|
2016-10-30 13:25:38 +01:00
|
|
|
#define OSSwapLittleToHostInt64(x) OSSwapInt64(x)
|
2017-05-19 18:55:11 +02:00
|
|
|
#endif
|
2013-10-04 22:26:54 +02:00
|
|
|
#define le64toh(x) OSSwapLittleToHostInt64(x)
|
|
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
/* clock_gettime() and related clock definitions are missing on __APPLE__ */
|
|
|
|
|
#ifndef CLOCK_REALTIME
|
|
|
|
|
/* from GNU C Library time.h */
|
|
|
|
|
/* Identifier for system-wide realtime clock. ( == 1) */
|
|
|
|
|
#define CLOCK_REALTIME CALENDAR_CLOCK
|
|
|
|
|
/* Monotonic system-wide clock. (== 0) */
|
|
|
|
|
#define CLOCK_MONOTONIC SYSTEM_CLOCK
|
|
|
|
|
/* High-resolution timer from the CPU. */
|
|
|
|
|
#define CLOCK_PROCESS_CPUTIME_ID 2
|
|
|
|
|
/* Thread-specific CPU-time clock. */
|
|
|
|
|
#define CLOCK_THREAD_CPUTIME_ID 3
|
|
|
|
|
/* Monotonic system-wide clock, not adjusted for frequency scaling. */
|
|
|
|
|
#define CLOCK_MONOTONIC_RAW 4
|
|
|
|
|
typedef int clockid_t;
|
|
|
|
|
#include <mach/clock.h>
|
|
|
|
|
#include <mach/mach.h>
|
|
|
|
|
#endif
|
2022-02-06 21:39:29 +01:00
|
|
|
|
|
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
void do_macos_dyld_interpose(void);
|
|
|
|
|
#define DYLD_INTERPOSE(_new,_target) \
|
|
|
|
|
__attribute__((used)) static struct{ const void* new; const void* target; } _interpose_##_target \
|
|
|
|
|
__attribute__ ((section ("__DATA,__interpose"))) = { (const void*)(unsigned long)&_new, (const void*)(unsigned long)&_target };
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
#endif
|
|
|
|
|
|
2013-10-20 17:16:16 +02:00
|
|
|
/* some systems lack raw clock */
|
|
|
|
|
#ifndef CLOCK_MONOTONIC_RAW
|
2013-10-30 21:47:56 +01:00
|
|
|
#define CLOCK_MONOTONIC_RAW (CLOCK_MONOTONIC + 1)
|
2013-10-20 17:16:16 +02:00
|
|
|
#endif
|
|
|
|
|
|
2020-07-28 17:34:38 -07:00
|
|
|
#if defined FAKE_UTIME && !defined FAKE_FILE_TIMESTAMPS
|
|
|
|
|
#define FAKE_FILE_TIMESTAMPS
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-12-14 21:49:23 +01:00
|
|
|
#ifdef FAKE_FILE_TIMESTAMPS
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifndef __APPLE__
|
2019-12-14 21:49:23 +01:00
|
|
|
struct utimbuf {
|
|
|
|
|
time_t actime; /* access time */
|
|
|
|
|
time_t modtime; /* modification time */
|
|
|
|
|
};
|
|
|
|
|
#endif
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2019-12-14 21:49:23 +01:00
|
|
|
|
2020-11-15 21:57:10 +01:00
|
|
|
#ifdef FAKE_RANDOM
|
|
|
|
|
#include <sys/random.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-01-12 22:23:16 +00:00
|
|
|
/* __timespec64 is needed for clock_gettime64 on 32-bit architectures */
|
|
|
|
|
struct __timespec64
|
|
|
|
|
{
|
|
|
|
|
uint64_t tv_sec; /* Seconds */
|
Fix interposition of clock_gettime64
timespec.tv_nsec is 32-bit, even though timeval.tv_usec is
64-bit (weirdly). This doesn't matter very much in practice because
* on little endian architectures (which is all our 32-bit release
arches) writing to a too big integer ends up writing the
desired value in the desired location, and
* it doesn't affect the overall struct size on any of our actual
architectures (which align the uint64_t to 8 so must make the
whole struct 16 not 12), so the write overflow is harmless.
> #include <time.h>
> #include <sys/time.h>
> #include <stdio.h>
> struct timeval tv;
> struct timespec ts;
> int main(void) {
> printf("time_t %lld\n", (unsigned long long) sizeof(time_t));
> printf("timeval %lld %lld %lld\n",
> (unsigned long long) sizeof(tv),
> (unsigned long long) sizeof(tv.tv_sec),
> (unsigned long long) sizeof(tv.tv_usec)
> );
> printf("timespec %lld %lld %lld\n",
> (unsigned long long) sizeof(ts),
> (unsigned long long) sizeof(ts.tv_sec),
> (unsigned long long) sizeof(ts.tv_nsec)
> );
> }
> (sid_armhf-dchroot)iwj@amdahl:~/Faketime/test$ gcc t.c
> (sid_armhf-dchroot)iwj@amdahl:~/Faketime/test$ ./a.out
> time_t 8
> timeval 16 8 8
> timespec 16 8 4
> (sid_armhf-dchroot)iwj@amdahl:~/Faketime/test$
2025-01-17 12:05:09 +00:00
|
|
|
uint32_t tv_nsec; /* this is 32-bit, apparently! */
|
2025-01-12 22:23:16 +00:00
|
|
|
};
|
|
|
|
|
|
2025-01-17 12:08:23 +00:00
|
|
|
/* __timespec64 is needed for clock_gettime64 on 32-bit architectures */
|
|
|
|
|
struct __timeval64
|
|
|
|
|
{
|
|
|
|
|
uint64_t tv_sec; /* Seconds */
|
|
|
|
|
uint64_t tv_usec; /* this is 64-bit, apparently! */
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/*
|
|
|
|
|
* Per thread variable, which we turn on inside real_* calls to avoid modifying
|
2013-10-28 22:58:03 +01:00
|
|
|
* time multiple times of for the whole process to prevent faking time
|
2013-09-01 16:04:21 +02:00
|
|
|
*/
|
2014-12-11 08:13:40 +01:00
|
|
|
static __thread bool dont_fake = false;
|
2013-09-01 16:04:21 +02:00
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/* Wrapper for function calls, which we want to return system time */
|
2013-11-07 19:35:18 +01:00
|
|
|
#define DONT_FAKE_TIME(call) \
|
2021-09-17 10:41:55 -04:00
|
|
|
do { \
|
2013-11-07 19:35:18 +01:00
|
|
|
bool dont_fake_orig = dont_fake; \
|
|
|
|
|
if (!dont_fake) \
|
|
|
|
|
{ \
|
|
|
|
|
dont_fake = true; \
|
|
|
|
|
} \
|
|
|
|
|
call; \
|
|
|
|
|
dont_fake = dont_fake_orig; \
|
2013-09-01 16:04:21 +02:00
|
|
|
} while (0)
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/* pointers to real (not faked) functions */
|
2022-02-16 12:01:35 +02:00
|
|
|
static int (*real_stat) (const char *, struct stat *);
|
|
|
|
|
static int (*real_fstat) (int, struct stat *);
|
|
|
|
|
static int (*real_lstat) (const char *, struct stat *);
|
2022-02-16 11:27:19 +02:00
|
|
|
static int (*real_xstat) (int, const char *, struct stat *);
|
|
|
|
|
static int (*real_fxstat) (int, int, struct stat *);
|
|
|
|
|
static int (*real_fxstatat) (int, int, const char *, struct stat *, int);
|
|
|
|
|
static int (*real_lxstat) (int, const char *, struct stat *);
|
libfaketime.c: get rid of stat64 things on aarch64-darwin
To give more context, stat64 is a child of large-file support (LFS)
back in 1996, during the transition from 32-bit to 64-bit. People
wanted 64-bit inodes in 32-bit systems, hence stat and stat64.
Nowadays where everything is 64-bit, stat64 is mostly just an alias
to stat, as stat is already native 64-bit. On modern implementations
like musl, stat64 is even dropped entirely as a sane default. We
observe the same in darwin's stat.h:
#if !__DARWIN_ONLY_64_BIT_INO_T
struct stat64 __DARWIN_STRUCT_STAT64;
#endif /* !__DARWIN_ONLY_64_BIT_INO_T */
Because struct stat64 doesn't ever exist on aarch64-darwin, and we
don't have to worry about people using stat64 calls, we can safely
remove all stat64 bloat, according to __DARWIN_ONLY_64_BIT_INO_T.
I nuked fake_stat64buf because only STAT64_HANDLER is using it, and
only non-darwin stat64 things use that handler. I didn't do more
because people might still use stat64 things on x86_64 (on glibc)
and other older 32-bit platforms, and we still need to hook those.
A loose follow up to PR #453. Fixes the remaining clang warnings on
aarch64-darwin.
2025-06-08 18:33:33 +08:00
|
|
|
#if !defined(__APPLE__) || !__DARWIN_ONLY_64_BIT_INO_T
|
Handle stat64() call
This fixes missing modification of timestamps in stat() calls for
programs built with large file support (-D_FILE_OFFSET_BITS=64), both
32- and 64-bit.
Demo code:
$ cat <<EOF >test.c
#include <sys/stat.h>
int main()
{
struct stat buf;
return stat("/", &buf);
}
EOF
32-bit build:
$ nix-shell -p gcc --argstr system i686-linux
nix-shell$ gcc test.c && ltrace ./a.out
__libc_start_main([ "./a.out" ] <unfinished ...>
stat(0x804a008, 0xffa4b644, 895, 0) = 0
+++ exited (status 0) +++
nix-shell$ gcc -D_FILE_OFFSET_BITS=64 test.c && ltrace ./a.out
__libc_start_main([ "./a.out" ] <unfinished ...>
stat64(0x804a008, 0xffdcf61c, 100, 0xffdcfaeb) = 0
+++ exited (status 0) +++
nix-shell$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, [...]
64-bit build:
$ nix-shell -p gcc
nix-shell$ gcc test.c && ltrace ./a.out
stat(0x402004, 0x7ffc50a9d740, 0x7ffc50a9d908, 0x403db0) = 0
+++ exited (status 0) +++
nix-shell$ gcc -D_FILE_OFFSET_BITS=64 test.c && ltrace ./a.out
stat64(0x402004, 0x7ffd5cbafba0, 0x7ffd5cbafd68, 0x403db0) = 0
+++ exited (status 0) +++
nix-shell$ file a.out
a.out: ELF 64-bit LSB executable, x86-64, [...]
2025-07-09 17:32:54 +02:00
|
|
|
static int (*real_stat64) (const char *, struct stat64 *);
|
2022-02-16 11:27:19 +02:00
|
|
|
static int (*real_xstat64) (int, const char *, struct stat64 *);
|
|
|
|
|
static int (*real_fxstat64) (int, int , struct stat64 *);
|
|
|
|
|
static int (*real_fxstatat64) (int, int , const char *, struct stat64 *, int);
|
|
|
|
|
static int (*real_lxstat64) (int, const char *, struct stat64 *);
|
libfaketime.c: get rid of stat64 things on aarch64-darwin
To give more context, stat64 is a child of large-file support (LFS)
back in 1996, during the transition from 32-bit to 64-bit. People
wanted 64-bit inodes in 32-bit systems, hence stat and stat64.
Nowadays where everything is 64-bit, stat64 is mostly just an alias
to stat, as stat is already native 64-bit. On modern implementations
like musl, stat64 is even dropped entirely as a sane default. We
observe the same in darwin's stat.h:
#if !__DARWIN_ONLY_64_BIT_INO_T
struct stat64 __DARWIN_STRUCT_STAT64;
#endif /* !__DARWIN_ONLY_64_BIT_INO_T */
Because struct stat64 doesn't ever exist on aarch64-darwin, and we
don't have to worry about people using stat64 calls, we can safely
remove all stat64 bloat, according to __DARWIN_ONLY_64_BIT_INO_T.
I nuked fake_stat64buf because only STAT64_HANDLER is using it, and
only non-darwin stat64 things use that handler. I didn't do more
because people might still use stat64 things on x86_64 (on glibc)
and other older 32-bit platforms, and we still need to hook those.
A loose follow up to PR #453. Fixes the remaining clang warnings on
aarch64-darwin.
2025-06-08 18:33:33 +08:00
|
|
|
#endif
|
2023-07-30 20:55:48 -04:00
|
|
|
#ifdef STATX_TYPE
|
|
|
|
|
static int (*real_statx) (int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf);
|
|
|
|
|
#endif
|
2013-09-03 13:23:42 +02:00
|
|
|
static time_t (*real_time) (time_t *);
|
|
|
|
|
static int (*real_ftime) (struct timeb *);
|
|
|
|
|
static int (*real_gettimeofday) (struct timeval *, void *);
|
|
|
|
|
static int (*real_clock_gettime) (clockid_t clk_id, struct timespec *tp);
|
2025-01-12 22:23:16 +00:00
|
|
|
static int (*real_clock_gettime64) (clockid_t clk_id, struct __timespec64 *tp);
|
2022-12-20 00:35:53 +08:00
|
|
|
#ifdef TIME_UTC
|
2021-07-30 13:46:24 -04:00
|
|
|
static int (*real_timespec_get) (struct timespec *ts, int base);
|
2022-12-20 00:35:53 +08:00
|
|
|
#endif
|
2013-10-16 09:33:50 +02:00
|
|
|
#ifdef FAKE_INTERNAL_CALLS
|
2013-10-16 09:16:05 +02:00
|
|
|
static int (*real___ftime) (struct timeb *);
|
|
|
|
|
static int (*real___gettimeofday) (struct timeval *, void *);
|
|
|
|
|
static int (*real___clock_gettime) (clockid_t clk_id, struct timespec *tp);
|
2013-10-16 09:33:50 +02:00
|
|
|
#endif
|
2017-12-31 02:23:56 +01:00
|
|
|
#ifdef FAKE_PTHREAD
|
|
|
|
|
static int (*real_pthread_cond_timedwait_225) (pthread_cond_t *, pthread_mutex_t*, struct timespec *);
|
|
|
|
|
static int (*real_pthread_cond_timedwait_232) (pthread_cond_t *, pthread_mutex_t*, struct timespec *);
|
|
|
|
|
static int (*real_pthread_cond_init_232) (pthread_cond_t *restrict, const pthread_condattr_t *restrict);
|
|
|
|
|
static int (*real_pthread_cond_destroy_232) (pthread_cond_t *);
|
2019-05-31 22:27:10 +02:00
|
|
|
static pthread_rwlock_t monotonic_conds_lock;
|
2017-12-31 02:23:56 +01:00
|
|
|
#endif
|
|
|
|
|
|
2025-06-07 20:28:33 +08:00
|
|
|
#ifndef __APPLE__
|
2013-09-04 14:20:43 +02:00
|
|
|
#ifdef FAKE_TIMERS
|
2013-09-30 16:36:13 +02:00
|
|
|
static int (*real_timer_settime_22) (int timerid, int flags, const struct itimerspec *new_value,
|
2013-10-30 21:47:56 +01:00
|
|
|
struct itimerspec * old_value);
|
2013-09-30 16:36:13 +02:00
|
|
|
static int (*real_timer_settime_233) (timer_t timerid, int flags,
|
|
|
|
|
const struct itimerspec *new_value,
|
|
|
|
|
struct itimerspec * old_value);
|
|
|
|
|
static int (*real_timer_gettime_22) (int timerid,
|
|
|
|
|
struct itimerspec *curr_value);
|
|
|
|
|
static int (*real_timer_gettime_233) (timer_t timerid,
|
|
|
|
|
struct itimerspec *curr_value);
|
2021-03-30 12:34:26 -04:00
|
|
|
static int (*real_timerfd_settime) (int fd, int flags,
|
|
|
|
|
const struct itimerspec *new_value,
|
|
|
|
|
struct itimerspec *old_value);
|
|
|
|
|
static int (*real_timerfd_gettime) (int fd,
|
|
|
|
|
struct itimerspec *curr_value);
|
2013-09-03 17:13:16 +02:00
|
|
|
#endif
|
2013-09-04 14:20:43 +02:00
|
|
|
#endif
|
|
|
|
|
#ifdef FAKE_SLEEP
|
2013-09-03 13:23:42 +02:00
|
|
|
static int (*real_nanosleep) (const struct timespec *req, struct timespec *rem);
|
2019-08-21 18:04:48 +02:00
|
|
|
#ifndef __APPLE__
|
2019-09-05 22:52:07 +02:00
|
|
|
static int (*real_clock_nanosleep) (clockid_t clock_id, int flags, const struct timespec *req, struct timespec *rem);
|
2019-08-21 18:04:48 +02:00
|
|
|
#endif
|
2013-09-03 13:23:42 +02:00
|
|
|
static int (*real_usleep) (useconds_t usec);
|
|
|
|
|
static unsigned int (*real_sleep) (unsigned int seconds);
|
|
|
|
|
static unsigned int (*real_alarm) (unsigned int seconds);
|
|
|
|
|
static int (*real_poll) (struct pollfd *, nfds_t, int);
|
|
|
|
|
static int (*real_ppoll) (struct pollfd *, nfds_t, const struct timespec *, const sigset_t *);
|
2019-08-22 23:29:48 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
|
static int (*real_epoll_wait) (int epfd, struct epoll_event *events, int maxevents, int timeout);
|
|
|
|
|
static int (*real_epoll_pwait) (int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *sigmask);
|
|
|
|
|
#endif
|
2017-05-18 17:52:03 -07:00
|
|
|
static int (*real_select) (int nfds, fd_set *restrict readfds,
|
|
|
|
|
fd_set *restrict writefds,
|
|
|
|
|
fd_set *restrict errorfds,
|
|
|
|
|
struct timeval *restrict timeout);
|
2019-08-22 23:29:48 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
|
static int (*real_pselect) (int nfds, fd_set *restrict readfds,
|
|
|
|
|
fd_set *restrict writefds,
|
|
|
|
|
fd_set *restrict errorfds,
|
|
|
|
|
const struct timespec *timeout,
|
|
|
|
|
const sigset_t *sigmask);
|
|
|
|
|
#endif
|
2014-08-26 00:18:02 -04:00
|
|
|
static int (*real_sem_timedwait) (sem_t*, const struct timespec*);
|
2022-05-11 21:47:35 +02:00
|
|
|
static int (*real_sem_clockwait) (sem_t *sem, clockid_t clockid, const struct timespec *abstime);
|
2013-09-04 14:20:43 +02:00
|
|
|
#endif
|
2025-06-07 20:28:33 +08:00
|
|
|
#ifdef __APPLE__
|
2013-09-03 13:23:42 +02:00
|
|
|
static int (*real_clock_get_time) (clock_serv_t clock_serv, mach_timespec_t *cur_timeclockid_t);
|
2013-09-05 11:15:51 +02:00
|
|
|
static int apple_clock_gettime (clockid_t clk_id, struct timespec *tp);
|
2013-09-01 16:04:21 +02:00
|
|
|
static clock_serv_t clock_serv_real;
|
|
|
|
|
#endif
|
2013-09-03 13:23:42 +02:00
|
|
|
|
2019-12-14 21:49:23 +01:00
|
|
|
#ifdef FAKE_FILE_TIMESTAMPS
|
|
|
|
|
static int (*real_utimes) (const char *filename, const struct timeval times[2]);
|
|
|
|
|
static int (*real_utime) (const char *filename, const struct utimbuf *times);
|
2020-07-28 17:34:38 -07:00
|
|
|
static int (*real_utimensat) (int dirfd, const char *filename, const struct timespec times[2], int flags);
|
|
|
|
|
static int (*real_futimens) (int fd, const struct timespec times[2]);
|
2019-12-14 21:49:23 +01:00
|
|
|
#endif
|
|
|
|
|
|
2020-11-15 21:57:10 +01:00
|
|
|
#ifdef FAKE_RANDOM
|
|
|
|
|
static ssize_t (*real_getrandom) (void *buf, size_t buflen, unsigned int flags);
|
2021-02-24 16:14:02 -05:00
|
|
|
static int (*real_getentropy) (void *buffer, size_t length);
|
2020-11-15 21:57:10 +01:00
|
|
|
#endif
|
2021-02-23 21:42:42 -05:00
|
|
|
#ifdef FAKE_PID
|
|
|
|
|
static pid_t (*real_getpid) ();
|
|
|
|
|
#endif
|
2020-11-15 21:57:10 +01:00
|
|
|
|
2021-02-24 12:36:38 -05:00
|
|
|
#ifdef INTERCEPT_SYSCALL
|
|
|
|
|
static long (*real_syscall) (long, ...);
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-02-16 11:46:18 +02:00
|
|
|
static bool check_missing_real(const char *name, bool missing)
|
|
|
|
|
{
|
|
|
|
|
if (missing)
|
|
|
|
|
{ /* dlsym() failed */
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
(void) fprintf(stderr, "faketime problem: original %s not found.\n", name);
|
|
|
|
|
#else
|
|
|
|
|
(void) name; /* unused */
|
|
|
|
|
#endif
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
#define CHECK_MISSING_REAL(name) \
|
|
|
|
|
check_missing_real(#name, (NULL == real_##name))
|
|
|
|
|
|
2025-12-16 11:50:40 +01:00
|
|
|
static pthread_mutex_t initialized_once_mutex;
|
2025-01-21 18:44:42 +00:00
|
|
|
static pthread_once_t initialized_once_control = PTHREAD_ONCE_INIT;
|
2014-12-10 14:55:01 -07:00
|
|
|
|
2013-08-19 11:47:49 +02:00
|
|
|
/* prototypes */
|
2014-12-11 08:13:40 +01:00
|
|
|
static int fake_gettimeofday(struct timeval *tv);
|
|
|
|
|
static int fake_clock_gettime(clockid_t clk_id, struct timespec *tp);
|
2020-03-26 20:07:24 +01:00
|
|
|
int read_config_file();
|
2024-03-12 02:35:47 +01:00
|
|
|
bool str_array_contains(const char *haystack, const char *needle);
|
|
|
|
|
void *ft_dlvsym(void *handle, const char *symbol, const char *version, const char *full_name, char *ignore_list, bool should_debug_dlsym);
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2025-06-02 17:02:01 +02:00
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
/** Semaphore protecting shared data */
|
2025-06-02 12:49:28 +02:00
|
|
|
static ft_sem_t shared_sem;
|
|
|
|
|
static bool shared_sem_initialized = false;
|
2013-09-01 16:04:21 +02:00
|
|
|
|
|
|
|
|
/** Data shared among faketime-spawned processes */
|
|
|
|
|
static struct ft_shared_s *ft_shared = NULL;
|
|
|
|
|
|
2021-09-21 20:52:08 +00:00
|
|
|
/** Storage format for timestamps written to file. Big endian. */
|
2013-09-04 12:50:00 +02:00
|
|
|
struct saved_timestamp
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
int64_t sec;
|
|
|
|
|
uint64_t nsec;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static inline void timespec_from_saved (struct timespec *tp,
|
2013-10-30 21:47:56 +01:00
|
|
|
struct saved_timestamp *saved)
|
2013-09-01 16:04:21 +02:00
|
|
|
{
|
|
|
|
|
/* read as big endian */
|
2013-10-04 22:26:54 +02:00
|
|
|
tp->tv_sec = be64toh(saved->sec);
|
|
|
|
|
tp->tv_nsec = be64toh(saved->nsec);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Saved timestamps */
|
|
|
|
|
static struct saved_timestamp *stss = NULL;
|
|
|
|
|
static size_t infile_size;
|
|
|
|
|
static bool infile_set = false;
|
|
|
|
|
|
|
|
|
|
/** File fd to save timestamps to */
|
|
|
|
|
static int outfile = -1;
|
|
|
|
|
|
|
|
|
|
static bool limited_faking = false;
|
|
|
|
|
static long callcounter = 0;
|
|
|
|
|
static long ft_start_after_secs = -1;
|
|
|
|
|
static long ft_stop_after_secs = -1;
|
|
|
|
|
static long ft_start_after_ncalls = -1;
|
|
|
|
|
static long ft_stop_after_ncalls = -1;
|
|
|
|
|
|
|
|
|
|
static bool spawnsupport = false;
|
|
|
|
|
static int spawned = 0;
|
|
|
|
|
static char ft_spawn_target[1024];
|
|
|
|
|
static long ft_spawn_secs = -1;
|
|
|
|
|
static long ft_spawn_ncalls = -1;
|
|
|
|
|
|
2019-08-19 19:16:07 +02:00
|
|
|
#ifdef __ARM_ARCH
|
|
|
|
|
static int fake_monotonic_clock = 0;
|
|
|
|
|
#else
|
2014-07-24 16:54:08 -07:00
|
|
|
static int fake_monotonic_clock = 1;
|
2019-08-19 19:16:07 +02:00
|
|
|
#endif
|
2014-11-17 10:42:15 +01:00
|
|
|
static int cache_enabled = 1;
|
|
|
|
|
static int cache_duration = 10; /* cache fake time input for 10 seconds */
|
2019-09-03 12:01:33 +02:00
|
|
|
static int force_cache_expiration = 0;
|
2014-07-24 16:54:08 -07:00
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/*
|
2013-09-01 16:04:21 +02:00
|
|
|
* Static timespec to store our startup time, followed by a load-time library
|
|
|
|
|
* initialization declaration.
|
|
|
|
|
*/
|
2017-05-19 19:14:58 +02:00
|
|
|
#ifndef CLOCK_BOOTTIME
|
2013-09-01 16:04:21 +02:00
|
|
|
static struct system_time_s ftpl_starttime = {{0, -1}, {0, -1}, {0, -1}};
|
2019-08-22 00:49:21 +02:00
|
|
|
static struct system_time_s ftpl_timecache = {{0, -1}, {0, -1}, {0, -1}};
|
|
|
|
|
static struct system_time_s ftpl_faketimecache = {{0, -1}, {0, -1}, {0, -1}};
|
2017-05-19 19:14:58 +02:00
|
|
|
#else
|
|
|
|
|
static struct system_time_s ftpl_starttime = {{0, -1}, {0, -1}, {0, -1}, {0, -1}};
|
2019-08-22 00:49:21 +02:00
|
|
|
static struct system_time_s ftpl_timecache = {{0, -1}, {0, -1}, {0, -1}, {0, -1}};
|
|
|
|
|
static struct system_time_s ftpl_faketimecache = {{0, -1}, {0, -1}, {0, -1}, {0, -1}};
|
2017-05-19 19:14:58 +02:00
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
|
|
|
|
|
static char user_faked_time_fmt[BUFSIZ] = {0};
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/* User supplied base time to fake */
|
2013-09-01 16:04:21 +02:00
|
|
|
static struct timespec user_faked_time_timespec = {0, -1};
|
2013-09-03 13:23:42 +02:00
|
|
|
/* User supplied base time is set */
|
2013-09-01 16:04:21 +02:00
|
|
|
static bool user_faked_time_set = false;
|
2014-08-26 10:51:50 -04:00
|
|
|
static char user_faked_time_saved[BUFFERLEN] = {0};
|
|
|
|
|
|
2021-09-21 20:52:08 +00:00
|
|
|
/* Fractional user offset provided through FAKETIME env. var. */
|
2013-09-01 16:04:21 +02:00
|
|
|
static struct timespec user_offset = {0, -1};
|
2013-09-03 13:23:42 +02:00
|
|
|
/* Speed up or slow down clock */
|
2013-09-01 16:04:21 +02:00
|
|
|
static double user_rate = 1.0;
|
|
|
|
|
static bool user_rate_set = false;
|
|
|
|
|
static struct timespec user_per_tick_inc = {0, -1};
|
|
|
|
|
static bool user_per_tick_inc_set = false;
|
2013-10-28 22:58:03 +01:00
|
|
|
enum ft_mode_t {FT_FREEZE, FT_START_AT, FT_NOOP} ft_mode = FT_FREEZE;
|
2013-09-01 16:04:21 +02:00
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/* Time to fake is not provided through FAKETIME env. var. */
|
2013-09-01 16:04:21 +02:00
|
|
|
static bool parse_config_file = true;
|
2013-08-19 14:08:51 +02:00
|
|
|
|
2018-01-02 00:15:27 +01:00
|
|
|
static void ft_cleanup (void) __attribute__ ((destructor));
|
|
|
|
|
static void ftpl_init (void) __attribute__ ((constructor));
|
2013-08-19 14:08:51 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:23:42 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
* Shared memory related functions === SHM
|
|
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
2026-01-26 13:31:55 +01:00
|
|
|
static void system_time_to_shared(const struct system_time_s *src,
|
|
|
|
|
struct ft_shared_s *dst)
|
|
|
|
|
{
|
|
|
|
|
dst->start_time_real.sec = (int64_t)src->real.tv_sec;
|
|
|
|
|
dst->start_time_real.nsec = (int64_t)src->real.tv_nsec;
|
|
|
|
|
dst->start_time_mon.sec = (int64_t)src->mon.tv_sec;
|
|
|
|
|
dst->start_time_mon.nsec = (int64_t)src->mon.tv_nsec;
|
|
|
|
|
dst->start_time_mon_raw.sec = (int64_t)src->mon_raw.tv_sec;
|
|
|
|
|
dst->start_time_mon_raw.nsec = (int64_t)src->mon_raw.tv_nsec;
|
|
|
|
|
#ifdef CLOCK_BOOTTIME
|
|
|
|
|
dst->start_time_boot.sec = (int64_t)src->boot.tv_sec;
|
|
|
|
|
dst->start_time_boot.nsec = (int64_t)src->boot.tv_nsec;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void shared_to_system_time(const struct ft_shared_s *src,
|
|
|
|
|
struct system_time_s *dst)
|
|
|
|
|
{
|
|
|
|
|
dst->real.tv_sec = (time_t)src->start_time_real.sec;
|
|
|
|
|
dst->real.tv_nsec = (long)src->start_time_real.nsec;
|
|
|
|
|
dst->mon.tv_sec = (time_t)src->start_time_mon.sec;
|
|
|
|
|
dst->mon.tv_nsec = (long)src->start_time_mon.nsec;
|
|
|
|
|
dst->mon_raw.tv_sec = (time_t)src->start_time_mon_raw.sec;
|
|
|
|
|
dst->mon_raw.tv_nsec = (long)src->start_time_mon_raw.nsec;
|
|
|
|
|
#ifdef CLOCK_BOOTTIME
|
|
|
|
|
dst->boot.tv_sec = (time_t)src->start_time_boot.sec;
|
|
|
|
|
dst->boot.tv_nsec = (long)src->start_time_boot.nsec;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 19:48:29 +02:00
|
|
|
static bool shmCreator = false;
|
|
|
|
|
|
2019-08-13 19:35:45 +02:00
|
|
|
static void ft_shm_create(void) {
|
2019-08-18 19:06:52 +02:00
|
|
|
char sem_name[256], shm_name[256], sem_nameT[256], shm_nameT[256];
|
2019-08-13 19:35:45 +02:00
|
|
|
int shm_fdN;
|
2025-06-02 12:49:28 +02:00
|
|
|
ft_sem_t semN;
|
2019-08-13 19:35:45 +02:00
|
|
|
struct ft_shared_s *ft_sharedN;
|
|
|
|
|
char shared_objsN[513];
|
2025-06-02 12:49:28 +02:00
|
|
|
ft_sem_t shared_semT;
|
2021-02-25 10:38:48 -05:00
|
|
|
pid_t pid;
|
2019-08-13 19:35:45 +02:00
|
|
|
|
2021-02-25 10:38:48 -05:00
|
|
|
#ifdef FAKE_PID
|
|
|
|
|
pid = real_getpid();
|
|
|
|
|
#else
|
|
|
|
|
pid = getpid();
|
|
|
|
|
#endif
|
|
|
|
|
snprintf(sem_name, 255, "/faketime_sem_%ld", (long)pid);
|
|
|
|
|
snprintf(shm_name, 255, "/faketime_shm_%ld", (long)pid);
|
2025-06-02 12:49:28 +02:00
|
|
|
if (-1 == ft_sem_create(sem_name, &semN))
|
|
|
|
|
{ /* silently fail on platforms that do not support semaphores */
|
2019-09-05 22:52:07 +02:00
|
|
|
return;
|
2019-08-13 19:35:45 +02:00
|
|
|
}
|
|
|
|
|
/* create shm */
|
|
|
|
|
if (-1 == (shm_fdN = shm_open(shm_name, O_CREAT|O_EXCL|O_RDWR, S_IWUSR|S_IRUSR)))
|
2019-09-05 22:52:07 +02:00
|
|
|
{
|
2019-08-16 15:53:47 +02:00
|
|
|
perror("libfaketime: In ft_shm_create(), shm_open failed");
|
2019-08-13 19:35:45 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
/* set shm size */
|
2026-01-26 13:31:55 +01:00
|
|
|
if (-1 == ftruncate(shm_fdN, sizeof(struct ft_shared_s)))
|
2019-08-13 19:35:45 +02:00
|
|
|
{
|
2019-08-16 15:53:47 +02:00
|
|
|
perror("libfaketime: In ft_shm_create(), ftruncate failed");
|
2019-08-13 19:35:45 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2019-09-05 22:52:07 +02:00
|
|
|
}
|
2019-08-13 19:35:45 +02:00
|
|
|
/* map shm */
|
|
|
|
|
if (MAP_FAILED == (ft_sharedN = mmap(NULL, sizeof(struct ft_shared_s), PROT_READ|PROT_WRITE,
|
|
|
|
|
MAP_SHARED, shm_fdN, 0)))
|
|
|
|
|
{
|
2019-08-16 15:53:47 +02:00
|
|
|
perror("libfaketime: In ft_shm_create(), mmap failed");
|
2019-08-13 19:35:45 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_lock(&semN) == -1)
|
2019-08-13 19:35:45 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In ft_shm_create(), ft_sem_lock failed");
|
2019-08-13 19:35:45 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
/* init elapsed time ticks to zero */
|
|
|
|
|
ft_sharedN->ticks = 0;
|
|
|
|
|
ft_sharedN->file_idx = 0;
|
2026-01-26 13:31:55 +01:00
|
|
|
ft_sharedN->start_time_real.sec = 0;
|
|
|
|
|
ft_sharedN->start_time_real.nsec = -1;
|
|
|
|
|
ft_sharedN->start_time_mon.sec = 0;
|
|
|
|
|
ft_sharedN->start_time_mon.nsec = -1;
|
|
|
|
|
ft_sharedN->start_time_mon_raw.sec = 0;
|
|
|
|
|
ft_sharedN->start_time_mon_raw.nsec = -1;
|
|
|
|
|
#ifdef CLOCK_BOOTTIME
|
|
|
|
|
ft_sharedN->start_time_boot.sec = 0;
|
|
|
|
|
ft_sharedN->start_time_boot.nsec = -1;
|
|
|
|
|
#endif
|
2019-08-13 19:35:45 +02:00
|
|
|
|
|
|
|
|
if (-1 == munmap(ft_sharedN, (sizeof(struct ft_shared_s))))
|
|
|
|
|
{
|
2019-08-23 14:46:06 +02:00
|
|
|
perror("libfaketime: In ft_shm_create(), munmap failed");
|
2019-08-13 19:35:45 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_unlock(&semN) == -1)
|
2019-08-13 19:35:45 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In ft_shm_create(), ft_sem_unlock failed");
|
2019-08-13 19:35:45 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2019-09-05 22:52:07 +02:00
|
|
|
}
|
2019-08-13 19:35:45 +02:00
|
|
|
|
|
|
|
|
snprintf(shared_objsN, sizeof(shared_objsN), "%s %s", sem_name, shm_name);
|
2019-08-18 19:06:52 +02:00
|
|
|
|
|
|
|
|
int semSafetyCheckPassed = 0;
|
2025-06-02 12:49:28 +02:00
|
|
|
ft_sem_close(&semN);
|
2019-08-13 19:48:29 +02:00
|
|
|
|
2019-08-18 19:06:52 +02:00
|
|
|
sscanf(shared_objsN, "%255s %255s", sem_nameT, shm_nameT);
|
2025-06-02 12:49:28 +02:00
|
|
|
if (-1 == ft_sem_open(sem_nameT, &shared_semT))
|
2019-08-18 19:06:52 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
fprintf(stderr, "libfaketime: In ft_shm_create(), non-fatal ft_sem_open issue with %s\n", sem_nameT);
|
2019-08-18 19:06:52 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
semSafetyCheckPassed = 1;
|
2025-06-02 12:49:28 +02:00
|
|
|
ft_sem_close(&shared_semT);
|
2019-08-18 19:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (semSafetyCheckPassed == 1) {
|
|
|
|
|
setenv("FAKETIME_SHARED", shared_objsN, true);
|
|
|
|
|
shmCreator = true;
|
|
|
|
|
}
|
2019-08-13 19:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ft_shm_destroy(void)
|
|
|
|
|
{
|
|
|
|
|
char sem_name[256], shm_name[256], *ft_shared_env = getenv("FAKETIME_SHARED");
|
2025-06-02 12:49:28 +02:00
|
|
|
ft_sem_t ft_sem;
|
2019-08-13 19:48:29 +02:00
|
|
|
|
|
|
|
|
if (ft_shared_env != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (sscanf(ft_shared_env, "%255s %255s", sem_name, shm_name) < 2)
|
|
|
|
|
{
|
2019-08-23 14:46:06 +02:00
|
|
|
printf("libfaketime: In ft_shm_destroy(), error parsing semaphore name and shared memory id from string: %s", ft_shared_env);
|
2019-08-13 19:48:29 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
2019-08-20 09:53:22 +02:00
|
|
|
/*
|
|
|
|
|
To avoid shared memory / semaphores left after quitting, we have to clean
|
|
|
|
|
up here similar to how the faketime wrapper does.
|
|
|
|
|
However, there is no guarantee that all child processes have quit before
|
|
|
|
|
we clean up here, which potentially leaves us in a stale state.
|
|
|
|
|
Since there is no easy solution for this problem (see issue #56),
|
|
|
|
|
ft_shm_init() below at least tries to handle this carefully.
|
|
|
|
|
*/
|
2025-06-02 12:49:28 +02:00
|
|
|
if (0 == ft_sem_open(sem_name, &ft_sem))
|
|
|
|
|
{
|
Only let the owner clean up semaphore and shared memory
I noticed a bug in the semaphore handling, when using the System V semaphore
backend:
$ LD_PRELOAD=./src/libfaketime.so.1 bash -c "echo foo | sed s/foo/bar/"
libfaketime: In lock_for_stat(), ft_sem_lock failed: Invalid argument
[...exited with error...]
(Beware, the above command-line is not 100% deterministic; sometimes it
succeeds.)
Looking at the strace for the above command-line, it seems the bash echo
builtin process (or thread?) decides to remove the semaphore upon
exiting, while it's still in use by the sed process. sed then gets
EINVAL error ("Invalid argument") on its next semop call.
The root cause is a semantic difference between POSIX sem_unlink and
SysV semop(..., IPC_RMID), the two implementations for ft_sem_unlink:
* sem_unlink allows the semaphore to be used afterwards, as long as a
process has a reference to the semaphore.
* semop(..., IPC_RMID) removes the semaphore immediately, and further
use results in EINVAL error.
AFAICT, the simplest fix is to only let the owner of the semaphore (and
shared memory) do the clean up, which is what this patch does. Both
semaphore backends pass the tests with this change.
2025-09-26 20:01:43 +02:00
|
|
|
// Only delete the semaphore (and shared memory) if we're the owner.
|
|
|
|
|
pid_t sem_owner_pid;
|
|
|
|
|
int num_matched = sscanf(sem_name, "/faketime_sem_%d", &sem_owner_pid);
|
|
|
|
|
if (num_matched != 1)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "libfaketime: failed to parse semaphore owner pid from sem_name %s\n", sem_name);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
if (getpid() == sem_owner_pid)
|
|
|
|
|
{
|
|
|
|
|
ft_sem_unlink(&ft_sem);
|
|
|
|
|
shm_unlink(shm_name);
|
|
|
|
|
unsetenv("FAKETIME_SHARED");
|
|
|
|
|
}
|
2025-06-02 12:49:28 +02:00
|
|
|
}
|
2019-08-13 19:48:29 +02:00
|
|
|
}
|
2019-08-13 19:35:45 +02:00
|
|
|
}
|
|
|
|
|
|
2025-12-16 16:30:25 +01:00
|
|
|
static void ft_initialize_errorcheck_mutex (pthread_mutex_t* mutex)
|
|
|
|
|
{
|
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
|
int ret = pthread_mutexattr_init(&attr);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
fprintf(stderr, "libfaketime: failed to initialize mutex attribute: %d\n", ret);
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
fprintf(stderr, "libfaketime: failed to set errorcheck mutex attribute: %d\n", ret);
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
ret = pthread_mutex_init(mutex, &attr);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
fprintf(stderr, "libfaketime: failed to initialize errorcheck mutex: %d\n", ret);
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ft_init_once_generic (bool* init_done, pthread_once_t* once_control, pthread_mutex_t* mutex, void (*init_mutex_cb)(void), void (*initializer_cb)(void))
|
|
|
|
|
{
|
|
|
|
|
pthread_once(once_control, init_mutex_cb);
|
|
|
|
|
int ret = pthread_mutex_lock(mutex);
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
if (!*init_done) {
|
|
|
|
|
// Set this to `true` before we call the initialisation; the effect is that
|
|
|
|
|
// recursive calls to `ftpl_init` or `ft_shm_really_init` will be suppressed.
|
|
|
|
|
// If anything that they use calls back to a time function
|
|
|
|
|
// it will get some not- or partially-set-up faketime state.
|
|
|
|
|
// We are betting that that's good enough.
|
|
|
|
|
// (Empirically, on some platforms the shm functions call `statx`;
|
|
|
|
|
// we think the timestamps in that call probably don't matter.)
|
|
|
|
|
*init_done = true;
|
|
|
|
|
initializer_cb();
|
|
|
|
|
}
|
|
|
|
|
pthread_mutex_unlock(mutex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-16 11:50:40 +01:00
|
|
|
static pthread_mutex_t ft_shm_initialized_once_mutex;
|
2025-01-21 18:50:07 +00:00
|
|
|
static pthread_once_t ft_shm_initialized_once_control = PTHREAD_ONCE_INIT;
|
|
|
|
|
|
2025-12-16 11:50:40 +01:00
|
|
|
static void ft_shm_init_mutex (void)
|
|
|
|
|
{
|
2025-12-16 16:30:25 +01:00
|
|
|
ft_initialize_errorcheck_mutex(&ft_shm_initialized_once_mutex);
|
2025-12-16 11:50:40 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-21 18:50:07 +00:00
|
|
|
static void ft_shm_really_init (void);
|
2013-08-19 21:13:10 +02:00
|
|
|
static void ft_shm_init (void)
|
2025-01-21 18:50:07 +00:00
|
|
|
{
|
2025-12-16 11:50:40 +01:00
|
|
|
static bool init_done = false;
|
2025-12-16 16:30:25 +01:00
|
|
|
ft_init_once_generic(&init_done, &ft_shm_initialized_once_control, &ft_shm_initialized_once_mutex, &ft_shm_init_mutex, &ft_shm_really_init);
|
2025-01-21 18:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ft_shm_really_init (void)
|
2013-08-19 14:08:51 +02:00
|
|
|
{
|
|
|
|
|
int ticks_shm_fd;
|
2013-09-01 16:04:21 +02:00
|
|
|
char sem_name[256], shm_name[256], *ft_shared_env = getenv("FAKETIME_SHARED");
|
2025-06-02 12:49:28 +02:00
|
|
|
ft_sem_t shared_semR;
|
2019-11-20 08:43:24 +01:00
|
|
|
static int nt=1;
|
2014-07-24 16:54:08 -07:00
|
|
|
|
2019-08-20 09:53:22 +02:00
|
|
|
/* create semaphore and shared memory locally unless it has been passed along */
|
2019-08-13 19:35:45 +02:00
|
|
|
if (ft_shared_env == NULL)
|
|
|
|
|
{
|
|
|
|
|
ft_shm_create();
|
|
|
|
|
ft_shared_env = getenv("FAKETIME_SHARED");
|
|
|
|
|
}
|
2019-09-05 22:52:07 +02:00
|
|
|
|
2019-08-20 09:53:22 +02:00
|
|
|
/* check for stale semaphore / shared memory information */
|
|
|
|
|
if (ft_shared_env != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (sscanf(ft_shared_env, "%255s %255s", sem_name, shm_name) < 2)
|
|
|
|
|
{
|
|
|
|
|
printf("libfaketime: In ft_shm_init(), error parsing semaphore name and shared memory id from string: %s", ft_shared_env);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2025-06-02 12:49:28 +02:00
|
|
|
if (-1 == ft_sem_open(sem_name, &shared_semR)) /* gone stale? */
|
2019-08-20 09:53:22 +02:00
|
|
|
{
|
|
|
|
|
ft_shm_create();
|
|
|
|
|
ft_shared_env = getenv("FAKETIME_SHARED");
|
|
|
|
|
}
|
2019-09-05 22:52:07 +02:00
|
|
|
else
|
2019-08-20 09:53:22 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
ft_sem_close(&shared_semR);
|
2019-08-20 09:53:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-08-13 19:35:45 +02:00
|
|
|
|
2019-08-20 09:53:22 +02:00
|
|
|
/* process the semaphore / shared memory information */
|
2013-09-04 12:50:00 +02:00
|
|
|
if (ft_shared_env != NULL)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-04 12:50:00 +02:00
|
|
|
if (sscanf(ft_shared_env, "%255s %255s", sem_name, shm_name) < 2)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2019-08-16 15:53:47 +02:00
|
|
|
printf("libfaketime: In ft_shm_init(), error parsing semaphore name and shared memory id from string: %s", ft_shared_env);
|
2013-08-19 14:08:51 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
2013-08-24 21:24:57 +02:00
|
|
|
|
2025-06-02 12:49:28 +02:00
|
|
|
if (-1 == ft_sem_open(sem_name, &shared_sem))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2019-11-20 08:43:24 +01:00
|
|
|
if (shmCreator)
|
|
|
|
|
{
|
|
|
|
|
perror("libfaketime: In ft_shm_init(), sem_open failed");
|
|
|
|
|
fprintf(stderr, "libfaketime: sem_name was %s, created locally: %s\n", sem_name, shmCreator ? "true":"false");
|
|
|
|
|
fprintf(stderr, "libfaketime: parsed from env: %s\n", ft_shared_env);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
nt++;
|
|
|
|
|
if (nt > 3)
|
|
|
|
|
{
|
|
|
|
|
perror("libfaketime: In ft_shm_init(), sem_open failed and recreation attempts failed");
|
|
|
|
|
fprintf(stderr, "libfaketime: sem_name was %s, created locally: %s\n", sem_name, shmCreator ? "true":"false");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
ft_shm_init();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2013-08-19 14:08:51 +02:00
|
|
|
}
|
2025-06-02 12:49:28 +02:00
|
|
|
shared_sem_initialized = true;
|
2013-08-19 14:08:51 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if (-1 == (ticks_shm_fd = shm_open(shm_name, O_CREAT|O_RDWR, S_IWUSR|S_IRUSR)))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2019-08-16 15:53:47 +02:00
|
|
|
perror("libfaketime: In ft_shm_init(), shm_open failed");
|
2013-08-19 14:08:51 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
2013-11-07 19:35:18 +01:00
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
if (MAP_FAILED == (ft_shared = mmap(NULL, sizeof(struct ft_shared_s), PROT_READ|PROT_WRITE,
|
2013-09-04 12:50:00 +02:00
|
|
|
MAP_SHARED, ticks_shm_fd, 0)))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2019-08-16 15:53:47 +02:00
|
|
|
perror("libfaketime: In ft_shm_init(), mmap failed");
|
2013-08-19 14:08:51 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-08 13:12:39 +02:00
|
|
|
if (getenv("FAKETIME_FLSHM") != NULL)
|
|
|
|
|
{ /* force the deletion of the shm sync env variable */
|
|
|
|
|
unsetenv("FAKETIME_SHARED");
|
|
|
|
|
}
|
2013-08-19 14:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
2018-01-02 00:15:27 +01:00
|
|
|
static void ft_cleanup (void)
|
2013-08-19 14:08:51 +02:00
|
|
|
{
|
|
|
|
|
/* detach from shared memory */
|
2013-09-04 12:50:00 +02:00
|
|
|
if (ft_shared != NULL)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2026-01-26 13:31:55 +01:00
|
|
|
munmap(ft_shared, sizeof(struct ft_shared_s));
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2013-09-04 12:50:00 +02:00
|
|
|
if (stss != NULL)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
munmap(stss, infile_size);
|
|
|
|
|
}
|
2025-06-02 12:49:28 +02:00
|
|
|
if (shared_sem_initialized)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
ft_sem_close(&shared_sem);
|
|
|
|
|
shared_sem_initialized = false;
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2019-05-31 22:27:10 +02:00
|
|
|
#ifdef FAKE_PTHREAD
|
|
|
|
|
if (pthread_rwlock_destroy(&monotonic_conds_lock) != 0) {
|
2019-08-16 15:53:47 +02:00
|
|
|
fprintf(stderr, "libfaketime: In ft_cleanup(), monotonic_conds_lock destroy failed\n");
|
2019-05-31 22:27:10 +02:00
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2019-08-13 19:48:29 +02:00
|
|
|
if (shmCreator == true) ft_shm_destroy();
|
2013-08-19 14:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
|
2020-02-14 10:36:29 +10:00
|
|
|
/*
|
|
|
|
|
* =======================================================================
|
|
|
|
|
* Get monotonic faketime setting === GETENV
|
|
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static void get_fake_monotonic_setting(int* current_value)
|
|
|
|
|
{
|
|
|
|
|
char *tmp_env;
|
|
|
|
|
if ((tmp_env = getenv("FAKETIME_DONT_FAKE_MONOTONIC")) != NULL
|
|
|
|
|
|| (tmp_env = getenv("DONT_FAKE_MONOTONIC")) != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (0 == strcmp(tmp_env, "1"))
|
|
|
|
|
{
|
|
|
|
|
(*current_value) = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
(*current_value) = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:23:42 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
* Internal time retrieval === INTTIME
|
|
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* Get system time from system for all clocks */
|
2013-09-04 12:50:00 +02:00
|
|
|
static void system_time_from_system (struct system_time_s * systime)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2025-06-07 20:28:33 +08:00
|
|
|
#ifdef __APPLE__
|
2022-02-04 16:47:44 -05:00
|
|
|
/* from https://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x */
|
2013-09-01 16:04:21 +02:00
|
|
|
clock_serv_t cclock;
|
|
|
|
|
mach_timespec_t mts;
|
|
|
|
|
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &clock_serv_real);
|
|
|
|
|
(*real_clock_get_time)(clock_serv_real, &mts);
|
|
|
|
|
systime->real.tv_sec = mts.tv_sec;
|
|
|
|
|
systime->real.tv_nsec = mts.tv_nsec;
|
|
|
|
|
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
|
|
|
|
|
(*real_clock_get_time)(cclock, &mts);
|
|
|
|
|
mach_port_deallocate(mach_task_self(), cclock);
|
|
|
|
|
systime->mon.tv_sec = mts.tv_sec;
|
|
|
|
|
systime->mon.tv_nsec = mts.tv_nsec;
|
|
|
|
|
systime->mon_raw.tv_sec = mts.tv_sec;
|
|
|
|
|
systime->mon_raw.tv_nsec = mts.tv_nsec;
|
|
|
|
|
#else
|
2017-05-19 18:55:11 +02:00
|
|
|
DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_REALTIME, &systime->real))
|
|
|
|
|
;
|
|
|
|
|
DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_MONOTONIC, &systime->mon))
|
|
|
|
|
;
|
2017-05-19 19:14:58 +02:00
|
|
|
DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_MONOTONIC_RAW, &systime->mon_raw))
|
|
|
|
|
;
|
|
|
|
|
#ifdef CLOCK_BOOTTIME
|
|
|
|
|
DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_BOOTTIME, &systime->boot))
|
|
|
|
|
;
|
|
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void next_time(struct timespec *tp, struct timespec *ticklen)
|
2013-08-19 14:08:51 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
if (shared_sem_initialized)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
struct timespec inc;
|
|
|
|
|
/* lock */
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_lock(&shared_sem) == -1)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2021-02-09 20:16:08 +01:00
|
|
|
if (errno == EINTR)
|
|
|
|
|
{
|
2022-05-27 00:44:13 +02:00
|
|
|
next_time(tp, ticklen);
|
|
|
|
|
return;
|
2021-02-09 20:16:08 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In next_time(), ft_sem_lock failed");
|
2021-02-09 20:16:08 +01:00
|
|
|
exit(1);
|
|
|
|
|
}
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
/* calculate and update elapsed time */
|
|
|
|
|
timespecmul(ticklen, ft_shared->ticks, &inc);
|
|
|
|
|
timespecadd(&user_faked_time_timespec, &inc, tp);
|
|
|
|
|
(ft_shared->ticks)++;
|
|
|
|
|
/* unlock */
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_unlock(&shared_sem) == -1)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In next_time(), ft_sem_unlock failed");
|
2013-09-01 16:04:21 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-27 12:30:56 +02:00
|
|
|
|
2022-06-13 11:42:35 +02:00
|
|
|
static void reset_time()
|
|
|
|
|
{
|
|
|
|
|
system_time_from_system(&ftpl_starttime);
|
2025-06-02 12:49:28 +02:00
|
|
|
if (shared_sem_initialized)
|
2022-06-13 11:42:35 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_lock(&shared_sem) == -1)
|
2022-06-13 11:42:35 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In reset_time(), ft_sem_lock failed");
|
2022-06-13 11:42:35 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
2026-01-26 13:31:55 +01:00
|
|
|
system_time_to_shared(&ftpl_starttime, ft_shared);
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_unlock(&shared_sem) == -1)
|
2022-06-13 11:42:35 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In reset_time(), ft_sem_unlock failed");
|
2022-06-13 11:42:35 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:23:42 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
* Saving & loading time === SAVE
|
|
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
static void save_time(struct timespec *tp)
|
|
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
if (shared_sem_initialized && (outfile != -1))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
struct saved_timestamp time_write;
|
2013-10-04 22:54:07 +02:00
|
|
|
ssize_t written;
|
|
|
|
|
size_t n = 0;
|
2013-09-01 16:04:21 +02:00
|
|
|
|
2013-10-04 22:26:54 +02:00
|
|
|
time_write.sec = htobe64(tp->tv_sec);
|
|
|
|
|
time_write.nsec = htobe64(tp->tv_nsec);
|
|
|
|
|
|
2013-08-27 12:30:56 +02:00
|
|
|
/* lock */
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_lock(&shared_sem) == -1)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2021-02-09 20:16:08 +01:00
|
|
|
if (errno == EINTR)
|
|
|
|
|
{
|
2022-05-27 00:44:13 +02:00
|
|
|
save_time(tp);
|
|
|
|
|
return;
|
2021-02-09 20:16:08 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In save_time(), ft_sem_lock failed");
|
2021-02-09 20:16:08 +01:00
|
|
|
exit(1);
|
|
|
|
|
}
|
2013-08-27 12:30:56 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
lseek(outfile, 0, SEEK_END);
|
2013-11-07 19:35:18 +01:00
|
|
|
do
|
|
|
|
|
{
|
2013-10-04 22:54:07 +02:00
|
|
|
written = write(outfile, &(((char*)&time_write)[n]), sizeof(time_write) - n);
|
2013-11-07 19:35:18 +01:00
|
|
|
}
|
|
|
|
|
while (((written == -1) && (errno == EINTR)) ||
|
|
|
|
|
(sizeof(time_write) < (n += written)));
|
2013-09-01 16:04:21 +02:00
|
|
|
|
2013-10-04 22:54:07 +02:00
|
|
|
if ((written == -1) || (n < sizeof(time_write)))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2019-08-16 15:53:47 +02:00
|
|
|
perror("libfaketime: In save_time(), saving timestamp to file failed");
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
2013-08-19 14:08:51 +02:00
|
|
|
/* unlock */
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_unlock(&shared_sem) == -1)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In save_time(), ft_sem_unlcok failed");
|
2013-08-19 14:08:51 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/*
|
2013-09-01 16:04:21 +02:00
|
|
|
* Provide faked time from file.
|
|
|
|
|
* @return time is set from filen
|
|
|
|
|
*/
|
|
|
|
|
static bool load_time(struct timespec *tp)
|
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
2025-06-02 12:49:28 +02:00
|
|
|
if (shared_sem_initialized && (infile_set))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
/* lock */
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_lock(&shared_sem) == -1)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2021-02-09 20:16:08 +01:00
|
|
|
if (errno == EINTR)
|
|
|
|
|
{
|
|
|
|
|
return load_time(tp);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In load_time(), ft_sem_lock failed");
|
2021-02-09 20:16:08 +01:00
|
|
|
exit(1);
|
|
|
|
|
}
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2013-08-29 10:15:15 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((sizeof(stss[0]) * (ft_shared->file_idx + 1)) > infile_size)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2021-09-21 20:43:41 +00:00
|
|
|
/* we are out of timestamps to replay, return to faking time by rules
|
2013-09-01 16:04:21 +02:00
|
|
|
* using last timestamp from file as the user provided timestamp */
|
|
|
|
|
timespec_from_saved(&user_faked_time_timespec, &stss[(infile_size / sizeof(stss[0])) - 1 ]);
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if (ft_shared->ticks == 0)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-10-30 21:47:56 +01:00
|
|
|
/* we set shared memory to stop using infile */
|
|
|
|
|
ft_shared->ticks = 1;
|
|
|
|
|
system_time_from_system(&ftpl_starttime);
|
2026-01-26 13:31:55 +01:00
|
|
|
system_time_to_shared(&ftpl_starttime, ft_shared);
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2026-01-26 13:31:55 +01:00
|
|
|
shared_to_system_time(ft_shared, &ftpl_starttime);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
munmap(stss, infile_size);
|
|
|
|
|
infile_set = false;
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
timespec_from_saved(tp, &stss[ft_shared->file_idx]);
|
|
|
|
|
ft_shared->file_idx++;
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* unlock */
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_unlock(&shared_sem) == -1)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In load_time(), ft_sem_unlock failed");
|
2013-09-01 16:04:21 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
2013-08-19 14:08:51 +02:00
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:23:42 +02:00
|
|
|
* =======================================================================
|
2013-09-03 13:42:10 +02:00
|
|
|
* Faked system functions: file related === FAKE(FILE)
|
2013-09-03 13:23:42 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
2022-03-18 12:03:24 +00:00
|
|
|
#ifdef FAKE_UTIME
|
2025-01-19 22:17:08 +00:00
|
|
|
static int fake_utime_disabled = 1;
|
2022-03-18 12:03:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
|
2013-08-21 09:54:10 +02:00
|
|
|
#ifdef FAKE_STAT
|
|
|
|
|
|
|
|
|
|
#ifndef NO_ATFILE
|
|
|
|
|
#ifndef _ATFILE_SOURCE
|
|
|
|
|
#define _ATFILE_SOURCE
|
|
|
|
|
#endif
|
|
|
|
|
#include <fcntl.h> /* Definition of AT_* constants */
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
|
|
static int fake_stat_disabled = 0;
|
2019-11-14 09:27:27 -06:00
|
|
|
static bool user_per_tick_inc_set_backup = false;
|
2013-08-21 09:54:10 +02:00
|
|
|
|
2019-08-21 10:23:01 +02:00
|
|
|
void lock_for_stat()
|
|
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
if (shared_sem_initialized)
|
2019-08-21 10:23:01 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_lock(&shared_sem) == -1)
|
2019-08-21 10:23:01 +02:00
|
|
|
{
|
2021-02-09 20:16:08 +01:00
|
|
|
if (errno == EINTR)
|
|
|
|
|
{
|
2022-05-27 00:44:13 +02:00
|
|
|
lock_for_stat();
|
|
|
|
|
return;
|
2021-02-09 20:16:08 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In lock_for_stat(), ft_sem_lock failed");
|
2021-02-09 20:16:08 +01:00
|
|
|
exit(1);
|
|
|
|
|
}
|
2019-08-21 10:23:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
user_per_tick_inc_set_backup = user_per_tick_inc_set;
|
|
|
|
|
user_per_tick_inc_set = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void unlock_for_stat()
|
|
|
|
|
{
|
|
|
|
|
user_per_tick_inc_set = user_per_tick_inc_set_backup;
|
|
|
|
|
|
2025-06-02 12:49:28 +02:00
|
|
|
if (shared_sem_initialized)
|
2019-08-21 10:23:01 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_unlock(&shared_sem) == -1)
|
2019-08-21 10:23:01 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In unlock_for_stat(), ft_sem_unlock failed");
|
2019-08-21 10:23:01 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-16 10:56:45 +02:00
|
|
|
#define FAKE_STRUCT_STAT_TIME(which) { \
|
|
|
|
|
struct timespec t = {buf->st_##which##time, \
|
|
|
|
|
buf->st_##which##timensec}; \
|
|
|
|
|
fake_clock_gettime(CLOCK_REALTIME, &t); \
|
|
|
|
|
buf->st_##which##time = t.tv_sec; \
|
|
|
|
|
buf->st_##which##timensec = t.tv_nsec; \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
static inline void fake_statbuf (struct stat *buf) {
|
|
|
|
|
#ifndef st_atime
|
2019-08-21 10:23:01 +02:00
|
|
|
lock_for_stat();
|
2013-10-16 10:56:45 +02:00
|
|
|
FAKE_STRUCT_STAT_TIME(c);
|
|
|
|
|
FAKE_STRUCT_STAT_TIME(a);
|
|
|
|
|
FAKE_STRUCT_STAT_TIME(m);
|
2019-08-21 10:23:01 +02:00
|
|
|
unlock_for_stat();
|
2013-10-16 10:56:45 +02:00
|
|
|
#else
|
2019-08-21 10:23:01 +02:00
|
|
|
lock_for_stat();
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifndef __APPLE__
|
2013-10-16 10:56:45 +02:00
|
|
|
fake_clock_gettime(CLOCK_REALTIME, &buf->st_ctim);
|
|
|
|
|
fake_clock_gettime(CLOCK_REALTIME, &buf->st_atim);
|
|
|
|
|
fake_clock_gettime(CLOCK_REALTIME, &buf->st_mtim);
|
2022-02-20 17:39:17 +01:00
|
|
|
#else
|
|
|
|
|
fake_clock_gettime(CLOCK_REALTIME, &buf->st_ctimespec);
|
|
|
|
|
fake_clock_gettime(CLOCK_REALTIME, &buf->st_atimespec);
|
|
|
|
|
fake_clock_gettime(CLOCK_REALTIME, &buf->st_mtimespec);
|
|
|
|
|
#endif
|
2019-08-21 10:23:01 +02:00
|
|
|
unlock_for_stat();
|
2013-10-16 10:56:45 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
libfaketime.c: get rid of stat64 things on aarch64-darwin
To give more context, stat64 is a child of large-file support (LFS)
back in 1996, during the transition from 32-bit to 64-bit. People
wanted 64-bit inodes in 32-bit systems, hence stat and stat64.
Nowadays where everything is 64-bit, stat64 is mostly just an alias
to stat, as stat is already native 64-bit. On modern implementations
like musl, stat64 is even dropped entirely as a sane default. We
observe the same in darwin's stat.h:
#if !__DARWIN_ONLY_64_BIT_INO_T
struct stat64 __DARWIN_STRUCT_STAT64;
#endif /* !__DARWIN_ONLY_64_BIT_INO_T */
Because struct stat64 doesn't ever exist on aarch64-darwin, and we
don't have to worry about people using stat64 calls, we can safely
remove all stat64 bloat, according to __DARWIN_ONLY_64_BIT_INO_T.
I nuked fake_stat64buf because only STAT64_HANDLER is using it, and
only non-darwin stat64 things use that handler. I didn't do more
because people might still use stat64 things on x86_64 (on glibc)
and other older 32-bit platforms, and we still need to hook those.
A loose follow up to PR #453. Fixes the remaining clang warnings on
aarch64-darwin.
2025-06-08 18:33:33 +08:00
|
|
|
#ifndef __APPLE__
|
2013-10-16 10:56:45 +02:00
|
|
|
static inline void fake_stat64buf (struct stat64 *buf) {
|
|
|
|
|
#ifndef st_atime
|
2019-08-21 10:23:01 +02:00
|
|
|
lock_for_stat();
|
2013-10-16 10:56:45 +02:00
|
|
|
FAKE_STRUCT_STAT_TIME(c);
|
|
|
|
|
FAKE_STRUCT_STAT_TIME(a);
|
|
|
|
|
FAKE_STRUCT_STAT_TIME(m);
|
2019-08-21 10:23:01 +02:00
|
|
|
unlock_for_stat();
|
2013-10-16 10:56:45 +02:00
|
|
|
#else
|
2019-08-21 10:23:01 +02:00
|
|
|
lock_for_stat();
|
2025-06-09 19:12:41 +08:00
|
|
|
fake_clock_gettime(CLOCK_REALTIME, &buf->st_ctim);
|
|
|
|
|
fake_clock_gettime(CLOCK_REALTIME, &buf->st_atim);
|
|
|
|
|
fake_clock_gettime(CLOCK_REALTIME, &buf->st_mtim);
|
2019-08-21 10:23:01 +02:00
|
|
|
unlock_for_stat();
|
2013-10-16 10:56:45 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
libfaketime.c: get rid of stat64 things on aarch64-darwin
To give more context, stat64 is a child of large-file support (LFS)
back in 1996, during the transition from 32-bit to 64-bit. People
wanted 64-bit inodes in 32-bit systems, hence stat and stat64.
Nowadays where everything is 64-bit, stat64 is mostly just an alias
to stat, as stat is already native 64-bit. On modern implementations
like musl, stat64 is even dropped entirely as a sane default. We
observe the same in darwin's stat.h:
#if !__DARWIN_ONLY_64_BIT_INO_T
struct stat64 __DARWIN_STRUCT_STAT64;
#endif /* !__DARWIN_ONLY_64_BIT_INO_T */
Because struct stat64 doesn't ever exist on aarch64-darwin, and we
don't have to worry about people using stat64 calls, we can safely
remove all stat64 bloat, according to __DARWIN_ONLY_64_BIT_INO_T.
I nuked fake_stat64buf because only STAT64_HANDLER is using it, and
only non-darwin stat64 things use that handler. I didn't do more
because people might still use stat64 things on x86_64 (on glibc)
and other older 32-bit platforms, and we still need to hook those.
A loose follow up to PR #453. Fixes the remaining clang warnings on
aarch64-darwin.
2025-06-08 18:33:33 +08:00
|
|
|
#endif
|
2013-10-16 10:56:45 +02:00
|
|
|
|
2022-02-20 17:39:17 +01:00
|
|
|
/* macOS dyld interposing uses the function's real name instead of real_name */
|
|
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
#define STAT_HANDLER_COMMON(name, buf, fake_statbuf, ...) \
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init(); \
|
2022-02-20 17:39:17 +01:00
|
|
|
if (!CHECK_MISSING_REAL(name)) return -1; \
|
|
|
|
|
\
|
|
|
|
|
int result; \
|
|
|
|
|
DONT_FAKE_TIME(result = name(__VA_ARGS__)); \
|
|
|
|
|
if (result == -1) \
|
|
|
|
|
{ \
|
|
|
|
|
return -1; \
|
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
if (buf != NULL) \
|
|
|
|
|
{ \
|
|
|
|
|
if (!fake_stat_disabled) \
|
|
|
|
|
{ \
|
|
|
|
|
if (!dont_fake) fake_statbuf(buf); \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
return result;
|
|
|
|
|
#else
|
2022-02-16 11:58:42 +02:00
|
|
|
#define STAT_HANDLER_COMMON(name, buf, fake_statbuf, ...) \
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init(); \
|
2022-02-16 11:58:42 +02:00
|
|
|
if (!CHECK_MISSING_REAL(name)) return -1; \
|
|
|
|
|
\
|
|
|
|
|
int result; \
|
|
|
|
|
DONT_FAKE_TIME(result = real_##name(__VA_ARGS__)); \
|
|
|
|
|
if (result == -1) \
|
|
|
|
|
{ \
|
|
|
|
|
return -1; \
|
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
if (buf != NULL) \
|
|
|
|
|
{ \
|
|
|
|
|
if (!fake_stat_disabled) \
|
|
|
|
|
{ \
|
|
|
|
|
if (!dont_fake) fake_statbuf(buf); \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
return result;
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2022-02-16 11:58:42 +02:00
|
|
|
#define STAT_HANDLER(name, buf, ...) \
|
|
|
|
|
STAT_HANDLER_COMMON(name, buf, fake_statbuf, __VA_ARGS__)
|
|
|
|
|
#define STAT64_HANDLER(name, buf, ...) \
|
|
|
|
|
STAT_HANDLER_COMMON(name, buf, fake_stat64buf, __VA_ARGS__)
|
|
|
|
|
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_stat (const char *path, struct stat *buf)
|
|
|
|
|
#else
|
2022-02-16 12:01:35 +02:00
|
|
|
int stat (const char *path, struct stat *buf)
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2022-02-16 12:01:35 +02:00
|
|
|
STAT_HANDLER(stat, buf, path, buf);
|
|
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_fstat (int fildes, struct stat *buf)
|
|
|
|
|
#else
|
2022-02-16 12:01:35 +02:00
|
|
|
int fstat (int fildes, struct stat *buf)
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2022-02-16 12:01:35 +02:00
|
|
|
{
|
|
|
|
|
STAT_HANDLER(fstat, buf, fildes, buf);
|
|
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_lstat (const char *path, struct stat *buf)
|
|
|
|
|
#else
|
2022-02-16 12:01:35 +02:00
|
|
|
int lstat (const char *path, struct stat *buf)
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2022-02-16 12:01:35 +02:00
|
|
|
{
|
|
|
|
|
STAT_HANDLER(lstat, buf, path, buf);
|
|
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifndef __APPLE__
|
2013-08-19 11:47:49 +02:00
|
|
|
/* Contributed by Philipp Hachtmann in version 0.6 */
|
2013-09-04 12:50:00 +02:00
|
|
|
int __xstat (int ver, const char *path, struct stat *buf)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2022-02-16 11:58:42 +02:00
|
|
|
STAT_HANDLER(xstat, buf, ver, path, buf);
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifndef __APPLE__
|
2013-08-19 11:47:49 +02:00
|
|
|
/* Contributed by Philipp Hachtmann in version 0.6 */
|
2013-09-04 12:50:00 +02:00
|
|
|
int __fxstat (int ver, int fildes, struct stat *buf)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2022-02-16 11:58:42 +02:00
|
|
|
STAT_HANDLER(fxstat, buf, ver, fildes, buf);
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifndef __APPLE__
|
2013-08-19 11:47:49 +02:00
|
|
|
/* Added in v0.8 as suggested by Daniel Kahn Gillmor */
|
|
|
|
|
#ifndef NO_ATFILE
|
2013-09-04 12:50:00 +02:00
|
|
|
int __fxstatat(int ver, int fildes, const char *filename, struct stat *buf, int flag)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2022-02-16 11:58:42 +02:00
|
|
|
STAT_HANDLER(fxstatat, buf, ver, fildes, filename, buf, flag);
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifndef __APPLE__
|
2013-08-19 11:47:49 +02:00
|
|
|
/* Contributed by Philipp Hachtmann in version 0.6 */
|
2013-09-04 12:50:00 +02:00
|
|
|
int __lxstat (int ver, const char *path, struct stat *buf)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2022-02-16 11:58:42 +02:00
|
|
|
STAT_HANDLER(lxstat, buf, ver, path, buf);
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-07 09:12:17 +02:00
|
|
|
#ifdef __GLIBC__
|
Handle stat64() call
This fixes missing modification of timestamps in stat() calls for
programs built with large file support (-D_FILE_OFFSET_BITS=64), both
32- and 64-bit.
Demo code:
$ cat <<EOF >test.c
#include <sys/stat.h>
int main()
{
struct stat buf;
return stat("/", &buf);
}
EOF
32-bit build:
$ nix-shell -p gcc --argstr system i686-linux
nix-shell$ gcc test.c && ltrace ./a.out
__libc_start_main([ "./a.out" ] <unfinished ...>
stat(0x804a008, 0xffa4b644, 895, 0) = 0
+++ exited (status 0) +++
nix-shell$ gcc -D_FILE_OFFSET_BITS=64 test.c && ltrace ./a.out
__libc_start_main([ "./a.out" ] <unfinished ...>
stat64(0x804a008, 0xffdcf61c, 100, 0xffdcfaeb) = 0
+++ exited (status 0) +++
nix-shell$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, [...]
64-bit build:
$ nix-shell -p gcc
nix-shell$ gcc test.c && ltrace ./a.out
stat(0x402004, 0x7ffc50a9d740, 0x7ffc50a9d908, 0x403db0) = 0
+++ exited (status 0) +++
nix-shell$ gcc -D_FILE_OFFSET_BITS=64 test.c && ltrace ./a.out
stat64(0x402004, 0x7ffd5cbafba0, 0x7ffd5cbafd68, 0x403db0) = 0
+++ exited (status 0) +++
nix-shell$ file a.out
a.out: ELF 64-bit LSB executable, x86-64, [...]
2025-07-09 17:32:54 +02:00
|
|
|
int stat64 (const char *path, struct stat64 *buf)
|
|
|
|
|
{
|
|
|
|
|
STAT64_HANDLER(stat64, buf, path, buf);
|
|
|
|
|
}
|
2025-08-07 09:12:17 +02:00
|
|
|
#endif
|
Handle stat64() call
This fixes missing modification of timestamps in stat() calls for
programs built with large file support (-D_FILE_OFFSET_BITS=64), both
32- and 64-bit.
Demo code:
$ cat <<EOF >test.c
#include <sys/stat.h>
int main()
{
struct stat buf;
return stat("/", &buf);
}
EOF
32-bit build:
$ nix-shell -p gcc --argstr system i686-linux
nix-shell$ gcc test.c && ltrace ./a.out
__libc_start_main([ "./a.out" ] <unfinished ...>
stat(0x804a008, 0xffa4b644, 895, 0) = 0
+++ exited (status 0) +++
nix-shell$ gcc -D_FILE_OFFSET_BITS=64 test.c && ltrace ./a.out
__libc_start_main([ "./a.out" ] <unfinished ...>
stat64(0x804a008, 0xffdcf61c, 100, 0xffdcfaeb) = 0
+++ exited (status 0) +++
nix-shell$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, [...]
64-bit build:
$ nix-shell -p gcc
nix-shell$ gcc test.c && ltrace ./a.out
stat(0x402004, 0x7ffc50a9d740, 0x7ffc50a9d908, 0x403db0) = 0
+++ exited (status 0) +++
nix-shell$ gcc -D_FILE_OFFSET_BITS=64 test.c && ltrace ./a.out
stat64(0x402004, 0x7ffd5cbafba0, 0x7ffd5cbafd68, 0x403db0) = 0
+++ exited (status 0) +++
nix-shell$ file a.out
a.out: ELF 64-bit LSB executable, x86-64, [...]
2025-07-09 17:32:54 +02:00
|
|
|
|
2013-08-19 11:47:49 +02:00
|
|
|
/* Contributed by Philipp Hachtmann in version 0.6 */
|
2013-09-04 12:50:00 +02:00
|
|
|
int __xstat64 (int ver, const char *path, struct stat64 *buf)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2022-02-16 11:58:42 +02:00
|
|
|
STAT64_HANDLER(xstat64, buf, ver, path, buf);
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Contributed by Philipp Hachtmann in version 0.6 */
|
2013-09-04 12:50:00 +02:00
|
|
|
int __fxstat64 (int ver, int fildes, struct stat64 *buf)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2022-02-16 11:58:42 +02:00
|
|
|
STAT64_HANDLER(fxstat64, buf, ver, fildes, buf);
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Added in v0.8 as suggested by Daniel Kahn Gillmor */
|
|
|
|
|
#ifndef NO_ATFILE
|
2013-09-04 12:50:00 +02:00
|
|
|
int __fxstatat64 (int ver, int fildes, const char *filename, struct stat64 *buf, int flag)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2022-02-16 11:58:42 +02:00
|
|
|
STAT64_HANDLER(fxstatat64, buf, ver, fildes, filename, buf, flag);
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Contributed by Philipp Hachtmann in version 0.6 */
|
2013-09-03 13:23:42 +02:00
|
|
|
int __lxstat64 (int ver, const char *path, struct stat64 *buf)
|
|
|
|
|
{
|
2022-02-16 11:58:42 +02:00
|
|
|
STAT64_HANDLER(lxstat64, buf, ver, path, buf);
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
libfaketime.c: get rid of stat64 things on aarch64-darwin
To give more context, stat64 is a child of large-file support (LFS)
back in 1996, during the transition from 32-bit to 64-bit. People
wanted 64-bit inodes in 32-bit systems, hence stat and stat64.
Nowadays where everything is 64-bit, stat64 is mostly just an alias
to stat, as stat is already native 64-bit. On modern implementations
like musl, stat64 is even dropped entirely as a sane default. We
observe the same in darwin's stat.h:
#if !__DARWIN_ONLY_64_BIT_INO_T
struct stat64 __DARWIN_STRUCT_STAT64;
#endif /* !__DARWIN_ONLY_64_BIT_INO_T */
Because struct stat64 doesn't ever exist on aarch64-darwin, and we
don't have to worry about people using stat64 calls, we can safely
remove all stat64 bloat, according to __DARWIN_ONLY_64_BIT_INO_T.
I nuked fake_stat64buf because only STAT64_HANDLER is using it, and
only non-darwin stat64 things use that handler. I didn't do more
because people might still use stat64 things on x86_64 (on glibc)
and other older 32-bit platforms, and we still need to hook those.
A loose follow up to PR #453. Fixes the remaining clang warnings on
aarch64-darwin.
2025-06-08 18:33:33 +08:00
|
|
|
#endif /* ifndef __APPLE__ */
|
|
|
|
|
#endif /* ifdef FAKE_STAT */
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2023-07-30 20:55:48 -04:00
|
|
|
#ifdef STATX_TYPE
|
|
|
|
|
static inline void fake_statx_timestamp(struct statx_timestamp* p)
|
|
|
|
|
{
|
|
|
|
|
struct timespec t = {p->tv_sec,p->tv_nsec};
|
|
|
|
|
fake_clock_gettime(CLOCK_REALTIME, &t);
|
|
|
|
|
p->tv_sec = t.tv_sec;
|
|
|
|
|
p->tv_nsec = t.tv_nsec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void fake_statxbuf (struct statx *buf) {
|
|
|
|
|
lock_for_stat();
|
|
|
|
|
if (buf->stx_mask & STATX_ATIME) {
|
|
|
|
|
fake_statx_timestamp(&buf->stx_atime);
|
|
|
|
|
}
|
|
|
|
|
if (buf->stx_mask & STATX_CTIME) {
|
|
|
|
|
fake_statx_timestamp(&buf->stx_ctime);
|
|
|
|
|
}
|
|
|
|
|
if (buf->stx_mask & STATX_MTIME) {
|
|
|
|
|
fake_statx_timestamp(&buf->stx_mtime);
|
|
|
|
|
}
|
|
|
|
|
if (buf->stx_mask & STATX_BTIME) {
|
|
|
|
|
fake_statx_timestamp(&buf->stx_btime);
|
|
|
|
|
}
|
|
|
|
|
unlock_for_stat();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf)
|
|
|
|
|
{
|
|
|
|
|
STAT_HANDLER_COMMON(statx, statxbuf, fake_statxbuf, dirfd, pathname, flags, mask, statxbuf)
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-12-14 21:49:23 +01:00
|
|
|
#ifdef FAKE_FILE_TIMESTAMPS
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_utime(const char *filename, const struct utimbuf *times)
|
|
|
|
|
#else
|
2019-12-14 21:49:23 +01:00
|
|
|
int utime(const char *filename, const struct utimbuf *times)
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2019-12-14 21:49:23 +01:00
|
|
|
{
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(utime)) return -1;
|
2019-12-14 21:49:23 +01:00
|
|
|
|
|
|
|
|
int result;
|
|
|
|
|
struct utimbuf ntbuf;
|
2020-07-28 17:34:38 -07:00
|
|
|
if (fake_utime_disabled)
|
2019-12-14 21:49:23 +01:00
|
|
|
{
|
2020-07-28 17:34:38 -07:00
|
|
|
if (times == NULL)
|
|
|
|
|
{ /* The user wants their given fake times left alone but they requested NOW, so turn it into fake NOW */
|
|
|
|
|
ntbuf.actime = ntbuf.modtime = time(NULL);
|
|
|
|
|
times = &ntbuf;
|
|
|
|
|
}
|
2019-12-14 21:49:23 +01:00
|
|
|
}
|
2020-07-28 17:34:38 -07:00
|
|
|
else if (times != NULL)
|
|
|
|
|
{
|
|
|
|
|
ntbuf.actime = times->actime - user_offset.tv_sec;
|
|
|
|
|
ntbuf.modtime = times->modtime - user_offset.tv_sec;
|
|
|
|
|
times = &ntbuf;
|
|
|
|
|
}
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = utime(filename, times));
|
|
|
|
|
#else
|
2020-07-28 17:34:38 -07:00
|
|
|
DONT_FAKE_TIME(result = real_utime(filename, times));
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2019-12-14 21:49:23 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_utimes(const char *filename, const struct timeval times[2])
|
|
|
|
|
#else
|
2019-12-14 21:49:23 +01:00
|
|
|
int utimes(const char *filename, const struct timeval times[2])
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2019-12-14 21:49:23 +01:00
|
|
|
{
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(utimes)) return -1;
|
2019-12-14 21:49:23 +01:00
|
|
|
|
|
|
|
|
int result;
|
|
|
|
|
struct timeval tn[2];
|
2020-07-28 17:34:38 -07:00
|
|
|
if (fake_utime_disabled)
|
2019-12-14 21:49:23 +01:00
|
|
|
{
|
2020-07-28 17:34:38 -07:00
|
|
|
if (times == NULL)
|
|
|
|
|
{ /* The user wants their given fake times left alone but they requested NOW, so turn it into fake NOW */
|
|
|
|
|
fake_gettimeofday(&tn[0]);
|
|
|
|
|
tn[1] = tn[0];
|
|
|
|
|
times = tn;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (times != NULL)
|
|
|
|
|
{
|
|
|
|
|
struct timeval user_offset2;
|
|
|
|
|
user_offset2.tv_sec = user_offset.tv_sec;
|
|
|
|
|
user_offset2.tv_usec = user_offset.tv_nsec / 1000;
|
|
|
|
|
timersub(×[0], &user_offset2, &tn[0]);
|
|
|
|
|
timersub(×[1], &user_offset2, &tn[1]);
|
|
|
|
|
times = tn;
|
|
|
|
|
}
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = utimes(filename, times));
|
|
|
|
|
#else
|
2020-07-28 17:34:38 -07:00
|
|
|
DONT_FAKE_TIME(result = real_utimes(filename, times));
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2020-07-28 17:34:38 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This conditionally offsets 2 timespec values. The caller's out_times array
|
|
|
|
|
* always contains valid translated values, even if in_times was NULL. */
|
|
|
|
|
static void fake_two_timespec(const struct timespec in_times[2], struct timespec out_times[2])
|
|
|
|
|
{
|
|
|
|
|
if (in_times == NULL) /* Translate NULL into 2 UTIME_NOW values */
|
|
|
|
|
{
|
|
|
|
|
out_times[0].tv_sec = out_times[1].tv_sec = 0;
|
|
|
|
|
out_times[0].tv_nsec = out_times[1].tv_nsec = UTIME_NOW;
|
|
|
|
|
in_times = out_times;
|
|
|
|
|
}
|
|
|
|
|
struct timespec now;
|
|
|
|
|
now.tv_nsec = UTIME_OMIT; /* Wait to grab the current time to see if it's actually needed */
|
|
|
|
|
int j;
|
|
|
|
|
for (j = 0; j <= 1; j++)
|
|
|
|
|
{
|
|
|
|
|
/* We need to preserve 2 special time values in addition to when the user disables utime offsets */
|
|
|
|
|
if (fake_utime_disabled || in_times[j].tv_nsec == UTIME_OMIT || in_times[j].tv_nsec == UTIME_NOW)
|
|
|
|
|
{
|
|
|
|
|
if (fake_utime_disabled && in_times[j].tv_nsec == UTIME_NOW)
|
|
|
|
|
{ /* The user wants their given fake times left alone but they requested NOW, so turn it into fake NOW */
|
|
|
|
|
if (now.tv_nsec == UTIME_OMIT) /* did we grab "now" yet? */
|
|
|
|
|
{
|
|
|
|
|
DONT_FAKE_TIME(real_clock_gettime(CLOCK_REALTIME, &now));
|
|
|
|
|
}
|
|
|
|
|
timeradd2(&now, &user_offset, &out_times[j], n);
|
|
|
|
|
}
|
|
|
|
|
else if (out_times != in_times)
|
|
|
|
|
{ /* Just preserve the input value */
|
|
|
|
|
out_times[j] = in_times[j];
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-15 21:57:10 +01:00
|
|
|
else
|
2020-07-28 17:34:38 -07:00
|
|
|
{
|
|
|
|
|
timersub2(&in_times[j], &user_offset, &out_times[j], n);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_utimensat(int dirfd, const char *filename, const struct timespec times[2], int flags)
|
|
|
|
|
#else
|
2020-07-28 17:34:38 -07:00
|
|
|
int utimensat(int dirfd, const char *filename, const struct timespec times[2], int flags)
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2020-07-28 17:34:38 -07:00
|
|
|
{
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(utimensat)) return -1;
|
2020-07-28 17:34:38 -07:00
|
|
|
|
|
|
|
|
int result;
|
|
|
|
|
struct timespec tn[2];
|
|
|
|
|
fake_two_timespec(times, tn);
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = utimensat(dirfd, filename, tn, flags));
|
|
|
|
|
#else
|
2020-07-28 17:34:38 -07:00
|
|
|
DONT_FAKE_TIME(result = real_utimensat(dirfd, filename, tn, flags));
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2020-07-28 17:34:38 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_futimens(int fd, const struct timespec times[2])
|
|
|
|
|
#else
|
2020-07-28 17:34:38 -07:00
|
|
|
int futimens(int fd, const struct timespec times[2])
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2020-07-28 17:34:38 -07:00
|
|
|
{
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(futimens)) return -1;
|
2019-12-14 21:49:23 +01:00
|
|
|
|
2020-07-28 17:34:38 -07:00
|
|
|
int result;
|
|
|
|
|
struct timespec tn[2];
|
|
|
|
|
fake_two_timespec(times, tn);
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = futimens(fd, tn));
|
|
|
|
|
#else
|
2020-07-28 17:34:38 -07:00
|
|
|
DONT_FAKE_TIME(result = real_futimens(fd, tn));
|
2022-02-20 17:39:17 +01:00
|
|
|
#endif
|
2019-12-14 21:49:23 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:42:10 +02:00
|
|
|
* =======================================================================
|
2013-09-03 17:13:16 +02:00
|
|
|
* Faked system functions: sleep/alarm/poll/timer related === FAKE(SLEEP)
|
2013-09-03 13:42:10 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
* Contributed by Balint Reczey in v0.9.5
|
|
|
|
|
*/
|
|
|
|
|
|
2013-09-04 14:20:43 +02:00
|
|
|
#ifdef FAKE_SLEEP
|
2013-09-03 13:23:42 +02:00
|
|
|
/*
|
2013-09-01 16:04:21 +02:00
|
|
|
* Faked nanosleep()
|
2013-08-29 10:15:15 +02:00
|
|
|
*/
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_nanosleep(const struct timespec *req, struct timespec *rem)
|
|
|
|
|
#else
|
2013-09-01 16:04:21 +02:00
|
|
|
int nanosleep(const struct timespec *req, struct timespec *rem)
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
struct timespec real_req;
|
2013-08-28 18:22:51 +02:00
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-04 12:50:00 +02:00
|
|
|
if (real_nanosleep == NULL)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-09-04 12:50:00 +02:00
|
|
|
if (req != NULL)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-04 12:50:00 +02:00
|
|
|
if (user_rate_set && !dont_fake)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
timespecmul(req, 1.0 / user_rate, &real_req);
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
real_req = *req;
|
|
|
|
|
}
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-08-28 18:22:51 +02:00
|
|
|
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = (*nanosleep)(&real_req, rem));
|
|
|
|
|
#else
|
2013-09-01 16:04:21 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_nanosleep)(&real_req, rem));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-04 12:50:00 +02:00
|
|
|
if (result == -1)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
2013-08-28 18:22:51 +02:00
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
/* fake returned parts */
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((rem != NULL) && ((rem->tv_sec != 0) || (rem->tv_nsec != 0)))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-04 12:50:00 +02:00
|
|
|
if (user_rate_set && !dont_fake)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
timespecmul(rem, user_rate, rem);
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
/* return the result to the caller */
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2013-08-28 18:22:51 +02:00
|
|
|
|
2019-08-21 18:04:48 +02:00
|
|
|
#ifndef __APPLE__
|
|
|
|
|
/*
|
|
|
|
|
* Faked clock_nanosleep()
|
|
|
|
|
*/
|
|
|
|
|
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *req, struct timespec *rem)
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
struct timespec real_req;
|
|
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2019-08-21 18:04:48 +02:00
|
|
|
if (real_clock_nanosleep == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (req != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (flags & TIMER_ABSTIME) /* sleep until absolute time */
|
|
|
|
|
{
|
|
|
|
|
struct timespec tdiff, timeadj;
|
|
|
|
|
timespecsub(req, &user_faked_time_timespec, &timeadj);
|
|
|
|
|
if (user_rate_set)
|
|
|
|
|
{
|
|
|
|
|
timespecmul(&timeadj, 1.0/user_rate, &tdiff);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tdiff = timeadj;
|
|
|
|
|
}
|
2023-06-06 20:10:31 +02:00
|
|
|
|
2019-08-21 18:04:48 +02:00
|
|
|
if (clock_id == CLOCK_REALTIME)
|
|
|
|
|
{
|
|
|
|
|
timespecadd(&ftpl_starttime.real, &tdiff, &real_req);
|
|
|
|
|
}
|
|
|
|
|
else if (clock_id == CLOCK_MONOTONIC)
|
|
|
|
|
{
|
2023-06-06 19:48:18 +02:00
|
|
|
get_fake_monotonic_setting(&fake_monotonic_clock);
|
2023-06-06 20:10:31 +02:00
|
|
|
if (fake_monotonic_clock)
|
|
|
|
|
{
|
2023-06-04 13:21:09 +02:00
|
|
|
timespecadd(&ftpl_starttime.mon, &tdiff, &real_req);
|
|
|
|
|
}
|
2023-06-06 20:10:31 +02:00
|
|
|
else
|
|
|
|
|
{ /* leave untouched if CLOCK_MONOTONIC but faking monotonic clock disabled */
|
|
|
|
|
real_req = *req;
|
|
|
|
|
}
|
2019-08-21 18:04:48 +02:00
|
|
|
}
|
|
|
|
|
else /* presumably only CLOCK_PROCESS_CPUTIME_ID, leave untouched */
|
|
|
|
|
{
|
|
|
|
|
real_req = *req;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else /* sleep for a relative time interval */
|
|
|
|
|
{
|
|
|
|
|
if (user_rate_set && !dont_fake && ((clock_id == CLOCK_REALTIME) || (clock_id == CLOCK_MONOTONIC))) /* don't touch CLOCK_PROCESS_CPUTIME_ID */
|
|
|
|
|
{
|
|
|
|
|
timespecmul(req, 1.0 / user_rate, &real_req);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
real_req = *req;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_clock_nanosleep)(clock_id, flags, &real_req, rem));
|
|
|
|
|
if (result == -1)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
/* fake returned parts */
|
|
|
|
|
if ((rem != NULL) && ((rem->tv_sec != 0) || (rem->tv_nsec != 0)))
|
|
|
|
|
{
|
|
|
|
|
if (user_rate_set && !dont_fake)
|
|
|
|
|
{
|
|
|
|
|
timespecmul(rem, user_rate, rem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* return the result to the caller */
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/*
|
2013-09-01 16:04:21 +02:00
|
|
|
* Faked usleep()
|
|
|
|
|
*/
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_usleep(useconds_t usec)
|
|
|
|
|
#else
|
2013-09-01 16:04:21 +02:00
|
|
|
int usleep(useconds_t usec)
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
{
|
|
|
|
|
int result;
|
2014-12-10 14:55:01 -07:00
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-04 13:05:04 +02:00
|
|
|
if (user_rate_set && !dont_fake)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-04 13:05:04 +02:00
|
|
|
struct timespec real_req;
|
|
|
|
|
|
|
|
|
|
if (real_nanosleep == NULL)
|
|
|
|
|
{
|
|
|
|
|
/* fall back to usleep() */
|
|
|
|
|
if (real_usleep == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = (*usleep)((1.0 / user_rate) * usec));
|
|
|
|
|
#else
|
2013-09-04 13:05:04 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_usleep)((1.0 / user_rate) * usec));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-04 13:05:04 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
real_req.tv_sec = usec / 1000000;
|
|
|
|
|
real_req.tv_nsec = (usec % 1000000) * 1000;
|
|
|
|
|
timespecmul(&real_req, 1.0 / user_rate, &real_req);
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = (*nanosleep)(&real_req, NULL));
|
|
|
|
|
#else
|
2013-09-04 13:05:04 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_nanosleep)(&real_req, NULL));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-04 13:05:04 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = (*usleep)(usec));
|
|
|
|
|
#else
|
2013-09-04 13:05:04 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_usleep)(usec));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2013-08-28 18:22:51 +02:00
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/*
|
2013-09-01 16:04:21 +02:00
|
|
|
* Faked sleep()
|
|
|
|
|
*/
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
unsigned int macos_sleep(unsigned int seconds)
|
|
|
|
|
#else
|
2013-09-01 16:04:21 +02:00
|
|
|
unsigned int sleep(unsigned int seconds)
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
{
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-04 13:05:04 +02:00
|
|
|
if (user_rate_set && !dont_fake)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-10-30 21:47:56 +01:00
|
|
|
if (real_nanosleep == NULL)
|
2013-09-04 13:05:04 +02:00
|
|
|
{
|
|
|
|
|
/* fall back to sleep */
|
|
|
|
|
unsigned int ret;
|
2013-10-30 21:47:56 +01:00
|
|
|
if (real_sleep == NULL)
|
2013-09-04 13:05:04 +02:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(ret = (*sleep)((1.0 / user_rate) * seconds));
|
|
|
|
|
#else
|
2013-09-04 13:05:04 +02:00
|
|
|
DONT_FAKE_TIME(ret = (*real_sleep)((1.0 / user_rate) * seconds));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-04 13:05:04 +02:00
|
|
|
return (user_rate_set && !dont_fake)?(user_rate * ret):ret;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
struct timespec real_req = {seconds, 0}, rem;
|
|
|
|
|
timespecmul(&real_req, 1.0 / user_rate, &real_req);
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = (*nanosleep)(&real_req, &rem));
|
|
|
|
|
#else
|
2013-09-04 13:05:04 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_nanosleep)(&real_req, &rem));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-04 13:05:04 +02:00
|
|
|
if (result == -1)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2013-08-28 18:22:51 +02:00
|
|
|
|
2013-09-04 13:05:04 +02:00
|
|
|
/* fake returned parts */
|
|
|
|
|
if ((rem.tv_sec != 0) || (rem.tv_nsec != 0))
|
|
|
|
|
{
|
|
|
|
|
timespecmul(&rem, user_rate, &rem);
|
|
|
|
|
}
|
|
|
|
|
/* return the result to the caller */
|
|
|
|
|
return rem.tv_sec;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* no need to fake anything */
|
|
|
|
|
unsigned int ret;
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(ret = (*sleep)(seconds));
|
|
|
|
|
#else
|
2013-09-04 13:05:04 +02:00
|
|
|
DONT_FAKE_TIME(ret = (*real_sleep)(seconds));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-04 13:05:04 +02:00
|
|
|
return ret;
|
|
|
|
|
}
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/*
|
2013-09-01 16:04:21 +02:00
|
|
|
* Faked alarm()
|
2013-09-04 13:05:04 +02:00
|
|
|
* @note due to rounding alarm(2) with faketime -f '+0 x7' won't wait 2/7
|
|
|
|
|
* wall clock seconds but 0 seconds
|
2013-09-01 16:04:21 +02:00
|
|
|
*/
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
unsigned int macos_alarm(unsigned int seconds)
|
|
|
|
|
#else
|
2013-09-01 16:04:21 +02:00
|
|
|
unsigned int alarm(unsigned int seconds)
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
{
|
|
|
|
|
unsigned int ret;
|
|
|
|
|
unsigned int seconds_real = (user_rate_set && !dont_fake)?((1.0 / user_rate) * seconds):seconds;
|
2014-12-10 14:55:01 -07:00
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-04 12:50:00 +02:00
|
|
|
if (real_alarm == NULL)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(ret = (*alarm)(seconds_real));
|
|
|
|
|
#else
|
2013-09-01 16:04:21 +02:00
|
|
|
DONT_FAKE_TIME(ret = (*real_alarm)(seconds_real));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
return (user_rate_set && !dont_fake)?(user_rate * ret):ret;
|
2013-08-28 18:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/*
|
2013-08-28 19:32:26 +02:00
|
|
|
* Faked ppoll()
|
|
|
|
|
*/
|
|
|
|
|
int ppoll(struct pollfd *fds, nfds_t nfds,
|
2013-10-30 21:47:56 +01:00
|
|
|
const struct timespec *timeout_ts, const sigset_t *sigmask)
|
2013-08-28 19:32:26 +02:00
|
|
|
{
|
|
|
|
|
struct timespec real_timeout, *real_timeout_pt;
|
|
|
|
|
int ret;
|
|
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-04 12:50:00 +02:00
|
|
|
if (real_ppoll == NULL)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-08-28 19:32:26 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-09-04 12:50:00 +02:00
|
|
|
if (timeout_ts != NULL)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2022-04-02 13:52:18 +02:00
|
|
|
if (user_rate_set && !dont_fake && ((timeout_ts->tv_sec > 0) || (timeout_ts->tv_nsec > 0)))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-08-28 19:32:26 +02:00
|
|
|
timespecmul(timeout_ts, 1.0 / user_rate, &real_timeout);
|
|
|
|
|
real_timeout_pt = &real_timeout;
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-08-28 19:32:26 +02:00
|
|
|
/* cast away constness */
|
|
|
|
|
real_timeout_pt = (struct timespec *)timeout_ts;
|
|
|
|
|
}
|
2013-11-07 19:35:18 +01:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-08-28 19:32:26 +02:00
|
|
|
real_timeout_pt = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DONT_FAKE_TIME(ret = (*real_ppoll)(fds, nfds, real_timeout_pt, sigmask));
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 23:39:36 +02:00
|
|
|
#ifdef __linux__
|
2019-08-22 23:29:48 +02:00
|
|
|
/*
|
|
|
|
|
* Faked epoll_wait()
|
|
|
|
|
*/
|
|
|
|
|
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
|
|
|
|
|
{
|
|
|
|
|
int ret, real_timeout;
|
|
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2019-08-22 23:29:48 +02:00
|
|
|
if (real_epoll_wait == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (user_rate_set && !dont_fake && timeout > 0)
|
|
|
|
|
{
|
|
|
|
|
real_timeout = (int) timeout * 1.0/user_rate;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
real_timeout = timeout;
|
|
|
|
|
}
|
|
|
|
|
DONT_FAKE_TIME(ret = (*real_epoll_wait)(epfd, events, maxevents, real_timeout));
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Faked epoll_pwait()
|
|
|
|
|
*/
|
|
|
|
|
int epoll_pwait(int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *sigmask)
|
|
|
|
|
{
|
|
|
|
|
int ret, real_timeout;
|
|
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2019-08-22 23:29:48 +02:00
|
|
|
if (real_epoll_pwait == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (user_rate_set && !dont_fake && timeout > 0)
|
|
|
|
|
{
|
|
|
|
|
real_timeout = (int) timeout * 1.0/user_rate;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
real_timeout = timeout;
|
|
|
|
|
}
|
|
|
|
|
DONT_FAKE_TIME(ret = (*real_epoll_pwait)(epfd, events, maxevents, real_timeout, sigmask));
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2019-08-22 23:39:36 +02:00
|
|
|
#endif
|
2019-08-22 23:29:48 +02:00
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/*
|
2013-08-28 19:32:26 +02:00
|
|
|
* Faked poll()
|
|
|
|
|
*/
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
|
|
|
|
#else
|
2013-08-28 19:32:26 +02:00
|
|
|
int poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-08-28 19:32:26 +02:00
|
|
|
{
|
|
|
|
|
int ret, timeout_real = (user_rate_set && !dont_fake && (timeout > 0))?(timeout / user_rate):timeout;
|
2014-12-10 14:55:01 -07:00
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-04 12:50:00 +02:00
|
|
|
if (real_poll == NULL)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-08-28 19:32:26 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(ret = (*poll)(fds, nfds, timeout_real));
|
|
|
|
|
#else
|
2013-08-28 19:32:26 +02:00
|
|
|
DONT_FAKE_TIME(ret = (*real_poll)(fds, nfds, timeout_real));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-08-28 19:32:26 +02:00
|
|
|
return ret;
|
|
|
|
|
}
|
2014-08-26 00:18:02 -04:00
|
|
|
|
2017-05-18 17:52:03 -07:00
|
|
|
/*
|
|
|
|
|
* Faked select()
|
|
|
|
|
*/
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_select(int nfds, fd_set *readfds,
|
|
|
|
|
fd_set *writefds,
|
|
|
|
|
fd_set *errorfds,
|
|
|
|
|
struct timeval *timeout)
|
|
|
|
|
#else
|
2017-05-18 17:52:03 -07:00
|
|
|
int select(int nfds, fd_set *readfds,
|
2017-06-07 20:37:12 +02:00
|
|
|
fd_set *writefds,
|
|
|
|
|
fd_set *errorfds,
|
|
|
|
|
struct timeval *timeout)
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2017-05-18 17:52:03 -07:00
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
struct timeval timeout_real;
|
|
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2017-06-07 20:37:12 +02:00
|
|
|
|
2017-05-18 17:52:03 -07:00
|
|
|
if (real_select == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 20:37:12 +02:00
|
|
|
if (timeout != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (user_rate_set && !dont_fake && (timeout->tv_sec > 0 || timeout->tv_usec > 0))
|
|
|
|
|
{
|
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
|
|
ts.tv_sec = timeout->tv_sec;
|
|
|
|
|
ts.tv_nsec = timeout->tv_usec * 1000;
|
|
|
|
|
|
|
|
|
|
timespecmul(&ts, 1.0 / user_rate, &ts);
|
|
|
|
|
|
|
|
|
|
timeout_real.tv_sec = ts.tv_sec;
|
|
|
|
|
timeout_real.tv_usec = ts.tv_nsec / 1000;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
timeout_real.tv_sec = timeout->tv_sec;
|
|
|
|
|
timeout_real.tv_usec = timeout->tv_usec;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(ret = (*select)(nfds, readfds, writefds, errorfds, timeout == NULL ? timeout : &timeout_real));
|
|
|
|
|
#else
|
2017-06-08 19:50:34 +02:00
|
|
|
DONT_FAKE_TIME(ret = (*real_select)(nfds, readfds, writefds, errorfds, timeout == NULL ? timeout : &timeout_real));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2022-04-02 13:47:04 +02:00
|
|
|
|
|
|
|
|
/* scale timeout back if user rate is set, #382 */
|
2022-05-14 23:09:03 +02:00
|
|
|
if (user_rate_set && (timeout != NULL))
|
2022-04-02 13:47:04 +02:00
|
|
|
{
|
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
|
|
ts.tv_sec = timeout_real.tv_sec;
|
|
|
|
|
ts.tv_nsec = timeout_real.tv_usec * 1000;
|
|
|
|
|
|
|
|
|
|
timespecmul(&ts, user_rate, &ts);
|
|
|
|
|
|
|
|
|
|
timeout->tv_sec = ts.tv_sec;
|
|
|
|
|
timeout->tv_usec = ts.tv_nsec / 1000;
|
2022-05-14 23:09:03 +02:00
|
|
|
}
|
2022-04-02 13:47:04 +02:00
|
|
|
|
2017-05-18 17:52:03 -07:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 23:29:48 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
|
/*
|
|
|
|
|
* Faked pselect()
|
|
|
|
|
*/
|
|
|
|
|
int pselect(int nfds, fd_set *readfds,
|
|
|
|
|
fd_set *writefds,
|
|
|
|
|
fd_set *errorfds,
|
|
|
|
|
const struct timespec *timeout,
|
|
|
|
|
const sigset_t *sigmask)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
struct timespec timeout_real;
|
|
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2019-08-22 23:29:48 +02:00
|
|
|
|
|
|
|
|
if (real_pselect == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timeout != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (user_rate_set && !dont_fake && (timeout->tv_sec > 0 || timeout->tv_nsec > 0))
|
|
|
|
|
{
|
|
|
|
|
timespecmul(timeout, 1.0 / user_rate, &timeout_real);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
timeout_real.tv_sec = timeout->tv_sec;
|
|
|
|
|
timeout_real.tv_nsec = timeout->tv_nsec;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DONT_FAKE_TIME(ret = (*real_pselect)(nfds, readfds, writefds, errorfds, timeout == NULL ? timeout : &timeout_real, sigmask));
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-08-26 00:18:02 -04:00
|
|
|
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
struct timespec real_abs_timeout, *real_abs_timeout_pt;
|
|
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
|
if (abs_timeout == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(sem_timedwait)) return -1;
|
2014-08-26 00:18:02 -04:00
|
|
|
|
|
|
|
|
if (!dont_fake)
|
|
|
|
|
{
|
|
|
|
|
struct timespec tdiff, timeadj;
|
|
|
|
|
|
2017-11-17 20:27:58 +01:00
|
|
|
timespecsub(abs_timeout, &user_faked_time_timespec, &tdiff);
|
2014-08-26 00:18:02 -04:00
|
|
|
|
|
|
|
|
if (user_rate_set)
|
|
|
|
|
{
|
|
|
|
|
timespecmul(&tdiff, user_rate, &timeadj);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
timeadj = tdiff;
|
|
|
|
|
}
|
2017-11-17 20:27:58 +01:00
|
|
|
timespecadd(&ftpl_starttime.real, &timeadj, &real_abs_timeout);
|
2014-08-26 00:18:02 -04:00
|
|
|
real_abs_timeout_pt = &real_abs_timeout;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* cast away constness */
|
|
|
|
|
real_abs_timeout_pt = (struct timespec *)abs_timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_sem_timedwait)(sem, real_abs_timeout_pt));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-05-11 21:47:35 +02:00
|
|
|
|
|
|
|
|
/* EXPERIMENTAL */
|
|
|
|
|
int sem_clockwait(sem_t *sem, clockid_t clockid, const struct timespec *abstime)
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
struct timespec real_abstime, *real_abstime_pt;
|
|
|
|
|
|
2022-05-16 19:20:33 +02:00
|
|
|
if ((!fake_monotonic_clock) && (clockid == CLOCK_MONOTONIC))
|
|
|
|
|
{
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, abstime));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 21:47:35 +02:00
|
|
|
/* sanity check */
|
|
|
|
|
if (abstime == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!CHECK_MISSING_REAL(sem_clockwait)) return -1;
|
|
|
|
|
|
|
|
|
|
if (!dont_fake)
|
|
|
|
|
{
|
|
|
|
|
struct timespec tdiff, timeadj;
|
|
|
|
|
|
|
|
|
|
timespecsub(abstime, &user_faked_time_timespec, &tdiff);
|
|
|
|
|
|
|
|
|
|
if (user_rate_set)
|
|
|
|
|
{
|
2022-05-14 23:09:03 +02:00
|
|
|
timespecmul(&tdiff, 1.0 / user_rate, &timeadj);
|
2022-05-11 21:47:35 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
timeadj = tdiff;
|
|
|
|
|
}
|
|
|
|
|
if (clockid == CLOCK_REALTIME)
|
|
|
|
|
{
|
|
|
|
|
timespecadd(&ftpl_starttime.real, &timeadj, &real_abstime);
|
|
|
|
|
}
|
|
|
|
|
if (clockid == CLOCK_MONOTONIC)
|
|
|
|
|
{
|
|
|
|
|
timespecadd(&ftpl_starttime.mon, &timeadj, &real_abstime);
|
|
|
|
|
}
|
|
|
|
|
real_abstime_pt = &real_abstime;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* cast away constness */
|
|
|
|
|
real_abstime_pt = (struct timespec *)abstime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, real_abstime_pt));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2013-09-04 14:20:43 +02:00
|
|
|
#endif
|
2013-08-28 19:32:26 +02:00
|
|
|
|
2013-09-03 17:13:16 +02:00
|
|
|
#ifndef __APPLE__
|
2013-09-04 14:20:43 +02:00
|
|
|
#ifdef FAKE_TIMERS
|
2013-09-30 16:36:13 +02:00
|
|
|
|
|
|
|
|
/* timer related functions and structures */
|
|
|
|
|
typedef union {
|
|
|
|
|
int int_member;
|
|
|
|
|
timer_t timer_t_member;
|
|
|
|
|
} timer_t_or_int;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Faketime's function implementation's compatibility mode
|
|
|
|
|
*/
|
2021-03-30 12:34:26 -04:00
|
|
|
typedef enum {
|
|
|
|
|
FT_COMPAT_GLIBC_2_2,
|
|
|
|
|
FT_COMPAT_GLIBC_2_3_3,
|
|
|
|
|
FT_FD,
|
|
|
|
|
} ft_lib_compat_timer;
|
2013-09-30 16:36:13 +02:00
|
|
|
|
|
|
|
|
|
2013-09-03 17:13:16 +02:00
|
|
|
/*
|
|
|
|
|
* Faked timer_settime()
|
|
|
|
|
* Does not affect timer speed when stepping clock with each time() call.
|
|
|
|
|
*/
|
2013-09-30 16:36:13 +02:00
|
|
|
static int
|
|
|
|
|
timer_settime_common(timer_t_or_int timerid, int flags,
|
|
|
|
|
const struct itimerspec *new_value,
|
2021-03-30 12:34:26 -04:00
|
|
|
struct itimerspec *old_value, ft_lib_compat_timer compat,
|
|
|
|
|
int abstime_flag)
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
|
|
|
|
int result;
|
2013-09-30 16:36:13 +02:00
|
|
|
struct itimerspec new_real;
|
|
|
|
|
struct itimerspec *new_real_pt = &new_real;
|
2013-09-03 17:13:16 +02:00
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-04 12:50:00 +02:00
|
|
|
if (new_value == NULL)
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
|
|
|
|
new_real_pt = NULL;
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else if (dont_fake)
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
2021-09-21 20:52:08 +00:00
|
|
|
/* cast away constness */
|
2013-09-03 17:13:16 +02:00
|
|
|
new_real_pt = (struct itimerspec *)new_value;
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
2013-09-30 16:36:13 +02:00
|
|
|
/* set it_value */
|
|
|
|
|
if ((new_value->it_value.tv_sec != 0) ||
|
2013-10-30 21:47:56 +01:00
|
|
|
(new_value->it_value.tv_nsec != 0))
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
2021-03-30 12:34:26 -04:00
|
|
|
if (flags & abstime_flag)
|
2013-09-30 16:36:13 +02:00
|
|
|
{
|
|
|
|
|
struct timespec tdiff, timeadj;
|
|
|
|
|
timespecsub(&new_value->it_value, &user_faked_time_timespec, &timeadj);
|
2013-10-30 21:47:56 +01:00
|
|
|
if (user_rate_set)
|
2013-09-30 16:36:13 +02:00
|
|
|
{
|
|
|
|
|
timespecmul(&timeadj, 1.0/user_rate, &tdiff);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tdiff = timeadj;
|
|
|
|
|
}
|
|
|
|
|
/* only CLOCK_REALTIME is handled */
|
|
|
|
|
timespecadd(&ftpl_starttime.real, &tdiff, &new_real.it_value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-10-30 21:47:56 +01:00
|
|
|
if (user_rate_set)
|
2013-09-30 16:36:13 +02:00
|
|
|
{
|
|
|
|
|
timespecmul(&new_value->it_value, 1.0/user_rate, &new_real.it_value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
new_real.it_value = new_value->it_value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
2013-09-30 16:36:13 +02:00
|
|
|
new_real.it_value = new_value->it_value;
|
2013-09-03 17:13:16 +02:00
|
|
|
}
|
2013-09-30 16:36:13 +02:00
|
|
|
/* set it_interval */
|
|
|
|
|
if (user_rate_set && ((new_value->it_interval.tv_sec != 0) ||
|
|
|
|
|
(new_value->it_interval.tv_nsec != 0)))
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
|
|
|
|
timespecmul(&new_value->it_interval, 1.0/user_rate, &new_real.it_interval);
|
|
|
|
|
}
|
2013-09-30 16:36:13 +02:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
new_real.it_interval = new_value->it_interval;
|
|
|
|
|
}
|
2013-09-03 17:13:16 +02:00
|
|
|
}
|
2013-09-30 16:36:13 +02:00
|
|
|
|
|
|
|
|
switch (compat)
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
2013-09-30 16:36:13 +02:00
|
|
|
case FT_COMPAT_GLIBC_2_2:
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_timer_settime_22)(timerid.int_member, flags,
|
|
|
|
|
new_real_pt, old_value));
|
2013-10-04 22:34:12 +02:00
|
|
|
break;
|
2013-11-07 19:35:18 +01:00
|
|
|
case FT_COMPAT_GLIBC_2_3_3:
|
2013-09-30 16:36:13 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_timer_settime_233)(timerid.timer_t_member,
|
|
|
|
|
flags, new_real_pt, old_value));
|
2013-10-04 22:34:12 +02:00
|
|
|
break;
|
2021-03-30 12:34:26 -04:00
|
|
|
case FT_FD:
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_timerfd_settime)(timerid.int_member,
|
|
|
|
|
flags, new_real_pt, old_value));
|
|
|
|
|
break;
|
2013-10-04 22:54:07 +02:00
|
|
|
default:
|
|
|
|
|
result = -1;
|
|
|
|
|
break;
|
2013-09-03 17:13:16 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if (result == -1)
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* fake returned parts */
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((old_value != NULL) && !dont_fake)
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
2013-09-30 16:36:13 +02:00
|
|
|
if ((old_value->it_value.tv_sec != 0) ||
|
|
|
|
|
(old_value->it_value.tv_nsec != 0))
|
|
|
|
|
{
|
|
|
|
|
result = fake_clock_gettime(CLOCK_REALTIME, &old_value->it_value);
|
|
|
|
|
}
|
|
|
|
|
if (user_rate_set && ((old_value->it_interval.tv_sec != 0) ||
|
|
|
|
|
(old_value->it_interval.tv_nsec != 0)))
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
2013-09-30 16:36:13 +02:00
|
|
|
timespecmul(&old_value->it_interval, user_rate, &old_value->it_interval);
|
2013-09-03 17:13:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-09-30 16:36:13 +02:00
|
|
|
|
2013-09-03 17:13:16 +02:00
|
|
|
/* return the result to the caller */
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-30 16:36:13 +02:00
|
|
|
/*
|
|
|
|
|
* Faked timer_settime() compatible with implementation in GLIBC 2.2
|
|
|
|
|
*/
|
|
|
|
|
int timer_settime_22(int timerid, int flags,
|
|
|
|
|
const struct itimerspec *new_value,
|
|
|
|
|
struct itimerspec *old_value)
|
|
|
|
|
{
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-30 16:36:13 +02:00
|
|
|
if (real_timer_settime_22 == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-05-27 00:44:13 +02:00
|
|
|
timer_t_or_int temp;
|
|
|
|
|
temp.int_member = timerid;
|
|
|
|
|
return (timer_settime_common(temp, flags, new_value, old_value,
|
2021-03-30 12:34:26 -04:00
|
|
|
FT_COMPAT_GLIBC_2_2, TIMER_ABSTIME));
|
2013-09-30 16:36:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Faked timer_settime() compatible with implementation in GLIBC 2.3.3
|
|
|
|
|
*/
|
|
|
|
|
int timer_settime_233(timer_t timerid, int flags,
|
|
|
|
|
const struct itimerspec *new_value,
|
|
|
|
|
struct itimerspec *old_value)
|
|
|
|
|
{
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-30 16:36:13 +02:00
|
|
|
if (real_timer_settime_233 == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-05-27 00:44:13 +02:00
|
|
|
timer_t_or_int temp;
|
|
|
|
|
temp.timer_t_member = timerid;
|
|
|
|
|
return (timer_settime_common(temp, flags, new_value, old_value,
|
2021-03-30 12:34:26 -04:00
|
|
|
FT_COMPAT_GLIBC_2_3_3, TIMER_ABSTIME));
|
2013-09-30 16:36:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2013-09-03 17:13:16 +02:00
|
|
|
* Faked timer_gettime()
|
|
|
|
|
* Does not affect timer speed when stepping clock with each time() call.
|
|
|
|
|
*/
|
2018-01-01 21:46:09 +01:00
|
|
|
int timer_gettime_common(timer_t_or_int timerid, struct itimerspec *curr_value, ft_lib_compat_timer compat)
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-30 16:36:13 +02:00
|
|
|
if (real_timer_gettime_233 == NULL)
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-30 16:36:13 +02:00
|
|
|
switch (compat)
|
|
|
|
|
{
|
|
|
|
|
case FT_COMPAT_GLIBC_2_2:
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_timer_gettime_22)(timerid.int_member, curr_value));
|
2013-10-04 22:34:12 +02:00
|
|
|
break;
|
2013-09-30 16:36:13 +02:00
|
|
|
case FT_COMPAT_GLIBC_2_3_3:
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_timer_gettime_233)(timerid.timer_t_member, curr_value));
|
2013-10-04 22:34:12 +02:00
|
|
|
break;
|
2021-03-30 12:34:26 -04:00
|
|
|
case FT_FD:
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_timerfd_gettime)(timerid.int_member, curr_value));
|
|
|
|
|
break;
|
2013-10-04 22:54:07 +02:00
|
|
|
default:
|
|
|
|
|
result = -1;
|
|
|
|
|
break;
|
2013-09-30 16:36:13 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if (result == -1)
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* fake returned parts */
|
2013-09-04 12:50:00 +02:00
|
|
|
if (curr_value != NULL)
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
2013-09-04 12:50:00 +02:00
|
|
|
if (user_rate_set && !dont_fake)
|
2013-09-03 17:13:16 +02:00
|
|
|
{
|
|
|
|
|
timespecmul(&curr_value->it_interval, user_rate, &curr_value->it_interval);
|
|
|
|
|
timespecmul(&curr_value->it_value, user_rate, &curr_value->it_value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* return the result to the caller */
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2013-09-30 16:36:13 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Faked timer_gettime() compatible with implementation in GLIBC 2.2
|
|
|
|
|
*/
|
|
|
|
|
int timer_gettime_22(timer_t timerid, struct itimerspec *curr_value)
|
|
|
|
|
{
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-30 16:36:13 +02:00
|
|
|
if (real_timer_gettime_22 == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-05-27 00:44:13 +02:00
|
|
|
timer_t_or_int temp;
|
|
|
|
|
temp.timer_t_member = timerid;
|
|
|
|
|
return (timer_gettime_common(temp, curr_value,
|
2013-09-30 16:36:13 +02:00
|
|
|
FT_COMPAT_GLIBC_2_2));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Faked timer_gettime() compatible with implementation in GLIBC 2.3.3
|
|
|
|
|
*/
|
|
|
|
|
int timer_gettime_233(timer_t timerid, struct itimerspec *curr_value)
|
|
|
|
|
{
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-30 16:36:13 +02:00
|
|
|
if (real_timer_gettime_233 == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-05-27 00:44:13 +02:00
|
|
|
timer_t_or_int temp;
|
|
|
|
|
temp.timer_t_member = timerid;
|
|
|
|
|
return (timer_gettime_common(temp, curr_value,
|
2013-09-30 16:36:13 +02:00
|
|
|
FT_COMPAT_GLIBC_2_3_3));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__asm__(".symver timer_gettime_22, timer_gettime@GLIBC_2.2");
|
|
|
|
|
__asm__(".symver timer_gettime_233, timer_gettime@@GLIBC_2.3.3");
|
|
|
|
|
__asm__(".symver timer_settime_22, timer_settime@GLIBC_2.2");
|
|
|
|
|
__asm__(".symver timer_settime_233, timer_settime@@GLIBC_2.3.3");
|
|
|
|
|
|
2021-03-30 12:34:26 -04:00
|
|
|
#ifdef __linux__
|
|
|
|
|
/*
|
|
|
|
|
* Faked timerfd_settime
|
|
|
|
|
*/
|
|
|
|
|
int timerfd_settime(int fd, int flags,
|
|
|
|
|
const struct itimerspec *new_value,
|
|
|
|
|
struct itimerspec *old_value)
|
|
|
|
|
{
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2021-03-30 12:34:26 -04:00
|
|
|
if (real_timerfd_settime == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-05-27 00:44:13 +02:00
|
|
|
timer_t_or_int temp;
|
|
|
|
|
temp.int_member = fd;
|
|
|
|
|
return (timer_settime_common(temp, flags, new_value, old_value, FT_FD,
|
2021-03-30 12:34:26 -04:00
|
|
|
TFD_TIMER_ABSTIME));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Faked timerfd_gettime()
|
|
|
|
|
*/
|
|
|
|
|
int timerfd_gettime(int fd, struct itimerspec *curr_value)
|
|
|
|
|
{
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2021-03-30 12:34:26 -04:00
|
|
|
if (real_timerfd_gettime == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-05-27 00:44:13 +02:00
|
|
|
timer_t_or_int temp;
|
|
|
|
|
temp.int_member = fd;
|
|
|
|
|
return (timer_gettime_common(temp, curr_value, FT_FD));
|
2021-03-30 12:34:26 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-09-03 17:13:16 +02:00
|
|
|
#endif
|
2013-09-04 14:20:43 +02:00
|
|
|
#endif
|
2013-09-03 17:13:16 +02:00
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:42:10 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
* Faked system functions: basic time functions === FAKE(TIME)
|
|
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
2013-09-05 11:15:51 +02:00
|
|
|
/*
|
|
|
|
|
* time() implementation using clock_gettime()
|
|
|
|
|
* @note Does not check for EFAULT, see man 2 time
|
|
|
|
|
*/
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
time_t macos_time(time_t *time_tptr)
|
|
|
|
|
#else
|
2013-09-04 12:50:00 +02:00
|
|
|
time_t time(time_t *time_tptr)
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-05 11:15:51 +02:00
|
|
|
struct timespec tp;
|
2013-09-03 13:42:10 +02:00
|
|
|
time_t result;
|
2013-09-05 11:15:51 +02:00
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = (*clock_gettime)(CLOCK_REALTIME, &tp));
|
|
|
|
|
#else
|
2013-09-05 11:15:51 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_clock_gettime)(CLOCK_REALTIME, &tp));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-05 11:15:51 +02:00
|
|
|
if (result == -1) return -1;
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
/* pass the real current time to our faking version, overwriting it */
|
2013-09-05 11:15:51 +02:00
|
|
|
(void)fake_clock_gettime(CLOCK_REALTIME, &tp);
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-05 11:15:51 +02:00
|
|
|
if (time_tptr != NULL)
|
|
|
|
|
{
|
|
|
|
|
*time_tptr = tp.tv_sec;
|
|
|
|
|
}
|
|
|
|
|
return tp.tv_sec;
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_ftime(struct timeb *tb)
|
|
|
|
|
#else
|
2013-09-05 11:15:51 +02:00
|
|
|
int ftime(struct timeb *tb)
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-05 11:15:51 +02:00
|
|
|
struct timespec tp;
|
2013-09-03 13:42:10 +02:00
|
|
|
int result;
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-03 13:42:10 +02:00
|
|
|
/* sanity check */
|
2013-09-05 11:15:51 +02:00
|
|
|
if (tb == NULL)
|
2013-09-03 13:42:10 +02:00
|
|
|
return 0; /* ftime() always returns 0, see manpage */
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
/* Check whether we've got a pointer to the real ftime() function yet */
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(ftime)) return 0;
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-05 11:15:51 +02:00
|
|
|
/* initialize our TZ result with the real current time */
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = (*ftime)(tb));
|
|
|
|
|
#else
|
2013-09-05 11:15:51 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_ftime)(tb));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-05 11:15:51 +02:00
|
|
|
if (result == -1)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-05 11:15:51 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_clock_gettime)(CLOCK_REALTIME, &tp));
|
|
|
|
|
if (result == -1) return -1;
|
|
|
|
|
|
|
|
|
|
/* pass the real current time to our faking version, overwriting it */
|
|
|
|
|
(void)fake_clock_gettime(CLOCK_REALTIME, &tp);
|
|
|
|
|
|
|
|
|
|
tb->time = tp.tv_sec;
|
|
|
|
|
tb->millitm = tp.tv_nsec / 1000000;
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
/* return the result to the caller */
|
|
|
|
|
return result; /* will always be 0 (see manpage) */
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_gettimeofday(struct timeval *tv, void *tz)
|
|
|
|
|
#else
|
2013-09-04 12:50:00 +02:00
|
|
|
int gettimeofday(struct timeval *tv, void *tz)
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-03 13:42:10 +02:00
|
|
|
int result;
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2013-09-03 13:42:10 +02:00
|
|
|
/* sanity check */
|
2013-09-04 12:50:00 +02:00
|
|
|
if (tv == NULL)
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
/* Check whether we've got a pointer to the real ftime() function yet */
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(gettimeofday)) return -1;
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
/* initialize our result with the real current time */
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = (*gettimeofday)(tv, tz));
|
|
|
|
|
#else
|
2013-09-03 13:42:10 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_gettimeofday)(tv, tz));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-03 13:42:10 +02:00
|
|
|
if (result == -1) return result; /* original function failed */
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
/* pass the real current time to our faking version, overwriting it */
|
2013-10-04 22:54:07 +02:00
|
|
|
result = fake_gettimeofday(tv);
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
/* return the result to the caller */
|
|
|
|
|
return result;
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_clock_gettime(clockid_t clk_id, struct timespec *tp)
|
|
|
|
|
#else
|
2013-09-04 12:50:00 +02:00
|
|
|
int clock_gettime(clockid_t clk_id, struct timespec *tp)
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-03 13:42:10 +02:00
|
|
|
int result;
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2025-01-21 18:44:42 +00:00
|
|
|
ftpl_init();
|
|
|
|
|
// If ftpl_init ends up recursing, pthread_once will deadlock.
|
|
|
|
|
// (Previously we attempted to detect this situation, and bomb out,
|
|
|
|
|
// but the approach taken wasn't thread-safe and broke in practice.)
|
|
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
/* sanity check */
|
2013-09-04 12:50:00 +02:00
|
|
|
if (tp == NULL)
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(clock_gettime)) return -1;
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
/* initialize our result with the real current time */
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = (*clock_gettime)(clk_id, tp));
|
|
|
|
|
#else
|
2013-09-03 13:42:10 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, tp));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2013-09-03 13:42:10 +02:00
|
|
|
if (result == -1) return result; /* original function failed */
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
/* pass the real current time to our faking version, overwriting it */
|
2018-01-01 21:46:35 +01:00
|
|
|
if (fake_monotonic_clock || (clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW
|
|
|
|
|
#ifdef CLOCK_MONOTONIC_COARSE
|
|
|
|
|
&& clk_id != CLOCK_MONOTONIC_COARSE
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef CLOCK_BOOTTIME
|
|
|
|
|
&& clk_id != CLOCK_BOOTTIME
|
|
|
|
|
#endif
|
|
|
|
|
))
|
2014-07-24 16:54:08 -07:00
|
|
|
{
|
|
|
|
|
result = fake_clock_gettime(clk_id, tp);
|
|
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
/* return the result to the caller */
|
|
|
|
|
return result;
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-12 22:23:16 +00:00
|
|
|
/* this is used by 32-bit architectures only */
|
|
|
|
|
int __clock_gettime64(clockid_t clk_id, struct __timespec64 *tp64)
|
|
|
|
|
{
|
|
|
|
|
struct timespec tp;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
result = clock_gettime(clk_id, &tp);
|
|
|
|
|
tp64->tv_sec = tp.tv_sec;
|
|
|
|
|
tp64->tv_nsec = tp.tv_nsec;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2013-09-03 13:42:10 +02:00
|
|
|
|
2025-01-17 12:08:23 +00:00
|
|
|
/* this is used by 32-bit architectures only */
|
|
|
|
|
int __gettimeofday64(struct __timeval64 *tv64, void *tz)
|
|
|
|
|
{
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
result = gettimeofday(&tv, tz);
|
|
|
|
|
tv64->tv_sec = tv.tv_sec;
|
|
|
|
|
tv64->tv_usec = tv.tv_usec;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-17 09:03:21 +00:00
|
|
|
/* this is used by 32-bit architectures only */
|
|
|
|
|
uint64_t __time64(uint64_t *write_out)
|
|
|
|
|
{
|
|
|
|
|
struct timespec tp;
|
|
|
|
|
uint64_t output;
|
|
|
|
|
int error;
|
|
|
|
|
|
|
|
|
|
error = clock_gettime(CLOCK_REALTIME, &tp);
|
|
|
|
|
if (error == -1)
|
|
|
|
|
{
|
|
|
|
|
return (uint64_t)error;
|
|
|
|
|
}
|
|
|
|
|
output = tp.tv_sec;
|
|
|
|
|
|
|
|
|
|
if (write_out)
|
|
|
|
|
{
|
|
|
|
|
*write_out = output;
|
|
|
|
|
}
|
|
|
|
|
return output;
|
|
|
|
|
}
|
2013-09-03 13:42:10 +02:00
|
|
|
|
2022-12-20 00:35:53 +08:00
|
|
|
#ifdef TIME_UTC
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_timespec_get(struct timespec *ts, int base)
|
|
|
|
|
#else
|
2021-07-30 13:46:24 -04:00
|
|
|
int timespec_get(struct timespec *ts, int base)
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2021-07-30 13:46:24 -04:00
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2021-07-30 13:46:24 -04:00
|
|
|
/* sanity check */
|
|
|
|
|
if (ts == NULL)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(timespec_get)) return 0;
|
2021-07-30 13:46:24 -04:00
|
|
|
|
|
|
|
|
/* initialize our result with the real current time */
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(result = (*timespec_get)(ts, base));
|
|
|
|
|
#else
|
2021-07-30 13:46:24 -04:00
|
|
|
DONT_FAKE_TIME(result = (*real_timespec_get)(ts, base));
|
2022-02-06 21:39:29 +01:00
|
|
|
#endif
|
2021-07-30 13:46:24 -04:00
|
|
|
if (result == 0) return result; /* original function failed */
|
|
|
|
|
|
|
|
|
|
/* pass the real current time to our faking version, overwriting it */
|
|
|
|
|
(void)fake_clock_gettime(CLOCK_REALTIME, ts);
|
|
|
|
|
|
|
|
|
|
/* return the result to the caller */
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-12-20 00:35:53 +08:00
|
|
|
#endif
|
2021-07-30 13:46:24 -04:00
|
|
|
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:42:10 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
* Parsing the user's faketime requests === PARSE
|
|
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
static void parse_ft_string(const char *user_faked_time)
|
|
|
|
|
{
|
|
|
|
|
struct tm user_faked_time_tm;
|
2025-12-15 11:03:21 +01:00
|
|
|
const char * tmp_time_fmt;
|
2015-12-04 13:41:05 -05:00
|
|
|
char * nstime_str;
|
2014-08-26 10:51:50 -04:00
|
|
|
|
|
|
|
|
if (!strncmp(user_faked_time, user_faked_time_saved, BUFFERLEN))
|
|
|
|
|
{
|
2021-06-22 21:51:28 +02:00
|
|
|
/* No change but eventually when using FAKETIME_FOLLOW_FILE */
|
|
|
|
|
if (user_faked_time[0] != '%')
|
|
|
|
|
return;
|
2014-08-26 10:51:50 -04:00
|
|
|
}
|
|
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
/* check whether the user gave us an absolute time to fake */
|
2013-09-04 12:50:00 +02:00
|
|
|
switch (user_faked_time[0])
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
default: /* Try and interpret this as a specified time */
|
2013-10-28 22:58:03 +01:00
|
|
|
if (ft_mode != FT_NOOP) ft_mode = FT_FREEZE;
|
2013-09-03 13:23:42 +02:00
|
|
|
user_faked_time_tm.tm_isdst = -1;
|
2015-12-04 13:41:05 -05:00
|
|
|
nstime_str = strptime(user_faked_time, user_faked_time_fmt, &user_faked_time_tm);
|
|
|
|
|
|
|
|
|
|
if (NULL != nstime_str)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
user_faked_time_timespec.tv_sec = mktime(&user_faked_time_tm);
|
|
|
|
|
user_faked_time_timespec.tv_nsec = 0;
|
2015-12-04 13:41:05 -05:00
|
|
|
|
|
|
|
|
if (nstime_str[0] == '.')
|
|
|
|
|
{
|
|
|
|
|
double nstime = atof(--nstime_str);
|
|
|
|
|
user_faked_time_timespec.tv_nsec = (nstime - floor(nstime)) * SEC_TO_nSEC;
|
|
|
|
|
}
|
2013-09-03 13:23:42 +02:00
|
|
|
user_faked_time_set = true;
|
2013-11-07 19:35:18 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-02-04 17:32:17 -05:00
|
|
|
fprintf(stderr, "libfaketime: In parse_ft_string(), failed to parse FAKETIME timestamp.\n"
|
|
|
|
|
"Please check specification %s with format %s\n", user_faked_time, user_faked_time_fmt);
|
2013-10-03 15:42:53 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-09-03 13:23:42 +02:00
|
|
|
}
|
2022-02-20 21:22:01 +01:00
|
|
|
goto parse_modifiers;
|
2013-09-03 13:23:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
|
case '-': /* User-specified offset */
|
2013-10-28 22:58:03 +01:00
|
|
|
if (ft_mode != FT_NOOP) ft_mode = FT_START_AT;
|
2013-09-03 13:23:42 +02:00
|
|
|
/* fractional time offsets contributed by Karl Chen in v0.8 */
|
|
|
|
|
double frac_offset = atof(user_faked_time);
|
|
|
|
|
|
|
|
|
|
/* offset is in seconds by default, but the string may contain
|
|
|
|
|
* multipliers...
|
|
|
|
|
*/
|
|
|
|
|
if (strchr(user_faked_time, 'm') != NULL) frac_offset *= 60;
|
|
|
|
|
else if (strchr(user_faked_time, 'h') != NULL) frac_offset *= 60 * 60;
|
|
|
|
|
else if (strchr(user_faked_time, 'd') != NULL) frac_offset *= 60 * 60 * 24;
|
|
|
|
|
else if (strchr(user_faked_time, 'y') != NULL) frac_offset *= 60 * 60 * 24 * 365;
|
|
|
|
|
|
|
|
|
|
user_offset.tv_sec = floor(frac_offset);
|
|
|
|
|
user_offset.tv_nsec = (frac_offset - user_offset.tv_sec) * SEC_TO_nSEC;
|
|
|
|
|
timespecadd(&ftpl_starttime.real, &user_offset, &user_faked_time_timespec);
|
|
|
|
|
goto parse_modifiers;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* Contributed by David North, TDI in version 0.7 */
|
|
|
|
|
case '@': /* Specific time, but clock along relative to that starttime */
|
|
|
|
|
ft_mode = FT_START_AT;
|
|
|
|
|
user_faked_time_tm.tm_isdst = -1;
|
2015-12-04 13:41:05 -05:00
|
|
|
nstime_str = strptime(&user_faked_time[1], user_faked_time_fmt, &user_faked_time_tm);
|
|
|
|
|
if (NULL != nstime_str)
|
|
|
|
|
{
|
|
|
|
|
user_faked_time_timespec.tv_sec = mktime(&user_faked_time_tm);
|
|
|
|
|
user_faked_time_timespec.tv_nsec = 0;
|
2013-09-01 16:04:21 +02:00
|
|
|
|
2015-12-04 13:41:05 -05:00
|
|
|
if (nstime_str[0] == '.')
|
|
|
|
|
{
|
|
|
|
|
double nstime = atof(--nstime_str);
|
|
|
|
|
user_faked_time_timespec.tv_nsec = (nstime - floor(nstime)) * SEC_TO_nSEC;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-02-04 17:32:17 -05:00
|
|
|
fprintf(stderr, "libfaketime: In parse_ft_string(), failed to parse FAKETIME timestamp.\n");
|
2015-12-04 13:41:05 -05:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
2014-08-26 13:23:25 -04:00
|
|
|
|
|
|
|
|
/* Reset starttime */
|
2019-08-20 15:43:29 +02:00
|
|
|
if (NULL == getenv("FAKETIME_DONT_RESET"))
|
2022-06-13 11:42:35 +02:00
|
|
|
reset_time();
|
2013-10-03 15:42:53 +02:00
|
|
|
goto parse_modifiers;
|
|
|
|
|
break;
|
|
|
|
|
|
2019-08-20 19:43:15 +02:00
|
|
|
case '%': /* follow file timestamp as suggested by Hitoshi Harada (umitanuki) */
|
|
|
|
|
ft_mode = FT_START_AT;
|
|
|
|
|
struct stat master_file_stats;
|
|
|
|
|
int ret;
|
|
|
|
|
if (NULL == getenv("FAKETIME_FOLLOW_FILE"))
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "libfaketime: %% operator in FAKETIME setting requires environment variable FAKETIME_FOLLOW_FILE set.\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DONT_FAKE_TIME(ret = stat(getenv("FAKETIME_FOLLOW_FILE"), &master_file_stats));
|
|
|
|
|
if (ret == -1)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "libfaketime: Cannot get timestamp of file %s as requested by %% operator.\n", getenv("FAKETIME_FOLLOW_FILE"));
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
user_faked_time_timespec.tv_sec = master_file_stats.st_mtime;
|
2026-02-17 10:51:56 -05:00
|
|
|
if (NULL == getenv("FAKETIME_FOLLOW_ABSOLUTE"))
|
|
|
|
|
{
|
|
|
|
|
/* Normal FAKETIME_FOLLOW_FILE behavior; fake time coasts between mtime updates */
|
|
|
|
|
user_faked_time_timespec.tv_nsec = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Set fake time to nanosecond-precision mtime */
|
|
|
|
|
user_faked_time_timespec.tv_nsec = master_file_stats.st_mtim.tv_nsec;
|
|
|
|
|
|
|
|
|
|
/* Freeze fake time (mtime is absolute truth in this mode) */
|
|
|
|
|
if (ft_mode != FT_NOOP)
|
|
|
|
|
{
|
|
|
|
|
ft_mode = FT_FREEZE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Bypass parse_modifiers since we have absolute time */
|
|
|
|
|
user_faked_time_set = true;
|
|
|
|
|
}
|
2019-08-20 19:43:15 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (NULL == getenv("FAKETIME_DONT_RESET"))
|
2022-06-13 11:42:35 +02:00
|
|
|
reset_time();
|
2026-02-17 10:51:56 -05:00
|
|
|
if (!user_faked_time_set)
|
|
|
|
|
{
|
|
|
|
|
goto parse_modifiers;
|
|
|
|
|
}
|
2019-08-20 19:43:15 +02:00
|
|
|
break;
|
2019-08-21 10:23:01 +02:00
|
|
|
|
2013-10-03 15:42:53 +02:00
|
|
|
case 'i':
|
|
|
|
|
case 'x': /* Only modifiers are passed, don't fall back to strptime */
|
2013-09-03 13:23:42 +02:00
|
|
|
parse_modifiers:
|
|
|
|
|
/* Speed-up / slow-down contributed by Karl Chen in v0.8 */
|
2013-09-04 12:50:00 +02:00
|
|
|
if (strchr(user_faked_time, 'x') != NULL)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
user_rate = atof(strchr(user_faked_time, 'x')+1);
|
|
|
|
|
user_rate_set = true;
|
2019-09-05 22:52:07 +02:00
|
|
|
if (NULL != getenv("FAKETIME_XRESET")) {
|
2019-08-22 00:49:21 +02:00
|
|
|
if (ftpl_timecache.real.tv_nsec >= 0) {
|
|
|
|
|
user_faked_time_timespec.tv_sec = ftpl_faketimecache.real.tv_sec;
|
|
|
|
|
user_faked_time_timespec.tv_nsec = ftpl_faketimecache.real.tv_nsec;
|
|
|
|
|
ftpl_starttime.real.tv_sec = ftpl_timecache.real.tv_sec;
|
|
|
|
|
ftpl_starttime.real.tv_nsec = ftpl_timecache.real.tv_nsec;
|
|
|
|
|
ftpl_starttime.mon.tv_sec = ftpl_timecache.mon.tv_sec;
|
|
|
|
|
ftpl_starttime.mon.tv_nsec = ftpl_timecache.mon.tv_nsec;
|
|
|
|
|
ftpl_starttime.mon_raw.tv_sec = ftpl_timecache.mon_raw.tv_sec;
|
|
|
|
|
ftpl_starttime.mon_raw.tv_nsec = ftpl_timecache.mon_raw.tv_nsec;
|
|
|
|
|
#ifdef CLOCK_BOOTTIME
|
|
|
|
|
ftpl_starttime.boot.tv_sec = ftpl_timecache.boot.tv_sec;
|
|
|
|
|
ftpl_starttime.boot.tv_nsec = ftpl_timecache.boot.tv_nsec;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2019-09-05 22:52:07 +02:00
|
|
|
}
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else if (NULL != (tmp_time_fmt = strchr(user_faked_time, 'i')))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
double tick_inc = atof(tmp_time_fmt + 1);
|
2021-09-21 20:52:08 +00:00
|
|
|
/* increment time with every time() call */
|
2013-09-03 13:23:42 +02:00
|
|
|
user_per_tick_inc.tv_sec = floor(tick_inc);
|
|
|
|
|
user_per_tick_inc.tv_nsec = (tick_inc - user_per_tick_inc.tv_sec) * SEC_TO_nSEC ;
|
|
|
|
|
user_per_tick_inc_set = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
} // end of switch
|
2014-08-26 10:51:50 -04:00
|
|
|
|
|
|
|
|
strncpy(user_faked_time_saved, user_faked_time, BUFFERLEN-1);
|
|
|
|
|
user_faked_time_saved[BUFFERLEN-1] = 0;
|
2014-08-26 13:23:25 -04:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
fprintf(stderr, "new FAKETIME: %s\n", user_faked_time_saved);
|
|
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:23:42 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
* Initialization === INIT
|
|
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
static void ftpl_really_init(void)
|
2013-08-19 11:47:49 +02:00
|
|
|
{
|
2013-10-28 23:02:21 +01:00
|
|
|
char *tmp_env;
|
2016-03-15 10:46:05 +01:00
|
|
|
bool dont_fake_final;
|
2013-10-28 23:02:21 +01:00
|
|
|
|
2019-08-20 10:49:01 +02:00
|
|
|
/* moved up here from below the dlsym calls #130 */
|
|
|
|
|
dont_fake = true; // Do not fake times during initialization
|
|
|
|
|
dont_fake_final = false;
|
|
|
|
|
|
2013-10-28 23:02:21 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
const char *progname = getprogname();
|
|
|
|
|
#else
|
|
|
|
|
const char *progname = __progname;
|
|
|
|
|
#endif
|
2013-09-03 13:42:10 +02:00
|
|
|
|
2024-03-12 02:35:47 +01:00
|
|
|
char *ignore_list = getenv("FAKETIME_IGNORE_SYMBOLS");
|
|
|
|
|
bool should_debug_dlsym = getenv("FAKETIME_DEBUG_DLSYM");
|
|
|
|
|
#define dlsym(handle, symbol) ft_dlvsym(handle, symbol, NULL, symbol, ignore_list, should_debug_dlsym)
|
|
|
|
|
#define dlvsym(handle, symbol, version) ft_dlvsym(handle, symbol, version, symbol "@" version, ignore_list, should_debug_dlsym)
|
|
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
/* Look up all real_* functions. NULL will mark missing ones. */
|
2022-02-16 12:01:35 +02:00
|
|
|
real_stat = dlsym(RTLD_NEXT, "stat");
|
|
|
|
|
real_lstat = dlsym(RTLD_NEXT, "lstat");
|
|
|
|
|
real_fstat = dlsym(RTLD_NEXT, "fstat");
|
2022-02-16 11:27:19 +02:00
|
|
|
real_xstat = dlsym(RTLD_NEXT, "__xstat");
|
|
|
|
|
real_fxstat = dlsym(RTLD_NEXT, "__fxstat");
|
|
|
|
|
real_fxstatat = dlsym(RTLD_NEXT, "__fxstatat");
|
|
|
|
|
real_lxstat = dlsym(RTLD_NEXT, "__lxstat");
|
libfaketime.c: get rid of stat64 things on aarch64-darwin
To give more context, stat64 is a child of large-file support (LFS)
back in 1996, during the transition from 32-bit to 64-bit. People
wanted 64-bit inodes in 32-bit systems, hence stat and stat64.
Nowadays where everything is 64-bit, stat64 is mostly just an alias
to stat, as stat is already native 64-bit. On modern implementations
like musl, stat64 is even dropped entirely as a sane default. We
observe the same in darwin's stat.h:
#if !__DARWIN_ONLY_64_BIT_INO_T
struct stat64 __DARWIN_STRUCT_STAT64;
#endif /* !__DARWIN_ONLY_64_BIT_INO_T */
Because struct stat64 doesn't ever exist on aarch64-darwin, and we
don't have to worry about people using stat64 calls, we can safely
remove all stat64 bloat, according to __DARWIN_ONLY_64_BIT_INO_T.
I nuked fake_stat64buf because only STAT64_HANDLER is using it, and
only non-darwin stat64 things use that handler. I didn't do more
because people might still use stat64 things on x86_64 (on glibc)
and other older 32-bit platforms, and we still need to hook those.
A loose follow up to PR #453. Fixes the remaining clang warnings on
aarch64-darwin.
2025-06-08 18:33:33 +08:00
|
|
|
#if !defined(__APPLE__) || !__DARWIN_ONLY_64_BIT_INO_T
|
Handle stat64() call
This fixes missing modification of timestamps in stat() calls for
programs built with large file support (-D_FILE_OFFSET_BITS=64), both
32- and 64-bit.
Demo code:
$ cat <<EOF >test.c
#include <sys/stat.h>
int main()
{
struct stat buf;
return stat("/", &buf);
}
EOF
32-bit build:
$ nix-shell -p gcc --argstr system i686-linux
nix-shell$ gcc test.c && ltrace ./a.out
__libc_start_main([ "./a.out" ] <unfinished ...>
stat(0x804a008, 0xffa4b644, 895, 0) = 0
+++ exited (status 0) +++
nix-shell$ gcc -D_FILE_OFFSET_BITS=64 test.c && ltrace ./a.out
__libc_start_main([ "./a.out" ] <unfinished ...>
stat64(0x804a008, 0xffdcf61c, 100, 0xffdcfaeb) = 0
+++ exited (status 0) +++
nix-shell$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, [...]
64-bit build:
$ nix-shell -p gcc
nix-shell$ gcc test.c && ltrace ./a.out
stat(0x402004, 0x7ffc50a9d740, 0x7ffc50a9d908, 0x403db0) = 0
+++ exited (status 0) +++
nix-shell$ gcc -D_FILE_OFFSET_BITS=64 test.c && ltrace ./a.out
stat64(0x402004, 0x7ffd5cbafba0, 0x7ffd5cbafd68, 0x403db0) = 0
+++ exited (status 0) +++
nix-shell$ file a.out
a.out: ELF 64-bit LSB executable, x86-64, [...]
2025-07-09 17:32:54 +02:00
|
|
|
real_stat64 = dlsym(RTLD_NEXT, "stat64");
|
2022-02-16 11:27:19 +02:00
|
|
|
real_xstat64 = dlsym(RTLD_NEXT,"__xstat64");
|
|
|
|
|
real_fxstat64 = dlsym(RTLD_NEXT, "__fxstat64");
|
|
|
|
|
real_fxstatat64 = dlsym(RTLD_NEXT, "__fxstatat64");
|
|
|
|
|
real_lxstat64 = dlsym(RTLD_NEXT, "__lxstat64");
|
libfaketime.c: get rid of stat64 things on aarch64-darwin
To give more context, stat64 is a child of large-file support (LFS)
back in 1996, during the transition from 32-bit to 64-bit. People
wanted 64-bit inodes in 32-bit systems, hence stat and stat64.
Nowadays where everything is 64-bit, stat64 is mostly just an alias
to stat, as stat is already native 64-bit. On modern implementations
like musl, stat64 is even dropped entirely as a sane default. We
observe the same in darwin's stat.h:
#if !__DARWIN_ONLY_64_BIT_INO_T
struct stat64 __DARWIN_STRUCT_STAT64;
#endif /* !__DARWIN_ONLY_64_BIT_INO_T */
Because struct stat64 doesn't ever exist on aarch64-darwin, and we
don't have to worry about people using stat64 calls, we can safely
remove all stat64 bloat, according to __DARWIN_ONLY_64_BIT_INO_T.
I nuked fake_stat64buf because only STAT64_HANDLER is using it, and
only non-darwin stat64 things use that handler. I didn't do more
because people might still use stat64 things on x86_64 (on glibc)
and other older 32-bit platforms, and we still need to hook those.
A loose follow up to PR #453. Fixes the remaining clang warnings on
aarch64-darwin.
2025-06-08 18:33:33 +08:00
|
|
|
#endif
|
2023-07-30 20:55:48 -04:00
|
|
|
#ifdef STATX_TYPE
|
|
|
|
|
real_statx = dlsym(RTLD_NEXT, "statx");
|
|
|
|
|
#endif
|
2013-09-30 16:36:13 +02:00
|
|
|
real_time = dlsym(RTLD_NEXT, "time");
|
|
|
|
|
real_ftime = dlsym(RTLD_NEXT, "ftime");
|
2022-12-20 00:35:53 +08:00
|
|
|
#ifdef TIME_UTC
|
2021-07-30 13:46:24 -04:00
|
|
|
real_timespec_get = dlsym(RTLD_NEXT, "timespec_get");
|
2022-12-20 00:35:53 +08:00
|
|
|
#endif
|
2019-12-14 21:49:23 +01:00
|
|
|
#ifdef FAKE_FILE_TIMESTAMPS
|
|
|
|
|
real_utimes = dlsym(RTLD_NEXT, "utimes");
|
|
|
|
|
real_utime = dlsym(RTLD_NEXT, "utime");
|
2020-07-28 17:34:38 -07:00
|
|
|
real_utimensat = dlsym(RTLD_NEXT, "utimensat");
|
|
|
|
|
real_futimens = dlsym(RTLD_NEXT, "futimens");
|
2019-12-14 21:49:23 +01:00
|
|
|
#endif
|
2018-02-19 11:28:27 -08:00
|
|
|
#if defined(__alpha__) && defined(__GLIBC__)
|
|
|
|
|
real_gettimeofday = dlvsym(RTLD_NEXT, "gettimeofday", "GLIBC_2.1");
|
|
|
|
|
#else
|
2013-09-30 16:36:13 +02:00
|
|
|
real_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
|
2018-02-19 11:28:27 -08:00
|
|
|
#endif
|
2013-09-04 14:20:43 +02:00
|
|
|
#ifdef FAKE_SLEEP
|
2013-09-30 16:36:13 +02:00
|
|
|
real_nanosleep = dlsym(RTLD_NEXT, "nanosleep");
|
2019-08-21 18:04:48 +02:00
|
|
|
#ifndef __APPLE__
|
|
|
|
|
real_clock_nanosleep = dlsym(RTLD_NEXT, "clock_nanosleep");
|
|
|
|
|
#endif
|
2013-09-30 16:36:13 +02:00
|
|
|
real_usleep = dlsym(RTLD_NEXT, "usleep");
|
|
|
|
|
real_sleep = dlsym(RTLD_NEXT, "sleep");
|
|
|
|
|
real_alarm = dlsym(RTLD_NEXT, "alarm");
|
|
|
|
|
real_poll = dlsym(RTLD_NEXT, "poll");
|
|
|
|
|
real_ppoll = dlsym(RTLD_NEXT, "ppoll");
|
2019-08-22 23:29:48 +02:00
|
|
|
#ifdef linux
|
|
|
|
|
real_epoll_wait = dlsym(RTLD_NEXT, "epoll_wait");
|
|
|
|
|
real_epoll_pwait = dlsym(RTLD_NEXT, "epoll_pwait");
|
|
|
|
|
#endif
|
2017-05-18 17:52:03 -07:00
|
|
|
real_select = dlsym(RTLD_NEXT, "select");
|
2019-08-22 23:29:48 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
|
real_pselect = dlsym(RTLD_NEXT, "pselect");
|
|
|
|
|
#endif
|
2014-08-26 00:18:02 -04:00
|
|
|
real_sem_timedwait = dlsym(RTLD_NEXT, "sem_timedwait");
|
2022-05-11 21:47:35 +02:00
|
|
|
real_sem_clockwait = dlsym(RTLD_NEXT, "sem_clockwait");
|
2013-09-04 14:20:43 +02:00
|
|
|
#endif
|
2013-10-16 09:46:29 +02:00
|
|
|
#ifdef FAKE_INTERNAL_CALLS
|
|
|
|
|
real___ftime = dlsym(RTLD_NEXT, "__ftime");
|
2018-02-19 11:28:27 -08:00
|
|
|
# if defined(__alpha__) && defined(__GLIBC__)
|
|
|
|
|
real___gettimeofday = dlvsym(RTLD_NEXT, "__gettimeofday", "GLIBC_2.1");
|
|
|
|
|
# else
|
2013-10-16 09:46:29 +02:00
|
|
|
real___gettimeofday = dlsym(RTLD_NEXT, "__gettimeofday");
|
2018-02-19 11:28:27 -08:00
|
|
|
# endif
|
2013-10-16 09:46:29 +02:00
|
|
|
real___clock_gettime = dlsym(RTLD_NEXT, "__clock_gettime");
|
|
|
|
|
#endif
|
2020-11-15 21:57:10 +01:00
|
|
|
|
|
|
|
|
#ifdef FAKE_RANDOM
|
|
|
|
|
real_getrandom = dlsym(RTLD_NEXT, "getrandom");
|
2021-02-24 16:14:02 -05:00
|
|
|
real_getentropy = dlsym(RTLD_NEXT, "getentropy");
|
2020-11-15 21:57:10 +01:00
|
|
|
#endif
|
|
|
|
|
|
2021-02-23 21:42:42 -05:00
|
|
|
#ifdef FAKE_PID
|
|
|
|
|
real_getpid = dlsym(RTLD_NEXT, "getpid");
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-02-24 12:36:38 -05:00
|
|
|
#ifdef INTERCEPT_SYSCALL
|
|
|
|
|
real_syscall = dlsym(RTLD_NEXT, "syscall");
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-12-31 02:23:56 +01:00
|
|
|
#ifdef FAKE_PTHREAD
|
2018-01-14 12:31:39 -06:00
|
|
|
|
|
|
|
|
#ifdef __GLIBC__
|
2017-12-31 02:23:56 +01:00
|
|
|
real_pthread_cond_timedwait_225 = dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.2.5");
|
|
|
|
|
|
|
|
|
|
real_pthread_cond_timedwait_232 = dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.3.2");
|
|
|
|
|
real_pthread_cond_init_232 = dlvsym(RTLD_NEXT, "pthread_cond_init", "GLIBC_2.3.2");
|
|
|
|
|
real_pthread_cond_destroy_232 = dlvsym(RTLD_NEXT, "pthread_cond_destroy", "GLIBC_2.3.2");
|
2018-01-14 12:31:39 -06:00
|
|
|
#endif
|
2017-12-31 02:23:56 +01:00
|
|
|
|
|
|
|
|
if (NULL == real_pthread_cond_timedwait_232)
|
|
|
|
|
{
|
|
|
|
|
real_pthread_cond_timedwait_232 = dlsym(RTLD_NEXT, "pthread_cond_timedwait");
|
|
|
|
|
}
|
|
|
|
|
if (NULL == real_pthread_cond_init_232)
|
|
|
|
|
{
|
|
|
|
|
real_pthread_cond_init_232 = dlsym(RTLD_NEXT, "pthread_cond_init");
|
|
|
|
|
}
|
|
|
|
|
if (NULL == real_pthread_cond_destroy_232)
|
|
|
|
|
{
|
|
|
|
|
real_pthread_cond_destroy_232 = dlsym(RTLD_NEXT, "pthread_cond_destroy");
|
|
|
|
|
}
|
2019-05-31 22:27:10 +02:00
|
|
|
|
|
|
|
|
if (pthread_rwlock_init(&monotonic_conds_lock,NULL) != 0) {
|
|
|
|
|
fprintf(stderr,"monotonic_conds_lock init failed\n");
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
2017-12-31 02:23:56 +01:00
|
|
|
#endif
|
2025-06-07 20:28:33 +08:00
|
|
|
#ifdef __APPLE__
|
2013-09-30 16:36:13 +02:00
|
|
|
real_clock_get_time = dlsym(RTLD_NEXT, "clock_get_time");
|
|
|
|
|
real_clock_gettime = apple_clock_gettime;
|
2013-09-03 17:13:16 +02:00
|
|
|
#else
|
2014-07-19 11:07:37 +02:00
|
|
|
real_clock_gettime = dlsym(RTLD_NEXT, "__clock_gettime");
|
|
|
|
|
if (NULL == real_clock_gettime)
|
|
|
|
|
{
|
|
|
|
|
real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
|
|
|
|
|
}
|
2025-01-12 22:23:16 +00:00
|
|
|
real_clock_gettime64 = dlsym(RTLD_NEXT, "clock_gettime64");
|
|
|
|
|
if (NULL == real_clock_gettime64)
|
|
|
|
|
{
|
|
|
|
|
real_clock_gettime64 = dlsym(RTLD_NEXT, "__clock_gettime64");
|
|
|
|
|
}
|
2013-09-04 14:20:43 +02:00
|
|
|
#ifdef FAKE_TIMERS
|
2014-08-05 16:43:34 -07:00
|
|
|
#if defined(__sun)
|
|
|
|
|
real_timer_gettime_233 = dlsym(RTLD_NEXT, "timer_gettime");
|
|
|
|
|
real_timer_settime_233 = dlsym(RTLD_NEXT, "timer_settime");
|
|
|
|
|
#else
|
2018-01-14 12:31:39 -06:00
|
|
|
#ifdef __GLIBC__
|
2013-09-30 16:36:13 +02:00
|
|
|
real_timer_settime_22 = dlvsym(RTLD_NEXT, "timer_settime","GLIBC_2.2");
|
|
|
|
|
real_timer_settime_233 = dlvsym(RTLD_NEXT, "timer_settime","GLIBC_2.3.3");
|
2018-01-14 12:31:39 -06:00
|
|
|
#endif
|
2013-11-07 19:35:18 +01:00
|
|
|
if (NULL == real_timer_settime_233)
|
|
|
|
|
{
|
2013-10-20 16:53:40 +02:00
|
|
|
real_timer_settime_233 = dlsym(RTLD_NEXT, "timer_settime");
|
|
|
|
|
}
|
2018-01-14 12:31:39 -06:00
|
|
|
#ifdef __GLIBC__
|
2013-09-30 16:36:13 +02:00
|
|
|
real_timer_gettime_22 = dlvsym(RTLD_NEXT, "timer_gettime","GLIBC_2.2");
|
|
|
|
|
real_timer_gettime_233 = dlvsym(RTLD_NEXT, "timer_gettime","GLIBC_2.3.3");
|
2018-01-14 12:31:39 -06:00
|
|
|
#endif
|
2013-11-07 19:35:18 +01:00
|
|
|
if (NULL == real_timer_gettime_233)
|
|
|
|
|
{
|
2013-10-20 16:53:40 +02:00
|
|
|
real_timer_gettime_233 = dlsym(RTLD_NEXT, "timer_gettime");
|
|
|
|
|
}
|
2013-09-04 14:20:43 +02:00
|
|
|
#endif
|
2021-03-30 12:34:26 -04:00
|
|
|
#ifdef __linux__
|
|
|
|
|
real_timerfd_gettime = dlsym(RTLD_NEXT, "timerfd_gettime");
|
|
|
|
|
real_timerfd_settime = dlsym(RTLD_NEXT, "timerfd_settime");
|
|
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
#endif
|
|
|
|
|
#endif
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
do_macos_dyld_interpose();
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-03-12 02:35:47 +01:00
|
|
|
#undef dlsym
|
|
|
|
|
#undef dlvsym
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2021-08-01 08:41:17 -04:00
|
|
|
#ifdef FAKE_STATELESS
|
|
|
|
|
if (0) ft_shm_init();
|
|
|
|
|
#else
|
2024-03-12 23:57:35 +01:00
|
|
|
tmp_env = getenv("FAKETIME_DISABLE_SHM");
|
|
|
|
|
if (!tmp_env || tmp_env[0] == '0') {
|
|
|
|
|
ft_shm_init();
|
|
|
|
|
}
|
2021-08-01 08:41:17 -04:00
|
|
|
#endif
|
2013-08-19 11:47:49 +02:00
|
|
|
#ifdef FAKE_STAT
|
2013-09-04 12:50:00 +02:00
|
|
|
if (getenv("NO_FAKE_STAT")!=NULL)
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
fake_stat_disabled = 1; //Note that this is NOT re-checked
|
|
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
#endif
|
2020-07-28 17:34:38 -07:00
|
|
|
#if defined FAKE_FILE_TIMESTAMPS
|
2022-03-18 12:03:24 +00:00
|
|
|
#ifdef FAKE_UTIME
|
|
|
|
|
// fake_utime_disabled is 0 by default
|
2020-07-28 17:34:38 -07:00
|
|
|
if ((tmp_env = getenv("FAKE_UTIME")) != NULL) //Note that this is NOT re-checked
|
|
|
|
|
{
|
|
|
|
|
if (!*tmp_env || *tmp_env == 'y' || *tmp_env == 'Y' || *tmp_env == 't' || *tmp_env == 'T')
|
|
|
|
|
{ /* an empty string or a yes/true value turns off disabling */
|
|
|
|
|
fake_utime_disabled = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{ /* Any other non-number disables the utime functions, but we also support FAKE_UTIME=1 to enable */
|
|
|
|
|
fake_utime_disabled = !atoi(tmp_env);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-18 12:03:24 +00:00
|
|
|
#else
|
|
|
|
|
// compiled without FAKE_UTIME support, so don't allow it to be controlled by the env var
|
|
|
|
|
fake_utime_disabled = 1;
|
|
|
|
|
#endif
|
2020-07-28 17:34:38 -07:00
|
|
|
#endif
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2014-11-17 10:42:15 +01:00
|
|
|
if ((tmp_env = getenv("FAKETIME_CACHE_DURATION")) != NULL)
|
|
|
|
|
{
|
|
|
|
|
cache_duration = atoi(tmp_env);
|
|
|
|
|
}
|
|
|
|
|
if ((tmp_env = getenv("FAKETIME_NO_CACHE")) != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (0 == strcmp(tmp_env, "1"))
|
|
|
|
|
{
|
|
|
|
|
cache_enabled = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-14 10:36:29 +10:00
|
|
|
get_fake_monotonic_setting(&fake_monotonic_clock);
|
2013-09-03 13:42:10 +02:00
|
|
|
/* Check whether we actually should be faking the returned timestamp. */
|
2013-09-01 16:04:21 +02:00
|
|
|
|
2013-10-28 22:58:03 +01:00
|
|
|
/* We can prevent faking time for specified commands */
|
2013-11-07 19:35:18 +01:00
|
|
|
if ((tmp_env = getenv("FAKETIME_SKIP_CMDS")) != NULL)
|
|
|
|
|
{
|
2015-05-21 07:32:16 +00:00
|
|
|
char *skip_cmd, *saveptr, *tmpvar;
|
|
|
|
|
/* Don't mess with the env variable directly. */
|
|
|
|
|
tmpvar = strdup(tmp_env);
|
|
|
|
|
if (tmpvar != NULL)
|
2013-11-07 19:35:18 +01:00
|
|
|
{
|
2015-05-21 07:32:16 +00:00
|
|
|
skip_cmd = strtok_r(tmpvar, ",", &saveptr);
|
|
|
|
|
while (skip_cmd != NULL)
|
2013-11-07 19:35:18 +01:00
|
|
|
{
|
2015-05-21 07:32:16 +00:00
|
|
|
if (0 == strcmp(progname, skip_cmd))
|
|
|
|
|
{
|
|
|
|
|
ft_mode = FT_NOOP;
|
2016-03-15 10:46:05 +01:00
|
|
|
dont_fake_final = true;
|
2015-05-21 07:32:16 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
skip_cmd = strtok_r(NULL, ",", &saveptr);
|
2013-10-28 22:58:03 +01:00
|
|
|
}
|
2015-05-21 07:32:16 +00:00
|
|
|
free(tmpvar);
|
|
|
|
|
tmpvar = NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Error: Could not copy the environment variable value.\n");
|
|
|
|
|
exit(EXIT_FAILURE);
|
2013-10-28 22:58:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We can limit faking time to specified commands */
|
2013-11-07 19:35:18 +01:00
|
|
|
if ((tmp_env = getenv("FAKETIME_ONLY_CMDS")) != NULL)
|
|
|
|
|
{
|
2016-06-24 17:29:48 +02:00
|
|
|
char *only_cmd, *saveptr, *tmpvar;
|
2013-10-28 22:58:03 +01:00
|
|
|
bool cmd_matched = false;
|
|
|
|
|
|
2013-11-07 19:35:18 +01:00
|
|
|
if (getenv("FAKETIME_SKIP_CMDS") != NULL)
|
|
|
|
|
{
|
2013-10-30 21:47:56 +01:00
|
|
|
fprintf(stderr, "Error: Both FAKETIME_SKIP_CMDS and FAKETIME_ONLY_CMDS can't be set.\n");
|
2013-10-28 22:58:03 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 17:29:48 +02:00
|
|
|
/* Don't mess with the env variable directly. */
|
|
|
|
|
tmpvar = strdup(tmp_env);
|
|
|
|
|
if (tmpvar != NULL) {
|
|
|
|
|
only_cmd = strtok_r(tmpvar, ",", &saveptr);
|
|
|
|
|
while (only_cmd != NULL)
|
2013-11-07 19:35:18 +01:00
|
|
|
{
|
2016-06-24 17:29:48 +02:00
|
|
|
if (0 == strcmp(progname, only_cmd))
|
|
|
|
|
{
|
|
|
|
|
cmd_matched = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
only_cmd = strtok_r(NULL, ",", &saveptr);
|
2013-10-28 22:58:03 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-24 17:29:48 +02:00
|
|
|
if (!cmd_matched)
|
|
|
|
|
{
|
|
|
|
|
ft_mode = FT_NOOP;
|
|
|
|
|
dont_fake_final = true;
|
|
|
|
|
}
|
|
|
|
|
free(tmpvar);
|
|
|
|
|
} else {
|
|
|
|
|
fprintf(stderr, "Error: Could not copy the environment variable value.\n");
|
|
|
|
|
exit(EXIT_FAILURE);
|
2013-10-28 22:58:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((tmp_env = getenv("FAKETIME_START_AFTER_SECONDS")) != NULL)
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
ft_start_after_secs = atol(tmp_env);
|
|
|
|
|
limited_faking = true;
|
|
|
|
|
}
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((tmp_env = getenv("FAKETIME_STOP_AFTER_SECONDS")) != NULL)
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
ft_stop_after_secs = atol(tmp_env);
|
|
|
|
|
limited_faking = true;
|
|
|
|
|
}
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((tmp_env = getenv("FAKETIME_START_AFTER_NUMCALLS")) != NULL)
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
ft_start_after_ncalls = atol(tmp_env);
|
|
|
|
|
limited_faking = true;
|
|
|
|
|
}
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((tmp_env = getenv("FAKETIME_STOP_AFTER_NUMCALLS")) != NULL)
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
ft_stop_after_ncalls = atol(tmp_env);
|
|
|
|
|
limited_faking = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* check whether we should spawn an external command */
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((tmp_env = getenv("FAKETIME_SPAWN_TARGET")) != NULL)
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
spawnsupport = true;
|
libfaketime.c: fix strncpy() issues raised by gcc 8.x
gcc 8.x introduced stricter checking on strncpy(), and causes the
following build failures:
libfaketime.c: In function 'fake_clock_gettime.part.4':
libfaketime.c:2134:7: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
strncpy(user_faked_time, tmp_env, BUFFERLEN);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c:2134:7: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
strncpy(user_faked_time, tmp_env, BUFFERLEN);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c: In function 'ftpl_init':
libfaketime.c:1884:12: error: 'strncpy' specified bound 1024 equals destination size [-Werror=stringop-truncation]
(void) strncpy(ft_spawn_target, getenv("FAKETIME_SPAWN_TARGET"), 1024);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c:1945:5: error: 'strncpy' specified bound 8192 equals destination size [-Werror=stringop-truncation]
strncpy(user_faked_time_fmt, tmp_env, BUFSIZ);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c: In function 'ftpl_init':
libfaketime.c:1884:12: error: 'strncpy' specified bound 1024 equals destination size [-Werror=stringop-truncation]
(void) strncpy(ft_spawn_target, getenv("FAKETIME_SPAWN_TARGET"), 1024);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c:1945:5: error: 'strncpy' specified bound 8192 equals destination size [-Werror=stringop-truncation]
strncpy(user_faked_time_fmt, tmp_env, BUFSIZ);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This commit fixes that by making sure we keep one final byte for the
nul terminator, as suggested by
https://github.com/wolfcw/libfaketime/issues/150.
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2018-05-17 22:55:22 +02:00
|
|
|
(void) strncpy(ft_spawn_target, getenv("FAKETIME_SPAWN_TARGET"), sizeof(ft_spawn_target) - 1);
|
|
|
|
|
ft_spawn_target[sizeof(ft_spawn_target) - 1] = 0;
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((tmp_env = getenv("FAKETIME_SPAWN_SECONDS")) != NULL)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-03 13:42:10 +02:00
|
|
|
ft_spawn_secs = atol(tmp_env);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((tmp_env = getenv("FAKETIME_SPAWN_NUMCALLS")) != NULL)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-10-30 21:47:56 +01:00
|
|
|
ft_spawn_ncalls = atol(tmp_env);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2013-09-03 13:42:10 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((tmp_env = getenv("FAKETIME_SAVE_FILE")) != NULL)
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
if (-1 == (outfile = open(tmp_env, O_RDWR | O_APPEND | O_CLOEXEC | O_CREAT,
|
2013-10-30 21:47:56 +01:00
|
|
|
S_IWUSR | S_IRUSR)))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2019-08-16 15:53:47 +02:00
|
|
|
perror("libfaketime: In ftpl_init(), opening file for saving timestamps failed");
|
2013-10-30 21:47:56 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2013-09-03 13:42:10 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 20:43:41 +00:00
|
|
|
/* load file only if reading timestamps from it is not finished yet */
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((tmp_env = getenv("FAKETIME_LOAD_FILE")) != NULL)
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
int infile = -1;
|
|
|
|
|
struct stat sb;
|
2013-09-04 12:50:00 +02:00
|
|
|
if (-1 == (infile = open(tmp_env, O_RDONLY|O_CLOEXEC)))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2019-08-16 15:53:47 +02:00
|
|
|
perror("libfaketime: In ftpl_init(), opening file for loading timestamps failed");
|
2013-09-03 13:42:10 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
fstat(infile, &sb);
|
2013-09-04 12:50:00 +02:00
|
|
|
if (sizeof(stss[0]) > (infile_size = sb.st_size))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-03 13:42:10 +02:00
|
|
|
printf("There are no timestamps in the provided file to load timestamps from");
|
2013-10-30 21:47:56 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((infile_size % sizeof(stss[0])) != 0)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-03 13:42:10 +02:00
|
|
|
printf("File size is not multiple of timestamp size. It is probably damaged.");
|
|
|
|
|
exit(EXIT_FAILURE);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
stss = mmap(NULL, infile_size, PROT_READ, MAP_SHARED, infile, 0);
|
2013-09-04 12:50:00 +02:00
|
|
|
if (stss == MAP_FAILED)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2019-08-16 15:53:47 +02:00
|
|
|
perror("libfaketime: In ftpl_init(), mapping file for loading timestamps failed");
|
2013-10-30 21:47:56 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2013-09-03 13:42:10 +02:00
|
|
|
infile_set = true;
|
|
|
|
|
}
|
2013-09-01 16:04:21 +02:00
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
tmp_env = getenv("FAKETIME_FMT");
|
2013-09-04 12:50:00 +02:00
|
|
|
if (tmp_env == NULL)
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
strcpy(user_faked_time_fmt, "%Y-%m-%d %T");
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
libfaketime.c: fix strncpy() issues raised by gcc 8.x
gcc 8.x introduced stricter checking on strncpy(), and causes the
following build failures:
libfaketime.c: In function 'fake_clock_gettime.part.4':
libfaketime.c:2134:7: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
strncpy(user_faked_time, tmp_env, BUFFERLEN);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c:2134:7: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
strncpy(user_faked_time, tmp_env, BUFFERLEN);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c: In function 'ftpl_init':
libfaketime.c:1884:12: error: 'strncpy' specified bound 1024 equals destination size [-Werror=stringop-truncation]
(void) strncpy(ft_spawn_target, getenv("FAKETIME_SPAWN_TARGET"), 1024);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c:1945:5: error: 'strncpy' specified bound 8192 equals destination size [-Werror=stringop-truncation]
strncpy(user_faked_time_fmt, tmp_env, BUFSIZ);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c: In function 'ftpl_init':
libfaketime.c:1884:12: error: 'strncpy' specified bound 1024 equals destination size [-Werror=stringop-truncation]
(void) strncpy(ft_spawn_target, getenv("FAKETIME_SPAWN_TARGET"), 1024);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c:1945:5: error: 'strncpy' specified bound 8192 equals destination size [-Werror=stringop-truncation]
strncpy(user_faked_time_fmt, tmp_env, BUFSIZ);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This commit fixes that by making sure we keep one final byte for the
nul terminator, as suggested by
https://github.com/wolfcw/libfaketime/issues/150.
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2018-05-17 22:55:22 +02:00
|
|
|
strncpy(user_faked_time_fmt, tmp_env, BUFSIZ - 1);
|
|
|
|
|
user_faked_time_fmt[BUFSIZ - 1] = 0;
|
2013-09-03 13:42:10 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-02 12:49:28 +02:00
|
|
|
if (shared_sem_initialized)
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_lock(&shared_sem) == -1)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In ftpl_init(), ft_sem_lock failed");
|
2013-09-03 13:42:10 +02:00
|
|
|
exit(1);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2026-01-26 13:31:55 +01:00
|
|
|
if (ft_shared->start_time_real.nsec == -1)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-03 13:42:10 +02:00
|
|
|
/* set up global start time */
|
2013-10-30 21:47:56 +01:00
|
|
|
system_time_from_system(&ftpl_starttime);
|
2026-01-26 13:31:55 +01:00
|
|
|
system_time_to_shared(&ftpl_starttime, ft_shared);
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-03 13:42:10 +02:00
|
|
|
/** get preset start time */
|
2026-01-26 13:31:55 +01:00
|
|
|
shared_to_system_time(ft_shared, &ftpl_starttime);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2025-06-02 12:49:28 +02:00
|
|
|
if (ft_sem_unlock(&shared_sem) == -1)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2025-06-02 12:49:28 +02:00
|
|
|
perror("libfaketime: In ftpl_init(), ft_sem_unlock failed");
|
2013-09-03 13:42:10 +02:00
|
|
|
exit(1);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
system_time_from_system(&ftpl_starttime);
|
|
|
|
|
}
|
|
|
|
|
/* fake time supplied as environment variable? */
|
2013-09-04 12:50:00 +02:00
|
|
|
if (NULL != (tmp_env = getenv("FAKETIME")))
|
2013-09-03 13:42:10 +02:00
|
|
|
{
|
|
|
|
|
parse_config_file = false;
|
|
|
|
|
parse_ft_string(tmp_env);
|
|
|
|
|
}
|
2020-03-26 20:07:24 +01:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
read_config_file();
|
|
|
|
|
}
|
2016-03-15 10:46:05 +01:00
|
|
|
|
|
|
|
|
dont_fake = dont_fake_final;
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
2025-12-16 11:50:40 +01:00
|
|
|
static void init_initialized_once_mutex (void)
|
|
|
|
|
{
|
2025-12-16 16:30:25 +01:00
|
|
|
ft_initialize_errorcheck_mutex(&initialized_once_mutex);
|
2025-12-16 11:50:40 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
inline static void ftpl_init(void) {
|
2025-12-16 11:50:40 +01:00
|
|
|
static bool init_done = false;
|
2025-12-16 16:30:25 +01:00
|
|
|
ft_init_once_generic(&init_done, &initialized_once_control, &initialized_once_mutex, &init_initialized_once_mutex, &ftpl_really_init);
|
2023-01-16 21:25:56 -05:00
|
|
|
}
|
2013-09-03 13:42:10 +02:00
|
|
|
|
2024-03-12 02:35:47 +01:00
|
|
|
void *ft_dlvsym(void *handle, const char *symbol, const char *version,
|
|
|
|
|
const char *full_name, char *ignore_list, bool should_debug_dlsym)
|
|
|
|
|
{
|
|
|
|
|
// dlsym or dlvsym with a non-resolving symbol results in a malloc call,
|
|
|
|
|
// which can trigger infinite recursion or deadlock, as seen in
|
|
|
|
|
// https://github.com/wolfcw/libfaketime/issues/130
|
|
|
|
|
// As a work-around, enable users to identify the list of calls,
|
|
|
|
|
// FAKETIME_DEBUG_DLSYM=1 faketime ...
|
|
|
|
|
// and then repeat the faketime call again to skip dlsym/dlvsym calls
|
|
|
|
|
// for these symbols, for example:
|
|
|
|
|
// FAKETIME_IGNORE_SYMBOLS=__ftime,timer_settime@GLIBC_2.2,timer_gettime@GLIBC_2.2 faketime ...
|
|
|
|
|
if (ignore_list && str_array_contains(ignore_list, full_name)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
void *addr = NULL;
|
|
|
|
|
#ifdef __GLIBC__
|
|
|
|
|
if (version) {
|
|
|
|
|
addr = dlvsym(handle, symbol, version);
|
|
|
|
|
} else {
|
|
|
|
|
addr = dlsym(handle, symbol);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
// dlvsym does not exists, version is always NULL at compile time.
|
|
|
|
|
addr = dlsym(handle, symbol);
|
2024-03-24 21:08:36 +01:00
|
|
|
if (version != NULL) fprintf(stderr, "ft_dlvsym(): version is not NULL\n");
|
2024-03-12 02:35:47 +01:00
|
|
|
#endif
|
|
|
|
|
if (!addr && should_debug_dlsym) {
|
|
|
|
|
fprintf(stderr, "[FAKETIME_DEBUG_DLSYM] Cannot find symbol: %s\n", full_name);
|
|
|
|
|
}
|
|
|
|
|
return addr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:42:10 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
* Helper functions === HELPER
|
|
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
Fix another hang under ASAN
We have a long-running program that we want to run under sanitizers for
extra error checking and under faketime to speed up the clock. This
program hangs after a while. Backtraces suggest that the hangs occur
because of recursion in the memory allocator, which apparently locks a
non-recursive mutex.
Specifically, what we see is that due to our use of FAKETIME_NO_CACHE=1,
libfaketime wants to reload the config file inside a (random) call to
clock_gettime(). libfaketime then uses fopen() / fclose() for reading
the config files. These function allocate / free a buffer for reading
data and specifically the call to free() that happens inside fclose()
ends up calling clock_gettime() again. At this point, libfaketime locks
up because it has a time_mutex that is locked and none-recursive.
Sadly, I did not manage to come up with a stand-alone reproducer for
this problem. Also, the above description is from memory after half a
week of vacation, so it might be (partially) wrong.
More information can be found here:
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1115802530
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1116178907
This commit first adds a test case. This new test uses the already
existing libmallocintercept.so to cause calls to clock_gettime() during
memory allocation routines. The difference to the already existing
version is that additionally FAKETIME_NO_CACHE and
FAKETIME_TIMESTAMP_FILE are set. This new test hung with its last output
suggesting a recursive call to malloc:
Called malloc() from libmallocintercept...successfully
Called free() on from libmallocintercept...successfully
Called malloc() from libmallocintercept...Called malloc() from libmallocintercept...
Sadly, gdb was unable to provide a meaningful backtrace for this hang.
Next, I replaced the use of fopen()/fgets()/fgets() with
open()/read()/close(). This code no longer reads the config file
line-by-line, but instead it reads all of it at once and then "filters
out" the result (ignores comment lines, removes end of line markers).
I tried to keep the behaviour of the code the same, but I know at least
one difference: Previously, the config file was read line-by-line and
lines that began with a comment character were immediately ignored. The
new code only reads the config once and then removes comment lines.
Since the buffer that is used contains only 256 characters, it is
possible that config files that were previously parsed correctly now
longer parse. A specific example: if a file begins with 500 '#'
characters in its first line and then a timestamp in the second line,
the old code was able to parse this file while the new code would only
see an empty file.
After this change, the new test no longer hangs. Sadly, I do not
actually know its effect on the "actual bug" that I wanted to fix, but
since there are no longer any calls to fclose(), there cannot be any
hangs inside fclose().
Signed-off-by: Uli Schlachter <psychon@znc.in>
2022-05-09 13:53:51 +02:00
|
|
|
static void prepare_config_contents(char *contents)
|
2013-08-19 11:47:49 +02:00
|
|
|
{
|
Fix another hang under ASAN
We have a long-running program that we want to run under sanitizers for
extra error checking and under faketime to speed up the clock. This
program hangs after a while. Backtraces suggest that the hangs occur
because of recursion in the memory allocator, which apparently locks a
non-recursive mutex.
Specifically, what we see is that due to our use of FAKETIME_NO_CACHE=1,
libfaketime wants to reload the config file inside a (random) call to
clock_gettime(). libfaketime then uses fopen() / fclose() for reading
the config files. These function allocate / free a buffer for reading
data and specifically the call to free() that happens inside fclose()
ends up calling clock_gettime() again. At this point, libfaketime locks
up because it has a time_mutex that is locked and none-recursive.
Sadly, I did not manage to come up with a stand-alone reproducer for
this problem. Also, the above description is from memory after half a
week of vacation, so it might be (partially) wrong.
More information can be found here:
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1115802530
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1116178907
This commit first adds a test case. This new test uses the already
existing libmallocintercept.so to cause calls to clock_gettime() during
memory allocation routines. The difference to the already existing
version is that additionally FAKETIME_NO_CACHE and
FAKETIME_TIMESTAMP_FILE are set. This new test hung with its last output
suggesting a recursive call to malloc:
Called malloc() from libmallocintercept...successfully
Called free() on from libmallocintercept...successfully
Called malloc() from libmallocintercept...Called malloc() from libmallocintercept...
Sadly, gdb was unable to provide a meaningful backtrace for this hang.
Next, I replaced the use of fopen()/fgets()/fgets() with
open()/read()/close(). This code no longer reads the config file
line-by-line, but instead it reads all of it at once and then "filters
out" the result (ignores comment lines, removes end of line markers).
I tried to keep the behaviour of the code the same, but I know at least
one difference: Previously, the config file was read line-by-line and
lines that began with a comment character were immediately ignored. The
new code only reads the config once and then removes comment lines.
Since the buffer that is used contains only 256 characters, it is
possible that config files that were previously parsed correctly now
longer parse. A specific example: if a file begins with 500 '#'
characters in its first line and then a timestamp in the second line,
the old code was able to parse this file while the new code would only
see an empty file.
After this change, the new test no longer hangs. Sadly, I do not
actually know its effect on the "actual bug" that I wanted to fix, but
since there are no longer any calls to fclose(), there cannot be any
hangs inside fclose().
Signed-off-by: Uli Schlachter <psychon@znc.in>
2022-05-09 13:53:51 +02:00
|
|
|
/* This function
|
|
|
|
|
* - removes line separators (\r and \n)
|
|
|
|
|
* - removes lines beginning with a comment character (# or ;)
|
2013-09-03 13:42:10 +02:00
|
|
|
*/
|
Fix another hang under ASAN
We have a long-running program that we want to run under sanitizers for
extra error checking and under faketime to speed up the clock. This
program hangs after a while. Backtraces suggest that the hangs occur
because of recursion in the memory allocator, which apparently locks a
non-recursive mutex.
Specifically, what we see is that due to our use of FAKETIME_NO_CACHE=1,
libfaketime wants to reload the config file inside a (random) call to
clock_gettime(). libfaketime then uses fopen() / fclose() for reading
the config files. These function allocate / free a buffer for reading
data and specifically the call to free() that happens inside fclose()
ends up calling clock_gettime() again. At this point, libfaketime locks
up because it has a time_mutex that is locked and none-recursive.
Sadly, I did not manage to come up with a stand-alone reproducer for
this problem. Also, the above description is from memory after half a
week of vacation, so it might be (partially) wrong.
More information can be found here:
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1115802530
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1116178907
This commit first adds a test case. This new test uses the already
existing libmallocintercept.so to cause calls to clock_gettime() during
memory allocation routines. The difference to the already existing
version is that additionally FAKETIME_NO_CACHE and
FAKETIME_TIMESTAMP_FILE are set. This new test hung with its last output
suggesting a recursive call to malloc:
Called malloc() from libmallocintercept...successfully
Called free() on from libmallocintercept...successfully
Called malloc() from libmallocintercept...Called malloc() from libmallocintercept...
Sadly, gdb was unable to provide a meaningful backtrace for this hang.
Next, I replaced the use of fopen()/fgets()/fgets() with
open()/read()/close(). This code no longer reads the config file
line-by-line, but instead it reads all of it at once and then "filters
out" the result (ignores comment lines, removes end of line markers).
I tried to keep the behaviour of the code the same, but I know at least
one difference: Previously, the config file was read line-by-line and
lines that began with a comment character were immediately ignored. The
new code only reads the config once and then removes comment lines.
Since the buffer that is used contains only 256 characters, it is
possible that config files that were previously parsed correctly now
longer parse. A specific example: if a file begins with 500 '#'
characters in its first line and then a timestamp in the second line,
the old code was able to parse this file while the new code would only
see an empty file.
After this change, the new test no longer hangs. Sadly, I do not
actually know its effect on the "actual bug" that I wanted to fix, but
since there are no longer any calls to fclose(), there cannot be any
hangs inside fclose().
Signed-off-by: Uli Schlachter <psychon@znc.in>
2022-05-09 13:53:51 +02:00
|
|
|
char *read_position = contents;
|
|
|
|
|
char *write_position = contents;
|
|
|
|
|
bool in_comment = false;
|
|
|
|
|
bool beginning_of_line = true;
|
|
|
|
|
|
|
|
|
|
while (*read_position != '\0') {
|
|
|
|
|
if (beginning_of_line && (*read_position == '#' || *read_position == ';')) {
|
|
|
|
|
/* The line begins with a comment character and should be completely ignored */
|
|
|
|
|
in_comment = true;
|
|
|
|
|
}
|
|
|
|
|
beginning_of_line = false;
|
|
|
|
|
|
|
|
|
|
if (*read_position == '\n') {
|
|
|
|
|
/* We reached the end of the line that should be ignored (if any is ignored) */
|
|
|
|
|
in_comment = false;
|
|
|
|
|
/* The next character begins a new line */
|
|
|
|
|
beginning_of_line = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If we are not in a comment and are not looking at a line break, copy the
|
|
|
|
|
* character from the read position to the write position. */
|
|
|
|
|
if (!in_comment && *read_position != '\r' && *write_position != '\n') {
|
|
|
|
|
*write_position = *read_position;
|
|
|
|
|
write_position++;
|
|
|
|
|
}
|
|
|
|
|
read_position++;
|
2013-10-30 21:47:56 +01:00
|
|
|
}
|
Fix another hang under ASAN
We have a long-running program that we want to run under sanitizers for
extra error checking and under faketime to speed up the clock. This
program hangs after a while. Backtraces suggest that the hangs occur
because of recursion in the memory allocator, which apparently locks a
non-recursive mutex.
Specifically, what we see is that due to our use of FAKETIME_NO_CACHE=1,
libfaketime wants to reload the config file inside a (random) call to
clock_gettime(). libfaketime then uses fopen() / fclose() for reading
the config files. These function allocate / free a buffer for reading
data and specifically the call to free() that happens inside fclose()
ends up calling clock_gettime() again. At this point, libfaketime locks
up because it has a time_mutex that is locked and none-recursive.
Sadly, I did not manage to come up with a stand-alone reproducer for
this problem. Also, the above description is from memory after half a
week of vacation, so it might be (partially) wrong.
More information can be found here:
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1115802530
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1116178907
This commit first adds a test case. This new test uses the already
existing libmallocintercept.so to cause calls to clock_gettime() during
memory allocation routines. The difference to the already existing
version is that additionally FAKETIME_NO_CACHE and
FAKETIME_TIMESTAMP_FILE are set. This new test hung with its last output
suggesting a recursive call to malloc:
Called malloc() from libmallocintercept...successfully
Called free() on from libmallocintercept...successfully
Called malloc() from libmallocintercept...Called malloc() from libmallocintercept...
Sadly, gdb was unable to provide a meaningful backtrace for this hang.
Next, I replaced the use of fopen()/fgets()/fgets() with
open()/read()/close(). This code no longer reads the config file
line-by-line, but instead it reads all of it at once and then "filters
out" the result (ignores comment lines, removes end of line markers).
I tried to keep the behaviour of the code the same, but I know at least
one difference: Previously, the config file was read line-by-line and
lines that began with a comment character were immediately ignored. The
new code only reads the config once and then removes comment lines.
Since the buffer that is used contains only 256 characters, it is
possible that config files that were previously parsed correctly now
longer parse. A specific example: if a file begins with 500 '#'
characters in its first line and then a timestamp in the second line,
the old code was able to parse this file while the new code would only
see an empty file.
After this change, the new test no longer hangs. Sadly, I do not
actually know its effect on the "actual bug" that I wanted to fix, but
since there are no longer any calls to fclose(), there cannot be any
hangs inside fclose().
Signed-off-by: Uli Schlachter <psychon@znc.in>
2022-05-09 13:53:51 +02:00
|
|
|
*write_position = '\0';
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
2024-03-12 02:35:47 +01:00
|
|
|
bool str_array_contains(const char *haystack, const char *needle)
|
|
|
|
|
{
|
|
|
|
|
size_t needle_len = strlen(needle);
|
2025-12-15 11:03:21 +01:00
|
|
|
const char *pos = strstr(haystack, needle);
|
2024-03-12 02:35:47 +01:00
|
|
|
while (pos) {
|
|
|
|
|
if (pos == haystack || *(pos - 1) == ',') {
|
|
|
|
|
char nextc = *(pos + needle_len);
|
|
|
|
|
if (nextc == '\0' || nextc == ',') {
|
|
|
|
|
// Found needle in comma-separated haystack.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pos = strstr(pos + 1, needle);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:23:42 +02:00
|
|
|
* =======================================================================
|
2013-09-03 13:42:10 +02:00
|
|
|
* Implementation of faked functions === FAKE(FAKE)
|
2013-09-03 13:23:42 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2018-05-17 22:56:38 +02:00
|
|
|
#ifdef PTHREAD_SINGLETHREADED_TIME
|
2020-03-03 16:39:42 +01:00
|
|
|
/*
|
|
|
|
|
* To avoid a deadlock if a faketime function is interrupted by a signal while
|
|
|
|
|
* holding the lock, we block all signals while the mutex is locked.
|
|
|
|
|
* The original_mask field is used to restore the previous set of signals
|
|
|
|
|
* after the lock has been released.
|
|
|
|
|
* (Prompted by issues with parallel garbage collection in D 2.090. D uses signals
|
|
|
|
|
* to freeze all but one thread. The frozen threads may be in faketime operations.)
|
|
|
|
|
*/
|
|
|
|
|
struct LockedState {
|
2020-03-16 07:57:07 +01:00
|
|
|
pthread_mutex_t *mutex;
|
2020-03-03 16:39:42 +01:00
|
|
|
sigset_t original_mask;
|
|
|
|
|
};
|
|
|
|
|
|
2018-05-17 22:56:38 +02:00
|
|
|
static void pthread_cleanup_mutex_lock(void *data)
|
|
|
|
|
{
|
2020-03-03 16:39:42 +01:00
|
|
|
struct LockedState *state = data;
|
2020-03-16 07:57:07 +01:00
|
|
|
pthread_mutex_unlock(state->mutex);
|
|
|
|
|
pthread_sigmask(SIG_SETMASK, &state->original_mask, NULL);
|
2018-05-17 22:56:38 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-03-26 20:07:24 +01:00
|
|
|
int read_config_file()
|
|
|
|
|
{
|
|
|
|
|
static char user_faked_time[BUFFERLEN]; /* changed to static for caching in v0.6 */
|
|
|
|
|
static char custom_filename[BUFSIZ];
|
|
|
|
|
static char filename[BUFSIZ];
|
Fix another hang under ASAN
We have a long-running program that we want to run under sanitizers for
extra error checking and under faketime to speed up the clock. This
program hangs after a while. Backtraces suggest that the hangs occur
because of recursion in the memory allocator, which apparently locks a
non-recursive mutex.
Specifically, what we see is that due to our use of FAKETIME_NO_CACHE=1,
libfaketime wants to reload the config file inside a (random) call to
clock_gettime(). libfaketime then uses fopen() / fclose() for reading
the config files. These function allocate / free a buffer for reading
data and specifically the call to free() that happens inside fclose()
ends up calling clock_gettime() again. At this point, libfaketime locks
up because it has a time_mutex that is locked and none-recursive.
Sadly, I did not manage to come up with a stand-alone reproducer for
this problem. Also, the above description is from memory after half a
week of vacation, so it might be (partially) wrong.
More information can be found here:
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1115802530
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1116178907
This commit first adds a test case. This new test uses the already
existing libmallocintercept.so to cause calls to clock_gettime() during
memory allocation routines. The difference to the already existing
version is that additionally FAKETIME_NO_CACHE and
FAKETIME_TIMESTAMP_FILE are set. This new test hung with its last output
suggesting a recursive call to malloc:
Called malloc() from libmallocintercept...successfully
Called free() on from libmallocintercept...successfully
Called malloc() from libmallocintercept...Called malloc() from libmallocintercept...
Sadly, gdb was unable to provide a meaningful backtrace for this hang.
Next, I replaced the use of fopen()/fgets()/fgets() with
open()/read()/close(). This code no longer reads the config file
line-by-line, but instead it reads all of it at once and then "filters
out" the result (ignores comment lines, removes end of line markers).
I tried to keep the behaviour of the code the same, but I know at least
one difference: Previously, the config file was read line-by-line and
lines that began with a comment character were immediately ignored. The
new code only reads the config once and then removes comment lines.
Since the buffer that is used contains only 256 characters, it is
possible that config files that were previously parsed correctly now
longer parse. A specific example: if a file begins with 500 '#'
characters in its first line and then a timestamp in the second line,
the old code was able to parse this file while the new code would only
see an empty file.
After this change, the new test no longer hangs. Sadly, I do not
actually know its effect on the "actual bug" that I wanted to fix, but
since there are no longer any calls to fclose(), there cannot be any
hangs inside fclose().
Signed-off-by: Uli Schlachter <psychon@znc.in>
2022-05-09 13:53:51 +02:00
|
|
|
int faketimerc;
|
2020-03-26 20:07:24 +01:00
|
|
|
/* check whether there's a .faketimerc in the user's home directory, or
|
|
|
|
|
* a system-wide /etc/faketimerc present.
|
|
|
|
|
* The /etc/faketimerc handling has been contributed by David Burley,
|
|
|
|
|
* Jacob Moorman, and Wayne Davison of SourceForge, Inc. in version 0.6 */
|
|
|
|
|
(void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE"));
|
|
|
|
|
(void) snprintf(filename, BUFSIZ, "%s/.faketimerc", getenv("HOME"));
|
Fix another hang under ASAN
We have a long-running program that we want to run under sanitizers for
extra error checking and under faketime to speed up the clock. This
program hangs after a while. Backtraces suggest that the hangs occur
because of recursion in the memory allocator, which apparently locks a
non-recursive mutex.
Specifically, what we see is that due to our use of FAKETIME_NO_CACHE=1,
libfaketime wants to reload the config file inside a (random) call to
clock_gettime(). libfaketime then uses fopen() / fclose() for reading
the config files. These function allocate / free a buffer for reading
data and specifically the call to free() that happens inside fclose()
ends up calling clock_gettime() again. At this point, libfaketime locks
up because it has a time_mutex that is locked and none-recursive.
Sadly, I did not manage to come up with a stand-alone reproducer for
this problem. Also, the above description is from memory after half a
week of vacation, so it might be (partially) wrong.
More information can be found here:
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1115802530
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1116178907
This commit first adds a test case. This new test uses the already
existing libmallocintercept.so to cause calls to clock_gettime() during
memory allocation routines. The difference to the already existing
version is that additionally FAKETIME_NO_CACHE and
FAKETIME_TIMESTAMP_FILE are set. This new test hung with its last output
suggesting a recursive call to malloc:
Called malloc() from libmallocintercept...successfully
Called free() on from libmallocintercept...successfully
Called malloc() from libmallocintercept...Called malloc() from libmallocintercept...
Sadly, gdb was unable to provide a meaningful backtrace for this hang.
Next, I replaced the use of fopen()/fgets()/fgets() with
open()/read()/close(). This code no longer reads the config file
line-by-line, but instead it reads all of it at once and then "filters
out" the result (ignores comment lines, removes end of line markers).
I tried to keep the behaviour of the code the same, but I know at least
one difference: Previously, the config file was read line-by-line and
lines that began with a comment character were immediately ignored. The
new code only reads the config once and then removes comment lines.
Since the buffer that is used contains only 256 characters, it is
possible that config files that were previously parsed correctly now
longer parse. A specific example: if a file begins with 500 '#'
characters in its first line and then a timestamp in the second line,
the old code was able to parse this file while the new code would only
see an empty file.
After this change, the new test no longer hangs. Sadly, I do not
actually know its effect on the "actual bug" that I wanted to fix, but
since there are no longer any calls to fclose(), there cannot be any
hangs inside fclose().
Signed-off-by: Uli Schlachter <psychon@znc.in>
2022-05-09 13:53:51 +02:00
|
|
|
if ((faketimerc = open(custom_filename, O_RDONLY)) != -1 ||
|
|
|
|
|
(faketimerc = open(filename, O_RDONLY)) != -1 ||
|
|
|
|
|
(faketimerc = open("/etc/faketimerc", O_RDONLY)) != -1)
|
2020-03-26 20:07:24 +01:00
|
|
|
{
|
2022-08-22 11:21:14 +02:00
|
|
|
ssize_t bytes;
|
|
|
|
|
ssize_t length = 0;
|
|
|
|
|
while ((bytes = read(faketimerc, user_faked_time + length, sizeof(user_faked_time) - 1 - length)) > 0) {
|
|
|
|
|
length += bytes;
|
|
|
|
|
}
|
Fix another hang under ASAN
We have a long-running program that we want to run under sanitizers for
extra error checking and under faketime to speed up the clock. This
program hangs after a while. Backtraces suggest that the hangs occur
because of recursion in the memory allocator, which apparently locks a
non-recursive mutex.
Specifically, what we see is that due to our use of FAKETIME_NO_CACHE=1,
libfaketime wants to reload the config file inside a (random) call to
clock_gettime(). libfaketime then uses fopen() / fclose() for reading
the config files. These function allocate / free a buffer for reading
data and specifically the call to free() that happens inside fclose()
ends up calling clock_gettime() again. At this point, libfaketime locks
up because it has a time_mutex that is locked and none-recursive.
Sadly, I did not manage to come up with a stand-alone reproducer for
this problem. Also, the above description is from memory after half a
week of vacation, so it might be (partially) wrong.
More information can be found here:
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1115802530
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1116178907
This commit first adds a test case. This new test uses the already
existing libmallocintercept.so to cause calls to clock_gettime() during
memory allocation routines. The difference to the already existing
version is that additionally FAKETIME_NO_CACHE and
FAKETIME_TIMESTAMP_FILE are set. This new test hung with its last output
suggesting a recursive call to malloc:
Called malloc() from libmallocintercept...successfully
Called free() on from libmallocintercept...successfully
Called malloc() from libmallocintercept...Called malloc() from libmallocintercept...
Sadly, gdb was unable to provide a meaningful backtrace for this hang.
Next, I replaced the use of fopen()/fgets()/fgets() with
open()/read()/close(). This code no longer reads the config file
line-by-line, but instead it reads all of it at once and then "filters
out" the result (ignores comment lines, removes end of line markers).
I tried to keep the behaviour of the code the same, but I know at least
one difference: Previously, the config file was read line-by-line and
lines that began with a comment character were immediately ignored. The
new code only reads the config once and then removes comment lines.
Since the buffer that is used contains only 256 characters, it is
possible that config files that were previously parsed correctly now
longer parse. A specific example: if a file begins with 500 '#'
characters in its first line and then a timestamp in the second line,
the old code was able to parse this file while the new code would only
see an empty file.
After this change, the new test no longer hangs. Sadly, I do not
actually know its effect on the "actual bug" that I wanted to fix, but
since there are no longer any calls to fclose(), there cannot be any
hangs inside fclose().
Signed-off-by: Uli Schlachter <psychon@znc.in>
2022-05-09 13:53:51 +02:00
|
|
|
close(faketimerc);
|
2022-08-22 11:21:14 +02:00
|
|
|
if (bytes < 0) {
|
Fix another hang under ASAN
We have a long-running program that we want to run under sanitizers for
extra error checking and under faketime to speed up the clock. This
program hangs after a while. Backtraces suggest that the hangs occur
because of recursion in the memory allocator, which apparently locks a
non-recursive mutex.
Specifically, what we see is that due to our use of FAKETIME_NO_CACHE=1,
libfaketime wants to reload the config file inside a (random) call to
clock_gettime(). libfaketime then uses fopen() / fclose() for reading
the config files. These function allocate / free a buffer for reading
data and specifically the call to free() that happens inside fclose()
ends up calling clock_gettime() again. At this point, libfaketime locks
up because it has a time_mutex that is locked and none-recursive.
Sadly, I did not manage to come up with a stand-alone reproducer for
this problem. Also, the above description is from memory after half a
week of vacation, so it might be (partially) wrong.
More information can be found here:
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1115802530
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1116178907
This commit first adds a test case. This new test uses the already
existing libmallocintercept.so to cause calls to clock_gettime() during
memory allocation routines. The difference to the already existing
version is that additionally FAKETIME_NO_CACHE and
FAKETIME_TIMESTAMP_FILE are set. This new test hung with its last output
suggesting a recursive call to malloc:
Called malloc() from libmallocintercept...successfully
Called free() on from libmallocintercept...successfully
Called malloc() from libmallocintercept...Called malloc() from libmallocintercept...
Sadly, gdb was unable to provide a meaningful backtrace for this hang.
Next, I replaced the use of fopen()/fgets()/fgets() with
open()/read()/close(). This code no longer reads the config file
line-by-line, but instead it reads all of it at once and then "filters
out" the result (ignores comment lines, removes end of line markers).
I tried to keep the behaviour of the code the same, but I know at least
one difference: Previously, the config file was read line-by-line and
lines that began with a comment character were immediately ignored. The
new code only reads the config once and then removes comment lines.
Since the buffer that is used contains only 256 characters, it is
possible that config files that were previously parsed correctly now
longer parse. A specific example: if a file begins with 500 '#'
characters in its first line and then a timestamp in the second line,
the old code was able to parse this file while the new code would only
see an empty file.
After this change, the new test no longer hangs. Sadly, I do not
actually know its effect on the "actual bug" that I wanted to fix, but
since there are no longer any calls to fclose(), there cannot be any
hangs inside fclose().
Signed-off-by: Uli Schlachter <psychon@znc.in>
2022-05-09 13:53:51 +02:00
|
|
|
length = 0;
|
2020-03-26 20:07:24 +01:00
|
|
|
}
|
Fix another hang under ASAN
We have a long-running program that we want to run under sanitizers for
extra error checking and under faketime to speed up the clock. This
program hangs after a while. Backtraces suggest that the hangs occur
because of recursion in the memory allocator, which apparently locks a
non-recursive mutex.
Specifically, what we see is that due to our use of FAKETIME_NO_CACHE=1,
libfaketime wants to reload the config file inside a (random) call to
clock_gettime(). libfaketime then uses fopen() / fclose() for reading
the config files. These function allocate / free a buffer for reading
data and specifically the call to free() that happens inside fclose()
ends up calling clock_gettime() again. At this point, libfaketime locks
up because it has a time_mutex that is locked and none-recursive.
Sadly, I did not manage to come up with a stand-alone reproducer for
this problem. Also, the above description is from memory after half a
week of vacation, so it might be (partially) wrong.
More information can be found here:
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1115802530
- https://github.com/wolfcw/libfaketime/issues/365#issuecomment-1116178907
This commit first adds a test case. This new test uses the already
existing libmallocintercept.so to cause calls to clock_gettime() during
memory allocation routines. The difference to the already existing
version is that additionally FAKETIME_NO_CACHE and
FAKETIME_TIMESTAMP_FILE are set. This new test hung with its last output
suggesting a recursive call to malloc:
Called malloc() from libmallocintercept...successfully
Called free() on from libmallocintercept...successfully
Called malloc() from libmallocintercept...Called malloc() from libmallocintercept...
Sadly, gdb was unable to provide a meaningful backtrace for this hang.
Next, I replaced the use of fopen()/fgets()/fgets() with
open()/read()/close(). This code no longer reads the config file
line-by-line, but instead it reads all of it at once and then "filters
out" the result (ignores comment lines, removes end of line markers).
I tried to keep the behaviour of the code the same, but I know at least
one difference: Previously, the config file was read line-by-line and
lines that began with a comment character were immediately ignored. The
new code only reads the config once and then removes comment lines.
Since the buffer that is used contains only 256 characters, it is
possible that config files that were previously parsed correctly now
longer parse. A specific example: if a file begins with 500 '#'
characters in its first line and then a timestamp in the second line,
the old code was able to parse this file while the new code would only
see an empty file.
After this change, the new test no longer hangs. Sadly, I do not
actually know its effect on the "actual bug" that I wanted to fix, but
since there are no longer any calls to fclose(), there cannot be any
hangs inside fclose().
Signed-off-by: Uli Schlachter <psychon@znc.in>
2022-05-09 13:53:51 +02:00
|
|
|
user_faked_time[length] = 0;
|
|
|
|
|
|
|
|
|
|
prepare_config_contents(user_faked_time);
|
2020-03-26 20:07:24 +01:00
|
|
|
parse_ft_string(user_faked_time);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
int fake_clock_gettime(clockid_t clk_id, struct timespec *tp)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
/* variables used for caching, introduced in version 0.6 */
|
|
|
|
|
static time_t last_data_fetch = 0; /* not fetched previously at first call */
|
|
|
|
|
static int cache_expired = 1; /* considered expired at first call */
|
|
|
|
|
|
2020-07-28 17:34:38 -07:00
|
|
|
/* Karl Chan's v0.8 sanity check moved here for 0.9.9 */
|
|
|
|
|
if (tp == NULL) return -1;
|
|
|
|
|
|
2019-08-22 00:49:21 +02:00
|
|
|
/* create a copy of the timespec containing the real system time for clk_id */
|
|
|
|
|
struct timespec tp_save;
|
|
|
|
|
tp_save.tv_sec = tp->tv_sec;
|
|
|
|
|
tp_save.tv_nsec = tp->tv_nsec;
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
if (dont_fake) return 0;
|
|
|
|
|
/* Per process timers are only sped up or slowed down */
|
2013-09-04 12:50:00 +02:00
|
|
|
if ((clk_id == CLOCK_PROCESS_CPUTIME_ID ) || (clk_id == CLOCK_THREAD_CPUTIME_ID))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-04 12:50:00 +02:00
|
|
|
if (user_rate_set)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
timespecmul(tp, user_rate, tp);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2013-09-03 13:23:42 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2013-08-29 10:15:15 +02:00
|
|
|
|
2020-03-11 14:34:25 +01:00
|
|
|
// {ret = value; goto abort;} to call matching pthread_cleanup_pop and return value
|
2021-02-02 19:43:00 -05:00
|
|
|
volatile int ret = INT_MAX;
|
2020-03-11 14:34:25 +01:00
|
|
|
|
2013-08-19 11:47:49 +02:00
|
|
|
#ifdef PTHREAD_SINGLETHREADED_TIME
|
2020-03-16 07:57:07 +01:00
|
|
|
static pthread_mutex_t time_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2020-03-03 16:39:42 +01:00
|
|
|
|
|
|
|
|
// block all signals while locked. prevents deadlocks if signal interrupts in in mid-operation.
|
2020-03-12 06:37:05 +01:00
|
|
|
sigset_t all_signals, original_mask;
|
2020-03-03 16:39:42 +01:00
|
|
|
sigfillset(&all_signals);
|
2020-03-12 06:37:05 +01:00
|
|
|
pthread_sigmask(SIG_SETMASK, &all_signals, &original_mask);
|
2020-03-16 07:57:07 +01:00
|
|
|
pthread_mutex_lock(&time_mutex);
|
|
|
|
|
|
|
|
|
|
struct LockedState state = { .mutex = &time_mutex, .original_mask = original_mask };
|
2020-03-03 16:39:42 +01:00
|
|
|
pthread_cleanup_push(pthread_cleanup_mutex_lock, &state);
|
2013-08-19 11:47:49 +02:00
|
|
|
#endif
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
if ((limited_faking &&
|
|
|
|
|
((ft_start_after_ncalls != -1) || (ft_stop_after_ncalls != -1))) ||
|
2013-09-04 12:50:00 +02:00
|
|
|
(spawnsupport && ft_spawn_ncalls))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-10-04 11:45:55 +02:00
|
|
|
if (callcounter < LONG_MAX) callcounter++;
|
2013-09-03 13:23:42 +02:00
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if (limited_faking || spawnsupport)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
struct timespec tmp_ts;
|
|
|
|
|
/* For debugging, output #seconds and #calls */
|
2013-09-04 12:50:00 +02:00
|
|
|
switch (clk_id)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
case CLOCK_REALTIME:
|
2016-01-29 11:20:39 +00:00
|
|
|
#ifdef CLOCK_REALTIME_COARSE
|
2015-12-28 16:54:55 +03:00
|
|
|
case CLOCK_REALTIME_COARSE:
|
2016-01-29 11:20:39 +00:00
|
|
|
#endif
|
2013-09-03 13:23:42 +02:00
|
|
|
timespecsub(tp, &ftpl_starttime.real, &tmp_ts);
|
2013-11-07 19:35:18 +01:00
|
|
|
break;
|
2013-09-01 16:04:21 +02:00
|
|
|
case CLOCK_MONOTONIC:
|
2016-01-29 11:20:39 +00:00
|
|
|
#ifdef CLOCK_MONOTONIC_COARSE
|
2015-12-28 16:54:55 +03:00
|
|
|
case CLOCK_MONOTONIC_COARSE:
|
2016-01-29 11:20:39 +00:00
|
|
|
#endif
|
2013-09-03 13:23:42 +02:00
|
|
|
timespecsub(tp, &ftpl_starttime.mon, &tmp_ts);
|
|
|
|
|
break;
|
2013-09-01 16:04:21 +02:00
|
|
|
case CLOCK_MONOTONIC_RAW:
|
2013-11-07 19:35:18 +01:00
|
|
|
timespecsub(tp, &ftpl_starttime.mon_raw, &tmp_ts);
|
2013-09-03 13:23:42 +02:00
|
|
|
break;
|
2017-05-19 19:14:58 +02:00
|
|
|
#ifdef CLOCK_BOOTTIME
|
|
|
|
|
case CLOCK_BOOTTIME:
|
|
|
|
|
timespecsub(tp, &ftpl_starttime.boot, &tmp_ts);
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
2013-09-01 16:04:21 +02:00
|
|
|
default:
|
2017-11-30 20:28:06 +01:00
|
|
|
timespecsub(tp, &ftpl_starttime.real, &tmp_ts);
|
|
|
|
|
break;
|
2013-09-03 13:23:42 +02:00
|
|
|
}
|
2013-09-01 16:04:21 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if (limited_faking)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
/* Check whether we actually should be faking the returned timestamp. */
|
2013-11-07 19:35:18 +01:00
|
|
|
/* fprintf(stderr, "(libfaketime limits -> runtime: %lu, callcounter: %lu\n", (*time_tptr - ftpl_starttime), callcounter); */
|
2020-03-11 14:34:25 +01:00
|
|
|
if (((ft_start_after_secs != -1) && (tmp_ts.tv_sec < ft_start_after_secs))
|
|
|
|
|
|| ((ft_stop_after_secs != -1) && (tmp_ts.tv_sec >= ft_stop_after_secs))
|
|
|
|
|
|| ((ft_start_after_ncalls != -1) && (callcounter < ft_start_after_ncalls))
|
|
|
|
|
|| ((ft_stop_after_ncalls != -1) && (callcounter >= ft_stop_after_ncalls)))
|
|
|
|
|
{
|
|
|
|
|
ret = 0;
|
|
|
|
|
goto abort;
|
|
|
|
|
}
|
2013-11-07 19:35:18 +01:00
|
|
|
/* fprintf(stderr, "(libfaketime limits -> runtime: %lu, callcounter: %lu continues\n", (*time_tptr - ftpl_starttime), callcounter); */
|
2013-09-03 13:23:42 +02:00
|
|
|
}
|
2013-09-01 16:04:21 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if (spawnsupport)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
/* check whether we should spawn an external command */
|
2013-09-04 12:50:00 +02:00
|
|
|
if (spawned == 0)
|
2013-09-03 13:23:42 +02:00
|
|
|
{ /* exec external command once only */
|
2023-04-30 20:26:07 +02:00
|
|
|
if ((((ft_spawn_secs > -1) && (tmp_ts.tv_sec >= ft_spawn_secs)) || (callcounter == ft_spawn_ncalls)) && (spawned == 0))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
spawned = 1;
|
2013-10-04 11:48:54 +02:00
|
|
|
(void) (system(ft_spawn_target) + 1);
|
2013-09-03 13:23:42 +02:00
|
|
|
}
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
2013-09-03 13:23:42 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-26 17:40:37 +02:00
|
|
|
struct timespec current_ts;
|
|
|
|
|
DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_REALTIME, ¤t_ts));
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if (last_data_fetch > 0)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2019-06-26 17:40:37 +02:00
|
|
|
if ((current_ts.tv_sec - last_data_fetch) > cache_duration)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
cache_expired = 1;
|
|
|
|
|
}
|
2013-09-04 12:50:00 +02:00
|
|
|
else
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
cache_expired = 0;
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
2013-09-03 13:23:42 +02:00
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2014-11-17 10:42:15 +01:00
|
|
|
if (cache_enabled == 0)
|
|
|
|
|
{
|
|
|
|
|
cache_expired = 1;
|
|
|
|
|
}
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2019-09-03 12:01:33 +02:00
|
|
|
if (force_cache_expiration != 0)
|
|
|
|
|
{
|
|
|
|
|
cache_expired = 1;
|
|
|
|
|
force_cache_expiration = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
if (cache_expired == 1)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2014-08-26 10:50:44 -04:00
|
|
|
static char user_faked_time[BUFFERLEN]; /* changed to static for caching in v0.6 */
|
|
|
|
|
/* initialize with default or env. variable */
|
|
|
|
|
char *tmp_env;
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/* Can be enabled for testing ...
|
|
|
|
|
fprintf(stderr, "***************++ Cache expired ++**************\n");
|
|
|
|
|
*/
|
|
|
|
|
|
2014-08-26 10:50:44 -04:00
|
|
|
if (NULL != (tmp_env = getenv("FAKETIME")))
|
|
|
|
|
{
|
libfaketime.c: fix strncpy() issues raised by gcc 8.x
gcc 8.x introduced stricter checking on strncpy(), and causes the
following build failures:
libfaketime.c: In function 'fake_clock_gettime.part.4':
libfaketime.c:2134:7: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
strncpy(user_faked_time, tmp_env, BUFFERLEN);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c:2134:7: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
strncpy(user_faked_time, tmp_env, BUFFERLEN);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c: In function 'ftpl_init':
libfaketime.c:1884:12: error: 'strncpy' specified bound 1024 equals destination size [-Werror=stringop-truncation]
(void) strncpy(ft_spawn_target, getenv("FAKETIME_SPAWN_TARGET"), 1024);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c:1945:5: error: 'strncpy' specified bound 8192 equals destination size [-Werror=stringop-truncation]
strncpy(user_faked_time_fmt, tmp_env, BUFSIZ);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c: In function 'ftpl_init':
libfaketime.c:1884:12: error: 'strncpy' specified bound 1024 equals destination size [-Werror=stringop-truncation]
(void) strncpy(ft_spawn_target, getenv("FAKETIME_SPAWN_TARGET"), 1024);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libfaketime.c:1945:5: error: 'strncpy' specified bound 8192 equals destination size [-Werror=stringop-truncation]
strncpy(user_faked_time_fmt, tmp_env, BUFSIZ);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This commit fixes that by making sure we keep one final byte for the
nul terminator, as suggested by
https://github.com/wolfcw/libfaketime/issues/150.
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2018-05-17 22:55:22 +02:00
|
|
|
strncpy(user_faked_time, tmp_env, BUFFERLEN - 1);
|
|
|
|
|
user_faked_time[BUFFERLEN - 1] = 0;
|
2014-08-26 10:50:44 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
snprintf(user_faked_time, BUFFERLEN, "+0");
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-26 17:40:37 +02:00
|
|
|
last_data_fetch = current_ts.tv_sec;
|
2013-09-03 13:23:42 +02:00
|
|
|
/* fake time supplied as environment variable? */
|
2013-09-04 12:50:00 +02:00
|
|
|
if (parse_config_file)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2020-03-26 20:07:24 +01:00
|
|
|
if (read_config_file() == 0) parse_ft_string(user_faked_time);
|
2013-09-03 13:23:42 +02:00
|
|
|
} /* read fake time from file */
|
2020-03-26 20:07:24 +01:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
parse_ft_string(user_faked_time);
|
|
|
|
|
}
|
2020-02-14 10:36:29 +10:00
|
|
|
/* read monotonic faketime setting from envar */
|
|
|
|
|
get_fake_monotonic_setting(&fake_monotonic_clock);
|
2013-09-03 13:23:42 +02:00
|
|
|
} /* cache had expired */
|
2013-09-04 12:50:00 +02:00
|
|
|
|
|
|
|
|
if (infile_set)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-04 12:50:00 +02:00
|
|
|
if (load_time(tp))
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2020-03-11 14:34:25 +01:00
|
|
|
ret = 0;
|
|
|
|
|
goto abort;
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
2013-09-03 13:23:42 +02:00
|
|
|
}
|
2013-08-27 12:30:56 +02:00
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
/* check whether the user gave us an absolute time to fake */
|
2013-09-04 12:50:00 +02:00
|
|
|
switch (ft_mode)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
case FT_FREEZE: /* a specified time */
|
2013-09-04 12:50:00 +02:00
|
|
|
if (user_faked_time_set)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
*tp = user_faked_time_timespec;
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FT_START_AT: /* User-specified offset */
|
2013-09-04 12:50:00 +02:00
|
|
|
if (user_per_tick_inc_set)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2021-09-21 20:52:08 +00:00
|
|
|
/* increment time with every time() call */
|
2013-09-03 13:23:42 +02:00
|
|
|
next_time(tp, &user_per_tick_inc);
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
/* Speed-up / slow-down contributed by Karl Chen in v0.8 */
|
2013-11-07 19:35:18 +01:00
|
|
|
struct timespec tdiff, timeadj;
|
2013-09-04 12:50:00 +02:00
|
|
|
switch (clk_id)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
case CLOCK_REALTIME:
|
2016-01-29 11:20:39 +00:00
|
|
|
#ifdef CLOCK_REALTIME_COARSE
|
2015-12-28 16:54:55 +03:00
|
|
|
case CLOCK_REALTIME_COARSE:
|
2016-01-29 11:20:39 +00:00
|
|
|
#endif
|
2019-08-04 18:47:30 +02:00
|
|
|
timespecsub(tp, &ftpl_starttime.real, &tdiff);
|
2013-10-30 21:47:56 +01:00
|
|
|
break;
|
|
|
|
|
case CLOCK_MONOTONIC:
|
2016-01-29 11:20:39 +00:00
|
|
|
#ifdef CLOCK_MONOTONIC_COARSE
|
2015-12-28 16:54:55 +03:00
|
|
|
case CLOCK_MONOTONIC_COARSE:
|
2016-01-29 11:20:39 +00:00
|
|
|
#endif
|
2013-09-03 13:23:42 +02:00
|
|
|
timespecsub(tp, &ftpl_starttime.mon, &tdiff);
|
2013-10-30 21:47:56 +01:00
|
|
|
break;
|
2013-09-03 13:23:42 +02:00
|
|
|
case CLOCK_MONOTONIC_RAW:
|
2013-10-30 21:47:56 +01:00
|
|
|
timespecsub(tp, &ftpl_starttime.mon_raw, &tdiff);
|
2013-09-03 13:23:42 +02:00
|
|
|
break;
|
2017-05-19 19:14:58 +02:00
|
|
|
#ifdef CLOCK_BOOTTIME
|
|
|
|
|
case CLOCK_BOOTTIME:
|
|
|
|
|
timespecsub(tp, &ftpl_starttime.boot, &tdiff);
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
2013-10-30 21:47:56 +01:00
|
|
|
default:
|
2017-11-30 20:28:06 +01:00
|
|
|
timespecsub(tp, &ftpl_starttime.real, &tdiff);
|
|
|
|
|
break;
|
2013-09-03 13:23:42 +02:00
|
|
|
} // end of switch (clk_id)
|
2013-09-04 12:50:00 +02:00
|
|
|
if (user_rate_set)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-10-30 21:47:56 +01:00
|
|
|
timespecmul(&tdiff, user_rate, &timeadj);
|
2013-09-04 12:50:00 +02:00
|
|
|
}
|
|
|
|
|
else
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
|
|
|
|
timeadj = tdiff;
|
2013-10-30 21:47:56 +01:00
|
|
|
}
|
2013-09-03 13:23:42 +02:00
|
|
|
timespecadd(&user_faked_time_timespec, &timeadj, tp);
|
2013-09-01 16:04:21 +02:00
|
|
|
}
|
|
|
|
|
break;
|
2013-09-03 13:23:42 +02:00
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
default:
|
2020-03-11 14:34:25 +01:00
|
|
|
ret = -1;
|
|
|
|
|
goto abort;
|
2013-09-03 13:23:42 +02:00
|
|
|
} // end of switch(ft_mode)
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2020-03-11 14:34:25 +01:00
|
|
|
abort:
|
2013-08-19 11:47:49 +02:00
|
|
|
#ifdef PTHREAD_SINGLETHREADED_TIME
|
2013-09-03 13:23:42 +02:00
|
|
|
pthread_cleanup_pop(1);
|
2013-08-19 11:47:49 +02:00
|
|
|
#endif
|
2020-03-11 14:34:25 +01:00
|
|
|
// came here via goto abort?
|
|
|
|
|
if (ret != INT_MAX) return ret;
|
2013-09-03 13:23:42 +02:00
|
|
|
save_time(tp);
|
2019-08-22 00:49:21 +02:00
|
|
|
|
|
|
|
|
/* Cache this most recent real and faked time we encountered */
|
|
|
|
|
if (clk_id == CLOCK_REALTIME)
|
|
|
|
|
{
|
|
|
|
|
ftpl_timecache.real.tv_sec = tp_save.tv_sec;
|
|
|
|
|
ftpl_timecache.real.tv_nsec = tp_save.tv_nsec;
|
|
|
|
|
ftpl_faketimecache.real.tv_sec = tp->tv_sec;
|
|
|
|
|
ftpl_faketimecache.real.tv_nsec = tp->tv_nsec;
|
|
|
|
|
}
|
|
|
|
|
else if (clk_id == CLOCK_MONOTONIC)
|
|
|
|
|
{
|
|
|
|
|
ftpl_timecache.mon.tv_sec = tp_save.tv_sec;
|
|
|
|
|
ftpl_timecache.mon.tv_nsec = tp_save.tv_nsec;
|
|
|
|
|
ftpl_faketimecache.mon.tv_sec = tp->tv_sec;
|
|
|
|
|
ftpl_faketimecache.mon.tv_nsec = tp->tv_nsec;
|
|
|
|
|
}
|
|
|
|
|
else if (clk_id == CLOCK_MONOTONIC_RAW)
|
|
|
|
|
{
|
|
|
|
|
ftpl_timecache.mon_raw.tv_sec = tp_save.tv_sec;
|
|
|
|
|
ftpl_timecache.mon_raw.tv_nsec = tp_save.tv_nsec;
|
|
|
|
|
ftpl_faketimecache.mon_raw.tv_sec = tp->tv_sec;
|
|
|
|
|
ftpl_faketimecache.mon_raw.tv_nsec = tp->tv_nsec;
|
|
|
|
|
}
|
|
|
|
|
#ifdef CLOCK_BOOTTIME
|
|
|
|
|
else if (clk_id == CLOCK_BOOTTIME)
|
|
|
|
|
{
|
|
|
|
|
ftpl_timecache.boot.tv_sec = tp_save.tv_sec;
|
|
|
|
|
ftpl_timecache.boot.tv_nsec = tp_save.tv_nsec;
|
|
|
|
|
ftpl_faketimecache.boot.tv_sec = tp->tv_sec;
|
|
|
|
|
ftpl_faketimecache.boot.tv_nsec = tp->tv_nsec;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2019-09-05 22:52:07 +02:00
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
return 0;
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-04 22:54:07 +02:00
|
|
|
int fake_gettimeofday(struct timeval *tv)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-09-01 16:04:21 +02:00
|
|
|
struct timespec ts;
|
|
|
|
|
int ret;
|
|
|
|
|
ts.tv_sec = tv->tv_sec;
|
2013-09-04 13:10:26 +02:00
|
|
|
ts.tv_nsec = tv->tv_usec * 1000 + ftpl_starttime.real.tv_nsec % 1000;
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
ret = fake_clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
|
tv->tv_sec = ts.tv_sec;
|
|
|
|
|
tv->tv_usec =ts.tv_nsec / 1000;
|
2013-08-19 11:47:49 +02:00
|
|
|
|
2013-09-01 16:04:21 +02:00
|
|
|
return ret;
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-03 13:42:10 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:42:10 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
* Faked system functions: Apple Mac OS X specific === FAKE(OSX)
|
|
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
2025-06-07 20:28:33 +08:00
|
|
|
#ifdef __APPLE__
|
2013-09-05 11:15:51 +02:00
|
|
|
/*
|
|
|
|
|
* clock_gettime implementation for __APPLE__
|
|
|
|
|
* @note It always behave like being called with CLOCK_REALTIME.
|
|
|
|
|
*/
|
2013-11-07 19:35:18 +01:00
|
|
|
static int apple_clock_gettime(clockid_t clk_id, struct timespec *tp)
|
|
|
|
|
{
|
2013-08-30 20:15:19 +02:00
|
|
|
int result;
|
2013-09-05 11:15:51 +02:00
|
|
|
mach_timespec_t cur_timeclockid_t;
|
|
|
|
|
(void) clk_id; /* unused */
|
|
|
|
|
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(clock_get_time)) return -1;
|
2013-08-30 20:15:19 +02:00
|
|
|
|
2013-09-05 11:15:51 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_clock_get_time)(clock_serv_real, &cur_timeclockid_t));
|
|
|
|
|
tp->tv_sec = cur_timeclockid_t.tv_sec;
|
|
|
|
|
tp->tv_nsec = cur_timeclockid_t.tv_nsec;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int clock_get_time(clock_serv_t clock_serv, mach_timespec_t *cur_timeclockid_t)
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
2013-08-30 20:15:19 +02:00
|
|
|
/*
|
|
|
|
|
* Initialize our result with the real current time from CALENDAR_CLOCK.
|
|
|
|
|
* This is a bit of cheating, but we don't keep track of obtained clock
|
|
|
|
|
* services.
|
|
|
|
|
*/
|
2013-09-05 11:15:51 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_clock_gettime)(CLOCK_REALTIME, &ts));
|
2013-08-30 20:15:19 +02:00
|
|
|
if (result == -1) return result; /* original function failed */
|
|
|
|
|
|
|
|
|
|
/* pass the real current time to our faking version, overwriting it */
|
|
|
|
|
result = fake_clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
|
cur_timeclockid_t->tv_sec = ts.tv_sec;
|
|
|
|
|
cur_timeclockid_t->tv_nsec = ts.tv_nsec;
|
|
|
|
|
|
|
|
|
|
/* return the result to the caller */
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-09-03 13:23:42 +02:00
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
2013-09-03 13:23:42 +02:00
|
|
|
* =======================================================================
|
2013-09-03 13:42:10 +02:00
|
|
|
* Faked system-internal functions === FAKE(INT)
|
2013-09-03 13:23:42 +02:00
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
2013-08-19 11:47:49 +02:00
|
|
|
#ifdef FAKE_INTERNAL_CALLS
|
2013-09-04 12:50:00 +02:00
|
|
|
int __gettimeofday(struct timeval *tv, void *tz)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-10-16 09:16:05 +02:00
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
|
if (tv == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check whether we've got a pointer to the real ftime() function yet */
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(__gettimeofday)) return -1;
|
2013-10-16 09:16:05 +02:00
|
|
|
|
|
|
|
|
/* initialize our result with the real current time */
|
|
|
|
|
DONT_FAKE_TIME(result = (*real___gettimeofday)(tv, tz));
|
|
|
|
|
if (result == -1) return result; /* original function failed */
|
|
|
|
|
|
|
|
|
|
/* pass the real current time to our faking version, overwriting it */
|
|
|
|
|
result = fake_gettimeofday(tv);
|
|
|
|
|
|
|
|
|
|
/* return the result to the caller */
|
|
|
|
|
return result;
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
int __clock_gettime(clockid_t clk_id, struct timespec *tp)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-10-16 09:16:05 +02:00
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
|
if (tp == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(__clock_gettime)) return -1;
|
2013-10-16 09:16:05 +02:00
|
|
|
|
|
|
|
|
/* initialize our result with the real current time */
|
|
|
|
|
DONT_FAKE_TIME(result = (*real___clock_gettime)(clk_id, tp));
|
|
|
|
|
if (result == -1) return result; /* original function failed */
|
|
|
|
|
|
|
|
|
|
/* pass the real current time to our faking version, overwriting it */
|
2018-01-01 21:46:35 +01:00
|
|
|
if (fake_monotonic_clock || (clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW
|
|
|
|
|
#ifdef CLOCK_MONOTONIC_COARSE
|
|
|
|
|
&& clk_id != CLOCK_MONOTONIC_COARSE
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef CLOCK_BOOTTIME
|
|
|
|
|
&& clk_id != CLOCK_BOOTTIME
|
|
|
|
|
#endif
|
|
|
|
|
))
|
|
|
|
|
|
2014-07-24 16:54:08 -07:00
|
|
|
{
|
|
|
|
|
result = fake_clock_gettime(clk_id, tp);
|
|
|
|
|
}
|
2013-10-16 09:16:05 +02:00
|
|
|
|
|
|
|
|
/* return the result to the caller */
|
|
|
|
|
return result;
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-16 09:16:05 +02:00
|
|
|
time_t __time(time_t *time_tptr)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-10-16 09:16:05 +02:00
|
|
|
struct timespec tp;
|
|
|
|
|
time_t result;
|
|
|
|
|
|
2013-10-16 09:46:29 +02:00
|
|
|
DONT_FAKE_TIME(result = (*real_clock_gettime)(CLOCK_REALTIME, &tp));
|
2013-10-16 09:16:05 +02:00
|
|
|
if (result == -1) return -1;
|
|
|
|
|
|
|
|
|
|
/* pass the real current time to our faking version, overwriting it */
|
|
|
|
|
(void)fake_clock_gettime(CLOCK_REALTIME, &tp);
|
|
|
|
|
|
|
|
|
|
if (time_tptr != NULL)
|
|
|
|
|
{
|
|
|
|
|
*time_tptr = tp.tv_sec;
|
|
|
|
|
}
|
|
|
|
|
return tp.tv_sec;
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-16 09:16:05 +02:00
|
|
|
int __ftime(struct timeb *tb)
|
2013-09-03 13:23:42 +02:00
|
|
|
{
|
2013-10-16 09:16:05 +02:00
|
|
|
struct timespec tp;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
|
if (tb == NULL)
|
|
|
|
|
return 0; /* ftime() always returns 0, see manpage */
|
|
|
|
|
|
|
|
|
|
/* Check whether we've got a pointer to the real ftime() function yet */
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(__ftime)) return 0;
|
2013-10-16 09:16:05 +02:00
|
|
|
|
|
|
|
|
/* initialize our TZ result with the real current time */
|
|
|
|
|
DONT_FAKE_TIME(result = (*real___ftime)(tb));
|
|
|
|
|
if (result == -1)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_clock_gettime)(CLOCK_REALTIME, &tp));
|
|
|
|
|
if (result == -1) return -1;
|
|
|
|
|
|
|
|
|
|
/* pass the real current time to our faking version, overwriting it */
|
|
|
|
|
(void)fake_clock_gettime(CLOCK_REALTIME, &tp);
|
|
|
|
|
|
|
|
|
|
tb->time = tp.tv_sec;
|
|
|
|
|
tb->millitm = tp.tv_nsec / 1000000;
|
|
|
|
|
|
|
|
|
|
/* return the result to the caller */
|
|
|
|
|
return result; /* will always be 0 (see manpage) */
|
2013-08-19 11:47:49 +02:00
|
|
|
}
|
2013-10-16 09:16:05 +02:00
|
|
|
|
2013-08-19 11:47:49 +02:00
|
|
|
#endif
|
|
|
|
|
|
2017-12-31 02:23:56 +01:00
|
|
|
/*
|
|
|
|
|
* =======================================================================
|
|
|
|
|
* Faked pthread_cond_timedwait === FAKE(pthread)
|
|
|
|
|
* =======================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* pthread_cond_timedwait
|
|
|
|
|
|
|
|
|
|
The specified absolute time in pthread_cond_timedwait is directly
|
|
|
|
|
passed to the kernel via the futex syscall. The kernel, however,
|
|
|
|
|
does not know about the fake time. In 99.9% of cases, the time
|
|
|
|
|
until this function should wait is calculated by an application
|
|
|
|
|
relatively to the current time, which has been faked in the
|
|
|
|
|
application. Hence, we should convert the waiting time back to real
|
|
|
|
|
time.
|
|
|
|
|
|
|
|
|
|
pthread_cond_timedwait in GLIBC_2_2_5 only supports
|
|
|
|
|
CLOCK_REALTIME. Since the init and destroy functions are not
|
|
|
|
|
redefined for GLIBC_2_2_5, a corresponding cond will never be
|
|
|
|
|
added to monotonic_conds and hence the correct branch will
|
|
|
|
|
always be taken.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef FAKE_PTHREAD
|
|
|
|
|
|
|
|
|
|
typedef enum {FT_COMPAT_GLIBC_2_2_5, FT_COMPAT_GLIBC_2_3_2} ft_lib_compat_pthread;
|
|
|
|
|
|
|
|
|
|
struct pthread_cond_monotonic {
|
|
|
|
|
pthread_cond_t *ptr;
|
|
|
|
|
UT_hash_handle hh;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct pthread_cond_monotonic *monotonic_conds = NULL;
|
|
|
|
|
|
|
|
|
|
int pthread_cond_init_232(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr)
|
|
|
|
|
{
|
|
|
|
|
clockid_t clock_id;
|
|
|
|
|
int result;
|
|
|
|
|
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2022-02-16 11:46:18 +02:00
|
|
|
if (!CHECK_MISSING_REAL(pthread_cond_init_232)) return -1;
|
2017-12-31 02:23:56 +01:00
|
|
|
result = real_pthread_cond_init_232(cond, attr);
|
|
|
|
|
|
|
|
|
|
if (result != 0 || attr == NULL)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
pthread_condattr_getclock(attr, &clock_id);
|
|
|
|
|
|
|
|
|
|
if (clock_id == CLOCK_MONOTONIC) {
|
|
|
|
|
struct pthread_cond_monotonic *e = (struct pthread_cond_monotonic*)malloc(sizeof(struct pthread_cond_monotonic));
|
|
|
|
|
e->ptr = cond;
|
2019-05-31 22:27:10 +02:00
|
|
|
|
2025-01-22 09:29:58 +00:00
|
|
|
if (pthread_rwlock_wrlock(&monotonic_conds_lock) != 0) {
|
|
|
|
|
fprintf(stderr,"can't acquire write monotonic_conds_lock\n");
|
|
|
|
|
exit(-1);
|
2019-05-31 22:27:10 +02:00
|
|
|
}
|
2017-12-31 02:23:56 +01:00
|
|
|
HASH_ADD_PTR(monotonic_conds, ptr, e);
|
2019-05-31 22:27:10 +02:00
|
|
|
pthread_rwlock_unlock(&monotonic_conds_lock);
|
2017-12-31 02:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pthread_cond_destroy_232(pthread_cond_t *cond)
|
|
|
|
|
{
|
|
|
|
|
struct pthread_cond_monotonic* e;
|
2019-05-31 22:27:10 +02:00
|
|
|
|
2025-01-22 09:11:46 +00:00
|
|
|
ftpl_init();
|
|
|
|
|
|
2025-01-22 09:29:58 +00:00
|
|
|
if (pthread_rwlock_wrlock(&monotonic_conds_lock) != 0) {
|
|
|
|
|
fprintf(stderr,"can't acquire write monotonic_conds_lock\n");
|
|
|
|
|
exit(-1);
|
2019-05-31 22:27:10 +02:00
|
|
|
}
|
2017-12-31 02:23:56 +01:00
|
|
|
HASH_FIND_PTR(monotonic_conds, &cond, e);
|
|
|
|
|
if (e) {
|
|
|
|
|
HASH_DEL(monotonic_conds, e);
|
|
|
|
|
free(e);
|
|
|
|
|
}
|
2019-05-31 22:27:10 +02:00
|
|
|
pthread_rwlock_unlock(&monotonic_conds_lock);
|
2017-12-31 02:23:56 +01:00
|
|
|
|
|
|
|
|
return real_pthread_cond_destroy_232(cond);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 20:57:38 +01:00
|
|
|
/*
|
|
|
|
|
* Check whether we need a run-time activation of the
|
|
|
|
|
* forced monotonic fix to avoid faked / unfaked timestamp
|
|
|
|
|
* confusion between the application and glibc internals.
|
|
|
|
|
*/
|
|
|
|
|
bool needs_forced_monotonic_fix(char *function_name)
|
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
char *env_var;
|
2022-02-28 15:19:08 +01:00
|
|
|
#ifdef __GLIBC__
|
2022-02-25 21:25:58 +01:00
|
|
|
const char *glibc_version_string = gnu_get_libc_version();
|
2022-02-28 15:19:08 +01:00
|
|
|
#endif
|
2022-02-25 20:57:38 +01:00
|
|
|
|
|
|
|
|
if (function_name == NULL) return false;
|
|
|
|
|
|
|
|
|
|
/* The forced monotonic fix can be activated by setting an
|
|
|
|
|
* environment variable to 1, or disabled by setting it to 0 */
|
|
|
|
|
if ((env_var = getenv("FAKETIME_FORCE_MONOTONIC_FIX")) != NULL)
|
|
|
|
|
{
|
2022-02-26 11:07:38 +01:00
|
|
|
if (env_var[0] == '0')
|
|
|
|
|
result = false;
|
|
|
|
|
else
|
|
|
|
|
result = true;
|
2022-02-25 20:57:38 +01:00
|
|
|
}
|
|
|
|
|
else
|
2022-05-14 23:09:03 +02:00
|
|
|
{
|
2022-02-28 15:19:08 +01:00
|
|
|
#ifdef __GLIBC__
|
2022-05-14 23:09:03 +02:00
|
|
|
/* Here we try to derive the necessity for a forced monotonic fix *
|
2022-02-25 20:57:38 +01:00
|
|
|
* based on glibc version. What could possibly go wrong? */
|
|
|
|
|
|
2022-02-25 21:25:58 +01:00
|
|
|
int glibc_major, glibc_minor;
|
|
|
|
|
sscanf(glibc_version_string, "%d.%d", &glibc_major, &glibc_minor);
|
|
|
|
|
|
|
|
|
|
/* The following decision logic is yet purely based on experiences
|
|
|
|
|
* with pthread_cond_timedwait(). The used boundaries may still be
|
|
|
|
|
* imprecise. */
|
|
|
|
|
if ( (glibc_major == 2) &&
|
2022-02-26 11:07:38 +01:00
|
|
|
((glibc_minor <= 24) || (glibc_minor >= 30)) )
|
2022-02-25 21:25:58 +01:00
|
|
|
{
|
|
|
|
|
result = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
2022-02-28 15:19:08 +01:00
|
|
|
#endif
|
2022-02-25 21:25:58 +01:00
|
|
|
result = false; // avoid forced monotonic fixes unless really necessary
|
2022-02-25 20:57:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (getenv("FAKETIME_DEBUG") != NULL)
|
2022-02-28 15:19:08 +01:00
|
|
|
#ifdef __GLIBC__
|
2022-02-26 11:07:38 +01:00
|
|
|
fprintf(stderr, "libfaketime: forced monotonic fix for %s = %s (glibc version %s)\n",
|
2022-02-25 21:25:58 +01:00
|
|
|
function_name, result ? "yes":"no", glibc_version_string);
|
2022-02-28 15:19:08 +01:00
|
|
|
#else
|
2022-05-14 23:09:03 +02:00
|
|
|
fprintf(stderr, "libfaketime: forced monotonic fix for %s = %s (not glibc-compiled)\n",
|
2022-02-28 15:19:08 +01:00
|
|
|
function_name, result ? "yes":"no");
|
|
|
|
|
#endif
|
2022-02-25 20:57:38 +01:00
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2019-05-31 22:27:10 +02:00
|
|
|
|
2017-12-31 02:23:56 +01:00
|
|
|
int pthread_cond_timedwait_common(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime, ft_lib_compat_pthread compat)
|
|
|
|
|
{
|
2025-08-13 15:15:12 -04:00
|
|
|
struct timespec tp, tdiff_actual, realtime, faketime, current_monotonic;
|
2017-12-31 02:23:56 +01:00
|
|
|
struct timespec *tf = NULL;
|
|
|
|
|
struct pthread_cond_monotonic* e;
|
2018-02-19 10:25:13 +01:00
|
|
|
char *tmp_env;
|
|
|
|
|
int wait_ms;
|
2017-12-31 02:23:56 +01:00
|
|
|
clockid_t clk_id;
|
2018-05-17 22:57:53 +02:00
|
|
|
int result = 0;
|
2017-12-31 02:23:56 +01:00
|
|
|
|
2025-01-22 09:11:46 +00:00
|
|
|
ftpl_init();
|
|
|
|
|
|
2017-12-31 02:23:56 +01:00
|
|
|
if (abstime != NULL)
|
|
|
|
|
{
|
2025-01-22 09:29:58 +00:00
|
|
|
if (pthread_rwlock_rdlock(&monotonic_conds_lock) != 0) {
|
|
|
|
|
fprintf(stderr,"can't acquire read monotonic_conds_lock\n");
|
|
|
|
|
exit(-1);
|
2019-05-31 22:27:10 +02:00
|
|
|
}
|
2017-12-31 02:23:56 +01:00
|
|
|
HASH_FIND_PTR(monotonic_conds, &cond, e);
|
2019-05-31 22:27:10 +02:00
|
|
|
pthread_rwlock_unlock(&monotonic_conds_lock);
|
2017-12-31 02:23:56 +01:00
|
|
|
if (e != NULL)
|
|
|
|
|
clk_id = CLOCK_MONOTONIC;
|
|
|
|
|
else
|
|
|
|
|
clk_id = CLOCK_REALTIME;
|
|
|
|
|
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, &realtime));
|
|
|
|
|
if (result == -1)
|
|
|
|
|
{
|
|
|
|
|
return EINVAL;
|
|
|
|
|
}
|
|
|
|
|
faketime = realtime;
|
|
|
|
|
(void)fake_clock_gettime(clk_id, &faketime);
|
|
|
|
|
|
2018-02-19 10:25:13 +01:00
|
|
|
if ((tmp_env = getenv("FAKETIME_WAIT_MS")) != NULL)
|
2017-12-31 02:23:56 +01:00
|
|
|
{
|
2018-02-19 10:25:13 +01:00
|
|
|
wait_ms = atol(tmp_env);
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, &realtime));
|
|
|
|
|
if (result == -1)
|
|
|
|
|
{
|
|
|
|
|
return EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tdiff_actual.tv_sec = wait_ms / 1000;
|
|
|
|
|
tdiff_actual.tv_nsec = (wait_ms % 1000) * 1000000;
|
|
|
|
|
timespecadd(&realtime, &tdiff_actual, &tp);
|
|
|
|
|
|
|
|
|
|
tf = &tp;
|
2017-12-31 02:23:56 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-06-13 17:06:33 -04:00
|
|
|
if (!fake_monotonic_clock && clk_id == CLOCK_MONOTONIC)
|
|
|
|
|
{
|
2025-08-13 15:15:12 -04:00
|
|
|
DONT_FAKE_TIME(result = (*real_clock_gettime)(CLOCK_MONOTONIC, ¤t_monotonic));
|
|
|
|
|
if (result == -1)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
timespecsub(abstime, ¤t_monotonic, &tp);
|
2025-06-13 17:06:33 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
timespecsub(abstime, &faketime, &tp);
|
|
|
|
|
}
|
2018-02-19 10:25:13 +01:00
|
|
|
if (user_rate_set)
|
|
|
|
|
{
|
|
|
|
|
timespecmul(&tp, 1.0 / user_rate, &tdiff_actual);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tdiff_actual = tp;
|
|
|
|
|
}
|
2017-12-31 02:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* For CLOCK_MONOTONIC, pthread_cond_timedwait uses clock_gettime
|
|
|
|
|
internally to calculate the appropriate duration for the
|
|
|
|
|
waiting time. This already uses the faked functions, hence, the
|
|
|
|
|
fake time needs to be passed to pthread_cond_timedwait for
|
|
|
|
|
CLOCK_MONOTONIC. */
|
2019-08-19 19:38:01 +02:00
|
|
|
#ifndef __ARM_ARCH
|
2019-08-23 10:29:53 +02:00
|
|
|
#ifndef FORCE_MONOTONIC_FIX
|
2022-02-25 20:57:38 +01:00
|
|
|
if (clk_id == CLOCK_MONOTONIC) {
|
2025-08-13 15:15:12 -04:00
|
|
|
if (!fake_monotonic_clock) {
|
|
|
|
|
/* When not faking monotonic clock, use real monotonic time as base */
|
|
|
|
|
DONT_FAKE_TIME(result = (*real_clock_gettime)(CLOCK_MONOTONIC, ¤t_monotonic));
|
|
|
|
|
if (result == -1)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
timespecadd(¤t_monotonic, &tdiff_actual, &tp);
|
|
|
|
|
}
|
|
|
|
|
else if (needs_forced_monotonic_fix("pthread_cond_timedwait") == true) {
|
2022-02-25 20:57:38 +01:00
|
|
|
timespecadd(&realtime, &tdiff_actual, &tp);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
timespecadd(&faketime, &tdiff_actual, &tp);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-31 02:23:56 +01:00
|
|
|
else
|
2019-08-23 10:29:53 +02:00
|
|
|
#endif
|
2019-08-19 19:38:01 +02:00
|
|
|
#endif
|
2017-12-31 02:23:56 +01:00
|
|
|
timespecadd(&realtime, &tdiff_actual, &tp);
|
|
|
|
|
|
|
|
|
|
tf = &tp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (compat) {
|
|
|
|
|
case FT_COMPAT_GLIBC_2_3_2:
|
|
|
|
|
result = real_pthread_cond_timedwait_232(cond, mutex, tf);
|
|
|
|
|
break;
|
|
|
|
|
case FT_COMPAT_GLIBC_2_2_5:
|
|
|
|
|
result = real_pthread_cond_timedwait_225(cond, mutex, tf);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pthread_cond_timedwait_225(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
|
|
|
|
|
{
|
|
|
|
|
return pthread_cond_timedwait_common(cond, mutex, abstime, FT_COMPAT_GLIBC_2_2_5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pthread_cond_timedwait_232(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
|
|
|
|
|
{
|
|
|
|
|
return pthread_cond_timedwait_common(cond, mutex, abstime, FT_COMPAT_GLIBC_2_3_2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__asm__(".symver pthread_cond_timedwait_225, pthread_cond_timedwait@GLIBC_2.2.5");
|
2019-08-29 09:55:04 +02:00
|
|
|
#if defined __ARM_ARCH || defined FORCE_PTHREAD_NONVER
|
2019-08-19 19:16:07 +02:00
|
|
|
__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@");
|
|
|
|
|
__asm__(".symver pthread_cond_init_232, pthread_cond_init@@");
|
|
|
|
|
__asm__(".symver pthread_cond_destroy_232, pthread_cond_destroy@@");
|
|
|
|
|
#else
|
2017-12-31 02:23:56 +01:00
|
|
|
__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@GLIBC_2.3.2");
|
|
|
|
|
__asm__(".symver pthread_cond_init_232, pthread_cond_init@@GLIBC_2.3.2");
|
|
|
|
|
__asm__(".symver pthread_cond_destroy_232, pthread_cond_destroy@@GLIBC_2.3.2");
|
2019-08-19 19:16:07 +02:00
|
|
|
#endif
|
2017-12-31 02:23:56 +01:00
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-09-03 12:01:33 +02:00
|
|
|
/*
|
|
|
|
|
* Intercept calls to time-setting functions if compiled with FAKE_SETTIME set.
|
|
|
|
|
* Based on suggestion and prototype by @ojura, see https://github.com/wolfcw/libfaketime/issues/179
|
|
|
|
|
*/
|
|
|
|
|
#ifdef FAKE_SETTIME
|
2022-02-16 21:27:08 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_clock_settime(clockid_t clk_id, const struct timespec *tp) {
|
|
|
|
|
#else
|
2019-09-03 12:01:33 +02:00
|
|
|
int clock_settime(clockid_t clk_id, const struct timespec *tp) {
|
2022-02-16 21:27:08 +01:00
|
|
|
#endif
|
2019-09-03 12:01:33 +02:00
|
|
|
|
|
|
|
|
/* only CLOCK_REALTIME can be set */
|
|
|
|
|
if (clk_id != CLOCK_REALTIME) {
|
|
|
|
|
errno = EPERM;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* sanity check for the pointer */
|
|
|
|
|
if (tp == NULL) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* When setting the FAKETIME environment variable to the new timestamp,
|
|
|
|
|
we do not have to care about 'x' or 'i' modifiers given previously,
|
|
|
|
|
as they are not erased when parsing them. */
|
|
|
|
|
struct timespec current_time;
|
2022-02-16 21:27:08 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
DONT_FAKE_TIME(macos_clock_gettime(clk_id, ¤t_time))
|
|
|
|
|
#else
|
2019-09-03 12:01:33 +02:00
|
|
|
DONT_FAKE_TIME(clock_gettime(clk_id, ¤t_time))
|
2022-02-16 21:27:08 +01:00
|
|
|
#endif
|
2019-09-03 12:01:33 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
time_t sec_diff = tp->tv_sec - current_time.tv_sec;
|
|
|
|
|
long nsec_diff = tp->tv_nsec - current_time.tv_nsec;
|
|
|
|
|
char newenv_string[256];
|
|
|
|
|
double offset = (double) sec_diff;
|
|
|
|
|
offset += (double) nsec_diff/SEC_TO_nSEC;
|
|
|
|
|
snprintf(newenv_string, 255, "%+f", offset);
|
|
|
|
|
|
2020-04-09 13:04:40 +02:00
|
|
|
parse_config_file = false; /* #247: make sure environment takes precedence */
|
2019-09-03 12:01:33 +02:00
|
|
|
setenv("FAKETIME", newenv_string, 1);
|
|
|
|
|
force_cache_expiration = 1; /* make sure it becomes effective immediately */
|
|
|
|
|
|
2020-04-09 15:54:23 +02:00
|
|
|
/* If FAKETIME_TIMESTAMP_FILE was given in environment,
|
|
|
|
|
* and if FAKETIME_UPDATE_TIMESTAMP_FILE=1, then update it.
|
|
|
|
|
* This allows other process instances to share the same time. */
|
|
|
|
|
if ( (getenv("FAKETIME_TIMESTAMP_FILE") != NULL)
|
|
|
|
|
&& (*getenv("FAKETIME_TIMESTAMP_FILE") != '\0')
|
|
|
|
|
&& (getenv("FAKETIME_UPDATE_TIMESTAMP_FILE") != NULL)
|
|
|
|
|
&& (strcmp(getenv("FAKETIME_UPDATE_TIMESTAMP_FILE"), "1") == 0))
|
|
|
|
|
{
|
|
|
|
|
const char *error = NULL;
|
|
|
|
|
FILE *envfile;
|
|
|
|
|
static char custom_filename[BUFSIZ];
|
|
|
|
|
(void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE"));
|
|
|
|
|
|
|
|
|
|
if ((envfile = fopen(custom_filename, "wt")) != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (fprintf(envfile, "%+f\n", offset) < 0)
|
|
|
|
|
{
|
|
|
|
|
error = "to write to file";
|
|
|
|
|
}
|
|
|
|
|
if (fclose(envfile) != 0)
|
|
|
|
|
{
|
|
|
|
|
error = "to close file";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
error = "to open file";
|
|
|
|
|
}
|
|
|
|
|
if (error)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "libfaketime: In clock_settime(), failed to "
|
|
|
|
|
"%s while updating FAKETIME_TIMESTAMP_FILE (`%s'): %s\n",
|
|
|
|
|
error, getenv("FAKETIME_TIMESTAMP_FILE"), strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-03 12:01:33 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 21:27:08 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_settimeofday(const struct timeval *tv, void *tz)
|
|
|
|
|
#else
|
2019-09-05 22:52:07 +02:00
|
|
|
int settimeofday(const struct timeval *tv, void *tz)
|
2022-02-16 21:27:08 +01:00
|
|
|
#endif
|
2019-09-03 12:01:33 +02:00
|
|
|
{
|
|
|
|
|
/* The use of timezone *tz is obsolete and simply ignored here. */
|
|
|
|
|
if (tz == NULL) tz = NULL;
|
|
|
|
|
|
|
|
|
|
if (tv == NULL)
|
|
|
|
|
{
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
struct timespec tp;
|
|
|
|
|
tp.tv_sec = tv->tv_sec;
|
|
|
|
|
tp.tv_nsec = tv->tv_usec * 1000;
|
2022-02-16 21:27:08 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
macos_clock_settime(CLOCK_REALTIME, &tp);
|
|
|
|
|
#else
|
2019-09-05 22:52:07 +02:00
|
|
|
clock_settime(CLOCK_REALTIME, &tp);
|
2022-02-16 21:27:08 +01:00
|
|
|
#endif
|
2019-09-03 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 21:27:08 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_adjtime (const struct timeval *delta, struct timeval *olddelta)
|
|
|
|
|
#else
|
2019-09-05 22:52:07 +02:00
|
|
|
int adjtime (const struct timeval *delta, struct timeval *olddelta)
|
2022-02-16 21:27:08 +01:00
|
|
|
#endif
|
2019-09-03 12:01:33 +02:00
|
|
|
{
|
|
|
|
|
/* Always signal true full success when olddelta is requested. */
|
|
|
|
|
if (olddelta != NULL)
|
|
|
|
|
{
|
|
|
|
|
olddelta->tv_sec = 0;
|
|
|
|
|
olddelta->tv_usec = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (delta != NULL)
|
|
|
|
|
{
|
|
|
|
|
struct timespec tp;
|
2022-02-16 21:27:08 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
macos_clock_gettime(CLOCK_REALTIME, &tp);
|
|
|
|
|
#else
|
2019-09-03 12:01:33 +02:00
|
|
|
clock_gettime(CLOCK_REALTIME, &tp);
|
2022-02-16 21:27:08 +01:00
|
|
|
#endif
|
2019-09-03 12:01:33 +02:00
|
|
|
tp.tv_sec += delta->tv_sec;
|
|
|
|
|
tp.tv_nsec += delta->tv_usec * 1000;
|
|
|
|
|
/* This actually will make the clock jump instead of gradually
|
|
|
|
|
adjusting it, but we fulfill the caller's intention and an
|
|
|
|
|
additional thread just for the gradual changes does not seem
|
|
|
|
|
to be worth the effort presently. */
|
2022-02-16 21:27:08 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
2019-09-05 22:52:07 +02:00
|
|
|
clock_settime(CLOCK_REALTIME, &tp);
|
2022-02-16 21:27:08 +01:00
|
|
|
#else
|
|
|
|
|
clock_settime(CLOCK_REALTIME, &tp);
|
|
|
|
|
#endif
|
2019-09-03 12:01:33 +02:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2017-12-31 02:23:56 +01:00
|
|
|
|
2020-11-15 21:57:10 +01:00
|
|
|
#ifdef FAKE_RANDOM
|
|
|
|
|
/*
|
|
|
|
|
Local copy of
|
|
|
|
|
Middle Square Weyl Sequence Random Number Generator
|
|
|
|
|
Copyright (c) 2014-2020 Bernard Widynski
|
|
|
|
|
License: GNU GPL v3
|
|
|
|
|
see https://mswsrng.wixsite.com/rand
|
|
|
|
|
|
|
|
|
|
adapted to take the seed s as a parameter and return only a byte
|
|
|
|
|
*/
|
|
|
|
|
inline static uint32_t fakerandom_msws(uint64_t s) {
|
|
|
|
|
static uint64_t x = 0, w = 0;
|
|
|
|
|
x *= x; x += (w += s);
|
|
|
|
|
x = (x>>32) | (x<<32);
|
|
|
|
|
return (char) x & 0xFF;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-24 16:14:02 -05:00
|
|
|
/* return 0 if no FAKERANDOM_SEED was seen */
|
|
|
|
|
static int bypass_randomness(void* buf, size_t buflen) {
|
2020-11-15 21:57:10 +01:00
|
|
|
char *seedstring = getenv("FAKERANDOM_SEED");
|
|
|
|
|
char *b = buf;
|
|
|
|
|
|
|
|
|
|
if (seedstring != NULL) {
|
|
|
|
|
long long int seed = strtoll(seedstring, NULL, 0);
|
|
|
|
|
for (size_t i = 0; i < buflen; i++) {
|
|
|
|
|
b[i] = fakerandom_msws(seed);
|
|
|
|
|
}
|
2021-02-24 16:14:02 -05:00
|
|
|
return 1;
|
2020-11-15 21:57:10 +01:00
|
|
|
}
|
2021-02-24 16:14:02 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) {
|
|
|
|
|
if (bypass_randomness(buf, buflen)) {
|
|
|
|
|
return buflen;
|
|
|
|
|
} else {
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2020-11-15 21:57:10 +01:00
|
|
|
return real_getrandom(buf, buflen, flags);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-16 21:27:08 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
int macos_getentropy(void *buffer, size_t length) {
|
|
|
|
|
#else
|
2021-02-24 16:14:02 -05:00
|
|
|
int getentropy(void *buffer, size_t length) {
|
2022-02-16 21:27:08 +01:00
|
|
|
#endif
|
2021-02-24 16:14:02 -05:00
|
|
|
if (bypass_randomness(buffer, length)) {
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2022-02-16 21:27:08 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
return getentropy(buffer, length);
|
|
|
|
|
#else
|
2021-02-24 16:14:02 -05:00
|
|
|
return real_getentropy(buffer, length);
|
2022-02-16 21:27:08 +01:00
|
|
|
#endif
|
2021-02-24 16:14:02 -05:00
|
|
|
}
|
|
|
|
|
}
|
2020-11-15 21:57:10 +01:00
|
|
|
#endif
|
|
|
|
|
|
2021-02-23 21:42:42 -05:00
|
|
|
#ifdef FAKE_PID
|
2022-02-16 21:27:08 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
pid_t macos_getpid() {
|
|
|
|
|
#else
|
2021-02-23 21:42:42 -05:00
|
|
|
pid_t getpid() {
|
2022-02-16 21:27:08 +01:00
|
|
|
#endif
|
2021-02-23 21:42:42 -05:00
|
|
|
const char *pidstring = getenv("FAKETIME_FAKEPID");
|
|
|
|
|
if (pidstring != NULL) {
|
|
|
|
|
long int pid = strtol(pidstring, NULL, 0);
|
|
|
|
|
return (pid_t)(pid);
|
|
|
|
|
} else {
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2021-02-23 21:42:42 -05:00
|
|
|
return real_getpid();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-02-24 12:36:38 -05:00
|
|
|
#ifdef INTERCEPT_SYSCALL
|
2025-03-26 21:02:47 +00:00
|
|
|
#ifdef INTERCEPT_FUTEX
|
2025-12-23 01:26:35 +00:00
|
|
|
static inline long make_futex_syscall(long number, uint32_t* uaddr, int futex_op, uint32_t val, struct timespec* timeout, uint32_t* uaddr2, uint32_t val3) {
|
2025-03-26 21:02:47 +00:00
|
|
|
if (timeout == NULL) {
|
|
|
|
|
// not timeout related, just call the real syscall
|
2025-12-23 01:26:35 +00:00
|
|
|
return real_syscall(number, uaddr, futex_op, val, timeout, uaddr2, val3);
|
|
|
|
|
}
|
|
|
|
|
if (timeout->tv_sec < 0) {
|
2025-12-23 02:48:55 +00:00
|
|
|
// fprintf(stderr, "libfaketime: invalid timeout.tv_sec < 0\n");
|
2025-12-23 01:26:35 +00:00
|
|
|
timeout->tv_sec = 0;
|
|
|
|
|
}
|
|
|
|
|
if (timeout->tv_nsec < 0) {
|
2025-12-23 02:48:55 +00:00
|
|
|
// fprintf(stderr, "libfaketime: invalid timeout.tv_nsec < 0\n");
|
2025-12-23 01:26:35 +00:00
|
|
|
timeout->tv_nsec = 0;
|
|
|
|
|
}
|
|
|
|
|
return real_syscall(number, uaddr, futex_op, val, timeout, uaddr2, val3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline long handle_futex_syscall(long number, uint32_t* uaddr, int futex_op, uint32_t val, struct timespec* timeout, uint32_t* uaddr2, uint32_t val3) {
|
|
|
|
|
if (timeout == NULL) {
|
2025-03-26 21:02:47 +00:00
|
|
|
goto futex_fallback;
|
|
|
|
|
}
|
2025-05-25 10:00:14 +02:00
|
|
|
|
2025-12-22 12:23:01 +00:00
|
|
|
int futex_cmd = futex_op & FUTEX_CMD_MASK;
|
|
|
|
|
clockid_t clk_id = CLOCK_MONOTONIC;
|
|
|
|
|
if (futex_op & FUTEX_CLOCK_REALTIME)
|
|
|
|
|
clk_id = CLOCK_REALTIME;
|
2025-03-26 21:02:47 +00:00
|
|
|
|
2025-12-22 12:23:01 +00:00
|
|
|
if (futex_cmd == FUTEX_WAIT_BITSET) {
|
|
|
|
|
// FUTEX_WAIT_BITSET uses absolute timeout
|
2025-03-26 21:02:47 +00:00
|
|
|
struct timespec real_tp, fake_tp;
|
2025-05-25 10:00:14 +02:00
|
|
|
|
2025-03-26 21:02:47 +00:00
|
|
|
DONT_FAKE_TIME((*real_clock_gettime)(clk_id, &real_tp));
|
|
|
|
|
fake_tp = real_tp;
|
|
|
|
|
if (fake_clock_gettime(clk_id, &fake_tp) == -1) {
|
|
|
|
|
goto futex_fallback;
|
|
|
|
|
}
|
2025-05-25 10:00:14 +02:00
|
|
|
// Create a corrected timeout by adjusting with the difference between
|
2025-03-26 21:02:47 +00:00
|
|
|
// real and fake timestamps
|
|
|
|
|
struct timespec adjusted_timeout, time_diff;
|
|
|
|
|
timespecsub(&fake_tp, &real_tp, &time_diff);
|
|
|
|
|
timespecsub(timeout, &time_diff, &adjusted_timeout);
|
2025-12-23 01:26:35 +00:00
|
|
|
long result = make_futex_syscall(number, uaddr, futex_op, val, &adjusted_timeout, uaddr2, val3);
|
2025-03-26 21:02:47 +00:00
|
|
|
if (result != 0) {
|
2025-05-25 10:00:14 +02:00
|
|
|
return result;
|
2025-03-26 21:02:47 +00:00
|
|
|
}
|
2025-05-25 10:00:14 +02:00
|
|
|
|
2025-03-26 21:02:47 +00:00
|
|
|
// Check if the futex timeout has already passed according to fake time
|
|
|
|
|
struct timespec now_fake;
|
|
|
|
|
if (fake_clock_gettime(clk_id, &now_fake) != 0) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2025-05-25 10:00:14 +02:00
|
|
|
|
2025-03-26 21:02:47 +00:00
|
|
|
// If the timeout is already passed in fake time, return 0.
|
|
|
|
|
while (!timespeccmp(&now_fake, timeout, >=)) {
|
|
|
|
|
// Calculate how much real time we need to wait
|
|
|
|
|
struct timespec real_now, fake_now, wait_time;
|
|
|
|
|
DONT_FAKE_TIME((*real_clock_gettime)(clk_id, &real_now));
|
|
|
|
|
fake_clock_gettime(clk_id, &fake_now);
|
|
|
|
|
|
|
|
|
|
// Calculate how much fake time is left until the timeout
|
|
|
|
|
struct timespec fake_time_left;
|
|
|
|
|
timespecsub(timeout, &fake_now, &fake_time_left);
|
|
|
|
|
|
|
|
|
|
// Scale the fake time left by the user rate if set
|
|
|
|
|
if (user_rate_set && !dont_fake) {
|
|
|
|
|
timespecmul(&fake_time_left, 1.0 / user_rate, &wait_time);
|
|
|
|
|
} else {
|
|
|
|
|
wait_time = fake_time_left;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate the real timeout by adding the wait time to the current real time
|
|
|
|
|
struct timespec real_timeout;
|
|
|
|
|
timespecadd(&real_now, &wait_time, &real_timeout);
|
|
|
|
|
|
|
|
|
|
// Call the real syscall with the recalculated timeout
|
2025-12-23 01:26:35 +00:00
|
|
|
result = make_futex_syscall(number, uaddr, futex_op, val, &real_timeout, uaddr2, val3);
|
2025-03-26 21:02:47 +00:00
|
|
|
if (result != 0) {
|
2025-05-25 10:00:14 +02:00
|
|
|
return result;
|
2025-03-26 21:02:47 +00:00
|
|
|
}
|
2025-05-25 10:00:14 +02:00
|
|
|
|
2025-03-26 21:02:47 +00:00
|
|
|
// Check if the futex timeout has already passed according to fake time
|
|
|
|
|
if (fake_clock_gettime(clk_id, &now_fake) != 0) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2025-12-22 12:23:01 +00:00
|
|
|
} else if (futex_cmd == FUTEX_WAIT) {
|
|
|
|
|
// FUTEX_WAIT uses relative timeout - scale by time rate
|
|
|
|
|
struct timespec adjusted_timeout;
|
|
|
|
|
|
|
|
|
|
if (user_rate_set && !dont_fake && ((clk_id == CLOCK_REALTIME) || (clk_id == CLOCK_MONOTONIC))) {
|
|
|
|
|
timespecmul(timeout, 1.0 / user_rate, &adjusted_timeout);
|
|
|
|
|
} else {
|
|
|
|
|
adjusted_timeout = *timeout;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 01:26:35 +00:00
|
|
|
return make_futex_syscall(number, uaddr, futex_op, val, &adjusted_timeout, uaddr2, val3);
|
2025-03-26 21:02:47 +00:00
|
|
|
} else {
|
2025-12-22 12:23:01 +00:00
|
|
|
// Other futex operations - pass through unchanged
|
|
|
|
|
goto futex_fallback;
|
2025-03-26 21:02:47 +00:00
|
|
|
}
|
2025-05-25 10:00:14 +02:00
|
|
|
|
2025-03-26 21:02:47 +00:00
|
|
|
futex_fallback:
|
2025-12-23 01:26:35 +00:00
|
|
|
return make_futex_syscall(number, uaddr, futex_op, val, timeout, uaddr2, val3);
|
2025-03-26 21:02:47 +00:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-02-24 12:36:38 -05:00
|
|
|
/* see https://github.com/wolfcw/libfaketime/issues/301 */
|
|
|
|
|
long syscall(long number, ...) {
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, number);
|
|
|
|
|
#ifdef FAKE_RANDOM
|
|
|
|
|
if (number == __NR_getrandom && getenv("FAKERANDOM_SEED")) {
|
|
|
|
|
void *buf;
|
|
|
|
|
size_t buflen;
|
|
|
|
|
unsigned int flags;
|
|
|
|
|
buf = va_arg(ap, void*);
|
|
|
|
|
buflen = va_arg(ap, size_t);
|
|
|
|
|
flags = va_arg(ap, unsigned int);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
return getrandom(buf, buflen, flags);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-02-25 21:22:41 +01:00
|
|
|
// static int (*real_clock_gettime) (clockid_t clk_id, struct timespec *tp);
|
2025-11-08 12:40:49 +01:00
|
|
|
/* Intercept clock_gettime syscall if available */
|
|
|
|
|
#ifdef __NR_clock_gettime
|
2024-01-02 15:09:12 +01:00
|
|
|
if (number == __NR_clock_gettime && (getenv("FAKETIME") || getenv("FAKETIME_TIMESTAMP_FILE"))) {
|
2021-02-25 21:22:41 +01:00
|
|
|
clockid_t clk_id;
|
|
|
|
|
struct timespec *tp;
|
|
|
|
|
clk_id = va_arg(ap, clockid_t);
|
|
|
|
|
tp = va_arg(ap, struct timespec*);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
return clock_gettime(clk_id, tp);
|
|
|
|
|
}
|
2025-11-08 12:40:49 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef FAKE_SLEEP
|
|
|
|
|
#ifdef __NR_clock_nanosleep
|
|
|
|
|
/* Intercept raw clock_nanosleep syscall */
|
|
|
|
|
if (number == __NR_clock_nanosleep && (getenv("FAKETIME") || getenv("FAKETIME_TIMESTAMP_FILE")))
|
|
|
|
|
{
|
|
|
|
|
clockid_t clk_id;
|
|
|
|
|
int flags;
|
|
|
|
|
const struct timespec *req;
|
|
|
|
|
struct timespec *rem;
|
|
|
|
|
|
|
|
|
|
clk_id = va_arg(ap, clockid_t);
|
|
|
|
|
flags = va_arg(ap, int);
|
|
|
|
|
req = va_arg(ap, const struct timespec*);
|
|
|
|
|
rem = va_arg(ap, struct timespec*);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
|
|
if (req == NULL)
|
|
|
|
|
{
|
|
|
|
|
/* Pass through invalid input to maintain behavior */
|
|
|
|
|
return real_syscall(number, clk_id, flags, req, rem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct timespec real_req;
|
|
|
|
|
|
|
|
|
|
if (flags & TIMER_ABSTIME)
|
|
|
|
|
{
|
|
|
|
|
/* Sleep until absolute fake time 'req': convert to corresponding real abstime */
|
|
|
|
|
struct timespec tdiff, timeadj;
|
|
|
|
|
/* time difference between target fake abstime and fake base */
|
|
|
|
|
timespecsub(req, &user_faked_time_timespec, &timeadj);
|
|
|
|
|
if (user_rate_set) {
|
|
|
|
|
timespecmul(&timeadj, 1.0 / user_rate, &tdiff);
|
|
|
|
|
} else {
|
|
|
|
|
tdiff = timeadj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (clk_id == CLOCK_REALTIME)
|
|
|
|
|
{
|
|
|
|
|
timespecadd(&ftpl_starttime.real, &tdiff, &real_req);
|
|
|
|
|
} else if (clk_id == CLOCK_MONOTONIC)
|
|
|
|
|
{
|
|
|
|
|
get_fake_monotonic_setting(&fake_monotonic_clock);
|
|
|
|
|
if (fake_monotonic_clock) {
|
|
|
|
|
timespecadd(&ftpl_starttime.mon, &tdiff, &real_req);
|
|
|
|
|
} else {
|
|
|
|
|
/* leave untouched if monotonic faking disabled */
|
|
|
|
|
real_req = *req;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* other clocks: leave untouched */
|
|
|
|
|
real_req = *req;
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
/* Relative sleep: scale by 1/rate for realtime/monotonic when faking */
|
|
|
|
|
if (user_rate_set && !dont_fake && ((clk_id == CLOCK_REALTIME) || (clk_id == CLOCK_MONOTONIC)))
|
|
|
|
|
{
|
|
|
|
|
timespecmul(req, 1.0 / user_rate, &real_req);
|
|
|
|
|
} else {
|
|
|
|
|
real_req = *req;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long rc = real_syscall(number, clk_id, flags, &real_req, rem);
|
|
|
|
|
if (rc != 0)
|
|
|
|
|
{
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
if (rem != NULL && (rem->tv_sec != 0 || rem->tv_nsec != 0))
|
|
|
|
|
{
|
|
|
|
|
if (user_rate_set && !dont_fake)
|
|
|
|
|
{
|
|
|
|
|
timespecmul(rem, user_rate, rem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
#endif /* __NR_clock_nanosleep */
|
|
|
|
|
#endif /* FAKE_SLEEP */
|
2025-05-25 10:00:14 +02:00
|
|
|
|
2025-03-26 21:02:47 +00:00
|
|
|
#ifdef INTERCEPT_FUTEX
|
|
|
|
|
if (number == __NR_futex) {
|
|
|
|
|
uint32_t *uaddr;
|
|
|
|
|
int futex_op;
|
|
|
|
|
uint32_t val;
|
|
|
|
|
struct timespec *timeout; /* or: uint32_t val2 */
|
|
|
|
|
uint32_t* uaddr2;
|
|
|
|
|
uint32_t val3;
|
|
|
|
|
|
|
|
|
|
uaddr = va_arg(ap, uint32_t*);
|
|
|
|
|
futex_op = va_arg(ap, int);
|
|
|
|
|
val = va_arg(ap, uint32_t);
|
|
|
|
|
timeout = va_arg(ap, struct timespec*);
|
|
|
|
|
uaddr2 = va_arg(ap, uint32_t*);
|
|
|
|
|
val3 = va_arg(ap, uint32_t);
|
2025-05-25 10:00:14 +02:00
|
|
|
va_end(ap);
|
2025-03-26 21:02:47 +00:00
|
|
|
|
|
|
|
|
return handle_futex_syscall(number, uaddr, futex_op, val, timeout, uaddr2, val3);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-02-25 21:22:41 +01:00
|
|
|
|
2021-03-05 21:28:53 -05:00
|
|
|
variadic_promotion_t a[syscall_max_args];
|
2021-02-24 12:36:38 -05:00
|
|
|
for (int i = 0; i < syscall_max_args; i++)
|
2021-03-05 21:28:53 -05:00
|
|
|
a[i] = va_arg(ap, variadic_promotion_t);
|
2021-02-24 12:36:38 -05:00
|
|
|
va_end(ap);
|
2023-01-16 21:25:56 -05:00
|
|
|
ftpl_init();
|
2021-02-24 12:36:38 -05:00
|
|
|
return real_syscall(number, a[0], a[1], a[2], a[3], a[4], a[5]);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-02-06 21:39:29 +01:00
|
|
|
#ifdef MACOS_DYLD_INTERPOSE
|
|
|
|
|
void do_macos_dyld_interpose(void) {
|
|
|
|
|
DYLD_INTERPOSE(macos_clock_gettime, clock_gettime);
|
|
|
|
|
DYLD_INTERPOSE(macos_gettimeofday, gettimeofday);
|
|
|
|
|
DYLD_INTERPOSE(macos_time, time);
|
|
|
|
|
DYLD_INTERPOSE(macos_ftime, ftime);
|
2022-02-16 21:27:08 +01:00
|
|
|
#ifdef FAKE_SLEEP
|
|
|
|
|
DYLD_INTERPOSE(macos_alarm, alarm);
|
2022-02-06 21:39:29 +01:00
|
|
|
DYLD_INTERPOSE(macos_sleep, sleep);
|
|
|
|
|
DYLD_INTERPOSE(macos_usleep, usleep);
|
|
|
|
|
DYLD_INTERPOSE(macos_nanosleep, nanosleep);
|
|
|
|
|
DYLD_INTERPOSE(macos_poll, poll);
|
2022-02-16 21:27:08 +01:00
|
|
|
#endif
|
2022-12-20 00:35:53 +08:00
|
|
|
#ifdef TIME_UTC
|
2022-02-06 21:39:29 +01:00
|
|
|
DYLD_INTERPOSE(macos_timespec_get, timespec_get);
|
2022-12-20 00:35:53 +08:00
|
|
|
#endif
|
2022-02-16 21:27:08 +01:00
|
|
|
DYLD_INTERPOSE(macos_select, select);
|
|
|
|
|
#ifdef FAKE_RANDOM
|
|
|
|
|
DYLD_INTERPOSE(macos_getentropy, getentropy);
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef FAKE_SETTIME
|
|
|
|
|
DYLD_INTERPOSE(macos_clock_settime, clock_settime);
|
|
|
|
|
DYLD_INTERPOSE(macos_settimeofday, settimeofday);
|
|
|
|
|
DYLD_INTERPOSE(macos_adjtime, adjtime);
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef FAKE_PID
|
|
|
|
|
DYLD_INTERPOSE(macos_getpid, getpid);
|
|
|
|
|
#endif
|
2022-02-20 17:39:17 +01:00
|
|
|
#ifdef FAKE_STAT
|
|
|
|
|
DYLD_INTERPOSE(macos_stat, stat);
|
|
|
|
|
// DYLD_INTERPOSE(macos_fstat, fstat);
|
|
|
|
|
DYLD_INTERPOSE(macos_lstat, lstat);
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef FAKE_FILE_TIMESTAMPS
|
|
|
|
|
DYLD_INTERPOSE(macos_utime, utime);
|
|
|
|
|
DYLD_INTERPOSE(macos_utimes, utimes);
|
|
|
|
|
DYLD_INTERPOSE(macos_utimensat, utimensat);
|
|
|
|
|
DYLD_INTERPOSE(macos_futimens, futimens);
|
|
|
|
|
#endif
|
2022-02-06 21:39:29 +01:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-09-04 12:50:00 +02:00
|
|
|
/*
|
|
|
|
|
* Editor modelines
|
|
|
|
|
*
|
|
|
|
|
* Local variables:
|
|
|
|
|
* c-basic-offset: 2
|
|
|
|
|
* tab-width: 2
|
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
|
* End:
|
|
|
|
|
*
|
|
|
|
|
* vi: set shiftwidth=2 tabstop=2 expandtab:
|
|
|
|
|
* :indentSize=2:tabSize=2:noTabs=true:
|
|
|
|
|
*/
|
|
|
|
|
|
2013-08-19 11:47:49 +02:00
|
|
|
/* eof */
|