This is the mail archive of the cygwin 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]

Re: 1.5.19-3 parent socket binding remains after closing when spawning child process

On Jan 20 11:53, Martin wrote:
> Well, this is a strange one.
> I open a socket in a parent process, bind it and
> listen.
> I set the socket to close-on-exec with the FD_CLOEXEC
> flag.
> A child process is spawned, which doesn't inherit the
> socket.
> I then close the server socket in the parent process.
> For a laugh, I attempt to connect to the port
> associated with the server socket.  I would expect
> this connection request to fail, since I just closed
> the socket.  It doesn't.
> The attached test case illustrates this.
> Please let me know if I'm doing something wrong.

Thanks very much for the testcase.  I found that when duplicating
sockets between processes using the WSADuplicateSocket/WSASocket method,
the duplicated socket is inheritable by child processes again, even when
the original socket has the inheritance flag removed.  So, what
basically happend behind the scenes was this:

  sock = socket()
  SetHandleInformation(sock, DONT_INHERIT);

    WSADuplicateSocket(sock)    in parent
    sock = WSASocket            in child, BUT sock is inheritable again.

      close_on_exec is set?  Yes, don't duplicate socket.
      ... but sock was inheritable, so the exec'ed process got a
      valid handle to a valid, listening socket.  Bummer.

I've checked in a patch to CVS which resets the inheritance for the
duplicated socket in the child process, so there's no valid, open handle
to the listening socket anymore, after the parent called close() and the
forked child exec'ed.  Consequentially the 2nd connect call fails now.

Thanks again for the testcase, it was highly appreciated,

Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

Unsubscribe info:
Problem reports:

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