如何在C语言中使用Linux中的伪终端?

时间:2021-02-04 21:01:35

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 good resources on the net for using the forkpty(), openpty(),or another API?

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

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

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

3 个解决方案

#1


9  

Advanced Programming in the Unix Environment, 2nd Edition has a superb chapter on the pseudo-terminal layer available in Linux. The best part is the source code which contains a pty driver and very clearly demonstrates how to use the pty interfaces. (The pty program 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).)

Unix环境中的高级编程,第2版对Linux中可用的伪终端层有一个很好的章节。最好的部分是包含pty驱动程序的源代码,并且非常清楚地演示了如何使用pty接口。 (如果你想以编程方式驱动一个仅限终端的程序但不希望使用expect(1),它构建的pty程序本身就很有用。)

#2


3  

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/

#3


0  

You don't lauch a getty for 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 creating a pty pair, and fork()ing / exec()ing. And indeed: APUE handles ptys very well.

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

#1


9  

Advanced Programming in the Unix Environment, 2nd Edition has a superb chapter on the pseudo-terminal layer available in Linux. The best part is the source code which contains a pty driver and very clearly demonstrates how to use the pty interfaces. (The pty program 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).)

Unix环境中的高级编程,第2版对Linux中可用的伪终端层有一个很好的章节。最好的部分是包含pty驱动程序的源代码,并且非常清楚地演示了如何使用pty接口。 (如果你想以编程方式驱动一个仅限终端的程序但不希望使用expect(1),它构建的pty程序本身就很有用。)

#2


3  

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/

#3


0  

You don't lauch a getty for 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 creating a pty pair, and fork()ing / exec()ing. And indeed: APUE handles ptys very well.

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