]>
Commit | Line | Data |
---|---|---|
6328fd9c CB |
1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * Copyright © 2017 Canonical Ltd. | |
5 | * | |
6 | * Authors: | |
7 | * Serge Hallyn <serge.hallyn@ubuntu.com> | |
8 | * Christian Brauner <christian.brauner@ubuntu.com> | |
9 | * | |
10 | * This library is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU Lesser General Public | |
12 | * License as published by the Free Software Foundation; either | |
13 | * version 2.1 of the License, or (at your option) any later version. | |
14 | * | |
15 | * This library is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * Lesser General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU Lesser General Public | |
21 | * License along with this library; if not, write to the Free Software | |
22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
23 | */ | |
24 | ||
d38dd64a CB |
25 | #ifndef _GNU_SOURCE |
26 | #define _GNU_SOURCE 1 | |
27 | #endif | |
6328fd9c CB |
28 | #include <stdbool.h> |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <unistd.h> | |
32 | ||
33 | #include "cgroup_utils.h" | |
d38dd64a CB |
34 | #include "config.h" |
35 | #include "macro.h" | |
57e76250 | 36 | #include "memory_utils.h" |
6328fd9c CB |
37 | #include "utils.h" |
38 | ||
49ff3958 CB |
39 | int get_cgroup_version(char *line) |
40 | { | |
41 | if (is_cgroupfs_v1(line)) | |
d6337a5f | 42 | return CGROUP_SUPER_MAGIC; |
49ff3958 CB |
43 | |
44 | if (is_cgroupfs_v2(line)) | |
d6337a5f | 45 | return CGROUP2_SUPER_MAGIC; |
49ff3958 | 46 | |
d6337a5f | 47 | return 0; |
49ff3958 CB |
48 | } |
49 | ||
6328fd9c CB |
50 | bool is_cgroupfs_v1(char *line) |
51 | { | |
52 | char *p = strstr(line, " - "); | |
53 | if (!p) | |
54 | return false; | |
55 | return strncmp(p, " - cgroup ", 10) == 0; | |
56 | } | |
57 | ||
58 | bool is_cgroupfs_v2(char *line) | |
59 | { | |
60 | char *p = strstr(line, " - "); | |
61 | if (!p) | |
62 | return false; | |
63 | ||
64 | return strncmp(p, " - cgroup2 ", 11) == 0; | |
65 | } | |
66 | ||
67 | bool test_writeable_v1(char *mountpoint, char *path) | |
68 | { | |
57e76250 CB |
69 | __do_free char *fullpath = must_make_path(mountpoint, path, NULL); |
70 | return (access(fullpath, W_OK) == 0); | |
6328fd9c CB |
71 | } |
72 | ||
73 | bool test_writeable_v2(char *mountpoint, char *path) | |
74 | { | |
75 | /* In order to move ourselves into an appropriate sub-cgroup we need to | |
76 | * have write access to the parent cgroup's "cgroup.procs" file, i.e. we | |
77 | * need to have write access to the our current cgroups's "cgroup.procs" | |
78 | * file. | |
79 | */ | |
80 | int ret; | |
57e76250 CB |
81 | __do_free char *cgroup_path = NULL, *cgroup_procs_file = NULL, |
82 | *cgroup_threads_file = NULL; | |
6328fd9c CB |
83 | |
84 | cgroup_path = must_make_path(mountpoint, path, NULL); | |
85 | cgroup_procs_file = must_make_path(cgroup_path, "cgroup.procs", NULL); | |
86 | ||
87 | ret = access(cgroup_path, W_OK); | |
57e76250 | 88 | if (ret < 0) |
6328fd9c | 89 | return false; |
6328fd9c CB |
90 | |
91 | ret = access(cgroup_procs_file, W_OK); | |
57e76250 | 92 | if (ret < 0) |
49ff3958 | 93 | return false; |
49ff3958 CB |
94 | |
95 | /* Newer versions of cgroup2 now also require write access to the | |
96 | * "cgroup.threads" file. | |
97 | */ | |
98 | cgroup_threads_file = must_make_path(cgroup_path, "cgroup.threads", NULL); | |
57e76250 | 99 | if (!file_exists(cgroup_threads_file)) |
49ff3958 | 100 | return true; |
6328fd9c | 101 | |
57e76250 | 102 | return (access(cgroup_threads_file, W_OK) == 0); |
6328fd9c | 103 | } |