On Thu, Aug 23, 2001 at 10:13:01AM -0700, Julian Elischer wrote:
> exec gives you an new vm space..
> inherrit only applies to forks

Then the manpage is absolutely wrong:

     MAP_INHERIT       Permit regions to be inherited across execve(2) system
                       calls.

I asumed MAP_SHARED alone is suffient for fork!?

> On Thu, 23 Aug 2001, Alfred Perlstein wrote:
> 
> > * Bernd Walter <[EMAIL PROTECTED]> [010823 06:16] wrote:
> > > I do the following:
> > >         buf = (char*)mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE,
> > >       MAP_ANON | MAP_INHERIT | MAP_SHARED, -1, 0);
> > > 
> > > Now I vfork/execve a child.
> > > But the child can't access the mmaped memory.
> > > It was my understanding that MAP_INHERIT | MAP_SHARED keep the memory
> > > over the execve.
> > 
> > Without sample code this is impossible to explain.

It's attached.
Start prog1 and prog2 should see the mmaped memory.

In the complex case I found it the second program wasn't able to read.
In this simple case it is able to read some different value and crashed
my i386-current (4.7.2001) when prog1 finishes.
I can't say if it's also a problem on recent current and the system did
not wrote a crashdump...

-- 
B.Walter              COSMO-Project         http://www.cosmo-project.de
[EMAIL PROTECTED]         Usergroup           [EMAIL PROTECTED]


#include <sys/types.h>
#include <sys/mman.h>
#include <sys/socket.h>

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

void ncox(int fd) {
        int val;
        int res;

        val = fcntl(fd, F_GETFD, 0);
        res = fcntl(fd, F_SETFD, val & ~FD_CLOEXEC);
}

void cox(int fd) {
        int val;
        int res;

        val = fcntl(fd, F_GETFD, 0);
        res = fcntl(fd, F_SETFD, val | FD_CLOEXEC);
}

int
main(int argc, char *argv[]) {
        char *buf;
        pid_t pid;
        int sv[2];
        int val;
        char arg0[] = "prog2";
        char* arg[] = {arg0, 0};

        buf = (char*)mmap(NULL, 8192, PROT_READ | PROT_WRITE,
            MAP_ANON | MAP_INHERIT | MAP_SHARED, -1, 0);
        buf[0] = 'a';
        buf[1] = '\0';
        printf("Buffer Prog1 = %p(%s)\n", buf, buf);

        socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
        printf("socketpair returned %i,%i\n", sv[0], sv[1]);
        sv[0];
        cox(sv[0]);
        cox(sv[1]);
        val = fcntl(sv[0], F_GETFL, 0);
        fcntl(sv[0], F_SETFL, val | O_NONBLOCK);
        pid = vfork();
        if (pid == 0) {
                dup2(sv[1], 3);
                ncox(3);
                execve("./prog2", arg, 0);
        }
        close(sv[1]);
        write(sv[0], &buf, sizeof(buf));
        sleep(100);
        return 0;
}

#include <sys/types.h>

#include <stdio.h>

int
main(int argc, char *argv[]) {
        char *buf;

        read(3, &buf, sizeof(buf));
        printf("Buffer Prog2 = %p", buf);
        printf("(%s)\n", buf);
        sleep(100);

}

Reply via email to