<?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="local-seqpacket-socket-accept">

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

<refnamediv>
<refname>local-seqpacket-socket-accept</refname>
<refpurpose>accept incoming local socket connections and spawn a program per connection</refpurpose>
</refnamediv>

<refsynopsisdiv>
<cmdsynopsis>
<command>local-seqpacket-socket-accept</command>
<arg choice='opt'>--verbose</arg>
<arg choice='opt'>--connection-limit <replaceable>number</replaceable></arg>
<arg choice='opt'>--localname <replaceable>pathname</replaceable></arg>
<arg choice='req'><replaceable>next-prog</replaceable></arg>
</cmdsynopsis>
</refsynopsisdiv>

<refsection><title>Description</title>

<para>
<command>local-seqpacket-socket-accept</command> expects to inherit 1 or more sockets that have been set up to listen for incoming local socket connections, as done by <citerefentry><refentrytitle>local-seqpacket-socket-listen</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
It loops forever, accepting local socket connections and (for each such connection) spawning <replaceable>next-prog</replaceable>.
</para>

<para>
<replaceable>next-prog</replaceable> may contain its own command line options, which <command>local-seqpacket-socket-accept</command> will ignore.
</para>

<para>
If the <arg choice='plain'>--verbose</arg> option is used, <command>local-seqpacket-socket-accept</command> logs information about processes spawned and the current and maximum number of connections.
The latter is set with the <arg choice='plain'>--connection-limit</arg> option, which defaults to 40.
If it is reached, <command>local-seqpacket-socket-accept</command> stops accepting new connections until one or more child processes exit.
<command>local-seqpacket-socket-accept</command> always limits the number of connections, and has no notion of an "unlimited" number of connections.
</para>

<refsection><title>Listening socket conventions</title>

<para>
There are no UCSPI conventions for listening sockets before a connection has been accepted.
<command>local-seqpacket-socket-accept</command> follows the systemd convention, falling back on a daemontools-like default.
</para>

<para>
If the <envar>LISTEN_PID</envar> environment variable is set, if its value is parseable as a (decimal) number, if that number is the process ID of the <command>local-seqpacket-socket-accept</command> process, if the <envar>LISTEN_FDS</envar> environment variable is set, and if its value is parseable as a (positive decimal) number <replaceable>n</replaceable>: then the listening sockets are taken to be file descriptors 3 up to but not including 3+<replaceable>n</replaceable>.
(It is an error for there to be zero listening file descriptors.)
Otherwise file descriptor 3 is taken unconditionally to be the single listening open file descriptor.
</para>

<para>
<command>local-seqpacket-socket-accept</command> unconditionally unsets the <envar>LISTEN_PID</envar>, <envar>LISTEN_FDS</envar>, and <envar>LISTEN_FDNAMES</envar> environment variables after it has determined what the open file descriptors to use are.
</para>

</refsection>

<refsection><title>UCSPI (specifically UCSPI-UNIX) conventions</title>

<para>
<replaceable>next-prog</replaceable> is expected to follow the UCSPI conventions: its standard input and output are the connected socket, which it should treat as generic I/O streams and not expect to be a socket, and information about the local and remote ends of the connected socket is in environment variables.
</para>

<para>
In the child process, <command>local-seqpacket-socket-accept</command> closes all of the listening sockets.
It obtains information about the local socket connection, using the <citerefentry><refentrytitle><code>getpeername</code></refentrytitle><manvolnum>2</manvolnum></citerefentry>, <citerefentry><refentrytitle><code>getsockname</code></refentrytitle><manvolnum>2</manvolnum></citerefentry>, and (if available) <code>SO_PEERCRED</code> option of the <citerefentry><refentrytitle><code>getsockopt</code></refentrytitle><manvolnum>2</manvolnum></citerefentry> library functions, and sets up the conventional UCSPI-UNIX environment variables as follows:
</para>

<variablelist>

<varlistentry>
<term><envar>PROTO</envar></term>
<listitem>
<para>
This always has the value <code>UNIX</code>, indicating the prefix to the names of the remaining environment variables.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><envar>UNIXLOCALPATH</envar></term>
<listitem>
<para>
The local (server) pathname of the socket.
The <replaceable>pathname</replaceable> set by the <arg>--localname</arg> option forcibly overrides the information supplied by the library function.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><envar>UNIXREMOTEPATH</envar></term>
<listitem>
<para>
The remote (client) pathname of the socket.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><envar>UNIXREMOTEPID</envar></term>
<listitem>
<para>
The PID of the remote (client) process that connected.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><envar>UNIXREMOTEEUID</envar></term>
<listitem>
<para>
The effective UID of the remote (client) process that connected.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><envar>UNIXREMOTEEGID</envar></term>
<listitem>
<para>
The effective GID of the remote (client) process that connected.
</para>
</listitem>
</varlistentry>

</variablelist>

</refsection>

</refsection>

<refsection><title>USAGE</title>

<para>
<command>local-seqpacket-socket-accept</command> can be used
</para>
<itemizedlist>
<listitem><para>
as a "socket-activated" d&#xe6;mon monitored by <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry> (with the systemd-recommended <code>Accept=false</code>),
</para></listitem>
<listitem><para>
as a d&#xe6;mon started by <citerefentry><refentrytitle>inetd</refentrytitle><manvolnum>1</manvolnum></citerefentry> (with <code>wait</code> and as long as it is chained via <command>fdmove</command> <arg choice="plain">0</arg> <arg choice="plain">3</arg>), and
</para></listitem>
<listitem><para>
as a simple d&#xe6;mon chained from <citerefentry><refentrytitle>local-seqpacket-socket-listen</refentrytitle><manvolnum>1</manvolnum></citerefentry> and monitored by <citerefentry><refentrytitle>service-manager</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
</para></listitem>
</itemizedlist>
<para>
Together, <command>local-seqpacket-socket-accept</command> and <citerefentry><refentrytitle>local-seqpacket-socket-listen</refentrytitle><manvolnum>1</manvolnum></citerefentry> replace <citerefentry><refentrytitle>unixserver</refentrytitle><manvolnum>1</manvolnum></citerefentry> from ucspi-unix.
</para>

<para>
To change the process' UID and GID after a successful call to <citerefentry><refentrytitle>accept</refentrytitle><manvolnum>2</manvolnum></citerefentry>, simply chain to <citerefentry><refentrytitle>setuidgid</refentrytitle><manvolnum>1</manvolnum></citerefentry> or <citerefentry><refentrytitle>setuidgid-fromenv</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
This is, however, not usually necessary because unprivileged processes can accept any connections.
It is preferable to chain <command>local-seqpacket-socket-accept</command> from <citerefentry><refentrytitle>setuidgid</refentrytitle><manvolnum>1</manvolnum></citerefentry> or <citerefentry><refentrytitle>setuidgid-fromenv</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
</para>

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

</refentry>
