shm.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_general.h"
  17. #include "fspr_errno.h"
  18. #include "fspr_file_io.h"
  19. #include "fspr_shm.h"
  20. #include "fspr_arch_file_io.h"
  21. #include "limits.h"
  22. typedef struct memblock_t {
  23. fspr_size_t size;
  24. fspr_size_t length;
  25. } memblock_t;
  26. struct fspr_shm_t {
  27. fspr_pool_t *pool;
  28. memblock_t *memblk;
  29. void *usrmem;
  30. fspr_size_t size;
  31. fspr_size_t length;
  32. HANDLE hMap;
  33. };
  34. static fspr_status_t shm_cleanup(void* shm)
  35. {
  36. fspr_status_t rv = APR_SUCCESS;
  37. fspr_shm_t *m = shm;
  38. if (UnmapViewOfFile(m->memblk)) {
  39. rv = fspr_get_os_error();
  40. }
  41. if (CloseHandle(m->hMap)) {
  42. return (rv != APR_SUCCESS) ? rv : fspr_get_os_error();
  43. }
  44. /* ### Do we want to make a point of unlinking m->file here?
  45. * Need to add the fname to the fspr_shm_t, in that case.
  46. */
  47. return rv;
  48. }
  49. APR_DECLARE(fspr_status_t) fspr_shm_create(fspr_shm_t **m,
  50. fspr_size_t reqsize,
  51. const char *file,
  52. fspr_pool_t *pool)
  53. {
  54. static fspr_size_t memblock = 0;
  55. HANDLE hMap, hFile;
  56. fspr_status_t rv;
  57. fspr_size_t size;
  58. fspr_file_t *f;
  59. void *base;
  60. void *mapkey;
  61. DWORD err, sizelo, sizehi;
  62. reqsize += sizeof(memblock_t);
  63. if (!memblock)
  64. {
  65. SYSTEM_INFO si;
  66. GetSystemInfo(&si);
  67. memblock = si.dwAllocationGranularity;
  68. }
  69. /* Compute the granualar multiple of the pagesize */
  70. size = memblock * (1 + (reqsize - 1) / memblock);
  71. sizelo = (DWORD)size;
  72. #ifdef WIN64
  73. sizehi = (DWORD)(size >> 32);
  74. #else
  75. sizehi = 0;
  76. #endif
  77. if (!file) {
  78. /* Do Anonymous, which must be passed as a duplicated handle */
  79. #ifndef _WIN32_WCE
  80. hFile = INVALID_HANDLE_VALUE;
  81. #endif
  82. mapkey = NULL;
  83. }
  84. else {
  85. /* Do file backed, which is not an inherited handle
  86. * While we could open APR_EXCL, it doesn't seem that Unix
  87. * ever did. Ignore that error here, but fail later when
  88. * we discover we aren't the creator of the file map object.
  89. */
  90. rv = fspr_file_open(&f, file,
  91. APR_READ | APR_WRITE | APR_BINARY | APR_CREATE,
  92. APR_UREAD | APR_UWRITE, pool);
  93. if ((rv != APR_SUCCESS)
  94. || ((rv = fspr_os_file_get(&hFile, f)) != APR_SUCCESS)) {
  95. return rv;
  96. }
  97. rv = fspr_file_trunc(f, size);
  98. /* res_name_from_filename turns file into a pseudo-name
  99. * without slashes or backslashes, and prepends the \global
  100. * prefix on Win2K and later
  101. */
  102. mapkey = res_name_from_filename(file, 1, pool);
  103. }
  104. #if APR_HAS_UNICODE_FS
  105. IF_WIN_OS_IS_UNICODE
  106. {
  107. hMap = CreateFileMappingW(hFile, NULL, PAGE_READWRITE,
  108. sizehi, sizelo, mapkey);
  109. }
  110. #endif
  111. #if APR_HAS_ANSI_FS
  112. ELSE_WIN_OS_IS_ANSI
  113. {
  114. hMap = CreateFileMappingA(hFile, NULL, PAGE_READWRITE,
  115. sizehi, sizelo, mapkey);
  116. }
  117. #endif
  118. err = fspr_get_os_error();
  119. if (file) {
  120. fspr_file_close(f);
  121. }
  122. if (hMap && err == ERROR_ALREADY_EXISTS) {
  123. CloseHandle(hMap);
  124. return APR_EEXIST;
  125. }
  126. if (!hMap) {
  127. return err;
  128. }
  129. base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE,
  130. 0, 0, size);
  131. if (!base) {
  132. CloseHandle(hMap);
  133. return fspr_get_os_error();
  134. }
  135. *m = (fspr_shm_t *) fspr_palloc(pool, sizeof(fspr_shm_t));
  136. (*m)->pool = pool;
  137. (*m)->hMap = hMap;
  138. (*m)->memblk = base;
  139. (*m)->size = size;
  140. (*m)->usrmem = (char*)base + sizeof(memblock_t);
  141. (*m)->length = reqsize - sizeof(memblock_t);;
  142. (*m)->memblk->length = (*m)->length;
  143. (*m)->memblk->size = (*m)->size;
  144. fspr_pool_cleanup_register((*m)->pool, *m,
  145. shm_cleanup, fspr_pool_cleanup_null);
  146. return APR_SUCCESS;
  147. }
  148. APR_DECLARE(fspr_status_t) fspr_shm_destroy(fspr_shm_t *m)
  149. {
  150. fspr_status_t rv = shm_cleanup(m);
  151. fspr_pool_cleanup_kill(m->pool, m, shm_cleanup);
  152. return rv;
  153. }
  154. APR_DECLARE(fspr_status_t) fspr_shm_remove(const char *filename,
  155. fspr_pool_t *pool)
  156. {
  157. return APR_ENOTIMPL;
  158. }
  159. APR_DECLARE(fspr_status_t) fspr_shm_attach(fspr_shm_t **m,
  160. const char *file,
  161. fspr_pool_t *pool)
  162. {
  163. HANDLE hMap;
  164. void *mapkey;
  165. void *base;
  166. if (!file) {
  167. return APR_EINVAL;
  168. }
  169. else {
  170. /* res_name_from_filename turns file into a pseudo-name
  171. * without slashes or backslashes, and prepends the \global
  172. * prefix on Win2K and later
  173. */
  174. mapkey = res_name_from_filename(file, 1, pool);
  175. }
  176. #if APR_HAS_UNICODE_FS
  177. IF_WIN_OS_IS_UNICODE
  178. {
  179. #ifndef _WIN32_WCE
  180. hMap = OpenFileMappingW(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapkey);
  181. #else
  182. /* The WCE 3.0 lacks OpenFileMapping. So we emulate one with
  183. * opening the existing shmem and reading its size from the header
  184. */
  185. hMap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
  186. PAGE_READWRITE, 0, sizeof(fspr_shm_t), mapkey);
  187. #endif
  188. }
  189. #endif
  190. #if APR_HAS_ANSI_FS
  191. ELSE_WIN_OS_IS_ANSI
  192. {
  193. hMap = OpenFileMappingA(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapkey);
  194. }
  195. #endif
  196. if (!hMap) {
  197. return fspr_get_os_error();
  198. }
  199. base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
  200. if (!base) {
  201. CloseHandle(hMap);
  202. return fspr_get_os_error();
  203. }
  204. *m = (fspr_shm_t *) fspr_palloc(pool, sizeof(fspr_shm_t));
  205. (*m)->pool = pool;
  206. (*m)->memblk = base;
  207. /* Real (*m)->mem->size could be recovered with VirtualQuery */
  208. (*m)->size = (*m)->memblk->size;
  209. #if _WIN32_WCE
  210. /* Reopen with real size */
  211. UnmapViewOfFile(base);
  212. CloseHandle(hMap);
  213. hMap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
  214. PAGE_READWRITE, 0, (*m)->size, mapkey);
  215. if (!hMap) {
  216. return fspr_get_os_error();
  217. }
  218. base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
  219. if (!base) {
  220. CloseHandle(hMap);
  221. return fspr_get_os_error();
  222. }
  223. #endif
  224. (*m)->hMap = hMap;
  225. (*m)->length = (*m)->memblk->length;
  226. (*m)->usrmem = (char*)base + sizeof(memblock_t);
  227. fspr_pool_cleanup_register((*m)->pool, *m,
  228. shm_cleanup, fspr_pool_cleanup_null);
  229. return APR_SUCCESS;
  230. }
  231. APR_DECLARE(fspr_status_t) fspr_shm_detach(fspr_shm_t *m)
  232. {
  233. fspr_status_t rv = shm_cleanup(m);
  234. fspr_pool_cleanup_kill(m->pool, m, shm_cleanup);
  235. return rv;
  236. }
  237. APR_DECLARE(void *) fspr_shm_baseaddr_get(const fspr_shm_t *m)
  238. {
  239. return m->usrmem;
  240. }
  241. APR_DECLARE(fspr_size_t) fspr_shm_size_get(const fspr_shm_t *m)
  242. {
  243. return m->length;
  244. }
  245. APR_POOL_IMPLEMENT_ACCESSOR(shm)
  246. APR_DECLARE(fspr_status_t) fspr_os_shm_get(fspr_os_shm_t *osshm,
  247. fspr_shm_t *shm)
  248. {
  249. *osshm = shm->hMap;
  250. return APR_SUCCESS;
  251. }
  252. APR_DECLARE(fspr_status_t) fspr_os_shm_put(fspr_shm_t **m,
  253. fspr_os_shm_t *osshm,
  254. fspr_pool_t *pool)
  255. {
  256. void* base;
  257. base = MapViewOfFile(*osshm, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
  258. if (!base) {
  259. return fspr_get_os_error();
  260. }
  261. *m = (fspr_shm_t *) fspr_palloc(pool, sizeof(fspr_shm_t));
  262. (*m)->pool = pool;
  263. (*m)->hMap = *osshm;
  264. (*m)->memblk = base;
  265. (*m)->usrmem = (char*)base + sizeof(memblock_t);
  266. /* Real (*m)->mem->size could be recovered with VirtualQuery */
  267. (*m)->size = (*m)->memblk->size;
  268. (*m)->length = (*m)->memblk->length;
  269. fspr_pool_cleanup_register((*m)->pool, *m,
  270. shm_cleanup, fspr_pool_cleanup_null);
  271. return APR_SUCCESS;
  272. }