delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2018/11/01/16:56:42

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:from:reply-to:to:subject:date:message-id
:content-type:content-transfer-encoding:mime-version; q=dns; s=
default; b=DgM9Mhdc62SKeQVjEajy6vYHpgc9AjwN3/mvUtk+0j0gNWjhWjGJz
GA684u18jHWcPLDCNf+H9Cpsc3ME5o2D8IvLgDm+DZ5iwWrXSdLLOIakij/PdGar
R6YpkzInJ7Fuk2JtgFyrVYGAk6yvSdE1ESgxRueHU8hFJfy5nht7Js=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:from:reply-to:to:subject:date:message-id
:content-type:content-transfer-encoding:mime-version; s=default;
bh=TSNfnnjJwSzPdF0nMPIJkscVUu4=; b=qlKNgr9nNFapHGlwR3lzgi2597o+
Br/lyOknsbaLXjjbf3rLHysaZsIi9Nbjfpms8sugFvj35xi9WMFshrVIBmOd19UK
Ol6I2Ava1H0psdohXCoYmpHcdCKbMBlJFbhUVUFJFur8Md0uT8DKGhdwXd23B0kg
T3S6vVoXIvFw0PA=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.3.2 spammy=H*F:D*gov, CYGWIN, UD:nih.gov, Lavrentiev
X-HELO: nihcesxway2.hub.nih.gov
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nih.gov; i=@nih.gov; q=dns/txt; s=NIH; t=1541105790; x=1572641790; h=from:to:subject:date:message-id: content-transfer-encoding:mime-version; bh=H4G5nj1OAty8bSzvNK1TQxn9mqALjLSWvGnOKD4X9FU=; b=Jz+OZTzB4QMh+W+DKq03pky9rpdWFsON6m4TMr+vRfizFgjT+0W303Kp 5D1N6ZTbSIDqk2VUzzYxlMkVFceB/3cn8JkCz3quWG4bAmcH2bJrv91Ib 1fCd2z66uw6e0lc+9mC19FxkKa3bTosHXJ0tvpgnfAWtVmqaXn7VpOQl1 mbj/g7Hvcm+uJ8KM7YkY6LODPkNj/QYct3ljkht2iGdvHaTNX0PSgU/g+ SOZxBLBI2T4z3b0vEbSStcPTc1bxntBm2qOxwOJ3QPpHO0cQQ3KxmLvcb tG6K+49ZlyjwG1HixnCeYrfXvxRTDAy+56/7SAvrnmUYNZYCN9XgAqD61 w==;
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nih.onmicrosoft.com; s=selector1-ncbi-nlm-nih-gov; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=H4G5nj1OAty8bSzvNK1TQxn9mqALjLSWvGnOKD4X9FU=; b=Eoz+++hhilwjNUjQkHg+ZFVH3YDLSiQ8IZMGnhuSsaYpiY65SPMaylnETIFxI0bKfXN9/gCVOcmaewixleYn0N+1+FRRfG0iS9sOg8W2qIVUt73Ae1jNskaLyuHY2FtXGg2Q1Zg3Lo/3gKeGDcorUPHHM1zrZf/ygnFgFEw67/U=
From: "Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin" <cygwin AT cygwin DOT com>
Reply-To: "Lavrentiev, Anton (NIH/NLM/NCBI) [C]" <lavr AT ncbi DOT nlm DOT nih DOT gov>
To: "'cygwin AT cygwin DOT com'" <cygwin AT cygwin DOT com>
Subject: SOCK_NONBLOCK not honored
Date: Thu, 1 Nov 2018 20:56:22 +0000
Message-ID: <SN6PR09MB3247DE8659997EDB93309403A5CE0@SN6PR09MB3247.namprd09.prod.outlook.com>
authentication-results: spf=none (sender IP is ) smtp.mailfrom=lavr AT ncbi DOT nlm DOT nih DOT gov;
MIME-Version: 1.0
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id wA1Kue7s003645

Hi,

Looks like CYGWIN defines but does not honor the SOCK_NONBLOCK flag when used with socket(2).

(It also defines SOCK_CLOEXEC but I haven't checked whether it is honored -- full disclosure.)

Consider the following code:

$ cat bug.c
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>


#ifdef BUG
#  define SOCK_TYPE  (SOCK_STREAM | SOCK_NONBLOCK)
#else
#  define SOCK_TYPE   SOCK_STREAM
#endif


int main()
{
    char buf[80];
    ssize_t n;
    struct sockaddr_in in;
    int s = socket(AF_INET, SOCK_TYPE, 0);

#ifndef BUG
    fcntl(s, F_SETFL, O_NONBLOCK);
#endif

    memset(&in, 0, sizeof(in));
    in.sin_family      = AF_INET;
    in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    in.sin_port        = htons(6666);

    if (connect(s, (struct sockaddr*) &in, sizeof(in)) < 0) {
        int err;
        socklen_t len = sizeof(err);
        struct pollfd pfd;
        if (errno != EINPROGRESS) {
            printf("connect failed immediately: %m\n");
            return 1;
        }
        memset(&pfd, 0, sizeof(pfd));
        pfd.fd = s;
        pfd.events = POLLOUT;
        if (!poll(&pfd, 1, 1000)) {
            printf("poll timed-out\n");
            return 2;
        }
        if (pfd.revents & (POLLERR | POLLHUP)) {
            printf("poll failed 0x%04X\n", pfd.revents);
            return 3;
        }
        if (getsockopt(s, SOL_SOCKET, SO_ERROR, (void*) &err, &len) != 0) {
            printf("getsockopt failed: %m\n");
            return 4;
        }
        if (err != 0) {
            printf("connect failed: %s\n", strerror(err));
            return 5;
        }
    }
    errno = 0;
    n = recv(s, buf, sizeof(buf), 0);
    printf("Got %zd byte(s): %m\n", n);
    return 0;
}

When the program is built on Linux with and without the macro BUG defined, the result is all the same,
the last recv() correctly finishes with -1 and EAGAIN (run "nc -l 6666" as a server).

$ gcc -Wall -o bug bug.c
$ ./bug
Got -1 byte(s): Resource temporarily unavailable
$ gcc -Wall -DBUG -o bug bug.c
$ ./bug
Got -1 byte(s): Resource temporarily unavailable

However, on CYGWIN when the code is compiled with -DBUG, the last recv() becomes blocking, and the program never exits:

$ gcc -Wall -o bug bug.c
$ ./bug
Got -1 byte(s): Resource temporarily unavailable
$ gcc -Wall -DBUG -o bug bug.c
$ ./bug
...

strace confirms that the recv() has blocked the execution.  Also, connect() appears to be blocking, too, but it's harder to see.

Thanks,
Anton Lavrentiev


--
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


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019