Blame SOURCES/0194-man-document-the-new-Type-exec-type.patch

a3e2b5
From d77d5a9399e393734fe8c8a5ad085036775854a7 Mon Sep 17 00:00:00 2001
a3e2b5
From: Lennart Poettering <lennart@poettering.net>
a3e2b5
Date: Tue, 17 Jul 2018 12:01:26 +0200
a3e2b5
Subject: [PATCH] man: document the new Type=exec type
a3e2b5
a3e2b5
And while we are at it, let's rearrange and extend the Type=
a3e2b5
documentation a bit. Let's make it an itemized list, and let's add a
a3e2b5
paragraph explaining which type best to use.
a3e2b5
a3e2b5
(cherry picked from commit 79905a246d645d21633f09f564b3672d5085a85c)
a3e2b5
a3e2b5
Resolves: #1683334
a3e2b5
---
a3e2b5
 man/systemd-run.xml     |  10 +++
a3e2b5
 man/systemd.service.xml | 158 ++++++++++++++++++++++------------------
a3e2b5
 2 files changed, 97 insertions(+), 71 deletions(-)
a3e2b5
a3e2b5
diff --git a/man/systemd-run.xml b/man/systemd-run.xml
a3e2b5
index 1c254afae3..a134b2c0dc 100644
a3e2b5
--- a/man/systemd-run.xml
a3e2b5
+++ b/man/systemd-run.xml
a3e2b5
@@ -83,6 +83,16 @@
a3e2b5
     <replaceable>COMMAND</replaceable> may be omitted. In this case, <command>systemd-run</command> creates only a
a3e2b5
     <filename>.path</filename>, <filename>.socket</filename>, or <filename>.timer</filename> unit that triggers the
a3e2b5
     specified unit.</para>
a3e2b5
+
a3e2b5
+    <para>By default, services created with <command>systemd-run</command> default to the <option>simple</option> type,
a3e2b5
+    see the description of <varname>Type=</varname> in
a3e2b5
+    <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
a3e2b5
+    details. Note that when this type is used the service manager (and thus the <command>systemd-run</command> command)
a3e2b5
+    considers service start-up successful as soon as the <function>fork()</function> for the main service process
a3e2b5
+    succeeded, i.e. before the <function>execve()</function> is invoked, and thus even if the specified command cannot
a3e2b5
+    be started. Consider using the <option>exec</option> service type (i.e. <option>--property=Type=exec</option>) to
a3e2b5
+    ensure that <command>systemd-run</command> returns successfully only if the specified command line has been
a3e2b5
+    successfully started.</para>
a3e2b5
   </refsect1>
a3e2b5
 
a3e2b5
   <refsect1>
a3e2b5
diff --git a/man/systemd.service.xml b/man/systemd.service.xml
a3e2b5
index add54524ce..315b80e704 100644
a3e2b5
--- a/man/systemd.service.xml
a3e2b5
+++ b/man/systemd.service.xml
a3e2b5
@@ -153,77 +153,93 @@
a3e2b5
       <varlistentry>
a3e2b5
         <term><varname>Type=</varname></term>
a3e2b5
 
