Branch data Line data Source code
1 : : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : :
3 : : #include <stdio.h>
4 : :
5 : : #include "cgroup.h"
6 : : #include "cgroup-util.h"
7 : : #include "macro.h"
8 : : #include "manager.h"
9 : : #include "rm-rf.h"
10 : : #include "string-util.h"
11 : : #include "test-helper.h"
12 : : #include "tests.h"
13 : : #include "unit.h"
14 : :
15 : : #define ASSERT_CGROUP_MASK(got, expected) \
16 : : log_cgroup_mask(got, expected); \
17 : : assert_se(got == expected)
18 : :
19 : : #define ASSERT_CGROUP_MASK_JOINED(got, expected) ASSERT_CGROUP_MASK(got, CGROUP_MASK_EXTEND_JOINED(expected))
20 : :
21 : 128 : static void log_cgroup_mask(CGroupMask got, CGroupMask expected) {
22 : 128 : _cleanup_free_ char *e_store = NULL, *g_store = NULL;
23 : :
24 [ - + ]: 128 : assert_se(cg_mask_to_string(expected, &e_store) >= 0);
25 [ + - ]: 128 : log_info("Expected mask: %s\n", e_store);
26 [ - + ]: 128 : assert_se(cg_mask_to_string(got, &g_store) >= 0);
27 [ + - ]: 128 : log_info("Got mask: %s\n", g_store);
28 : 128 : }
29 : :
30 : 4 : static int test_cgroup_mask(void) {
31 : 4 : _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
32 : 4 : _cleanup_(manager_freep) Manager *m = NULL;
33 : : Unit *son, *daughter, *parent, *root, *grandchild, *parent_deep, *nomem_parent, *nomem_leaf;
34 : : int r;
35 : 4 : CGroupMask cpu_accounting_mask = get_cpu_accounting_mask();
36 : :
37 : 4 : r = enter_cgroup_subroot();
38 [ - + ]: 4 : if (r == -ENOMEDIUM)
39 : 0 : return log_tests_skipped("cgroupfs not available");
40 : :
41 : : /* Prepare the manager. */
42 [ - + ]: 4 : assert_se(set_unit_path(get_testdata_dir()) >= 0);
43 [ - + ]: 4 : assert_se(runtime_dir = setup_fake_runtime_dir());
44 : 4 : r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &m);
45 [ - + - + ]: 4 : if (IN_SET(r, -EPERM, -EACCES)) {
46 [ # # ]: 0 : log_error_errno(r, "manager_new: %m");
47 : 0 : return log_tests_skipped("cannot create manager");
48 : : }
49 : :
50 [ - + ]: 4 : assert_se(r >= 0);
51 : :
52 : : /* Turn off all kinds of default accouning, so that we can
53 : : * verify the masks resulting of our configuration and nothing
54 : : * else. */
55 : 8 : m->default_cpu_accounting =
56 : 12 : m->default_memory_accounting =
57 : 12 : m->default_blockio_accounting =
58 : 12 : m->default_io_accounting =
59 : 8 : m->default_tasks_accounting = false;
60 : 4 : m->default_tasks_max = (uint64_t) -1;
61 : :
62 [ - + ]: 4 : assert_se(manager_startup(m, NULL, NULL) >= 0);
63 : :
64 : : /* Load units and verify hierarchy. */
65 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "parent.slice", NULL, &parent) >= 0);
66 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "son.service", NULL, &son) >= 0);
67 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "daughter.service", NULL, &daughter) >= 0);
68 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "grandchild.service", NULL, &grandchild) >= 0);
69 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "parent-deep.slice", NULL, &parent_deep) >= 0);
70 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "nomem.slice", NULL, &nomem_parent) >= 0);
71 [ - + ]: 4 : assert_se(manager_load_startable_unit_or_warn(m, "nomemleaf.service", NULL, &nomem_leaf) >= 0);
72 [ - + ]: 4 : assert_se(UNIT_DEREF(son->slice) == parent);
73 [ - + ]: 4 : assert_se(UNIT_DEREF(daughter->slice) == parent);
74 [ - + ]: 4 : assert_se(UNIT_DEREF(parent_deep->slice) == parent);
75 [ - + ]: 4 : assert_se(UNIT_DEREF(grandchild->slice) == parent_deep);
76 [ - + ]: 4 : assert_se(UNIT_DEREF(nomem_leaf->slice) == nomem_parent);
77 : 4 : root = UNIT_DEREF(parent->slice);
78 [ - + ]: 4 : assert_se(UNIT_DEREF(nomem_parent->slice) == root);
79 : :
80 : : /* Verify per-unit cgroups settings. */
81 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(son), CGROUP_MASK_CPU);
82 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(daughter), cpu_accounting_mask);
83 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(grandchild), 0);
84 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(parent_deep), CGROUP_MASK_MEMORY);
85 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(parent), (CGROUP_MASK_IO | CGROUP_MASK_BLKIO));
86 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(nomem_parent), 0);
87 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(nomem_leaf), (CGROUP_MASK_IO | CGROUP_MASK_BLKIO));
88 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(root), 0);
89 : :
90 : : /* Verify aggregation of member masks */
91 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(son), 0);
92 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(daughter), 0);
93 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(grandchild), 0);
94 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(parent_deep), 0);
95 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(parent), (CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_MEMORY));
96 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(nomem_parent), (CGROUP_MASK_IO | CGROUP_MASK_BLKIO));
97 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(nomem_leaf), 0);
98 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(root), (CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
99 : :
100 : : /* Verify aggregation of sibling masks. */
101 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(son), (CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_MEMORY));
102 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(daughter), (CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_MEMORY));
103 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(grandchild), 0);
104 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(parent_deep), (CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_MEMORY));
105 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(parent), (CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
106 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(nomem_parent), (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
107 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(nomem_leaf), (CGROUP_MASK_IO | CGROUP_MASK_BLKIO));
108 [ - + ]: 4 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(root), (CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
109 : :
110 : : /* Verify aggregation of target masks. */
111 [ - + ]: 4 : ASSERT_CGROUP_MASK(unit_get_target_mask(son), (CGROUP_MASK_EXTEND_JOINED(CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_MEMORY) & m->cgroup_supported));
112 [ - + ]: 4 : ASSERT_CGROUP_MASK(unit_get_target_mask(daughter), (CGROUP_MASK_EXTEND_JOINED(CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_MEMORY) & m->cgroup_supported));
113 [ - + ]: 4 : ASSERT_CGROUP_MASK(unit_get_target_mask(grandchild), 0);
114 [ - + ]: 4 : ASSERT_CGROUP_MASK(unit_get_target_mask(parent_deep), (CGROUP_MASK_EXTEND_JOINED(CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_MEMORY) & m->cgroup_supported));
115 [ - + ]: 4 : ASSERT_CGROUP_MASK(unit_get_target_mask(parent), (CGROUP_MASK_EXTEND_JOINED(CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY) & m->cgroup_supported));
116 [ - + ]: 4 : ASSERT_CGROUP_MASK(unit_get_target_mask(nomem_parent), (CGROUP_MASK_EXTEND_JOINED(CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_IO | CGROUP_MASK_BLKIO) & m->cgroup_supported));
117 [ - + ]: 4 : ASSERT_CGROUP_MASK(unit_get_target_mask(nomem_leaf), (CGROUP_MASK_EXTEND_JOINED(CGROUP_MASK_IO | CGROUP_MASK_BLKIO) & m->cgroup_supported));
118 [ - + ]: 4 : ASSERT_CGROUP_MASK(unit_get_target_mask(root), (CGROUP_MASK_EXTEND_JOINED(CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_IO | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY) & m->cgroup_supported));
119 : :
120 : 4 : return 0;
121 : : }
122 : :
123 : 56 : static void test_cg_mask_to_string_one(CGroupMask mask, const char *t) {
124 : 56 : _cleanup_free_ char *b = NULL;
125 : :
126 [ - + ]: 56 : assert_se(cg_mask_to_string(mask, &b) >= 0);
127 [ - + ]: 56 : assert_se(streq_ptr(b, t));
128 : 56 : }
129 : :
130 : 4 : static void test_cg_mask_to_string(void) {
131 : 4 : test_cg_mask_to_string_one(0, NULL);
132 : 4 : test_cg_mask_to_string_one(_CGROUP_MASK_ALL, "cpu cpuacct io blkio memory devices pids bpf-firewall bpf-devices");
133 : 4 : test_cg_mask_to_string_one(CGROUP_MASK_CPU, "cpu");
134 : 4 : test_cg_mask_to_string_one(CGROUP_MASK_CPUACCT, "cpuacct");
135 : 4 : test_cg_mask_to_string_one(CGROUP_MASK_IO, "io");
136 : 4 : test_cg_mask_to_string_one(CGROUP_MASK_BLKIO, "blkio");
137 : 4 : test_cg_mask_to_string_one(CGROUP_MASK_MEMORY, "memory");
138 : 4 : test_cg_mask_to_string_one(CGROUP_MASK_DEVICES, "devices");
139 : 4 : test_cg_mask_to_string_one(CGROUP_MASK_PIDS, "pids");
140 : 4 : test_cg_mask_to_string_one(CGROUP_MASK_CPU|CGROUP_MASK_CPUACCT, "cpu cpuacct");
141 : 4 : test_cg_mask_to_string_one(CGROUP_MASK_CPU|CGROUP_MASK_PIDS, "cpu pids");
142 : 4 : test_cg_mask_to_string_one(CGROUP_MASK_CPUACCT|CGROUP_MASK_PIDS, "cpuacct pids");
143 : 4 : test_cg_mask_to_string_one(CGROUP_MASK_DEVICES|CGROUP_MASK_PIDS, "devices pids");
144 : 4 : test_cg_mask_to_string_one(CGROUP_MASK_IO|CGROUP_MASK_BLKIO, "io blkio");
145 : 4 : }
146 : :
147 : 4 : int main(int argc, char* argv[]) {
148 : 4 : int rc = EXIT_SUCCESS;
149 : :
150 : 4 : test_setup_logging(LOG_DEBUG);
151 : :
152 : 4 : test_cg_mask_to_string();
153 [ + - ]: 4 : TEST_REQ_RUNNING_SYSTEMD(rc = test_cgroup_mask());
154 : :
155 : 4 : return rc;
156 : : }
|