]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-helper.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-helper.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2017 Lennart Poettering
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 "test-helper.h"
22 #include "random-util.h"
23 #include "alloc-util.h"
24 #include "cgroup-util.h"
25
26 int enter_cgroup_subroot(void) {
27 _cleanup_free_ char *cgroup_root = NULL, *cgroup_subroot = NULL;
28 CGroupMask supported;
29 int r;
30
31 r = cg_pid_get_path(NULL, 0, &cgroup_root);
32 if (r == -ENOMEDIUM)
33 return log_warning_errno(r, "cg_pid_get_path(NULL, 0, ...) failed: %m");
34 assert(r >= 0);
35
36 assert_se(asprintf(&cgroup_subroot, "%s/%" PRIx64, cgroup_root, random_u64()) >= 0);
37 assert_se(cg_mask_supported(&supported) >= 0);
38
39 /* If this fails, then we don't mind as the later cgroup operations will fail too, and it's fine if we handle
40 * any errors at that point. */
41
42 r = cg_create_everywhere(supported, _CGROUP_MASK_ALL, cgroup_subroot);
43 if (r < 0)
44 return r;
45
46 return cg_attach_everywhere(supported, cgroup_subroot, 0, NULL, NULL);
47 }