Irresponsible Newb
Irresponsible Newb

Reputation: 613

How to use pseudo-terminals in Linux with C?

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.

Upvotes: 16

Views: 20293

Answers (3)

Suresh Annaiah
Suresh Annaiah

Reputation: 51

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/

Upvotes: 5

sarnold
sarnold

Reputation: 104050

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

Upvotes: 13

wildplasser
wildplasser

Reputation: 44240

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.

Upvotes: 0

Related Questions