• 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 	int kernel_bits = get_kernel_bits_from_uname(&buf);
41 
42 	if (kernel_bits == -1)
43 		return -1;
44 
45 	/*
46 	 * ARM64 (aarch64) defines 32-bit compatibility modes as
47 	 * armv8l and armv8b (little and big endian).
48 	 * s390x is 64bit but not contain 64 in the words.
49 	 */
50 	if (!strcmp(buf.machine, "armv8l") || !strcmp(buf.machine, "armv8b")
51 			|| !strcmp(buf.machine, "s390x"))
52 		kernel_bits = 64;
53 
54 #ifdef __ANDROID__
55 	/* Android's bionic libc sets the PER_LINUX32 personality for all 32-bit
56 	 * programs. This will cause buf.machine to report as i686 even though
57 	 * the kernel itself is 64-bit.
58 	 */
59 	if (!strcmp(buf.machine, "i686") &&
60 			(personality(0xffffffff) & PER_MASK) == PER_LINUX32) {
61 		/* Set the personality back to the default. */
62 		if (personality(PER_LINUX) == -1) {
63 			tst_brkm(TBROK | TERRNO, NULL, "personality()");
64 			return -1;
65 		}
66 
67 		/* Redo the uname check without the PER_LINUX32 personality to
68 		 * determine the actual kernel bits value.
69 		 */
70 		kernel_bits = get_kernel_bits_from_uname(&buf);
71 		if (kernel_bits == -1)
72 			return -1;
73 
74 		/* Set the personality back to PER_LINUX32. */
75 		if (personality(PER_LINUX32) == -1) {
76 			tst_brkm(TBROK | TERRNO, NULL, "personality()");
77 			return -1;
78 		}
79 	}
80 #endif  /* __ANDROID__ */
81 
82 	tst_resm(TINFO, "uname.machine=%s kernel is %ibit",
83 	         buf.machine, kernel_bits);
84 
85 	return kernel_bits;
86 }
87 
tst_search_driver(const char * driver,const char * file)88 static int tst_search_driver(const char *driver, const char *file)
89 {
90 	struct stat st;
91 	char buf[PATH_MAX];
92 	char *path = NULL, *search = NULL, *sep = NULL;
93 	FILE *f;
94 	int ret = -1;
95 
96 	struct utsname uts;
97 
98 	if (uname(&uts)) {
99 		tst_brkm(TBROK | TERRNO, NULL, "uname() failed");
100 		return -1;
101 	}
102 	SAFE_ASPRINTF(NULL, &path, "/lib/modules/%s/%s", uts.release, file);
103 
104 	if (stat(path, &st) || !(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
105 		tst_resm(TWARN, "expected file %s does not exist or not a file", path);
106 		return -1;
107 	}
108 
109 	if (access(path, R_OK)) {
110 		tst_resm(TWARN, "file %s cannot be read", path);
111 		return -1;
112 	}
113 
114 	SAFE_ASPRINTF(NULL, &search, "/%s.ko", driver);
115 
116 	f = SAFE_FOPEN(NULL, path, "r");
117 
118 	while (fgets(buf, sizeof(buf), f)) {
119 		/* cut dependencies after : */
120 		if ((sep = strchr(buf, ':')))
121 			*sep = 0;
122 
123 		/* driver found */
124 		if (strstr(buf, search) != NULL) {
125 			ret = 0;
126 			break;
127 		}
128 	}
129 
130 	SAFE_FCLOSE(NULL, f);
131 	free(search);
132 	free(path);
133 
134 	return ret;
135 }
136 
tst_check_driver_(const char * driver)137 static int tst_check_driver_(const char *driver)
138 {
139 	if (!tst_search_driver(driver, "modules.dep") ||
140 		!tst_search_driver(driver, "modules.builtin"))
141 		return 0;
142 
143 	return -1;
144 }
145 
tst_check_driver(const char * driver)146 int tst_check_driver(const char *driver)
147 {
148 #ifdef __ANDROID__
149 	/*
150 	 * Android may not have properly installed modules.* files. We could
151 	 * search modules in /system/lib/modules, but to to determine built-in
152 	 * drivers we need modules.builtin. Therefore assume all drivers are
153 	 * available.
154 	 */
155 	return 0;
156 #endif
157 
158 	if (!tst_check_driver_(driver))
159 		return 0;
160 
161 	int ret = -1;
162 
163 	if (strrchr(driver, '-') || strrchr(driver, '_')) {
164 		char *driver2 = strdup(driver);
165 		char *ix = driver2;
166 		char find = '-', replace = '_';
167 
168 		if (strrchr(driver, '_')) {
169 			find = '_';
170 			replace = '-';
171 		}
172 
173 		while ((ix = strchr(ix, find)))
174 			*ix++ = replace;
175 
176 		ret = tst_check_driver_(driver2);
177 		free(driver2);
178 	}
179 
180 	return ret;
181 }
182