C plug new tty

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

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

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

With a small program in C, I open / dev / ptmx as follows:

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

But there are no new ptys created in / dev / pts /.

+4
source share
2 answers

To create a useful pty pair, you must also call grantpt (3) and unlockpt (3) on the fd returned by the open call. It does not specify exactly where in this process the actual slave pty node file is created in the file system - some systems (those where / dev / pts is a special file system, as a rule) create it open, while others will create it as part of a grantpt or unlockpt call. It also does not guarantee that the slave will be in / dev / pts - it could be somewhere else - so you need to call ptsname (3) to find out where it is.

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

+4
source

Here is a good tutorial on the topic: Using pseudo-terminals to manage interactive programs, pty, pdip

In particular, see an example of a source in the middle of the page under the heading "Inter-process communication through a pseudo-terminal." This is an example of a process that develops itself, then the two processes communicate with each other via PTY, which previously opened the parent process.

+3
source

Source: https://habr.com/ru/post/1397450/


All Articles