Re: mmap() on write only files

Antonio Dell'elce (dellelce@freenet.hut.fi)
Fri, 18 Jul 1997 09:56:05 +0200


> Hello all,
> The following program makes a call to mmap() which fails under
> Linux 2.0.27 2.0.29 and Digital Unix 4.0B. I have not tested it
> under other operating systems. If the file descriptor is opened
> with O_RDWR the mmap() call works. Is this a bug or a feature?
>
> Thanks,
>
> Jim
>
> #include <stdlib.h>
> #include <stdio.h>
> #include <unistd.h>
> #include <fcntl.h>
> #include <sys/mman.h>
>
> int main(void)
> {
> void *ptr;
> char *fname = "/tmp/_my_test";
> int fd = open(fname, O_WRONLY /*O_RDWR*/, 0600);
>
> printf("Opened \"%s\" and got fd %d\n", fname, fd);
>
> ftruncate(fd, 8192);
> ptr = mmap(0, 8192, PROT_WRITE, MAP_SHARED, fd, 0);
>
> printf("mmap of file returns 0x%lx\n", (unsigned long) ptr);
> }
>

I tested it under AIX 4.1. It failed too.

// Opened "/tmp/_my_test" and got fd -1
// mmap of file returns 0xffffffff

uname was: AIX sst05 1 4 002114054800

--
Antonio Dell'elce .. Also at uh886@freenet.victoria.bc.ca