]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/cgroup-util.h
fix: UnsetProperty example in systemd.link.xml
[thirdparty/systemd.git] / src / basic / cgroup-util.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c2f1db8f 2#pragma once
8c6db833 3
0c15577a 4#include "forward.h"
8c6db833 5
68ac0d05
LP
6#define SYSTEMD_CGROUP_CONTROLLER_LEGACY "name=systemd"
7#define SYSTEMD_CGROUP_CONTROLLER_HYBRID "name=unified"
8#define SYSTEMD_CGROUP_CONTROLLER "_systemd"
9
efdb0237
LP
10/* An enum of well known cgroup controllers */
11typedef enum CGroupController {
17f14955 12 /* Original cgroup controllers */
efdb0237 13 CGROUP_CONTROLLER_CPU,
00b4a247 14 CGROUP_CONTROLLER_CPUACCT, /* v1 only */
047f5d63 15 CGROUP_CONTROLLER_CPUSET, /* v2 only */
00b4a247
LP
16 CGROUP_CONTROLLER_IO, /* v2 only */
17 CGROUP_CONTROLLER_BLKIO, /* v1 only */
efdb0237 18 CGROUP_CONTROLLER_MEMORY,
00b4a247 19 CGROUP_CONTROLLER_DEVICES, /* v1 only */
03a7b521 20 CGROUP_CONTROLLER_PIDS,
17f14955
RG
21
22 /* BPF-based pseudo-controllers, v2 only */
23 CGROUP_CONTROLLER_BPF_FIREWALL,
084c7007 24 CGROUP_CONTROLLER_BPF_DEVICES,
506ea51b 25 CGROUP_CONTROLLER_BPF_FOREIGN,
a8e5eb17 26 CGROUP_CONTROLLER_BPF_SOCKET_BIND,
6f50d4f7 27 CGROUP_CONTROLLER_BPF_RESTRICT_NETWORK_INTERFACES,
b1994387 28 /* The BPF hook implementing RestrictFileSystems= is not defined here.
bb5232b6 29 * It's applied as late as possible in exec_invoke() so we don't block
b1994387 30 * our own unit setup code. */
17f14955 31
efdb0237 32 _CGROUP_CONTROLLER_MAX,
2d93c20e 33 _CGROUP_CONTROLLER_INVALID = -EINVAL,
efdb0237
LP
34} CGroupController;
35
46f84f95 36#define CGROUP_CONTROLLER_TO_MASK(c) (1U << (c))
efdb0237 37
4ad49000 38/* A bit mask of well known cgroup controllers */
efdb0237
LP
39typedef enum CGroupMask {
40 CGROUP_MASK_CPU = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_CPU),
41 CGROUP_MASK_CPUACCT = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_CPUACCT),
047f5d63 42 CGROUP_MASK_CPUSET = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_CPUSET),
13c31542 43 CGROUP_MASK_IO = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_IO),
efdb0237
LP
44 CGROUP_MASK_BLKIO = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_BLKIO),
45 CGROUP_MASK_MEMORY = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_MEMORY),
3905f127 46 CGROUP_MASK_DEVICES = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_DEVICES),
03a7b521 47 CGROUP_MASK_PIDS = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_PIDS),
17f14955 48 CGROUP_MASK_BPF_FIREWALL = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_BPF_FIREWALL),
084c7007 49 CGROUP_MASK_BPF_DEVICES = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_BPF_DEVICES),
506ea51b 50 CGROUP_MASK_BPF_FOREIGN = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_BPF_FOREIGN),
a8e5eb17 51 CGROUP_MASK_BPF_SOCKET_BIND = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_BPF_SOCKET_BIND),
6f50d4f7 52 CGROUP_MASK_BPF_RESTRICT_NETWORK_INTERFACES = CGROUP_CONTROLLER_TO_MASK(CGROUP_CONTROLLER_BPF_RESTRICT_NETWORK_INTERFACES),
4edd65e4 53
4e1dfa45 54 /* All real cgroup v1 controllers */
4edd65e4
LP
55 CGROUP_MASK_V1 = CGROUP_MASK_CPU|CGROUP_MASK_CPUACCT|CGROUP_MASK_BLKIO|CGROUP_MASK_MEMORY|CGROUP_MASK_DEVICES|CGROUP_MASK_PIDS,
56
4e1dfa45 57 /* All real cgroup v2 controllers */
047f5d63 58 CGROUP_MASK_V2 = CGROUP_MASK_CPU|CGROUP_MASK_CPUSET|CGROUP_MASK_IO|CGROUP_MASK_MEMORY|CGROUP_MASK_PIDS,
4edd65e4 59
d282d55d 60 /* All controllers we want to delegate in case of Delegate=yes. Which are pretty much the v2 controllers only, as delegation on v1 is not safe, and bpf stuff isn't a real controller */
677e6c14
LP
61 CGROUP_MASK_DELEGATE = CGROUP_MASK_V2,
62
4e1dfa45 63 /* All cgroup v2 BPF pseudo-controllers */
6f50d4f7 64 CGROUP_MASK_BPF = CGROUP_MASK_BPF_FIREWALL|CGROUP_MASK_BPF_DEVICES|CGROUP_MASK_BPF_FOREIGN|CGROUP_MASK_BPF_SOCKET_BIND|CGROUP_MASK_BPF_RESTRICT_NETWORK_INTERFACES,
4edd65e4 65
677e6c14 66 _CGROUP_MASK_ALL = CGROUP_CONTROLLER_TO_MASK(_CGROUP_CONTROLLER_MAX) - 1,
efdb0237 67} CGroupMask;
4ad49000 68
13c31542 69/* Special values for all weight knobs on unified hierarchy */
fd4e991d 70#define CGROUP_WEIGHT_INVALID UINT64_MAX
c8340822 71#define CGROUP_WEIGHT_IDLE UINT64_C(0)
13c31542
TH
72#define CGROUP_WEIGHT_MIN UINT64_C(1)
73#define CGROUP_WEIGHT_MAX UINT64_C(10000)
74#define CGROUP_WEIGHT_DEFAULT UINT64_C(100)
75
76#define CGROUP_LIMIT_MIN UINT64_C(0)
fd4e991d 77#define CGROUP_LIMIT_MAX UINT64_MAX
13c31542
TH
78
79static inline bool CGROUP_WEIGHT_IS_OK(uint64_t x) {
80 return
81 x == CGROUP_WEIGHT_INVALID ||
82 (x >= CGROUP_WEIGHT_MIN && x <= CGROUP_WEIGHT_MAX);
83}
84
9be57249
TH
85/* IO limits on unified hierarchy */
86typedef enum CGroupIOLimitType {
87 CGROUP_IO_RBPS_MAX,
88 CGROUP_IO_WBPS_MAX,
ac06a0cf
TH
89 CGROUP_IO_RIOPS_MAX,
90 CGROUP_IO_WIOPS_MAX,
9be57249
TH
91
92 _CGROUP_IO_LIMIT_TYPE_MAX,
2d93c20e 93 _CGROUP_IO_LIMIT_TYPE_INVALID = -EINVAL,
9be57249
TH
94} CGroupIOLimitType;
95
96extern const uint64_t cgroup_io_limit_defaults[_CGROUP_IO_LIMIT_TYPE_MAX];
97
98const char* cgroup_io_limit_type_to_string(CGroupIOLimitType t) _const_;
99CGroupIOLimitType cgroup_io_limit_type_from_string(const char *s) _pure_;
100
9e73d375 101/* Special values for the io.bfq.weight attribute */
d2bd0bfa
MK
102#define CGROUP_BFQ_WEIGHT_INVALID UINT64_MAX
103#define CGROUP_BFQ_WEIGHT_MIN UINT64_C(1)
104#define CGROUP_BFQ_WEIGHT_MAX UINT64_C(1000)
105#define CGROUP_BFQ_WEIGHT_DEFAULT UINT64_C(100)
106
107/* Convert the normal io.weight value to io.bfq.weight */
108static inline uint64_t BFQ_WEIGHT(uint64_t io_weight) {
109 return
110 io_weight <= CGROUP_WEIGHT_DEFAULT ?
111 CGROUP_BFQ_WEIGHT_DEFAULT - (CGROUP_WEIGHT_DEFAULT - io_weight) * (CGROUP_BFQ_WEIGHT_DEFAULT - CGROUP_BFQ_WEIGHT_MIN) / (CGROUP_WEIGHT_DEFAULT - CGROUP_WEIGHT_MIN) :
112 CGROUP_BFQ_WEIGHT_DEFAULT + (io_weight - CGROUP_WEIGHT_DEFAULT) * (CGROUP_BFQ_WEIGHT_MAX - CGROUP_BFQ_WEIGHT_DEFAULT) / (CGROUP_WEIGHT_MAX - CGROUP_WEIGHT_DEFAULT);
113}
114
5da38d07
TH
115typedef enum CGroupUnified {
116 CGROUP_UNIFIED_UNKNOWN = -1,
117 CGROUP_UNIFIED_NONE = 0, /* Both systemd and controllers on legacy */
118 CGROUP_UNIFIED_SYSTEMD = 1, /* Only systemd on unified */
119 CGROUP_UNIFIED_ALL = 2, /* Both systemd and controllers on unified */
120} CGroupUnified;
121
5954c074
LP
122/*
123 * General rules:
124 *
125 * We accept named hierarchies in the syntax "foo" and "name=foo".
126 *
127 * We expect that named hierarchies do not conflict in name with a
128 * kernel hierarchy, modulo the "name=" prefix.
129 *
130 * We always generate "normalized" controller names, i.e. without the
131 * "name=" prefix.
132 *
133 * We require absolute cgroup paths. When returning, we will always
134 * generate paths with multiple adjacent / removed.
135 */
136
3b287495 137int cg_path_open(const char *controller, const char *path);
93117b0f 138int cg_cgroupid_open(int cgroupfs_fd, uint64_t id);
3b287495 139
f1ba5c90 140int cg_path_from_cgroupid(int cgroupfs_fd, uint64_t id, char **ret);
eded4272
MY
141int cg_get_cgroupid_at(int dfd, const char *path, uint64_t *ret);
142static inline int cg_path_get_cgroupid(const char *path, uint64_t *ret) {
143 return cg_get_cgroupid_at(AT_FDCWD, path, ret);
144}
145static inline int cg_fd_get_cgroupid(int fd, uint64_t *ret) {
146 return cg_get_cgroupid_at(fd, NULL, ret);
147}
148
00f17143
TR
149typedef enum CGroupFlags {
150 CGROUP_SIGCONT = 1 << 0,
151 CGROUP_IGNORE_SELF = 1 << 1,
031860d6 152 CGROUP_DONT_SKIP_UNMAPPED = 1 << 2,
00f17143
TR
153} CGroupFlags;
154
aef43552 155int cg_enumerate_processes(const char *controller, const char *path, FILE **ret);
00f17143
TR
156int cg_read_pid(FILE *f, pid_t *ret, CGroupFlags flags);
157int cg_read_pidref(FILE *f, PidRef *ret, CGroupFlags flags);
c6c18be3 158
aef43552
LP
159int cg_enumerate_subgroups(const char *controller, const char *path, DIR **ret);
160int cg_read_subgroup(DIR *d, char **ret);
35d2e7ec 161
4d1b2df1 162typedef int (*cg_kill_log_func_t)(const PidRef *pid, int sig, void *userdata);
1d98fef1 163
3ce64b0f 164int cg_kill(const char *path, int sig, CGroupFlags flags, Set *killed_pids, cg_kill_log_func_t log_kill, void *userdata);
bd1791b5 165int cg_kill_kernel_sigkill(const char *path);
3ce64b0f 166int cg_kill_recursive(const char *path, int sig, CGroupFlags flags, Set *killed_pids, cg_kill_log_func_t log_kill, void *userdata);
1d98fef1 167
2a8020fe 168int cg_split_spec(const char *spec, char **ret_controller, char **ret_path);
820fe745 169int cg_mangle_path(const char *path, char **ret);
8c6db833 170
820fe745
LP
171int cg_get_path(const char *controller, const char *path, const char *suffix, char **ret);
172int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **ret);
7027ff61 173
820fe745 174int cg_pid_get_path(const char *controller, pid_t pid, char **ret);
f2a2e60b 175int cg_pidref_get_path(const char *controller, const PidRef *pidref, char **ret);
8c6db833 176
bd1791b5 177int cg_is_threaded(const char *path);
084e7706 178
b426b4ee 179int cg_is_delegated(const char *path);
6414203c 180int cg_is_delegated_fd(int fd);
b426b4ee 181
6cf96ab4
NR
182int cg_has_coredump_receive(const char *path);
183
4ad49000 184int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value);
934277fe 185int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret);
dbe592a0 186int cg_get_keyed_attribute(const char *controller, const char *path, const char *attribute, char * const *keys, char **values);
4ad49000 187
613328c3 188int cg_get_attribute_as_uint64(const char *controller, const char *path, const char *attribute, uint64_t *ret);
606ef91a 189int cg_get_attribute_as_bool(const char *controller, const char *path, const char *attribute);
b41dcc51 190
bd1791b5 191int cg_get_owner(const char *path, uid_t *ret_uid);
8c6db833 192
bd1791b5 193int cg_set_xattr(const char *path, const char *name, const void *value, size_t size, int flags);
7b4022bd 194int cg_get_xattr(const char *path, const char *name, char **ret, size_t *ret_size);
59331b8e 195/* Returns negative on error, and 0 or 1 on success for the bool value */
bd1791b5
LP
196int cg_get_xattr_bool(const char *path, const char *name);
197int cg_remove_xattr(const char *path, const char *name);
4b58153d 198
6f883237 199int cg_is_empty(const char *controller, const char *path);
8c6db833 200
7027ff61 201int cg_get_root_path(char **path);
6c03089c 202
820fe745
LP
203int cg_path_get_session(const char *path, char **ret_session);
204int cg_path_get_owner_uid(const char *path, uid_t *ret_uid);
205int cg_path_get_unit(const char *path, char **ret_unit);
206int cg_path_get_unit_path(const char *path, char **ret_unit);
207int cg_path_get_user_unit(const char *path, char **ret_unit);
208int cg_path_get_machine_name(const char *path, char **ret_machine);
209int cg_path_get_slice(const char *path, char **ret_slice);
210int cg_path_get_user_slice(const char *path, char **ret_slice);
211
212int cg_shift_path(const char *cgroup, const char *cached_root, const char **ret_shifted);
213int cg_pid_get_path_shifted(pid_t pid, const char *cached_root, char **ret_cgroup);
214
215int cg_pid_get_session(pid_t pid, char **ret_session);
92d78966 216int cg_pidref_get_session(const PidRef *pidref, char **ret);
820fe745 217int cg_pid_get_owner_uid(pid_t pid, uid_t *ret_uid);
92d78966 218int cg_pidref_get_owner_uid(const PidRef *pidref, uid_t *ret);
820fe745 219int cg_pid_get_unit(pid_t pid, char **ret_unit);
2c302e89 220int cg_pidref_get_unit(const PidRef *pidref, char **ret);
820fe745
LP
221int cg_pid_get_user_unit(pid_t pid, char **ret_unit);
222int cg_pid_get_machine_name(pid_t pid, char **ret_machine);
223int cg_pid_get_slice(pid_t pid, char **ret_slice);
224int cg_pid_get_user_slice(pid_t pid, char **ret_slice);
225
226int cg_path_decode_unit(const char *cgroup, char **ret_unit);
96cde13a 227
1a56b0c0
LP
228bool cg_needs_escape(const char *p);
229int cg_escape(const char *p, char **ret);
ff3f2953 230char* cg_unescape(const char *p) _pure_;
78edb35a 231
185a0874 232bool cg_controller_is_valid(const char *p);
a016b922
LP
233
234int cg_slice_to_path(const char *unit, char **ret);
4ad49000 235
efdb0237 236int cg_mask_supported(CGroupMask *ret);
0fa7b500 237int cg_mask_supported_subtree(const char *root, CGroupMask *ret);
aae7e17f
FB
238int cg_mask_from_string(const char *s, CGroupMask *ret);
239int cg_mask_to_string(CGroupMask mask, char **ret);
b12afc8c 240
8a513eee 241bool cg_kill_supported(void);
3228995c 242
b4cccbc1
LP
243int cg_all_unified(void);
244int cg_hybrid_unified(void);
c22800e4 245int cg_unified_controller(const char *controller);
d4d99bc6
ZJS
246int cg_unified_cached(bool flush);
247static inline int cg_unified(void) {
248 return cg_unified_cached(true);
249}
efdb0237
LP
250
251const char* cgroup_controller_to_string(CGroupController c) _const_;
252CGroupController cgroup_controller_from_string(const char *s) _pure_;
d53d9474 253
4d824a4e
AZ
254typedef enum ManagedOOMMode {
255 MANAGED_OOM_AUTO,
256 MANAGED_OOM_KILL,
257 _MANAGED_OOM_MODE_MAX,
2d93c20e 258 _MANAGED_OOM_MODE_INVALID = -EINVAL,
4d824a4e
AZ
259} ManagedOOMMode;
260
261const char* managed_oom_mode_to_string(ManagedOOMMode m) _const_;
262ManagedOOMMode managed_oom_mode_from_string(const char *s) _pure_;
242d75bd
AZ
263
264typedef enum ManagedOOMPreference {
265 MANAGED_OOM_PREFERENCE_NONE = 0,
266 MANAGED_OOM_PREFERENCE_AVOID = 1,
267 MANAGED_OOM_PREFERENCE_OMIT = 2,
268 _MANAGED_OOM_PREFERENCE_MAX,
925484aa 269 _MANAGED_OOM_PREFERENCE_INVALID = -EINVAL,
242d75bd
AZ
270} ManagedOOMPreference;
271
272const char* managed_oom_preference_to_string(ManagedOOMPreference a) _const_;
273ManagedOOMPreference managed_oom_preference_from_string(const char *s) _pure_;