xref: /illumos-gate/usr/src/man/man3c/posix_openpt.3c (revision 8b913f79fc4449ba917263a4c79fcb647ed4e0af)
1.\"
2.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3.\" permission to reproduce portions of its copyrighted documentation.
4.\" Original documentation from The Open Group can be obtained online at
5.\" http://www.opengroup.org/bookstore/.
6.\"
7.\" The Institute of Electrical and Electronics Engineers and The Open
8.\" Group, have given us permission to reprint portions of their
9.\" documentation.
10.\"
11.\" In the following statement, the phrase ``this text'' refers to portions
12.\" of the system documentation.
13.\"
14.\" Portions of this text are reprinted and reproduced in electronic form
15.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16.\" Standard for Information Technology -- Portable Operating System
17.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20.\" between these versions and the original IEEE and The Open Group
21.\" Standard, the original IEEE and The Open Group Standard is the referee
22.\" document.  The original Standard can be obtained online at
23.\" http://www.opengroup.org/unix/online.html.
24.\"
25.\" This notice shall appear on any product containing this material.
26.\"
27.\" The contents of this file are subject to the terms of the
28.\" Common Development and Distribution License (the "License").
29.\" You may not use this file except in compliance with the License.
30.\"
31.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32.\" or http://www.opensolaris.org/os/licensing.
33.\" See the License for the specific language governing permissions
34.\" and limitations under the License.
35.\"
36.\" When distributing Covered Code, include this CDDL HEADER in each
37.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38.\" If applicable, add the following below this CDDL HEADER, with the
39.\" fields enclosed by brackets "[]" replaced with your own identifying
40.\" information: Portions Copyright [yyyy] [name of copyright owner]
41.\"
42.\"
43.\" Copyright (c) 2001, The IEEE and The Open Group.  All Rights Reserved.
44.\" Portions Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved.
45.\" Copyright 2024 Oxide Computer Company
46.\"
47.Dd July 3, 2024
48.Dt POSIX_OPENPT 3C
49.Os
50.Sh NAME
51.Nm posix_openpt
52.Nd open a pseudo-terminal manager device
53.Sh SYNOPSIS
54.In stdlib.h
55.In fcntl.h
56.Ft int
57.Fo posix_openpt
58.Fa "int oflag"
59.Fc
60.Sh DESCRIPTION
61The
62.Fn posix_openpt
63function establishes a connection between a manager device for a
64pseudo-terminal and a file descriptor.
65The file descriptor is used by other I/O functions that refer to that
66pseudo-terminal.
67.Pp
68The file status flags and file access modes of the open file description are
69set according to the value of
70.Fa oflag .
71.Pp
72Values for
73.Fa oflag
74are constructed by a bitwise-inclusive OR of flags from
75the following list, defined in
76.Xr fcntl.h 3HEAD :
77.Bl -tag -width Ds
78.It Dv O_RDWR
79Open for reading and writing.
80.It Dv O_NOCTTY
81If set,
82.Fn posix_openpt
83does not cause the terminal device to become the controlling terminal for the
84process.
85.El
86.Pp
87The behavior of other values for the
88.Fa oflag
89argument is unspecified.
90.Sh RETURN VALUES
91The
92.Fn posix_openpt
93function opens a manager pseudo-terminal device and, if successful, returns a
94non-negative integer representing the lowest numbered unused file descriptor ;
95otherwise, the value
96.Sy -1
97is returned and the global variable
98.Va errno
99is set to indicate the error.
100.Sh EXAMPLES
101.Sy Example 1
102Open a pseudo-terminal.
103.Pp
104The following example opens a pseudo-terminal and returns the name of the
105subsidiary device and a file descriptor.
106.Bd -literal -offset Ds
107#include <fcntl.h>
108#include <stdio.h>
109#include <err.h>
110
111int managerfd, subsidiaryfd;
112char *subsidiarydevice;
113
114if ((managerfd = posix_openpt(O_RDWR|O_NOCTTY)) < 0) {
115        err(1, "opening pseudo-terminal manager");
116}
117
118if (grantpt(managerfd) != 0 ||
119    unlockpt(managerfd) != 0 ||
120    (subsidiarydevice = ptsname(managerfd)) == NULL) {
121        (void) close(managerfd);
122        err(1, "locating pseudo-terminal subsidiary");
123}
124
125printf("subsidiary device is: %s\en", subsidiarydevice);
126
127if ((subsidiaryfd = open(subsidiary, O_RDWR|O_NOCTTY)) < 0) {
128        err(1, "opening pseudo-terminal subsidiary");
129}
130.Ed
131.Sh ERRORS
132The
133.Fn posix_openpt
134function will fail if:
135.Bl -tag -width Er
136.It Er EMFILE
137.Brq Dv OPEN_MAX
138file descriptors are currently open in the calling process.
139.It Er ENFILE
140The maximum allowable number of files is currently open in the system.
141.El
142.Pp
143The
144.Fn posix_openpt
145function may fail if:
146.Bl -tag -width Er
147.It Er EINVAL
148The value of
149.Fa oflag
150is not valid.
151.It Er EAGAIN
152The system has run out of pseudo-terminal resources.
153.It Er ENOSR
154The system has run out of STREAMS resources.
155.El
156.Sh USAGE
157This function provides a portable method for obtaining the file descriptor of a
158manager terminal device for a pseudo-terminal, as opposed to using
159.Xr open 2
160on the
161.Xr ptm 4D
162device which is system-specific.
163.Pp
164The
165.Xr grantpt 3C
166function can be used to manipulate the mode and ownership permissions
167of the subsidiary device.
168The
169.Xr ptsname 3C
170function can be used to obtain the name of the subsidiary device.
171.Sh INTERFACE STABILITY
172.Sy Committed
173.Sh MT LEVEL
174.Sy MT-Safe
175.Sh SEE ALSO
176.Xr open 2 ,
177.Xr grantpt 3C ,
178.Xr ptsname 3C ,
179.Xr unlockpt 3C ,
180.Xr ptm 4D ,
181.Xr pts 4D ,
182.Xr attributes 7 ,
183.Xr standards 7
184