Commit Graph

157 Commits

Author SHA1 Message Date
Daan De Meyer
f102bc3e5f tree-wide: Introduce sd-forward.h and shared-forward.h headers
Let's not leak details from src/shared and src/libsystemd into
src/basic, even though you can't actually do anything useful with
just forward declarations from src/shared.

The sd-forward.h header is put in src/libsystemd/sd-common as we
don't have a directory for shared internal headers for libsystemd
yet.

Let's also rename forward.h to basic-forward.h to keep things
self-explanatory.
2025-10-16 17:00:29 +02:00
Mike Yuan
180341e732 basic,shared: mark eligible functions with _pure_/_const_
Following the recent witch hunt done to static inline functions
I figure we should at least give the compiler some more hint
on optimizations.
2025-05-26 20:26:58 +02:00
Daan De Meyer
0c15577abe basic + fundamental: Clean up includes
Split out of #37344.
2025-05-25 10:06:07 +02:00
Mike Yuan
5fd63a5a5e process-util: introduce procfs_file_get_field() wrapper
which combines procfs_file_alloca() and get_proc_field()
2025-05-01 13:10:26 +09:00
Mike Yuan
3589d6c01f process-util: assert on pid in procfs_file_alloca(), use strjoina() 2025-05-01 13:10:26 +09:00
Yu Watanabe
147f1d7b26 process-util: make wait_for_terminate() as trivial wrapper of its PidRef version 2025-04-03 00:11:27 +09:00
Lennart Poettering
4ecc87bf1c process-util: add pidref-based version of wait_for_terminate_and_check() 2025-04-03 00:11:27 +09:00
Mike Yuan
37149e692a process-util: introduce SIGINFO_CODE_IS_DEAD helper 2025-02-21 18:08:02 +01:00
Daan De Meyer
f48103ea61 process-util: Implement safe_fork_full() on top of pidref_safe_fork_full()
Let's switch things around, and move the internals of safe_fork_full() into
pidref_safe_fork_full() and make safe_fork_full() a trivial wrapper on top
of pidref_safe_fork_full().
2025-02-20 20:13:53 +01:00
Lennart Poettering
4ace93da8c pidref: now that we have the cached pidfdid of our own process, use it
Note that this drops a lot of "const" qualifiers on PidRef arguments.
That's because pidref_is_self() suddenly might end changing the PidRef
because it acquires the pidfd ID.

We had this previously already with pidfd_equal(), but this amplifies
the problem.

I guess we C's "const" doesn't really work for stuff that contains
caches, that is just conceptually constant, but not actually.
2025-01-20 21:51:40 +01:00
Lennart Poettering
d6267b9b18 process-util: port pid_from_same_root_fs() to pidref, and port three places over to it 2025-01-16 11:55:21 +01:00
Lennart Poettering
6eeeef9f66 process-util: introduce new FORK_FREEZE flag for safe_fork()
Often we want to fork off a process that just hangs until we kill it,
let's add a simple flag to create one of this type, and use it at
various places.
2025-01-16 11:55:21 +01:00
Lennart Poettering
9237a63a80 process-util: add new helper pidref_get_ppid_as_pidref() 2025-01-10 14:14:17 +01:00
Ivan Kruglov
64db44f7fb process-util: read_errno() 2025-01-09 10:47:24 +01:00
Mike Yuan
47f64104d1 process-util: port pidref_get_uid() and pidref_is_my_child() to pidfd helpers 2025-01-04 17:48:22 +01:00
Mike Yuan
a33f691374 process-util: move namespace_get_leader() to namespace-util
This allows us to drop the hack for recursive includes.
2025-01-04 17:08:00 +01:00
Mike Yuan
b234026d09 process-util: extract pidfd-related funcs into pidfd-util.[ch] 2025-01-04 16:58:13 +01:00
Lennart Poettering
25b1a73f71 journald: get rid of get_process_capeff(), use pidref_get_capability() instead
This does pretty much the same, but is nicer, since it parses things
properly.
2024-12-17 19:06:54 +01:00
Ivan Kruglov
a567de392d process-util: introduce report_errno_and_exit() as part of src/basic/process-util.{h,c} 2024-11-06 11:18:38 +01:00
Daan De Meyer
406f177501 core: Introduce PrivatePIDs=
This new setting allows unsharing the pid namespace in a unit. Because
you have to fork to get a process into a pid namespace, we fork in
systemd-executor to get into the new pid namespace. The parent then
sends the pid of the child process back to the manager and exits while
the child process continues on with the rest of exec_invoke() and then
executes the actual payload.

