LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple mmap() call failed with EINVAL on Redhat EL 3 update 9 (https://www.linuxquestions.org/questions/programming-9/simple-mmap-call-failed-with-einval-on-redhat-el-3-update-9-a-711379/)

modemer 03-13-2009 11:30 AM

simple mmap() call failed with EINVAL on Redhat EL 3 update 9
 
Hi,

I am facing a weird problem on mmap call, not sure if it's caused by kernal or my code, below is the detail.

- uname -a
Linux 2.4.21-47.ELsmp #1 SMP
- redhat-release
Red Hat Enterprise Linux AS release 3 (Taroon Update 9)
- my testing code: test.c
Code:

#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>

int main(int argc, char **argv) {
    int len=0;
    char *addr = NULL;
    int fd =  open( "test.txt", O_RDWR | O_CREAT, 0644 );

    if (argc > 1)
        len = atoi(argv[1]);

    addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    perror("perror");
    printf("mmap return[%x]\n", addr);

    close(fd);
    return 0;
}

- build with gcc
gcc test.c
- create a test.txt file with lengh 100 or you like for testing
- run testing
a.out 10
I specify 10 bytes length from the beginning of test.txt file to be mapped into memory. mmap failed with EINVAL error. If I changed the length to 0, it returns successfully. It also failed with 4096 which is pagesize.

Anybody knows what might cause this problem?

Thanks!
Mod

dwhitney67 03-13-2009 11:45 AM

Did you clean the dust off of your distro? Jeez that RH version is old.

Your code, which btw was replete (I like to exaggerate sometimes) with compiler errors, did not run because of an invalid argument.

Here's the code I tested with:
Code:

#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(int argc, char **argv) {
    int len=0;
    int fd =  open( "test.txt", O_RDWR | O_CREAT, 0644 );

    if (argc > 1)
        len = atoi(argv[1]);

    void* addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

    if (addr == MAP_FAILED)
    {
      printf("mmap returned, errno = %d, %s\n", errno, strerror(errno));
    }

    close(fd);
    return 0;
}

The invalid argument is the 'len', which you have set to zero. Perhaps it should be set to the size of the file? Or perhaps you should guess the size of the file with the command-line arg?

modemer 03-13-2009 12:17 PM

sorry about the compiling errors as I am using a smart configured(might be stupid:D) gcc...

By the way, len=any# works fine on another box which has "Linux 2.4.21-9.ELsmp #1 SMP" and "Red Hat Enterprise Linux AS release 3 (Taroon Update 1)"

I don't want to clean or reinstall the system to fix this problem, I really want to figure out the problem first and know what is the real cause, such as system configuration caused the problem or a system file caused the problem etc...

dwhitney67 03-13-2009 01:37 PM

On my system, the code runs fine as long as 'len' is not zero.

You may want to refer to the man-page for mmap() on your system to see if there is any information given concerning the 'addr' and/or 'len' being zero (or null).


All times are GMT -5. The time now is 11:34 AM.