text_mmap.c 11 KB

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