From 3f993746b2ad674f204f23daa85ada0e0c84865a Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Mon, 4 Feb 2019 11:42:28 +0100 Subject: blogc: added "inside docker" template variable --- src/blogc/funcvars.c | 1 + src/blogc/sysinfo.c | 39 +++++++++++++++++++++++++++++++++++++++ src/blogc/sysinfo.h | 3 +++ src/common/file.c | 2 +- 4 files changed, 44 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/blogc/funcvars.c b/src/blogc/funcvars.c index beb5f8a..25a22bf 100644 --- a/src/blogc/funcvars.c +++ b/src/blogc/funcvars.c @@ -38,6 +38,7 @@ static const struct func_map { {"BLOGC_SYSINFO_DATETIME", blogc_sysinfo_inject_datetime}, #endif + {"BLOGC_SYSINFO_INSIDE_DOCKER", blogc_sysinfo_inject_inside_docker}, {NULL, NULL}, }; diff --git a/src/blogc/sysinfo.c b/src/blogc/sysinfo.c index 61c8b1d..9d03b61 100644 --- a/src/blogc/sysinfo.c +++ b/src/blogc/sysinfo.c @@ -26,7 +26,11 @@ #include #endif /* HAVE_TIME_H */ +#include #include +#include +#include "../common/error.h" +#include "../common/file.h" #include "../common/utils.h" #include "sysinfo.h" @@ -116,3 +120,38 @@ blogc_sysinfo_inject_datetime(bc_trie_t *global) bc_trie_insert(global, "BLOGC_SYSINFO_DATETIME", t); } + + +// it is obviously impossible that the same process runs inside and outside +// docker at the same time, then an unprotected global variable should be fine +// here +static bool inside_docker_evaluated = false; +static bool inside_docker = false; + +bool +blogc_sysinfo_get_inside_docker(void) +{ + if (inside_docker_evaluated) + return inside_docker; + inside_docker_evaluated = true; + + size_t len; + bc_error_t *err = NULL; + char *contents = bc_file_get_contents("/proc/1/cgroup", false, &len, &err); + if (err != NULL) { + inside_docker = false; + return inside_docker; + } + + bool inside_docker = NULL != strstr(contents, "/docker/"); + free(contents); + return inside_docker; +} + + +void +blogc_sysinfo_inject_inside_docker(bc_trie_t *global) +{ + if (blogc_sysinfo_get_inside_docker()) + bc_trie_insert(global, "BLOGC_SYSINFO_INSIDE_DOCKER", bc_strdup("1")); +} diff --git a/src/blogc/sysinfo.h b/src/blogc/sysinfo.h index 25f2177..783b656 100644 --- a/src/blogc/sysinfo.h +++ b/src/blogc/sysinfo.h @@ -31,6 +31,7 @@ #define HAVE_SYSINFO_DATETIME 1 #endif /* HAVE_TIME_H */ +#include #include "../common/utils.h" char* blogc_sysinfo_get_hostname(void); @@ -39,5 +40,7 @@ char* blogc_sysinfo_get_username(void); void blogc_sysinfo_inject_username(bc_trie_t *global); char* blogc_sysinfo_get_datetime(void); void blogc_sysinfo_inject_datetime(bc_trie_t *global); +bool blogc_sysinfo_get_inside_docker(void); +void blogc_sysinfo_inject_inside_docker(bc_trie_t *global); #endif /* ___SYSINFO_H */ diff --git a/src/common/file.c b/src/common/file.c index d64a0d8..1e55f64 100644 --- a/src/common/file.c +++ b/src/common/file.c @@ -20,7 +20,7 @@ char* bc_file_get_contents(const char *path, bool utf8, size_t *len, bc_error_t **err) { - if (path == NULL || err == NULL || *err != NULL) + if (path == NULL || len == NULL || err == NULL || *err != NULL) return NULL; *len = 0; -- cgit v1.2.3-18-g5258