xref: /freebsd/lib/libutil/pty.3 (revision a0409676120c1e558d0ade943019934e0f15118d)
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 June 2, 2018
29.Dt PTY 3
30.Os
31.Sh NAME
32.Nm openpty ,
33.Nm forkpty
34.Nd auxiliary functions to obtain a pseudo-terminal
35.Sh LIBRARY
36.Lb libutil
37.Sh SYNOPSIS
38.In sys/types.h
39.In sys/ioctl.h
40.In termios.h
41.In libutil.h
42.Ft int
43.Fn openpty "int *amaster" "int *aslave" "char *name" "struct termios *termp" "struct winsize *winp"
44.Ft int
45.Fn forkpty "int *amaster" "char *name" "struct termios *termp" "struct winsize *winp"
46.Sh DESCRIPTION
47The function
48.Fn openpty
49attempts to obtain the next available pseudo-terminal from the system (see
50.Xr pty 4 ) .
51If it successfully finds one, it subsequently changes the
52ownership of the slave device to the real UID of the current process,
53the group membership to the group
54.Dq tty
55(if such a group exists in the system), the access permissions for
56reading and writing by the owner, and for writing by the group, and
57invalidates any current use of the line by calling
58.Xr revoke 2 .
59.Pp
60If the argument
61.Fa name
62is not
63.Dv NULL ,
64.Fn openpty
65copies the pathname of the slave pty to this area.
66The caller is
67responsible for allocating the required space in this array.
68.Pp
69If the arguments
70.Fa termp
71or
72.Fa winp
73are not
74.Dv NULL ,
75.Fn openpty
76initializes the termios and window size settings from the structures
77these arguments point to, respectively.
78.Pp
79Upon return, the open file descriptors for the master and slave side
80of the pty are returned in the locations pointed to by
81.Fa amaster
82and
83.Fa aslave ,
84respectively.
85.Pp
86The
87.Fn forkpty
88function first calls
89.Fn openpty
90to obtain the next available pseudo-terminal from the system.
91Upon success,
92it forks off a new process.
93In 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.
97In the parent process, it closes the descriptor for the
98slave side of the pty.
99The arguments
100.Fa amaster ,
101.Fa name ,
102.Fa termp ,
103and
104.Fa winp
105have the same meaning as described for
106.Fn openpty .
107.Sh RETURN VALUES
108The
109.Fn openpty
110function returns 0 on success, or -1 on failure.
111.Pp
112The
113.Fn forkpty
114function returns -1 on failure, 0 in the slave process, and the process ID of
115the slave process in the parent process.
116.Sh ERRORS
117The
118.Fn openpty
119function may fail and set the global variable
120.Dv errno
121for any of the errors specified for the
122.Xr grantpt 3 ,
123.Xr posix_openpt 2 ,
124.Xr ptsname 3 ,
125and
126.Xr unlockpt 3
127functions and the
128.Xr revoke 2
129system call.
130.Pp
131In addition to this,
132.Fn forkpty
133may set it to any value as described for
134.Xr fork 2 .
135.Sh SEE ALSO
136.Xr chmod 2 ,
137.Xr chown 2 ,
138.Xr fork 2 ,
139.Xr getuid 2 ,
140.Xr open 2 ,
141.Xr revoke 2 ,
142.Xr login_tty 3 ,
143.Xr pty 4 ,
144.Xr termios 4 ,
145.Xr group 5
146.Sh HISTORY
147The
148.Fn openpty
149and
150.Fn forkpty
151functions first appeared in
152.Bx 4.3 Reno.
153.Sh BUGS
154.Fn openpty
155writes the slave terminal's name to
156.Fa name ,
157but does not check that sufficient space is available.
158It is advisable to use
159.Xr ptsname 3
160instead.
161