1.\" $OpenBSD: openpty.3,v 1.15 2007/05/31 19:19:38 jmc Exp $ 2.\" Copyright (c) 1995 3.\" The Regents of the University of California. All rights reserved. 4.\" 5.\" This code is derived from software developed by the Computer Systems 6.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract 7.\" BG 91-66 and contributed to Berkeley. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 3. Neither the name of the University nor the names of its contributors 18.\" may be used to endorse or promote products derived from this software 19.\" without specific prior written permission. 20.\" 21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" SUCH DAMAGE. 32.\" 33.Dd $Mdocdate: May 31 2007 $ 34.Dt OPENPTY 3 35.Os 36.Sh NAME 37.Nm openpty , 38.Nm login_tty , 39.Nm forkpty 40.Nd tty utility functions 41.Sh SYNOPSIS 42.Fd #include <termios.h> 43.Fd #include <util.h> 44.Ft int 45.Fn openpty "int *amaster" "int *aslave" "char *name" "struct termios *termp" "struct winsize *winp" 46.Ft int 47.Fn login_tty "int fd" 48.Ft pid_t 49.Fn forkpty "int *amaster" "char *name" "struct termios *termp" "struct winsize *winp" 50.Sh DESCRIPTION 51The 52.Fn openpty , 53.Fn login_tty , 54and 55.Fn forkpty 56functions perform manipulations on ttys and pseudo-ttys. 57.Pp 58The 59.Fn openpty 60function finds an available pseudo-tty and returns file descriptors 61for the master and slave in 62.Fa amaster 63and 64.Fa aslave . 65If 66.Fa name 67is non-null, the filename of the slave is returned in 68.Fa name 69(a string of at least 16 characters). 70If 71.Fa termp 72is non-null, the terminal parameters of the slave will be set to the 73values in 74.Fa termp . 75If 76.Fa winp 77is non-null, the window size of the slave will be set to the values in 78.Fa winp . 79.Pp 80The 81.Fn openpty 82function works in the following way: 83first it attempts to allocate the pseudo-tty through the 84.Pa /dev/ptm 85device (see 86.Xr pty 4 87for details) and if that fails it searches for a free pseudo-tty by iterating 88through all existing pseudo-tty devices in 89.Pa /dev . 90When a free pseudo-tty is found, its ownership is changed to 91the UID of the caller, permissions are set to correct values, 92and all earlier uses of that device are revoked (see 93.Xr revoke 2 94for details). 95The first method can work for any user, the second method requires 96super-user privileges in most cases. 97.Pp 98The 99.Fn login_tty 100function prepares for a login on the tty 101.Fa fd 102(which may be a real tty device, or the slave of a pseudo-tty as 103returned by 104.Fn openpty ) 105by creating a new session, making 106.Fa fd 107the controlling terminal for the current process, setting 108.Fa fd 109to be the standard input, output, and error streams of the current 110process, and closing 111.Fa fd . 112.Pp 113The 114.Fn forkpty 115function combines 116.Fn openpty , 117.Fn fork , 118and 119.Fn login_tty 120to create a new process operating in a pseudo-tty. 121The file 122descriptor of the master side of the pseudo-tty is returned in 123.Fa amaster , 124and the filename of the slave in 125.Fa name 126if it is non-null. 127The 128.Fa termp 129and 130.Fa winp 131parameters, if non-null, will determine the terminal attributes and 132window size of the slave side of the pseudo-tty. 133.Sh RETURN VALUES 134If a call to 135.Fn openpty , 136.Fn login_tty , 137or 138.Fn forkpty 139is not successful, \-1 is returned and 140.Va errno 141is set to indicate the error. 142Otherwise, 143.Fn openpty , 144.Fn login_tty , 145and the child process of 146.Fn forkpty 147return 0, and the parent process of 148.Fn forkpty 149returns the process ID of the child process. 150.Sh FILES 151.Bl -tag -width /dev/tty[p-zP-T][0-9a-zA-Z]x -compact 152.It Pa /dev/pty[p-zP-T][0-9a-zA-Z] 153master pseudo terminals 154.It Pa /dev/tty[p-zP-T][0-9a-zA-Z] 155slave pseudo terminals 156.It Pa /dev/ptm 157pseudo terminal management device 158.El 159.Sh ERRORS 160.Fn openpty 161will fail if: 162.Bl -tag -width Er 163.It Bq Er ENOENT 164There are no available ttys. 165.El 166.Pp 167.Fn login_tty 168will fail if 169.Fn ioctl 170fails to set 171.Fa fd 172to the controlling terminal of the current process. 173.Fn forkpty 174will fail if either 175.Fn openpty 176or 177.Fn fork 178fails. 179.Sh SEE ALSO 180.Xr fork 2 , 181.Xr revoke 2 , 182.Xr pty 4 183