xref: /aosp_15_r20/external/ltp/testcases/lib/tst_kvcmp.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2016 Cyril Hrubis <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
7*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
8*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
9*49cdfc7eSAndroid Build Coastguard Worker #include <sys/utsname.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker enum op {
13*49cdfc7eSAndroid Build Coastguard Worker 	EQ,
14*49cdfc7eSAndroid Build Coastguard Worker 	NE,
15*49cdfc7eSAndroid Build Coastguard Worker 	GE,
16*49cdfc7eSAndroid Build Coastguard Worker 	GT,
17*49cdfc7eSAndroid Build Coastguard Worker 	LE,
18*49cdfc7eSAndroid Build Coastguard Worker 	LT,
19*49cdfc7eSAndroid Build Coastguard Worker 	AND,
20*49cdfc7eSAndroid Build Coastguard Worker 	OR,
21*49cdfc7eSAndroid Build Coastguard Worker 	ERR,
22*49cdfc7eSAndroid Build Coastguard Worker };
23*49cdfc7eSAndroid Build Coastguard Worker 
strtop(const char * op)24*49cdfc7eSAndroid Build Coastguard Worker static enum op strtop(const char *op)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(op, "-eq"))
27*49cdfc7eSAndroid Build Coastguard Worker 		return EQ;
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(op, "-ne"))
30*49cdfc7eSAndroid Build Coastguard Worker 		return NE;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(op, "-ge"))
33*49cdfc7eSAndroid Build Coastguard Worker 		return GE;
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(op, "-gt"))
36*49cdfc7eSAndroid Build Coastguard Worker 		return GT;
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(op, "-le"))
39*49cdfc7eSAndroid Build Coastguard Worker 		return LE;
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(op, "-lt"))
42*49cdfc7eSAndroid Build Coastguard Worker 		return LT;
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(op, "-a"))
45*49cdfc7eSAndroid Build Coastguard Worker 		return AND;
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(op, "-o"))
48*49cdfc7eSAndroid Build Coastguard Worker 		return OR;
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	return ERR;
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker 
help(const char * fname)53*49cdfc7eSAndroid Build Coastguard Worker static void help(const char *fname)
54*49cdfc7eSAndroid Build Coastguard Worker {
55*49cdfc7eSAndroid Build Coastguard Worker 	printf("usage: %s -eq|-ne|-gt|-ge|-lt|-le kver [-a|-o] ...\n\n", fname);
56*49cdfc7eSAndroid Build Coastguard Worker 	printf("-eq kver\tReturns true if kernel version is equal\n");
57*49cdfc7eSAndroid Build Coastguard Worker 	printf("-ne kver\tReturns true if kernel version is not equal\n");
58*49cdfc7eSAndroid Build Coastguard Worker 	printf("-gt kver\tReturns true if kernel version is greater\n");
59*49cdfc7eSAndroid Build Coastguard Worker 	printf("-ge kver\tReturns true if kernel version is greater or equal\n");
60*49cdfc7eSAndroid Build Coastguard Worker 	printf("-lt kver\tReturns true if kernel version is lesser\n");
61*49cdfc7eSAndroid Build Coastguard Worker 	printf("-le kver\tReturns true if kernel version is lesser or equal\n");
62*49cdfc7eSAndroid Build Coastguard Worker 	printf("-a  \t\tDoes logical and between two expressions\n");
63*49cdfc7eSAndroid Build Coastguard Worker 	printf("-o  \t\tDoes logical or between two expressions\n\n");
64*49cdfc7eSAndroid Build Coastguard Worker 	printf("Kernel version format has either one or two dots:\n\n");
65*49cdfc7eSAndroid Build Coastguard Worker 	printf("'2.6' or '4.8.1'\n\n");
66*49cdfc7eSAndroid Build Coastguard Worker 	printf("Kernel version can also be followed by a space separated list\n");
67*49cdfc7eSAndroid Build Coastguard Worker 	printf("of extra versions prefixed by distribution which when matched\n");
68*49cdfc7eSAndroid Build Coastguard Worker 	printf("take precedence:\n\n'3.0 RHEL6:2.6.18'\n\n");
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker 
compare_kver(const char * cur_kver,char * kver)71*49cdfc7eSAndroid Build Coastguard Worker static int compare_kver(const char *cur_kver, char *kver)
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker 	const char *ver, *exver;
74*49cdfc7eSAndroid Build Coastguard Worker 	const char *distname = tst_kvcmp_distname(cur_kver);
75*49cdfc7eSAndroid Build Coastguard Worker 	int v1, v2, v3;
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	ver = strtok(kver, " ");
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	while ((exver = strtok(NULL, " "))) {
80*49cdfc7eSAndroid Build Coastguard Worker 		char *exkver = strchr(exver, ':');
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 		if (!exkver) {
83*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, "Invalid extra version '%s'\n", exver);
84*49cdfc7eSAndroid Build Coastguard Worker 			exit(2);
85*49cdfc7eSAndroid Build Coastguard Worker 		}
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 		*(exkver++) = '\0';
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 		if (!distname || strcmp(distname, exver))
90*49cdfc7eSAndroid Build Coastguard Worker 			continue;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 		return tst_kvexcmp(exkver, cur_kver);
93*49cdfc7eSAndroid Build Coastguard Worker 	}
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_parse_kver(ver, &v1, &v2, &v3)) {
96*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr,
97*49cdfc7eSAndroid Build Coastguard Worker 			"Invalid kernel version '%s'\n",
98*49cdfc7eSAndroid Build Coastguard Worker 			ver);
99*49cdfc7eSAndroid Build Coastguard Worker 		return 2;
100*49cdfc7eSAndroid Build Coastguard Worker 	}
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	return tst_kvcmp(cur_kver, v1, v2, v3);
103*49cdfc7eSAndroid Build Coastguard Worker }
104*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])105*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
106*49cdfc7eSAndroid Build Coastguard Worker {
107*49cdfc7eSAndroid Build Coastguard Worker 	int i = 1;
108*49cdfc7eSAndroid Build Coastguard Worker 	int ret = -1;
109*49cdfc7eSAndroid Build Coastguard Worker 	enum op prev_op = ERR;
110*49cdfc7eSAndroid Build Coastguard Worker 	struct utsname buf;
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 	if (argc <= 1 || !strcmp(argv[1], "-h")) {
113*49cdfc7eSAndroid Build Coastguard Worker 		help(argv[0]);
114*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
115*49cdfc7eSAndroid Build Coastguard Worker 	}
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	uname(&buf);
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	while (i < argc) {
120*49cdfc7eSAndroid Build Coastguard Worker 		const char *strop = argv[i++];
121*49cdfc7eSAndroid Build Coastguard Worker 		char *strkver;
122*49cdfc7eSAndroid Build Coastguard Worker 		int res;
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 		enum op op = strtop(strop);
125*49cdfc7eSAndroid Build Coastguard Worker 
126*49cdfc7eSAndroid Build Coastguard Worker 		switch (op) {
127*49cdfc7eSAndroid Build Coastguard Worker 		case EQ:
128*49cdfc7eSAndroid Build Coastguard Worker 		case NE:
129*49cdfc7eSAndroid Build Coastguard Worker 		case GE:
130*49cdfc7eSAndroid Build Coastguard Worker 		case GT:
131*49cdfc7eSAndroid Build Coastguard Worker 		case LE:
132*49cdfc7eSAndroid Build Coastguard Worker 		case LT:
133*49cdfc7eSAndroid Build Coastguard Worker 			if (ret != -1 && prev_op == ERR) {
134*49cdfc7eSAndroid Build Coastguard Worker 				fprintf(stderr, "Expected -a or -o\n");
135*49cdfc7eSAndroid Build Coastguard Worker 				return 2;
136*49cdfc7eSAndroid Build Coastguard Worker 			}
137*49cdfc7eSAndroid Build Coastguard Worker 
138*49cdfc7eSAndroid Build Coastguard Worker 			if (i >= argc) {
139*49cdfc7eSAndroid Build Coastguard Worker 				fprintf(stderr,
140*49cdfc7eSAndroid Build Coastguard Worker 					"Expected kernel version after '%s'\n",
141*49cdfc7eSAndroid Build Coastguard Worker 					strop);
142*49cdfc7eSAndroid Build Coastguard Worker 				return 2;
143*49cdfc7eSAndroid Build Coastguard Worker 			}
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker 			strkver = argv[i++];
146*49cdfc7eSAndroid Build Coastguard Worker 		break;
147*49cdfc7eSAndroid Build Coastguard Worker 		case AND:
148*49cdfc7eSAndroid Build Coastguard Worker 		case OR:
149*49cdfc7eSAndroid Build Coastguard Worker 			if (ret == -1) {
150*49cdfc7eSAndroid Build Coastguard Worker 				fprintf(stderr,
151*49cdfc7eSAndroid Build Coastguard Worker 					"The %s must follow expression\n",
152*49cdfc7eSAndroid Build Coastguard Worker 					strop);
153*49cdfc7eSAndroid Build Coastguard Worker 				return 2;
154*49cdfc7eSAndroid Build Coastguard Worker 			}
155*49cdfc7eSAndroid Build Coastguard Worker 			prev_op = op;
156*49cdfc7eSAndroid Build Coastguard Worker 			continue;
157*49cdfc7eSAndroid Build Coastguard Worker 		break;
158*49cdfc7eSAndroid Build Coastguard Worker 		case ERR:
159*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, "Invalid operation %s\n", argv[i]);
160*49cdfc7eSAndroid Build Coastguard Worker 			return 2;
161*49cdfc7eSAndroid Build Coastguard Worker 		}
162*49cdfc7eSAndroid Build Coastguard Worker 
163*49cdfc7eSAndroid Build Coastguard Worker 		res = compare_kver(buf.release, strkver);
164*49cdfc7eSAndroid Build Coastguard Worker 
165*49cdfc7eSAndroid Build Coastguard Worker 		switch (op) {
166*49cdfc7eSAndroid Build Coastguard Worker 		case EQ:
167*49cdfc7eSAndroid Build Coastguard Worker 			res = (res == 0);
168*49cdfc7eSAndroid Build Coastguard Worker 		break;
169*49cdfc7eSAndroid Build Coastguard Worker 		case NE:
170*49cdfc7eSAndroid Build Coastguard Worker 			res = (res != 0);
171*49cdfc7eSAndroid Build Coastguard Worker 		break;
172*49cdfc7eSAndroid Build Coastguard Worker 		case GE:
173*49cdfc7eSAndroid Build Coastguard Worker 			res = (res >= 0);
174*49cdfc7eSAndroid Build Coastguard Worker 		break;
175*49cdfc7eSAndroid Build Coastguard Worker 		case GT:
176*49cdfc7eSAndroid Build Coastguard Worker 			res = (res > 0);
177*49cdfc7eSAndroid Build Coastguard Worker 		break;
178*49cdfc7eSAndroid Build Coastguard Worker 		case LE:
179*49cdfc7eSAndroid Build Coastguard Worker 			res = (res <= 0);
180*49cdfc7eSAndroid Build Coastguard Worker 		break;
181*49cdfc7eSAndroid Build Coastguard Worker 		case LT:
182*49cdfc7eSAndroid Build Coastguard Worker 			res = (res < 0);
183*49cdfc7eSAndroid Build Coastguard Worker 		break;
184*49cdfc7eSAndroid Build Coastguard Worker 		default:
185*49cdfc7eSAndroid Build Coastguard Worker 		break;
186*49cdfc7eSAndroid Build Coastguard Worker 		}
187*49cdfc7eSAndroid Build Coastguard Worker 
188*49cdfc7eSAndroid Build Coastguard Worker 		switch (prev_op) {
189*49cdfc7eSAndroid Build Coastguard Worker 		case ERR:
190*49cdfc7eSAndroid Build Coastguard Worker 			ret = res;
191*49cdfc7eSAndroid Build Coastguard Worker 		break;
192*49cdfc7eSAndroid Build Coastguard Worker 		case AND:
193*49cdfc7eSAndroid Build Coastguard Worker 			ret = ret && res;
194*49cdfc7eSAndroid Build Coastguard Worker 			prev_op = ERR;
195*49cdfc7eSAndroid Build Coastguard Worker 		break;
196*49cdfc7eSAndroid Build Coastguard Worker 		case OR:
197*49cdfc7eSAndroid Build Coastguard Worker 			ret = ret || res;
198*49cdfc7eSAndroid Build Coastguard Worker 			prev_op = ERR;
199*49cdfc7eSAndroid Build Coastguard Worker 		break;
200*49cdfc7eSAndroid Build Coastguard Worker 		default:
201*49cdfc7eSAndroid Build Coastguard Worker 		break;
202*49cdfc7eSAndroid Build Coastguard Worker 		}
203*49cdfc7eSAndroid Build Coastguard Worker 	}
204*49cdfc7eSAndroid Build Coastguard Worker 
205*49cdfc7eSAndroid Build Coastguard Worker 	if (prev_op != ERR) {
206*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "Useless -a or -o at the end\n");
207*49cdfc7eSAndroid Build Coastguard Worker 		return 2;
208*49cdfc7eSAndroid Build Coastguard Worker 	}
209*49cdfc7eSAndroid Build Coastguard Worker 
210*49cdfc7eSAndroid Build Coastguard Worker 	return !ret;
211*49cdfc7eSAndroid Build Coastguard Worker }
212