Commit Graph
313 Commits
Author SHA1 Message Date
Lennart Poettering 015025cba2 cgroup-util: add pidref+full counter parts for cg_pid_get_user_unit()
This completes the set of functions for getting the user unit of a
process, mirroring the four functions we already have for the system
unit.
2025-09-25 14:09:55 +02:00
Lennart Poettering c24f405ace tree-wide: don't play games with alignment around file_handle
The payload of a file_handle structure is not 64bit aligned. So far used
_alignas_() to align it to 64bit as a whole, which by accident has the
side-effect that the payload ends up being aligned to 64bit too, but
this is ugly, because it's really just an accident...

Let's do this properly, and just use proper unaligned 64bit reads to
access the field, and do not assume aligning the structure as a whole
also aligns the payload part of it.

Follow-up for: fd51a7d8b5
2025-08-18 21:50:55 +09:00
Luca Boccassi 24b27a0000 cg: get attribute for requested controller in cg_is_empty
This was changed SYSTEMD_CGROUP_CONTROLLER instead of the provided
parameter, which broke sd-cgls. Change it back.

Fixes https://github.com/systemd/systemd/issues/38426

Follow-up for c3f900770d
2025-08-01 08:02:57 +09:00
Luca Boccassi a3eb0e99d3 cgroup-util: enforce alignment of f_handle
The change in 4d2e61211d
broke armv7 where uintptr_t is 4 bytes:

/* test_id */
Assertion '((uintptr_t) _p) % alignof(uint64_t) == 0' failed at src/basic/cgroup-util.c:108, function cg_get_cgroupid_at(). Aborting.

(gdb) p (uintptr_t)fh.file_handle.f_handle % _Alignof(uint64_t)
$9 = 4

Enforce that the structure is aligned as expected

Follow-up for 4d2e61211d
2025-07-25 20:42:23 +01:00
Lennart Poettering 7bb1147b00 cgroup-util: add cg_path_get_unit_full() helper and related calls
This helper returns not only the unit a cgroup belongs to, but also the
cgroup sub-path beyond it.
2025-07-11 18:15:08 +02:00
Yu Watanabe 3fc2a44043 include: move trivial kernel header wrappers to src/include/override/ 2025-07-11 13:05:46 +09:00
Lennart Poettering 726183627b cgroup: handle ENODEV on cg_read_pid() gracefully
The recently added test case TEST-07-PID1.subgroup-kill.sh surfaced a
race: if we enumerate PIDs in a cgroup, and the cgroup is unlinked at
the very same time reading will result in ENODEV. We need to handle that
gracefully. Hence let's do so.

Noticed while looking at:

https://github.com/systemd/systemd/actions/runs/16143084441/job/45554929264?pr=38120
2025-07-09 20:45:59 +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
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
Zbigniew Jędrzejewski-Szmek ff0bbeff3a basic/cgroup-util: align tables 2025-07-03 10:37:33 +02:00
Daan De Meyer 4fe348cfdf cgroup-util: Always open cgroupv2 attribute files in O_NONBLOCK mode
As explained in https://lore.kernel.org/all/20250419183545.1982187-1-shakeel.butt@linux.dev/,
writing to memory.max or memory.high triggers synchronous memory reclaim
if the limit is lowered. This can end up taking nonnegligible amounts
of time, completely blocking pid1 from doing any other work while the
reclaim is ongoing.

To address this problem, the kernel going to add O_NONBLOCK semantics
to memory.max and memory.high. If the file is opened with O_NONBLOCK,
the synchronous memory reclaim is skipped and only triggered later
without blocking the process writing the file. Let's make sure we make
use of this by opening cgroupv2 attribute files with O_NONBLOCK.

We opt to do this for all cgroupv2 attribute files, to make sure that
if the same problem happens elsewhere in the future and is fixed in the
same way, we immediately take advantage of that fix without having to
make changes in systemd as well. We probably never want to block when
writing cgroupv2 attributes and any cases where we do want to block should
indicate so explicitly instead of blocking by default.
2025-06-17 15:07:32 +01:00
Lennart Poettering eac4a028df sd-login: modernize return parameter handling
Let's rename the return parameters as "ret_xyz" systematically in
sd-login.

