Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lxc
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
lxc
Commits
d4cff352
Unverified
Commit
d4cff352
authored
Feb 16, 2021
by
Christian Brauner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cgroups: stash host's cgroupfs file descriptor
Signed-off-by:
Christian Brauner
<
christian.brauner@ubuntu.com
>
parent
7414bc72
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
47 additions
and
40 deletions
+47
-40
cgfsng.c
src/lxc/cgroups/cgfsng.c
+32
-22
cgroup.c
src/lxc/cgroups/cgroup.c
+3
-0
cgroup.h
src/lxc/cgroups/cgroup.h
+12
-0
cgroup_utils.c
src/lxc/cgroups/cgroup_utils.c
+0
-16
cgroup_utils.h
src/lxc/cgroups/cgroup_utils.h
+0
-2
No files found.
src/lxc/cgroups/cgfsng.c
View file @
d4cff352
...
@@ -3474,18 +3474,9 @@ static char *cg_unified_get_current_cgroup(bool relative)
...
@@ -3474,18 +3474,9 @@ static char *cg_unified_get_current_cgroup(bool relative)
static
int
cg_unified_init
(
struct
cgroup_ops
*
ops
,
bool
relative
,
static
int
cg_unified_init
(
struct
cgroup_ops
*
ops
,
bool
relative
,
bool
unprivileged
)
bool
unprivileged
)
{
{
__do_close
int
cgroup_root_fd
=
-
EBADF
;
__do_free
char
*
base_cgroup
=
NULL
,
*
controllers_path
=
NULL
;
__do_free
char
*
base_cgroup
=
NULL
,
*
controllers_path
=
NULL
;
__do_free_string_list
char
**
delegatable
=
NULL
;
__do_free_string_list
char
**
delegatable
=
NULL
;
__do_free
struct
hierarchy
*
new
=
NULL
;
__do_free
struct
hierarchy
*
new
=
NULL
;
int
ret
;
ret
=
unified_cgroup_hierarchy
();
if
(
ret
==
-
ENOMEDIUM
)
return
ret_errno
(
ENOMEDIUM
);
if
(
ret
!=
CGROUP2_SUPER_MAGIC
)
return
0
;
base_cgroup
=
cg_unified_get_current_cgroup
(
relative
);
base_cgroup
=
cg_unified_get_current_cgroup
(
relative
);
if
(
!
base_cgroup
)
if
(
!
base_cgroup
)
...
@@ -3493,18 +3484,13 @@ static int cg_unified_init(struct cgroup_ops *ops, bool relative,
...
@@ -3493,18 +3484,13 @@ static int cg_unified_init(struct cgroup_ops *ops, bool relative,
if
(
!
relative
)
if
(
!
relative
)
prune_init_scope
(
base_cgroup
);
prune_init_scope
(
base_cgroup
);
cgroup_root_fd
=
openat
(
-
EBADF
,
DEFAULT_CGROUP_MOUNTPOINT
,
O_NOCTTY
|
O_CLOEXEC
|
O_NOFOLLOW
|
O_DIRECTORY
);
if
(
cgroup_root_fd
<
0
)
return
-
errno
;
/*
/*
* We assume that the cgroup we're currently in has been delegated to
* We assume that the cgroup we're currently in has been delegated to
* us and we are free to further delege all of the controllers listed
* us and we are free to further delege all of the controllers listed
* in cgroup.controllers further down the hierarchy.
* in cgroup.controllers further down the hierarchy.
*/
*/
controllers_path
=
must_make_path_relative
(
base_cgroup
,
"cgroup.controllers"
,
NULL
);
controllers_path
=
must_make_path_relative
(
base_cgroup
,
"cgroup.controllers"
,
NULL
);
delegatable
=
cg_unified_get_controllers
(
cgroup_root_fd
,
controllers_path
);
delegatable
=
cg_unified_get_controllers
(
ops
->
dfd_mnt_cgroupfs_host
,
controllers_path
);
if
(
!
delegatable
)
if
(
!
delegatable
)
delegatable
=
cg_unified_make_empty_controller
();
delegatable
=
cg_unified_make_empty_controller
();
if
(
!
delegatable
[
0
])
if
(
!
delegatable
[
0
])
...
@@ -3539,9 +3525,23 @@ static int cg_unified_init(struct cgroup_ops *ops, bool relative,
...
@@ -3539,9 +3525,23 @@ static int cg_unified_init(struct cgroup_ops *ops, bool relative,
static
int
__cgroup_init
(
struct
cgroup_ops
*
ops
,
struct
lxc_conf
*
conf
)
static
int
__cgroup_init
(
struct
cgroup_ops
*
ops
,
struct
lxc_conf
*
conf
)
{
{
__do_close
int
dfd
=
-
EBADF
;
bool
relative
=
conf
->
cgroup_meta
.
relative
;
int
ret
;
int
ret
;
const
char
*
tmp
;
const
char
*
tmp
;
bool
relative
=
conf
->
cgroup_meta
.
relative
;
if
(
ops
->
dfd_mnt_cgroupfs_host
>=
0
)
return
ret_errno
(
EINVAL
);
/*
* I don't see the need for allowing symlinks here. If users want to
* have their hierarchy available in different locations I strongly
* suggest bind-mounts.
*/
dfd
=
open_at
(
-
EBADF
,
DEFAULT_CGROUP_MOUNTPOINT
,
PROTECT_OPATH_DIRECTORY
,
PROTECT_LOOKUP_ABSOLUTE_XDEV
,
0
);
if
(
dfd
<
0
)
return
syserrno
(
-
errno
,
"Failed to open "
DEFAULT_CGROUP_MOUNTPOINT
);
tmp
=
lxc_global_config_value
(
"lxc.cgroup.use"
);
tmp
=
lxc_global_config_value
(
"lxc.cgroup.use"
);
if
(
tmp
)
{
if
(
tmp
)
{
...
@@ -3555,14 +3555,23 @@ static int __cgroup_init(struct cgroup_ops *ops, struct lxc_conf *conf)
...
@@ -3555,14 +3555,23 @@ static int __cgroup_init(struct cgroup_ops *ops, struct lxc_conf *conf)
must_append_string
(
&
ops
->
cgroup_use
,
cur
);
must_append_string
(
&
ops
->
cgroup_use
,
cur
);
}
}
ret
=
cg_unified_init
(
ops
,
relative
,
!
lxc_list_empty
(
&
conf
->
id_map
));
/*
if
(
ret
<
0
)
* Keep dfd referenced by the cleanup function and actually move the fd
return
-
1
;
* once we know the initialization succeeded. So if we fail we clean up
* the dfd.
*/
ops
->
dfd_mnt_cgroupfs_host
=
dfd
;
if
(
ret
==
CGROUP2_SUPER_MAGIC
)
if
(
unified_cgroup_fd
(
dfd
))
return
0
;
ret
=
cg_unified_init
(
ops
,
relative
,
!
lxc_list_empty
(
&
conf
->
id_map
));
else
ret
=
cg_hybrid_init
(
ops
,
relative
,
!
lxc_list_empty
(
&
conf
->
id_map
));
if
(
ret
<
0
)
return
syserrno
(
ret
,
"Failed to initialize cgroups"
);
return
cg_hybrid_init
(
ops
,
relative
,
!
lxc_list_empty
(
&
conf
->
id_map
));
/* Transfer ownership to cgroup_ops. */
move_fd
(
dfd
);
return
0
;
}
}
__cgfsng_ops
static
int
cgfsng_data_init
(
struct
cgroup_ops
*
ops
)
__cgfsng_ops
static
int
cgfsng_data_init
(
struct
cgroup_ops
*
ops
)
...
@@ -3589,6 +3598,7 @@ struct cgroup_ops *cgfsng_ops_init(struct lxc_conf *conf)
...
@@ -3589,6 +3598,7 @@ struct cgroup_ops *cgfsng_ops_init(struct lxc_conf *conf)
return
ret_set_errno
(
NULL
,
ENOMEM
);
return
ret_set_errno
(
NULL
,
ENOMEM
);
cgfsng_ops
->
cgroup_layout
=
CGROUP_LAYOUT_UNKNOWN
;
cgfsng_ops
->
cgroup_layout
=
CGROUP_LAYOUT_UNKNOWN
;
cgfsng_ops
->
dfd_mnt_cgroupfs_host
=
-
EBADF
;
if
(
__cgroup_init
(
cgfsng_ops
,
conf
))
if
(
__cgroup_init
(
cgfsng_ops
,
conf
))
return
NULL
;
return
NULL
;
...
...
src/lxc/cgroups/cgroup.c
View file @
d4cff352
...
@@ -68,6 +68,9 @@ void cgroup_exit(struct cgroup_ops *ops)
...
@@ -68,6 +68,9 @@ void cgroup_exit(struct cgroup_ops *ops)
if
(
ops
->
cgroup2_devices
)
if
(
ops
->
cgroup2_devices
)
bpf_program_free
(
ops
->
cgroup2_devices
);
bpf_program_free
(
ops
->
cgroup2_devices
);
if
(
ops
->
dfd_mnt_cgroupfs_host
>=
0
)
close
(
ops
->
dfd_mnt_cgroupfs_host
);
for
(
struct
hierarchy
**
it
=
ops
->
hierarchies
;
it
&&
*
it
;
it
++
)
{
for
(
struct
hierarchy
**
it
=
ops
->
hierarchies
;
it
&&
*
it
;
it
++
)
{
for
(
char
**
p
=
(
*
it
)
->
controllers
;
p
&&
*
p
;
p
++
)
for
(
char
**
p
=
(
*
it
)
->
controllers
;
p
&&
*
p
;
p
++
)
free
(
*
p
);
free
(
*
p
);
...
...
src/lxc/cgroups/cgroup.h
View file @
d4cff352
...
@@ -106,6 +106,18 @@ struct cgroup_ops {
...
@@ -106,6 +106,18 @@ struct cgroup_ops {
/* string constant */
/* string constant */
const
char
*
version
;
const
char
*
version
;
/*
* File descriptor for the host's cgroupfs mount. On
* CGROUP_LAYOUT_LEGACY or CGROUP_LAYOUT_HYBRID hybrid systems
* @dfd_mnt_cgroupfs_host will be a tmpfs fd and the individual
* controllers will be cgroupfs fds. On CGROUP_LAYOUT_UNIFIED it will
* be a cgroupfs fd itself.
*
* So for CGROUP_LAYOUT_LEGACY or CGROUP_LAYOUT_HYBRID we allow
* mountpoint crossing iff we cross from a tmpfs into a cgroupfs mount.
* */
int
dfd_mnt_cgroupfs_host
;
/* What controllers is the container supposed to use. */
/* What controllers is the container supposed to use. */
char
**
cgroup_use
;
char
**
cgroup_use
;
char
*
cgroup_pattern
;
char
*
cgroup_pattern
;
...
...
src/lxc/cgroups/cgroup_utils.c
View file @
d4cff352
...
@@ -83,22 +83,6 @@ bool test_writeable_v2(char *mountpoint, char *path)
...
@@ -83,22 +83,6 @@ bool test_writeable_v2(char *mountpoint, char *path)
return
(
access
(
cgroup_threads_file
,
W_OK
)
==
0
);
return
(
access
(
cgroup_threads_file
,
W_OK
)
==
0
);
}
}
int
unified_cgroup_hierarchy
(
void
)
{
int
ret
;
struct
statfs
fs
;
ret
=
statfs
(
DEFAULT_CGROUP_MOUNTPOINT
,
&
fs
);
if
(
ret
<
0
)
return
-
ENOMEDIUM
;
if
(
is_fs_type
(
&
fs
,
CGROUP2_SUPER_MAGIC
))
return
CGROUP2_SUPER_MAGIC
;
return
0
;
}
int
unified_cgroup_fd
(
int
fd
)
int
unified_cgroup_fd
(
int
fd
)
{
{
...
...
src/lxc/cgroups/cgroup_utils.h
View file @
d4cff352
...
@@ -29,8 +29,6 @@ __hidden extern bool test_writeable_v1(char *mountpoint, char *path);
...
@@ -29,8 +29,6 @@ __hidden extern bool test_writeable_v1(char *mountpoint, char *path);
*/
*/
__hidden
extern
bool
test_writeable_v2
(
char
*
mountpoint
,
char
*
path
);
__hidden
extern
bool
test_writeable_v2
(
char
*
mountpoint
,
char
*
path
);
__hidden
extern
int
unified_cgroup_hierarchy
(
void
);
__hidden
extern
int
unified_cgroup_fd
(
int
fd
);
__hidden
extern
int
unified_cgroup_fd
(
int
fd
);
static
inline
bool
cgns_supported
(
void
)
static
inline
bool
cgns_supported
(
void
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment