]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/cgroup.h
Merge pull request #1269 from zonque/netcls
[thirdparty/systemd.git] / src / core / cgroup.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6 This file is part of systemd.
7
8 Copyright 2013 Lennart Poettering
9
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <stdbool.h>
25
26 #include "list.h"
27 #include "time-util.h"
28
29 /* Maximum value for fixed (manual) net class ID assignment,
30 * and also the value at which the range of automatic assignments starts
31 */
32 #define CGROUP_NETCLASS_FIXED_MAX UINT32_C(65535)
33
34 typedef struct CGroupContext CGroupContext;
35 typedef struct CGroupDeviceAllow CGroupDeviceAllow;
36 typedef struct CGroupBlockIODeviceWeight CGroupBlockIODeviceWeight;
37 typedef struct CGroupBlockIODeviceBandwidth CGroupBlockIODeviceBandwidth;
38
39 typedef enum CGroupDevicePolicy {
40
41 /* When devices listed, will allow those, plus built-in ones,
42 if none are listed will allow everything. */
43 CGROUP_AUTO,
44
45 /* Everything forbidden, except built-in ones and listed ones. */
46 CGROUP_CLOSED,
47
48 /* Everythings forbidden, except for the listed devices */
49 CGROUP_STRICT,
50
51 _CGROUP_DEVICE_POLICY_MAX,
52 _CGROUP_DEVICE_POLICY_INVALID = -1
53 } CGroupDevicePolicy;
54
55 typedef enum CGroupNetClassType {
56 /* Default - do not assign a net class */
57 CGROUP_NETCLASS_TYPE_NONE,
58
59 /* Automatically assign a net class */
60 CGROUP_NETCLASS_TYPE_AUTO,
61
62 /* Assign the net class that was provided by the user */
63 CGROUP_NETCLASS_TYPE_FIXED,
64 } CGroupNetClassType;
65
66 struct CGroupDeviceAllow {
67 LIST_FIELDS(CGroupDeviceAllow, device_allow);
68 char *path;
69 bool r:1;
70 bool w:1;
71 bool m:1;
72 };
73
74 struct CGroupBlockIODeviceWeight {
75 LIST_FIELDS(CGroupBlockIODeviceWeight, device_weights);
76 char *path;
77 uint64_t weight;
78 };
79
80 struct CGroupBlockIODeviceBandwidth {
81 LIST_FIELDS(CGroupBlockIODeviceBandwidth, device_bandwidths);
82 char *path;
83 uint64_t bandwidth;
84 bool read;
85 };
86
87 struct CGroupContext {
88 bool cpu_accounting;
89 bool blockio_accounting;
90 bool memory_accounting;
91 bool tasks_accounting;
92
93 uint64_t cpu_shares;
94 uint64_t startup_cpu_shares;
95 usec_t cpu_quota_per_sec_usec;
96
97 uint64_t blockio_weight;
98 uint64_t startup_blockio_weight;
99 LIST_HEAD(CGroupBlockIODeviceWeight, blockio_device_weights);
100 LIST_HEAD(CGroupBlockIODeviceBandwidth, blockio_device_bandwidths);
101
102 uint64_t memory_limit;
103
104 CGroupDevicePolicy device_policy;
105 LIST_HEAD(CGroupDeviceAllow, device_allow);
106
107 CGroupNetClassType netclass_type;
108 uint32_t netclass_id;
109
110 uint64_t tasks_max;
111
112 bool delegate;
113 };
114
115 #include "unit.h"
116 #include "cgroup-util.h"
117
118 void cgroup_context_init(CGroupContext *c);
119 void cgroup_context_done(CGroupContext *c);
120 void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix);
121 void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, uint32_t netclass_id, ManagerState state);
122
123 CGroupMask cgroup_context_get_mask(CGroupContext *c);
124
125 void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a);
126 void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w);
127 void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b);
128
129 CGroupMask unit_get_own_mask(Unit *u);
130 CGroupMask unit_get_siblings_mask(Unit *u);
131 CGroupMask unit_get_members_mask(Unit *u);
132 CGroupMask unit_get_subtree_mask(Unit *u);
133
134 CGroupMask unit_get_target_mask(Unit *u);
135 CGroupMask unit_get_enable_mask(Unit *u);
136
137 void unit_update_cgroup_members_masks(Unit *u);
138
139 char *unit_default_cgroup_path(Unit *u);
140 int unit_set_cgroup_path(Unit *u, const char *path);
141
142 int unit_realize_cgroup(Unit *u);
143 void unit_release_cgroup(Unit *u);
144 void unit_prune_cgroup(Unit *u);
145 int unit_watch_cgroup(Unit *u);
146
147 int unit_attach_pids_to_cgroup(Unit *u);
148
149 int unit_add_to_netclass_cgroup(Unit *u);
150 int unit_remove_from_netclass_cgroup(Unit *u);
151
152 int manager_setup_cgroup(Manager *m);
153 void manager_shutdown_cgroup(Manager *m, bool delete);
154
155 unsigned manager_dispatch_cgroup_queue(Manager *m);
156
157 Unit *manager_get_unit_by_cgroup(Manager *m, const char *cgroup);
158 Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid);
159 Unit* manager_get_unit_by_pid(Manager *m, pid_t pid);
160
161 int unit_search_main_pid(Unit *u, pid_t *ret);
162 int unit_watch_all_pids(Unit *u);
163
164 int unit_get_memory_current(Unit *u, uint64_t *ret);
165 int unit_get_tasks_current(Unit *u, uint64_t *ret);
166 int unit_get_cpu_usage(Unit *u, nsec_t *ret);
167 int unit_reset_cpu_usage(Unit *u);
168
169 bool unit_cgroup_delegate(Unit *u);
170
171 int unit_notify_cgroup_empty(Unit *u);
172 int manager_notify_cgroup_empty(Manager *m, const char *group);
173
174 void unit_invalidate_cgroup(Unit *u, CGroupMask m);
175
176 void manager_invalidate_startup_units(Manager *m);
177
178 const char* cgroup_device_policy_to_string(CGroupDevicePolicy i) _const_;
179 CGroupDevicePolicy cgroup_device_policy_from_string(const char *s) _pure_;