summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/basic/virt.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/basic/virt.c b/src/basic/virt.c
index 710f0372ea..9a0b5a28d1 100644
--- a/src/basic/virt.c
+++ b/src/basic/virt.c
@@ -867,10 +867,26 @@ int running_in_userns(void) {
int running_in_chroot(void) {
int r;
+ /* If we're PID1, /proc may not be mounted (and most likely we're not in a chroot). But PID1 will
+ * mount /proc, so all other programs can assume that if /proc is *not* available, we're in some
+ * chroot. */
+
if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
return 0;
+ if (getpid_cached() == 1)
+ return false; /* We're PID 1, we can't be in a chroot. */
+
r = files_same("/proc/1/root", "/", 0);
+ if (r == -ENOENT) {
+ r = proc_mounted();
+ if (r == 0) {
+ log_debug("/proc is not mounted, assuming we're in a chroot.");
+ return 1;
+ }
+ if (r > 0) /* If we have fake /proc/, we can't do the check properly. */
+ return -ENOSYS;
+ }
if (r < 0)
return r;