Merge pull request #298 from dkg/fakepid

Enable intercepting getpid()
This commit is contained in:
Wolfgang Hommel
2021-02-24 13:53:45 +01:00
committed by GitHub
3 changed files with 45 additions and 0 deletions

View File

@@ -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

View File

@@ -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__
@@ -3693,6 +3700,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
View 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