chiark / gitweb /
journalctl: use _COMM= match for scripts
[elogind.git] / src / shared / virt.c
index fc62c72328e381341850d021dbd076fb6d5e73ee..4f8134a7732c964839eea9cb91056f4f988a7ff8 100644 (file)
 
 #include "util.h"
 #include "virt.h"
+#include "fileio.h"
 
 /* Returns a short identifier for the various VM implementations */
 int detect_vm(const char **id) {
+        _cleanup_free_ char *cpuinfo_contents = NULL;
+        int r;
 
 #if defined(__i386__) || defined(__x86_64__)
 
@@ -61,13 +64,27 @@ int detect_vm(const char **id) {
         union {
                 uint32_t sig32[3];
                 char text[13];
-        } sig;
+        } sig = {};
         unsigned i;
         const char *j, *k;
         bool hypervisor;
+        _cleanup_free_ char *hvtype = NULL;
+
+        /* Try high-level hypervisor sysfs file first:
+         *
+         * https://bugs.freedesktop.org/show_bug.cgi?id=61491 */
+        r = read_one_line_file("/sys/hypervisor/type", &hvtype);
+        if (r >= 0) {
+                if (streq(hvtype, "xen")) {
+                        if (id)
+                                *id = "xen";
+
+                        return 1;
+                }
+        } else if (r != -ENOENT)
+                return r;
 
         /* http://lwn.net/Articles/301888/ */
-        zero(sig);
 
 #if defined (__i386__)
 #define REG_a "eax"
@@ -117,11 +134,11 @@ int detect_vm(const char **id) {
         }
 
         for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
-                char *s;
-                int r;
+                _cleanup_free_ char *s = NULL;
                 const char *found = NULL;
 
-                if ((r = read_one_line_file(dmi_vendors[i], &s)) < 0) {
+                r = read_one_line_file(dmi_vendors[i], &s);
+                if (r < 0) {
                         if (r != -ENOENT)
                                 return r;
 
@@ -131,7 +148,6 @@ int detect_vm(const char **id) {
                 NULSTR_FOREACH_PAIR(j, k, dmi_vendor_table)
                         if (startswith(s, j))
                                 found = k;
-                free(s);
 
                 if (found) {
                         if (id)
@@ -141,7 +157,7 @@ int detect_vm(const char **id) {
                 }
         }
 
-        if (hypervisor) {
+        if (hypervisor || hvtype) {
                 if (id)
                         *id = "other";
 
@@ -149,11 +165,21 @@ int detect_vm(const char **id) {
         }
 
 #endif
+
+        /* Detect User-Mode Linux by reading /proc/cpuinfo */
+        r = read_full_file("/proc/cpuinfo", &cpuinfo_contents, NULL);
+        if (r < 0)
+                return r;
+        if (strstr(cpuinfo_contents, "\nvendor_id\t: User Mode Linux\n")) {
+                *id = "uml";
+                return 1;
+        }
+
         return 0;
 }
 
 int detect_container(const char **id) {
-        char *e = NULL;
+        _cleanup_free_ char *e = NULL;
         int r;
 
         /* Unfortunately many of these operations require root access
@@ -201,8 +227,6 @@ int detect_container(const char **id) {
                         *id = "other";
         }
 
-        free(e);
-
         return r;
 }