NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |STANDARDS |HISTORY |BUGS |SEE ALSO |COLOPHON | |
openpty(3) Library Functions Manualopenpty(3)openpty, login_tty, forkpty - terminal utility functions
System utilities library (libutil,-lutil)
#include <pty.h>int openpty(int *amaster, int *aslave, char *name,const struct termios *termp,const struct winsize *winp);pid_t forkpty(int *amaster, char *name,const struct termios *termp,const struct winsize *winp);#include <utmp.h>int login_tty(intfd);
Theopenpty() function finds an available pseudoterminal and returns file descriptors for the master and slave inamaster andaslave. Ifname is not NULL, the filename of the slave is returned inname. Iftermp is not NULL, the terminal parameters of the slave will be set to the values intermp. Ifwinp is not NULL, the window size of the slave will be set to the values inwinp. Thelogin_tty() function prepares for a login on the terminal referred to by the file descriptorfd (which may be a real terminal device, or the slave of a pseudoterminal as returned byopenpty()) by creating a new session, makingfd the controlling terminal for the calling process, settingfd to be the standard input, output, and error streams of the current process, and closingfd. Theforkpty() function combinesopenpty(),fork(2), andlogin_tty() to create a new process operating in a pseudoterminal. A file descriptor referring to master side of the pseudoterminal is returned inamaster. Ifname is not NULL, the buffer it points to is used to return the filename of the slave. Thetermp andwinp arguments, if not NULL, will determine the terminal attributes and window size of the slave side of the pseudoterminal.
If a call toopenpty(),login_tty(), orforkpty() is not successful, -1 is returned anderrno is set to indicate the error. Otherwise,openpty(),login_tty(), and the child process offorkpty() return 0, and the parent process offorkpty() returns the process ID of the child process.
openpty() fails if:ENOENTThere are no available terminals.login_tty() fails ifioctl(2) fails to setfd to the controlling terminal of the calling process.forkpty() fails if eitheropenpty() orfork(2) fails.
For an explanation of the terms used in this section, seeattributes(7). ┌───────────────────────┬───────────────┬────────────────────────┐ │Interface│Attribute│Value│ ├───────────────────────┼───────────────┼────────────────────────┤ │forkpty(),openpty() │ Thread safety │ MT-Safe locale │ ├───────────────────────┼───────────────┼────────────────────────┤ │login_tty() │ Thread safety │ MT-Unsafe race:ttyname │ └───────────────────────┴───────────────┴────────────────────────┘
BSD.
Theconstmodifiers were added to the structure pointer arguments ofopenpty() andforkpty() in glibc 2.8. Before glibc 2.0.92,openpty() returns file descriptors for a BSD pseudoterminal pair; since glibc 2.0.92, it first attempts to open a UNIX 98 pseudoterminal pair, and falls back to opening a BSD pseudoterminal pair if that fails.
Nobody knows how much space should be reserved forname. So, callingopenpty() orforkpty() with non-NULLname may not be secure.
fork(2),ttyname(3),pty(7)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17openpty(3)Pages that refer to this page:pty(7)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |