sysconftool

package: sysconftool

title: sysconftool(7)

text:
# sysconftool(7)

## NAME
sysconftool - format of configuration files installed by sysconftool

## SYNOPSIS

```
   #

   ##VERSION: $Id$

   ##NAME: configname1:configname1version

   #

   # Description

   SETTING1=VALUE1

   ##NAME: configname2:configname2version

   #

   # Description

   SETTING2=VALUE2

   ...
```

## DESCRIPTION
This manual page describes the format of configuration files installed by sysconftool(1)[1]. This format is flexible enough to accomodate any kind of application configuration file format. sysconftool makes four assumptions about the configuration file:

1.It is a plain text file.

2.Lines that begin with a single '#' character are comments that contain a description of the following configuration setting.

3.Lines that do not begin with the '#' character contain the configuration setting described by the previous comment lines.

4.Configuration settings are self contained, and completely independent, changing one configuration setting never requires that another, different, configuration setting must be changed too (perhaps any logical conflicts are automatically resolved by the application in a safe, fallback, manner)

The additional information used by sysconftool is encoded as specially-formatted comment lines that begin with two '#' characters.

## FILENAMES
An application installs a default configuration file as "filename.dist", when the actual name of the configuration file is really "filename". If there is no existing filename, sysconftool simply copies filename.dist to filename, and calls it a day. Otherwise, sysconftool copies the existing filename to filename.bak and creates a new filename based on the contents of both files.

sysconftool is designed to solve a common problem with application configuration. New versions of applications often include additional functionality that requires new configuration settings. Without the new configuration settings the application will not work, so new configuration files should be installed during the upgrade. However, when that happens, any changes to the existing configuration settings are lost. sysconftool is designed to solve this dillemma, and merge old configuration settings with new ones. sysconftool is designed in a fail-safe way. Whenever there's a doubt as to what's The Right Thing To Do, sysconftool will use the configuration settings from the new file, that are supposedly known to be good, and leave it up to a physical being to sort out any conflicts and make any manual decisions.

## FILE VERSIONING
The following line should appear at the beginning of filename.dist:

```
   ##VERSION: version
```

This doesn't have to be the very first line in filename.dist, but it should appear somewhere within the first twenty lines, right before the first configuration setting. "version" should be some kind of an identifier for this particular version of the configuration files. All that sysconftool cares about is that any change to the default configuration, in filename.dist, results in a different version. An excellent way to do this is to simply use the $Id$ RCS/CVS version identification strings, and have this little detail taken care of automatically.

New revisions of an application should not necessarily have a new configuration file version. If the default application configuration settings have not changed from the previous release, version can remain the same. version is copied from filename.dist to filename.

If there's an existing filename, and it includes the same version identifier, sysconftool silently skips over this configuration file, and doesn't do anything, assuming that this configuration file has already been installed. Therefore, running sysconftool more than once (accidentally) will not break anything.

If there's an existing filename, but it's version is different, sysconftool backs it up to filename.bak, then creates a new filename. If there's an existing filename, but it doesn't contain a recognizable "##VERSION: version" line, sysconftool assumes that the previous version of the application did not use the sysconftool tool. That's not a problem. filename is copied to filename.bak, and filename.dist gets installed as the new filename, allowing sysconftool to work with the next version of this configuration file.

## CONFIGURATION SETTING VERSIONING
Each configuration setting uses the following format in the configuration file:

```
   ##NAME: name:revision

   #

   # description

   setting
```

sysconftool looks for a line that begins with "##NAME". This line gives the name and the revision of the following setting. name must be unique within its configuration file (the same name can be used by different configuration files, sysconftool works with one file at a time). revision is used by sysconftool to decide when the configuration setting can be safely carried over from an older configuration file, and when it is better to reinstall the default setting from the new configuration file.

One or more comment lines - lines that begin with the '#' character - may follow "##NAME". The first line that does not begin with '#' is considered to be the first line that contains the value of the configuration setting, which lasts. The value can be spread over multiple lines. The configuration setting is considered to last until either the end of the file, or until the first following line that begins with another "##NAME".

Aside from that, sysconftool does not care how the configuration setting is represented. It can be "NAME=VALUE", it can be "NAME: VALUE", or "NAME<tab>VALUE", it can even be a base64-encoded binary object, and it can always have leading or trailing blank lines. sysconftool merely looks at which lines begin with the '#' comment character. After the '##NAME:' line, sysconftool looks ahead until the first line that does not begin with '#', and that's the first line of the configuration setting. Then, sysconftool looks ahead until the next line that starts with a "##NAME:", which marks the end of this configuration setting.

For this reason it is important that all commented description lines that follow '##NAME:' must begin with the '#' character. If a blank line follows the line with '##NAME:' it is assumed to be the start of the corresponding configuration setting. For example, this is correct:

```
   ##NAME: flag1:0

   #

   #

   # This is the first configuration flag

   #

   flag1=1
```

This is not correct:

```
   ##NAME: flag1:0

   #

   # This is the first configuration flag

   #

   flag1=1
```

## CONFIGURATION FILE CREATION
A new configuration file, "filename", is created from its previous version, "filename.bak" and the new default configuration file, "filename.dist", using the following, simple, two-step process.

1.sysconftool begins with filename.dist in hand. This makes sure that sysconftool begins with a good, known, default configuration file.

2.sysconftool then takes each configuration setting in filename.dist, then searches filename.bak. If it finds a configuration setting that has an identical "name" and "version", then the corresponding configuration setting value is taken from filename.bak, replacing the default in filename.dist. After all configuration settings in filename.dist are looked up (and potentially replaced), what's left becomes the new filename.

## THE END RESULT
The above process is a logical description. The actual technical implementation is slightly different (for example, filename is not backed up to filename.bak until the new configuration file has been already created), but is logically equivalent to this process. This process carries a number of consequences that must be considered.

If a new application revision needs a new configuration setting, it will get a new name and version. Since filename.dist is used as a starting point for the new configuration file, the new configuration file will include the new configuration setting. When a configuration setting is removed, it will disappear from the new configuration file for the same exact reason.

## CONFIGURATION SETTING VERSION
sysconftool looks at both name and version. A configuration setting with the same name but different versions are seen by sysconftool as completely different settings. The existence of version allows a finer-grained control of configuration upgrades, as described below.

sysconftool copies setting values with the same name and version from the old configuration file to the new configuration file. However, the associated descriptive comments are not copied, and are taken from the new filename.dist. Therefore, if a new version of the configuration file contains an updated or an embellished description of a particular setting, it will be included in the new configuration file, but the existing configuration value will be preserved! Generally, if a configuration setting does not change its meaning or function, its name and version should remain the same. Its comments can be edited to fix a typo, or revised in a more substantive fashion. Name and version should only be changed if there's a functional change in the configuration setting.

What to do with name and version after a functional change depends on the nature and the magnitude of the change. The nature and the magnitude of the change must be considered not only with respect to the most recent revision of the application, but to all the previous revisions as well. When in doubt, go based upon the largest change in magnitude, in order to guarantee a functional default setting, from filename.dist, and leave it up to a living being to manually figure it out.

If only the default value of a setting should be changed for new application installation, but the existing installations can continue to use the existing value of the setting, both the name and version should be left alone. Existing configuration settings will be preserved, and new installations will get the new default. The descriptive comment of this setting can be updated too (see above).

This should be done only as long as ALL previous values of this configuration setting will ALWAYS be valid in the new application revision. If some possible values of this configuration setting will no longer be valid, version should be changed. sysconftool does not care how name and version are formatted. Both are opaque labels. The only requirements is for the label to be different. The difference between changing version and changing both name and version is this:

If there's an old configuration setting with the same name but different version, sysconftool will still use the new, safe, default value from filename.dist, however sysconftool will also append an additional comment, on its own accord, reminding the reader that this configuration value has been reset, and the reader should consider whether to manually restore the configuration value from the old configuration.

