mmap.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "apr.h"
  17. #include "apr_private.h"
  18. #include "apr_general.h"
  19. #include "apr_strings.h"
  20. #include "apr_mmap.h"
  21. #include "apr_errno.h"
  22. #include "apr_arch_file_io.h"
  23. #include "apr_portable.h"
  24. /* System headers required for the mmap library */
  25. #ifdef BEOS
  26. #include <kernel/OS.h>
  27. #endif
  28. #if APR_HAVE_STRING_H
  29. #include <string.h>
  30. #endif
  31. #if APR_HAVE_STDIO_H
  32. #include <stdio.h>
  33. #endif
  34. #ifdef HAVE_SYS_STAT_H
  35. #include <sys/stat.h>
  36. #endif
  37. #ifdef HAVE_SYS_MMAN_H
  38. #include <sys/mman.h>
  39. #endif
  40. #if APR_HAS_MMAP || defined(BEOS)
  41. static apr_status_t mmap_cleanup(void *themmap)
  42. {
  43. apr_mmap_t *mm = themmap;
  44. apr_mmap_t *next = APR_RING_NEXT(mm,link);
  45. int rv = 0;
  46. /* we no longer refer to the mmaped region */
  47. APR_RING_REMOVE(mm,link);
  48. APR_RING_NEXT(mm,link) = NULL;
  49. APR_RING_PREV(mm,link) = NULL;
  50. if (next != mm) {
  51. /* more references exist, so we're done */
  52. return APR_SUCCESS;
  53. }
  54. #ifdef BEOS
  55. rv = delete_area(mm->area);
  56. #else
  57. rv = munmap(mm->mm, mm->size);
  58. #endif
  59. mm->mm = (void *)-1;
  60. if (rv == 0) {
  61. return APR_SUCCESS;
  62. }
  63. return errno;
  64. }
  65. APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new,
  66. apr_file_t *file, apr_off_t offset,
  67. apr_size_t size, apr_int32_t flag,
  68. apr_pool_t *cont)
  69. {
  70. void *mm;
  71. #ifdef BEOS
  72. area_id aid = -1;
  73. uint32 pages = 0;
  74. #else
  75. apr_int32_t native_flags = 0;
  76. #endif
  77. #if APR_HAS_LARGE_FILES && defined(HAVE_MMAP64)
  78. #define mmap mmap64
  79. #elif APR_HAS_LARGE_FILES && SIZEOF_OFF_T == 4
  80. /* LFS but no mmap64: check for overflow */
  81. if ((apr_int64_t)offset + size > INT_MAX)
  82. return APR_EINVAL;
  83. #endif
  84. if (size == 0)
  85. return APR_EINVAL;
  86. if (file == NULL || file->filedes == -1 || file->buffered)
  87. return APR_EBADF;
  88. (*new) = (apr_mmap_t *)apr_pcalloc(cont, sizeof(apr_mmap_t));
  89. #ifdef BEOS
  90. /* XXX: mmap shouldn't really change the seek offset */
  91. apr_file_seek(file, APR_SET, &offset);
  92. /* There seems to be some strange interactions that mean our area must
  93. * be set as READ & WRITE or writev will fail! Go figure...
  94. * So we ignore the value in flags and always ask for both READ and WRITE
  95. */
  96. pages = (size + B_PAGE_SIZE -1) / B_PAGE_SIZE;
  97. aid = create_area("apr_mmap", &mm , B_ANY_ADDRESS, pages * B_PAGE_SIZE,
  98. B_NO_LOCK, B_WRITE_AREA|B_READ_AREA);
  99. if (aid < B_NO_ERROR) {
  100. /* we failed to get an area we can use... */
  101. *new = NULL;
  102. return APR_ENOMEM;
  103. }
  104. if (aid >= B_NO_ERROR)
  105. read(file->filedes, mm, size);
  106. (*new)->area = aid;
  107. #else
  108. if (flag & APR_MMAP_WRITE) {
  109. native_flags |= PROT_WRITE;
  110. }
  111. if (flag & APR_MMAP_READ) {
  112. native_flags |= PROT_READ;
  113. }
  114. mm = mmap(NULL, size, native_flags, MAP_SHARED, file->filedes, offset);
  115. if (mm == (void *)-1) {
  116. /* we failed to get an mmap'd file... */
  117. *new = NULL;
  118. return errno;
  119. }
  120. #endif
  121. (*new)->mm = mm;
  122. (*new)->size = size;
  123. (*new)->cntxt = cont;
  124. APR_RING_ELEM_INIT(*new, link);
  125. /* register the cleanup... */
  126. apr_pool_cleanup_register((*new)->cntxt, (void*)(*new), mmap_cleanup,
  127. apr_pool_cleanup_null);
  128. return APR_SUCCESS;
  129. }
  130. APR_DECLARE(apr_status_t) apr_mmap_dup(apr_mmap_t **new_mmap,
  131. apr_mmap_t *old_mmap,
  132. apr_pool_t *p)
  133. {
  134. *new_mmap = (apr_mmap_t *)apr_pmemdup(p, old_mmap, sizeof(apr_mmap_t));
  135. (*new_mmap)->cntxt = p;
  136. APR_RING_INSERT_AFTER(old_mmap, *new_mmap, link);
  137. apr_pool_cleanup_register(p, *new_mmap, mmap_cleanup,
  138. apr_pool_cleanup_null);
  139. return APR_SUCCESS;
  140. }
  141. APR_DECLARE(apr_status_t) apr_mmap_delete(apr_mmap_t *mm)
  142. {
  143. return apr_pool_cleanup_run(mm->cntxt, mm, mmap_cleanup);
  144. }
  145. #endif