Commit Graph
520 Commits
Author SHA1 Message Date
Daan De Meyer 4ea4abb651 core: Remove circular dependencies between headers
Currently there are various circular dependencies between headers
in core/. Let's get rid of these by making judicious use of forward
declarations and moving includes into implementation files instead of
having them in header files.

Getting rid of circular header includes simplifies the code and makes
various clang based tooling such as iwyu work much better on our code.

The most important change is getting rid of the manager.h include in
unit.h which is possible thanks to the previous commits. We also move
the OOMPolicy and StatusType enums to unit.h to remove the need for
other unit headers to include manager.h to get access to these enums.
2025-04-23 10:33:35 +02:00
Mike Yuan 418082596c namespace-util: generalize namespace_type_supported() 2025-04-21 16:54:37 +02:00
Zbigniew Jędrzejewski-Szmek 1ae9b0cfa8 basic/glyph-util: rename "special glyph" to just "glyph"
Admittedly, some of our glyphs _are_ special, e.g. "O=" for SPECIAL_GLYPH_TOUCH ;)
But we don't need this in the name. The very long names make some invocations
very wordy, e.g. special_glyph(SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY).
Also, I want to add GLYPH_SPACE, which is not special at all.
2025-03-15 14:40:39 +01:00
Mike Yuan 5d873679b1 core/socket: include peer pidfd id in socket service instance if available 2025-01-04 17:07:58 +01:00
Mike Yuan 58a86a1504 core/socket: introduce intermediate SOCKET_START_OPEN state
Prior to this commit, if no Exec*= is defined for socket,
and the unit was in SOCKET_FAILED state, failure of socket_open_fds()
would induce state transition SOCKET_FAILED -> SOCKET_FAILED,
and OnFailure= deps get unexpectedly skipped. Let's introduce
an intermediate state, so that during unit start we enter
UNIT_ACTIVATING at least once.

Fixes #35635
2024-12-31 00:22:52 +01:00
Mike Yuan 80706c3269 core/socket: clean up usbffs handling
- Log about errors in socket_open_fds()
- Open parent dir fd and operate under it, instead of constructing paths
  everywhere
- Replace scandir() with readdir_all()
2024-12-31 00:22:52 +01:00
Mike Yuan d1e3536d6b core/socket: close fds also on SOCKET_CLEAN state
Follow-up for c968d76a38

We'd only transition to SOCKET_CLEAN state if previously
inactive (see socket_clean()), hence no fds shall be persisted
in this state.
2024-12-30 21:43:52 +01:00
Mike Yuan 240457204b core/socket: use correct kill operation for SOCKET_STOP_PRE_SIGTERM
Follow-up for a232ebcc2c
2024-12-30 21:43:52 +01:00
Mike Yuan 2fbdfe0b0d core/socket: trivial coding style cleanups 2024-12-30 21:43:52 +01:00
Mike Yuan 703b1b7f24 core/service: preserve RuntimeDirectory= even if oneshot service exits
Follow-up for c26948c6da

We only want to get rid of cred mount here, and RuntimeDirectory=
is documented to be retained for SERVICE_EXITED state.

Fixes #35427
2024-12-02 10:57:45 +01:00
Lennart Poettering acc35e5129 core/service: don't propagate stop jobs if RestartMode=direct (#34768)
Fixes https://github.com/systemd/systemd/issues/34758
2024-11-01 17:25:49 +01:00
David Michael 3eec82f6b3 socket: support setting ownership of message queues
This applies the existing SocketUser=/SocketGroup= options to units
defining a POSIX message queue, bringing them in line with UNIX
sockets and FIFOs.  They are set on the file descriptor rather than
a file system path because the /dev/mqueue path interface is an
optional mount unit.
2024-10-28 23:40:42 +01:00
Mike Yuan d993ad6c6f core/manager: introduce manager_add_job_full() which takes extra TransactionAddFlags
No functional change. Preparation for later commits.
2024-10-27 20:02:46 +01:00
Yu Watanabe f769518c9a tree-wide: drop doubled empty lines 2024-10-07 09:51:37 +02:00
Luca Boccassi 5162829ec8 core: do BindMount/MountImage operations in async control process
These operations might require slow I/O, and thus might block PID1's main
loop for an undeterminated amount of time. Instead of performing them
inline, fork a worker process and stash away the D-Bus message, and reply
once we get a SIGCHILD indicating they have completed. That way we don't
break compatibility and callers can continue to rely on the fact that when
they get the method reply the operation either succeeded or failed.

