Hello, everyone. It appears that in GNU/Hurd, memory mappings obtained by 'mmap' with MAP_SHARED and MAP_ANON as its flags are not being inherited by children processes.
Here's a simple program that illustrates the issue: =============================== #include <stdio.h> #include <unistd.h> #include <sys/mman.h> #include <sys/wait.h> int main (void) { void *p = mmap (0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); if (p == MAP_FAILED) { puts ("mmap failed."); return (1); } int pid = fork (); if (pid < 0) { puts ("fork failed."); return (1); } else if (pid == 0) { *(int *)p = 69; puts ("value was set."); } else { int r; wait (&r); printf ("done waiting for the child" "\nvalue is: %d\n", *(int *)p); } return (0); } ================================== The parent process ends up printing zero, which is wrong. A quick inspection at the source code tells me that this code ends up calling 'vm_allocate' as an optimization when it sees that the user requested an anonymous mapping with protection RW. However, it's not taking into account that 'vm_allocate' has a default inheritance value of 'COPY'. An easy workaround is to simply remove the 'vm_allocate' optimization, and always use 'vm_map'. There's a patch attached that does just this.
diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c index c6ddb39..a4e240e 100644 --- a/sysdeps/mach/hurd/mmap.c +++ b/sysdeps/mach/hurd/mmap.c @@ -44,30 +44,6 @@ __mmap (__ptr_t addr, size_t len, int prot, int flags, int fd, off_t offset) if ((mapaddr & (vm_page_size - 1)) || (offset & (vm_page_size - 1))) return (__ptr_t) (long int) __hurd_fail (EINVAL); - if ((flags & (MAP_TYPE|MAP_INHERIT)) == MAP_ANON - && prot == (PROT_READ|PROT_WRITE)) /* cf VM_PROT_DEFAULT */ - { - /* vm_allocate has (a little) less overhead in the kernel too. */ - err = __vm_allocate (__mach_task_self (), &mapaddr, len, - mapaddr == NULL); - - if (err == KERN_NO_SPACE) - { - if (flags & MAP_FIXED) - { - /* XXX this is not atomic as it is in unix! */ - /* The region is already allocated; deallocate it first. */ - err = __vm_deallocate (__mach_task_self (), mapaddr, len); - if (!err) - err = __vm_allocate (__mach_task_self (), &mapaddr, len, 0); - } - else if (mapaddr != NULL) - err = __vm_allocate (__mach_task_self (), &mapaddr, len, 1); - } - - return err ? (__ptr_t) (long int) __hurd_fail (err) : (__ptr_t) mapaddr; - } - vmprot = VM_PROT_NONE; if (prot & PROT_READ) vmprot |= VM_PROT_READ;