LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2009, 11:30 AM   #1
modemer
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
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
 
Old 03-13-2009, 11:45 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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?

Last edited by dwhitney67; 03-13-2009 at 11:54 AM.
 
Old 03-13-2009, 12:17 PM   #3
modemer
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Original Poster
Rep: Reputation: 0
sorry about the compiling errors as I am using a smart configured(might be stupid) 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...
 
Old 03-13-2009, 01:37 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mmap call return value v_sharma Programming 4 01-05-2009 12:19 AM
got mmap (19 no such device) when running apt-get update rabbit2345 Linux - Software 1 08-14-2008 08:21 AM
poll returns errno 22, EINVAL mallport Programming 5 08-21-2006 03:24 PM
skype call failed SofNine Linux - Software 2 07-04-2006 07:40 PM
call to bind() getting failed kranti Linux - Software 4 01-18-2006 07:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration