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 <sys/stat.h>
27 #include "test.h"
28 #include "safe_macros.h"
29
file_exist(const char * path)30 static int file_exist(const char *path)
31 {
32 struct stat st;
33
34 if (!access(path, R_OK) && !stat(path, &st))
35 return 1;
36
37 return 0;
38 }
39
is_kvm(void)40 static int is_kvm(void)
41 {
42 FILE *cpuinfo;
43 char line[64];
44 int found;
45
46 /* this doesn't work with custom -cpu values, since there's
47 * no easy, reasonable or reliable way to work around those */
48 cpuinfo = SAFE_FOPEN(NULL, "/proc/cpuinfo", "r");
49 found = 0;
50 while (fgets(line, sizeof(line), cpuinfo) != NULL) {
51 if (strstr(line, "QEMU Virtual CPU")) {
52 found = 1;
53 break;
54 }
55 }
56
57 SAFE_FCLOSE(NULL, cpuinfo);
58
59 if (file_exist("/dev/vda") || file_exist("/dev/block/vda")
60 || file_exist("/sys/block/vda"))
61 found = 1;
62
63 return found;
64 }
65
is_xen(void)66 static int is_xen(void)
67 {
68 char hypervisor_type[4];
69
70 if (access("/proc/xen", F_OK) == 0)
71 return 1;
72
73 if (access("/sys/hypervisor/type", F_OK) == 0) {
74 SAFE_FILE_SCANF(NULL, "/sys/hypervisor/type", "%3s",
75 hypervisor_type);
76 return strncmp("xen", hypervisor_type,
77 sizeof(hypervisor_type)) == 0;
78 }
79
80 return 0;
81 }
82
is_ibmz(int virt_type)83 static int is_ibmz(int virt_type)
84 {
85 FILE *sysinfo;
86 char line[64];
87 int found_lpar, found_zvm;
88
89 if (access("/proc/sysinfo", F_OK) != 0)
90 return 0;
91
92 sysinfo = SAFE_FOPEN(NULL, "/proc/sysinfo", "r");
93 found_lpar = 0;
94 found_zvm = 0;
95 while (fgets(line, sizeof(line), sysinfo) != NULL) {
96 if (strstr(line, "LPAR"))
97 found_lpar = 1;
98 else if (strstr(line, "z/VM"))
99 found_zvm = 1;
100 }
101
102 SAFE_FCLOSE(NULL, sysinfo);
103
104 switch (virt_type) {
105 case VIRT_IBMZ:
106 return found_lpar;
107 case VIRT_IBMZ_LPAR:
108 return found_lpar && !found_zvm;
109 case VIRT_IBMZ_ZVM:
110 return found_lpar && found_zvm;
111 default:
112 return 0;
113 }
114 }
115
try_systemd_detect_virt(void)116 static int try_systemd_detect_virt(void)
117 {
118 FILE *f;
119 char virt_type[64];
120 int ret;
121
122 /* See tst_cmd.c */
123 void *old_handler = signal(SIGCHLD, SIG_DFL);
124
125 f = popen("systemd-detect-virt", "r");
126 if (!f) {
127 signal(SIGCHLD, old_handler);
128 return 0;
129 }
130
131 if (!fgets(virt_type, sizeof(virt_type), f))
132 virt_type[0] = '\0';
133
134 ret = pclose(f);
135
136 signal(SIGCHLD, old_handler);
137
138 /*
139 * systemd-detect-virt not found by shell or no virtualization detected
140 * (systemd-detect-virt returns non-zero)
141 */
142 if (ret < 0 || (WIFEXITED(ret) && WEXITSTATUS(ret) == 127))
143 return -1;
144
145 if (ret)
146 return 0;
147
148 if (!strncmp("kvm", virt_type, 3))
149 return VIRT_KVM;
150
151 if (!strncmp("xen", virt_type, 3))
152 return VIRT_XEN;
153
154 if (!strncmp("zvm", virt_type, 3))
155 return VIRT_IBMZ_ZVM;
156
157 return VIRT_OTHER;
158 }
159
tst_is_virt(int virt_type)160 int tst_is_virt(int virt_type)
161 {
162 int ret = try_systemd_detect_virt();
163
164 if (ret > 0) {
165 if (virt_type == VIRT_ANY)
166 return 1;
167 else
168 return ret == virt_type;
169 }
170
171 switch (virt_type) {
172 case VIRT_ANY:
173 return is_xen() || is_kvm() || is_ibmz(VIRT_IBMZ);
174 case VIRT_XEN:
175 return is_xen();
176 case VIRT_KVM:
177 return is_kvm();
178 case VIRT_IBMZ:
179 case VIRT_IBMZ_LPAR:
180 case VIRT_IBMZ_ZVM:
181 return is_ibmz(virt_type);
182 case VIRT_OTHER:
183 return 0;
184 }
185
186 tst_brkm(TBROK, NULL, "invalid virt_type flag: %d", virt_type);
187 return -1;
188 }
189