2 * $Id: text_mmap.c,v 4.15 2006/11/27 01:52:23 bkorb Exp $
4 * Time-stamp: "2006-09-10 14:50:04 bkorb"
9 # define MAP_ANONYMOUS MAP_ANON
14 * Some weird systems require that a specifically invalid FD number
15 * get passed in as an argument value. Which value is that? Well,
16 * as everybody knows, if open(2) fails, it returns -1, so that must
19 #define AO_INVALID_FD -1
21 #define FILE_WRITABLE(_prt,_flg) \
22 ( (_prt & PROT_WRITE) \
23 && ((_flg & (MAP_SHARED|MAP_PRIVATE)) == MAP_SHARED))
24 #define MAP_FAILED_PTR ((void*)MAP_FAILED)
26 /*=export_func text_mmap
29 * what: map a text file with terminating NUL
31 * arg: char const*, pzFile, name of the file to map
32 * arg: int, prot, mmap protections (see mmap(2))
33 * arg: int, flags, mmap flags (see mmap(2))
34 * arg: tmap_info_t*, mapinfo, returned info about the mapping
37 * ret-desc: The mmaped data address
41 * This routine will mmap a file into memory ensuring that there is at least
42 * one @file{NUL} character following the file data. It will return the
43 * address where the file contents have been mapped into memory. If there is a
44 * problem, then it will return @code{MAP_FAILED} and set @file{errno}
47 * The named file does not exist, @code{stat(2)} will set @file{errno} as it
48 * will. If the file is not a regular file, @file{errno} will be
49 * @code{EINVAL}. At that point, @code{open(2)} is attempted with the access
50 * bits set appropriately for the requested @code{mmap(2)} protections and flag
51 * bits. On failure, @file{errno} will be set according to the documentation
52 * for @code{open(2)}. If @code{mmap(2)} fails, @file{errno} will be set as
53 * that routine sets it. If @code{text_mmap} works to this point, a valid
54 * address will be returned, but there may still be ``issues''.
56 * If the file size is not an even multiple of the system page size, then
57 * @code{text_map} will return at this point and @file{errno} will be zero.
58 * Otherwise, an anonymous map is attempted. If not available, then an attempt
59 * is made to @code{mmap(2)} @file{/dev/zero}. If any of these fail, the
60 * address of the file's data is returned, bug @code{no} @file{NUL} characters
61 * are mapped after the end of the data.
63 * see: mmap(2), open(2), stat(2)
65 * err: Any error code issued by mmap(2), open(2), stat(2) is possible.
66 * Additionally, if the specified file is not a regular file, then
67 * errno will be set to @code{EINVAL}.
73 * void* data = text_mmap( "file", PROT_WRITE, MAP_PRIVATE, &mi );
74 * if (data == MAP_FAILED) return;
75 * no_nul = (mi.txt_size == mi.txt_full_size);
80 text_mmap( char const* pzFile, int prot, int flags, tmap_info_t* pMI )
82 memset( pMI, 0, sizeof(*pMI) );
84 pMI->txt_zero_fd = -1;
89 * Make sure we can stat the regular file. Save the file size.
93 if (stat( pzFile, &sb ) != 0) {
94 pMI->txt_errno = errno;
95 return MAP_FAILED_PTR;
98 if (! S_ISREG( sb.st_mode )) {
99 pMI->txt_errno = errno = EINVAL;
100 return MAP_FAILED_PTR;
103 pMI->txt_size = sb.st_size;
107 * Map mmap flags and protections into open flags and do the open.
112 * See if we will be updating the file. If we can alter the memory
113 * and if we share the data and we are *not* copy-on-writing the data,
114 * then our updates will show in the file, so we must open with
117 if (FILE_WRITABLE(prot,flags))
123 * If you're not sharing the file and you are writing to it,
124 * then don't let anyone else have access to the file.
126 if (((flags & MAP_SHARED) == 0) && (prot & PROT_WRITE))
129 pMI->txt_fd = open( pzFile, o_flag );
132 if (pMI->txt_fd == AO_INVALID_FD) {
133 pMI->txt_errno = errno;
134 return MAP_FAILED_PTR;
137 #ifdef HAVE_MMAP /* * * * * WITH MMAP * * * * * */
139 * do the mmap. If we fail, then preserve errno, close the file and
140 * return the failure.
143 mmap(NULL, pMI->txt_size+1, prot, flags, pMI->txt_fd, (size_t)0);
144 if (pMI->txt_data == MAP_FAILED_PTR) {
145 pMI->txt_errno = errno;
150 * Most likely, everything will turn out fine now. The only difficult
151 * part at this point is coping with files with sizes that are a multiple
152 * of the page size. Handling that is what this whole thing is about.
154 pMI->txt_zero_fd = -1;
160 size_t pgsz = sysconf(_SC_PAGESIZE);
162 size_t pgsz = getpagesize();
165 * Compute the pagesize rounded mapped memory size.
166 * IF this is not the same as the file size, then there are NUL's
167 * at the end of the file mapping and all is okay.
169 pMI->txt_full_size = (pMI->txt_size + (pgsz - 1)) & ~(pgsz - 1);
170 if (pMI->txt_size != pMI->txt_full_size)
171 return pMI->txt_data;
174 * Still here? We have to remap the trailing inaccessible page
175 * either anonymously or to /dev/zero.
177 pMI->txt_full_size += pgsz;
178 #if defined(MAP_ANONYMOUS)
180 (void*)(((char*)pMI->txt_data) + pMI->txt_size),
181 pgsz, PROT_READ|PROT_WRITE,
182 MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE, AO_INVALID_FD, (size_t)0);
184 if (pNuls != MAP_FAILED_PTR)
185 return pMI->txt_data;
187 pMI->txt_errno = errno;
189 #elif defined(HAVE_DEV_ZERO)
190 pMI->txt_zero_fd = open( "/dev/zero", O_RDONLY );
192 if (pMI->txt_zero_fd == AO_INVALID_FD) {
193 pMI->txt_errno = errno;
197 (void*)(((char*)pMI->txt_data) + pMI->txt_size), pgsz,
198 PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED,
199 pMI->txt_zero_fd, 0 );
201 if (pNuls != MAP_FAILED_PTR)
202 return pMI->txt_data;
204 pMI->txt_errno = errno;
205 close( pMI->txt_zero_fd );
206 pMI->txt_zero_fd = -1;
210 pMI->txt_full_size = pMI->txt_size;
214 void* p = AGALOC( pMI->txt_size+1, "file text" );
215 memcpy( p, pMI->txt_data, pMI->txt_size );
216 ((char*)p)[pMI->txt_size] = NUL;
217 munmap(pMI->txt_data, pMI->txt_size );
221 return pMI->txt_data;
223 #else /* * * * * * no HAVE_MMAP * * * * * */
225 pMI->txt_data = AGALOC( pMI->txt_size+1, "file text" );
226 if (pMI->txt_data == NULL) {
227 pMI->txt_errno = ENOMEM;
232 size_t sz = pMI->txt_size;
233 char* pz = pMI->txt_data;
236 ssize_t rdct = read( pMI->txt_fd, pz, sz );
238 pMI->txt_errno = errno;
239 fprintf( stderr, zFSErrReadFile,
240 errno, strerror( errno ), pzFile );
241 free( pMI->txt_data );
253 * We never need a dummy page mapped in
255 pMI->txt_zero_fd = -1;
258 return pMI->txt_data;
260 #endif /* * * * * * no HAVE_MMAP * * * * * */
263 if (pMI->txt_fd >= 0) {
264 close( pMI->txt_fd );
267 errno = pMI->txt_errno;
268 pMI->txt_data = MAP_FAILED_PTR;
269 return pMI->txt_data;
273 /*=export_func text_munmap
276 * what: unmap the data mapped in by text_mmap
278 * arg: tmap_info_t*, mapinfo, info about the mapping
281 * ret-desc: -1 or 0. @file{errno} will have the error code.
285 * This routine will unmap the data mapped in with @code{text_mmap} and close
286 * the associated file descriptors opened by that function.
288 * see: munmap(2), close(2)
290 * err: Any error code issued by munmap(2) or close(2) is possible.
293 text_munmap( tmap_info_t* pMI )
297 if (pMI->txt_alloc) {
299 * IF the user has write permission and the text is not mapped private,
300 * then write back any changes. Hopefully, nobody else has modified
301 * the file in the mean time.
303 if ( ((pMI->txt_prot & PROT_WRITE) != 0)
304 && ((pMI->txt_flags & MAP_PRIVATE) == 0)) {
306 if (lseek(pMI->txt_fd, (size_t)0, SEEK_SET) != 0)
309 res = (write( pMI->txt_fd, pMI->txt_data, pMI->txt_size ) < 0)
313 AGFREE( pMI->txt_data );
316 res = munmap( pMI->txt_data, pMI->txt_full_size );
321 res = close( pMI->txt_fd );
327 if (pMI->txt_zero_fd != -1) {
328 res = close( pMI->txt_zero_fd );
329 pMI->txt_zero_fd = -1;
333 pMI->txt_errno = errno;
335 #else /* HAVE_MMAP */
339 * IF the memory is writable *AND* it is not private (copy-on-write)
340 * *AND* the memory is "sharable" (seen by other processes)
341 * THEN rewrite the data.
343 if ( FILE_WRITABLE(pMI->txt_prot, pMI->txt_flags)
344 && (lseek( pMI->txt_fd, 0, SEEK_SET ) >= 0) ) {
345 write( pMI->txt_fd, pMI->txt_data, pMI->txt_size );
348 close( pMI->txt_fd );
350 pMI->txt_errno = errno;
351 free( pMI->txt_data );
353 return pMI->txt_errno;
354 #endif /* HAVE_MMAP */
360 * c-file-style: "stroustrup"
361 * indent-tabs-mode: nil
363 * end of autoopts/text_mmap.c */