aboutsummaryrefslogtreecommitdiff
path: root/sntp/libopts/text_mmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'sntp/libopts/text_mmap.c')
-rw-r--r--sntp/libopts/text_mmap.c504
1 files changed, 250 insertions, 254 deletions
diff --git a/sntp/libopts/text_mmap.c b/sntp/libopts/text_mmap.c
index 01aa7d821255..f30e71c6c9c3 100644
--- a/sntp/libopts/text_mmap.c
+++ b/sntp/libopts/text_mmap.c
@@ -1,11 +1,15 @@
/**
- * \file text_mmap.c
+ * @file text_mmap.c
*
- * Time-stamp: "2010-07-17 10:15:32 bkorb"
+ * Map a text file, ensuring the text always has an ending NUL byte.
*
+ * @addtogroup autoopts
+ * @{
+ */
+/*
* This file is part of AutoOpts, a companion to AutoGen.
* AutoOpts is free software.
- * AutoOpts is Copyright (c) 1992-2011 by Bruce Korb - all rights reserved
+ * AutoOpts is Copyright (C) 1992-2014 by Bruce Korb - all rights reserved
*
* AutoOpts is available under any one of two licenses. The license
* in use must be one of these two and the choice is under the control
@@ -17,16 +21,32 @@
* The Modified Berkeley Software Distribution License
* See the file "COPYING.mbsd"
*
- * These files have the following md5sums:
+ * These files have the following sha256 sums:
*
- * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
- * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
- * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
+ * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
+ * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
+ * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
*/
+#if defined(HAVE_MMAP)
+# ifndef MAP_ANONYMOUS
+# ifdef MAP_ANON
+# define MAP_ANONYMOUS MAP_ANON
+# endif
+# endif
-#ifndef MAP_ANONYMOUS
-# ifdef MAP_ANON
-# define MAP_ANONYMOUS MAP_ANON
+# if ! defined(MAP_ANONYMOUS) && ! defined(HAVE_DEV_ZERO)
+ /*
+ * We must have either /dev/zero or anonymous mapping for
+ * this to work.
+ */
+# undef HAVE_MMAP
+
+# else
+# ifdef _SC_PAGESIZE
+# define GETPAGESIZE() sysconf(_SC_PAGESIZE)
+# else
+# define GETPAGESIZE() getpagesize()
+# endif
# endif
#endif
@@ -43,6 +63,192 @@
&& ((_flg & (MAP_SHARED|MAP_PRIVATE)) == MAP_SHARED))
#define MAP_FAILED_PTR ((void*)MAP_FAILED)
+/**
+ * Load the contents of a text file. There are two separate implementations,
+ * depending up on whether mmap(3) is available.
+ *
+ * If not available, malloc the file length plus one byte. Read it in
+ * and NUL terminate.
+ *
+ * If available, first check to see if the text file size is a multiple of a
+ * page size. If it is, map the file size plus an extra page from either
+ * anonymous memory or from /dev/zero. Then map the file text on top of the
+ * first pages of the anonymous/zero pages. Otherwise, just map the file
+ * because there will be NUL bytes provided at the end.
+ *
+ * @param mapinfo a structure holding everything we need to know
+ * about the mapping.
+ *
+ * @param pzFile name of the file, for error reporting.
+ */
+static void
+load_text_file(tmap_info_t * mapinfo, char const * pzFile)
+{
+#if ! defined(HAVE_MMAP)
+ mapinfo->txt_data = AGALOC(mapinfo->txt_size+1, "file text");
+ if (mapinfo->txt_data == NULL) {
+ mapinfo->txt_errno = ENOMEM;
+ return;
+ }
+
+ {
+ size_t sz = mapinfo->txt_size;
+ char* pz = mapinfo->txt_data;
+
+ while (sz > 0) {
+ ssize_t rdct = read(mapinfo->txt_fd, pz, sz);
+ if (rdct <= 0) {
+ mapinfo->txt_errno = errno;
+ fserr_warn("libopts", "read", pzFile);
+ free(mapinfo->txt_data);
+ return;
+ }
+
+ pz += rdct;
+ sz -= rdct;
+ }
+
+ *pz = NUL;
+ }
+
+ mapinfo->txt_errno = 0;
+
+#else /* HAVE mmap */
+ size_t const pgsz = (size_t)GETPAGESIZE();
+ void * map_addr = NULL;
+
+ (void)pzFile;
+
+ mapinfo->txt_full_size = (mapinfo->txt_size + pgsz) & ~(pgsz - 1);
+ if (mapinfo->txt_full_size == (mapinfo->txt_size + pgsz)) {
+ /*
+ * The text is a multiple of a page boundary. We must map an
+ * extra page so the text ends with a NUL.
+ */
+#if defined(MAP_ANONYMOUS)
+ map_addr = mmap(NULL, mapinfo->txt_full_size, PROT_READ|PROT_WRITE,
+ MAP_ANONYMOUS|MAP_PRIVATE, AO_INVALID_FD, 0);
+#else
+ mapinfo->txt_zero_fd = open("/dev/zero", O_RDONLY);
+
+ if (mapinfo->txt_zero_fd == AO_INVALID_FD) {
+ mapinfo->txt_errno = errno;
+ return;
+ }
+ map_addr = mmap(NULL, mapinfo->txt_full_size, PROT_READ|PROT_WRITE,
+ MAP_PRIVATE, mapinfo->txt_zero_fd, 0);
+#endif
+ if (map_addr == MAP_FAILED_PTR) {
+ mapinfo->txt_errno = errno;
+ return;
+ }
+ mapinfo->txt_flags |= MAP_FIXED;
+ }
+
+ mapinfo->txt_data =
+ mmap(map_addr, mapinfo->txt_size, mapinfo->txt_prot,
+ mapinfo->txt_flags, mapinfo->txt_fd, 0);
+
+ if (mapinfo->txt_data == MAP_FAILED_PTR)
+ mapinfo->txt_errno = errno;
+#endif /* HAVE_MMAP */
+}
+
+/**
+ * Make sure all the parameters are correct: we have a file name that
+ * is a text file that we can read.
+ *
+ * @param fname the text file to map
+ * @param prot the memory protections requested (read/write/etc.)
+ * @param flags mmap flags
+ * @param mapinfo a structure holding everything we need to know
+ * about the mapping.
+ */
+static void
+validate_mmap(char const * fname, int prot, int flags, tmap_info_t * mapinfo)
+{
+ memset(mapinfo, 0, sizeof(*mapinfo));
+#if defined(HAVE_MMAP) && ! defined(MAP_ANONYMOUS)
+ mapinfo->txt_zero_fd = AO_INVALID_FD;
+#endif
+ mapinfo->txt_fd = AO_INVALID_FD;
+ mapinfo->txt_prot = prot;
+ mapinfo->txt_flags = flags;
+
+ /*
+ * Map mmap flags and protections into open flags and do the open.
+ */
+ {
+ /*
+ * See if we will be updating the file. If we can alter the memory
+ * and if we share the data and we are *not* copy-on-writing the data,
+ * then our updates will show in the file, so we must open with
+ * write access.
+ */
+ int o_flag = FILE_WRITABLE(prot, flags) ? O_RDWR : O_RDONLY;
+
+ /*
+ * If you're not sharing the file and you are writing to it,
+ * then don't let anyone else have access to the file.
+ */
+ if (((flags & MAP_SHARED) == 0) && (prot & PROT_WRITE))
+ o_flag |= O_EXCL;
+
+ mapinfo->txt_fd = open(fname, o_flag);
+ if (mapinfo->txt_fd < 0) {
+ mapinfo->txt_errno = errno;
+ mapinfo->txt_fd = AO_INVALID_FD;
+ return;
+ }
+ }
+
+ /*
+ * Make sure we can stat the regular file. Save the file size.
+ */
+ {
+ struct stat sb;
+ if (fstat(mapinfo->txt_fd, &sb) != 0) {
+ mapinfo->txt_errno = errno;
+ close(mapinfo->txt_fd);
+ return;
+ }
+
+ if (! S_ISREG(sb.st_mode)) {
+ mapinfo->txt_errno = errno = EINVAL;
+ close(mapinfo->txt_fd);
+ return;
+ }
+
+ mapinfo->txt_size = (size_t)sb.st_size;
+ }
+
+ if (mapinfo->txt_fd == AO_INVALID_FD)
+ mapinfo->txt_errno = errno;
+}
+
+/**
+ * Close any files opened by the mapping.
+ *
+ * @param mi a structure holding everything we need to know about the map.
+ */
+static void
+close_mmap_files(tmap_info_t * mi)
+{
+ if (mi->txt_fd == AO_INVALID_FD)
+ return;
+
+ close(mi->txt_fd);
+ mi->txt_fd = AO_INVALID_FD;
+
+#if defined(HAVE_MMAP) && ! defined(MAP_ANONYMOUS)
+ if (mi->txt_zero_fd == AO_INVALID_FD)
+ return;
+
+ close(mi->txt_zero_fd);
+ mi->txt_zero_fd = AO_INVALID_FD;
+#endif
+}
+
/*=export_func text_mmap
* private:
*
@@ -61,20 +267,20 @@
* This routine will mmap a file into memory ensuring that there is at least
* one @file{NUL} character following the file data. It will return the
* address where the file contents have been mapped into memory. If there is a
- * problem, then it will return @code{MAP_FAILED} and set @file{errno}
+ * problem, then it will return @code{MAP_FAILED} and set @code{errno}
* appropriately.
*
- * The named file does not exist, @code{stat(2)} will set @file{errno} as it
- * will. If the file is not a regular file, @file{errno} will be
+ * The named file does not exist, @code{stat(2)} will set @code{errno} as it
+ * will. If the file is not a regular file, @code{errno} will be
* @code{EINVAL}. At that point, @code{open(2)} is attempted with the access
* bits set appropriately for the requested @code{mmap(2)} protections and flag
- * bits. On failure, @file{errno} will be set according to the documentation
- * for @code{open(2)}. If @code{mmap(2)} fails, @file{errno} will be set as
+ * bits. On failure, @code{errno} will be set according to the documentation
+ * for @code{open(2)}. If @code{mmap(2)} fails, @code{errno} will be set as
* that routine sets it. If @code{text_mmap} works to this point, a valid
* address will be returned, but there may still be ``issues''.
*
* If the file size is not an even multiple of the system page size, then
- * @code{text_map} will return at this point and @file{errno} will be zero.
+ * @code{text_map} will return at this point and @code{errno} will be zero.
* Otherwise, an anonymous map is attempted. If not available, then an attempt
* is made to @code{mmap(2)} @file{/dev/zero}. If any of these fail, the
* address of the file's data is returned, bug @code{no} @file{NUL} characters
@@ -96,197 +302,23 @@
* << use the data >>
* text_munmap(&mi);
=*/
-void*
-text_mmap(char const* pzFile, int prot, int flags, tmap_info_t* pMI)
+void *
+text_mmap(char const * pzFile, int prot, int flags, tmap_info_t * mi)
{
- memset(pMI, 0, sizeof(*pMI));
-#ifdef HAVE_MMAP
- pMI->txt_zero_fd = -1;
-#endif
- pMI->txt_fd = -1;
-
- /*
- * Make sure we can stat the regular file. Save the file size.
- */
- {
- struct stat sb;
- if (stat(pzFile, &sb) != 0) {
- pMI->txt_errno = errno;
- return MAP_FAILED_PTR;
- }
-
- if (! S_ISREG(sb.st_mode)) {
- pMI->txt_errno = errno = EINVAL;
- return MAP_FAILED_PTR;
- }
-
- pMI->txt_size = sb.st_size;
- }
-
- /*
- * Map mmap flags and protections into open flags and do the open.
- */
- {
- int o_flag;
- /*
- * See if we will be updating the file. If we can alter the memory
- * and if we share the data and we are *not* copy-on-writing the data,
- * then our updates will show in the file, so we must open with
- * write access.
- */
- if (FILE_WRITABLE(prot,flags))
- o_flag = O_RDWR;
- else
- o_flag = O_RDONLY;
-
- /*
- * If you're not sharing the file and you are writing to it,
- * then don't let anyone else have access to the file.
- */
- if (((flags & MAP_SHARED) == 0) && (prot & PROT_WRITE))
- o_flag |= O_EXCL;
-
- pMI->txt_fd = open(pzFile, o_flag);
- }
-
- if (pMI->txt_fd == AO_INVALID_FD) {
- pMI->txt_errno = errno;
+ validate_mmap(pzFile, prot, flags, mi);
+ if (mi->txt_errno != 0)
return MAP_FAILED_PTR;
- }
-
-#ifdef HAVE_MMAP /* * * * * WITH MMAP * * * * * */
- /*
- * do the mmap. If we fail, then preserve errno, close the file and
- * return the failure.
- */
- pMI->txt_data =
- mmap(NULL, pMI->txt_size+1, prot, flags, pMI->txt_fd, (size_t)0);
- if (pMI->txt_data == MAP_FAILED_PTR) {
- pMI->txt_errno = errno;
- goto fail_return;
- }
-
- /*
- * Most likely, everything will turn out fine now. The only difficult
- * part at this point is coping with files with sizes that are a multiple
- * of the page size. Handling that is what this whole thing is about.
- */
- pMI->txt_zero_fd = -1;
- pMI->txt_errno = 0;
-
- {
- void* pNuls;
-#ifdef _SC_PAGESIZE
- size_t pgsz = sysconf(_SC_PAGESIZE);
-#else
- size_t pgsz = getpagesize();
-#endif
- /*
- * Compute the pagesize rounded mapped memory size.
- * IF this is not the same as the file size, then there are NUL's
- * at the end of the file mapping and all is okay.
- */
- pMI->txt_full_size = (pMI->txt_size + (pgsz - 1)) & ~(pgsz - 1);
- if (pMI->txt_size != pMI->txt_full_size)
- return pMI->txt_data;
-
- /*
- * Still here? We have to remap the trailing inaccessible page
- * either anonymously or to /dev/zero.
- */
- pMI->txt_full_size += pgsz;
-#if defined(MAP_ANONYMOUS)
- pNuls = mmap(
- (void*)(((char*)pMI->txt_data) + pMI->txt_size),
- pgsz, PROT_READ|PROT_WRITE,
- MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE, AO_INVALID_FD, (size_t)0);
-
- if (pNuls != MAP_FAILED_PTR)
- return pMI->txt_data;
-
- pMI->txt_errno = errno;
-#elif defined(HAVE_DEV_ZERO)
- pMI->txt_zero_fd = open("/dev/zero", O_RDONLY);
+ load_text_file(mi, pzFile);
- if (pMI->txt_zero_fd == AO_INVALID_FD) {
- pMI->txt_errno = errno;
+ if (mi->txt_errno == 0)
+ return mi->txt_data;
- } else {
- pNuls = mmap(
- (void*)(((char*)pMI->txt_data) + pMI->txt_size), pgsz,
- PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED,
- pMI->txt_zero_fd, 0 );
+ close_mmap_files(mi);
- if (pNuls != MAP_FAILED_PTR)
- return pMI->txt_data;
-
- pMI->txt_errno = errno;
- close(pMI->txt_zero_fd);
- pMI->txt_zero_fd = -1;
- }
-#endif
-
- pMI->txt_full_size = pMI->txt_size;
- }
-
- {
- void* p = AGALOC(pMI->txt_size+1, "file text");
- memcpy(p, pMI->txt_data, pMI->txt_size);
- ((char*)p)[pMI->txt_size] = NUL;
- munmap(pMI->txt_data, pMI->txt_size );
- pMI->txt_data = p;
- }
- pMI->txt_alloc = 1;
- return pMI->txt_data;
-
-#else /* * * * * * no HAVE_MMAP * * * * * */
-
- pMI->txt_data = AGALOC(pMI->txt_size+1, "file text");
- if (pMI->txt_data == NULL) {
- pMI->txt_errno = ENOMEM;
- goto fail_return;
- }
-
- {
- size_t sz = pMI->txt_size;
- char* pz = pMI->txt_data;
-
- while (sz > 0) {
- ssize_t rdct = read(pMI->txt_fd, pz, sz);
- if (rdct <= 0) {
- pMI->txt_errno = errno;
- fprintf(stderr, zFSErrReadFile,
- errno, strerror(errno), pzFile);
- free(pMI->txt_data);
- goto fail_return;
- }
-
- pz += rdct;
- sz -= rdct;
- }
-
- *pz = NUL;
- }
-
- /*
- * We never need a dummy page mapped in
- */
- pMI->txt_zero_fd = -1;
- pMI->txt_errno = 0;
-
- return pMI->txt_data;
-
-#endif /* * * * * * no HAVE_MMAP * * * * * */
-
- fail_return:
- if (pMI->txt_fd >= 0) {
- close(pMI->txt_fd);
- pMI->txt_fd = -1;
- }
- errno = pMI->txt_errno;
- pMI->txt_data = MAP_FAILED_PTR;
- return pMI->txt_data;
+ errno = mi->txt_errno;
+ mi->txt_data = MAP_FAILED_PTR;
+ return mi->txt_data;
}
@@ -298,7 +330,7 @@ text_mmap(char const* pzFile, int prot, int flags, tmap_info_t* pMI)
* arg: tmap_info_t*, mapinfo, info about the mapping
*
* ret-type: int
- * ret-desc: -1 or 0. @file{errno} will have the error code.
+ * ret-desc: -1 or 0. @code{errno} will have the error code.
*
* doc:
*
@@ -310,71 +342,35 @@ text_mmap(char const* pzFile, int prot, int flags, tmap_info_t* pMI)
* err: Any error code issued by munmap(2) or close(2) is possible.
=*/
int
-text_munmap(tmap_info_t* pMI)
+text_munmap(tmap_info_t * mi)
{
-#ifdef HAVE_MMAP
- int res = 0;
- if (pMI->txt_alloc) {
- /*
- * IF the user has write permission and the text is not mapped private,
- * then write back any changes. Hopefully, nobody else has modified
- * the file in the mean time.
- */
- if ( ((pMI->txt_prot & PROT_WRITE) != 0)
- && ((pMI->txt_flags & MAP_PRIVATE) == 0)) {
-
- if (lseek(pMI->txt_fd, (size_t)0, SEEK_SET) != 0)
- goto error_return;
-
- res = (write(pMI->txt_fd, pMI->txt_data, pMI->txt_size) < 0)
- ? errno : 0;
- }
-
- AGFREE(pMI->txt_data);
- errno = res;
- } else {
- res = munmap(pMI->txt_data, pMI->txt_full_size);
- }
- if (res != 0)
- goto error_return;
-
- res = close(pMI->txt_fd);
- if (res != 0)
- goto error_return;
-
- pMI->txt_fd = -1;
errno = 0;
- if (pMI->txt_zero_fd != -1) {
- res = close(pMI->txt_zero_fd);
- pMI->txt_zero_fd = -1;
- }
- error_return:
- pMI->txt_errno = errno;
- return res;
-#else /* HAVE_MMAP */
+#ifdef HAVE_MMAP
+ (void)munmap(mi->txt_data, mi->txt_full_size);
- errno = 0;
+#else /* don't HAVE_MMAP */
/*
* IF the memory is writable *AND* it is not private (copy-on-write)
* *AND* the memory is "sharable" (seen by other processes)
- * THEN rewrite the data.
+ * THEN rewrite the data. Emulate mmap visibility.
*/
- if ( FILE_WRITABLE(pMI->txt_prot, pMI->txt_flags)
- && (lseek(pMI->txt_fd, 0, SEEK_SET) >= 0) ) {
- write(pMI->txt_fd, pMI->txt_data, pMI->txt_size);
+ if ( FILE_WRITABLE(mi->txt_prot, mi->txt_flags)
+ && (lseek(mi->txt_fd, 0, SEEK_SET) >= 0) ) {
+ write(mi->txt_fd, mi->txt_data, mi->txt_size);
}
- close(pMI->txt_fd);
- pMI->txt_fd = -1;
- pMI->txt_errno = errno;
- free(pMI->txt_data);
-
- return pMI->txt_errno;
+ free(mi->txt_data);
#endif /* HAVE_MMAP */
+
+ mi->txt_errno = errno;
+ close_mmap_files(mi);
+
+ return mi->txt_errno;
}
-/*
+/** @}
+ *
* Local Variables:
* mode: C
* c-file-style: "stroustrup"