Communicating the child pid is done via a new pidref socket pair that is
set up on manager startup.

We unshare the PID namespace right before the mount namespace so we
mount procfs correctly. Note PrivatePIDs=yes always implies MountAPIVFS=yes
to mount procfs.

When running unprivileged in a user session, user namespace is set up first
to allow for PID namespace to be unshared. However, when running in
privileged mode, we unshare the user namespace last to ensure the user
namespace does not own the PID namespace and cannot break out of the sandbox.

Note we disallow Type=forking services from using PrivatePIDs=yes since the
init proess inside the PID namespace must not exit for other processes in
the namespace to exist.

Note Daan De Meyer did the original work for this commit with Ryan Wilson
addressing follow-ups.

Co-authored-by: Daan De Meyer <daan.j.demeyer@gmail.com>
2024-11-05 05:32:02 -08:00
Lennart Poettering
625f109604 sd-json: drop sd_json_dispatch_pid() again, as we prefer json_dispatch_pidref() now
The calls are now unused, and we generally prefer if people send a PID
triplet rather than a single PID, hence stop supporting a high-level
dispacher for pid_t.
2024-10-14 12:47:17 +02:00
Lennart Poettering
de34ec188c pidref: hookup PID_AUTOMATIC special pid_t value with PidRef
The PID_AUTOMATIC value is now properly recognized by the PidRef logic
too. This needed some massaging of header includes, to ensure pidref.h
can access process-util.h's definitions and vice versa.
2024-10-14 12:44:36 +02:00
Ivan Kruglov
dafd44de4f sd-json: add sd_json_dispatch_pid() 2024-10-10 13:40:45 +02:00
Zbigniew Jędrzejewski-Szmek
bfd5a0687f various: move const ptr indicator to return value 2024-06-19 16:28:28 +02:00
Mike Yuan
6fb97a85c7 process-util: make pid*_get_start_time return usec_t 2024-05-22 18:47:16 +08:00
Adrian Vovk
85f660d46b fd-util: Expose helper to pack fds into 3,4,5,...
This is useful for situations where an array of FDs is to be passed into
a child process (i.e. by passing it through safe_fork). This function
can be called in the child (before calling exec) to pack the FDs to all
be next to each-other starting from SD_LISTEN_FDS_START (i.e. 3)
2024-02-19 11:18:11 +00:00
Frantisek Sumsal
14ce9b4ed8 process-util: unify the hex value format a bit
And fix the man page reference as well.
2024-02-07 19:29:53 +01:00
Frantisek Sumsal
3dc51ab2cf process-util: use only the least significant byte from personality()
The personality() syscall returns a 32-bit value where the top three
bytes are reserved for flags that emulate historical or architectural
quirks, and only the least significant byte reflects the actual
personality we're interested in (in opinionated_personality()).

Use the newly defined mask in the corresponding test as well, otherwise
the test fails on some more "exotic" architectures that set some of the
"quirk" flags:

~# uname -m
armv7l
~# build/test-seccomp
...
/* test_lock_personality */
current personality=0x0
safe_personality(PERSONALITY_INVALID)=0x800000
Assertion '(unsigned long) safe_personality(current) == current' failed at src/test/test-seccomp.c:970, function test_lock_personality(). Aborting.
lockpersonalityseccomp terminated by signal ABRT.
Assertion 'wait_for_terminate_and_check("lockpersonalityseccomp", pid, WAIT_LOG) == EXIT_SUCCESS' failed at src/test/test-seccomp.c:996, function test_lock_personality(). Aborting.
Aborted (core dumped)

See: personality(2) and comments in sys/personality.h
2024-02-07 19:29:53 +01:00
Mike Yuan
c90335403c process-util: minor follow-up for pidfd_spawn 2024-02-06 12:26:38 +00:00
Luca Boccassi
2e106312e2 core: add support for pidfd_spawn
Added in glibc 2.39, allows cloning into a cgroup and to get
a pid fd back instead of a pid. Removes race conditions for
both changing cgroups and getting a reliable reference for the
child process.

Fixes https://github.com/systemd/systemd/pull/18843
Replaces https://github.com/systemd/systemd/pull/16706
2024-02-05 21:52:36 +00:00
Luca Boccassi
556d2bc4a1 core: use PidRef in exec_spawn 2024-02-01 21:06:14 +00:00
Yu Watanabe
387f39ea30 process-util: introduce FORK_NEW_NETNS for safe_fork()
Similar to FORK_NEW_MOUNTNS or FORK_NEW_USERNS.
2024-01-19 15:06:08 +09:00
Lennart Poettering
3dee63b762 process-util: add new pid{ref,}_get_start_time() helper
This also adds a test case that test pidref_safe_fork(), pidref_wait()
and related calls.
2024-01-02 17:57:34 +01:00
Lennart Poettering
f17132260f process-util: add pidref_safe_fork() helper
This combines safe_fork() with pidref_set_pid().