To keep backward compatibility, unlike reload control processes, these
are ran inside init.scope and not the target cgroup. Unlike ExecReload,
this is under our control and is not defined by the unit. This is necessary
because previously the operation also wasn't ran from the target cgroup,
so suddenly forking a copy-on-write copy of pid1 into the target cgroup
will make memory usage spike, and if there is a MemoryMax= or MemoryHigh=
set and the cgroup is already close to the limit, it will cause an OOM
kill, where previously it would have worked fine.
2024-08-29 12:48:55 +01:00
Mike Yuan daa78907af core: honor FileDescriptorName= too for Accept=yes sockets
So far we manually hardcoded $LISTEN_FDNAMES to "varlink" in various
varlink service units we ship, even though FileDescriptorName=varlink
is specified in associated socket units already, because
FileDescriptorName= is currently silently ignored when combined with
Accept=yes. Let's step away from this, which seems saner.

Note that this is technically a compat break, but a mostly negligible
one as there shall be few users setting FileDescriptorName= but
still expecting LISTEN_FDNAMES=connection in the actual executable.

Preparation for #34080
2024-08-26 15:40:15 +02:00
Mike Yuan 2234032c47 core: move check for combination of PAMName= + KillMode= to unit_verify_contexts()
While at it, allow "mixed" for all unit types too, i.e.
also apply ebc2259da1 to
socket/mount/swap units.
2024-08-26 14:52:25 +02:00
Mike Yuan bca5505839 core/socket: refuse MaxConnection=0 for Accept=no sockets too
This makes no sense at all, and we already refuse such setting
for Accept=yes sockets. I see no reason not to extend this to
Accept=no ones.
2024-08-26 14:52:25 +02:00
Mike Yuan f220d98945 core/socket: use UNIT_ISSET rather than _DEREF where suitable 2024-08-26 14:52:25 +02:00
Daan De Meyer 14702b9c18 core: Use RateLimit struct to store ratelimits
This makes it easier to serialize these ratelimits with
JSON_BUILD_CALLBACK().
2024-08-14 14:18:40 +02:00
Mike Yuan 95e631da43 core/unit: unexport cg_kill log funcs, rather take in bool
It seems unnecessary to duplicate the func ptrs everywhere.
2024-08-02 16:36:08 +02:00
Mike Yuan 4c523d62d2 core/socket: stop hardcoding every service inactive state
History (c068650fcf,
941a12dcba) has proven
that we're not good at keeping socket and service states
in sync. Instead, let's query the high-level unit_active_state()
first, and only hardcode the two special auto-restart
service states.

Additionally, allow returning to listening state on SERVICE_CLEANING.
2024-07-31 21:56:28 +02:00
Michal Sekletar 941a12dcba socket: fix socket activation of stopped services with pinned FD store 2024-07-31 16:29:07 +02:00
Zbigniew Jędrzejewski-Szmek ff3f29537c various: move ptr indicator to return value 2024-06-19 16:37:12 +02:00
Mike Yuan 42fdb14286 core: cast ignored retval of deserialize_* to void
Fixes CID#1547098
2024-06-17 16:56:19 +02:00
Mike Yuan 42e9967baf core/socket: use FOREACH_ARRAY at one more place 2024-06-17 16:56:19 +02:00
Zbigniew Jędrzejewski-Szmek 6623c64a02 selinux: kill mac_selinux_free()
It was a redundant wrapper around freecon.

test-selinux needs to be adjusted because it now needs to be linked to
libselinux, instead of pulling it in via libshared.
2024-06-12 15:21:21 +02:00
Yu Watanabe a07c903a38 Merge pull request #33071 from keszybz/sd_event_source-cleanup
Clean up calls to sd_event_source_set_enabled()
2024-06-12 18:42:14 +09:00
Matthieu Baerts (NGI0) 3f69070598 core/socket: allow MPTCP protocol
Multipath TCP (MPTCP), standardized in RFC8684 [1], is a TCP extension
that enables a TCP connection to use different paths. It allows a device
to make use of multiple interfaces at once to send and receive TCP
packets over a single MPTCP connection. MPTCP can aggregate the
bandwidth of multiple interfaces or prefer the one with the lowest
latency, it also allows a fail-over if one path is down, and the traffic
is seamlessly re-injected on other paths.

