Re: mmap(MAP_SHARED | MAP_ANON) broke!

Sujal Patel (smpatel@prognet.com)
Sun, 12 Jan 1997 13:08:52 -0800 (PST)


On 11 Jan 1997, Eric W. Biederman wrote:

> There is also another solution (That's a real hack).
> x = mmap(..., MAP_ANON |MAP_PRIVATE, ...);
> fd = open("/proc/self/mem");
> y = mmap(...., MAP_SHARED, ..., fd, x);
> munmap(x);

This didn't work for me... I also tried forking and then mmap-ing() the
space out of the other processes /proc/.../mem--

Any idea what's going on (code attached).

Thanks

Sujal

-- Cut --
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>

extern int errno;

main()
{
char *a;
char *b;
int f;

a = mmap(0, 8192, PROT_READ | PROT_WRITE, MAP_ANONYMOUS |
MAP_PRIVATE, -1, 0);

f = open("/proc/self/mem", O_RDWR);

printf ("%d\n", f); // Prints 3

b = mmap(0, 8192, PROT_READ | PROT_WRITE, MAP_SHARED, f, a);

munmap(a, 8192);

printf ("%d %d\n", b, errno); // Prints -1 22

if (fork()) {
*b = 100;
} else {
sleep (1);
printf ("%d\n", *b);
}
}