text_mmap.c 11 KB

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