run-locking.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <stdlib.h>
  2. #include <setjmp.h>
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <assert.h>
  6. #include <ccan/tap/tap.h>
  7. /* Include the C files directly. */
  8. #include <ccan/failtest/failtest.c>
  9. #define SIZE 8
  10. /* We don't want to fork and fail; we're just testing lock recording. */
  11. static enum failtest_result dont_fail(struct failtest_call *history,
  12. unsigned num)
  13. {
  14. return FAIL_DONT_FAIL;
  15. }
  16. static bool place_lock(int fd, char lockarr[], unsigned pos, unsigned size,
  17. int type)
  18. {
  19. struct flock fl;
  20. /* Update record keeping. */
  21. if (type == F_RDLCK)
  22. memset(lockarr+pos, 1, size);
  23. else if (type == F_WRLCK)
  24. memset(lockarr+pos, 2, size);
  25. else
  26. memset(lockarr+pos, 0, size);
  27. fl.l_whence = SEEK_SET;
  28. fl.l_type = type;
  29. fl.l_start = pos;
  30. fl.l_len = size;
  31. return failtest_fcntl(fd, "run-locking.c", 1, F_SETLK, &fl) == 0;
  32. }
  33. static char lock_lookup(int fd, unsigned pos)
  34. {
  35. char ret = 0;
  36. unsigned int i;
  37. struct lock_info *l;
  38. for (i = 0; i < lock_num; i++) {
  39. l = &locks[i];
  40. if (l->fd != fd)
  41. continue;
  42. if (pos >= l->start && pos <= l->end) {
  43. if (ret)
  44. ret = 3;
  45. else if (l->type == F_RDLCK)
  46. ret = 1;
  47. else
  48. ret = 2;
  49. }
  50. }
  51. return ret;
  52. }
  53. static bool test(int fd,
  54. unsigned p1, unsigned s1,
  55. unsigned p2, unsigned s2,
  56. unsigned p3, unsigned s3)
  57. {
  58. unsigned int i;
  59. char lockarr[SIZE];
  60. memset(lockarr, 0, sizeof(lockarr));
  61. if (!place_lock(fd, lockarr, p1, s1, F_WRLCK))
  62. return false;
  63. if (!place_lock(fd, lockarr, p2, s2, F_RDLCK))
  64. return false;
  65. if (!place_lock(fd, lockarr, p3, s3, F_UNLCK))
  66. return false;
  67. for (i = 0; i < SIZE; i++) {
  68. if (lock_lookup(fd, i) != lockarr[i])
  69. return false;
  70. }
  71. /* Reset lock info. */
  72. lock_num = 0;
  73. return true;
  74. }
  75. int main(void)
  76. {
  77. int fd;
  78. long flags;
  79. unsigned int isize;
  80. plan_tests(5835);
  81. failtest_hook = dont_fail;
  82. fd = open("run-locking-scratch", O_RDWR|O_CREAT, 0600);
  83. /* GETFL and SETFL wrappers should pass through. */
  84. flags = fcntl(fd, F_GETFL);
  85. ok1(failtest_fcntl(fd, "run-locking.c", 1, F_GETFL) == flags);
  86. flags |= O_NONBLOCK;
  87. ok1(failtest_fcntl(fd, "run-locking.c", 1, F_SETFL, flags) == 0);
  88. ok1(failtest_fcntl(fd, "run-locking.c", 1, F_GETFL) == flags);
  89. for (isize = 1; isize < 4; isize++) {
  90. unsigned int ipos;
  91. for (ipos = 0; ipos + isize < SIZE; ipos++) {
  92. unsigned int jsize;
  93. for (jsize = 1; jsize < 4; jsize++) {
  94. unsigned int jpos;
  95. for (jpos = 0; jpos + jsize < SIZE; jpos++) {
  96. unsigned int ksize;
  97. for (ksize = 1; ksize < 4; ksize++) {
  98. unsigned int kpos;
  99. for (kpos = 0;
  100. kpos + ksize < SIZE;
  101. kpos++) {
  102. ok1(test(fd,
  103. ipos, isize,
  104. jpos, jsize,
  105. kpos, ksize));
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. return exit_status();
  113. }