[systemd-commits] 2 commits - src/shared src/test test/udev-test.pl

Lennart Poettering lennart at kemper.freedesktop.org
Wed Dec 10 04:36:19 PST 2014


 src/shared/virt.c       |   22 ++++++++++++++++++++--
 src/test/test-execute.c |    2 +-
 src/test/test-util.c    |    4 +++-
 test/udev-test.pl       |    8 ++++++++
 4 files changed, 32 insertions(+), 4 deletions(-)

New commits:
commit 0eb3cc88504b5d8f740764047ac5162b67992386
Author: Jan Synacek <jsynacek at redhat.com>
Date:   Wed Dec 10 10:20:11 2014 +0100

    test: fix some tests when running inside a container

diff --git a/src/test/test-execute.c b/src/test/test-execute.c
index 85deb27..60466f0 100644
--- a/src/test/test-execute.c
+++ b/src/test/test-execute.c
@@ -164,7 +164,7 @@ int main(int argc, char *argv[]) {
         r = manager_new(SYSTEMD_USER, true, &m);
         if (IN_SET(r, -EPERM, -EACCES, -EADDRINUSE, -EHOSTDOWN, -ENOENT)) {
                 printf("Skipping test: manager_new: %s", strerror(-r));
-                return -EXIT_TEST_SKIP;
+                return EXIT_TEST_SKIP;
         }
         assert_se(r >= 0);
         assert_se(manager_startup(m, NULL, NULL) >= 0);
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 20e711d..fe54586 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -35,6 +35,7 @@
 #include "def.h"
 #include "fileio.h"
 #include "conf-parser.h"
+#include "virt.h"
 
 static void test_streq_ptr(void) {
         assert_se(streq_ptr(NULL, NULL));
@@ -544,7 +545,8 @@ static void test_get_process_comm(void) {
         assert_se(r >= 0 || r == -EACCES);
         log_info("self strlen(environ): '%zd'", strlen(env));
 
-        assert_se(get_ctty_devnr(1, &h) == -ENOENT);
+        if (!detect_container(NULL))
+                assert_se(get_ctty_devnr(1, &h) == -ENOENT);
 
         getenv_for_pid(1, "PATH", &i);
         log_info("pid1 $PATH: '%s'", strna(i));
diff --git a/test/udev-test.pl b/test/udev-test.pl
index 14f11df..3e05b61 100755
--- a/test/udev-test.pl
+++ b/test/udev-test.pl
@@ -27,6 +27,7 @@ my $udev_dev            = "test/dev";
 my $udev_run            = "test/run";
 my $udev_rules_dir      = "$udev_run/udev/rules.d";
 my $udev_rules          = "$udev_rules_dir/udev-test.rules";
+my $EXIT_TEST_SKIP      = 77;
 
 my @tests = (
         {
@@ -1485,6 +1486,13 @@ if (!($<==0)) {
         exit;
 }
 
+# skip the test when running in a container
+system("systemd-detect-virt", "-c", "-q");
+if ($? >> 8 == 0) {
+    print "Running in a container, skipping the test.\n";
+    exit($EXIT_TEST_SKIP);
+}
+
 udev_setup();
 
 my $test_num = 1;

commit 536bfdab4cca38916ec8b112a6f80b0c068cc806
Author: Lennart Poettering <lennart at poettering.net>
Date:   Wed Dec 10 13:23:49 2014 +0100

    virt: when detecting containers and /run/systemd/container cannot be read, check /proc/1/environ
    
    This way, we should be in a slightly better situation if a container is
    booted up with only a shell as PID 1. In that case
    /run/systemd/container will not be populated, and a check for it hence
    be ineffective.
    
    Checking /proc/1/environ doesn't fully fix the problem though, as the
    file is only accessible with privileges. This means if PID 1 is not
    systemd, and if privileges have been dropped the container detection
    will continue to fail.

diff --git a/src/shared/virt.c b/src/shared/virt.c
index f9c4e67..f10baab 100644
--- a/src/shared/virt.c
+++ b/src/shared/virt.c
@@ -293,8 +293,26 @@ int detect_container(const char **id) {
 
                 r = read_one_line_file("/run/systemd/container", &m);
                 if (r == -ENOENT) {
-                        r = 0;
-                        goto finish;
+
+                        /* Fallback for cases where PID 1 was not
+                         * systemd (for example, cases where
+                         * init=/bin/sh is used. */
+
+                        r = getenv_for_pid(1, "container", &m);
+                        if (r <= 0) {
+
+                                /* If that didn't work, give up,
+                                 * assume no container manager.
+                                 *
+                                 * Note: This means we still cannot
+                                 * detect containers if init=/bin/sh
+                                 * is passed but privileges dropped,
+                                 * as /proc/1/environ is only readable
+                                 * with privileges. */
+
+                                r = 0;
+                                goto finish;
+                        }
                 }
                 if (r < 0)
                         return r;



More information about the systemd-commits mailing list