text_mmap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /**
  2. * @file text_mmap.c
  3. *
  4. * Map a text file, ensuring the text always has an ending NUL byte.
  5. *
  6. * @addtogroup autoopts
  7. * @{
  8. */
  9. /*
  10. * This file is part of AutoOpts, a companion to AutoGen.
  11. * AutoOpts is free software.
  12. * AutoOpts is Copyright (C) 1992-2014 by Bruce Korb - all rights reserved
  13. *
  14. * AutoOpts is available under any one of two licenses. The license
  15. * in use must be one of these two and the choice is under the control
  16. * of the user of the license.
  17. *
  18. * The GNU Lesser General Public License, version 3 or later
  19. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  20. *
  21. * The Modified Berkeley Software Distribution License
  22. * See the file "COPYING.mbsd"
  23. *
  24. * These files have the following sha256 sums:
  25. *
  26. * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
  27. * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
  28. * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
  29. */
  30. #if defined(HAVE_MMAP)
  31. # ifndef MAP_ANONYMOUS
  32. # ifdef MAP_ANON
  33. # define MAP_ANONYMOUS MAP_ANON
  34. # endif
  35. # endif
  36. # if ! defined(MAP_ANONYMOUS) && ! defined(HAVE_DEV_ZERO)
  37. /*
  38. * We must have either /dev/zero or anonymous mapping for
  39. * this to work.
  40. */
  41. # undef HAVE_MMAP
  42. # else
  43. # ifdef _SC_PAGESIZE
  44. # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
  45. # else
  46. # define GETPAGESIZE() getpagesize()
  47. # endif
  48. # endif
  49. #endif
  50. /*
  51. * Some weird systems require that a specifically invalid FD number
  52. * get passed in as an argument value. Which value is that? Well,
  53. * as everybody knows, if open(2) fails, it returns -1, so that must
  54. * be the value. :)
  55. */
  56. #define AO_INVALID_FD -1
  57. #define FILE_WRITABLE(_prt,_flg) \
  58. ( (_prt & PROT_WRITE) \
  59. && ((_flg & (MAP_SHARED|MAP_PRIVATE)) == MAP_SHARED))
  60. #define MAP_FAILED_PTR ((void*)MAP_FAILED)
  61. /**
  62. * Load the contents of a text file. There are two separate implementations,
  63. * depending up on whether mmap(3) is available.
  64. *
  65. * If not available, malloc the file length plus one byte. Read it in
  66. * and NUL terminate.
  67. *
  68. * If available, first check to see if the text file size is a multiple of a
  69. * page size. If it is, map the file size plus an extra page from either
  70. * anonymous memory or from /dev/zero. Then map the file text on top of the
  71. * first pages of the anonymous/zero pages. Otherwise, just map the file
  72. * because there will be NUL bytes provided at the end.
  73. *
  74. * @param mapinfo a structure holding everything we need to know
  75. * about the mapping.
  76. *
  77. * @param pzFile name of the file, for error reporting.
  78. */
  79. static void
  80. load_text_file(tmap_info_t * mapinfo, char const * pzFile)
  81. {
  82. #if ! defined(HAVE_MMAP)
  83. mapinfo->txt_data = AGALOC(mapinfo->txt_size+1, "file text");
  84. if (mapinfo->txt_data == NULL) {
  85. mapinfo->txt_errno = ENOMEM;
  86. return;
  87. }
  88. {
  89. size_t sz = mapinfo->txt_size;
  90. char* pz = mapinfo->txt_data;
  91. while (sz > 0) {
  92. ssize_t rdct = read(mapinfo->txt_fd, pz, sz);
  93. if (rdct <= 0) {
  94. mapinfo->txt_errno = errno;
  95. fserr_warn("libopts", "read", pzFile);
  96. free(mapinfo->txt_data);
  97. return;
  98. }
  99. pz += rdct;
  100. sz -= rdct;
  101. }
  102. *pz = NUL;
  103. }
  104. mapinfo->txt_errno = 0;
  105. #else /* HAVE mmap */
  106. size_t const pgsz = (size_t)GETPAGESIZE();
  107. void * map_addr = NULL;
  108. (void)pzFile;
  109. mapinfo->txt_full_size = (mapinfo->txt_size + pgsz) & ~(pgsz - 1);
  110. if (mapinfo->txt_full_size == (mapinfo->txt_size + pgsz)) {
  111. /*
  112. * The text is a multiple of a page boundary. We must map an
  113. * extra page so the text ends with a NUL.
  114. */
  115. #if defined(MAP_ANONYMOUS)
  116. map_addr = mmap(NULL, mapinfo->txt_full_size, PROT_READ|PROT_WRITE,
  117. MAP_ANONYMOUS|MAP_PRIVATE, AO_INVALID_FD, 0);
  118. #else
  119. mapinfo->txt_zero_fd = open("/dev/zero", O_RDONLY);
  120. if (mapinfo->txt_zero_fd == AO_INVALID_FD) {
  121. mapinfo->txt_errno = errno;
  122. return;
  123. }
  124. map_addr = mmap(NULL, mapinfo->txt_full_size, PROT_READ|PROT_WRITE,
  125. MAP_PRIVATE, mapinfo->txt_zero_fd, 0);
  126. #endif
  127. if (map_addr == MAP_FAILED_PTR) {
  128. mapinfo->txt_errno = errno;
  129. return;
  130. }
  131. mapinfo->txt_flags |= MAP_FIXED;
  132. }
  133. mapinfo->txt_data =
  134. mmap(map_addr, mapinfo->txt_size, mapinfo->txt_prot,
  135. mapinfo->txt_flags, mapinfo->txt_fd, 0);
  136. if (mapinfo->txt_data == MAP_FAILED_PTR)
  137. mapinfo->txt_errno = errno;
  138. #endif /* HAVE_MMAP */
  139. }
  140. /**
  141. * Make sure all the parameters are correct: we have a file name that
  142. * is a text file that we can read.
  143. *
  144. * @param fname the text file to map
  145. * @param prot the memory protections requested (read/write/etc.)
  146. * @param flags mmap flags
  147. * @param mapinfo a structure holding everything we need to know
  148. * about the mapping.
  149. */
  150. static void
  151. validate_mmap(char const * fname, int prot, int flags, tmap_info_t * mapinfo)
  152. {
  153. memset(mapinfo, 0, sizeof(*mapinfo));
  154. #if defined(HAVE_MMAP) && ! defined(MAP_ANONYMOUS)
  155. mapinfo->txt_zero_fd = AO_INVALID_FD;
  156. #endif
  157. mapinfo->txt_fd = AO_INVALID_FD;
  158. mapinfo->txt_prot = prot;
  159. mapinfo->txt_flags = flags;
  160. /*
  161. * Map mmap flags and protections into open flags and do the open.
  162. */
  163. {
  164. /*
  165. * See if we will be updating the file. If we can alter the memory
  166. * and if we share the data and we are *not* copy-on-writing the data,
  167. * then our updates will show in the file, so we must open with
  168. * write access.
  169. */
  170. int o_flag = FILE_WRITABLE(prot, flags) ? O_RDWR : O_RDONLY;
  171. /*
  172. * If you're not sharing the file and you are writing to it,
  173. * then don't let anyone else have access to the file.
  174. */
  175. if (((flags & MAP_SHARED) == 0) && (prot & PROT_WRITE))
  176. o_flag |= O_EXCL;
  177. mapinfo->txt_fd = open(fname, o_flag);
  178. if (mapinfo->txt_fd < 0) {
  179. mapinfo->txt_errno = errno;
  180. mapinfo->txt_fd = AO_INVALID_FD;
  181. return;
  182. }
  183. }
  184. /*
  185. * Make sure we can stat the regular file. Save the file size.
  186. */
  187. {
  188. struct stat sb;
  189. if (fstat(mapinfo->txt_fd, &sb) != 0) {
  190. mapinfo->txt_errno = errno;
  191. close(mapinfo->txt_fd);
  192. return;
  193. }
  194. if (! S_ISREG(sb.st_mode)) {
  195. mapinfo->txt_errno = errno = EINVAL;
  196. close(mapinfo->txt_fd);
  197. return;
  198. }
  199. mapinfo->txt_size = (size_t)sb.st_size;
  200. }
  201. if (mapinfo->txt_fd == AO_INVALID_FD)
  202. mapinfo->txt_errno = errno;
  203. }
  204. /**
  205. * Close any files opened by the mapping.
  206. *
  207. * @param mi a structure holding everything we need to know about the map.
  208. */
  209. static void
  210. close_mmap_files(tmap_info_t * mi)
  211. {
  212. if (mi->txt_fd == AO_INVALID_FD)
  213. return;
  214. close(mi->txt_fd);
  215. mi->txt_fd = AO_INVALID_FD;
  216. #if defined(HAVE_MMAP) && ! defined(MAP_ANONYMOUS)
  217. if (mi->txt_zero_fd == AO_INVALID_FD)
  218. return;
  219. close(mi->txt_zero_fd);
  220. mi->txt_zero_fd = AO_INVALID_FD;
  221. #endif
  222. }
  223. /*=export_func text_mmap
  224. * private:
  225. *
  226. * what: map a text file with terminating NUL
  227. *
  228. * arg: char const*, pzFile, name of the file to map
  229. * arg: int, prot, mmap protections (see mmap(2))
  230. * arg: int, flags, mmap flags (see mmap(2))
  231. * arg: tmap_info_t*, mapinfo, returned info about the mapping
  232. *
  233. * ret-type: void*
  234. * ret-desc: The mmaped data address
  235. *
  236. * doc:
  237. *
  238. * This routine will mmap a file into memory ensuring that there is at least
  239. * one @file{NUL} character following the file data. It will return the
  240. * address where the file contents have been mapped into memory. If there is a
  241. * problem, then it will return @code{MAP_FAILED} and set @code{errno}
  242. * appropriately.
  243. *
  244. * The named file does not exist, @code{stat(2)} will set @code{errno} as it
  245. * will. If the file is not a regular file, @code{errno} will be
  246. * @code{EINVAL}. At that point, @code{open(2)} is attempted with the access
  247. * bits set appropriately for the requested @code{mmap(2)} protections and flag
  248. * bits. On failure, @code{errno} will be set according to the documentation
  249. * for @code{open(2)}. If @code{mmap(2)} fails, @code{errno} will be set as
  250. * that routine sets it. If @code{text_mmap} works to this point, a valid
  251. * address will be returned, but there may still be ``issues''.
  252. *
  253. * If the file size is not an even multiple of the system page size, then
  254. * @code{text_map} will return at this point and @code{errno} will be zero.
  255. * Otherwise, an anonymous map is attempted. If not available, then an attempt
  256. * is made to @code{mmap(2)} @file{/dev/zero}. If any of these fail, the
  257. * address of the file's data is returned, bug @code{no} @file{NUL} characters
  258. * are mapped after the end of the data.
  259. *
  260. * see: mmap(2), open(2), stat(2)
  261. *
  262. * err: Any error code issued by mmap(2), open(2), stat(2) is possible.
  263. * Additionally, if the specified file is not a regular file, then
  264. * errno will be set to @code{EINVAL}.
  265. *
  266. * example:
  267. * #include <mylib.h>
  268. * tmap_info_t mi;
  269. * int no_nul;
  270. * void* data = text_mmap("file", PROT_WRITE, MAP_PRIVATE, &mi);
  271. * if (data == MAP_FAILED) return;
  272. * no_nul = (mi.txt_size == mi.txt_full_size);
  273. * << use the data >>
  274. * text_munmap(&mi);
  275. =*/
  276. void *
  277. text_mmap(char const * pzFile, int prot, int flags, tmap_info_t * mi)
  278. {
  279. validate_mmap(pzFile, prot, flags, mi);
  280. if (mi->txt_errno != 0)
  281. return MAP_FAILED_PTR;
  282. load_text_file(mi, pzFile);
  283. if (mi->txt_errno == 0)
  284. return mi->txt_data;
  285. close_mmap_files(mi);
  286. errno = mi->txt_errno;
  287. mi->txt_data = MAP_FAILED_PTR;
  288. return mi->txt_data;
  289. }
  290. /*=export_func text_munmap
  291. * private:
  292. *
  293. * what: unmap the data mapped in by text_mmap
  294. *
  295. * arg: tmap_info_t*, mapinfo, info about the mapping
  296. *
  297. * ret-type: int
  298. * ret-desc: -1 or 0. @code{errno} will have the error code.
  299. *
  300. * doc:
  301. *
  302. * This routine will unmap the data mapped in with @code{text_mmap} and close
  303. * the associated file descriptors opened by that function.
  304. *
  305. * see: munmap(2), close(2)
  306. *
  307. * err: Any error code issued by munmap(2) or close(2) is possible.
  308. =*/
  309. int
  310. text_munmap(tmap_info_t * mi)
  311. {
  312. errno = 0;
  313. #ifdef HAVE_MMAP
  314. (void)munmap(mi->txt_data, mi->txt_full_size);
  315. #else /* don't HAVE_MMAP */
  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. Emulate mmap visibility.
  320. */
  321. if ( FILE_WRITABLE(mi->txt_prot, mi->txt_flags)
  322. && (lseek(mi->txt_fd, 0, SEEK_SET) >= 0) ) {
  323. write(mi->txt_fd, mi->txt_data, mi->txt_size);
  324. }
  325. free(mi->txt_data);
  326. #endif /* HAVE_MMAP */
  327. mi->txt_errno = errno;
  328. close_mmap_files(mi);
  329. return mi->txt_errno;
  330. }
  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 */