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