mirror of
https://github.com/wolfcw/libfaketime.git
synced 2026-05-17 00:26:16 +03:00
Enable intercepting getpid()
I went with the runtime environment variable being FAKETIME_FAKEPID since it seems less likely to collide with anything else. Closes: #297
This commit is contained in:
@@ -41,6 +41,9 @@
|
||||
# FAKE_RANDOM
|
||||
# - Intercept getrandom()
|
||||
#
|
||||
# FAKE_PID
|
||||
# - Intercept getpid()
|
||||
#
|
||||
# FORCE_MONOTONIC_FIX
|
||||
# - If the test program hangs forever on
|
||||
# " pthread_cond_timedwait: CLOCK_MONOTONIC test
|
||||
|
||||
@@ -226,6 +226,9 @@ static int (*real_futimens) (int fd, const struct timespec times
|
||||
#ifdef FAKE_RANDOM
|
||||
static ssize_t (*real_getrandom) (void *buf, size_t buflen, unsigned int flags);
|
||||
#endif
|
||||
#ifdef FAKE_PID
|
||||
static pid_t (*real_getpid) ();
|
||||
#endif
|
||||
|
||||
static int initialized = 0;
|
||||
|
||||
@@ -2453,6 +2456,10 @@ static void ftpl_init(void)
|
||||
real_getrandom = dlsym(RTLD_NEXT, "getrandom");
|
||||
#endif
|
||||
|
||||
#ifdef FAKE_PID
|
||||
real_getpid = dlsym(RTLD_NEXT, "getpid");
|
||||
#endif
|
||||
|
||||
#ifdef FAKE_PTHREAD
|
||||
|
||||
#ifdef __GLIBC__
|
||||
@@ -3689,6 +3696,22 @@ ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef FAKE_PID
|
||||
pid_t getpid() {
|
||||
const char *pidstring = getenv("FAKETIME_FAKEPID");
|
||||
if (pidstring != NULL) {
|
||||
long int pid = strtol(pidstring, NULL, 0);
|
||||
return (pid_t)(pid);
|
||||
} else {
|
||||
if (!initialized)
|
||||
{
|
||||
ftpl_init();
|
||||
}
|
||||
return real_getpid();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Editor modelines
|
||||
*
|
||||
|
||||
19
test/pidtest.sh
Executable file
19
test/pidtest.sh
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/bin/sh
|
||||
|
||||
FTPL="${FAKETIME_TESTLIB:-../src/libfaketime.so.1}"
|
||||
|
||||
set -e
|
||||
run1=$(LD_PRELOAD="$FTPL" sh -c 'echo $$')
|
||||
run2=$(LD_PRELOAD="$FTPL" sh -c 'echo $$')
|
||||
|
||||
if [ $run1 = $run2 ]; then
|
||||
printf >&2 'got the same pid twice in a row without setting FAKETIME_FAKEPID\n'
|
||||
exit 1
|
||||
fi
|
||||
|
||||
output=$(FAKETIME_FAKEPID=13 LD_PRELOAD="$FTPL" sh -c 'echo $$')
|
||||
|
||||
if [ $output != 13 ]; then
|
||||
printf >&2 'Failed to enforce a rigid response to getpid()\n'
|
||||
exit 2
|
||||
fi
|
||||
Reference in New Issue
Block a user