## MISCELLANEA
When sysconftool decides to keep an existing setting, with the same name and value, it will also insert a short comment to that effect, reminding the reader to check the default in filename.dist.

## SEE ALSO
sysconftool(1)[1].

## AUTHORS
Double Precision, Inc.

## NOTES
1.

sysconftool(1)

[set $man.base.url.for.relative.links]/sysconftool.1.html


secure-logging

package: syslog-ng

title: secure-logging(7)

text:
# secure-logging(7)

## NAME
secure-logging - Forward integrity and confidentiality for system logs

## SYNOPSIS
$(slog --key-file <host key file> --mac-file <MAC file> $RAWMSG)

## DESCRIPTION
Secure logging is an extension to syslog-ng providing forward integrity and confidentiality of system logs. It is implemented in form of a module and is configured as a template in the syslog-ng configuration file.

The main objective of the secure logging module is to provide tamper evident logging, i.e. to adequately protect log records of an information system and to provide a sensor indicating attack attempts. The secure logging module achieves this by authentically encrypting each log record with an individual cryptographic key used only once and protecting the integrity of the whole log archive by a cryptographic authentication code. Each attempt to tamper with either an individual log record or the log archive itself will be immediately detected during log archive verification. Therefore, an attacker can no longer tamper with log records without being detected.

In order to use the log file created by the secure logging module for analysis, the log file must first be decrypted and its integrity verified. This is achieved with a command line utility that is part of the secure logging module and is installed as part of the syslog-ng package. This utility can easily be integrated into the import module of existing analysis environments.

The secure logging environment uses a cryptographic key for encrypting log entries. Each individual log entry is encrypted with its own key which is immediately discarded after successful encryption in order to provide forward integrity. An efficient algorithm generates the key for the next log entry based the key used for encrypting the previous log entry. The resulting chain of keys preserves forward integrity, i.e. a potential attacker cannot deduce the previous key from the current key.

In order to ease log verification and analysis, a sequence number is added to each log entry. This sequence number is also added to the encryption key creating a one-to-one correspondence of the encryption key with the log entry. In order to prevent truncation attacks or deletion of individual log entries, a message authentication code (MAC) is iteratively applied to the complete log archive and stored in a separate file. It is used upon verification to check the integrity of the whole log archive.

Here is an example of three short original log messages that will be sent to a destination with secure logging enabled:

```
This is a log message
And here comes another log message
This is a log message with a longer text that is processed without any problems

```

In order to inspect the status of the secure logging environment, one can check the sequence counter by querying the key file with the slogkey utility like this:

```
user@host:~> slogkey --counter /etc/syslog-ng/host.key
counter=3

```

Counting starts at zero. This is why the counter is set to three after processing three messages.

The output of the secure logging template for the three example messages is shown below. One clearly sees the sequence number that was prepended to each message. The colon indicates the end of the sequence number and the start of the original message. As three message were processed, the sequence counter of the key will be three.

```
AAAAAAAAAAA=:k3/dYpLsgO2tUJKSauo6dycIBzW6OTC3pyA9TP+7AnqFgEojBzgC2rcK4OPfRtr8yg==
AQAAAAAAAAA=:smw0ATISVgN+BYEu5d7OLBE7aQhHpK9Ro4MndmNgSVrqhcmRCBCj6DUnD6ku0Z29CKJ0N6LAJUgByX4Ev+g=
AgAAAAAAAAA=:5UVybnKL1EAbgC4CLfd8HpgurjREf4LEN61/yWHSD2hbXjRD4QmQdtbwguT1chzdItKSQASps9QRIvR5Jd4AHzHfqxI4aRgdUBcNbAq26nwUCg5vPWygjmbtQaxZgCJYkry8slxCigmbTVs=

```

The output of a successful verification run is shown below.

```
0000000000000000: This is a log message
0000000000000001: And here comes another log message
0000000000000002: This is a log message with a longer text that is processed without any problems

```

The original log messages have been successfully restored. Additionally, the sequence counter is also prepended to the clear text messages. This helps when in analyzing problems with a particular log entry. As real log files will contain thousands of entries. the sequence counter eases identification of erroneous entries.

Before the secure logging module can be used as part of an existing syslog-ng installation, several preparatory activities need to be performed.

## KEY GENERATION
In order to bootstrap the system, an initial key k0 must be created and installed on the log host before secure logging environment is started for the first time.

The newly created host key k0 has its counter set to 0 indicating that it represents the initial host key k0. This host key k0 must be kept secret and not be disclosed to third parties. It will be required to successfully decrypt and verify log archives processed by the secure logging environment. As each log entry will be encrypted with its own key, a new host key will be created after successful processing of a log entry and will replace the previous key. Therefore, the initial host key needs to be stored in a safe place before starting the secure logging environment, as it will be deleted from the log host after processing of the first log entry. The following steps must be done before starting the secure logging environment. Steps 1 and 2 are performed with the slogkey utility. See slogkey(1) for details on how to generate a master key and to derive a host key from it. Step 3 and 4 depend on the actual deployment in a target environment.

1.Create a master key

2.Derive an initial host key k0 from a previously created master key

3.Store the initial host key k0 in a safe location outside of the log host

4.Deploy the key k0 on the log host where the secure logging module will be used

## CONFIGURATION
Secure logging is configured by adding the corresponding statements to the syslog-ng.conf file. See syslog-ng.conf(5) for information on how to configure syslog-ng using the configuration file. Details can be found in the The syslog-ng Administrator Guide[1].

Secure logging is implemented as a template and is configured accordingly. Apart from the actual template configuration, no other settings are required in order to activate secure logging. The secure logging is activated by placing the following statement in the configuration file

template("$(slog --key-file <host key file> --mac-file <MAC file> $RAWMSG)\n");

where

slog

The name of the secure logging template function. This name can be also be found by calling syslog-ng with the --module-registry arguments and checking the template-func property of the secure logging module in the corresponding output.

--key-file or -k

The host key. <host key file> is the full path of the file storing the host key on the log host. If this arguments is not supplied or does not point to a valid regular key file, syslog-ng will not start and a display a corresponding error message.

--mac-file or -m

The MAC file. <MAC file> is the full path of the MAC file on the log host. The file does not need to exist, as it will be automatically created upon the initial start. If the path is not correct, syslog-ng will not start and a display a corresponding error message.

$RAWMSG

$RAWMSG provides access to the original log message received at the source. This macro is only available if the store-raw-message flag was set for the source. Otherwise, an empty string will be passed to the secure logging template. If access to the original message is not available, e.g. if the source does not support the store-raw-message flag, then the $MSG macro can also be used. In this case, however, the integrity guarantee provided by secure logging is limited to the content that this macro provides and does not protect the complete original message.

\n

\n is the line separator. This is important, as the secure logging template expects log entries to be separated by a line separator. When detecting a line separator, the log entry is regarded as complete and is encrypted with the current host key. Therefore, only a single line separator is allowed.

The secure logging template can be combined with any source or destination within the following limitations:

•Sources must be line-oriented. Secure logging uses a line separator in order to distinguish between individual log entries. Sources which provide data in a different format, e.g. in the form of raw data obtained directly from a database system, cannot currently be used with the secure logging template, as the separation of log entries is not clearly defined for this type of data.

•Only sources for which the store-raw-message flag is implemented and set do benefit from the integrity guarantee provided by the secure logging template. Secure logging aims at protecting the integrity of complete log messages including all associated meta-data, such as timestamps and host names. syslog-ng parses the log message into its internal format and provide easy access to parts of a message through macros. While this is convenient when rewriting log messages, it is not helpful for secure logging. syslog-ng provides the store-raw-message flag which provides access to a copy of the original log message after parsing. This is the log message processed and protected by the secure logging template. If the source does not support the store-raw-message flag, then the $MSG macro can also be used. However, in this case the integrity guarantee provided by secure logging is limited to the content that this macro provides.

•Log rotation of any kind cannot be used with destinations using secure logging. The reason is that log rotate will overwrite, i.e. delete previous log files. This destroys the cryptographic chain of trust of the log entries making recovery impossible. In order to allow for a an efficient handling of log files, the secure logging environment features iterative verification. Using iterative verification, a log file can be verified in steps. For this to work, the log file must first be downloaded from the log host, together with the corresponding host key and MAC file to a verification host. After this download the log file can be safely deleted from the log host. Verification is then performed on the verification host using the iterative mode of the slogverify utility. See slogverify(1) for details.

The following example configuration shows the use of the secure logging template on a file destination.

```
#############################################################################
# Minimal syslog-ng.conf file with secure logging enabled. Encrypted log
# entries will be logged to a single file called /var/log/messages.slog
#
@version: 3.34
@include "scl.conf"
source s_local {

    system();

    internal();
};
source s_network {

    network(

        transport("udp")

        port(514)

        flags(store-raw-message)

    );
};
# Secure logging template definition
template secure_logging {

    template("$(slog --key-file /etc/syslog-ng/host.key --mac-file /etc/syslog-ng/mac.dat $RAWMSG)\n");
};
# This configures a secure logging destination
destination d_local {
	file("/var/log/messages.slog" template(secure_logging));
};
log {

    source(s_local);

    # This source has the raw message flag set

    source(s_network);

    # This statement activates secure logging for this destination

    destination(d_local);
};

```

## LOG VERIFICATION
In order to analyze the log file created in a secure logging environment, the log files must be decrypted and their integrity be verified. Verification requires both the initial host key k0 and the corresponding MAC file and is performed with the slogverify utility. It is not normally performed on the log host where the secure logging environment is producing log data. In a typical deployment, log files would be retrieved from the log host and transferred to a central log collector where verification it performed. As verification requires the use of the initial host key k0, it should only be performed in a trusted environment.

### Normal mode
In normal mode, a complete log archive is verified at once. In a typical environment, this would mean retrieving a log file together with is MAC file from a log host and retrieving the corresponding initial key k0 form a safe location and supplying them to the slogverify utility. A typical call sequence for verification in normal mode would look like this

slogverify --key-file host0.key --mac-file mac.dat /var/log/messages.slog /var/log/verified/messages

with

host0.key

The initial host key k0. Supplying the initial key k0 is enough for decrypting all log entries, as the key derivation algorithm is able to generate the necessary keys for all subsequent log entries based on the initial key k0.

mac.dat

The MAC file from the log host.

/var/log/messages.slog

The file containing the encrypted log entries as retrieved from a log host.

/var/log/verified/messages

The file receiving the plain text log after decryption.

Log files may become large and not fit into system memory. Verification is therefore performed in chunks. Each part of the log file is transferred to an internal buffer on which verification is performed. After the buffer has been processed, the next chunk is fetched. An optional buffer argument can be supplied to the slogverify utility in order to change the default buffer size of 1000 log entries to a number suitable for the system on which the verification is performed, for example

slogverify --key-file host.key --mac-file mac.dat /var/log/messages.slog /var/log/verified/messages 8000

See slogverify(1) for details on verification in normal mode.

### Iterative mode
Verification in normal mode may not be suitable for some application scenarios. Many log hosts use log rotation in order to preserve storage space. In log rotation, a threshold for the maximum amount of storage space and the number of generations is defined for different type of log files. When either storage space is exhausted or the number of generations is reached, the oldest log file will be overwritten by new incoming log data. This procedure is not possible in secure logging environment, as overwriting, i.e. deleting a log file would break the cryptographic chain that is established between the log entries. This comes as no surprise, as one of the main objectives of secure logging is to protect against deletion of log entries by a potential attacker.

In order allow for a procedure similar to log rotation, the secure logging environment features an iterative mode. In iterative mode, log files can be split into different files and each of these files can be verified separately. Care must be taken when performing verification in iterative mode, as each of the different log files needs to be accompanied by a copy of the host key and the MAC files present on the system at the time of retrieval. A typical usage scenario for the iterative mode would look like this:

1.Define a storage threshold for the secure logging file destination. In this example we assume 500MB.

2.Let the secure logging environment produce log data that is written to the destination until 500MB are reached.

3.Stop the secure logging environment and retrieve the log file, the host key and the MAC files from the log host.

4.Delete the log file on the log host but leave host key and MAC file untouched.

5.Restart the secure logging environment.

6.Perform verification in iterative mode with the log file, the host key and the MAC just retrieved.

Steps 2-6 have to repeated each time the log file reaches a size of 50 MB. Assuming that the log file parts will be named after the iteration, e.g. log.1, log.2, log.3, etc. and a similar convention is applied to the host keys and MAC files, a typical call sequence for the validation of a log file part in iterative mode after three iterations will look like this:

slogverify --iterative --prev-key-file host.key.2 --prev-mac-file mac.dat.2 --mac-file mac.dat /var/log/messages.slog.3 /var/log/verified/messages.3

with

host.key.2

The host key from the previous iteration. In this example, this is the second iteration.

mac.dat.2

The MAC file from the previous iteration. In the example, verification is performed during the third iteration, so the MAC file from the second iteration is required.

mac.dat

The current MAC file from the log host.

/var/log/messages.slog.3

The file part containing the encrypted log entries as retrieved from the log host during the third iteration.

/var/log/verified/messages.3

The file receiving the plain text log after decryption during the third iteration.

In a real deployment, the above steps would typically be automated using a scripting engine.

See slogverify(1) for details on verification in iterative mode.

## FILES
/usr/bin/slogkey

/usr/bin/slogencrypt

/usr/bin/slogverify

/etc/syslog-ng.conf

## SEE ALSO
syslog-ng.conf(5)

slogkey(1)

slogencrypt(1)

slogverify(1)

Note

For the detailed documentation of see The syslog-ng Administrator Guide[1]

If you experience any problems or need help with syslog-ng, visit the syslog-ng mailing list[2].

For news and notifications about of syslog-ng, visit the syslog-ng blogs[3].

For specific information requests related to secure logging send a mail to the Airbus Secure Logging Team <secure-logging@airbus.com>.

## AUTHOR
This manual page was written by the Airbus Secure Logging Team <secure-logging@airbus.com>.

## NOTES
1.

The syslog-ng Administrator Guide

https://www.balabit.com/documents/syslog-ng-ose-latest-guides/en/syslog-ng-ose-guide-admin/html/index.html

2.

syslog-ng mailing list

https://lists.balabit.hu/mailman/listinfo/syslog-ng

3.

syslog-ng blogs

https://syslog-ng.org/blogs/


bootup

package: systemd

title: bootup(7)

summary: System bootup process

text:
# bootup(7)

## NAME
bootup - System bootup process

## DESCRIPTION
A number of different components are involved in the boot of a Linux system. Immediately after power-up, the system firmware will do minimal hardware initialization, and hand control over to a boot loader (e.g. systemd-boot(7) or GRUB[1]) stored on a persistent storage device. This boot loader will then invoke an OS kernel from disk (or the network). On systems using EFI or other types of firmware, this firmware may also load the kernel directly.

The kernel (optionally) mounts an in-memory file system, which looks for the root file system. Nowadays this is implemented as an "initramfs" — a compressed CPIO archive that the kernel extracts into a tmpfs. In the past normal file systems using an in-memory block device (ramdisk) were used, and the name "initrd" is still used to describe both concepts. It's the boot loader or the firmware that loads both the kernel and initrd/initramfs images into memory, but the kernel which interprets it as a file system. systemd(1) may be used to manage services in the initrd, similarly to the real system.

After the root file system is found and mounted, the initrd hands over control to the host's system manager (such as systemd(1)) stored in the root file system, which is then responsible for probing all remaining hardware, mounting all necessary file systems and spawning all configured services.

On shutdown, the system manager stops all services, unmounts all non-busy file systems (detaching the storage technologies backing them), and then (optionally) jumps into the exitrd. The exitrd is backed by tmpfs and unmounts/detaches the remaining file systems, including the real root. As a last step, the system is powered down.