To benefit from MPTCP, both the client and the server have to support
it. Multipath TCP is a backward-compatible TCP extension that is enabled
by default on recent Linux distributions (Debian, Ubuntu, Redhat, ...).
Multipath TCP is included in the Linux kernel since version 5.6 [2]. To
use it on Linux, an application must explicitly enable it when creating
the socket:

  int sd = socket(AF_INET(6), SOCK_STREAM, IPPROTO_MPTCP);

No need to change anything else in the application.

This patch allows MPTCP protocol in the Socket unit configuration. So
now, a <unit>.socket can contain this to use MPTCP instead of TCP:

  [Socket]
  SocketProtocol=mptcp

MPTCP support has been allowed similarly to what has been already done
to allow SCTP: just one line in core/socket.c, a very simple addition
thanks to the flexible architecture already in place.

On top of that, IPPROTO_MPTCP has also been added in the list of allowed
protocols in two other places, and in the doc. It has also been added to
the missing_network.h file, for systems with an old libc -- note that it
was also required to include <netinet/in.h> in this file to avoid
redefinition errors.

Link: https://www.rfc-editor.org/rfc/rfc8684.html [1]
Link: https://www.mptcp.dev [2]
2024-06-12 00:14:08 +01:00
Zbigniew Jędrzejewski-Szmek a3ada90a37 various: remove unnecessary check before sd_event_source_set_enabled()
sd_event_source_set_enabled() does the same check internally.
2024-05-30 12:11:58 +02:00
Lennart Poettering 1f41b10ee0 socket: hookup handoff timestamps with processes forked off by socket units 2024-04-25 13:40:41 +02:00
Lennart Poettering 3a7bb5c99c manager: use proper unicode arrow in dump 2024-04-24 16:15:53 +02:00
Daan De Meyer aaa872a713 core: Serialize both pid and pidfd to keep downgrades working
Currently, when downgrading from a version with pidfd support to a
version without pidfd support, all information about running processes
is lost as the newer systemd will serialized pidfds which are not recognized
by the older systemd when deserializing.

To improve the situation, let's serialize both the pid and the pidfd.
This is safe because existing versions will either replace the first
deserialized pidref with the second one or discard the second one in
favor of the first one depending on the unit and field. Older versions
that don't support pidfd's will silently discard any fields that contain
a pidfd as those will try to parse the field as a pid and since a pidfd
field will start with '@', those versions will debug error log and ignore
the value.

To make sure we reuse the existing pidfd as much as possible, the pidfd
is serialized first. Both for scopes and service main pids, if the same
pid is seen multiple times, the first pidref is kept. So by serializing
the pidfd first we make sure the original pidfd is used instead of the
new one which is opened when deserializing the first pid field.

For other control units, older versions with pidfd support will discard
the first pidfd and replace it with a new pidfd from the second pid field.
This is a slight regression on downgrades, but we make sure it doesn't
happen for future versions (and older versions when this commit is
backported) by modifying the logic to only use the first successfully
deserialized pidref so that the raw pid without pidfd is discarded instead
of it replacing the existing pidfd.
2024-04-05 12:56:26 +09:00
Yu Watanabe 17f6b64038 core: align table 2024-03-29 13:51:15 +09:00
Jakub Sitnicki 97df75d7bd socket: pass socket FDs to all ExecXYZ= commands but ExecStartPre=
Today listen file descriptors created by socket unit don't get passed to
commands in Exec{Start,Stop}{Pre,Post}= socket options.

This prevents ExecXYZ= commands from accessing the created socket FDs to do
any kind of system setup which involves the socket but is not covered by
existing socket unit options.

One concrete example is to insert a socket FD into a BPF map capable of
holding socket references, such as BPF sockmap/sockhash [1] or
reuseport_sockarray [2]. Or, similarly, send the file descriptor with
SCM_RIGHTS to another process, which has access to a BPF map for storing
sockets.

To unblock this use case, pass ListenXYZ= file descriptors to ExecXYZ=
commands as listen FDs [4]. As an exception, ExecStartPre= command does not
inherit any file descriptors because it gets invoked before the listen FDs
are created.

This new behavior can potentially break existing configurations. Commands
invoked from ExecXYZ= might not expect to inherit file descriptors through
sd_listen_fds protocol.

To prevent breakage, add a new socket unit parameter,
PassFileDescriptorsToExec=, to control whether ExecXYZ= programs inherit
listen FDs.

