]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
wrapper: Add function to remove name/value pair from a controller
authorTom Hromatka <tom.hromatka@oracle.com>
Wed, 15 Dec 2021 20:39:52 +0000 (20:39 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 3 Feb 2022 21:42:34 +0000 (14:42 -0700)
Add a function to remove a name/value settings pair from
a cgroup_controller instance.  As part of this change, I did a small
refactor on cgroup_free_controllers() to make the code more reusable
throughout libcgroup.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
src/libcgroup-internal.h
src/wrapper.c

index 056085bc57c8d0f2b21e62c482e2713397ec305c..a63244d1df2b21705bd03f7363f30972e48c3605 100644 (file)
@@ -378,6 +378,16 @@ int cgroup_test_subsys_mounted(const char *ctrl_name);
 int cgroup_copy_controller_values(struct cgroup_controller * const dst,
                                  const struct cgroup_controller * const src);
 
+/**
+ * Remove a name/value pair from a controller.
+ *
+ * @param controller
+ * @param name Name of the name/value pair to be removed
+ * @return 0 on success.  ECGROUPNOTEXIST if name does not exist.
+ */
+int cgroup_remove_value(struct cgroup_controller * const controller,
+                       const char * const name);
+
 /**
  * Functions that are defined as STATIC can be placed within the UNIT_TEST
  * ifdef.  This will allow them to be included in the unit tests while
index 150e6c4bbda46ea9b3fba8af43a38dcc635ecc15..77467d3519bef928521388b279631ae26d4ae665 100644 (file)
@@ -159,24 +159,36 @@ end:
        return ret;
 }
 
+static void cgroup_free_value(struct control_value *value)
+{
+       if (value->multiline_value)
+               free(value->multiline_value);
+       if (value->prev_name)
+               free(value->prev_name);
+
+       free(value);
+}
+
+static void cgroup_free_controller(struct cgroup_controller *ctrl)
+{
+       int i;
+
+       for (i = 0; i < ctrl->index; i++)
+               cgroup_free_value(ctrl->values[i]);
+       ctrl->index = 0;
+
+       free(ctrl);
+}
+
 void cgroup_free_controllers(struct cgroup *cgroup)
 {
-       int i, j;
+       int i;
 
        if (!cgroup)
                return;
 
        for (i = 0; i < cgroup->index; i++) {
-               for (j = 0; j < cgroup->controller[i]->index; j++) {
-                       if (cgroup->controller[i]->values[j]->multiline_value)
-                               free(cgroup->controller[i]->values[j]->multiline_value);
-                       if (cgroup->controller[i]->values[j]->prev_name)
-                               free(cgroup->controller[i]->values[j]->prev_name);
-
-                       free(cgroup->controller[i]->values[j]);
-               }
-               cgroup->controller[i]->index = 0;
-               free(cgroup->controller[i]);
+               cgroup_free_controller(cgroup->controller[i]);
        }
        cgroup->index = 0;
 }
@@ -297,6 +309,35 @@ int cgroup_add_value_bool(struct cgroup_controller *controller,
        return ret;
 }
 
+int cgroup_remove_value(struct cgroup_controller * const controller,
+                       const char * const name)
+{
+       int i;
+
+       for (i = 0; i < controller->index; i++) {
+               if (strcmp(controller->values[i]->name, name) == 0) {
+                       cgroup_free_value(controller->values[i]);
+
+                       if (i == (controller->index - 1)) {
+                               /* This is the last entry in the table.
+                                * There's nothing to move
+                                */
+                               controller->index--;
+                       } else {
+                               memmove(&controller->values[i],
+                                       &controller->values[i + 1],
+                                       sizeof(struct control_value *) *
+                                               (controller->index - i - 1));
+                               controller->index--;
+                       }
+
+                       return 0;
+               }
+       }
+
+       return ECGROUPNOTEXIST;
+}
+
 int cgroup_compare_controllers(struct cgroup_controller *cgca,
                                        struct cgroup_controller *cgcb)
 {