Additional information about the system boot process may be found in boot(7).

## SYSTEM MANAGER BOOTUP
At boot, the system manager on the OS image is responsible for initializing the required file systems, services and drivers that are necessary for operation of the system. On systemd(1) systems, this process is split up in various discrete steps which are exposed as target units. (See systemd.target(5) for detailed information about target units.) The boot-up process is highly parallelized so that the order in which specific target units are reached is not deterministic, but still adheres to a limited amount of ordering structure.

When systemd starts up the system, it will activate all units that are dependencies of default.target (as well as recursively all dependencies of these dependencies). Usually, default.target is simply an alias of graphical.target or multi-user.target, depending on whether the system is configured for a graphical UI or only for a text console. To enforce minimal ordering between the units pulled in, a number of well-known target units are available, as listed on systemd.special(7).

The following chart is a structural overview of these well-known units and their position in the boot-up logic. The arrows describe which units are pulled in and ordered before which other units. Units near the top are started before units nearer to the bottom of the chart.

```
                             cryptsetup-pre.target veritysetup-pre.target

                                                  |
(various low-level                                v

 API VFS mounts:             (various cryptsetup/veritysetup devices...)

 mqueue, configfs,                                |    |

 debugfs, ...)                                    v    |

 |                                  cryptsetup.target  |

 |  (various swap                                 |    |    remote-fs-pre.target

 |   devices...)                                  |    |     |        |

 |    |                                           |    |     |        v

 |    v                       local-fs-pre.target |    |     |  (network file systems)

 |  swap.target                       |           |    v     v                 |

 |    |                               v           |  remote-cryptsetup.target  |

 |    |  (various low-level  (various mounts and  |  remote-veritysetup.target |

 |    |   services: udevd,    fsck services...)   |             |              |

 |    |   tmpfiles, random            |           |             |    remote-fs.target

 |    |   seed, sysctl, ...)          v           |             |              |

 |    |      |                 local-fs.target    |             | _____________/

 |    |      |                        |           |             |/

 \____|______|_______________   ______|___________/             |

                             \ /                                |

                              v                                 |

                       sysinit.target                           |

                              |                                 |

       ______________________/|\_____________________           |

      /              |        |      |               \          |

      |              |        |      |               |          |

      v              v        |      v               |          |

 (various       (various      |  (various            |          |

  timers...)      paths...)   |   sockets...)        |          |

      |              |        |      |               |          |

      v              v        |      v               |          |
timers.target  paths.target   |  sockets.target      |          |

      |              |        |      |               v          |

      v              \_______ | _____/         rescue.service   |

                             \|/                     |          |

                              v                      v          |

                          basic.target         rescue.target    |

                              |                                 |

                      ________v____________________             |

                     /              |              \            |

                     |              |              |            |

                     v              v              v            |

                 display-    (various system   (various system  |

             manager.service     services        services)      |

                     |         required for        |            |

                     |        graphical UIs)       v            v

                     |              |            multi-user.target
emergency.service    |              |              |

        |            \_____________ | _____________/

        v                          \|/
emergency.target                    v

                              graphical.target
```

Target units that are commonly used as boot targets are emphasized. These units are good choices as goal targets, for example by passing them to the systemd.unit= kernel command line option (see systemd(1)) or by symlinking default.target to them.

timers.target is pulled-in by basic.target asynchronously. This allows timers units to depend on services which become only available later in boot.

## USER MANAGER STARTUP
The system manager starts the user@uid.service unit for each user, which launches a separate unprivileged instance of systemd for each user — the user manager. Similarly to the system manager, the user manager starts units which are pulled in by default.target. The following chart is a structural overview of the well-known user units. For non-graphical sessions, default.target is used. Whenever the user logs into a graphical session, the login manager will start the graphical-session.target target that is used to pull in units required for the graphical session. A number of targets (shown on the right side) are started when specific hardware is available to the user.

```
   (various           (various         (various

    timers...)         paths...)        sockets...)    (sound devices)

        |                  |                 |               |

        v                  v                 v               v

  timers.target      paths.target     sockets.target    sound.target

        |                  |                 |

        \______________   _|_________________/         (bluetooth devices)

                       \ /                                   |

                        V                                    v

                  basic.target                          bluetooth.target

                        |

             __________/ \_______                      (smartcard devices)

            /                    \                           |

            |                    |                           v

            |                    v                      smartcard.target

            v            graphical-session-pre.target
(various user services)          |                       (printers)

            |                    v                           |

            |       (services for the graphical session)     v

            |                    |                       printer.target

            v                    v

     default.target      graphical-session.target
```

## BOOTUP IN THE INITRD
Systemd can be used in the initrd as well. It detects the initrd environment by checking for the /etc/initrd-release file. The default target in the initrd is initrd.target. The bootup process is identical to the system manager bootup until the target basic.target. After that, systemd executes the special target initrd.target. Before any file systems are mounted, the manager will determine whether the system shall resume from hibernation or proceed with normal boot. This is accomplished by systemd-hibernate-resume.service which must be finished before local-fs-pre.target, so no filesystems can be mounted before the check is complete. When the root device becomes available, initrd-root-device.target is reached. If the root device can be mounted at /sysroot, the sysroot.mount unit becomes active and initrd-root-fs.target is reached. The service initrd-parse-etc.service scans /sysroot/etc/fstab for a possible /usr/ mount point and additional entries marked with the x-initrd.mount option. All entries found are mounted below /sysroot, and initrd-fs.target is reached. The service initrd-cleanup.service isolates to the initrd-switch-root.target, where cleanup services can run. As the very last step, the initrd-switch-root.service is activated, which will cause the system to switch its root to /sysroot.

```
                               : (beginning identical to above)

                               :

                               v

                         basic.target

                               |                       emergency.service

        ______________________/|                               |

       /                       |                               v

       |            initrd-root-device.target          emergency.target

       |                       |

       |                       v

       |                  sysroot.mount

       |                       |

       |                       v

       |             initrd-root-fs.target

       |                       |

       |                       v

       v            initrd-parse-etc.service
(custom initrd                 |

 services...)                  v

       |            (sysroot-usr.mount and

       |             various mounts marked

       |               with fstab option

       |              x-initrd.mount...)

       |                       |

       |                       v

       |                initrd-fs.target

       \______________________ |

                              \|

                               v

                          initrd.target

                               |

                               v

                     initrd-cleanup.service

                          isolates to

                    initrd-switch-root.target

                               |

                               v

        ______________________/|

       /                       v

       |        initrd-udevadm-cleanup-db.service

       v                       |
(custom initrd                 |

 services...)                  |

       \______________________ |

                              \|

                               v

                   initrd-switch-root.target

                               |

                               v

                   initrd-switch-root.service

                               |

                               v

                     Transition to Host OS
```

## SYSTEM MANAGER SHUTDOWN
System shutdown with systemd also consists of various target units with some minimal ordering structure applied:

```
                       (conflicts with  (conflicts with

                          all system     all file system

                           services)     mounts, swaps,

                               |           cryptsetup/

                               |           veritysetup

                               |          devices, ...)

                               |                |

                               v                v

                        shutdown.target    umount.target

                               |                |

                               \_______   ______/

                                       \ /

                                        v

                               (various low-level

                                    services)

                                        |

                                        v

                                  final.target

                                        |

            ___________________________/ \_________________________________

           /               |               |               |               \

           |               |               |               |               |

           v               |               |               |               |
systemd-reboot.service     |               |               |               |

           |               v               |               |               |

           |    systemd-poweroff.service   |               |               |

           v               |               v               |               |

     reboot.target         |      systemd-halt.service     |               |

                           v               |               v               |

                   poweroff.target         |    systemd-kexec.service      |

                                           v               |               |

                                      halt.target          |  systemd-soft-reboot.service

                                                           v               |

                                                     kexec.target          |

                                                                           v

                                                                   soft-reboot.target
```

