123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- /*
- * $Id: text_mmap.c,v 4.5 2005/04/16 17:07:29 bkorb Exp $
- *
- * Time-stamp: "2005-02-24 11:56:43 bkorb"
- */
- #define FILE_WRITABLE(_prt,_flg) \
- ((_prt & PROT_WRITE) && (_flg & (MAP_SHARED|MAP_PRIVATE) == MAP_SHARED))
- /***export_func text_mmap
- *
- * what: map a text file with terminating NUL
- *
- * arg: const char*, pzFile, name of the file to map
- * arg: int, prot, mmap protections (see mmap(2))
- * arg: int, flags, mmap flags (see mmap(2))
- * arg: tmap_info_t*, mapinfo, returned info about the mapping
- *
- * ret-type: void*
- * ret-desc: The mmaped data address
- *
- * doc:
- *
- * 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}
- * 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
- * @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
- * 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.
- * 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
- * are mapped after the end of the data.
- *
- * see: mmap(2), open(2), stat(2)
- *
- * err: Any error code issued by mmap(2), open(2), stat(2) is possible.
- * Additionally, if the specified file is not a regular file, then
- * errno will be set to @code{EINVAL}.
- *
- * example:
- * #include <mylib.h>
- * tmap_info_t mi;
- * int no_nul;
- * void* data = text_mmap( "file", PROT_WRITE, MAP_PRIVATE, &mi );
- * if (data == MAP_FAILED) return;
- * no_nul = (mi.txt_size == mi.txt_full_size);
- * << use the data >>
- * text_munmap( &mi );
- =*/
- LOCAL void*
- text_mmap( const char* pzFile, int prot, int flags, tmap_info_t* pMI )
- {
- 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;
- }
- if (! S_ISREG( sb.st_mode )) {
- pMI->txt_errno = errno = EINVAL;
- return MAP_FAILED;
- }
- 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 < 0) {
- pMI->txt_errno = errno;
- return MAP_FAILED;
- }
- #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, prot, flags, pMI->txt_fd, 0 );
- if (pMI->txt_data == MAP_FAILED) {
- 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;
- do {
- #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)
- break;
- /*
- * Still here? We have to append a page of NUL's
- */
- pMI->txt_full_size += pgsz;
- {
- #ifdef MAP_ANONYMOUS
- void* pNuls = mmap(
- (void*)(((char*)pMI->txt_data) + pMI->txt_size), pgsz,
- PROT_READ, MAP_ANONYMOUS|MAP_FIXED, 0, 0 );
- if (pNuls == MAP_FAILED) {
- pMI->txt_errno = errno;
- pMI->txt_full_size = pMI->txt_size;
- }
- #else
- pMI->txt_zero_fd = open( "/dev/zero", O_RDONLY );
- if (pMI->txt_zero_fd < 0) {
- pMI->txt_errno = errno;
- pMI->txt_full_size = pMI->txt_size;
- } else {
- void* pNuls = mmap(
- (void*)(((char*)pMI->txt_data) + pMI->txt_size), pgsz,
- PROT_READ, MAP_PRIVATE|MAP_FIXED, pMI->txt_zero_fd, 0 );
- if (pNuls == MAP_FAILED) {
- pMI->txt_errno = errno;
- close( pMI->txt_zero_fd );
- pMI->txt_zero_fd = -1;
- }
- }
- #endif
- }
- } while(0);
- 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;
- return pMI->txt_data;
- }
- /***export_func text_munmap
- *
- * what: unmap the data mapped in by text_mmap
- *
- * arg: tmap_info_t*, mapinfo, info about the mapping
- *
- * ret-type: int
- * ret-desc: -1 or 0. @file{errno} will have the error code.
- *
- * doc:
- *
- * This routine will unmap the data mapped in with @code{text_mmap} and close
- * the associated file descriptors opened by that function.
- *
- * see: munmap(2), close(2)
- *
- * err: Any error code issued by munmap(2) or close(2) is possible.
- =*/
- LOCAL int
- text_munmap( tmap_info_t* pMI )
- {
- #ifdef HAVE_MMAP
- int 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 );
- if (res == 0)
- pMI->txt_zero_fd = -1;
- }
- error_return:
- pMI->txt_errno = errno;
- return res;
- #else /* HAVE_MMAP */
- errno = 0;
- /*
- * 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.
- */
- 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 );
- }
- close( pMI->txt_fd );
- pMI->txt_fd = -1;
- pMI->txt_errno = errno;
- free( pMI->txt_data );
- return pMI->txt_errno;
- #endif /* HAVE_MMAP */
- }
- /*
- * Local Variables:
- * mode: C
- * c-file-style: "stroustrup"
- * tab-width: 4
- * indent-tabs-mode: nil
- * End:
- * end of autoopts/text_mmap.c */
|