Skip to content
Myr

inject_errors.c

This file

Path

inject_errors.c

112 lines · 2.8 KiB · C

Last changed by

Add error injection libraryNicolas Williamsbefore adoption821d640

History of this file

Take it

12#define _GNU_SOURCE /* for RTLD_NEXT */3#include <assert.h>4#include <dlfcn.h>5#include <errno.h>6#include <stdio.h>7#include <string.h>8#include <unistd.h>910static FILE *fail;11static FILE *fail_read;12static FILE *fail_write;13static FILE *fail_close;14static int error;1516static FILE * (*real_fopen)(const char *, const char *);17static int           (*real_fclose)(FILE *);18static int           (*real_ferror)(FILE *);19static void          (*real_clearerr)(FILE *);20static char *        (*real_fgets)(char *, int, FILE *);21static size_t        (*real_fread)(void *, size_t, size_t, FILE *);22static size_t        (*real_fwrite)(const void *, size_t, size_t, FILE *);2324#define GET_REAL(sym)                         \25  do {                                        \26    if (real_ ## sym == 0) {                  \27      real_ ## sym = dlsym(RTLD_NEXT, #sym);  \28      assert(real_ ## sym != 0);              \29    }                                         \30  } while (0)3132#define dbg_write(msg) (void)write(2, msg, sizeof(msg) - 1)3334#define dbg() \35  do {                                      \36    dbg_write("here: ");                    \37    dbg_write(__func__);                    \38    dbg_write("!\n");                       \39  } while (0)4041FILE *fopen(const char *path, const char *mode) {42  GET_REAL(fopen);43  fail = fail_read = fail_write = fail_close = 0;44  FILE *f = real_fopen(path, mode);45  error = EIO;46  if (strcmp(path, "fail_read") == 0) {47    fail = fail_read = f;48  } else if (strncmp(path, "fail_write", sizeof("fail_write") - 1) == 0) {49    // Not that jq opens files for write anyways...50    fail = fail_write = f;51    if (strcmp(path, "fail_write_enospc") == 0)52      error = ENOSPC;53  } else if (strncmp(path, "fail_close", sizeof("fail_close") - 1) == 0) {54    fail = fail_close = f;55    if (strcmp(path, "fail_close_enospc") == 0)56      error = ENOSPC;57  }58  return f;59}6061int fclose(FILE *f) {62  GET_REAL(fclose);63  int res = real_fclose(f);64  if (fail_close == f) {65    fail = fail_read = fail_write = fail_close = 0;66    return EOF;67  }68  return res;69}7071char * fgets(char *buf, int len, FILE *f) {72  GET_REAL(fgets);73  char *res = real_fgets(buf, len, f);74  if (fail_read == f)75    return 0;76  return res;77}7879size_t fread(void *buf, size_t sz, size_t nemb, FILE *f) {80  GET_REAL(fread);81  size_t res = real_fread(buf, sz, nemb, f);82  if (fail_read == f)83    return 0;84  return res;85}8687size_t fwrite(const void *buf, size_t sz, size_t nemb, FILE *f) {88  GET_REAL(fwrite);89  size_t res = real_fwrite(buf, sz, nemb, f);90  if (fail_write == f)91    return 0;92  return res;93}9495int ferror(FILE *f) {96  GET_REAL(ferror);97  int res = real_ferror(f);98  if (fail == f) {99    errno = error;100    return 1;101  }102  return res;103}104105void clearerr(FILE *f) {106  GET_REAL(clearerr);107  real_clearerr(f);108  if (fail == f) {109    fail = fail_read = fail_write = fail_close = 0;110    error = 0;111  }112}