Commonly used system shutdown targets are emphasized.

Note that systemd-halt.service(8), systemd-reboot.service, systemd-poweroff.service and systemd-kexec.service will transition the system and server manager (PID 1) into the second phase of system shutdown (implemented in the systemd-shutdown binary), which will unmount any remaining file systems, kill any remaining processes and release any other remaining resources, in a simple and robust fashion, without taking any service or unit concept into account anymore. At that point, regular applications and resources are generally terminated and released already, the second phase hence operates only as safety net for everything that could not be stopped or released for some reason during the primary, unit-based shutdown phase described above.

## THE EXITRD
The "exitrd" is a concept symmetrical to the initrd. When the system manager is shutting down and /run/initramfs/shutdown exists, it will switch root to /run/initramfs/ and execute /shutdown. This program runs from the tmpfs mounted on /run/, so it can unmount the old root file system and perform additional steps, for example dismantle complex storage or perform additional logging about the shutdown.

## SEE ALSO
systemd(1), boot(7), systemd.special(7), systemd.target(5), systemd-halt.service(8), systemd-soft-reboot.service(8)

## NOTES
1.

GRUB

https://www.gnu.org/software/grub/


daemon

title: daemon(7)

summary: Writing and packaging system daemons

text:
# daemon(7)

## NAME
daemon - Writing and packaging system daemons

## DESCRIPTION
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 systemd(1). This manual page covers both schemes, and in particular includes recommendations for daemons that shall be included in the systemd init system.

### SysV Daemons
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.

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 getrlimit() for RLIMIT_NOFILE.

2.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.

3.Reset the signal mask using sigprocmask().

4.Sanitize the environment block, removing or resetting environment variables that might negatively impact daemon runtime.

5.Call fork(), to create a background process.

6.In the child, call setsid() to detach from any terminal and create an independent session.

7.In the child, call fork() again, to ensure that the daemon can never re-acquire a terminal again. (This is relevant if the program — and all its dependencies — does not carefully specify `O_NOCTTY` on each and every single `open()` call that might potentially open a TTY device node.)

8.Call exit() 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.

9.In the daemon process, connect /dev/null to standard input, output, and error.

10.In the daemon process, reset the umask to 0, so that the file modes passed to open(), mkdir() and suchlike directly control the access mode of the created files and directories.

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.

