From 064024e70afd6ad08993444b3839eb3ba67426f2 Mon Sep 17 00:00:00 2001 From: Pavel Hrdina Date: Tue, 25 Sep 2018 01:14:36 +0200 Subject: [PATCH] vircgroup: extract virCgroupV1AddTask MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Fabiano Fidêncio Reviewed-by: Ján Tomko Signed-off-by: Pavel Hrdina --- src/util/vircgroup.c | 56 ++++--------------------------------- src/util/vircgroupbackend.h | 20 +++++++++++++ src/util/vircgrouppriv.h | 5 ++++ src/util/vircgroupv1.c | 31 ++++++++++++++++++++ 4 files changed, 62 insertions(+), 50 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index fe90b6038e..418becf21b 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -540,7 +540,7 @@ virCgroupSetValueU64(virCgroupPtr group, } -static int +int virCgroupSetValueI64(virCgroupPtr group, int controller, const char *key, @@ -668,50 +668,6 @@ virCgroupNew(pid_t pid, } -typedef enum { - /* Adds a whole process with all threads to specific cgroup except - * to systemd named controller. */ - VIR_CGROUP_TASK_PROCESS = 1 << 0, - - /* Same as VIR_CGROUP_TASK_PROCESS but it also adds the task to systemd - * named controller. */ - VIR_CGROUP_TASK_SYSTEMD = 1 << 1, - - /* Moves only specific thread into cgroup except to systemd - * named controller. */ - VIR_CGROUP_TASK_THREAD = 1 << 2, -} virCgroupTaskFlags; - - -static int -virCgroupAddTaskInternal(virCgroupPtr group, - pid_t pid, - unsigned int flags) -{ - int ret = -1; - size_t i; - - for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { - /* Skip over controllers not mounted */ - if (!group->controllers[i].mountPoint) - continue; - - /* We must never add tasks in systemd's hierarchy - * unless we're intentionally trying to move a - * task into a systemd machine scope */ - if (i == VIR_CGROUP_CONTROLLER_SYSTEMD && - !(flags & VIR_CGROUP_TASK_SYSTEMD)) - continue; - - if (virCgroupSetValueI64(group, i, "tasks", pid) < 0) - goto cleanup; - } - - ret = 0; - cleanup: - return ret; -} - /** * virCgroupAddProcess: * @@ -726,7 +682,7 @@ virCgroupAddTaskInternal(virCgroupPtr group, int virCgroupAddProcess(virCgroupPtr group, pid_t pid) { - return virCgroupAddTaskInternal(group, pid, VIR_CGROUP_TASK_PROCESS); + return group->backend->addTask(group, pid, VIR_CGROUP_TASK_PROCESS); } /** @@ -743,9 +699,9 @@ virCgroupAddProcess(virCgroupPtr group, pid_t pid) int virCgroupAddMachineProcess(virCgroupPtr group, pid_t pid) { - return virCgroupAddTaskInternal(group, pid, - VIR_CGROUP_TASK_PROCESS | - VIR_CGROUP_TASK_SYSTEMD); + return group->backend->addTask(group, pid, + VIR_CGROUP_TASK_PROCESS | + VIR_CGROUP_TASK_SYSTEMD); } /** @@ -763,7 +719,7 @@ int virCgroupAddThread(virCgroupPtr group, pid_t pid) { - return virCgroupAddTaskInternal(group, pid, VIR_CGROUP_TASK_THREAD); + return group->backend->addTask(group, pid, VIR_CGROUP_TASK_THREAD); } diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h index 1f28c51c49..bff1f8938e 100644 --- a/src/util/vircgroupbackend.h +++ b/src/util/vircgroupbackend.h @@ -35,6 +35,20 @@ typedef enum { */ } virCgroupBackendFlags; +typedef enum { + /* Adds a whole process with all threads to specific cgroup except + * to systemd named controller. */ + VIR_CGROUP_TASK_PROCESS = 1 << 0, + + /* Same as VIR_CGROUP_TASK_PROCESS but it also adds the task to systemd + * named controller. */ + VIR_CGROUP_TASK_SYSTEMD = 1 << 1, + + /* Moves only specific thread into cgroup except to systemd + * named controller. */ + VIR_CGROUP_TASK_THREAD = 1 << 2, +} virCgroupBackendTaskFlags; + typedef enum { VIR_CGROUP_BACKEND_TYPE_V1 = 0, VIR_CGROUP_BACKEND_TYPE_LAST, @@ -103,6 +117,11 @@ typedef int typedef int (*virCgroupRemoveCB)(virCgroupPtr group); +typedef int +(*virCgroupAddTaskCB)(virCgroupPtr group, + pid_t pid, + unsigned int flags); + struct _virCgroupBackend { virCgroupBackendType type; @@ -121,6 +140,7 @@ struct _virCgroupBackend { virCgroupPathOfControllerCB pathOfController; virCgroupMakeGroupCB makeGroup; virCgroupRemoveCB remove; + virCgroupAddTaskCB addTask; }; typedef struct _virCgroupBackend virCgroupBackend; typedef virCgroupBackend *virCgroupBackendPtr; diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h index a760b9bcfd..01714370be 100644 --- a/src/util/vircgrouppriv.h +++ b/src/util/vircgrouppriv.h @@ -73,6 +73,11 @@ int virCgroupGetValueU64(virCgroupPtr group, const char *key, unsigned long long int *value); +int virCgroupSetValueI64(virCgroupPtr group, + int controller, + const char *key, + long long int value); + int virCgroupPartitionEscape(char **path); int virCgroupNewPartition(const char *path, diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c index 90d39e7fbe..8545d28ff1 100644 --- a/src/util/vircgroupv1.c +++ b/src/util/vircgroupv1.c @@ -705,6 +705,36 @@ virCgroupV1Remove(virCgroupPtr group) } +static int +virCgroupV1AddTask(virCgroupPtr group, + pid_t pid, + unsigned int flags) +{ + int ret = -1; + size_t i; + + for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { + /* Skip over controllers not mounted */ + if (!group->controllers[i].mountPoint) + continue; + + /* We must never add tasks in systemd's hierarchy + * unless we're intentionally trying to move a + * task into a systemd machine scope */ + if (i == VIR_CGROUP_CONTROLLER_SYSTEMD && + !(flags & VIR_CGROUP_TASK_SYSTEMD)) + continue; + + if (virCgroupSetValueI64(group, i, "tasks", pid) < 0) + goto cleanup; + } + + ret = 0; + cleanup: + return ret; +} + + virCgroupBackend virCgroupV1Backend = { .type = VIR_CGROUP_BACKEND_TYPE_V1, @@ -722,6 +752,7 @@ virCgroupBackend virCgroupV1Backend = { .pathOfController = virCgroupV1PathOfController, .makeGroup = virCgroupV1MakeGroup, .remove = virCgroupV1Remove, + .addTask = virCgroupV1AddTask, }; -- GitLab