file_locking_helpers.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2009-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_FILE_LOCKING_HELPERS_HPP
  11. #define BOOST_INTERPROCESS_FILE_LOCKING_HELPERS_HPP
  12. #if defined(_MSC_VER)&&(_MSC_VER>=1200)
  13. #pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <sstream>
  18. #include <string>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <errno.h>
  22. #include <cstddef>
  23. #include <boost/interprocess/detail/os_file_functions.hpp>
  24. #include <boost/interprocess/detail/tmp_dir_helpers.hpp>
  25. #if defined(BOOST_INTERPROCESS_WINDOWS)
  26. #include <fcntl.h>
  27. #include <io.h>
  28. #include <sys/locking.h>
  29. #else //defined(BOOST_INTERPROCESS_WINDOWS)
  30. #include <fcntl.h>
  31. #include <sys/stat.h>
  32. #include <unistd.h>
  33. #endif //defined(BOOST_INTERPROCESS_WINDOWS)
  34. namespace boost{
  35. namespace interprocess{
  36. namespace ipcdetail{
  37. #if defined(BOOST_INTERPROCESS_WINDOWS)
  38. struct locking_file_serial_id
  39. {
  40. int fd;
  41. unsigned long dwVolumeSerialNumber;
  42. unsigned long nFileIndexHigh;
  43. unsigned long nFileIndexLow;
  44. //This reference count counts the number of modules attached
  45. //to the shared memory and lock file. This serves to unlink
  46. //the locking file and shared memory when all modules are
  47. //done with the global memory (shared memory)
  48. volatile boost::uint32_t modules_attached_to_gmem_count;
  49. };
  50. inline bool lock_locking_file(int fd)
  51. {
  52. int ret = 0;
  53. while(ret != 0 && errno == EDEADLK){
  54. ret = _locking(fd, _LK_LOCK, 1/*lock_file_contents_length()*/);
  55. }
  56. return 0 == ret;
  57. }
  58. inline bool try_lock_locking_file(int fd)
  59. {
  60. return 0 == _locking(fd, _LK_NBLCK , 1);
  61. }
  62. inline int open_or_create_and_lock_file(const char *name)
  63. {
  64. permissions p;
  65. p.set_unrestricted();
  66. while(1){
  67. file_handle_t handle = create_or_open_file(name, read_write, p);
  68. int fd = _open_osfhandle((intptr_t)handle, _O_TEXT);
  69. if(fd < 0){
  70. close_file(handle);
  71. return fd;
  72. }
  73. if(!try_lock_locking_file(fd)){
  74. _close(fd);
  75. return -1;
  76. }
  77. struct _stat s;
  78. if(0 == _stat(name, &s)){
  79. return fd;
  80. }
  81. else{
  82. _close(fd);
  83. }
  84. }
  85. }
  86. inline int try_open_and_lock_file(const char *name)
  87. {
  88. file_handle_t handle = open_existing_file(name, read_write);
  89. int fd = _open_osfhandle((intptr_t)handle, _O_TEXT);
  90. if(fd < 0){
  91. close_file(handle);
  92. return fd;
  93. }
  94. if(!try_lock_locking_file(fd)){
  95. _close(fd);
  96. return -1;
  97. }
  98. return fd;
  99. }
  100. inline void close_lock_file(int fd)
  101. { _close(fd); }
  102. inline bool is_valid_fd(int fd)
  103. {
  104. struct _stat s;
  105. return EBADF != _fstat(fd, &s);
  106. }
  107. inline bool is_normal_file(int fd)
  108. {
  109. if(_isatty(fd))
  110. return false;
  111. struct _stat s;
  112. if(0 != _fstat(fd, &s))
  113. return false;
  114. return 0 != (s.st_mode & _S_IFREG);
  115. }
  116. inline std::size_t get_size(int fd)
  117. {
  118. struct _stat s;
  119. if(0 != _fstat(fd, &s))
  120. return 0u;
  121. return (std::size_t)s.st_size;
  122. }
  123. inline bool fill_file_serial_id(int fd, locking_file_serial_id &id)
  124. {
  125. winapi::interprocess_by_handle_file_information info;
  126. if(!winapi::get_file_information_by_handle((void*)_get_osfhandle(fd), &info))
  127. return false;
  128. id.fd = fd;
  129. id.dwVolumeSerialNumber = info.dwVolumeSerialNumber;
  130. id.nFileIndexHigh = info.nFileIndexHigh;
  131. id.nFileIndexLow = info.nFileIndexLow;
  132. id.modules_attached_to_gmem_count = 1; //Initialize attached count
  133. return true;
  134. }
  135. inline bool compare_file_serial(int fd, const locking_file_serial_id &id)
  136. {
  137. winapi::interprocess_by_handle_file_information info;
  138. if(!winapi::get_file_information_by_handle((void*)_get_osfhandle(fd), &info))
  139. return false;
  140. return id.dwVolumeSerialNumber == info.dwVolumeSerialNumber &&
  141. id.nFileIndexHigh == info.nFileIndexHigh &&
  142. id.nFileIndexLow == info.nFileIndexLow;
  143. }
  144. #else //UNIX
  145. struct locking_file_serial_id
  146. {
  147. int fd;
  148. dev_t st_dev;
  149. ino_t st_ino;
  150. //This reference count counts the number of modules attached
  151. //to the shared memory and lock file. This serves to unlink
  152. //the locking file and shared memory when all modules are
  153. //done with the global memory (shared memory)
  154. volatile boost::uint32_t modules_attached_to_gmem_count;
  155. };
  156. inline bool lock_locking_file(int fd)
  157. {
  158. int ret = 0;
  159. while(ret != 0 && errno != EINTR){
  160. struct flock lock;
  161. lock.l_type = F_WRLCK;
  162. lock.l_whence = SEEK_SET;
  163. lock.l_start = 0;
  164. lock.l_len = 1;
  165. ret = fcntl (fd, F_SETLKW, &lock);
  166. }
  167. return 0 == ret;
  168. }
  169. inline bool try_lock_locking_file(int fd)
  170. {
  171. struct flock lock;
  172. lock.l_type = F_WRLCK;
  173. lock.l_whence = SEEK_SET;
  174. lock.l_start = 0;
  175. lock.l_len = 1;
  176. return 0 == fcntl (fd, F_SETLK, &lock);
  177. }
  178. inline int open_or_create_and_lock_file(const char *name)
  179. {
  180. permissions p;
  181. p.set_unrestricted();
  182. while(1){
  183. int fd = create_or_open_file(name, read_write, p);
  184. if(fd < 0){
  185. return fd;
  186. }
  187. if(!try_lock_locking_file(fd)){
  188. close(fd);
  189. return -1;
  190. }
  191. struct stat s;
  192. if(0 == stat(name, &s)){
  193. return fd;
  194. }
  195. else{
  196. close(fd);
  197. }
  198. }
  199. }
  200. inline int try_open_and_lock_file(const char *name)
  201. {
  202. int fd = open_existing_file(name, read_write);
  203. if(fd < 0){
  204. return fd;
  205. }
  206. if(!try_lock_locking_file(fd)){
  207. close(fd);
  208. return -1;
  209. }
  210. return fd;
  211. }
  212. inline void close_lock_file(int fd)
  213. { close(fd); }
  214. inline bool is_valid_fd(int fd)
  215. {
  216. struct stat s;
  217. return EBADF != fstat(fd, &s);
  218. }
  219. inline bool is_normal_file(int fd)
  220. {
  221. struct stat s;
  222. if(0 != fstat(fd, &s))
  223. return false;
  224. return 0 != (s.st_mode & S_IFREG);
  225. }
  226. inline std::size_t get_size(int fd)
  227. {
  228. struct stat s;
  229. if(0 != fstat(fd, &s))
  230. return 0u;
  231. return (std::size_t)s.st_size;
  232. }
  233. inline bool fill_file_serial_id(int fd, locking_file_serial_id &id)
  234. {
  235. struct stat s;
  236. if(0 != fstat(fd, &s))
  237. return false;
  238. id.fd = fd;
  239. id.st_dev = s.st_dev;
  240. id.st_ino = s.st_ino;
  241. id.modules_attached_to_gmem_count = 1; //Initialize attached count
  242. return true;
  243. }
  244. inline bool compare_file_serial(int fd, const locking_file_serial_id &id)
  245. {
  246. struct stat info;
  247. if(0 != fstat(fd, &info))
  248. return false;
  249. return id.st_dev == info.st_dev &&
  250. id.st_ino == info.st_ino;
  251. }
  252. #endif
  253. } //namespace ipcdetail{
  254. } //namespace interprocess{
  255. } //namespace boost{
  256. #include <boost/interprocess/detail/config_end.hpp>
  257. #endif //BOOST_INTERPROCESS_FILE_LOCKING_HELPERS_HPP