api-locktimeout.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "config.h"
  2. #include "../ntdb.h"
  3. #include "../private.h"
  4. #include "tap-interface.h"
  5. #include <limits.h>
  6. #include "logging.h"
  7. #include "external-agent.h"
  8. #include "helpapi-external-agent.h"
  9. #undef alarm
  10. #define alarm fast_alarm
  11. /* Speed things up by doing things in milliseconds. */
  12. static unsigned int fast_alarm(unsigned int milli_seconds)
  13. {
  14. struct itimerval it;
  15. it.it_interval.tv_sec = it.it_interval.tv_usec = 0;
  16. it.it_value.tv_sec = milli_seconds / 1000;
  17. it.it_value.tv_usec = milli_seconds * 1000;
  18. setitimer(ITIMER_REAL, &it, NULL);
  19. return 0;
  20. }
  21. #define CatchSignal(sig, handler) signal((sig), (handler))
  22. static void do_nothing(int signum)
  23. {
  24. }
  25. /* This example code is taken from SAMBA, so try not to change it. */
  26. static struct flock flock_struct;
  27. /* Return a value which is none of v1, v2 or v3. */
  28. static inline short int invalid_value(short int v1, short int v2, short int v3)
  29. {
  30. short int try = (v1+v2+v3)^((v1+v2+v3) << 16);
  31. while (try == v1 || try == v2 || try == v3)
  32. try++;
  33. return try;
  34. }
  35. /* We invalidate in as many ways as we can, so the OS rejects it */
  36. static void invalidate_flock_struct(int signum)
  37. {
  38. flock_struct.l_type = invalid_value(F_RDLCK, F_WRLCK, F_UNLCK);
  39. flock_struct.l_whence = invalid_value(SEEK_SET, SEEK_CUR, SEEK_END);
  40. flock_struct.l_start = -1;
  41. /* A large negative. */
  42. flock_struct.l_len = (((off_t)1 << (sizeof(off_t)*CHAR_BIT - 1)) + 1);
  43. }
  44. static int timeout_lock(int fd, int rw, off_t off, off_t len, bool waitflag,
  45. void *_timeout)
  46. {
  47. int ret, saved_errno = errno;
  48. unsigned int timeout = *(unsigned int *)_timeout;
  49. flock_struct.l_type = rw;
  50. flock_struct.l_whence = SEEK_SET;
  51. flock_struct.l_start = off;
  52. flock_struct.l_len = len;
  53. CatchSignal(SIGALRM, invalidate_flock_struct);
  54. alarm(timeout);
  55. for (;;) {
  56. if (waitflag)
  57. ret = fcntl(fd, F_SETLKW, &flock_struct);
  58. else
  59. ret = fcntl(fd, F_SETLK, &flock_struct);
  60. if (ret == 0)
  61. break;
  62. /* Not signalled? Something else went wrong. */
  63. if (flock_struct.l_len == len) {
  64. if (errno == EAGAIN || errno == EINTR)
  65. continue;
  66. saved_errno = errno;
  67. break;
  68. } else {
  69. saved_errno = EINTR;
  70. break;
  71. }
  72. }
  73. alarm(0);
  74. errno = saved_errno;
  75. return ret;
  76. }
  77. static int ntdb_chainlock_with_timeout_internal(struct ntdb_context *ntdb,
  78. NTDB_DATA key,
  79. unsigned int timeout,
  80. int rw_type)
  81. {
  82. union ntdb_attribute locking;
  83. enum NTDB_ERROR ecode;
  84. if (timeout) {
  85. locking.base.attr = NTDB_ATTRIBUTE_FLOCK;
  86. ecode = ntdb_get_attribute(ntdb, &locking);
  87. if (ecode != NTDB_SUCCESS)
  88. return ecode;
  89. /* Replace locking function with our own. */
  90. locking.flock.data = &timeout;
  91. locking.flock.lock = timeout_lock;
  92. ecode = ntdb_set_attribute(ntdb, &locking);
  93. if (ecode != NTDB_SUCCESS)
  94. return ecode;
  95. }
  96. if (rw_type == F_RDLCK)
  97. ecode = ntdb_chainlock_read(ntdb, key);
  98. else
  99. ecode = ntdb_chainlock(ntdb, key);
  100. if (timeout) {
  101. ntdb_unset_attribute(ntdb, NTDB_ATTRIBUTE_FLOCK);
  102. }
  103. return ecode;
  104. }
  105. int main(int argc, char *argv[])
  106. {
  107. unsigned int i;
  108. struct ntdb_context *ntdb;
  109. NTDB_DATA key = ntdb_mkdata("hello", 5);
  110. int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
  111. NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
  112. struct agent *agent;
  113. plan_tests(sizeof(flags) / sizeof(flags[0]) * 15);
  114. agent = prepare_external_agent();
  115. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  116. enum NTDB_ERROR ecode;
  117. ntdb = ntdb_open("run-locktimeout.ntdb",
  118. flags[i]|MAYBE_NOSYNC,
  119. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  120. if (!ok1(ntdb))
  121. break;
  122. /* Simple cases: should succeed. */
  123. ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
  124. F_RDLCK);
  125. ok1(ecode == NTDB_SUCCESS);
  126. ok1(tap_log_messages == 0);
  127. ntdb_chainunlock_read(ntdb, key);
  128. ok1(tap_log_messages == 0);
  129. ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
  130. F_WRLCK);
  131. ok1(ecode == NTDB_SUCCESS);
  132. ok1(tap_log_messages == 0);
  133. ntdb_chainunlock(ntdb, key);
  134. ok1(tap_log_messages == 0);
  135. /* OK, get agent to start transaction, then we should time out. */
  136. ok1(external_agent_operation(agent, OPEN, "run-locktimeout.ntdb")
  137. == SUCCESS);
  138. ok1(external_agent_operation(agent, TRANSACTION_START, "")
  139. == SUCCESS);
  140. ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
  141. F_WRLCK);
  142. ok1(ecode == NTDB_ERR_LOCK);
  143. ok1(tap_log_messages == 0);
  144. /* Even if we get a different signal, should be fine. */
  145. CatchSignal(SIGUSR1, do_nothing);
  146. external_agent_operation(agent, SEND_SIGNAL, "");
  147. ecode = ntdb_chainlock_with_timeout_internal(ntdb, key, 20,
  148. F_WRLCK);
  149. ok1(ecode == NTDB_ERR_LOCK);
  150. ok1(tap_log_messages == 0);
  151. ok1(external_agent_operation(agent, TRANSACTION_COMMIT, "")
  152. == SUCCESS);
  153. ok1(external_agent_operation(agent, CLOSE, "")
  154. == SUCCESS);
  155. ntdb_close(ntdb);
  156. }
  157. free_external_agent(agent);
  158. return exit_status();
  159. }