delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2013/12/16/08:21:34

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:content-type:content-transfer-encoding
:subject:message-id:date:to:mime-version; q=dns; s=default; b=FK
K55mR+hYZYgO/Qgx7JrGrj5aQIJYOcgYjgk7lr/2EinMqV1seeSpBRhlieQJAzBW
v7X0rAsZZ2jHkRa0oH8rUYIBIOi36pRqVRJUyFnfPmyB9SBAYlQvXlXTb5/NfE1l
XW14NrlMXohH/95iYiy/bKYCAFdBn1To1uK7aA8sw=
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:content-type:content-transfer-encoding
:subject:message-id:date:to:mime-version; s=default; bh=9AyqskWI
4vXhrycwqNW2oSzZURQ=; b=V/NS3LSn6CI9YqZI1eCAm7xQ4TKroCRau1HoZTun
LhZ93WyapRj983bpNdUu6PvNwS7GVvS1gJXQPgCh1j6a4MFFxTanzW/IAdrpTjxW
LKkIy9M1TIZxjfFatbFSgZ1tEgO8UAjYCesocVTCT8gUpAiil0RYnfBzSCpLtFMP
09M=
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-Virus-Found: No
X-Spam-SWARE-Status: No, score=2.4 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,RP_MATCHES_RCVD,SPF_PASS autolearn=ham version=3.3.2
X-HELO: rcpt-expgw.biglobe.ne.jp
X-Biglobe-Sender: <takimoto-j AT kba DOT biglobe DOT ne DOT jp>
From: "Jun T." <takimoto-j AT kba DOT biglobe DOT ne DOT jp>
Subject: moving pty master by dup()/close() causes read() to fail
Message-Id: <83119D07-3F2D-4658-991D-9A756F1255A9@kba.biglobe.ne.jp>
Date: Mon, 16 Dec 2013 22:21:12 +0900
To: cygwin AT cygwin DOT com
Mime-Version: 1.0 (Mac OS X Mail 6.6 \(1510\))
X-IsSubscribed: yes

Hi all. I'm new to this list.

The test code at the end of this post has some problem on Cygwin.
What the code does are:
  use forkpty() to open pty master (and slave for child),
  write a character 'A' to the slave,
  duplicate the master file descriptor,
  and read() from the (duplicated) master.

If I run this code on either cygwin or cygwin64, I get:

$ ./a.exe
c = A
$ ./a.exe foo      # any argument is OK. just to make argc>1
read: Bad file descriptor

If no arg is given (argc=1) then the read() succeeds, while
if argc>1, in which case **the original master is closed**,
then read() fails with "Bad file descriptor".

Am I doing something stupid?

The code runs normally (read() never fails) on Linux and Mac OS X.
I tried dup2() or fcntl(oldfd,F_DUPFD) to no avail. Also tried
open("/dev/ptmx") to open the master but it didn't work either.


#include <stdio.h>
#ifdef __APPLE__
#include <util.h>
#else
#include <pty.h>
#endif
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
    int fd;
    pid_t pid = forkpty(&fd, 0, 0, 0);
    if(pid < 0) {
        perror("forkpty");
        return 1;
    }
    else if(pid==0) {   /* child */
        close(fd);
        write(1, "A", 1);
        exit(0);
    }
    else {              /* parent */
        char c;
        int oldfd = fd;
        fd = dup(oldfd);
        if(fd < 0) {
            perror("dup");
            return 2;
        }
        if(argc > 1)
            close(oldfd); /* this causes a trouble */

        if(read(fd, &c, 1) == 1)
            fprintf(stderr, "c = %c\n", c);
        else
            perror("read");
        close(fd);
        return 0;
    }
}



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