DuplicateHandle() question

egor duda deo@logos-m.ru
Wed Jun 12 10:36:00 GMT 2002


Hi!

Wednesday, 12 June, 2002 Conrad Scott Conrad.Scott@dsl.pipex.com wrote:

>> I don't quite understand why server would want client to discard some
>> handle. Can you describe step-by-step scenario when it's needed? Do
>> you have shmctl(..., IPC_RMID, ...) in mind?

CS> Not shmctl(2) but shmget(2): when creating and accessing shm segments for a
CS> client, the cygserver calls CreateFileMapping() to create the memory areas
CS> and then uses DuplicateHandle() on the resulting handles to create handles
CS> for the client (in client process) to these file mappings. The problem then
CS> arises that the sever can fail having duplicated some but not all of the
CS> handles for the client, i.e. it's just an error cleanup question: I'm not
CS> looking to have the server arbitrarily close client handles.

CS> AIUI at that point the handles are valid in the client process but the
CS> server can't close them, i.e. the client has to close them. Is that true or
CS> is there some way for the server to close them (i.e. a "close in other
CS> process" function)? I assume not.

You are creating some object in client process' address space. This
object contains some handles. Some are possibly created by client
(imagine that we want to serialize access to our object and create a
mutex for this), and some are "pushed in" by server. I.e. on the
client side we have something like

shmget (...)
{
  ...

  handle1 = handle2 = ... = 0;

  handle1 = CreateMutex (...);
  if (!handle1) goto failure;

  handle2 = GetHandleFromServer (...);
  if (!handle2) goto failure;

  ...
  return NO_ERROR;

failure:
// cleanup
  if (handle1) CloseHandle (handle1);
  if (handle2) CloseHandle (handle2);
  ...

  return ERROR;
}

 All server has to do is to return valid handle on success and 0 on
error. It's up to client to interpret the results. Or you can make
server return all handles in some structure so that client will have
to call server only once. But then again, all server has to do is to
fill this structure and it's up to client to discard all valid handles
if at least one of returned handles is 0.

Egor.            mailto:deo@logos-m.ru ICQ 5165414 FidoNet 2:5020/496.19



More information about the Cygwin-developers mailing list