<?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="service-manager">

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

<refnamediv>
<refname>service-manager</refname>
<refpurpose>service manager</refpurpose>
</refnamediv>

<refsynopsisdiv>
<cmdsynopsis>
<command>service-manager</command>
</cmdsynopsis>
</refsynopsisdiv>

<refsection><title>Description</title>

<para>
<command>service-manager</command> manages a set of services, allowing their service processes to be programmatically brought up and down, and providing automatic restart upon failure.
</para>

<para>
It expects file descriptor 3 to be a (datagram) socket that has been set up to listen for incoming datagrams.
This is its main control socket, through which it receives requests to load, unload, and pipe together services from utilities such as <citerefentry><refentrytitle>service-dt-scanner</refentrytitle><manvolnum>1</manvolnum></citerefentry> and <citerefentry><refentrytitle>system-control</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
It creates individual control FIFOs for each service, through which it receives requests to send signals the service and bring it up or down, from utilities such as <citerefentry><refentrytitle>service-control</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
</para>

<para>
<citerefentry><refentrytitle>system-manager</refentrytitle><manvolnum>8</manvolnum></citerefentry> invokes <command>service-manager</command> with the appropriate socket (which it sets up itself) and output directed to a logging d&#xe6;mon.
So also does <citerefentry><refentrytitle>per-user-manager</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
Alternatively, <command>service-manager</command> can be started by <citerefentry><refentrytitle>local-datagram-socket-listen</refentrytitle><manvolnum>1</manvolnum></citerefentry>, which will set up the appropriate socket.
(<command>service-manager</command> can even be started as a "socket-activated" d&#xe6;mon by <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry> with the systemd-recommended <code>Accept=false</code>.)
</para>

</refsection><refsection><title>Services</title>

<para>
Each service comprises several files in the filesystem, contained in two directories.
The service manager neither knows nor cares where in the filesystem these directories are.
That is the province of the utilities that feed control requests to it.
See <citerefentry><refentrytitle>service-bundle</refentrytitle><manvolnum>5</manvolnum></citerefentry> for the details of these files and directories.
</para>

<note>
<citerefentry><refentrytitle>system-control</refentrytitle><manvolnum>1</manvolnum></citerefentry> builds upon these two with further directories, to construct a service bundle, and likewise <citerefentry><refentrytitle>service-dt-scanner</refentrytitle><manvolnum>1</manvolnum></citerefentry> has its compatibility convention.
But the all of this is entirely unknown to <command>service-manager</command>, which only deals with the core two directories, and places no restrictions on their locations in the filesystem, absolute or relative to each other.
</note>

<para>
The initial states of service processes &#x2013; the user and group IDs, session, controlling terminal, session login name, process group, environment variables, open file descriptors, current root directory, and current working directory &#x2013; are uniformly inherited from <command>service-manager</command> itself, <emphasis>not</emphasis> from the utilities such as <citerefentry><refentrytitle>service-dt-scanner</refentrytitle><manvolnum>1</manvolnum></citerefentry> and <citerefentry><refentrytitle>system-control</refentrytitle><manvolnum>8</manvolnum></citerefentry>; with the exceptions that no file descriptors for service bundles or control sockets are available, and the current working directory is set to the <filename>service</filename> directory of the service bundle being invoked.
(<command>service-manager</command> marks all such file descriptors as close-upon-<citerefentry><refentrytitle>exec</refentrytitle><manvolnum>2</manvolnum></citerefentry>, and the operating system kernel thus ensures that they are closed in the service process at the point that it tries to execute the designated <command>start</command>, <command>run</command>, <command>restart</command>, or <command>stop</command> program.)
<command>service-manager</command> in turn inherits its state from (conventionally) the <citerefentry><refentrytitle>system-manager</refentrytitle><manvolnum>8</manvolnum></citerefentry> or <citerefentry><refentrytitle>per-user-manager</refentrytitle><manvolnum>1</manvolnum></citerefentry> that invoked it, and does not change it.
</para>

<para>
The control utilities can direct <command>service-manager</command> to plumb the standard input of one service to a pipe that is connected to the standard output and standard error of another service.
In this case, <command>service-manager</command> retains open file descriptors to both ends of the pipe, only inherited by the relevant services.
If not plumbed, the standard error of a service process in particular goes to the same place as the standard error of <command>service-manager</command>.
Conventionally, <citerefentry><refentrytitle>system-manager</refentrytitle><manvolnum>8</manvolnum></citerefentry> or <citerefentry><refentrytitle>per-user-manager</refentrytitle><manvolnum>1</manvolnum></citerefentry> will have set up a <citerefentry><refentrytitle>cyclog</refentrytitle><manvolnum>1</manvolnum></citerefentry> process to capture this to a fall-back global log.
</para>

<para>
The upshot of this is that all service processes start in their <filename>service</filename> directory; with the global root directory; with the service manager's credentials, session, and process group; with no controlling terminal or session login name; with the global environment that <citerefentry><refentrytitle>system-manager</refentrytitle><manvolnum>8</manvolnum></citerefentry> or <citerefentry><refentrytitle>per-user-manager</refentrytitle><manvolnum>1</manvolnum></citerefentry> either set up from the likes of <filename>/etc/default/locale</filename> and the like (c.f. those tools's respective manuals) or itself inherited from the kernel's own command-line/environment set by the operating system boot loader; with no open file descriptors other than standard input+output+error (unless the kernel or an "initial RAM disc" system, if one is in use, left some file descriptors open in error); and with standard error either going to the same place as the <command>service-manager</command>'s or to another service.
</para>

<para>
The <command>start</command>, <command>run</command>, <command>restart</command>, and <command>stop</command> programs have the responsibility for (as appropriate) dropping privileges, changing working or root directory, creating separate sessions and process groups, assigning login names to those sessions, opening controlling terminals, and clearing or changing the environment.
</para>

</refsection>

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

</refentry>
