1.\" 2.\" Copyright (c) 1996 Joerg Wunsch 3.\" 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25.\" 26.\" $FreeBSD$ 27.\" " 28.Dd December 29, 1996 29.Os 30.Dt PTY 3 31.Sh NAME 32.Nm openpty , 33.Nm forkpty 34.Nd auxiliary functions to obtain a pseudo-terminal 35.Sh SYNOPSIS 36.Fd #include <sys/types.h> 37.Fd #include <sys/ioctl.h> 38.Fd #include <termios.h> 39.Fd #include <libutil.h> 40.Ft int 41.Fn openpty "int *amaster" "int *aslave" "char *name" "struct termios *termp" "struct winsize *winp" 42.Ft int 43.Fn forkpty "int *amaster" "char *name" "struct termios *termp" "struct winsize *winp" 44.Pp 45Link with 46.Va -lutil 47on the 48.Xr cc 1 49command line. 50.Sh DESCRIPTION 51The function 52.Fn openpty 53attempts to obtain the next available pseudo-terminal from the system (see 54.Xr pty 4 ) . 55If it successfully finds one, it subsequently tries to change the 56ownership of the slave device to the real UID of the current process, 57the group membership to the group 58.Dq tty 59(if such a group exists in the system), the access permissions for 60reading and writing by the owner, and for writing by the group, and to 61invalidate any current use of the line by calling 62.Xr revoke 2 . 63.Pp 64If the argument 65.Fa name 66is not 67.Dv NULL , 68.Fn openpty 69copies the pathname of the slave pty to this area. The caller is 70responsible for allocating the required space in this array. 71.Pp 72If the arguments 73.Fa termp 74or 75.Fa winp 76are not 77.Dv NULL , 78.Fn openpty 79initializes the termios and window size settings from the structures 80these arguments point to, respectively. 81.Pp 82Upon return, the open file descriptors for the master and slave side 83of the pty are returned in the locations pointed to by 84.Fa amaster 85and 86.Fa aslave , 87respectively. 88.Pp 89.Fn Forkpty 90first calls 91.Fn openpty 92to obtain the next available pseudo-terminal from the system. Upon success, 93it forks off a new process. In the child process, it closes the descriptor 94for the master side of the pty, and calls 95.Xr login_tty 3 96for the slave pty. In the parent process, it closes the descriptor for the 97slave side of the pty. The arguments 98.Fa amaster , 99.Fa name , 100.Fa termp , 101and 102.Fa winp 103have the same meaning as described for 104.Fn openpty . 105.Sh RETURN VALUES 106.Fn Openpty 107returns 0 on success, or -1 on failure. 108.Pp 109.Fn Forkpty 110returns -1 on failure, 0 in the slave process, and the process ID of the 111slave process in the parent process. 112.Sh ERRORS 113On failure, 114.Fn openpty 115will set the global variable 116.Dv errno 117to 118.Er ENOENT . 119.Pp 120In addition to this, 121.Fn forkpty 122may set it to any value as described for 123.Xr fork 2 . 124.Sh SEE ALSO 125.Xr chmod 2 , 126.Xr chown 2 , 127.Xr fork 2 , 128.Xr getuid 2 , 129.Xr open 2 , 130.Xr revoke 2 , 131.Xr login_tty 3 , 132.Xr termios 3 , 133.Xr pty 4 , 134.Xr group 5 135.Sh BUGS 136The calling process must have an effective UID of super-user in order 137to perform all the intended actions. No notification will occur if 138.Fn openpty 139or 140.Fn forkpty 141failed to proceed with one of the described steps, as long as they could 142at least allocate the pty at all (and create the new process in the case 143of 144.Fn forkpty ) . 145