libptytty(3)
π libptytty π
okay bestie, so this is the libptytty page from the LIBPTYTTY manual, and honestly? it's giving "small library, big main character energy" β¨ if you're here you probably need a pty and you're tired of every OS doing it differently. same. let's go π
πΈ NAME πΈ
libptytty β OS independent and secure pty/tty and utmp/wtmp/lastlog handling π«Ά
β¨ SYNOPSIS β¨
like, the whole vibe in one block, no notes:
cc ... -lptytty
#include <libptytty.h>
// C++
ptytty *pty = ptytty::create ();
if (!pty->get ())
// error allocating pty
if (we want utmp)
pty->login (process_pid, 0, "remote.host");
else if (we want utmp AND wtmp/lastlog)
pty->login (process_pid, 1, "remote.host");
// we are done with it
delete pty;
// C
PTYTTY pty = ptytty_create ();
if (!ptytty_get (pty))
// error allocating pty
if (we want utmp)
ptytty_login (pty, process_pid, 0, "remote.host");
else if (we want utmp AND wtmp/lastlog)
ptytty_login (pty, process_pid, 1, "remote.host");
// we are done with it
ptytty_delete (pty);
also, bestie, go look at the eg/ directory β it currently has the c-sample.c file in it, which spawns a login shell from C using libptytty π iykyk.
π DESCRIPTION π
Libptytty is a small library that offers pseudo-tty management in an OS-independent way. It was created out of frustration over the many differences of pty/tty handling in different operating systems for the use inside rxvt-unicode. literally the origin story π
and it's not just pty/tty management β it also offers session database support, aka utmp and optional wtmp/lastlog updates for login shells π
it also supports fork'ing after startup and dropping privileges in the calling process, so in case the calling process gets compromised by the user starting the program there is less to gain, as only the helper process runs with privileges (e.g. setuid/setgid), which reduces the area of attack immensely. that's the security slay fr fr π₯
Libptytty is written in C++, but it also offers a C-only API. both. it contains multitudes β¨
π οΈ INSTALLATION π οΈ
libptytty uses "CMake" as build system. To build libptytty, install "CMake" and run the following commands from either the libptytty source directory or a separate build directory:
cmake -DCMAKE_INSTALL_PREFIX=<prefix> -DBUILD_SHARED_LIBS=ON <path/to/libptytty>
cmake --build .
cmake --install .
three commands, that's it, we love a low-effort build π«Ά
π¨ SECURITY CONSIDERATIONS π¨
It is of paramount importance that you at least read the following paragraph!
like for real bestie, do not skip this one, it's not a vibe check you can fail π
If you write a typical terminal-like program that just wants one or more ptys, you should call the "ptytty::init ()" method (C: "ptytty_init ()" function) as the very first thing in your program:
int main (int argc, char *argv[])
{
// do nothing here
ptytty::init ();
// in C: ptytty_init ();
// initialise, parse arguments, etc.
}
This checks whether the program runs setuid or setgid. If yes then it will fork a helper process and drop privileges. π
Some programs need finer control over if and when this helper process is started, and if and how to drop privileges. For those programs, the methods "ptytty::use_helper" and "ptytty::drop_privileges" (and possibly "ptytty::sanitise_stdfd") are more useful.
π C++ INTERFACE: THE ptytty CLASS π
β STATIC METHODS β
ptytty::init ()β the default way to initialise libptytty. Must be called immediately as the first thing in the"main"function, or earlier e.g. during static construction time. The earlier, the better. β°
This method calls "sanitise_stdfd" and then checks whether the program runs with setuid/setgid permissions and, if yes, spawns a helper process for pty/tty management. It then drops the privileges completely, so the actual program runs without setuid/setgid privileges. π
On failure, this method throws a "ptytty_error" exception.
ptytty::use_helper ()β tries to start a helper process that retains privileges even when the calling process does not. This is usually called from"ptytty::init"when it detects that the program is running setuid or setgid, but can be called manually if it is inconvenient to drop privileges at startup, or when you are not running setuid/setgid but want to drop privileges (e.g. when running as a root-started daemon). π§
This method will try not to start more than one helper process. The same helper process can usually be used both from the process starting it and all its fork'ed (not exec'ed) children. π
On failure, this method throws a "ptytty_error" exception.
ptytty::drop_privileges ()β drops privileges completely, i.e. sets real, effective and saved user id to the real user id. Useful to make sure that the process doesn't run with special privileges. π
On failure, this method throws a "ptytty_error" exception.
ptytty::sanitise_stdfd ()β checks whether file descriptors0,1and2(stdin, stdout and stderr) are valid (open) and, if not, connects them to/dev/ttyor/dev/nullif possible. This is necessary because libptytty might want to output error messages to those descriptors, which at the time of outputting the error message, might be connected to something unsuitable opened by the unsuspecting program itself (this can be a security issue). π
On failure, this method throws a "ptytty_error" exception.
bool success = ptytty::send_fd (int socket, int fd)β utility method to send a file descriptor over a unix domain socket. Returns true if successful, false otherwise. This method is only exposed for your convenience and is not required for normal operation. πint fd = ptytty::recv_fd (int socket)β utility method to receive a file descriptor over a unix domain socket. Returns the fd if successful and"-1"otherwise. This method is only exposed for your convenience and is not required for normal operation. πptytty *pty = ptytty::create ()β creates new ptytty object. Creation does not yet do anything besides allocating the structure. π±
A static method is used because the actual ptytty implementation can differ at runtime, so you need a dynamic object creation facility. it's giving polymorphic, periodt β¨
π¦ DYNAMIC/SESSION-RELATED DATA MEMBERS AND METHODS π¦
int pty_fd = pty->ptyint tty_fd = pty->tty
These members contain the pty and tty file descriptors, respectively. They initially contain "-1" until a successful call to "ptytty::get". π
bool success = pty->get ()β tries to find, allocate and initialise a new pty/tty pair. Returns"true"when successful. β
If the helper process is running and there is a protocol error, this method throws a "ptytty_error" exception. π
pty->login (int cmd_pid, bool login_shell, const char *hostname)β creates an entry in the systems session database(s) (utmp, wtmp, lastlog)."cmd_pid"must be the pid of the process representing the session (such as the login shell),"login_shell"defines whether the session is associated with a login, which influences whether wtmp and lastlog entries are created, and"hostname"should identify the"hostname"the user logs in from, which often is the value of the"DISPLAY"variable or tty line in case of local logins. πΈ
Calling this method is optional. A session starts at the time of the login call and extends until the ptytty object is destroyed. π«Ά
pty->close_tty ()β closes the tty. Useful after forking in the parent/pty process. πbool success = pty->make_controlling_tty ()β tries to make the pty/tty pair the controlling terminal of the current process. Useful after forking in the child/tty process. πpty->set_utf8_mode (bool on)β on systems supporting special UTF-8 line disciplines (e.g. Linux), this tries to enable this discipline for the given pty. Can be called at any time to change the mode. π¦
π C INTERFACE: THE ptytty FAMILY OF FUNCTIONS π
ptytty_init ()β see"ptytty::init ()".PTYTTY ptytty_create ()β creates a new opaquePTYTTYobject and returns it. Do not try to access it in any way except by testing it for truthness (e.g."if (pty) ...."). See"ptytty::create ()". πint ptytty_pty (PTYTTY ptytty)β return the pty file descriptor. See"pty->pty".int ptytty_tty (PTYTTY ptytty)β return the tty file descriptor. See"pty->tty".void ptytty_delete (PTYTTY ptytty)β destroys thePTYTTYobject, freeing the pty/tty pair and cleaning up the utmp/wtmp/lastlog databases, if initialised/used. Same as"delete pty"in C++. π§Ήint ptytty_get (PTYTTY ptytty)β see"pty->get", returns0in case of an error, non-zero otherwise.void ptytty_login (PTYTTY ptytty, int cmd_pid, bool login_shell, const char *hostname)β see"pty->login".void ptytty_close_tty (PTYTTY ptytty)β see"pty->close_tty".int ptytty_make_controlling_tty (PTYTTY ptytty)β see"pty->make_controlling_tty".void ptytty_set_utf8_mode (PTYTTY ptytty, int on)β see"pty->set_utf8_mode".void ptytty_drop_privileges ()β see"ptytty::drop_privileges".void ptytty_use_helper ()β see"ptytty::use_helper".
π PORTABILITY π
To date, libptytty has been tested on the following platforms:
- GNU/Linux
- FreeBSD
- NetBSD
- OpenBSD
- macOS
- Solaris
- AIX
seven whole platforms, no cap, it really said "OS independent" and meant it π β¨
π BUGS π
You kiddin'?
honestly iconic. no bugs on the list, just attitude ππ
π AUTHORS π
Emanuele Giaquinta <emanuele.giaquinta@gmail.com>, Marc Alexander Lehmann <rxvt-unicode@schmorp.de>.
and that's the whole page bestie π this was libptytty(3), version 2.0, dated 2021-07-27 β go forth and manage your ptys securely, mwah πβ¨π«Ά
π¦ where this page comes from
- alpine 3.24: libptytty-doc 2.0-r0 Β· GPL-2.0-or-later
- arch rolling: libptytty 2.0-6 Β· GPL-2.0-or-later
- debian bookworm: libptytty-dev 2.0-1+b1 Β· GPL-2.0-or-later
- debian trixie: libptytty-dev 2.0-1+b2 Β· GPL-2.0-or-later
- epel 10: libptytty-devel 2.0-13.el10_2 Β· GPL-2.0-or-later
- epel 9: libptytty-devel 2.0-3.el9 Β· GPL-2.0-or-later
- fedora 43: libptytty-devel 2.0-13.fc43 Β· GPL-2.0-or-later
- fedora 43: libptytty-devel 2.0-13.fc43 Β· GPL-2.0-or-later
- fedora 44: libptytty-devel 2.0-14.fc44 Β· GPL-2.0-or-later
- fedora 44: libptytty-devel 2.0-14.fc44 Β· GPL-2.0-or-later
- nixos 26.05: libptytty 2.0
- nixpkgs unstable: libptytty 2.0
- opensuse-leap 16.0: libptytty-devel 2.0-bp160.1.12 Β· GPL-2.0-only
- opensuse-leap 16.0: libptytty-devel 2.0-bp160.1.12 Β· GPL-2.0-only
- opensuse-leap 16.0: libptytty-devel 2.0-bp160.1.12 Β· GPL-2.0-only
- opensuse-leap 16.0: libptytty-devel 2.0-bp160.1.12 Β· GPL-2.0-only
- ubuntu jammy: libptytty-dev 2.0-1 Β· GPL-2.0-or-later
- ubuntu noble: libptytty-dev 2.0-1 Β· GPL-2.0-or-later
- ubuntu resolute: libptytty-dev 2.0-1build1 Β· GPL-2.0-or-later
- ubuntu stonking: libptytty-dev 2.0-1build1 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms