1 /*
2 * Copyright (C) 2013 Linux Test Project
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it
13 * is free of the rightful claim of any third person regarding
14 * infringement or the like. Any license provided herein, whether
15 * implied or otherwise, applies only to this software file. Patent
16 * licenses, if any, provided herein do not apply to combinations of
17 * this program with other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
23 */
24
25 #include <unistd.h>
26 #include "test.h"
27 #include "safe_macros.h"
28
is_kvm(void)29 static int is_kvm(void)
30 {
31 FILE *cpuinfo;
32 char line[64];
33 int found;
34
35 /* this doesn't work with custom -cpu values, since there's
36 * no easy, reasonable or reliable way to work around those */
37 cpuinfo = SAFE_FOPEN(NULL, "/proc/cpuinfo", "r");
38 found = 0;
39 while (fgets(line, sizeof(line), cpuinfo) != NULL) {
40 if (strstr(line, "QEMU Virtual CPU")) {
41 found = 1;
42 break;
43 }
44 }
45
46 SAFE_FCLOSE(NULL, cpuinfo);
47 return found;
48 }
49
is_xen(void)50 static int is_xen(void)
51 {
52 char hypervisor_type[3];
53
54 if (access("/proc/xen", F_OK) == 0)
55 return 1;
56
57 if (access("/sys/hypervisor/type", F_OK) == 0) {
58 SAFE_FILE_SCANF(NULL, "/sys/hypervisor/type", "%3s",
59 hypervisor_type);
60 return strncmp("xen", hypervisor_type,
61 sizeof(hypervisor_type)) == 0;
62 }
63
64 return 0;
65 }
66
tst_is_virt(int virt_type)67 int tst_is_virt(int virt_type)
68 {
69 switch (virt_type) {
70 case VIRT_XEN:
71 return is_xen();
72 case VIRT_KVM:
73 return is_kvm();
74 }
75 tst_brkm(TBROK, NULL, "invalid virt_type flag: %d", virt_type);
76 }
77