xref: /aosp_15_r20/external/ltp/testcases/lib/tst_checkpoint.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 Linux Test Project
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
7*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
8*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
9*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
10*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
11*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
12*49cdfc7eSAndroid Build Coastguard Worker 
print_help(void)13*49cdfc7eSAndroid Build Coastguard Worker static void print_help(void)
14*49cdfc7eSAndroid Build Coastguard Worker {
15*49cdfc7eSAndroid Build Coastguard Worker 	printf("Usage: tst_checkpoint wait TIMEOUT ID\n");
16*49cdfc7eSAndroid Build Coastguard Worker 	printf("   or: tst_checkpoint wake TIMEOUT ID NR_WAKE\n");
17*49cdfc7eSAndroid Build Coastguard Worker 	printf("       TIMEOUT - timeout in ms\n");
18*49cdfc7eSAndroid Build Coastguard Worker 	printf("       ID - checkpoint id\n");
19*49cdfc7eSAndroid Build Coastguard Worker 	printf("       NR_WAKE - number of processes to wake up\n");
20*49cdfc7eSAndroid Build Coastguard Worker }
21*49cdfc7eSAndroid Build Coastguard Worker 
get_val(const char * name,const char * arg,unsigned int * val)22*49cdfc7eSAndroid Build Coastguard Worker static int get_val(const char *name, const char *arg, unsigned int *val)
23*49cdfc7eSAndroid Build Coastguard Worker {
24*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long temp;
25*49cdfc7eSAndroid Build Coastguard Worker 	char *e;
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker 	errno = 0;
28*49cdfc7eSAndroid Build Coastguard Worker 	temp = strtoul(arg, &e, 10);
29*49cdfc7eSAndroid Build Coastguard Worker 	if (errno || (e == arg) || (temp > UINT_MAX)) {
30*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "ERROR: Invalid %s '%s'\n",
31*49cdfc7eSAndroid Build Coastguard Worker 			name, arg);
32*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
33*49cdfc7eSAndroid Build Coastguard Worker 	}
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	*val = temp;
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])40*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int id, timeout, nr_wake;
43*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
44*49cdfc7eSAndroid Build Coastguard Worker 	int type;
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	if (argc < 3)
47*49cdfc7eSAndroid Build Coastguard Worker 		goto help;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(argv[1], "wait")) {
50*49cdfc7eSAndroid Build Coastguard Worker 		type = 0;
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 		if (argc != 4)
53*49cdfc7eSAndroid Build Coastguard Worker 			goto help;
54*49cdfc7eSAndroid Build Coastguard Worker 	} else if (!strcmp(argv[1], "wake")) {
55*49cdfc7eSAndroid Build Coastguard Worker 		type = 1;
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 		if (argc != 5)
58*49cdfc7eSAndroid Build Coastguard Worker 			goto help;
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 		if (get_val("NR_WAKE", argv[4], &nr_wake))
61*49cdfc7eSAndroid Build Coastguard Worker 			goto help;
62*49cdfc7eSAndroid Build Coastguard Worker 	} else {
63*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "ERROR: Invalid COMMAND '%s'\n",
64*49cdfc7eSAndroid Build Coastguard Worker 			argv[1]);
65*49cdfc7eSAndroid Build Coastguard Worker 		goto help;
66*49cdfc7eSAndroid Build Coastguard Worker 	}
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	if (get_val("TIMEOUT", argv[2], &timeout)
69*49cdfc7eSAndroid Build Coastguard Worker 	    || get_val("ID", argv[3], &id)) {
70*49cdfc7eSAndroid Build Coastguard Worker 		goto help;
71*49cdfc7eSAndroid Build Coastguard Worker 	}
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	tst_reinit();
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	if (type)
76*49cdfc7eSAndroid Build Coastguard Worker 		ret = tst_checkpoint_wake(id, nr_wake, timeout);
77*49cdfc7eSAndroid Build Coastguard Worker 	else
78*49cdfc7eSAndroid Build Coastguard Worker 		ret = tst_checkpoint_wait(id, timeout);
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	if (ret)
81*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
82*49cdfc7eSAndroid Build Coastguard Worker 	else
83*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker help:
86*49cdfc7eSAndroid Build Coastguard Worker 	print_help();
87*49cdfc7eSAndroid Build Coastguard Worker 	return 1;
88*49cdfc7eSAndroid Build Coastguard Worker }
89