]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/cgroups/cgroup_utils.c
cgroups: simplify cgfsng_setup_limits()
[mirror_lxc.git] / src / lxc / cgroups / cgroup_utils.c
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
25 #ifndef _GNU_SOURCE
26 #define _GNU_SOURCE 1
27 #endif
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include "cgroup_utils.h"
34 #include "config.h"
35 #include "macro.h"
36 #include "memory_utils.h"
37 #include "utils.h"
38
39 int get_cgroup_version(char *line)
40 {
41 if (is_cgroupfs_v1(line))
42 return CGROUP_SUPER_MAGIC;
43
44 if (is_cgroupfs_v2(line))
45 return CGROUP2_SUPER_MAGIC;
46
47 return 0;
48 }
49
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 {
69 __do_free char *fullpath = must_make_path(mountpoint, path, NULL);
70 return (access(fullpath, W_OK) == 0);
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;
81 __do_free char *cgroup_path = NULL, *cgroup_procs_file = NULL,
82 *cgroup_threads_file = NULL;
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);
88 if (ret < 0)
89 return false;
90
91 ret = access(cgroup_procs_file, W_OK);
92 if (ret < 0)
93 return false;
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);
99 if (!file_exists(cgroup_threads_file))
100 return true;
101
102 return (access(cgroup_threads_file, W_OK) == 0);
103 }