Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 08 Nov 1998 23:34:34 +0000
From:      Brian Somers <brian@Awfulhak.org>
To:        Marc Slemko <marcs@znep.com>
Cc:        Brian Somers <brian@Awfulhak.org>, freebsd-hackers@FreeBSD.ORG
Subject:   Re: bind()/listen() race 
Message-ID:  <199811082334.XAA06518@woof.lan.awfulhak.org>
In-Reply-To: Your message of "Sun, 08 Nov 1998 11:37:37 PST." <Pine.BSF.4.05.9811081135410.5670-100000@alive.znep.com> 

next in thread | previous in thread | raw e-mail | index | archive | help
> On Sun, 8 Nov 1998, Brian Somers wrote:
> 
> > If I create a local domain socket with bind(), there's a period 
> > between that and the listen() where a connect() will fail.  Is there 
> > any way to circumvent the problem ?
> 
> Why is it a problem to have connects fail before you listen() but not a
> problem to have them fail before you bind()?
> 
> I'm not sure I see the race condition.  Once you listen(), you can accept
> connections.  Before you listen(), you can't.  

The code says something like:

  if (bind(blah) < 0) {
    /* we're happy to be the client */
    if (connect(blah) < 0) {
      socket is bound without a listen !
    }
  } else if (listen(blah) < 0) {
    oops !
  }

The sockaddrs are local domain sockets used by ppp in multilink mode. 
Whoever gets to be the server will survive.  The other ppp will 
become the client, pass a file descriptor to the server and hang 
around holding the session 'till the other ppp kills it.

However, if the two ppps get unlucky, one bind()s and the second 
fails then tries to connect() and fails 'cos the server hasn't 
listen()ed yet.  This is bad news.

The only way I can see around it - given that I can't sleep() in ppp 
without screwing up with other timing issues - is to detect the error 
and do a 1 second timeout, and try again then.  This is a nasty thing 
to have to do....  I'd prefer an atomic bind()/listen() facility....

Hence the question :-)
-- 
Brian <brian@Awfulhak.org>, <brian@FreeBSD.org>, <brian@OpenBSD.org>
      <http://www.Awfulhak.org>;
Don't _EVER_ lose your sense of humour....



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199811082334.XAA06518>