blob: abdec12066818c1db3f9e997166531e88eb5bf1b [file] [log] [blame]
plarsfea599e2003-02-27 16:53:21 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2003
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
13 * the 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, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plarsfea599e2003-02-27 16:53:21 +000018 */
vapier45a8ba02009-07-20 10:59:32 +000019
plarsfea599e2003-02-27 16:53:21 +000020/*
vapier45a8ba02009-07-20 10:59:32 +000021 *
plarsfea599e2003-02-27 16:53:21 +000022 * AUTHOR
23 * Paul Larson <plars@linuxtestproject.org>
24 *
25 * DESCRIPTION
26 * Compare a given kernel version against the current kernel version.
27 * If they are the same - return 0
28 * If the argument is > current kernel version - return positive int
29 * If the argument is < current kernel version - return negative int
30 *
31 */
32
robbiew88e7b182003-03-13 19:00:13 +000033#include <stdlib.h>
plarsfea599e2003-02-27 16:53:21 +000034#include <unistd.h>
35#include <string.h>
36#include <sys/utsname.h>
Wanlong Gaoef73cc92013-07-09 15:54:51 +080037#include "test.h"
plarsfea599e2003-02-27 16:53:21 +000038
39void get_kver(int *k1, int *k2, int *k3)
40{
41 struct utsname uval;
42 char *kver;
43 char *r1, *r2, *r3;
mridgee6508f82005-01-04 21:00:17 +000044
plarsfea599e2003-02-27 16:53:21 +000045 uname(&uval);
46 kver = uval.release;
47 r1 = strsep(&kver, ".");
48 r2 = strsep(&kver, ".");
49 r3 = strsep(&kver, ".");
plarsfea599e2003-02-27 16:53:21 +000050 *k1 = atoi(r1);
51 *k2 = atoi(r2);
Cyril Hrubiscd5983d2014-10-01 16:57:37 +020052
53 if (r3)
54 *k3 = atoi(r3);
55 else
56 *k3 = 0;
plarsfea599e2003-02-27 16:53:21 +000057}
58
Wanlong Gao354ebb42012-12-07 10:10:04 +080059int tst_kvercmp(int r1, int r2, int r3)
60{
plarsfea599e2003-02-27 16:53:21 +000061 int a1, a2, a3;
62 int testver, currver;
63
64 get_kver(&a1, &a2, &a3);
65 testver = (r1 << 16) + (r2 << 8) + r3;
66 currver = (a1 << 16) + (a2 << 8) + a3;
67
plars878713c2003-03-03 22:02:07 +000068 return currver - testver;
Chris Dearmanec6edca2012-10-17 19:54:01 -070069}
Wanlong Gaoef73cc92013-07-09 15:54:51 +080070
71static int tst_kexvcmp(char *tst_exv, char *cur_ver)
72{
Wanlong Gao18f7be72013-07-16 22:29:48 +080073 int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0;
74 int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0;
Wanlong Gaoef73cc92013-07-09 15:54:51 +080075 int ret;
76
Wanlong Gao18f7be72013-07-16 22:29:48 +080077 sscanf(cur_ver, "%d.%d.%d-%d.%d", &c1, &c2, &c3, &c4, &c5);
78 sscanf(tst_exv, "%d.%d.%d-%d.%d", &t1, &t2, &t3, &t4, &t5);
79
Wanlong Gaoef73cc92013-07-09 15:54:51 +080080 if ((ret = c1 - t1))
81 return ret;
Wanlong Gao18f7be72013-07-16 22:29:48 +080082 if ((ret = c2 - t2))
83 return ret;
84 if ((ret = c3 - t3))
85 return ret;
86 if ((ret = c4 - t4))
87 return ret;
88
89 return c5 - t5;
Wanlong Gaoef73cc92013-07-09 15:54:51 +080090}
91
92int tst_kvercmp2(int r1, int r2, int r3, struct tst_kern_exv *vers)
93{
Wanlong Gaoef73cc92013-07-09 15:54:51 +080094 int i;
95 struct utsname uval;
96 char *kver;
97 const char *cur_dist_name = NULL;
98
Wanlong Gaoef73cc92013-07-09 15:54:51 +080099 uname(&uval);
100 kver = uval.release;
Stanislav Kholmanskikhb217b3e2013-10-09 17:11:09 +0400101 if (strstr(kver, ".el5uek")) {
102 cur_dist_name = "OL5UEK";
103 } else if (strstr(kver, ".el5")) {
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800104 cur_dist_name = "RHEL5";
Stanislav Kholmanskikhb217b3e2013-10-09 17:11:09 +0400105 } else if (strstr(kver, ".el6uek")) {
106 cur_dist_name = "OL6UEK";
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800107 } else if (strstr(kver, ".el6")) {
108 cur_dist_name = "RHEL6";
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800109 }
110
Wanlong Gaoe323edc2013-07-23 19:40:15 +0800111 if (cur_dist_name == NULL)
112 return tst_kvercmp(r1, r2, r3);
113
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800114 for (i = 0; vers[i].dist_name; i++) {
Wanlong Gao18f7be72013-07-16 22:29:48 +0800115 if (!strcmp(vers[i].dist_name, cur_dist_name)) {
Cyril Hrubise94c67a2013-08-01 15:38:55 +0200116 tst_resm(TINFO, "Detected %s using kernel version %s",
Wanlong Gao18f7be72013-07-16 22:29:48 +0800117 cur_dist_name, kver);
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800118 return tst_kexvcmp(vers[i].extra_ver, kver);
Wanlong Gao18f7be72013-07-16 22:29:48 +0800119 }
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800120 }
121
Wanlong Gao18f7be72013-07-16 22:29:48 +0800122 return tst_kvercmp(r1, r2, r3);
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800123}