diff --git a/common/include/villas/utils.hpp b/common/include/villas/utils.hpp index ca64c0a1c..e60e93c42 100644 --- a/common/include/villas/utils.hpp +++ b/common/include/villas/utils.hpp @@ -36,8 +36,6 @@ #include #include -#include -#include #include @@ -138,10 +136,10 @@ assertExcept(bool condition, const T &exception) } /** Register a exit callback for program termination: SIGINT, SIGKILL & SIGALRM. */ -int signals_init(void (*cb)(int signal, siginfo_t *sinfo, void *ctx), std::list cbSignals = {}, std::list ignoreSignals = { SIGCHLD }) __attribute__ ((warn_unused_result)); +int signalsInit(void (*cb)(int signal, siginfo_t *sinfo, void *ctx), std::list cbSignals = {}, std::list ignoreSignals = { SIGCHLD }) __attribute__ ((warn_unused_result)); /** Fill buffer with random data */ -ssize_t read_random(char *buf, size_t len); +ssize_t readRandom(char *buf, size_t len); /** Remove ANSI control sequences for colored output. */ char * decolor(char *str); @@ -152,7 +150,7 @@ char * decolor(char *str); * @param s Standard deviation * @return Normal variate random variable (Gaussian) */ -double box_muller(float m, float s); +double boxMuller(float m, float s); /** Double precission uniform random variable */ double randf(); @@ -201,26 +199,14 @@ size_t strlenp(const char *str); */ int sha1sum(FILE *f, unsigned char *sha1); -/** Generate an UUID by MD5 hashing the provided string */ -int uuid_generate_from_str(uuid_t out, const std::string &data, const std::string &ns = ""); - -/** Generate an UUID by MD5 hashing the provided string */ -int uuid_generate_from_str(uuid_t out, const std::string &data, const uuid_t ns); - -/** Generate an UUID by MD5 hashing the serialized representation of the provided JSON object */ -void uuid_generate_from_json(uuid_t out, json_t *json, const std::string &ns = ""); - -/** Generate an UUID by MD5 hashing the serialized representation of the provided JSON object */ -int uuid_generate_from_json(uuid_t out, json_t *json, const uuid_t ns); - /** Check if process is running inside a Docker container */ -bool is_docker(); +bool isDocker(); /** Check if process is running inside a Kubernetes container */ -bool is_kubernetes(); +bool isKubernetes(); /** Check if process is running inside a containerized environment */ -bool is_container(); +bool isContainer(); namespace base64 { diff --git a/common/include/villas/uuid.hpp b/common/include/villas/uuid.hpp new file mode 100644 index 000000000..9aca7b7c3 --- /dev/null +++ b/common/include/villas/uuid.hpp @@ -0,0 +1,47 @@ +/** UUID helpers. + * + * @file + * @author Steffen Vogel + * @copyright 2014-2021, Institute for Automation of Complex Power Systems, EONERC + * @license GNU General Public License (version 3) + * + * VILLAScommon + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *********************************************************************************/ + +#pragma once + +#include + +#include +#include + +namespace villas { +namespace uuid { + +/** Generate an UUID by MD5 hashing the provided string */ +int generateFromString(uuid_t out, const std::string &data, const std::string &ns = ""); + +/** Generate an UUID by MD5 hashing the provided string */ +int generateFromString(uuid_t out, const std::string &data, const uuid_t ns); + +/** Generate an UUID by MD5 hashing the serialized representation of the provided JSON object */ +void generateFromJson(uuid_t out, json_t *json, const std::string &ns = ""); + +/** Generate an UUID by MD5 hashing the serialized representation of the provided JSON object */ +int generateFromJson(uuid_t out, json_t *json, const uuid_t ns); + +} /* namespace uuid */ +} /* namespace villas */ diff --git a/common/lib/CMakeLists.txt b/common/lib/CMakeLists.txt index 18f8ebfc2..f6c0fa5ea 100644 --- a/common/lib/CMakeLists.txt +++ b/common/lib/CMakeLists.txt @@ -42,6 +42,7 @@ add_library(villas-common SHARED timing.cpp tool.cpp utils.cpp + uuid.cpp version.cpp ) diff --git a/common/lib/kernel/kernel.cpp b/common/lib/kernel/kernel.cpp index 54286601e..c16bb5439 100644 --- a/common/lib/kernel/kernel.cpp +++ b/common/lib/kernel/kernel.cpp @@ -262,18 +262,8 @@ int villas::kernel::setNrHugepages(int nr) FILE *f; f = fopen(PROCFS_PATH "/sys/vm/nr_hugepages", "w"); - if (!f) { - auto logger = logging.get("kernel"); - - if (is_container()) { - logger->warn("This functionality is unavailable in this mode. Please run the container in the privileged mode:"); - logger->warn(" $ docker run --privilged ..."); - } - else - logger->warn("Failed to open {}", PROCFS_PATH "/sys/vm/nr_hugepages"); - + if (!f) return -1; - } fprintf(f, "%d\n", nr); fclose(f); diff --git a/common/lib/tool.cpp b/common/lib/tool.cpp index c7bfdb0bd..37f89dcfd 100644 --- a/common/lib/tool.cpp +++ b/common/lib/tool.cpp @@ -65,7 +65,7 @@ int Tool::run() CLR_BLD(CLR_YEL(PROJECT_BUILD_ID)), CLR_BLD(CLR_MAG(__DATE__)), CLR_BLD(CLR_MAG(__TIME__))); - ret = utils::signals_init(staticHandler, handlerSignals); + ret = utils::signalsInit(staticHandler, handlerSignals); if (ret) throw RuntimeError("Failed to initialize signal subsystem"); diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index 031978179..060dde11f 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -39,7 +39,6 @@ #include #include #include -#include #include #include @@ -78,7 +77,7 @@ std::vector tokenize(std::string s, std::string delimiter) return tokens; } -ssize_t read_random(char *buf, size_t len) +ssize_t readRandom(char *buf, size_t len) { int fd; ssize_t bytes = -1; @@ -102,7 +101,7 @@ ssize_t read_random(char *buf, size_t len) } /* Setup exit handler */ -int signals_init(void (*cb)(int signal, siginfo_t *sinfo, void *ctx), std::list cbSignals, std::list ignoreSignals) +int signalsInit(void (*cb)(int signal, siginfo_t *sinfo, void *ctx), std::list cbSignals, std::list ignoreSignals) { int ret; @@ -177,14 +176,14 @@ char * decolor(char *str) void killme(int sig) { - /* Send only to main thread in case the ID was initilized by signals_init() */ + /* Send only to main thread in case the ID was initilized by signalsInit() */ if (main_thread) pthread_kill(main_thread, sig); else kill(0, sig); } -double box_muller(float m, float s) +double boxMuller(float m, float s) { double x1, x2, y1; static double y2; @@ -357,82 +356,19 @@ int sha1sum(FILE *f, unsigned char *sha1) return 0; } -int uuid_generate_from_str(uuid_t out, const std::string &data, const std::string &ns) -{ - int ret; - MD5_CTX c; - - ret = MD5_Init(&c); - if (!ret) - return -1; - - /* Namespace */ - ret = MD5_Update(&c, (unsigned char *) ns.c_str(), ns.size()); - if (!ret) - return -1; - - /* Data */ - ret = MD5_Update(&c, (unsigned char *) data.c_str(), data.size()); - if (!ret) - return -1; - - ret = MD5_Final((unsigned char *) out, &c); - if (!ret) - return -1; - - return 0; -} - -int uuid_generate_from_str(uuid_t out, const std::string &data, const uuid_t ns) -{ - int ret; - MD5_CTX c; - - ret = MD5_Init(&c); - if (!ret) - return -1; - - /* Namespace */ - ret = MD5_Update(&c, (unsigned char *) ns, 16); - if (!ret) - return -1; - - /* Data */ - ret = MD5_Update(&c, (unsigned char *) data.c_str(), data.size()); - if (!ret) - return -1; - - ret = MD5_Final((unsigned char *) out, &c); - if (!ret) - return -1; - - return 0; -} - -int uuid_generate_from_json(uuid_t out, json_t *json, const uuid_t ns) -{ - char *str = json_dumps(json, JSON_COMPACT | JSON_SORT_KEYS); - - int ret = uuid_generate_from_str(out, str, ns); - - free(str); - - return ret; -} - -bool is_docker() +bool isDocker() { return access("/.dockerenv", F_OK) != -1; } -bool is_kubernetes() +bool isKubernetes() { return access("/var/run/secrets/kubernetes.io", F_OK) != -1 || getenv("KUBERNETES_SERVICE_HOST") != nullptr; } -bool is_container() { - return is_docker() || is_kubernetes(); +bool isContainer() { + return isDocker() || isKubernetes(); } } /* namespace utils */ diff --git a/common/lib/uuid.cpp b/common/lib/uuid.cpp new file mode 100644 index 000000000..eea91b28a --- /dev/null +++ b/common/lib/uuid.cpp @@ -0,0 +1,90 @@ +/** UUID helpers. + * + * @author Steffen Vogel + * @copyright 2014-2021, Institute for Automation of Complex Power Systems, EONERC + * @license GNU General Public License (version 3) + * + * VILLAScommon + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *********************************************************************************/ + +#include + +#include + +using namespace villas::uuid; + +int villas::uuid::generateFromString(uuid_t out, const std::string &data, const std::string &ns) +{ + int ret; + MD5_CTX c; + + ret = MD5_Init(&c); + if (!ret) + return -1; + + /* Namespace */ + ret = MD5_Update(&c, (unsigned char *) ns.c_str(), ns.size()); + if (!ret) + return -1; + + /* Data */ + ret = MD5_Update(&c, (unsigned char *) data.c_str(), data.size()); + if (!ret) + return -1; + + ret = MD5_Final((unsigned char *) out, &c); + if (!ret) + return -1; + + return 0; +} + +int villas::uuid::generateFromString(uuid_t out, const std::string &data, const uuid_t ns) +{ + int ret; + MD5_CTX c; + + ret = MD5_Init(&c); + if (!ret) + return -1; + + /* Namespace */ + ret = MD5_Update(&c, (unsigned char *) ns, 16); + if (!ret) + return -1; + + /* Data */ + ret = MD5_Update(&c, (unsigned char *) data.c_str(), data.size()); + if (!ret) + return -1; + + ret = MD5_Final((unsigned char *) out, &c); + if (!ret) + return -1; + + return 0; +} + +int villas::uuid::generateFromJson(uuid_t out, json_t *json, const uuid_t ns) +{ + char *str = json_dumps(json, JSON_COMPACT | JSON_SORT_KEYS); + + int ret = generateFromString(out, str, ns); + + free(str); + + return ret; +} diff --git a/common/tests/unit/utils.cpp b/common/tests/unit/utils.cpp index 88e7cf0f3..23917a016 100644 --- a/common/tests/unit/utils.cpp +++ b/common/tests/unit/utils.cpp @@ -41,7 +41,7 @@ Test(utils, box_muller) unsigned iter = 1000000; for (unsigned i = 0; i < iter; i++) { - n = box_muller(0, 1); + n = boxMuller(0, 1); if (n > 2 || n < -2) sigma[2]++; else if (n > 1 || n < -1) sigma[1]++; @@ -123,7 +123,7 @@ Test(utils, memdup) char orig[1024], *copy; size_t len; - len = read_random(orig, sizeof(orig)); + len = readRandom(orig, sizeof(orig)); cr_assert_eq(len, sizeof(orig)); copy = (char *) memdup(orig, sizeof(orig));