LCOV - code coverage report
Current view: top level - basic - virt.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 142 280 50.7 %
Date: 2019-08-22 15:41:25 Functions: 14 16 87.5 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #if defined(__i386__) || defined(__x86_64__)
       4             : #include <cpuid.h>
       5             : #endif
       6             : #include <errno.h>
       7             : #include <stdint.h>
       8             : #include <stdlib.h>
       9             : #include <string.h>
      10             : #include <unistd.h>
      11             : 
      12             : #include "alloc-util.h"
      13             : #include "dirent-util.h"
      14             : #include "env-util.h"
      15             : #include "fd-util.h"
      16             : #include "fileio.h"
      17             : #include "macro.h"
      18             : #include "process-util.h"
      19             : #include "stat-util.h"
      20             : #include "string-table.h"
      21             : #include "string-util.h"
      22             : #include "virt.h"
      23             : 
      24           3 : static int detect_vm_cpuid(void) {
      25             : 
      26             :         /* CPUID is an x86 specific interface. */
      27             : #if defined(__i386__) || defined(__x86_64__)
      28             : 
      29             :         static const struct {
      30             :                 const char *cpuid;
      31             :                 int id;
      32             :         } cpuid_vendor_table[] = {
      33             :                 { "XenVMMXenVMM", VIRTUALIZATION_XEN       },
      34             :                 { "KVMKVMKVM",    VIRTUALIZATION_KVM       },
      35             :                 { "TCGTCGTCGTCG", VIRTUALIZATION_QEMU      },
      36             :                 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
      37             :                 { "VMwareVMware", VIRTUALIZATION_VMWARE    },
      38             :                 /* https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs */
      39             :                 { "Microsoft Hv", VIRTUALIZATION_MICROSOFT },
      40             :                 /* https://wiki.freebsd.org/bhyve */
      41             :                 { "bhyve bhyve ", VIRTUALIZATION_BHYVE     },
      42             :                 { "QNXQVMBSQG",   VIRTUALIZATION_QNX       },
      43             :                 /* https://projectacrn.org */
      44             :                 { "ACRNACRNACRN", VIRTUALIZATION_ACRN      },
      45             :         };
      46             : 
      47             :         uint32_t eax, ebx, ecx, edx;
      48             :         bool hypervisor;
      49             : 
      50             :         /* http://lwn.net/Articles/301888/ */
      51             : 
      52             :         /* First detect whether there is a hypervisor */
      53           3 :         if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0)
      54           0 :                 return VIRTUALIZATION_NONE;
      55             : 
      56           3 :         hypervisor = ecx & 0x80000000U;
      57             : 
      58           3 :         if (hypervisor) {
      59             :                 union {
      60             :                         uint32_t sig32[3];
      61             :                         char text[13];
      62           0 :                 } sig = {};
      63             :                 unsigned j;
      64             : 
      65             :                 /* There is a hypervisor, see what it is */
      66           0 :                 __cpuid(0x40000000U, eax, ebx, ecx, edx);
      67             : 
      68           0 :                 sig.sig32[0] = ebx;
      69           0 :                 sig.sig32[1] = ecx;
      70           0 :                 sig.sig32[2] = edx;
      71             : 
      72           0 :                 log_debug("Virtualization found, CPUID=%s", sig.text);
      73             : 
      74           0 :                 for (j = 0; j < ELEMENTSOF(cpuid_vendor_table); j ++)
      75           0 :                         if (streq(sig.text, cpuid_vendor_table[j].cpuid))
      76           0 :                                 return cpuid_vendor_table[j].id;
      77             : 
      78           0 :                 return VIRTUALIZATION_VM_OTHER;
      79             :         }
      80             : #endif
      81           3 :         log_debug("No virtualization found in CPUID");
      82             : 
      83           3 :         return VIRTUALIZATION_NONE;
      84             : }
      85             : 
      86           3 : static int detect_vm_device_tree(void) {
      87             : #if defined(__arm__) || defined(__aarch64__) || defined(__powerpc__) || defined(__powerpc64__)
      88             :         _cleanup_free_ char *hvtype = NULL;
      89             :         int r;
      90             : 
      91             :         r = read_one_line_file("/proc/device-tree/hypervisor/compatible", &hvtype);
      92             :         if (r == -ENOENT) {
      93             :                 _cleanup_closedir_ DIR *dir = NULL;
      94             :                 struct dirent *dent;
      95             : 
      96             :                 dir = opendir("/proc/device-tree");
      97             :                 if (!dir) {
      98             :                         if (errno == ENOENT) {
      99             :                                 log_debug_errno(errno, "/proc/device-tree: %m");
     100             :                                 return VIRTUALIZATION_NONE;
     101             :                         }
     102             :                         return -errno;
     103             :                 }
     104             : 
     105             :                 FOREACH_DIRENT(dent, dir, return -errno)
     106             :                         if (strstr(dent->d_name, "fw-cfg")) {
     107             :                                 log_debug("Virtualization QEMU: \"fw-cfg\" present in /proc/device-tree/%s", dent->d_name);
     108             :                                 return VIRTUALIZATION_QEMU;
     109             :                         }
     110             : 
     111             :                 log_debug("No virtualization found in /proc/device-tree/*");
     112             :                 return VIRTUALIZATION_NONE;
     113             :         } else if (r < 0)
     114             :                 return r;
     115             : 
     116             :         log_debug("Virtualization %s found in /proc/device-tree/hypervisor/compatible", hvtype);
     117             :         if (streq(hvtype, "linux,kvm"))
     118             :                 return VIRTUALIZATION_KVM;
     119             :         else if (strstr(hvtype, "xen"))
     120             :                 return VIRTUALIZATION_XEN;
     121             :         else
     122             :                 return VIRTUALIZATION_VM_OTHER;
     123             : #else
     124           3 :         log_debug("This platform does not support /proc/device-tree");
     125           3 :         return VIRTUALIZATION_NONE;
     126             : #endif
     127             : }
     128             : 
     129           3 : static int detect_vm_dmi(void) {
     130             : #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
     131             : 
     132             :         static const char *const dmi_vendors[] = {
     133             :                 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
     134             :                 "/sys/class/dmi/id/sys_vendor",
     135             :                 "/sys/class/dmi/id/board_vendor",
     136             :                 "/sys/class/dmi/id/bios_vendor"
     137             :         };
     138             : 
     139             :         static const struct {
     140             :                 const char *vendor;
     141             :                 int id;
     142             :         } dmi_vendor_table[] = {
     143             :                 { "KVM",           VIRTUALIZATION_KVM       },
     144             :                 { "QEMU",          VIRTUALIZATION_QEMU      },
     145             :                 /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
     146             :                 { "VMware",        VIRTUALIZATION_VMWARE    },
     147             :                 { "VMW",           VIRTUALIZATION_VMWARE    },
     148             :                 { "innotek GmbH",  VIRTUALIZATION_ORACLE    },
     149             :                 { "Xen",           VIRTUALIZATION_XEN       },
     150             :                 { "Bochs",         VIRTUALIZATION_BOCHS     },
     151             :                 { "Parallels",     VIRTUALIZATION_PARALLELS },
     152             :                 /* https://wiki.freebsd.org/bhyve */
     153             :                 { "BHYVE",         VIRTUALIZATION_BHYVE     },
     154             :         };
     155             :         unsigned i;
     156             :         int r;
     157             : 
     158          15 :         for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
     159          12 :                 _cleanup_free_ char *s = NULL;
     160             :                 unsigned j;
     161             : 
     162          12 :                 r = read_one_line_file(dmi_vendors[i], &s);
     163          12 :                 if (r < 0) {
     164           0 :                         if (r == -ENOENT)
     165           0 :                                 continue;
     166             : 
     167           0 :                         return r;
     168             :                 }
     169             : 
     170         120 :                 for (j = 0; j < ELEMENTSOF(dmi_vendor_table); j++)
     171         108 :                         if (startswith(s, dmi_vendor_table[j].vendor)) {
     172           0 :                                 log_debug("Virtualization %s found in DMI (%s)", s, dmi_vendors[i]);
     173           0 :                                 return dmi_vendor_table[j].id;
     174             :                         }
     175             :         }
     176             : #endif
     177             : 
     178           3 :         log_debug("No virtualization found in DMI");
     179             : 
     180           3 :         return VIRTUALIZATION_NONE;
     181             : }
     182             : 
     183           3 : static int detect_vm_xen(void) {
     184             : 
     185             :         /* Check for Dom0 will be executed later in detect_vm_xen_dom0
     186             :            The presence of /proc/xen indicates some form of a Xen domain */
     187           3 :         if (access("/proc/xen", F_OK) < 0) {
     188           3 :                 log_debug("Virtualization XEN not found, /proc/xen does not exist");
     189           3 :                 return VIRTUALIZATION_NONE;
     190             :         }
     191             : 
     192           0 :         log_debug("Virtualization XEN found (/proc/xen exists)");
     193           0 :         return VIRTUALIZATION_XEN;
     194             : }
     195             : 
     196             : #define XENFEAT_dom0 11 /* xen/include/public/features.h */
     197             : #define PATH_FEATURES "/sys/hypervisor/properties/features"
     198             : /* Returns -errno, or 0 for domU, or 1 for dom0 */
     199           0 : static int detect_vm_xen_dom0(void) {
     200           0 :         _cleanup_free_ char *domcap = NULL;
     201             :         int r;
     202             : 
     203           0 :         r = read_one_line_file(PATH_FEATURES, &domcap);
     204           0 :         if (r < 0 && r != -ENOENT)
     205           0 :                 return r;
     206           0 :         if (r >= 0) {
     207             :                 unsigned long features;
     208             : 
     209             :                 /* Here, we need to use sscanf() instead of safe_atoul()
     210             :                  * as the string lacks the leading "0x". */
     211           0 :                 r = sscanf(domcap, "%lx", &features);
     212           0 :                 if (r == 1) {
     213           0 :                         r = !!(features & (1U << XENFEAT_dom0));
     214           0 :                         log_debug("Virtualization XEN, found %s with value %08lx, "
     215             :                                   "XENFEAT_dom0 (indicating the 'hardware domain') is%s set.",
     216             :                                   PATH_FEATURES, features, r ? "" : " not");
     217           0 :                         return r;
     218             :                 }
     219           0 :                 log_debug("Virtualization XEN, found %s, unhandled content '%s'",
     220             :                           PATH_FEATURES, domcap);
     221             :         }
     222             : 
     223           0 :         r = read_one_line_file("/proc/xen/capabilities", &domcap);
     224           0 :         if (r == -ENOENT) {
     225           0 :                 log_debug("Virtualization XEN because /proc/xen/capabilities does not exist");
     226           0 :                 return 0;
     227             :         }
     228           0 :         if (r < 0)
     229           0 :                 return r;
     230             : 
     231           0 :         for (const char *i = domcap;;) {
     232           0 :                 _cleanup_free_ char *cap = NULL;
     233             : 
     234           0 :                 r = extract_first_word(&i, &cap, ",", 0);
     235           0 :                 if (r < 0)
     236           0 :                         return r;
     237           0 :                 if (r == 0) {
     238           0 :                         log_debug("Virtualization XEN DomU found (/proc/xen/capabilities)");
     239           0 :                         return 0;
     240             :                 }
     241             : 
     242           0 :                 if (streq(cap, "control_d")) {
     243           0 :                         log_debug("Virtualization XEN Dom0 ignored (/proc/xen/capabilities)");
     244           0 :                         return 1;
     245             :                 }
     246             :         }
     247             : }
     248             : 
     249           3 : static int detect_vm_hypervisor(void) {
     250           3 :         _cleanup_free_ char *hvtype = NULL;
     251             :         int r;
     252             : 
     253           3 :         r = read_one_line_file("/sys/hypervisor/type", &hvtype);
     254           3 :         if (r == -ENOENT)
     255           3 :                 return VIRTUALIZATION_NONE;
     256           0 :         if (r < 0)
     257           0 :                 return r;
     258             : 
     259           0 :         log_debug("Virtualization %s found in /sys/hypervisor/type", hvtype);
     260             : 
     261           0 :         if (streq(hvtype, "xen"))
     262           0 :                 return VIRTUALIZATION_XEN;
     263             :         else
     264           0 :                 return VIRTUALIZATION_VM_OTHER;
     265             : }
     266             : 
     267           3 : static int detect_vm_uml(void) {
     268           3 :         _cleanup_fclose_ FILE *f = NULL;
     269             :         int r;
     270             : 
     271             :         /* Detect User-Mode Linux by reading /proc/cpuinfo */
     272           3 :         f = fopen("/proc/cpuinfo", "re");
     273           3 :         if (!f) {
     274           0 :                 if (errno == ENOENT) {
     275           0 :                         log_debug("/proc/cpuinfo not found, assuming no UML virtualization.");
     276           0 :                         return VIRTUALIZATION_NONE;
     277             :                 }
     278           0 :                 return -errno;
     279             :         }
     280             : 
     281           3 :         for (;;) {
     282           6 :                 _cleanup_free_ char *line = NULL;
     283             :                 const char *t;
     284             : 
     285           6 :                 r = read_line(f, LONG_LINE_MAX, &line);
     286           6 :                 if (r < 0)
     287           0 :                         return r;
     288           6 :                 if (r == 0)
     289           0 :                         break;
     290             : 
     291           6 :                 t = startswith(line, "vendor_id\t: ");
     292           6 :                 if (t) {
     293           3 :                         if (startswith(t, "User Mode Linux")) {
     294           0 :                                 log_debug("UML virtualization found in /proc/cpuinfo");
     295           0 :                                 return VIRTUALIZATION_UML;
     296             :                         }
     297             : 
     298           3 :                         break;
     299             :                 }
     300             :         }
     301             : 
     302           3 :         log_debug("UML virtualization not found in /proc/cpuinfo.");
     303           3 :         return VIRTUALIZATION_NONE;
     304             : }
     305             : 
     306           3 : static int detect_vm_zvm(void) {
     307             : 
     308             : #if defined(__s390__)
     309             :         _cleanup_free_ char *t = NULL;
     310             :         int r;
     311             : 
     312             :         r = get_proc_field("/proc/sysinfo", "VM00 Control Program", WHITESPACE, &t);
     313             :         if (r == -ENOENT)
     314             :                 return VIRTUALIZATION_NONE;
     315             :         if (r < 0)
     316             :                 return r;
     317             : 
     318             :         log_debug("Virtualization %s found in /proc/sysinfo", t);
     319             :         if (streq(t, "z/VM"))
     320             :                 return VIRTUALIZATION_ZVM;
     321             :         else
     322             :                 return VIRTUALIZATION_KVM;
     323             : #else
     324           3 :         log_debug("This platform does not support /proc/sysinfo");
     325           3 :         return VIRTUALIZATION_NONE;
     326             : #endif
     327             : }
     328             : 
     329             : /* Returns a short identifier for the various VM implementations */
     330          18 : int detect_vm(void) {
     331             :         static thread_local int cached_found = _VIRTUALIZATION_INVALID;
     332          18 :         bool other = false;
     333             :         int r, dmi;
     334             : 
     335          18 :         if (cached_found >= 0)
     336          15 :                 return cached_found;
     337             : 
     338             :         /* We have to use the correct order here:
     339             :          *
     340             :          * → First, try to detect Oracle Virtualbox, even if it uses KVM, as well as Xen even if it cloaks as Microsoft
     341             :          *   Hyper-V.
     342             :          *
     343             :          * → Second, try to detect from CPUID, this will report KVM for whatever software is used even if info in DMI is
     344             :          *   overwritten.
     345             :          *
     346             :          * → Third, try to detect from DMI. */
     347             : 
     348           3 :         dmi = detect_vm_dmi();
     349           3 :         if (IN_SET(dmi, VIRTUALIZATION_ORACLE, VIRTUALIZATION_XEN)) {
     350           0 :                 r = dmi;
     351           0 :                 goto finish;
     352             :         }
     353             : 
     354           3 :         r = detect_vm_cpuid();
     355           3 :         if (r < 0)
     356           0 :                 return r;
     357           3 :         if (r == VIRTUALIZATION_VM_OTHER)
     358           0 :                 other = true;
     359           3 :         else if (r != VIRTUALIZATION_NONE)
     360           0 :                 goto finish;
     361             : 
     362             :         /* Now, let's get back to DMI */
     363           3 :         if (dmi < 0)
     364           0 :                 return dmi;
     365           3 :         if (dmi == VIRTUALIZATION_VM_OTHER)
     366           0 :                 other = true;
     367           3 :         else if (dmi != VIRTUALIZATION_NONE) {
     368           0 :                 r = dmi;
     369           0 :                 goto finish;
     370             :         }
     371             : 
     372             :         /* x86 xen will most likely be detected by cpuid. If not (most likely
     373             :          * because we're not an x86 guest), then we should try the /proc/xen
     374             :          * directory next. If that's not found, then we check for the high-level
     375             :          * hypervisor sysfs file.
     376             :          */
     377             : 
     378           3 :         r = detect_vm_xen();
     379           3 :         if (r < 0)
     380           0 :                 return r;
     381           3 :         if (r == VIRTUALIZATION_VM_OTHER)
     382           0 :                 other = true;
     383           3 :         else if (r != VIRTUALIZATION_NONE)
     384           0 :                 goto finish;
     385             : 
     386           3 :         r = detect_vm_hypervisor();
     387           3 :         if (r < 0)
     388           0 :                 return r;
     389           3 :         if (r == VIRTUALIZATION_VM_OTHER)
     390           0 :                 other = true;
     391           3 :         else if (r != VIRTUALIZATION_NONE)
     392           0 :                 goto finish;
     393             : 
     394           3 :         r = detect_vm_device_tree();
     395           3 :         if (r < 0)
     396           0 :                 return r;
     397           3 :         if (r == VIRTUALIZATION_VM_OTHER)
     398           0 :                 other = true;
     399           3 :         else if (r != VIRTUALIZATION_NONE)
     400           0 :                 goto finish;
     401             : 
     402           3 :         r = detect_vm_uml();
     403           3 :         if (r < 0)
     404           0 :                 return r;
     405           3 :         if (r == VIRTUALIZATION_VM_OTHER)
     406           0 :                 other = true;
     407           3 :         else if (r != VIRTUALIZATION_NONE)
     408           0 :                 goto finish;
     409             : 
     410           3 :         r = detect_vm_zvm();
     411           3 :         if (r < 0)
     412           0 :                 return r;
     413             : 
     414           3 : finish:
     415             :         /* x86 xen Dom0 is detected as XEN in hypervisor and maybe others.
     416             :          * In order to detect the Dom0 as not virtualization we need to
     417             :          * double-check it */
     418           3 :         if (r == VIRTUALIZATION_XEN) {
     419             :                 int dom0;
     420             : 
     421           0 :                 dom0 = detect_vm_xen_dom0();
     422           0 :                 if (dom0 < 0)
     423           0 :                         return dom0;
     424           0 :                 if (dom0 > 0)
     425           0 :                         r = VIRTUALIZATION_NONE;
     426           3 :         } else if (r == VIRTUALIZATION_NONE && other)
     427           0 :                 r = VIRTUALIZATION_VM_OTHER;
     428             : 
     429           3 :         cached_found = r;
     430           3 :         log_debug("Found VM virtualization %s", virtualization_to_string(r));
     431           3 :         return r;
     432             : }
     433             : 
     434         487 : int detect_container(void) {
     435             :         static const struct {
     436             :                 const char *value;
     437             :                 int id;
     438             :         } value_table[] = {
     439             :                 { "lxc",            VIRTUALIZATION_LXC            },
     440             :                 { "lxc-libvirt",    VIRTUALIZATION_LXC_LIBVIRT    },
     441             :                 { "systemd-nspawn", VIRTUALIZATION_SYSTEMD_NSPAWN },
     442             :                 { "docker",         VIRTUALIZATION_DOCKER         },
     443             :                 { "podman",         VIRTUALIZATION_PODMAN         },
     444             :                 { "rkt",            VIRTUALIZATION_RKT            },
     445             :                 { "wsl",            VIRTUALIZATION_WSL            },
     446             :         };
     447             : 
     448             :         static thread_local int cached_found = _VIRTUALIZATION_INVALID;
     449         487 :         _cleanup_free_ char *m = NULL;
     450         487 :         _cleanup_free_ char *o = NULL;
     451         487 :         const char *e = NULL;
     452             :         unsigned j;
     453             :         int r;
     454             : 
     455         487 :         if (cached_found >= 0)
     456          94 :                 return cached_found;
     457             : 
     458             :         /* /proc/vz exists in container and outside of the container, /proc/bc only outside of the container. */
     459         393 :         if (access("/proc/vz", F_OK) >= 0 &&
     460           0 :             access("/proc/bc", F_OK) < 0) {
     461           0 :                 r = VIRTUALIZATION_OPENVZ;
     462           0 :                 goto finish;
     463             :         }
     464             : 
     465             :         /* "Official" way of detecting WSL https://github.com/Microsoft/WSL/issues/423#issuecomment-221627364 */
     466         393 :         r = read_one_line_file("/proc/sys/kernel/osrelease", &o);
     467         393 :         if (r >= 0) {
     468         393 :                 if (strstr(o, "Microsoft") || strstr(o, "WSL")) {
     469           0 :                         r = VIRTUALIZATION_WSL;
     470           0 :                         goto finish;
     471             :                 }
     472             :         }
     473             : 
     474         393 :         if (getpid_cached() == 1) {
     475             :                 /* If we are PID 1 we can just check our own environment variable, and that's authoritative.
     476             :                  * We distinguish three cases:
     477             :                  * - the variable is not defined → we jump to other checks
     478             :                  * - the variable is defined to an empty value → we are not in a container
     479             :                  * - anything else → some container, either one of the known ones or "container-other"
     480             :                  */
     481           0 :                 e = getenv("container");
     482           0 :                 if (!e)
     483           0 :                         goto check_sched;
     484           0 :                 if (isempty(e)) {
     485           0 :                         r = VIRTUALIZATION_NONE;
     486           0 :                         goto finish;
     487             :                 }
     488             : 
     489           0 :                 goto translate_name;
     490             :         }
     491             : 
     492             :         /* Otherwise, PID 1 might have dropped this information into a file in /run. This is better than accessing
     493             :          * /proc/1/environ, since we don't need CAP_SYS_PTRACE for that. */
     494         393 :         r = read_one_line_file("/run/systemd/container", &m);
     495         393 :         if (r > 0) {
     496           0 :                 e = m;
     497           0 :                 goto translate_name;
     498             :         }
     499         393 :         if (!IN_SET(r, -ENOENT, 0))
     500           0 :                 return log_debug_errno(r, "Failed to read /run/systemd/container: %m");
     501             : 
     502             :         /* Fallback for cases where PID 1 was not systemd (for example, cases where init=/bin/sh is used. */
     503         393 :         r = getenv_for_pid(1, "container", &m);
     504         393 :         if (r > 0) {
     505           0 :                 e = m;
     506           0 :                 goto translate_name;
     507             :         }
     508         393 :         if (r < 0) /* This only works if we have CAP_SYS_PTRACE, hence let's better ignore failures here */
     509         393 :                 log_debug_errno(r, "Failed to read $container of PID 1, ignoring: %m");
     510             : 
     511             :         /* Interestingly /proc/1/sched actually shows the host's PID for what we see as PID 1. If the PID
     512             :          * shown there is not 1, we know we are in a PID namespace and hence a container. */
     513           0 :  check_sched:
     514         393 :         r = read_one_line_file("/proc/1/sched", &m);
     515         393 :         if (r >= 0) {
     516             :                 const char *t;
     517             : 
     518         393 :                 t = strrchr(m, '(');
     519         393 :                 if (!t)
     520           0 :                         return -EIO;
     521             : 
     522         393 :                 if (!startswith(t, "(1,")) {
     523           0 :                         r = VIRTUALIZATION_CONTAINER_OTHER;
     524           0 :                         goto finish;
     525             :                 }
     526           0 :         } else if (r != -ENOENT)
     527           0 :                 return r;
     528             : 
     529             :         /* If that didn't work, give up, assume no container manager. */
     530         393 :         r = VIRTUALIZATION_NONE;
     531         393 :         goto finish;
     532             : 
     533           0 : translate_name:
     534           0 :         for (j = 0; j < ELEMENTSOF(value_table); j++)
     535           0 :                 if (streq(e, value_table[j].value)) {
     536           0 :                         r = value_table[j].id;
     537           0 :                         goto finish;
     538             :                 }
     539             : 
     540           0 :         r = VIRTUALIZATION_CONTAINER_OTHER;
     541             : 
     542         393 : finish:
     543         393 :         log_debug("Found container virtualization %s.", virtualization_to_string(r));
     544         393 :         cached_found = r;
     545         393 :         return r;
     546             : }
     547             : 
     548          17 : int detect_virtualization(void) {
     549             :         int r;
     550             : 
     551          17 :         r = detect_container();
     552          17 :         if (r == 0)
     553          17 :                 r = detect_vm();
     554             : 
     555          17 :         return r;
     556             : }
     557             : 
     558           4 : static int userns_has_mapping(const char *name) {
     559           4 :         _cleanup_fclose_ FILE *f = NULL;
     560           4 :         _cleanup_free_ char *buf = NULL;
     561           4 :         size_t n_allocated = 0;
     562             :         ssize_t n;
     563             :         uint32_t a, b, c;
     564             :         int r;
     565             : 
     566           4 :         f = fopen(name, "re");
     567           4 :         if (!f) {
     568           0 :                 log_debug_errno(errno, "Failed to open %s: %m", name);
     569           0 :                 return errno == ENOENT ? false : -errno;
     570             :         }
     571             : 
     572           4 :         n = getline(&buf, &n_allocated, f);
     573           4 :         if (n < 0) {
     574           0 :                 if (feof(f)) {
     575           0 :                         log_debug("%s is empty, we're in an uninitialized user namespace", name);
     576           0 :                         return true;
     577             :                 }
     578             : 
     579           0 :                 return log_debug_errno(errno, "Failed to read %s: %m", name);
     580             :         }
     581             : 
     582           4 :         r = sscanf(buf, "%"PRIu32" %"PRIu32" %"PRIu32, &a, &b, &c);
     583           4 :         if (r < 3)
     584           0 :                 return log_debug_errno(errno, "Failed to parse %s: %m", name);
     585             : 
     586           4 :         if (a == 0 && b == 0 && c == UINT32_MAX) {
     587             :                 /* The kernel calls mappings_overlap() and does not allow overlaps */
     588           4 :                 log_debug("%s has a full 1:1 mapping", name);
     589           4 :                 return false;
     590             :         }
     591             : 
     592             :         /* Anything else implies that we are in a user namespace */
     593           0 :         log_debug("Mapping found in %s, we're in a user namespace", name);
     594           0 :         return true;
     595             : }
     596             : 
     597           2 : int running_in_userns(void) {
     598           2 :         _cleanup_free_ char *line = NULL;
     599             :         int r;
     600             : 
     601           2 :         r = userns_has_mapping("/proc/self/uid_map");
     602           2 :         if (r != 0)
     603           0 :                 return r;
     604             : 
     605           2 :         r = userns_has_mapping("/proc/self/gid_map");
     606           2 :         if (r != 0)
     607           0 :                 return r;
     608             : 
     609             :         /* "setgroups" file was added in kernel v3.18-rc6-15-g9cc46516dd. It is also
     610             :          * possible to compile a kernel without CONFIG_USER_NS, in which case "setgroups"
     611             :          * also does not exist. We cannot distinguish those two cases, so assume that
     612             :          * we're running on a stripped-down recent kernel, rather than on an old one,
     613             :          * and if the file is not found, return false.
     614             :          */
     615           2 :         r = read_one_line_file("/proc/self/setgroups", &line);
     616           2 :         if (r < 0) {
     617           0 :                 log_debug_errno(r, "/proc/self/setgroups: %m");
     618           0 :                 return r == -ENOENT ? false : r;
     619             :         }
     620             : 
     621           2 :         truncate_nl(line);
     622           2 :         r = streq(line, "deny");
     623             :         /* See user_namespaces(7) for a description of this "setgroups" contents. */
     624           2 :         log_debug("/proc/self/setgroups contains \"%s\", %s user namespace", line, r ? "in" : "not in");
     625           2 :         return r;
     626             : }
     627             : 
     628           0 : int running_in_chroot(void) {
     629             :         int r;
     630             : 
     631           0 :         if (getenv_bool("SYSTEMD_IGNORE_CHROOT") > 0)
     632           0 :                 return 0;
     633             : 
     634           0 :         r = files_same("/proc/1/root", "/", 0);
     635           0 :         if (r < 0)
     636           0 :                 return r;
     637             : 
     638           0 :         return r == 0;
     639             : }
     640             : 
     641             : static const char *const virtualization_table[_VIRTUALIZATION_MAX] = {
     642             :         [VIRTUALIZATION_NONE] = "none",
     643             :         [VIRTUALIZATION_KVM] = "kvm",
     644             :         [VIRTUALIZATION_QEMU] = "qemu",
     645             :         [VIRTUALIZATION_BOCHS] = "bochs",
     646             :         [VIRTUALIZATION_XEN] = "xen",
     647             :         [VIRTUALIZATION_UML] = "uml",
     648             :         [VIRTUALIZATION_VMWARE] = "vmware",
     649             :         [VIRTUALIZATION_ORACLE] = "oracle",
     650             :         [VIRTUALIZATION_MICROSOFT] = "microsoft",
     651             :         [VIRTUALIZATION_ZVM] = "zvm",
     652             :         [VIRTUALIZATION_PARALLELS] = "parallels",
     653             :         [VIRTUALIZATION_BHYVE] = "bhyve",
     654             :         [VIRTUALIZATION_QNX] = "qnx",
     655             :         [VIRTUALIZATION_ACRN] = "acrn",
     656             :         [VIRTUALIZATION_VM_OTHER] = "vm-other",
     657             : 
     658             :         [VIRTUALIZATION_SYSTEMD_NSPAWN] = "systemd-nspawn",
     659             :         [VIRTUALIZATION_LXC_LIBVIRT] = "lxc-libvirt",
     660             :         [VIRTUALIZATION_LXC] = "lxc",
     661             :         [VIRTUALIZATION_OPENVZ] = "openvz",
     662             :         [VIRTUALIZATION_DOCKER] = "docker",
     663             :         [VIRTUALIZATION_PODMAN] = "podman",
     664             :         [VIRTUALIZATION_RKT] = "rkt",
     665             :         [VIRTUALIZATION_WSL] = "wsl",
     666             :         [VIRTUALIZATION_CONTAINER_OTHER] = "container-other",
     667             : };
     668             : 
     669         128 : DEFINE_STRING_TABLE_LOOKUP(virtualization, int);

Generated by: LCOV version 1.14