2010-07-28 16:20:38 +04:00
/*
* Copyright ( C ) 2010 Red Hat , Inc . All rights reserved .
*
* This file is part of LVM2 .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU General Public License v .2 .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*/
2009-01-12 21:45:44 +03:00
# include <unistd.h>
# include <stdio.h>
2015-04-20 11:05:27 +03:00
# include <stdlib.h>
2009-01-12 21:45:44 +03:00
# include <stdarg.h>
2011-01-05 18:03:43 +03:00
# include <string.h>
2009-01-12 21:45:44 +03:00
# include <sys/types.h>
# include <sys/wait.h>
2015-04-20 20:16:33 +03:00
static int _finished ( const char * cmd , int status , int pid ) {
int ret ;
2010-04-30 18:33:39 +04:00
if ( ! strcmp ( cmd , " not " ) )
return ! status ;
if ( ! strcmp ( cmd , " should " ) ) {
2015-04-20 20:16:33 +03:00
if ( status ) {
2010-05-12 09:55:42 +04:00
fprintf ( stderr , " TEST WARNING: Ignoring command failure. \n " ) ;
2015-04-20 20:16:33 +03:00
/* TODO: avoid using shell here */
/* Show log for failing command which should be passing */
ret = system ( " ls debug.log*${LVM_LOG_FILE_EPOCH}* 2>/dev/null " ) ;
if ( WIFEXITED ( ret ) & & WEXITSTATUS ( ret ) = = 0 ) {
printf ( " ## timing off \n <======== Debug log ========> \n " ) ; /* timing off */
fflush ( stdout ) ;
system ( " sed -e 's,^,## DEBUG: ,' debug.log*${LVM_LOG_FILE_EPOCH}* 2>/dev/null " ) ;
printf ( " ## timing on \n " ) ; /* timing on */
system ( " rm -f debug.log*${LVM_LOG_FILE_EPOCH}* " ) ;
fflush ( stdout ) ;
}
}
2010-04-30 18:33:39 +04:00
return 0 ;
2014-03-28 03:34:04 +04:00
} else if ( ! strcmp ( cmd , " invalid " ) ) {
if ( status = = 3 )
return 0 ;
fprintf ( stderr , " Test expected exit code 3 (invalid), but got %d. \n " , status ) ;
} else if ( ! strcmp ( cmd , " fail " ) ) {
if ( status = = 5 )
return 0 ;
fprintf ( stderr , " Test expected exit code 5 (fail), but got %d. \n " , status ) ;
2010-04-30 18:33:39 +04:00
}
return 6 ;
}
2009-01-12 21:45:44 +03:00
int main ( int args , char * * argv ) {
2015-04-20 11:05:27 +03:00
const char * val = NULL ;
2009-01-12 21:45:44 +03:00
pid_t pid ;
int status ;
int FAILURE = 6 ;
if ( args < 2 ) {
fprintf ( stderr , " Need args \n " ) ;
return FAILURE ;
}
pid = fork ( ) ;
if ( pid = = - 1 ) {
fprintf ( stderr , " Could not fork \n " ) ;
return FAILURE ;
} else if ( pid = = 0 ) { /* child */
2015-04-20 11:05:27 +03:00
if ( ! strcmp ( argv [ 0 ] , " not " ) )
2015-04-20 20:16:33 +03:00
val = " >1 " ;
2015-04-20 11:05:27 +03:00
else if ( ! strcmp ( argv [ 0 ] , " invalid " ) )
val = " 3 " ;
else if ( ! strcmp ( argv [ 0 ] , " fail " ) )
val = " 5 " ;
if ( val )
2015-04-20 20:16:33 +03:00
setenv ( " LVM_EXPECTED_EXIT_STATUS " , val , 1 ) ;
2015-04-20 11:05:27 +03:00
2009-01-12 21:45:44 +03:00
execvp ( argv [ 1 ] , & argv [ 1 ] ) ;
/* should not be accessible */
return FAILURE ;
} else { /* parent */
waitpid ( pid , & status , 0 ) ;
if ( ! WIFEXITED ( status ) ) {
2009-02-17 22:37:28 +03:00
if ( WIFSIGNALED ( status ) )
fprintf ( stderr ,
" Process %d died of signal %d. \n " ,
pid , WTERMSIG ( status ) ) ;
2009-01-12 21:45:44 +03:00
/* did not exit correctly */
return FAILURE ;
}
2010-04-30 18:33:39 +04:00
2015-04-20 20:16:33 +03:00
return _finished ( argv [ 0 ] , WEXITSTATUS ( status ) , pid ) ;
2009-01-12 21:45:44 +03:00
}
/* not accessible */
return FAILURE ;
}