diff --git a/libIRDB/include/utils.hpp b/libIRDB/include/utils.hpp index 224f26a3d06de28cc0af03bed8ce9993ed7389d9..6f4a9046e6a03b03bbdc2eb0d320ae03434ba2e2 100644 --- a/libIRDB/include/utils.hpp +++ b/libIRDB/include/utils.hpp @@ -120,6 +120,9 @@ inline Funct for_randomOrder_each(const IterType &b, const IterType & e, const F return callback; } +int command_to_stream(const std::string& command, std::ostream& stream); + + #endif diff --git a/libIRDB/src/core/address.cpp b/libIRDB/src/core/address.cpp index ea1cfe71e464c4dc51314cc29bb7ead62d6798d8..f06a0783a0aff1a79f45fa3386bf907a1171a6f2 100644 --- a/libIRDB/src/core/address.cpp +++ b/libIRDB/src/core/address.cpp @@ -22,6 +22,7 @@ #include <all.hpp> #include <utils.hpp> #include <stdlib.h> + using namespace libIRDB; using namespace std; diff --git a/libIRDB/src/core/file.cpp b/libIRDB/src/core/file.cpp index b7560f892ea421811aeafb8bbfb2b78c37b0f98b..7aa10476f6a13748430cde33781069b76d51e6f4 100644 --- a/libIRDB/src/core/file.cpp +++ b/libIRDB/src/core/file.cpp @@ -26,12 +26,11 @@ #include <fstream> #include <iostream> +#include <utils.hpp> + using namespace libIRDB; using namespace std; -static void ignore_result(int /* res */) { } - - File_t::File_t(const db_id_t &myfile_id, const db_id_t &my_orig_fid, const std::string &myurl, const std::string &myhash, const std::string &myarch, const int &myoid, @@ -82,7 +81,8 @@ void File_t::CreateTables() ehcss_table_name+" "+ tmpfile; - ignore_result(system(command.c_str())); + // ignore_result(system(command.c_str())); + command_to_stream(command,cout); std::ifstream t(tmpfile.c_str()); diff --git a/libIRDB/src/core/fileir.cpp b/libIRDB/src/core/fileir.cpp index 1eef33f0b9288e351001fcd05f7e96ea15fef63f..59ec13bcf49ea7734f7ec3b913527b09373e90a2 100644 --- a/libIRDB/src/core/fileir.cpp +++ b/libIRDB/src/core/fileir.cpp @@ -28,9 +28,6 @@ #include <stdlib.h> #include <sys/wait.h> #include <iomanip> -//#include <bea_deprecated.hpp> - - using namespace libIRDB; using namespace std; @@ -42,6 +39,29 @@ using namespace std; #undef EIP +int command_to_stream(const string& command, ostream& stream) +{ + auto redirect_command=command+" 2>&1 "; + auto buffer=array<char,128>(); + + std::cout << "Issuing subcommand: "<< command << std::endl; + auto pipe = popen(redirect_command.c_str(), "r"); + if (!pipe) + { + stream << "Couldn't start command:"<< strerror(errno) << endl; + return 1; + } + while (fgets(buffer.data(), 128, pipe) != NULL) + { + stream<<buffer.data(); + } + auto returnCode = pclose(pipe); + if(returnCode==-1) + stream << "Could not close pipe: "<< strerror(errno) << endl; + + std::cout << "Return code = "<<returnCode << std::endl; + return returnCode; +} static void UpdateEntryPoints( const std::map<db_id_t,Instruction_t*> &insnMap, @@ -192,14 +212,8 @@ void FileIR_t::AssembleRegistry() string binaryOutputFile = "tmp.bin"; string command = "rm -f " + assemblyFile + " " + binaryOutputFile; - int rt = system(command.c_str()); + auto actual_exit = command_to_stream(command, cout); // system(command.c_str()); - int actual_exit = -1; - //int actual_signal = -1; - - if (WIFEXITED(rt)) actual_exit = WEXITSTATUS(rt); - //else actual_signal = WTERMSIG(rt); - assert(actual_exit == 0); ofstream asmFile; @@ -219,14 +233,7 @@ void FileIR_t::AssembleRegistry() asmFile.close(); command = string("nasm ") + assemblyFile + string(" -o ") + binaryOutputFile; - rt = system(command.c_str()); - - actual_exit = -1; - //actual_signal = -1; - - if (WIFEXITED(rt)) actual_exit = WEXITSTATUS(rt); - //else actual_signal = WTERMSIG(rt); - + actual_exit = command_to_stream(command,cout); // system(command.c_str()); assert(actual_exit == 0); diff --git a/libIRDB/src/core/instruction.cpp b/libIRDB/src/core/instruction.cpp index 27a990218f4f83a65ef03faa8d418a325497700c..7a876821660082bf3116ac9b57082fdf200c7ade 100644 --- a/libIRDB/src/core/instruction.cpp +++ b/libIRDB/src/core/instruction.cpp @@ -24,15 +24,13 @@ #include <fstream> #include <sstream> #include <iomanip> +#include <utils.hpp> #undef EIP using namespace libIRDB; using namespace std; -static void ignore_result(int /* res */) { } - - Instruction_t::Instruction_t() : BaseObj_t(NULL), my_address(NULL), @@ -115,9 +113,9 @@ bool Instruction_t::Assemble(string assembly) //remove any preexisting assembly or nasm generated files string command = "rm -f " + assemblyFile; - ignore_result(system(command.c_str())); + command_to_stream(command,cout); command = "rm -f "+assemblyFile+".bin"; - ignore_result(system(command.c_str())); + command_to_stream(command,cout); ofstream asmFile; asmFile.open(assemblyFile.c_str()); @@ -132,7 +130,7 @@ bool Instruction_t::Assemble(string assembly) asmFile.close(); command = "nasm " + assemblyFile + " -o "+ binaryOutputFile; - ignore_result(system(command.c_str())); + command_to_stream(command,cout); ifstream binreader; unsigned int filesize; diff --git a/plugins_install/p1transform.sh b/plugins_install/p1transform.sh deleted file mode 100755 index c288c305cf6b7d1998a8d8de75101233b00a9225..0000000000000000000000000000000000000000 --- a/plugins_install/p1transform.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -PN_TIMEOUT_VALUE=21600 - -varid=$1 -shift - -$PEASOUP_HOME/tools/do_p1transform.sh $varid a.ncexe a.ncexe.annot $PEASOUP_HOME/tools/bed.sh $PN_TIMEOUT_VALUE $* - diff --git a/tools/thanos/thanos.cpp b/tools/thanos/thanos.cpp index 18766be55dc4ba9a848d97ff36e7c45200ee0901..b17231b568274eb75dddb2d40966322542bb0152 100644 --- a/tools/thanos/thanos.cpp +++ b/tools/thanos/thanos.cpp @@ -9,6 +9,8 @@ #include <fcntl.h> #include <fstream> #include <ctime> +#include <ext/stdio_filebuf.h> + using namespace std; @@ -18,7 +20,8 @@ using namespace Transform_SDK; #define ALLOF(a) begin(a),end(a) // global to be used like cout/cerr for writing to the logs -ofstream thanos_log; +int thanos_log_fd=-1; +ostream *thanos_log; ostream *real_cout; ostream *real_cerr; string thanos_path; @@ -69,22 +72,32 @@ PluginList_t getPlugins(const int argc, char const *const argv[]); int main(int argc, char* argv[]) { thanos_path=argv[0]; - ostream my_real_cerr(cerr.rdbuf()); - ostream my_real_cout(cout.rdbuf()); - real_cerr=&my_real_cerr; - real_cout=&my_real_cout; - thanos_log.open("logs/thanos.log", ofstream::out|ofstream::app); + auto new_stdout_fd=dup(STDOUT_FILENO); + auto new_stderr_fd=dup(STDERR_FILENO); + + __gnu_cxx::stdio_filebuf<char> stdout_filebuf(new_stdout_fd, ios::out); + __gnu_cxx::stdio_filebuf<char> stderr_filebuf(new_stderr_fd, ios::out); + + ostream my_real_cout(&stderr_filebuf); + ostream my_real_cerr(&stdout_filebuf); + real_cout=&my_real_cout; + real_cerr=&my_real_cerr; - if(!thanos_log) + auto thanos_log_fileptr=fopen("logs/thanos.log", "a+"); + if(!thanos_log_fileptr) { - cerr<<"Cannot open logs/thanos.log"<<endl; + *real_cerr<<"Cannot open logs/thanos.log"<<endl; exit(1); } - // catch any misc stuff to the thanos log - cout.rdbuf(thanos_log.rdbuf()); - cerr.rdbuf(thanos_log.rdbuf()); + thanos_log_fd=fileno(thanos_log_fileptr); + __gnu_cxx::stdio_filebuf<char> thanos_log_filebuf(thanos_log_fd, ios::out); + ostream thanos_log_stream(&thanos_log_filebuf); + thanos_log=&thanos_log_stream; + // make sure stuff goes to the log unless otherwise indicated by using real_cout + dup2(thanos_log_fd, STDOUT_FILENO); + dup2(thanos_log_fd, STDERR_FILENO); // get plugins auto argv_iter=1; @@ -110,8 +123,8 @@ int main(int argc, char* argv[]) // for now, usage is pretty strict to enable simple // parsing, because this program is only used by an // automated script - thanos_log << "Syntax error in arguments." << endl; - thanos_log << "USAGE: <thanos opts> (\"<step name> [-optional] [--step-args [ARGS]]\")+" << endl; + *thanos_log << "Syntax error in arguments." << endl; + *thanos_log << "USAGE: <thanos opts> (\"<step name> [-optional] [--step-args [ARGS]]\")+" << endl; return 1; } @@ -123,8 +136,8 @@ int main(int argc, char* argv[]) // if that returns failure AND the step is not optional if(result != 0 && !plugin->isOptional()) { - thanos_log << "A critical step failed: " << plugin->getStepName() << endl; - thanos_log << "If DEBUG_STEPS is not on, this failure could " + *thanos_log << "A critical step failed: " << plugin->getStepName() << endl; + *thanos_log << "If DEBUG_STEPS is not on, this failure could " << "be due to an earlier critical step." << endl; return 1; // critical step failed, abort } @@ -133,9 +146,9 @@ int main(int argc, char* argv[]) const int result = ThanosPlugin_t::saveChanges(); if(result != 0) { - thanos_log << "A critical step failed: " << (thanos_plugins.back())->getStepName() + *thanos_log << "A critical step failed: " << (thanos_plugins.back())->getStepName() << endl; - thanos_log << "If DEBUG_STEPS is not on, this failure could " + *thanos_log << "If DEBUG_STEPS is not on, this failure could " << "be due to an earlier critical step." << endl; return 1; // critical step failed, abort } @@ -211,7 +224,7 @@ int ThanosPlugin_t::runPlugin() static const char *const base_path = getenv("SECURITY_TRANSFORMS_HOME"); if(base_path == NULL) { - thanos_log << "Environment variables not set." << endl; + *thanos_log << "Environment variables not set." << endl; return -1; } static const auto plugin_path (string(base_path).append("/plugins_install/")); @@ -220,7 +233,7 @@ int ThanosPlugin_t::runPlugin() if(dlhdl == NULL) { const auto err=dlerror(); - thanos_log<<"Cannot open "<<step_name<<": "<<err<<endl; + *thanos_log<<"Cannot open "<<step_name<<": "<<err<<endl; return -1; } @@ -228,7 +241,7 @@ int ThanosPlugin_t::runPlugin() if(sym == NULL) { const auto err=dlerror(); - thanos_log<<"Cannot find GetTransformStep in "<<step_name<<": "<<err<<endl; + *thanos_log<<"Cannot find GetTransformStep in "<<step_name<<": "<<err<<endl; return -1; } @@ -239,17 +252,14 @@ int ThanosPlugin_t::runPlugin() static const char *const are_debugging = getenv("DEBUG_STEPS"); - - auto saved_cerrbuf = cerr.rdbuf(); - auto saved_coutbuf = cout.rdbuf(); - ofstream logfile; + auto logfile=(FILE*)nullptr; auto are_logging = !((bool) are_debugging); if(are_logging) { // setup logging auto logfile_path = "./logs/"+step_name+".log"; - logfile.open(logfile_path,ofstream::out); + logfile=fopen(logfile_path.c_str(), "a+"); if(!logfile) { *real_cout<<"Cannot open log file "<<logfile_path<<endl; @@ -257,8 +267,8 @@ int ThanosPlugin_t::runPlugin() } if(redirect_opt) { - cout.rdbuf(logfile.rdbuf()); - cerr.rdbuf(logfile.rdbuf()); + dup2(fileno(logfile), STDOUT_FILENO); + dup2(fileno(logfile), STDERR_FILENO); } } @@ -283,8 +293,10 @@ int ThanosPlugin_t::runPlugin() cout<< "#ATTRIBUTE step_exitcode="<<dec<<step_result<<endl; - cerr.rdbuf(saved_cerrbuf); - cout.rdbuf(saved_coutbuf); + dup2(thanos_log_fd, STDOUT_FILENO); + dup2(thanos_log_fd, STDERR_FILENO); + if(logfile) + fclose(logfile); the_step.reset(); // explicitly get rid of the handle to the library so we can close it. dlclose(dlhdl); @@ -315,7 +327,7 @@ int ThanosPlugin_t::executeStep(TransformStep_t& the_step, const bool are_debugg pqxxDB_t* pqxx_interface = shared_objects->getDBInterface(); if(step_optional) { - const int error = shared_objects->writeBackAll(&thanos_log); + const int error = shared_objects->writeBackAll(thanos_log); if(error) { return 1; // the failure must be from a critical step, abort @@ -355,7 +367,7 @@ int ThanosPlugin_t::executeStep(TransformStep_t& the_step, const bool are_debugg if(step_optional) { // write changes to DB to see if it succeeds - const int error = shared_objects->writeBackAll(&thanos_log); + const int error = shared_objects->writeBackAll(thanos_log); if(error) { // abort changes by resetting DB interface @@ -371,7 +383,7 @@ int ThanosPlugin_t::executeStep(TransformStep_t& the_step, const bool are_debugg else if(are_debugging) { // write changes to DB in case next step fails - const int error = shared_objects->writeBackAll(&thanos_log); + const int error = shared_objects->writeBackAll(thanos_log); if(error) { return 1; // critical step failed, abort @@ -391,7 +403,7 @@ int ThanosPlugin_t::executeStep(TransformStep_t& the_step, const bool are_debugg int ThanosPlugin_t::saveChanges() { pqxxDB_t* pqxx_interface = shared_objects->getDBInterface(); - const int error = shared_objects->writeBackAll(&thanos_log); + const int error = shared_objects->writeBackAll(thanos_log); if(error) { return 1; // critical step failed, abort