xref: /freebsd/lib/libutil/pty.3 (revision 0552350ecc55f34e3385dc06abf3c9c45092b332)
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 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 tries to change 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 to
57invalidate 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.  The caller is
66responsible for allocating the required space in this array.
67.Pp
68If the arguments
69.Fa termp
70or
71.Fa winp
72are not
73.Dv NULL ,
74.Fn openpty
75initializes the termios and window size settings from the structures
76these arguments point to, respectively.
77.Pp
78Upon return, the open file descriptors for the master and slave side
79of the pty are returned in the locations pointed to by
80.Fa amaster
81and
82.Fa aslave ,
83respectively.
84.Pp
85The
86.Fn forkpty
87function first calls
88.Fn openpty
89to obtain the next available pseudo-terminal from the system.  Upon success,
90it forks off a new process.  In the child process, it closes the descriptor
91for the master side of the pty, and calls
92.Xr login_tty 3
93for the slave pty.  In the parent process, it closes the descriptor for the
94slave side of the pty.  The arguments
95.Fa amaster ,
96.Fa name ,
97.Fa termp ,
98and
99.Fa winp
100have the same meaning as described for
101.Fn openpty .
102.Sh RETURN VALUES
103The
104.Fn openpty
105function returns 0 on success, or -1 on failure.
106.Pp
107The
108.Fn forkpty
109function returns -1 on failure, 0 in the slave process, and the process ID of
110the slave process in the parent process.
111.Sh ERRORS
112On failure,
113.Fn openpty
114will set the global variable
115.Dv errno
116to
117.Er ENOENT .
118.Pp
119In addition to this,
120.Fn forkpty
121may set it to any value as described for
122.Xr fork 2 .
123.Sh SEE ALSO
124.Xr chmod 2 ,
125.Xr chown 2 ,
126.Xr fork 2 ,
127.Xr getuid 2 ,
128.Xr open 2 ,
129.Xr revoke 2 ,
130.Xr login_tty 3 ,
131.Xr pty 4 ,
132.Xr termios 4 ,
133.Xr group 5
134.Sh BUGS
135The calling process must have an effective UID of super-user in order
136to perform all the intended actions.  No notification will occur if
137.Fn openpty
138or
139.Fn forkpty
140failed to proceed with one of the described steps, as long as they could
141at least allocate the pty at all (and create the new process in the case
142of
143.Fn forkpty ) .
144