summaryrefslogtreecommitdiff
path: root/tools/slowdown/slowdown.c
blob: 80a1ff76e75b2f1c8e444bbb9488738a2f2ce603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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);
}