This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Socket Handles duplicated twice after fork


Hello,
	I've been trying to get the developers attention to this problem
but have failed at it.  Perhaps the third time is the charm.

Below we have some code that creates a socketpair and forks.  Then each
process tells you what the handle of the sockets are.  The output looks
something like this:

Parent: Sockets are 0x10c 0x104
Child: Sockets are 0xc 0x10

Now if you run sysinternals's Process Explorer (www.sysinternals.com)
and click on the parent process you'll see that sure enough the handles
0x10c and 0x104 are there and point to a /Device/Afd/Endpoint.  Now if you
go to the child process you'll see that 0xc and 0x10 are there and
pointing to /Device/Afd/Endpoint.  However you'll also notice that 0x10c
and 0x104 are also there.  This is were the error is.  The bad part is
that you don't have access to those handles to close them so they will
always remain open even if you close 0xc and 0x10.  This was all tested in
Win2k.

Let me know what you guys think.  Is this an error in Cygwin, is it
fixable, is this an error in my code?  Thanks.

#include <unistd.h>
#include <io.h>
#include <sys/socket.h>
#include <stdio.h>

int main() {
  int fds[2];
  int pid;

  socketpair (AF_UNIX, SOCK_STREAM, 0, fds);

  pid = fork ();
  if (pid == 0) {
    printf("Child: Sockets are 0x%x 0x%x\n",
	   get_osfhandle(fds[0]), get_osfhandle(fds[1]));
    sleep(300);
  }
  else {
    printf("Parent: Sockets are 0x%x 0x%x\n",
	   get_osfhandle(fds[0]), get_osfhandle(fds[1]));
    sleep(300);
  }
}



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]