[systemd-devel] [PATCH] virt: fix container detection when we're not PID 1

Jan Synacek jsynacek at redhat.com
Wed Dec 10 00:21:23 PST 2014


systemd-detect-virt would print "none" when using nspawn to run a shell
inside a container and then running systemd-detect-virt in it, because
the shell would be PID 1, not the actuall systemd-detect-virt process.
---
 src/shared/virt.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)

diff --git a/src/shared/virt.c b/src/shared/virt.c
index f9c4e67..298e005 100644
--- a/src/shared/virt.c
+++ b/src/shared/virt.c
@@ -275,18 +275,10 @@ int detect_container(const char **id) {
                 goto finish;
         }
 
-        if (getpid() == 1) {
-                /* If we are PID 1 we can just check our own
-                 * environment variable */
-
-                e = getenv("container");
-                if (isempty(e)) {
-                        r = 0;
-                        goto finish;
-                }
-        } else {
-
-                /* Otherwise, PID 1 dropped this information into a
+        /* Check our own environment variable */
+        e = getenv("container");
+        if (isempty(e)) {
+                /* PID 1 dropped this information into a
                  * file in /run. This is better than accessing
                  * /proc/1/environ, since we don't need CAP_SYS_PTRACE
                  * for that. */
@@ -300,7 +292,8 @@ int detect_container(const char **id) {
                         return r;
 
                 e = m;
-        }
+        } else
+                r = 0;
 
         /* We only recognize a selected few here, since we want to
          * enforce a redacted namespace */
-- 
1.9.3



More information about the systemd-devel mailing list