aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2019-02-04 11:42:28 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2019-02-04 11:42:28 +0100
commit3f993746b2ad674f204f23daa85ada0e0c84865a (patch)
tree95d48ecfce4f99e7a47e9730e032a1233dda574a /src
parent013a37c8738d671d9d5f8381146d7d1e8f651762 (diff)
downloadblogc-3f993746b2ad674f204f23daa85ada0e0c84865a.tar.gz
blogc-3f993746b2ad674f204f23daa85ada0e0c84865a.tar.bz2
blogc-3f993746b2ad674f204f23daa85ada0e0c84865a.zip
blogc: added "inside docker" template variable
Diffstat (limited to 'src')
-rw-r--r--src/blogc/funcvars.c1
-rw-r--r--src/blogc/sysinfo.c39
-rw-r--r--src/blogc/sysinfo.h3
-rw-r--r--src/common/file.c2
4 files changed, 44 insertions, 1 deletions
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 <time.h>
#endif /* HAVE_TIME_H */
+#include <stdbool.h>
#include <stdlib.h>
+#include <string.h>
+#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 <stdbool.h>
#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;