• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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[4];
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 
is_ibmz(int virt_type)67 static int is_ibmz(int virt_type)
68 {
69 	FILE *sysinfo;
70 	char line[64];
71 	int found_lpar, found_zvm;
72 
73 	if (access("/proc/sysinfo", F_OK) != 0)
74 		return 0;
75 
76 	sysinfo = SAFE_FOPEN(NULL, "/proc/sysinfo", "r");
77 	found_lpar = 0;
78 	found_zvm = 0;
79 	while (fgets(line, sizeof(line), sysinfo) != NULL) {
80 		if (strstr(line, "LPAR"))
81 			found_lpar = 1;
82 		else if (strstr(line, "z/VM"))
83 			found_zvm = 1;
84 	}
85 
86 	SAFE_FCLOSE(NULL, sysinfo);
87 
88 	switch (virt_type) {
89 	case VIRT_IBMZ:
90 		return found_lpar;
91 	case VIRT_IBMZ_LPAR:
92 		return found_lpar && !found_zvm;
93 	case VIRT_IBMZ_ZVM:
94 		return found_lpar && found_zvm;
95 	default:
96 		return 0;
97 	}
98 }
99 
try_systemd_detect_virt(void)100 static int try_systemd_detect_virt(void)
101 {
102 	FILE *f;
103 	char virt_buf[64];
104 	int ret;
105 	char *virt_type = getenv("LTP_VIRT_OVERRIDE");
106 
107 	if (virt_type) {
108 		if (!strcmp("", virt_type))
109 			return 0;
110 
111 		goto cmp;
112 	}
113 
114 	virt_type = virt_buf;
115 
116 	/* See tst_cmd.c */
117 	void *old_handler = signal(SIGCHLD, SIG_DFL);
118 
119 	f = popen("systemd-detect-virt", "r");
120 	if (!f) {
121 		signal(SIGCHLD, old_handler);
122 		return 0;
123 	}
124 
125 	if (!fgets(virt_type, sizeof(virt_type), f))
126 		virt_type[0] = '\0';
127 
128 	ret = pclose(f);
129 
130 	signal(SIGCHLD, old_handler);
131 
132 	/*
133 	 * systemd-detect-virt not found by shell or no virtualization detected
134 	 * (systemd-detect-virt returns non-zero)
135          */
136 	if (ret < 0 || (WIFEXITED(ret) && WEXITSTATUS(ret) == 127))
137 		return -1;
138 
139 	if (ret)
140 		return 0;
141 
142 cmp:
143 	if (!strncmp("kvm", virt_type, 3))
144 		return VIRT_KVM;
145 
146 	if (!strncmp("xen", virt_type, 3))
147 		return VIRT_XEN;
148 
149 	if (!strncmp("zvm", virt_type, 3))
150 		return VIRT_IBMZ_ZVM;
151 
152 	if (!strncmp("microsoft", virt_type, 9))
153 		return VIRT_HYPERV;
154 
155 	return VIRT_OTHER;
156 }
157 
tst_is_virt(int virt_type)158 int tst_is_virt(int virt_type)
159 {
160 	int ret = try_systemd_detect_virt();
161 
162 	if (ret > 0) {
163 		if (virt_type == VIRT_ANY)
164 			return 1;
165 		else
166 			return ret == virt_type;
167 	}
168 
169 	switch (virt_type) {
170 	case VIRT_ANY:
171 		return is_xen() || is_kvm() || is_ibmz(VIRT_IBMZ);
172 	case VIRT_XEN:
173 		return is_xen();
174 	case VIRT_KVM:
175 		return is_kvm();
176 	case VIRT_IBMZ:
177 	case VIRT_IBMZ_LPAR:
178 	case VIRT_IBMZ_ZVM:
179 		return is_ibmz(virt_type);
180 	case VIRT_HYPERV:
181 	case VIRT_OTHER:
182 		return 0;
183 	}
184 
185 	tst_brkm(TBROK, NULL, "invalid virt_type flag: %d", virt_type);
186 	return -1;
187 }
188