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