Debian Codegraph @

upstream/debian-unstable/man7/daemon.7

raw
1 '\" t
2 .TH "DAEMON" "7" "" "systemd 261~rc3" "daemon"
3 .\" -----------------------------------------------------------------
4 .\" * Define some portability stuff
5 .\" -----------------------------------------------------------------
6 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 .\" http://bugs.debian.org/507673
8 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
9 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 .ie \n(.g .ds Aq \(aq
11 .el .ds Aq '
12 .\" -----------------------------------------------------------------
13 .\" * set default formatting
14 .\" -----------------------------------------------------------------
15 .\" disable hyphenation
16 .nh
17 .\" disable justification (adjust text to left margin only)
18 .ad l
19 .\" -----------------------------------------------------------------
20 .\" * MAIN CONTENT STARTS HERE *
21 .\" -----------------------------------------------------------------
22 .SH "NAME"
23 daemon \- Writing and packaging system daemons
24 .SH "DESCRIPTION"
25 .PP
26 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
27 \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\&.
28 .SS "SysV Daemons"
29 .PP
30 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\&.
31 .sp
32 .RS 4
33 .ie n \{\
34 \h'-04' 1.\h'+01'\c
35 .\}
36 .el \{\
37 .sp -1
38 .IP " 1." 4.2
39 .\}
40 Close all open file descriptors except standard input, output, and error (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
41 /proc/self/fd, with a fallback of iterating from file descriptor 3 to the value returned by
42 \fBgetrlimit()\fR
43 for
44 \fBRLIMIT_NOFILE\fR\&.
45 .RE
46 .sp
47 .RS 4
48 .ie n \{\
49 \h'-04' 2.\h'+01'\c
50 .\}
51 .el \{\
52 .sp -1
53 .IP " 2." 4.2
54 .\}
55 Reset all signal handlers to their default\&. This is best done by iterating through the available signals up to the limit of
56 \fB_NSIG\fR
57 and resetting them to
58 \fBSIG_DFL\fR\&.
59 .RE
60 .sp
61 .RS 4
62 .ie n \{\
63 \h'-04' 3.\h'+01'\c
64 .\}
65 .el \{\
66 .sp -1
67 .IP " 3." 4.2
68 .\}
69 Reset the signal mask using
70 \fBsigprocmask()\fR\&.
71 .RE
72 .sp
73 .RS 4
74 .ie n \{\
75 \h'-04' 4.\h'+01'\c
76 .\}
77 .el \{\
78 .sp -1
79 .IP " 4." 4.2
80 .\}
81 Sanitize the environment block, removing or resetting environment variables that might negatively impact daemon runtime\&.
82 .RE
83 .sp
84 .RS 4
85 .ie n \{\
86 \h'-04' 5.\h'+01'\c
87 .\}
88 .el \{\
89 .sp -1
90 .IP " 5." 4.2
91 .\}
92 Call
93 \fBfork()\fR, to create a background process\&.
94 .RE
95 .sp
96 .RS 4
97 .ie n \{\
98 \h'-04' 6.\h'+01'\c
99 .\}
100 .el \{\
101 .sp -1
102 .IP " 6." 4.2
103 .\}
104 In the child, call
105 \fBsetsid()\fR
106 to detach from any terminal and create an independent session\&.
107 .RE
108 .sp
109 .RS 4
110 .ie n \{\
111 \h'-04' 7.\h'+01'\c
112 .\}
113 .el \{\
114 .sp -1
115 .IP " 7." 4.2
116 .\}
117 In the child, call
118 \fBfork()\fR
119 again, to ensure that the daemon can never re\-acquire a terminal again\&. (This is relevant if the program \(em and all its dependencies \(em does not carefully specify `O_NOCTTY` on each and every single `open()` call that might potentially open a TTY device node\&.)
120 .RE
121 .sp
122 .RS 4
123 .ie n \{\
124 \h'-04' 8.\h'+01'\c
125 .\}
126 .el \{\
127 .sp -1
128 .IP " 8." 4.2
129 .\}
130 Call
131 \fBexit()\fR
132 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\&.
133 .RE
134 .sp
135 .RS 4
136 .ie n \{\
137 \h'-04' 9.\h'+01'\c
138 .\}
139 .el \{\
140 .sp -1
141 .IP " 9." 4.2
142 .\}
143 In the daemon process, connect
144 /dev/null
145 to standard input, output, and error\&.
146 .RE
147 .sp
148 .RS 4
149 .ie n \{\
150 \h'-04'10.\h'+01'\c
151 .\}
152 .el \{\
153 .sp -1
154 .IP "10." 4.2
155 .\}
156 In the daemon process, reset the umask to 0, so that the file modes passed to
157 \fBopen()\fR,
158 \fBmkdir()\fR
159 and suchlike directly control the access mode of the created files and directories\&.
160 .RE
161 .sp
162 .RS 4
163 .ie n \{\
164 \h'-04'11.\h'+01'\c
165 .\}
166 .el \{\
167 .sp -1
168 .IP "11." 4.2
169 .\}
170 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\&.
171 .RE
172 .sp
173 .RS 4
174 .ie n \{\
175 \h'-04'12.\h'+01'\c
176 .\}
177 .el \{\
178 .sp -1
179 .IP "12." 4.2
180 .\}
181 In the daemon process, write the daemon PID (as returned by
182 \fBgetpid()\fR) to a PID file, for example
183 /run/foobar\&.pid
184 (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 it is verified at the same time that the PID previously stored in the PID file no longer exists or belongs to a foreign process\&.
185 .RE
186 .sp
187 .RS 4
188 .ie n \{\
189 \h'-04'13.\h'+01'\c
190 .\}
191 .el \{\
192 .sp -1
193 .IP "13." 4.2
194 .\}
195 In the daemon process, drop privileges, if possible and applicable\&.
196 .RE
197 .sp
198 .RS 4
199 .ie n \{\
200 \h'-04'14.\h'+01'\c
201 .\}
202 .el \{\
203 .sp -1
204 .IP "14." 4.2
205 .\}
206 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
207 \fBfork()\fR
208 and hence available in both the original and the daemon process\&.
209 .RE
210 .sp
211 .RS 4
212 .ie n \{\
213 \h'-04'15.\h'+01'\c
214 .\}
215 .el \{\
216 .sp -1
217 .IP "15." 4.2
218 .\}
219 Call
220 \fBexit()\fR
221 in the original process\&. The process that invoked the daemon must be able to rely on that this
222 \fBexit()\fR
223 happens after initialization is complete and all external communication channels are established and accessible\&.
224 .RE
225 .PP
226 The BSD
227 \fBdaemon()\fR
228 function should not be used, as it implements only a subset of these steps\&.
229 .PP
230 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\&.
231 .SS "New\-Style Daemons"
232 .PP
233 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\&.
234 .PP
235 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 service manager, it is recommended not to execute them when run as new\-style service\&.
236 .PP
237 Note that new\-style init systems guarantee execution of daemon processes in a clean process context: 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, with standard input connected to
238 /dev/null
239 and standard output/error connected to the
240 \fBsystemd-journald.service\fR(8)
241 logging service, unless otherwise configured\&. The umask is reset\&.
242 .PP
243 It is recommended for new\-style daemons to implement the following:
244 .sp
245 .RS 4
246 .ie n \{\
247 \h'-04' 1.\h'+01'\c
248 .\}
249 .el \{\
250 .sp -1
251 .IP " 1." 4.2
252 .\}
253 If applicable, the daemon should notify the service manager about startup completion or status updates via the
254 \fBsd_notify\fR(3)
255 interface, in particular
256 \fIREADY=1\fR
257 and
258 \fISTATUS=\&...\fR\&.
259 .RE
260 .sp
261 .RS 4
262 .ie n \{\
263 \h'-04' 2.\h'+01'\c
264 .\}
265 .el \{\
266 .sp -1
267 .IP " 2." 4.2
268 .\}
269 If
270 \fBSIGTERM\fR
271 is received, shut down the daemon and exit cleanly\&. A
272 \fISTOPPING=1\fR
273 notification should be sent via
274 \fBsd_notify\fR(3)\&.
275 .RE
276 .sp
277 .RS 4
278 .ie n \{\
279 \h'-04' 3.\h'+01'\c
280 .\}
281 .el \{\
282 .sp -1
283 .IP " 3." 4.2
284 .\}
285 If
286 \fBSIGHUP\fR
287 is received, reload the configuration files, if this applies\&. This should be combined with notifications via
288 \fBsd_notify\fR(3):
289 \fIRELOADING=1\fR
290 and
291 \fIREADY=1\fR\&.
292 .RE
293 .sp
294 .RS 4
295 .ie n \{\
296 \h'-04' 4.\h'+01'\c
297 .\}
298 .el \{\
299 .sp -1
300 .IP " 4." 4.2
301 .\}
302 Provide a correct exit code from the main daemon process, as this is used by the service manager to detect service errors and problems\&. It is recommended to follow the exit code scheme as defined in the
303 \m[blue]\fBLSB recommendations for SysV init scripts\fR\m[]\&\s-2\u[1]\d\s+2\&.
304 .RE
305 .sp
306 .RS 4
307 .ie n \{\
308 \h'-04' 5.\h'+01'\c
309 .\}
310 .el \{\
311 .sp -1
312 .IP " 5." 4.2
313 .\}
314 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\&.
315 .RE
316 .sp
317 .RS 4
318 .ie n \{\
319 \h'-04' 6.\h'+01'\c
320 .\}
321 .el \{\
322 .sp -1
323 .IP " 6." 4.2
324 .\}
325 For integration in systemd, provide a
326 \&.service
327 unit file that carries information about starting, stopping and otherwise maintaining the daemon\&. See
328 \fBsystemd.service\fR(5)
329 for details\&.
330 .RE
331 .sp
332 .RS 4
333 .ie n \{\
334 \h'-04' 7.\h'+01'\c
335 .\}
336 .el \{\
337 .sp -1
338 .IP " 7." 4.2
339 .\}
340 As much as possible, rely on the service manager\*(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 so on\&. See
341 \fBsystemd.exec\fR(5)
342 for the available controls\&.
343 .RE
344 .sp
345 .RS 4
346 .ie n \{\
347 \h'-04' 8.\h'+01'\c
348 .\}
349 .el \{\
350 .sp -1
351 .IP " 8." 4.2
352 .\}
353 If D\-Bus is used, make your daemon bus\-activatable by 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 \(em 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\&.
354 .RE
355 .sp
356 .RS 4
357 .ie n \{\
358 \h'-04' 9.\h'+01'\c
359 .\}
360 .el \{\
361 .sp -1
362 .IP " 9." 4.2
363 .\}
364 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\&.
365 .RE
366 .sp
367 .RS 4
368 .ie n \{\
369 \h'-04'10.\h'+01'\c
370 .\}
371 .el \{\
372 .sp -1
373 .IP "10." 4.2
374 .\}
375 If the service opens sockets or other files on it own, and those file descriptors shall survive a restart, the daemon should store them in the service manager via
376 \fBsd_notify\fR(3)
377 with
378 \fIFDSTORE=1\fR\&.
379 .RE
380 .sp
381 .RS 4
382 .ie n \{\
383 \h'-04'11.\h'+01'\c
384 .\}
385 .el \{\
386 .sp -1
387 .IP "11." 4.2
388 .\}
389 Instead of using the
390 \fBsyslog()\fR
391 call to log directly to the system syslog service, a new\-style daemon may choose to simply log to standard error via
392 \fBfprintf()\fR, which is then forwarded to syslog\&. If log levels are necessary, these can be encoded by prefixing individual log lines with strings like
393 "<4>"
394 (for log level 4 "WARNING" in the syslog priority scheme), following a similar style as the Linux kernel\*(Aqs
395 \fBprintk()\fR
396 level system\&. For details, see
397 \fBsd-daemon\fR(3)
398 and
399 \fBsystemd.exec\fR(5)\&.
400 .RE
401 .sp
402 .RS 4
403 .ie n \{\
404 \h'-04'12.\h'+01'\c
405 .\}
406 .el \{\
407 .sp -1
408 .IP "12." 4.2
409 .\}
410 As new\-style daemons are invoked without a controlling TTY (but as their own session leaders) care should be taken to always specify
411 \fBO_NOCTTY\fR
412 on
413 \fBopen\fR(2)
414 calls that possibly reference a TTY device node, so that no controlling TTY is accidentally acquired\&.
415 .RE
416 .PP
417 These recommendations are similar but not identical to the
418 \m[blue]\fBApple MacOS X Daemon Requirements\fR\m[]\&\s-2\u[2]\d\s+2\&.
419 .SH "ACTIVATION"
420 .PP
421 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:
422 bluetoothd\&.service
423 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\&.
424 .SS "Activation on Boot"
425 .PP
426 Old\-style daemons are usually activated exclusively on boot (and manually by the administrator) via SysV init scripts, as detailed in the
427 \m[blue]\fBLSB Linux Standard Base Core Specification\fR\m[]\&\s-2\u[1]\d\s+2\&. Among other issues, SysV init scripts have the disadvantage of involving shell scripts in the boot process\&. Support for init scripts has been phased out in modern Linux init systems, and should not be relied upon\&. New\-style init systems generally use updated versions of activation, both during boot\-up and during runtime and using more minimal service description files\&.
428 .PP
429 In systemd, if the developer or administrator wants to make sure that a service or other unit is activated automatically on boot, it is recommended to place a symlink to the unit file in the
430 \&.wants/
431 directory of either
432 multi\-user\&.target
433 or
434 graphical\&.target, which are normally used as boot targets at system startup\&. See
435 \fBsystemd.unit\fR(5)
436 for details about the
437 \&.wants/
438 directories, and
439 \fBsystemd.special\fR(7)
440 for details about the two boot targets\&.
441 .SS "Socket\-Based Activation"
442 .PP
443 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 use 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 service manager\&. Based on per\-daemon configuration, the service manager 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\&.
444 .PP
445 New\-style daemons which support socket activation must be able to receive their sockets from the service manager instead of creating and binding them themselves\&. For details about the programming interfaces for this scheme provided by systemd, see
446 \fBsd_listen_fds\fR(3)
447 and
448 \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\&.
449 .PP
450 systemd implements socket\-based activation via
451 \&.socket
452 units, which are described in
453 \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
454 sockets\&.target\&. It is recommended to place a
455 \fIWantedBy=sockets\&.target\fR
456 directive in the [Install] section to automatically add such a dependency on installation of a socket unit\&. Unless
457 \fIDefaultDependencies=no\fR
458 is set, the necessary ordering dependencies are implicitly created for all socket units\&. For more information about
459 sockets\&.target, see
460 \fBsystemd.special\fR(7)\&. It is not necessary or recommended to place any additional dependencies on socket units (for example from
461 multi\-user\&.target
462 or suchlike) when one is installed in
463 sockets\&.target\&.
464 .SS "Bus\-Based Activation"
465 .PP
466 When the D\-Bus IPC system is used for communication with clients, new\-style daemons should use 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
467 \fISystemdService=\fR
468 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
469 org\&.freedesktop\&.RealtimeKit\&.service, make sure to set
470 \fISystemdService=rtkit\-daemon\&.service\fR
471 in that file to bind it to the systemd service
472 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\&.
473 .SS "Device\-Based Activation"
474 .PP
475 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
476 "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
477 \fISYSTEMD_WANTS=\fR
478 property\&. See
479 \fBsystemd.device\fR(5)
480 for details\&. Often, it is nicer to pull in services from devices only indirectly via dedicated targets\&. Example: Instead of pulling in
481 bluetoothd\&.service
482 from all the various bluetooth dongles and other hardware available, pull in bluetooth\&.target from them and
483 bluetoothd\&.service
484 from that target\&. This provides for nicer abstraction and gives administrators the option to enable
485 bluetoothd\&.service
486 via controlling a
487 bluetooth\&.target\&.wants/
488 symlink uniformly with a command like
489 \fBenable\fR
490 of
491 \fBsystemctl\fR(1)
492 instead of manipulating the udev ruleset\&.
493 .SS "Path\-Based Activation"
494 .PP
495 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
496 \&.path
497 units, as outlined in
498 \fBsystemd.path\fR(5)\&.
499 .SS "Timer\-Based Activation"
500 .PP
501 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
502 \&.timer
503 units, as described in
504 \fBsystemd.timer\fR(5)\&.
505 .SS "Other Forms of Activation"
506 .PP
507 Other forms of activation have been suggested and implemented in some systems\&. However, there are often 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
508 \&.socket
509 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
510 \fBIP_FREEBIND\fR/\fBIPV6_FREEBIND\fR
511 socket option, as accessible via
512 \fIFreeBind=yes\fR
513 in systemd socket files (see
514 \fBsystemd.socket\fR(5)
515 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 I/O 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 I/O schedulers\&. If a process executed by the service manager shall not negatively impact the amount of CPU or I/O bandwidth available to other processes, it should be configured with
516 \fICPUSchedulingPolicy=idle\fR
517 and/or
518 \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\&.
519 .SH "INTEGRATION WITH SYSTEMD"
520 .SS "Writing systemd Unit Files"
521 .PP
522 When writing systemd unit files, it is recommended to consider the following suggestions:
523 .sp
524 .RS 4
525 .ie n \{\
526 \h'-04' 1.\h'+01'\c
527 .\}
528 .el \{\
529 .sp -1
530 .IP " 1." 4.2
531 .\}
532 If possible, do not use the
533 \fIType=forking\fR
534 setting in service files\&. But if you do, make sure to set the PID file path using
535 \fIPIDFile=\fR\&. See
536 \fBsystemd.service\fR(5)
537 for details\&.
538 .RE
539 .sp
540 .RS 4
541 .ie n \{\
542 \h'-04' 2.\h'+01'\c
543 .\}
544 .el \{\
545 .sp -1
546 .IP " 2." 4.2
547 .\}
548 If your daemon registers a D\-Bus name on the bus, make sure to use
549 \fIType=dbus\fR
550 in the service file if possible\&.
551 .RE
552 .sp
553 .RS 4
554 .ie n \{\
555 \h'-04' 3.\h'+01'\c
556 .\}
557 .el \{\
558 .sp -1
559 .IP " 3." 4.2
560 .\}
561 Make sure to set a good human\-readable description string with
562 \fIDescription=\fR\&.
563 .RE
564 .sp
565 .RS 4
566 .ie n \{\
567 \h'-04' 4.\h'+01'\c
568 .\}
569 .el \{\
570 .sp -1
571 .IP " 4." 4.2
572 .\}
573 Do not disable
574 \fIDefaultDependencies=\fR, unless you really know what you do and your unit is involved in early boot or late system shutdown\&.
575 .RE
576 .sp
577 .RS 4
578 .ie n \{\
579 \h'-04' 5.\h'+01'\c
580 .\}
581 .el \{\
582 .sp -1
583 .IP " 5." 4.2
584 .\}
585 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
586 \fBsystemd.special\fR(7)
587 or names introduced by your own package to keep the unit file operating system\-independent\&.
588 .RE
589 .sp
590 .RS 4
591 .ie n \{\
592 \h'-04' 6.\h'+01'\c
593 .\}
594 .el \{\
595 .sp -1
596 .IP " 6." 4.2
597 .\}
598 Make sure to include an [Install] section including installation information for the unit file\&. See
599 \fBsystemd.unit\fR(5)
600 for details\&. To activate your service on boot, make sure to add a
601 \fIWantedBy=multi\-user\&.target\fR
602 or
603 \fIWantedBy=graphical\&.target\fR
604 directive\&. To activate your socket on boot, make sure to add
605 \fIWantedBy=sockets\&.target\fR\&. Usually, you also want to make sure that when your service is installed, your socket is installed too, hence add
606 \fIAlso=foo\&.socket\fR
607 in your service file
608 foo\&.service, for a hypothetical program
609 foo\&.
610 .RE
611 .SS "Installing systemd Service Files"
612 .PP
613 At the build installation time (e\&.g\&.
614 \fBmake install\fR
615 during package build), packages are recommended to install their systemd unit files in the directory returned by
616 \fBpkg\-config systemd \-\-variable=systemdsystemunitdir\fR
617 (for system services) or
618 \fBpkg\-config systemd \-\-variable=systemduserunitdir\fR
619 (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\&.
620 \fBrpm \-i\fR
621 by the administrator), symlinks should be created in the systemd configuration directories via the
622 \fBenable\fR
623 command of the
624 \fBsystemctl\fR(1)
625 tool to activate them automatically on boot\&.
626 .PP
627 Packages using
628 \fBautoconf\fR(1)
629 are recommended to use a configure script excerpt like the following to determine the unit installation path during source configuration:
630 .sp
631 .if n \{\
632 .RS 4
633 .\}
634 .nf
635 PKG_PROG_PKG_CONFIG()
636 AC_ARG_WITH([systemdsystemunitdir],
637 [AS_HELP_STRING([\-\-with\-systemdsystemunitdir=DIR], [Directory for systemd service files])],,
638 [with_systemdsystemunitdir=auto])
639 AS_IF([test "x$with_systemdsystemunitdir" = "xyes" \-o "x$with_systemdsystemunitdir" = "xauto"], [
640 def_systemdsystemunitdir=$($PKG_CONFIG \-\-variable=systemdsystemunitdir systemd)
641
642 AS_IF([test "x$def_systemdsystemunitdir" = "x"],
643 [AS_IF([test "x$with_systemdsystemunitdir" = "xyes"],
644 [AC_MSG_ERROR([systemd support requested but pkg\-config unable to query systemd package])])
645 with_systemdsystemunitdir=no],
646 [with_systemdsystemunitdir="$def_systemdsystemunitdir"])])
647 AS_IF([test "x$with_systemdsystemunitdir" != "xno"],
648 [AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])])
649 AM_CONDITIONAL([HAVE_SYSTEMD], [test "x$with_systemdsystemunitdir" != "xno"])
650 .fi
651 .if n \{\
652 .RE
653 .\}
654 .PP
655 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\&.)
656 .PP
657 Additionally, to ensure that
658 \fBmake distcheck\fR
659 continues to work, it is recommended to add the following to the top\-level
660 Makefile\&.am
661 file in
662 \fBautomake\fR(1)\-based projects:
663 .sp
664 .if n \{\
665 .RS 4
666 .\}
667 .nf
668 AM_DISTCHECK_CONFIGURE_FLAGS = \e
669 \-\-with\-systemdsystemunitdir=$$dc_install_base/$(systemdsystemunitdir)
670 .fi
671 .if n \{\
672 .RE
673 .\}
674 .PP
675 Finally, unit files should be installed in the system with an automake excerpt like the following:
676 .sp
677 .if n \{\
678 .RS 4
679 .\}
680 .nf
681 if HAVE_SYSTEMD
682 systemdsystemunit_DATA = \e
683 foobar\&.socket \e
684 foobar\&.service
685 endif
686 .fi
687 .if n \{\
688 .RE
689 .\}
690 .PP
691 In the
692 \fBrpm\fR(8)
693 \&.spec
694 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\&.
695 .PP
696 At the top of the file:
697 .sp
698 .if n \{\
699 .RS 4
700 .\}
701 .nf
702 BuildRequires: systemd
703 %{?systemd_requires}
704 .fi
705 .if n \{\
706 .RE
707 .\}
708 .PP
709 And as scriptlets, further down:
710 .sp
711 .if n \{\
712 .RS 4
713 .\}
714 .nf
715 %post
716 %systemd_post foobar\&.service foobar\&.socket
717
718 %preun
719 %systemd_preun foobar\&.service foobar\&.socket
720
721 %postun
722 %systemd_postun
723 .fi
724 .if n \{\
725 .RE
726 .\}
727 .PP
728 If the service shall be restarted during upgrades, replace the
729 "%postun"
730 scriptlet above with the following:
731 .sp
732 .if n \{\
733 .RS 4
734 .\}
735 .nf
736 %postun
737 %systemd_postun_with_restart foobar\&.service
738 .fi
739 .if n \{\
740 .RE
741 .\}
742 .PP
743 Note that
744 "%systemd_post"
745 and
746 "%systemd_preun"
747 expect the names of all units that are installed/removed as arguments, separated by spaces\&.
748 "%systemd_postun"
749 expects no arguments\&.
750 "%systemd_postun_with_restart"
751 expects the units to restart as arguments\&.
752 .PP
753 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:
754 .sp
755 .if n \{\
756 .RS 4
757 .\}
758 .nf
759 %triggerun \-\- foobar < 0\&.47\&.11\-1
760 if /sbin/chkconfig \-\-level 5 foobar ; then
761 /bin/systemctl \-\-no\-reload enable foobar\&.service foobar\&.socket >/dev/null 2>&1 || :
762 fi
763 .fi
764 .if n \{\
765 .RE
766 .\}
767 .PP
768 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
769 \fBchkconfig\fR
770 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\&.
771 .SH "PORTING EXISTING DAEMONS"
772 .PP
773 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\&.
774 .PP
775 To port an existing SysV compatible daemon, the following steps are recommended:
776 .sp
777 .RS 4
778 .ie n \{\
779 \h'-04' 1.\h'+01'\c
780 .\}
781 .el \{\
782 .sp -1
783 .IP " 1." 4.2
784 .\}
785 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\&.
786 .RE
787 .sp
788 .RS 4
789 .ie n \{\
790 \h'-04' 2.\h'+01'\c
791 .\}
792 .el \{\
793 .sp -1
794 .IP " 2." 4.2
795 .\}
796 If the daemon offers interfaces to other software running on the local system via local
797 \fBAF_UNIX\fR
798 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
799 \fBsd_listen_fds\fR(3)
800 is checked for already passed sockets first\&. If sockets are passed (i\&.e\&. when
801 \fBsd_listen_fds()\fR
802 returns a positive value), skip the socket creation step and use the passed sockets\&. Secondly, ensure that the file system socket nodes for local
803 \fBAF_UNIX\fR
804 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 service manager 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\&.
805 .RE
806 .sp
807 .RS 4
808 .ie n \{\
809 \h'-04' 3.\h'+01'\c
810 .\}
811 .el \{\
812 .sp -1
813 .IP " 3." 4.2
814 .\}
815 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\&.
816 .RE
817 .sp
818 .RS 4
819 .ie n \{\
820 \h'-04' 4.\h'+01'\c
821 .\}
822 .el \{\
823 .sp -1
824 .IP " 4." 4.2
825 .\}
826 If the daemon exposes interfaces via D\-Bus, write and install a D\-Bus activation file for the service, see above for details\&.
827 .RE
828 .SH "PLACING DAEMON DATA"
829 .PP
830 It is recommended to follow the general guidelines for placing package files, as discussed in
831 \fBfile-hierarchy\fR(7)\&.
832 .SH "NOTES"
833 .PP
834 All example codes in this page are licensed under
835 "MIT No Attribution"
836 (SPDX\-License\-Identifier: MIT\-0)\&.
837 .SH "SEE ALSO"
838 .PP
839 \fBsystemd\fR(1), \fBsd-daemon\fR(3), \fBsd_listen_fds\fR(3), \fBsd_notify\fR(3), \fBdaemon\fR(3), \fBsystemd.service\fR(5), \fBfile-hierarchy\fR(7)
840 .SH "NOTES"
841 .IP " 1." 4
842 LSB recommendations for SysV init scripts
843 .RS 4
844 \%http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
845 .RE
846 .IP " 2." 4
847 Apple MacOS X Daemon Requirements
848 .RS 4
849 \%https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html
850 .RE