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 2022 Oxide Computer Company
  46 .\"
  47 .Dd February 5, 2022
  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
  61 The
  62 .Fn posix_openpt
  63 function establishes a connection between a manager device for a
  64 pseudo-terminal and a file descriptor.
  65 The file descriptor is used by other I/O functions that refer to that
  66 pseudo-terminal.
  67 .Pp
  68 The file status flags and file access modes of the open file description are
  69 set according to the value of
  70 .Fa oflag .
  71 .Pp
  72 Values for
  73 .Fa oflag
  74 are constructed by a bitwise-inclusive OR of flags from
  75 the following list, defined in
  76 .Xr fcntl.h 3HEAD :
  77 .Bl -tag -width Ds
  78 .It Dv O_RDWR
  79 Open for reading and writing.
  80 .It Dv O_NOCTTY
  81 If set,
  82 .Fn posix_openpt
  83 does not cause the terminal device to become the controlling terminal for the
  84 process.
  85 .El
  86 .Pp
  87 The behavior of other values for the
  88 .Fa oflag
  89 argument is unspecified.
  90 .Sh RETURN VALUES
  91 The
  92 .Fn posix_getopt
  93 function opens a manager pseudo-terminal device and, if successful, returns a
  94 non-negative integer representing the lowest numbered unused file descriptor ;
  95 otherwise, the value
  96 .Sy -1
  97 is returned and the global variable
  98 .Va errno
  99 is set to indicate the error.
 100 .Sh EXAMPLES
 101 .Sy Example 1
 102 Open a pseudo-terminal.
 103 .Pp
 104 The following example opens a pseudo-terminal and returns the name of the
 105 subsidiary device and a file descriptor.
 106 .Bd -literal -offset Ds
 107 #include <fcntl.h>
 108 #include <stdio.h>
 109 #include <err.h>
 110 
 111 int managerfd, subsidiaryfd;
 112 char *subsidiarydevice;
 113 
 114 if ((managerfd = posix_openpt(O_RDWR|O_NOCTTY)) < 0) {
 115         err(1, "opening pseudo-terminal manager");
 116 }
 117 
 118 if (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 
 125 printf("subsidiary device is: %s\en", subsidiarydevice);
 126 
 127 if ((subsidiaryfd = open(subsidiary, O_RDWR|O_NOCTTY)) < 0) {
 128         err(1, "opening pseudo-terminal subsidiary");
 129 }
 130 .Ed
 131 .Sh ERRORS
 132 The
 133 .Fn posix_openpt
 134 function will fail if:
 135 .Bl -tag -width Er
 136 .It Er EMFILE
 137 .Brq Dv OPEN_MAX
 138 file descriptors are currently open in the calling process.
 139 .It Er ENFILE
 140 The maximum allowable number of files is currently open in the system.
 141 .El
 142 .Pp
 143 The
 144 .Fn posix_openpt
 145 function may fail if:
 146 .Bl -tag -width Er
 147 .It Er EINVAL
 148 The value of
 149 .Fa oflag
 150 is not valid.
 151 .It Er EAGAIN
 152 The system has run out of pseudo-terminal resources.
 153 .It Er ENOSR
 154 The system has run out of STREAMS resources.
 155 .El
 156 .Sh USAGE
 157 This function provides a portable method for obtaining the file descriptor of a
 158 manager terminal device for a pseudo-terminal, as opposed to using
 159 .Xr open 2
 160 on the
 161 .Xr ptm 7D
 162 device which is system-specific.
 163 .Pp
 164 The
 165 .Xr grantpt 3C
 166 function can be used to manipulate the mode and ownership permissions
 167 of the subsidiary device.
 168 The
 169 .Xr ptsname 3C
 170 function 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 attributes 5 ,
 181 .Xr standards 5 ,
 182 .Xr ptm 7D ,
 183 .Xr pts 7D