mmap() on write only files

Jim Nance (jlnance@avanticorp.com)
Thu, 17 Jul 1997 10:08:26 -0400 (EDT)


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);
}