X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=man%2Fdaemon.xml;h=197138e51d97537b057052e9165c614e238e6bc0;hb=772c552f763a125d17d814c4e5a35bbba49fc27a;hp=853b3bb814790d6b3a35ad4c449b78ad7ee3e87f;hpb=62adf224d1d3e225de072a2815dd50e973230f5c;p=elogind.git
diff --git a/man/daemon.xml b/man/daemon.xml
index 853b3bb81..197138e51 100644
--- a/man/daemon.xml
+++ b/man/daemon.xml
@@ -8,16 +8,16 @@
Copyright 2010 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ Lesser General Public License for more details.
- You should have received a copy of the GNU General Public License
+ You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see .
-->
@@ -44,7 +44,7 @@
daemon
- Writing and Packaging System Daemons
+ Writing and packaging system daemons
@@ -57,7 +57,10 @@
in SysV Unix. Modern daemons should follow a simpler
yet more powerful scheme (here called "new-style"
daemons), as implemented by
- systemd1.
+ systemd1. This
+ manual page covers both schemes, and in
+ particular includes recommendations for daemons that
+ shall be included in the systemd init system.
SysV Daemons
@@ -112,7 +115,7 @@
In the child, call
fork() again, to
- ensure the daemon can never re-aquire
+ ensure the daemon can never re-acquire
a terminal again.
Call exit() in the
@@ -178,11 +181,11 @@
exit() in the
original process. The process that
invoked the daemon must be able to
- rely that this
+ rely on that this
exit() happens
after initialization is complete and
all external communication channels
- established and
+ are established and
accessible.
@@ -193,7 +196,7 @@
compatibility with SysV systems should
implement the scheme pointed out
above. However, it is recommended to make this
- behaviour optional and configurable via a
+ behavior optional and configurable via a
command line argument, to ease debugging as
well as to simplify integration into systems
using systemd.
@@ -252,26 +255,35 @@
recommendations for SysV init
scripts.
- As much as possible,
- rely on systemd's functionality to
- limit the access of the daemon to
- files, services and other
- resources. i.e. rely on systemd's
- resource limit control instead of
- implementing your own, rely on
- systemd's privilege dropping code
- instead of implementing it in the
- daemon, and similar. See
- systemd.exec5
- for the available
- controls.
-
If possible and
applicable expose the daemon's control
interface via the D-Bus IPC system and
grab a bus name as last step of
initialization.
+ For integration in
+ systemd, provide a
+ .service unit
+ file that carries information about
+ starting, stopping and otherwise
+ maintaining the daemon. See
+ systemd.service5
+ for details.
+
+ As much as possible,
+ rely on the init system's
+ functionality to limit the access of
+ the daemon to files, services and
+ other resources. i.e. in the case of
+ systemd, rely on systemd's resource
+ limit control instead of implementing
+ your own, rely on systemd's privilege
+ dropping code instead of implementing
+ it in the daemon, and similar. See
+ systemd.exec5
+ for the available
+ controls.
+
If D-Bus is used, make
your daemon bus-activatable, via
supplying a D-Bus service activation
@@ -311,7 +323,7 @@
Instead of using the
syslog() call to log directly to the
- system logger, a new-style daemon may
+ system syslog service, a new-style daemon may
choose to simply log to STDERR via
fprintf(), which is then forwarded to
syslog by the init system. If log
@@ -333,7 +345,7 @@
StandardError=syslog
in the service unit file. For details
see
- sd-daemon7
+ sd-daemon3
and
systemd.exec5.
@@ -345,18 +357,309 @@
MacOS X Daemon Requirements.
+
+
+ Activation
+
+ New-style init systems provide multiple
+ additional mechanisms to activate services, as
+ detailed below. It is common that services are
+ configured to be activated via more than one mechanism
+ at the same time. An example for systemd:
+ bluetoothd.service might get
+ activated either when Bluetooth hardware is plugged
+ in, or when an application accesses its programming
+ interfaces via D-Bus. Or, a print server daemon might
+ get activated when traffic arrives at an IPP port, or
+ when a printer is plugged in, or when a file is queued
+ in the printer spool directory. Even for services that
+ are intended to be started on system bootup
+ unconditionally it is a good idea to implement some of
+ the various activation schemes outlined below, in
+ order to maximize parallelization: if a daemon
+ implements a D-Bus service or listening socket,
+ implementing the full bus and socket activation scheme
+ allows starting of the daemon with its clients in
+ parallel (which speeds up boot-up), since all its
+ communication channels are established already, and no
+ request is lost because client requests will be queued
+ by the bus system (in case of D-Bus) or the kernel (in
+ case of sockets), until the activation is
+ completed.
+
+
+ Activation on Boot
+
+ Old-style daemons are usually activated
+ exclusively on boot (and manually by the
+ administrator) via SysV init scripts, as
+ detailed in the LSB
+ Linux Standard Base Core
+ Specification. This method of
+ activation is supported ubiquitously on Linux
+ init systems, both old-style and new-style
+ systems. Among other issues SysV init scripts
+ have the disadvantage of involving shell
+ scripts in the boot process. New-style init
+ systems generally employ updated versions of
+ activation, both during boot-up and during
+ runtime and using more minimal service
+ description files.
+
+ In systemd, if the developer or
+ administrator wants to make sure a service or
+ other unit is activated automatically on boot
+ it is recommended to place a symlink to the
+ unit file in the .wants/
+ directory of either
+ multi-user.target or
+ graphical.target, which
+ are normally used as boot targets at system
+ startup. See
+ systemd.unit5
+ for details about the
+ .wants/ directories, and
+ systemd.special7
+ for details about the two boot targets.
+
+
+
Socket-Based Activation
+
+ In order to maximize the possible
+ parallelization and robustness and simplify
+ configuration and development, it is
+ recommended for all new-style daemons that
+ communicate via listening sockets to employ
+ socket-based activation. In a socket-based
+ activation scheme the creation and binding of
+ the listening socket as primary communication
+ channel of daemons to local (and sometimes
+ remote) clients is moved out of the daemon
+ code and into the init system. Based on
+ per-daemon configuration the init system
+ installs the sockets and then hands them off
+ to the spawned process as soon as the
+ respective daemon is to be started.
+ Optionally activation of the service can be
+ delayed until the first inbound traffic
+ arrives at the socket, to implement on-demand
+ activation of daemons. However, the primary
+ advantage of this scheme is that all providers
+ and all consumers of the sockets can be
+ started in parallel as soon as all sockets
+ are established. In addition to that daemons
+ can be restarted with losing only a minimal
+ number of client transactions or even any
+ client request at all (the latter is
+ particularly true for state-less protocols,
+ such as DNS or syslog), because the socket
+ stays bound and accessible during the restart,
+ and all requests are queued while the daemon
+ cannot process them.
+
+ New-style daemons which support socket
+ activation must be able to receive their
+ sockets from the init system, instead of
+ creating and binding them themselves. For
+ details about the programming interfaces for
+ this scheme provided by systemd see
+ sd_listen_fds3
+ and
+ sd-daemon3. For
+ details about porting existing daemons to
+ socket-based activation see below. With
+ minimal effort it is possible to implement
+ socket-based activation in addition to
+ traditional internal socket creation in the
+ same codebase in order to support both
+ new-style and old-style init systems from the
+ same daemon binary.
+
+ systemd implements socket-based
+ activation via .socket
+ units, which are described in
+ systemd.socket5. When
+ configuring socket units for socket-based
+ activation it is essential that all listening
+ sockets are pulled in by the special target
+ unit sockets.target. It
+ is recommended to place a
+ WantedBy=sockets.target
+ directive in the [Install]
+ section, to automatically add such a
+ dependency on installation of a socket
+ unit. Unless
+ DefaultDependencies=no is
+ set the necessary ordering dependencies are
+ implicitly created for all socket units. For
+ more information about
+ sockets.target see
+ systemd.special7. It
+ is not necessary or recommended to place any
+ additional dependencies on socket units (for
+ example from
+ multi-user.target or
+ suchlike) when one is installed in
+ sockets.target.
Bus-Based Activation
+
+ When the D-Bus IPC system is used for
+ communication with clients, new-style daemons
+ should employ bus activation so that they are
+ automatically activated when a client
+ application accesses their IPC
+ interfaces. This is configured in D-Bus
+ service files (not to be confused with systemd
+ service unit files!). To ensure that D-Bus
+ uses systemd to start-up and maintain the
+ daemon use the
+ SystemdService= directive
+ in these service files, to configure the
+ matching systemd service for a D-Bus
+ service. e.g.: for a D-Bus service whose D-Bus
+ activation file is named
+ org.freedesktop.RealtimeKit.service,
+ make sure to set
+ SystemdService=rtkit-daemon.service
+ in that file, to bind it to the systemd
+ service
+ rtkit-daemon.service. This
+ is needed to make sure that the daemon is
+ started in a race-free fashion when activated
+ via multiple mechanisms simultaneously.
+
+
+
+ Device-Based Activation
+
+ Often, daemons that manage a particular
+ type of hardware should be activated only when
+ the hardware of the respective kind is plugged
+ in or otherwise becomes available. In a
+ new-style init system it is possible to bind
+ activation to hardware plug/unplug events. In
+ systemd, kernel devices appearing in the
+ sysfs/udev device tree can be exposed as units
+ if they are tagged with the string
+ "systemd". Like any other
+ kind of unit they may then pull in other units
+ when activated (i.e. Plugged in) and thus
+ implement device-based activation. Systemd
+ dependencies may be encoded in the udev
+ database via the
+ SYSTEMD_WANTS=
+ property. See
+ systemd.device5
+ for details. Often it is nicer to pull in
+ services from devices only indirectly via
+ dedicated targets. Example: instead of pulling
+ in bluetoothd.service
+ from all the various bluetooth dongles and
+ other hardware available, pull in
+ bluetooth.target from them and
+ bluetoothd.service from
+ that target. This provides for nicer
+ abstraction and gives administrators the
+ option to enable
+ bluetoothd.service via
+ controlling a
+ bluetooth.target.wants/
+ symlink uniformly with a command like
+ enable of
+ systemctl1
+ instead of manipulating the udev
+ ruleset.
Path-Based Activation
+
+ Often, runtime of daemons processing
+ spool files or directories (such as a printing
+ system) can be delayed until these file system
+ objects change state, or become
+ non-empty. New-style init systems provide a
+ way to bind service activation to file system
+ changes. systemd implements this scheme via
+ path-based activation configured in
+ .path units, as outlined
+ in
+ systemd.path5.
+
+
+
+ Timer-Based Activation
+
+ Some daemons that implement clean-up
+ jobs that are intended to be executed in
+ regular intervals benefit from timer-based
+ activation. In systemd, this is implemented
+ via .timer units, as
+ described in
+ systemd.timer5.
+
+ Other Forms of Activation
+
+ Other forms of activation have been
+ suggested and implemented in some
+ systems. However, often there are simpler or
+ better alternatives, or they can be put
+ together of combinations of the schemes
+ above. Example: sometimes it appears useful to
+ start daemons or .socket
+ units when a specific IP address is configured
+ on a network interface, because network
+ sockets shall be bound to the
+ address. However, an alternative to implement
+ this is by utilizing the Linux IP_FREEBIND
+ socket option, as accessible via
+ FreeBind=yes in systemd
+ socket files (see
+ systemd.socket5
+ for details). This option, when enabled,
+ allows sockets to be bound to a non-local, not
+ configured IP address, and hence allows
+ bindings to a particular IP address before it
+ actually becomes available, making such an
+ explicit dependency to the configured address
+ redundant. Another often suggested trigger for
+ service activation is low system
+ load. However, here too, a more convincing
+ approach might be to make proper use of
+ features of the operating system: in
+ particular, the CPU or IO scheduler of
+ Linux. Instead of scheduling jobs from
+ userspace based on monitoring the OS
+ scheduler, it is advisable to leave the
+ scheduling of processes to the OS scheduler
+ itself. systemd provides fine-grained access
+ to the CPU and IO schedulers. If a process
+ executed by the init system shall not
+ negatively impact the amount of CPU or IO
+ bandwidth available to other processes, it
+ should be configured with
+ CPUSchedulingPolicy=idle
+ and/or
+ IOSchedulingClass=idle. Optionally,
+ this may be combined with timer-based
+ activation to schedule background jobs during
+ runtime and with minimal impact on the system,
+ and remove it from the boot phase
+ itself.
+
+
+
+
+ Integration with Systemd
+
Writing Systemd Unit Files
@@ -376,7 +679,8 @@
If your daemon
registers a D-Bus name on the bus,
make sure to use
- Type=dbus if
+ Type=dbus in the
+ service file if
possible.
Make sure to set a
@@ -402,21 +706,32 @@
system-independent.
Make sure to include
- an [Install] section including
- installation information for the unit
- file. See
+ an [Install]
+ section including installation
+ information for the unit file. See
systemd.unit5
for details. To activate your service
on boot make sure to add a
WantedBy=multi-user.target
or
- WantedBy=graphical.target directive.
+ WantedBy=graphical.target
+ directive. To activate your socket on
+ boot, make sure to add
+ WantedBy=sockets.target. Usually
+ you also want to make sure that when
+ your service is installed your socket
+ is installed too, hence add
+ Also=foo.socket in
+ your service file
+ foo.service, for
+ a hypothetical program
+ foo.
- Installing Service Files
+ Installing Systemd Service Files
At the build installation time
(e.g. make install during
@@ -424,19 +739,20 @@
install their systemd unit files in the
directory returned by pkg-config
systemd
- --variable=systemdsystemnunitdir
- (for system services),
- resp. pkg-config systemd
- --variable=systemdsessionunitdir
- (for session services). This will make the
+ --variable=systemdsystemunitdir (for
+ system services) or pkg-config
+ systemd
+ --variable=systemduserunitdir
+ (for user services). This will make the
services available in the system on explicit
request but not activate them automatically
during boot. Optionally, during package
installation (e.g. rpm -i
by the administrator) symlinks should be
created in the systemd configuration
- directories via the
- systemd-install1
+ directories via the enable
+ command of the
+ systemctl1
tool, to activate them automatically on
boot.
@@ -451,15 +767,17 @@
AC_ARG_WITH([systemdsystemunitdir],
AS_HELP_STRING([--with-systemdsystemunitdir=DIR], [Directory for systemd service files]),
[], [with_systemdsystemunitdir=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)])
-AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])
-AM_CONDITIONAL(HAVE_SYSTEMD, [test -n "$with_systemdsystemunitdir"])
+if test "x$with_systemdsystemunitdir" != xno; then
+ AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])
+fi
+AM_CONDITIONAL(HAVE_SYSTEMD, [test -n "$with_systemdsystemunitdir" -a "x$with_systemdsystemunitdir" != xno ])
This snippet allows automatic
installation of the unit files on systemd
machines, and optionally allows their
installation even on machines lacking
systemd. (Modification of this snippet for the
- session unit directory is left as excercise to the
+ user unit directory is left as an exercise for the
reader.)
Additionally, to ensure that
@@ -483,111 +801,148 @@ endif
In the
rpm8
- .spec file use a snippet like
- the following to enable/disable the service
- during installation/deinstallation. Consult
+ .spec file use snippets
+ like the following to enable/disable the
+ service during
+ installation/deinstallation. This makes use of
+ the RPM macros shipped along systemd. Consult
the packaging guidelines of your distribution
for details and the equivalent for other
- packaging managers:
+ package managers.
- %post
-/usr/bin/systemd-install enable foobar.service foobar.socket >/dev/null 2>&1 || :
+ At the top of the file:
-%preun
-if [ "$1" -eq 0 ]; then
- /usr/bin/systemd-install disable foobar.service foobar.socket >/dev/null 2>&1 || :
-fi
+ BuildRequires: systemd
+%{?systemd_requires}
-
+ And as scriptlets, further down:
-
- Porting Existing Daemons
-
- Since new-style init systems such as
- systemd are compatible with traditional SysV
- init systems it is not strictly necessary to
- port existing daemons to the new
- style. However doing this offers additional
- functionality to the daemons as well as it
- simplifies integration into new-style init
- systems.
-
- To port an existing SysV compatible
- daemon the following steps are
- recommended:
-
-
- If not already
- implemented, add an optional command
- line switch to the daemon to disable
- daemonization. This is useful not only
- for using the daemon in new-style init
- systems, but also to ease debugging.
-
- If the daemon offers
- interfaces to other software running
- on the local system via local AF_UNIX
- sockets, consider implementing
- socket-based activation (see
- above). Usually a minimal patch is
- sufficient to implement this: Extend
- the socket creation in the daemon code
- so that
- sd_listen_fds3
- is checked for already passed sockets
- first. If sockets are passed
- (i.e. when
- sd_listen_fds()
- returns a positive value), skip the
- socket createn step and use the passed
- sockets. Secondly, ensure that the
- file-system socket nodes for local
- AF_UNIX sockets used in the
- socket-based activation are not
- removed when the daemon shuts down, if
- sockets have been passed. Third, if
- the daemon normally closes all
- remaining open file descriptors as
- part of its initialization, the
- sockets passed from the init system
- must be spared. Since new-style init
- systems guarantee that no left-over
- file descriptors are passed to
- executed processes, it might be a good
- choice to simply skip the closing of
- all remaining open file descriptors if
- file descriptors are
- passed.
-
- Write and install a
- systemd unit file for the service (and
- the sockets if socket-based activation
- is used, as well as a path unit file,
- if the daemon processes a spool
- directory), see above for
- details.
+ %post
+%systemd_post foobar.service foobar.socket
- If the daemon exposes
- interfaces via D-Bus, write and
- install a D-Bus activation file for
- the service, see above for
- details.
-
+%preun
+%systemd_preun foobar.service foobar.socket
+
+%postun
+%systemd_postun
+
+ If the service shall be restarted during
+ upgrades replace the
+ %postun scriptlet above
+ with the following:
+
+ %postun
+%systemd_postun_with_restart foobar.service
+
+ Note that
+ %systemd_post and
+ %systemd_preun expect the
+ names of all units that are installed/removed
+ as arguments, separated by
+ spaces. %systemd_postun
+ expects no
+ arguments. %systemd_postun_with_restart
+ expects the units to restart as
+ arguments.
+
+ To facilitate upgrades from a package
+ version that shipped only SysV init scripts to
+ a package version that ships both a SysV init
+ script and a native systemd service file, use
+ a fragment like the following:
+
+ %triggerun -- foobar < 0.47.11-1
+if /sbin/chkconfig --level 5 foobar ; then
+ /bin/systemctl --no-reload enable foobar.service foobar.socket >/dev/null 2>&1 || :
+fi
+ Where 0.47.11-1 is the first package
+ version that includes the native unit
+ file. This fragment will ensure that the first
+ time the unit file is installed it will be
+ enabled if and only if the SysV init script is
+ enabled, thus making sure that the enable
+ status is not changed. Note that
+ chkconfig is a command
+ specific to Fedora which can be used to check
+ whether a SysV init script is enabled. Other
+ operating systems will have to use different
+ commands here.
-
+
+ Porting Existing Daemons
+
+ Since new-style init systems such as systemd are
+ compatible with traditional SysV init systems it is
+ not strictly necessary to port existing daemons to the
+ new style. However doing so offers additional
+ functionality to the daemons as well as simplifying
+ integration into new-style init systems.
+
+ To port an existing SysV compatible daemon the
+ following steps are recommended:
+
+
+ If not already implemented,
+ add an optional command line switch to the
+ daemon to disable daemonization. This is
+ useful not only for using the daemon in
+ new-style init systems, but also to ease
+ debugging.
+
+ If the daemon offers
+ interfaces to other software running on the
+ local system via local AF_UNIX sockets,
+ consider implementing socket-based activation
+ (see above). Usually a minimal patch is
+ sufficient to implement this: Extend the
+ socket creation in the daemon code so that
+ sd_listen_fds3
+ is checked for already passed sockets
+ first. If sockets are passed (i.e. when
+ sd_listen_fds() returns a
+ positive value), skip the socket creation step
+ and use the passed sockets. Secondly, ensure
+ that the file-system socket nodes for local
+ AF_UNIX sockets used in the socket-based
+ activation are not removed when the daemon
+ shuts down, if sockets have been
+ passed. Third, if the daemon normally closes
+ all remaining open file descriptors as part of
+ its initialization, the sockets passed from
+ the init system must be spared. Since
+ new-style init systems guarantee that no
+ left-over file descriptors are passed to
+ executed processes, it might be a good choice
+ to simply skip the closing of all remaining
+ open file descriptors if sockets are
+ passed.
+
+ Write and install a systemd
+ unit file for the service (and the sockets if
+ socket-based activation is used, as well as a
+ path unit file, if the daemon processes a
+ spool directory), see above for
+ details.
+
+ If the daemon exposes
+ interfaces via D-Bus, write and install a
+ D-Bus activation file for the service, see
+ above for details.
+
+
See Also
systemd1,
- systemd-install1,
- sd-daemon7,
+ sd-daemon3,
sd_listen_fds3,
sd_notify3,
- daemon3
+ daemon3,
+ systemd.service5