From 588cbff370fee235b63fb81696e96af41b7c110e Mon Sep 17 00:00:00 2001 From: Tom Hromatka Date: Fri, 28 Jan 2022 10:20:38 -0700 Subject: [PATCH] abstraction: Add cgroup_strtol() Add two new files, abstraction-common.[ch], and within them add a function, cgroup_strtol(), that can convert a string to a long. Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- src/Makefile.am | 3 +- src/abstraction-common.c | 67 ++++++++++++++++++++++++++++++++++++++++ src/abstraction-common.h | 50 ++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 src/abstraction-common.c create mode 100644 src/abstraction-common.h diff --git a/src/Makefile.am b/src/Makefile.am index 811bce3c..8a7136ae 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,7 +20,8 @@ TESTING_MAP_FILE = $(top_srcdir)/tests/gunit/libcgroup_unittest.map lib_LTLIBRARIES = libcgroup.la libcgroup_la_SOURCES = parse.h parse.y lex.l api.c config.c \ - libcgroup-internal.h libcgroup.map wrapper.c log.c + libcgroup-internal.h libcgroup.map wrapper.c log.c \ + abstraction-common.c abstraction-common.h libcgroup_la_LIBADD = -lpthread $(CODE_COVERAGE_LIBS) libcgroup_la_CFLAGS = $(CODE_COVERAGE_CFLAGS) -DSTATIC=static libcgroup_la_LDFLAGS = -Wl,--version-script,$(srcdir)/libcgroup.map \ diff --git a/src/abstraction-common.c b/src/abstraction-common.c new file mode 100644 index 00000000..04fec848 --- /dev/null +++ b/src/abstraction-common.c @@ -0,0 +1,67 @@ +/** + * Libcgroup abstraction layer + * + * Copyright (c) 2021-2022 Oracle and/or its affiliates. + * Author: Tom Hromatka + */ + +/* + * This library is free software; you can redistribute it and/or modify it + * under the terms of version 2.1 of the GNU Lesser General Public License as + * published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, see . + */ + +#include +#include + +#include +#include +#include +#include +#include + +#include "abstraction-common.h" + +int cgroup_strtol(const char * const in_str, int base, + long int * const out_value) +{ + char *endptr = NULL; + int ret = 0; + + if (out_value == NULL) { + cgroup_err("Error: Invalid parameter to %s\n", __func__); + ret = ECGINVAL; + goto out; + } + + errno = 0; + *out_value = strtol(in_str, &endptr, base); + + /* taken directly from strtol's man page */ + if ((errno == ERANGE && + (*out_value == LONG_MAX || *out_value == LONG_MIN)) + || (errno != 0 && *out_value == 0)) { + cgroup_err("Error: Failed to convert %s from strtol: %s\n", + in_str); + ret = ECGFAIL; + goto out; + } + + if (endptr == in_str) { + cgroup_err("Error: No long value found in %s\n", + in_str); + ret = ECGFAIL; + goto out; + } + +out: + return ret; +} diff --git a/src/abstraction-common.h b/src/abstraction-common.h new file mode 100644 index 00000000..dc44bb67 --- /dev/null +++ b/src/abstraction-common.h @@ -0,0 +1,50 @@ +/** + * Libcgroup abstraction layer prototypes and structs + * + * Copyright (c) 2021-2022 Oracle and/or its affiliates. + * Author: Tom Hromatka + */ + +/* + * This library is free software; you can redistribute it and/or modify it + * under the terms of version 2.1 of the GNU Lesser General Public License as + * published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, see . + */ +#ifndef __ABSTRACTION_COMMON +#define __ABSTRACTION_COMMON + +#ifdef __cplusplus +extern "C" { +#endif + +#include "config.h" +#include +#include "libcgroup-internal.h" + +/** + * Convert a string to a long + * + * @param in_str String to be converted + * @param base Integer base + * @param out_value Pointer to hold the output long value + * + * @return 0 on success, + * ECGFAIL if the conversion to long failed, + * ECGINVAL upon an invalid parameter + */ +int cgroup_strtol(const char * const in_str, int base, + long int * const out_value); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* __ABSTRACTION_COMMON */ -- 2.47.2