public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: cygwin@cygwin.com
Subject: Re: Hangs on connect to UNIX socket being listened on in the same process (was: Cygwin hanging in pselect)
Date: Mon, 09 Jan 2017 17:16:00 -0000	[thread overview]
Message-ID: <20170109171635.GB26337@calimero.vinschen.de> (raw)
In-Reply-To: <CAOTD34b-h4WyYiUtA8sizN6riPe1YsDWdaypDNbODC71xv13UQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3048 bytes --]

On Jan  9 16:46, Erik Bray wrote:
> Hi Corinna,
> 
> Thanks for the response.
> 
> On Mon, Jan 9, 2017 at 3:13 PM, Corinna Vinschen wrote:
> > Right.  It has to do with how connect/accept works on AF_LOCAL sockets.
> > The handshake doesn't work well for situations like yours, where the
> > same thread tries to connect and accept on the same socket.
> 
> Actually I'm not entirely sure now that that's the issue, even
> considering that this has come up before.  Or at the very least,
> there's an additional issue.  I realized that when I tried separate
> client/server processes, in the server I had put an accept() call at
> the end so it would block there.  With the server waiting to accept a
> connection it succeeded.  However, when I replaced the accept() with a
> long sleep(), the client's connect() never returns.

That's because connect infinitely waits for the accept to reply the
second half of the handshake.

> IIUC the handshake can't succeed until and unless the server accepts a
> connection from the client.

This is exactly the underlying problem.  And interesting enough, even
though the handshake is in Cygwin since 2001, we never had a problem
with this until Christian started porting postfix in 2014!

> I almost wonder if the server side in this case
> shouldn't start up a thread to accept the af_local handshake, but you
> would know better.

No, I don't.  We discussed this issue briefly back in 2014, but as
you can see we don't have a solution for this border case yet.

Starting a thread may or may not work, but there are a couple of
use-cases to keep in mind (which I can't reproduce off the top of my head).
The old postfix cygwin-apps thread from 2014 might give you some idea.

> > This has been found a problem in porting postfix already and at the time
> > we added a patch to circumvent the problem.  Before calling connect, add
> > this:
> >
> >   setsockopt (sock_server, SOL_SOCKET, SO_PEERCRED, NULL, 0);
> >   setsockopt (sock_client, SOL_SOCKET, SO_PEERCRED, NULL, 0);
> >
> > This is, of course, a hack.  The problem here is that server and client
> > of a socket are independent of each other, and there's typically no
> > way to know which process created the server side unless you already
> > are connected.  Chicken/egg.
> 
> I tried it and it worked, both in the single process and separate
> process examples.  I see now--this sets
> fhandler_socket::no_getpeerid=true, so it doesn't have to do the
> handshake at all.

Right.  A better solution for the problem would be nice.  Ultimately
we want to check if the other side of the socket is actually a Cygwin
process which knows the secret, not a stray native Windows process
which accidentally hopped on the bandwagon, and we want to exchange
the credentials so a subsequent SO_PEERCRED call returns correct values.


Corinna

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

  reply	other threads:[~2017-01-09 17:16 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-09 13:29 Erik Bray
2017-01-09 14:13 ` Corinna Vinschen
2017-01-09 15:46   ` Erik Bray
2017-01-09 17:16     ` Corinna Vinschen [this message]
2017-01-12 10:59       ` Erik Bray
2017-01-12 22:13         ` Corinna Vinschen
2017-01-13  0:54           ` Michael Enright
2017-01-13  8:42             ` Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170109171635.GB26337@calimero.vinschen.de \
    --to=corinna-cygwin@cygwin.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).