Commit dcbf6ddc authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

use C++ code-style

parent 3e062fdc
......@@ -36,8 +36,6 @@
#include <sys/types.h>
#include <openssl/sha.h>
#include <jansson.h>
#include <uuid/uuid.h>
#include <villas/config.h>
......@@ -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<int> cbSignals = {}, std::list<int> ignoreSignals = { SIGCHLD }) __attribute__ ((warn_unused_result));
int signalsInit(void (*cb)(int signal, siginfo_t *sinfo, void *ctx), std::list<int> cbSignals = {}, std::list<int> 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 {
......
/** UUID helpers.
*
* @file
* @author Steffen Vogel <github@daniel-krebs.net>
* @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 <http://www.gnu.org/licenses/>.
*********************************************************************************/
#pragma once
#include <string>
#include <jansson.h>
#include <uuid/uuid.h>
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 */
......@@ -42,6 +42,7 @@ add_library(villas-common SHARED
timing.cpp
tool.cpp
utils.cpp
uuid.cpp
version.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);
......
......@@ -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");
......
......@@ -39,7 +39,6 @@
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/evp.h>
#include <openssl/md5.h>
#include <jansson.h>
#include <uuid/uuid.h>
......@@ -78,7 +77,7 @@ std::vector<std::string> 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<int> cbSignals, std::list<int> ignoreSignals)
int signalsInit(void (*cb)(int signal, siginfo_t *sinfo, void *ctx), std::list<int> cbSignals, std::list<int> 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 */
......
/** UUID helpers.
*
* @author Steffen Vogel <svogel2@eonerc.rwth-aachen.de>
* @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 <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <openssl/md5.h>
#include <villas/uuid.hpp>
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;
}
......@@ -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));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment