<?xml version="1.0" encoding="UTF-8"?>
<!-- **************************************************************************
.... For copyright and licensing terms, see the file named COPYING.
.... **************************************************************************
.-->
<?xml-stylesheet href="docbook-xml.css" type="text/css"?>

<refentry id="pty-get-tty">

<refmeta xmlns:xi="http://www.w3.org/2001/XInclude">
<refentrytitle>pty-allocate</refentrytitle>
<manvolnum>1</manvolnum>
<refmiscinfo class="manual">user commands</refmiscinfo>
<refmiscinfo class="source">nosh</refmiscinfo>
<xi:include href="version.xml" />
</refmeta>

<refnamediv>
<refname>pty-get-tty</refname>
<refpurpose>allocate a pseudo-terminal</refpurpose>
</refnamediv>

<refsynopsisdiv>
<cmdsynopsis>
<command>pty-allocate</command>
<arg choice='req'><replaceable>next-prog</replaceable></arg>
</cmdsynopsis>
</refsynopsisdiv>

<refsection><title>Description</title>

<para>
<command>pty-allocate</command> is a chain-loading utility that allocates and opens the back end (called "master side" in older documentation) of a new pseudo-terminal, sets up environment variables appropriate to it, and then chain loads to <replaceable>next-prog</replaceable> with the <citerefentry><refentrytitle>execvp</refentrytitle><manvolnum>3</manvolnum></citerefentry> function.
</para>

<para>
<replaceable>next-prog</replaceable> may contain its own command line options, which <command>pty-allocate</command> will ignore.
</para>

<para>
<command>pty-allocate</command> sets the <envar>TTY</envar> environment variable to the full device filename of the pseudo-terminal front end.
Programs such as <citerefentry><refentrytitle>open-controlling-terminal</refentrytitle><manvolnum>1</manvolnum></citerefentry>, <citerefentry><refentrytitle>login-banner</refentrytitle><manvolnum>1</manvolnum></citerefentry> and <citerefentry><refentrytitle>login-process</refentrytitle><manvolnum>1</manvolnum></citerefentry> will make use of this environment variable.
</para>

<para>
<command>pty-allocate</command> opens the back end of the pseudo-terminal as file descriptor number 4, closing that descriptor if it was already open.
<citerefentry><refentrytitle>pty-run</refentrytitle><manvolnum>1</manvolnum></citerefentry> and <citerefentry><refentrytitle>console-terminal-emulator</refentrytitle><manvolnum>1</manvolnum></citerefentry> expect the pseudo-terminal back end on this open file descriptor.
</para>

<para>
<command>pty-allocate</command> otherwise leaves its environment and open file descriptors unchanged.
Thus <replaceable>next-prog</replaceable> will inherit the <citerefentry><refentrytitle><envar>TERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry> environment variable and the (as far as the kernel is concerned anyway) original controlling terminal.
</para>

</refsection>

<refsection><title>Security</title>

<refsection><title>Linux</title>

<para>
To prevent a race condition in <citerefentry><refentrytitle>open-controlling-terminal</refentrytitle><manvolnum>1</manvolnum></citerefentry> (q.v.), <command>pty-allocate</command> calls <citerefentry><refentrytitle>grantpt</refentrytitle><manvolnum>3</manvolnum></citerefentry> to change the ownership of the front-end device to the (real, not effective) UID of the current process and set its permissions to allow only access to the owner; and then calls <citerefentry><refentrytitle>unlockpt</refentrytitle><manvolnum>3</manvolnum></citerefentry> to release the operating system interlock that prevents other processes from opening the front-end device before ownership and permissions have been set.
</para>

<para>
The <citerefentry><refentrytitle>grantpt</refentrytitle><manvolnum>3</manvolnum></citerefentry> library function is only rendered a no-operation if the relevant <filename>devpts</filename> is mounted with some specific mount options.
<citerefentry><refentrytitle>system-manager</refentrytitle><manvolnum>1</manvolnum></citerefentry> specifies these mount options, but that might not be what is performing system management for the machine.
</para>

</refsection><refsection><title>BSD</title>

<para>
The <citerefentry><refentrytitle>grantpt</refentrytitle><manvolnum>3</manvolnum></citerefentry> and <citerefentry><refentrytitle>unlockpt</refentrytitle><manvolnum>3</manvolnum></citerefentry> library functions are in fact no-ops on BSD, as the front-end devices of pseudo-terminals are created with these permissions and UID in the first place.
</para>

</refsection>

</refsection>

<refsection><title>See also</title>

<variablelist>
<varlistentry>
<term>
<citerefentry><refentrytitle>pty-run</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</term>
<listitem><para>
used to run a program attached to the newly created pseudo-terminal, passing its input and output to standard input and standard output
</para></listitem>
</varlistentry>
<varlistentry>
<term>
<citerefentry><refentrytitle>console-terminal-emulator</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</term>
<listitem><para>
use to run a terminal emulator on the newly created pseudo-terminal, that sends and responds to the same control sequences as the kernel's built-in terminal emulator for (kernel) virtual terminals
</para></listitem>
</varlistentry>
</variablelist>

</refsection>

<refsection><title>Author</title>
<para><author><personname><firstname>Jonathan</firstname> <surname>de Boyne Pollard</surname></personname></author></para>
</refsection>

</refentry>
