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