xref: /openbsd-src/lib/libutil/openpty.3 (revision 78fa49ead5f0889b599940410aaa89ed00d2943e)
1*78fa49eaSmillert.\"	$OpenBSD: openpty.3,v 1.20 2022/04/20 14:00:19 millert Exp $
24b826ba8Sderaadt.\" Copyright (c) 1995
34b826ba8Sderaadt.\"	The Regents of the University of California.  All rights reserved.
44b826ba8Sderaadt.\"
54b826ba8Sderaadt.\" This code is derived from software developed by the Computer Systems
64b826ba8Sderaadt.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract
74b826ba8Sderaadt.\" BG 91-66 and contributed to Berkeley.
84b826ba8Sderaadt.\"
94b826ba8Sderaadt.\" Redistribution and use in source and binary forms, with or without
104b826ba8Sderaadt.\" modification, are permitted provided that the following conditions
114b826ba8Sderaadt.\" are met:
124b826ba8Sderaadt.\" 1. Redistributions of source code must retain the above copyright
134b826ba8Sderaadt.\"    notice, this list of conditions and the following disclaimer.
144b826ba8Sderaadt.\" 2. Redistributions in binary form must reproduce the above copyright
154b826ba8Sderaadt.\"    notice, this list of conditions and the following disclaimer in the
164b826ba8Sderaadt.\"    documentation and/or other materials provided with the distribution.
176580fee3Smillert.\" 3. Neither the name of the University nor the names of its contributors
184b826ba8Sderaadt.\"    may be used to endorse or promote products derived from this software
194b826ba8Sderaadt.\"    without specific prior written permission.
204b826ba8Sderaadt.\"
214b826ba8Sderaadt.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224b826ba8Sderaadt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234b826ba8Sderaadt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244b826ba8Sderaadt.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254b826ba8Sderaadt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264b826ba8Sderaadt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274b826ba8Sderaadt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284b826ba8Sderaadt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294b826ba8Sderaadt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304b826ba8Sderaadt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314b826ba8Sderaadt.\" SUCH DAMAGE.
324b826ba8Sderaadt.\"
33*78fa49eaSmillert.Dd $Mdocdate: April 20 2022 $
344b826ba8Sderaadt.Dt OPENPTY 3
354b826ba8Sderaadt.Os
364b826ba8Sderaadt.Sh NAME
3757f7e8aeSjmc.Nm getptmfd ,
384b826ba8Sderaadt.Nm openpty ,
3957f7e8aeSjmc.Nm fdopenpty ,
404b826ba8Sderaadt.Nm login_tty ,
4157f7e8aeSjmc.Nm forkpty ,
4257f7e8aeSjmc.Nm fdforkpty
434b826ba8Sderaadt.Nd tty utility functions
444b826ba8Sderaadt.Sh SYNOPSIS
4586f9d4cdStedu.In termios.h
4686f9d4cdStedu.In util.h
47c247f093Sdownsj.Ft int
48c2a62bf3Snicm.Fn getptmfd "void"
49c2a62bf3Snicm.Ft int
50*78fa49eaSmillert.Fn openpty "int *amaster" "int *aslave" "char *name" "const struct termios *termp" "const struct winsize *winp"
514b826ba8Sderaadt.Ft int
52*78fa49eaSmillert.Fn fdopenpty "int ptmfd" "int *amaster" "int *aslave" "char *name" "const struct termios *termp" "const struct winsize *winp"
53c2a62bf3Snicm.Ft int
544b826ba8Sderaadt.Fn login_tty "int fd"
554b826ba8Sderaadt.Ft pid_t
56*78fa49eaSmillert.Fn forkpty "int *amaster" "char *name" "const struct termios *termp" "const struct winsize *winp"
57c2a62bf3Snicm.Ft pid_t
58*78fa49eaSmillert.Fn fdforkpty "int ptmfd" "int *amaster" "char *name" "const struct termios *termp" "const struct winsize *winp"
594b826ba8Sderaadt.Sh DESCRIPTION
604b826ba8SderaadtThe
614b826ba8Sderaadt.Fn openpty ,
624b826ba8Sderaadt.Fn login_tty ,
634b826ba8Sderaadtand
64fd62628bSmillert.Fn forkpty
654b826ba8Sderaadtfunctions perform manipulations on ttys and pseudo-ttys.
664b826ba8Sderaadt.Pp
674b826ba8SderaadtThe
684b826ba8Sderaadt.Fn openpty
694b826ba8Sderaadtfunction finds an available pseudo-tty and returns file descriptors
704b826ba8Sderaadtfor the master and slave in
714b826ba8Sderaadt.Fa amaster
724b826ba8Sderaadtand
734b826ba8Sderaadt.Fa aslave .
744b826ba8SderaadtIf
754b826ba8Sderaadt.Fa name
764b826ba8Sderaadtis non-null, the filename of the slave is returned in
77505edac4Sderaadt.Fa name
78505edac4Sderaadt(a string of at least 16 characters).
794b826ba8SderaadtIf
804b826ba8Sderaadt.Fa termp
814b826ba8Sderaadtis non-null, the terminal parameters of the slave will be set to the
824b826ba8Sderaadtvalues in
834b826ba8Sderaadt.Fa termp .
844b826ba8SderaadtIf
854b826ba8Sderaadt.Fa winp
864b826ba8Sderaadtis non-null, the window size of the slave will be set to the values in
874b826ba8Sderaadt.Fa winp .
884b826ba8Sderaadt.Pp
894b826ba8SderaadtThe
903113de35Smillert.Fn openpty
914e92611eSkettenisfunction allocates the pseudo-tty through the
923113de35Smillert.Pa /dev/ptm
933113de35Smillertdevice (see
943113de35Smillert.Xr pty 4
954e92611eSkettenisfor details) which means that its ownership is changed to the UID of
964e92611eSkettenisthe caller, permissions are set to correct values, and all earlier
974e92611eSkettenisuses of that device are revoked (see
983113de35Smillert.Xr revoke 2
993113de35Smillertfor details).
1003113de35Smillert.Pp
1013113de35SmillertThe
102c2a62bf3Snicm.Fn fdopenpty
103c2a62bf3Snicmand
104c2a62bf3Snicm.Fn fdforkpty
105c2a62bf3Snicmfunctions work like
106c2a62bf3Snicm.Fn openpty
107c2a62bf3Snicmand
108c2a62bf3Snicm.Fn forkpty
109c2a62bf3Snicmbut expect a
110c2a62bf3Snicm.Pa /dev/ptm
111c2a62bf3Snicmfile descriptor
112c2a62bf3Snicm.Fa ptmfd
113c2a62bf3Snicmobtained from the
114c2a62bf3Snicm.Fn getptmfd
115c2a62bf3Snicmfunction.
116c2a62bf3Snicm.Pp
117c2a62bf3SnicmThe
1184b826ba8Sderaadt.Fn login_tty
1194b826ba8Sderaadtfunction prepares for a login on the tty
1204b826ba8Sderaadt.Fa fd
1214b826ba8Sderaadt(which may be a real tty device, or the slave of a pseudo-tty as
1224b826ba8Sderaadtreturned by
1234b826ba8Sderaadt.Fn openpty )
1244b826ba8Sderaadtby creating a new session, making
1254b826ba8Sderaadt.Fa fd
1264b826ba8Sderaadtthe controlling terminal for the current process, setting
1274b826ba8Sderaadt.Fa fd
1284b826ba8Sderaadtto be the standard input, output, and error streams of the current
1294b826ba8Sderaadtprocess, and closing
1304b826ba8Sderaadt.Fa fd .
1314b826ba8Sderaadt.Pp
1324b826ba8SderaadtThe
1334b826ba8Sderaadt.Fn forkpty
1344b826ba8Sderaadtfunction combines
1354b826ba8Sderaadt.Fn openpty ,
1364b826ba8Sderaadt.Fn fork ,
1374b826ba8Sderaadtand
1384b826ba8Sderaadt.Fn login_tty
1396b9dd65bSjmcto create a new process operating in a pseudo-tty.
140ee997aeaSmpechThe file
1414b826ba8Sderaadtdescriptor of the master side of the pseudo-tty is returned in
1424b826ba8Sderaadt.Fa amaster ,
1434b826ba8Sderaadtand the filename of the slave in
1444b826ba8Sderaadt.Fa name
145ee997aeaSmpechif it is non-null.
146ee997aeaSmpechThe
1474b826ba8Sderaadt.Fa termp
1484b826ba8Sderaadtand
1494b826ba8Sderaadt.Fa winp
1504b826ba8Sderaadtparameters, if non-null, will determine the terminal attributes and
1514b826ba8Sderaadtwindow size of the slave side of the pseudo-tty.
1524b826ba8Sderaadt.Sh RETURN VALUES
1534b826ba8SderaadtIf a call to
1544b826ba8Sderaadt.Fn openpty ,
1554b826ba8Sderaadt.Fn login_tty ,
1564b826ba8Sderaadtor
157fd62628bSmillert.Fn forkpty
15862127031Saaronis not successful, \-1 is returned and
1594b826ba8Sderaadt.Va errno
160ee997aeaSmpechis set to indicate the error.
161ee997aeaSmpechOtherwise,
162fd62628bSmillert.Fn openpty ,
163fd62628bSmillert.Fn login_tty ,
164fd62628bSmillertand the child process of
165fd62628bSmillert.Fn forkpty
166fd62628bSmillertreturn 0, and the parent process of
167fd62628bSmillert.Fn forkpty
168fd62628bSmillertreturns the process ID of the child process.
169404f6687Sjmc.Sh FILES
1701982d594Sjmc.Bl -tag -width /dev/tty[p-zP-T][0-9a-zA-Z]x -compact
1711982d594Sjmc.It Pa /dev/pty[p-zP-T][0-9a-zA-Z]
1721982d594Sjmcmaster pseudo terminals
1731982d594Sjmc.It Pa /dev/tty[p-zP-T][0-9a-zA-Z]
1741982d594Sjmcslave pseudo terminals
1753113de35Smillert.It Pa /dev/ptm
1761982d594Sjmcpseudo terminal management device
177404f6687Sjmc.El
1784b826ba8Sderaadt.Sh ERRORS
179c2a62bf3Snicm.Fn getptmfd
180c2a62bf3Snicmmay fail and set
181c2a62bf3Snicm.Va errno
182c2a62bf3Snicmfor any of the errors specified for the routine
183c2a62bf3Snicm.Xr open 2 .
184c2a62bf3Snicm.Pp
1854b826ba8Sderaadt.Fn openpty
186c2a62bf3Snicmand
187c2a62bf3Snicm.Fn fdopenpty
1884b826ba8Sderaadtwill fail if:
1894b826ba8Sderaadt.Bl -tag -width Er
1904b826ba8Sderaadt.It Bq Er ENOENT
1914b826ba8SderaadtThere are no available ttys.
1924b826ba8Sderaadt.El
1934b826ba8Sderaadt.Pp
194c2a62bf3Snicm.Fn fdopenpty
195c2a62bf3Snicmand
196c2a62bf3Snicm.Fn fdforkpty
197c2a62bf3Snicmwill fail if
198c2a62bf3Snicm.Fn getptmfd
199c2a62bf3Snicmfails.
200c2a62bf3Snicm.Fn forkpty
201c2a62bf3Snicmand
202c2a62bf3Snicm.Fn fdforkpty
203c2a62bf3Snicmwill fail if either
204c2a62bf3Snicm.Fn openpty
205c2a62bf3Snicmor
206c2a62bf3Snicm.Fn fork
207c2a62bf3Snicmfails.
208c2a62bf3Snicm.Pp
2094b826ba8Sderaadt.Fn login_tty
2104b826ba8Sderaadtwill fail if
2114b826ba8Sderaadt.Fn ioctl
2124b826ba8Sderaadtfails to set
2134b826ba8Sderaadt.Fa fd
2144b826ba8Sderaadtto the controlling terminal of the current process.
215fd62628bSmillert.Sh SEE ALSO
2163113de35Smillert.Xr fork 2 ,
2173113de35Smillert.Xr revoke 2 ,
2183113de35Smillert.Xr pty 4
219