When pty [Pseudo terminal] slave fd settings are changed by “tcsetattr” , how can the master end capture this event without delay?

跟風遠走 提交于 2019-12-01 05:15:18

Yes, this is possible (in Linux and pre-2004 FreeBSD) , using the pty in packet mode and setting the EXTPROC flag on it.

  • Packet mode is enabled on the master end by ioctl(master, TIOCPKT, &nonzero). Now every read on the master end will yield a packet: whatever was written on the other side, plus a status byte that says something about the situation on the slave end ("The write queue for the terminal (i.e. the slave end) is flushed")

  • This doesn't mean, however, that the master is immediately aware of changes on the slave end, e.g. a select() on the master will not return as soon as those changes happen, only when there is something to read

  • However, after setting EXTPROC in the pty's local flag word - using tcsetattr() - select() will return as soon as the slave state changes, and one can then inspect the slaves termios - either directly via the slave fd which we keep open in the parent process, or, on linux at least, simply by tcgetattr() on the master side.

  • Note that EXTPROC disables certain parts of the pty driver, e.g. local echoing is turned off.

EXTPROC is not used a lot (here is a possible use case), not very portable and not documented at all (it shoud at least have been somewhere in the linux termios or tty_ioctl manpages). Looking at the linux kernel source drivers/tty/n_tty.c I conclude that any change in the slaves termios structure will make the select() on the master end return - but a tcsetattr() that doesn't change anything won't.

Edit: in response to J.F. Sebastians's request below I give an example program that should make clear how to use EXTRPOC in packet mode on a linux machine:

/* Demo program for managing a pty in packet mode with the slave's
** EXTPROC bit set, where the master gets notified of changes in the
** slaves terminal attributes
**   
** save as extproc.c, compile with gcc -o extproc extproc.c -lutil
*/



#include <stdio.h>
#include <pty.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

#define BUFSIZE 512

void main() {
  int master;      // fd of master side
  pid_t pid;
  if((pid = forkpty(&master, NULL, NULL, NULL))) { // we're parent
    fd_set rfds, xfds;
    int retval, nread, status = 0, nonzero = 1;
    char buf[BUFSIZE];

    ioctl(master, TIOCPKT, &nonzero); // initiate packet mode - necessary to get notified of
                                      // ioctl() on the slave side
    while(1) {
      // set stdout unbuffered (we want to see stuff as it happens)                                                                                 
      setbuf(stdout, NULL);

      // prepare the file descriptor sets
      FD_ZERO(&rfds);
      FD_SET(master, &rfds);

      FD_ZERO(&xfds);
      FD_SET(master, &xfds);

      // now wait until status of master changes     
      printf("---- waiting for something to happen -----\n");
      select(1 + master, &rfds, NULL, &xfds, NULL);

      char *r_text = (FD_ISSET(master, &rfds) ? "master ready for reading" : "- ");
      char *x_text = (FD_ISSET(master, &xfds) ? "exception on master" : "- ");

      printf("rfds: %s, xfds: %s\n", r_text, x_text);
      if ((nread = read(master, buf, BUFSIZE-1)) < 0) 
        perror("read error");
      else {         
        buf[nread] = '\0';
        // In packet mode *buf will be the status byte , and buf + 1 the "payload"
        char *pkt_txt = (*buf &  TIOCPKT_IOCTL ? " (TIOCPKT_IOCTL)" : "");
        printf("read %d bytes: status byte %x%s, payload <%s>\n", nread, *buf, pkt_txt, buf + 1);
      }
      if (waitpid(pid, &status, WNOHANG) && WIFEXITED(status)) {
        printf("child exited with status %x\n", status);
        exit(EXIT_SUCCESS);
      } 
    }
  } else { // child
    struct termios tio;

    // First set the EXTPROC bit in the slave end termios structure
    tcgetattr(STDIN_FILENO, &tio);
    tio.c_lflag |= EXTPROC;
    tcsetattr(STDIN_FILENO, TCSANOW, &tio); 

    // Wait a bit and do an ordinary write()
    sleep(1);
    write(STDOUT_FILENO,"blah", 4);

    // Wait a bit and change the pty terminal attributes. This will be picked up by the master end
    sleep(1);
    tio.c_cc[VINTR] = 0x07; 
    tcsetattr(STDIN_FILENO, TCSANOW, &tio);

    // Wait a bit and exit
    sleep(1);
  } 
}

The output will be something like:

---- waiting for something to happen -----
rfds: master ready for reading, xfds: exception on master
read 1 bytes: status byte 40 (TIOCPKT_IOCTL), payload <>
---- waiting for something to happen -----
rfds: master ready for reading, xfds: - 
read 5 bytes: status byte 0, payload <blah>
---- waiting for something to happen -----
rfds: master ready for reading, xfds: exception on master
read 1 bytes: status byte 40 (TIOCPKT_IOCTL), payload <>
---- waiting for something to happen -----
rfds: master ready for reading, xfds: - 
read error: Input/output error
child exited with status 0

In general, the master end of a pty need not be a tty (and have an associated termios structure), but under Linux, it is (with a shared termios: changes on one end will change both termios structures at the same time).

This means that we can modify the example program above and set EXTPROC on the master side, it won't make any difference.

All of this is not documented as far as I can see, and even less portable, of course.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!