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 32 : static void log_cgroup_mask(CGroupMask got, CGroupMask expected) {
22 32 : _cleanup_free_ char *e_store = NULL, *g_store = NULL;
23 :
24 32 : assert_se(cg_mask_to_string(expected, &e_store) >= 0);
25 32 : log_info("Expected mask: %s\n", e_store);
26 32 : assert_se(cg_mask_to_string(got, &g_store) >= 0);
27 32 : log_info("Got mask: %s\n", g_store);
28 32 : }
29 :
30 1 : static int test_cgroup_mask(void) {
31 1 : _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
32 1 : _cleanup_(manager_freep) Manager *m = NULL;
33 : Unit *son, *daughter, *parent, *root, *grandchild, *parent_deep, *nomem_parent, *nomem_leaf;
34 : int r;
35 1 : CGroupMask cpu_accounting_mask = get_cpu_accounting_mask();
36 :
37 1 : r = enter_cgroup_subroot();
38 1 : if (r == -ENOMEDIUM)
39 0 : return log_tests_skipped("cgroupfs not available");
40 :
41 : /* Prepare the manager. */
42 1 : assert_se(set_unit_path(get_testdata_dir()) >= 0);
43 1 : assert_se(runtime_dir = setup_fake_runtime_dir());
44 1 : r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &m);
45 1 : 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 1 : 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 2 : m->default_cpu_accounting =
56 3 : m->default_memory_accounting =
57 3 : m->default_blockio_accounting =
58 3 : m->default_io_accounting =
59 2 : m->default_tasks_accounting = false;
60 1 : m->default_tasks_max = (uint64_t) -1;
61 :
62 1 : assert_se(manager_startup(m, NULL, NULL) >= 0);
63 :
64 : /* Load units and verify hierarchy. */
65 1 : assert_se(manager_load_startable_unit_or_warn(m, "parent.slice", NULL, &parent) >= 0);
66 1 : assert_se(manager_load_startable_unit_or_warn(m, "son.service", NULL, &son) >= 0);
67 1 : assert_se(manager_load_startable_unit_or_warn(m, "daughter.service", NULL, &daughter) >= 0);
68 1 : assert_se(manager_load_startable_unit_or_warn(m, "grandchild.service", NULL, &grandchild) >= 0);
69 1 : assert_se(manager_load_startable_unit_or_warn(m, "parent-deep.slice", NULL, &parent_deep) >= 0);
70 1 : assert_se(manager_load_startable_unit_or_warn(m, "nomem.slice", NULL, &nomem_parent) >= 0);
71 1 : assert_se(manager_load_startable_unit_or_warn(m, "nomemleaf.service", NULL, &nomem_leaf) >= 0);
72 1 : assert_se(UNIT_DEREF(son->slice) == parent);
73 1 : assert_se(UNIT_DEREF(daughter->slice) == parent);
74 1 : assert_se(UNIT_DEREF(parent_deep->slice) == parent);
75 1 : assert_se(UNIT_DEREF(grandchild->slice) == parent_deep);
76 1 : assert_se(UNIT_DEREF(nomem_leaf->slice) == nomem_parent);
77 1 : root = UNIT_DEREF(parent->slice);
78 1 : assert_se(UNIT_DEREF(nomem_parent->slice) == root);
79 :
80 : /* Verify per-unit cgroups settings. */
81 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(son), CGROUP_MASK_CPU);
82 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(daughter), cpu_accounting_mask);
83 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(grandchild), 0);
84 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(parent_deep), CGROUP_MASK_MEMORY);
85 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(parent), (CGROUP_MASK_IO | CGROUP_MASK_BLKIO));
86 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(nomem_parent), 0);
87 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(nomem_leaf), (CGROUP_MASK_IO | CGROUP_MASK_BLKIO));
88 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_own_mask(root), 0);
89 :
90 : /* Verify aggregation of member masks */
91 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(son), 0);
92 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(daughter), 0);
93 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(grandchild), 0);
94 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(parent_deep), 0);
95 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(parent), (CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_MEMORY));
96 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(nomem_parent), (CGROUP_MASK_IO | CGROUP_MASK_BLKIO));
97 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_members_mask(nomem_leaf), 0);
98 1 : 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 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(son), (CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_MEMORY));
102 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(daughter), (CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_MEMORY));
103 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(grandchild), 0);
104 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(parent_deep), (CGROUP_MASK_CPU | cpu_accounting_mask | CGROUP_MASK_MEMORY));
105 1 : 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 1 : 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 1 : ASSERT_CGROUP_MASK_JOINED(unit_get_siblings_mask(nomem_leaf), (CGROUP_MASK_IO | CGROUP_MASK_BLKIO));
108 1 : 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 1 : 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 1 : 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 1 : ASSERT_CGROUP_MASK(unit_get_target_mask(grandchild), 0);
114 1 : 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 1 : 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 1 : 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 1 : ASSERT_CGROUP_MASK(unit_get_target_mask(nomem_leaf), (CGROUP_MASK_EXTEND_JOINED(CGROUP_MASK_IO | CGROUP_MASK_BLKIO) & m->cgroup_supported));
118 1 : 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 1 : return 0;
121 : }
122 :
123 14 : static void test_cg_mask_to_string_one(CGroupMask mask, const char *t) {
124 14 : _cleanup_free_ char *b = NULL;
125 :
126 14 : assert_se(cg_mask_to_string(mask, &b) >= 0);
127 14 : assert_se(streq_ptr(b, t));
128 14 : }
129 :
130 1 : static void test_cg_mask_to_string(void) {
131 1 : test_cg_mask_to_string_one(0, NULL);
132 1 : test_cg_mask_to_string_one(_CGROUP_MASK_ALL, "cpu cpuacct io blkio memory devices pids bpf-firewall bpf-devices");
133 1 : test_cg_mask_to_string_one(CGROUP_MASK_CPU, "cpu");
134 1 : test_cg_mask_to_string_one(CGROUP_MASK_CPUACCT, "cpuacct");
135 1 : test_cg_mask_to_string_one(CGROUP_MASK_IO, "io");
136 1 : test_cg_mask_to_string_one(CGROUP_MASK_BLKIO, "blkio");
137 1 : test_cg_mask_to_string_one(CGROUP_MASK_MEMORY, "memory");
138 1 : test_cg_mask_to_string_one(CGROUP_MASK_DEVICES, "devices");
139 1 : test_cg_mask_to_string_one(CGROUP_MASK_PIDS, "pids");
140 1 : test_cg_mask_to_string_one(CGROUP_MASK_CPU|CGROUP_MASK_CPUACCT, "cpu cpuacct");
141 1 : test_cg_mask_to_string_one(CGROUP_MASK_CPU|CGROUP_MASK_PIDS, "cpu pids");
142 1 : test_cg_mask_to_string_one(CGROUP_MASK_CPUACCT|CGROUP_MASK_PIDS, "cpuacct pids");
143 1 : test_cg_mask_to_string_one(CGROUP_MASK_DEVICES|CGROUP_MASK_PIDS, "devices pids");
144 1 : test_cg_mask_to_string_one(CGROUP_MASK_IO|CGROUP_MASK_BLKIO, "io blkio");
145 1 : }
146 :
147 1 : int main(int argc, char* argv[]) {
148 1 : int rc = EXIT_SUCCESS;
149 :
150 1 : test_setup_logging(LOG_DEBUG);
151 :
152 1 : test_cg_mask_to_string();
153 1 : TEST_REQ_RUNNING_SYSTEMD(rc = test_cgroup_mask());
154 :
155 1 : return rc;
156 : }
|