a3e2b5
-        <listitem><para>Configures the process start-up type for this
a3e2b5
-        service unit. One of
a3e2b5
-        <option>simple</option>,
a3e2b5
-        <option>forking</option>,
a3e2b5
-        <option>oneshot</option>,
a3e2b5
-        <option>dbus</option>,
a3e2b5
-        <option>notify</option> or
a3e2b5
-        <option>idle</option>.</para>
a3e2b5
-
a3e2b5
-        <para>If set to <option>simple</option> (the default if
a3e2b5
-        neither <varname>Type=</varname> nor
a3e2b5
-        <varname>BusName=</varname>, but <varname>ExecStart=</varname>
a3e2b5
-        are specified), it is expected that the process configured
a3e2b5
-        with <varname>ExecStart=</varname> is the main process of the
a3e2b5
-        service. In this mode, if the process offers functionality to
a3e2b5
-        other processes on the system, its communication channels
a3e2b5
-        should be installed before the daemon is started up (e.g.
a3e2b5
-        sockets set up by systemd, via socket activation), as systemd
a3e2b5
-        will immediately proceed starting follow-up units.</para>
a3e2b5
-
a3e2b5
-        <para>If set to <option>forking</option>, it is expected that
a3e2b5
-        the process configured with <varname>ExecStart=</varname> will
a3e2b5
-        call <function>fork()</function> as part of its start-up. The
a3e2b5
-        parent process is expected to exit when start-up is complete
a3e2b5
-        and all communication channels are set up. The child continues
a3e2b5
-        to run as the main daemon process. This is the behavior of
a3e2b5
-        traditional UNIX daemons. If this setting is used, it is
a3e2b5
-        recommended to also use the <varname>PIDFile=</varname>
a3e2b5
-        option, so that systemd can identify the main process of the
a3e2b5
-        daemon. systemd will proceed with starting follow-up units as
a3e2b5
-        soon as the parent process exits.</para>
a3e2b5
-
a3e2b5
-        <para>Behavior of <option>oneshot</option> is similar to
a3e2b5
-        <option>simple</option>; however, it is expected that the
a3e2b5
-        process has to exit before systemd starts follow-up units.
a3e2b5
-        <varname>RemainAfterExit=</varname> is particularly useful for
a3e2b5
-        this type of service. This is the implied default if neither
a3e2b5
-        <varname>Type=</varname> nor <varname>ExecStart=</varname> are
a3e2b5
-        specified.</para>
a3e2b5
-
a3e2b5
-        <para>Behavior of <option>dbus</option> is similar to
a3e2b5
-        <option>simple</option>; however, it is expected that the
a3e2b5
-        daemon acquires a name on the D-Bus bus, as configured by
a3e2b5
-        <varname>BusName=</varname>. systemd will proceed with
a3e2b5
-        starting follow-up units after the D-Bus bus name has been
a3e2b5
-        acquired. Service units with this option configured implicitly
a3e2b5
-        gain dependencies on the <filename>dbus.socket</filename>
a3e2b5
-        unit. This type is the default if <varname>BusName=</varname>
a3e2b5
-        is specified.</para>
a3e2b5
-
a3e2b5
-        <para>Behavior of <option>notify</option> is similar to
a3e2b5
-        <option>simple</option>; however, it is expected that the
a3e2b5
-        daemon sends a notification message via
a3e2b5
-        <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
a3e2b5
-        or an equivalent call when it has finished starting up.
a3e2b5
-        systemd will proceed with starting follow-up units after this
a3e2b5
-        notification message has been sent. If this option is used,
a3e2b5
-        <varname>NotifyAccess=</varname> (see below) should be set to
a3e2b5
-        open access to the notification socket provided by systemd. If
a3e2b5
-        <varname>NotifyAccess=</varname> is missing or set to
a3e2b5
-        <option>none</option>, it will be forcibly set to
a3e2b5
-        <option>main</option>. Note that currently
a3e2b5
-        <varname>Type=</varname><option>notify</option> will not work
a3e2b5
-        if used in combination with
a3e2b5
-        <varname>PrivateNetwork=</varname><option>yes</option>.</para>
a3e2b5
-
a3e2b5
-        <para>Behavior of <option>idle</option> is very similar to <option>simple</option>; however, actual execution
a3e2b5
-        of the service program is delayed until all active jobs are dispatched. This may be used to avoid interleaving
a3e2b5
-        of output of shell services with the status output on the console. Note that this type is useful only to
a3e2b5
-        improve console output, it is not useful as a general unit ordering tool, and the effect of this service type
a3e2b5
-        is subject to a 5s time-out, after which the service program is invoked anyway.</para>
a3e2b5
+        <listitem>
a3e2b5
+          <para>Configures the process start-up type for this service unit. One of <option>simple</option>,
a3e2b5
+          <option>exec</option>, <option>forking</option>, <option>oneshot</option>, <option>dbus</option>,
a3e2b5
+          <option>notify</option> or <option>idle</option>:</para>
a3e2b5
+
a3e2b5
+          <itemizedlist>
a3e2b5
+            <listitem><para>If set to <option>simple</option> (the default if <varname>ExecStart=</varname> is
a3e2b5
+            specified but neither <varname>Type=</varname> nor <varname>BusName=</varname> are), the service manager
a3e2b5
+            will consider the unit started immediately after the main service process has been forked off. It is
a3e2b5
+            expected that the process configured with <varname>ExecStart=</varname> is the main process of the
a3e2b5
+            service. In this mode, if the process offers functionality to other processes on the system, its
a3e2b5
+            communication channels should be installed before the service is started up (e.g.  sockets set up by
a3e2b5
+            systemd, via socket activation), as the service manager will immediately proceed starting follow-up units,
a3e2b5
+            right after creating the main service process, and before executing the service's binary. Note that this
a3e2b5
+            means <command>systemctl start</command> command lines for <option>simple</option> services will report
a3e2b5
+            success even if the service's binary cannot be invoked successfully (for example because the selected
a3e2b5
+            <varname>User=</varname> doesn't exist, or the service binary is missing).</para></listitem>
a3e2b5
+
a3e2b5
+            <listitem><para>The <option>exec</option> type is similar to <option>simple</option>, but the service
a3e2b5
+            manager will consider the unit started immediately after the main service binary has been executed. The service
a3e2b5
+            manager will delay starting of follow-up units until that point. (Or in other words:
a3e2b5
+            <option>simple</option> proceeds with further jobs right after <function>fork()</function> returns, while
a3e2b5
+            <option>exec</option> will not proceed before both <function>fork()</function> and
a3e2b5
+            <function>execve()</function> in the service process succeeded.) Note that this means <command>systemctl
a3e2b5
+            start</command> command lines for <option>exec</option> services will report failure when the service's
a3e2b5
+            binary cannot be invoked successfully (for example because the selected <varname>User=</varname> doesn't
a3e2b5
+            exist, or the service binary is missing).</para></listitem>
a3e2b5
+
a3e2b5
+            <listitem><para>If set to <option>forking</option>, it is expected that the process configured with
a3e2b5
+            <varname>ExecStart=</varname> will call <function>fork()</function> as part of its start-up. The parent
a3e2b5
+            process is expected to exit when start-up is complete and all communication channels are set up. The child
a3e2b5
+            continues to run as the main service process, and the service manager will consider the unit started when
a3e2b5
+            the parent process exits. This is the behavior of traditional UNIX services. If this setting is used, it is
a3e2b5
+            recommended to also use the <varname>PIDFile=</varname> option, so that systemd can reliably identify the
a3e2b5
+            main process of the service. systemd will proceed with starting follow-up units as soon as the parent
a3e2b5
+            process exits.</para></listitem>
a3e2b5
+
a3e2b5
+            <listitem><para>Behavior of <option>oneshot</option> is similar to <option>simple</option>; however, the
a3e2b5
+            service manager will consider the unit started after the main process exits. It will then start follow-up
a3e2b5
+            units. <varname>RemainAfterExit=</varname> is particularly useful for this type of
a3e2b5
+            service. <varname>Type=</varname><option>oneshot</option> is the implied default if neither
a3e2b5
+            <varname>Type=</varname> nor <varname>ExecStart=</varname> are specified.</para></listitem>
a3e2b5
+
a3e2b5
+            <listitem><para>Behavior of <option>dbus</option> is similar to <option>simple</option>; however, it is
a3e2b5
+            expected that the service acquires a name on the D-Bus bus, as configured by
a3e2b5
+            <varname>BusName=</varname>. systemd will proceed with starting follow-up units after the D-Bus bus name
a3e2b5
+            has been acquired. Service units with this option configured implicitly gain dependencies on the
a3e2b5
+            <filename>dbus.socket</filename> unit. This type is the default if <varname>BusName=</varname> is
a3e2b5
+            specified.</para></listitem>
a3e2b5
+
a3e2b5
+            <listitem><para>Behavior of <option>notify</option> is similar to <option>exec</option>; however, it is
a3e2b5
+            expected that the service sends a notification message via
a3e2b5
+            <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry> or an
a3e2b5
+            equivalent call when it has finished starting up. systemd will proceed with starting follow-up units after
a3e2b5
+            this notification message has been sent. If this option is used, <varname>NotifyAccess=</varname> (see
a3e2b5
+            below) should be set to open access to the notification socket provided by systemd. If
a3e2b5
+            <varname>NotifyAccess=</varname> is missing or set to <option>none</option>, it will be forcibly set to
a3e2b5
+            <option>main</option>. Note that currently <varname>Type=</varname><option>notify</option> will not work if
a3e2b5
+            used in combination with <varname>PrivateNetwork=</varname><option>yes</option>.</para></listitem>
a3e2b5
+
a3e2b5
+            <listitem><para>Behavior of <option>idle</option> is very similar to <option>simple</option>; however,
a3e2b5
+            actual execution of the service program is delayed until all active jobs are dispatched. This may be used
a3e2b5
+            to avoid interleaving of output of shell services with the status output on the console. Note that this
a3e2b5
+            type is useful only to improve console output, it is not useful as a general unit ordering tool, and the
a3e2b5
+            effect of this service type is subject to a 5s time-out, after which the service program is invoked
a3e2b5
+            anyway.</para></listitem>
a3e2b5
+          </itemizedlist>
a3e2b5
+
a3e2b5
+          <para>It is generally recommended to use <varname>Type=</varname><option>simple</option> for long-running
a3e2b5
+          services whenever possible, as it is the simplest and fastest option. However, as this service type won't
a3e2b5
+          propagate service start-up failures and doesn't allow ordering of other units against completion of
a3e2b5
+          initialization of the service (which for example is useful if clients need to connect to the service through
a3e2b5
+          some form of IPC, and the IPC channel is only established by the service itself — in contrast to doing this
a3e2b5
+          ahead of time through socket or bus activation or similar), it might not be sufficient for many cases. If so,
a3e2b5
+          <option>notify</option> or <option>dbus</option> (the latter only in case the service provides a D-Bus
a3e2b5
+          interface) are the preferred options as they allow service program code to precisely schedule when to
a3e2b5
+          consider the service started up successfully and when to proceed with follow-up units. The
a3e2b5
+          <option>notify</option> service type requires explicit support in the service codebase (as
a3e2b5
+          <function>sd_notify()</function> or an equivalent API needs to be invoked by the service at the appropriate
a3e2b5
+          time) — if it's not supported, then <option>forking</option> is an alternative: it supports the traditional
a3e2b5
+          UNIX service start-up protocol. Finally, <option>exec</option> might be an option for cases where it is
a3e2b5
+          enough to ensure the service binary is invoked, and where the service binary itself executes no or little
a3e2b5
+          initialization on its own (and its initialization is unlikely to fail). Note that using any type other than
a3e2b5
+          <option>simple</option> possibly delays the boot process, as the service manager needs to wait for service
a3e2b5
+          initialization to complete. It is hence recommended not to needlessly use any types other than
a3e2b5
+          <option>simple</option>. (Also note it is generally not recommended to use <option>idle</option> or
a3e2b5
+          <option>oneshot</option> for long-running services.)</para>
a3e2b5
         </listitem>
a3e2b5
       </varlistentry>
a3e2b5