]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-cgroup-mask.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-cgroup-mask.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 David Strauss
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <stdio.h>
22
23 #include "macro.h"
24 #include "manager.h"
25 #include "rm-rf.h"
26 #include "string-util.h"
27 #include "test-helper.h"
28 #include "tests.h"
29 #include "unit.h"
30
31 static int test_cgroup_mask(void) {
32 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
33 Manager *m = NULL;
34 Unit *son, *daughter, *parent, *root, *grandchild, *parent_deep;
35 FILE *serial = NULL;
36 FDSet *fdset = NULL;
37 int r;
38
39 r = enter_cgroup_subroot();
40 if (r == -ENOMEDIUM) {
41 puts("Skipping test: cgroupfs not available");
42 return EXIT_TEST_SKIP;
43 }
44
45 /* Prepare the manager. */
46 assert_se(set_unit_path(get_testdata_dir("")) >= 0);
47 assert_se(runtime_dir = setup_fake_runtime_dir());
48 r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
49 if (IN_SET(r, -EPERM, -EACCES)) {
50 puts("manager_new: Permission denied. Skipping test.");
51 return EXIT_TEST_SKIP;
52 }
53 assert_se(r >= 0);
54
55 /* Turn off all kinds of default accouning, so that we can
56 * verify the masks resulting of our configuration and nothing
57 * else. */
58 m->default_cpu_accounting =
59 m->default_memory_accounting =
60 m->default_blockio_accounting =
61 m->default_io_accounting =
62 m->default_tasks_accounting = false;
63 m->default_tasks_max = (uint64_t) -1;
64
65 assert_se(r >= 0);
66 assert_se(manager_startup(m, serial, fdset) >= 0);
67
68 /* Load units and verify hierarchy. */
69 assert_se(manager_load_unit(m, "parent.slice", NULL, NULL, &parent) >= 0);
70 assert_se(manager_load_unit(m, "son.service", NULL, NULL, &son) >= 0);
71 assert_se(manager_load_unit(m, "daughter.service", NULL, NULL, &daughter) >= 0);
72 assert_se(manager_load_unit(m, "grandchild.service", NULL, NULL, &grandchild) >= 0);
73 assert_se(manager_load_unit(m, "parent-deep.slice", NULL, NULL, &parent_deep) >= 0);
74 assert_se(parent->load_state == UNIT_LOADED);
75 assert_se(son->load_state == UNIT_LOADED);
76 assert_se(daughter->load_state == UNIT_LOADED);
77 assert_se(grandchild->load_state == UNIT_LOADED);
78 assert_se(parent_deep->load_state == UNIT_LOADED);
79 assert_se(UNIT_DEREF(son->slice) == parent);
80 assert_se(UNIT_DEREF(daughter->slice) == parent);
81 assert_se(UNIT_DEREF(parent_deep->slice) == parent);
82 assert_se(UNIT_DEREF(grandchild->slice) == parent_deep);
83 root = UNIT_DEREF(parent->slice);
84
85 /* Verify per-unit cgroups settings. */
86 assert_se(unit_get_own_mask(son) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT));
87 assert_se(unit_get_own_mask(daughter) == 0);
88 assert_se(unit_get_own_mask(grandchild) == 0);
89 assert_se(unit_get_own_mask(parent_deep) == CGROUP_MASK_MEMORY);
90 assert_se(unit_get_own_mask(parent) == (CGROUP_MASK_IO | CGROUP_MASK_BLKIO));
91 assert_se(unit_get_own_mask(root) == 0);
92
93 /* Verify aggregation of member masks */
94 assert_se(unit_get_members_mask(son) == 0);
95 assert_se(unit_get_members_mask(daughter) == 0);
96 assert_se(unit_get_members_mask(grandchild) == 0);
97 assert_se(unit_get_members_mask(parent_deep) == 0);
98 assert_se(unit_get_members_mask(parent) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
99 assert_se(unit_get_members_mask(root) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
100
101 /* Verify aggregation of sibling masks. */
102 assert_se(unit_get_siblings_mask(son) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
103 assert_se(unit_get_siblings_mask(daughter) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
104 assert_se(unit_get_siblings_mask(grandchild) == 0);
105 assert_se(unit_get_siblings_mask(parent_deep) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
106 assert_se(unit_get_siblings_mask(parent) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
107 assert_se(unit_get_siblings_mask(root) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
108
109 /* Verify aggregation of target masks. */
110 assert_se(unit_get_target_mask(son) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY) & m->cgroup_supported));
111 assert_se(unit_get_target_mask(daughter) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY) & m->cgroup_supported));
112 assert_se(unit_get_target_mask(grandchild) == 0);
113 assert_se(unit_get_target_mask(parent_deep) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY) & m->cgroup_supported));
114 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));
115 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));
116
117 manager_free(m);
118
119 return 0;
120 }
121
122 static void test_cg_mask_to_string_one(CGroupMask mask, const char *t) {
123 _cleanup_free_ char *b = NULL;
124
125 assert_se(cg_mask_to_string(mask, &b) >= 0);
126 assert_se(streq_ptr(b, t));
127 }
128
129 static void test_cg_mask_to_string(void) {
130 test_cg_mask_to_string_one(0, NULL);
131 test_cg_mask_to_string_one(_CGROUP_MASK_ALL, "cpu cpuacct io blkio memory devices pids");
132 test_cg_mask_to_string_one(CGROUP_MASK_CPU, "cpu");
133 test_cg_mask_to_string_one(CGROUP_MASK_CPUACCT, "cpuacct");
134 test_cg_mask_to_string_one(CGROUP_MASK_IO, "io");
135 test_cg_mask_to_string_one(CGROUP_MASK_BLKIO, "blkio");
136 test_cg_mask_to_string_one(CGROUP_MASK_MEMORY, "memory");
137 test_cg_mask_to_string_one(CGROUP_MASK_DEVICES, "devices");
138 test_cg_mask_to_string_one(CGROUP_MASK_PIDS, "pids");
139 test_cg_mask_to_string_one(CGROUP_MASK_CPU|CGROUP_MASK_CPUACCT, "cpu cpuacct");
140 test_cg_mask_to_string_one(CGROUP_MASK_CPU|CGROUP_MASK_PIDS, "cpu pids");
141 test_cg_mask_to_string_one(CGROUP_MASK_CPUACCT|CGROUP_MASK_PIDS, "cpuacct pids");
142 test_cg_mask_to_string_one(CGROUP_MASK_DEVICES|CGROUP_MASK_PIDS, "devices pids");
143 test_cg_mask_to_string_one(CGROUP_MASK_IO|CGROUP_MASK_BLKIO, "io blkio");
144 }
145
146 int main(int argc, char* argv[]) {
147 int rc = 0;
148
149 log_parse_environment();
150 log_open();
151
152 TEST_REQ_RUNNING_SYSTEMD(rc = test_cgroup_mask());
153 test_cg_mask_to_string();
154
155 return rc;
156 }