PTY / TTY - 只有Slave FD你不能做什么

时间:2021-08-12 01:02:41

Question:

If I have a pty or tty master/slave pair, what can I not do with it if I only have the slave node's file descriptor? Or, put another way: what can I only do if I have the master node's file descriptor?

如果我有一个pty或tty主/从对,如果我只有从节点的文件描述符,我怎么办呢?或者,换句话说:如果我拥有主节点的文件描述符,我该怎么做?

My Current Understanding:

[edit]

[编辑]

I grok the "typical" relationship of a terminal/console/SSH having the master end for interfacing with a human, and one or more program (e.g. a shell and its children processes) being on the slave end. And I (loosely) grok the more unusual(/archaic?) usecases like using a TTY for other kinds of data links, like PPP. This question is not a "I don't get this TTY business" question. I'm asking about the ("low-level"?) "API" stuff: e.g. is there any termios/ioctl manipulations or other programmatic changes to the tty pair that cannot be accomplished if you don't have access to the master FD?

我理解终端/控制台/ SSH的“典型”关系,其具有用于与人交互的主端,以及在从端上的一个或多个程序(例如,shell及其子进程)。我(松散地)了解更多不寻常的(/古老的?)用例,例如将TTY用于其他类型的数据链接,如PPP。这个问题不是“我没有得到这个TTY业务”的问题。我问的是(“低级”?)“API”的东西:例如是否存在对tty对的任何termios / ioctl操作或其他程序更改,如果您无法访问主FD,则无法完成?

[/edit]

[/编辑]

I guess the obvious ones are:

我想明显的是:

  • I can only read/write from the master end if I have the master end's FD.
  • 如果我有主端FD,我只能从主端读/写。
  • grantpt/unlockpt/ptsname can only be used on the master end's FD.
  • grantpt / unlockpt / ptsname只能在主端FD上使用。

Anything else?

还要别的吗?

I've been on/off reading some man pages and experimenting on my Linux machines: the basic stuff one would want to do with a pty (e.g. stty columns 78, etc) seems to work on "either end". But I would suspect there's stuff only a process holding a file descriptor of the master end can do (especially because the master-slave name dichotomy suggests some unilateral control/dominion). And of course since I'm only testing on Linux, there's possible behavior differences between various versions/configurations of Linux and vs. the Unixes, so I don't want to assume that what I'm seeing is portable.

我一直在阅读一些手册页并在我的Linux机器上进行实验:人们想要用pty做的基本内容(例如stty专栏78等)似乎在“两端”上工作。但我怀疑只有一个进程持有主端的文件描述符才能做到(特别是因为主从名称二分法暗示了一些单边控制/统治)。当然,因为我只是在Linux上进行测试,所以Linux和Unix之间的各种版本/配置之间可能存在行为差异,所以我不想假设我所看到的是可移植的。

Motivation

(In case someone wants to know why I want to know)

(如果有人想知道我为什么想知道)

  1. General knowledge/curiosity.
  2. 一般知识/好奇心。
  3. I'm not in love with the current selection of command line tools for working with ptys. Without getting into the details, I've looked at reptyr, ptyget, expect/empty, screen/tmux(/neercs? the one with reptyr-like feature), dtach/abduco, and none of them hit my sweetspot of minimalist versatility. I'm trying to become more informed so I can better evaluate existing solutions and/or better design my own tool(s) to scratch my particular itch.
  4. 我不喜欢当前选择使用ptys的命令行工具。在没有详细介绍的情况下,我看了一下reptyr,ptyget,expect / empty,screen / tmux(/ neercs?具有reptyr-like功能的那个),dtach / abduco,而且没有一个能够达到我极简主义的多功能性。我正在努力变得更加明智,以便我可以更好地评估现有的解决方案和/或更好地设计我自己的工具来划伤我的特殊痒。

1 个解决方案

#1


2  

Thanks to *'s related-questions suggestions and other online searching since asking this, I've found a (partial?) answer:

感谢*的相关问题建议和其他在线搜索问题,我发现了(部分?)答案:

  • Enabling or disabling packet mode on a PTY in Linux can only be done if you have the master FD [See TIOCPKT at this manpage]
  • 只有拥有主FD才能在Linux上的PTY上启用或禁用数据包模式[参见本手册页中的TIOCPKT]
  • Getting the Session ID associated with a TTY in Linux can only be done if you have the master FD (unclear if this is expected/intended behavior) [See TIOCGSID at this manpage]
  • 在Linux中获取与TTY关联的会话ID只能在拥有主FD的情况下完成(不清楚这是否是预期/预期的行为)[参见本手册页中的TIOCGSID]
  • Re-sizing the TTY is only portable from the master FD in practice (a terminal emulator might resize the TTY when it's resized, but an application with just the slave FD has no real certainty that the master size resizes accordingly, or that the terminal driver will even accept a resize from the slave end). [Source]
  • 重新调整TTY的大小只能在实际中从主FD移植(终端仿真器可能会在调整大小时调整TTY的大小,但只有从FD的应用程序不能确定主大小相应地调整大小,或者终端驱动程序甚至会接受来自奴隶端的调整大小)。 [资源]
  • There is a trick for telling if the slave end of a TTY is opened, that you can't do if you don't have the master FD. [Source]
  • 有一个技巧可以告诉TTY的从属端是否打开,如果你没有主FD,你就无法做到。 [资源]

I likely won't accept my own answer unless I learn enough to make my own answer exhaustively answer this question, but I'll try to keep coming back to edit this as I learn more until this or another answer sufficiently answers it.

我可能不会接受我自己的答案,除非我学到足够的答案,让我自己的答案详尽地回答这个问题,但我会尽力回去编辑这个,因为我学到了更多,直到这个或另一个答案充分回答它。

#1


2  

Thanks to *'s related-questions suggestions and other online searching since asking this, I've found a (partial?) answer:

感谢*的相关问题建议和其他在线搜索问题,我发现了(部分?)答案:

  • Enabling or disabling packet mode on a PTY in Linux can only be done if you have the master FD [See TIOCPKT at this manpage]
  • 只有拥有主FD才能在Linux上的PTY上启用或禁用数据包模式[参见本手册页中的TIOCPKT]
  • Getting the Session ID associated with a TTY in Linux can only be done if you have the master FD (unclear if this is expected/intended behavior) [See TIOCGSID at this manpage]
  • 在Linux中获取与TTY关联的会话ID只能在拥有主FD的情况下完成(不清楚这是否是预期/预期的行为)[参见本手册页中的TIOCGSID]
  • Re-sizing the TTY is only portable from the master FD in practice (a terminal emulator might resize the TTY when it's resized, but an application with just the slave FD has no real certainty that the master size resizes accordingly, or that the terminal driver will even accept a resize from the slave end). [Source]
  • 重新调整TTY的大小只能在实际中从主FD移植(终端仿真器可能会在调整大小时调整TTY的大小,但只有从FD的应用程序不能确定主大小相应地调整大小,或者终端驱动程序甚至会接受来自奴隶端的调整大小)。 [资源]
  • There is a trick for telling if the slave end of a TTY is opened, that you can't do if you don't have the master FD. [Source]
  • 有一个技巧可以告诉TTY的从属端是否打开,如果你没有主FD,你就无法做到。 [资源]

I likely won't accept my own answer unless I learn enough to make my own answer exhaustively answer this question, but I'll try to keep coming back to edit this as I learn more until this or another answer sufficiently answers it.

我可能不会接受我自己的答案,除非我学到足够的答案,让我自己的答案详尽地回答这个问题,但我会尽力回去编辑这个,因为我学到了更多,直到这个或另一个答案充分回答它。