• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
3  * Copyright (c) 2020-2021 Petr Vorel <pvorel@suse.cz>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <sys/personality.h>
20 #include <sys/utsname.h>
21 #include <limits.h>
22 
23 #include "test.h"
24 #include "tst_kernel.h"
25 #include "old_safe_stdio.h"
26 
get_kernel_bits_from_uname(struct utsname * buf)27 static int get_kernel_bits_from_uname(struct utsname *buf)
28 {
29 	if (uname(buf)) {
30 		tst_brkm(TBROK | TERRNO, NULL, "uname()");
31 		return -1;
32 	}
33 
34 	return strstr(buf->machine, "64") ? 64 : 32;
35 }
36 
tst_kernel_bits(void)37 int tst_kernel_bits(void)
38 {
39 	struct utsname buf;
40 	static int kernel_bits;
41 
42 	if (kernel_bits)
43 		return kernel_bits;
44 
45 	kernel_bits = get_kernel_bits_from_uname(&buf);
46 
47 	if (kernel_bits == -1)
48 		return -1;
49 
50 	/*
51 	 * ARM64 (aarch64) defines 32-bit compatibility modes as
52 	 * armv8l and armv8b (little and big endian).
53 	 * s390x is 64bit but not contain 64 in the words.
54 	 */
55 	if (!strcmp(buf.machine, "armv8l") || !strcmp(buf.machine, "armv8b")
56 			|| !strcmp(buf.machine, "s390x"))
57 		kernel_bits = 64;
58 
59 #ifdef __ANDROID__
60 	/* Android's bionic libc sets the PER_LINUX32 personality for all 32-bit
61 	 * programs. This will cause buf.machine to report as i686 even though
62 	 * the kernel itself is 64-bit.
63 	 */
64 	if (!strcmp(buf.machine, "i686") &&
65 			(personality(0xffffffff) & PER_MASK) == PER_LINUX32) {
66 		/* Set the personality back to the default. */
67 		if (personality(PER_LINUX) == -1) {
68 			tst_brkm(TBROK | TERRNO, NULL, "personality()");
69 			return -1;
70 		}
71 
72 		/* Redo the uname check without the PER_LINUX32 personality to
73 		 * determine the actual kernel bits value.
74 		 */
75 		kernel_bits = get_kernel_bits_from_uname(&buf);
76 		if (kernel_bits == -1)
77 			return -1;
78 
79 		/* Set the personality back to PER_LINUX32. */
80 		if (personality(PER_LINUX32) == -1) {
81 			tst_brkm(TBROK | TERRNO, NULL, "personality()");
82 			return -1;
83 		}
84 	}
85 #endif  /* __ANDROID__ */
86 
87 	tst_resm(TINFO, "uname.machine=%s kernel is %ibit",
88 	         buf.machine, kernel_bits);
89 
90 	return kernel_bits;
91 }
92 
tst_search_driver(const char * driver,const char * file)93 static int tst_search_driver(const char *driver, const char *file)
94 {
95 	struct stat st;
96 	char buf[PATH_MAX];
97 	char *path = NULL, *search = NULL, *sep = NULL;
98 	FILE *f;
99 	int ret = -1;
100 
101 	struct utsname uts;
102 
103 	if (uname(&uts)) {
104 		tst_brkm(TBROK | TERRNO, NULL, "uname() failed");
105 		return -1;
106 	}
107 	SAFE_ASPRINTF(NULL, &path, "/lib/modules/%s/%s", uts.release, file);
108 
109 	if (stat(path, &st) || !(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
110 		tst_resm(TWARN, "expected file %s does not exist or not a file", path);
111 		return -1;
112 	}
113 
114 	if (access(path, R_OK)) {
115 		tst_resm(TWARN, "file %s cannot be read", path);
116 		return -1;
117 	}
118 
119 	SAFE_ASPRINTF(NULL, &search, "/%s.ko", driver);
120 
121 	f = SAFE_FOPEN(NULL, path, "r");
122 
123 	while (fgets(buf, sizeof(buf), f)) {
124 		/* cut dependencies after : */
125 		if ((sep = strchr(buf, ':')))
126 			*sep = 0;
127 
128 		/* driver found */
129 		if (strstr(buf, search) != NULL) {
130 			ret = 0;
131 			break;
132 		}
133 	}
134 
135 	SAFE_FCLOSE(NULL, f);
136 	free(search);
137 	free(path);
138 
139 	return ret;
140 }
141 
tst_check_driver_(const char * driver)142 static int tst_check_driver_(const char *driver)
143 {
144 	if (!tst_search_driver(driver, "modules.dep") ||
145 		!tst_search_driver(driver, "modules.builtin"))
146 		return 0;
147 
148 	return -1;
149 }
150 
tst_check_driver(const char * driver)151 int tst_check_driver(const char *driver)
152 {
153 #ifdef __ANDROID__
154 	/*
155 	 * Android may not have properly installed modules.* files. We could
156 	 * search modules in /system/lib/modules, but to to determine built-in
157 	 * drivers we need modules.builtin. Therefore assume all drivers are
158 	 * available.
159 	 */
160 	return 0;
161 #endif
162 
163 	if (!tst_check_driver_(driver))
164 		return 0;
165 
166 	int ret = -1;
167 
168 	if (strrchr(driver, '-') || strrchr(driver, '_')) {
169 		char *driver2 = strdup(driver);
170 		char *ix = driver2;
171 		char find = '-', replace = '_';
172 
173 		if (strrchr(driver, '_')) {
174 			find = '_';
175 			replace = '-';
176 		}
177 
178 		while ((ix = strchr(ix, find)))
179 			*ix++ = replace;
180 
181 		ret = tst_check_driver_(driver2);
182 		free(driver2);
183 	}
184 
185 	return ret;
186 }
187