]> git.ipfire.org Git - thirdparty/systemd.git/blame - docs/ARCHITECTURE.md
Merge pull request #32324 from mrc0mmand/more-website-fixes
[thirdparty/systemd.git] / docs / ARCHITECTURE.md
CommitLineData
2ecce1f1 1---
00d06c99 2title: systemd Repository Architecture
2ecce1f1
LB
3category: Contributing
4layout: default
0aff7b75 5SPDX-License-Identifier: LGPL-2.1-or-later
2ecce1f1
LB
6---
7
818e46ae
BF
8# The systemd Repository Architecture
9
10## Code Map
2ecce1f1 11
51d89006 12This document provides a high-level overview of the various components of the systemd repository.
2ecce1f1 13
818e46ae 14## Source Code
2ecce1f1 15
51d89006 16Directories in `src/` provide the implementation of all daemons, libraries and command-line tools shipped by the project.
17There are many, and more are constantly added, so we will not enumerate them all here — the directory names are self-explanatory.
2ecce1f1 18
818e46ae 19### Shared Code
2ecce1f1 20
51d89006 21The code that is shared between components is split into a few directories, each with a different purpose:
22
23- `src/basic/` and `src/fundamental/` — those directories contain code primitives that are used by all other code.
24 `src/fundamental/` is stricter, because it used for EFI and user-space code, while `src/basic/` is only used for user-space code.
25 The code in `src/fundamental/` cannot depend on any other code in the tree, and `src/basic/` can depend only on itself and `src/fundamental/`.
26 For user-space, a static library is built from this code and linked statically in various places.
27
28- `src/libsystemd/` implements the `libsystemd.so` shared library (also available as static `libsystemd.a`).
29 This code may use anything in `src/basic/` or `src/fundamental/`.
30
31- `src/shared/` provides various utilities and code shared between other components that is exposed as the `libsystemd-shared-<nnn>.so` shared library.
32
33The other subdirectories implement individual components.
34They may depend only on `src/fundamental/` + `src/basic/`, or also on `src/libsystemd/`, or also on `src/shared/`.
35
36You might wonder what kind of code belongs where.
37In general, the rule is that code should be linked as few times as possible, ideally only once.
38Thus code that is used by "higher-level" components (e.g. our binaries which are linked to `libsystemd-shared-<nnn>.so`),
39would go to a subdirectory specific to that component if it is only used there.
40If the code is to be shared between components, it'd go to `src/shared/`.
41Shared code that is used by multiple components that do not link to `libsystemd-shared-<nnn>.so` may live either in `src/libsystemd/`, `src/basic/`, or `src/fundamental/`.
42Any code that is used only for EFI goes under `src/boot/efi/`, and `src/fundamental/` if is shared with non-EFI compoenents.
2ecce1f1
LB
43
44To summarize:
45
e1c52cb2
ZJS
46`src/fundamental/`
47- may be used by all code in the tree
48- may not use any code outside of `src/fundamental/`
49
2ecce1f1
LB
50`src/basic/`
51- may be used by all code in the tree
e1c52cb2 52- may not use any code outside of `src/fundamental/` and `src/basic/`
2ecce1f1
LB
53
54`src/libsystemd/`
e1c52cb2 55- may be used by all code in the tree that links to `libsystem.so`
51d89006 56- may not use any code outside of `src/fundamental/`, `src/basic/`, and `src/libsystemd/`
2ecce1f1
LB
57
58`src/shared/`
51d89006 59- may be used by all code in the tree, except for code in `src/basic/`, `src/libsystemd/`, `src/nss-*`, `src/login/pam_systemd.*`,
60 and files under `src/journal/` that end up in `libjournal-client.a` convenience library.
61- may not use any code outside of `src/fundamental/`, `src/basic/`, `src/libsystemd/`, `src/shared/`
2ecce1f1 62
818e46ae 63### PID 1
2ecce1f1 64
51d89006 65Code located in `src/core/` implements the main logic of the systemd system (and user) service manager.
2ecce1f1
LB
66
67BPF helpers written in C and used by PID 1 can be found under `src/core/bpf/`.
68
818e46ae 69#### Implementing Unit Settings
76ab98fa 70
51d89006 71The system and session manager supports a large number of unit settings.
72These can generally be configured in three ways:
76ab98fa
LB
73
741. Via textual, INI-style configuration files called *unit* *files*
752. Via D-Bus messages to the manager
763. Via the `systemd-run` and `systemctl set-property` commands
77
51d89006 78From a user's perspective, the third is a wrapper for the second.
79To implement a new unit setting, it is necessary to support all three input methods:
76ab98fa 80
51d89006 811. *unit* *files* are parsed in `src/core/load-fragment.c`, with many simple and fixed-type unit settings being parsed by common helpers, with the definition in the generator file `src/core/load-fragment-gperf.gperf.in`
76ab98fa 822. D-Bus messages are defined and parsed in `src/core/dbus-*.c`
51d89006 833. `systemd-run` and `systemctl set-property` do client-side parsing and translation into D-Bus messages in `src/shared/bus-unit-util.c`
76ab98fa 84
51d89006 85So that they are exercised by the fuzzing CI, new unit settings should also be listed in the text files under `test/fuzz/fuzz-unit-file/`.
76ab98fa 86
818e46ae 87### systemd-udev
2ecce1f1 88
51d89006 89Sources for the udev daemon and command-line tool (single binary) can be found under `src/udev/`.
2ecce1f1 90
818e46ae 91### Unit Tests
2ecce1f1 92
51d89006 93Source files found under `src/test/` implement unit-level testing, mostly for modules found in `src/basic/` and `src/shared/`, but not exclusively.
94Each test file is compiled in a standalone binary that can be run to exercise the corresponding module.
95While most of the tests can be run by any user, some require privileges, and will attempt to clearly log about what they need (mostly in the form of effective capabilities).
96These tests are self-contained, and generally safe to run on the host without side effects.
2ecce1f1 97
51d89006 98Ideally, every module in `src/basic/` and `src/shared/` should have a corresponding unit test under `src/test/`, exercising every helper function.
2ecce1f1 99
818e46ae 100### Fuzzing
c04361d7 101
51d89006 102Fuzzers are a type of unit tests that execute code on an externally-supplied input sample.
103Fuzzers are called `fuzz-*`.
104Fuzzers for `src/basic/` and `src/shared` live under `src/fuzz/`, and those for other parts of the codebase should be located next to the code they test.
105
106Files under `test/fuzz/` contain input data for fuzzers, one subdirectory for each fuzzer.
107Some of the files are "seed corpora", i.e. files that contain lists of settings and input values intended to generate initial coverage, and other files are samples saved by the fuzzing engines when they find an issue.
108
109When adding new input samples under `test/fuzz/*/`, please use some short-but-meaningful names.
110Names of meson tests include the input file name and output looks awkward if they are too long.
111
112Fuzzers are invoked primarily in three ways:
113firstly, each fuzzer is compiled as a normal executable and executed for each of the input samples under `test/fuzz/` as part of the test suite.
114Secondly, fuzzers may be instrumented with sanitizers and invoked as part of the test suite (if `-Dfuzz-tests=true` is configured).
0d592a5e 115Thirdly, fuzzers are executed through fuzzing engines that tryto find new "interesting" inputs through coverage feedback and massive parallelization; see the links for oss-fuzz in [Code quality](/CODE_QUALITY).
51d89006 116For testing and debugging, fuzzers can be executed as any other program, including under `valgrind` or `gdb`.
c04361d7 117
818e46ae 118## Integration Tests
2ecce1f1 119
51d89006 120Sources in `test/TEST-*` implement system-level testing for executables, libraries and daemons that are shipped by the project.
121They require privileges to run, and are not safe to execute directly on a host.
122By default they will build an image and run the test under it via `qemu` or `systemd-nspawn`.
2ecce1f1 123
51d89006 124Most of those tests should be able to run via `systemd-nspawn`, which is orders-of-magnitude faster than `qemu`, but some tests require privileged operations like using `dm-crypt` or `loopdev`.
125They are clearly marked if that is the case.
2ecce1f1 126
4ba04a05 127See [`test/README.testsuite`](https://github.com/systemd/systemd/blob/main/test/README.testsuite) for more specific details.
2ecce1f1 128
818e46ae 129## hwdb
2ecce1f1 130
51d89006 131Rules built in the static hardware database shipped by the project can be found under `hwdb.d/`.
132Some of these files are updated automatically, some are filled by contributors.
2ecce1f1 133
818e46ae 134## Documentation
2ecce1f1 135
818e46ae 136### systemd.io
2ecce1f1 137
51d89006 138Markdown files found under `docs/` are automatically published on the [systemd.io](https://systemd.io) website using Github Pages.
139A minimal unit test to ensure the formatting doesn't have errors is included in the `meson test -C build/ github-pages` run as part of the CI.
2ecce1f1 140
818e46ae 141### Man pages
2ecce1f1 142
51d89006 143Manpages for binaries and libraries, and the DBUS interfaces, can be found under `man/` and should ideally be kept in sync with changes to the corresponding binaries and libraries.
2ecce1f1 144
818e46ae 145### Translations
2ecce1f1 146
51d89006 147Translations files for binaries and daemons, provided by volunteers, can be found under `po/` in the usual format.
148They are kept up to date by contributors and by automated tools.
2ecce1f1 149
818e46ae 150## System Configuration files and presets
2ecce1f1 151
51d89006 152Presets (or templates from which they are generated) for various daemons and tools can be found under various directories such as
153`factory/`, `modprobe.d/`, `network/`, `presets/`, `rules.d/`, `shell-completion/`, `sysctl.d/`, `sysusers.d/`, `tmpfiles.d/`.
2ecce1f1 154
818e46ae 155## Utilities for Developers
2ecce1f1 156
51d89006 157`tools/`, `coccinelle/`, `.github/`, `.semaphore/`, `.mkosi/` host various utilities and scripts that are used by maintainers and developers.
158They are not shipped or installed.
bb5232b6
LB
159
160# Service Manager Overview
161
51d89006 162The Service Manager takes configuration in the form of unit files, credentials, kernel command line options and D-Bus commands, and based on those manages the system and spawns other processes.
163It runs in system mode as PID1, and in user mode with one instance per user session.
164
165When starting a unit requires forking a new process, configuration for the new process will be serialized and passed over to the new process, created via a posix_spawn() call.
166This is done in order to avoid excessive processing after a fork() but before an exec(), which is against glibc's best practices and can also result in a copy-on-write trap.
167The new process will start as the `systemd-executor` binary, which will deserialize the configuration and apply all the options (sandboxing, namespacing, cgroup, etc.) before exec'ing the configured executable.
bb5232b6
LB
168
169```
170 ┌──────┐posix_spawn() ┌───────────┐execve() ┌────────┐
171 │ PID1 ├─────────────►│sd-executor├────────►│program │
172 └──────┘ (memfd) └───────────┘ └────────┘
173```