How to use pseudo-terminals in Linux with C?

16,384

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

Solution 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/

Share:
16,384

Related videos on Youtube

Irresponsible Newb
Author by

Irresponsible Newb

Updated on June 04, 2022

Comments

  • Irresponsible Newb
    Irresponsible Newb almost 2 years

    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.

  • sarnold
    sarnold about 10 years
    @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. :)
  • EdH
    EdH about 9 years
    This is the simple straightforward answer. It's documented here: linux.die.net/man/4/ptmx
  • sarnold
    sarnold almost 8 years
    @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.
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com almost 8 years
    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
    Ciro Santilli OurBigBook.com almost 8 years
    kerrisk's linux programming interface also has a chapter on it: github.com/cirosantilli/linux-programming-interface-kerrisk/‌​…