Sophie

Sophie

distrib > Fedora > 17 > i386 > media > updates-src > by-pkgid > ab4b662b9827b6375ffd451bf4abd615 > files > 472

systemd-44-24.fc17.src.rpm

From 32cc12e26a23f62d4151032ccf2d8f516235ccfe Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Tue, 10 Jul 2012 18:46:26 +0200
Subject: [PATCH] util: add extra safety check to in_initrd()

initrds can only be on tmpfs or ramfs, so check for that
(cherry picked from commit 825c6fe5eb362437aa46faa52b683a62eede9a13)

Conflicts:
	TODO
---
 src/shared/util.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/shared/util.c b/src/shared/util.c
index 1b2fa15..62d502b 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -54,6 +54,8 @@
 #include <glob.h>
 #include <grp.h>
 #include <sys/mman.h>
+#include <sys/vfs.h>
+#include <linux/magic.h>
 
 #include "macro.h"
 #include "util.h"
@@ -5738,9 +5740,24 @@ bool is_valid_documentation_url(const char *url) {
 
 bool in_initrd(void) {
         static int saved = -1;
+        struct statfs s;
 
-        if (saved < 0)
-                saved = access("/etc/initrd-release", F_OK) >= 0;
+        if (saved >= 0)
+                return saved;
+
+        /* We make two checks here:
+         *
+         * 1. the flag file /etc/initrd-release must exist
+         * 2. the root file system must be a memory file system
+         *
+         * The second check is extra paranoia, since misdetecting an
+         * initrd can have bad bad consequences due the initrd
+         * emptying when transititioning to the main systemd.
+         */
+
+        saved = access("/etc/initrd-release", F_OK) >= 0 &&
+                statfs("/", &s) >= 0 &&
+                (s.f_type == TMPFS_MAGIC || s.f_type == RAMFS_MAGIC);
 
         return saved;
 }