~ubuntu-branches/ubuntu/trusty/systemd/trusty

« back to all changes in this revision

Viewing changes to man/daemon.7

Tags: upstream-202
ImportĀ upstreamĀ versionĀ 202

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
'\" t
2
 
.\"     Title: daemon
3
 
.\"    Author: Lennart Poettering <lennart@poettering.net>
4
 
.\" Generator: DocBook XSL Stylesheets v1.77.1 <http://docbook.sf.net/>
5
 
.\"      Date: 03/07/2013
6
 
.\"    Manual: daemon
7
 
.\"    Source: systemd
8
 
.\"  Language: English
9
 
.\"
10
 
.TH "DAEMON" "7" "" "systemd" "daemon"
11
 
.\" -----------------------------------------------------------------
12
 
.\" * Define some portability stuff
13
 
.\" -----------------------------------------------------------------
14
 
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15
 
.\" http://bugs.debian.org/507673
16
 
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17
 
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18
 
.ie \n(.g .ds Aq \(aq
19
 
.el       .ds Aq '
20
 
.\" -----------------------------------------------------------------
21
 
.\" * set default formatting
22
 
.\" -----------------------------------------------------------------
23
 
.\" disable hyphenation
24
 
.nh
25
 
.\" disable justification (adjust text to left margin only)
26
 
.ad l
27
 
.\" -----------------------------------------------------------------
28
 
.\" * MAIN CONTENT STARTS HERE *
29
 
.\" -----------------------------------------------------------------
30
 
.SH "NAME"
31
 
daemon \- Writing and packaging system daemons
32
 
.SH "DESCRIPTION"
33
 
.PP
34
 
A daemon is a service process that runs in the background and supervises the system or provides functionality to other processes\&. Traditionally, daemons are implemented following a scheme originating in SysV Unix\&. Modern daemons should follow a simpler yet more powerful scheme (here called "new\-style" daemons), as implemented by
35
 
\fBsystemd\fR(1)\&. This manual page covers both schemes, and in particular includes recommendations for daemons that shall be included in the systemd init system\&.
36
 
.SS "SysV Daemons"
37
 
.PP
38
 
When a traditional SysV daemon starts, it should execute the following steps as part of the initialization\&. Note that these steps are unnecessary for new\-style daemons (see below), and should only be implemented if compatibility with SysV is essential\&.
39
 
.sp
40
 
.RS 4
41
 
.ie n \{\
42
 
\h'-04' 1.\h'+01'\c
43
 
.\}
44
 
.el \{\
45
 
.sp -1
46
 
.IP "  1." 4.2
47
 
.\}
48
 
Close all open file descriptors except STDIN, STDOUT, STDERR (i\&.e\&. the first three file descriptors 0, 1, 2)\&. This ensures that no accidentally passed file descriptor stays around in the daemon process\&. On Linux this is best implemented by iterating through
49
 
/proc/self/fd, with a fallback of iterating from file descriptor 3 to the value returned by
50
 
\fBgetrlimit()\fR
51
 
for RLIMIT_NOFILE\&.
52
 
.RE
53
 
.sp
54
 
.RS 4
55
 
.ie n \{\
56
 
\h'-04' 2.\h'+01'\c
57
 
.\}
58
 
.el \{\
59
 
.sp -1
60
 
.IP "  2." 4.2
61
 
.\}
62
 
Reset all signal handlers to their default\&. This is best done by iterating through the available signals up to the limit of _NSIG and resetting them to SIG_DFL\&.
63
 
.RE
64
 
.sp
65
 
.RS 4
66
 
.ie n \{\
67
 
\h'-04' 3.\h'+01'\c
68
 
.\}
69
 
.el \{\
70
 
.sp -1
71
 
.IP "  3." 4.2
72
 
.\}
73
 
Reset the signal mask using
74
 
\fBsigprocmask()\fR\&.
75
 
.RE
76
 
.sp
77
 
.RS 4
78
 
.ie n \{\
79
 
\h'-04' 4.\h'+01'\c
80
 
.\}
81
 
.el \{\
82
 
.sp -1
83
 
.IP "  4." 4.2
84
 
.\}
85
 
Sanitize the environment block, removing or resetting environment variables that might negatively impact daemon runtime\&.
86
 
.RE
87
 
.sp
88
 
.RS 4
89
 
.ie n \{\
90
 
\h'-04' 5.\h'+01'\c
91
 
.\}
92
 
.el \{\
93
 
.sp -1
94
 
.IP "  5." 4.2
95
 
.\}
96
 
Call
97
 
\fBfork()\fR, to create a background process\&.
98
 
.RE
99
 
.sp
100
 
.RS 4
101
 
.ie n \{\
102
 
\h'-04' 6.\h'+01'\c
103
 
.\}
104
 
.el \{\
105
 
.sp -1
106
 
.IP "  6." 4.2
107
 
.\}
108
 
In the child, call
109
 
\fBsetsid()\fR
110
 
to detach from any terminal and create an independent session\&.
111
 
.RE
112
 
.sp
113
 
.RS 4
114
 
.ie n \{\
115
 
\h'-04' 7.\h'+01'\c
116
 
.\}
117
 
.el \{\
118
 
.sp -1
119
 
.IP "  7." 4.2
120
 
.\}
121
 
In the child, call
122
 
\fBfork()\fR
123
 
again, to ensure the daemon can never re\-acquire a terminal again\&.
124
 
.RE
125
 
.sp
126
 
.RS 4
127
 
.ie n \{\
128
 
\h'-04' 8.\h'+01'\c
129
 
.\}
130
 
.el \{\
131
 
.sp -1
132
 
.IP "  8." 4.2
133
 
.\}
134
 
Call
135
 
\fBexit()\fR
136
 
in the first child, so that only the second child (the actual daemon process) stays around\&. This ensures that the daemon process is re\-parented to init/PID 1, as all daemons should be\&.
137
 
.RE
138
 
.sp
139
 
.RS 4
140
 
.ie n \{\
141
 
\h'-04' 9.\h'+01'\c
142
 
.\}
143
 
.el \{\
144
 
.sp -1
145
 
.IP "  9." 4.2
146
 
.\}
147
 
In the daemon process, connect
148
 
/dev/null
149
 
to STDIN, STDOUT, STDERR\&.
150
 
.RE
151
 
.sp
152
 
.RS 4
153
 
.ie n \{\
154
 
\h'-04'10.\h'+01'\c
155
 
.\}
156
 
.el \{\
157
 
.sp -1
158
 
.IP "10." 4.2
159
 
.\}
160
 
In the daemon process, reset the umask to 0, so that the file modes passed to
161
 
\fBopen()\fR,
162
 
\fBmkdir()\fR
163
 
and suchlike directly control the access mode of the created files and directories\&.
164
 
.RE
165
 
.sp
166
 
.RS 4
167
 
.ie n \{\
168
 
\h'-04'11.\h'+01'\c
169
 
.\}
170
 
.el \{\
171
 
.sp -1
172
 
.IP "11." 4.2
173
 
.\}
174
 
In the daemon process, change the current directory to the root directory (/), in order to avoid that the daemon involuntarily blocks mount points from being unmounted\&.
175
 
.RE
176
 
.sp
177
 
.RS 4
178
 
.ie n \{\
179
 
\h'-04'12.\h'+01'\c
180
 
.\}
181
 
.el \{\
182
 
.sp -1
183
 
.IP "12." 4.2
184
 
.\}
185
 
In the daemon process, write the daemon PID (as returned by
186
 
\fBgetpid()\fR) to a PID file, for example
187
 
/var/run/foobar\&.pid
188
 
(for a hypothetical daemon "foobar"), to ensure that the daemon cannot be started more than once\&. This must be implemented in race\-free fashion so that the PID file is only updated when at the same time it is verified that the PID previously stored in the PID file no longer exists or belongs to a foreign process\&. Commonly some kind of file locking is employed to implement this logic\&.
189
 
.RE
190
 
.sp
191
 
.RS 4
192
 
.ie n \{\
193
 
\h'-04'13.\h'+01'\c
194
 
.\}
195
 
.el \{\
196
 
.sp -1
197
 
.IP "13." 4.2
198
 
.\}
199
 
In the daemon process, drop privileges, if possible and applicable\&.
200
 
.RE
201
 
.sp
202
 
.RS 4
203
 
.ie n \{\
204
 
\h'-04'14.\h'+01'\c
205
 
.\}
206
 
.el \{\
207
 
.sp -1
208
 
.IP "14." 4.2
209
 
.\}
210
 
From the daemon process notify the original process started that initialization is complete\&. This can be implemented via an unnamed pipe or similar communication channel that is created before the first
211
 
\fBfork()\fR
212
 
and hence available in both the original and the daemon process\&.
213
 
.RE
214
 
.sp
215
 
.RS 4
216
 
.ie n \{\
217
 
\h'-04'15.\h'+01'\c
218
 
.\}
219
 
.el \{\
220
 
.sp -1
221
 
.IP "15." 4.2
222
 
.\}
223
 
Call
224
 
\fBexit()\fR
225
 
in the original process\&. The process that invoked the daemon must be able to rely on that this
226
 
\fBexit()\fR
227
 
happens after initialization is complete and all external communication channels are established and accessible\&.
228
 
.RE
229
 
.PP
230
 
The BSD
231
 
\fBdaemon()\fR
232
 
function should not be used, as it implements only a subset of these steps\&.
233
 
.PP
234
 
A daemon that needs to provide compatibility with SysV systems should implement the scheme pointed out above\&. However, it is recommended to make this behavior optional and configurable via a command line argument, to ease debugging as well as to simplify integration into systems using systemd\&.
235
 
.SS "New\-Style Daemons"
236
 
.PP
237
 
Modern services for Linux should be implemented as new\-style daemons\&. This makes it easier to supervise and control them at runtime and simplifies their implementation\&.
238
 
.PP
239
 
For developing a new\-style daemon none of the initialization steps recommended for SysV daemons need to be implemented\&. New\-style init systems such as systemd make all of them redundant\&. Moreover, since some of these steps interfere with process monitoring, file descriptor passing and other functionality of the init system it is recommended not to execute them when run as new\-style service\&.
240
 
.PP
241
 
Note that new\-style init systems guarantee execution of daemon processes in clean process contexts: it is guaranteed that the environment block is sanitized, that the signal handlers and mask is reset and that no left\-over file descriptors are passed\&. Daemons will be executed in their own session, and STDIN/STDOUT/STDERR connected to
242
 
/dev/null
243
 
unless otherwise configured\&. The umask is reset\&.
244
 
.PP
245
 
It is recommended for new\-style daemons to implement the following:
246
 
.sp
247
 
.RS 4
248
 
.ie n \{\
249
 
\h'-04' 1.\h'+01'\c
250
 
.\}
251
 
.el \{\
252
 
.sp -1
253
 
.IP "  1." 4.2
254
 
.\}
255
 
If SIGTERM is received, shut down the daemon and exit cleanly\&.
256
 
.RE
257
 
.sp
258
 
.RS 4
259
 
.ie n \{\
260
 
\h'-04' 2.\h'+01'\c
261
 
.\}
262
 
.el \{\
263
 
.sp -1
264
 
.IP "  2." 4.2
265
 
.\}
266
 
If SIGHUP is received, reload the configuration files, if this applies\&.
267
 
.RE
268
 
.sp
269
 
.RS 4
270
 
.ie n \{\
271
 
\h'-04' 3.\h'+01'\c
272
 
.\}
273
 
.el \{\
274
 
.sp -1
275
 
.IP "  3." 4.2
276
 
.\}
277
 
Provide a correct exit code from the main daemon process, as this is used by the init system to detect service errors and problems\&. It is recommended to follow the exit code scheme as defined in the
278
 
\m[blue]\fBLSB recommendations for SysV init scripts\fR\m[]\&\s-2\u[1]\d\s+2\&.
279
 
.RE
280
 
.sp
281
 
.RS 4
282
 
.ie n \{\
283
 
\h'-04' 4.\h'+01'\c
284
 
.\}
285
 
.el \{\
286
 
.sp -1
287
 
.IP "  4." 4.2
288
 
.\}
289
 
If possible and applicable expose the daemon\*(Aqs control interface via the D\-Bus IPC system and grab a bus name as last step of initialization\&.
290
 
.RE
291
 
.sp
292
 
.RS 4
293
 
.ie n \{\
294
 
\h'-04' 5.\h'+01'\c
295
 
.\}
296
 
.el \{\
297
 
.sp -1
298
 
.IP "  5." 4.2
299
 
.\}
300
 
For integration in systemd, provide a
301
 
\&.service
302
 
unit file that carries information about starting, stopping and otherwise maintaining the daemon\&. See
303
 
\fBsystemd.service\fR(5)
304
 
for details\&.
305
 
.RE
306
 
.sp
307
 
.RS 4
308
 
.ie n \{\
309
 
\h'-04' 6.\h'+01'\c
310
 
.\}
311
 
.el \{\
312
 
.sp -1
313
 
.IP "  6." 4.2
314
 
.\}
315
 
As much as possible, rely on the init system\*(Aqs functionality to limit the access of the daemon to files, services and other resources\&. i\&.e\&. in the case of systemd, rely on systemd\*(Aqs resource limit control instead of implementing your own, rely on systemd\*(Aqs privilege dropping code instead of implementing it in the daemon, and similar\&. See
316
 
\fBsystemd.exec\fR(5)
317
 
for the available controls\&.
318
 
.RE
319
 
.sp
320
 
.RS 4
321
 
.ie n \{\
322
 
\h'-04' 7.\h'+01'\c
323
 
.\}
324
 
.el \{\
325
 
.sp -1
326
 
.IP "  7." 4.2
327
 
.\}
328
 
If D\-Bus is used, make your daemon bus\-activatable, via supplying a D\-Bus service activation configuration file\&. This has multiple advantages: your daemon may be started lazily on\-demand; it may be started in parallel to other daemons requiring it \-\- which maximizes parallelization and boot\-up speed; your daemon can be restarted on failure, without losing any bus requests, as the bus queues requests for activatable services\&. See below for details\&.
329
 
.RE
330
 
.sp
331
 
.RS 4
332
 
.ie n \{\
333
 
\h'-04' 8.\h'+01'\c
334
 
.\}
335
 
.el \{\
336
 
.sp -1
337
 
.IP "  8." 4.2
338
 
.\}
339
 
If your daemon provides services to other local processes or remote clients via a socket, it should be made socket\-activatable following the scheme pointed out below\&. Like D\-Bus activation this enables on\-demand starting of services as well as it allows improved parallelization of service start\-up\&. Also, for state\-less protocols (such as syslog, DNS) a daemon implementing socket\-based activation can be restarted without losing a single request\&. See below for details\&.
340
 
.RE
341
 
.sp
342
 
.RS 4
343
 
.ie n \{\
344
 
\h'-04' 9.\h'+01'\c
345
 
.\}
346
 
.el \{\
347
 
.sp -1
348
 
.IP "  9." 4.2
349
 
.\}
350
 
If applicable a daemon should notify the init system about startup completion or status updates via the
351
 
\fBsd_notify\fR(3)
352
 
interface\&.
353
 
.RE
354
 
.sp
355
 
.RS 4
356
 
.ie n \{\
357
 
\h'-04'10.\h'+01'\c
358
 
.\}
359
 
.el \{\
360
 
.sp -1
361
 
.IP "10." 4.2
362
 
.\}
363
 
Instead of using the
364
 
\fBsyslog()\fR
365
 
call to log directly to the system syslog service, a new\-style daemon may choose to simply log to STDERR via
366
 
\fBfprintf()\fR, which is then forwarded to syslog by the init system\&. If log priorities are necessary these can be encoded by prefixing individual log lines with strings like "<4>" (for log priority 4 "WARNING" in the syslog priority scheme), following a similar style as the Linux kernel\*(Aqs
367
 
\fBprintk()\fR
368
 
priority system\&. In fact, using this style of logging also enables the init system to optionally direct all application logging to the kernel log buffer (kmsg), as accessible via
369
 
\fBdmesg\fR(1)\&. This kind of logging may be enabled by setting
370
 
\fIStandardError=syslog\fR
371
 
in the service unit file\&. For details see
372
 
\fBsd-daemon\fR(3)
373
 
and
374
 
\fBsystemd.exec\fR(5)\&.
375
 
.RE
376
 
.PP
377
 
These recommendations are similar but not identical to the
378
 
\m[blue]\fBApple MacOS X Daemon Requirements\fR\m[]\&\s-2\u[2]\d\s+2\&.
379
 
.SH "ACTIVATION"
380
 
.PP
381
 
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:
382
 
bluetoothd\&.service
383
 
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\&.
384
 
.SS "Activation on Boot"
385
 
.PP
386
 
Old\-style daemons are usually activated exclusively on boot (and manually by the administrator) via SysV init scripts, as detailed in the
387
 
\m[blue]\fBLSB Linux Standard Base Core Specification\fR\m[]\&\s-2\u[1]\d\s+2\&. 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\&.
388
 
.PP
389
 
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
390
 
\&.wants/
391
 
directory of either
392
 
multi\-user\&.target
393
 
or
394
 
graphical\&.target, which are normally used as boot targets at system startup\&. See
395
 
\fBsystemd.unit\fR(5)
396
 
for details about the
397
 
\&.wants/
398
 
directories, and
399
 
\fBsystemd.special\fR(7)
400
 
for details about the two boot targets\&.
401
 
.SS "Socket\-Based Activation"
402
 
.PP
403
 
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\&.
404
 
.PP
405
 
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
406
 
\fBsd_listen_fds\fR(3)
407
 
and
408
 
\fBsd-daemon\fR(3)\&. 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\&.
409
 
.PP
410
 
systemd implements socket\-based activation via
411
 
\&.socket
412
 
units, which are described in
413
 
\fBsystemd.socket\fR(5)\&. When configuring socket units for socket\-based activation it is essential that all listening sockets are pulled in by the special target unit
414
 
sockets\&.target\&. It is recommended to place a
415
 
\fIWantedBy=sockets\&.target\fR
416
 
directive in the
417
 
[Install]
418
 
section, to automatically add such a dependency on installation of a socket unit\&. Unless
419
 
\fIDefaultDependencies=no\fR
420
 
is set the necessary ordering dependencies are implicitly created for all socket units\&. For more information about
421
 
sockets\&.target
422
 
see
423
 
\fBsystemd.special\fR(7)\&. It is not necessary or recommended to place any additional dependencies on socket units (for example from
424
 
multi\-user\&.target
425
 
or suchlike) when one is installed in
426
 
sockets\&.target\&.
427
 
.SS "Bus\-Based Activation"
428
 
.PP
429
 
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
430
 
\fISystemdService=\fR
431
 
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
432
 
org\&.freedesktop\&.RealtimeKit\&.service, make sure to set
433
 
\fISystemdService=rtkit\-daemon\&.service\fR
434
 
in that file, to bind it to the systemd service
435
 
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\&.
436
 
.SS "Device\-Based Activation"
437
 
.PP
438
 
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
439
 
\fISYSTEMD_WANTS=\fR
440
 
property\&. See
441
 
\fBsystemd.device\fR(5)
442
 
for details\&. Often it is nicer to pull in services from devices only indirectly via dedicated targets\&. Example: instead of pulling in
443
 
bluetoothd\&.service
444
 
from all the various bluetooth dongles and other hardware available, pull in bluetooth\&.target from them and
445
 
bluetoothd\&.service
446
 
from that target\&. This provides for nicer abstraction and gives administrators the option to enable
447
 
bluetoothd\&.service
448
 
via controlling a
449
 
bluetooth\&.target\&.wants/
450
 
symlink uniformly with a command like
451
 
\fBenable\fR
452
 
of
453
 
\fBsystemctl\fR(1)
454
 
instead of manipulating the udev ruleset\&.
455
 
.SS "Path\-Based Activation"
456
 
.PP
457
 
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
458
 
\&.path
459
 
units, as outlined in
460
 
\fBsystemd.path\fR(5)\&.
461
 
.SS "Timer\-Based Activation"
462
 
.PP
463
 
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
464
 
\&.timer
465
 
units, as described in
466
 
\fBsystemd.timer\fR(5)\&.
467
 
.SS "Other Forms of Activation"
468
 
.PP
469
 
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
470
 
\&.socket
471
 
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
472
 
\fIFreeBind=yes\fR
473
 
in systemd socket files (see
474
 
\fBsystemd.socket\fR(5)
475
 
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
476
 
\fICPUSchedulingPolicy=idle\fR
477
 
and/or
478
 
\fIIOSchedulingClass=idle\fR\&. 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\&.
479
 
.SH "INTEGRATION WITH SYSTEMD"
480
 
.SS "Writing Systemd Unit Files"
481
 
.PP
482
 
When writing systemd unit files, it is recommended to consider the following suggestions:
483
 
.sp
484
 
.RS 4
485
 
.ie n \{\
486
 
\h'-04' 1.\h'+01'\c
487
 
.\}
488
 
.el \{\
489
 
.sp -1
490
 
.IP "  1." 4.2
491
 
.\}
492
 
If possible do not use the
493
 
\fIType=forking\fR
494
 
setting in service files\&. But if you do, make sure to set the PID file path using
495
 
\fIPIDFile=\fR\&. See
496
 
\fBsystemd.service\fR(5)
497
 
for details\&.
498
 
.RE
499
 
.sp
500
 
.RS 4
501
 
.ie n \{\
502
 
\h'-04' 2.\h'+01'\c
503
 
.\}
504
 
.el \{\
505
 
.sp -1
506
 
.IP "  2." 4.2
507
 
.\}
508
 
If your daemon registers a D\-Bus name on the bus, make sure to use
509
 
\fIType=dbus\fR
510
 
in the service file if possible\&.
511
 
.RE
512
 
.sp
513
 
.RS 4
514
 
.ie n \{\
515
 
\h'-04' 3.\h'+01'\c
516
 
.\}
517
 
.el \{\
518
 
.sp -1
519
 
.IP "  3." 4.2
520
 
.\}
521
 
Make sure to set a good human\-readable description string with
522
 
\fIDescription=\fR\&.
523
 
.RE
524
 
.sp
525
 
.RS 4
526
 
.ie n \{\
527
 
\h'-04' 4.\h'+01'\c
528
 
.\}
529
 
.el \{\
530
 
.sp -1
531
 
.IP "  4." 4.2
532
 
.\}
533
 
Do not disable
534
 
\fIDefaultDependencies=\fR, unless you really know what you do and your unit is involved in early boot or late system shutdown\&.
535
 
.RE
536
 
.sp
537
 
.RS 4
538
 
.ie n \{\
539
 
\h'-04' 5.\h'+01'\c
540
 
.\}
541
 
.el \{\
542
 
.sp -1
543
 
.IP "  5." 4.2
544
 
.\}
545
 
Normally, little if any dependencies should need to be defined explicitly\&. However, if you do configure explicit dependencies, only refer to unit names listed on
546
 
\fBsystemd.special\fR(7)
547
 
or names introduced by your own package to keep the unit file operating system\-independent\&.
548
 
.RE
549
 
.sp
550
 
.RS 4
551
 
.ie n \{\
552
 
\h'-04' 6.\h'+01'\c
553
 
.\}
554
 
.el \{\
555
 
.sp -1
556
 
.IP "  6." 4.2
557
 
.\}
558
 
Make sure to include an
559
 
[Install]
560
 
section including installation information for the unit file\&. See
561
 
\fBsystemd.unit\fR(5)
562
 
for details\&. To activate your service on boot make sure to add a
563
 
\fIWantedBy=multi\-user\&.target\fR
564
 
or
565
 
\fIWantedBy=graphical\&.target\fR
566
 
directive\&. To activate your socket on boot, make sure to add
567
 
\fIWantedBy=sockets\&.target\fR\&. Usually you also want to make sure that when your service is installed your socket is installed too, hence add
568
 
\fIAlso=foo\&.socket\fR
569
 
in your service file
570
 
foo\&.service, for a hypothetical program
571
 
foo\&.
572
 
.RE
573
 
.SS "Installing Systemd Service Files"
574
 
.PP
575
 
At the build installation time (e\&.g\&.
576
 
\fBmake install\fR
577
 
during package build) packages are recommended to install their systemd unit files in the directory returned by
578
 
\fBpkg\-config systemd \-\-variable=systemdsystemunitdir\fR
579
 
(for system services) or
580
 
\fBpkg\-config systemd \-\-variable=systemduserunitdir\fR
581
 
(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\&.
582
 
\fBrpm \-i\fR
583
 
by the administrator) symlinks should be created in the systemd configuration directories via the
584
 
\fBenable\fR
585
 
command of the
586
 
\fBsystemctl\fR(1)
587
 
tool, to activate them automatically on boot\&.
588
 
.PP
589
 
Packages using
590
 
\fBautoconf\fR(1)
591
 
are recommended to use a configure script excerpt like the following to determine the unit installation path during source configuration:
592
 
.sp
593
 
.if n \{\
594
 
.RS 4
595
 
.\}
596
 
.nf
597
 
PKG_PROG_PKG_CONFIG
598
 
AC_ARG_WITH([systemdsystemunitdir],
599
 
        AS_HELP_STRING([\-\-with\-systemdsystemunitdir=DIR], [Directory for systemd service files]),
600
 
        [], [with_systemdsystemunitdir=$($PKG_CONFIG \-\-variable=systemdsystemunitdir systemd)])
601
 
if test "x$with_systemdsystemunitdir" != xno; then
602
 
        AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])
603
 
fi
604
 
AM_CONDITIONAL(HAVE_SYSTEMD, [test \-n "$with_systemdsystemunitdir" \-a "x$with_systemdsystemunitdir" != xno ])
605
 
.fi
606
 
.if n \{\
607
 
.RE
608
 
.\}
609
 
.PP
610
 
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 user unit directory is left as an exercise for the reader\&.)
611
 
.PP
612
 
Additionally, to ensure that
613
 
\fBmake distcheck\fR
614
 
continues to work, it is recommended to add the following to the top\-level
615
 
Makefile\&.am
616
 
file in
617
 
\fBautomake\fR(1)\-based projects:
618
 
.sp
619
 
.if n \{\
620
 
.RS 4
621
 
.\}
622
 
.nf
623
 
DISTCHECK_CONFIGURE_FLAGS = \e
624
 
        \-\-with\-systemdsystemunitdir=$$dc_install_base/$(systemdsystemunitdir)
625
 
.fi
626
 
.if n \{\
627
 
.RE
628
 
.\}
629
 
.PP
630
 
Finally, unit files should be installed in the system with an automake excerpt like the following:
631
 
.sp
632
 
.if n \{\
633
 
.RS 4
634
 
.\}
635
 
.nf
636
 
if HAVE_SYSTEMD
637
 
systemdsystemunit_DATA = \e
638
 
        foobar\&.socket \e
639
 
        foobar\&.service
640
 
endif
641
 
.fi
642
 
.if n \{\
643
 
.RE
644
 
.\}
645
 
.PP
646
 
In the
647
 
\fBrpm\fR(8)
648
 
\&.spec
649
 
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 package managers\&.
650
 
.PP
651
 
At the top of the file:
652
 
.sp
653
 
.if n \{\
654
 
.RS 4
655
 
.\}
656
 
.nf
657
 
BuildRequires: systemd
658
 
%{?systemd_requires}
659
 
.fi
660
 
.if n \{\
661
 
.RE
662
 
.\}
663
 
.PP
664
 
And as scriptlets, further down:
665
 
.sp
666
 
.if n \{\
667
 
.RS 4
668
 
.\}
669
 
.nf
670
 
%post
671
 
%systemd_post foobar\&.service foobar\&.socket
672
 
 
673
 
%preun
674
 
%systemd_preun foobar\&.service foobar\&.socket
675
 
 
676
 
%postun
677
 
%systemd_postun
678
 
.fi
679
 
.if n \{\
680
 
.RE
681
 
.\}
682
 
.PP
683
 
If the service shall be restarted during upgrades replace the
684
 
%postun
685
 
scriptlet above with the following:
686
 
.sp
687
 
.if n \{\
688
 
.RS 4
689
 
.\}
690
 
.nf
691
 
%postun
692
 
%systemd_postun_with_restart foobar\&.service
693
 
.fi
694
 
.if n \{\
695
 
.RE
696
 
.\}
697
 
.PP
698
 
Note that
699
 
%systemd_post
700
 
and
701
 
%systemd_preun
702
 
expect the names of all units that are installed/removed as arguments, separated by spaces\&.
703
 
%systemd_postun
704
 
expects no arguments\&.
705
 
%systemd_postun_with_restart
706
 
expects the units to restart as arguments\&.
707
 
.PP
708
 
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:
709
 
.sp
710
 
.if n \{\
711
 
.RS 4
712
 
.\}
713
 
.nf
714
 
%triggerun \-\- foobar < 0\&.47\&.11\-1
715
 
if /sbin/chkconfig \-\-level 5 foobar ; then
716
 
        /bin/systemctl \-\-no\-reload enable foobar\&.service foobar\&.socket >/dev/null 2>&1 || :
717
 
fi
718
 
.fi
719
 
.if n \{\
720
 
.RE
721
 
.\}
722
 
.PP
723
 
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
724
 
\fBchkconfig\fR
725
 
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\&.
726
 
.SH "PORTING EXISTING DAEMONS"
727
 
.PP
728
 
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\&.
729
 
.PP
730
 
To port an existing SysV compatible daemon the following steps are recommended:
731
 
.sp
732
 
.RS 4
733
 
.ie n \{\
734
 
\h'-04' 1.\h'+01'\c
735
 
.\}
736
 
.el \{\
737
 
.sp -1
738
 
.IP "  1." 4.2
739
 
.\}
740
 
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\&.
741
 
.RE
742
 
.sp
743
 
.RS 4
744
 
.ie n \{\
745
 
\h'-04' 2.\h'+01'\c
746
 
.\}
747
 
.el \{\
748
 
.sp -1
749
 
.IP "  2." 4.2
750
 
.\}
751
 
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
752
 
\fBsd_listen_fds\fR(3)
753
 
is checked for already passed sockets first\&. If sockets are passed (i\&.e\&. when
754
 
\fBsd_listen_fds()\fR
755
 
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\&.
756
 
.RE
757
 
.sp
758
 
.RS 4
759
 
.ie n \{\
760
 
\h'-04' 3.\h'+01'\c
761
 
.\}
762
 
.el \{\
763
 
.sp -1
764
 
.IP "  3." 4.2
765
 
.\}
766
 
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\&.
767
 
.RE
768
 
.sp
769
 
.RS 4
770
 
.ie n \{\
771
 
\h'-04' 4.\h'+01'\c
772
 
.\}
773
 
.el \{\
774
 
.sp -1
775
 
.IP "  4." 4.2
776
 
.\}
777
 
If the daemon exposes interfaces via D\-Bus, write and install a D\-Bus activation file for the service, see above for details\&.
778
 
.RE
779
 
.SH "SEE ALSO"
780
 
.PP
781
 
 
782
 
\fBsystemd\fR(1),
783
 
\fBsd-daemon\fR(3),
784
 
\fBsd_listen_fds\fR(3),
785
 
\fBsd_notify\fR(3),
786
 
\fBdaemon\fR(3),
787
 
\fBsystemd.service\fR(5)
788
 
.SH "NOTES"
789
 
.IP " 1." 4
790
 
LSB recommendations for SysV init scripts
791
 
.RS 4
792
 
\%http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
793
 
.RE
794
 
.IP " 2." 4
795
 
Apple MacOS X Daemon Requirements
796
 
.RS 4
797
 
\%http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/LaunchOnDemandDaemons.html#//apple_ref/doc/uid/TP40001762-104738
798
 
.RE