12.In the daemon process, write the daemon PID (as returned by getpid()) to a PID file, for example /run/foobar.pid (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.

13.In the daemon process, drop privileges, if possible and applicable.

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 fork() and hence available in both the original and the daemon process.

15.Call exit() in the original process. The process that invoked the daemon must be able to rely on that this exit() happens after initialization is complete and all external communication channels are established and accessible.

The BSD daemon() function should not be used, as it implements only a subset of these steps.

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.

### New-Style Daemons
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.

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.

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 /dev/null and standard output/error connected to the systemd-journald.service(8) logging service, unless otherwise configured. The umask is reset.

It is recommended for new-style daemons to implement the following:

1.If applicable, the daemon should notify the service manager about startup completion or status updates via the sd_notify(3) interface, in particular READY=1 and STATUS=....

2.If SIGTERM is received, shut down the daemon and exit cleanly. A STOPPING=1 notification should be sent via sd_notify(3).

3.If SIGHUP is received, reload the configuration files, if this applies. This should be combined with notifications via sd_notify(3): RELOADING=1 and READY=1.

4.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 LSB recommendations for SysV init scripts[1].

5.If possible and applicable, expose the daemon's control interface via the D-Bus IPC system and grab a bus name as last step of initialization.

6.For integration in systemd, provide a .service unit file that carries information about starting, stopping and otherwise maintaining the daemon. See systemd.service(5) for details.

7.As much as possible, rely on the service manager'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's resource limit control instead of implementing your own, rely on systemd's privilege dropping code instead of implementing it in the daemon, and so on. See systemd.exec(5) for the available controls.

8.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 — 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.

9.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.

10.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 sd_notify(3) with FDSTORE=1.

11.Instead of using the syslog() call to log directly to the system syslog service, a new-style daemon may choose to simply log to standard error via fprintf(), which is then forwarded to syslog. If log levels are necessary, these can be encoded by prefixing individual log lines with strings like "<4>" (for log level 4 "WARNING" in the syslog priority scheme), following a similar style as the Linux kernel's printk() level system. For details, see sd-daemon(3) and systemd.exec(5).

12.As new-style daemons are invoked without a controlling TTY (but as their own session leaders) care should be taken to always specify O_NOCTTY on open(2) calls that possibly reference a TTY device node, so that no controlling TTY is accidentally acquired.

These recommendations are similar but not identical to the Apple MacOS X Daemon Requirements[2].

## ACTIVATION
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: bluetoothd.service 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.

### Activation on Boot
Old-style daemons are usually activated exclusively on boot (and manually by the administrator) via SysV init scripts, as detailed in the LSB Linux Standard Base Core Specification[1]. 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 use updated versions of activation, both during boot-up and during runtime and using more minimal service description files.

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-user.target or graphical.target, which are normally used as boot targets at system startup. See systemd.unit(5) for details about the .wants/ directories, and systemd.special(7) for details about the two boot targets.

### Socket-Based Activation
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.

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 sd_listen_fds(3) and sd-daemon(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.

systemd implements socket-based activation via .socket units, which are described in systemd.socket(5). When configuring socket units for socket-based activation, it is essential that all listening sockets are pulled in by the special target unit sockets.target. It is recommended to place a WantedBy=sockets.target directive in the [Install] section to automatically add such a dependency on installation of a socket unit. Unless DefaultDependencies=no is set, the necessary ordering dependencies are implicitly created for all socket units. For more information about sockets.target, see systemd.special(7). It is not necessary or recommended to place any additional dependencies on socket units (for example from multi-user.target or suchlike) when one is installed in sockets.target.

### Bus-Based Activation
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 SystemdService= 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 org.freedesktop.RealtimeKit.service, make sure to set SystemdService=rtkit-daemon.service in that file to bind it to the systemd service 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.

### Device-Based Activation
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 SYSTEMD_WANTS= property. See systemd.device(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 enable of systemctl(1) instead of manipulating the udev ruleset.

### Path-Based Activation
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 .path units, as outlined in systemd.path(5).

### Timer-Based Activation
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 .timer units, as described in systemd.timer(5).

### Other Forms of Activation
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 IP_FREEBIND/IPV6_FREEBIND socket option, as accessible via FreeBind=yes in systemd socket files (see systemd.socket(5) 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 CPUSchedulingPolicy=idle and/or IOSchedulingClass=idle. 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.

## INTEGRATION WITH SYSTEMD

### Writing systemd Unit Files
When writing systemd unit files, it is recommended to consider the following suggestions:

1.If possible, do not use the Type=forking setting in service files. But if you do, make sure to set the PID file path using PIDFile=. See systemd.service(5) for details.

2.If your daemon registers a D-Bus name on the bus, make sure to use Type=dbus in the service file if possible.

3.Make sure to set a good human-readable description string with Description=.

4.Do not disable DefaultDependencies=, unless you really know what you do and your unit is involved in early boot or late system shutdown.

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 systemd.special(7) or names introduced by your own package to keep the unit file operating system-independent.

6.Make sure to include an [Install] section including installation information for the unit file. See systemd.unit(5) for details. To activate your service on boot, make sure to add a WantedBy=multi-user.target or WantedBy=graphical.target directive. To activate your socket on boot, make sure to add WantedBy=sockets.target. Usually, you also want to make sure that when your service is installed, your socket is installed too, hence add Also=foo.socket in your service file foo.service, for a hypothetical program foo.

### Installing systemd Service Files
At the build installation time (e.g. make install during package build), packages are recommended to install their systemd unit files in the directory returned by pkg-config systemd --variable=systemdsystemunitdir (for system services) or pkg-config systemd --variable=systemduserunitdir (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. rpm -i by the administrator), symlinks should be created in the systemd configuration directories via the enable command of the systemctl(1) tool to activate them automatically on boot.

Packages using autoconf(1) are recommended to use a configure script excerpt like the following to determine the unit installation path during source configuration:

```
PKG_PROG_PKG_CONFIG()
AC_ARG_WITH([systemdsystemunitdir],

     [AS_HELP_STRING([--with-systemdsystemunitdir=DIR], [Directory for systemd service files])],,

     [with_systemdsystemunitdir=auto])
AS_IF([test "x$with_systemdsystemunitdir" = "xyes" -o "x$with_systemdsystemunitdir" = "xauto"], [

     def_systemdsystemunitdir=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)

     AS_IF([test "x$def_systemdsystemunitdir" = "x"],

   [AS_IF([test "x$with_systemdsystemunitdir" = "xyes"],

    [AC_MSG_ERROR([systemd support requested but pkg-config unable to query systemd package])])

    with_systemdsystemunitdir=no],

   [with_systemdsystemunitdir="$def_systemdsystemunitdir"])])
AS_IF([test "x$with_systemdsystemunitdir" != "xno"],

      [AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])])
AM_CONDITIONAL([HAVE_SYSTEMD], [test "x$with_systemdsystemunitdir" != "xno"])
```

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.)

Additionally, to ensure that make distcheck continues to work, it is recommended to add the following to the top-level Makefile.am file in automake(1)-based projects:

```
AM_DISTCHECK_CONFIGURE_FLAGS = \

  --with-systemdsystemunitdir=$$dc_install_base/$(systemdsystemunitdir)
```

Finally, unit files should be installed in the system with an automake excerpt like the following:

```
if HAVE_SYSTEMD
systemdsystemunit_DATA = \

  foobar.socket \

  foobar.service
endif
```

In the rpm(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.

At the top of the file:

```
BuildRequires: systemd
%{?systemd_requires}
```

And as scriptlets, further down:

```
%post
%systemd_post foobar.service foobar.socket
%preun
%systemd_preun foobar.service foobar.socket
%postun
%systemd_postun
```

If the service shall be restarted during upgrades, replace the "%postun" scriptlet above with the following:

```
%postun
%systemd_postun_with_restart foobar.service
```

Note that "%systemd_post" and "%systemd_preun" expect the names of all units that are installed/removed as arguments, separated by spaces. "%systemd_postun" expects no arguments. "%systemd_postun_with_restart" expects the units to restart as arguments.

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:

```
%triggerun -- foobar < 0.47.11-1
if /sbin/chkconfig --level 5 foobar ; then

  /bin/systemctl --no-reload enable foobar.service foobar.socket >/dev/null 2>&1 || :
fi
```

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 chkconfig 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.

## PORTING EXISTING DAEMONS
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.

To port an existing SysV compatible daemon, the following steps are recommended:

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-style init systems, but also to ease debugging.

2.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 sd_listen_fds(3) is checked for already passed sockets first. If sockets are passed (i.e. when sd_listen_fds() 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 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.

3.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.

4.If the daemon exposes interfaces via D-Bus, write and install a D-Bus activation file for the service, see above for details.

## PLACING DAEMON DATA
It is recommended to follow the general guidelines for placing package files, as discussed in file-hierarchy(7).

## NOTES
All example codes in this page are licensed under "MIT No Attribution" (SPDX-License-Identifier: MIT-0).

## SEE ALSO
systemd(1), sd-daemon(3), sd_listen_fds(3), sd_notify(3), daemon(3), systemd.service(5), file-hierarchy(7)

## NOTES
1.

LSB recommendations for SysV init scripts

http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html

2.

Apple MacOS X Daemon Requirements

https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html


file-hierarchy

title: file-hierarchy(7)

summary: File system hierarchy overview

text:
# file-hierarchy(7)

## NAME
file-hierarchy - File system hierarchy overview

## DESCRIPTION
Operating systems using the systemd(1) system and service manager are organized based on a file system hierarchy inspired by UNIX, more specifically the hierarchy described in the File System Hierarchy[1] specification and hier(7), with various extensions, partially documented in the XDG Base Directory Specification[2] and XDG User Directories[3]. This manual page describes a more generalized, though minimal and modernized subset of these specifications that defines more strictly the suggestions and restrictions systemd makes on the file system hierarchy. Note that this document makes no attempt to define the directory structure comprehensively, it only documents a skeleton of a directory tree, that downstreams can extend. Because of that traditional directories such as /usr/include/ or /var/spool/ are not covered, even though it might (or might not) make a lot of sense to include them in the structure of an actually deployed OS.

Many of the paths described here can be queried with the systemd-path(1) tool.

## GENERAL STRUCTURE
/

The file system root. Usually writable, but this is not required. Possibly a temporary file system ("tmpfs"). Not shared with other hosts (unless read-only).

/boot/

The boot partition used for bringing up the system. On EFI systems, this is possibly the EFI System Partition (ESP), also see systemd-gpt-auto-generator(8). This directory is usually strictly local to the host, and should be considered read-only, except when a new kernel or boot loader is installed. This directory only exists on systems that run on physical or emulated hardware that requires boot loaders.

/efi/

If the boot partition /boot/ is maintained separately from the EFI System Partition (ESP), the latter is mounted here. Tools that need to operate on the EFI system partition should look for it at this mount point first, and fall back to /boot/ — if the former does not qualify (for example if it is not a mount point or does not have the correct file system type MSDOS_SUPER_MAGIC).

/etc/

System-specific configuration. This directory may or may not be read-only. Frequently, this directory is pre-populated with vendor-supplied configuration files, but applications should not make assumptions about this directory being fully populated or populated at all, and should fall back to defaults if configuration is missing.

/home/

The location for normal user's home directories. Possibly shared with other systems, and never read-only. This directory should only be used for normal users, never for system users. This directory and possibly the directories contained within it might only become available or writable in late boot or even only after user authentication. This directory might be placed on limited-functionality network file systems, hence applications should not assume the full set of file API is available on this directory. Applications should generally not reference this directory directly, but via the per-user $HOME environment variable, or via the home directory field of the user database.

/root/

The home directory of the root user. The root user's home directory is located outside of /home/ in order to make sure the root user may log in even without /home/ being available and mounted.

/srv/

The place to store general server payload, managed by the administrator. No restrictions are made how this directory is organized internally. Generally writable, and possibly shared among systems. This directory might become available or writable only very late during boot.

/tmp/

The place for small temporary files. This directory is usually mounted as a "tmpfs" instance, and should hence not be used for larger files. (Use /var/tmp/ for larger files.) This directory is usually flushed at boot-up. Also, files that are not accessed within a certain time may be automatically deleted. If applications find the environment variable $TMPDIR set, they should use the directory specified in it instead of /tmp/ (see environ(7) and IEEE Std 1003.1[4] for details).

Since /tmp/ is accessible to other users of the system, it is essential that files and subdirectories under this directory are only created with mkstemp(3), mkdtemp(3), and similar calls. For more details, see Using /tmp/ and /var/tmp/ Safely[5].

## RUNTIME DATA
/run/

A "tmpfs" file system for system packages to place runtime data, socket files, and similar. This directory is flushed on boot, and generally writable for privileged programs only. Always writable.

/run/log/

Runtime system logs. System components may place private logs in this directory. Always writable, even when /var/log/ might not be accessible yet.

/run/user/

Contains per-user runtime directories, each usually individually mounted "tmpfs" instances. Always writable, flushed at each reboot and when the user logs out. User code should not reference this directory directly, but via the $XDG_RUNTIME_DIR environment variable, as documented in the XDG Base Directory Specification[2].

## VENDOR-SUPPLIED OPERATING SYSTEM RESOURCES
/usr/

Vendor-supplied operating system resources. Usually read-only, but this is not required. Possibly shared between multiple hosts. This directory should not be modified by the administrator, except when installing or removing vendor-supplied packages.

/usr/bin/

Binaries and executables for user commands that shall appear in the $PATH search path. It is recommended not to place binaries in this directory that are not useful for invocation from a shell (such as daemon binaries); these should be placed in a subdirectory of /usr/lib/ instead.

/usr/lib/

Static, private vendor data that is compatible with all architectures (though not necessarily architecture-independent). Note that this includes internal executables or other binaries that are not regularly invoked from a shell. Such binaries may be for any architecture supported by the system. Do not place public libraries in this directory, use $libdir (see below), instead.

/usr/lib/arch-id/

Location for placing dynamic libraries into, also called $libdir. The architecture identifier to use is defined on Multiarch Architecture Specifiers (Tuples)[6] list. Legacy locations of $libdir are /usr/lib/, /usr/lib64/. This directory should not be used for package-specific data, unless this data is architecture-dependent, too. To query $libdir for the primary architecture of the system, invoke:

```
systemd-path system-library-arch
```

/usr/share/

Resources shared between multiple packages, such as documentation, man pages, time zone information, fonts and other resources. Usually, the precise location and format of files stored below this directory is subject to specifications that ensure interoperability. Note that resources placed in this directory typically are under shared ownership, i.e. multiple different packages have provided and consumed these resources, on equal footing, without any obvious primary owner. This makes things systematically different from /usr/lib/, where ownership is generally not shared.

/usr/share/doc/

Documentation for the operating system or system packages.

/usr/share/factory/etc/

Repository for vendor-supplied default configuration files. This directory should be populated with pristine vendor versions of all configuration files that may be placed in /etc/. This is useful to compare the local configuration of a system with vendor defaults and to populate the local configuration with defaults.

/usr/share/factory/var/

Similar to /usr/share/factory/etc/, but for vendor versions of files in the variable, persistent data directory /var/.

## PERSISTENT VARIABLE SYSTEM DATA
/var/

Persistent, variable system data. Writable during normal system operation. This directory might be pre-populated with vendor-supplied data, but applications should be able to reconstruct necessary files and directories in this subhierarchy should they be missing, as the system might start up without this directory being populated. Persistency is recommended, but optional, to support ephemeral systems. This directory might become available or writable only very late during boot. Components that are required to operate during early boot hence shall not unconditionally rely on this directory.

/var/cache/

Persistent system cache data. System components may place non-essential data in this directory. Flushing this directory should have no effect on operation of programs, except for increased runtimes necessary to rebuild these caches.

/var/lib/

Persistent system data. System components may place private data in this directory.

/var/log/

Persistent system logs. System components may place private logs in this directory, though it is recommended to do most logging via the syslog(3) and sd_journal_print(3) calls.

/var/tmp/

The place for larger and persistent temporary files. In contrast to /tmp/, this directory is usually mounted from a persistent physical file system and can thus accept larger files. (Use /tmp/ for small ephemeral files.) This directory is generally not flushed at boot-up, but time-based cleanup of files that have not been accessed for a certain time is applied. If applications find the environment variable $TMPDIR set, they should use the directory specified in it instead of /var/tmp/ (see environ(7) for details).

The same security restrictions as with /tmp/ apply: mkstemp(3), mkdtemp(3), and similar calls should be used. For further details about this directory, see Using /tmp/ and /var/tmp/ Safely[5].

## VIRTUAL KERNEL AND API FILE SYSTEMS
/dev/

The root directory for device nodes. Usually, this directory is mounted as a "devtmpfs" instance, but might be of a different type in sandboxed/containerized setups. This directory is managed jointly by the kernel and systemd-udevd(8), and should not be written to by other components. A number of special purpose virtual file systems might be mounted below this directory.

/dev/shm/

Place for POSIX shared memory segments, as created via shm_open(3). This directory is flushed on boot, and is a "tmpfs" file system. Since all users have write access to this directory, special care should be taken to avoid name clashes and vulnerabilities. For normal users, shared memory segments in this directory are usually deleted when the user logs out. Usually, it is a better idea to use memory mapped files in /run/ (for system programs) or $XDG_RUNTIME_DIR (for user programs) instead of POSIX shared memory segments, since these directories are not world-writable and hence not vulnerable to security-sensitive name clashes.

/proc/

A virtual kernel file system exposing the process list and other functionality. This file system is mostly an API to interface with the kernel and not a place where normal files may be stored. For details, see proc(5). A number of special purpose virtual file systems might be mounted below this directory.

/proc/sys/

A hierarchy below /proc/ that exposes a number of kernel tunables. The primary way to configure the settings in this API file tree is via sysctl.d(5) files. In sandboxed/containerized setups, this directory is generally mounted read-only.

/sys/

A virtual kernel file system exposing discovered devices and other functionality. This file system is mostly an API to interface with the kernel and not a place where normal files may be stored. In sandboxed/containerized setups, this directory is generally mounted read-only. A number of special purpose virtual file systems might be mounted below this directory.

/sys/fs/cgroup/

A virtual kernel file system exposing process control groups (cgroups). This file system is an API to interface with the kernel and not a place where normal files may be stored. On current systems running in the default "unified" mode, this directory serves as the mount point for the "cgroup2" filesystem, which provides a unified cgroup hierarchy for all resource controllers. On systems with non-default configurations, this directory may instead be a tmpfs filesystem containing mount points for various "cgroup" (v1) resource controllers; in such configurations, if "cgroup2" is mounted it will be mounted on /sys/fs/cgroup/unified/, but cgroup2 will not have resource controllers attached. In sandboxed/containerized setups, this directory may either not exist or may include a subset of functionality.

## COMPATIBILITY SYMLINKS
/bin/, /sbin/, /usr/sbin/

These compatibility symlinks point to /usr/bin/, ensuring that scripts and binaries referencing these legacy paths correctly find their binaries.

/lib/

This compatibility symlink points to /usr/lib/, ensuring that programs referencing this legacy path correctly find their resources.

/lib64/

On some architecture ABIs, this compatibility symlink points to $libdir, ensuring that binaries referencing this legacy path correctly find their dynamic loader. This symlink only exists on architectures whose ABI places the dynamic loader in this path.

/var/run/

This compatibility symlink points to /run/, ensuring that programs referencing this legacy path correctly find their runtime data.

## HOME DIRECTORY
User applications may want to place files and directories in the user's home directory. They should follow the following basic structure. Note that some of these directories are also standardized (though more weakly) by the XDG Base Directory Specification[2]. Additional locations for high-level user resources are defined by xdg-user-dirs[3].

~/.cache/

Persistent user cache data. User programs may place non-essential data in this directory. Flushing this directory should have no effect on operation of programs, except for increased runtimes necessary to rebuild these caches. If an application finds $XDG_CACHE_HOME set, it should use the directory specified in it instead of this directory.

~/.config/

Application configuration. When a new user is created, this directory will be empty or not exist at all. Applications should fall back to defaults should their configuration in this directory be missing. If an application finds $XDG_CONFIG_HOME set, it should use the directory specified in it instead of this directory.

~/.local/bin/

Executables that shall appear in the user's $PATH search path. It is recommended not to place executables in this directory that are not useful for invocation from a shell; these should be placed in a subdirectory of ~/.local/lib/ instead. Care should be taken when placing architecture-dependent binaries in this place, which might be problematic if the home directory is shared between multiple hosts with different architectures.

~/.local/lib/

Static, private vendor data that is compatible with all architectures.

~/.local/lib/arch-id/

Location for placing public dynamic libraries. The architecture identifier to use is defined on Multiarch Architecture Specifiers (Tuples)[6] list.

~/.local/share/

Resources shared between multiple packages, such as fonts or artwork. Usually, the precise location and format of files stored below this directory is subject to specifications that ensure interoperability. If an application finds $XDG_DATA_HOME set, it should use the directory specified in it instead of this directory.

~/.local/state/

Application state. When a new user is created, this directory will be empty or not exist at all. Applications should fall back to defaults should their state in this directory be missing. If an application finds $XDG_STATE_HOME set, it should use the directory specified in it instead of this directory.

## WRITE ACCESS

### Unprivileged Write Access
Unprivileged processes generally lack write access to most of the hierarchy.

The exceptions for normal users are /tmp/, /var/tmp/, /dev/shm/, as well as the home directory $HOME (usually found below /home/) and the runtime directory $XDG_RUNTIME_DIR (found below /run/user/) of the user, which are all writable.

For unprivileged system processes, only /tmp/, /var/tmp/ and /dev/shm/ are writable. If an unprivileged system process needs a private writable directory in /var/ or /run/, it is recommended to either create it before dropping privileges in the daemon code, to create it via tmpfiles.d(5) fragments during boot, or via the StateDirectory= and RuntimeDirectory= directives of service units (see systemd.unit(5) for details).

/tmp/, /var/tmp/ and /dev/shm/ should be mounted nosuid and nodev, which means that set-user-id mode and character or block special devices are not interpreted on those file systems. In general it is not possible to mount them noexec, because various programs use those directories for dynamically generated or optimized code, and with that flag those use cases would break. Using this flag is OK on special-purpose installations or systems where all software that may be installed is known and does not require such functionality. See the discussion of nosuid/nodev/noexec in mount(8) and PROT_EXEC in mmap(2).

### Lack of Write Access on Read-Only Systems and during System Recovery
As noted above, some systems operate with the /usr and /etc hierarchies mounted read-only, possibly only allowing write access during package upgrades. Other part of the hierarchy are generally mounted read-write (in particular /var and /var/tmp), but may be read-only when the kernel remounts the file system read-only in response to errors, or when the system is booted read-only for recovery purposes. To the extent reasonable, applications should be prepared to execute without write access, so that for example, failure to save non-essential data to /var/cache/ or failure to create a custom log file under /var/log does not prevent the application from running.

The /run/ directory is available since the earliest boot and is always writable. It should be used for any runtime data and sockets, so that write access to e.g. /etc or /var is not needed.

## NODE TYPES
Unix file systems support different types of file nodes, including regular files, directories, symlinks, character and block device nodes, sockets and FIFOs.

It is strongly recommended that /dev/ is the only location below which device nodes shall be placed. Similarly, /run/ shall be the only location to place sockets and FIFOs. Regular files, directories and symlinks may be used in all directories.

Applications should expect that a security policy might be enforced on a system that enforces these rules.

## SYSTEM PACKAGES
Developers of system packages should follow strict rules when placing their files in the file system. The following table lists recommended locations for specific types of files supplied by the vendor.

Table 1. System package vendor files locations

Directory

Purpose

/usr/bin/

Package executables that shall appear in the $PATH executable search path, compiled for any of the supported architectures compatible with the operating system. It is not recommended to place internal binaries or binaries that are not commonly invoked from the shell in this directory, such as daemon binaries. As this directory is shared with most other packages of the system, special care should be taken to pick unique names for files placed here, that are unlikely to clash with other package's files.

/usr/lib/arch-id/

Public shared libraries of the package. As above, be careful with using too generic names, and pick unique names for your libraries to place here to avoid name clashes.

/usr/lib/package/

Private static vendor resources of the package, including private binaries and libraries, or any other kind of read-only vendor data.

/usr/lib/arch-id/package/

Private other vendor resources of the package that are architecture-specific and cannot be shared between architectures. Note that this generally does not include private executables since binaries of a specific architecture may be freely invoked from any other supported system architecture.

Additional static vendor files with shared ownership may be installed in the

/usr/share/ hierarchy to the locations defined by the various relevant specifications.

The following directories shall be used by the package for local configuration and files created during runtime:

Table 2. System package variable files locations

Directory

Purpose

/etc/package/

System-specific configuration for the package. It is recommended to default to safe fallbacks if this configuration is missing, if this is possible. Alternatively, a tmpfiles.d(5) fragment may be used to copy or symlink the necessary files and directories from /usr/share/factory/ during boot, via the "L" or "C" directives.

/run/package/

Runtime data for the package. Packages must be able to create the necessary subdirectories in this tree on their own, since the directory is flushed automatically on boot. Alternatively, a tmpfiles.d(5) fragment may be used to create the necessary directories during boot, or the RuntimeDirectory= directive of service units may be used to create them at service startup (see systemd.unit(5) for details).

/run/log/package/

Runtime log data for the package. As above, the package needs to make sure to create this directory if necessary, as it will be flushed on every boot.

/var/cache/package/

Persistent cache data of the package. If this directory is flushed, the application should work correctly on next invocation, though possibly slowed down due to the need to rebuild any local cache files. The application must be capable of recreating this directory should it be missing and necessary. To create an empty directory, a tmpfiles.d(5) fragment or the CacheDirectory= directive of service units (see systemd.unit(5)) may be used.

/var/lib/package/

Persistent private data of the package. This is the primary place to put persistent data that does not fall into the other categories listed. Packages should be able to create the necessary subdirectories in this tree on their own, since the directory might be missing on boot. To create an empty directory, a tmpfiles.d(5) fragment or the StateDirectory= directive of service units (see systemd.unit(5)) may be used.

/var/log/package/

Persistent log data of the package. As above, the package should make sure to create this directory if necessary, possibly using tmpfiles.d(5) or LogsDirectory= (see systemd.exec(5)), as it might be missing.

## USER PACKAGES
Programs running in user context should follow strict rules when placing their own files in the user's home directory. The following table lists recommended locations in the home directory for specific types of files supplied by the vendor if the application is installed in the home directory. (User applications installed system-wide are covered by the rules outlined above for vendor files.)

Table 3. Vendor package file locations under the home directory of the user

Directory

Purpose

~/.local/bin/

Package executables that shall appear in the $PATH executable search path. It is not recommended to place internal executables or executables that are not commonly invoked from the shell in this directory, such as daemon executables. As this directory is shared with most other packages of the user, special care should be taken to pick unique names for files placed here, that are unlikely to clash with other package's files.

~/.local/lib/arch-id/

Public shared libraries of the package. As above, be careful with using overly generic names, and pick unique names for your libraries to place here to avoid name clashes.

~/.local/lib/package/

Private, static vendor resources of the package, compatible with any architecture, or any other kind of read-only vendor data.

~/.local/lib/arch-id/package/

Private other vendor resources of the package that are architecture-specific and cannot be shared between architectures.

Additional static vendor files with shared ownership may be installed in the

~/.local/share/ hierarchy, mirroring the subdirectories specified in the section "Vendor-supplied operating system resources" above.

The following directories shall be used by the package for per-user local configuration and files created during runtime:

Table 4. User package variable file locations

Directory

Purpose

~/.config/package/

User-specific configuration for the package. It is required to default to safe fallbacks if this configuration is missing.

$XDG_RUNTIME_DIR/package/

User runtime data for the package.

~/.cache/package/

Persistent cache data of the package. If this directory is flushed, the application should work correctly on next invocation, though possibly slowed down due to the need to rebuild any local cache files. The application must be capable of recreating this directory should it be missing and necessary.

~/.local/state/package/

Persistent state data of the package.

## SEE ALSO
systemd(1), hier(7), systemd-path(1), systemd-gpt-auto-generator(8), sysctl.d(5), tmpfiles.d(5), pkg-config(1), systemd.unit(5)

## NOTES
1.

File System Hierarchy

http://refspecs.linuxfoundation.org/FHS_3.0/fhs-3.0.html

2.

XDG Base Directory Specification

https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html

3.

XDG User Directories

https://www.freedesktop.org/wiki/Software/xdg-user-dirs

4.

IEEE Std 1003.1

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03

5.

Using /tmp/ and /var/tmp/ Safely

https://systemd.io/TEMPORARY_DIRECTORIES

6.

Multiarch Architecture Specifiers (Tuples)

https://wiki.debian.org/Multiarch/Tuples