lockfile.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "lib/fs/files.h"
  7. #include "lib/fs/lockfile.h"
  8. #include "lib/log/torlog.h"
  9. #include "lib/log/util_bug.h"
  10. #include "lib/malloc/util_malloc.h"
  11. #ifdef HAVE_SYS_FILE_H
  12. #include <sys/file.h>
  13. #endif
  14. #ifdef HAVE_FCNTL_H
  15. #include <fcntl.h>
  16. #endif
  17. #ifdef HAVE_UNISTD_H
  18. #include <unistd.h>
  19. #endif
  20. #ifdef _WIN32
  21. #include <windows.h>
  22. #include <sys/locking.h>
  23. #endif
  24. #include <errno.h>
  25. #include <string.h>
  26. /** Represents a lockfile on which we hold the lock. */
  27. struct tor_lockfile_t {
  28. /** Name of the file */
  29. char *filename;
  30. /** File descriptor used to hold the file open */
  31. int fd;
  32. };
  33. /** Try to get a lock on the lockfile <b>filename</b>, creating it as
  34. * necessary. If someone else has the lock and <b>blocking</b> is true,
  35. * wait until the lock is available. Otherwise return immediately whether
  36. * we succeeded or not.
  37. *
  38. * Set *<b>locked_out</b> to true if somebody else had the lock, and to false
  39. * otherwise.
  40. *
  41. * Return a <b>tor_lockfile_t</b> on success, NULL on failure.
  42. *
  43. * (Implementation note: because we need to fall back to fcntl on some
  44. * platforms, these locks are per-process, not per-thread. If you want
  45. * to do in-process locking, use tor_mutex_t like a normal person.
  46. * On Windows, when <b>blocking</b> is true, the maximum time that
  47. * is actually waited is 10 seconds, after which NULL is returned
  48. * and <b>locked_out</b> is set to 1.)
  49. */
  50. tor_lockfile_t *
  51. tor_lockfile_lock(const char *filename, int blocking, int *locked_out)
  52. {
  53. tor_lockfile_t *result;
  54. int fd;
  55. *locked_out = 0;
  56. log_info(LD_FS, "Locking \"%s\"", filename);
  57. fd = tor_open_cloexec(filename, O_RDWR|O_CREAT|O_TRUNC, 0600);
  58. if (fd < 0) {
  59. log_warn(LD_FS,"Couldn't open \"%s\" for locking: %s", filename,
  60. strerror(errno));
  61. return NULL;
  62. }
  63. #ifdef _WIN32
  64. _lseek(fd, 0, SEEK_SET);
  65. if (_locking(fd, blocking ? _LK_LOCK : _LK_NBLCK, 1) < 0) {
  66. if (errno != EACCES && errno != EDEADLOCK)
  67. log_warn(LD_FS,"Couldn't lock \"%s\": %s", filename, strerror(errno));
  68. else
  69. *locked_out = 1;
  70. close(fd);
  71. return NULL;
  72. }
  73. #elif defined(HAVE_FLOCK)
  74. if (flock(fd, LOCK_EX|(blocking ? 0 : LOCK_NB)) < 0) {
  75. if (errno != EWOULDBLOCK)
  76. log_warn(LD_FS,"Couldn't lock \"%s\": %s", filename, strerror(errno));
  77. else
  78. *locked_out = 1;
  79. close(fd);
  80. return NULL;
  81. }
  82. #else
  83. {
  84. struct flock lock;
  85. memset(&lock, 0, sizeof(lock));
  86. lock.l_type = F_WRLCK;
  87. lock.l_whence = SEEK_SET;
  88. if (fcntl(fd, blocking ? F_SETLKW : F_SETLK, &lock) < 0) {
  89. if (errno != EACCES && errno != EAGAIN)
  90. log_warn(LD_FS, "Couldn't lock \"%s\": %s", filename, strerror(errno));
  91. else
  92. *locked_out = 1;
  93. close(fd);
  94. return NULL;
  95. }
  96. }
  97. #endif /* defined(_WIN32) || ... */
  98. result = tor_malloc(sizeof(tor_lockfile_t));
  99. result->filename = tor_strdup(filename);
  100. result->fd = fd;
  101. return result;
  102. }
  103. /** Release the lock held as <b>lockfile</b>. */
  104. void
  105. tor_lockfile_unlock(tor_lockfile_t *lockfile)
  106. {
  107. tor_assert(lockfile);
  108. log_info(LD_FS, "Unlocking \"%s\"", lockfile->filename);
  109. #ifdef _WIN32
  110. _lseek(lockfile->fd, 0, SEEK_SET);
  111. if (_locking(lockfile->fd, _LK_UNLCK, 1) < 0) {
  112. log_warn(LD_FS,"Error unlocking \"%s\": %s", lockfile->filename,
  113. strerror(errno));
  114. }
  115. #elif defined(HAVE_FLOCK)
  116. if (flock(lockfile->fd, LOCK_UN) < 0) {
  117. log_warn(LD_FS, "Error unlocking \"%s\": %s", lockfile->filename,
  118. strerror(errno));
  119. }
  120. #else
  121. /* Closing the lockfile is sufficient. */
  122. #endif /* defined(_WIN32) || ... */
  123. close(lockfile->fd);
  124. lockfile->fd = -1;
  125. tor_free(lockfile->filename);
  126. tor_free(lockfile);
  127. }