Commit Graph
7103 Commits
Author SHA1 Message Date
Zbigniew Jędrzejewski-Szmek 048a94c8f6 basic/stdio-util: use a fixed message in xsprintf
We put the name of the variable in the message, but it is a local variable
and the name does not have global meaning. We end up with pointless copies
of the error string:

$ strings build/libsystemd.so.0.40.0 | grep 'big enough'
xsprintf: p[] must be big enough
xsprintf: error[] must be big enough
xsprintf: prefix[] must be big enough
xsprintf: pty[] must be big enough
xsprintf: mode[] must be big enough
xsprintf: t[] must be big enough
xsprintf: s[] must be big enough
xsprintf: spid[] must be big enough
xsprintf: header_priority[] must be big enough
xsprintf: header_pid[] must be big enough
xsprintf: path[] must be big enough
xsprintf: buf[] must be big enough

The error message already shows the file, line, and function name, which
is enough to identify the problem:

  Assertion 'xsprintf: buffer too small' failed at src/test/test-string-util.c:20, function test_xsprintf(). Aborting.
2025-07-08 13:02:37 +02:00
Zbigniew Jędrzejewski-Szmek c179466616 Merge shared/exec-directory-util.? into basic/unit-def.?
Suggested in
https://github.com/systemd/systemd/pull/35892#discussion_r2180322856.

This is a tiny amount of code and does not warrant having a separate file
and spawning a separate instance of the compiler during the build.

