Question on Patch E notes

In the notes for Patch E it states

CAUTION: The QNX 4 kernel does NOT maintain reference counts
on shared-memory fds. If you mmap a pointer to your application, don’t
close the fd and then shmem_unlink the memory object and continue to
reference that memory object through the pointer.

" For example, here are the incorrect and correct forms:

Incorrect form:

fd = shmem_open(“memory”…);
ptr = mmap(…);

shmem_unlink(“memory”);
close(fd);
modify data at ptr

Correct form:

fd = shmem_open(“memory”…);
ptr = mmap(…);

shmem_unlink(“memory”);
modify data at ptr

If you don’t use the correct form, the memory that your ptr refers to
could be given back to the OS for other allocation. As a result, you
could end up writing into memory regions owned by another process,
thus causing that other process to fail."


I took a look at how we have done this for shared memory and I am a bit
confused.
We are doing it in this manner

fd = shmem_open(“memory”…);
ptr = mmap(…);

close(fd);
modify data at ptr

This is in a library routine called by several apps to get the sharted
memory pointer.
My question is are we causing problems doing this? Should be call
shmem_unlink
instead of close? TIA.


\

Ivan Bannon
RJG Inc.

Ivan Bannon <ivan.bannon@rjginc.com> wrote:

I took a look at how we have done this for shared memory and I am a bit
confused.
We are doing it in this manner

fd = shmem_open(“memory”…);
ptr = mmap(…);

close(fd);
modify data at ptr

This is in a library routine called by several apps to get the sharted
memory pointer.
My question is are we causing problems doing this? Should be call
shmem_unlink
instead of close? TIA.

You are fine. If you don’t shm_unlink() the memory, you won’t have
the problem.

The issue: Proc is only maintaining reference counts for open fds
and for the pathname space entry. It is not maintaining reference
counts for the mappings.

As long as one reference exists, you are fine – in your case, you
haven’t unlinked it, so the name in the pathname space maintains the (at
least) one reference to it that you need to avoid the problem.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.