[1] https://docs.kernel.org/bpf/map_sockmap.html
[2] https://lore.kernel.org/r/20180808075917.3009181-1-kafai@fb.com
[3] https://man.archlinux.org/man/socket.7#SO_INCOMING_CPU
[4] https://www.freedesktop.org/software/systemd/man/latest/sd_listen_fds.html
2024-03-27 01:41:26 +08:00
Mike Yuan 54668cb9a0 core/socket: use FOREACH_ARRAY at one more place 2024-03-24 01:08:40 +08:00
Mike Yuan d6a911e906 core/socket: clean up socket peer handling a bit
Currently, SocketPeer object acquired through
socket_acquire_peer() are referenced twice
in socket_enter_running and service_set_socket_fd,
and the reference taken by former gets dropped
through _cleanup_. This is a bit confusing.
Let's just pass ownership instead.
2024-03-24 01:08:40 +08:00
Mike Yuan 23908d8410 core/socket: remove duplicate unit_add_two_dependencies 2024-03-24 01:08:40 +08:00
Mike Yuan f61e4e5380 core/socket: validate service unit load state before continuing
Fixes #31915
2024-03-24 01:08:40 +08:00
Mike Yuan e9fa1bf704 core: use ASSERT_PTR(CAST(u)) everywhere 2024-03-22 01:36:37 +08:00
Lennart Poettering 9cc545447e core: split out cgroup specific state fields from Unit → CGroupRuntime
This refactors the Unit structure a bit: all cgroup-related state fields
are moved to a new structure CGroupRuntime, which is only allocated as
we realize a cgroup.

This is both a nice cleanup and should make unit structures considerably
smaller that have no cgroup associated, because never realized or
because they belong to a unit type that doesn#t have cgroups anyway.

This makes things nicely symmetric:

        ExecContext → static user configuration about execution
        ExecRuntime → dynamic user state of execution
        CGroupContext → static user configuration about cgroups
        CGroupRuntime → dynamic user state of cgroups

And each time the XyzContext is part of the unit type structures such as
Service or Slice that need it, but the runtime object is only allocated
when a unit is started.
2024-02-16 10:17:40 +01:00
Lennart Poettering 48930a5ded pid1: make MaxConnectionsPerSource= also work for AF_UNIX sockets
The setting currently puts limits on connections per IP address and
AF_UNIX CID. Let's extend it to cover AF_UNIX too, where it puts a limit
on connections per UID.

This is particularly useful for the various Accept=yes Varlink services
we now have, as it means, the number of per-user instance services
cannot grow without bounds.
2024-02-12 11:57:16 +01:00
Luca Boccassi 556d2bc4a1 core: use PidRef in exec_spawn 2024-02-01 21:06:14 +00:00
Yu Watanabe 6b09c99cba core/socket: introduce SOCKET_STATE_WITH_PROCESS() helper function 2024-01-27 23:08:42 +09:00
Yu Watanabe ea1e0bf10b core: introduce unit_unwatch_pidref_done() helper function
No functional change, just refactoring.
2024-01-26 00:31:02 +09:00
Yu Watanabe b826e31754 core: use helper functions like unit_main_pid() in unit_kill_context()
No functional changes. Just refactoring.
2024-01-24 14:43:18 +09:00
Yu Watanabe c01a5c0527 siphash24: introduce siphash24_compress_typesafe() macro
To prevent copy-and-paste mistake.

This also introduce in_addr_hash_func().

No functional change, just refactoring.
2023-12-25 15:38:59 +09:00
Lennart Poettering 8017ed7e0e service: don't try to determine selinux label for socket activation if RootImage= is used
We cannot determine the SELinux label ahead of time if RootImage= is
used, since we'd have to mount the image then, hence don't, and handle
this cleanly, and gracefully.

While we are at it, stop "reaching over" so much from the socket code to
the service code, and instead provide function that most of the hard
work in service.c that socket.c just calls.

While we are at it, add debug logging and stuff.

I noticed the issue when also noticing #30560, but that one is harder to
fix, hence I avoided it for now.
2023-12-22 11:51:51 +09:00
Luca Boccassi 9e615fa3aa core: add WantsMountsFor=
This is the equivalent of RequiresMountsFor=, but adds Wants= instead
of Requires=. It will be useful for example for the autogenerated
systemd-cryptsetup units.

Fixes https://github.com/systemd/systemd/issues/11646
2023-11-29 11:04:59 +00:00
Zbigniew Jędrzejewski-Szmek 51ad723d20 core: serialize and deserialize trigger ratelimits for socket and path 2023-11-22 12:32:31 +00:00