summaryrefslogtreecommitdiffstats
path: root/tests/common/rename_failure.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-06 02:22:06 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-06 02:22:06 +0000
commit741c1ef7a4f2ac316ad6e557ddbe03023413478d (patch)
tree38890f681daa26c57e865b4feca10d0ca53e1046 /tests/common/rename_failure.c
parentInitial commit. (diff)
downloadshadow-upstream.tar.xz
shadow-upstream.zip
Adding upstream version 1:4.5.upstream/1%4.5upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--tests/common/rename_failure.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/common/rename_failure.c b/tests/common/rename_failure.c
new file mode 100644
index 0000000..dd02fe5
--- /dev/null
+++ b/tests/common/rename_failure.c
@@ -0,0 +1,50 @@
+/*
+ * gcc rename_failure.c -o rename_failure.so -shared -ldl
+ * LD_PRELOAD=./rename_failure.so FAILURE_PATH=/etc/shadow ./test /etc/shadow
+ */
+
+#define _GNU_SOURCE
+#include <dlfcn.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdarg.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+
+typedef int (*rename_type) (const char *old, const char *new);
+static rename_type next_rename;
+
+static const char *failure_path = NULL;
+
+int rename (const char *old, const char *new)
+{
+ if (NULL == next_rename)
+ {
+ next_rename = dlsym (RTLD_NEXT, "rename");
+ assert (NULL != next_rename);
+ }
+ if (NULL == failure_path) {
+ failure_path = getenv ("FAILURE_PATH");
+ if (NULL == failure_path) {
+ fputs ("No FAILURE_PATH defined\n", stderr);
+ }
+ }
+
+ if ( (NULL != new)
+ && (NULL != failure_path)
+ && (strcmp (new, failure_path) == 0))
+ {
+ fprintf (stderr, "rename FAILURE %s %s\n", old, new);
+ errno = EIO;
+ return -1;
+ }
+
+ return next_rename (old, new);
+}
+