failtest_helper.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "failtest_helper.h"
  2. #include "logging.h"
  3. #include <string.h>
  4. #include "tap-interface.h"
  5. bool failtest_suppress = false;
  6. bool failmatch(const struct failtest_call *call,
  7. const char *file, int line, enum failtest_call_type type)
  8. {
  9. return call->type == type
  10. && call->line == line
  11. && ((strcmp(call->file, file) == 0)
  12. || (strends(call->file, file)
  13. && (call->file[strlen(call->file) - strlen(file) - 1]
  14. == '/')));
  15. }
  16. static bool is_nonblocking_lock(const struct failtest_call *call)
  17. {
  18. return call->type == FAILTEST_FCNTL && call->u.fcntl.cmd == F_SETLK;
  19. }
  20. static bool is_unlock(const struct failtest_call *call)
  21. {
  22. return call->type == FAILTEST_FCNTL
  23. && call->u.fcntl.arg.fl.l_type == F_UNLCK;
  24. }
  25. bool exit_check_log(struct tlist_calls *history)
  26. {
  27. const struct failtest_call *i;
  28. unsigned int malloc_count = 0;
  29. tlist_for_each(history, i, list) {
  30. if (!i->fail)
  31. continue;
  32. /* Failing the /dev/urandom open doesn't count: we fall back. */
  33. if (failmatch(i, URANDOM_OPEN))
  34. continue;
  35. /* Similarly with read fail. */
  36. if (failmatch(i, URANDOM_READ))
  37. continue;
  38. /* Initial allocation of ntdb doesn't log. */
  39. if (i->type == FAILTEST_MALLOC) {
  40. if (malloc_count++ == 0) {
  41. continue;
  42. }
  43. }
  44. /* We don't block "failures" on non-blocking locks. */
  45. if (is_nonblocking_lock(i))
  46. continue;
  47. if (!tap_log_messages)
  48. diag("We didn't log for %s:%u", i->file, i->line);
  49. return tap_log_messages != 0;
  50. }
  51. return true;
  52. }
  53. /* Some places we soldier on despite errors: only fail them once. */
  54. enum failtest_result
  55. block_repeat_failures(struct tlist_calls *history)
  56. {
  57. const struct failtest_call *last;
  58. last = tlist_tail(history, list);
  59. if (failtest_suppress)
  60. return FAIL_DONT_FAIL;
  61. if (failmatch(last, URANDOM_OPEN)
  62. || failmatch(last, URANDOM_READ)) {
  63. return FAIL_PROBE;
  64. }
  65. /* We handle mmap failing, by falling back to read/write, so
  66. * don't try all possible paths. */
  67. if (last->type == FAILTEST_MMAP)
  68. return FAIL_PROBE;
  69. /* Unlock or non-blocking lock is fail-once. */
  70. if (is_unlock(last) || is_nonblocking_lock(last))
  71. return FAIL_PROBE;
  72. return FAIL_OK;
  73. }