diff --git a/tests/common/rmdir_failure.c b/tests/common/rmdir_failure.c new file mode 100644 index 00000000..9d775b10 --- /dev/null +++ b/tests/common/rmdir_failure.c @@ -0,0 +1,51 @@ +/* + * gcc rmdir_failure.c -o rmdir_failure.so -shared -ldl + * LD_PRELOAD=./rmdir_failure.so FAILURE_PATH=/etc/shadow ./test /etc/shadow + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + + +typedef int (*rmdir_type) (const char *path); +static rmdir_type next_rmdir; + +static const char *failure_path = NULL; + +int rmdir (const char *path) +{ + if (NULL == next_rmdir) + { + next_rmdir = dlsym (RTLD_NEXT, "rmdir"); + assert (NULL != next_rmdir); + } + 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, "rmdir FAILURE %s\n", path); + errno = EBUSY; + return -1; + } + + return next_rmdir (path); +} + diff --git a/tests/common/unlink_failure.c b/tests/common/unlink_failure.c new file mode 100644 index 00000000..2281c8af --- /dev/null +++ b/tests/common/unlink_failure.c @@ -0,0 +1,51 @@ +/* + * gcc unlink_failure.c -o unlink_failure.so -shared -ldl + * LD_PRELOAD=./unlink_failure.so FAILURE_PATH=/etc/shadow ./test /etc/shadow + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + + +typedef int (*unlink_type) (const char *path); +static unlink_type next_unlink; + +static const char *failure_path = NULL; + +int unlink (const char *path) +{ + if (NULL == next_unlink) + { + next_unlink = dlsym (RTLD_NEXT, "unlink"); + assert (NULL != next_unlink); + } + 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, "unlink FAILURE %s\n", path); + errno = EBUSY; + return -1; + } + + return next_unlink (path); +} +