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