1829598 Members
1746 Online
109992 Solutions
New Discussion

mmap problem

 
SOLVED
Go to solution
Gopinath_3
Advisor

mmap problem

I have a C program that opens a file and mmaps it. The file has both read & write permissions for everyone.

I compile with gcc and run it on hp-ux 11i, 64 bit.

Here are relevant portions of the code:

....
name = tempnam(NULL, "abc");
fd = open (name, O_CREAT|O_RDWR|O_EXCL, 0666);
ftruncate(fd, 4096);
mmap(NULL, nbytes, PROT_READ|PROT_WRITE|PROT_EXEC|MAP_SHARED, fd, (off_t)0);

The mmap call fails with error 13. (I check the return value from each call).

Is this a known bug? I searched the forums but did not see anything.

The exact same program runs correctly on another unix platform!!

Thanks

Gopi
5 REPLIES 5
Varghese Mathew
Trusted Contributor

Re: mmap problem

Hi,

Looks like a file permission problem (from the error code you have given).
you can verify the same by typing in the following command:

#grep 13 /usr/include/sys/errno.h

Then, the permissions of / and /lib have to be verified ..

1. ll -d /
drwxr-xr-x 45 root root 2048 Nov 8 12:34 /

2. ll -d /lib
dr-xr-xr-x 4 bin bin 1024 Oct 23 12:11 /lib

Correct them if needed..

Hope this helps ..
Cheers !!!
Mathew
Cheers !!!
A. Clay Stephenson
Acclaimed Contributor

Re: mmap problem

Hi:

One thing I see immediately is that you are one paramter short in your call to mmap. You need a comma where you have a '|' before the MAP_SHARED argument. Just to be safe, I would specify flags to be MAP_FILE | MAP_SHARED. I know MAP_FILE is default but I never trust such things.

Clay
If it ain't broke, I can fix that.
Gopinath_3
Advisor

Re: mmap problem

I made a mistake while copying the code from my terminal to the browser. There is a comma before MAP_SHARED. I also added MAP_FILE just to be sure.

It still doesn't work.

I set the TMPDIR env variable to my current working dir and I run the program as myself to eliminate permission problems.

I have also tried running as root without setting the TMPDIR env variable so that the temp file gets created in /var/tmp where root has permissions to read & write.

Thanks for the suggestions.

Gopi
Gregory Fruth
Esteemed Contributor
Solution

Re: mmap problem

You specify PROT_EXEC, but you open the file
with permissions 0666. Try opening the file with
0777 (or whatever is appropriate, like 0755), or
try deleting the PROT_EXEC if you don't need it.

Also, what is nbytes? You truncate the file to
4096 bytes, but map bytes [0,nbytes-1] of it.
If nbytes > the size of the file you'll get into
trouble. (Having nbytes < the size of the file is
OK, however.)

HTH



Gopinath_3
Advisor

Re: mmap problem

Gregory,

Thanks for catching the bug in my program. Your answer was correct.

I was going down the wrong path because the code worked on linux & solaris.

It seems that hp-ux does a better job of catching user errors!!

Thanks

Gopi