text_mmap.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * $Id: text_mmap.c,v 4.5 2005/04/16 17:07:29 bkorb Exp $
  3. *
  4. * Time-stamp: "2005-02-24 11:56:43 bkorb"
  5. */
  6. #define FILE_WRITABLE(_prt,_flg) \
  7. ((_prt & PROT_WRITE) && (_flg & (MAP_SHARED|MAP_PRIVATE) == MAP_SHARED))
  8. /***export_func text_mmap
  9. *
  10. * what: map a text file with terminating NUL
  11. *
  12. * arg: const char*, pzFile, name of the file to map
  13. * arg: int, prot, mmap protections (see mmap(2))
  14. * arg: int, flags, mmap flags (see mmap(2))
  15. * arg: tmap_info_t*, mapinfo, returned info about the mapping
  16. *
  17. * ret-type: void*
  18. * ret-desc: The mmaped data address
  19. *
  20. * doc:
  21. *
  22. * This routine will mmap a file into memory ensuring that there is at least
  23. * one @file{NUL} character following the file data. It will return the
  24. * address where the file contents have been mapped into memory. If there is a
  25. * problem, then it will return @code{MAP_FAILED} and set @file{errno}
  26. * appropriately.
  27. *
  28. * The named file does not exist, @code{stat(2)} will set @file{errno} as it
  29. * will. If the file is not a regular file, @file{errno} will be
  30. * @code{EINVAL}. At that point, @code{open(2)} is attempted with the access
  31. * bits set appropriately for the requested @code{mmap(2)} protections and flag
  32. * bits. On failure, @file{errno} will be set according to the documentation
  33. * for @code{open(2)}. If @code{mmap(2)} fails, @file{errno} will be set as
  34. * that routine sets it. If @code{text_mmap} works to this point, a valid
  35. * address will be returned, but there may still be ``issues''.
  36. *
  37. * If the file size is not an even multiple of the system page size, then
  38. * @code{text_map} will return at this point and @file{errno} will be zero.
  39. * Otherwise, an anonymous map is attempted. If not available, then an attempt
  40. * is made to @code{mmap(2)} @file{/dev/zero}. If any of these fail, the
  41. * address of the file's data is returned, bug @code{no} @file{NUL} characters
  42. * are mapped after the end of the data.
  43. *
  44. * see: mmap(2), open(2), stat(2)
  45. *
  46. * err: Any error code issued by mmap(2), open(2), stat(2) is possible.
  47. * Additionally, if the specified file is not a regular file, then
  48. * errno will be set to @code{EINVAL}.
  49. *
  50. * example:
  51. * #include <mylib.h>
  52. * tmap_info_t mi;
  53. * int no_nul;
  54. * void* data = text_mmap( "file", PROT_WRITE, MAP_PRIVATE, &mi );
  55. * if (data == MAP_FAILED) return;
  56. * no_nul = (mi.txt_size == mi.txt_full_size);
  57. * << use the data >>
  58. * text_munmap( &mi );
  59. =*/
  60. LOCAL void*
  61. text_mmap( const char* pzFile, int prot, int flags, tmap_info_t* pMI )
  62. {
  63. memset( pMI, 0, sizeof(*pMI) );
  64. #ifdef HAVE_MMAP
  65. pMI->txt_zero_fd = -1;
  66. #endif
  67. pMI->txt_fd = -1;
  68. /*
  69. * Make sure we can stat the regular file. Save the file size.
  70. */
  71. {
  72. struct stat sb;
  73. if (stat( pzFile, &sb ) != 0) {
  74. pMI->txt_errno = errno;
  75. return MAP_FAILED;
  76. }
  77. if (! S_ISREG( sb.st_mode )) {
  78. pMI->txt_errno = errno = EINVAL;
  79. return MAP_FAILED;
  80. }
  81. pMI->txt_size = sb.st_size;
  82. }
  83. /*
  84. * Map mmap flags and protections into open flags and do the open.
  85. */
  86. {
  87. int o_flag;
  88. /*
  89. * See if we will be updating the file. If we can alter the memory
  90. * and if we share the data and we are *not* copy-on-writing the data,
  91. * then our updates will show in the file, so we must open with
  92. * write access.
  93. */
  94. if (FILE_WRITABLE(prot,flags))
  95. o_flag = O_RDWR;
  96. else
  97. o_flag = O_RDONLY;
  98. /*
  99. * If you're not sharing the file and you are writing to it,
  100. * then don't let anyone else have access to the file.
  101. */
  102. if (((flags & MAP_SHARED) == 0) && (prot & PROT_WRITE))
  103. o_flag |= O_EXCL;
  104. pMI->txt_fd = open( pzFile, o_flag );
  105. }
  106. if (pMI->txt_fd < 0) {
  107. pMI->txt_errno = errno;
  108. return MAP_FAILED;
  109. }
  110. #ifdef HAVE_MMAP /* * * * * WITH MMAP * * * * * */
  111. /*
  112. * do the mmap. If we fail, then preserve errno, close the file and
  113. * return the failure.
  114. */
  115. pMI->txt_data = mmap( NULL, pMI->txt_size, prot, flags, pMI->txt_fd, 0 );
  116. if (pMI->txt_data == MAP_FAILED) {
  117. pMI->txt_errno = errno;
  118. goto fail_return;
  119. }
  120. /*
  121. * Most likely, everything will turn out fine now. The only difficult
  122. * part at this point is coping with files with sizes that are a multiple
  123. * of the page size. Handling that is what this whole thing is about.
  124. */
  125. pMI->txt_zero_fd = -1;
  126. pMI->txt_errno = 0;
  127. do {
  128. #ifdef _SC_PAGESIZE
  129. size_t pgsz = sysconf(_SC_PAGESIZE);
  130. #else
  131. size_t pgsz = getpagesize();
  132. #endif
  133. /*
  134. * Compute the pagesize rounded mapped memory size.
  135. * IF this is not the same as the file size, then there are NUL's
  136. * at the end of the file mapping and all is okay.
  137. */
  138. pMI->txt_full_size = (pMI->txt_size + (pgsz - 1)) & ~(pgsz - 1);
  139. if (pMI->txt_size != pMI->txt_full_size)
  140. break;
  141. /*
  142. * Still here? We have to append a page of NUL's
  143. */
  144. pMI->txt_full_size += pgsz;
  145. {
  146. #ifdef MAP_ANONYMOUS
  147. void* pNuls = mmap(
  148. (void*)(((char*)pMI->txt_data) + pMI->txt_size), pgsz,
  149. PROT_READ, MAP_ANONYMOUS|MAP_FIXED, 0, 0 );
  150. if (pNuls == MAP_FAILED) {
  151. pMI->txt_errno = errno;
  152. pMI->txt_full_size = pMI->txt_size;
  153. }
  154. #else
  155. pMI->txt_zero_fd = open( "/dev/zero", O_RDONLY );
  156. if (pMI->txt_zero_fd < 0) {
  157. pMI->txt_errno = errno;
  158. pMI->txt_full_size = pMI->txt_size;
  159. } else {
  160. void* pNuls = mmap(
  161. (void*)(((char*)pMI->txt_data) + pMI->txt_size), pgsz,
  162. PROT_READ, MAP_PRIVATE|MAP_FIXED, pMI->txt_zero_fd, 0 );
  163. if (pNuls == MAP_FAILED) {
  164. pMI->txt_errno = errno;
  165. close( pMI->txt_zero_fd );
  166. pMI->txt_zero_fd = -1;
  167. }
  168. }
  169. #endif
  170. }
  171. } while(0);
  172. return pMI->txt_data;
  173. #else /* * * * * * no HAVE_MMAP * * * * * */
  174. pMI->txt_data = AGALOC( pMI->txt_size+1, "file text" );
  175. if (pMI->txt_data == NULL) {
  176. pMI->txt_errno = ENOMEM;
  177. goto fail_return;
  178. }
  179. {
  180. size_t sz = pMI->txt_size;
  181. char* pz = pMI->txt_data;
  182. while (sz > 0) {
  183. ssize_t rdct = read( pMI->txt_fd, pz, sz );
  184. if (rdct <= 0) {
  185. pMI->txt_errno = errno;
  186. fprintf( stderr, zFSErrReadFile,
  187. errno, strerror( errno ), pzFile );
  188. free( pMI->txt_data );
  189. goto fail_return;
  190. }
  191. pz += rdct;
  192. sz -= rdct;
  193. }
  194. *pz = NUL;
  195. }
  196. /*
  197. * We never need a dummy page mapped in
  198. */
  199. pMI->txt_zero_fd = -1;
  200. pMI->txt_errno = 0;
  201. return pMI->txt_data;
  202. #endif /* * * * * * no HAVE_MMAP * * * * * */
  203. fail_return:
  204. if (pMI->txt_fd >= 0) {
  205. close( pMI->txt_fd );
  206. pMI->txt_fd = -1;
  207. }
  208. errno = pMI->txt_errno;
  209. pMI->txt_data = MAP_FAILED;
  210. return pMI->txt_data;
  211. }
  212. /***export_func text_munmap
  213. *
  214. * what: unmap the data mapped in by text_mmap
  215. *
  216. * arg: tmap_info_t*, mapinfo, info about the mapping
  217. *
  218. * ret-type: int
  219. * ret-desc: -1 or 0. @file{errno} will have the error code.
  220. *
  221. * doc:
  222. *
  223. * This routine will unmap the data mapped in with @code{text_mmap} and close
  224. * the associated file descriptors opened by that function.
  225. *
  226. * see: munmap(2), close(2)
  227. *
  228. * err: Any error code issued by munmap(2) or close(2) is possible.
  229. =*/
  230. LOCAL int
  231. text_munmap( tmap_info_t* pMI )
  232. {
  233. #ifdef HAVE_MMAP
  234. int res = munmap( pMI->txt_data, pMI->txt_full_size );
  235. if (res != 0)
  236. goto error_return;
  237. res = close( pMI->txt_fd );
  238. if (res != 0)
  239. goto error_return;
  240. pMI->txt_fd = -1;
  241. errno = 0;
  242. if (pMI->txt_zero_fd != -1) {
  243. res = close( pMI->txt_zero_fd );
  244. if (res == 0)
  245. pMI->txt_zero_fd = -1;
  246. }
  247. error_return:
  248. pMI->txt_errno = errno;
  249. return res;
  250. #else /* HAVE_MMAP */
  251. errno = 0;
  252. /*
  253. * IF the memory is writable *AND* it is not private (copy-on-write)
  254. * *AND* the memory is "sharable" (seen by other processes)
  255. * THEN rewrite the data.
  256. */
  257. if ( FILE_WRITABLE(pMI->txt_prot, pMI->txt_flags)
  258. && (lseek( pMI->txt_fd, 0, SEEK_SET ) >= 0) ) {
  259. write( pMI->txt_fd, pMI->txt_data, pMI->txt_size );
  260. }
  261. close( pMI->txt_fd );
  262. pMI->txt_fd = -1;
  263. pMI->txt_errno = errno;
  264. free( pMI->txt_data );
  265. return pMI->txt_errno;
  266. #endif /* HAVE_MMAP */
  267. }
  268. /*
  269. * Local Variables:
  270. * mode: C
  271. * c-file-style: "stroustrup"
  272. * tab-width: 4
  273. * indent-tabs-mode: nil
  274. * End:
  275. * end of autoopts/text_mmap.c */