[net.unix-wizards] Problem with pseudo ttys on Sun.

richb@yarra.OZ (Rich Burridge) (07/06/86)

Help is desperately needed with the following "problem".

I am running Sun 3.0 (Generic) on a Sun 3/50 with an 112 Meg shoebox.

I am trying to use the pseudo ttys ttyq0-ttyq6 to transfer data from other
programs into my program.

Inside my program I open say, ptyq5 (the MASTER pty) with:

      int fpty ;

      if (fpty = open("/dev/ptyq5",2)) < 0)
        {
          fprintf(stderr,"can't open pseudo\n") ;
          exit(1) ;
        }

I then set it into RAW mode with:

      struct sgttyb pty ;

      gtty(fpty,&pty) ;
      pty.sg_flags |= RAW ;
      stty(fpty,&pty) ;

Now to test if there is any thing to read on that pty, I use the select call.

      int readmask,nfds ;
      static struct timeval timeout = { 0,0 } ;

      readmask = 1 << fpty ;
      nfds = select(32,&readmask,(int *)0,(int *)0,&timeout) ;

Say I used 'cat' to redirect a small file to /dev/ttyq5, as this was happening
nfds returned a 1 to indicate that there was a file descriptor with 
outstanding data to be read, and the readmask had the appropriate bit set.

So, I read the data from the pseudo tty with:

      #define  MAXDATA  32

      char buffer[MAXDATA] ;
      int nread ;

      if (nfds ==1)
        nread = read(fpty,buffer,MAXDATA) ;

nread normally contains the number of bytes read (which will be MAXDATA, up
until the "last" time which will return a smaller value; the remainder left
to read).
Now what I would expect is that when I come to do the select call again, it
should return 0 and the readmask should also be zero. But no, it tells me
that there is data to be read, and when I use the read call as above, it
returns -1 in nread with errno set to 5 (EIO I/O error).

Where am I going wrong?

Please answer by mail, as I'm about a week behind reading the net.

Thanks in advance.

Rich Burridge            ISD:  +61 3 267-6222
Sun Australia            STD:  (03) 267-6222
14 Queens Rd,            ARPA: richb%yarra.oz@seismo.arpa
Melbourne, VIC 3004.     UUCP: seismo!munnari!yarra.oz!richb
AUSTRALIA.               ACS:  richb@yarra.oz
D

richb@yarra.OZ (Rich Burridge) (07/07/86)

In article <3@yarra.OZ>, richb@yarra.OZ, I write:
> 
> Say I used 'cat' to redirect a small file to /dev/ttyq5, as this was happening
> nfds returned a 1 to indicate that there was a file descriptor with 
> outstanding data to be read, and the readmask had the appropriate bit set.
> 
> nread normally contains the number of bytes read (which will be MAXDATA, up
> until the "last" time which will return a smaller value; the remainder left
> to read).
> Now what I would expect is that when I come to do the select call again, it
> should return 0 and the readmask should also be zero. But no, it tells me
> that there is data to be read, and when I use the read call as above, it
> returns -1 in nread with errno set to 5 (EIO I/O error).
> 
> Where am I going wrong?
> 

Steve Schoch at the NASA Ames Research Center provides the solution:

What the problem is is that after the 'cat' is done writing the file to
/dev/ttyq5, it exits, which closes that tty.  When you close a tty file for
the last time it causes it to hang up.  If this was a real tty, it would
drop DTR, but on a pseudo tty, it show that it is hung up by having the read
fail on the master side.

Select is correct in returning, and what you should do at this point is to
close the master side, as it is no use keeping it open past this point.


Many thanks Steve, nice to see somebody else works on a Sunday :-)

Regards Rich.

Rich Burridge            ISD:  +61 3 267-6222
Sun Australia            STD:  (03) 267-6222
14 Queens Rd,            ARPA: richb%yarra.oz@seismo.arpa
Melbourne, VIC 3004.     UUCP: seismo!munnari!yarra.oz!richb
AUSTRALIA.               ACS:  richb@yarra.oz
D