diff options
author | Lennart Poettering <lennart@poettering.net> | 2011-09-23 04:38:39 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2011-09-23 16:28:27 +0200 |
commit | 65bc2c21140d20e757b0aed9bb23286939426abb (patch) | |
tree | ea1be9f787323ba98113382965745e31c51c17f4 /src | |
parent | 3bb1c6b04f93841c10d2cb1c4e2945d5a0bb8ff1 (diff) |
util: detect systemd-nspawn without relying on ns cgroup tree
Diffstat (limited to 'src')
-rw-r--r-- | src/detect-virt.c | 3 | ||||
-rw-r--r-- | src/util.c | 51 |
2 files changed, 50 insertions, 4 deletions
diff --git a/src/detect-virt.c b/src/detect-virt.c index 57f0176668..324f182c7e 100644 --- a/src/detect-virt.c +++ b/src/detect-virt.c @@ -34,7 +34,8 @@ int main(int argc, char *argv[]) { * to detect whether we are being run in a virtualized * environment or not */ - if ((r = detect_virtualization(&id)) < 0) { + r = detect_virtualization(&id); + if (r < 0) { log_error("Failed to check for virtualization: %s", strerror(-r)); return EXIT_FAILURE; } diff --git a/src/util.c b/src/util.c index 36c8938c2f..33b6fd4809 100644 --- a/src/util.c +++ b/src/util.c @@ -4384,7 +4384,7 @@ int detect_vm(const char **id) { if (hypervisor) { if (id) - *id = "other"; + *id = "other-vm"; return 1; } @@ -4421,7 +4421,51 @@ int detect_container(const char **id) { return 1; } - if ((f = fopen("/proc/self/cgroup", "re"))) { + f = fopen("/proc/1/environ", "re"); + if (f) { + bool done = false; + + do { + char line[LINE_MAX]; + unsigned i; + + for (i = 0; i < sizeof(line)-1; i++) { + int c; + + c = getc(f); + if (_unlikely_(c == EOF)) { + done = true; + break; + } else if (c == 0) + break; + + line[i] = c; + } + line[i] = 0; + + if (streq(line, "container=lxc")) { + fclose(f); + *id = "lxc"; + return 1; + + } else if (streq(line, "container=systemd-nspawn")) { + fclose(f); + *id = "systemd-nspawn"; + return 1; + + } else if (startswith(line, "container=")) { + fclose(f); + *id = "other-container"; + return 1; + } + + } while (!done); + + fclose(f); + } + + f = fopen("/proc/self/cgroup", "re"); + if (f) { for (;;) { char line[LINE_MAX], *p; @@ -4429,7 +4473,8 @@ int detect_container(const char **id) { if (!fgets(line, sizeof(line), f)) break; - if (!(p = strchr(strstrip(line), ':'))) + p = strchr(strstrip(line), ':'); + if (!p) continue; if (strncmp(p, ":ns:", 4)) |