X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fvirt.c;h=1c86a3dd1e62dee901b4c482203b8b3a3e91839b;hb=7fd1b19bc9e9f5574f2877936b8ac267c7706947;hp=4c526ff45471620616237ac3176ca594e7921bc5;hpb=d7832d2c6e0ef5f2839a2296c1cc2fc85c7d9632;p=elogind.git diff --git a/src/shared/virt.c b/src/shared/virt.c index 4c526ff45..1c86a3dd1 100644 --- a/src/shared/virt.c +++ b/src/shared/virt.c @@ -6,16 +6,16 @@ Copyright 2011 Lennart Poettering systemd is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + 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 - General Public License for more details. + Lesser General Public License for more details. - You should have received a copy of the GNU General Public License + You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see . ***/ @@ -25,6 +25,7 @@ #include "util.h" #include "virt.h" +#include "fileio.h" /* Returns a short identifier for the various VM implementations */ int detect_vm(const char **id) { @@ -61,13 +62,28 @@ 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; + int r; + + /* 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 +133,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 +147,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 +156,7 @@ int detect_vm(const char **id) { } } - if (hypervisor) { + if (hypervisor || hvtype) { if (id) *id = "other"; @@ -153,15 +168,16 @@ int detect_vm(const char **id) { } int detect_container(const char **id) { - FILE *f; + _cleanup_free_ char *e = NULL; + int r; /* Unfortunately many of these operations require root access * in one way or another */ - if (geteuid() != 0) - return -EPERM; - - if (running_in_chroot() > 0) { + r = running_in_chroot(); + if (r < 0) + return r; + if (r > 0) { if (id) *id = "chroot"; @@ -180,63 +196,27 @@ int detect_container(const char **id) { return 1; } - f = fopen("/proc/1/environ", "re"); - if (f) { - bool done = false; - - do { - char line[LINE_MAX]; - unsigned i; - - for (i = 0; i < sizeof(line)-1; i++) { - int c; - - c = getc(f); - if (_unlikely_(c == EOF)) { - done = true; - break; - } else if (c == 0) - break; - - line[i] = c; - } - line[i] = 0; - - if (streq(line, "container=lxc")) { - fclose(f); - - if (id) - *id = "lxc"; - return 1; - - } else if (streq(line, "container=lxc-libvirt")) { - fclose(f); + r = getenv_for_pid(1, "container", &e); + if (r <= 0) + return r; - if (id) - *id = "lxc-libvirt"; - return 1; - - } else if (streq(line, "container=systemd-nspawn")) { - fclose(f); - - if (id) - *id = "systemd-nspawn"; - return 1; - - } else if (startswith(line, "container=")) { - fclose(f); - - if (id) - *id = "other"; - return 1; - } - - } while (!done); - - fclose(f); + /* We only recognize a selected few here, since we want to + * enforce a redacted namespace */ + if (streq(e, "lxc")) { + if (id) + *id = "lxc"; + } else if (streq(e, "lxc-libvirt")) { + if (id) + *id = "lxc-libvirt"; + } else if (streq(e, "systemd-nspawn")) { + if (id) + *id = "systemd-nspawn"; + } else { + if (id) + *id = "other"; } - return 0; + return r; } /* Returns a short identifier for the various VM/container implementations */