posix_openpt(2)

posix_openpt2

Open a pseudo-terminal device

Usage

#include <fcntl.h>

#include <stdlib.h>

int

posix_openpt(int oflag);

Library

Standard C Library (libc, -lc)

Overview

The posix_openpt() function allocates a new pseudo-terminal and establishes a connection with its master device. A slave device shall be created in /dev/pts. After the pseudo-terminal has been allocated, the slave device should have the proper permissions before it can be used (see grantpt(3)). The name of the slave device can be determined by calling ptsname(3).

The file status flags and file access modes of the open file description shall be set according to the value of oflag. Values for oflag are constructed by a bitwise-inclusive OR of flags from the following list, defined in ⟨fcntl.h⟩:

O_RDWR

Open for reading and writing.

O_NOCTTY

If set posix_openpt() shall not cause the terminal device to become the controlling terminal for the process.

O_CLOEXEC

Set the close-on-exec flag for the new file descriptor.

The posix_openpt() function shall fail when oflag contains other values.

Return values

Upon successful completion, the posix_openpt() function shall allocate a new pseudo-terminal device and return a non-negative integer representing a file descriptor, which is connected to its master device. Otherwise, -1 shall be returned and errno set to indicate the error.

Errors

The posix_openpt() function shall fail if:

[ENFILE]

The system file table is full.

[EINVAL]

The value of oflag is not valid.

[EAGAIN]

Out of pseudo-terminal resources.

Notes

The flag O_NOCTTY is included for compatibility; in FreeBSD, opening a terminal does not cause it to become a process's controlling terminal.

Releases

39 releases since FreeBSD 8.0-RELEASE; the text changed 25 times.

  1. 8.0
  2. 8.1
  3. 8.2
  4. 8.3
  5. 8.4
  6. 9.0
  7. 9.1
  8. 9.2
  9. 9.3
  10. 10.0
  11. 10.1
  12. 10.2
  13. 10.3
  14. 10.4
  15. 11.0
  16. 11.1
  17. 11.2
  18. 11.3
  19. 11.4
  20. 12.0
  21. 12.1
  22. 12.2
  23. 12.3
  24. 12.4
  25. 13.0–13.2
  26. 13.3–16.0

Standards

The posix_openpt() function conforms to IEEE Std 1003.1-2001 (“POSIX.1”). The ability to use O_CLOEXEC is an extension to the standard.

History

The posix_openpt() function appeared in FreeBSD 5.0. In FreeBSD 8.0, this function was changed to a system call.

Authors

Ed Schouten ⟨[email protected]⟩

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.