xref: /aosp_15_r20/external/ltp/lib/tst_virt.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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_buf[64];
120 	int ret;
121 	char *virt_type = getenv("LTP_VIRT_OVERRIDE");
122 
123 	if (virt_type) {
124 		if (!strcmp("", virt_type))
125 			return 0;
126 
127 		goto cmp;
128 	}
129 
130 	virt_type = virt_buf;
131 
132 	/* See tst_cmd.c */
133 	void *old_handler = signal(SIGCHLD, SIG_DFL);
134 
135 	f = popen("systemd-detect-virt", "r");
136 	if (!f) {
137 		signal(SIGCHLD, old_handler);
138 		return 0;
139 	}
140 
141 	if (!fgets(virt_type, sizeof(virt_type), f))
142 		virt_type[0] = '\0';
143 
144 	ret = pclose(f);
145 
146 	signal(SIGCHLD, old_handler);
147 
148 	/*
149 	 * systemd-detect-virt not found by shell or no virtualization detected
150 	 * (systemd-detect-virt returns non-zero)
151          */
152 	if (ret < 0 || (WIFEXITED(ret) && WEXITSTATUS(ret) == 127))
153 		return -1;
154 
155 	if (ret)
156 		return 0;
157 
158 cmp:
159 	if (!strncmp("kvm", virt_type, 3))
160 		return VIRT_KVM;
161 
162 	if (!strncmp("xen", virt_type, 3))
163 		return VIRT_XEN;
164 
165 	if (!strncmp("zvm", virt_type, 3))
166 		return VIRT_IBMZ_ZVM;
167 
168 	if (!strncmp("microsoft", virt_type, 9))
169 		return VIRT_HYPERV;
170 
171 	return VIRT_OTHER;
172 }
173 
tst_is_virt(int virt_type)174 int tst_is_virt(int virt_type)
175 {
176 	int ret = try_systemd_detect_virt();
177 
178 	if (ret > 0) {
179 		if (virt_type == VIRT_ANY)
180 			return 1;
181 		else
182 			return ret == virt_type;
183 	}
184 
185 	switch (virt_type) {
186 	case VIRT_ANY:
187 		return is_xen() || is_kvm() || is_ibmz(VIRT_IBMZ);
188 	case VIRT_XEN:
189 		return is_xen();
190 	case VIRT_KVM:
191 		return is_kvm();
192 	case VIRT_IBMZ:
193 	case VIRT_IBMZ_LPAR:
194 	case VIRT_IBMZ_ZVM:
195 		return is_ibmz(virt_type);
196 	case VIRT_HYPERV:
197 	case VIRT_OTHER:
198 		return 0;
199 	}
200 
201 	tst_brkm(TBROK, NULL, "invalid virt_type flag: %d", virt_type);
202 	return -1;
203 }
204