changing mmap() permissions

Randy Kaelber plug-discuss@lists.plug.phoenix.az.us
Tue, 20 Aug 2002 11:44:40 -0700


Hello, all.

I have a question for the programmer crowd here:

I'm trying to implement a distributed shared memory system using network
sockets and mmap().  A problem I have run into is that I want to protect
segments of memory from being written to by a process.  While it's easy
enough to mmap() a region read only, What I want to do is receive a
chunk of data from a remote process, mmap() it to a specific address,
and then make that block read-only, such that if a process tries to
write it after I've copied it in, it will get a seg fault.  If I start
read only, I cannot copy data to the region at all.

What I currently have is this:  I read the data from the network into a
mmap()ed read-write temp file, munmap() the region, and then re mmap()
the temp file to the same address, this time with read only access. 
This works, but it is kind of a hack, IMO.  I have all these temp files
hanging around that burn up disk needlessly.  I'd like to mmap()
/dev/zero in read-write, copy the data in, and then change my
permissions on the region to read-only directly.

I've searched the 'net quite a bit for an answer, but none have been
forthcoming.  Just mmap()ing over the already mmap()ed region returns an
error.  Note that I *am* forcing a mmap() address (i.e. specifying a
start address and the MAP_FIXED flag), since my addresses need to be
synchronized over multiple systems, such that pointers point to where I
think they're pointing at.  

-- 
Randy Kaelber                                       
Randy.Kaelber@asu.edu
Software Engineer  
Mars Space Flight Facility, Department of Geological Sciences
Arizona State University, Tempe, Arizona, USA

"Anarchy is the sure consequence of tyranny; for no power that is not
limited by laws can ever be protected by them." - Milton