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 /* always search for x86_64 */
120 char *fix = strstr(driver, "x86-64");
121 if (fix)
122 fix[3] = '_';
123
124 SAFE_ASPRINTF(NULL, &search, "/%s.ko", driver);
125
126 f = SAFE_FOPEN(NULL, path, "r");
127
128 while (fgets(buf, sizeof(buf), f)) {
129 /* cut dependencies after : */
130 if ((sep = strchr(buf, ':')))
131 *sep = 0;
132
133 /* driver found */
134 if (strstr(buf, search) != NULL) {
135 ret = 0;
136 break;
137 }
138 }
139
140 SAFE_FCLOSE(NULL, f);
141 free(search);
142 free(path);
143
144 return ret;
145 }
146
tst_check_driver_(const char * driver)147 static int tst_check_driver_(const char *driver)
148 {
149 if (!tst_search_driver(driver, "modules.dep") ||
150 !tst_search_driver(driver, "modules.builtin"))
151 return 0;
152
153 return -1;
154 }
155
tst_check_driver(const char * driver)156 int tst_check_driver(const char *driver)
157 {
158 #ifdef __ANDROID__
159 /*
160 * Android may not have properly installed modules.* files. We could
161 * search modules in /system/lib/modules, but to to determine built-in
162 * drivers we need modules.builtin. Therefore assume all drivers are
163 * available.
164 */
165 return 0;
166 #endif
167
168 if (!tst_check_driver_(driver))
169 return 0;
170
171 int ret = -1;
172
173 if (strrchr(driver, '-') || strrchr(driver, '_')) {
174 char *driver2 = strdup(driver);
175 char *ix = driver2;
176 char find = '-', replace = '_';
177
178 if (strrchr(driver, '_')) {
179 find = '_';
180 replace = '-';
181 }
182
183 while ((ix = strchr(ix, find)))
184 *ix++ = replace;
185
186 ret = tst_check_driver_(driver2);
187 free(driver2);
188 }
189
190 return ret;
191 }
192