text_mmap.c 10 KB

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