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