xref: /freebsd/lib/libutil/pty.3 (revision 483f4c83716bc5db371e6fff01ac31b9d4323aa3)
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.\" $Id$
27.\" "
28.Dd December 29, 1996
29.Os
30.Dt PTY 3
31.Sh NAME
32.Nm openpty, forkpty
33.Nd auxiliary functions to obtain a pseudo-terminal
34.Sh SYNOPSIS
35.Fd #include <sys/types.h>
36.Fd #include <sys/ioctl.h>
37.Fd #include <termios.h>
38.Fd #include <libutil.h>
39.Ft int
40.Fn openpty "int *amaster" "int *aslave" "char *name" "struct termios *termp" "struct winsize *winp"
41.Ft int
42.Fn forkpty "int *amaster" "char *name" "struct termios *termp" "struct winsize *winp"
43.Pp
44Link with
45.Va -lutil
46on the
47.Xr cc 1
48command line.
49.Sh DESCRIPTION
50The function
51.Nm openpty
52attempts to obtain the next available pseudo-terminal from the system (see
53.Xr pty 4 ) .
54If it successfully found one, it subsequently tries to change the
55ownership of the slave device to the real UID of the current process,
56the group membership to the group
57.Dq tty
58(if such a group exists in the system), the access permissions for
59reading and writing by the owner, and for writing by the group, and to
60invalidate any current use of the line by calling
61.Xr revoke 2 .
62.Pp
63If the argument
64.Ar name
65is not
66.Dv NULL ,
67.Nm openpty
68copies the pathname of the slave pty to this area.  The caller is
69responsible to allocate the required space in this array.
70.Pp
71If the arguments
72.Ar termp
73or
74.Ar winp
75are not
76.Dv NULL ,
77.Nm openpty
78initializes the termios and window size settings from the structures
79these argument point to, respectively.
80.Pp
81Upon return, the open filedescriptors for the master and slave side
82of the pty are returned in the locations pointed to by
83.Ar amaster
84and
85.Ar aslave ,
86respectively.
87.Pp
88.Nm Forkpty
89first calls
90.Xr openpty 3
91to obtain the next available pseudo-terminal from the system.  Upon success,
92it forks off a new process.  In the child process, it closes the descriptor
93for the master side of the pty, and calls
94.Xr login_tty 3
95for the slave pty.  In the parent process, it closes the descriptor for the
96slave side of the pty.  The arguments
97.Ar amaster ,
98.Ar name ,
99.Ar termp ,
100and
101.Ar winp
102have the same meaning as described for
103.Nm openpty .
104.Sh RETURN VALUES
105.Nm Openpty
106returns 0 on success, or -1 on failure.
107.Pp
108.Nm Forkpty
109returns -1 on failure, 0 in the slave process, and the process ID of the
110slave process in the parent process.
111.Sh ERRORS
112On failure,
113.Nm openpty
114will set the global variable
115.Dv errno
116to
117.Er ENOENT .
118.Pp
119In addition to this,
120.Nm 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 termios 3 ,
132.Xr pty 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.Nm openpty
138or
139.Nm 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.Nm forkpty ) .
144