xref: /aosp_15_r20/external/ltp/testcases/realtime/func/pi-tests/test-skeleton.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright © International Business Machines  Corp., 2005, 2008
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * NAME
20*49cdfc7eSAndroid Build Coastguard Worker  *      test-skeleton.c
21*49cdfc7eSAndroid Build Coastguard Worker  *
22*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
23*49cdfc7eSAndroid Build Coastguard Worker  *
24*49cdfc7eSAndroid Build Coastguard Worker  *
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * USAGE:
27*49cdfc7eSAndroid Build Coastguard Worker  *      Use run_auto.sh script in current directory to build and run test.
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  * AUTHOR
30*49cdfc7eSAndroid Build Coastguard Worker  *
31*49cdfc7eSAndroid Build Coastguard Worker  *
32*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
33*49cdfc7eSAndroid Build Coastguard Worker  *
34*49cdfc7eSAndroid Build Coastguard Worker  *
35*49cdfc7eSAndroid Build Coastguard Worker  *****************************************************************************/
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <time.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <librttest.h>
45*49cdfc7eSAndroid Build Coastguard Worker 
usage(void)46*49cdfc7eSAndroid Build Coastguard Worker void usage(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	rt_help();
49*49cdfc7eSAndroid Build Coastguard Worker 	printf("testpi-5 and 6 specific options:\n");
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
parse_args(int c,char * v)52*49cdfc7eSAndroid Build Coastguard Worker int parse_args(int c, char *v)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	int handled = 1;
56*49cdfc7eSAndroid Build Coastguard Worker 	switch (c) {
57*49cdfc7eSAndroid Build Coastguard Worker 	case 'h':
58*49cdfc7eSAndroid Build Coastguard Worker 		usage();
59*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
60*49cdfc7eSAndroid Build Coastguard Worker 	default:
61*49cdfc7eSAndroid Build Coastguard Worker 		handled = 0;
62*49cdfc7eSAndroid Build Coastguard Worker 		break;
63*49cdfc7eSAndroid Build Coastguard Worker 	}
64*49cdfc7eSAndroid Build Coastguard Worker 	return handled;
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FUNCTION do_test(argc, argv)
68*49cdfc7eSAndroid Build Coastguard Worker #define TIMEOUT 20
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker static pid_t pid;
71*49cdfc7eSAndroid Build Coastguard Worker 
timeout_handler(int sig)72*49cdfc7eSAndroid Build Coastguard Worker static void timeout_handler(int sig)
73*49cdfc7eSAndroid Build Coastguard Worker {
74*49cdfc7eSAndroid Build Coastguard Worker 	int i, killed, status;
75*49cdfc7eSAndroid Build Coastguard Worker 	struct timespec ts = {.tv_sec = 0,.tv_nsec = 100000000 };
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	printf("Inside the timeout handler, killing the TC threads \n");
78*49cdfc7eSAndroid Build Coastguard Worker 	kill(pid, SIGKILL);
79*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 5; i++) {
80*49cdfc7eSAndroid Build Coastguard Worker 		killed = waitpid(pid, &status, WNOHANG | WUNTRACED);
81*49cdfc7eSAndroid Build Coastguard Worker 		if (0 != killed)
82*49cdfc7eSAndroid Build Coastguard Worker 			break;
83*49cdfc7eSAndroid Build Coastguard Worker 		nanosleep(&ts, NULL);
84*49cdfc7eSAndroid Build Coastguard Worker 	}
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	if (0 != killed && pid != killed) {
87*49cdfc7eSAndroid Build Coastguard Worker 		printf("\n Failed to kill child process ");
88*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
89*49cdfc7eSAndroid Build Coastguard Worker 	}
90*49cdfc7eSAndroid Build Coastguard Worker 	printf("\nResult:PASS\n");
91*49cdfc7eSAndroid Build Coastguard Worker 	exit(1);
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char ** argv)94*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char **argv)
95*49cdfc7eSAndroid Build Coastguard Worker {
96*49cdfc7eSAndroid Build Coastguard Worker 	pid_t termpid;
97*49cdfc7eSAndroid Build Coastguard Worker 	int status;
98*49cdfc7eSAndroid Build Coastguard Worker 	setup();
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	rt_init("h", parse_args, argc, argv);
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	pid = fork();
103*49cdfc7eSAndroid Build Coastguard Worker 	if (0 == pid) {
104*49cdfc7eSAndroid Build Coastguard Worker 		exit(TEST_FUNCTION);
105*49cdfc7eSAndroid Build Coastguard Worker 	} else if (pid < 0) {
106*49cdfc7eSAndroid Build Coastguard Worker 		printf("\n Cannot fork test program \n");
107*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
108*49cdfc7eSAndroid Build Coastguard Worker 	}
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 	signal(SIGALRM, timeout_handler);
111*49cdfc7eSAndroid Build Coastguard Worker 	alarm(TIMEOUT);
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	do {
114*49cdfc7eSAndroid Build Coastguard Worker 		termpid = waitpid(pid, &status, 0);
115*49cdfc7eSAndroid Build Coastguard Worker 	} while (termpid == -1 && errno == EINTR);
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	if (termpid == -1) {
118*49cdfc7eSAndroid Build Coastguard Worker 		printf("\n Waiting for test program failed, Exiting \n");
119*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
120*49cdfc7eSAndroid Build Coastguard Worker 	}
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
123*49cdfc7eSAndroid Build Coastguard Worker }
124