Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Fork a new tty

Tags:

c

file-io

tty

pty

I have to create a new pair of tty (master and slave) without using forkpty().

In the man of pts(4), it is written that :

When a process opens /dev/ptmx, it gets a file descriptor for a pseudo-terminal master (PTM), and a pseudo-terminal slave (PTS) device is created in the /dev/pts directory.

With a little program in C, I open /dev/ptmx like that :

open("/dev/ptmx", O_RDWR);

But there is no new pty created in /dev/pts/.

like image 374
Jeffrey Muller Avatar asked Feb 20 '12 15:02

Jeffrey Muller


2 Answers

Here is a good tutorial on the topic: Using pseudo-terminals to control interactive programs, pty, pdip. It this link ends in an error 403, here is another one: http://rkoucha.fr/tech_corner/pty_pdip.html

Particularly, look at the sample source at the middle of the page, under title “Inter-process communication through a pseudo-terminal”. That's an example of a process which fork it‑self, then the two processes communicate each‑others via a PTY the parent process priorly opened.

like image 94
Hibou57 Avatar answered Sep 25 '22 13:09

Hibou57


To actually create a usable pty pair, you must also call grantpt(3) and unlockpt(3) on the fd returned by the open call. Its not well-defined exactly where in that process the actual slave pty file node in the filesystem is created -- some systems (those where /dev/pts is a special filesystem, usually) will create it on the open, while others will create it as part of the grantpt or unlockpt call. Its also not guarenteed that the slave will be in /dev/pts -- it might be somewhere else -- so you need to call ptsname(3) to find out where it is.

It also may be slightly more portable to call posix_openpt(3) rather than open directly.

like image 35
Chris Dodd Avatar answered Sep 21 '22 13:09

Chris Dodd