vircgroup: introduce virCgroupV1Exists and virCgroupV2Exists

This will check if the cgroup actually exists on the system.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Pavel Hrdina 2021-02-09 12:31:13 +01:00
parent 382fa15cde
commit badc2bcc73
3 changed files with 46 additions and 0 deletions

View File

@ -118,6 +118,9 @@ typedef int
const char *key,
char **path);
typedef bool
(*virCgroupExistsCB)(virCgroupPtr group);
typedef int
(*virCgroupMakeGroupCB)(virCgroupPtr parent,
virCgroupPtr group,
@ -382,6 +385,7 @@ struct _virCgroupBackend {
virCgroupGetAnyControllerCB getAnyController;
virCgroupPathOfControllerCB pathOfController;
virCgroupMakeGroupCB makeGroup;
virCgroupExistsCB exists;
virCgroupRemoveCB remove;
virCgroupAddTaskCB addTask;
virCgroupHasEmptyTasksCB hasEmptyTasks;

View File

@ -690,6 +690,32 @@ virCgroupV1MakeGroup(virCgroupPtr parent,
}
static bool
virCgroupV1Exists(virCgroupPtr group)
{
size_t i;
for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
g_autofree char *path = NULL;
if (i == VIR_CGROUP_CONTROLLER_SYSTEMD)
continue;
if (!group->legacy[i].mountPoint)
continue;
if (virCgroupV1PathOfController(group, i, "", &path) < 0)
return false;
if (!virFileExists(path)) {
return false;
}
}
return true;
}
static int
virCgroupV1Remove(virCgroupPtr group)
{
@ -2154,6 +2180,7 @@ virCgroupBackend virCgroupV1Backend = {
.getAnyController = virCgroupV1GetAnyController,
.pathOfController = virCgroupV1PathOfController,
.makeGroup = virCgroupV1MakeGroup,
.exists = virCgroupV1Exists,
.remove = virCgroupV1Remove,
.addTask = virCgroupV1AddTask,
.hasEmptyTasks = virCgroupV1HasEmptyTasks,

View File

@ -499,6 +499,20 @@ virCgroupV2MakeGroup(virCgroupPtr parent,
}
static bool
virCgroupV2Exists(virCgroupPtr group)
{
g_autofree char *path = NULL;
int controller;
controller = virCgroupV2GetAnyController(group);
if (virCgroupV2PathOfController(group, controller, "", &path) < 0)
return false;
return virFileExists(path);
}
static int
virCgroupV2Remove(virCgroupPtr group)
{
@ -1895,6 +1909,7 @@ virCgroupBackend virCgroupV2Backend = {
.getAnyController = virCgroupV2GetAnyController,
.pathOfController = virCgroupV2PathOfController,
.makeGroup = virCgroupV2MakeGroup,
.exists = virCgroupV2Exists,
.remove = virCgroupV2Remove,
.addTask = virCgroupV2AddTask,
.hasEmptyTasks = virCgroupV2HasEmptyTasks,