]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-cgroup-mask.c
cgroup-util: add brief comments clarifying which controllers are v2-only and which...
[thirdparty/systemd.git] / src / test / test-cgroup-mask.c
CommitLineData
6414b7c9
DS
1/***
2 This file is part of systemd.
3
4 Copyright 2013 David Strauss
5
6 systemd is free software; you can redistribute it and/or modify it
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
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <stdio.h>
6414b7c9 21
6414b7c9 22#include "macro.h"
cf0fbc49 23#include "manager.h"
d2120590 24#include "rm-rf.h"
6414b7c9 25#include "test-helper.h"
d2120590 26#include "tests.h"
cf0fbc49 27#include "unit.h"
6414b7c9
DS
28
29static int test_cgroup_mask(void) {
3e29e810 30 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
39883f62 31 Manager *m = NULL;
bc432dc7 32 Unit *son, *daughter, *parent, *root, *grandchild, *parent_deep;
6414b7c9
DS
33 FILE *serial = NULL;
34 FDSet *fdset = NULL;
35 int r;
36
651d47d1
ZJS
37 r = enter_cgroup_subroot();
38 if (r == -ENOMEDIUM) {
39 puts("Skipping test: cgroupfs not available");
40 return EXIT_TEST_SKIP;
41 }
8c759b33 42
6414b7c9 43 /* Prepare the manager. */
cc100a5a 44 assert_se(set_unit_path(get_testdata_dir("")) >= 0);
3e29e810 45 assert_se(runtime_dir = setup_fake_runtime_dir());
e0a3da1f 46 r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
4c701096 47 if (IN_SET(r, -EPERM, -EACCES)) {
6414b7c9
DS
48 puts("manager_new: Permission denied. Skipping test.");
49 return EXIT_TEST_SKIP;
50 }
def8b4c5 51 assert_se(r >= 0);
9ded9cd1
LP
52
53 /* Turn off all kinds of default accouning, so that we can
54 * verify the masks resulting of our configuration and nothing
55 * else. */
56 m->default_cpu_accounting =
57 m->default_memory_accounting =
58 m->default_blockio_accounting =
538b4852 59 m->default_io_accounting =
9ded9cd1
LP
60 m->default_tasks_accounting = false;
61 m->default_tasks_max = (uint64_t) -1;
62
bdf7026e 63 assert_se(r >= 0);
6414b7c9
DS
64 assert_se(manager_startup(m, serial, fdset) >= 0);
65
66 /* Load units and verify hierarchy. */
67 assert_se(manager_load_unit(m, "parent.slice", NULL, NULL, &parent) >= 0);
68 assert_se(manager_load_unit(m, "son.service", NULL, NULL, &son) >= 0);
69 assert_se(manager_load_unit(m, "daughter.service", NULL, NULL, &daughter) >= 0);
bc432dc7
LP
70 assert_se(manager_load_unit(m, "grandchild.service", NULL, NULL, &grandchild) >= 0);
71 assert_se(manager_load_unit(m, "parent-deep.slice", NULL, NULL, &parent_deep) >= 0);
bdf7026e
TA
72 assert_se(parent->load_state == UNIT_LOADED);
73 assert_se(son->load_state == UNIT_LOADED);
74 assert_se(daughter->load_state == UNIT_LOADED);
75 assert_se(grandchild->load_state == UNIT_LOADED);
76 assert_se(parent_deep->load_state == UNIT_LOADED);
77 assert_se(UNIT_DEREF(son->slice) == parent);
78 assert_se(UNIT_DEREF(daughter->slice) == parent);
79 assert_se(UNIT_DEREF(parent_deep->slice) == parent);
80 assert_se(UNIT_DEREF(grandchild->slice) == parent_deep);
6414b7c9
DS
81 root = UNIT_DEREF(parent->slice);
82
83 /* Verify per-unit cgroups settings. */
efdb0237
LP
84 assert_se(unit_get_own_mask(son) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT));
85 assert_se(unit_get_own_mask(daughter) == 0);
86 assert_se(unit_get_own_mask(grandchild) == 0);
87 assert_se(unit_get_own_mask(parent_deep) == CGROUP_MASK_MEMORY);
538b4852 88 assert_se(unit_get_own_mask(parent) == (CGROUP_MASK_IO | CGROUP_MASK_BLKIO));
efdb0237 89 assert_se(unit_get_own_mask(root) == 0);
bc432dc7
LP
90
91 /* Verify aggregation of member masks */
bdf7026e
TA
92 assert_se(unit_get_members_mask(son) == 0);
93 assert_se(unit_get_members_mask(daughter) == 0);
94 assert_se(unit_get_members_mask(grandchild) == 0);
95 assert_se(unit_get_members_mask(parent_deep) == 0);
efdb0237 96 assert_se(unit_get_members_mask(parent) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
538b4852 97 assert_se(unit_get_members_mask(root) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
bc432dc7
LP
98
99 /* Verify aggregation of sibling masks. */
efdb0237
LP
100 assert_se(unit_get_siblings_mask(son) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
101 assert_se(unit_get_siblings_mask(daughter) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
bdf7026e 102 assert_se(unit_get_siblings_mask(grandchild) == 0);
efdb0237 103 assert_se(unit_get_siblings_mask(parent_deep) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
538b4852
TH
104 assert_se(unit_get_siblings_mask(parent) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
105 assert_se(unit_get_siblings_mask(root) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
bc432dc7
LP
106
107 /* Verify aggregation of target masks. */
efdb0237
LP
108 assert_se(unit_get_target_mask(son) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY) & m->cgroup_supported));
109 assert_se(unit_get_target_mask(daughter) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY) & m->cgroup_supported));
bdf7026e 110 assert_se(unit_get_target_mask(grandchild) == 0);
efdb0237 111 assert_se(unit_get_target_mask(parent_deep) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY) & m->cgroup_supported));
538b4852
TH
112 assert_se(unit_get_target_mask(parent) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY) & m->cgroup_supported));
113 assert_se(unit_get_target_mask(root) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY) & m->cgroup_supported));
6414b7c9
DS
114
115 manager_free(m);
116
117 return 0;
118}
119
120int main(int argc, char* argv[]) {
121 int rc = 0;
d2120590 122
6414b7c9 123 TEST_REQ_RUNNING_SYSTEMD(rc = test_cgroup_mask());
d2120590 124
6414b7c9
DS
125 return rc;
126}