chiark / gitweb /
tree-wide: remove Lennart's copyright lines
[elogind.git] / src / basic / virt.c
index 9290dff3be8184499ed07ddfe1f3dc802264a854..ac42947a4b16b97054b8a857a885a9c1b050648a 100644 (file)
@@ -1,22 +1,4 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2011 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
 
 #if defined(__i386__) || defined(__x86_64__)
 //#include <cpuid.h>
@@ -70,7 +52,7 @@ static int detect_vm_cpuid(void) {
         if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0)
                 return VIRTUALIZATION_NONE;
 
-        hypervisor = !!(ecx & 0x80000000U);
+        hypervisor = ecx & 0x80000000U;
 
         if (hypervisor) {
                 union {
@@ -325,8 +307,8 @@ static int detect_vm_zvm(void) {
 /* Returns a short identifier for the various VM implementations */
 int detect_vm(void) {
         static thread_local int cached_found = _VIRTUALIZATION_INVALID;
-        int r, dmi;
         bool other = false;
+        int r, dmi;
 
         if (cached_found >= 0)
                 return cached_found;
@@ -350,68 +332,58 @@ int detect_vm(void) {
         r = detect_vm_cpuid();
         if (r < 0)
                 return r;
-        if (r != VIRTUALIZATION_NONE) {
-                if (r == VIRTUALIZATION_VM_OTHER)
-                        other = true;
-                else
-                        goto finish;
-        }
+        if (r == VIRTUALIZATION_VM_OTHER)
+                other = true;
+        else if (r != VIRTUALIZATION_NONE)
+                goto finish;
 
-        r = dmi;
-        if (r < 0)
-                return r;
-        if (r != VIRTUALIZATION_NONE) {
-                if (r == VIRTUALIZATION_VM_OTHER)
-                        other = true;
-                else
-                        goto finish;
+        /* Now, let's get back to DMI */
+        if (dmi < 0)
+                return dmi;
+        if (dmi == VIRTUALIZATION_VM_OTHER)
+                other = true;
+        else if (dmi != VIRTUALIZATION_NONE) {
+                r = dmi;
+                goto finish;
         }
 
         /* x86 xen will most likely be detected by cpuid. If not (most likely
          * because we're not an x86 guest), then we should try the /proc/xen
          * directory next. If that's not found, then we check for the high-level
          * hypervisor sysfs file.
-        */
+         */
 
         r = detect_vm_xen();
         if (r < 0)
                 return r;
-        if (r != VIRTUALIZATION_NONE) {
-                if (r == VIRTUALIZATION_VM_OTHER)
-                        other = true;
-                else
-                        goto finish;
-        }
+        if (r == VIRTUALIZATION_VM_OTHER)
+                other = true;
+        else if (r != VIRTUALIZATION_NONE)
+                goto finish;
 
         r = detect_vm_hypervisor();
         if (r < 0)
                 return r;
-        if (r != VIRTUALIZATION_NONE) {
-                if (r == VIRTUALIZATION_VM_OTHER)
-                        other = true;
-                else
-                        goto finish;
-        }
+        if (r == VIRTUALIZATION_VM_OTHER)
+                other = true;
+        else if (r != VIRTUALIZATION_NONE)
+                goto finish;
 
         r = detect_vm_device_tree();
         if (r < 0)
                 return r;
-        if (r != VIRTUALIZATION_NONE) {
-                if (r == VIRTUALIZATION_VM_OTHER)
-                        other = true;
-                else
-                        goto finish;
-        }
+        if (r == VIRTUALIZATION_VM_OTHER)
+                other = true;
+        else if (r != VIRTUALIZATION_NONE)
+                goto finish;
 
         r = detect_vm_uml();
         if (r < 0)
                 return r;
-        if (r != VIRTUALIZATION_NONE) {
-                if (r == VIRTUALIZATION_VM_OTHER)
-                        other = true;
-                else
-                        goto finish;
-        }
+        if (r == VIRTUALIZATION_VM_OTHER)
+                other = true;
+        else if (r != VIRTUALIZATION_NONE)
+                goto finish;
 
         r = detect_vm_zvm();
         if (r < 0)
@@ -422,10 +394,12 @@ finish:
          * In order to detect the Dom0 as not virtualization we need to
          * double-check it */
         if (r == VIRTUALIZATION_XEN) {
-                int ret = detect_vm_xen_dom0();
-                if (ret < 0)
-                        return ret;
-                if (ret > 0)
+                int dom0;
+
+                dom0 = detect_vm_xen_dom0();
+                if (dom0 < 0)
+                        return dom0;
+                if (dom0 > 0)
                         r = VIRTUALIZATION_NONE;
         } else if (r == VIRTUALIZATION_NONE && other)
                 r = VIRTUALIZATION_VM_OTHER;