text_mmap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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-2018 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 (VOIDP(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 =
  171. #ifdef _WIN32
  172. O_BINARY |
  173. #endif
  174. FILE_WRITABLE(prot, flags) ? O_RDWR : O_RDONLY;
  175. /*
  176. * If you're not sharing the file and you are writing to it,
  177. * then don't let anyone else have access to the file.
  178. */
  179. if (((flags & MAP_SHARED) == 0) && (prot & PROT_WRITE))
  180. o_flag |= O_EXCL;
  181. mapinfo->txt_fd = open(fname, o_flag);
  182. if (mapinfo->txt_fd < 0) {
  183. mapinfo->txt_errno = errno;
  184. mapinfo->txt_fd = AO_INVALID_FD;
  185. return;
  186. }
  187. }
  188. /*
  189. * Make sure we can stat the regular file. Save the file size.
  190. */
  191. {
  192. struct stat sb;
  193. if (fstat(mapinfo->txt_fd, &sb) != 0) {
  194. mapinfo->txt_errno = errno;
  195. close(mapinfo->txt_fd);
  196. return;
  197. }
  198. if (! S_ISREG(sb.st_mode)) {
  199. mapinfo->txt_errno = errno = EINVAL;
  200. close(mapinfo->txt_fd);
  201. return;
  202. }
  203. mapinfo->txt_size = (size_t)sb.st_size;
  204. }
  205. if (mapinfo->txt_fd == AO_INVALID_FD)
  206. mapinfo->txt_errno = errno;
  207. }
  208. /**
  209. * Close any files opened by the mapping.
  210. *
  211. * @param mi a structure holding everything we need to know about the map.
  212. */
  213. static void
  214. close_mmap_files(tmap_info_t * mi)
  215. {
  216. if (mi->txt_fd == AO_INVALID_FD)
  217. return;
  218. close(mi->txt_fd);
  219. mi->txt_fd = AO_INVALID_FD;
  220. #if defined(HAVE_MMAP) && ! defined(MAP_ANONYMOUS)
  221. if (mi->txt_zero_fd == AO_INVALID_FD)
  222. return;
  223. close(mi->txt_zero_fd);
  224. mi->txt_zero_fd = AO_INVALID_FD;
  225. #endif
  226. }
  227. /*=export_func text_mmap
  228. * private:
  229. *
  230. * what: map a text file with terminating NUL
  231. *
  232. * arg: char const *, pzFile, name of the file to map
  233. * arg: int, prot, mmap protections (see mmap(2))
  234. * arg: int, flags, mmap flags (see mmap(2))
  235. * arg: tmap_info_t *, mapinfo, returned info about the mapping
  236. *
  237. * ret-type: void *
  238. * ret-desc: The mmaped data address
  239. *
  240. * doc:
  241. *
  242. * This routine will mmap a file into memory ensuring that there is at least
  243. * one @file{NUL} character following the file data. It will return the
  244. * address where the file contents have been mapped into memory. If there is a
  245. * problem, then it will return @code{MAP_FAILED} and set @code{errno}
  246. * appropriately.
  247. *
  248. * The named file does not exist, @code{stat(2)} will set @code{errno} as it
  249. * will. If the file is not a regular file, @code{errno} will be
  250. * @code{EINVAL}. At that point, @code{open(2)} is attempted with the access
  251. * bits set appropriately for the requested @code{mmap(2)} protections and flag
  252. * bits. On failure, @code{errno} will be set according to the documentation
  253. * for @code{open(2)}. If @code{mmap(2)} fails, @code{errno} will be set as
  254. * that routine sets it. If @code{text_mmap} works to this point, a valid
  255. * address will be returned, but there may still be ``issues''.
  256. *
  257. * If the file size is not an even multiple of the system page size, then
  258. * @code{text_map} will return at this point and @code{errno} will be zero.
  259. * Otherwise, an anonymous map is attempted. If not available, then an attempt
  260. * is made to @code{mmap(2)} @file{/dev/zero}. If any of these fail, the
  261. * address of the file's data is returned, bug @code{no} @file{NUL} characters
  262. * are mapped after the end of the data.
  263. *
  264. * see: mmap(2), open(2), stat(2)
  265. *
  266. * err: Any error code issued by mmap(2), open(2), stat(2) is possible.
  267. * Additionally, if the specified file is not a regular file, then
  268. * errno will be set to @code{EINVAL}.
  269. *
  270. * example:
  271. * #include <mylib.h>
  272. * tmap_info_t mi;
  273. * int no_nul;
  274. * void * data = text_mmap("file", PROT_WRITE, MAP_PRIVATE, &mi);
  275. * if (data == MAP_FAILED) return;
  276. * no_nul = (mi.txt_size == mi.txt_full_size);
  277. * << use the data >>
  278. * text_munmap(&mi);
  279. =*/
  280. void *
  281. text_mmap(char const * pzFile, int prot, int flags, tmap_info_t * mi)
  282. {
  283. validate_mmap(pzFile, prot, flags, mi);
  284. if (mi->txt_errno != 0)
  285. return MAP_FAILED_PTR;
  286. load_text_file(mi, pzFile);
  287. if (mi->txt_errno == 0)
  288. return mi->txt_data;
  289. close_mmap_files(mi);
  290. errno = mi->txt_errno;
  291. mi->txt_data = MAP_FAILED_PTR;
  292. return mi->txt_data;
  293. }
  294. /*=export_func text_munmap
  295. * private:
  296. *
  297. * what: unmap the data mapped in by text_mmap
  298. *
  299. * arg: tmap_info_t *, mapinfo, info about the mapping
  300. *
  301. * ret-type: int
  302. * ret-desc: -1 or 0. @code{errno} will have the error code.
  303. *
  304. * doc:
  305. *
  306. * This routine will unmap the data mapped in with @code{text_mmap} and close
  307. * the associated file descriptors opened by that function.
  308. *
  309. * see: munmap(2), close(2)
  310. *
  311. * err: Any error code issued by munmap(2) or close(2) is possible.
  312. =*/
  313. int
  314. text_munmap(tmap_info_t * mi)
  315. {
  316. errno = 0;
  317. #ifdef HAVE_MMAP
  318. (void)munmap(mi->txt_data, mi->txt_full_size);
  319. #else // don't HAVE_MMAP
  320. /*
  321. * IF the memory is writable *AND* it is not private (copy-on-write)
  322. * *AND* the memory is "sharable" (seen by other processes)
  323. * THEN rewrite the data. Emulate mmap visibility.
  324. */
  325. if ( FILE_WRITABLE(mi->txt_prot, mi->txt_flags)
  326. && (lseek(mi->txt_fd, 0, SEEK_SET) >= 0) )
  327. write(mi->txt_fd, mi->txt_data, mi->txt_size);
  328. free(mi->txt_data);
  329. #endif /* HAVE_MMAP */
  330. mi->txt_errno = errno;
  331. close_mmap_files(mi);
  332. return mi->txt_errno;
  333. }
  334. /** @}
  335. *
  336. * Local Variables:
  337. * mode: C
  338. * c-file-style: "stroustrup"
  339. * indent-tabs-mode: nil
  340. * End:
  341. * end of autoopts/text_mmap.c */