2
* $Id: text_mmap.c,v 4.14 2006/09/24 02:11:16 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.
142
pMI->txt_data = mmap( NULL, pMI->txt_size+1, prot, flags, pMI->txt_fd, 0 );
143
if (pMI->txt_data == MAP_FAILED_PTR) {
144
pMI->txt_errno = errno;
149
* Most likely, everything will turn out fine now. The only difficult
150
* part at this point is coping with files with sizes that are a multiple
151
* of the page size. Handling that is what this whole thing is about.
153
pMI->txt_zero_fd = -1;
159
size_t pgsz = sysconf(_SC_PAGESIZE);
161
size_t pgsz = getpagesize();
164
* Compute the pagesize rounded mapped memory size.
165
* IF this is not the same as the file size, then there are NUL's
166
* at the end of the file mapping and all is okay.
168
pMI->txt_full_size = (pMI->txt_size + (pgsz - 1)) & ~(pgsz - 1);
169
if (pMI->txt_size != pMI->txt_full_size)
170
return pMI->txt_data;
173
* Still here? We have to remap the trailing inaccessible page
174
* either anonymously or to /dev/zero.
176
pMI->txt_full_size += pgsz;
177
#if defined(MAP_ANONYMOUS)
179
(void*)(((char*)pMI->txt_data) + pMI->txt_size),
180
pgsz, PROT_READ|PROT_WRITE,
181
MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE, AO_INVALID_FD, 0 );
183
if (pNuls != MAP_FAILED_PTR)
184
return pMI->txt_data;
186
pMI->txt_errno = errno;
188
#elif defined(HAVE_DEV_ZERO)
189
pMI->txt_zero_fd = open( "/dev/zero", O_RDONLY );
191
if (pMI->txt_zero_fd == AO_INVALID_FD) {
192
pMI->txt_errno = errno;
196
(void*)(((char*)pMI->txt_data) + pMI->txt_size), pgsz,
197
PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED,
198
pMI->txt_zero_fd, 0 );
200
if (pNuls != MAP_FAILED_PTR)
201
return pMI->txt_data;
203
pMI->txt_errno = errno;
204
close( pMI->txt_zero_fd );
205
pMI->txt_zero_fd = -1;
209
pMI->txt_full_size = pMI->txt_size;
213
void* p = AGALOC( pMI->txt_size+1, "file text" );
214
memcpy( p, pMI->txt_data, pMI->txt_size );
215
((char*)p)[pMI->txt_size] = NUL;
216
munmap(pMI->txt_data, pMI->txt_size );
220
return pMI->txt_data;
222
#else /* * * * * * no HAVE_MMAP * * * * * */
224
pMI->txt_data = AGALOC( pMI->txt_size+1, "file text" );
225
if (pMI->txt_data == NULL) {
226
pMI->txt_errno = ENOMEM;
231
size_t sz = pMI->txt_size;
232
char* pz = pMI->txt_data;
235
ssize_t rdct = read( pMI->txt_fd, pz, sz );
237
pMI->txt_errno = errno;
238
fprintf( stderr, zFSErrReadFile,
239
errno, strerror( errno ), pzFile );
240
free( pMI->txt_data );
252
* We never need a dummy page mapped in
254
pMI->txt_zero_fd = -1;
257
return pMI->txt_data;
259
#endif /* * * * * * no HAVE_MMAP * * * * * */
262
if (pMI->txt_fd >= 0) {
263
close( pMI->txt_fd );
266
errno = pMI->txt_errno;
267
pMI->txt_data = MAP_FAILED_PTR;
268
return pMI->txt_data;
272
/*=export_func text_munmap
275
* what: unmap the data mapped in by text_mmap
277
* arg: tmap_info_t*, mapinfo, info about the mapping
280
* ret-desc: -1 or 0. @file{errno} will have the error code.
284
* This routine will unmap the data mapped in with @code{text_mmap} and close
285
* the associated file descriptors opened by that function.
287
* see: munmap(2), close(2)
289
* err: Any error code issued by munmap(2) or close(2) is possible.
292
text_munmap( tmap_info_t* pMI )
296
if (pMI->txt_alloc) {
298
* IF the user has write permission and the text is not mapped private,
299
* then write back any changes. Hopefully, nobody else has modified
300
* the file in the mean time.
302
if ( ((pMI->txt_prot & PROT_WRITE) != 0)
303
&& ((pMI->txt_flags & MAP_PRIVATE) == 0)) {
305
if (lseek( pMI->txt_fd, 0, SEEK_SET) != 0)
308
res = (write( pMI->txt_fd, pMI->txt_data, pMI->txt_size ) < 0)
312
AGFREE( pMI->txt_data );
315
res = munmap( pMI->txt_data, pMI->txt_full_size );
320
res = close( pMI->txt_fd );
326
if (pMI->txt_zero_fd != -1) {
327
res = close( pMI->txt_zero_fd );
328
pMI->txt_zero_fd = -1;
332
pMI->txt_errno = errno;
334
#else /* HAVE_MMAP */
338
* IF the memory is writable *AND* it is not private (copy-on-write)
339
* *AND* the memory is "sharable" (seen by other processes)
340
* THEN rewrite the data.
342
if ( FILE_WRITABLE(pMI->txt_prot, pMI->txt_flags)
343
&& (lseek( pMI->txt_fd, 0, SEEK_SET ) >= 0) ) {
344
write( pMI->txt_fd, pMI->txt_data, pMI->txt_size );
347
close( pMI->txt_fd );
349
pMI->txt_errno = errno;
350
free( pMI->txt_data );
352
return pMI->txt_errno;
353
#endif /* HAVE_MMAP */
359
* c-file-style: "stroustrup"
360
* indent-tabs-mode: nil
362
* end of autoopts/text_mmap.c */