Changeset 1582


Ignore:
Timestamp:
01/06/07 21:01:25 (6 years ago)
Author:
sam
Message:
  • Implemented the mmap() family.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/src/load-fd.c

    r1560 r1582  
    3434#include <sys/types.h> 
    3535#include <sys/socket.h> 
     36#include <sys/mman.h> 
    3637#include <unistd.h> 
    3738#include <fcntl.h> 
     
    5657static off64_t (*lseek64_orig) (int fd, off64_t offset, int whence); 
    5758#endif 
     59static void *  (*mmap_orig)    (void *start, size_t length, int prot, 
     60                                int flags, int fd, off_t offset); 
     61#ifdef HAVE_LSEEK64 
     62static void *  (*mmap64_orig)  (void *start, size_t length, int prot, 
     63                                int flags, int fd, off64_t offset); 
     64#endif 
     65static int     (*munmap_orig)  (void *start, size_t length); 
    5866static int     (*close_orig)   (int fd); 
     67 
    5968 
    6069void _zz_load_fd(void) 
     
    7180    LOADSYM(lseek64); 
    7281#endif 
     82    LOADSYM(mmap); 
     83#ifdef HAVE_MMAP64 
     84    LOADSYM(mmap64); 
     85#endif 
     86    LOADSYM(munmap); 
    7387    LOADSYM(close); 
    7488} 
     
    186200 
    187201#define LSEEK(fn, off_t) \ 
    188     do { \ 
     202    do \ 
     203    { \ 
    189204        if(!_zz_ready) \ 
    190205            LOADSYM(fn); \ 
     
    214229#endif 
    215230 
     231/* Used for mmap() and munmap() */ 
     232void **maps = NULL; 
     233int nbmaps = 0; 
     234 
     235#define MMAP(fn, off_t) \ 
     236    do { \ 
     237        if(!_zz_ready) \ 
     238            LOADSYM(fn); \ 
     239        ret = ORIG(fn)(start, length, prot, flags, fd, offset); \ 
     240        if(!_zz_ready || !_zz_iswatched(fd) || _zz_disabled) \ 
     241            return ret; \ 
     242        if(ret) \ 
     243        { \ 
     244            void *tmp = malloc(length); \ 
     245            int i; \ 
     246            for(i = 0; i < nbmaps; i += 2) \ 
     247                if(maps[i] == NULL) \ 
     248                    break; \ 
     249            if(i == nbmaps) \ 
     250            { \ 
     251                nbmaps += 2; \ 
     252                maps = realloc(maps, nbmaps * sizeof(void *)); \ 
     253            } \ 
     254            maps[i] = tmp; \ 
     255            maps[i + 1] = ret; \ 
     256            memcpy(tmp, ret, length); /* FIXME: get rid of this */ \ 
     257            _zz_fuzz(fd, tmp, length); \ 
     258            ret = tmp; \ 
     259        } \ 
     260        debug(STR(fn)"(%p, %li, %i, %i, %i, %lli) = %p", start, \ 
     261              (long int)length, prot, flags, fd, (long long int)offset, ret); \ 
     262    } while(0) 
     263 
     264void *mmap(void *start, size_t length, int prot, int flags, 
     265           int fd, off_t offset) 
     266{ 
     267    void *ret; MMAP(mmap, off_t); return ret; 
     268} 
     269 
     270#ifdef HAVE_MMAP64 
     271void *mmap64(void *start, size_t length, int prot, int flags, 
     272             int fd, off64_t offset) 
     273{ 
     274    void *ret; MMAP(mmap64, off64_t); return ret; 
     275} 
     276#endif 
     277 
     278int munmap(void *start, size_t length) 
     279{ 
     280    int ret, i; 
     281 
     282    if(!_zz_ready) 
     283        LOADSYM(munmap); 
     284    for(i = 0; i < nbmaps; i++) 
     285    { 
     286        if(maps[i] != start) 
     287            continue; 
     288 
     289        free(start); 
     290        ret = munmap_orig(maps[i + 1], length); 
     291        maps[i] = NULL; 
     292        maps[i + 1] = NULL; 
     293        debug("munmap(%p, %li) = %i", start, (long int)length, ret); 
     294        return ret; 
     295    } 
     296 
     297    return munmap_orig(start, length); 
     298} 
     299 
    216300int close(int fd) 
    217301{ 
Note: See TracChangeset for help on using the changeset viewer.