{"id":3995,"date":"2022-12-20T17:28:39","date_gmt":"2022-12-20T20:28:39","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/daemon-man7\/"},"modified":"2022-12-20T17:28:39","modified_gmt":"2022-12-20T20:28:39","slug":"daemon-man7","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/daemon-man7\/","title":{"rendered":"DAEMON (man7)"},"content":{"rendered":"<h1 align=\"center\">DAEMON<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#ACTIVATION\">ACTIVATION<\/a><br \/> <a href=\"#INTEGRATION WITH SYSTEMD\">INTEGRATION WITH SYSTEMD<\/a><br \/> <a href=\"#PORTING EXISTING DAEMONS\">PORTING EXISTING DAEMONS<\/a><br \/> <a href=\"#PLACING DAEMON DATA\">PLACING DAEMON DATA<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">daemon \u2212 Writing and packaging system daemons<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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 &#8220;new\u2212style&#8221; daemons), as implemented by <b>systemd<\/b>(1). This manual page covers both schemes, and in particular includes recommendations for daemons that shall be included in the systemd init system.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>SysV Daemons<\/b> <br \/> 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\u2212style daemons (see below), and should only be implemented if compatibility with SysV is essential.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">1. 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 \/proc\/self\/fd, with a fallback of iterating from file descriptor 3 to the value returned by <b>getrlimit()<\/b> for <b>RLIMIT_NOFILE<\/b>.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">2. Reset all signal handlers to their default. This is best done by iterating through the available signals up to the limit of <b>_NSIG<\/b> and resetting them to <b>SIG_DFL<\/b>.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">3. Reset the signal mask using <b>sigprocmask()<\/b>.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">4. Sanitize the environment block, removing or resetting environment variables that might negatively impact daemon runtime.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">5. Call <b>fork()<\/b>, to create a background process.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">6. In the child, call <b>setsid()<\/b> to detach from any terminal and create an independent session.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">7. In the child, call <b>fork()<\/b> again, to ensure that the daemon can never re\u2212acquire a terminal again. (This relevant if the program \u2014 and all its dependencies \u2014 does not carefully specify \u2018O_NOCTTY\u2018 on each and every single \u2018open()\u2018 call that might potentially open a TTY device node.)<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">8. Call <b>exit()<\/b> in the first child, so that only the second child (the actual daemon process) stays around. This ensures that the daemon process is re\u2212parented to init\/PID 1, as all daemons should be.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">9. In the daemon process, connect \/dev\/null to standard input, output, and error.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">10. In the daemon process, reset the umask to 0, so that the file modes passed to <b>open()<\/b>, <b>mkdir()<\/b> and suchlike directly control the access mode of the created files and directories.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">11. 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.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">12. In the daemon process, write the daemon PID (as returned by <b>getpid()<\/b>) to a PID file, for example \/run\/foobar.pid (for a hypothetical daemon &#8220;foobar&#8221;) to ensure that the daemon cannot be started more than once. This must be implemented in race\u2212free 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.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">13. In the daemon process, drop privileges, if possible and applicable.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">14. 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 <b>fork()<\/b> and hence available in both the original and the daemon process.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">15. Call <b>exit()<\/b> in the original process. The process that invoked the daemon must be able to rely on that this <b>exit()<\/b> happens after initialization is complete and all external communication channels are established and accessible.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The BSD <b>daemon()<\/b> function should not be used, as it implements only a subset of these steps.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>New\u2212Style Daemons<\/b> <br \/> Modern services for Linux should be implemented as new\u2212style daemons. This makes it easier to supervise and control them at runtime and simplifies their implementation.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For developing a new\u2212style daemon, none of the initialization steps recommended for SysV daemons need to be implemented. New\u2212style 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\u2212style service.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that new\u2212style 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\u2212over file descriptors are passed. Daemons will be executed in their own session, with standard input connected to \/dev\/null and standard output\/error connected to the <b>systemd-journald.service<\/b>(8) logging service, unless otherwise configured. The umask is reset.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">It is recommended for new\u2212style daemons to implement the following:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">1. If <b>SIGTERM<\/b> is received, shut down the daemon and exit cleanly.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">2. If <b>SIGHUP<\/b> is received, reload the configuration files, if this applies.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">3. 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 <b><font color=\"#0000FF\">LSB recommendations for SysV init scripts<\/font><\/b> <small><font color=\"#000000\">[1]<\/font><\/small> <font color=\"#000000\">.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">4. If possible and applicable, expose the daemon&#8217;s control interface via the D\u2212Bus IPC system and grab a bus name as last step of initialization.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">5. For integration in systemd, provide a .service unit file that carries information about starting, stopping and otherwise maintaining the daemon. See <b>systemd.service<\/b>(5) for details.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">6. As much as possible, rely on the init system&#8217;s functionality to limit the access of the daemon to files, services and other resources, i.e. in the case of systemd, rely on systemd&#8217;s resource limit control instead of implementing your own, rely on systemd&#8217;s privilege dropping code instead of implementing it in the daemon, and similar. See <b>systemd.exec<\/b>(5) for the available controls.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">7. If D\u2212Bus is used, make your daemon bus\u2212activatable by supplying a D\u2212Bus service activation configuration file. This has multiple advantages: your daemon may be started lazily on\u2212demand; it may be started in parallel to other daemons requiring it \u2014 which maximizes parallelization and boot\u2212up 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.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">8. If your daemon provides services to other local processes or remote clients via a socket, it should be made socket\u2212activatable following the scheme pointed out below. Like D\u2212Bus activation, this enables on\u2212demand starting of services as well as it allows improved parallelization of service start\u2212up. Also, for state\u2212less protocols (such as syslog, DNS), a daemon implementing socket\u2212based activation can be restarted without losing a single request. See below for details.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">9. If applicable, a daemon should notify the init system about startup completion or status updates via the <b>sd_notify<\/b>(3) interface.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">10. Instead of using the <b>syslog()<\/b> call to log directly to the system syslog service, a new\u2212style daemon may choose to simply log to standard error via <b>fprintf()<\/b>, which is then forwarded to syslog by the init system. If log levels are necessary, these can be encoded by prefixing individual log lines with strings like &#8220;<4>&#8221; (for log level 4 &#8220;WARNING&#8221; in the syslog priority scheme), following a similar style as the Linux kernel&#8217;s <b>printk()<\/b> level system. For details, see <b>sd-daemon<\/b>(3) and <b>systemd.exec<\/b>(5).<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">11. As new\u2212style daemons are invoked without a controlling TTY (but as their own session leaders) care should be taken to always specify \u2018O_NOCTTY\u2018 on \u2018open()\u2018 calls that possibly reference a TTY device node, so that no controlling TTY is accidentally acquired.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">These recommendations are similar but not identical to the<\/font> <b><font color=\"#0000FF\">Apple MacOS X Daemon Requirements<\/font><\/b> <small><font color=\"#000000\">[2]<\/font><\/small> <font color=\"#000000\">.<\/font><\/p>\n<h2>ACTIVATION <a name=\"ACTIVATION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">New\u2212style init systems provide multiple additional mechanisms to activate services, as detailed below. It is common that services are configured to be activated via more than one mechanism at the same time. An example for systemd: bluetoothd.service might get activated either when Bluetooth hardware is plugged in, or when an application accesses its programming interfaces via D\u2212Bus. 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\u2212Bus 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\u2212up), 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\u2212Bus) or the kernel (in case of sockets) until the activation is completed.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\"><b>Activation on Boot<\/b> <br \/> Old\u2212style daemons are usually activated exclusively on boot (and manually by the administrator) via SysV init scripts, as detailed in the<\/font> <b><font color=\"#0000FF\">LSB Linux Standard Base Core Specification<\/font><\/b> <small><font color=\"#000000\">[1]<\/font><\/small> <font color=\"#000000\">. This method of activation is supported ubiquitously on Linux init systems, both old\u2212style and new\u2212style systems. Among other issues, SysV init scripts have the disadvantage of involving shell scripts in the boot process. New\u2212style init systems generally employ updated versions of activation, both during boot\u2212up and during runtime and using more minimal service description files.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">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 .wants\/ directory of either multi\u2212user.target or graphical.target, which are normally used as boot targets at system startup. See <b>systemd.unit<\/b>(5) for details about the .wants\/ directories, and <b>systemd.special<\/b>(7) for details about the two boot targets.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\"><b>Socket\u2212Based Activation<\/b> <br \/> In order to maximize the possible parallelization and robustness and simplify configuration and development, it is recommended for all new\u2212style daemons that communicate via listening sockets to employ socket\u2212based activation. In a socket\u2212based 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\u2212daemon 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\u2212demand 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\u2212less 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.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">New\u2212style 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 <b>sd_listen_fds<\/b>(3) and <b>sd-daemon<\/b>(3). For details about porting existing daemons to socket\u2212based activation, see below. With minimal effort, it is possible to implement socket\u2212based activation in addition to traditional internal socket creation in the same codebase in order to support both new\u2212style and old\u2212style init systems from the same daemon binary.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">systemd implements socket\u2212based activation via .socket units, which are described in <b>systemd.socket<\/b>(5). When configuring socket units for socket\u2212based activation, it is essential that all listening sockets are pulled in by the special target unit sockets.target. It is recommended to place a <i>WantedBy=sockets.target<\/i> directive in the [Install] section to automatically add such a dependency on installation of a socket unit. Unless <i>DefaultDependencies=no<\/i> is set, the necessary ordering dependencies are implicitly created for all socket units. For more information about sockets.target, see <b>systemd.special<\/b>(7). It is not necessary or recommended to place any additional dependencies on socket units (for example from multi\u2212user.target or suchlike) when one is installed in sockets.target.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\"><b>Bus\u2212Based Activation<\/b> <br \/> When the D\u2212Bus IPC system is used for communication with clients, new\u2212style daemons should employ bus activation so that they are automatically activated when a client application accesses their IPC interfaces. This is configured in D\u2212Bus service files (not to be confused with systemd service unit files!). To ensure that D\u2212Bus uses systemd to start\u2212up and maintain the daemon, use the <i>SystemdService=<\/i> directive in these service files to configure the matching systemd service for a D\u2212Bus service. e.g.: For a D\u2212Bus service whose D\u2212Bus activation file is named org.freedesktop.RealtimeKit.service, make sure to set <i>SystemdService=rtkit\u2212daemon.service<\/i> in that file to bind it to the systemd service rtkit\u2212daemon.service. This is needed to make sure that the daemon is started in a race\u2212free fashion when activated via multiple mechanisms simultaneously.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\"><b>Device\u2212Based Activation<\/b> <br \/> 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\u2212style 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 &#8220;systemd&#8221;. Like any other kind of unit, they may then pull in other units when activated (i.e. plugged in) and thus implement device\u2212based activation. systemd dependencies may be encoded in the udev database via the <i>SYSTEMD_WANTS=<\/i> property. See <b>systemd.device<\/b>(5) for details. Often, it is nicer to pull in services from devices only indirectly via dedicated targets. Example: Instead of pulling in bluetoothd.service from all the various bluetooth dongles and other hardware available, pull in bluetooth.target from them and bluetoothd.service from that target. This provides for nicer abstraction and gives administrators the option to enable bluetoothd.service via controlling a bluetooth.target.wants\/ symlink uniformly with a command like <b>enable<\/b> of <b>systemctl<\/b>(1) instead of manipulating the udev ruleset.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\"><b>Path\u2212Based Activation<\/b> <br \/> 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\u2212empty. New\u2212style init systems provide a way to bind service activation to file system changes. systemd implements this scheme via path\u2212based activation configured in .path units, as outlined in <b>systemd.path<\/b>(5).<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\"><b>Timer\u2212Based Activation<\/b> <br \/> Some daemons that implement clean\u2212up jobs that are intended to be executed in regular intervals benefit from timer\u2212based activation. In systemd, this is implemented via .timer units, as described in <b>systemd.timer<\/b>(5).<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\"><b>Other Forms of Activation<\/b> <br \/> 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 .socket units when a specific IP address is configured on a network interface, because network sockets shall be bound to the address. However, an alternative to implement this is by utilizing the Linux <b>IP_FREEBIND<\/b> socket option, as accessible via <i>FreeBind=yes<\/i> in systemd socket files (see <b>systemd.socket<\/b>(5) for details). This option, when enabled, allows sockets to be bound to a non\u2212local, 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\u2212grained access to the CPU and I\/O schedulers. If a process executed by the init system shall not negatively impact the amount of CPU or I\/O bandwidth available to other processes, it should be configured with <i>CPUSchedulingPolicy=idle<\/i> and\/or <i>IOSchedulingClass=idle<\/i>. Optionally, this may be combined with timer\u2212based activation to schedule background jobs during runtime and with minimal impact on the system, and remove it from the boot phase itself.<\/font><\/p>\n<h2>INTEGRATION WITH SYSTEMD <a name=\"INTEGRATION WITH SYSTEMD\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\"><b>Writing systemd Unit Files<\/b> <br \/> When writing systemd unit files, it is recommended to consider the following suggestions:<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">1. If possible, do not use the <i>Type=forking<\/i> setting in service files. But if you do, make sure to set the PID file path using <i>PIDFile=<\/i>. See <b>systemd.service<\/b>(5) for details.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">2. If your daemon registers a D\u2212Bus name on the bus, make sure to use <i>Type=dbus<\/i> in the service file if possible.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">3. Make sure to set a good human\u2212readable description string with <i>Description=<\/i>.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">4. Do not disable <i>DefaultDependencies=<\/i>, unless you really know what you do and your unit is involved in early boot or late system shutdown.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">5. 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 <b>systemd.special<\/b>(7) or names introduced by your own package to keep the unit file operating system\u2212independent.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">6. Make sure to include an [Install] section including installation information for the unit file. See <b>systemd.unit<\/b>(5) for details. To activate your service on boot, make sure to add a <i>WantedBy=multi\u2212user.target<\/i> or <i>WantedBy=graphical.target<\/i> directive. To activate your socket on boot, make sure to add <i>WantedBy=sockets.target<\/i>. Usually, you also want to make sure that when your service is installed, your socket is installed too, hence add <i>Also=foo.socket<\/i> in your service file foo.service, for a hypothetical program foo.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\"><b>Installing systemd Service Files<\/b> <br \/> At the build installation time (e.g. <b>make install<\/b> during package build), packages are recommended to install their systemd unit files in the directory returned by <b>pkg\u2212config systemd \u2212\u2212variable=systemdsystemunitdir<\/b> (for system services) or <b>pkg\u2212config systemd \u2212\u2212variable=systemduserunitdir<\/b> (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. <b>rpm \u2212i<\/b> by the administrator), symlinks should be created in the systemd configuration directories via the <b>enable<\/b> command of the <b>systemctl<\/b>(1) tool to activate them automatically on boot.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">Packages using <b>autoconf<\/b>(1) are recommended to use a configure script excerpt like the following to determine the unit installation path during source configuration:<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">PKG_PROG_PKG_CONFIG <br \/> AC_ARG_WITH([systemdsystemunitdir], <br \/> [AS_HELP_STRING([\u2212\u2212with\u2212systemdsystemunitdir=DIR], [Directory for systemd service files])],, <br \/> [with_systemdsystemunitdir=auto]) <br \/> AS_IF([test &#8220;x$with_systemdsystemunitdir&#8221; = &#8220;xyes&#8221; \u2212o &#8220;x$with_systemdsystemunitdir&#8221; = &#8220;xauto&#8221;], [ <br \/> def_systemdsystemunitdir=$($PKG_CONFIG \u2212\u2212variable=systemdsystemunitdir systemd)<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">AS_IF([test &#8220;x$def_systemdsystemunitdir&#8221; = &#8220;x&#8221;], <br \/> [AS_IF([test &#8220;x$with_systemdsystemunitdir&#8221; = &#8220;xyes&#8221;], <br \/> [AC_MSG_ERROR([systemd support requested but pkg\u2212config unable to query systemd package])]) <br \/> with_systemdsystemunitdir=no], <br \/> [with_systemdsystemunitdir=&#8221;$def_systemdsystemunitdir&#8221;])]) <br \/> AS_IF([test &#8220;x$with_systemdsystemunitdir&#8221; != &#8220;xno&#8221;], <br \/> [AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])]) <br \/> AM_CONDITIONAL([HAVE_SYSTEMD], [test &#8220;x$with_systemdsystemunitdir&#8221; != &#8220;xno&#8221;])<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">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.)<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">Additionally, to ensure that <b>make distcheck<\/b> continues to work, it is recommended to add the following to the top\u2212level Makefile.am file in <b>automake<\/b>(1)\u2212based projects:<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">AM_DISTCHECK_CONFIGURE_FLAGS =  <br \/> \u2212\u2212with\u2212systemdsystemunitdir=$$dc_install_base\/$(systemdsystemunitdir)<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">Finally, unit files should be installed in the system with an automake excerpt like the following:<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">if HAVE_SYSTEMD <br \/> systemdsystemunit_DATA =  <br \/> foobar.socket  <br \/> foobar.service <br \/> endif<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">In the <b>rpm<\/b>(8) .spec file, use snippets like the following to enable\/disable the service during installation\/deinstallation. This makes use of the RPM macros shipped along systemd. Consult the packaging guidelines of your distribution for details and the equivalent for other package managers.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">At the top of the file:<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">BuildRequires: systemd <br \/> %{?systemd_requires}<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">And as scriptlets, further down:<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">%post <br \/> %systemd_post foobar.service foobar.socket<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">%preun <br \/> %systemd_preun foobar.service foobar.socket<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">%postun <br \/> %systemd_postun<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">If the service shall be restarted during upgrades, replace the &#8220;%postun&#8221; scriptlet above with the following:<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">%postun <br \/> %systemd_postun_with_restart foobar.service<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">Note that &#8220;%systemd_post&#8221; and &#8220;%systemd_preun&#8221; expect the names of all units that are installed\/removed as arguments, separated by spaces. &#8220;%systemd_postun&#8221; expects no arguments. &#8220;%systemd_postun_with_restart&#8221; expects the units to restart as arguments.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">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:<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">%triggerun \u2212\u2212 foobar < 0.47.11\u22121 <br \/> if \/sbin\/chkconfig \u2212\u2212level 5 foobar ; then <br \/> \/bin\/systemctl \u2212\u2212no\u2212reload enable foobar.service foobar.socket >\/dev\/null 2>&#038;1 || : <br \/> fi<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">Where 0.47.11\u22121 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 <b>chkconfig<\/b> 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.<\/font><\/p>\n<h2>PORTING EXISTING DAEMONS <a name=\"PORTING EXISTING DAEMONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">Since new\u2212style 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\u2212style init systems.<\/font><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">To port an existing SysV compatible daemon, the following steps are recommended:<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">1. 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\u2212style init systems, but also to ease debugging.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">2. If the daemon offers interfaces to other software running on the local system via local <b>AF_UNIX<\/b> sockets, consider implementing socket\u2212based activation (see above). Usually, a minimal patch is sufficient to implement this: Extend the socket creation in the daemon code so that <b>sd_listen_fds<\/b>(3) is checked for already passed sockets first. If sockets are passed (i.e. when <b>sd_listen_fds()<\/b> returns a positive value), skip the socket creation step and use the passed sockets. Secondly, ensure that the file system socket nodes for local <b>AF_UNIX<\/b> sockets used in the socket\u2212based 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\u2212style init systems guarantee that no left\u2212over 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.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">3. Write and install a systemd unit file for the service (and the sockets if socket\u2212based activation is used, as well as a path unit file, if the daemon processes a spool directory), see above for details.<\/font><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><font color=\"#000000\">4. If the daemon exposes interfaces via D\u2212Bus, write and install a D\u2212Bus activation file for the service, see above for details.<\/font><\/p>\n<h2>PLACING DAEMON DATA <a name=\"PLACING DAEMON DATA\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\">It is recommended to follow the general guidelines for placing package files, as discussed in <b>file-hierarchy<\/b>(7).<\/font><\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><font color=\"#000000\"><b>systemd<\/b>(1), <b>sd-daemon<\/b>(3), <b>sd_listen_fds<\/b>(3), <b>sd_notify<\/b>(3), <b>daemon<\/b>(3), <b>systemd.service<\/b>(5), <b>file-hierarchy<\/b>(7)<\/font><\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"12%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\"><font color=\"#000000\">1.<\/font><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"63%\">\n<p style=\"margin-top: 1em\"><font color=\"#000000\">LSB recommendations for SysV init scripts<\/font><\/p>\n<\/td>\n<td width=\"20%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%;\"><font color=\"#000000\">http:\/\/refspecs.linuxbase.org\/LSB_3.1.1\/LSB-Core-generic\/LSB-Core-generic\/iniscrptact.html<\/font><\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"12%\"><\/td>\n<td width=\"3%\">\n<p style=\"margin-top: 1em\"><font color=\"#000000\">2.<\/font><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"51%\">\n<p style=\"margin-top: 1em\"><font color=\"#000000\">Apple MacOS X Daemon Requirements<\/font><\/p>\n<\/td>\n<td width=\"32%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%;\"><font color=\"#000000\">https:\/\/developer.apple.com\/library\/mac\/documentation\/MacOSX\/Conceptual\/BPSystemStartup\/Chapters\/CreatingLaunchdJobs.html<\/font><\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  daemon \u2212 Writing and packaging system daemons <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[971],"tags":[973,1083,972],"class_list":["post-3995","post","type-post","status-publish","format-standard","hentry","category-7-miscelanea","tag-973","tag-daemon","tag-man7"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/3995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=3995"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/3995\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=3995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=3995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=3995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}