16

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.

Community
  • 1
  • 1
Irresponsible Newb
  • 603
  • 2
  • 7
  • 15

3 Answers3

13

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

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • @JérémyPouyet, thanks for the notice; I've fixed the link to point to the second edition code (which is the code I'm familiar with) -- perhaps the third edition would be the better choice, but I haven't personally read that code yet. Some day. :) – sarnold Feb 22 '14 at 00:29
  • @CiroSantilli巴拿馬文件六四事件法轮功, sorry, I cannot find a license. Neither the second edition tarball nor the third edition tarball specify a license, the FAQs on the website do not specify a license. There is a `DISCLAIMER` file in the top level of the tarball which suggests it's intended to be used and useful but disclaims all liability for all consequences. – sarnold Jun 29 '16 at 19:48
  • Yeah, that's about what I've found too. Maybe I'll email the author, if open that has to go to GitHub :-) – Ciro Santilli OurBigBook.com Jun 29 '16 at 19:53
  • kerrisk's linux programming interface also has a chapter on it: https://github.com/cirosantilli/linux-programming-interface-kerrisk/tree/c02ede2d146e0529f3b01d513a4d3894a1761f08/pty – Ciro Santilli OurBigBook.com Jul 01 '16 at 12:40
5

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/

  • This is the simple straightforward answer. It's documented here: http://linux.die.net/man/4/ptmx – EdH Apr 13 '15 at 03:16
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.

wildplasser
  • 43,142
  • 8
  • 66
  • 109