blob: 15fb571da5c1f970963dda6733d82df2dd9e66cb [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{
70 int c1 = 0, c2 = 0, t1 = 0, t2 = 0;
71 int ret;
72
73 sscanf(cur_ver, "%*d.%*d.%*d-%d.%d", &c1, &c2);
74 sscanf(tst_exv, "%d.%d", &t1, &t2);
75 if ((ret = c1 - t1))
76 return ret;
77 else
78 return c2 - t2;
79}
80
81int tst_kvercmp2(int r1, int r2, int r3, struct tst_kern_exv *vers)
82{
83 int ret;
84 int i;
85 struct utsname uval;
86 char *kver;
87 const char *cur_dist_name = NULL;
88
89 if ((ret = tst_kvercmp(r1, r2, r3)))
90 return ret;
91
92 uname(&uval);
93 kver = uval.release;
94 if (strstr(kver, ".el5")) {
95 cur_dist_name = "RHEL5";
96 } else if (strstr(kver, ".el6")) {
97 cur_dist_name = "RHEL6";
98 } else {
99 return ret;
100 }
101
102 for (i = 0; vers[i].dist_name; i++) {
103 if (!strcmp(vers[i].dist_name, cur_dist_name))
104 return tst_kexvcmp(vers[i].extra_ver, kver);
105 }
106
107 return ret;
108}