summaryrefslogtreecommitdiffstats
path: root/src/test/test-cgroup.c
blob: 8bd4af94e4e6f0e73dfd1ae40374ef68a6f9f6ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <unistd.h>

#include "cgroup-setup.h"
#include "cgroup-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "path-util.h"
#include "process-util.h"
#include "string-util.h"
#include "tests.h"

TEST(cg_split_spec) {
        char *c, *p;

        assert_se(cg_split_spec("foobar:/", &c, &p) == 0);
        ASSERT_STREQ(c, "foobar");
        ASSERT_STREQ(p, "/");
        c = mfree(c);
        p = mfree(p);

        assert_se(cg_split_spec("foobar:", &c, &p) == 0);
        c = mfree(c);
        p = mfree(p);

        assert_se(cg_split_spec("foobar:asdfd", &c, &p) < 0);
        assert_se(cg_split_spec(":///", &c, &p) < 0);
        assert_se(cg_split_spec(":", &c, &p) < 0);
        assert_se(cg_split_spec("", &c, &p) < 0);
        assert_se(cg_split_spec("fo/obar:/", &c, &p) < 0);

        assert_se(cg_split_spec("/", &c, &p) >= 0);
        ASSERT_NULL(c);
        ASSERT_STREQ(p, "/");
        p = mfree(p);

        assert_se(cg_split_spec("foo", &c, &p) >= 0);
        ASSERT_STREQ(c, "foo");
        ASSERT_NULL(p);
        c = mfree(c);
}

TEST(cg_create) {
        int r;

        r = cg_unified_cached(false);
        if (IN_SET(r, -ENOMEDIUM, -ENOENT)) {
                log_tests_skipped("cgroupfs is not mounted");
                return;
        }
        assert_se(r >= 0);

        _cleanup_free_ char *here = NULL;
        assert_se(cg_pid_get_path_shifted(0, NULL, &here) >= 0);

        const char *test_a = prefix_roota(here, "/test-a"),
                   *test_b = prefix_roota(here, "/test-b"),
                   *test_c = prefix_roota(here, "/test-b/test-c"),
                   *test_d = prefix_roota(here, "/test-b/test-d");
        char *path;

        log_info("Paths for test:\n%s\n%s", test_a, test_b);

        /* Possibly clean up left-overs from aboted previous runs */
        (void) cg_trim(SYSTEMD_CGROUP_CONTROLLER, test_a, /* delete_root= */ true);
        (void) cg_trim(SYSTEMD_CGROUP_CONTROLLER, test_b, /* delete_root= */ true);

        r = cg_create(SYSTEMD_CGROUP_CONTROLLER, test_a);
        if (IN_SET(r, -EPERM, -EACCES, -EROFS)) {
                log_info_errno(r, "Skipping %s: %m", __func__);
                return;
        }

        assert_se(r == 1);
        assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, test_a) == 0);
        assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, test_b) == 1);
        assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, test_c) == 1);
        assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, test_b, 0) == 0);

        assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path) == 0);
        ASSERT_STREQ(path, test_b);
        free(path);

        assert_se(cg_attach(SYSTEMD_CGROUP_CONTROLLER, test_a, 0) == 0);

        assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path) == 0);
        assert_se(path_equal(path, test_a));
        free(path);

        assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, test_d, 0) == 1);

        assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path) == 0);
        assert_se(path_equal(path, test_d));
        free(path);

        assert_se(cg_get_path(SYSTEMD_CGROUP_CONTROLLER, test_d, NULL, &path) == 0);
        log_debug("test_d: %s", path);
        const char *full_d;
        if (cg_all_unified())
                full_d = strjoina("/sys/fs/cgroup", test_d);
        else if (cg_hybrid_unified())
                full_d = strjoina("/sys/fs/cgroup/unified", test_d);
        else
                full_d = strjoina("/sys/fs/cgroup/systemd", test_d);
        assert_se(path_equal(path, full_d));
        free(path);

        assert_se(cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, test_a) > 0);
        assert_se(cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, test_b) > 0);
        assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a) > 0);
        assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b) == 0);

        assert_se(cg_kill_recursive(test_a, 0, 0, NULL, NULL, NULL) == 0);
        assert_se(cg_kill_recursive(test_b, 0, 0, NULL, NULL, NULL) > 0);

        assert_se(cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b, SYSTEMD_CGROUP_CONTROLLER, test_a, 0) > 0);

        assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a) == 0);
        assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b) > 0);

        assert_se(cg_kill_recursive(test_a, 0, 0, NULL, NULL, NULL) > 0);
        assert_se(cg_kill_recursive(test_b, 0, 0, NULL, NULL, NULL) == 0);

        (void) cg_trim(SYSTEMD_CGROUP_CONTROLLER, test_b, false);

        assert_se(cg_rmdir(SYSTEMD_CGROUP_CONTROLLER, test_b) == 0);
        assert_se(cg_rmdir(SYSTEMD_CGROUP_CONTROLLER, test_a) < 0);
        assert_se(cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a, SYSTEMD_CGROUP_CONTROLLER, here, 0) > 0);
        assert_se(cg_rmdir(SYSTEMD_CGROUP_CONTROLLER, test_a) == 0);
}

TEST(id) {
        _cleanup_free_ char *p = NULL, *p2 = NULL;
        _cleanup_close_ int fd = -EBADF, fd2 = -EBADF;
        uint64_t id, id2;
        int r;

        r = cg_all_unified();
        if (r == 0) {
                log_tests_skipped("skipping cgroupid test, not running in unified mode");
                return;
        }
        if (IN_SET(r, -ENOMEDIUM, -ENOENT)) {
                log_tests_skipped("cgroupfs is not mounted");
                return;
        }
        assert_se(r > 0);

        fd = cg_path_open(SYSTEMD_CGROUP_CONTROLLER, "/");
        assert_se(fd >= 0);

        assert_se(fd_get_path(fd, &p) >= 0);
        assert_se(path_equal(p, "/sys/fs/cgroup"));

        assert_se(cg_fd_get_cgroupid(fd, &id) >= 0);

        fd2 = cg_cgroupid_open(fd, id);

        if (ERRNO_IS_NEG_PRIVILEGE(fd2))
                log_notice("Skipping open-by-cgroup-id test because lacking privs.");
        else {
                assert_se(fd2 >= 0);

                assert_se(fd_get_path(fd2, &p2) >= 0);
                assert_se(path_equal(p2, "/sys/fs/cgroup"));

                assert_se(cg_fd_get_cgroupid(fd2, &id2) >= 0);

                assert_se(id == id2);

                assert_se(inode_same_at(fd, NULL, fd2, NULL, AT_EMPTY_PATH) > 0);
        }
}

DEFINE_TEST_MAIN(LOG_DEBUG);