plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 18 | */ |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 19 | |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 20 | /* |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 21 | * |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 22 | * 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 | |
robbiew | 88e7b18 | 2003-03-13 19:00:13 +0000 | [diff] [blame] | 33 | #include <stdlib.h> |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | #include <string.h> |
| 36 | #include <sys/utsname.h> |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 37 | #include "test.h" |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 38 | |
| 39 | void get_kver(int *k1, int *k2, int *k3) |
| 40 | { |
| 41 | struct utsname uval; |
| 42 | char *kver; |
| 43 | char *r1, *r2, *r3; |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 44 | |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 45 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 56 | int tst_kvercmp(int r1, int r2, int r3) |
| 57 | { |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 58 | 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 | |
plars | 878713c | 2003-03-03 22:02:07 +0000 | [diff] [blame] | 65 | return currver - testver; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 66 | } |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 67 | |
| 68 | static int tst_kexvcmp(char *tst_exv, char *cur_ver) |
| 69 | { |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 70 | int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0; |
| 71 | int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0; |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 72 | int ret; |
| 73 | |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 74 | 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 Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 77 | if ((ret = c1 - t1)) |
| 78 | return ret; |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 79 | 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 Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | int tst_kvercmp2(int r1, int r2, int r3, struct tst_kern_exv *vers) |
| 90 | { |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 91 | int i; |
| 92 | struct utsname uval; |
| 93 | char *kver; |
| 94 | const char *cur_dist_name = NULL; |
| 95 | |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 96 | 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 Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 102 | } |
| 103 | |
Wanlong Gao | e323edc | 2013-07-23 19:40:15 +0800 | [diff] [blame] | 104 | if (cur_dist_name == NULL) |
| 105 | return tst_kvercmp(r1, r2, r3); |
| 106 | |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 107 | for (i = 0; vers[i].dist_name; i++) { |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 108 | if (!strcmp(vers[i].dist_name, cur_dist_name)) { |
Cyril Hrubis | e94c67a | 2013-08-01 15:38:55 +0200 | [diff] [blame] | 109 | tst_resm(TINFO, "Detected %s using kernel version %s", |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 110 | cur_dist_name, kver); |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 111 | return tst_kexvcmp(vers[i].extra_ver, kver); |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 112 | } |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 113 | } |
| 114 | |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 115 | return tst_kvercmp(r1, r2, r3); |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 116 | } |