<?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="vc-get-tty">

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

<refnamediv>
<refname>vc-get-tty</refname>
<refpurpose>set up the environment appropriate to a virtual terminal and chain</refpurpose>
</refnamediv>

<refsynopsisdiv>
<cmdsynopsis>
<command>vc-get-terminal</command>
<arg choice='opt'>--term <replaceable>type</replaceable></arg>
<arg choice='req'><replaceable>terminal-device-name</replaceable></arg>
<arg choice='req'><replaceable>next-prog</replaceable></arg>
</cmdsynopsis>
</refsynopsisdiv>

<refsection><title>Description</title>

<para>
<command>vc-get-terminal</command> is a chain-loading utility that sets up environment variables appropriate to a specified virtual terminal, secures the device against access by other users, 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>vc-get-terminal</command> will ignore.
</para>

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

<para>
It determines the terminal character device filename from the passed <replaceable>terminal-device-name</replaceable>.
This can be just the absolute pathname of the device file itself or a relative pathname (such as <code>tty1</code>).
If it is a relative pathname, <command>vc-get-terminal</command> tries first prefixing <code>/dev/</code> and looking for a file, then prefixing <code>/var/dev/</code>, and then prefixing <code>/run/dev/</code>.
In the case of the latter twain, it assumes that the virtual terminal is a user-space rather than a kernel one.
</para>

<para>
<command>vc-get-terminal</command> sets the <citerefentry><refentrytitle><envar>TERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry> environment variable to a system-dependent default for virtual terminals.
For user-space virtual terminals, it also sets the <citerefentry><refentrytitle><envar>COLORTERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry> environment variable similarly.
</para>

<para>
It <emphasis>does not</emphasis> make any attempt to deduce terminal types or read the BSD <filename>/etc/ttys</filename> or <filename>/etc/gettytab</filename> databases.
Virtual terminals are either the kernel's built-in terminal emulator or user-space virtual terminals assumed to be running <citerefentry><refentrytitle>console-terminal-emulator</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
In both cases, the terminal type is fixed and well-known, and can be hardwired.
</para>

<refsection><title>FreeBSD (including GhostBSD and DragonFly BSD)</title>

<para>
The <citerefentry><refentrytitle><envar>TERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry> variable is set to <code>teken</code> for the kernel's built-in terminal emulator and <code>teken-16color</code> for user-space virtual terminals.
The <citerefentry><refentrytitle><envar>COLORTERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry> variable is set to <code>teken-256color-truecolor</code>.
</para>

<note>
In FreeBSD 9.0 the old kernel terminal emulator was replaced with the teken library, improving the escape sequence handler and enabling UTF-8 support.
<command>vc-get-terminal</command> expects either <citerefentry><refentrytitle>syscons</refentrytitle><manvolnum>4</manvolnum></citerefentry> in its default teken mode or <citerefentry><refentrytitle>vt</refentrytitle><manvolnum>4</manvolnum></citerefentry>, and does not support either the old kernel terminal emulator or <citerefentry><refentrytitle>syscons</refentrytitle><manvolnum>4</manvolnum></citerefentry> pretending to be the old kernel terminal emulator in its "cons25" mode.
</note>

</refsection>

<refsection><title>Linux</title>

<para>
The <citerefentry><refentrytitle><envar>TERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry> variable is set to <code>linux-16color</code> for all virtual terminals.
Both the kernel's built-in terminal emulator and <citerefentry><refentrytitle>console-terminal-emulator</refentrytitle><manvolnum>1</manvolnum></citerefentry> are compatible with this.
The <citerefentry><refentrytitle><envar>COLORTERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry> variable is set to <code>linux-256color-truecolor</code>.
</para>

</refsection>

<refsection><title>NetBSD</title>

<para>
The <citerefentry><refentrytitle><envar>TERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry> variable is set to <code>netbsd6</code> for all virtual terminals.
The <citerefentry><refentrytitle><envar>COLORTERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry> variable is set to <code>netbsd6-256color-truecolor</code>.
</para>
<note>
The old <code>pcvtXX</code> terminal type in <citerefentry><refentrytitle><filename>terminfo</filename></refentrytitle><manvolnum>5</manvolnum></citerefentry> was replaced by <code>wsvt25</code> in 1999, and that was replaced by <code>netbsd6</code> in 2014.
</note>
<note>
In theory, NetBSD also supports <code>sun</code> and <code>dumb</code> terminals.
In practice, the former is unfortunately configured out on most platforms and the latter is barely even a "glass lineprinter" which does not even do what an actual lineprinter could, such as overstrike.
</note>

