shmat.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "shmat.h"
  2. #include <util/generic/guid.h>
  3. #if defined(_win_)
  4. #include <stdio.h>
  5. #include "winint.h"
  6. #elif defined(_bionic_)
  7. #include <sys/types.h>
  8. #include <sys/ipc.h>
  9. #include <sys/syscall.h>
  10. #elif defined(_unix_)
  11. #include <sys/types.h>
  12. #include <sys/ipc.h>
  13. #include <sys/shm.h>
  14. #endif
  15. #if defined(_cygwin_)
  16. #define WINAPI __stdcall
  17. #define FILE_MAP_ALL_ACCESS ((long)983071)
  18. #define PAGE_READWRITE 4
  19. #define FALSE 0
  20. extern "C" {
  21. using HANDLE = OS_HANDLE;
  22. using BOOL = int;
  23. using DWORD = ui32;
  24. using LPCTSTR = const char*;
  25. using LPVOID = void*;
  26. using LPCVOID = void const*;
  27. using SIZE_T = size_t;
  28. BOOL WINAPI CloseHandle(HANDLE hObject);
  29. HANDLE WINAPI OpenFileMappingA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpName);
  30. LPVOID WINAPI MapViewOfFile(HANDLE hFileMappingObject, DWORD DesiredAccess, DWORD FileOffsetHigh, DWORD FileOffsetLow, SIZE_T NumberOfBytesToMap);
  31. HANDLE WINAPI CreateFileMappingA(HANDLE hFile, LPVOID lpAttributes, DWORD flProtect, DWORD MaximumSizeHigh, DWORD MaximumSizeLow, LPCTSTR lpName);
  32. BOOL WINAPI UnmapViewOfFile(LPCVOID lpBaseAddress);
  33. DWORD WINAPI GetLastError(void);
  34. }
  35. #endif
  36. #if defined(_bionic_)
  37. namespace {
  38. #if !defined(__i386__)
  39. static int shmget(key_t key, size_t size, int flag) {
  40. if (size > PTRDIFF_MAX) {
  41. size = SIZE_MAX;
  42. }
  43. return syscall(__NR_shmget, key, size, flag);
  44. }
  45. static void* shmat(int id, const void* addr, int flag) {
  46. return (void*)syscall(__NR_shmat, id, addr, flag);
  47. }
  48. static int shmctl(int id, int cmd, void* buf) {
  49. return syscall(__NR_shmctl, id, cmd | IPC_64, buf);
  50. }
  51. static int shmdt(const void* addr) {
  52. return syscall(__NR_shmdt, addr);
  53. }
  54. #else
  55. #define IPCOP_shmat 21
  56. #define IPCOP_shmdt 22
  57. #define IPCOP_shmget 23
  58. #define IPCOP_shmctl 24
  59. static int shmget(key_t key, size_t size, int flag) {
  60. return syscall(__NR_ipc, IPCOP_shmget, key, size, flag, 0);
  61. }
  62. static void* shmat(int id, const void* addr, int flag) {
  63. void* retval;
  64. long res = syscall(__NR_ipc, IPCOP_shmat, id, flag, (long)&retval, addr);
  65. return (res >= 0) ? retval : (void*)-1;
  66. }
  67. static int shmctl(int id, int cmd, void* buf) {
  68. return syscall(__NR_ipc, IPCOP_shmctl, id, cmd | IPC_64, 0, buf);
  69. }
  70. static int shmdt(const void* addr) {
  71. return syscall(__NR_ipc, IPCOP_shmdt, 0, 0, 0, addr);
  72. }
  73. #endif
  74. } // namespace
  75. #endif
  76. TSharedMemory::TSharedMemory()
  77. : Handle(INVALID_FHANDLE)
  78. , Data(nullptr)
  79. , Size(0)
  80. {
  81. }
  82. #if defined(_win_)
  83. static void FormatName(char* buf, const TGUID& id) {
  84. sprintf(buf, "Global\\shmat-%s", GetGuidAsString(id).c_str());
  85. }
  86. bool TSharedMemory::Open(const TGUID& id, int size) {
  87. // Y_ASSERT(Data == 0);
  88. Id = id;
  89. Size = size;
  90. char name[100];
  91. FormatName(name, Id);
  92. Handle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, name);
  93. if (Handle == 0) {
  94. return false;
  95. }
  96. Data = MapViewOfFile(Handle, FILE_MAP_ALL_ACCESS, 0, 0, size);
  97. if (Data == 0) {
  98. // Y_ASSERT(0);
  99. CloseHandle(Handle);
  100. Handle = INVALID_OS_HANDLE;
  101. return false;
  102. }
  103. return true;
  104. }
  105. bool TSharedMemory::Create(int size) {
  106. // Y_ASSERT(Data == 0);
  107. Size = size;
  108. CreateGuid(&Id);
  109. char name[100];
  110. FormatName(name, Id);
  111. Handle = CreateFileMappingA(INVALID_OS_HANDLE, nullptr, PAGE_READWRITE, 0, size, name);
  112. if (Handle == 0) {
  113. // Y_ASSERT(0);
  114. return false;
  115. }
  116. Data = MapViewOfFile(Handle, FILE_MAP_ALL_ACCESS, 0, 0, size);
  117. if (Data == 0) {
  118. // Y_ASSERT(0);
  119. CloseHandle(Handle);
  120. Handle = INVALID_OS_HANDLE;
  121. return false;
  122. }
  123. return true;
  124. }
  125. TSharedMemory::~TSharedMemory() {
  126. if (Data) {
  127. UnmapViewOfFile(Handle);
  128. }
  129. CloseHandle(Handle);
  130. }
  131. #else
  132. static key_t GetKey(const TGUID& id) {
  133. i64 id64 = (ui64)(((ui64)id.dw[0] + (ui64)id.dw[2]) << 32) + (ui64)id.dw[1] + (ui64)id.dw[3];
  134. return id64;
  135. }
  136. bool TSharedMemory::Open(const TGUID& id, int size) {
  137. Y_ABORT_UNLESS(id, "invalid shared memory guid: %s", GetGuidAsString(id).data());
  138. // Y_ASSERT(Data == 0);
  139. Size = size;
  140. key_t k = GetKey(id);
  141. int shmId = shmget(k, Size, 0777); // do not fill Handle, since IPC_RMID should be called by owner
  142. if (shmId < 0) {
  143. return false;
  144. }
  145. Data = shmat(shmId, nullptr, 0);
  146. if (Data == nullptr) {
  147. // Y_ASSERT(0);
  148. return false;
  149. }
  150. return true;
  151. }
  152. bool TSharedMemory::Create(int size) {
  153. // Y_ASSERT(Data == 0);
  154. Size = size;
  155. CreateGuid(&Id);
  156. key_t k = GetKey(Id);
  157. Handle = shmget(k, Size, IPC_CREAT | IPC_EXCL | 0777);
  158. if (Handle < 0) {
  159. // Y_ASSERT(0);
  160. return false;
  161. }
  162. Data = shmat(Handle, nullptr, 0);
  163. if (Data == (void*)-1) {
  164. // Y_ASSERT(0);
  165. shmctl(Handle, IPC_RMID, nullptr);
  166. Handle = -1;
  167. return false;
  168. }
  169. return true;
  170. }
  171. TSharedMemory::~TSharedMemory() {
  172. if (Data) {
  173. shmdt(Data);
  174. }
  175. if (Handle >= 0) {
  176. shmctl(Handle, IPC_RMID, nullptr);
  177. }
  178. }
  179. #endif