]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/cgroups/cgroup_utils.c
2 * lxc: linux Container library
4 * Copyright © 2017 Canonical Ltd.
7 * Serge Hallyn <serge.hallyn@ubuntu.com>
8 * Christian Brauner <christian.brauner@ubuntu.com>
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.
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.
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
33 #include "cgroup_utils.h"
36 #include "memory_utils.h"
39 int get_cgroup_version(char *line
)
41 if (is_cgroupfs_v1(line
))
42 return CGROUP_SUPER_MAGIC
;
44 if (is_cgroupfs_v2(line
))
45 return CGROUP2_SUPER_MAGIC
;
50 bool is_cgroupfs_v1(char *line
)
52 char *p
= strstr(line
, " - ");
55 return strncmp(p
, " - cgroup ", 10) == 0;
58 bool is_cgroupfs_v2(char *line
)
60 char *p
= strstr(line
, " - ");
64 return strncmp(p
, " - cgroup2 ", 11) == 0;
67 bool test_writeable_v1(char *mountpoint
, char *path
)
69 __do_free
char *fullpath
= must_make_path(mountpoint
, path
, NULL
);
70 return (access(fullpath
, W_OK
) == 0);
73 bool test_writeable_v2(char *mountpoint
, char *path
)
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"
81 __do_free
char *cgroup_path
= NULL
, *cgroup_procs_file
= NULL
,
82 *cgroup_threads_file
= NULL
;
84 cgroup_path
= must_make_path(mountpoint
, path
, NULL
);
85 cgroup_procs_file
= must_make_path(cgroup_path
, "cgroup.procs", NULL
);
87 ret
= access(cgroup_path
, W_OK
);
91 ret
= access(cgroup_procs_file
, W_OK
);
95 /* Newer versions of cgroup2 now also require write access to the
96 * "cgroup.threads" file.
98 cgroup_threads_file
= must_make_path(cgroup_path
, "cgroup.threads", NULL
);
99 if (!file_exists(cgroup_threads_file
))
102 return (access(cgroup_threads_file
, W_OK
) == 0);