in_container(): cache positive results

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2022-03-23 06:25:43 +01:00
1 parent f87e9d8880
commit 063d645d95
1 file changed
+10 -6
+10 -6
View File
@@ -548,29 +548,33 @@ done:
*/ */
int in_container(void) int in_container(void)
{ {
const char *files[] = {
"/run/.containerenv",
"/.dockerenv"
};
const char *containers[] = { const char *containers[] = {
"lxc", "lxc",
"docker", "docker",
"kubepod" "kubepod"
}; };
const char *files[] = {
"/run/.containerenv",
"/.dockerenv"
};
static int incont = -1;
size_t i; size_t i;
char *c; char *c;
if (incont > 0)
return incont;
c = getenv("container"); c = getenv("container");
if (c) { if (c) {
for (i = 0; i < NELEMS(containers); i++) { for (i = 0; i < NELEMS(containers); i++) {
if (!strcmp(containers[i], c)) if (!strcmp(containers[i], c))
return 1; return incont = 1;
} }
} }
for (i = 0; i < NELEMS(files); i++) { for (i = 0; i < NELEMS(files); i++) {
if (!access(files[i], F_OK)) if (!access(files[i], F_OK))
return 1; return incont = 1;
} }
return 0; return 0;