plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2003 |
| 3 | * AUTHOR: Paul Larson <plars@linuxtestproject.org> |
| 4 | * Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz> |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 5 | * |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 6 | * 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. |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 10 | * |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 11 | * 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. |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 15 | * |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 16 | * 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 |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
robbiew | 88e7b18 | 2003-03-13 19:00:13 +0000 | [diff] [blame] | 21 | #include <stdlib.h> |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | #include <string.h> |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 24 | #include <limits.h> |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 25 | #include <sys/utsname.h> |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 26 | #include "test.h" |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 27 | |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 28 | static char *parse_digit(const char *str, int *d) |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 29 | { |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 30 | unsigned long v; |
| 31 | char *end; |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 32 | |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 33 | v = strtoul(str, &end, 10); |
| 34 | if (str == end) |
| 35 | return NULL; |
Cyril Hrubis | cd5983d | 2014-10-01 16:57:37 +0200 | [diff] [blame] | 36 | |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 37 | if (v > INT_MAX) |
| 38 | return NULL; |
| 39 | |
| 40 | *d = v; |
| 41 | |
Cyril Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 42 | return end; |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Cyril Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 45 | int tst_parse_kver(const char *str_kver, int *v1, int *v2, int *v3) |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 46 | { |
| 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 Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 54 | return 1; |
| 55 | |
| 56 | if (*(str++) != '.') |
| 57 | return 1; |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 58 | |
| 59 | if (!(str = parse_digit(str, v2))) |
Cyril Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 60 | return 1; |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 61 | |
| 62 | /* |
Cyril Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 63 | * Check for a short version e.g '2.4' |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 64 | */ |
Cyril Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 65 | if (*str == ' ' || *str == '\0') |
| 66 | return 0; |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 67 | |
Cyril Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 68 | 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; |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Cyril Hrubis | 882f45a | 2017-04-21 16:31:48 +0200 | [diff] [blame] | 81 | int tst_kvcmp(const char *cur_kver, int r1, int r2, int r3) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 82 | { |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 83 | int a1, a2, a3; |
| 84 | int testver, currver; |
| 85 | |
Cyril Hrubis | 882f45a | 2017-04-21 16:31:48 +0200 | [diff] [blame] | 86 | if (tst_parse_kver(cur_kver, &a1, &a2, &a3)) { |
Cyril Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 87 | tst_resm(TWARN, |
| 88 | "Invalid kernel version %s, expected %%d.%%d.%%d", |
Cyril Hrubis | 882f45a | 2017-04-21 16:31:48 +0200 | [diff] [blame] | 89 | cur_kver); |
Cyril Hrubis | 4dcfd28 | 2016-11-01 15:07:12 +0100 | [diff] [blame] | 90 | } |
Cyril Hrubis | 9c74eef | 2016-02-04 10:32:22 +0100 | [diff] [blame] | 91 | |
plars | fea599e | 2003-02-27 16:53:21 +0000 | [diff] [blame] | 92 | testver = (r1 << 16) + (r2 << 8) + r3; |
| 93 | currver = (a1 << 16) + (a2 << 8) + a3; |
| 94 | |
plars | 878713c | 2003-03-03 22:02:07 +0000 | [diff] [blame] | 95 | return currver - testver; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 96 | } |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 97 | |
Cyril Hrubis | 882f45a | 2017-04-21 16:31:48 +0200 | [diff] [blame] | 98 | int tst_kvercmp(int r1, int r2, int r3) |
| 99 | { |
| 100 | struct utsname uval; |
| 101 | |
| 102 | uname(&uval); |
| 103 | |
| 104 | return tst_kvcmp(uval.release, r1, r2, r3); |
| 105 | } |
| 106 | |
| 107 | int tst_kvexcmp(const char *tst_exv, const char *cur_ver) |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 108 | { |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 109 | int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0; |
| 110 | int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0; |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 111 | int ret; |
| 112 | |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 113 | sscanf(cur_ver, "%d.%d.%d-%d.%d", &c1, &c2, &c3, &c4, &c5); |
| 114 | sscanf(tst_exv, "%d.%d.%d-%d.%d", &t1, &t2, &t3, &t4, &t5); |
| 115 | |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 116 | if ((ret = c1 - t1)) |
| 117 | return ret; |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 118 | if ((ret = c2 - t2)) |
| 119 | return ret; |
| 120 | if ((ret = c3 - t3)) |
| 121 | return ret; |
| 122 | if ((ret = c4 - t4)) |
| 123 | return ret; |
| 124 | |
| 125 | return c5 - t5; |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 126 | } |
| 127 | |
Cyril Hrubis | 882f45a | 2017-04-21 16:31:48 +0200 | [diff] [blame] | 128 | const char *tst_kvcmp_distname(const char *kver) |
| 129 | { |
| 130 | if (strstr(kver, ".el5uek")) |
| 131 | return "OL5UEK"; |
| 132 | |
| 133 | if (strstr(kver, ".el5")) |
| 134 | return "RHEL5"; |
| 135 | |
| 136 | if (strstr(kver, ".el6uek")) |
| 137 | return "OL6UEK"; |
| 138 | |
| 139 | if (strstr(kver, ".el6")) |
| 140 | return "RHEL6"; |
| 141 | |
| 142 | return NULL; |
| 143 | } |
| 144 | |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 145 | int tst_kvercmp2(int r1, int r2, int r3, struct tst_kern_exv *vers) |
| 146 | { |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 147 | int i; |
Cyril Hrubis | 882f45a | 2017-04-21 16:31:48 +0200 | [diff] [blame] | 148 | const char *kver; |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 149 | struct utsname uval; |
Cyril Hrubis | 882f45a | 2017-04-21 16:31:48 +0200 | [diff] [blame] | 150 | const char *cur_dist_name; |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 151 | |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 152 | uname(&uval); |
| 153 | kver = uval.release; |
Cyril Hrubis | 882f45a | 2017-04-21 16:31:48 +0200 | [diff] [blame] | 154 | cur_dist_name = tst_kvcmp_distname(kver); |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 155 | |
Wanlong Gao | e323edc | 2013-07-23 19:40:15 +0800 | [diff] [blame] | 156 | if (cur_dist_name == NULL) |
| 157 | return tst_kvercmp(r1, r2, r3); |
| 158 | |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 159 | for (i = 0; vers[i].dist_name; i++) { |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 160 | if (!strcmp(vers[i].dist_name, cur_dist_name)) { |
Cyril Hrubis | e94c67a | 2013-08-01 15:38:55 +0200 | [diff] [blame] | 161 | tst_resm(TINFO, "Detected %s using kernel version %s", |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 162 | cur_dist_name, kver); |
Cyril Hrubis | 882f45a | 2017-04-21 16:31:48 +0200 | [diff] [blame] | 163 | return tst_kvexcmp(vers[i].extra_ver, kver); |
Wanlong Gao | 18f7be7 | 2013-07-16 22:29:48 +0800 | [diff] [blame] | 164 | } |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 165 | } |
| 166 | |
Cyril Hrubis | 882f45a | 2017-04-21 16:31:48 +0200 | [diff] [blame] | 167 | return tst_kvcmp(kver, r1, r2, r3); |
Wanlong Gao | ef73cc9 | 2013-07-09 15:54:51 +0800 | [diff] [blame] | 168 | } |