Commit Graph

4111 Commits

Author SHA1 Message Date
Albert Brox
8a513eee30 pid1: add support for cgroup.kill 2021-08-09 12:14:26 +02:00
Zbigniew Jędrzejewski-Szmek
2fec408e67 basic/log: invert loop to avoid repeated evaluation of condition 2021-08-07 08:50:16 +02:00
Zbigniew Jędrzejewski-Szmek
4dc2ecd227 basic/log: use structured initialization, drop unused initialization
We had 'msghdr' and 'mh' in various places. Now 'const struct msghdr msghdr' is
used consistently. With structured init the variable is only used in the call
to sendmsg(), so let's make it a bit more descriptive.
2021-08-07 08:49:11 +02:00
Zbigniew Jędrzejewski-Szmek
9bc0173275 basic/log: use appropriate glyph in log_assert_failed_unreachable()
Per popular demand.
2021-08-03 15:46:21 +02:00
Zbigniew Jędrzejewski-Szmek
04499a70fb Drop the text argument from assert_not_reached()
In general we almost never hit those asserts in production code, so users see
them very rarely, if ever. But either way, we just need something that users
can pass to the developers.

We have quite a few of those asserts, and some have fairly nice messages, but
many are like "WTF?" or "???" or "unexpected something". The error that is
printed includes the file location, and function name. In almost all functions
there's at most one assert, so the function name alone is enough to identify
the failure for a developer. So we don't get much extra from the message, and
we might just as well drop them.

Dropping them makes our code a tiny bit smaller, and most importantly, improves
development experience by making it easy to insert such an assert in the code
without thinking how to phrase the argument.
2021-08-03 10:05:10 +02:00
Yu Watanabe
c7cfde640d Merge pull request #20346 from poettering/strlen-unsigned-fix
CONST_MAX() integer size fix
2021-08-03 11:03:29 +09:00
David Seifert
f8d54f7810 Use correct <fcntl.h> include
* `<sys/fcntl.h>` is not specified in POSIX
2021-08-02 14:51:50 +02:00
Lennart Poettering
e3709627e6 signal-util: fix typo
Noticed by @behrmann: https://github.com/systemd/systemd/pull/20156#discussion_r667451006
2021-07-30 21:55:47 +02:00
Lennart Poettering
30763a32b2 util: add one more helper for generating colored check mark glyphs
This one is useful for a outputs with a slightly more "positive"
outlook, i.e. where only the checkmarks are shown but the crossmarks are
replaced by spaces.

(Usecase: a larger table with many checkmarks, where the red crossmarks
might just be too much negative noise)
2021-07-30 16:18:40 +02:00
Lennart Poettering
bf18f4b671 glyph-util: add three more emojis to emoji list 2021-07-30 16:18:05 +02:00
Lennart Poettering
d844529dc7 macro: change DECIMAL_STR_WIDTH() return type to size_t, like strlen() and so on 2021-07-30 15:59:29 +02:00
Lennart Poettering
777003365a macro: sizeof() returns size_t, and that's good
Now that CONST_MAX() is a bit more foregiving, let's stick to the native
return type of sizeof() everywhere, which is size_t, instead of casting
to "unsigned", so that on the common archs we don't unnecessarily lose
the upper 32bits.

This semi-reverts d3e4029457.
2021-07-30 15:58:09 +02:00
Lennart Poettering
7ecc424fbe alloc-util: drop double eval from free_and_replace()
Inspired by: 2744c7bb01
2021-07-29 21:12:58 +02:00
Lennart Poettering
c62f67f730 parse-util: use oom_score_adjust_is_valid() at one more place 2021-07-28 18:39:41 +02:00
Lennart Poettering
c4412d4d33 Revert "Make oom_score_adjust_is_valid() static"
This reverts commit 6bf3c6c900.
2021-07-28 18:39:41 +02:00
Zbigniew Jędrzejewski-Szmek
cbcf371abc Add variant of close_all_fds() that does not allocate and use it in freeze()
Even though it's just a fallback path, let's not be sloppy and allocate in
the crash handler.

> The deadlock happens because systemd crash in malloc() then in signal
> handler, it calls malloc() (close_all_fds()-> opendir()-> __alloc_dir())
> again. malloc() is not a signal-safe function, maybe we should re-think
> the logic here.

Fixes #20266.
2021-07-23 11:39:45 +02:00
Zbigniew Jędrzejewski-Szmek
45a68ed307 Move freeze() into shared/
Library code should not call freeze(), this is something that should
only be done by "application code", so moving it into shared/ is appropriate.

