]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
wrapper.c: Add support for empty values in cgroup_add_value_string()
authorTom Hromatka <tom.hromatka@oracle.com>
Mon, 8 Feb 2021 23:05:24 +0000 (16:05 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Mon, 1 Mar 2021 17:33:00 +0000 (10:33 -0700)
cgroup_add_value_string() is the fundamental building block
for adding a name/value pair to a cgroup_controller struct.
Add support for a NULL value field.

Currently it only supports adding a valid value, but in a
subsequent commit, cgget will utilize this function to build
up a hierarchy of cgroups, controllers, and setting names.
The values are NULL at the time the struct are populated and
will be read from sysfs at a later time.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/wrapper.c

index b7932545ebfa6a6dbb42ef8e361f92f65c0853e8..35f5dbd8afa581c7122012c71d1fc94dabe7301f 100644 (file)
@@ -195,18 +195,22 @@ int cgroup_add_value_string(struct cgroup_controller *controller,
        if (!cntl_value)
                return ECGCONTROLLERCREATEFAILED;
 
-       if (strlen(value) >= sizeof(cntl_value->value)) {
-               fprintf(stderr, "value exceeds the maximum of %d characters\n",
-                       sizeof(cntl_value->value) - 1);
-               free(cntl_value);
-               return ECGCONFIGPARSEFAIL;
-       }
-
        strncpy(cntl_value->name, name, sizeof(cntl_value->name));
        cntl_value->name[sizeof(cntl_value->name)-1] = '\0';
-       strncpy(cntl_value->value, value, sizeof(cntl_value->value));
-       cntl_value->value[sizeof(cntl_value->value)-1] = '\0';
-       cntl_value->dirty = true;
+
+       if (value) {
+               if (strlen(value) >= sizeof(cntl_value->value)) {
+                       fprintf(stderr, "value exceeds the maximum of %d characters\n",
+                               sizeof(cntl_value->value) - 1);
+                       free(cntl_value);
+                       return ECGCONFIGPARSEFAIL;
+               }
+
+               strncpy(cntl_value->value, value, sizeof(cntl_value->value));
+               cntl_value->value[sizeof(cntl_value->value)-1] = '\0';
+               cntl_value->dirty = true;
+       }
+
        controller->values[controller->index] = cntl_value;
        controller->index++;