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 <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <limits.h>
25 #include <sys/utsname.h>
26 #include "test.h"
27
parse_digit(const char * str,int * d)28 static char *parse_digit(const char *str, int *d)
29 {
30 unsigned long v;
31 char *end;
32
33 v = strtoul(str, &end, 10);
34 if (str == end)
35 return NULL;
36
37 if (v > INT_MAX)
38 return NULL;
39
40 *d = v;
41
42 return end;
43 }
44
tst_parse_kver(const char * str_kver,int * v1,int * v2,int * v3)45 int tst_parse_kver(const char *str_kver, int *v1, int *v2, int *v3)
46 {
47 const char *str = str_kver;
48
49 *v1 = 0;
50 *v2 = 0;
51 *v3 = 0;
52
53 if (!(str = parse_digit(str, v1)))
54 return 1;
55
56 if (*(str++) != '.')
57 return 1;
58
59 if (!(str = parse_digit(str, v2)))
60 return 1;
61
62 /*
63 * Check for a short version e.g '2.4'
64 */
65 if (*str == ' ' || *str == '\0')
66 return 0;
67
68 if (*(str++) != '.')
69 return 1;
70
71 /*
72 * Ignore rest of the string in order not to break on versions as
73 * 4.8.1-52-default.
74 */
75 if (!parse_digit(str, v3))
76 return 1;
77
78 return 0;
79 }
80
tst_kvcmp(const char * cur_kver,int r1,int r2,int r3)81 int tst_kvcmp(const char *cur_kver, int r1, int r2, int r3)
82 {
83 int a1, a2, a3;
84 int testver, currver;
85
86 if (tst_parse_kver(cur_kver, &a1, &a2, &a3)) {
87 tst_resm(TWARN,
88 "Invalid kernel version %s, expected %%d.%%d.%%d",
89 cur_kver);
90 }
91
92 testver = (r1 << 16) + (r2 << 8) + r3;
93 currver = (a1 << 16) + (a2 << 8) + a3;
94
95 return currver - testver;
96 }
97
tst_kvercmp(int r1,int r2,int r3)98 int tst_kvercmp(int r1, int r2, int r3)
99 {
100 struct utsname uval;
101
102 uname(&uval);
103
104 return tst_kvcmp(uval.release, r1, r2, r3);
105 }
106
tst_kvexcmp(const char * tst_exv,const char * cur_ver)107 int tst_kvexcmp(const char *tst_exv, const char *cur_ver)
108 {
109 int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0;
110 int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0;
111 int ret;
112
113 sscanf(cur_ver, "%d.%d.%d-%d.%d", &c1, &c2, &c3, &c4, &c5);
114 sscanf(tst_exv, "%d.%d.%d-%d.%d", &t1, &t2, &t3, &t4, &t5);
115
116 if ((ret = c1 - t1))
117 return ret;
118 if ((ret = c2 - t2))
119 return ret;
120 if ((ret = c3 - t3))
121 return ret;
122 if ((ret = c4 - t4))
123 return ret;
124
125 return c5 - t5;
126 }
127
tst_kvcmp_distname(const char * kver)128 const char *tst_kvcmp_distname(const char *kver)
129 {
130 if (strstr(kver, ".el5uek"))
131 return "OL5UEK";
132
133 if (strstr(kver, ".el5"))
134 return "RHEL5";
135
136 if (strstr(kver, ".el6uek"))
137 return "OL6UEK";
138
139 if (strstr(kver, ".el6"))
140 return "RHEL6";
141
142 return NULL;
143 }
144
tst_kvercmp2(int r1,int r2,int r3,struct tst_kern_exv * vers)145 int tst_kvercmp2(int r1, int r2, int r3, struct tst_kern_exv *vers)
146 {
147 int i;
148 const char *kver;
149 struct utsname uval;
150 const char *cur_dist_name;
151
152 uname(&uval);
153 kver = uval.release;
154 cur_dist_name = tst_kvcmp_distname(kver);
155
156 if (cur_dist_name == NULL)
157 return tst_kvercmp(r1, r2, r3);
158
159 for (i = 0; vers[i].dist_name; i++) {
160 if (!strcmp(vers[i].dist_name, cur_dist_name)) {
161 tst_resm(TINFO, "Detected %s using kernel version %s",
162 cur_dist_name, kver);
163 return tst_kvexcmp(vers[i].extra_ver, kver);
164 }
165 }
166
167 return tst_kvcmp(kver, r1, r2, r3);
168 }
169