无法从Boost子进程获取分段故障退出代码

时间:2019-08-30 19:56:11

标签: c++11 boost-asio boost-process

当子进程由于分段违规而被杀死或者除数为零或任何其他终止信号时,我试图获取子进程的退出代码(使用boost :: process和boost :: asio)。退出代码和错误代码始终返回0并成功。

我正在使用g ++ 4.8.5和boost 1.66在CentOS 7上运行它

如果我在子进程中运行相同的代码,而该子进程仅返回一个非零的退出代码,则它会成功返回该退出代码。

#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;
}

和崩溃代码

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

结果显示退出代码为0和成功错误代码

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

仅运行崩溃可执行文件将返回139的退出代码

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

1 个答案:

答案 0 :(得分:3)

进程终止和退出代码的详细信息取决于平台。

增强处理过程以解决默认接口中的差异:调用on_exit处理程序时,返回状态为boost::process::detail::posix::eval_exit_status()的结果,这意味着:

inline int eval_exit_status(int code)
{
    if (WIFEXITED(code))
    {
        return WEXITSTATUS(code);
    }
    else if (WIFSIGNALED(code))
    {
        return WTERMSIG(code);
    }
    else
    {
        return code;
    }
}

因此,您会得到“退出代码11”,表示段错误...如果您想真正知道,可以查看native_exit_code()

bp::on_exit = [&result, &c](int /*ignored*/, const std::error_code &ec) {
    auto exit_status = c.native_exit_code();
    result.exit_code = boost::make_optional(WIFEXITED(exit_status), WEXITSTATUS(exit_status));
    result.signal    = boost::make_optional(WIFSIGNALED(exit_status), WTERMSIG(exit_status));
    result.ec = ec;
}

现在,假定结果变量有所更改。完整列表:

列表

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

namespace bp = boost::process;

int main(int argc, char**) {
    std::string exe = argc>1? "./ltua" : "./crashes";

    boost::asio::io_service ios;

    struct {
        boost::optional<int> exit_code;
        boost::optional<int> signal;
        std::error_code ec{};
    } result;

    std::future<std::string> ostr;

    bp::group g;
    bp::child c(exe, g, (bp::std_out & bp::std_err) > ostr, ios,
        bp::on_exit = [&result, &c](int /*ignored*/, const std::error_code &ec) {
            auto exit_status = c.native_exit_code();
            result.exit_code = boost::make_optional(WIFEXITED(exit_status), WEXITSTATUS(exit_status));
            result.signal    = boost::make_optional(WIFSIGNALED(exit_status), WTERMSIG(exit_status));
            result.ec = ec;
        });

    //g.wait();
    ios.run();

    if (result.exit_code) {
        std::cout << "Exited with " << *result.exit_code << std::endl;
    }
    if (result.signal) {
        std::cout << "Signaled with sginal #" << *result.signal << ", aka " << ::strsignal(*result.signal) <<  std::endl;
    }

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

输出

ltua.cpp一起运行时:

#include <iostream>
int main() {
    std::cout << "so  long"   << std::end;
    std::cerr << "and thanks" << std::end;
    std::cout << "for all"    << std::end;
    std::cerr << "the fish"   << std::end;
    return 42;
}

打印

Exited with 42
Error Code = Success
child stdin & stderr:
so  long
and thanks
for all
the fish

使用crashes.cpp

int main() {
    int *y = 0;
    int c = *y;
}

打印

Signaled with sginal #11, aka Segmentation fault
Error Code = Success
child stdin & stderr: