1
0

text_mmap.c 10 KB

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