Eventually we really should switch this to use CLONE_PIDFD, but as that
is not wrapped by glibc yet, it's hard. But this is not crucial anyway,
as a child we just forked off can always safely be referenced also by
PID, given the reaping is under our own control.

A simple test case is added in a follow-up commit.
2024-01-02 17:57:34 +01:00
Lennart Poettering
e9ccae3135 process-util: add new FORK_DEATHSIG_SIGKILL flag, rename FORK_DEATHSIG → FORK_DEATHSIG_SIGTERM
Sometimes it makes sense to hard kill a client if we die. Let's hence
add a third FORK_DEATHSIG flag for this purpose: FORK_DEATHSIG_SIGKILL.

To make things less confusing this also renames FORK_DEATHSIG to
FORK_DEATHSIG_SIGTERM to make clear it sends SIGTERM. We already had
FORK_DEATHSIG_SIGINT, hence this makes things nicely symmetric.

A bunch of users are switched over for FORK_DEATHSIG_SIGKILL where we
know it's safe to abort things abruptly. This should make some kernel
cases more robust, since we cannot get confused by signal masks or such.

While we are at it, also fix a bunch of bugs where we didn't take
FORK_DEATHSIG_SIGINT into account in safe_fork()
2023-11-02 14:09:23 +01:00
Lennart Poettering
eefb7d22ce process-util: add API for enumerating processes in /proc/ and pinning them via PidRef 2023-10-18 14:49:40 +02:00
Lennart Poettering
4d9f092b5e process-util: add pidref_is_unwaited() and make pid_is_unwaited() return errors 2023-10-18 14:49:40 +02:00
Lennart Poettering
6774be4206 process-util: add pidref_is_my_child() 2023-10-18 14:49:40 +02:00
Lennart Poettering
becdfcb9f1 process-util: change pid_is_alive() to not eat up errors, and add pidref_is_alive()
Let's no eat up errors, but propagate unexpected ones.
2023-10-18 14:40:25 +02:00
Lennart Poettering
8b51341545 process-util: add pidref_get_uid() and rename get_process_uid() → pidref_get_uid() 2023-10-18 14:39:33 +02:00
Lennart Poettering
d7d748548b process-util: add pidref_get_comm() and rename get_process_comm() to pid_get_comm() 2023-10-18 14:39:33 +02:00
Lennart Poettering
fc87713bed process-util: add pidref_is_kernel_thread() 2023-10-18 14:39:33 +02:00
Lennart Poettering
a034620f1a process-util: add pidref_get_cmdline() 2023-10-18 14:39:33 +02:00
Lennart Poettering
cde8cc946b Merge pull request #29272 from enr0n/coredump-container
coredump: support forwarding coredumps to containers
2023-10-16 16:13:16 +02:00
Nick Rosbrook
ade39d9ab8 process-util: introduce namespace_get_leader helper
For a given PID and namespace type, this helper function gives the PID
of the leader of the namespace containing the given PID. Use this in
systemd-coredump instead of using the existing get_mount_namespace_leader.

This helper will be used again in a later commit.
2023-10-13 15:13:11 -04:00
Luca Boccassi
6ecdfe7d10 process-util: add posix_spawn helper
This provides CLONE_VM + CLONE_VFORK semantics, so it is useful to
avoid CoW traps and other issues around doing work between fork()
and exec().
2023-10-12 13:37:22 +01:00
Lennart Poettering
8c3fe1b5b5 process-util: add simple wrapper around PR_SET_CHILD_SUBREAPER
Let's a simple helper that knows how to deal with PID == 1.
2023-06-23 10:05:16 +02:00
Lennart Poettering
2e7b105eb9 process-util: add FORK_DETACH flag for forking of detached child
A test for this is later added indirectly, via aynchronous_rm_rf() that
uses this and comes with a suitable test.
2023-06-23 10:02:15 +02:00
Lennart Poettering
01ab446c35 basic: add comments about raw_clone() calls not supporting threads/malloc in child 2023-06-23 10:02:15 +02:00
Lennart Poettering
29c3520f28 process-util: add clone_with_nested_stack() helper
This wraps glibc's clone() but deals with the 'stack' parameter in a
sensible way. Only supports invocations without CLONE_VM, i.e. when
child is a CoW copy of parent.
2023-06-23 10:00:30 +02:00