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/.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With