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