]>
Commit | Line | Data |
---|---|---|
8e274523 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4ad49000 | 4 | Copyright 2013 Lennart Poettering |
8e274523 LP |
5 | |
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
8e274523 LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
8e274523 | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
8e274523 LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
c6c18be3 | 20 | #include <fcntl.h> |
e41969e3 | 21 | #include <fnmatch.h> |
8c6db833 | 22 | |
b5efdb8a | 23 | #include "alloc-util.h" |
03a7b521 | 24 | #include "cgroup-util.h" |
3ffd4af2 LP |
25 | #include "cgroup.h" |
26 | #include "fd-util.h" | |
0d39fa9c | 27 | #include "fileio.h" |
77601719 | 28 | #include "fs-util.h" |
6bedfcbb | 29 | #include "parse-util.h" |
9eb977db | 30 | #include "path-util.h" |
03a7b521 | 31 | #include "process-util.h" |
9444b1f2 | 32 | #include "special.h" |
8b43440b | 33 | #include "string-table.h" |
07630cea | 34 | #include "string-util.h" |
13c31542 | 35 | #include "stdio-util.h" |
8e274523 | 36 | |
9a054909 LP |
37 | #define CGROUP_CPU_QUOTA_PERIOD_USEC ((usec_t) 100 * USEC_PER_MSEC) |
38 | ||
2b40998d | 39 | static void cgroup_compat_warn(void) { |
128fadc9 TH |
40 | static bool cgroup_compat_warned = false; |
41 | ||
42 | if (cgroup_compat_warned) | |
43 | return; | |
44 | ||
45 | log_warning("cgroup compatibility translation between legacy and unified hierarchy settings activated. See cgroup-compat debug messages for details."); | |
46 | cgroup_compat_warned = true; | |
47 | } | |
48 | ||
49 | #define log_cgroup_compat(unit, fmt, ...) do { \ | |
50 | cgroup_compat_warn(); \ | |
51 | log_unit_debug(unit, "cgroup-compat: " fmt, ##__VA_ARGS__); \ | |
2b40998d | 52 | } while (false) |
128fadc9 | 53 | |
4ad49000 LP |
54 | void cgroup_context_init(CGroupContext *c) { |
55 | assert(c); | |
56 | ||
57 | /* Initialize everything to the kernel defaults, assuming the | |
58 | * structure is preinitialized to 0 */ | |
59 | ||
d53d9474 LP |
60 | c->cpu_shares = CGROUP_CPU_SHARES_INVALID; |
61 | c->startup_cpu_shares = CGROUP_CPU_SHARES_INVALID; | |
62 | c->cpu_quota_per_sec_usec = USEC_INFINITY; | |
63 | ||
da4d897e TH |
64 | c->memory_high = CGROUP_LIMIT_MAX; |
65 | c->memory_max = CGROUP_LIMIT_MAX; | |
66 | ||
67 | c->memory_limit = CGROUP_LIMIT_MAX; | |
b2f8b02e | 68 | |
13c31542 TH |
69 | c->io_weight = CGROUP_WEIGHT_INVALID; |
70 | c->startup_io_weight = CGROUP_WEIGHT_INVALID; | |
71 | ||
d53d9474 LP |
72 | c->blockio_weight = CGROUP_BLKIO_WEIGHT_INVALID; |
73 | c->startup_blockio_weight = CGROUP_BLKIO_WEIGHT_INVALID; | |
74 | ||
75 | c->tasks_max = (uint64_t) -1; | |
4ad49000 | 76 | } |
8e274523 | 77 | |
4ad49000 LP |
78 | void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a) { |
79 | assert(c); | |
80 | assert(a); | |
81 | ||
71fda00f | 82 | LIST_REMOVE(device_allow, c->device_allow, a); |
4ad49000 LP |
83 | free(a->path); |
84 | free(a); | |
85 | } | |
86 | ||
13c31542 TH |
87 | void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w) { |
88 | assert(c); | |
89 | assert(w); | |
90 | ||
91 | LIST_REMOVE(device_weights, c->io_device_weights, w); | |
92 | free(w->path); | |
93 | free(w); | |
94 | } | |
95 | ||
96 | void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l) { | |
97 | assert(c); | |
98 | assert(l); | |
99 | ||
100 | LIST_REMOVE(device_limits, c->io_device_limits, l); | |
101 | free(l->path); | |
102 | free(l); | |
103 | } | |
104 | ||
4ad49000 LP |
105 | void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w) { |
106 | assert(c); | |
107 | assert(w); | |
108 | ||
71fda00f | 109 | LIST_REMOVE(device_weights, c->blockio_device_weights, w); |
4ad49000 LP |
110 | free(w->path); |
111 | free(w); | |
112 | } | |
113 | ||
114 | void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b) { | |
115 | assert(c); | |
8e274523 | 116 | assert(b); |
8e274523 | 117 | |
71fda00f | 118 | LIST_REMOVE(device_bandwidths, c->blockio_device_bandwidths, b); |
4ad49000 LP |
119 | free(b->path); |
120 | free(b); | |
121 | } | |
122 | ||
123 | void cgroup_context_done(CGroupContext *c) { | |
124 | assert(c); | |
125 | ||
13c31542 TH |
126 | while (c->io_device_weights) |
127 | cgroup_context_free_io_device_weight(c, c->io_device_weights); | |
128 | ||
129 | while (c->io_device_limits) | |
130 | cgroup_context_free_io_device_limit(c, c->io_device_limits); | |
131 | ||
4ad49000 LP |
132 | while (c->blockio_device_weights) |
133 | cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights); | |
134 | ||
135 | while (c->blockio_device_bandwidths) | |
136 | cgroup_context_free_blockio_device_bandwidth(c, c->blockio_device_bandwidths); | |
137 | ||
138 | while (c->device_allow) | |
139 | cgroup_context_free_device_allow(c, c->device_allow); | |
140 | } | |
141 | ||
142 | void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) { | |
13c31542 TH |
143 | CGroupIODeviceLimit *il; |
144 | CGroupIODeviceWeight *iw; | |
4ad49000 LP |
145 | CGroupBlockIODeviceBandwidth *b; |
146 | CGroupBlockIODeviceWeight *w; | |
147 | CGroupDeviceAllow *a; | |
9a054909 | 148 | char u[FORMAT_TIMESPAN_MAX]; |
4ad49000 LP |
149 | |
150 | assert(c); | |
151 | assert(f); | |
152 | ||
153 | prefix = strempty(prefix); | |
154 | ||
155 | fprintf(f, | |
156 | "%sCPUAccounting=%s\n" | |
13c31542 | 157 | "%sIOAccounting=%s\n" |
4ad49000 LP |
158 | "%sBlockIOAccounting=%s\n" |
159 | "%sMemoryAccounting=%s\n" | |
d53d9474 LP |
160 | "%sTasksAccounting=%s\n" |
161 | "%sCPUShares=%" PRIu64 "\n" | |
162 | "%sStartupCPUShares=%" PRIu64 "\n" | |
b2f8b02e | 163 | "%sCPUQuotaPerSecSec=%s\n" |
13c31542 TH |
164 | "%sIOWeight=%" PRIu64 "\n" |
165 | "%sStartupIOWeight=%" PRIu64 "\n" | |
d53d9474 LP |
166 | "%sBlockIOWeight=%" PRIu64 "\n" |
167 | "%sStartupBlockIOWeight=%" PRIu64 "\n" | |
da4d897e TH |
168 | "%sMemoryLow=%" PRIu64 "\n" |
169 | "%sMemoryHigh=%" PRIu64 "\n" | |
170 | "%sMemoryMax=%" PRIu64 "\n" | |
4ad49000 | 171 | "%sMemoryLimit=%" PRIu64 "\n" |
03a7b521 | 172 | "%sTasksMax=%" PRIu64 "\n" |
a931ad47 LP |
173 | "%sDevicePolicy=%s\n" |
174 | "%sDelegate=%s\n", | |
4ad49000 | 175 | prefix, yes_no(c->cpu_accounting), |
13c31542 | 176 | prefix, yes_no(c->io_accounting), |
4ad49000 LP |
177 | prefix, yes_no(c->blockio_accounting), |
178 | prefix, yes_no(c->memory_accounting), | |
d53d9474 | 179 | prefix, yes_no(c->tasks_accounting), |
4ad49000 | 180 | prefix, c->cpu_shares, |
95ae05c0 | 181 | prefix, c->startup_cpu_shares, |
b1d6dcf5 | 182 | prefix, format_timespan(u, sizeof(u), c->cpu_quota_per_sec_usec, 1), |
13c31542 TH |
183 | prefix, c->io_weight, |
184 | prefix, c->startup_io_weight, | |
4ad49000 | 185 | prefix, c->blockio_weight, |
95ae05c0 | 186 | prefix, c->startup_blockio_weight, |
da4d897e TH |
187 | prefix, c->memory_low, |
188 | prefix, c->memory_high, | |
189 | prefix, c->memory_max, | |
4ad49000 | 190 | prefix, c->memory_limit, |
03a7b521 | 191 | prefix, c->tasks_max, |
a931ad47 LP |
192 | prefix, cgroup_device_policy_to_string(c->device_policy), |
193 | prefix, yes_no(c->delegate)); | |
4ad49000 LP |
194 | |
195 | LIST_FOREACH(device_allow, a, c->device_allow) | |
196 | fprintf(f, | |
197 | "%sDeviceAllow=%s %s%s%s\n", | |
198 | prefix, | |
199 | a->path, | |
200 | a->r ? "r" : "", a->w ? "w" : "", a->m ? "m" : ""); | |
201 | ||
13c31542 TH |
202 | LIST_FOREACH(device_weights, iw, c->io_device_weights) |
203 | fprintf(f, | |
204 | "%sIODeviceWeight=%s %" PRIu64, | |
205 | prefix, | |
206 | iw->path, | |
207 | iw->weight); | |
208 | ||
209 | LIST_FOREACH(device_limits, il, c->io_device_limits) { | |
210 | char buf[FORMAT_BYTES_MAX]; | |
9be57249 TH |
211 | CGroupIOLimitType type; |
212 | ||
213 | for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++) | |
214 | if (il->limits[type] != cgroup_io_limit_defaults[type]) | |
215 | fprintf(f, | |
216 | "%s%s=%s %s\n", | |
217 | prefix, | |
218 | cgroup_io_limit_type_to_string(type), | |
219 | il->path, | |
220 | format_bytes(buf, sizeof(buf), il->limits[type])); | |
13c31542 TH |
221 | } |
222 | ||
4ad49000 LP |
223 | LIST_FOREACH(device_weights, w, c->blockio_device_weights) |
224 | fprintf(f, | |
d53d9474 | 225 | "%sBlockIODeviceWeight=%s %" PRIu64, |
4ad49000 LP |
226 | prefix, |
227 | w->path, | |
228 | w->weight); | |
229 | ||
230 | LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) { | |
231 | char buf[FORMAT_BYTES_MAX]; | |
232 | ||
979d0311 TH |
233 | if (b->rbps != CGROUP_LIMIT_MAX) |
234 | fprintf(f, | |
235 | "%sBlockIOReadBandwidth=%s %s\n", | |
236 | prefix, | |
237 | b->path, | |
238 | format_bytes(buf, sizeof(buf), b->rbps)); | |
239 | if (b->wbps != CGROUP_LIMIT_MAX) | |
240 | fprintf(f, | |
241 | "%sBlockIOWriteBandwidth=%s %s\n", | |
242 | prefix, | |
243 | b->path, | |
244 | format_bytes(buf, sizeof(buf), b->wbps)); | |
4ad49000 LP |
245 | } |
246 | } | |
247 | ||
13c31542 | 248 | static int lookup_block_device(const char *p, dev_t *dev) { |
4ad49000 LP |
249 | struct stat st; |
250 | int r; | |
251 | ||
252 | assert(p); | |
253 | assert(dev); | |
254 | ||
255 | r = stat(p, &st); | |
4a62c710 MS |
256 | if (r < 0) |
257 | return log_warning_errno(errno, "Couldn't stat device %s: %m", p); | |
8e274523 | 258 | |
4ad49000 LP |
259 | if (S_ISBLK(st.st_mode)) |
260 | *dev = st.st_rdev; | |
261 | else if (major(st.st_dev) != 0) { | |
262 | /* If this is not a device node then find the block | |
263 | * device this file is stored on */ | |
264 | *dev = st.st_dev; | |
265 | ||
266 | /* If this is a partition, try to get the originating | |
267 | * block device */ | |
268 | block_get_whole_disk(*dev, dev); | |
269 | } else { | |
270 | log_warning("%s is not a block device and file system block device cannot be determined or is not local.", p); | |
271 | return -ENODEV; | |
272 | } | |
8e274523 | 273 | |
8e274523 | 274 | return 0; |
8e274523 LP |
275 | } |
276 | ||
4ad49000 LP |
277 | static int whitelist_device(const char *path, const char *node, const char *acc) { |
278 | char buf[2+DECIMAL_STR_MAX(dev_t)*2+2+4]; | |
279 | struct stat st; | |
8c6db833 | 280 | int r; |
8e274523 | 281 | |
4ad49000 LP |
282 | assert(path); |
283 | assert(acc); | |
8e274523 | 284 | |
4ad49000 LP |
285 | if (stat(node, &st) < 0) { |
286 | log_warning("Couldn't stat device %s", node); | |
287 | return -errno; | |
288 | } | |
289 | ||
290 | if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) { | |
291 | log_warning("%s is not a device.", node); | |
292 | return -ENODEV; | |
293 | } | |
294 | ||
295 | sprintf(buf, | |
296 | "%c %u:%u %s", | |
297 | S_ISCHR(st.st_mode) ? 'c' : 'b', | |
298 | major(st.st_rdev), minor(st.st_rdev), | |
299 | acc); | |
300 | ||
301 | r = cg_set_attribute("devices", path, "devices.allow", buf); | |
1aeab12b | 302 | if (r < 0) |
077ba06e | 303 | log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
714e2e1d | 304 | "Failed to set devices.allow on %s: %m", path); |
4ad49000 LP |
305 | |
306 | return r; | |
8e274523 LP |
307 | } |
308 | ||
90060676 LP |
309 | static int whitelist_major(const char *path, const char *name, char type, const char *acc) { |
310 | _cleanup_fclose_ FILE *f = NULL; | |
311 | char line[LINE_MAX]; | |
312 | bool good = false; | |
313 | int r; | |
314 | ||
315 | assert(path); | |
316 | assert(acc); | |
317 | assert(type == 'b' || type == 'c'); | |
318 | ||
319 | f = fopen("/proc/devices", "re"); | |
4a62c710 MS |
320 | if (!f) |
321 | return log_warning_errno(errno, "Cannot open /proc/devices to resolve %s (%c): %m", name, type); | |
90060676 LP |
322 | |
323 | FOREACH_LINE(line, f, goto fail) { | |
324 | char buf[2+DECIMAL_STR_MAX(unsigned)+3+4], *p, *w; | |
325 | unsigned maj; | |
326 | ||
327 | truncate_nl(line); | |
328 | ||
329 | if (type == 'c' && streq(line, "Character devices:")) { | |
330 | good = true; | |
331 | continue; | |
332 | } | |
333 | ||
334 | if (type == 'b' && streq(line, "Block devices:")) { | |
335 | good = true; | |
336 | continue; | |
337 | } | |
338 | ||
339 | if (isempty(line)) { | |
340 | good = false; | |
341 | continue; | |
342 | } | |
343 | ||
344 | if (!good) | |
345 | continue; | |
346 | ||
347 | p = strstrip(line); | |
348 | ||
349 | w = strpbrk(p, WHITESPACE); | |
350 | if (!w) | |
351 | continue; | |
352 | *w = 0; | |
353 | ||
354 | r = safe_atou(p, &maj); | |
355 | if (r < 0) | |
356 | continue; | |
357 | if (maj <= 0) | |
358 | continue; | |
359 | ||
360 | w++; | |
361 | w += strspn(w, WHITESPACE); | |
e41969e3 LP |
362 | |
363 | if (fnmatch(name, w, 0) != 0) | |
90060676 LP |
364 | continue; |
365 | ||
366 | sprintf(buf, | |
367 | "%c %u:* %s", | |
368 | type, | |
369 | maj, | |
370 | acc); | |
371 | ||
372 | r = cg_set_attribute("devices", path, "devices.allow", buf); | |
1aeab12b | 373 | if (r < 0) |
077ba06e | 374 | log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
714e2e1d | 375 | "Failed to set devices.allow on %s: %m", path); |
90060676 LP |
376 | } |
377 | ||
378 | return 0; | |
379 | ||
380 | fail: | |
56f64d95 | 381 | log_warning_errno(errno, "Failed to read /proc/devices: %m"); |
90060676 LP |
382 | return -errno; |
383 | } | |
384 | ||
508c45da | 385 | static bool cgroup_context_has_io_config(CGroupContext *c) { |
538b4852 TH |
386 | return c->io_accounting || |
387 | c->io_weight != CGROUP_WEIGHT_INVALID || | |
388 | c->startup_io_weight != CGROUP_WEIGHT_INVALID || | |
389 | c->io_device_weights || | |
390 | c->io_device_limits; | |
391 | } | |
392 | ||
508c45da | 393 | static bool cgroup_context_has_blockio_config(CGroupContext *c) { |
538b4852 TH |
394 | return c->blockio_accounting || |
395 | c->blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID || | |
396 | c->startup_blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID || | |
397 | c->blockio_device_weights || | |
398 | c->blockio_device_bandwidths; | |
399 | } | |
400 | ||
508c45da | 401 | static uint64_t cgroup_context_io_weight(CGroupContext *c, ManagerState state) { |
64faf04c TH |
402 | if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && |
403 | c->startup_io_weight != CGROUP_WEIGHT_INVALID) | |
404 | return c->startup_io_weight; | |
405 | else if (c->io_weight != CGROUP_WEIGHT_INVALID) | |
406 | return c->io_weight; | |
407 | else | |
408 | return CGROUP_WEIGHT_DEFAULT; | |
409 | } | |
410 | ||
508c45da | 411 | static uint64_t cgroup_context_blkio_weight(CGroupContext *c, ManagerState state) { |
64faf04c TH |
412 | if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && |
413 | c->startup_blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID) | |
414 | return c->startup_blockio_weight; | |
415 | else if (c->blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID) | |
416 | return c->blockio_weight; | |
417 | else | |
418 | return CGROUP_BLKIO_WEIGHT_DEFAULT; | |
419 | } | |
420 | ||
508c45da | 421 | static uint64_t cgroup_weight_blkio_to_io(uint64_t blkio_weight) { |
538b4852 TH |
422 | return CLAMP(blkio_weight * CGROUP_WEIGHT_DEFAULT / CGROUP_BLKIO_WEIGHT_DEFAULT, |
423 | CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX); | |
424 | } | |
425 | ||
508c45da | 426 | static uint64_t cgroup_weight_io_to_blkio(uint64_t io_weight) { |
538b4852 TH |
427 | return CLAMP(io_weight * CGROUP_BLKIO_WEIGHT_DEFAULT / CGROUP_WEIGHT_DEFAULT, |
428 | CGROUP_BLKIO_WEIGHT_MIN, CGROUP_BLKIO_WEIGHT_MAX); | |
429 | } | |
430 | ||
f29ff115 | 431 | static void cgroup_apply_io_device_weight(Unit *u, const char *dev_path, uint64_t io_weight) { |
64faf04c TH |
432 | char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1]; |
433 | dev_t dev; | |
434 | int r; | |
435 | ||
436 | r = lookup_block_device(dev_path, &dev); | |
437 | if (r < 0) | |
438 | return; | |
439 | ||
440 | xsprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), io_weight); | |
f29ff115 | 441 | r = cg_set_attribute("io", u->cgroup_path, "io.weight", buf); |
64faf04c | 442 | if (r < 0) |
f29ff115 TH |
443 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
444 | "Failed to set io.weight: %m"); | |
64faf04c TH |
445 | } |
446 | ||
f29ff115 | 447 | static void cgroup_apply_blkio_device_weight(Unit *u, const char *dev_path, uint64_t blkio_weight) { |
64faf04c TH |
448 | char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1]; |
449 | dev_t dev; | |
450 | int r; | |
451 | ||
452 | r = lookup_block_device(dev_path, &dev); | |
453 | if (r < 0) | |
454 | return; | |
455 | ||
456 | xsprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), blkio_weight); | |
f29ff115 | 457 | r = cg_set_attribute("blkio", u->cgroup_path, "blkio.weight_device", buf); |
64faf04c | 458 | if (r < 0) |
f29ff115 TH |
459 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
460 | "Failed to set blkio.weight_device: %m"); | |
64faf04c TH |
461 | } |
462 | ||
f29ff115 | 463 | static unsigned cgroup_apply_io_device_limit(Unit *u, const char *dev_path, uint64_t *limits) { |
64faf04c TH |
464 | char limit_bufs[_CGROUP_IO_LIMIT_TYPE_MAX][DECIMAL_STR_MAX(uint64_t)]; |
465 | char buf[DECIMAL_STR_MAX(dev_t)*2+2+(6+DECIMAL_STR_MAX(uint64_t)+1)*4]; | |
466 | CGroupIOLimitType type; | |
467 | dev_t dev; | |
468 | unsigned n = 0; | |
469 | int r; | |
470 | ||
471 | r = lookup_block_device(dev_path, &dev); | |
472 | if (r < 0) | |
473 | return 0; | |
474 | ||
475 | for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++) { | |
476 | if (limits[type] != cgroup_io_limit_defaults[type]) { | |
477 | xsprintf(limit_bufs[type], "%" PRIu64, limits[type]); | |
478 | n++; | |
479 | } else { | |
480 | xsprintf(limit_bufs[type], "%s", limits[type] == CGROUP_LIMIT_MAX ? "max" : "0"); | |
481 | } | |
482 | } | |
483 | ||
484 | xsprintf(buf, "%u:%u rbps=%s wbps=%s riops=%s wiops=%s\n", major(dev), minor(dev), | |
485 | limit_bufs[CGROUP_IO_RBPS_MAX], limit_bufs[CGROUP_IO_WBPS_MAX], | |
486 | limit_bufs[CGROUP_IO_RIOPS_MAX], limit_bufs[CGROUP_IO_WIOPS_MAX]); | |
f29ff115 | 487 | r = cg_set_attribute("io", u->cgroup_path, "io.max", buf); |
64faf04c | 488 | if (r < 0) |
f29ff115 TH |
489 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
490 | "Failed to set io.max: %m"); | |
64faf04c TH |
491 | return n; |
492 | } | |
493 | ||
f29ff115 | 494 | static unsigned cgroup_apply_blkio_device_limit(Unit *u, const char *dev_path, uint64_t rbps, uint64_t wbps) { |
64faf04c TH |
495 | char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1]; |
496 | dev_t dev; | |
497 | unsigned n = 0; | |
498 | int r; | |
499 | ||
500 | r = lookup_block_device(dev_path, &dev); | |
501 | if (r < 0) | |
502 | return 0; | |
503 | ||
504 | if (rbps != CGROUP_LIMIT_MAX) | |
505 | n++; | |
506 | sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), rbps); | |
f29ff115 | 507 | r = cg_set_attribute("blkio", u->cgroup_path, "blkio.throttle.read_bps_device", buf); |
64faf04c | 508 | if (r < 0) |
f29ff115 TH |
509 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
510 | "Failed to set blkio.throttle.read_bps_device: %m"); | |
64faf04c TH |
511 | |
512 | if (wbps != CGROUP_LIMIT_MAX) | |
513 | n++; | |
514 | sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), wbps); | |
f29ff115 | 515 | r = cg_set_attribute("blkio", u->cgroup_path, "blkio.throttle.write_bps_device", buf); |
64faf04c | 516 | if (r < 0) |
f29ff115 TH |
517 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
518 | "Failed to set blkio.throttle.write_bps_device: %m"); | |
64faf04c TH |
519 | |
520 | return n; | |
521 | } | |
522 | ||
da4d897e TH |
523 | static bool cgroup_context_has_unified_memory_config(CGroupContext *c) { |
524 | return c->memory_low > 0 || c->memory_high != CGROUP_LIMIT_MAX || c->memory_max != CGROUP_LIMIT_MAX; | |
525 | } | |
526 | ||
f29ff115 | 527 | static void cgroup_apply_unified_memory_limit(Unit *u, const char *file, uint64_t v) { |
da4d897e TH |
528 | char buf[DECIMAL_STR_MAX(uint64_t) + 1] = "max"; |
529 | int r; | |
530 | ||
531 | if (v != CGROUP_LIMIT_MAX) | |
532 | xsprintf(buf, "%" PRIu64 "\n", v); | |
533 | ||
f29ff115 | 534 | r = cg_set_attribute("memory", u->cgroup_path, file, buf); |
da4d897e | 535 | if (r < 0) |
f29ff115 TH |
536 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
537 | "Failed to set %s: %m", file); | |
da4d897e TH |
538 | } |
539 | ||
f29ff115 TH |
540 | static void cgroup_context_apply(Unit *u, CGroupMask mask, ManagerState state) { |
541 | const char *path; | |
542 | CGroupContext *c; | |
01efdf13 | 543 | bool is_root; |
4ad49000 LP |
544 | int r; |
545 | ||
f29ff115 TH |
546 | assert(u); |
547 | ||
548 | c = unit_get_cgroup_context(u); | |
549 | path = u->cgroup_path; | |
550 | ||
4ad49000 LP |
551 | assert(c); |
552 | assert(path); | |
8e274523 | 553 | |
4ad49000 LP |
554 | if (mask == 0) |
555 | return; | |
8e274523 | 556 | |
71c26873 | 557 | /* Some cgroup attributes are not supported on the root cgroup, |
01efdf13 LP |
558 | * hence silently ignore */ |
559 | is_root = isempty(path) || path_equal(path, "/"); | |
6da13913 ZJS |
560 | if (is_root) |
561 | /* Make sure we don't try to display messages with an empty path. */ | |
562 | path = "/"; | |
01efdf13 | 563 | |
714e2e1d LP |
564 | /* We generally ignore errors caused by read-only mounted |
565 | * cgroup trees (assuming we are running in a container then), | |
566 | * and missing cgroups, i.e. EROFS and ENOENT. */ | |
567 | ||
efdb0237 | 568 | if ((mask & CGROUP_MASK_CPU) && !is_root) { |
d53d9474 | 569 | char buf[MAX(DECIMAL_STR_MAX(uint64_t), DECIMAL_STR_MAX(usec_t)) + 1]; |
8e274523 | 570 | |
d53d9474 LP |
571 | sprintf(buf, "%" PRIu64 "\n", |
572 | IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && c->startup_cpu_shares != CGROUP_CPU_SHARES_INVALID ? c->startup_cpu_shares : | |
573 | c->cpu_shares != CGROUP_CPU_SHARES_INVALID ? c->cpu_shares : CGROUP_CPU_SHARES_DEFAULT); | |
4ad49000 | 574 | r = cg_set_attribute("cpu", path, "cpu.shares", buf); |
1aeab12b | 575 | if (r < 0) |
f29ff115 TH |
576 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
577 | "Failed to set cpu.shares: %m"); | |
b2f8b02e | 578 | |
9a054909 | 579 | sprintf(buf, USEC_FMT "\n", CGROUP_CPU_QUOTA_PERIOD_USEC); |
b2f8b02e | 580 | r = cg_set_attribute("cpu", path, "cpu.cfs_period_us", buf); |
1aeab12b | 581 | if (r < 0) |
f29ff115 TH |
582 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
583 | "Failed to set cpu.cfs_period_us: %m"); | |
b2f8b02e | 584 | |
3a43da28 | 585 | if (c->cpu_quota_per_sec_usec != USEC_INFINITY) { |
9a054909 | 586 | sprintf(buf, USEC_FMT "\n", c->cpu_quota_per_sec_usec * CGROUP_CPU_QUOTA_PERIOD_USEC / USEC_PER_SEC); |
b2f8b02e LP |
587 | r = cg_set_attribute("cpu", path, "cpu.cfs_quota_us", buf); |
588 | } else | |
589 | r = cg_set_attribute("cpu", path, "cpu.cfs_quota_us", "-1"); | |
1aeab12b | 590 | if (r < 0) |
f29ff115 TH |
591 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
592 | "Failed to set cpu.cfs_quota_us: %m"); | |
4ad49000 LP |
593 | } |
594 | ||
13c31542 | 595 | if (mask & CGROUP_MASK_IO) { |
538b4852 TH |
596 | bool has_io = cgroup_context_has_io_config(c); |
597 | bool has_blockio = cgroup_context_has_blockio_config(c); | |
13c31542 TH |
598 | |
599 | if (!is_root) { | |
64faf04c TH |
600 | char buf[8+DECIMAL_STR_MAX(uint64_t)+1]; |
601 | uint64_t weight; | |
13c31542 | 602 | |
538b4852 TH |
603 | if (has_io) |
604 | weight = cgroup_context_io_weight(c, state); | |
128fadc9 TH |
605 | else if (has_blockio) { |
606 | uint64_t blkio_weight = cgroup_context_blkio_weight(c, state); | |
607 | ||
608 | weight = cgroup_weight_blkio_to_io(blkio_weight); | |
609 | ||
610 | log_cgroup_compat(u, "Applying [Startup]BlockIOWeight %" PRIu64 " as [Startup]IOWeight %" PRIu64, | |
611 | blkio_weight, weight); | |
612 | } else | |
538b4852 | 613 | weight = CGROUP_WEIGHT_DEFAULT; |
13c31542 TH |
614 | |
615 | xsprintf(buf, "default %" PRIu64 "\n", weight); | |
616 | r = cg_set_attribute("io", path, "io.weight", buf); | |
617 | if (r < 0) | |
f29ff115 TH |
618 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
619 | "Failed to set io.weight: %m"); | |
13c31542 | 620 | |
538b4852 TH |
621 | if (has_io) { |
622 | CGroupIODeviceWeight *w; | |
623 | ||
624 | /* FIXME: no way to reset this list */ | |
625 | LIST_FOREACH(device_weights, w, c->io_device_weights) | |
f29ff115 | 626 | cgroup_apply_io_device_weight(u, w->path, w->weight); |
538b4852 TH |
627 | } else if (has_blockio) { |
628 | CGroupBlockIODeviceWeight *w; | |
629 | ||
630 | /* FIXME: no way to reset this list */ | |
128fadc9 TH |
631 | LIST_FOREACH(device_weights, w, c->blockio_device_weights) { |
632 | weight = cgroup_weight_blkio_to_io(w->weight); | |
633 | ||
634 | log_cgroup_compat(u, "Applying BlockIODeviceWeight %" PRIu64 " as IODeviceWeight %" PRIu64 " for %s", | |
635 | w->weight, weight, w->path); | |
636 | ||
637 | cgroup_apply_io_device_weight(u, w->path, weight); | |
638 | } | |
538b4852 | 639 | } |
13c31542 TH |
640 | } |
641 | ||
64faf04c | 642 | /* Apply limits and free ones without config. */ |
538b4852 TH |
643 | if (has_io) { |
644 | CGroupIODeviceLimit *l, *next; | |
645 | ||
646 | LIST_FOREACH_SAFE(device_limits, l, next, c->io_device_limits) { | |
f29ff115 | 647 | if (!cgroup_apply_io_device_limit(u, l->path, l->limits)) |
538b4852 TH |
648 | cgroup_context_free_io_device_limit(c, l); |
649 | } | |
650 | } else if (has_blockio) { | |
651 | CGroupBlockIODeviceBandwidth *b, *next; | |
652 | ||
653 | LIST_FOREACH_SAFE(device_bandwidths, b, next, c->blockio_device_bandwidths) { | |
654 | uint64_t limits[_CGROUP_IO_LIMIT_TYPE_MAX]; | |
655 | CGroupIOLimitType type; | |
656 | ||
657 | for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++) | |
658 | limits[type] = cgroup_io_limit_defaults[type]; | |
659 | ||
660 | limits[CGROUP_IO_RBPS_MAX] = b->rbps; | |
661 | limits[CGROUP_IO_WBPS_MAX] = b->wbps; | |
662 | ||
128fadc9 TH |
663 | log_cgroup_compat(u, "Applying BlockIO{Read|Write}Bandwidth %" PRIu64 " %" PRIu64 " as IO{Read|Write}BandwidthMax for %s", |
664 | b->rbps, b->wbps, b->path); | |
665 | ||
f29ff115 | 666 | if (!cgroup_apply_io_device_limit(u, b->path, limits)) |
538b4852 TH |
667 | cgroup_context_free_blockio_device_bandwidth(c, b); |
668 | } | |
13c31542 TH |
669 | } |
670 | } | |
671 | ||
efdb0237 | 672 | if (mask & CGROUP_MASK_BLKIO) { |
538b4852 TH |
673 | bool has_io = cgroup_context_has_io_config(c); |
674 | bool has_blockio = cgroup_context_has_blockio_config(c); | |
4ad49000 | 675 | |
01efdf13 | 676 | if (!is_root) { |
64faf04c TH |
677 | char buf[DECIMAL_STR_MAX(uint64_t)+1]; |
678 | uint64_t weight; | |
64faf04c | 679 | |
538b4852 TH |
680 | if (has_blockio) |
681 | weight = cgroup_context_blkio_weight(c, state); | |
128fadc9 TH |
682 | else if (has_io) { |
683 | uint64_t io_weight = cgroup_context_io_weight(c, state); | |
684 | ||
538b4852 | 685 | weight = cgroup_weight_io_to_blkio(cgroup_context_io_weight(c, state)); |
128fadc9 TH |
686 | |
687 | log_cgroup_compat(u, "Applying [Startup]IOWeight %" PRIu64 " as [Startup]BlockIOWeight %" PRIu64, | |
688 | io_weight, weight); | |
689 | } else | |
538b4852 | 690 | weight = CGROUP_BLKIO_WEIGHT_DEFAULT; |
64faf04c TH |
691 | |
692 | xsprintf(buf, "%" PRIu64 "\n", weight); | |
01efdf13 | 693 | r = cg_set_attribute("blkio", path, "blkio.weight", buf); |
1aeab12b | 694 | if (r < 0) |
f29ff115 TH |
695 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
696 | "Failed to set blkio.weight: %m"); | |
4ad49000 | 697 | |
538b4852 TH |
698 | if (has_blockio) { |
699 | CGroupBlockIODeviceWeight *w; | |
700 | ||
701 | /* FIXME: no way to reset this list */ | |
702 | LIST_FOREACH(device_weights, w, c->blockio_device_weights) | |
f29ff115 | 703 | cgroup_apply_blkio_device_weight(u, w->path, w->weight); |
538b4852 TH |
704 | } else if (has_io) { |
705 | CGroupIODeviceWeight *w; | |
706 | ||
707 | /* FIXME: no way to reset this list */ | |
128fadc9 TH |
708 | LIST_FOREACH(device_weights, w, c->io_device_weights) { |
709 | weight = cgroup_weight_io_to_blkio(w->weight); | |
710 | ||
711 | log_cgroup_compat(u, "Applying IODeviceWeight %" PRIu64 " as BlockIODeviceWeight %" PRIu64 " for %s", | |
712 | w->weight, weight, w->path); | |
713 | ||
714 | cgroup_apply_blkio_device_weight(u, w->path, weight); | |
715 | } | |
538b4852 | 716 | } |
4ad49000 LP |
717 | } |
718 | ||
64faf04c | 719 | /* Apply limits and free ones without config. */ |
538b4852 TH |
720 | if (has_blockio) { |
721 | CGroupBlockIODeviceBandwidth *b, *next; | |
722 | ||
723 | LIST_FOREACH_SAFE(device_bandwidths, b, next, c->blockio_device_bandwidths) { | |
f29ff115 | 724 | if (!cgroup_apply_blkio_device_limit(u, b->path, b->rbps, b->wbps)) |
538b4852 TH |
725 | cgroup_context_free_blockio_device_bandwidth(c, b); |
726 | } | |
727 | } else if (has_io) { | |
728 | CGroupIODeviceLimit *l, *next; | |
729 | ||
730 | LIST_FOREACH_SAFE(device_limits, l, next, c->io_device_limits) { | |
128fadc9 TH |
731 | log_cgroup_compat(u, "Applying IO{Read|Write}Bandwidth %" PRIu64 " %" PRIu64 " as BlockIO{Read|Write}BandwidthMax for %s", |
732 | l->limits[CGROUP_IO_RBPS_MAX], l->limits[CGROUP_IO_WBPS_MAX], l->path); | |
733 | ||
f29ff115 | 734 | if (!cgroup_apply_blkio_device_limit(u, l->path, l->limits[CGROUP_IO_RBPS_MAX], l->limits[CGROUP_IO_WBPS_MAX])) |
538b4852 TH |
735 | cgroup_context_free_io_device_limit(c, l); |
736 | } | |
d686d8a9 | 737 | } |
8e274523 LP |
738 | } |
739 | ||
efdb0237 | 740 | if ((mask & CGROUP_MASK_MEMORY) && !is_root) { |
da4d897e TH |
741 | if (cg_unified() > 0) { |
742 | uint64_t max = c->memory_max; | |
efdb0237 | 743 | |
da4d897e TH |
744 | if (cgroup_context_has_unified_memory_config(c)) |
745 | max = c->memory_max; | |
128fadc9 | 746 | else { |
da4d897e | 747 | max = c->memory_limit; |
efdb0237 | 748 | |
128fadc9 TH |
749 | if (max != CGROUP_LIMIT_MAX) |
750 | log_cgroup_compat(u, "Applying MemoryLimit %" PRIu64 " as MemoryMax", max); | |
751 | } | |
752 | ||
f29ff115 TH |
753 | cgroup_apply_unified_memory_limit(u, "memory.low", c->memory_low); |
754 | cgroup_apply_unified_memory_limit(u, "memory.high", c->memory_high); | |
755 | cgroup_apply_unified_memory_limit(u, "memory.max", max); | |
efdb0237 | 756 | } else { |
da4d897e | 757 | char buf[DECIMAL_STR_MAX(uint64_t) + 1]; |
78a4ee59 | 758 | uint64_t val = c->memory_limit; |
da4d897e | 759 | |
78a4ee59 DM |
760 | if (val == CGROUP_LIMIT_MAX) { |
761 | val = c->memory_max; | |
8e274523 | 762 | |
78a4ee59 DM |
763 | if (val != CGROUP_LIMIT_MAX) |
764 | log_cgroup_compat(u, "Applying MemoryMax %" PRIi64 " as MemoryLimit", c->memory_max); | |
128fadc9 TH |
765 | } |
766 | ||
78a4ee59 DM |
767 | if (val == CGROUP_LIMIT_MAX) |
768 | strncpy(buf, "-1\n", sizeof(buf)); | |
769 | else | |
770 | xsprintf(buf, "%" PRIu64 "\n", val); | |
771 | ||
da4d897e TH |
772 | r = cg_set_attribute("memory", path, "memory.limit_in_bytes", buf); |
773 | if (r < 0) | |
f29ff115 TH |
774 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
775 | "Failed to set memory.limit_in_bytes: %m"); | |
da4d897e | 776 | } |
4ad49000 | 777 | } |
8e274523 | 778 | |
3905f127 | 779 | if ((mask & CGROUP_MASK_DEVICES) && !is_root) { |
4ad49000 | 780 | CGroupDeviceAllow *a; |
8e274523 | 781 | |
714e2e1d LP |
782 | /* Changing the devices list of a populated cgroup |
783 | * might result in EINVAL, hence ignore EINVAL | |
784 | * here. */ | |
785 | ||
4ad49000 LP |
786 | if (c->device_allow || c->device_policy != CGROUP_AUTO) |
787 | r = cg_set_attribute("devices", path, "devices.deny", "a"); | |
788 | else | |
789 | r = cg_set_attribute("devices", path, "devices.allow", "a"); | |
1aeab12b | 790 | if (r < 0) |
f29ff115 TH |
791 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
792 | "Failed to reset devices.list: %m"); | |
fb385181 | 793 | |
4ad49000 LP |
794 | if (c->device_policy == CGROUP_CLOSED || |
795 | (c->device_policy == CGROUP_AUTO && c->device_allow)) { | |
796 | static const char auto_devices[] = | |
7d711efb LP |
797 | "/dev/null\0" "rwm\0" |
798 | "/dev/zero\0" "rwm\0" | |
799 | "/dev/full\0" "rwm\0" | |
800 | "/dev/random\0" "rwm\0" | |
801 | "/dev/urandom\0" "rwm\0" | |
802 | "/dev/tty\0" "rwm\0" | |
803 | "/dev/pts/ptmx\0" "rw\0"; /* /dev/pts/ptmx may not be duplicated, but accessed */ | |
4ad49000 LP |
804 | |
805 | const char *x, *y; | |
806 | ||
807 | NULSTR_FOREACH_PAIR(x, y, auto_devices) | |
808 | whitelist_device(path, x, y); | |
7d711efb LP |
809 | |
810 | whitelist_major(path, "pts", 'c', "rw"); | |
811 | whitelist_major(path, "kdbus", 'c', "rw"); | |
812 | whitelist_major(path, "kdbus/*", 'c', "rw"); | |
4ad49000 LP |
813 | } |
814 | ||
815 | LIST_FOREACH(device_allow, a, c->device_allow) { | |
816 | char acc[4]; | |
817 | unsigned k = 0; | |
818 | ||
819 | if (a->r) | |
820 | acc[k++] = 'r'; | |
821 | if (a->w) | |
822 | acc[k++] = 'w'; | |
823 | if (a->m) | |
824 | acc[k++] = 'm'; | |
fb385181 | 825 | |
4ad49000 LP |
826 | if (k == 0) |
827 | continue; | |
fb385181 | 828 | |
4ad49000 | 829 | acc[k++] = 0; |
90060676 LP |
830 | |
831 | if (startswith(a->path, "/dev/")) | |
832 | whitelist_device(path, a->path, acc); | |
833 | else if (startswith(a->path, "block-")) | |
834 | whitelist_major(path, a->path + 6, 'b', acc); | |
835 | else if (startswith(a->path, "char-")) | |
836 | whitelist_major(path, a->path + 5, 'c', acc); | |
837 | else | |
f29ff115 | 838 | log_unit_debug(u, "Ignoring device %s while writing cgroup attribute.", a->path); |
4ad49000 LP |
839 | } |
840 | } | |
03a7b521 LP |
841 | |
842 | if ((mask & CGROUP_MASK_PIDS) && !is_root) { | |
843 | ||
844 | if (c->tasks_max != (uint64_t) -1) { | |
845 | char buf[DECIMAL_STR_MAX(uint64_t) + 2]; | |
846 | ||
847 | sprintf(buf, "%" PRIu64 "\n", c->tasks_max); | |
848 | r = cg_set_attribute("pids", path, "pids.max", buf); | |
849 | } else | |
850 | r = cg_set_attribute("pids", path, "pids.max", "max"); | |
851 | ||
852 | if (r < 0) | |
f29ff115 TH |
853 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, |
854 | "Failed to set pids.max: %m"); | |
03a7b521 | 855 | } |
fb385181 LP |
856 | } |
857 | ||
efdb0237 LP |
858 | CGroupMask cgroup_context_get_mask(CGroupContext *c) { |
859 | CGroupMask mask = 0; | |
8e274523 | 860 | |
4ad49000 | 861 | /* Figure out which controllers we need */ |
8e274523 | 862 | |
b2f8b02e | 863 | if (c->cpu_accounting || |
d53d9474 LP |
864 | c->cpu_shares != CGROUP_CPU_SHARES_INVALID || |
865 | c->startup_cpu_shares != CGROUP_CPU_SHARES_INVALID || | |
3a43da28 | 866 | c->cpu_quota_per_sec_usec != USEC_INFINITY) |
efdb0237 | 867 | mask |= CGROUP_MASK_CPUACCT | CGROUP_MASK_CPU; |
ecedd90f | 868 | |
538b4852 TH |
869 | if (cgroup_context_has_io_config(c) || cgroup_context_has_blockio_config(c)) |
870 | mask |= CGROUP_MASK_IO | CGROUP_MASK_BLKIO; | |
ecedd90f | 871 | |
4ad49000 | 872 | if (c->memory_accounting || |
da4d897e TH |
873 | c->memory_limit != CGROUP_LIMIT_MAX || |
874 | cgroup_context_has_unified_memory_config(c)) | |
efdb0237 | 875 | mask |= CGROUP_MASK_MEMORY; |
8e274523 | 876 | |
a931ad47 LP |
877 | if (c->device_allow || |
878 | c->device_policy != CGROUP_AUTO) | |
3905f127 | 879 | mask |= CGROUP_MASK_DEVICES; |
4ad49000 | 880 | |
03a7b521 LP |
881 | if (c->tasks_accounting || |
882 | c->tasks_max != (uint64_t) -1) | |
883 | mask |= CGROUP_MASK_PIDS; | |
884 | ||
4ad49000 | 885 | return mask; |
8e274523 LP |
886 | } |
887 | ||
efdb0237 | 888 | CGroupMask unit_get_own_mask(Unit *u) { |
4ad49000 | 889 | CGroupContext *c; |
8e274523 | 890 | |
efdb0237 LP |
891 | /* Returns the mask of controllers the unit needs for itself */ |
892 | ||
4ad49000 LP |
893 | c = unit_get_cgroup_context(u); |
894 | if (!c) | |
895 | return 0; | |
8e274523 | 896 | |
a931ad47 | 897 | /* If delegation is turned on, then turn on all cgroups, |
19af675e LP |
898 | * unless we are on the legacy hierarchy and the process we |
899 | * fork into it is known to drop privileges, and hence | |
900 | * shouldn't get access to the controllers. | |
901 | * | |
902 | * Note that on the unified hierarchy it is safe to delegate | |
903 | * controllers to unprivileged services. */ | |
a931ad47 LP |
904 | |
905 | if (c->delegate) { | |
906 | ExecContext *e; | |
907 | ||
908 | e = unit_get_exec_context(u); | |
19af675e LP |
909 | if (!e || |
910 | exec_context_maintains_privileges(e) || | |
911 | cg_unified() > 0) | |
efdb0237 | 912 | return _CGROUP_MASK_ALL; |
a931ad47 LP |
913 | } |
914 | ||
db785129 | 915 | return cgroup_context_get_mask(c); |
8e274523 LP |
916 | } |
917 | ||
efdb0237 | 918 | CGroupMask unit_get_members_mask(Unit *u) { |
4ad49000 | 919 | assert(u); |
bc432dc7 | 920 | |
efdb0237 LP |
921 | /* Returns the mask of controllers all of the unit's children |
922 | * require, merged */ | |
923 | ||
bc432dc7 LP |
924 | if (u->cgroup_members_mask_valid) |
925 | return u->cgroup_members_mask; | |
926 | ||
927 | u->cgroup_members_mask = 0; | |
928 | ||
929 | if (u->type == UNIT_SLICE) { | |
930 | Unit *member; | |
931 | Iterator i; | |
932 | ||
933 | SET_FOREACH(member, u->dependencies[UNIT_BEFORE], i) { | |
934 | ||
935 | if (member == u) | |
936 | continue; | |
937 | ||
d4fdc205 | 938 | if (UNIT_DEREF(member->slice) != u) |
bc432dc7 LP |
939 | continue; |
940 | ||
941 | u->cgroup_members_mask |= | |
efdb0237 | 942 | unit_get_own_mask(member) | |
bc432dc7 LP |
943 | unit_get_members_mask(member); |
944 | } | |
945 | } | |
946 | ||
947 | u->cgroup_members_mask_valid = true; | |
6414b7c9 | 948 | return u->cgroup_members_mask; |
246aa6dd LP |
949 | } |
950 | ||
efdb0237 | 951 | CGroupMask unit_get_siblings_mask(Unit *u) { |
4ad49000 | 952 | assert(u); |
246aa6dd | 953 | |
efdb0237 LP |
954 | /* Returns the mask of controllers all of the unit's siblings |
955 | * require, i.e. the members mask of the unit's parent slice | |
956 | * if there is one. */ | |
957 | ||
bc432dc7 | 958 | if (UNIT_ISSET(u->slice)) |
637f421e | 959 | return unit_get_members_mask(UNIT_DEREF(u->slice)); |
4ad49000 | 960 | |
efdb0237 | 961 | return unit_get_own_mask(u) | unit_get_members_mask(u); |
246aa6dd LP |
962 | } |
963 | ||
efdb0237 LP |
964 | CGroupMask unit_get_subtree_mask(Unit *u) { |
965 | ||
966 | /* Returns the mask of this subtree, meaning of the group | |
967 | * itself and its children. */ | |
968 | ||
969 | return unit_get_own_mask(u) | unit_get_members_mask(u); | |
970 | } | |
971 | ||
972 | CGroupMask unit_get_target_mask(Unit *u) { | |
973 | CGroupMask mask; | |
974 | ||
975 | /* This returns the cgroup mask of all controllers to enable | |
976 | * for a specific cgroup, i.e. everything it needs itself, | |
977 | * plus all that its children need, plus all that its siblings | |
978 | * need. This is primarily useful on the legacy cgroup | |
979 | * hierarchy, where we need to duplicate each cgroup in each | |
980 | * hierarchy that shall be enabled for it. */ | |
6414b7c9 | 981 | |
efdb0237 LP |
982 | mask = unit_get_own_mask(u) | unit_get_members_mask(u) | unit_get_siblings_mask(u); |
983 | mask &= u->manager->cgroup_supported; | |
984 | ||
985 | return mask; | |
986 | } | |
987 | ||
988 | CGroupMask unit_get_enable_mask(Unit *u) { | |
989 | CGroupMask mask; | |
990 | ||
991 | /* This returns the cgroup mask of all controllers to enable | |
992 | * for the children of a specific cgroup. This is primarily | |
993 | * useful for the unified cgroup hierarchy, where each cgroup | |
994 | * controls which controllers are enabled for its children. */ | |
995 | ||
996 | mask = unit_get_members_mask(u); | |
6414b7c9 DS |
997 | mask &= u->manager->cgroup_supported; |
998 | ||
999 | return mask; | |
1000 | } | |
1001 | ||
1002 | /* Recurse from a unit up through its containing slices, propagating | |
1003 | * mask bits upward. A unit is also member of itself. */ | |
bc432dc7 | 1004 | void unit_update_cgroup_members_masks(Unit *u) { |
efdb0237 | 1005 | CGroupMask m; |
bc432dc7 LP |
1006 | bool more; |
1007 | ||
1008 | assert(u); | |
1009 | ||
1010 | /* Calculate subtree mask */ | |
efdb0237 | 1011 | m = unit_get_subtree_mask(u); |
bc432dc7 LP |
1012 | |
1013 | /* See if anything changed from the previous invocation. If | |
1014 | * not, we're done. */ | |
1015 | if (u->cgroup_subtree_mask_valid && m == u->cgroup_subtree_mask) | |
1016 | return; | |
1017 | ||
1018 | more = | |
1019 | u->cgroup_subtree_mask_valid && | |
1020 | ((m & ~u->cgroup_subtree_mask) != 0) && | |
1021 | ((~m & u->cgroup_subtree_mask) == 0); | |
1022 | ||
1023 | u->cgroup_subtree_mask = m; | |
1024 | u->cgroup_subtree_mask_valid = true; | |
1025 | ||
6414b7c9 DS |
1026 | if (UNIT_ISSET(u->slice)) { |
1027 | Unit *s = UNIT_DEREF(u->slice); | |
bc432dc7 LP |
1028 | |
1029 | if (more) | |
1030 | /* There's more set now than before. We | |
1031 | * propagate the new mask to the parent's mask | |
1032 | * (not caring if it actually was valid or | |
1033 | * not). */ | |
1034 | ||
1035 | s->cgroup_members_mask |= m; | |
1036 | ||
1037 | else | |
1038 | /* There's less set now than before (or we | |
1039 | * don't know), we need to recalculate | |
1040 | * everything, so let's invalidate the | |
1041 | * parent's members mask */ | |
1042 | ||
1043 | s->cgroup_members_mask_valid = false; | |
1044 | ||
1045 | /* And now make sure that this change also hits our | |
1046 | * grandparents */ | |
1047 | unit_update_cgroup_members_masks(s); | |
6414b7c9 DS |
1048 | } |
1049 | } | |
1050 | ||
efdb0237 | 1051 | static const char *migrate_callback(CGroupMask mask, void *userdata) { |
03b90d4b LP |
1052 | Unit *u = userdata; |
1053 | ||
1054 | assert(mask != 0); | |
1055 | assert(u); | |
1056 | ||
1057 | while (u) { | |
1058 | if (u->cgroup_path && | |
1059 | u->cgroup_realized && | |
1060 | (u->cgroup_realized_mask & mask) == mask) | |
1061 | return u->cgroup_path; | |
1062 | ||
1063 | u = UNIT_DEREF(u->slice); | |
1064 | } | |
1065 | ||
1066 | return NULL; | |
1067 | } | |
1068 | ||
efdb0237 LP |
1069 | char *unit_default_cgroup_path(Unit *u) { |
1070 | _cleanup_free_ char *escaped = NULL, *slice = NULL; | |
1071 | int r; | |
1072 | ||
1073 | assert(u); | |
1074 | ||
1075 | if (unit_has_name(u, SPECIAL_ROOT_SLICE)) | |
1076 | return strdup(u->manager->cgroup_root); | |
1077 | ||
1078 | if (UNIT_ISSET(u->slice) && !unit_has_name(UNIT_DEREF(u->slice), SPECIAL_ROOT_SLICE)) { | |
1079 | r = cg_slice_to_path(UNIT_DEREF(u->slice)->id, &slice); | |
1080 | if (r < 0) | |
1081 | return NULL; | |
1082 | } | |
1083 | ||
1084 | escaped = cg_escape(u->id); | |
1085 | if (!escaped) | |
1086 | return NULL; | |
1087 | ||
1088 | if (slice) | |
1089 | return strjoin(u->manager->cgroup_root, "/", slice, "/", escaped, NULL); | |
1090 | else | |
1091 | return strjoin(u->manager->cgroup_root, "/", escaped, NULL); | |
1092 | } | |
1093 | ||
1094 | int unit_set_cgroup_path(Unit *u, const char *path) { | |
1095 | _cleanup_free_ char *p = NULL; | |
1096 | int r; | |
1097 | ||
1098 | assert(u); | |
1099 | ||
1100 | if (path) { | |
1101 | p = strdup(path); | |
1102 | if (!p) | |
1103 | return -ENOMEM; | |
1104 | } else | |
1105 | p = NULL; | |
1106 | ||
1107 | if (streq_ptr(u->cgroup_path, p)) | |
1108 | return 0; | |
1109 | ||
1110 | if (p) { | |
1111 | r = hashmap_put(u->manager->cgroup_unit, p, u); | |
1112 | if (r < 0) | |
1113 | return r; | |
1114 | } | |
1115 | ||
1116 | unit_release_cgroup(u); | |
1117 | ||
1118 | u->cgroup_path = p; | |
1119 | p = NULL; | |
1120 | ||
1121 | return 1; | |
1122 | } | |
1123 | ||
1124 | int unit_watch_cgroup(Unit *u) { | |
ab2c3861 | 1125 | _cleanup_free_ char *events = NULL; |
efdb0237 LP |
1126 | int r; |
1127 | ||
1128 | assert(u); | |
1129 | ||
1130 | if (!u->cgroup_path) | |
1131 | return 0; | |
1132 | ||
1133 | if (u->cgroup_inotify_wd >= 0) | |
1134 | return 0; | |
1135 | ||
1136 | /* Only applies to the unified hierarchy */ | |
1137 | r = cg_unified(); | |
1138 | if (r < 0) | |
595bfe7d | 1139 | return log_unit_error_errno(u, r, "Failed detect whether the unified hierarchy is used: %m"); |
efdb0237 LP |
1140 | if (r == 0) |
1141 | return 0; | |
1142 | ||
1143 | /* Don't watch the root slice, it's pointless. */ | |
1144 | if (unit_has_name(u, SPECIAL_ROOT_SLICE)) | |
1145 | return 0; | |
1146 | ||
1147 | r = hashmap_ensure_allocated(&u->manager->cgroup_inotify_wd_unit, &trivial_hash_ops); | |
1148 | if (r < 0) | |
1149 | return log_oom(); | |
1150 | ||
ab2c3861 | 1151 | r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, "cgroup.events", &events); |
efdb0237 LP |
1152 | if (r < 0) |
1153 | return log_oom(); | |
1154 | ||
ab2c3861 | 1155 | u->cgroup_inotify_wd = inotify_add_watch(u->manager->cgroup_inotify_fd, events, IN_MODIFY); |
efdb0237 LP |
1156 | if (u->cgroup_inotify_wd < 0) { |
1157 | ||
1158 | if (errno == ENOENT) /* If the directory is already | |
1159 | * gone we don't need to track | |
1160 | * it, so this is not an error */ | |
1161 | return 0; | |
1162 | ||
1163 | return log_unit_error_errno(u, errno, "Failed to add inotify watch descriptor for control group %s: %m", u->cgroup_path); | |
1164 | } | |
1165 | ||
1166 | r = hashmap_put(u->manager->cgroup_inotify_wd_unit, INT_TO_PTR(u->cgroup_inotify_wd), u); | |
1167 | if (r < 0) | |
1168 | return log_unit_error_errno(u, r, "Failed to add inotify watch descriptor to hash map: %m"); | |
1169 | ||
1170 | return 0; | |
1171 | } | |
1172 | ||
1173 | static int unit_create_cgroup( | |
1174 | Unit *u, | |
1175 | CGroupMask target_mask, | |
1176 | CGroupMask enable_mask) { | |
1177 | ||
0cd385d3 | 1178 | CGroupContext *c; |
bc432dc7 | 1179 | int r; |
64747e2d | 1180 | |
4ad49000 | 1181 | assert(u); |
64747e2d | 1182 | |
0cd385d3 LP |
1183 | c = unit_get_cgroup_context(u); |
1184 | if (!c) | |
1185 | return 0; | |
1186 | ||
7b3fd631 LP |
1187 | if (!u->cgroup_path) { |
1188 | _cleanup_free_ char *path = NULL; | |
64747e2d | 1189 | |
7b3fd631 LP |
1190 | path = unit_default_cgroup_path(u); |
1191 | if (!path) | |
1192 | return log_oom(); | |
1193 | ||
efdb0237 LP |
1194 | r = unit_set_cgroup_path(u, path); |
1195 | if (r == -EEXIST) | |
1196 | return log_unit_error_errno(u, r, "Control group %s exists already.", path); | |
1197 | if (r < 0) | |
1198 | return log_unit_error_errno(u, r, "Failed to set unit's control group path to %s: %m", path); | |
b58b8e11 HH |
1199 | } |
1200 | ||
03b90d4b | 1201 | /* First, create our own group */ |
efdb0237 | 1202 | r = cg_create_everywhere(u->manager->cgroup_supported, target_mask, u->cgroup_path); |
23bbb0de | 1203 | if (r < 0) |
efdb0237 LP |
1204 | return log_unit_error_errno(u, r, "Failed to create cgroup %s: %m", u->cgroup_path); |
1205 | ||
1206 | /* Start watching it */ | |
1207 | (void) unit_watch_cgroup(u); | |
1208 | ||
1209 | /* Enable all controllers we need */ | |
1210 | r = cg_enable_everywhere(u->manager->cgroup_supported, enable_mask, u->cgroup_path); | |
1211 | if (r < 0) | |
1212 | log_unit_warning_errno(u, r, "Failed to enable controllers on cgroup %s, ignoring: %m", u->cgroup_path); | |
03b90d4b LP |
1213 | |
1214 | /* Keep track that this is now realized */ | |
4ad49000 | 1215 | u->cgroup_realized = true; |
efdb0237 | 1216 | u->cgroup_realized_mask = target_mask; |
ccf78df1 | 1217 | u->cgroup_enabled_mask = enable_mask; |
4ad49000 | 1218 | |
0cd385d3 LP |
1219 | if (u->type != UNIT_SLICE && !c->delegate) { |
1220 | ||
1221 | /* Then, possibly move things over, but not if | |
1222 | * subgroups may contain processes, which is the case | |
1223 | * for slice and delegation units. */ | |
1224 | r = cg_migrate_everywhere(u->manager->cgroup_supported, u->cgroup_path, u->cgroup_path, migrate_callback, u); | |
1225 | if (r < 0) | |
efdb0237 | 1226 | log_unit_warning_errno(u, r, "Failed to migrate cgroup from to %s, ignoring: %m", u->cgroup_path); |
0cd385d3 | 1227 | } |
03b90d4b | 1228 | |
64747e2d LP |
1229 | return 0; |
1230 | } | |
1231 | ||
7b3fd631 LP |
1232 | int unit_attach_pids_to_cgroup(Unit *u) { |
1233 | int r; | |
1234 | assert(u); | |
1235 | ||
1236 | r = unit_realize_cgroup(u); | |
1237 | if (r < 0) | |
1238 | return r; | |
1239 | ||
1240 | r = cg_attach_many_everywhere(u->manager->cgroup_supported, u->cgroup_path, u->pids, migrate_callback, u); | |
1241 | if (r < 0) | |
1242 | return r; | |
1243 | ||
1244 | return 0; | |
1245 | } | |
1246 | ||
ccf78df1 | 1247 | static bool unit_has_mask_realized(Unit *u, CGroupMask target_mask, CGroupMask enable_mask) { |
bc432dc7 LP |
1248 | assert(u); |
1249 | ||
ccf78df1 | 1250 | return u->cgroup_realized && u->cgroup_realized_mask == target_mask && u->cgroup_enabled_mask == enable_mask; |
6414b7c9 DS |
1251 | } |
1252 | ||
1253 | /* Check if necessary controllers and attributes for a unit are in place. | |
1254 | * | |
1255 | * If so, do nothing. | |
1256 | * If not, create paths, move processes over, and set attributes. | |
1257 | * | |
1258 | * Returns 0 on success and < 0 on failure. */ | |
db785129 | 1259 | static int unit_realize_cgroup_now(Unit *u, ManagerState state) { |
efdb0237 | 1260 | CGroupMask target_mask, enable_mask; |
6414b7c9 | 1261 | int r; |
64747e2d | 1262 | |
4ad49000 | 1263 | assert(u); |
64747e2d | 1264 | |
4ad49000 | 1265 | if (u->in_cgroup_queue) { |
71fda00f | 1266 | LIST_REMOVE(cgroup_queue, u->manager->cgroup_queue, u); |
4ad49000 LP |
1267 | u->in_cgroup_queue = false; |
1268 | } | |
64747e2d | 1269 | |
efdb0237 | 1270 | target_mask = unit_get_target_mask(u); |
ccf78df1 TH |
1271 | enable_mask = unit_get_enable_mask(u); |
1272 | ||
1273 | if (unit_has_mask_realized(u, target_mask, enable_mask)) | |
0a1eb06d | 1274 | return 0; |
64747e2d | 1275 | |
4ad49000 | 1276 | /* First, realize parents */ |
6414b7c9 | 1277 | if (UNIT_ISSET(u->slice)) { |
db785129 | 1278 | r = unit_realize_cgroup_now(UNIT_DEREF(u->slice), state); |
6414b7c9 DS |
1279 | if (r < 0) |
1280 | return r; | |
1281 | } | |
4ad49000 LP |
1282 | |
1283 | /* And then do the real work */ | |
efdb0237 | 1284 | r = unit_create_cgroup(u, target_mask, enable_mask); |
6414b7c9 DS |
1285 | if (r < 0) |
1286 | return r; | |
1287 | ||
1288 | /* Finally, apply the necessary attributes. */ | |
f29ff115 | 1289 | cgroup_context_apply(u, target_mask, state); |
6414b7c9 DS |
1290 | |
1291 | return 0; | |
64747e2d LP |
1292 | } |
1293 | ||
4ad49000 | 1294 | static void unit_add_to_cgroup_queue(Unit *u) { |
ecedd90f | 1295 | |
4ad49000 LP |
1296 | if (u->in_cgroup_queue) |
1297 | return; | |
8e274523 | 1298 | |
71fda00f | 1299 | LIST_PREPEND(cgroup_queue, u->manager->cgroup_queue, u); |
4ad49000 LP |
1300 | u->in_cgroup_queue = true; |
1301 | } | |
8c6db833 | 1302 | |
4ad49000 | 1303 | unsigned manager_dispatch_cgroup_queue(Manager *m) { |
db785129 | 1304 | ManagerState state; |
4ad49000 | 1305 | unsigned n = 0; |
db785129 | 1306 | Unit *i; |
6414b7c9 | 1307 | int r; |
ecedd90f | 1308 | |
db785129 LP |
1309 | state = manager_state(m); |
1310 | ||
4ad49000 LP |
1311 | while ((i = m->cgroup_queue)) { |
1312 | assert(i->in_cgroup_queue); | |
ecedd90f | 1313 | |
db785129 | 1314 | r = unit_realize_cgroup_now(i, state); |
6414b7c9 | 1315 | if (r < 0) |
efdb0237 | 1316 | log_warning_errno(r, "Failed to realize cgroups for queued unit %s, ignoring: %m", i->id); |
0a1eb06d | 1317 | |
4ad49000 LP |
1318 | n++; |
1319 | } | |
ecedd90f | 1320 | |
4ad49000 | 1321 | return n; |
8e274523 LP |
1322 | } |
1323 | ||
4ad49000 LP |
1324 | static void unit_queue_siblings(Unit *u) { |
1325 | Unit *slice; | |
ca949c9d | 1326 | |
4ad49000 LP |
1327 | /* This adds the siblings of the specified unit and the |
1328 | * siblings of all parent units to the cgroup queue. (But | |
1329 | * neither the specified unit itself nor the parents.) */ | |
1330 | ||
1331 | while ((slice = UNIT_DEREF(u->slice))) { | |
1332 | Iterator i; | |
1333 | Unit *m; | |
8f53a7b8 | 1334 | |
4ad49000 LP |
1335 | SET_FOREACH(m, slice->dependencies[UNIT_BEFORE], i) { |
1336 | if (m == u) | |
1337 | continue; | |
8e274523 | 1338 | |
6414b7c9 DS |
1339 | /* Skip units that have a dependency on the slice |
1340 | * but aren't actually in it. */ | |
4ad49000 | 1341 | if (UNIT_DEREF(m->slice) != slice) |
50159e6a | 1342 | continue; |
8e274523 | 1343 | |
6414b7c9 DS |
1344 | /* No point in doing cgroup application for units |
1345 | * without active processes. */ | |
1346 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(m))) | |
1347 | continue; | |
1348 | ||
1349 | /* If the unit doesn't need any new controllers | |
1350 | * and has current ones realized, it doesn't need | |
1351 | * any changes. */ | |
ccf78df1 | 1352 | if (unit_has_mask_realized(m, unit_get_target_mask(m), unit_get_enable_mask(m))) |
6414b7c9 DS |
1353 | continue; |
1354 | ||
4ad49000 | 1355 | unit_add_to_cgroup_queue(m); |
50159e6a LP |
1356 | } |
1357 | ||
4ad49000 | 1358 | u = slice; |
8e274523 | 1359 | } |
4ad49000 LP |
1360 | } |
1361 | ||
0a1eb06d | 1362 | int unit_realize_cgroup(Unit *u) { |
4ad49000 LP |
1363 | assert(u); |
1364 | ||
35b7ff80 | 1365 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
0a1eb06d | 1366 | return 0; |
8e274523 | 1367 | |
4ad49000 LP |
1368 | /* So, here's the deal: when realizing the cgroups for this |
1369 | * unit, we need to first create all parents, but there's more | |
1370 | * actually: for the weight-based controllers we also need to | |
1371 | * make sure that all our siblings (i.e. units that are in the | |
73e231ab | 1372 | * same slice as we are) have cgroups, too. Otherwise, things |
4ad49000 LP |
1373 | * would become very uneven as each of their processes would |
1374 | * get as much resources as all our group together. This call | |
1375 | * will synchronously create the parent cgroups, but will | |
1376 | * defer work on the siblings to the next event loop | |
1377 | * iteration. */ | |
ca949c9d | 1378 | |
4ad49000 LP |
1379 | /* Add all sibling slices to the cgroup queue. */ |
1380 | unit_queue_siblings(u); | |
1381 | ||
6414b7c9 | 1382 | /* And realize this one now (and apply the values) */ |
db785129 | 1383 | return unit_realize_cgroup_now(u, manager_state(u->manager)); |
8e274523 LP |
1384 | } |
1385 | ||
efdb0237 LP |
1386 | void unit_release_cgroup(Unit *u) { |
1387 | assert(u); | |
1388 | ||
1389 | /* Forgets all cgroup details for this cgroup */ | |
1390 | ||
1391 | if (u->cgroup_path) { | |
1392 | (void) hashmap_remove(u->manager->cgroup_unit, u->cgroup_path); | |
1393 | u->cgroup_path = mfree(u->cgroup_path); | |
1394 | } | |
1395 | ||
1396 | if (u->cgroup_inotify_wd >= 0) { | |
1397 | if (inotify_rm_watch(u->manager->cgroup_inotify_fd, u->cgroup_inotify_wd) < 0) | |
1398 | log_unit_debug_errno(u, errno, "Failed to remove cgroup inotify watch %i for %s, ignoring", u->cgroup_inotify_wd, u->id); | |
1399 | ||
1400 | (void) hashmap_remove(u->manager->cgroup_inotify_wd_unit, INT_TO_PTR(u->cgroup_inotify_wd)); | |
1401 | u->cgroup_inotify_wd = -1; | |
1402 | } | |
1403 | } | |
1404 | ||
1405 | void unit_prune_cgroup(Unit *u) { | |
8e274523 | 1406 | int r; |
efdb0237 | 1407 | bool is_root_slice; |
8e274523 | 1408 | |
4ad49000 | 1409 | assert(u); |
8e274523 | 1410 | |
efdb0237 LP |
1411 | /* Removes the cgroup, if empty and possible, and stops watching it. */ |
1412 | ||
4ad49000 LP |
1413 | if (!u->cgroup_path) |
1414 | return; | |
8e274523 | 1415 | |
efdb0237 LP |
1416 | is_root_slice = unit_has_name(u, SPECIAL_ROOT_SLICE); |
1417 | ||
1418 | r = cg_trim_everywhere(u->manager->cgroup_supported, u->cgroup_path, !is_root_slice); | |
dab5bf85 | 1419 | if (r < 0) { |
f29ff115 | 1420 | log_unit_debug_errno(u, r, "Failed to destroy cgroup %s, ignoring: %m", u->cgroup_path); |
dab5bf85 RL |
1421 | return; |
1422 | } | |
8e274523 | 1423 | |
efdb0237 LP |
1424 | if (is_root_slice) |
1425 | return; | |
1426 | ||
1427 | unit_release_cgroup(u); | |
0a1eb06d | 1428 | |
4ad49000 | 1429 | u->cgroup_realized = false; |
bc432dc7 | 1430 | u->cgroup_realized_mask = 0; |
ccf78df1 | 1431 | u->cgroup_enabled_mask = 0; |
8e274523 LP |
1432 | } |
1433 | ||
efdb0237 | 1434 | int unit_search_main_pid(Unit *u, pid_t *ret) { |
4ad49000 LP |
1435 | _cleanup_fclose_ FILE *f = NULL; |
1436 | pid_t pid = 0, npid, mypid; | |
efdb0237 | 1437 | int r; |
4ad49000 LP |
1438 | |
1439 | assert(u); | |
efdb0237 | 1440 | assert(ret); |
4ad49000 LP |
1441 | |
1442 | if (!u->cgroup_path) | |
efdb0237 | 1443 | return -ENXIO; |
4ad49000 | 1444 | |
efdb0237 LP |
1445 | r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, &f); |
1446 | if (r < 0) | |
1447 | return r; | |
4ad49000 LP |
1448 | |
1449 | mypid = getpid(); | |
1450 | while (cg_read_pid(f, &npid) > 0) { | |
1451 | pid_t ppid; | |
1452 | ||
1453 | if (npid == pid) | |
1454 | continue; | |
8e274523 | 1455 | |
4ad49000 | 1456 | /* Ignore processes that aren't our kids */ |
6bc73acb | 1457 | if (get_process_ppid(npid, &ppid) >= 0 && ppid != mypid) |
4ad49000 | 1458 | continue; |
8e274523 | 1459 | |
efdb0237 | 1460 | if (pid != 0) |
4ad49000 LP |
1461 | /* Dang, there's more than one daemonized PID |
1462 | in this group, so we don't know what process | |
1463 | is the main process. */ | |
efdb0237 LP |
1464 | |
1465 | return -ENODATA; | |
8e274523 | 1466 | |
4ad49000 | 1467 | pid = npid; |
8e274523 LP |
1468 | } |
1469 | ||
efdb0237 LP |
1470 | *ret = pid; |
1471 | return 0; | |
1472 | } | |
1473 | ||
1474 | static int unit_watch_pids_in_path(Unit *u, const char *path) { | |
b3c5bad3 | 1475 | _cleanup_closedir_ DIR *d = NULL; |
efdb0237 LP |
1476 | _cleanup_fclose_ FILE *f = NULL; |
1477 | int ret = 0, r; | |
1478 | ||
1479 | assert(u); | |
1480 | assert(path); | |
1481 | ||
1482 | r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, path, &f); | |
1483 | if (r < 0) | |
1484 | ret = r; | |
1485 | else { | |
1486 | pid_t pid; | |
1487 | ||
1488 | while ((r = cg_read_pid(f, &pid)) > 0) { | |
1489 | r = unit_watch_pid(u, pid); | |
1490 | if (r < 0 && ret >= 0) | |
1491 | ret = r; | |
1492 | } | |
1493 | ||
1494 | if (r < 0 && ret >= 0) | |
1495 | ret = r; | |
1496 | } | |
1497 | ||
1498 | r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, path, &d); | |
1499 | if (r < 0) { | |
1500 | if (ret >= 0) | |
1501 | ret = r; | |
1502 | } else { | |
1503 | char *fn; | |
1504 | ||
1505 | while ((r = cg_read_subgroup(d, &fn)) > 0) { | |
1506 | _cleanup_free_ char *p = NULL; | |
1507 | ||
1508 | p = strjoin(path, "/", fn, NULL); | |
1509 | free(fn); | |
1510 | ||
1511 | if (!p) | |
1512 | return -ENOMEM; | |
1513 | ||
1514 | r = unit_watch_pids_in_path(u, p); | |
1515 | if (r < 0 && ret >= 0) | |
1516 | ret = r; | |
1517 | } | |
1518 | ||
1519 | if (r < 0 && ret >= 0) | |
1520 | ret = r; | |
1521 | } | |
1522 | ||
1523 | return ret; | |
1524 | } | |
1525 | ||
1526 | int unit_watch_all_pids(Unit *u) { | |
1527 | assert(u); | |
1528 | ||
1529 | /* Adds all PIDs from our cgroup to the set of PIDs we | |
1530 | * watch. This is a fallback logic for cases where we do not | |
1531 | * get reliable cgroup empty notifications: we try to use | |
1532 | * SIGCHLD as replacement. */ | |
1533 | ||
1534 | if (!u->cgroup_path) | |
1535 | return -ENOENT; | |
1536 | ||
1537 | if (cg_unified() > 0) /* On unified we can use proper notifications */ | |
1538 | return 0; | |
1539 | ||
1540 | return unit_watch_pids_in_path(u, u->cgroup_path); | |
1541 | } | |
1542 | ||
1543 | int unit_notify_cgroup_empty(Unit *u) { | |
1544 | int r; | |
1545 | ||
1546 | assert(u); | |
1547 | ||
1548 | if (!u->cgroup_path) | |
1549 | return 0; | |
1550 | ||
1551 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path); | |
1552 | if (r <= 0) | |
1553 | return r; | |
1554 | ||
1555 | unit_add_to_gc_queue(u); | |
1556 | ||
1557 | if (UNIT_VTABLE(u)->notify_cgroup_empty) | |
1558 | UNIT_VTABLE(u)->notify_cgroup_empty(u); | |
1559 | ||
1560 | return 0; | |
1561 | } | |
1562 | ||
1563 | static int on_cgroup_inotify_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
1564 | Manager *m = userdata; | |
1565 | ||
1566 | assert(s); | |
1567 | assert(fd >= 0); | |
1568 | assert(m); | |
1569 | ||
1570 | for (;;) { | |
1571 | union inotify_event_buffer buffer; | |
1572 | struct inotify_event *e; | |
1573 | ssize_t l; | |
1574 | ||
1575 | l = read(fd, &buffer, sizeof(buffer)); | |
1576 | if (l < 0) { | |
1577 | if (errno == EINTR || errno == EAGAIN) | |
1578 | return 0; | |
1579 | ||
1580 | return log_error_errno(errno, "Failed to read control group inotify events: %m"); | |
1581 | } | |
1582 | ||
1583 | FOREACH_INOTIFY_EVENT(e, buffer, l) { | |
1584 | Unit *u; | |
1585 | ||
1586 | if (e->wd < 0) | |
1587 | /* Queue overflow has no watch descriptor */ | |
1588 | continue; | |
1589 | ||
1590 | if (e->mask & IN_IGNORED) | |
1591 | /* The watch was just removed */ | |
1592 | continue; | |
1593 | ||
1594 | u = hashmap_get(m->cgroup_inotify_wd_unit, INT_TO_PTR(e->wd)); | |
1595 | if (!u) /* Not that inotify might deliver | |
1596 | * events for a watch even after it | |
1597 | * was removed, because it was queued | |
1598 | * before the removal. Let's ignore | |
1599 | * this here safely. */ | |
1600 | continue; | |
1601 | ||
1602 | (void) unit_notify_cgroup_empty(u); | |
1603 | } | |
1604 | } | |
8e274523 LP |
1605 | } |
1606 | ||
8e274523 | 1607 | int manager_setup_cgroup(Manager *m) { |
9444b1f2 | 1608 | _cleanup_free_ char *path = NULL; |
efdb0237 LP |
1609 | CGroupController c; |
1610 | int r, unified; | |
1611 | char *e; | |
8e274523 LP |
1612 | |
1613 | assert(m); | |
1614 | ||
35d2e7ec | 1615 | /* 1. Determine hierarchy */ |
efdb0237 | 1616 | m->cgroup_root = mfree(m->cgroup_root); |
9444b1f2 | 1617 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &m->cgroup_root); |
23bbb0de MS |
1618 | if (r < 0) |
1619 | return log_error_errno(r, "Cannot determine cgroup we are running in: %m"); | |
8e274523 | 1620 | |
efdb0237 LP |
1621 | /* Chop off the init scope, if we are already located in it */ |
1622 | e = endswith(m->cgroup_root, "/" SPECIAL_INIT_SCOPE); | |
0d8c31ff | 1623 | |
efdb0237 LP |
1624 | /* LEGACY: Also chop off the system slice if we are in |
1625 | * it. This is to support live upgrades from older systemd | |
1626 | * versions where PID 1 was moved there. Also see | |
1627 | * cg_get_root_path(). */ | |
463d0d15 | 1628 | if (!e && MANAGER_IS_SYSTEM(m)) { |
9444b1f2 | 1629 | e = endswith(m->cgroup_root, "/" SPECIAL_SYSTEM_SLICE); |
15c60e99 | 1630 | if (!e) |
efdb0237 | 1631 | e = endswith(m->cgroup_root, "/system"); /* even more legacy */ |
0baf24dd | 1632 | } |
efdb0237 LP |
1633 | if (e) |
1634 | *e = 0; | |
7ccfb64a | 1635 | |
9444b1f2 LP |
1636 | /* And make sure to store away the root value without trailing |
1637 | * slash, even for the root dir, so that we can easily prepend | |
1638 | * it everywhere. */ | |
efdb0237 LP |
1639 | while ((e = endswith(m->cgroup_root, "/"))) |
1640 | *e = 0; | |
8e274523 | 1641 | |
35d2e7ec | 1642 | /* 2. Show data */ |
9444b1f2 | 1643 | r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, NULL, &path); |
23bbb0de MS |
1644 | if (r < 0) |
1645 | return log_error_errno(r, "Cannot find cgroup mount point: %m"); | |
8e274523 | 1646 | |
efdb0237 LP |
1647 | unified = cg_unified(); |
1648 | if (unified < 0) | |
1649 | return log_error_errno(r, "Couldn't determine if we are running in the unified hierarchy: %m"); | |
1650 | if (unified > 0) | |
1651 | log_debug("Unified cgroup hierarchy is located at %s.", path); | |
1652 | else | |
1653 | log_debug("Using cgroup controller " SYSTEMD_CGROUP_CONTROLLER ". File system hierarchy is at %s.", path); | |
1654 | ||
0d8c31ff | 1655 | if (!m->test_run) { |
efdb0237 | 1656 | const char *scope_path; |
c6c18be3 | 1657 | |
0d8c31ff | 1658 | /* 3. Install agent */ |
efdb0237 LP |
1659 | if (unified) { |
1660 | ||
61233823 | 1661 | /* In the unified hierarchy we can get |
efdb0237 LP |
1662 | * cgroup empty notifications via inotify. */ |
1663 | ||
1664 | m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source); | |
1665 | safe_close(m->cgroup_inotify_fd); | |
1666 | ||
1667 | m->cgroup_inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); | |
1668 | if (m->cgroup_inotify_fd < 0) | |
1669 | return log_error_errno(errno, "Failed to create control group inotify object: %m"); | |
1670 | ||
1671 | r = sd_event_add_io(m->event, &m->cgroup_inotify_event_source, m->cgroup_inotify_fd, EPOLLIN, on_cgroup_inotify_event, m); | |
1672 | if (r < 0) | |
1673 | return log_error_errno(r, "Failed to watch control group inotify object: %m"); | |
1674 | ||
d8fdc620 LP |
1675 | /* Process cgroup empty notifications early, but after service notifications and SIGCHLD. Also |
1676 | * see handling of cgroup agent notifications, for the classic cgroup hierarchy support. */ | |
1677 | r = sd_event_source_set_priority(m->cgroup_inotify_event_source, SD_EVENT_PRIORITY_NORMAL-5); | |
efdb0237 LP |
1678 | if (r < 0) |
1679 | return log_error_errno(r, "Failed to set priority of inotify event source: %m"); | |
1680 | ||
1681 | (void) sd_event_source_set_description(m->cgroup_inotify_event_source, "cgroup-inotify"); | |
1682 | ||
463d0d15 | 1683 | } else if (MANAGER_IS_SYSTEM(m)) { |
efdb0237 LP |
1684 | |
1685 | /* On the legacy hierarchy we only get | |
1686 | * notifications via cgroup agents. (Which | |
1687 | * isn't really reliable, since it does not | |
1688 | * generate events when control groups with | |
1689 | * children run empty. */ | |
1690 | ||
0d8c31ff ZJS |
1691 | r = cg_install_release_agent(SYSTEMD_CGROUP_CONTROLLER, SYSTEMD_CGROUP_AGENT_PATH); |
1692 | if (r < 0) | |
da927ba9 | 1693 | log_warning_errno(r, "Failed to install release agent, ignoring: %m"); |
0d8c31ff ZJS |
1694 | else if (r > 0) |
1695 | log_debug("Installed release agent."); | |
efdb0237 | 1696 | else if (r == 0) |
0d8c31ff ZJS |
1697 | log_debug("Release agent already installed."); |
1698 | } | |
8e274523 | 1699 | |
efdb0237 LP |
1700 | /* 4. Make sure we are in the special "init.scope" unit in the root slice. */ |
1701 | scope_path = strjoina(m->cgroup_root, "/" SPECIAL_INIT_SCOPE); | |
1702 | r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, scope_path, 0); | |
23bbb0de | 1703 | if (r < 0) |
efdb0237 LP |
1704 | return log_error_errno(r, "Failed to create %s control group: %m", scope_path); |
1705 | ||
1706 | /* also, move all other userspace processes remaining | |
1707 | * in the root cgroup into that scope. */ | |
1d98fef1 | 1708 | r = cg_migrate(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, SYSTEMD_CGROUP_CONTROLLER, scope_path, 0); |
efdb0237 LP |
1709 | if (r < 0) |
1710 | log_warning_errno(r, "Couldn't move remaining userspace processes, ignoring: %m"); | |
c6c18be3 | 1711 | |
0d8c31ff ZJS |
1712 | /* 5. And pin it, so that it cannot be unmounted */ |
1713 | safe_close(m->pin_cgroupfs_fd); | |
0d8c31ff | 1714 | m->pin_cgroupfs_fd = open(path, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY|O_NONBLOCK); |
4a62c710 MS |
1715 | if (m->pin_cgroupfs_fd < 0) |
1716 | return log_error_errno(errno, "Failed to open pin file: %m"); | |
0d8c31ff | 1717 | |
cc98b302 | 1718 | /* 6. Always enable hierarchical support if it exists... */ |
efdb0237 LP |
1719 | if (!unified) |
1720 | (void) cg_set_attribute("memory", "/", "memory.use_hierarchy", "1"); | |
c6c18be3 LP |
1721 | } |
1722 | ||
0d8c31ff | 1723 | /* 7. Figure out which controllers are supported */ |
efdb0237 LP |
1724 | r = cg_mask_supported(&m->cgroup_supported); |
1725 | if (r < 0) | |
1726 | return log_error_errno(r, "Failed to determine supported controllers: %m"); | |
1727 | ||
1728 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) | |
eee0a1e4 | 1729 | log_debug("Controller '%s' supported: %s", cgroup_controller_to_string(c), yes_no(m->cgroup_supported & CGROUP_CONTROLLER_TO_MASK(c))); |
9156e799 | 1730 | |
a32360f1 | 1731 | return 0; |
8e274523 LP |
1732 | } |
1733 | ||
c6c18be3 | 1734 | void manager_shutdown_cgroup(Manager *m, bool delete) { |
8e274523 LP |
1735 | assert(m); |
1736 | ||
9444b1f2 LP |
1737 | /* We can't really delete the group, since we are in it. But |
1738 | * let's trim it. */ | |
1739 | if (delete && m->cgroup_root) | |
efdb0237 LP |
1740 | (void) cg_trim(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, false); |
1741 | ||
1742 | m->cgroup_inotify_wd_unit = hashmap_free(m->cgroup_inotify_wd_unit); | |
1743 | ||
1744 | m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source); | |
1745 | m->cgroup_inotify_fd = safe_close(m->cgroup_inotify_fd); | |
8e274523 | 1746 | |
03e334a1 | 1747 | m->pin_cgroupfs_fd = safe_close(m->pin_cgroupfs_fd); |
c6c18be3 | 1748 | |
efdb0237 | 1749 | m->cgroup_root = mfree(m->cgroup_root); |
8e274523 LP |
1750 | } |
1751 | ||
4ad49000 | 1752 | Unit* manager_get_unit_by_cgroup(Manager *m, const char *cgroup) { |
acb14d31 | 1753 | char *p; |
4ad49000 | 1754 | Unit *u; |
acb14d31 LP |
1755 | |
1756 | assert(m); | |
1757 | assert(cgroup); | |
acb14d31 | 1758 | |
4ad49000 LP |
1759 | u = hashmap_get(m->cgroup_unit, cgroup); |
1760 | if (u) | |
1761 | return u; | |
acb14d31 | 1762 | |
8e70580b | 1763 | p = strdupa(cgroup); |
acb14d31 LP |
1764 | for (;;) { |
1765 | char *e; | |
1766 | ||
1767 | e = strrchr(p, '/'); | |
efdb0237 LP |
1768 | if (!e || e == p) |
1769 | return hashmap_get(m->cgroup_unit, SPECIAL_ROOT_SLICE); | |
acb14d31 LP |
1770 | |
1771 | *e = 0; | |
1772 | ||
4ad49000 LP |
1773 | u = hashmap_get(m->cgroup_unit, p); |
1774 | if (u) | |
1775 | return u; | |
acb14d31 LP |
1776 | } |
1777 | } | |
1778 | ||
b3ac818b | 1779 | Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid) { |
4ad49000 | 1780 | _cleanup_free_ char *cgroup = NULL; |
acb14d31 | 1781 | int r; |
8e274523 | 1782 | |
8c47c732 LP |
1783 | assert(m); |
1784 | ||
b3ac818b LP |
1785 | if (pid <= 0) |
1786 | return NULL; | |
1787 | ||
1788 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup); | |
1789 | if (r < 0) | |
1790 | return NULL; | |
1791 | ||
1792 | return manager_get_unit_by_cgroup(m, cgroup); | |
1793 | } | |
1794 | ||
1795 | Unit *manager_get_unit_by_pid(Manager *m, pid_t pid) { | |
1796 | Unit *u; | |
1797 | ||
1798 | assert(m); | |
1799 | ||
efdb0237 | 1800 | if (pid <= 0) |
8c47c732 LP |
1801 | return NULL; |
1802 | ||
efdb0237 LP |
1803 | if (pid == 1) |
1804 | return hashmap_get(m->units, SPECIAL_INIT_SCOPE); | |
1805 | ||
fea72cc0 | 1806 | u = hashmap_get(m->watch_pids1, PID_TO_PTR(pid)); |
5fe8876b LP |
1807 | if (u) |
1808 | return u; | |
1809 | ||
fea72cc0 | 1810 | u = hashmap_get(m->watch_pids2, PID_TO_PTR(pid)); |
5fe8876b LP |
1811 | if (u) |
1812 | return u; | |
1813 | ||
b3ac818b | 1814 | return manager_get_unit_by_pid_cgroup(m, pid); |
6dde1f33 | 1815 | } |
4fbf50b3 | 1816 | |
4ad49000 LP |
1817 | int manager_notify_cgroup_empty(Manager *m, const char *cgroup) { |
1818 | Unit *u; | |
4fbf50b3 | 1819 | |
4ad49000 LP |
1820 | assert(m); |
1821 | assert(cgroup); | |
4fbf50b3 | 1822 | |
d8fdc620 LP |
1823 | log_debug("Got cgroup empty notification for: %s", cgroup); |
1824 | ||
4ad49000 | 1825 | u = manager_get_unit_by_cgroup(m, cgroup); |
5ad096b3 LP |
1826 | if (!u) |
1827 | return 0; | |
b56c28c3 | 1828 | |
efdb0237 | 1829 | return unit_notify_cgroup_empty(u); |
5ad096b3 LP |
1830 | } |
1831 | ||
1832 | int unit_get_memory_current(Unit *u, uint64_t *ret) { | |
1833 | _cleanup_free_ char *v = NULL; | |
1834 | int r; | |
1835 | ||
1836 | assert(u); | |
1837 | assert(ret); | |
1838 | ||
1839 | if (!u->cgroup_path) | |
1840 | return -ENODATA; | |
1841 | ||
efdb0237 | 1842 | if ((u->cgroup_realized_mask & CGROUP_MASK_MEMORY) == 0) |
5ad096b3 LP |
1843 | return -ENODATA; |
1844 | ||
efdb0237 LP |
1845 | if (cg_unified() <= 0) |
1846 | r = cg_get_attribute("memory", u->cgroup_path, "memory.usage_in_bytes", &v); | |
1847 | else | |
1848 | r = cg_get_attribute("memory", u->cgroup_path, "memory.current", &v); | |
5ad096b3 LP |
1849 | if (r == -ENOENT) |
1850 | return -ENODATA; | |
1851 | if (r < 0) | |
1852 | return r; | |
1853 | ||
1854 | return safe_atou64(v, ret); | |
1855 | } | |
1856 | ||
03a7b521 LP |
1857 | int unit_get_tasks_current(Unit *u, uint64_t *ret) { |
1858 | _cleanup_free_ char *v = NULL; | |
1859 | int r; | |
1860 | ||
1861 | assert(u); | |
1862 | assert(ret); | |
1863 | ||
1864 | if (!u->cgroup_path) | |
1865 | return -ENODATA; | |
1866 | ||
1867 | if ((u->cgroup_realized_mask & CGROUP_MASK_PIDS) == 0) | |
1868 | return -ENODATA; | |
1869 | ||
1870 | r = cg_get_attribute("pids", u->cgroup_path, "pids.current", &v); | |
1871 | if (r == -ENOENT) | |
1872 | return -ENODATA; | |
1873 | if (r < 0) | |
1874 | return r; | |
1875 | ||
1876 | return safe_atou64(v, ret); | |
1877 | } | |
1878 | ||
5ad096b3 LP |
1879 | static int unit_get_cpu_usage_raw(Unit *u, nsec_t *ret) { |
1880 | _cleanup_free_ char *v = NULL; | |
1881 | uint64_t ns; | |
1882 | int r; | |
1883 | ||
1884 | assert(u); | |
1885 | assert(ret); | |
1886 | ||
1887 | if (!u->cgroup_path) | |
1888 | return -ENODATA; | |
1889 | ||
efdb0237 | 1890 | if ((u->cgroup_realized_mask & CGROUP_MASK_CPUACCT) == 0) |
5ad096b3 LP |
1891 | return -ENODATA; |
1892 | ||
1893 | r = cg_get_attribute("cpuacct", u->cgroup_path, "cpuacct.usage", &v); | |
1894 | if (r == -ENOENT) | |
1895 | return -ENODATA; | |
1896 | if (r < 0) | |
1897 | return r; | |
1898 | ||
1899 | r = safe_atou64(v, &ns); | |
1900 | if (r < 0) | |
1901 | return r; | |
1902 | ||
1903 | *ret = ns; | |
1904 | return 0; | |
1905 | } | |
1906 | ||
1907 | int unit_get_cpu_usage(Unit *u, nsec_t *ret) { | |
1908 | nsec_t ns; | |
1909 | int r; | |
1910 | ||
1911 | r = unit_get_cpu_usage_raw(u, &ns); | |
1912 | if (r < 0) | |
1913 | return r; | |
1914 | ||
1915 | if (ns > u->cpuacct_usage_base) | |
1916 | ns -= u->cpuacct_usage_base; | |
1917 | else | |
1918 | ns = 0; | |
1919 | ||
1920 | *ret = ns; | |
1921 | return 0; | |
1922 | } | |
1923 | ||
1924 | int unit_reset_cpu_usage(Unit *u) { | |
1925 | nsec_t ns; | |
1926 | int r; | |
1927 | ||
1928 | assert(u); | |
1929 | ||
1930 | r = unit_get_cpu_usage_raw(u, &ns); | |
1931 | if (r < 0) { | |
1932 | u->cpuacct_usage_base = 0; | |
1933 | return r; | |
b56c28c3 | 1934 | } |
2633eb83 | 1935 | |
5ad096b3 | 1936 | u->cpuacct_usage_base = ns; |
4ad49000 | 1937 | return 0; |
4fbf50b3 LP |
1938 | } |
1939 | ||
e9db43d5 LP |
1940 | bool unit_cgroup_delegate(Unit *u) { |
1941 | CGroupContext *c; | |
1942 | ||
1943 | assert(u); | |
1944 | ||
1945 | c = unit_get_cgroup_context(u); | |
1946 | if (!c) | |
1947 | return false; | |
1948 | ||
1949 | return c->delegate; | |
1950 | } | |
1951 | ||
e7ab4d1a LP |
1952 | void unit_invalidate_cgroup(Unit *u, CGroupMask m) { |
1953 | assert(u); | |
1954 | ||
1955 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) | |
1956 | return; | |
1957 | ||
1958 | if (m == 0) | |
1959 | return; | |
1960 | ||
538b4852 TH |
1961 | /* always invalidate compat pairs together */ |
1962 | if (m & (CGROUP_MASK_IO | CGROUP_MASK_BLKIO)) | |
1963 | m |= CGROUP_MASK_IO | CGROUP_MASK_BLKIO; | |
1964 | ||
e7ab4d1a LP |
1965 | if ((u->cgroup_realized_mask & m) == 0) |
1966 | return; | |
1967 | ||
1968 | u->cgroup_realized_mask &= ~m; | |
1969 | unit_add_to_cgroup_queue(u); | |
1970 | } | |
1971 | ||
1972 | void manager_invalidate_startup_units(Manager *m) { | |
1973 | Iterator i; | |
1974 | Unit *u; | |
1975 | ||
1976 | assert(m); | |
1977 | ||
1978 | SET_FOREACH(u, m->startup_units, i) | |
13c31542 | 1979 | unit_invalidate_cgroup(u, CGROUP_MASK_CPU|CGROUP_MASK_IO|CGROUP_MASK_BLKIO); |
e7ab4d1a LP |
1980 | } |
1981 | ||
4ad49000 LP |
1982 | static const char* const cgroup_device_policy_table[_CGROUP_DEVICE_POLICY_MAX] = { |
1983 | [CGROUP_AUTO] = "auto", | |
1984 | [CGROUP_CLOSED] = "closed", | |
1985 | [CGROUP_STRICT] = "strict", | |
1986 | }; | |
4fbf50b3 | 1987 | |
4ad49000 | 1988 | DEFINE_STRING_TABLE_LOOKUP(cgroup_device_policy, CGroupDevicePolicy); |