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