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