Tuesday, September 28, 2010

mmap() is not the territory!! Part 2

Final code at end of debugging:

bool testFileCopyWithSharedMem(const string &srcFileName, const string &destFileName)
{
    bool isCopyOk = false;
    string diffCmd = "diff " + srcFileName + " " + destFileName;
    int retval = system(diffCmd.c_str());
    cout << "system(" << diffCmd << ") returned: " << retval << endl;
    if( retval == 0 )
    {
        isCopyOk = true;
    }
    return isCopyOk;
}

int doFileCopyWithSharedMem(const string &srcFileName, const string &destFileName, size_t sharedMemSize)
{
    //int retval = 0;

    //Map SourceFile
    errno = 0;
    int srcfd = open(srcFileName.c_str(), O_RDONLY);
    if (srcfd < 0) {
        const char * causeOfError = strerror(errno);
        cout << "open() returned:" << causeOfError << " at:" << __LINE__ << " in:" <<__FUNCTION__ << endl;
        cout << "open(" << srcFileName << ") returned: " << srcfd << endl;
        return -1;
    }
    struct stat sb;
    fstat(srcfd,&sb);
    long pageSize;
    //pageSize = sb.st_size;
    pageSize = sysconf(_SC_PAGESIZE);
    errno = 0;
    char *srcFilePtr = 0;
    srcFilePtr = (char *) mmap(0, pageSize, PROT_READ, MAP_SHARED, srcfd, 0);
    if (srcFilePtr == MAP_FAILED)
    {
        const char * causeOfError = strerror(errno);
        cout << "mmap() returned:" << causeOfError << " at:" << __LINE__ << " in:" <<__FUNCTION__ << endl;
        return -1;
    }

    //Map Dest File
    errno = 0;
    int destfd = open(destFileName.c_str(), O_RDWR|O_CREAT|O_TRUNC, 0600 );
    if (destfd < 0) {
        const char * causeOfError = strerror(errno);
        cout << "creat() returned:" << causeOfError << " at:" << __LINE__ << " in:" <<__FUNCTION__ << endl;
        cout << "creat(" << destFileName << ") returned: " << destfd << endl;
        return -1;
    }
    errno = 0;
    char *destFilePtr = 0;
    destFilePtr = (char *) mmap(0, pageSize, PROT_WRITE|PROT_READ, MAP_SHARED, destfd, 0);
    if (destFilePtr == MAP_FAILED) {
        const char * causeOfError = strerror(errno);
        cout << "mmap() returned:" << causeOfError << " at:" << __LINE__ << " in:" <<__FUNCTION__ << endl;
        return -1;
    }

    ftruncate(destfd, sb.st_size);
    cout<< "PID:" << getpid() <
    //cout << srcFilePtr << destFilePtr <
    system("cat /proc/self/maps");

    memcpy(destFilePtr, srcFilePtr, sb.st_size);
    //msync(destFilePtr, pageSize,MS_SYNC);

    munmap(srcFilePtr, pageSize);
    munmap(destFilePtr, pageSize);
    close(srcfd);
    close(destfd);
    return 0;
}
 
----------------------------------------------------------------------------
Time required:  
Debugging 1 AM to 5:40 AM. 
20 Minutes to note down these points in the blog. 
2 hrs to massage it into shape.

Interesting Links:
DevShed : development tutorials: http://www.devshed.com/
Gentoo Bug Reporting Guide: http://www.gentoo.org/doc/en/bugzilla-howto.xml 
C sample source on Gnu/Linux : http://www.c.happycodings.com/Gnu-Linux/index.html
Mmap() security bug with Null Pointers: http://blog.ksplice.com/2010/03/null-pointers-part-i/
Wiki on Chromium multi-process debugging with gdb: http://code.google.com/p/chromium/wiki/LinuxDebugging
BugReport: http://www.mail-archive.com/ubuntu-bugs@lists.ubuntu.com/msg2333427.html 


mmap is not the territory Part 1 : http://techtalkies.blogspot.com/2010/09/mmap-is-not-territory-or-mapfail-sigbus.html

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.