Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 23 Aug 2001 20:07:31 +0200
From:      Bernd Walter <ticso@mail.cicely.de>
To:        Julian Elischer <julian@elischer.org>
Cc:        Alfred Perlstein <bright@mu.org>, Bernd Walter <ticso@mail.cicely.de>, freebsd-hackers@freebsd.org
Subject:   Re: mmap MAP_INHERIT question.
Message-ID:  <20010823200731.A27427@cicely20.cicely.de>
In-Reply-To: <Pine.BSF.4.21.0108231012330.48732-100000@InterJet.elischer.org>; from julian@elischer.org on Thu, Aug 23, 2001 at 10:13:01AM -0700
References:  <20010823083711.Z81307@elvis.mu.org> <Pine.BSF.4.21.0108231012330.48732-100000@InterJet.elischer.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--9jxsPFA5p3P2qPhR
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

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 <ticso@mail.cicely.de> [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
ticso@cicely.de         Usergroup           info@cosmo-project.de


--9jxsPFA5p3P2qPhR
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="prog1.c"


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

--9jxsPFA5p3P2qPhR
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="prog2.c"


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

}

--9jxsPFA5p3P2qPhR--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010823200731.A27427>