flock.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* Emulate flock on platforms that lack it, primarily Windows and MinGW.
  2. This is derived from sqlite3 sources.
  3. http://www.sqlite.org/cvstrac/rlog?f=sqlite/src/os_win.c
  4. http://www.sqlite.org/copyright.html
  5. Written by Richard W.M. Jones <rjones.at.redhat.com>
  6. Copyright (C) 2008-2017 Free Software Foundation, Inc.
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  17. #include <config.h>
  18. #include <sys/file.h>
  19. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  20. /* LockFileEx */
  21. # define WIN32_LEAN_AND_MEAN
  22. # include <windows.h>
  23. # include <errno.h>
  24. /* _get_osfhandle */
  25. # include "msvc-nothrow.h"
  26. /* Determine the current size of a file. Because the other braindead
  27. * APIs we'll call need lower/upper 32 bit pairs, keep the file size
  28. * like that too.
  29. */
  30. static BOOL
  31. file_size (HANDLE h, DWORD * lower, DWORD * upper)
  32. {
  33. *lower = GetFileSize (h, upper);
  34. return 1;
  35. }
  36. /* LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems. */
  37. # ifndef LOCKFILE_FAIL_IMMEDIATELY
  38. # define LOCKFILE_FAIL_IMMEDIATELY 1
  39. # endif
  40. /* Acquire a lock. */
  41. static BOOL
  42. do_lock (HANDLE h, int non_blocking, int exclusive)
  43. {
  44. BOOL res;
  45. DWORD size_lower, size_upper;
  46. OVERLAPPED ovlp;
  47. int flags = 0;
  48. /* We're going to lock the whole file, so get the file size. */
  49. res = file_size (h, &size_lower, &size_upper);
  50. if (!res)
  51. return 0;
  52. /* Start offset is 0, and also zero the remaining members of this struct. */
  53. memset (&ovlp, 0, sizeof ovlp);
  54. if (non_blocking)
  55. flags |= LOCKFILE_FAIL_IMMEDIATELY;
  56. if (exclusive)
  57. flags |= LOCKFILE_EXCLUSIVE_LOCK;
  58. return LockFileEx (h, flags, 0, size_lower, size_upper, &ovlp);
  59. }
  60. /* Unlock reader or exclusive lock. */
  61. static BOOL
  62. do_unlock (HANDLE h)
  63. {
  64. int res;
  65. DWORD size_lower, size_upper;
  66. res = file_size (h, &size_lower, &size_upper);
  67. if (!res)
  68. return 0;
  69. return UnlockFile (h, 0, 0, size_lower, size_upper);
  70. }
  71. /* Now our BSD-like flock operation. */
  72. int
  73. flock (int fd, int operation)
  74. {
  75. HANDLE h = (HANDLE) _get_osfhandle (fd);
  76. DWORD res;
  77. int non_blocking;
  78. if (h == INVALID_HANDLE_VALUE)
  79. {
  80. errno = EBADF;
  81. return -1;
  82. }
  83. non_blocking = operation & LOCK_NB;
  84. operation &= ~LOCK_NB;
  85. switch (operation)
  86. {
  87. case LOCK_SH:
  88. res = do_lock (h, non_blocking, 0);
  89. break;
  90. case LOCK_EX:
  91. res = do_lock (h, non_blocking, 1);
  92. break;
  93. case LOCK_UN:
  94. res = do_unlock (h);
  95. break;
  96. default:
  97. errno = EINVAL;
  98. return -1;
  99. }
  100. /* Map Windows errors into Unix errnos. As usual MSDN fails to
  101. * document the permissible error codes.
  102. */
  103. if (!res)
  104. {
  105. DWORD err = GetLastError ();
  106. switch (err)
  107. {
  108. /* This means someone else is holding a lock. */
  109. case ERROR_LOCK_VIOLATION:
  110. errno = EAGAIN;
  111. break;
  112. /* Out of memory. */
  113. case ERROR_NOT_ENOUGH_MEMORY:
  114. errno = ENOMEM;
  115. break;
  116. case ERROR_BAD_COMMAND:
  117. errno = EINVAL;
  118. break;
  119. /* Unlikely to be other errors, but at least don't lose the
  120. * error code.
  121. */
  122. default:
  123. errno = err;
  124. }
  125. return -1;
  126. }
  127. return 0;
  128. }
  129. #else /* !Windows */
  130. # ifdef HAVE_STRUCT_FLOCK_L_TYPE
  131. /* We know how to implement flock in terms of fcntl. */
  132. # include <fcntl.h>
  133. # ifdef HAVE_UNISTD_H
  134. # include <unistd.h>
  135. # endif
  136. # include <errno.h>
  137. # include <string.h>
  138. int
  139. flock (int fd, int operation)
  140. {
  141. int cmd, r;
  142. struct flock fl;
  143. if (operation & LOCK_NB)
  144. cmd = F_SETLK;
  145. else
  146. cmd = F_SETLKW;
  147. operation &= ~LOCK_NB;
  148. memset (&fl, 0, sizeof fl);
  149. fl.l_whence = SEEK_SET;
  150. /* l_start & l_len are 0, which as a special case means "whole file". */
  151. switch (operation)
  152. {
  153. case LOCK_SH:
  154. fl.l_type = F_RDLCK;
  155. break;
  156. case LOCK_EX:
  157. fl.l_type = F_WRLCK;
  158. break;
  159. case LOCK_UN:
  160. fl.l_type = F_UNLCK;
  161. break;
  162. default:
  163. errno = EINVAL;
  164. return -1;
  165. }
  166. r = fcntl (fd, cmd, &fl);
  167. if (r == -1 && errno == EACCES)
  168. errno = EAGAIN;
  169. return r;
  170. }
  171. # else /* !HAVE_STRUCT_FLOCK_L_TYPE */
  172. # error "This platform lacks flock function, and Gnulib doesn't provide a replacement. This is a bug in Gnulib."
  173. # endif /* !HAVE_STRUCT_FLOCK_L_TYPE */
  174. #endif /* !Windows */