On Sat, Jul 11, 2020 at 09:12:55PM -0600, Theo de Raadt wrote:
> Peter J. Philipp <p...@centroid.eu> wrote:
> 
> > Is this possible at all?  I have mmap'ed (shared) a process and it has 
> > childs.
> > I would like to unmap this mmap in one child only but I'm not sure if the 
> > other childs that should have this mapping still will lose it or not?  Can 
> > someone enlighten me on this?
> 
> Write a test program.
> 
> The behaviour you see will soon, based upon the MAP_ options you use,
> will soon be precisely what is documented, and you'll understand how
> it works.

Thanks for the hint.  I wrote a test program and I'm happy that the mapping
does indeed stay on the other forked processes.  The test program is after
my signature for anyone else.

Thanks!
-peter


#include <sys/types.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>

int
main(void)
{
        char *ptr;
        pid_t pid;
        int i;


        ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED |\
                MAP_ANON, -1, 0);

        if (ptr == MAP_FAILED) {
                err(1, "mmap");
                exit(1);
        }

        memset(ptr, 0x32, 4096);


        pid = fork();
        switch (pid) {
        case -1:
                err(1, "fork");
                break;
        case 0:
                if (munmap(ptr, 4096) == -1)
                        err(1, "munmap");

                for (;;)
                        sleep(10);
        
                break;
        default:
                printf("continuing from forking to pid %d\n", pid);
                break;
        }

        pid = fork();
        switch (pid) {
        case -1:
                err(1, "fork");
                break;
        case 0:
                sleep(2);
                memset(ptr, 0x42, 4096);
                for (;;)
                        sleep(10);
        
                break;
        default:
                printf("continuing from forking to pid %d\n", pid);
                break;
        }

        sleep(5);
        printf("printing the first 16 bytes from shared memory\n");
        for (i = 0; i < 16; i++) {
                printf("%02x, ", ptr[i] & 0xff);
        }
        printf("\n");

        sleep(30);

        exit(0);
}

Reply via email to