Also, let's make the return parameters systematically optional, like we
typically do these days. So far some where optional, other's weren't.
Let's clean this up.
2025-06-04 17:58:42 +02:00
Mike Yuan a941822d1c cgroup-util: assert on attribute for cg_{get,set}_attribute()
It makes zero sense to ever perform IO operations on cgroup dirs.
2025-05-27 14:31:06 +02:00
Mike Yuan 606ef91a40 cgroup-util: make cg_get_attribute_as_bool() return 0/1 directly
As with getenv_bool() and getxattr_at_bool(), to match
our usual style.
2025-05-27 14:28:08 +02:00
Mike Yuan 7b4022bdb0 cgroup-util: rename cg_get_xattr_malloc -> cg_get_xattr
This is not a trivial wrapper around user-created-buffer-based
syscall, so do not use _malloc suffix. Most of our functions
return an internally allocated buffer and this one's not special.
2025-05-27 14:28:08 +02:00
Mike Yuan b823f7bb6a cgroup-util: remove now unused cg_read_event()
cg_get_keyed_attribute() is a more generic version of this,
and cg_is_empty_recursive() was the only user of this function,
which got converted in the previous commit.
2025-05-26 22:59:48 +02:00
Mike Yuan c3f900770d cgroup-util: drop handcrafted cg_is_empty(), always check cgroup.events populated field
This effectively renames cg_is_empty_recursive() to cg_is_empty().
Note that all existing code calls the former and not the latter,
hence with cgv1 support being dropped it's trivial to consult
cgroup.events directly for populated state everywhere.

Additionally, use more generic cg_get_keyed_attribute() helper
rather than cg_read_event().
2025-05-26 22:59:48 +02:00
Daan De Meyer 0c15577abe basic + fundamental: Clean up includes
Split out of #37344.
2025-05-25 10:06:07 +02:00
Jelle van der Waa 684cd01f0e cgroup-util: rename 's' -> 'killed_pids' 2025-05-24 16:58:56 +02:00
Lennart Poettering ceed11e465 path-util: invert PATH_STARTSWITH_ACCEPT_DOT_DOT flag
As requested: https://github.com/systemd/systemd/pull/37572#pullrequestreview-2861928094
2025-05-23 06:51:45 +02:00
Lennart Poettering 482107724f cgroup: port some code over to path_startswith_full() 2025-05-23 06:51:43 +02:00
Mike Yuan dbe592a067 cgroup-util: modernize cg_get_keyed_attribute()
- assert on supplied keys being unique
- Reject duplicate attributes with -EBADMSG
- Rename 'ret_values' to just 'values', given we don't
  allocate the array