</refsection>

<refsection><title>OpenBSD</title>

<para>
The <citerefentry><refentrytitle><envar>TERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry> variable is set to <code>pccon</code> for all virtual terminals.
The <citerefentry><refentrytitle><envar>COLORTERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry> variable is set to <code>pccon-256color-truecolor</code>.
</para>
<note>
In theory, OpenBSD also supports <code>sun</code> and <code>dumb</code> terminals.
</note>

</refsection>

<itemizedlist>

</itemizedlist>

<para>
The <code>--term</code> command line option is available for overriding these hardwired values, but there is little reason for using it as the terminal types for virtual terminals have not changed for several years on FreeBSD and for two and a half decades (as of 2025) on Linux.
</para>

</refsection>

<refsection><title>Terminal types</title>

<para>
Unfortunately, the FreeBSD termcap library has not been updated with a <code>teken</code> entry to match its replacement kernel virtual terminal emulator.
</para>

<para>
However, proper <code>teken</code>, <code>teken-16color</code>, <code>teken-256color</code>, <code>linux</code>, <code>linux-16color</code>, and <code>linux-256color</code> termcap entries for FreeBSD are supplied with the nosh toolset.
They simply require a directory to be added to the list in the <envar>TERMPATH</envar> environment variable and the files to be compiled to binary form.
Likewise, <code>linux-16color</code> was added to the Dickey terminfo database in 2009, <code>teken</code> was added in 2017, and <code>teken-16color</code> was added in 2022; but as of 2025 the terminfo database still lacks <code>teken-256color</code> and <code>linux-256color</code>.
</para>

<warning>
FreeBSD's mis-named <code>xterm-clear</code> terminal type that it employs, is incorrect.
The teken library does not fully nor correctly duplicate the functionality of XTerm, the terminal type is not <emphasis>any</emphasis> kind of <code>xterm</code>, and other operating systems have no <code>xterm-clear</code> in terminfo.
Do not use <code>xterm-clear</code>.
</warning>

<para>
The consequences of this are threefold:
</para>

<itemizedlist>

<listitem>
<para>
Remote access in both directions between FreeBSD and Linux systems is possible without undue difficulty, from virtual terminals on either.
The Linux-based operating systems have <code>teken-16color</code> in terminfo and the FreeBSD systems have <code>linux-16color</code> in termcap.
</para>
</listitem>

<listitem>
<para>
Although one cannot <emphasis>locally</emphasis> take full advantage of the 256-colour support that <citerefentry><refentrytitle>console-terminal-emulator</refentrytitle><manvolnum>1</manvolnum></citerefentry> actually has on Linux user-space virtual terminals, as there is no terminfo support; when remotely accessing FreeBSD systems <code>linux-16color</code> can be overridden with the <code>linux-256color</code> terminal type, as there is termcap support for the Linux user-space virtual terminals.
</para>
</listitem>

<listitem>
<para>
If remote access to other systems from user-space virtual terminals on FreeBSD is not a concern, <code>teken-16color</code> can be overridden with the <code>teken-256color</code> terminal type.
This enables full use of the 256-colour support that <citerefentry><refentrytitle>console-terminal-emulator</refentrytitle><manvolnum>1</manvolnum></citerefentry> actually has.
</para>
</listitem>

</itemizedlist>

</refsection>

<refsection><title>Security</title>

<para>
<command>vc-get-terminal</command> expects to be run either directly from <citerefentry><refentrytitle>init</refentrytitle><manvolnum>1</manvolnum></citerefentry> or under a d&#xe6;mon supervisor of some sort.
As such, it expects to start up with an <replaceable>already</replaceable> safe environment, open file descriptors, and resource limits for a process that is running under the aegis of the superuser.
</para>

<para>
To prevent a race condition in <citerefentry><refentrytitle>open-controlling-terminal</refentrytitle><manvolnum>1</manvolnum></citerefentry> (q.v.), <command>vc-get-tty</command> ensures that the virtual terminal is owned by the effective UID of the process, and has <code>rw-------</code> permissions.
</para>

</refsection>

<refsection><title>See Also</title>
<itemizedlist>
<listitem><para>
<citerefentry><refentrytitle><envar>TERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry>
</para></listitem>
<listitem><para>
<citerefentry><refentrytitle>terminal-types</refentrytitle><manvolnum>7</manvolnum></citerefentry>
</para></listitem>
<listitem><para>
<citerefentry><refentrytitle>linux-vt</refentrytitle><manvolnum>4</manvolnum></citerefentry>
</para></listitem>
</itemizedlist>
</refsection>

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

</refentry>
