]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/cgroup.h
core: add new new bus call for migrating foreign processes to scope/service units
[thirdparty/systemd.git] / src / core / cgroup.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c2f1db8f 2#pragma once
8e274523
LP
3
4/***
5 This file is part of systemd.
6
4ad49000 7 Copyright 2013 Lennart Poettering
8e274523
LP
8
9 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
8e274523
LP
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 17 Lesser General Public License for more details.
8e274523 18
5430f7f2 19 You should have received a copy of the GNU Lesser General Public License
8e274523
LP
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21***/
22
c1ff5570
TA
23#include <stdbool.h>
24
6a48d82f
DM
25#include "cgroup-util.h"
26#include "ip-address-access.h"
4ad49000 27#include "list.h"
c1ff5570 28#include "time-util.h"
8e274523 29
4ad49000
LP
30typedef struct CGroupContext CGroupContext;
31typedef struct CGroupDeviceAllow CGroupDeviceAllow;
13c31542
TH
32typedef struct CGroupIODeviceWeight CGroupIODeviceWeight;
33typedef struct CGroupIODeviceLimit CGroupIODeviceLimit;
4ad49000
LP
34typedef struct CGroupBlockIODeviceWeight CGroupBlockIODeviceWeight;
35typedef struct CGroupBlockIODeviceBandwidth CGroupBlockIODeviceBandwidth;
8e274523 36
4ad49000 37typedef enum CGroupDevicePolicy {
8e274523 38
4ad49000
LP
39 /* When devices listed, will allow those, plus built-in ones,
40 if none are listed will allow everything. */
41 CGROUP_AUTO,
8e274523 42
4ad49000
LP
43 /* Everything forbidden, except built-in ones and listed ones. */
44 CGROUP_CLOSED,
9d58f1db 45
4ad49000
LP
46 /* Everythings forbidden, except for the listed devices */
47 CGROUP_STRICT,
9d58f1db 48
4ad49000
LP
49 _CGROUP_DEVICE_POLICY_MAX,
50 _CGROUP_DEVICE_POLICY_INVALID = -1
51} CGroupDevicePolicy;
8e274523 52
4ad49000
LP
53struct CGroupDeviceAllow {
54 LIST_FIELDS(CGroupDeviceAllow, device_allow);
55 char *path;
56 bool r:1;
57 bool w:1;
58 bool m:1;
59};
8c6db833 60
13c31542
TH
61struct CGroupIODeviceWeight {
62 LIST_FIELDS(CGroupIODeviceWeight, device_weights);
63 char *path;
64 uint64_t weight;
65};
66
67struct CGroupIODeviceLimit {
68 LIST_FIELDS(CGroupIODeviceLimit, device_limits);
69 char *path;
9be57249 70 uint64_t limits[_CGROUP_IO_LIMIT_TYPE_MAX];
13c31542
TH
71};
72
4ad49000
LP
73struct CGroupBlockIODeviceWeight {
74 LIST_FIELDS(CGroupBlockIODeviceWeight, device_weights);
75 char *path;
d53d9474 76 uint64_t weight;
8e274523
LP
77};
78
4ad49000
LP
79struct CGroupBlockIODeviceBandwidth {
80 LIST_FIELDS(CGroupBlockIODeviceBandwidth, device_bandwidths);
81 char *path;
979d0311
TH
82 uint64_t rbps;
83 uint64_t wbps;
4ad49000 84};
8e274523 85
4ad49000
LP
86struct CGroupContext {
87 bool cpu_accounting;
13c31542 88 bool io_accounting;
4ad49000
LP
89 bool blockio_accounting;
90 bool memory_accounting;
03a7b521 91 bool tasks_accounting;
6a48d82f 92 bool ip_accounting;
8e274523 93
13c31542 94 /* For unified hierarchy */
66ebf6c0
TH
95 uint64_t cpu_weight;
96 uint64_t startup_cpu_weight;
97 usec_t cpu_quota_per_sec_usec;
98
13c31542
TH
99 uint64_t io_weight;
100 uint64_t startup_io_weight;
101 LIST_HEAD(CGroupIODeviceWeight, io_device_weights);
102 LIST_HEAD(CGroupIODeviceLimit, io_device_limits);
103
da4d897e
TH
104 uint64_t memory_low;
105 uint64_t memory_high;
106 uint64_t memory_max;
96e131ea 107 uint64_t memory_swap_max;
da4d897e 108
6a48d82f
DM
109 LIST_HEAD(IPAddressAccessItem, ip_address_allow);
110 LIST_HEAD(IPAddressAccessItem, ip_address_deny);
111
13c31542 112 /* For legacy hierarchies */
d53d9474
LP
113 uint64_t cpu_shares;
114 uint64_t startup_cpu_shares;
8e274523 115
d53d9474
LP
116 uint64_t blockio_weight;
117 uint64_t startup_blockio_weight;
4ad49000
LP
118 LIST_HEAD(CGroupBlockIODeviceWeight, blockio_device_weights);
119 LIST_HEAD(CGroupBlockIODeviceBandwidth, blockio_device_bandwidths);
246aa6dd 120
4ad49000 121 uint64_t memory_limit;
64747e2d 122
4ad49000
LP
123 CGroupDevicePolicy device_policy;
124 LIST_HEAD(CGroupDeviceAllow, device_allow);
a931ad47 125
13c31542 126 /* Common */
03a7b521 127 uint64_t tasks_max;
d53d9474
LP
128
129 bool delegate;
02638280 130 CGroupMask delegate_controllers;
4ad49000 131};
64747e2d 132
906c06f6
DM
133/* Used when querying IP accounting data */
134typedef enum CGroupIPAccountingMetric {
135 CGROUP_IP_INGRESS_BYTES,
136 CGROUP_IP_INGRESS_PACKETS,
137 CGROUP_IP_EGRESS_BYTES,
138 CGROUP_IP_EGRESS_PACKETS,
139 _CGROUP_IP_ACCOUNTING_METRIC_MAX,
140 _CGROUP_IP_ACCOUNTING_METRIC_INVALID = -1,
141} CGroupIPAccountingMetric;
142
71d35b6b 143#include "unit.h"
8e274523 144
4ad49000
LP
145void cgroup_context_init(CGroupContext *c);
146void cgroup_context_done(CGroupContext *c);
147void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix);
bc432dc7 148
efdb0237 149CGroupMask cgroup_context_get_mask(CGroupContext *c);
fb385181 150
4ad49000 151void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a);
13c31542
TH
152void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w);
153void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l);
4ad49000
LP
154void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w);
155void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b);
8e274523 156
efdb0237 157CGroupMask unit_get_own_mask(Unit *u);
02638280 158CGroupMask unit_get_delegate_mask(Unit *u);
efdb0237 159CGroupMask unit_get_members_mask(Unit *u);
02638280 160CGroupMask unit_get_siblings_mask(Unit *u);
efdb0237
LP
161CGroupMask unit_get_subtree_mask(Unit *u);
162
163CGroupMask unit_get_target_mask(Unit *u);
164CGroupMask unit_get_enable_mask(Unit *u);
bc432dc7 165
906c06f6
DM
166bool unit_get_needs_bpf(Unit *u);
167
bc432dc7 168void unit_update_cgroup_members_masks(Unit *u);
efdb0237 169
6592b975 170const char *unit_get_realized_cgroup_path(Unit *u, CGroupMask mask);
efdb0237
LP
171char *unit_default_cgroup_path(Unit *u);
172int unit_set_cgroup_path(Unit *u, const char *path);
a4634b21 173int unit_pick_cgroup_path(Unit *u);
efdb0237 174
0a1eb06d 175int unit_realize_cgroup(Unit *u);
efdb0237
LP
176void unit_release_cgroup(Unit *u);
177void unit_prune_cgroup(Unit *u);
178int unit_watch_cgroup(Unit *u);
179
09e24654
LP
180void unit_add_to_cgroup_empty_queue(Unit *u);
181
6592b975 182int unit_attach_pids_to_cgroup(Unit *u, Set *pids, const char *suffix_path);
8e274523 183
4ad49000
LP
184int manager_setup_cgroup(Manager *m);
185void manager_shutdown_cgroup(Manager *m, bool delete);
6dde1f33 186
91a6073e 187unsigned manager_dispatch_cgroup_realize_queue(Manager *m);
4fbf50b3 188
4ad49000 189Unit *manager_get_unit_by_cgroup(Manager *m, const char *cgroup);
b3ac818b 190Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid);
4ad49000 191Unit* manager_get_unit_by_pid(Manager *m, pid_t pid);
8e274523 192
efdb0237
LP
193int unit_search_main_pid(Unit *u, pid_t *ret);
194int unit_watch_all_pids(Unit *u);
8e274523 195
11aef522
LP
196int unit_synthesize_cgroup_empty_event(Unit *u);
197
5ad096b3 198int unit_get_memory_current(Unit *u, uint64_t *ret);
03a7b521 199int unit_get_tasks_current(Unit *u, uint64_t *ret);
5ad096b3 200int unit_get_cpu_usage(Unit *u, nsec_t *ret);
906c06f6
DM
201int unit_get_ip_accounting(Unit *u, CGroupIPAccountingMetric metric, uint64_t *ret);
202
203int unit_reset_cpu_accounting(Unit *u);
204int unit_reset_ip_accounting(Unit *u);
5ad096b3 205
2e4025c0
ZJS
206#define UNIT_CGROUP_BOOL(u, name) \
207 ({ \
208 CGroupContext *cc = unit_get_cgroup_context(u); \
209 cc ? cc->name : false; \
210 })
e9db43d5 211
f3725e64
LP
212bool unit_has_root_cgroup(Unit *u);
213
efdb0237
LP
214int manager_notify_cgroup_empty(Manager *m, const char *group);
215
e7ab4d1a 216void unit_invalidate_cgroup(Unit *u, CGroupMask m);
906c06f6 217void unit_invalidate_cgroup_bpf(Unit *u);
e7ab4d1a
LP
218
219void manager_invalidate_startup_units(Manager *m);
220
4ad49000
LP
221const char* cgroup_device_policy_to_string(CGroupDevicePolicy i) _const_;
222CGroupDevicePolicy cgroup_device_policy_from_string(const char *s) _pure_;
1d9cc876
LP
223
224bool unit_cgroup_delegate(Unit *u);