2

How can terminal emulator read from ptm device while it missing read function? There is a PTY driver: https://github.com/torvalds/linux/blob/master/drivers/tty/pty.c. I see pty_write function, but can't see something like pty_read() function. As I understand, there's no need of read function for pty slave because it is a TTY device which has its own read buffer. So the corresponding method for reading in tty line discipline: https://github.com/torvalds/linux/blob/master/drivers/tty/n_tty.c#L2132.

But what about a master device? How can terminal emulator read from this device while it is not even a generic tty device?

1

A read from a master pty will be dispatched to the same n_tty_read(), ie the same N_TTY line discipline is attached to the master as to the slave pty.

The whole trace of a read syscall is something like:

sys_read() -> ksys_read() -> vfs_read() -> __vfs_read()
  -> file->f_op->read = tty_fops.read -> tty_read()
    -> ld->ops->read = n_tty_ops.read -> n_tty_read()

and the line discipline is attached to a master pty via

ptmx_open()
  -> tty_init_dev()
     -> alloc_tty_struct()
        -> tty_ldisc_init()
           -> tty_ldisc_get(tty, N_TTY)

The situation may be confused by the fact that

a) the termios config used by a master pty is set to raw/passthrough, so it may appear like the line discipline is not even there ;-) (see the ptm_driver->init_termios... initializations in unix98_pty_init()).

b) all the termios ioctl (tcsetattr() -> ioctl(TCSETS), etc) will act on the slave's termios config even when called on the master pty (see tty_mode_ioctl() in drivers/tty/tty_ioctl.c).

  • If master pty do has associated ldisc, then can we set its termios from userspace? If no, then it would be completely the same as "it doesn't even exist". – 炸鱼薯条德里克 Jul 13 at 2:17
  • Have you read the point b) of my answer? All the termios ioctls on a master pty fd will operate on the slave. – mosvy Jul 13 at 2:19
  • Yes, I am just asking if there's another possibility to set that from userland. Seems that it's a on-purpose design of linux kernel. – 炸鱼薯条德里克 Jul 13 at 2:23
  • No, there isn't. All the line discipline / termios ioctls act on the slave, even when called on the master. – mosvy Jul 13 at 2:35
  • Thanks for answer, @mosvy. But I can't see how ldics is attached to the slave device. Where can I find corresponding code? – TwITe Jul 13 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.