daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
ff6046
From 886e5b028953404f2d924b561c0689d3e50dbbf4 Mon Sep 17 00:00:00 2001
ff6046
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
ff6046
Date: Thu, 13 Sep 2018 09:24:36 +0200
ff6046
Subject: [PATCH] detect-virt: do not try to read all of /proc/cpuinfo
ff6046
ff6046
Quoting https://github.com/systemd/systemd/issues/10074:
ff6046
> detect_vm_uml() reads /proc/cpuinfo with read_full_file()
ff6046
> read_full_file() has a file max limit size of READ_FULL_BYTES_MAX=(4U*1024U*1024U)
ff6046
> Unfortunately, the size of my /proc/cpuinfo is bigger, approximately:
ff6046
> echo $(( 4* $(cat /proc/cpuinfo | wc -c)))
ff6046
> 9918072
ff6046
> This causes read_full_file() to fail and the Condition test fallout.
ff6046
ff6046
Let's just read line by line until we find an intersting line. This also
ff6046
helps if not running under UML, because we avoid reading as much data.
ff6046
ff6046
(cherry picked from commit 6058516a14ada1748313af6783f5b4e7e3006654)
ff6046
ff6046
Resolves: #1631532
ff6046
---
ff6046
 src/basic/virt.c | 38 ++++++++++++++++++++++++++++----------
ff6046
 1 file changed, 28 insertions(+), 10 deletions(-)
ff6046
ff6046
diff --git a/src/basic/virt.c b/src/basic/virt.c
ff6046
index d347732bb3..e05b3e6d99 100644
ff6046
--- a/src/basic/virt.c
ff6046
+++ b/src/basic/virt.c
ff6046
@@ -11,6 +11,7 @@
ff6046
 
ff6046
 #include "alloc-util.h"
ff6046
 #include "dirent-util.h"
ff6046
+#include "def.h"
ff6046
 #include "env-util.h"
ff6046
 #include "fd-util.h"
ff6046
 #include "fileio.h"
ff6046
@@ -259,21 +260,38 @@ static int detect_vm_hypervisor(void) {
ff6046
 }
ff6046
 
ff6046
 static int detect_vm_uml(void) {
ff6046
-        _cleanup_free_ char *cpuinfo_contents = NULL;
ff6046
+        _cleanup_fclose_ FILE *f = NULL;
ff6046
         int r;
ff6046
 
ff6046
         /* Detect User-Mode Linux by reading /proc/cpuinfo */
ff6046
-        r = read_full_file("/proc/cpuinfo", &cpuinfo_contents, NULL);
ff6046
-        if (r == -ENOENT) {
ff6046
-                log_debug("/proc/cpuinfo not found, assuming no UML virtualization.");
ff6046
-                return VIRTUALIZATION_NONE;
ff6046
+        f = fopen("/proc/cpuinfo", "re");
ff6046
+        if (!f) {
ff6046
+                if (errno == ENOENT) {
ff6046
+                        log_debug("/proc/cpuinfo not found, assuming no UML virtualization.");
ff6046
+                        return VIRTUALIZATION_NONE;
ff6046
+                }
ff6046
+                return -errno;
ff6046
         }
ff6046
-        if (r < 0)
ff6046
-                return r;
ff6046
 
ff6046
-        if (strstr(cpuinfo_contents, "\nvendor_id\t: User Mode Linux\n")) {
ff6046
-                log_debug("UML virtualization found in /proc/cpuinfo");
ff6046
-                return VIRTUALIZATION_UML;
ff6046
+        for (;;) {
ff6046
+                _cleanup_free_ char *line = NULL;
ff6046
+                const char *t;
ff6046
+
ff6046
+                r = read_line(f, LONG_LINE_MAX, &line);
ff6046
+                if (r < 0)
ff6046
+                        return r;
ff6046
+                if (r == 0)
ff6046
+                        break;
ff6046
+
ff6046
+                t = startswith(line, "vendor_id\t: ");
ff6046
+                if (t) {
ff6046
+                        if (startswith(t, "User Mode Linux")) {
ff6046
+                                log_debug("UML virtualization found in /proc/cpuinfo");
ff6046
+                                return VIRTUALIZATION_UML;
ff6046
+                        }
ff6046
+
ff6046
+                        break;
ff6046
+                }
ff6046
         }
ff6046
 
ff6046
         log_debug("UML virtualization not found in /proc/cpuinfo.");