]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
abstraction: Add cgroup_strtol()
authorTom Hromatka <tom.hromatka@oracle.com>
Fri, 28 Jan 2022 17:20:38 +0000 (10:20 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 3 Feb 2022 21:41:50 +0000 (14:41 -0700)
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 <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
src/Makefile.am
src/abstraction-common.c [new file with mode: 0644]
src/abstraction-common.h [new file with mode: 0644]

index 811bce3c64cf0d0b552b20df315cafef78dea2d0..8a7136aebc13e962e5e74458771b7f9133da08c7 100644 (file)
@@ -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 (file)
index 0000000..04fec84
--- /dev/null
@@ -0,0 +1,67 @@
+/**
+ * Libcgroup abstraction layer
+ *
+ * Copyright (c) 2021-2022 Oracle and/or its affiliates.
+ * Author: Tom Hromatka <tom.hromatka@oracle.com>
+ */
+
+/*
+ * 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 <http://www.gnu.org/licenses>.
+ */
+
+#include <libcgroup.h>
+#include <libcgroup-internal.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#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 (file)
index 0000000..dc44bb6
--- /dev/null
@@ -0,0 +1,50 @@
+/**
+ * Libcgroup abstraction layer prototypes and structs
+ *
+ * Copyright (c) 2021-2022 Oracle and/or its affiliates.
+ * Author: Tom Hromatka <tom.hromatka@oracle.com>
+ */
+
+/*
+ * 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 <http://www.gnu.org/licenses>.
+ */
+#ifndef __ABSTRACTION_COMMON
+#define __ABSTRACTION_COMMON
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "config.h"
+#include <libcgroup.h>
+#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 */