vendredi 30 août 2019

Can't get segmentation fault exit code from boost child process

I am trying to get the exit code of a child process (using boost::process and boost::asio) when that child process is killed due to a segmentation violation or divide be zero or any other kill signal. The exit code and error code always return with 0 and success.

I am running this on CentOS 7 using g++ 4.8.5

If I run the same code with a child process that simply returns a non-zero exit code it successfully returns that exit code.

#include <iostream>
#include <boost/process.hpp>
#include <boost/asio/io_service.hpp>

namespace bp = boost::process;
using namespace std;

int main (int argc, char** argv)
{
   string exe = "./crashes";

   vector<string> data;
   boost::asio::io_service ios;

   int exit_code;
   error_code ec;
   future<string> ostr;

   bp::child c(exe,
               (bp::std_out & bp::std_err) > ostr,
               ios,
               bp::on_exit=[&exit_code, &ec](int exit, const error_code& ecin)
                                             {exit_code = exit; ec = ecin;});

   ios.run();

   cout << "Exit Code = " << exit_code << endl;
   cout << "Error Code = " << ec.message() << endl;
   cout << "child stdin & stderr:\n";
   cout << ostr.get() << endl;
   return exit_code;
}

and the crashes code

int main (int argc, char** argv)
{
   int* y = 0;
   int c = *y;
}

The results show a 0 exit code and Success error_code

Exit Code = 0
Error Code = Success
child stdin & stderr:


running the crashes executable alone returns an exit code of 139

bash-4.2$ ./crashes 
Segmentation fault (core dumped)
bash-4.2$ echo $?
139

Aucun commentaire:

Enregistrer un commentaire