]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/daemon.xml
man: generate configured paths in manpages
[thirdparty/systemd.git] / man / daemon.xml
1 <?xml version='1.0'?> <!--*-nxml-*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
4 <!ENTITY % entities SYSTEM "custom-entities.ent" >
5 %entities;
6 ]>
7
8 <!--
9 This file is part of systemd.
10
11 Copyright 2010 Lennart Poettering
12
13 systemd is free software; you can redistribute it and/or modify it
14 under the terms of the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 2.1 of the License, or
16 (at your option) any later version.
17
18 systemd is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public License
24 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 -->
26
27 <refentry id="daemon">
28
29 <refentryinfo>
30 <title>daemon</title>
31 <productname>systemd</productname>
32
33 <authorgroup>
34 <author>
35 <contrib>Developer</contrib>
36 <firstname>Lennart</firstname>
37 <surname>Poettering</surname>
38 <email>lennart@poettering.net</email>
39 </author>
40 </authorgroup>
41 </refentryinfo>
42
43 <refmeta>
44 <refentrytitle>daemon</refentrytitle>
45 <manvolnum>7</manvolnum>
46 </refmeta>
47
48 <refnamediv>
49 <refname>daemon</refname>
50 <refpurpose>Writing and packaging system daemons</refpurpose>
51 </refnamediv>
52
53 <refsect1>
54 <title>Description</title>
55
56 <para>A daemon is a service process that runs in the background
57 and supervises the system or provides functionality to other
58 processes. Traditionally, daemons are implemented following a
59 scheme originating in SysV Unix. Modern daemons should follow a
60 simpler yet more powerful scheme (here called "new-style"
61 daemons), as implemented by
62 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
63 This manual page covers both schemes, and in particular includes
64 recommendations for daemons that shall be included in the systemd
65 init system.</para>
66
67 <refsect2>
68 <title>SysV Daemons</title>
69
70 <para>When a traditional SysV daemon starts, it should execute
71 the following steps as part of the initialization. Note that
72 these steps are unnecessary for new-style daemons (see below),
73 and should only be implemented if compatibility with SysV is
74 essential.</para>
75
76 <orderedlist>
77 <listitem><para>Close all open file descriptors except
78 standard input, output, and error (i.e. the first three file
79 descriptors 0, 1, 2). This ensures that no accidentally passed
80 file descriptor stays around in the daemon process. On Linux,
81 this is best implemented by iterating through
82 <filename>/proc/self/fd</filename>, with a fallback of
83 iterating from file descriptor 3 to the value returned by
84 <function>getrlimit()</function> for
85 <constant>RLIMIT_NOFILE</constant>. </para></listitem>
86
87 <listitem><para>Reset all signal handlers to their default.
88 This is best done by iterating through the available signals
89 up to the limit of <constant>_NSIG</constant> and resetting
90 them to <constant>SIG_DFL</constant>.</para></listitem>
91
92 <listitem><para>Reset the signal mask
93 using
94 <function>sigprocmask()</function>.</para></listitem>
95
96 <listitem><para>Sanitize the environment block, removing or
97 resetting environment variables that might negatively impact
98 daemon runtime.</para></listitem>
99
100 <listitem><para>Call <function>fork()</function>, to create a
101 background process.</para></listitem>
102
103 <listitem><para>In the child, call
104 <function>setsid()</function> to detach from any terminal and
105 create an independent session.</para></listitem>
106
107 <listitem><para>In the child, call <function>fork()</function>
108 again, to ensure that the daemon can never re-acquire a
109 terminal again.</para></listitem>
110
111 <listitem><para>Call <function>exit()</function> in the first
112 child, so that only the second child (the actual daemon
113 process) stays around. This ensures that the daemon process is
114 re-parented to init/PID 1, as all daemons should
115 be.</para></listitem>
116
117 <listitem><para>In the daemon process, connect
118 <filename>/dev/null</filename> to standard input, output, and
119 error.</para></listitem>
120
121 <listitem><para>In the daemon process, reset the umask to 0,
122 so that the file modes passed to <function>open()</function>,
123 <function>mkdir()</function> and suchlike directly control the
124 access mode of the created files and
125 directories.</para></listitem>
126
127 <listitem><para>In the daemon process, change the current
128 directory to the root directory (/), in order to avoid that
129 the daemon involuntarily blocks mount points from being
130 unmounted.</para></listitem>
131
132 <listitem><para>In the daemon process, write the daemon PID
133 (as returned by <function>getpid()</function>) to a PID file,
134 for example <filename>/run/foobar.pid</filename> (for a
135 hypothetical daemon "foobar") to ensure that the daemon cannot
136 be started more than once. This must be implemented in
137 race-free fashion so that the PID file is only updated when it
138 is verified at the same time that the PID previously stored in
139 the PID file no longer exists or belongs to a foreign
140 process.</para></listitem>
141
142 <listitem><para>In the daemon process, drop privileges, if
143 possible and applicable.</para></listitem>
144
145 <listitem><para>From the daemon process, notify the original
146 process started that initialization is complete. This can be
147 implemented via an unnamed pipe or similar communication
148 channel that is created before the first
149 <function>fork()</function> and hence available in both the
150 original and the daemon process.</para></listitem>
151
152 <listitem><para>Call <function>exit()</function> in the
153 original process. The process that invoked the daemon must be
154 able to rely on that this <function>exit()</function> happens
155 after initialization is complete and all external
156 communication channels are established and
157 accessible.</para></listitem>
158 </orderedlist>
159
160 <para>The BSD <function>daemon()</function> function should not
161 be used, as it implements only a subset of these steps.</para>
162
163 <para>A daemon that needs to provide compatibility with SysV
164 systems should implement the scheme pointed out above. However,
165 it is recommended to make this behavior optional and
166 configurable via a command line argument to ease debugging as
167 well as to simplify integration into systems using
168 systemd.</para>
169 </refsect2>
170
171 <refsect2>
172 <title>New-Style Daemons</title>
173
174 <para>Modern services for Linux should be implemented as
175 new-style daemons. This makes it easier to supervise and control
176 them at runtime and simplifies their implementation.</para>
177
178 <para>For developing a new-style daemon, none of the
179 initialization steps recommended for SysV daemons need to be
180 implemented. New-style init systems such as systemd make all of
181 them redundant. Moreover, since some of these steps interfere
182 with process monitoring, file descriptor passing and other
183 functionality of the init system, it is recommended not to
184 execute them when run as new-style service.</para>
185
186 <para>Note that new-style init systems guarantee execution of
187 daemon processes in a clean process context: it is guaranteed
188 that the environment block is sanitized, that the signal
189 handlers and mask is reset and that no left-over file
190 descriptors are passed. Daemons will be executed in their own
191 session, with standard input/output/error connected to
192 <filename>/dev/null</filename> unless otherwise configured. The
193 umask is reset.
194 </para>
195
196 <para>It is recommended for new-style daemons to implement the
197 following:</para>
198
199 <orderedlist>
200 <listitem><para>If <constant>SIGTERM</constant> is received,
201 shut down the daemon and exit cleanly.</para></listitem>
202
203 <listitem><para>If <constant>SIGHUP</constant> is received,
204 reload the configuration files, if this
205 applies.</para></listitem>
206
207 <listitem><para>Provide a correct exit code from the main
208 daemon process, as this is used by the init system to detect
209 service errors and problems. It is recommended to follow the
210 exit code scheme as defined in the <ulink
211 url="http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html">LSB
212 recommendations for SysV init
213 scripts</ulink>.</para></listitem>
214
215 <listitem><para>If possible and applicable, expose the
216 daemon's control interface via the D-Bus IPC system and grab a
217 bus name as last step of initialization.</para></listitem>
218
219 <listitem><para>For integration in systemd, provide a
220 <filename>.service</filename> unit file that carries
221 information about starting, stopping and otherwise maintaining
222 the daemon. See
223 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
224 for details.</para></listitem>
225
226 <listitem><para>As much as possible, rely on the init system's
227 functionality to limit the access of the daemon to files,
228 services and other resources, i.e. in the case of systemd,
229 rely on systemd's resource limit control instead of
230 implementing your own, rely on systemd's privilege dropping
231 code instead of implementing it in the daemon, and similar.
232 See
233 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>
234 for the available controls.</para></listitem>
235
236 <listitem><para>If D-Bus is used, make your daemon
237 bus-activatable by supplying a D-Bus service activation
238 configuration file. This has multiple advantages: your daemon
239 may be started lazily on-demand; it may be started in parallel
240 to other daemons requiring it -- which maximizes
241 parallelization and boot-up speed; your daemon can be
242 restarted on failure without losing any bus requests, as the
243 bus queues requests for activatable services. See below for
244 details.</para></listitem>
245
246 <listitem><para>If your daemon provides services to other
247 local processes or remote clients via a socket, it should be
248 made socket-activatable following the scheme pointed out
249 below. Like D-Bus activation, this enables on-demand starting
250 of services as well as it allows improved parallelization of
251 service start-up. Also, for state-less protocols (such as
252 syslog, DNS), a daemon implementing socket-based activation
253 can be restarted without losing a single request. See below
254 for details.</para></listitem>
255
256 <listitem><para>If applicable, a daemon should notify the init
257 system about startup completion or status updates via the
258 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
259 interface.</para></listitem>
260
261 <listitem><para>Instead of using the
262 <function>syslog()</function> call to log directly to the
263 system syslog service, a new-style daemon may choose to simply
264 log to standard error via <function>fprintf()</function>,
265 which is then forwarded to syslog by the init system. If log
266 levels are necessary, these can be encoded by prefixing
267 individual log lines with strings like
268 <literal>&lt;4&gt;</literal> (for log level 4 "WARNING" in the
269 syslog priority scheme), following a similar style as the
270 Linux kernel's <function>printk()</function> level system. For
271 details, see
272 <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>
273 and
274 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para></listitem>
275
276 </orderedlist>
277
278 <para>These recommendations are similar but not identical to the
279 <ulink
280 url="https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html">Apple
281 MacOS X Daemon Requirements</ulink>.</para>
282 </refsect2>
283
284 </refsect1>
285 <refsect1>
286 <title>Activation</title>
287
288 <para>New-style init systems provide multiple additional
289 mechanisms to activate services, as detailed below. It is common
290 that services are configured to be activated via more than one
291 mechanism at the same time. An example for systemd:
292 <filename>bluetoothd.service</filename> might get activated either
293 when Bluetooth hardware is plugged in, or when an application
294 accesses its programming interfaces via D-Bus. Or, a print server
295 daemon might get activated when traffic arrives at an IPP port, or
296 when a printer is plugged in, or when a file is queued in the
297 printer spool directory. Even for services that are intended to be
298 started on system bootup unconditionally, it is a good idea to
299 implement some of the various activation schemes outlined below,
300 in order to maximize parallelization. If a daemon implements a
301 D-Bus service or listening socket, implementing the full bus and
302 socket activation scheme allows starting of the daemon with its
303 clients in parallel (which speeds up boot-up), since all its
304 communication channels are established already, and no request is
305 lost because client requests will be queued by the bus system (in
306 case of D-Bus) or the kernel (in case of sockets) until the
307 activation is completed.</para>
308
309 <refsect2>
310 <title>Activation on Boot</title>
311
312 <para>Old-style daemons are usually activated exclusively on
313 boot (and manually by the administrator) via SysV init scripts,
314 as detailed in the <ulink
315 url="http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html">LSB
316 Linux Standard Base Core Specification</ulink>. This method of
317 activation is supported ubiquitously on Linux init systems, both
318 old-style and new-style systems. Among other issues, SysV init
319 scripts have the disadvantage of involving shell scripts in the
320 boot process. New-style init systems generally employ updated
321 versions of activation, both during boot-up and during runtime
322 and using more minimal service description files.</para>
323
324 <para>In systemd, if the developer or administrator wants to
325 make sure that a service or other unit is activated
326 automatically on boot, it is recommended to place a symlink to
327 the unit file in the <filename>.wants/</filename> directory of
328 either <filename>multi-user.target</filename> or
329 <filename>graphical.target</filename>, which are normally used
330 as boot targets at system startup. See
331 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
332 for details about the <filename>.wants/</filename> directories,
333 and
334 <citerefentry><refentrytitle>systemd.special</refentrytitle><manvolnum>7</manvolnum></citerefentry>
335 for details about the two boot targets.</para>
336
337 </refsect2>
338
339 <refsect2>
340 <title>Socket-Based Activation</title>
341
342 <para>In order to maximize the possible parallelization and
343 robustness and simplify configuration and development, it is
344 recommended for all new-style daemons that communicate via
345 listening sockets to employ socket-based activation. In a
346 socket-based activation scheme, the creation and binding of the
347 listening socket as primary communication channel of daemons to
348 local (and sometimes remote) clients is moved out of the daemon
349 code and into the init system. Based on per-daemon
350 configuration, the init system installs the sockets and then
351 hands them off to the spawned process as soon as the respective
352 daemon is to be started. Optionally, activation of the service
353 can be delayed until the first inbound traffic arrives at the
354 socket to implement on-demand activation of daemons. However,
355 the primary advantage of this scheme is that all providers and
356 all consumers of the sockets can be started in parallel as soon
357 as all sockets are established. In addition to that, daemons can
358 be restarted with losing only a minimal number of client
359 transactions, or even any client request at all (the latter is
360 particularly true for state-less protocols, such as DNS or
361 syslog), because the socket stays bound and accessible during
362 the restart, and all requests are queued while the daemon cannot
363 process them.</para>
364
365 <para>New-style daemons which support socket activation must be
366 able to receive their sockets from the init system instead of
367 creating and binding them themselves. For details about the
368 programming interfaces for this scheme provided by systemd, see
369 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>
370 and
371 <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
372 For details about porting existing daemons to socket-based
373 activation, see below. With minimal effort, it is possible to
374 implement socket-based activation in addition to traditional
375 internal socket creation in the same codebase in order to
376 support both new-style and old-style init systems from the same
377 daemon binary.</para>
378
379 <para>systemd implements socket-based activation via
380 <filename>.socket</filename> units, which are described in
381 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
382 When configuring socket units for socket-based activation, it is
383 essential that all listening sockets are pulled in by the
384 special target unit <filename>sockets.target</filename>. It is
385 recommended to place a
386 <varname>WantedBy=sockets.target</varname> directive in the
387 <literal>[Install]</literal> section to automatically add such a
388 dependency on installation of a socket unit. Unless
389 <varname>DefaultDependencies=no</varname> is set, the necessary
390 ordering dependencies are implicitly created for all socket
391 units. For more information about
392 <filename>sockets.target</filename>, see
393 <citerefentry><refentrytitle>systemd.special</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
394 It is not necessary or recommended to place any additional
395 dependencies on socket units (for example from
396 <filename>multi-user.target</filename> or suchlike) when one is
397 installed in <filename>sockets.target</filename>.</para>
398 </refsect2>
399
400 <refsect2>
401 <title>Bus-Based Activation</title>
402
403 <para>When the D-Bus IPC system is used for communication with
404 clients, new-style daemons should employ bus activation so that
405 they are automatically activated when a client application
406 accesses their IPC interfaces. This is configured in D-Bus
407 service files (not to be confused with systemd service unit
408 files!). To ensure that D-Bus uses systemd to start-up and
409 maintain the daemon, use the <varname>SystemdService=</varname>
410 directive in these service files to configure the matching
411 systemd service for a D-Bus service. e.g.: For a D-Bus service
412 whose D-Bus activation file is named
413 <filename>org.freedesktop.RealtimeKit.service</filename>, make
414 sure to set
415 <varname>SystemdService=rtkit-daemon.service</varname> in that
416 file to bind it to the systemd service
417 <filename>rtkit-daemon.service</filename>. This is needed to
418 make sure that the daemon is started in a race-free fashion when
419 activated via multiple mechanisms simultaneously.</para>
420 </refsect2>
421
422 <refsect2>
423 <title>Device-Based Activation</title>
424
425 <para>Often, daemons that manage a particular type of hardware
426 should be activated only when the hardware of the respective
427 kind is plugged in or otherwise becomes available. In a
428 new-style init system, it is possible to bind activation to
429 hardware plug/unplug events. In systemd, kernel devices
430 appearing in the sysfs/udev device tree can be exposed as units
431 if they are tagged with the string <literal>systemd</literal>.
432 Like any other kind of unit, they may then pull in other units
433 when activated (i.e. plugged in) and thus implement device-based
434 activation. systemd dependencies may be encoded in the udev
435 database via the <varname>SYSTEMD_WANTS=</varname> property. See
436 <citerefentry><refentrytitle>systemd.device</refentrytitle><manvolnum>5</manvolnum></citerefentry>
437 for details. Often, it is nicer to pull in services from devices
438 only indirectly via dedicated targets. Example: Instead of
439 pulling in <filename>bluetoothd.service</filename> from all the
440 various bluetooth dongles and other hardware available, pull in
441 bluetooth.target from them and
442 <filename>bluetoothd.service</filename> from that target. This
443 provides for nicer abstraction and gives administrators the
444 option to enable <filename>bluetoothd.service</filename> via
445 controlling a <filename>bluetooth.target.wants/</filename>
446 symlink uniformly with a command like <command>enable</command>
447 of
448 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
449 instead of manipulating the udev ruleset.</para>
450 </refsect2>
451
452 <refsect2>
453 <title>Path-Based Activation</title>
454
455 <para>Often, runtime of daemons processing spool files or
456 directories (such as a printing system) can be delayed until
457 these file system objects change state, or become non-empty.
458 New-style init systems provide a way to bind service activation
459 to file system changes. systemd implements this scheme via
460 path-based activation configured in <filename>.path</filename>
461 units, as outlined in
462 <citerefentry><refentrytitle>systemd.path</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
463 </refsect2>
464
465 <refsect2>
466 <title>Timer-Based Activation</title>
467
468 <para>Some daemons that implement clean-up jobs that are
469 intended to be executed in regular intervals benefit from
470 timer-based activation. In systemd, this is implemented via
471 <filename>.timer</filename> units, as described in
472 <citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
473 </refsect2>
474
475 <refsect2>
476 <title>Other Forms of Activation</title>
477
478 <para>Other forms of activation have been suggested and
479 implemented in some systems. However, there are often simpler or
480 better alternatives, or they can be put together of combinations
481 of the schemes above. Example: Sometimes, it appears useful to
482 start daemons or <filename>.socket</filename> units when a
483 specific IP address is configured on a network interface,
484 because network sockets shall be bound to the address. However,
485 an alternative to implement this is by utilizing the Linux
486 <constant>IP_FREEBIND</constant> socket option, as accessible
487 via <varname>FreeBind=yes</varname> in systemd socket files (see
488 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>
489 for details). This option, when enabled, allows sockets to be
490 bound to a non-local, not configured IP address, and hence
491 allows bindings to a particular IP address before it actually
492 becomes available, making such an explicit dependency to the
493 configured address redundant. Another often suggested trigger
494 for service activation is low system load. However, here too, a
495 more convincing approach might be to make proper use of features
496 of the operating system, in particular, the CPU or IO scheduler
497 of Linux. Instead of scheduling jobs from userspace based on
498 monitoring the OS scheduler, it is advisable to leave the
499 scheduling of processes to the OS scheduler itself. systemd
500 provides fine-grained access to the CPU and IO schedulers. If a
501 process executed by the init system shall not negatively impact
502 the amount of CPU or IO bandwidth available to other processes,
503 it should be configured with
504 <varname>CPUSchedulingPolicy=idle</varname> and/or
505 <varname>IOSchedulingClass=idle</varname>. Optionally, this may
506 be combined with timer-based activation to schedule background
507 jobs during runtime and with minimal impact on the system, and
508 remove it from the boot phase itself.</para>
509 </refsect2>
510
511 </refsect1>
512 <refsect1>
513 <title>Integration with Systemd</title>
514
515 <refsect2>
516 <title>Writing Systemd Unit Files</title>
517
518 <para>When writing systemd unit files, it is recommended to
519 consider the following suggestions:</para>
520
521 <orderedlist>
522 <listitem><para>If possible, do not use the
523 <varname>Type=forking</varname> setting in service files. But
524 if you do, make sure to set the PID file path using
525 <varname>PIDFile=</varname>. See
526 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
527 for details.</para></listitem>
528
529 <listitem><para>If your daemon registers a D-Bus name on the
530 bus, make sure to use <varname>Type=dbus</varname> in the
531 service file if possible.</para></listitem>
532
533 <listitem><para>Make sure to set a good human-readable
534 description string with
535 <varname>Description=</varname>.</para></listitem>
536
537 <listitem><para>Do not disable
538 <varname>DefaultDependencies=</varname>, unless you really
539 know what you do and your unit is involved in early boot or
540 late system shutdown.</para></listitem>
541
542 <listitem><para>Normally, little if any dependencies should
543 need to be defined explicitly. However, if you do configure
544 explicit dependencies, only refer to unit names listed on
545 <citerefentry><refentrytitle>systemd.special</refentrytitle><manvolnum>7</manvolnum></citerefentry>
546 or names introduced by your own package to keep the unit file
547 operating system-independent.</para></listitem>
548
549 <listitem><para>Make sure to include an
550 <literal>[Install]</literal> section including installation
551 information for the unit file. See
552 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
553 for details. To activate your service on boot, make sure to
554 add a <varname>WantedBy=multi-user.target</varname> or
555 <varname>WantedBy=graphical.target</varname> directive. To
556 activate your socket on boot, make sure to add
557 <varname>WantedBy=sockets.target</varname>. Usually, you also
558 want to make sure that when your service is installed, your
559 socket is installed too, hence add
560 <varname>Also=foo.socket</varname> in your service file
561 <filename>foo.service</filename>, for a hypothetical program
562 <filename>foo</filename>.</para></listitem>
563
564 </orderedlist>
565 </refsect2>
566
567 <refsect2>
568 <title>Installing Systemd Service Files</title>
569
570 <para>At the build installation time (e.g. <command>make
571 install</command> during package build), packages are
572 recommended to install their systemd unit files in the directory
573 returned by <command>pkg-config systemd
574 --variable=systemdsystemunitdir</command> (for system services)
575 or <command>pkg-config systemd
576 --variable=systemduserunitdir</command> (for user services).
577 This will make the services available in the system on explicit
578 request but not activate them automatically during boot.
579 Optionally, during package installation (e.g. <command>rpm
580 -i</command> by the administrator), symlinks should be created
581 in the systemd configuration directories via the
582 <command>enable</command> command of the
583 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
584 tool to activate them automatically on boot.</para>
585
586 <para>Packages using
587 <citerefentry project='die-net'><refentrytitle>autoconf</refentrytitle><manvolnum>1</manvolnum></citerefentry>
588 are recommended to use a configure script
589 excerpt like the following to determine the
590 unit installation path during source
591 configuration:</para>
592
593 <programlisting>PKG_PROG_PKG_CONFIG
594 AC_ARG_WITH([systemdsystemunitdir],
595 [AS_HELP_STRING([--with-systemdsystemunitdir=DIR], [Directory for systemd service files])],,
596 [with_systemdsystemunitdir=auto])
597 AS_IF([test "x$with_systemdsystemunitdir" = "xyes" -o "x$with_systemdsystemunitdir" = "xauto"], [
598 def_systemdsystemunitdir=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)
599
600 AS_IF([test "x$def_systemdsystemunitdir" = "x"],
601 [AS_IF([test "x$with_systemdsystemunitdir" = "xyes"],
602 [AC_MSG_ERROR([systemd support requested but pkg-config unable to query systemd package])])
603 with_systemdsystemunitdir=no],
604 [with_systemdsystemunitdir="$def_systemdsystemunitdir"])])
605 AS_IF([test "x$with_systemdsystemunitdir" != "xno"],
606 [AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])])
607 AM_CONDITIONAL([HAVE_SYSTEMD], [test "x$with_systemdsystemunitdir" != "xno"])</programlisting>
608
609 <para>This snippet allows automatic
610 installation of the unit files on systemd
611 machines, and optionally allows their
612 installation even on machines lacking
613 systemd. (Modification of this snippet for the
614 user unit directory is left as an exercise for the
615 reader.)</para>
616
617 <para>Additionally, to ensure that
618 <command>make distcheck</command> continues to
619 work, it is recommended to add the following
620 to the top-level <filename>Makefile.am</filename>
621 file in
622 <citerefentry project='die-net'><refentrytitle>automake</refentrytitle><manvolnum>1</manvolnum></citerefentry>-based
623 projects:</para>
624
625 <programlisting>DISTCHECK_CONFIGURE_FLAGS = \
626 --with-systemdsystemunitdir=$$dc_install_base/$(systemdsystemunitdir)</programlisting>
627
628 <para>Finally, unit files should be installed in the system with an automake excerpt like the following:</para>
629
630 <programlisting>if HAVE_SYSTEMD
631 systemdsystemunit_DATA = \
632 foobar.socket \
633 foobar.service
634 endif</programlisting>
635
636 <para>In the
637 <citerefentry project='die-net'><refentrytitle>rpm</refentrytitle><manvolnum>8</manvolnum></citerefentry>
638 <filename>.spec</filename> file, use snippets like the following
639 to enable/disable the service during
640 installation/deinstallation. This makes use of the RPM macros
641 shipped along systemd. Consult the packaging guidelines of your
642 distribution for details and the equivalent for other package
643 managers.</para>
644
645 <para>At the top of the file:</para>
646
647 <programlisting>BuildRequires: systemd
648 %{?systemd_requires}</programlisting>
649
650 <para>And as scriptlets, further down:</para>
651
652 <programlisting>%post
653 %systemd_post foobar.service foobar.socket
654
655 %preun
656 %systemd_preun foobar.service foobar.socket
657
658 %postun
659 %systemd_postun</programlisting>
660
661 <para>If the service shall be restarted during upgrades, replace
662 the <literal>%postun</literal> scriptlet above with the
663 following:</para>
664
665 <programlisting>%postun
666 %systemd_postun_with_restart foobar.service</programlisting>
667
668 <para>Note that <literal>%systemd_post</literal> and
669 <literal>%systemd_preun</literal> expect the names of all units
670 that are installed/removed as arguments, separated by spaces.
671 <literal>%systemd_postun</literal> expects no arguments.
672 <literal>%systemd_postun_with_restart</literal> expects the
673 units to restart as arguments.</para>
674
675 <para>To facilitate upgrades from a package version that shipped
676 only SysV init scripts to a package version that ships both a
677 SysV init script and a native systemd service file, use a
678 fragment like the following:</para>
679
680 <programlisting>%triggerun -- foobar &lt; 0.47.11-1
681 if /sbin/chkconfig --level 5 foobar ; then
682 /bin/systemctl --no-reload enable foobar.service foobar.socket >/dev/null 2>&amp;1 || :
683 fi</programlisting>
684
685 <para>Where 0.47.11-1 is the first package version that includes
686 the native unit file. This fragment will ensure that the first
687 time the unit file is installed, it will be enabled if and only
688 if the SysV init script is enabled, thus making sure that the
689 enable status is not changed. Note that
690 <command>chkconfig</command> is a command specific to Fedora
691 which can be used to check whether a SysV init script is
692 enabled. Other operating systems will have to use different
693 commands here.</para>
694 </refsect2>
695 </refsect1>
696
697 <refsect1>
698 <title>Porting Existing Daemons</title>
699
700 <para>Since new-style init systems such as systemd are compatible
701 with traditional SysV init systems, it is not strictly necessary
702 to port existing daemons to the new style. However, doing so
703 offers additional functionality to the daemons as well as
704 simplifying integration into new-style init systems.</para>
705
706 <para>To port an existing SysV compatible daemon, the following
707 steps are recommended:</para>
708
709 <orderedlist>
710 <listitem><para>If not already implemented, add an optional
711 command line switch to the daemon to disable daemonization. This
712 is useful not only for using the daemon in new-style init
713 systems, but also to ease debugging.</para></listitem>
714
715 <listitem><para>If the daemon offers interfaces to other
716 software running on the local system via local
717 <constant>AF_UNIX</constant> sockets, consider implementing
718 socket-based activation (see above). Usually, a minimal patch is
719 sufficient to implement this: Extend the socket creation in the
720 daemon code so that
721 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>
722 is checked for already passed sockets first. If sockets are
723 passed (i.e. when <function>sd_listen_fds()</function> returns a
724 positive value), skip the socket creation step and use the
725 passed sockets. Secondly, ensure that the file system socket
726 nodes for local <constant>AF_UNIX</constant> sockets used in the
727 socket-based activation are not removed when the daemon shuts
728 down, if sockets have been passed. Third, if the daemon normally
729 closes all remaining open file descriptors as part of its
730 initialization, the sockets passed from the init system must be
731 spared. Since new-style init systems guarantee that no left-over
732 file descriptors are passed to executed processes, it might be a
733 good choice to simply skip the closing of all remaining open
734 file descriptors if sockets are passed.</para></listitem>
735
736 <listitem><para>Write and install a systemd unit file for the
737 service (and the sockets if socket-based activation is used, as
738 well as a path unit file, if the daemon processes a spool
739 directory), see above for details.</para></listitem>
740
741 <listitem><para>If the daemon exposes interfaces via D-Bus,
742 write and install a D-Bus activation file for the service, see
743 above for details.</para></listitem>
744 </orderedlist>
745 </refsect1>
746
747 <refsect1>
748 <title>Placing Daemon Data</title>
749
750 <para>It is recommended to follow the general guidelines for
751 placing package files, as discussed in
752 <citerefentry><refentrytitle>file-hierarchy</refentrytitle><manvolnum>7</manvolnum></citerefentry>.</para>
753 </refsect1>
754
755 <refsect1>
756 <title>See Also</title>
757 <para>
758 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
759 <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
760 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
761 <citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
762 <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
763 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
764 <citerefentry><refentrytitle>file-hierarchy</refentrytitle><manvolnum>7</manvolnum></citerefentry>
765 </para>
766 </refsect1>
767
768 </refentry>