Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pseudo-terminals in Linux with C?

Tags:

c

linux

pty

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.

like image 921
Irresponsible Newb Avatar asked Dec 04 '11 07:12

Irresponsible Newb


People also ask

What is a pseudo-terminal in Linux?

A pseudo-terminal is a special interprocess communication channel that acts like a terminal. One end of the channel is called the master side or master pseudo-terminal device, the other side is called the slave side.

What is pty in C?

DESCRIPTION top. A pseudoterminal (sometimes abbreviated "pty") is a pair of virtual character devices that provide a bidirectional communication channel. One end of the channel is called the master; the other end is called the slave.

How do I open pseudo-terminal?

You can use the os. openpty() to open a new pseudo-terminal pair using Python. This method gives a pair of file descriptors (master, slave), for the master and the slave end, respectively.

What is Unix pty?

In some operating systems, including Unix and Linux, a pseudoterminal, pseudotty, or PTY is a pair of pseudo-device endpoints (files) which establish asynchronous, bidirectional communication (IPC) channel (with two ports) between two or more processes.


1 Answers

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

like image 110
sarnold Avatar answered Oct 05 '22 07:10

sarnold