4 * Map a text file, ensuring the text always has an ending NUL byte.
10 * This file is part of AutoOpts, a companion to AutoGen.
11 * AutoOpts is free software.
12 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
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.
18 * The GNU Lesser General Public License, version 3 or later
19 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
21 * The Modified Berkeley Software Distribution License
22 * See the file "COPYING.mbsd"
24 * These files have the following sha256 sums:
26 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
27 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
28 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
30 #if defined(HAVE_MMAP)
31 # ifndef MAP_ANONYMOUS
33 # define MAP_ANONYMOUS MAP_ANON
37 # if ! defined(MAP_ANONYMOUS) && ! defined(HAVE_DEV_ZERO)
39 * We must have either /dev/zero or anonymous mapping for
46 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
48 # define GETPAGESIZE() getpagesize()
54 * Some weird systems require that a specifically invalid FD number
55 * get passed in as an argument value. Which value is that? Well,
56 * as everybody knows, if open(2) fails, it returns -1, so that must
59 #define AO_INVALID_FD -1
61 #define FILE_WRITABLE(_prt,_flg) \
62 ( (_prt & PROT_WRITE) \
63 && ((_flg & (MAP_SHARED|MAP_PRIVATE)) == MAP_SHARED))
64 #define MAP_FAILED_PTR (VOIDP(MAP_FAILED))
67 * Load the contents of a text file. There are two separate implementations,
68 * depending up on whether mmap(3) is available.
70 * If not available, malloc the file length plus one byte. Read it in
73 * If available, first check to see if the text file size is a multiple of a
74 * page size. If it is, map the file size plus an extra page from either
75 * anonymous memory or from /dev/zero. Then map the file text on top of the
76 * first pages of the anonymous/zero pages. Otherwise, just map the file
77 * because there will be NUL bytes provided at the end.
79 * @param mapinfo a structure holding everything we need to know
82 * @param pzFile name of the file, for error reporting.
85 load_text_file(tmap_info_t * mapinfo, char const * pzFile)
87 #if ! defined(HAVE_MMAP)
88 mapinfo->txt_data = AGALOC(mapinfo->txt_size+1, "file text");
89 if (mapinfo->txt_data == NULL) {
90 mapinfo->txt_errno = ENOMEM;
95 size_t sz = mapinfo->txt_size;
96 char * pz = mapinfo->txt_data;
99 ssize_t rdct = read(mapinfo->txt_fd, pz, sz);
101 mapinfo->txt_errno = errno;
102 fserr_warn("libopts", "read", pzFile);
103 free(mapinfo->txt_data);
114 mapinfo->txt_errno = 0;
116 #else /* HAVE mmap */
117 size_t const pgsz = (size_t)GETPAGESIZE();
118 void * map_addr = NULL;
122 mapinfo->txt_full_size = (mapinfo->txt_size + pgsz) & ~(pgsz - 1);
123 if (mapinfo->txt_full_size == (mapinfo->txt_size + pgsz)) {
125 * The text is a multiple of a page boundary. We must map an
126 * extra page so the text ends with a NUL.
128 #if defined(MAP_ANONYMOUS)
129 map_addr = mmap(NULL, mapinfo->txt_full_size, PROT_READ|PROT_WRITE,
130 MAP_ANONYMOUS|MAP_PRIVATE, AO_INVALID_FD, 0);
132 mapinfo->txt_zero_fd = open("/dev/zero", O_RDONLY);
134 if (mapinfo->txt_zero_fd == AO_INVALID_FD) {
135 mapinfo->txt_errno = errno;
138 map_addr = mmap(NULL, mapinfo->txt_full_size, PROT_READ|PROT_WRITE,
139 MAP_PRIVATE, mapinfo->txt_zero_fd, 0);
141 if (map_addr == MAP_FAILED_PTR) {
142 mapinfo->txt_errno = errno;
145 mapinfo->txt_flags |= MAP_FIXED;
149 mmap(map_addr, mapinfo->txt_size, mapinfo->txt_prot,
150 mapinfo->txt_flags, mapinfo->txt_fd, 0);
152 if (mapinfo->txt_data == MAP_FAILED_PTR)
153 mapinfo->txt_errno = errno;
154 #endif /* HAVE_MMAP */
158 * Make sure all the parameters are correct: we have a file name that
159 * is a text file that we can read.
161 * @param fname the text file to map
162 * @param prot the memory protections requested (read/write/etc.)
163 * @param flags mmap flags
164 * @param mapinfo a structure holding everything we need to know
168 validate_mmap(char const * fname, int prot, int flags, tmap_info_t * mapinfo)
170 memset(mapinfo, 0, sizeof(*mapinfo));
171 #if defined(HAVE_MMAP) && ! defined(MAP_ANONYMOUS)
172 mapinfo->txt_zero_fd = AO_INVALID_FD;
174 mapinfo->txt_fd = AO_INVALID_FD;
175 mapinfo->txt_prot = prot;
176 mapinfo->txt_flags = flags;
179 * Map mmap flags and protections into open flags and do the open.
183 * See if we will be updating the file. If we can alter the memory
184 * and if we share the data and we are *not* copy-on-writing the data,
185 * then our updates will show in the file, so we must open with
188 int o_flag = FILE_WRITABLE(prot, flags) ? O_RDWR : O_RDONLY;
191 * If you're not sharing the file and you are writing to it,
192 * then don't let anyone else have access to the file.
194 if (((flags & MAP_SHARED) == 0) && (prot & PROT_WRITE))
197 mapinfo->txt_fd = open(fname, o_flag);
198 if (mapinfo->txt_fd < 0) {
199 mapinfo->txt_errno = errno;
200 mapinfo->txt_fd = AO_INVALID_FD;
206 * Make sure we can stat the regular file. Save the file size.
210 if (fstat(mapinfo->txt_fd, &sb) != 0) {
211 mapinfo->txt_errno = errno;
212 close(mapinfo->txt_fd);
216 if (! S_ISREG(sb.st_mode)) {
217 mapinfo->txt_errno = errno = EINVAL;
218 close(mapinfo->txt_fd);
222 mapinfo->txt_size = (size_t)sb.st_size;
225 if (mapinfo->txt_fd == AO_INVALID_FD)
226 mapinfo->txt_errno = errno;
230 * Close any files opened by the mapping.
232 * @param mi a structure holding everything we need to know about the map.
235 close_mmap_files(tmap_info_t * mi)
237 if (mi->txt_fd == AO_INVALID_FD)
241 mi->txt_fd = AO_INVALID_FD;
243 #if defined(HAVE_MMAP) && ! defined(MAP_ANONYMOUS)
244 if (mi->txt_zero_fd == AO_INVALID_FD)
247 close(mi->txt_zero_fd);
248 mi->txt_zero_fd = AO_INVALID_FD;
252 /*=export_func text_mmap
255 * what: map a text file with terminating NUL
257 * arg: char const *, pzFile, name of the file to map
258 * arg: int, prot, mmap protections (see mmap(2))
259 * arg: int, flags, mmap flags (see mmap(2))
260 * arg: tmap_info_t *, mapinfo, returned info about the mapping
263 * ret-desc: The mmaped data address
267 * This routine will mmap a file into memory ensuring that there is at least
268 * one @file{NUL} character following the file data. It will return the
269 * address where the file contents have been mapped into memory. If there is a
270 * problem, then it will return @code{MAP_FAILED} and set @code{errno}
273 * The named file does not exist, @code{stat(2)} will set @code{errno} as it
274 * will. If the file is not a regular file, @code{errno} will be
275 * @code{EINVAL}. At that point, @code{open(2)} is attempted with the access
276 * bits set appropriately for the requested @code{mmap(2)} protections and flag
277 * bits. On failure, @code{errno} will be set according to the documentation
278 * for @code{open(2)}. If @code{mmap(2)} fails, @code{errno} will be set as
279 * that routine sets it. If @code{text_mmap} works to this point, a valid
280 * address will be returned, but there may still be ``issues''.
282 * If the file size is not an even multiple of the system page size, then
283 * @code{text_map} will return at this point and @code{errno} will be zero.
284 * Otherwise, an anonymous map is attempted. If not available, then an attempt
285 * is made to @code{mmap(2)} @file{/dev/zero}. If any of these fail, the
286 * address of the file's data is returned, bug @code{no} @file{NUL} characters
287 * are mapped after the end of the data.
289 * see: mmap(2), open(2), stat(2)
291 * err: Any error code issued by mmap(2), open(2), stat(2) is possible.
292 * Additionally, if the specified file is not a regular file, then
293 * errno will be set to @code{EINVAL}.
299 * void * data = text_mmap("file", PROT_WRITE, MAP_PRIVATE, &mi);
300 * if (data == MAP_FAILED) return;
301 * no_nul = (mi.txt_size == mi.txt_full_size);
306 text_mmap(char const * pzFile, int prot, int flags, tmap_info_t * mi)
308 validate_mmap(pzFile, prot, flags, mi);
309 if (mi->txt_errno != 0)
310 return MAP_FAILED_PTR;
312 load_text_file(mi, pzFile);
314 if (mi->txt_errno == 0)
317 close_mmap_files(mi);
319 errno = mi->txt_errno;
320 mi->txt_data = MAP_FAILED_PTR;
325 /*=export_func text_munmap
328 * what: unmap the data mapped in by text_mmap
330 * arg: tmap_info_t *, mapinfo, info about the mapping
333 * ret-desc: -1 or 0. @code{errno} will have the error code.
337 * This routine will unmap the data mapped in with @code{text_mmap} and close
338 * the associated file descriptors opened by that function.
340 * see: munmap(2), close(2)
342 * err: Any error code issued by munmap(2) or close(2) is possible.
345 text_munmap(tmap_info_t * mi)
350 (void)munmap(mi->txt_data, mi->txt_full_size);
352 #else /* don't HAVE_MMAP */
354 * IF the memory is writable *AND* it is not private (copy-on-write)
355 * *AND* the memory is "sharable" (seen by other processes)
356 * THEN rewrite the data. Emulate mmap visibility.
358 if ( FILE_WRITABLE(mi->txt_prot, mi->txt_flags)
359 && (lseek(mi->txt_fd, 0, SEEK_SET) >= 0) ) {
360 write(mi->txt_fd, mi->txt_data, mi->txt_size);
364 #endif /* HAVE_MMAP */
366 mi->txt_errno = errno;
367 close_mmap_files(mi);
369 return mi->txt_errno;
376 * c-file-style: "stroustrup"
377 * indent-tabs-mode: nil
379 * end of autoopts/text_mmap.c */