The fallback to call _exit() is dropped: let's trust that the infinite loop
is infinite.
2021-07-23 11:39:45 +02:00
Zbigniew Jędrzejewski-Szmek
3e24e8cd64 Move fork_agent() into shared/
Currently it's only used in two places in src/shared/, so the function was
already included just once in compiled code. But it seems appropriate to
move it there anyway, because library code should have no need to fork
agents, so it doesn't belong in basic/.
2021-07-23 11:39:45 +02:00
Zbigniew Jędrzejewski-Szmek
3bd6a01c01 basic/process-util: use xsprintf() in one more place 2021-07-23 11:39:45 +02:00
Zbigniew Jędrzejewski-Szmek
6bf3c6c900 Make oom_score_adjust_is_valid() static
It has only one user and we don't need to put it in basic/.
2021-07-23 11:39:45 +02:00
Zbigniew Jędrzejewski-Szmek
9c46228b7d basic/fd-util: sort the 'except' array in place
We need a sorted list of fds to skip over when closing. We would allocate a
copy of the passed array to do the sort. But all callers construct a temporary
array to pass to us, so it is pointless to copy it again.

close_all_fds/safe_fork_full/namespace_fork/fork_agent are changed to pass
a non-const int array. I checked all users, and all callers are fine with
the array being sorted.

The function was returning some number (sometimes 1, sometimes the extent
of the range passed over to close_range(), ???). Anyway, all callers only
check for error, so let's return 0 on success.
2021-07-23 11:37:44 +02:00
Zbigniew Jędrzejewski-Szmek
b34a4f0e67 Merge pull request #20256 from keszybz/one-alloca-too-many
basic/unit-name: do not use strdupa() on a path
2021-07-20 14:39:23 +02:00
Zbigniew Jędrzejewski-Szmek
c543f4d7dd basic/time-util: inline one more variable declaration 2021-07-19 19:58:13 +02:00
Yu Watanabe
e18f21e349 Merge pull request #20109 from keszybz/timestamp-macros
Add macros that define scratch buffer internally for timestamp/timespan formatting
2021-07-14 16:14:08 +09:00
Luca Boccassi
af55491028 Merge pull request #20186 from keszybz/coverity-fixes
Coverity fixes
2021-07-09 21:15:08 +01:00
Frantisek Sumsal
2f0927626a tree-wide: coccinelle fixes
Yet another batch of Coccinelle fixes.
2021-07-09 20:54:22 +01:00
Luca Boccassi
5c8cf104e7 Merge pull request #20157 from keszybz/numerical-uids-in--M
Allow numerical UIDs in systemctl -M uid@ and similar
2021-07-09 16:58:45 +01:00
Zbigniew Jędrzejewski-Szmek
bc1f27ff55 creds: drop unnecessary initialization
Coverity also thinks a leak happens here, CID #1458112.
This seems wrong, but let's add an assert, maybe that'll help.
2021-07-09 15:13:12 +02:00
Zbigniew Jędrzejewski-Szmek
1421705d9a core: drop unnecessary initialization
cunescape() sets output on success, so initialization is not necessary. There
was no comment, but I think they may have been added because the compiler
wasn't convinced that the return value is non-negative on success. It could
have been confused by the int return type on escape*(), which was changed by
the one of preceeding commits to ssize_t, or by the length calculation, so add
an assert to help the compiler.

