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