X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fbasic%2Fvirt.c;h=3957a5a8c5abde7fd1552e1df328845c64aadcd6;hp=06fe509f772ab4042b7cb5e122fabdfa2182908f;hb=62383061a34dc8233b3e26c49271d5cb43fcb0f6;hpb=d77d27f4ca2945f88343fbe72ed0bf1236ad2aa6 diff --git a/src/basic/virt.c b/src/basic/virt.c index 06fe509f7..3957a5a8c 100644 --- a/src/basic/virt.c +++ b/src/basic/virt.c @@ -486,7 +486,6 @@ int detect_virtualization(void) { return r; } -#endif // 0 static int userns_has_mapping(const char *name) { _cleanup_fclose_ FILE *f = NULL; @@ -499,7 +498,7 @@ static int userns_has_mapping(const char *name) { f = fopen(name, "re"); if (!f) { log_debug_errno(errno, "Failed to open %s: %m", name); - return errno == -ENOENT ? false : -errno; + return errno == ENOENT ? false : -errno; } n = getline(&buf, &n_allocated, f); @@ -557,20 +556,35 @@ int running_in_userns(void) { log_debug("/proc/self/setgroups contains \"%s\", %s user namespace", line, r ? "in" : "not in"); return r; } +#endif // 0 int running_in_chroot(void) { - int ret; + _cleanup_free_ char *self_mnt = NULL, *pid1_mnt = NULL; + int r; + + /* Try to detect whether we are running in a chroot() environment. Specifically, check whether we have a + * different root directory than PID 1, even though we live in the same mount namespace as it. */ #if 0 /// elogind does not allow to ignore chroots, we are never init! if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0) return 0; #endif // 0 - ret = files_same("/proc/1/root", "/"); - if (ret < 0) - return ret; + r = files_same("/proc/1/root", "/"); + if (r < 0) + return r; + if (r > 0) + return 0; + + r = readlink_malloc("/proc/self/ns/mnt", &self_mnt); + if (r < 0) + return r; + + r = readlink_malloc("/proc/1/ns/mnt", &pid1_mnt); + if (r < 0) + return r; - return ret == 0; + return streq(self_mnt, pid1_mnt); /* Only if we live in the same namespace! */ } static const char *const virtualization_table[_VIRTUALIZATION_MAX] = {