X-Spam-Check-By: sourceware.org Date: Mon, 23 Jan 2006 16:18:10 +0100 From: Corinna Vinschen To: cygwin AT cygwin DOT com Subject: Re: 1.5.19-3 parent socket binding remains after closing when spawning child process Message-ID: <20060123151810.GB8552@calimero.vinschen.de> Reply-To: cygwin AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com References: <20060120165308 DOT 59367 DOT qmail AT web30611 DOT mail DOT mud DOT yahoo DOT com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060120165308.59367.qmail@web30611.mail.mud.yahoo.com> User-Agent: Mutt/1.4.2i Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com 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); fork() WSADuplicateSocket(sock) in parent sock = WSASocket in child, BUT sock is inheritable again. exec() 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 -- Corinna Vinschen Please, send mails regarding Cygwin to Cygwin Project Co-Leader cygwin AT cygwin DOT com Red Hat -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/