public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: cygwin@cygwin.com
Subject: Re: Opening a socket connection in a fork ?
Date: Tue, 22 Sep 2009 14:24:00 -0000	[thread overview]
Message-ID: <20090922142423.GT20981@calimero.vinschen.de> (raw)
In-Reply-To: <cef27f050909161316x1f38676bie677bbdeae0bf0c@mail.gmail.com>

On Sep 16 22:16, Gu1ll4um3r0m41n wrote:
> Hello,
> I'm trying to run a program on cygwin that forks, opens a socket
> connection and sends the status of the connection to the parent
> process using pipes. The parent process then sends data through the
> opened socket.
> The program works well on linux but on cygwin, i cannot use recv on a
> socket opened in a fork. The recv function always return -1, errno 22
> ("EINVAL").
> The weird thing is that the socket stays open and send still works...
> 
> I attached a short example.
> I don't know if this is a bug or a limitation due to the fork()
> implementation in cygwin... I searched on google but couldn't find
> anything really useful.

It looks like a really weird problem in WinSock.  Cygwin doesn't do
anything special in this case.

The socket handle gets duplicated into the child process on fork.  The
child calls connect on the duplicated socket handle which apparently
succeeds.

In this scenario, calls to WinSock's recvfrom and WSARecvFrom in the
parent will fail with WSAEINVAL, regardless whether both address
parameters, name and namelen, are NULL or point to valid storage.
However, calls to recv and WSARecv succeed as expected.

Per MSDN, WSAEINVAL in the context of recv means  "The socket has not
been bound".  It is as if the recvfrom functions test if the socket is
bound locally, but in the parent process, WinSock doesn't know about
that and fails, while the same test is omitted in the recv functions.

The same problem does not exist for sendto/WSASendTo apparently.

I'm going to apply a patch which calls WSARecv rather than WSARecvFrom
if the name parameter is NULL.  This fixes the above problem and, given
that the `!wsamsg->name' check has to be called anyway to workaround
another WinSock problem, has no performance hit.

I also created a native Win32 testcase which proves that this isn't a
Cygwin problem, but a WinSock problem.


Thanks for the report,
Corinna

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

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

      reply	other threads:[~2009-09-22 14:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-16 20:16 Gu1ll4um3r0m41n
2009-09-22 14:24 ` Corinna Vinschen [this message]

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=20090922142423.GT20981@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).