Commit Graph
9865 Commits
Author SHA1 Message Date
Yu Watanabe 5056d8332e test: improve printing of assertion errors, allow more failures (#38004) 2025-07-03 09:01:22 +09:00
Zbigniew Jędrzejewski-Szmek 6b60354656 tests: print errno name rather than the message
The tests are written and consumed by developers. Errno descriptions are good
for users, but for developers the errno "name" is actually more useful, and
we need to always map the description back to the name to compare with the
code. Let's make things simpler for ourselves by printing the errno names
directly.

Example output:

src/test/test-tests.c:15: Assertion failed: Expected "-1" to succeed, but got error: -1/EPERM
src/test/test-tests.c:16: Assertion failed: Expected "-ENOANO" to succeed, but got error: -55/ENOANO
src/test/test-tests.c:20: Assertion failed: Expected "0" to be positive, but it is zero.
src/test/test-tests.c:62: Assertion failed: Expected "RET_NERRNO(mkdir("/i/will/fail/with/enoent", 666))" to fail with error -55/ENOANO, but got -2/ENOENT
src/test/test-tests.c:68: Assertion failed: Expected "0" to fail with errno 2/ENOENT, but it succeeded
src/test/test-tests.c:70: Assertion failed: Expected "mkdir("/i/will/fail/with/enoent", 666)" to fail with errno 55/ENOANO, but got 2/ENOENT
2025-07-01 17:53:22 +02:00
Zbigniew Jędrzejewski-Szmek 367b325554 tests: add new ASSERT_OK_OR macro
IN_SET() fails if __VA_ARGS__ is just one item. I inserted a bogus 0 item into
the check to work around this.
2025-07-01 17:51:49 +02:00
Zbigniew Jędrzejewski-Szmek 0adbb11b4e tests: print numeric error value too on failure
The error message is not always meaningful. Also, sometimes we care about the
sign of the value, and we ignore the sign of the error in the printing machinery.

The messages for errno are changed to say "errno" instead of "error". The problem with
the previous formalation is that our errors are always negative and errnos are
positive, so when we print the numerical value, we cannot use the word for both.

Example output:
src/test/test-tests.c:15: Assertion failed: Expected "-1" to succeed, but got error: -1/Operation not permitted
src/test/test-tests.c:16: Assertion failed: Expected "-ENOANO" to succeed, but got error: -55/No anode
src/test/test-tests.c:61: Assertion failed: Expected "0" to fail with error -2/"No such file or directory", but it succeeded
src/test/test-tests.c:62: Assertion failed: Expected "RET_NERRNO(mkdir("/i/will/fail/with/enoent", 666))" to fail with error -55/"No anode", but got the following error: -2/No such file or directory
src/test/test-tests.c:68: Assertion failed: Expected "0" to fail with errno 2/"No such file or directory", but it succeeded
src/test/test-tests.c:70: Assertion failed: Expected "mkdir("/i/will/fail/with/enoent", 666)" to fail with errno 55/"No anode", but got the following errno: 2/No such file or directory
2025-07-01 17:51:49 +02:00
Zbigniew Jędrzejewski-Szmek 236f7ab07c tests: cast to intmax_t instead of printing to a temp buffer
We can do this for int types, i.e. the variants where we expect a
success/error code. The macros which do generating comparison operations
also support floats so we shouldn't use intmax_t there.

The code is shorter and calling printf once is certainly more efficient.
2025-07-01 17:51:49 +02:00
Zbigniew Jędrzejewski-Szmek 89181651e6 shared/tests: indent defines 2025-07-01 17:47:34 +02:00
Mike Yuan 1b4ab5a209 core/socket: introduce DeferTrigger= and DeferTriggerMaxSec=
Alternative to b50f6dbe57

The commit naively returned early from socket_enter_running(), which however
is quite problematic, as the socket will be woken up over and over again
without doing a thing, until we eventually hit Poll/TriggerLimit*=.
On top of that it requires hacks to hold the start job for initrd-switch-root.service
up. Overall I doubt that is the right approach.

Let's instead hook this into our job engine, and try to activate
the service again when some other units are stopped. If all installed
jobs have been run yet we're still seeing the conflict or the manually
selected timeout is reached, fail the socket as before.
2025-06-30 13:10:43 +02:00
Yu Watanabe 4f18ff2e29 tree-wide: include unistd.h where necessary
We use symbols provided by unistd.h without including it. E.g.
open(), close(), read(), write(), access(), symlink(), unlink(), rmdir(),
fsync(), syncfs(), lseek(), ftruncate(), fchown(), dup2(), pipe2(),
getuid(), getgid(), gettid(), getppid(), pipe2(), execv(), _exit(),
environ, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, F_OK, and their
friends and variants, so on.

Currently, unistd.h is indirectly included mainly in the following two paths:
- through missing_syscall.h, which is planned to covert to .c file.
- through signal.h -> bits/sigstksz.h, which is new since glibc-2.34.
  Note, signal.h is included by sd-eevent.h. So, many source files
  indirectly include unistd.h if newer glibc is used.

Currently, our baseline on glibc is 2.31. We need to support glibc older
than 2.34, but unfortunately, we do not have any CI environments with
such old glibc. CIFuzz uses glibc-2.31, but it builds only fuzzers, and
many files are even not compiled.
2025-06-30 09:19:15 +02:00
Yu Watanabe 2d0ec7f900 Revert "pretty-print: also chase symlinks in the last component"
This reverts commit 2f2977cab8.

The commit was mistakenly merged without review. Also as pointed at
https://github.com/systemd/systemd/pull/37972#discussion_r2173234198
now the output is confusing. Let's revert at least now.
2025-06-29 10:15:56 +09:00
Yu Watanabe b351ebdc97 Revert "tree-wide: use CONF_FILES_CHASE_BASENAME where root directory is specified"
This reverts commit 6b606f3586.

The commit was mistakenly merged without review, and has several issues.
Let's revert at least now.
2025-06-29 10:15:56 +09:00
Zbigniew Jędrzejewski-Szmek 7d247d3cb8 meson: drop explicit custom_target names
[1] says:
> Since 0.60.0 the name argument is optional and defaults to the basename of
> the first output
We specify >= 0.62 as the supported version, so drop the duplicate name in all cases
where it is the same as outputs[0], i.e. almost all cases.

[1] https://mesonbuild.com/Reference-manual_functions.html#custom_target
2025-06-28 17:14:50 +02:00
Yu Watanabe 6b606f3586 tree-wide: use CONF_FILES_CHASE_BASENAME where root directory is specified
Otherwise, the result may point to outside of the root directory.

This also sets CONF_FILES_REGULAR and CONF_FILES_FILTER_MASKED, as the
callers will call fopen() or friends for the result, thus the enumerated
results must be non-empty regular files.
2025-06-28 04:13:43 +09:00
Yu Watanabe 2f2977cab8 pretty-print: also chase symlinks in the last component
Otherwise, this may shows files outside of the specified root directory.
2025-06-28 04:13:43 +09:00
Yu Watanabe d6570eafe3 userdb: fix enumeration of .membership files
The man page nss-systemd(8) says

> The contents of these files are currently not read, and the files
> should be created empty.

But previously we filtered out such files, as the CONF_FILES_FILTER_MASKED
flag checks if a file is empty (or symlink to null), thus any empty files
were ignored.

To accept empty .membership files, let's use CONF_FILES_FILTER_MASKED_BY_SYMLINK.

Fixes #37945.
2025-06-28 03:54:48 +09:00
Lennart Poettering 5584632f40 cpu-set-util: fix null pointer dereference and several cleanups (#37900)
Fixes CID#1611618.
2025-06-27 14:59:25 +02:00
Lennart Poettering c6bb846c04 shared: make sure RandomizedOffsetUSec can be set as transient prop
Fixes: #37870
Follow-up for: #36437
2025-06-26 17:31:00 +02:00
Lennart Poettering f789b17e87 terminal-util: switch from TCSADRAIN to TCSANOW for all tcsetattr() calls
TCSADRAIN means tcsetattr() will become blocking (waiting for ability to
write out queued bytes), which is problematic, if the referenced TTY is
dead for some reason.

Since all these calls just modify *input* parameters anyway (i.e. mostly
local echo, and canonical mode), forcing out queued output is kinda
pointless anyway, hence just don't do it: leave it in the queue and just
change the flags we want to change.

The tcsetattr(3) man page kinda hints that we want to use TCSANOW here,
because it documents for TCSADRAIN:

"This option should be used when changing parameters that affect
output."

Which one can read so that TCSADRAIN should not be used if it doesn't
affect output, which is the case here.

This probably fixes: #37854
2025-06-26 01:24:52 +09:00
Yu Watanabe 38704f5dc9 include: update syscall list
open_tree_attr is since kernel 6.15-rc1:
https://github.com/torvalds/linux/commit/c4a16820d90199409c9bf01c4f794e1e9e8d8fd8
2025-06-25 03:59:25 +09:00
Yu Watanabe fbf6836812 tree-wide: include missing_fs.h only where necessary
The additional definitions provided by the header are
- EXT4_IOC_RESIZE_FS, used in resize-fs.c,
- FILEID_KERNFS, used in cgroup-util.c and pidfd-util.c.

Let's drop the inclusion at other places.
2025-06-25 00:50:06 +09:00
Lennart Poettering 51cc3825d1 tree-wide: use sd_bus_message_send() instead of sd_bus_send() wherever possible
Also, add a coccinelle script that patches this automatically.
2025-06-24 23:23:40 +09:00
Lennart Poettering 170d7d49f5 user-record: also support "aliases" in the "status" section of user records
Some user record providers might want to implement case-insensitive user
record matching, or other forms of non-normalized matching. So far
uderdb didn't allow that, because client's typically revalidate the
returned user records against the search keys (at least our clients do)
– they check if the search user name is actually part of the user record
and its aliases.

In order to support such lookups we thus need to allow the looup keys to
be part of the user record, but also not be persisted in it, because
the number of casings/spellings of a username might be ridiculously
high.

A nice way out is to allow "aliases" not only in the main part of the
record, but also in the "status" part, that contains information
dynamically determined at query time. We can insert a second "aliases"
field there, which the parser will then merge with the primary "aliases"
field, but the existing rules around "status" ensure tha the data is
never persisted.

Follow-up: e2e1f38f5a
2025-06-23 22:26:24 +01:00
Yu Watanabe 5eab200758 homed,userdbd: mark "secrets" section of user records always to "sensitive" (#37935) 2025-06-24 00:50:44 +09:00
Lennart Poettering 1bd9a3c916 bootspec: ensure memory free in one error path 2025-06-24 00:49:46 +09:00
Yu Watanabe fe3ada076e cpu-set-util: introduce config_parse_cpu_set()
Then, make parse_cpu_set() as a tiny wrapper of it.

Note, previously when an invalid CPU range, e.g. "3-0", is specified,
we ignore the range but allocate an empty set. But, with this commit,
now the conf parser simply ignore it without no side effect.
This potentially changes behavior of a system with such invalid setting,
but the change should be favorable for consistency with other parsers.
2025-06-24 00:20:20 +09:00
Yu Watanabe 01eb1ce19b cpu-set-util: drop unused cpu_set_free() 2025-06-24 00:20:20 +09:00
Yu Watanabe 296fe3d511 cpu-set-util: rename cpu_set_reset() -> cpu_set_done()
This also introduces cpu_set_done_and_replace().

No functional change, just refactoring.
2025-06-24 00:20:20 +09:00
Yu Watanabe f4093f43fb cpu-set-util: rename cpu_set_add_all() and cpu_mask_add_all()
This renames
- cpu_set_add_all() -> cpu_set_add_set(),
- cpu_mask_add_all() -> cpu_set_add_all().

No functional change, just refactoring.
2025-06-24 00:20:20 +09:00
Yu Watanabe c4afb3c3e3 cpu-set-util: several cleanups for cpu_set_{from,to}_dbus()
- rename arguments,
- use SET_BIT() and BIT_SET() macros,
- explicitly (re)alloc before setting bits.

No functional change, just refactoring.
2025-06-24 00:20:20 +09:00
Yu Watanabe 8de95eafa1 cpu-set-util: several cleanups for cpu_set_realloc(), cpu_set_add() and friends
- introduce CPU_SET_MAX_NCPU and check overflow several more places,
- use GREEDY_REALLOC0(),
- introduce cpu_set_add_range() helper function,
- explicitly (re)alloc first before setting multiple bits.

No functional change, just refactoring.
2025-06-24 00:20:20 +09:00
Yu Watanabe 2d4613427e cpu-set-util: several cleanups for cpu_set_to_string() and cpu_set_to_range_string()
- Add missing assertions.
- Replace GREEDY_REALLOC() + sprintf() with strextendf_with_separator().
2025-06-24 00:20:20 +09:00
Yu Watanabe df04987af2 cpu-set-util: make cpu_set_to_mask_string() work with an empty cpu set
This also make it use strextendf_with_separator() and SET_BIT().

Fixes CID#1611618.
2025-06-24 00:20:20 +09:00
Valentin David 0dc39dffbd Use paths specified from environment variables for /etc configuration files
Some configuration files that need updates are directly under in /etc. To
update them atomically, we need write access to /etc. For Ubuntu Core this is
an issue as /etc is not writable. Only a selection of subdirectories can be
writable. The general solution is symlinks or bind mounts to writable places.
But for atomic writes in /etc, that does not work. So Ubuntu has had a patch
for that that did not age well.

Instead we would like to introduce some environment variables for alternate
paths.

 * SYSTEMD_ETC_HOSTNAME: /etc/hostname
 * SYSTEMD_ETC_MACHINE_INFO: /etc/machine-info
 * SYSTEMD_ETC_LOCALTIME: /etc/localtime
 * SYSTEMD_ETC_LOCALE_CONF: /etc/locale.conf
 * SYSTEMD_ETC_VCONSOLE_CONF: /etc/vconsole.conf
 * SYSTEMD_ETC_ADJTIME: /etc/adjtime

While it is for now expected that there is a symlink from the standard, we
still try to read them from that alternate path. This is important for
`/etc/localtime`, which is a symlink, so we cannot have an indirect symlink or
bind mount for it.

Since machine-id is typically written only once and not updated. This commit
does not cover it. An initrd can properly create it and bind mount it.
2025-06-23 15:32:11 +02:00
Lennart Poettering e3d0fa5ac4 userdb: when loading a user record from JSON, mark 'secret' section in JSON variant as sensitive as side effect
When we load a user record we retain a reference to the original JSON.
Thus the loaded objects might live at least as long as our user record
object, hence we better make sure we set the 'sensitive' flag for the
'secret' section if it's not marked like that yet.

This is paranoia only: we already should be setting this flag properly
earlier, when acquiring the json variant in the first place. But it's
better to be safe than sorry.
2025-06-23 14:48:45 +02:00
Yu Watanabe 4d05a19c95 bootctl: honour architecture when updating boot loaders (#37913)
Fixes: #33413
Follow-up for: #30418
2025-06-21 02:25:17 +09:00
Lennart Poettering e04d782416 copy: when looking for file holes, consider empty data segments
This could mean that we hit EOF, or it could mean that somebody punched
a hole concurrently where we are currently looking. Let's figure this
out by simply trying to copy a single byte, which will give us a
definitive answer.

Fixes: #35569
2025-06-21 02:19:27 +09:00
Lennart Poettering 6b8770b96b bootctl: when updating everything check PE machine type
Let's never accidentally over-write foreign-arch PE binaries with native
ones.

Fixes: #33413
2025-06-20 14:06:53 +02:00
Luca Boccassi da993edcf7 tpm2: print clear log message in case of dictionary lockout mode (#37894)
Fixes: #32260
2025-06-19 22:55:36 +01:00
Lennart Poettering df21ceca25 cryptsetup: handle DA lockout/bad PIN properly
Now that we have recognizable errors, let's print clear error messages
when we try to unlock a TPM slot. And in case of the token plugin
propagate this as ENOANO so that libcryptsetup recognizes this as bad
PIN. (ENOANO is documented as the error to return in that case)

Fixes: #32260
2025-06-19 12:33:52 +02:00
Lennart Poettering 48cb1ad9c3 tpm2-util: recognize DA lockout mode and PIN failures properly
When we create the policy session the previously passed PIN will be
checked. This means we'll see PIN errors here, in case the PIN is wrong
or if DA lockout mode has been triggered.

Recognize these two errors, and bubble up recognizable errors.
2025-06-19 12:32:15 +02:00
Yu Watanabe cef4ec347f ioprio-util: move from basic/ to shared/
It is not used by libsystemd.
2025-06-19 19:08:45 +09:00
Yu Watanabe dbfe2faa48 port write_env_file() to label_os_pre() + teach firstboot a bit of relabelling (#37869)
This is a clean-up, and also addresses some of the points of #37857.
2025-06-19 18:03:12 +09:00
Yu Watanabe abb99d3168 mallinfo-util: assume mallinfo() exists
The check existed for musl. Let's remove it, as we explicitly request glibc.

While removing the check, this also drops generic_mallinfo, introduces
a tiny converter from struct mallinfo to struct mallinfo2 if mallinfo2()
does not exist, and renames mallinfo-util.h to malloc.h.

With this change, we can drop many ifdefs and casts in .c files.
2025-06-18 10:45:27 +02:00
Lennart Poettering 3e5320e27d env-file: port write_env_file() to label_ops_pre()
Let's make more use of label_ops_pre()/label_ops_post(), and replace
write_env_file_label() by a flag to write_env_file().

This simplifies and normalizes the code.

This also makes one relevant change: it sets the new
WRITE_ENV_FILE_LABEL flag in firstboot.c when we write locale.conf,
where we previously did not (but should have). This should address one
detail of #37857.
2025-06-18 10:21:26 +02:00
Lennart Poettering 9ea5a6e7d6 fs-util: replace symlink_atomic_full_label() by a flag to symlinkat_atomic_full()
More porting work to label_ops_pre()/label_ops_post()

This also enables labelling of the /etc/localtime symlink in
systemd-firstboot, which should address one small facet of #37857
2025-06-18 10:21:25 +02:00
Yu Watanabe 9d5f05ae22 bus-util: coding style cleanups
- replace `type *func()` -> `type* func()`,
- rename arguments for storing results.
2025-06-18 01:55:46 +09:00
Yu Watanabe 31a1e15ccb sd-bus: coding style cleanups
- replace `type *func()` -> `type* func()`,
- rename arguments for storing results,
- add several missing assertions.
2025-06-18 01:54:07 +09:00
Mike Yuan 85352c095e various: turn off SO_PASSRIGHTS where fds are not expected 2025-06-17 13:16:44 +02:00
Mike Yuan 718e7eb184 notify-recv: disable SO_PASSRIGHTS by default in notify_socket_prepare() 2025-06-17 13:16:43 +02:00
Mike Yuan 5c12797fc3 core/socket: introduce AcceptFileDescriptors=
This controls the new SO_PASSRIGHTS socket option in kernel v6.16.
Note that I intentionally choose a different naming scheme than
Pass*=, since all other Pass*= options controls whether some extra
bits are attached to the message, while this one's about denying
file descriptor transfer and it feels more explicit this way.
And diverging from underlying socket option name is precedented
by Timestamping=. But happy to change it to just say PassRights=
if people disagree.
2025-06-17 13:16:42 +02:00
Mike Yuan 35462aa14a core/socket: add PassPIDFD= 2025-06-17 13:16:41 +02:00