• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2003
3  *    AUTHOR: Paul Larson <plars@linuxtestproject.org>
4  * Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
5  *
6  * This program is free software;  you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14  * the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program;  if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <sys/utsname.h>
27 #include "test.h"
28 
29 #define OSRELEASE_PATH "/etc/os-release"
30 
parse_digit(const char * str,int * d)31 static char *parse_digit(const char *str, int *d)
32 {
33 	unsigned long v;
34 	char *end;
35 
36 	v = strtoul(str, &end, 10);
37 	if (str == end)
38 		return NULL;
39 
40 	if (v > INT_MAX)
41 		return NULL;
42 
43 	*d = v;
44 
45 	return end;
46 }
47 
tst_parse_kver(const char * str_kver,int * v1,int * v2,int * v3)48 int tst_parse_kver(const char *str_kver, int *v1, int *v2, int *v3)
49 {
50 	const char *str = str_kver;
51 
52 	*v1 = 0;
53 	*v2 = 0;
54 	*v3 = 0;
55 
56 	if (!(str = parse_digit(str, v1)))
57 		return 1;
58 
59 	if (*(str++) != '.')
60 		return 1;
61 
62 	if (!(str = parse_digit(str, v2)))
63 		return 1;
64 
65 	/*
66 	 * Check for a short version e.g '2.4'
67 	 */
68 	if (*str == ' ' || *str == '\0' || *str == '-')
69 		return 0;
70 
71 	if (*(str++) != '.')
72 		return 1;
73 
74 	/*
75 	 * Ignore rest of the string in order not to break on versions as
76 	 * 4.8.1-52-default.
77 	 */
78 	if (!parse_digit(str, v3))
79 		return 1;
80 
81 	return 0;
82 }
83 
tst_kvcmp(const char * cur_kver,int r1,int r2,int r3)84 int tst_kvcmp(const char *cur_kver, int r1, int r2, int r3)
85 {
86 	int a1, a2, a3;
87 	int testver, currver;
88 
89 	if (tst_parse_kver(cur_kver, &a1, &a2, &a3)) {
90 		tst_resm(TWARN,
91 			 "Invalid kernel version %s, expected %%d.%%d.%%d",
92 		         cur_kver);
93 	}
94 
95 	testver = (r1 << 16) + (r2 << 8) + r3;
96 	currver = (a1 << 16) + (a2 << 8) + a3;
97 
98 	return currver - testver;
99 }
100 
tst_kvercmp(int r1,int r2,int r3)101 int tst_kvercmp(int r1, int r2, int r3)
102 {
103 	struct utsname uval;
104 
105 	uname(&uval);
106 
107 	return tst_kvcmp(uval.release, r1, r2, r3);
108 }
109 
tst_kvexcmp(const char * tst_exv,const char * cur_ver)110 int tst_kvexcmp(const char *tst_exv, const char *cur_ver)
111 {
112 	int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0;
113 	int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0;
114 	int ret;
115 
116 	sscanf(cur_ver, "%d.%d.%d-%d.%d", &c1, &c2, &c3, &c4, &c5);
117 	sscanf(tst_exv, "%d.%d.%d-%d.%d", &t1, &t2, &t3, &t4, &t5);
118 
119 	if ((ret = c1 - t1))
120 		return ret;
121 	if ((ret = c2 - t2))
122 		return ret;
123 	if ((ret = c3 - t3))
124 		return ret;
125 	if ((ret = c4 - t4))
126 		return ret;
127 
128 	return c5 - t5;
129 }
130 
tst_kvcmp_distname(const char * kver)131 const char *tst_kvcmp_distname(const char *kver)
132 {
133 	static char distname[64];
134 	char *ret = distname;
135 	char *p = distname;
136 
137 	if (strstr(kver, ".el5uek"))
138 		return "OL5UEK";
139 
140 	if (strstr(kver, ".el5"))
141 		return "RHEL5";
142 
143 	if (strstr(kver, ".el6uek"))
144 		return "OL6UEK";
145 
146 	if (strstr(kver, ".el6"))
147 		return "RHEL6";
148 
149 	if (strstr(kver, ".el7"))
150 		return "RHEL7";
151 
152 	if (strstr(kver, ".el8"))
153 		return "RHEL8";
154 
155 	if (access(OSRELEASE_PATH, F_OK) != -1) {
156 		SAFE_FILE_LINES_SCANF(NULL, OSRELEASE_PATH, "ID=%s", distname);
157 
158 		if (p[0] == '"') {
159 			ret = distname + 1;
160 			p = ret;
161 		}
162 
163 		while (*p) {
164 			if (*p == '"') {
165 				*p = 0;
166 				break;
167 			}
168 			*p = toupper((unsigned char)*p);
169 			p++;
170 		}
171 
172 		return ret;
173 	}
174 
175 	return NULL;
176 }
177 
tst_kvercmp2(int r1,int r2,int r3,struct tst_kern_exv * vers)178 int tst_kvercmp2(int r1, int r2, int r3, struct tst_kern_exv *vers)
179 {
180 	int i;
181 	const char *kver;
182 	struct utsname uval;
183 	const char *cur_dist_name;
184 
185 	uname(&uval);
186 	kver = uval.release;
187 	cur_dist_name = tst_kvcmp_distname(kver);
188 
189 	if (cur_dist_name == NULL)
190 		return tst_kvercmp(r1, r2, r3);
191 
192 	for (i = 0; vers[i].dist_name; i++) {
193 		if (!strcmp(vers[i].dist_name, cur_dist_name)) {
194 			tst_resm(TINFO, "Detected %s using kernel version %s",
195 				 cur_dist_name, kver);
196 			return tst_kvexcmp(vers[i].extra_ver, kver);
197 		}
198 	}
199 
200 	return tst_kvcmp(kver, r1, r2, r3);
201 }
202