diff --git a/.gitignore b/.gitignore index fa63451..afee928 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,10 @@ *.so.1 timetest test/getrandom_test -test/librandom.o -test/librandom.so +test/lib*.o +test/lib*.so test/use_lib_random +test/use_lib_getpid src/libfaketime.dylib.1 src/libfaketime.1.dylib diff --git a/test/Makefile b/test/Makefile index 2a8ad9c..8f56489 100644 --- a/test/Makefile +++ b/test/Makefile @@ -22,23 +22,26 @@ test: timetest functest functest: ./testframe.sh functests -getrandom_test: getrandom_test.c +%_test: %_test.c ${CC} -o $@ ${CFLAGS} $< -randomtest: getrandom_test use_lib_random +randomtest: getrandom_test use_lib_random librandom.so ./randomtest.sh -librandom.o: librandom.c +getpidtest: use_lib_getpid libgetpid.so + ./pidtest.sh + +lib%.o: lib%.c ${CC} -c -o $@ -fpic ${CFLAGS} $< -librandom.so: librandom.o +lib%.so: lib%.o ${CC} -o $@ -shared ${CFLAGS} $< -use_lib_random: use_lib_random.c librandom.so - ${CC} -L. -o $@ ${CFLAGS} $< -lrandom +use_lib_%: use_lib_%.c lib%.so + ${CC} -L. -o $@ ${CFLAGS} $< -l$* clean: - @rm -f ${OBJ} timetest getrandom_test librandom.o librandom.so use_lib_random + @rm -f ${OBJ} timetest getrandom_test lib*.o lib*.so use_lib_random use_lib_getpid distclean: clean @echo diff --git a/test/libgetpid.c b/test/libgetpid.c new file mode 100644 index 0000000..1fb84c5 --- /dev/null +++ b/test/libgetpid.c @@ -0,0 +1,13 @@ +#include +#include +#include + +void getpid_func() { + fprintf(stderr, " called getpid_func()\n"); +} + + +static __attribute__((constructor)) void getpid_init() { + pid_t pid = getpid(); + fprintf(stderr, " getpid() yielded %d\n", pid); +} diff --git a/test/libgetpid.h b/test/libgetpid.h new file mode 100644 index 0000000..e18c61d --- /dev/null +++ b/test/libgetpid.h @@ -0,0 +1,6 @@ +#ifndef __LIBGETPID_H__ +#define __LIBGETPID_H__ + +extern void getpid_func(); + +#endif diff --git a/test/pidtest.sh b/test/pidtest.sh index 617bad8..83e0771 100755 --- a/test/pidtest.sh +++ b/test/pidtest.sh @@ -17,3 +17,10 @@ if [ $output != 13 ]; then printf >&2 'Failed to enforce a rigid response to getpid()\n' exit 2 fi + +printf 'testing shared object with getpid() in library constructor\n' +LD_LIBRARY_PATH=. ./use_lib_getpid +printf 'now with LD_PRELOAD and FAKETIME_FAKEPID\n' +FAKETIME_FAKEPID=25 LD_PRELOAD="$FTPL" LD_LIBRARY_PATH=. ./use_lib_getpid +printf 'now with LD_PRELOAD without FAKETIME_FAKEPID\n' +LD_PRELOAD="$FTPL" LD_LIBRARY_PATH=. ./use_lib_getpid diff --git a/test/use_lib_getpid.c b/test/use_lib_getpid.c new file mode 100644 index 0000000..c9f3deb --- /dev/null +++ b/test/use_lib_getpid.c @@ -0,0 +1,12 @@ +#include +#include +#include +#include "libgetpid.h" + +int main() { + pid_t pid; + getpid_func(); + pid = getpid(); + fprintf(stderr, " getpid() -> %d\n", pid); + return 0; +}