1OPENPTY(3)                 Linux Programmer's Manual                OPENPTY(3)
2
3
4

NAME

6       openpty, login_tty, forkpty - terminal utility functions
7

SYNOPSIS

9       #include <pty.h>
10
11       int openpty(int *amaster, int *aslave, char *name,
12                     const struct termios *termp,
13                     const struct winsize *winp);
14       pid_t forkpty(int *amaster, char *name,
15                     const struct termios *termp,
16                     const struct winsize *winp);
17
18       #include <utmp.h>
19
20       int login_tty(int fd);
21
22       Link with -lutil.
23

DESCRIPTION

25       The  openpty()  function  finds an available pseudoterminal and returns
26       file descriptors for the master and slave in amaster  and  aslave.   If
27       name  is  not  NULL, the filename of the slave is returned in name.  If
28       termp is not NULL, the terminal parameters of the slave will be set  to
29       the values in termp.  If winp is not NULL, the window size of the slave
30       will be set to the values in winp.
31
32       The login_tty() function prepares for a login on the terminal  referred
33       to  by  the file descriptor fd (which may be a real terminal device, or
34       the slave of a pseudoterminal as returned by openpty()) by  creating  a
35       new  session,  making  fd  the  controlling  terminal  for  the calling
36       process, setting fd to be the standard input, output, and error streams
37       of the current process, and closing fd.
38
39       The  forkpty() function combines openpty(), fork(2), and login_tty() to
40       create a new process operating in a pseudoterminal.  A file  descriptor
41       referring  to master side of the pseudoterminal is returned in amaster.
42       If name is not NULL, the buffer it points to  is  used  to  return  the
43       filename of the slave.  The termp and winp arguments, if not NULL, will
44       determine the terminal attributes and window size of the slave side  of
45       the pseudoterminal.
46

RETURN VALUE

48       If a call to openpty(), login_tty(), or forkpty() is not successful, -1
49       is returned and  errno  is  set  to  indicate  the  error.   Otherwise,
50       openpty(),  login_tty(),  and  the child process of forkpty() return 0,
51       and the parent process of forkpty() returns the process ID of the child
52       process.
53

ERRORS

55       openpty() fails if:
56
57       ENOENT There are no available terminals.
58
59       login_tty() fails if ioctl(2) fails to set fd to the controlling termi‐
60       nal of the calling process.
61
62       forkpty() fails if either openpty() or fork(2) fails.
63

ATTRIBUTES

65       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
66       tributes(7).
67
68       ┌─────────────────────────────┬───────────────┬────────────────────────┐
69Interface                    Attribute     Value                  
70       ├─────────────────────────────┼───────────────┼────────────────────────┤
71forkpty(), openpty()         │ Thread safety │ MT-Safe locale         │
72       ├─────────────────────────────┼───────────────┼────────────────────────┤
73login_tty()                  │ Thread safety │ MT-Unsafe race:ttyname │
74       └─────────────────────────────┴───────────────┴────────────────────────┘
75

CONFORMING TO

77       These  are  BSD functions, present in glibc.  They are not standardized
78       in POSIX.
79

NOTES

81       The const modifiers were added to the structure  pointer  arguments  of
82       openpty() and forkpty() in glibc 2.8.
83
84       In  versions of glibc before 2.0.92, openpty() returns file descriptors
85       for a BSD pseudoterminal pair; since glibc 2.0.92, it first attempts to
86       open  a  UNIX  98  pseudoterminal pair, and falls back to opening a BSD
87       pseudoterminal pair if that fails.
88

BUGS

90       Nobody knows how much space should be reserved for name.   So,  calling
91       openpty() or forkpty() with non-NULL name may not be secure.
92

SEE ALSO

94       fork(2), ttyname(3), pty(7)
95

COLOPHON

97       This  page  is  part of release 5.12 of the Linux man-pages project.  A
98       description of the project, information about reporting bugs,  and  the
99       latest     version     of     this    page,    can    be    found    at
100       https://www.kernel.org/doc/man-pages/.
101
102
103
104GNU                               2021-03-22                        OPENPTY(3)
Impressum