summaryrefslogtreecommitdiff
path: root/tools/slowdown/slowdown.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/slowdown/slowdown.c')
-rw-r--r--tools/slowdown/slowdown.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/tools/slowdown/slowdown.c b/tools/slowdown/slowdown.c
new file mode 100644
index 0000000..80a1ff7
--- /dev/null
+++ b/tools/slowdown/slowdown.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2014, NORDUnet A/S.
+ * See COPYING for licensing information.
+ */
+
+/* slowdown -- a shared library for introducing delay in read(2) and
+ write(2) calls to simulate a slow disk */
+
+
+#define _GNU_SOURCE 1 /* For RTLD_NEXT. */
+
+#include <unistd.h>
+#include <dlfcn.h>
+#include <stdlib.h>
+
+static
+sleepsome (const char *envname)
+{
+ const char *delay_str = secure_getenv(envname);
+ useconds_t delay = 0;
+
+ if (delay_str != NULL)
+ delay = atoi(delay_str);
+ usleep (1000 * delay);
+}
+
+ssize_t
+read (int fd, void *buf, size_t count)
+{
+ ssize_t (*real_read)(int, void*, size_t) = dlsym(RTLD_NEXT, "read");
+ sleepsome ("SLOWDOWN_READ_DELAY_MS");
+ return real_read (fd, buf, count);
+}
+
+ssize_t
+write (int fd, const void *buf, size_t count)
+{
+ ssize_t (*real_write)(int, const void*, size_t) = dlsym(RTLD_NEXT, "write");
+ sleepsome ("SLOWDOWN_WRITE_DELAY_MS");
+ return real_write (fd, buf, count);
+}