xref: /aosp_15_r20/external/ltp/testcases/lib/tst_device.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) 2017 Cyril Hrubis <[email protected]>
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 #include "old/old_device.h"
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker extern struct tst_test *tst_test;
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
17*49cdfc7eSAndroid Build Coastguard Worker };
18*49cdfc7eSAndroid Build Coastguard Worker 
print_help(void)19*49cdfc7eSAndroid Build Coastguard Worker static void print_help(void)
20*49cdfc7eSAndroid Build Coastguard Worker {
21*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "\nUsage:\n");
22*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "tst_device acquire [size [filename]]\n");
23*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "tst_device release /path/to/device\n");
24*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "tst_device clear /path/to/device\n\n");
25*49cdfc7eSAndroid Build Coastguard Worker }
26*49cdfc7eSAndroid Build Coastguard Worker 
acquire_device(int argc,char * argv[])27*49cdfc7eSAndroid Build Coastguard Worker static int acquire_device(int argc, char *argv[])
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int size = 0;
30*49cdfc7eSAndroid Build Coastguard Worker 	const char *device;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	if (argc > 4)
33*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	if (argc >= 3) {
36*49cdfc7eSAndroid Build Coastguard Worker 		size = atoi(argv[2]);
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 		if (!size) {
39*49cdfc7eSAndroid Build Coastguard Worker 			fprintf(stderr, "ERROR: Invalid device size '%s'",
40*49cdfc7eSAndroid Build Coastguard Worker 				argv[2]);
41*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
42*49cdfc7eSAndroid Build Coastguard Worker 		}
43*49cdfc7eSAndroid Build Coastguard Worker 	}
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	if (argc >= 4)
46*49cdfc7eSAndroid Build Coastguard Worker 		device = tst_acquire_loop_device(size, argv[3]);
47*49cdfc7eSAndroid Build Coastguard Worker 	else
48*49cdfc7eSAndroid Build Coastguard Worker 		device = tst_acquire_device__(size);
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	if (!device)
51*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_clear_device(device)) {
54*49cdfc7eSAndroid Build Coastguard Worker 		tst_release_device(device);
55*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
56*49cdfc7eSAndroid Build Coastguard Worker 	}
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	printf("%s", device);
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker 
release_device(int argc,char * argv[])63*49cdfc7eSAndroid Build Coastguard Worker static int release_device(int argc, char *argv[])
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker 	if (argc != 3)
66*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	/*
69*49cdfc7eSAndroid Build Coastguard Worker 	 * tst_acquire_[loop_]device() was called in a different process.
70*49cdfc7eSAndroid Build Coastguard Worker 	 * tst_release_device() would think that no device was acquired yet
71*49cdfc7eSAndroid Build Coastguard Worker 	 * and do nothing. Call tst_detach_device() directly to bypass
72*49cdfc7eSAndroid Build Coastguard Worker 	 * the check.
73*49cdfc7eSAndroid Build Coastguard Worker 	 */
74*49cdfc7eSAndroid Build Coastguard Worker 	return tst_detach_device(argv[2]);
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker 
clear_device(int argc,char * argv[])77*49cdfc7eSAndroid Build Coastguard Worker static int clear_device(int argc, char *argv[])
78*49cdfc7eSAndroid Build Coastguard Worker {
79*49cdfc7eSAndroid Build Coastguard Worker 	if (argc != 3)
80*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_clear_device(argv[2]))
83*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])88*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
89*49cdfc7eSAndroid Build Coastguard Worker {
90*49cdfc7eSAndroid Build Coastguard Worker 	/*
91*49cdfc7eSAndroid Build Coastguard Worker 	 * Force messages to be printed from the new library i.e. tst_test.c
92*49cdfc7eSAndroid Build Coastguard Worker 	 *
93*49cdfc7eSAndroid Build Coastguard Worker 	 * The new library prints messages into stderr while the old one prints
94*49cdfc7eSAndroid Build Coastguard Worker 	 * them into stdout. When messages are printed into stderr we can
95*49cdfc7eSAndroid Build Coastguard Worker 	 * safely do:
96*49cdfc7eSAndroid Build Coastguard Worker 	 *
97*49cdfc7eSAndroid Build Coastguard Worker 	 * DEV=$(tst_device acquire)
98*49cdfc7eSAndroid Build Coastguard Worker 	 */
99*49cdfc7eSAndroid Build Coastguard Worker 	tst_test = &test;
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	if (argc < 2)
102*49cdfc7eSAndroid Build Coastguard Worker 		goto help;
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(argv[1], "acquire")) {
105*49cdfc7eSAndroid Build Coastguard Worker 		if (acquire_device(argc, argv))
106*49cdfc7eSAndroid Build Coastguard Worker 			goto help;
107*49cdfc7eSAndroid Build Coastguard Worker 	} else if (!strcmp(argv[1], "release")) {
108*49cdfc7eSAndroid Build Coastguard Worker 		if (release_device(argc, argv))
109*49cdfc7eSAndroid Build Coastguard Worker 			goto help;
110*49cdfc7eSAndroid Build Coastguard Worker 	} else if (!strcmp(argv[1], "clear")) {
111*49cdfc7eSAndroid Build Coastguard Worker 		if (clear_device(argc, argv))
112*49cdfc7eSAndroid Build Coastguard Worker 			goto help;
113*49cdfc7eSAndroid Build Coastguard Worker 	} else {
114*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "ERROR: Invalid COMMAND '%s'\n", argv[1]);
115*49cdfc7eSAndroid Build Coastguard Worker 		goto help;
116*49cdfc7eSAndroid Build Coastguard Worker 	}
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
119*49cdfc7eSAndroid Build Coastguard Worker help:
120*49cdfc7eSAndroid Build Coastguard Worker 	print_help();
121*49cdfc7eSAndroid Build Coastguard Worker 	return 1;
122*49cdfc7eSAndroid Build Coastguard Worker }
123