如何在带有 C 的 Linux 中使用伪终端?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8374000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:07:06  来源:igfitidea点击:

How to use pseudo-terminals in Linux with C?

clinuxpty

提问by Irresponsible Newb

I'm trying to figure out how to use pseudo-terminal's in linux, essentially I want to create a telnetd clone, something I mentioned in an earlier question.

我试图弄清楚如何在 linux 中使用伪终端,本质上我想创建一个 telnetd 克隆,我在前面的问题中提到

I understand the concept of master and slave terminal, and I have a basic grasp on how to use syscalls in C.

了解主从终端的概念,基本掌握C中如何使用系统调用。

My question concerns the next step after opening a slave / master file descriptor. How to I launch getty in the slave? Are there any goodresources on the net for using the forkpty(), openpty(),or another API?

我的问题涉及打开从/主文件描述符后的下一步。如何在奴隶中启动getty?网络上是否有使用 forkpty()、openpty() 或其他 API 的资源?

Some examples in C would help. This was a very similar question, but no one really provided any examples.

C 中的一些示例会有所帮助。这是一个非常相似的问题,但没有人真正提供任何示例。

采纳答案by sarnold

Advanced Programming in the Unix Environment, 2nd Editionhas a superb chapter on the pseudo-terminal layer available in Linux. The best part is the source codewhich contains a ptydriver and very clearly demonstrates how to use the ptyinterfaces. (The ptyprogram it builds is useful in its own right if you want to drive a terminal-only program programmatically but don't wish to use expect(1).)

Advanced Programming in the Unix Env​​ironment, 2nd Edition有一个关于 Linux 中可用的伪终端层的精彩章节。最好的部分是包含驱动程序的源代码pty并且非常清楚地演示了如何使用pty接口。(pty如果您想以编程方式驱动仅限终端的程序但不想使用expect(1).),那么它构建的程序本身就很有用。

回答by wildplasser

You don't lauch a gettyfor ptys. The getty is only the "listener" part of the process. For hardwired terminals, each individual terminal-device is "listening" constantly. For telnet, the daemon does the listening part(on a socket), and handles connection request by creatinga pty pair, and fork()ing / exec()ing. And indeed: APUE handles ptys very well.

你不会为ptys 发布一个 getty。getty 只是该过程的“侦听器”部分。对于硬接线终端,每个单独的终端设备都在不断地“收听”。对于 telnet,守护进程执行侦听部分(在套接字上),并通过创建pty 对和 fork()ing / exec()ing 来处理连接请求。事实上:APUE 可以很好地处理 pty。

回答by Suresh Annaiah

include

包括

#include <sys/stat.h>

#include <fcntl.h>

#define _XOPEN_SOURCE

#include <stdlib.h>

int main(int argc, char **argv) 
{
char *slavename;
int masterfd;
masterfd = open("/dev/ptmx", O_RDWR);
grantpt(masterfd);
unlockpt(masterfd);
slavename = ptsname(masterfd);
...
}

I posted simple example of demonstrating pseudo terminal master slave concept. please go through this link to get clear understanding of terminals in Linux http://www.linusakesson.net/programming/tty/

我发布了演示伪终端主从概念的简单示例。请通过此链接清楚了解 Linux 中的终端 http://www.linusakesson.net/programming/tty/