]> git.ipfire.org Git - thirdparty/systemd.git/blame - docs/CODING_STYLE.md
Merge pull request #17142 from poettering/catalog-fix-de
[thirdparty/systemd.git] / docs / CODING_STYLE.md
CommitLineData
c3e270f4
FB
1---
2title: Coding Style
4cdca0af 3category: Contributing
b41a3f66 4layout: default
c3e270f4
FB
5---
6
c1d3483d 7# Coding Style
82143987 8
8c9289e7
LP
9## Formatting
10
3b69b18f
ZJS
11- 8ch indent, no tabs, except for files in `man/` which are 2ch indent, and
12 still no tabs, and shell scripts, which are 4ch indent, and no tabs either.
82143987 13
8c9289e7
LP
14- We prefer `/* comments */` over `// comments` in code you commit,
15 please. This way `// comments` are left for developers to use for local,
16 temporary commenting of code for debug purposes (i.e. uncommittable stuff),
17 making such comments easily discernible from explanatory, documenting code
18 comments (i.e. committable stuff).
82143987
FA
19
20- Don't break code lines too eagerly. We do **not** force line breaks at 80ch,
21 all of today's screens should be much larger than that. But then again, don't
c90ee834 22 overdo it, ~109ch should be enough really. The `.editorconfig`, `.vimrc` and
82143987 23 `.dir-locals.el` files contained in the repository will set this limit up for
3b69b18f
ZJS
24 you automatically, if you let them (as well as a few other things). Please
25 note that emacs loads `.dir-locals.el` automatically, but vim needs to be
26 configured to load `.vimrc`, see that file for instructions.
82143987 27
7489ccc3
LB
28- If you break a function declaration over multiple lines, do it like this:
29
30 ```c
31 void some_function(
32 int foo,
33 bool bar,
34 char baz) {
35
36 int a, b, c;
37 ```
38
8c9289e7
LP
39- Try to write this:
40
41 ```c
42 void foo() {
43 }
44 ```
45
46 instead of this:
47
48 ```c
49 void foo()
50 {
51 }
52 ```
53
54- Single-line `if` blocks should not be enclosed in `{}`. Write this:
55
56 ```c
57 if (foobar)
58 waldo();
59 ```
60
61 instead of this:
62
63 ```c
64 if (foobar) {
65 waldo();
66 }
67 ```
68
69- Do not write `foo ()`, write `foo()`.
70
b4f12824 71## Code Organization and Semantics
8c9289e7 72
78e5b4d7
LP
73- Please name structures in `PascalCase` (with exceptions, such as public API
74 structs), variables and functions in `snake_case`.
82143987 75
b4f12824
LP
76- Avoid static variables, except for caches and very few other cases. Think
77 about thread-safety! While most of our code is never used in threaded
78 environments, at least the library code should make sure it works correctly
79 in them. Instead of doing a lot of locking for that, we tend to prefer using
80 TLS to do per-thread caching (which only works for small, fixed-size cache
81 objects), or we disable caching for any thread that is not the main
82 thread. Use `is_main_thread()` to detect whether the calling thread is the
83 main thread.
82143987 84
82143987 85- Do not write functions that clobber call-by-reference variables on
b4f12824
LP
86 failure. Use temporary variables for these cases and change the passed in
87 variables only on success.
82143987 88
82143987 89- The order in which header files are included doesn't matter too
b4f12824
LP
90 much. systemd-internal headers must not rely on an include order, so it is
91 safe to include them in any order possible. However, to not clutter global
92 includes, and to make sure internal definitions will not affect global
93 headers, please always include the headers of external components first
94 (these are all headers enclosed in <>), followed by our own exported headers
95 (usually everything that's prefixed by `sd-`), and then followed by internal
96 headers. Furthermore, in all three groups, order all includes alphabetically
82143987
FA
97 so duplicate includes can easily be detected.
98
b4f12824
LP
99- Please avoid using global variables as much as you can. And if you do use
100 them make sure they are static at least, instead of exported. Especially in
101 library-like code it is important to avoid global variables. Why are global
102 variables bad? They usually hinder generic reusability of code (since they
103 break in threaded programs, and usually would require locking there), and as
104 the code using them has side-effects make programs non-transparent. That
105 said, there are many cases where they explicitly make a lot of sense, and are
106 OK to use. For example, the log level and target in `log.c` is stored in a
107 global variable, and that's OK and probably expected by most. Also in many
108 cases we cache data in global variables. If you add more caches like this,
109 please be careful however, and think about threading. Only use static
110 variables if you are sure that thread-safety doesn't matter in your
111 case. Alternatively, consider using TLS, which is pretty easy to use with
112 gcc's `thread_local` concept. It's also OK to store data that is inherently
113 global in global variables, for example data parsed from command lines, see
82143987
FA
114 below.
115
82143987 116- You might wonder what kind of common code belongs in `src/shared/` and what
a527f70a
ZJS
117 belongs in `src/basic/`. The split is like this: anything that is used to
118 implement the public shared object we provide (sd-bus, sd-login, sd-id128,
119 nss-systemd, nss-mymachines, nss-resolve, nss-myhostname, pam_systemd), must
120 be located in `src/basic` (those objects are not allowed to link to
b2454670 121 libsystemd-shared.so). Conversely, anything which is shared between multiple
a527f70a
ZJS
122 components and does not need to be in `src/basic/`, should be in
123 `src/shared/`.
82143987
FA
124
125 To summarize:
126
127 `src/basic/`
128 - may be used by all code in the tree
129 - may not use any code outside of `src/basic/`
130
131 `src/libsystemd/`
132 - may be used by all code in the tree, except for code in `src/basic/`
133 - may not use any code outside of `src/basic/`, `src/libsystemd/`
134
135 `src/shared/`
a527f70a
ZJS
136 - may be used by all code in the tree, except for code in `src/basic/`,
137 `src/libsystemd/`, `src/nss-*`, `src/login/pam_systemd.*`, and files under
138 `src/journal/` that end up in `libjournal-client.a` convenience library.
82143987
FA
139 - may not use any code outside of `src/basic/`, `src/libsystemd/`, `src/shared/`
140
141- Our focus is on the GNU libc (glibc), not any other libcs. If other libcs are
142 incompatible with glibc it's on them. However, if there are equivalent POSIX
143 and Linux/GNU-specific APIs, we generally prefer the POSIX APIs. If there
144 aren't, we are happy to use GNU or Linux APIs, and expect non-GNU
145 implementations of libc to catch up with glibc.
146
78e5b4d7
LP
147## Using C Constructs
148
149- Preferably allocate local variables on the top of the block:
150
151 ```c
152 {
153 int a, b;
154
155 a = 5;
156 b = a;
157 }
158 ```
159
160- Do not mix function invocations with variable definitions in one line. Wrong:
161
162 ```c
163 {
164 int a = foobar();
165 uint64_t x = 7;
166 }
167 ```
168
169 Right:
170
171 ```c
172 {
173 int a;
174 uint64_t x = 7;
175
176 a = foobar();
177 }
178 ```
179
180- Use `goto` for cleaning up, and only use it for that. i.e. you may only jump
181 to the end of a function, and little else. Never jump backwards!
182
183- To minimize strict aliasing violations, we prefer unions over casting.
184
185- Instead of using `memzero()`/`memset()` to initialize structs allocated on
186 the stack, please try to use c99 structure initializers. It's short, prettier
187 and actually even faster at execution. Hence:
188
189 ```c
190 struct foobar t = {
191 .foo = 7,
192 .bar = "bazz",
193 };
194 ```
195
196 instead of:
197
198 ```c
199 struct foobar t;
200 zero(t);
201 t.foo = 7;
202 t.bar = "bazz";
203 ```
204
205- To implement an endless loop, use `for (;;)` rather than `while (1)`. The
206 latter is a bit ugly anyway, since you probably really meant `while
207 (true)`. To avoid the discussion what the right always-true expression for an
208 infinite while loop is, our recommendation is to simply write it without any
209 such expression by using `for (;;)`.
210
211- To determine the length of a constant string `"foo"`, don't bother with
212 `sizeof("foo")-1`, please use `strlen()` instead (both gcc and clang optimize
213 the call away for fixed strings). The only exception is when declaring an
214 array. In that case use STRLEN, which evaluates to a static constant and
215 doesn't force the compiler to create a VLA.
216
b5bd7a29
LP
217- Please use C's downgrade-to-bool feature only for expressions that are
218 actually booleans (or "boolean-like"), and not for variables that are really
219 numeric. Specifically, if you have an `int b` and it's only used in a boolean
220 sense, by all means check its state with `if (b) …` — but if `b` can actually
221 have more than two semantic values, and you want to compare for non-zero,
d238709c 222 then please write that explicitly with `if (b != 0) …`. This helps readability
b5bd7a29
LP
223 as the value range and semantical behaviour is directly clear from the
224 condition check. As a special addition: when dealing with pointers which you
225 want to check for non-NULL-ness, you may also use downgrade-to-bool feature.
226
227- Please do not use yoda comparisons, i.e. please prefer the more readable `if
228 (a == 7)` over the less readable `if (7 == a)`.
229
c159efe3
LP
230## Destructors
231
232- The destructors always deregister the object from the next bigger object, not
233 the other way around.
234
235- For robustness reasons, destructors should be able to destruct
236 half-initialized objects, too.
237
238- When you define a destructor or `unref()` call for an object, please accept a
239 `NULL` object and simply treat this as NOP. This is similar to how libc
240 `free()` works, which accepts `NULL` pointers and becomes a NOP for them. By
241 following this scheme a lot of `if` checks can be removed before invoking
242 your destructor, which makes the code substantially more readable and robust.
243
244- Related to this: when you define a destructor or `unref()` call for an
245 object, please make it return the same type it takes and always return `NULL`
246 from it. This allows writing code like this:
247
248 ```c
249 p = foobar_unref(p);
250 ```
251
2d1b9281 252 which will always work regardless if `p` is initialized or not, and
c159efe3
LP
253 guarantees that `p` is `NULL` afterwards, all in just one line.
254
b065e1f1
LP
255## Error Handling
256
257- Error codes are returned as negative `Exxx`. e.g. `return -EINVAL`. There are
258 some exceptions: for constructors, it is OK to return `NULL` on OOM. For
259 lookup functions, `NULL` is fine too for "not found".
260
261 Be strict with this. When you write a function that can fail due to more than
262 one cause, it *really* should have an `int` as the return value for the error
263 code.
264
265- Do not bother with error checking whether writing to stdout/stderr worked.
266
267- Do not log errors from "library" code, only do so from "main program"
268 code. (With one exception: it is OK to log with DEBUG level from any code,
269 with the exception of maybe inner loops).
270
271- In public API calls, you **must** validate all your input arguments for
272 programming error with `assert_return()` and return a sensible return
273 code. In all other calls, it is recommended to check for programming errors
274 with a more brutal `assert()`. We are more forgiving to public users than for
275 ourselves! Note that `assert()` and `assert_return()` really only should be
276 used for detecting programming errors, not for runtime errors. `assert()` and
277 `assert_return()` by usage of `_likely_()` inform the compiler that he should
278 not expect these checks to fail, and they inform fellow programmers about the
279 expected validity and range of parameters.
280
281- When you invoke certain calls like `unlink()`, or `mkdir_p()` and you know it
282 is safe to ignore the error it might return (because a later call would
283 detect the failure anyway, or because the error is in an error path and you
284 thus couldn't do anything about it anyway), then make this clear by casting
285 the invocation explicitly to `(void)`. Code checks like Coverity understand
286 that, and will not complain about ignored error codes. Hence, please use
287 this:
288
289 ```c
290 (void) unlink("/foo/bar/baz");
291 ```
292
293 instead of just this:
294
295 ```c
296 unlink("/foo/bar/baz");
297 ```
298
299 Don't cast function calls to `(void)` that return no error
300 conditions. Specifically, the various `xyz_unref()` calls that return a
301 `NULL` object shouldn't be cast to `(void)`, since not using the return value
302 does not hide any errors.
303
304- When returning a return code from `main()`, please preferably use
305 `EXIT_FAILURE` and `EXIT_SUCCESS` as defined by libc.
306
96f6cfbf
LP
307## Logging
308
309- For every function you add, think about whether it is a "logging" function or
310 a "non-logging" function. "Logging" functions do logging on their own,
311 "non-logging" function never log on their own and expect their callers to
312 log. All functions in "library" code, i.e. in `src/shared/` and suchlike must
313 be "non-logging". Every time a "logging" function calls a "non-logging"
314 function, it should log about the resulting errors. If a "logging" function
315 calls another "logging" function, then it should not generate log messages,
316 so that log messages are not generated twice for the same errors.
317
318- If possible, do a combined log & return operation:
319
320 ```c
321 r = operation(...);
322 if (r < 0)
323 return log_(error|warning|notice|...)_errno(r, "Failed to ...: %m");
324 ```
325
326 If the error value is "synthetic", i.e. it was not received from
327 the called function, use `SYNTHETIC_ERRNO` wrapper to tell the logging
328 system to not log the errno value, but still return it:
329
330 ```c
331 n = read(..., s, sizeof s);
332 if (n != sizeof s)
333 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read ...");
334 ```
335
04858240
LP
336## Memory Allocation
337
338- Always check OOM. There is no excuse. In program code, you can use
339 `log_oom()` for then printing a short message, but not in "library" code.
340
341- Avoid fixed-size string buffers, unless you really know the maximum size and
342 that maximum size is small. They are a source of errors, since they possibly
343 result in truncated strings. It is often nicer to use dynamic memory,
344 `alloca()` or VLAs. If you do allocate fixed-size strings on the stack, then
345 it is probably only OK if you either use a maximum size such as `LINE_MAX`,
346 or count in detail the maximum size a string can have. (`DECIMAL_STR_MAX` and
347 `DECIMAL_STR_WIDTH` macros are your friends for this!)
348
349 Or in other words, if you use `char buf[256]` then you are likely doing
350 something wrong!
351
352- Make use of `_cleanup_free_` and friends. It makes your code much nicer to
353 read (and shorter)!
354
355- Use `alloca()`, but never forget that it is not OK to invoke `alloca()`
356 within a loop or within function call parameters. `alloca()` memory is
357 released at the end of a function, and not at the end of a `{}` block. Thus,
358 if you invoke it in a loop, you keep increasing the stack pointer without
359 ever releasing memory again. (VLAs have better behavior in this case, so
360 consider using them as an alternative.) Regarding not using `alloca()`
361 within function parameters, see the BUGS section of the `alloca(3)` man page.
362
363- If you want to concatenate two or more strings, consider using `strjoina()`
364 or `strjoin()` rather than `asprintf()`, as the latter is a lot slower. This
365 matters particularly in inner loops (but note that `strjoina()` cannot be
366 used there).
367
4467d393
LP
368## Runtime Behaviour
369
370- Avoid leaving long-running child processes around, i.e. `fork()`s that are
371 not followed quickly by an `execv()` in the child. Resource management is
372 unclear in this case, and memory CoW will result in unexpected penalties in
373 the parent much, much later on.
374
375- Don't block execution for arbitrary amounts of time using `usleep()` or a
376 similar call, unless you really know what you do. Just "giving something some
377 time", or so is a lazy excuse. Always wait for the proper event, instead of
378 doing time-based poll loops.
379
380- Whenever installing a signal handler, make sure to set `SA_RESTART` for it,
381 so that interrupted system calls are automatically restarted, and we minimize
382 hassles with handling `EINTR` (in particular as `EINTR` handling is pretty
383 broken on Linux).
384
385- When applying C-style unescaping as well as specifier expansion on the same
c90b6abc 386 string, always apply the C-style unescaping first, followed by the specifier
4467d393
LP
387 expansion. When doing the reverse, make sure to escape `%` in specifier-style
388 first (i.e. `%` → `%%`), and then do C-style escaping where necessary.
389
390- Be exceptionally careful when formatting and parsing floating point
391 numbers. Their syntax is locale dependent (i.e. `5.000` in en_US is generally
392 understood as 5, while in de_DE as 5000.).
393
394- Make sure to enforce limits on every user controllable resource. If the user
395 can allocate resources in your code, your code must enforce some form of
396 limits after which it will refuse operation. It's fine if it is hard-coded
397 (at least initially), but it needs to be there. This is particularly
398 important for objects that unprivileged users may allocate, but also matters
399 for everything else any user may allocated.
400
f42c1cd4
LP
401## Types
402
403- Think about the types you use. If a value cannot sensibly be negative, do not
404 use `int`, but use `unsigned`.
405
406- Use `char` only for actual characters. Use `uint8_t` or `int8_t` when you
407 actually mean a byte-sized signed or unsigned integers. When referring to a
408 generic byte, we generally prefer the unsigned variant `uint8_t`. Do not use
409 types based on `short`. They *never* make sense. Use `int`, `long`, `long
410 long`, all in unsigned and signed fashion, and the fixed-size types
411 `uint8_t`, `uint16_t`, `uint32_t`, `uint64_t`, `int8_t`, `int16_t`, `int32_t`
412 and so on, as well as `size_t`, but nothing else. Do not use kernel types
413 like `u32` and so on, leave that to the kernel.
414
415- Stay uniform. For example, always use `usec_t` for time values. Do not mix
416 `usec` and `msec`, and `usec` and whatnot.
417
418- Never use the `off_t` type, and particularly avoid it in public APIs. It's
419 really weirdly defined, as it usually is 64-bit and we don't support it any
420 other way, but it could in theory also be 32-bit. Which one it is depends on
421 a compiler switch chosen by the compiled program, which hence corrupts APIs
422 using it unless they can also follow the program's choice. Moreover, in
423 systemd we should parse values the same way on all architectures and cannot
424 expose `off_t` values over D-Bus. To avoid any confusion regarding conversion
425 and ABIs, always use simply `uint64_t` directly.
426
427- Unless you allocate an array, `double` is always a better choice than
428 `float`. Processors speak `double` natively anyway, so there is no speed
429 benefit, and on calls like `printf()` `float`s get promoted to `double`s
430 anyway, so there is no point.
431
432- Use the bool type for booleans, not integers. One exception: in public
433 headers (i.e those in `src/systemd/sd-*.h`) use integers after all, as `bool`
434 is C99 and in our public APIs we try to stick to C89 (with a few extension).
435
3b75e079
LP
436## Deadlocks
437
38b38500 438- Do not issue NSS requests (that includes user name and hostname lookups)
3b75e079
LP
439 from PID 1 as this might trigger deadlocks when those lookups involve
440 synchronously talking to services that we would need to start up.
441
442- Do not synchronously talk to any other service from PID 1, due to risk of
443 deadlocks.
444
25553cd9
LP
445## File Descriptors
446
447- When you allocate a file descriptor, it should be made `O_CLOEXEC` right from
448 the beginning, as none of our files should leak to forked binaries by
449 default. Hence, whenever you open a file, `O_CLOEXEC` must be specified,
450 right from the beginning. This also applies to sockets. Effectively, this
451 means that all invocations to:
452
453 - `open()` must get `O_CLOEXEC` passed,
454 - `socket()` and `socketpair()` must get `SOCK_CLOEXEC` passed,
455 - `recvmsg()` must get `MSG_CMSG_CLOEXEC` set,
456 - `F_DUPFD_CLOEXEC` should be used instead of `F_DUPFD`, and so on,
457 - invocations of `fopen()` should take `e`.
458
459- It's a good idea to use `O_NONBLOCK` when opening 'foreign' regular files,
460 i.e. file system objects that are supposed to be regular files whose paths
461 where specified by the user and hence might actually refer to other types of
462 file system objects. This is a good idea so that we don't end up blocking on
463 'strange' file nodes, for example if the user pointed us to a FIFO or device
464 node which may block when opening. Moreover even for actual regular files
465 `O_NONBLOCK` has a benefit: it bypasses any mandatory lock that might be in
466 effect on the regular file. If in doubt consider turning off `O_NONBLOCK`
467 again after opening.
468
996f119d
LP
469## Command Line
470
471- If you parse a command line, and want to store the parsed parameters in
472 global variables, please consider prefixing their names with `arg_`. We have
473 been following this naming rule in most of our tools, and we should continue
474 to do so, as it makes it easy to identify command line parameter variables,
475 and makes it clear why it is OK that they are global variables.
476
477- Command line option parsing:
478 - Do not print full `help()` on error, be specific about the error.
479 - Do not print messages to stdout on error.
480 - Do not POSIX_ME_HARDER unless necessary, i.e. avoid `+` in option string.
481
56380761
LP
482## Exporting Symbols
483
484- Variables and functions **must** be static, unless they have a prototype, and
485 are supposed to be exported.
486
487- Public API calls (i.e. functions exported by our shared libraries)
488 must be marked `_public_` and need to be prefixed with `sd_`. No
489 other functions should be prefixed like that.
490
491- When exposing public C APIs, be careful what function parameters you make
492 `const`. For example, a parameter taking a context object should probably not
493 be `const`, even if you are writing an otherwise read-only accessor function
494 for it. The reason is that making it `const` fixates the contract that your
495 call won't alter the object ever, as part of the API. However, that's often
496 quite a promise, given that this even prohibits object-internal caching or
497 lazy initialization of object variables. Moreover, it's usually not too
498 useful for client applications. Hence, please be careful and avoid `const` on
499 object parameters, unless you are very sure `const` is appropriate.
500
971dfffa
LP
501## Referencing Concepts
502
82143987
FA
503- When referring to a configuration file option in the documentation and such,
504 please always suffix it with `=`, to indicate that it is a configuration file
505 setting.
506
507- When referring to a command line option in the documentation and such, please
508 always prefix with `--` or `-` (as appropriate), to indicate that it is a
509 command line option.
510
511- When referring to a file system path that is a directory, please always
512 suffix it with `/`, to indicate that it is a directory, not a regular file
513 (or other file system object).
514
2d0dce2a
LP
515## Functions to Avoid
516
517- Use `memzero()` or even better `zero()` instead of `memset(..., 0, ...)`
518
519- Please use `streq()` and `strneq()` instead of `strcmp()`, `strncmp()` where
520 applicable (i.e. wherever you just care about equality/inequality, not about
521 the sorting order).
522
523- Never use `strtol()`, `atoi()` and similar calls. Use `safe_atoli()`,
524 `safe_atou32()` and suchlike instead. They are much nicer to use in most
525 cases and correctly check for parsing errors.
526
527- `htonl()`/`ntohl()` and `htons()`/`ntohs()` are weird. Please use `htobe32()`
528 and `htobe16()` instead, it's much more descriptive, and actually says what
529 really is happening, after all `htonl()` and `htons()` don't operate on
530 `long`s and `short`s as their name would suggest, but on `uint32_t` and
531 `uint16_t`. Also, "network byte order" is just a weird name for "big endian",
532 hence we might want to call it "big endian" right-away.
533
534- Please never use `dup()`. Use `fcntl(fd, F_DUPFD_CLOEXEC, 3)` instead. For
c1495f8e 535 two reasons: first, you want `O_CLOEXEC` set on the new `fd` (see
2d0dce2a
LP
536 above). Second, `dup()` will happily duplicate your `fd` as 0, 1, 2,
537 i.e. stdin, stdout, stderr, should those `fd`s be closed. Given the special
538 semantics of those `fd`s, it's probably a good idea to avoid
539 them. `F_DUPFD_CLOEXEC` with `3` as parameter avoids them.
540
82143987
FA
541- Don't use `fgets()`, it's too hard to properly handle errors such as overly
542 long lines. Use `read_line()` instead, which is our own function that handles
c90b6abc 543 this much more nicely.
2d0dce2a
LP
544
545- Don't invoke `exit()`, ever. It is not replacement for proper error
546 handling. Please escalate errors up your call chain, and use normal `return`
547 to exit from the main function of a process. If you `fork()`ed off a child
548 process, please use `_exit()` instead of `exit()`, so that the exit handlers
549 are not run.
550
551- We never use the POSIX version of `basename()` (which glibc defines it in
552 `libgen.h`), only the GNU version (which glibc defines in `string.h`). The
553 only reason to include `libgen.h` is because `dirname()` is needed. Every
554 time you need that please immediately undefine `basename()`, and add a
555 comment about it, so that no code ever ends up using the POSIX version!
831781b9 556
ff2c2d08 557## Committing to git
831781b9
LP
558
559- Commit message subject lines should be prefixed with an appropriate component
560 name of some kind. For example "journal: ", "nspawn: " and so on.
561
562- Do not use "Signed-Off-By:" in your commit messages. That's a kernel thing we
563 don't do in the systemd project.