For some reason coverity thinks the output can be leaked here (CID #1458111).
I don't see how.
2021-07-09 15:12:18 +02:00
Zbigniew Jędrzejewski-Szmek
e437538f35 tree-wide: make cunescape*() functions return ssize_t
Strictly speaking, we are returning the size of a memory chunk of
arbitrary size, so ssize_t is more appropriate than int.
2021-07-09 15:07:40 +02:00
Zbigniew Jędrzejewski-Szmek
ddedf7ca69 basic/escape: use _cleanup_ in one more place
Also, let's not use 'r' for a char*.
2021-07-09 15:07:13 +02:00
Zbigniew Jędrzejewski-Szmek
eeb91d29b0 basic/escape: add helper for quoting command lines 2021-07-09 11:18:21 +02:00
Zbigniew Jędrzejewski-Szmek
12619d0a80 tree-wide: do not use (void) asprintf
asprintf(3) says that the pointer is "undefined" after a failed call.
In the current glibc implementation it is just NULL. In principle the
call could return a valid pointer with bad contents or something.

We have two styles of error handling: in a majority of cases we would
check the return value, but sometimes we used (void) and relied on the
pointer not being set. In practice both styles should be equivalent,
but gcc doesn't like the second one with -Wunused-result. (Though only
sometimes. E.g. on my F34 box I don't get the same warnings as in CI,
even though the compiler version is very similar and the compilation
options are the same…). It's also nice to be consistent in our code base.
So let's always use the first style of error checking.
2021-07-09 11:11:25 +02:00
Zbigniew Jędrzejewski-Szmek
cb3e854fed basic/time-util: indentation 2021-07-09 11:11:25 +02:00
Zbigniew Jędrzejewski-Szmek
6c6368e938 basic/{time,format}-util: warn when format result is unused
Now that anonymous buffers are used in almost all cases, code which
does not use the return value is usually broken.
2021-07-09 11:11:25 +02:00
Zbigniew Jędrzejewski-Szmek
9ca7e3d00d Replace format_bytes_cgroup_protection with FORMAT_BYTES_CGROUP_PROTECTION 2021-07-09 11:11:25 +02:00
Zbigniew Jędrzejewski-Szmek
46e23f9a8d tree-wide: add FORMAT_BYTES_FULL() 2021-07-09 11:11:25 +02:00
Zbigniew Jędrzejewski-Szmek
d3e4029457 basic/macro: make CONST_MAX(DECIMAL_STR_MAX(…), STRLEN(…)) possible
When those two macros were used together in CONST_MAX(), gcc would complain
about a type mismatch. So either DECIMAL_STR_MAX() should be made size_t like
STRLEN(), or STRLEN() be made unsigned.

Since those macros are only usable on arguments of (small) fixed size, any type
should be fine (even char would work…). For buffer size specifications, both
size_t and unsigned are OK. But unsigned was used for DECIMAL_STR_MAX macros
and FORMAT_foo_MAX macros, making STRLEN the only exception, so let's adjust
STRLEN() to be unsigned too.

Also: I don't think this is currently used anywhere, but if any of those macros
were used as an argument to sprintf, size_t would require a cast. ("%*s"
requires an int/unsigned argument.)
2021-07-09 11:11:25 +02:00
Zbigniew Jędrzejewski-Szmek
2b59bf51a0 tree-wide: add FORMAT_BYTES() 2021-07-09 11:11:21 +02:00
Zbigniew Jędrzejewski-Szmek
0086ef19cb tree-wide: add FORMAT_TIMESTAMP_STYLE() 2021-07-09 11:03:36 +02:00
Zbigniew Jędrzejewski-Szmek
32fc5c4763 tree-wide: add FORMAT_TIMESTAMP_RELATIVE() 2021-07-09 11:03:36 +02:00
Zbigniew Jędrzejewski-Szmek
5291f26d4a tree-wide: add FORMAT_TIMESPAN() 2021-07-09 11:03:36 +02:00
Zbigniew Jędrzejewski-Szmek
ae7c644c22 basic/time-util: add FORMAT_TIMESTAMP
This uses the same idea of an anonyous buffer as ETHER_ADDR_TO_STR().
2021-07-09 11:03:35 +02:00
Zbigniew Jędrzejewski-Szmek
e265fa8198 test-ether-addr-util: add a simple test that HW_ADDR_TO_STR works with nesting
It seems to, but I was a bit incredulous… The comment is adjusted to match
the standard.

The trick with a temporary buffer is neat. I wasn't sure if it is valid, but
the standard says so. The test really tests that we are reading the rules right
and that the compiler doesn't do anythign strange or emit an unexpected
warning.
2021-07-09 11:03:35 +02:00
Yu Watanabe
d0e18bb4b0 Merge pull request #20166 from poettering/fsync-more
various tweaks to existing fsync() helpers, and some new apis
2021-07-09 07:32:01 +09:00
Yu Watanabe
452a07cfd6 Merge pull request #20168 from poettering/signal-util-tweak
generalize SIGINT handling in copy.c
2021-07-09 07:24:43 +09:00
Lennart Poettering
6a24c995f0 dirent-util: no need to bother with fstatat() for "." and ".." to figure out if these are dirs 2021-07-09 04:18:09 +09:00
Lennart Poettering
1053967781 path-util: make path_compare() accept NULL 2021-07-08 13:56:06 +02:00
Lennart Poettering
949162552d conf-files: rename return parameters ret_xyz 2021-07-08 13:06:58 +02:00
Lennart Poettering
0178ff292b signal-util: add helper pop_pending_signal() 2021-07-08 10:33:38 +02:00