blob: c497f5d64aa97b56aeee0ad037d0ade528e90133 [file] [log] [blame]
plarsfea599e2003-02-27 16:53:21 +00001/*
Cyril Hrubis9c74eef2016-02-04 10:32:22 +01002 * Copyright (c) International Business Machines Corp., 2003
3 * AUTHOR: Paul Larson <plars@linuxtestproject.org>
4 * Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
plarsfea599e2003-02-27 16:53:21 +00005 *
Cyril Hrubis9c74eef2016-02-04 10:32:22 +01006 * 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.
plarsfea599e2003-02-27 16:53:21 +000010 *
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010011 * 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.
plarsfea599e2003-02-27 16:53:21 +000015 *
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010016 * 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
plarsfea599e2003-02-27 16:53:21 +000019 */
20
robbiew88e7b182003-03-13 19:00:13 +000021#include <stdlib.h>
plarsfea599e2003-02-27 16:53:21 +000022#include <unistd.h>
23#include <string.h>
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010024#include <limits.h>
plarsfea599e2003-02-27 16:53:21 +000025#include <sys/utsname.h>
Wanlong Gaoef73cc92013-07-09 15:54:51 +080026#include "test.h"
plarsfea599e2003-02-27 16:53:21 +000027
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010028static char *parse_digit(const char *str, int *d)
plarsfea599e2003-02-27 16:53:21 +000029{
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010030 unsigned long v;
31 char *end;
mridgee6508f82005-01-04 21:00:17 +000032
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010033 v = strtoul(str, &end, 10);
34 if (str == end)
35 return NULL;
Cyril Hrubiscd5983d2014-10-01 16:57:37 +020036
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010037 if (v > INT_MAX)
38 return NULL;
39
40 *d = v;
41
Cyril Hrubis4dcfd282016-11-01 15:07:12 +010042 return end;
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010043}
44
Cyril Hrubis4dcfd282016-11-01 15:07:12 +010045int tst_parse_kver(const char *str_kver, int *v1, int *v2, int *v3)
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010046{
47 const char *str = str_kver;
48
49 *v1 = 0;
50 *v2 = 0;
51 *v3 = 0;
52
53 if (!(str = parse_digit(str, v1)))
Cyril Hrubis4dcfd282016-11-01 15:07:12 +010054 return 1;
55
56 if (*(str++) != '.')
57 return 1;
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010058
59 if (!(str = parse_digit(str, v2)))
Cyril Hrubis4dcfd282016-11-01 15:07:12 +010060 return 1;
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010061
62 /*
Cyril Hrubis4dcfd282016-11-01 15:07:12 +010063 * Check for a short version e.g '2.4'
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010064 */
Cyril Hrubis4dcfd282016-11-01 15:07:12 +010065 if (*str == ' ' || *str == '\0')
66 return 0;
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010067
Cyril Hrubis4dcfd282016-11-01 15:07:12 +010068 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;
plarsfea599e2003-02-27 16:53:21 +000079}
80
Wanlong Gao354ebb42012-12-07 10:10:04 +080081int tst_kvercmp(int r1, int r2, int r3)
82{
plarsfea599e2003-02-27 16:53:21 +000083 int a1, a2, a3;
84 int testver, currver;
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010085 struct utsname uval;
plarsfea599e2003-02-27 16:53:21 +000086
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010087 uname(&uval);
Cyril Hrubis4dcfd282016-11-01 15:07:12 +010088 if (tst_parse_kver(uval.release, &a1, &a2, &a3)) {
89 tst_resm(TWARN,
90 "Invalid kernel version %s, expected %%d.%%d.%%d",
91 uval.release);
92 }
Cyril Hrubis9c74eef2016-02-04 10:32:22 +010093
plarsfea599e2003-02-27 16:53:21 +000094 testver = (r1 << 16) + (r2 << 8) + r3;
95 currver = (a1 << 16) + (a2 << 8) + a3;
96
plars878713c2003-03-03 22:02:07 +000097 return currver - testver;
Chris Dearmanec6edca2012-10-17 19:54:01 -070098}
Wanlong Gaoef73cc92013-07-09 15:54:51 +080099
100static int tst_kexvcmp(char *tst_exv, char *cur_ver)
101{
Wanlong Gao18f7be72013-07-16 22:29:48 +0800102 int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0;
103 int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0;
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800104 int ret;
105
Wanlong Gao18f7be72013-07-16 22:29:48 +0800106 sscanf(cur_ver, "%d.%d.%d-%d.%d", &c1, &c2, &c3, &c4, &c5);
107 sscanf(tst_exv, "%d.%d.%d-%d.%d", &t1, &t2, &t3, &t4, &t5);
108
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800109 if ((ret = c1 - t1))
110 return ret;
Wanlong Gao18f7be72013-07-16 22:29:48 +0800111 if ((ret = c2 - t2))
112 return ret;
113 if ((ret = c3 - t3))
114 return ret;
115 if ((ret = c4 - t4))
116 return ret;
117
118 return c5 - t5;
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800119}
120
121int tst_kvercmp2(int r1, int r2, int r3, struct tst_kern_exv *vers)
122{
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800123 int i;
124 struct utsname uval;
125 char *kver;
126 const char *cur_dist_name = NULL;
127
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800128 uname(&uval);
129 kver = uval.release;
Stanislav Kholmanskikhb217b3e2013-10-09 17:11:09 +0400130 if (strstr(kver, ".el5uek")) {
131 cur_dist_name = "OL5UEK";
132 } else if (strstr(kver, ".el5")) {
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800133 cur_dist_name = "RHEL5";
Stanislav Kholmanskikhb217b3e2013-10-09 17:11:09 +0400134 } else if (strstr(kver, ".el6uek")) {
135 cur_dist_name = "OL6UEK";
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800136 } else if (strstr(kver, ".el6")) {
137 cur_dist_name = "RHEL6";
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800138 }
139
Wanlong Gaoe323edc2013-07-23 19:40:15 +0800140 if (cur_dist_name == NULL)
141 return tst_kvercmp(r1, r2, r3);
142
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800143 for (i = 0; vers[i].dist_name; i++) {
Wanlong Gao18f7be72013-07-16 22:29:48 +0800144 if (!strcmp(vers[i].dist_name, cur_dist_name)) {
Cyril Hrubise94c67a2013-08-01 15:38:55 +0200145 tst_resm(TINFO, "Detected %s using kernel version %s",
Wanlong Gao18f7be72013-07-16 22:29:48 +0800146 cur_dist_name, kver);
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800147 return tst_kexvcmp(vers[i].extra_ver, kver);
Wanlong Gao18f7be72013-07-16 22:29:48 +0800148 }
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800149 }
150
Wanlong Gao18f7be72013-07-16 22:29:48 +0800151 return tst_kvercmp(r1, r2, r3);
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800152}