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 ,
2016-01-21 13:49:46 +03:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
2010-07-28 16:20:38 +04:00
*/
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 ) ;
2015-06-23 12:03:46 +03:00
if ( system ( " sed -e 's,^,## DEBUG: ,' debug.log*${LVM_LOG_FILE_EPOCH}* 2>/dev/null " ) ) {
/* Ignore result code */ ;
}
2015-04-20 20:16:33 +03:00
printf ( " ## timing on \n " ) ; /* timing on */
2015-06-23 12:03:46 +03:00
if ( system ( " rm -f debug.log*${LVM_LOG_FILE_EPOCH}* " ) ) {
/* Ignore result code */ ;
}
2015-04-20 20:16:33 +03:00
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 */
2016-09-13 14:23:47 +03:00
if ( ! strcmp ( argv [ 0 ] , " not " ) )
2015-04-20 20:16:33 +03:00
val = " >1 " ;
2016-09-13 14:23:47 +03:00
else if ( ! strcmp ( argv [ 0 ] , " invalid " ) )
2015-04-20 11:05:27 +03:00
val = " 3 " ;
else if ( ! strcmp ( argv [ 0 ] , " fail " ) )
val = " 5 " ;
if ( val )
2021-09-20 02:45:55 +03:00
( void ) setenv ( " LVM_EXPECTED_EXIT_STATUS " , val , 1 ) ;
2015-04-20 11:05:27 +03:00
2021-09-24 23:08:26 +03:00
/* coverity[os_cmd_sink] intentionally passing argv + 1 */
2009-01-12 21:45:44 +03:00
execvp ( argv [ 1 ] , & argv [ 1 ] ) ;
/* should not be accessible */
return FAILURE ;
} else { /* parent */
2021-09-20 11:31:45 +03:00
if ( waitpid ( pid , & status , 0 ) < 0 ) {
fprintf ( stderr , " Process %d failed on waitpid. \n " , pid ) ;
return FAILURE ;
}
2009-01-12 21:45:44 +03:00
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 ;
}