1.\" $NetBSD: openpty.3,v 1.9 2002/10/01 19:36:30 wiz 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. 108The file descriptor 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. 113The 114.Fa termp 115and 116.Fa winp 117parameters, if non-null, will determine the terminal attributes and 118window size of the slave side of the pseudo-tty. 119.Sh RETURN VALUES 120If a call to 121.Fn openpty , 122.Fn login_tty , 123or 124.Fn forkpty 125is not successful, -1 is returned and 126.Va errno 127is set to indicate the error. 128Otherwise, 129.Fn openpty , 130.Fn login_tty , 131and the child process of 132.Fn forkpty 133return 0, and the parent process of 134.Fn forkpty 135returns the process ID of the child process. 136.Sh FILES 137.Bl -tag -width /dev/[pt]ty[p-zP-T][0-9a-zA-Z] -compact 138.It Pa /dev/[pt]ty[p-zP-T][0-9a-zA-Z] 139.El 140.Sh ERRORS 141.Fn openpty 142will fail if: 143.Bl -tag -width Er 144.It Bq Er ENOENT 145There are no available ttys. 146.El 147.Pp 148.Fn login_tty 149will fail if 150.Fn ioctl 151fails to set 152.Fa fd 153to the controlling terminal of the current process. 154.Fn forkpty 155will fail if either 156.Fn openpty 157or 158.Fn fork 159fails. 160.Sh SEE ALSO 161.Xr fork 2 162