Skip to content

Commit

Permalink
pr-525 - [cgroups2] Introduces cgroups2::exists to check if a cgroup …
Browse files Browse the repository at this point in the history
…exists.
  • Loading branch information
andreaspeters committed Mar 26, 2024
2 parents 2d283e0 + c52815e commit d6dbccf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/linux/cgroups2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ Try<Nothing> unmount()
}


bool exists(const string& cgroup)
{
return os::exists(path::join(MOUNT_POINT, cgroup));
}

Try<Nothing> create(const string& cgroup, bool recursive)
{
const string absolutePath = path::join(MOUNT_POINT, cgroup);
Expand All @@ -259,12 +264,11 @@ Try<Nothing> create(const string& cgroup, bool recursive)

Try<Nothing> destroy(const string& cgroup)
{
const string absolutePath = path::join(MOUNT_POINT, cgroup);

if (!os::exists(absolutePath)) {
return Error("There does not exist a cgroup at '" + absolutePath + "'");
if (!cgroups2::exists(cgroup)) {
return Error("Cgroup '" + cgroup + "' does not exist");
}

const string absolutePath = path::join(MOUNT_POINT, cgroup);
Try<Nothing> rmdir = os::rmdir(absolutePath, false);
if (rmdir.isError()) {
return Error("Failed to remove directory '" + absolutePath + "': "
Expand Down
3 changes: 3 additions & 0 deletions src/linux/cgroups2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Try<bool> mounted();
Try<Nothing> unmount();


// Check if a cgroup exists.
bool exists(const std::string& cgroup);

// Creates a cgroup off of the base hierarchy, i.e. /sys/fs/cgroup/<cgroup>.
// cgroup can be a nested cgroup (e.g. foo/bar/baz). If cgroup is a nested
// cgroup and the parent cgroups do not exist, an error will be returned unless
Expand Down

0 comments on commit d6dbccf

Please sign in to comment.