From: Tom Hromatka Date: Mon, 8 Feb 2021 23:05:24 +0000 (-0700) Subject: wrapper.c: Add support for empty values in cgroup_add_value_string() X-Git-Tag: v2.0.rc1~10^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e7ac3fd8f1eaf515e70b67effd2af9b372e86e0b;p=thirdparty%2Flibcgroup.git wrapper.c: Add support for empty values in cgroup_add_value_string() 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 --- diff --git a/src/wrapper.c b/src/wrapper.c index b7932545..35f5dbd8 100644 --- a/src/wrapper.c +++ b/src/wrapper.c @@ -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++;