]> git.ipfire.org Git - thirdparty/systemd.git/blame - docs/HACKING.md
Fix typo in hacking guide: "no possible" -> "not possible" (#10905)
[thirdparty/systemd.git] / docs / HACKING.md
CommitLineData
5a8a9dee
FA
1# Hacking on systemd
2
3We welcome all contributions to systemd. If you notice a bug or a missing
4feature, please feel invited to fix it, and submit your work as a GitHub Pull
5Request (PR) at https://github.com/systemd/systemd/pull/new.
6
7Please make sure to follow our [Coding Style](CODING_STYLE.md) when submitting patches.
8Also have a look at our [Contribution Guidelines](CONTRIBUTING.md).
9
10When adding new functionality, tests should be added. For shared functionality
11(in `src/basic/` and `src/shared/`) unit tests should be sufficient. The general
12policy is to keep tests in matching files underneath `src/test/`,
13e.g. `src/test/test-path-util.c` contains tests for any functions in
14`src/basic/path-util.c`. If adding a new source file, consider adding a matching
15test executable. For features at a higher level, tests in `src/test/` are very
1e268f42 16strongly recommended. If that is not possible, integration tests in `test/` are
5a8a9dee
FA
17encouraged.
18
19Please also have a look at our list of [code quality tools](CODE_QUALITY.md) we have setup for systemd,
20to ensure our codebase stays in good shape.
21
22Please always test your work before submitting a PR. For many of the components
23of systemd testing is straight-forward as you can simply compile systemd and
24run the relevant tool from the build directory.
25
26For some components (most importantly, systemd/PID1 itself) this is not
27possible, however. In order to simplify testing for cases like this we provide
28a set of `mkosi` build files directly in the source tree. `mkosi` is a tool for
29building clean OS images from an upstream distribution in combination with a
30fresh build of the project in the local working directory. To make use of this,
31please acquire `mkosi` from https://github.com/systemd/mkosi first, unless your
32distribution has packaged it already and you can get it from there. After the
33tool is installed it is sufficient to type `mkosi` in the systemd project
34directory to generate a disk image `image.raw` you can boot either in
35`systemd-nspawn` or in an UEFI-capable VM:
36
37```
38# systemd-nspawn -bi image.raw
39```
40
41or:
42
43```
44# qemu-system-x86_64 -enable-kvm -m 512 -smp 2 -bios /usr/share/edk2/ovmf/OVMF_CODE.fd -hda image.raw
45```
46
47Every time you rerun the `mkosi` command a fresh image is built, incorporating
48all current changes you made to the project tree.
49
50Alternatively, you may install the systemd version from your git check-out
51directly on top of your host system's directory tree. This mostly works fine,
52but of course you should know what you are doing as you might make your system
53unbootable in case of a bug in your changes. Also, you might step into your
54package manager's territory with this. Be careful!
55
56And never forget: most distributions provide very simple and convenient ways to
57install all development packages necessary to build systemd. For example, on
58Fedora the following command line should be sufficient to install all of
59systemd's build dependencies:
60
61```
62# dnf builddep systemd
63```
64
65Putting this all together, here's a series of commands for preparing a patch
66for systemd (this example is for Fedora):
67
68```sh
69$ sudo dnf builddep systemd # install build dependencies
70$ sudo dnf install mkosi # install tool to quickly build images
71$ git clone https://github.com/systemd/systemd.git
72$ cd systemd
73$ vim src/core/main.c # or wherever you'd like to make your changes
74$ meson build # configure the build
75$ ninja -C build # build it locally, see if everything compiles fine
76$ ninja -C build test # run some simple regression tests
77$ (umask 077; echo 123 > mkosi.rootpw) # set root password used by mkosi
78$ sudo mkosi # build a test image
79$ sudo systemd-nspawn -bi image.raw # boot up the test image
80$ git add -p # interactively put together your patch
81$ git commit # commit it
82$ git push REMOTE HEAD:refs/heads/BRANCH
83 # where REMOTE is your "fork" on GitHub
84 # and BRANCH is a branch name.
85```
86
87And after that, head over to your repo on GitHub and click "Compare & pull request"
88
89Happy hacking!
90
91
92## Fuzzers
93
94systemd includes fuzzers in `src/fuzz/` that use libFuzzer and are automatically
95run by [OSS-Fuzz](https://github.com/google/oss-fuzz) with sanitizers. To add a
96fuzz target, create a new `src/fuzz/fuzz-foo.c` file with a `LLVMFuzzerTestOneInput`
97function and add it to the list in `src/fuzz/meson.build`.
98
99Whenever possible, a seed corpus and a dictionary should also be added with new
100fuzz targets. The dictionary should be named `src/fuzz/fuzz-foo.dict` and the seed
101corpus should be built and exported as `$OUT/fuzz-foo_seed_corpus.zip` in
102`tools/oss-fuzz.sh`.
103
104The fuzzers can be built locally if you have libFuzzer installed by running
105`tools/oss-fuzz.sh`. You should also confirm that the fuzzer runs in the
106OSS-Fuzz environment by checking out the OSS-Fuzz repo, and then running
107commands like this:
108
109```
110python infra/helper.py build_image systemd
111python infra/helper.py build_fuzzers --sanitizer memory systemd ../systemd
112python infra/helper.py run_fuzzer systemd fuzz-foo
113```
114
115If you find a bug that impacts the security of systemd, please follow the
116guidance in [CONTRIBUTING.md](CONTRIBUTING.md) on how to report a security vulnerability.
117
118For more details on building fuzzers and integrating with OSS-Fuzz, visit:
119
120- https://github.com/google/oss-fuzz/blob/master/docs/new_project_guide.md
121- https://llvm.org/docs/LibFuzzer.html
122- https://github.com/google/fuzzer-test-suite/blob/master/tutorial/libFuzzerTutorial.md
123- https://chromium.googlesource.com/chromium/src/testing/libfuzzer/+/HEAD/efficient_fuzzer.md