failed shared mmap'ing of /dev/zero

Sean Foderaro (jkf@tiger.franz.com)
Wed, 13 Mar 1996 12:32:35 -0800


kernel: 1.3.71

/dev/zero in linux refuses to do a MAP_SHARED mmap if PROT_WRITE
is also given.

Other Unixes (sunos, solaris, irix. ...) have no problem with this.

It's definitely intentional that such a mapping is refused but
it isn't clear why.

(from mem.c)
static int mmap_zero(struct inode * inode, struct file * file, struct vm_area_struct * vma)
{
if (vma->vm_flags & VM_SHARED)
return -EINVAL;
if (zeromap_page_range(vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
return -EAGAIN;
return 0;
}

Why would you want to do this kind of mapping? Well, I'm trying
to port some code that, needs to determine the location where shared
writable segments will be placed in the address space.

Here is a test case you can run to see if this mapping is
or is not supported:


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

main()
{
int fd;
caddr_t ret;

if ((fd = open("/dev/zero", O_RDWR)) < 0) {
perror("/dev/zero");
exit(1);
}

if ((int)(ret = mmap(0, 32*1024, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_SHARED, fd, 0)) == -1) {
perror("mmap");
exit(1);
}


printf("mmap returns 0x%x\n", ret);

}


-sean foderaro
franz inc.