Note: it took me a while to confirm that the contents of that table and
function don't end up in libsystemd.so. The issue is that they _are_ present in
it, unless LTO is used. We actually use link_whole[libbasic_static] for
libsystemd, so we end up with all that code there. LTO is needed to clean
that up.
2025-07-08 12:57:33 +02:00
Lennart Poettering 18eafedb1a nspawn: Support idmapped mounts on homed managed home directories (#38069)
Christian made this possible in Linux 6.15 with a new system call
open_tree_attr() that combines open_tree() and mount_setattr(). Because
idmapped mounts are (rightfully) not nested, we have to do some extra
shenanigans to make source we're putting the right source uid in the
userns for any idmapped mounts that we do in nspawn.

Of course we also add the necessary boilerplate to make open_tree_attr()
available in our code and wrap open_tree_attr() and the corresponding
fallback in a new function which we then use everywhere else.
2025-07-08 06:51:41 +02:00
Andres Beltran 652ba6e0dc chattr-util: add helpers to read and set project IDs 2025-07-07 17:28:47 +00:00
DaanDeMeyer bda934d4e5 forward: Add struct mount_attr forward declaration 2025-07-07 13:35:54 +02:00
DaanDeMeyer 2d9759ae6a basic: Add open_tree_attr() syscall wrapper 2025-07-07 13:35:47 +02:00
DaanDeMeyer 3bb59e28f9 basic: Use Static_assert() in missing_syscall_def.h
Let's use Static_assert() so we don't have to include forward.h in
missing_syscall_def.h.
2025-07-07 12:35:56 +02:00
Yu Watanabe 83a047fe23 tree-wide: include sys/stat.h where necessary
These source files uses symbols provided by sys/stat.h, e.g. struct stat,
S_IFREG, S_IFBLK, and so on. Let's explicitly include sys/stat.h where
necessary.

Glibc's fcntl.h includes bits/stat.h, which provides these symbols, so
these symbols can be used without explicitly including sys/stat.h. But,
based on the discussion in #37922, we should explicitly include relevant
headers, and should not rely on the indirect inclusion.

Similar to 4f18ff2e29, but for sys/stat.h.
2025-07-07 12:44:10 +09:00
Lennart Poettering 0ae2ac9759 Two follow-ups for recent PRs (#38062) 2025-07-06 22:06:52 +02:00
Yu Watanabe ffe8f21302 basic/include: replace _Static_assert() with static_assert()
If one of the header is included in a C++ source file, then using
_Static_assert() triggers compile error for some reasons.
Let's use static_assert(), which can be used by both C and C++ code.
2025-07-06 22:02:18 +09:00
Yu Watanabe 8d29f31bf6 namespace-util,nsresource: explicitly include sched.h
These source files uses symbols provided by sched.h, e.g.
setns(), unshare(), CLONE_NEWNS, and friends, but they do not explicitly
include sched.h. Currently, it is included indirectly via missing_syscall.h,
which is included by e.g. pidfd-util.h.
Let's explicitly include headers that provides symbols used in the code.

This is similar to 4f18ff2e29, but for sched.h.
2025-07-06 13:28:46 +02:00
cfba9b9eab tree-wide: several cleanups for reading/writing /proc/sys/fs/nr_open
- use unsigned for the return value of read_nr_open(), as it does not
  fail, and the kernel internally uses unsigned for the value,
- when bumping the value by PID1, let's start from the kernel's maximum
  value defined in fs/file.c. The maximum value should be mostly an API
  of the kernel, but may changed in a future, hence still try several
  times if we fail to bump the value.

Co-authored-by: Jared Baur <jaredbaur@fastmail.com>
Co-authored-by: John Rinehart <johnrichardrinehart@gmail.com>
2025-07-06 13:22:56 +02:00
Mike Yuan d33104eba2 cgroup-util: clean up skip_{slices,session,user_manager}
Let's avoid obscure memcmp()s in skip_* and instead use
strndupa() to extract the bits we care and call usual
string routines on it.
2025-07-05 22:26:23 +02:00
Mike Yuan 68c703872c string-table: drop unneeded initialization 2025-07-05 22:26:23 +02:00
Mike Yuan 1320e56283 chase: introduce openat_opath_with_automount() helper
Follow-up for c5de7b14ae

chase() is arguably a hot path in our code, hence it deserves
some caching whether open_tree() is available. Moreover,
the manual set of r to -EPERM feels kinda ugly. Let's
instead extract this bit into its own function.
2025-07-05 12:18:55 +02:00
Luca Boccassi b8fe5fa129 Small cleanups for tests (#38063) 2025-07-04 19:24:29 +01:00
Zbigniew Jędrzejewski-Szmek 54889c6cfb basic/rlimit-util.c: drop duplicate checks
parse_sec() and parse_time() already handle "infinity".
TEST(rlimit_parse_format) covered the first case, also extend it to cover
the second case.
2025-07-04 12:11:00 +02:00
Zbigniew Jędrzejewski-Szmek fb629de435 basic/rlimit-util.c: align table 2025-07-04 12:11:00 +02:00
Yu Watanabe c2f3bb742b Add systemd-analyze verb to list runtime unit properties (#37665) 2025-07-04 11:47:38 +09:00
Lennart Poettering c5de7b14ae chase: when chasing paths, trigger automounts
As it turns out open() with O_PATH does *not* trigger autofs, you get a
reference to the autofs inode, if not triggered.

But there's a way out: open_tree() (when specified without
OPEN_TREE_CLONE) is actually fully equivalent to open() with O_PATH –
with the exception of one thing: it *does* trigger automounts.

Thanks for Christian Brauner for pointing me to this and saving my day.

Fixes: #33155
2025-07-03 22:18:56 +01:00
Mike Yuan b68179c072 fd-util: Move RAW_O_LARGEFILE and O_ACCMODE_STRICT definitions to fd-util.h (#38051) 2025-07-03 22:27:39 +02:00
Zbigniew Jędrzejewski-Szmek 1c1626e063 shared/bus-unit-util: add bus_dump_transient_settings() helper
bus_append_unit_property() and associated functions accept a long list of
properties. But the specific names are only available through code. But it is
useful to be able to know the specific list of properties that is supported, in
particular for shell completions. Thus, add a way to list the properties that
are supported by the code.

In the future we could also turn this into a test for the documentation. For
various reasons, the list of properties listed in the docs is a partially
overlapping set. E.g. for service type, the pull request
https://github.com/systemd/systemd/pull/37661 creates a list with 212 entries,
and this code generates 7 entries less and 184 more. I didn't check all the
differences, but in the few cases I did, the list generated here was actually
correctly supported by 'systemd-run -p'.

A smoke test is added.
2025-07-03 21:03:26 +02:00
Lennart Poettering 77bf8f8905 conf-files: if CONF_FILES_REGULAR|CONF_FILES_DIRECTORY is used together allow either file or dir
This fixes a a bug introduced by
50c81130b6.
2025-07-03 17:41:07 +02:00
DaanDeMeyer 1543c2385c fd-util: Move O_ACCMODE_STRICT to fd-util.h
We're still discussing whether we want to remove this or not, but
regardless of whether we end up removing it or not, it's something
we add ourselves and as such should not live in our headers that
override glibc headers, so let's move it to fd-util.h.
2025-07-03 14:28:14 +02:00
DaanDeMeyer cefec8f6ce fd-util: Move RAW_O_LARGEFILE definition to fd-util.h
This is not something that comes from glibc, but which we invent
ourselves. As such, it should not be part of our overrides of glibc
headers, but instead should be part of one of our own headers, so
let's move it to fd-util.h.

Follow up for 194a690181
2025-07-03 14:18:50 +02:00
Daan De Meyer 76e3cc469d Move missing_xyz.h for glibc headers to src/basic/include/ (#37960) 2025-07-03 13:47:46 +02:00
Zbigniew Jędrzejewski-Szmek ff0bbeff3a basic/cgroup-util: align tables 2025-07-03 10:37:33 +02:00
Zbigniew Jędrzejewski-Szmek e47fb4abd2 Rename STRV_MAKE_EMPTY to just STRV_EMPTY
Even though this creates a stack allocation, it is not a function macro, so
drop the "make" from the name.
2025-07-03 10:37:33 +02:00
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 6020ca2c23 basic/errno-list: add helper to print errno names 2025-07-01 17:51:49 +02:00
Yu Watanabe 237bdd2771 basic: move missing_sched.h -> include/sched.h 2025-07-01 12:29:15 +09:00
Yu Watanabe 194a690181 basic: move missing_fcntl.h -> include/fcntl.h 2025-07-01 12:28:12 +09:00
Yu Watanabe 549f679a49 basic: move missing_socket.h -> include/sys/socket.h 2025-07-01 12:27:59 +09:00
Yu Watanabe 92953ea03d basic: move missing_pidfd.h -> include/sys/pidfd.h 2025-07-01 12:27:39 +09:00
Yu Watanabe bb66c2e480 basic: move missing_mman.h -> include/sys/mman.h
This also makes memfd-util.h include sys/mman.h, as it is necessary for
specifying flags to memfd_create_wrapper() or memfd_new_full().
2025-07-01 12:27:27 +09:00
Yu Watanabe 9a6dcc137c basic: move missing_wait.h -> include/sys/wait.h 2025-07-01 12:27:00 +09:00
Yu Watanabe 695921d7f8 basic: move missing_random.h -> include/sys/random.h 2025-07-01 12:24:32 +09: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
Mike Yuan 1b9400f2ee core/transaction: add job mode "lenient" as an even weaker version of _FAIL 2025-06-30 13:10:42 +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 9000db5f6e conf-files: fix an empty root handling in conf_files_list_strv()
Before 50c81130b6, the function used
chase(), hence if root is an empty string, each config directory made
prefixed with the current working directory if it is relative. See
implementation of chase().

With 50c81130b6, conf_files_list_strv()
internally uses chaseat(), hence each config directory is not prefixed
anymore even if it is relative.

To restore the previous behavior, this makes
- if root is an empty string, prefix each config directories with the
  current working directory if relative.
- if root is relative, make it absolute to make the prefixed results also
  absolute, and debugging logs show absolute paths.
- use chaseat_prefix_root() to prefix the results, for safety.

Follow-ups for 50c81130b6.
2025-06-30 05:35:03 +09:00
Yu Watanabe 8ce463639a path-util: move empty_or_root_to_null() from chase.c
And rename it to empty_or_root_harder_to_null(), as it also checks if
the input path effectively points to the root by calling path_is_root().
This also adds simple test cases for the function.
2025-06-30 03:45:29 +09:00
Yu Watanabe cdd3aba3df conf-files: fstatat() sets errno on failure
Follow-up for 50c81130b6.
2025-06-29 10:15:56 +09:00
Yu Watanabe 07c2fadbed Revert "conf-files: introduce CONF_FILES_CHASE_BASENAME flag"
This reverts commit 49653adef3.

The commit was merged without review, and has several issues.
Let's revert the change now, and address the issue pointed out by
the commit later in another way.
2025-06-29 10:15:56 +09:00
Yu Watanabe c43223ff9f conf-files: drop use of CONF_FILES_CHASE_BASENAME in conf_files_list_with_replacement()
The flag has several potential issues, and will be dropped in the next
commit.

Follow-up for 41fb58595a.
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 41fb58595a conf-files: rework conf_files_list_with_replacement()
Previously, symlinks in the replacement was not chased, hence we may
inserted a path to outside of the root directory, or we may have wrong
judgement whether we should insert the replacement or not.

This makes the symlinks in the replacement also resolved.
Also, as the function is only used by tmpfiles and sysusers, this
enables CONF_FILES_REGULAR, CONF_FILES_CHASE_BASENAME, and
CONF_FILES_FILTER_MASKED_BY_SYMLINK flags.
2025-06-28 04:13:43 +09:00
Yu Watanabe 49653adef3 conf-files: introduce CONF_FILES_CHASE_BASENAME flag
Previously, when a root directory is specified to conf_files_list() and
friends, the last component of each result was not resolved, even though
they internally chases the last component. So the caller needs to chase
the path again when the file is used.

This introduce a new flag that makes the whole path of each result is
resolved, hence caller can use it as is.
2025-06-28 04:13:43 +09:00
Yu Watanabe 50c81130b6 conf-files: chase symlink files in conf directories
Previously, symlinks in paths to conf directories are chased, but
symlink files in conf directories were not.

This also makes symlink files in conf directories chased. And, any
unresolvable symlinks are dropped, even if no verification is requested.
2025-06-28 04:13:38 +09:00
Yu Watanabe e93d8c844c conf-files: split CONF_FILES_FILTER_MASKED flag into two 2025-06-28 03:54:48 +09:00