xref: /openbsd-src/lib/libutil/openpty.3 (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1.\"	$OpenBSD: openpty.3,v 1.17 2015/08/28 19:54:06 kettenis 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: August 28 2015 $
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.In termios.h
43.In 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 allocates the pseudo-tty through the
83.Pa /dev/ptm
84device (see
85.Xr pty 4
86for details) which means that its ownership is changed to the UID of
87the caller, permissions are set to correct values, and all earlier
88uses of that device are revoked (see
89.Xr revoke 2
90for details).
91.Pp
92The
93.Fn login_tty
94function prepares for a login on the tty
95.Fa fd
96(which may be a real tty device, or the slave of a pseudo-tty as
97returned by
98.Fn openpty )
99by creating a new session, making
100.Fa fd
101the controlling terminal for the current process, setting
102.Fa fd
103to be the standard input, output, and error streams of the current
104process, and closing
105.Fa fd .
106.Pp
107The
108.Fn forkpty
109function combines
110.Fn openpty ,
111.Fn fork ,
112and
113.Fn login_tty
114to create a new process operating in a pseudo-tty.
115The file
116descriptor of the master side of the pseudo-tty is returned in
117.Fa amaster ,
118and the filename of the slave in
119.Fa name
120if it is non-null.
121The
122.Fa termp
123and
124.Fa winp
125parameters, if non-null, will determine the terminal attributes and
126window size of the slave side of the pseudo-tty.
127.Sh RETURN VALUES
128If a call to
129.Fn openpty ,
130.Fn login_tty ,
131or
132.Fn forkpty
133is not successful, \-1 is returned and
134.Va errno
135is set to indicate the error.
136Otherwise,
137.Fn openpty ,
138.Fn login_tty ,
139and the child process of
140.Fn forkpty
141return 0, and the parent process of
142.Fn forkpty
143returns the process ID of the child process.
144.Sh FILES
145.Bl -tag -width /dev/tty[p-zP-T][0-9a-zA-Z]x -compact
146.It Pa /dev/pty[p-zP-T][0-9a-zA-Z]
147master pseudo terminals
148.It Pa /dev/tty[p-zP-T][0-9a-zA-Z]
149slave pseudo terminals
150.It Pa /dev/ptm
151pseudo terminal management device
152.El
153.Sh ERRORS
154.Fn openpty
155will fail if:
156.Bl -tag -width Er
157.It Bq Er ENOENT
158There are no available ttys.
159.El
160.Pp
161.Fn login_tty
162will fail if
163.Fn ioctl
164fails to set
165.Fa fd
166to the controlling terminal of the current process.
167.Fn forkpty
168will fail if either
169.Fn openpty
170or
171.Fn fork
172fails.
173.Sh SEE ALSO
174.Xr fork 2 ,
175.Xr revoke 2 ,
176.Xr pty 4
177