xref: /openbsd-src/lib/libutil/openpty.3 (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1.\"	$OpenBSD: openpty.3,v 1.11 2003/06/02 20:18:42 millert 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 November 4, 1996
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.Fd #include <termios.h>
43.Fd #include <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 login_tty
82function prepares for a login on the tty
83.Fa fd
84(which may be a real tty device, or the slave of a pseudo-tty as
85returned by
86.Fn openpty )
87by creating a new session, making
88.Fa fd
89the controlling terminal for the current process, setting
90.Fa fd
91to be the standard input, output, and error streams of the current
92process, and closing
93.Fa fd .
94.Pp
95The
96.Fn forkpty
97function combines
98.Fn openpty ,
99.Fn fork ,
100and
101.Fn login_tty
102to create a new process operating in a pseudo-tty.
103The file
104descriptor of the master side of the pseudo-tty is returned in
105.Fa amaster ,
106and the filename of the slave in
107.Fa name
108if it is non-null.
109The
110.Fa termp
111and
112.Fa winp
113parameters, if non-null, will determine the terminal attributes and
114window size of the slave side of the pseudo-tty.
115.Sh RETURN VALUES
116If a call to
117.Fn openpty ,
118.Fn login_tty ,
119or
120.Fn forkpty
121is not successful, \-1 is returned and
122.Va errno
123is set to indicate the error.
124Otherwise,
125.Fn openpty ,
126.Fn login_tty ,
127and the child process of
128.Fn forkpty
129return 0, and the parent process of
130.Fn forkpty
131returns the process ID of the child process.
132.Sh FILES
133.Bl -tag -width /dev/[pt]ty[pqrstuvwxyzPQRST][0123456789abcdef] -compact
134.It Pa /dev/[pt]ty[pqrstuvwxyzPQRST][0123456789abcdef]
135.El
136.Sh ERRORS
137.Fn openpty
138will fail if:
139.Bl -tag -width Er
140.It Bq Er ENOENT
141There are no available ttys.
142.El
143.Pp
144.Fn login_tty
145will fail if
146.Fn ioctl
147fails to set
148.Fa fd
149to the controlling terminal of the current process.
150.Fn forkpty
151will fail if either
152.Fn openpty
153or
154.Fn fork
155fails.
156.Sh SEE ALSO
157.Xr fork 2
158