- Remove now unused cg_get_keyed_attribute_graceful()
2025-05-19 21:24:18 +02:00
Daan De Meyer ef10d4b526 Bugprone argument comment - round 5 (#37481) 2025-05-15 20:33:50 +02:00
Jelle van der Waa 346c62f9ca treewide: correct argument comments 2025-05-15 15:39:20 +02:00
Mike Yuan 29da53dde3 core: always enable CPU accounting
Our baseline is v5.4 and cgroup v2 is enforced now,
which means CPU accounting is cheap everywhere without
requiring any controller, hence just remove the directive.
2025-05-15 02:19:16 +02:00
Mike Yuan 68a2435cc2 cgroup-util: drop cg_kill() cgroup.threads kludge
Our baseline is v5.4 now.
2025-05-15 07:26:03 +09:00
Jelle van der Waa 09755ba511 treewide: fix argument comment for getxattr_at_bool 2025-05-14 11:15:45 +02:00
Daan De Meyer fda652117e fs-util: Reduce transitive includes 2025-05-07 22:04:46 +09:00
Mike Yuan 497e607ca0 cgroup-util: drop now unused cg_ns_supported() 2025-04-21 16:54:37 +02:00
Mike Yuan 3c86a935ad cgroup-util: drop cg_freezer_supported(), assume supported 2025-04-21 16:29:40 +02:00
Mike Yuan 77a0d2e3ec core/cgroup: remove legacy hierarchy setup
This also makes pin_cgroupfs_fd actually pin the fs root,
so that we can port all cg_* helpers to operate under
the fd later on.
2025-04-04 15:34:51 +02:00
Mike Yuan 2a010ef8fd cgroup-util: remove now unused cg_kernel_controllers() 2025-04-04 14:36:14 +02:00
Lennart Poettering 2d47f43b43 cgroup-util: remove cg_get_xattr(), it's apparently unused 2025-03-18 07:17:10 +01:00
Lennart Poettering 33cbda04eb xattr-util: rework getxattr_at_malloc()
Let's return the size in a return parameter instead of the return value.
And if NULL is specified this tells us the caller doesn't care about the
size and expects a NUL terminated string. In that case look for an
embedded NUL byte, and refuse in that case.

This should lock things down a bit, as we'll systematically refuse
embedded NUL strings now when we expect strings.
2025-03-18 07:17:10 +01:00
Mike Yuan b66fd12135 cgroup-util: drop is_cgroup_fs()
No need to bother with any cgroup v1 stuff anymore.
2025-03-16 18:02:08 +01:00
Michal Koutný 6c5703f9bc cgroup-util: Handle capsule@ paths like user@ paths
The capsule instances are related to user instances, so treat them
equally to user@.service when handling cgroup paths. This also saves us
from polluting public libsystemd API with variant for capsules too.

Fix: #36098
2025-03-11 10:37:19 +01:00
Yu Watanabe e75372958d missing_threads.h: threads.h exists since glibc-2.28 2025-03-04 02:24:49 +09:00
Lennart Poettering 336acebc77 basic: port various pidfd/pidref helpers to PIDFD_GET_INFO and PIDFD_GET_*_NAMESPACE (#35242)
Supersedes #35308 (cherry-picked one commit and replaced the rest)

(I left a few comments that's folded by GitHub. Please make sure to
check them too.)
2025-01-06 11:23:08 +01:00
Lennart Poettering dd445d6e99 cgroup-util: add remoteness checks to all cg_pidref_get_xyz() calls 2025-01-06 09:54:41 +01:00
Lennart Poettering 92d78966fd cgroup-util: add pidref counterparts for cg_pid_get_session() + cg_pid_get_owner_uid() 2025-01-06 09:54:41 +01:00
Mike Yuan 9598708a12 cgroup-util: explain why cg_pidref_get_path() cannot be ported over to pidfd helpers (yet)
See also: https://github.com/systemd/systemd/pull/35242#issuecomment-2506686806
2025-01-04 17:48:23 +01:00
Mike Yuan f1ba5c900b cgroup-util: introduce generic cg_path_from_cgroupid() helper
Taken from nsresourced/userns-registry.c userns_destroy_cgroup()
2025-01-04 17:48:22 +01:00
Mike Yuan eded4272d2 cgroup-util: introduce cg_get_cgroupid_at()
Suggested in https://github.com/systemd/systemd/pull/35242#discussion_r1862658163
2024-12-12 05:19:07 +09:00
Mike Yuan 4da9f38de1 cgroup-util: use RET_NERRNO where appropriate 2024-11-27 18:38:00 +01:00
Luca Boccassi b7eefa1996 cgroup-util: fix memory leak on error
CID#1565824

Follow-up for f6793bbcf0
2024-11-21 14:02:34 +09:00
Lennart Poettering f6793bbcf0 killall: gracefully handle processes inserted into containers via nsenter -a
"nsenter -a" doesn't migrate the specified process into the target
cgroup (it really should). Thus the cgroup will remain in a cgroup
that is (due to cgroup ns) outside our visibility. The kernel will
report the cgroup path of such cgroups as starting with "/../". Detect
that and print a reasonably error message instead of trying to resolve
that.
2024-11-20 18:11:38 +00:00
Mike Yuan 3f8999a76e fs-util: rename laccess to access_nofollow
In order to distinguish it from libc function naming.
2024-10-05 01:30:43 +02:00
Daan De Meyer 1ce69e0661 Revert "cgroup-util: Don't try to open pidfd for kernel threads"
The kernel patch was reverted so let's try again to open pidfds
for kernel threads.

This reverts commit ead48ec35c.
2024-08-21 14:32:54 +02:00
Yu Watanabe 564547d295 Merge pull request #33911 from YHNdnzj/cgroup-setup-cleanup
cgroup-setup/util: several cleanups; make use of cgroup.kill on client request
2024-08-03 06:20:02 +09:00
Yu Watanabe ec4964692a cgroup-util: fix typo
Follow-up for 0fbb569de1.
2024-08-03 05:48:54 +09:00