blob: a703c478e2478c240cfb989739582f6b3e1ebffc [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, ".");
50
51 *k1 = atoi(r1);
52 *k2 = atoi(r2);
53 *k3 = atoi(r3);
54}
55
Wanlong Gao354ebb42012-12-07 10:10:04 +080056int tst_kvercmp(int r1, int r2, int r3)
57{
plarsfea599e2003-02-27 16:53:21 +000058 int a1, a2, a3;
59 int testver, currver;
60
61 get_kver(&a1, &a2, &a3);
62 testver = (r1 << 16) + (r2 << 8) + r3;
63 currver = (a1 << 16) + (a2 << 8) + a3;
64
plars878713c2003-03-03 22:02:07 +000065 return currver - testver;
Chris Dearmanec6edca2012-10-17 19:54:01 -070066}
Wanlong Gaoef73cc92013-07-09 15:54:51 +080067
68static int tst_kexvcmp(char *tst_exv, char *cur_ver)
69{
Wanlong Gao18f7be72013-07-16 22:29:48 +080070 int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0;
71 int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0;
Wanlong Gaoef73cc92013-07-09 15:54:51 +080072 int ret;
73
Wanlong Gao18f7be72013-07-16 22:29:48 +080074 sscanf(cur_ver, "%d.%d.%d-%d.%d", &c1, &c2, &c3, &c4, &c5);
75 sscanf(tst_exv, "%d.%d.%d-%d.%d", &t1, &t2, &t3, &t4, &t5);
76
Wanlong Gaoef73cc92013-07-09 15:54:51 +080077 if ((ret = c1 - t1))
78 return ret;
Wanlong Gao18f7be72013-07-16 22:29:48 +080079 if ((ret = c2 - t2))
80 return ret;
81 if ((ret = c3 - t3))
82 return ret;
83 if ((ret = c4 - t4))
84 return ret;
85
86 return c5 - t5;
Wanlong Gaoef73cc92013-07-09 15:54:51 +080087}
88
89int tst_kvercmp2(int r1, int r2, int r3, struct tst_kern_exv *vers)
90{
Wanlong Gaoef73cc92013-07-09 15:54:51 +080091 int i;
92 struct utsname uval;
93 char *kver;
94 const char *cur_dist_name = NULL;
95
Wanlong Gaoef73cc92013-07-09 15:54:51 +080096 uname(&uval);
97 kver = uval.release;
98 if (strstr(kver, ".el5")) {
99 cur_dist_name = "RHEL5";
100 } else if (strstr(kver, ".el6")) {
101 cur_dist_name = "RHEL6";
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800102 }
103
Wanlong Gaoe323edc2013-07-23 19:40:15 +0800104 if (cur_dist_name == NULL)
105 return tst_kvercmp(r1, r2, r3);
106
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800107 for (i = 0; vers[i].dist_name; i++) {
Wanlong Gao18f7be72013-07-16 22:29:48 +0800108 if (!strcmp(vers[i].dist_name, cur_dist_name)) {
Cyril Hrubise94c67a2013-08-01 15:38:55 +0200109 tst_resm(TINFO, "Detected %s using kernel version %s",
Wanlong Gao18f7be72013-07-16 22:29:48 +0800110 cur_dist_name, kver);
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800111 return tst_kexvcmp(vers[i].extra_ver, kver);
Wanlong Gao18f7be72013-07-16 22:29:48 +0800112 }
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800113 }
114
Wanlong Gao18f7be72013-07-16 22:29:48 +0800115 return tst_kvercmp(r1, r2, r3);
Wanlong Gaoef73cc92013-07-09 15:54:51 +0800116}