]>
Commit | Line | Data |
---|---|---|
c3e270f4 FB |
1 | --- |
2 | title: Coding Style | |
4cdca0af | 3 | category: Contributing |
b41a3f66 | 4 | layout: default |
0aff7b75 | 5 | SPDX-License-Identifier: LGPL-2.1-or-later |
c3e270f4 FB |
6 | --- |
7 | ||
c1d3483d | 8 | # Coding Style |
82143987 | 9 | |
8c9289e7 LP |
10 | ## Formatting |
11 | ||
3b69b18f ZJS |
12 | - 8ch indent, no tabs, except for files in `man/` which are 2ch indent, and |
13 | still no tabs, and shell scripts, which are 4ch indent, and no tabs either. | |
82143987 | 14 | |
8c9289e7 LP |
15 | - We prefer `/* comments */` over `// comments` in code you commit, |
16 | please. This way `// comments` are left for developers to use for local, | |
17 | temporary commenting of code for debug purposes (i.e. uncommittable stuff), | |
18 | making such comments easily discernible from explanatory, documenting code | |
19 | comments (i.e. committable stuff). | |
82143987 FA |
20 | |
21 | - Don't break code lines too eagerly. We do **not** force line breaks at 80ch, | |
22 | all of today's screens should be much larger than that. But then again, don't | |
c90ee834 | 23 | overdo it, ~109ch should be enough really. The `.editorconfig`, `.vimrc` and |
82143987 | 24 | `.dir-locals.el` files contained in the repository will set this limit up for |
3b69b18f ZJS |
25 | you automatically, if you let them (as well as a few other things). Please |
26 | note that emacs loads `.dir-locals.el` automatically, but vim needs to be | |
27 | configured to load `.vimrc`, see that file for instructions. | |
82143987 | 28 | |
7489ccc3 LB |
29 | - If you break a function declaration over multiple lines, do it like this: |
30 | ||
31 | ```c | |
32 | void some_function( | |
33 | int foo, | |
34 | bool bar, | |
35 | char baz) { | |
36 | ||
37 | int a, b, c; | |
38 | ``` | |
39 | ||
cf33b707 LP |
40 | (i.e. use double indentation — 16 spaces — for the parameter list.) |
41 | ||
8c9289e7 LP |
42 | - Try to write this: |
43 | ||
44 | ```c | |
45 | void foo() { | |
46 | } | |
47 | ``` | |
48 | ||
49 | instead of this: | |
50 | ||
51 | ```c | |
52 | void foo() | |
53 | { | |
54 | } | |
55 | ``` | |
56 | ||
57 | - Single-line `if` blocks should not be enclosed in `{}`. Write this: | |
58 | ||
59 | ```c | |
60 | if (foobar) | |
61 | waldo(); | |
62 | ``` | |
63 | ||
64 | instead of this: | |
65 | ||
66 | ```c | |
67 | if (foobar) { | |
68 | waldo(); | |
69 | } | |
70 | ``` | |
71 | ||
72 | - Do not write `foo ()`, write `foo()`. | |
e8a587dc | 73 | |
4dbad977 AW |
74 | - `else` blocks should generally start on the same line as the closing `}`: |
75 | ```c | |
76 | if (foobar) { | |
77 | find(); | |
78 | waldo(); | |
79 | } else | |
80 | dont_find_waldo(); | |
81 | ``` | |
8c9289e7 | 82 | |
e28770e3 LP |
83 | - Please define flags types like this: |
84 | ||
85 | ```c | |
86 | typedef enum FoobarFlags { | |
87 | FOOBAR_QUUX = 1 << 0, | |
88 | FOOBAR_WALDO = 1 << 1, | |
89 | FOOBAR_XOXO = 1 << 2, | |
90 | … | |
91 | } FoobarFlags; | |
92 | ``` | |
93 | ||
94 | i.e. use an enum for it, if possible. Indicate bit values via `1 <<` | |
95 | expressions, and align them vertically. Define both an enum and a type for | |
96 | it. | |
97 | ||
98 | - If you define (non-flags) enums, follow this template: | |
99 | ||
100 | ```c | |
101 | typedef enum FoobarMode { | |
102 | FOOBAR_AAA, | |
103 | FOOBAR_BBB, | |
104 | FOOBAR_CCC, | |
105 | … | |
106 | _FOOBAR_MAX, | |
107 | _FOOBAR_INVALID = -EINVAL, | |
108 | } FoobarMode; | |
109 | ``` | |
110 | ||
111 | i.e. define a `_MAX` enum for the largest defined enum value, plus one. Since | |
112 | this is not a regular enum value, prefix it with `_`. Also, define a special | |
113 | "invalid" enum value, and set it to `-EINVAL`. That way the enum type can | |
114 | safely be used to propagate conversion errors. | |
115 | ||
116 | - If you define an enum in a public API, be extra careful, as the size of the | |
117 | enum might change when new values are added, which would break ABI | |
118 | compatibility. Since we typically want to allow adding new enum values to an | |
119 | existing enum type with later API versions, please use the | |
120 | `_SD_ENUM_FORCE_S64()` macro in the enum definition, which forces the size of | |
121 | the enum to be signed 64bit wide. | |
122 | ||
f591cf66 LP |
123 | - Empty lines to separate code blocks are a good thing, please add them |
124 | abundantly. However, please stick to one at a time, i.e. multiple empty lines | |
1e8f5f79 ZJS |
125 | immediately following each other are not OK. Also, we try to keep function |
126 | calls and their immediate error handling together. Hence: | |
f591cf66 LP |
127 | |
128 | ```c | |
129 | /* → empty line here is good */ | |
130 | r = some_function(…); | |
131 | /* → empty line here would be bad */ | |
132 | if (r < 0) | |
133 | return log_error_errno(r, "Some function failed: %m"); | |
134 | /* → empty line here is good */ | |
1e8f5f79 ZJS |
135 | |
136 | - In shell scripts, do not use whitespace after the redirection operator | |
137 | (`>some/file` instead of `> some/file`, `<<EOF` instead of `<< EOF`). | |
f591cf66 | 138 | |
b4f12824 | 139 | ## Code Organization and Semantics |
8c9289e7 | 140 | |
40f55f69 LP |
141 | - For our codebase we intend to use ISO C11 *with* GNU extensions (aka |
142 | "gnu11"). Public APIs (i.e. those we expose via `libsystemd.so` | |
143 | i.e. `systemd/sd-*.h`) should only use ISO C89 however (with a very limited | |
144 | set of conservative and common extensions, such as fixed size integer types | |
145 | from `<inttypes.h>`), so that we don't force consuming programs into C11 | |
146 | mode. (This discrepancy in particular means one thing: internally we use C99 | |
147 | `bool` booleans, externally C89-compatible `int` booleans which generally | |
148 | have different size in memory and slightly different semantics, also see | |
149 | below.) Both for internal and external code it's OK to use even newer | |
150 | features and GCC extension than "gnu11", as long as there's reasonable | |
151 | fallback #ifdeffery in place to ensure compatibility is retained with older | |
152 | compilers. | |
153 | ||
78e5b4d7 LP |
154 | - Please name structures in `PascalCase` (with exceptions, such as public API |
155 | structs), variables and functions in `snake_case`. | |
82143987 | 156 | |
b4f12824 LP |
157 | - Avoid static variables, except for caches and very few other cases. Think |
158 | about thread-safety! While most of our code is never used in threaded | |
159 | environments, at least the library code should make sure it works correctly | |
160 | in them. Instead of doing a lot of locking for that, we tend to prefer using | |
161 | TLS to do per-thread caching (which only works for small, fixed-size cache | |
162 | objects), or we disable caching for any thread that is not the main | |
163 | thread. Use `is_main_thread()` to detect whether the calling thread is the | |
164 | main thread. | |
82143987 | 165 | |
82143987 | 166 | - Do not write functions that clobber call-by-reference variables on |
b4f12824 | 167 | failure. Use temporary variables for these cases and change the passed in |
cf33b707 LP |
168 | variables only on success. The rule is: never clobber return parameters on |
169 | failure, always initialize return parameters on success. | |
170 | ||
171 | - Typically, function parameters fit into three categories: input parameters, | |
172 | mutable objects, and call-by-reference return parameters. Input parameters | |
173 | should always carry suitable "const" declarators if they are pointers, to | |
174 | indicate they are input-only and not changed by the function. Return | |
175 | parameters are best prefixed with "ret_", to clarify they are return | |
176 | parameters. (Conversely, please do not prefix parameters that aren't | |
177 | output-only with "ret_", in particular not mutable parameters that are both | |
178 | input as well as output). Example: | |
179 | ||
180 | ```c | |
181 | static int foobar_frobnicate( | |
182 | Foobar* object, /* the associated mutable object */ | |
183 | const char *input, /* immutable input parameter */ | |
184 | char **ret_frobnicated) { /* return parameter */ | |
185 | … | |
186 | return 0; | |
187 | } | |
188 | ``` | |
82143987 | 189 | |
82143987 | 190 | - The order in which header files are included doesn't matter too |
b4f12824 LP |
191 | much. systemd-internal headers must not rely on an include order, so it is |
192 | safe to include them in any order possible. However, to not clutter global | |
193 | includes, and to make sure internal definitions will not affect global | |
194 | headers, please always include the headers of external components first | |
195 | (these are all headers enclosed in <>), followed by our own exported headers | |
196 | (usually everything that's prefixed by `sd-`), and then followed by internal | |
197 | headers. Furthermore, in all three groups, order all includes alphabetically | |
82143987 FA |
198 | so duplicate includes can easily be detected. |
199 | ||
b4f12824 LP |
200 | - Please avoid using global variables as much as you can. And if you do use |
201 | them make sure they are static at least, instead of exported. Especially in | |
202 | library-like code it is important to avoid global variables. Why are global | |
203 | variables bad? They usually hinder generic reusability of code (since they | |
204 | break in threaded programs, and usually would require locking there), and as | |
205 | the code using them has side-effects make programs non-transparent. That | |
206 | said, there are many cases where they explicitly make a lot of sense, and are | |
207 | OK to use. For example, the log level and target in `log.c` is stored in a | |
208 | global variable, and that's OK and probably expected by most. Also in many | |
209 | cases we cache data in global variables. If you add more caches like this, | |
210 | please be careful however, and think about threading. Only use static | |
211 | variables if you are sure that thread-safety doesn't matter in your | |
212 | case. Alternatively, consider using TLS, which is pretty easy to use with | |
213 | gcc's `thread_local` concept. It's also OK to store data that is inherently | |
e347d53a | 214 | global in global variables, for example, data parsed from command lines, see |
82143987 FA |
215 | below. |
216 | ||
82143987 FA |
217 | - Our focus is on the GNU libc (glibc), not any other libcs. If other libcs are |
218 | incompatible with glibc it's on them. However, if there are equivalent POSIX | |
219 | and Linux/GNU-specific APIs, we generally prefer the POSIX APIs. If there | |
220 | aren't, we are happy to use GNU or Linux APIs, and expect non-GNU | |
221 | implementations of libc to catch up with glibc. | |
222 | ||
78e5b4d7 LP |
223 | ## Using C Constructs |
224 | ||
756755d0 | 225 | - Allocate local variables where it makes sense: at the top of the block, or at |
9214f299 ZJS |
226 | the point where they can be initialized. Avoid huge variable declaration |
227 | lists at the top of the function. | |
228 | ||
f591cf66 LP |
229 | As an exception, `int r` is typically used for a local state variable, but |
230 | should almost always be declared as the last variable at the top of the | |
231 | function. | |
78e5b4d7 LP |
232 | |
233 | ```c | |
234 | { | |
9214f299 | 235 | uint64_t a; |
756755d0 | 236 | int r; |
78e5b4d7 | 237 | |
9214f299 ZJS |
238 | r = frobnicate(&a); |
239 | if (r < 0) | |
240 | … | |
241 | ||
242 | uint64_t b = a + 1, c; | |
756755d0 | 243 | |
9214f299 | 244 | r = foobarify(a, b, &c); |
756755d0 ZJS |
245 | if (r < 0) |
246 | … | |
9214f299 ZJS |
247 | |
248 | const char *pretty = prettify(a, b, c); | |
249 | … | |
78e5b4d7 LP |
250 | } |
251 | ``` | |
252 | ||
9214f299 ZJS |
253 | - Do not mix multiple variable definitions with function invocations or |
254 | complicated expressions: | |
78e5b4d7 LP |
255 | |
256 | ```c | |
257 | { | |
78e5b4d7 | 258 | uint64_t x = 7; |
756755d0 ZJS |
259 | int a; |
260 | ||
261 | a = foobar(); | |
78e5b4d7 LP |
262 | } |
263 | ``` | |
264 | ||
756755d0 | 265 | instead of: |
78e5b4d7 LP |
266 | |
267 | ```c | |
268 | { | |
756755d0 | 269 | int a = foobar(); |
78e5b4d7 | 270 | uint64_t x = 7; |
78e5b4d7 LP |
271 | } |
272 | ``` | |
273 | ||
756755d0 | 274 | - Use `goto` for cleaning up, and only use it for that. I.e. you may only jump |
78e5b4d7 LP |
275 | to the end of a function, and little else. Never jump backwards! |
276 | ||
277 | - To minimize strict aliasing violations, we prefer unions over casting. | |
278 | ||
279 | - Instead of using `memzero()`/`memset()` to initialize structs allocated on | |
280 | the stack, please try to use c99 structure initializers. It's short, prettier | |
281 | and actually even faster at execution. Hence: | |
282 | ||
283 | ```c | |
284 | struct foobar t = { | |
285 | .foo = 7, | |
286 | .bar = "bazz", | |
287 | }; | |
288 | ``` | |
289 | ||
290 | instead of: | |
291 | ||
292 | ```c | |
293 | struct foobar t; | |
294 | zero(t); | |
295 | t.foo = 7; | |
296 | t.bar = "bazz"; | |
297 | ``` | |
298 | ||
299 | - To implement an endless loop, use `for (;;)` rather than `while (1)`. The | |
300 | latter is a bit ugly anyway, since you probably really meant `while | |
301 | (true)`. To avoid the discussion what the right always-true expression for an | |
302 | infinite while loop is, our recommendation is to simply write it without any | |
303 | such expression by using `for (;;)`. | |
304 | ||
305 | - To determine the length of a constant string `"foo"`, don't bother with | |
306 | `sizeof("foo")-1`, please use `strlen()` instead (both gcc and clang optimize | |
307 | the call away for fixed strings). The only exception is when declaring an | |
9214f299 | 308 | array. In that case use `STRLEN()`, which evaluates to a static constant and |
78e5b4d7 LP |
309 | doesn't force the compiler to create a VLA. |
310 | ||
b5bd7a29 LP |
311 | - Please use C's downgrade-to-bool feature only for expressions that are |
312 | actually booleans (or "boolean-like"), and not for variables that are really | |
313 | numeric. Specifically, if you have an `int b` and it's only used in a boolean | |
314 | sense, by all means check its state with `if (b) …` — but if `b` can actually | |
315 | have more than two semantic values, and you want to compare for non-zero, | |
d238709c | 316 | then please write that explicitly with `if (b != 0) …`. This helps readability |
b5bd7a29 LP |
317 | as the value range and semantical behaviour is directly clear from the |
318 | condition check. As a special addition: when dealing with pointers which you | |
319 | want to check for non-NULL-ness, you may also use downgrade-to-bool feature. | |
320 | ||
321 | - Please do not use yoda comparisons, i.e. please prefer the more readable `if | |
322 | (a == 7)` over the less readable `if (7 == a)`. | |
323 | ||
c159efe3 LP |
324 | ## Destructors |
325 | ||
326 | - The destructors always deregister the object from the next bigger object, not | |
327 | the other way around. | |
328 | ||
329 | - For robustness reasons, destructors should be able to destruct | |
330 | half-initialized objects, too. | |
331 | ||
332 | - When you define a destructor or `unref()` call for an object, please accept a | |
333 | `NULL` object and simply treat this as NOP. This is similar to how libc | |
334 | `free()` works, which accepts `NULL` pointers and becomes a NOP for them. By | |
335 | following this scheme a lot of `if` checks can be removed before invoking | |
336 | your destructor, which makes the code substantially more readable and robust. | |
337 | ||
338 | - Related to this: when you define a destructor or `unref()` call for an | |
339 | object, please make it return the same type it takes and always return `NULL` | |
340 | from it. This allows writing code like this: | |
341 | ||
342 | ```c | |
343 | p = foobar_unref(p); | |
344 | ``` | |
345 | ||
2d1b9281 | 346 | which will always work regardless if `p` is initialized or not, and |
c159efe3 LP |
347 | guarantees that `p` is `NULL` afterwards, all in just one line. |
348 | ||
e8a587dc LP |
349 | ## Common Function Naming |
350 | ||
351 | - Name destructor functions that destroy an object in full freeing all its | |
352 | memory and associated resources (and thus invalidating the pointer to it) | |
353 | `xyz_free()`. Example: `strv_free()`. | |
354 | ||
355 | - Name destructor functions that destroy only the referenced content of an | |
356 | object but leave the object itself allocated `xyz_done()`. If it resets all | |
357 | fields so that the object can be reused later call it `xyz_clear()`. | |
358 | ||
359 | - Functions that decrease the reference counter of an object by one should be | |
360 | called `xyz_unref()`. Example: `json_variant_unref()`. Functions that | |
361 | increase the reference counter by one should be called `xyz_ref()`. Example: | |
362 | `json_variant_ref()` | |
363 | ||
b065e1f1 LP |
364 | ## Error Handling |
365 | ||
366 | - Error codes are returned as negative `Exxx`. e.g. `return -EINVAL`. There are | |
367 | some exceptions: for constructors, it is OK to return `NULL` on OOM. For | |
368 | lookup functions, `NULL` is fine too for "not found". | |
369 | ||
370 | Be strict with this. When you write a function that can fail due to more than | |
371 | one cause, it *really* should have an `int` as the return value for the error | |
372 | code. | |
373 | ||
947796ea LP |
374 | - libc system calls typically return -1 on error (with the error code in |
375 | `errno`), and >= 0 on success. Use the RET_NERRNO() helper if you are looking | |
376 | for a simple way to convert this libc style error returning into systemd | |
377 | style error returning. e.g. | |
378 | ||
379 | ```c | |
380 | … | |
381 | r = RET_NERRNO(unlink(t)); | |
382 | … | |
383 | ``` | |
384 | ||
385 | or | |
386 | ||
387 | ```c | |
388 | … | |
389 | r = RET_NERRNO(open("/some/file", O_RDONLY|O_CLOEXEC)); | |
390 | … | |
391 | ``` | |
392 | ||
b065e1f1 LP |
393 | - Do not bother with error checking whether writing to stdout/stderr worked. |
394 | ||
395 | - Do not log errors from "library" code, only do so from "main program" | |
396 | code. (With one exception: it is OK to log with DEBUG level from any code, | |
397 | with the exception of maybe inner loops). | |
398 | ||
399 | - In public API calls, you **must** validate all your input arguments for | |
400 | programming error with `assert_return()` and return a sensible return | |
401 | code. In all other calls, it is recommended to check for programming errors | |
402 | with a more brutal `assert()`. We are more forgiving to public users than for | |
403 | ourselves! Note that `assert()` and `assert_return()` really only should be | |
404 | used for detecting programming errors, not for runtime errors. `assert()` and | |
be7148eb | 405 | `assert_return()` by usage of `_likely_()` inform the compiler that it should |
b065e1f1 LP |
406 | not expect these checks to fail, and they inform fellow programmers about the |
407 | expected validity and range of parameters. | |
408 | ||
409 | - When you invoke certain calls like `unlink()`, or `mkdir_p()` and you know it | |
410 | is safe to ignore the error it might return (because a later call would | |
411 | detect the failure anyway, or because the error is in an error path and you | |
412 | thus couldn't do anything about it anyway), then make this clear by casting | |
413 | the invocation explicitly to `(void)`. Code checks like Coverity understand | |
414 | that, and will not complain about ignored error codes. Hence, please use | |
415 | this: | |
416 | ||
417 | ```c | |
418 | (void) unlink("/foo/bar/baz"); | |
419 | ``` | |
420 | ||
421 | instead of just this: | |
422 | ||
423 | ```c | |
424 | unlink("/foo/bar/baz"); | |
425 | ``` | |
426 | ||
800d0802 AZ |
427 | When returning from a `void` function, you may also want to shorten the error |
428 | path boilerplate by returning a function invocation cast to `(void)` like so: | |
429 | ||
430 | ```c | |
431 | if (condition_not_met) | |
432 | return (void) log_tests_skipped("Cannot run ..."); | |
433 | ``` | |
434 | ||
b065e1f1 LP |
435 | Don't cast function calls to `(void)` that return no error |
436 | conditions. Specifically, the various `xyz_unref()` calls that return a | |
437 | `NULL` object shouldn't be cast to `(void)`, since not using the return value | |
438 | does not hide any errors. | |
439 | ||
440 | - When returning a return code from `main()`, please preferably use | |
441 | `EXIT_FAILURE` and `EXIT_SUCCESS` as defined by libc. | |
442 | ||
96f6cfbf LP |
443 | ## Logging |
444 | ||
445 | - For every function you add, think about whether it is a "logging" function or | |
cf33b707 | 446 | a "non-logging" function. "Logging" functions do (non-debug) logging on their |
f223fd6a | 447 | own, "non-logging" functions never log on their own (except at debug level) |
cf33b707 LP |
448 | and expect their callers to log. All functions in "library" code, i.e. in |
449 | `src/shared/` and suchlike must be "non-logging". Every time a "logging" | |
450 | function calls a "non-logging" function, it should log about the resulting | |
451 | errors. If a "logging" function calls another "logging" function, then it | |
452 | should not generate log messages, so that log messages are not generated | |
453 | twice for the same errors. (Note that debug level logging — at syslog level | |
454 | `LOG_DEBUG` — is not considered logging in this context, debug logging is | |
455 | generally always fine and welcome.) | |
96f6cfbf LP |
456 | |
457 | - If possible, do a combined log & return operation: | |
458 | ||
459 | ```c | |
460 | r = operation(...); | |
461 | if (r < 0) | |
462 | return log_(error|warning|notice|...)_errno(r, "Failed to ...: %m"); | |
463 | ``` | |
464 | ||
465 | If the error value is "synthetic", i.e. it was not received from | |
466 | the called function, use `SYNTHETIC_ERRNO` wrapper to tell the logging | |
467 | system to not log the errno value, but still return it: | |
468 | ||
469 | ```c | |
470 | n = read(..., s, sizeof s); | |
471 | if (n != sizeof s) | |
472 | return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read ..."); | |
473 | ``` | |
474 | ||
04858240 LP |
475 | ## Memory Allocation |
476 | ||
477 | - Always check OOM. There is no excuse. In program code, you can use | |
478 | `log_oom()` for then printing a short message, but not in "library" code. | |
479 | ||
480 | - Avoid fixed-size string buffers, unless you really know the maximum size and | |
756755d0 | 481 | that maximum size is small. It is often nicer to use dynamic memory, |
e3bde912 LP |
482 | `alloca_safe()` or VLAs. If you do allocate fixed-size strings on the stack, |
483 | then it is probably only OK if you either use a maximum size such as | |
484 | `LINE_MAX`, or count in detail the maximum size a string can | |
485 | have. (`DECIMAL_STR_MAX` and `DECIMAL_STR_WIDTH` macros are your friends for | |
486 | this!) | |
04858240 LP |
487 | |
488 | Or in other words, if you use `char buf[256]` then you are likely doing | |
489 | something wrong! | |
490 | ||
491 | - Make use of `_cleanup_free_` and friends. It makes your code much nicer to | |
492 | read (and shorter)! | |
493 | ||
e3bde912 LP |
494 | - Do not use `alloca()`, `strdupa()` or `strndupa()` directly. Use |
495 | `alloca_safe()`, `strdupa_safe()` or `strndupa_safe()` instead. (The | |
496 | difference is that the latter include an assertion that the specified size is | |
497 | below a safety threshold, so that the program rather aborts than runs into | |
498 | possible stack overruns.) | |
499 | ||
500 | - Use `alloca_safe()`, but never forget that it is not OK to invoke | |
501 | `alloca_safe()` within a loop or within function call | |
502 | parameters. `alloca_safe()` memory is released at the end of a function, and | |
503 | not at the end of a `{}` block. Thus, if you invoke it in a loop, you keep | |
504 | increasing the stack pointer without ever releasing memory again. (VLAs have | |
505 | better behavior in this case, so consider using them as an alternative.) | |
506 | Regarding not using `alloca_safe()` within function parameters, see the BUGS | |
507 | section of the `alloca(3)` man page. | |
04858240 LP |
508 | |
509 | - If you want to concatenate two or more strings, consider using `strjoina()` | |
510 | or `strjoin()` rather than `asprintf()`, as the latter is a lot slower. This | |
511 | matters particularly in inner loops (but note that `strjoina()` cannot be | |
512 | used there). | |
513 | ||
4467d393 LP |
514 | ## Runtime Behaviour |
515 | ||
516 | - Avoid leaving long-running child processes around, i.e. `fork()`s that are | |
517 | not followed quickly by an `execv()` in the child. Resource management is | |
518 | unclear in this case, and memory CoW will result in unexpected penalties in | |
519 | the parent much, much later on. | |
520 | ||
521 | - Don't block execution for arbitrary amounts of time using `usleep()` or a | |
522 | similar call, unless you really know what you do. Just "giving something some | |
523 | time", or so is a lazy excuse. Always wait for the proper event, instead of | |
524 | doing time-based poll loops. | |
525 | ||
526 | - Whenever installing a signal handler, make sure to set `SA_RESTART` for it, | |
527 | so that interrupted system calls are automatically restarted, and we minimize | |
528 | hassles with handling `EINTR` (in particular as `EINTR` handling is pretty | |
529 | broken on Linux). | |
530 | ||
531 | - When applying C-style unescaping as well as specifier expansion on the same | |
c90b6abc | 532 | string, always apply the C-style unescaping first, followed by the specifier |
4467d393 LP |
533 | expansion. When doing the reverse, make sure to escape `%` in specifier-style |
534 | first (i.e. `%` → `%%`), and then do C-style escaping where necessary. | |
535 | ||
536 | - Be exceptionally careful when formatting and parsing floating point | |
537 | numbers. Their syntax is locale dependent (i.e. `5.000` in en_US is generally | |
538 | understood as 5, while in de_DE as 5000.). | |
539 | ||
540 | - Make sure to enforce limits on every user controllable resource. If the user | |
541 | can allocate resources in your code, your code must enforce some form of | |
542 | limits after which it will refuse operation. It's fine if it is hard-coded | |
543 | (at least initially), but it needs to be there. This is particularly | |
544 | important for objects that unprivileged users may allocate, but also matters | |
6ae11e12 | 545 | for everything else any user may allocate. |
4467d393 | 546 | |
f42c1cd4 LP |
547 | ## Types |
548 | ||
549 | - Think about the types you use. If a value cannot sensibly be negative, do not | |
ba1ca5ef | 550 | use `int`, but use `unsigned`. We prefer `unsigned` form to `unsigned int`. |
f42c1cd4 LP |
551 | |
552 | - Use `char` only for actual characters. Use `uint8_t` or `int8_t` when you | |
553 | actually mean a byte-sized signed or unsigned integers. When referring to a | |
554 | generic byte, we generally prefer the unsigned variant `uint8_t`. Do not use | |
555 | types based on `short`. They *never* make sense. Use `int`, `long`, `long | |
556 | long`, all in unsigned and signed fashion, and the fixed-size types | |
557 | `uint8_t`, `uint16_t`, `uint32_t`, `uint64_t`, `int8_t`, `int16_t`, `int32_t` | |
558 | and so on, as well as `size_t`, but nothing else. Do not use kernel types | |
559 | like `u32` and so on, leave that to the kernel. | |
560 | ||
561 | - Stay uniform. For example, always use `usec_t` for time values. Do not mix | |
562 | `usec` and `msec`, and `usec` and whatnot. | |
563 | ||
564 | - Never use the `off_t` type, and particularly avoid it in public APIs. It's | |
565 | really weirdly defined, as it usually is 64-bit and we don't support it any | |
566 | other way, but it could in theory also be 32-bit. Which one it is depends on | |
567 | a compiler switch chosen by the compiled program, which hence corrupts APIs | |
568 | using it unless they can also follow the program's choice. Moreover, in | |
569 | systemd we should parse values the same way on all architectures and cannot | |
570 | expose `off_t` values over D-Bus. To avoid any confusion regarding conversion | |
571 | and ABIs, always use simply `uint64_t` directly. | |
572 | ||
573 | - Unless you allocate an array, `double` is always a better choice than | |
574 | `float`. Processors speak `double` natively anyway, so there is no speed | |
575 | benefit, and on calls like `printf()` `float`s get promoted to `double`s | |
576 | anyway, so there is no point. | |
577 | ||
578 | - Use the bool type for booleans, not integers. One exception: in public | |
579 | headers (i.e those in `src/systemd/sd-*.h`) use integers after all, as `bool` | |
40f55f69 LP |
580 | is C99 and in our public APIs we try to stick to C89 (with a few extensions; |
581 | also see above). | |
f42c1cd4 | 582 | |
3b75e079 LP |
583 | ## Deadlocks |
584 | ||
38b38500 | 585 | - Do not issue NSS requests (that includes user name and hostname lookups) |
3b75e079 LP |
586 | from PID 1 as this might trigger deadlocks when those lookups involve |
587 | synchronously talking to services that we would need to start up. | |
588 | ||
589 | - Do not synchronously talk to any other service from PID 1, due to risk of | |
590 | deadlocks. | |
591 | ||
25553cd9 LP |
592 | ## File Descriptors |
593 | ||
594 | - When you allocate a file descriptor, it should be made `O_CLOEXEC` right from | |
595 | the beginning, as none of our files should leak to forked binaries by | |
596 | default. Hence, whenever you open a file, `O_CLOEXEC` must be specified, | |
597 | right from the beginning. This also applies to sockets. Effectively, this | |
598 | means that all invocations to: | |
599 | ||
600 | - `open()` must get `O_CLOEXEC` passed, | |
601 | - `socket()` and `socketpair()` must get `SOCK_CLOEXEC` passed, | |
602 | - `recvmsg()` must get `MSG_CMSG_CLOEXEC` set, | |
603 | - `F_DUPFD_CLOEXEC` should be used instead of `F_DUPFD`, and so on, | |
604 | - invocations of `fopen()` should take `e`. | |
605 | ||
606 | - It's a good idea to use `O_NONBLOCK` when opening 'foreign' regular files, | |
607 | i.e. file system objects that are supposed to be regular files whose paths | |
d0515a28 | 608 | were specified by the user and hence might actually refer to other types of |
25553cd9 | 609 | file system objects. This is a good idea so that we don't end up blocking on |
e347d53a | 610 | 'strange' file nodes, for example, if the user pointed us to a FIFO or device |
25553cd9 LP |
611 | node which may block when opening. Moreover even for actual regular files |
612 | `O_NONBLOCK` has a benefit: it bypasses any mandatory lock that might be in | |
613 | effect on the regular file. If in doubt consider turning off `O_NONBLOCK` | |
614 | again after opening. | |
615 | ||
4d26b227 LP |
616 | - These days we generally prefer `openat()`-style file APIs, i.e. APIs that |
617 | accept a combination of file descriptor and path string, and where the path | |
618 | (if not absolute) is considered relative to the specified file | |
619 | descriptor. When implementing library calls in similar style, please make | |
620 | sure to imply `AT_EMPTY_PATH` if an empty or `NULL` path argument is | |
621 | specified (and convert that latter to an empty string). This differs from the | |
622 | underlying kernel semantics, where `AT_EMPTY_PATH` must always be specified | |
623 | explicitly, and `NULL` is not acepted as path. | |
624 | ||
996f119d LP |
625 | ## Command Line |
626 | ||
627 | - If you parse a command line, and want to store the parsed parameters in | |
628 | global variables, please consider prefixing their names with `arg_`. We have | |
629 | been following this naming rule in most of our tools, and we should continue | |
630 | to do so, as it makes it easy to identify command line parameter variables, | |
631 | and makes it clear why it is OK that they are global variables. | |
632 | ||
633 | - Command line option parsing: | |
634 | - Do not print full `help()` on error, be specific about the error. | |
635 | - Do not print messages to stdout on error. | |
636 | - Do not POSIX_ME_HARDER unless necessary, i.e. avoid `+` in option string. | |
637 | ||
56380761 LP |
638 | ## Exporting Symbols |
639 | ||
640 | - Variables and functions **must** be static, unless they have a prototype, and | |
641 | are supposed to be exported. | |
642 | ||
643 | - Public API calls (i.e. functions exported by our shared libraries) | |
644 | must be marked `_public_` and need to be prefixed with `sd_`. No | |
645 | other functions should be prefixed like that. | |
646 | ||
647 | - When exposing public C APIs, be careful what function parameters you make | |
648 | `const`. For example, a parameter taking a context object should probably not | |
649 | be `const`, even if you are writing an otherwise read-only accessor function | |
650 | for it. The reason is that making it `const` fixates the contract that your | |
651 | call won't alter the object ever, as part of the API. However, that's often | |
652 | quite a promise, given that this even prohibits object-internal caching or | |
653 | lazy initialization of object variables. Moreover, it's usually not too | |
654 | useful for client applications. Hence, please be careful and avoid `const` on | |
655 | object parameters, unless you are very sure `const` is appropriate. | |
656 | ||
971dfffa LP |
657 | ## Referencing Concepts |
658 | ||
82143987 FA |
659 | - When referring to a configuration file option in the documentation and such, |
660 | please always suffix it with `=`, to indicate that it is a configuration file | |
661 | setting. | |
662 | ||
663 | - When referring to a command line option in the documentation and such, please | |
664 | always prefix with `--` or `-` (as appropriate), to indicate that it is a | |
665 | command line option. | |
666 | ||
667 | - When referring to a file system path that is a directory, please always | |
668 | suffix it with `/`, to indicate that it is a directory, not a regular file | |
669 | (or other file system object). | |
670 | ||
2d0dce2a LP |
671 | ## Functions to Avoid |
672 | ||
673 | - Use `memzero()` or even better `zero()` instead of `memset(..., 0, ...)` | |
674 | ||
675 | - Please use `streq()` and `strneq()` instead of `strcmp()`, `strncmp()` where | |
676 | applicable (i.e. wherever you just care about equality/inequality, not about | |
677 | the sorting order). | |
678 | ||
679 | - Never use `strtol()`, `atoi()` and similar calls. Use `safe_atoli()`, | |
680 | `safe_atou32()` and suchlike instead. They are much nicer to use in most | |
681 | cases and correctly check for parsing errors. | |
682 | ||
683 | - `htonl()`/`ntohl()` and `htons()`/`ntohs()` are weird. Please use `htobe32()` | |
684 | and `htobe16()` instead, it's much more descriptive, and actually says what | |
685 | really is happening, after all `htonl()` and `htons()` don't operate on | |
686 | `long`s and `short`s as their name would suggest, but on `uint32_t` and | |
687 | `uint16_t`. Also, "network byte order" is just a weird name for "big endian", | |
688 | hence we might want to call it "big endian" right-away. | |
689 | ||
a5b28b77 ZJS |
690 | - Use `typesafe_inet_ntop()`, `typesafe_inet_ntop4()`, and |
691 | `typesafe_inet_ntop6()` instead of `inet_ntop()`. But better yet, use the | |
692 | `IN_ADDR_TO_STRING()`, `IN4_ADDR_TO_STRING()`, and `IN6_ADDR_TO_STRING()` | |
74223cbe | 693 | macros which allocate an anonymous buffer internally. |
a5b28b77 | 694 | |
2d0dce2a | 695 | - Please never use `dup()`. Use `fcntl(fd, F_DUPFD_CLOEXEC, 3)` instead. For |
c1495f8e | 696 | two reasons: first, you want `O_CLOEXEC` set on the new `fd` (see |
2d0dce2a LP |
697 | above). Second, `dup()` will happily duplicate your `fd` as 0, 1, 2, |
698 | i.e. stdin, stdout, stderr, should those `fd`s be closed. Given the special | |
699 | semantics of those `fd`s, it's probably a good idea to avoid | |
700 | them. `F_DUPFD_CLOEXEC` with `3` as parameter avoids them. | |
701 | ||
82143987 FA |
702 | - Don't use `fgets()`, it's too hard to properly handle errors such as overly |
703 | long lines. Use `read_line()` instead, which is our own function that handles | |
c90b6abc | 704 | this much more nicely. |
2d0dce2a LP |
705 | |
706 | - Don't invoke `exit()`, ever. It is not replacement for proper error | |
707 | handling. Please escalate errors up your call chain, and use normal `return` | |
708 | to exit from the main function of a process. If you `fork()`ed off a child | |
709 | process, please use `_exit()` instead of `exit()`, so that the exit handlers | |
710 | are not run. | |
711 | ||
e109541f LP |
712 | - Do not use `basename()` or `dirname()`. The semantics in corner cases are |
713 | full of pitfalls, and the fact that there are two quite different versions of | |
714 | `basename()` (one POSIX and one GNU, of which the latter is much more useful) | |
4e11b54b | 715 | doesn't make it better either. Use path_extract_filename() and |
e109541f | 716 | path_extract_directory() instead. |
831781b9 | 717 | |
bcef0f33 ZJS |
718 | - Never use `FILENAME_MAX`. Use `PATH_MAX` instead (for checking maximum size |
719 | of paths) and `NAME_MAX` (for checking maximum size of filenames). | |
720 | `FILENAME_MAX` is not POSIX, and is a confusingly named alias for `PATH_MAX` | |
6ae11e12 | 721 | on Linux. Note that `NAME_MAX` does not include space for a trailing `NUL`, |
bcef0f33 | 722 | but `PATH_MAX` does. UNIX FTW! |
b775b182 | 723 | |
ff2c2d08 | 724 | ## Committing to git |
831781b9 LP |
725 | |
726 | - Commit message subject lines should be prefixed with an appropriate component | |
5c7a4f21 | 727 | name of some kind. For example, "journal: ", "nspawn: " and so on. |
831781b9 LP |
728 | |
729 | - Do not use "Signed-Off-By:" in your commit messages. That's a kernel thing we | |
730 | don't do in the systemd project. | |
e8a587dc | 731 | |
bbb71e5c | 732 | ## Commenting |
e8a587dc LP |
733 | |
734 | - The best place for code comments and explanations is in the code itself. Only | |
735 | the second best is in git commit messages. The worst place is in the GitHub | |
736 | PR cover letter. Hence, whenever you type a commit message consider for a | |
737 | moment if what you are typing there wouldn't be a better fit for an in-code | |
738 | comment. And if you type the cover letter of a PR, think hard if this | |
739 | wouldn't be better as a commit message or even code comment. Comments are | |
740 | supposed to be useful for somebody who reviews the code, and hence hiding | |
741 | comments in git commits or PR cover letters makes reviews unnecessarily | |
742 | hard. Moreover, while we rely heavily on GitHub's project management | |
743 | infrastructure we'd like to keep everything that can reasonably be kept in | |
744 | the git repository itself in the git repository, so that we can theoretically | |
d8b67e05 | 745 | move things elsewhere with the least effort possible. |
e8a587dc LP |
746 | |
747 | - It's OK to reference GitHub PRs, GitHub issues and git commits from code | |
748 | comments. Cross-referencing code, issues, and documentation is a good thing. | |
749 | ||
750 | - Reasonable use of non-ASCII Unicode UTF-8 characters in code comments is | |
751 | welcome. If your code comment contains an emoji or two this will certainly | |
752 | brighten the day of the occasional reviewer of your code. Really! 😊 |