public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* moving pty master by dup()/close() causes read() to fail
@ 2013-12-16 13:21 Jun T.
  2013-12-16 20:39 ` Christopher Faylor
  0 siblings, 1 reply; 3+ messages in thread
From: Jun T. @ 2013-12-16 13:21 UTC (permalink / raw)
  To: cygwin

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: moving pty master by dup()/close() causes read() to fail
  2013-12-16 13:21 moving pty master by dup()/close() causes read() to fail Jun T.
@ 2013-12-16 20:39 ` Christopher Faylor
  2013-12-18  4:05   ` Christopher Faylor
  0 siblings, 1 reply; 3+ messages in thread
From: Christopher Faylor @ 2013-12-16 20:39 UTC (permalink / raw)
  To: cygwin

On Mon, Dec 16, 2013 at 10:21:12PM +0900, Jun T. wrote:
>Hi all. I'm new to this list.
>
>The test code at the end of this post has some problem on Cygwin.

Thanks for the test code.  I can duplicate the problem and am looking
into it.

cgf

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: moving pty master by dup()/close() causes read() to fail
  2013-12-16 20:39 ` Christopher Faylor
@ 2013-12-18  4:05   ` Christopher Faylor
  0 siblings, 0 replies; 3+ messages in thread
From: Christopher Faylor @ 2013-12-18  4:05 UTC (permalink / raw)
  To: cygwin

On Mon, Dec 16, 2013 at 03:39:54PM -0500, Christopher Faylor wrote:
>On Mon, Dec 16, 2013 at 10:21:12PM +0900, Jun T. wrote:
>>Hi all. I'm new to this list.
>>
>>The test code at the end of this post has some problem on Cygwin.
>
>Thanks for the test code.  I can duplicate the problem and am looking
>into it.

This should be fixed in the upcoming snapshot and, of course, in the
next release.

http://cygwin.com/snapshots/

Thanks again for the test case.  That always helps immensely when tracking
down problems.

cgf

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-12-18  4:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-16 13:21 moving pty master by dup()/close() causes read() to fail Jun T.
2013-12-16 20:39 ` Christopher Faylor
2013-12-18  4:05   ` Christopher Faylor

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