From 741c1ef7a4f2ac316ad6e557ddbe03023413478d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 6 May 2024 04:22:06 +0200 Subject: Adding upstream version 1:4.5. Signed-off-by: Daniel Baumann --- tests/common/fopen_failure.c | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 tests/common/fopen_failure.c (limited to 'tests/common/fopen_failure.c') diff --git a/tests/common/fopen_failure.c b/tests/common/fopen_failure.c new file mode 100644 index 0000000..750cd66 --- /dev/null +++ b/tests/common/fopen_failure.c @@ -0,0 +1,46 @@ +/* + * gcc fopen_failure.c -o fopen_failure.so -shared -ldl + * LD_PRELOAD=./fopen_failure.so FAILURE_PATH=/etc/shadow ./test /etc/shadow + */ + +#define _GNU_SOURCE +#include +#include +#include + +#include +#include + + +typedef FILE * (*fopen_type) (const char *path, const char *mode); +static fopen_type next_fopen; + +static const char *failure_path = NULL; + +FILE *fopen64 (const char *path, const char *mode) +{ +printf ("fopen64(%s, %s)\n", path, mode); + if (NULL == next_fopen) + { + next_fopen = dlsym (RTLD_NEXT, "fopen64"); + assert (NULL != next_fopen); + } + if (NULL == failure_path) { + failure_path = getenv ("FAILURE_PATH"); + if (NULL == failure_path) { + fputs ("No FAILURE_PATH defined\n", stderr); + } + } + + if ( (NULL != path) + && (NULL != failure_path) + && (strcmp (path, failure_path) == 0)) + { + fprintf (stderr, "fopen64 FAILURE %s %s ...\n", path, mode); + errno = EIO; + return NULL; + } + + return next_fopen (path, mode); +} + -- cgit v1.2.3