How to use pseudo-terminals in Linux with C?

霸气de小男生 提交于 2019-11-26 17:14:43

问题


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.

I understand the concept of master and slave terminal, and I have a basic grasp on how to use syscalls in 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?

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


回答1:


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).)




回答2:


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/




回答3:


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.



来源:https://stackoverflow.com/questions/8374000/how-to-use-pseudo-terminals-in-linux-with-c

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