From 025713a46ee9bae772a5198c96c6a5f5560eddfd Mon Sep 17 00:00:00 2001 From: Georg Reinke Date: Sat, 15 Apr 2017 20:38:58 +0200 Subject: [PATCH] don't link libext against any external libraries --- include/villas/crypt.h | 10 ++ include/villas/json.h | 22 ++++ include/villas/sample_io.h | 7 -- include/villas/utils.h | 16 --- lib/Makefile.inc | 5 +- lib/advio.c | 1 + lib/apis/config.c | 3 +- lib/apis/nodes.c | 3 +- lib/crypt.c | 29 +++++ lib/json.c | 220 +++++++++++++++++++++++++++++++++++++ lib/sample_io.c | 102 +---------------- lib/super_node.c | 1 + lib/utils.c | 143 ------------------------ src/shmem.c | 7 +- tests/unit/config_json.c | 3 +- tests/unit/utils.c | 3 +- tools/conf2json.c | 3 +- 17 files changed, 301 insertions(+), 277 deletions(-) create mode 100644 include/villas/crypt.h create mode 100644 include/villas/json.h create mode 100644 lib/crypt.c create mode 100644 lib/json.c diff --git a/include/villas/crypt.h b/include/villas/crypt.h new file mode 100644 index 000000000..d9f168f88 --- /dev/null +++ b/include/villas/crypt.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +/** Calculate SHA1 hash of complete file \p f and place it into \p sha1. + * + * @param sha1[out] Must be SHA_DIGEST_LENGTH (20) in size. + * @retval 0 Everything was okay. + */ +int sha1sum(FILE *f, unsigned char *sha1); diff --git a/include/villas/json.h b/include/villas/json.h new file mode 100644 index 000000000..868f007ce --- /dev/null +++ b/include/villas/json.h @@ -0,0 +1,22 @@ +#pragma once + +#ifdef WITH_JANSSON + +#include +#include + +#include "sample.h" + +/* Convert a libconfig object to a libjansson object */ +json_t * config_to_json(config_setting_t *cfg); + +int json_to_config(json_t *json, config_setting_t *parent); + +int sample_io_json_pack(json_t **j, struct sample *s, int flags); + +int sample_io_json_unpack(json_t *j, struct sample *s, int *flags); + +int sample_io_json_fprint(FILE *f, struct sample *s, int flags); + +int sample_io_json_fscan(FILE *f, struct sample *s, int *flags); +#endif diff --git a/include/villas/sample_io.h b/include/villas/sample_io.h index 4e14fe809..12acc46d6 100644 --- a/include/villas/sample_io.h +++ b/include/villas/sample_io.h @@ -74,10 +74,3 @@ int sample_io_villas_fscan(FILE *f, struct sample *s, int *flags); /* JSON format */ -int sample_io_json_pack(json_t **j, struct sample *s, int flags); - -int sample_io_json_unpack(json_t *j, struct sample *s, int *flags); - -int sample_io_json_fprint(FILE *f, struct sample *s, int flags); - -int sample_io_json_fscan(FILE *f, struct sample *s, int *flags); diff --git a/include/villas/utils.h b/include/villas/utils.h index 856c4ec1c..72a5efbd6 100644 --- a/include/villas/utils.h +++ b/include/villas/utils.h @@ -168,15 +168,6 @@ int cpulist_parse(const char *str, cpu_set_t *set, int fail); */ char * cpulist_create(char *str, size_t len, cpu_set_t *set); -#ifdef WITH_JANSSON - #include - -/* Convert a libconfig object to a libjansson object */ -json_t * config_to_json(config_setting_t *cfg); - -#endif -int json_to_config(json_t *json, config_setting_t *parent); - /** Allocate and initialize memory. */ void * alloc(size_t bytes); @@ -239,11 +230,4 @@ void rdtsc_sleep(uint64_t nanosecs, uint64_t start); /** Register a exit callback for program termination (SIGINT / SIGKILL). */ void signals_init(void (*cb)(int signal, siginfo_t *sinfo, void *ctx)); -/** Calculate SHA1 hash of complete file \p f and place it into \p sha1. - * - * @param sha1[out] Must be SHA_DIGEST_LENGTH (20) in size. - * @retval 0 Everything was okay. - */ -int sha1sum(FILE *f, unsigned char *sha1); - pid_t spawn(const char* name, char **argv); diff --git a/lib/Makefile.inc b/lib/Makefile.inc index c44e997b9..81ae0a760 100644 --- a/lib/Makefile.inc +++ b/lib/Makefile.inc @@ -13,10 +13,11 @@ LIB_SRCS = $(addprefix lib/nodes/, file.c cbuilder.c shmem.c) \ log.c utils.c super_node.c hist.c timing.c pool.c \ list.c queue.c queue_signalled.c memory.c advio.c web.c api.c \ plugin.c node_type.c stats.c mapping.c sample_io.c shmem.c \ + json.c crypt.c \ ) LIBEXT_SRCS = $(addprefix lib/, sample.c node.c queue.c queue_signalled.c \ - memory.c shmem.c utils.c kernel/kernel.c log.c list.c sample_io.c \ + memory.c shmem.c utils.c kernel/kernel.c log.c list.c \ plugin.c timing.c) LIB_CFLAGS = $(CFLAGS) -fPIC @@ -25,7 +26,7 @@ LIB_LDLIBS = $(LDLIBS) -ldl -lrt -Wl,-soname,$(LIB_NAME).so.$(LIB_ABI_VERSION) LIBEXT_CFLAGS = $(CFLAGS) -fPIC LIBEXT_LDFLAGS = -shared -LIBEXT_LDLIBS = -ljansson -lcrypto -lrt -Wl,-soname,$(LIBEXT_NAME).so.$(LIBEXT_ABI_VERSION) +LIBEXT_LDLIBS = -ldl -lrt -Wl,-soname,$(LIBEXT_NAME).so.$(LIBEXT_ABI_VERSION) -include lib/hooks/Makefile.inc -include lib/apis/Makefile.inc diff --git a/lib/advio.c b/lib/advio.c index f2d942394..36ff6f526 100644 --- a/lib/advio.c +++ b/lib/advio.c @@ -19,6 +19,7 @@ #include "utils.h" #include "config.h" #include "advio.h" +#include "crypt.h" #define BAR_WIDTH 60 /**< How wide you want the progress meter to be. */ diff --git a/lib/apis/config.c b/lib/apis/config.c index ae41e6e53..dbc249df3 100644 --- a/lib/apis/config.c +++ b/lib/apis/config.c @@ -11,6 +11,7 @@ #include "api.h" #include "utils.h" #include "plugin.h" +#include "json.h" static int api_config(struct api_ressource *h, json_t *args, json_t **resp, struct api_session *s) { @@ -28,4 +29,4 @@ static struct plugin p = { .api.cb = api_config }; -REGISTER_PLUGIN(&p) \ No newline at end of file +REGISTER_PLUGIN(&p) diff --git a/lib/apis/nodes.c b/lib/apis/nodes.c index b65038d3a..0cd047d86 100644 --- a/lib/apis/nodes.c +++ b/lib/apis/nodes.c @@ -12,6 +12,7 @@ #include "api.h" #include "node.h" #include "utils.h" +#include "json.h" extern struct list nodes; @@ -49,4 +50,4 @@ static struct plugin p = { .api.cb = api_nodes }; -REGISTER_PLUGIN(&p) \ No newline at end of file +REGISTER_PLUGIN(&p) diff --git a/lib/crypt.c b/lib/crypt.c new file mode 100644 index 000000000..f7282e4eb --- /dev/null +++ b/lib/crypt.c @@ -0,0 +1,29 @@ +#include "crypt.h" + +#include + +int sha1sum(FILE *f, unsigned char *sha1) +{ + SHA_CTX c; + char buf[512]; + ssize_t bytes; + long seek; + + seek = ftell(f); + fseek(f, 0, SEEK_SET); + + SHA1_Init(&c); + + bytes = fread(buf, 1, 512, f); + while (bytes > 0) { + SHA1_Update(&c, buf, bytes); + bytes = fread(buf, 1, 512, f); + } + + SHA1_Final(sha1, &c); + + fseek(f, seek, SEEK_SET); + + return 0; +} + diff --git a/lib/json.c b/lib/json.c new file mode 100644 index 000000000..16a1dc1b7 --- /dev/null +++ b/lib/json.c @@ -0,0 +1,220 @@ +#include "json.h" + +#ifdef WITH_JANSSON +static int json_to_config_type(int type) +{ + switch (type) { + case JSON_OBJECT: return CONFIG_TYPE_GROUP; + case JSON_ARRAY: return CONFIG_TYPE_LIST; + case JSON_STRING: return CONFIG_TYPE_STRING; + case JSON_INTEGER: return CONFIG_TYPE_INT64; + case JSON_REAL: return CONFIG_TYPE_FLOAT; + case JSON_TRUE: + case JSON_FALSE: + case JSON_NULL: return CONFIG_TYPE_BOOL; + } + + return -1; +} + +json_t * config_to_json(config_setting_t *cfg) +{ + switch (config_setting_type(cfg)) { + case CONFIG_TYPE_INT: return json_integer(config_setting_get_int(cfg)); + case CONFIG_TYPE_INT64: return json_integer(config_setting_get_int64(cfg)); + case CONFIG_TYPE_FLOAT: return json_real(config_setting_get_float(cfg)); + case CONFIG_TYPE_STRING: return json_string(config_setting_get_string(cfg)); + case CONFIG_TYPE_BOOL: return json_boolean(config_setting_get_bool(cfg)); + + case CONFIG_TYPE_ARRAY: + case CONFIG_TYPE_LIST: { + json_t *json = json_array(); + + for (int i = 0; i < config_setting_length(cfg); i++) + json_array_append_new(json, config_to_json(config_setting_get_elem(cfg, i))); + + return json; + } + + case CONFIG_TYPE_GROUP: { + json_t *json = json_object(); + + for (int i = 0; i < config_setting_length(cfg); i++) { + json_object_set_new(json, + config_setting_name(config_setting_get_elem(cfg, i)), + config_to_json(config_setting_get_elem(cfg, i)) + ); + } + + return json; + } + + default: + return json_object(); + } +} + +int json_to_config(json_t *json, config_setting_t *parent) +{ + config_setting_t *cfg; + int ret, type; + + if (config_setting_is_root(parent)) { + if (!json_is_object(json)) + return -1; /* The root must be an object! */ + } + + switch (json_typeof(json)) { + case JSON_OBJECT: { + const char *key; + json_t *json_value; + + json_object_foreach(json, key, json_value) { + type = json_to_config_type(json_typeof(json_value)); + + cfg = config_setting_add(parent, key, type); + ret = json_to_config(json_value, cfg); + if (ret) + return ret; + } + break; + } + + case JSON_ARRAY: { + size_t i; + json_t *json_value; + + json_array_foreach(json, i, json_value) { + type = json_to_config_type(json_typeof(json_value)); + + cfg = config_setting_add(parent, NULL, type); + ret = json_to_config(json_value, cfg); + if (ret) + return ret; + } + break; + } + + case JSON_STRING: + config_setting_set_string(parent, json_string_value(json)); + break; + + case JSON_INTEGER: + config_setting_set_int64(parent, json_integer_value(json)); + break; + + case JSON_REAL: + config_setting_set_float(parent, json_real_value(json)); + break; + + case JSON_TRUE: + case JSON_FALSE: + case JSON_NULL: + config_setting_set_bool(parent, json_is_true(json)); + break; + } + + return 0; +} + +int sample_io_json_pack(json_t **j, struct sample *s, int flags) +{ + json_error_t err; + json_t *json_data = json_array(); + + for (int i = 0; i < s->length; i++) { + json_t *json_value = sample_get_data_format(s, i) + ? json_integer(s->data[i].i) + : json_real(s->data[i].f); + + json_array_append(json_data, json_value); + } + + *j = json_pack_ex(&err, 0, "{ s: { s: [ I, I ], s: [ I, I ], s: [ I, I ] }, s: I, s: o }", + "ts", + "origin", s->ts.origin.tv_sec, s->ts.origin.tv_nsec, + "received", s->ts.received.tv_sec, s->ts.received.tv_nsec, + "sent", s->ts.sent.tv_sec, s->ts.sent.tv_nsec, + "sequence", s->sequence, + "data", json_data); + + if (!*j) + return -1; + + return 0; +} + +int sample_io_json_unpack(json_t *j, struct sample *s, int *flags) +{ + int ret, i; + json_t *json_data, *json_value; + + ret = json_unpack(j, "{ s: { s: [ I, I ], s: [ I, I ], s: [ I, I ] }, s: I, s: o }", + "ts", + "origin", &s->ts.origin.tv_sec, &s->ts.origin.tv_nsec, + "received", &s->ts.received.tv_sec, &s->ts.received.tv_nsec, + "sent", &s->ts.sent.tv_sec, &s->ts.sent.tv_nsec, + "sequence", &s->sequence, + "data", &json_data); + + if (ret) + return ret; + + s->length = 0; + + json_array_foreach(json_data, i, json_value) { + switch (json_typeof(json_value)) { + case JSON_REAL: + s->data[i].f = json_real_value(json_value); + sample_set_data_format(s, i, SAMPLE_DATA_FORMAT_FLOAT); + break; + + case JSON_INTEGER: + s->data[i].f = json_integer_value(json_value); + sample_set_data_format(s, i, SAMPLE_DATA_FORMAT_INT); + break; + + default: + return -1; + } + + s->length++; + } + + return 0; +} + +int sample_io_json_fprint(FILE *f, struct sample *s, int flags) +{ + int ret; + json_t *json; + + ret = sample_io_json_pack(&json, s, flags); + if (ret) + return ret; + + ret = json_dumpf(json, f, 0); + + json_decref(json); + + return ret; +} + +int sample_io_json_fscan(FILE *f, struct sample *s, int *flags) +{ + int ret; + json_t *json; + json_error_t err; + + json = json_loadf(f, JSON_DISABLE_EOF_CHECK, &err); + if (!json) + return -1; + + ret = sample_io_json_unpack(json, s, flags); + + json_decref(json); + + return ret; +} +#endif + diff --git a/lib/sample_io.c b/lib/sample_io.c index ccd1e871a..7c59b267b 100644 --- a/lib/sample_io.c +++ b/lib/sample_io.c @@ -6,6 +6,7 @@ #include +#include "json.h" #include "sample.h" #include "sample_io.h" #include "timing.h" @@ -179,104 +180,3 @@ skip: if (fgets(line, sizeof(line), f) == NULL) return sample_io_villas_scan(line, s, fl); } -#ifdef WITH_JANSSON -int sample_io_json_pack(json_t **j, struct sample *s, int flags) -{ - json_error_t err; - json_t *json_data = json_array(); - - for (int i = 0; i < s->length; i++) { - json_t *json_value = sample_get_data_format(s, i) - ? json_integer(s->data[i].i) - : json_real(s->data[i].f); - - json_array_append(json_data, json_value); - } - - *j = json_pack_ex(&err, 0, "{ s: { s: [ I, I ], s: [ I, I ], s: [ I, I ] }, s: I, s: o }", - "ts", - "origin", s->ts.origin.tv_sec, s->ts.origin.tv_nsec, - "received", s->ts.received.tv_sec, s->ts.received.tv_nsec, - "sent", s->ts.sent.tv_sec, s->ts.sent.tv_nsec, - "sequence", s->sequence, - "data", json_data); - - if (!*j) - return -1; - - return 0; -} - -int sample_io_json_unpack(json_t *j, struct sample *s, int *flags) -{ - int ret, i; - json_t *json_data, *json_value; - - ret = json_unpack(j, "{ s: { s: [ I, I ], s: [ I, I ], s: [ I, I ] }, s: I, s: o }", - "ts", - "origin", &s->ts.origin.tv_sec, &s->ts.origin.tv_nsec, - "received", &s->ts.received.tv_sec, &s->ts.received.tv_nsec, - "sent", &s->ts.sent.tv_sec, &s->ts.sent.tv_nsec, - "sequence", &s->sequence, - "data", &json_data); - - if (ret) - return ret; - - s->length = 0; - - json_array_foreach(json_data, i, json_value) { - switch (json_typeof(json_value)) { - case JSON_REAL: - s->data[i].f = json_real_value(json_value); - sample_set_data_format(s, i, SAMPLE_DATA_FORMAT_FLOAT); - break; - - case JSON_INTEGER: - s->data[i].f = json_integer_value(json_value); - sample_set_data_format(s, i, SAMPLE_DATA_FORMAT_INT); - break; - - default: - return -1; - } - - s->length++; - } - - return 0; -} - -int sample_io_json_fprint(FILE *f, struct sample *s, int flags) -{ - int ret; - json_t *json; - - ret = sample_io_json_pack(&json, s, flags); - if (ret) - return ret; - - ret = json_dumpf(json, f, 0); - - json_decref(json); - - return ret; -} - -int sample_io_json_fscan(FILE *f, struct sample *s, int *flags) -{ - int ret; - json_t *json; - json_error_t err; - - json = json_loadf(f, JSON_DISABLE_EOF_CHECK, &err); - if (!json) - return -1; - - ret = sample_io_json_unpack(json, s, flags); - - json_decref(json); - - return ret; -} -#endif \ No newline at end of file diff --git a/lib/super_node.c b/lib/super_node.c index da9d25586..62a390282 100644 --- a/lib/super_node.c +++ b/lib/super_node.c @@ -22,6 +22,7 @@ #include "plugin.h" #include "memory.h" #include "config.h" +#include "json.h" #include "kernel/rt.h" diff --git a/lib/utils.c b/lib/utils.c index 9f3b3edbe..50ba3756a 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -221,124 +221,6 @@ char *cpulist_create(char *str, size_t len, cpu_set_t *set) return str; } -#ifdef WITH_JANSSON -static int json_to_config_type(int type) -{ - switch (type) { - case JSON_OBJECT: return CONFIG_TYPE_GROUP; - case JSON_ARRAY: return CONFIG_TYPE_LIST; - case JSON_STRING: return CONFIG_TYPE_STRING; - case JSON_INTEGER: return CONFIG_TYPE_INT64; - case JSON_REAL: return CONFIG_TYPE_FLOAT; - case JSON_TRUE: - case JSON_FALSE: - case JSON_NULL: return CONFIG_TYPE_BOOL; - } - - return -1; -} - -json_t * config_to_json(config_setting_t *cfg) -{ - switch (config_setting_type(cfg)) { - case CONFIG_TYPE_INT: return json_integer(config_setting_get_int(cfg)); - case CONFIG_TYPE_INT64: return json_integer(config_setting_get_int64(cfg)); - case CONFIG_TYPE_FLOAT: return json_real(config_setting_get_float(cfg)); - case CONFIG_TYPE_STRING: return json_string(config_setting_get_string(cfg)); - case CONFIG_TYPE_BOOL: return json_boolean(config_setting_get_bool(cfg)); - - case CONFIG_TYPE_ARRAY: - case CONFIG_TYPE_LIST: { - json_t *json = json_array(); - - for (int i = 0; i < config_setting_length(cfg); i++) - json_array_append_new(json, config_to_json(config_setting_get_elem(cfg, i))); - - return json; - } - - case CONFIG_TYPE_GROUP: { - json_t *json = json_object(); - - for (int i = 0; i < config_setting_length(cfg); i++) { - json_object_set_new(json, - config_setting_name(config_setting_get_elem(cfg, i)), - config_to_json(config_setting_get_elem(cfg, i)) - ); - } - - return json; - } - - default: - return json_object(); - } -} - -int json_to_config(json_t *json, config_setting_t *parent) -{ - config_setting_t *cfg; - int ret, type; - - if (config_setting_is_root(parent)) { - if (!json_is_object(json)) - return -1; /* The root must be an object! */ - } - - switch (json_typeof(json)) { - case JSON_OBJECT: { - const char *key; - json_t *json_value; - - json_object_foreach(json, key, json_value) { - type = json_to_config_type(json_typeof(json_value)); - - cfg = config_setting_add(parent, key, type); - ret = json_to_config(json_value, cfg); - if (ret) - return ret; - } - break; - } - - case JSON_ARRAY: { - size_t i; - json_t *json_value; - - json_array_foreach(json, i, json_value) { - type = json_to_config_type(json_typeof(json_value)); - - cfg = config_setting_add(parent, NULL, type); - ret = json_to_config(json_value, cfg); - if (ret) - return ret; - } - break; - } - - case JSON_STRING: - config_setting_set_string(parent, json_string_value(json)); - break; - - case JSON_INTEGER: - config_setting_set_int64(parent, json_integer_value(json)); - break; - - case JSON_REAL: - config_setting_set_float(parent, json_real_value(json)); - break; - - case JSON_TRUE: - case JSON_FALSE: - case JSON_NULL: - config_setting_set_bool(parent, json_is_true(json)); - break; - } - - return 0; -} -#endif - void * alloc(size_t bytes) { void *p = malloc(bytes); @@ -418,31 +300,6 @@ void signals_init(void (*cb)(int signal, siginfo_t *sinfo, void *ctx)) sigaction(SIGCHLD, &sa_chld, NULL); } -int sha1sum(FILE *f, unsigned char *sha1) -{ - SHA_CTX c; - char buf[512]; - ssize_t bytes; - long seek; - - seek = ftell(f); - fseek(f, 0, SEEK_SET); - - SHA1_Init(&c); - - bytes = fread(buf, 1, 512, f); - while (bytes > 0) { - SHA1_Update(&c, buf, bytes); - bytes = fread(buf, 1, 512, f); - } - - SHA1_Final(sha1, &c); - - fseek(f, seek, SEEK_SET); - - return 0; -} - pid_t spawn(const char* name, char **argv) { pid_t pid = fork(); diff --git a/src/shmem.c b/src/shmem.c index 8949ec95b..3f98868cc 100644 --- a/src/shmem.c +++ b/src/shmem.c @@ -9,7 +9,6 @@ #include "pool.h" #include "queue_signalled.h" #include "sample.h" -#include "sample_io.h" #include "shmem.h" #include "utils.h" @@ -56,8 +55,10 @@ int main(int argc, char* argv[]) int avail = sample_alloc(&shared->pool, outsmps, r); if (avail < r) warn("pool underrun (%d/%d)\n", avail, r); - for (int i = 0; i < r; i++) - sample_io_villas_fprint(stdout, insmps[i], SAMPLE_IO_ALL); + for (int i = 0; i < r; i++) { + printf("got sample: seq %d recv %ld.%ld\n", insmps[i]->sequence, + insmps[i]->ts.received.tv_sec, insmps[i]->ts.received.tv_nsec); + } for (int i = 0; i < avail; i++) { outsmps[i]->sequence = insmps[i]->sequence; outsmps[i]->ts = insmps[i]->ts; diff --git a/tests/unit/config_json.c b/tests/unit/config_json.c index 10c67ff32..380c699d2 100644 --- a/tests/unit/config_json.c +++ b/tests/unit/config_json.c @@ -10,6 +10,7 @@ #include #include "utils.h" +#include "json.h" const char *cfg_example = "test : \n" "{\n" @@ -96,4 +97,4 @@ Test(utils, json_to_config) cr_assert_str_eq(str, cfg_example); json_decref(json); -} \ No newline at end of file +} diff --git a/tests/unit/utils.c b/tests/unit/utils.c index cc3981ab1..ba11a9412 100644 --- a/tests/unit/utils.c +++ b/tests/unit/utils.c @@ -6,6 +6,7 @@ #include +#include "crypt.h" #include "utils.h" /* Simple normality test for 1,2,3s intervals */ @@ -151,4 +152,4 @@ Test(utils, sha1sum) cr_assert_arr_eq(hash, expected, SHA_DIGEST_LENGTH); fclose(f); -} \ No newline at end of file +} diff --git a/tools/conf2json.c b/tools/conf2json.c index 2e442a1f6..d92b4a769 100644 --- a/tools/conf2json.c +++ b/tools/conf2json.c @@ -7,6 +7,7 @@ #include #include +#include #include void usage() @@ -48,4 +49,4 @@ int main(int argc, char *argv[]) config_destroy(&cfg); return 0; -} \ No newline at end of file +}