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