From 9740259fedfb807a23951d79a18d2d1a57e2cc1a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Radovan=20=C4=8Cerven=C3=BD?= <radovan.cerveny@gmail.com>
Date: Sun, 6 Mar 2016 10:30:52 +0100
Subject: [PATCH] work in progress

---
 .../provisioner/MeasurementProvisioner.cpp    | 45 ++++++++++++++++---
 ameasure2/src/ameasure.cpp                    |  2 +-
 2 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/alib2measurepp/src/provisioner/MeasurementProvisioner.cpp b/alib2measurepp/src/provisioner/MeasurementProvisioner.cpp
index 7e859ba784..5b11ac7461 100644
--- a/alib2measurepp/src/provisioner/MeasurementProvisioner.cpp
+++ b/alib2measurepp/src/provisioner/MeasurementProvisioner.cpp
@@ -116,6 +116,8 @@ MPRPipelineResults MeasurementProvisioner::runPipeline ( const MPPipeline & pipe
 
 	MPRPipelineResults pipelineResults { };
 
+	try {
+
 	for ( MPPipeline::const_iterator mppcIter = pipeline.cbegin ( ); mppcIter != pipeline.cend ( ); ++mppcIter ) {
 		if ( mppcIter == pipeline.cbegin ( ) ) {
 			dup ( inputTmpfileFd );
@@ -129,23 +131,24 @@ MPRPipelineResults MeasurementProvisioner::runPipeline ( const MPPipeline & pipe
 			dup ( inputTmpfileFd );
 
 			 // rewind previous stdout to the beginning, so the next command can read the whole file as input
-			lseek ( 0, SEEK_SET, 0 );
+			lseek ( 0, 0, SEEK_SET );
 
 			 // destroy the contents of previous stdin and rewind to the beginning
+			lseek ( 1, 0, SEEK_SET );
 			ftruncate ( 1, 0 );
-			lseek ( 1, SEEK_SET, 0 );
 		}
 
 		string command = mppcIter->substitute ( substitutionMap );
 
 		ofdlog << "| \t" << mppcIter->getRawCommand ( ) << flush;
 
+
 		 // leave the execution to the system shell
 		int status = system ( command.c_str ( ) );
 
 		if ( WEXITSTATUS ( status ) != 0 ) {
 			 // rewind stderr to read the error message
-			lseek ( 2, SEEK_SET, 0 );
+			lseek ( 2, 0, SEEK_SET );
 			ifdstream errfds ( 2 );
 
 			 // read the whole stderr
@@ -157,18 +160,18 @@ MPRPipelineResults MeasurementProvisioner::runPipeline ( const MPPipeline & pipe
 			pipelineResults.pipelineStatus.errorValue  = buffer.str ( );
 
 			 // destroy stderr contents
+			lseek ( 2, 0, SEEK_SET );
 			ftruncate ( 2, 0 );
-			lseek ( 2, SEEK_SET, 0 );
 
 			ofdlog << " ERROR!" << endl;
 			break;
 		}
 
-		ofdlog << " OK!" << endl;
 
 		if ( mppcIter->getMeasure ( ) ) {
 			 // since we are at the end of file after the output, we need to rewind
-			lseek ( measurementsFd, SEEK_SET, 0 );
+			lseek ( measurementsFd, 0, SEEK_SET );
+
 			ifdstream ifd ( measurementsFd );
 
 			deque < sax::Token > tokens;
@@ -179,9 +182,37 @@ MPRPipelineResults MeasurementProvisioner::runPipeline ( const MPPipeline & pipe
 			pipelineResults.commandResults.push_back ( { mppcIter->getRawCommand ( ), std::move ( measurementResults ) } );
 
 			 // destroy the contents and rewind for another commands measurements
+			lseek ( measurementsFd, 0, SEEK_SET );
 			ftruncate ( measurementsFd, 0 );
-			lseek ( measurementsFd, SEEK_SET, 0 );
 		}
+
+		ofdlog << " OK!" << endl;
+	}
+
+	}	catch(::exception::CommonException & ce) {
+			 // close tmp files, which results in their deletion
+		if ( measurementsFd != measurementsTmpfileFd )
+			close ( measurementsFd );
+
+		//MPUtils::closeShmFile ( measurementsTmpfile.second );
+		//MPUtils::closeShmFile ( inputTmpfile.second );
+		//MPUtils::closeShmFile ( outputTmpfile.second );
+		//MPUtils::closeShmFile ( errorTmpfile.second );
+
+		 // restore our main stdin, stdout, stderr
+		close ( 0 );
+		close ( 1 );
+		close ( 2 );
+
+		dup ( curStdinFd );
+		dup ( curStdoutFd );
+		dup ( curStderrFd );
+
+		close ( curStdinFd );
+		close ( curStdoutFd );
+		close ( curStderrFd );
+
+		throw ce;
 	}
 
 	 // close tmp files, which results in their deletion
diff --git a/ameasure2/src/ameasure.cpp b/ameasure2/src/ameasure.cpp
index 78c06016b0..c6502bc81f 100644
--- a/ameasure2/src/ameasure.cpp
+++ b/ameasure2/src/ameasure.cpp
@@ -34,7 +34,7 @@ int main ( int argc, char * * argv ) {
 		return 0;
 
 	} catch ( const exception::CommonException & exception ) {
-		std::cerr << "CommonException caught: " << exception.getCause ( ) << std::endl;
+		std::cerr << "CommonException caught: " << exception.what ( ) << std::endl << exception.getCause() << std::endl;
 		return 4;
 	} catch ( const TCLAP::ArgException & exception ) {
 		std::cout << exception.error ( ) << std::endl;
-- 
GitLab