几周后无法在linux上调用execve

时间:2018-11-26 12:45:21

标签: c++ linux embedded-linux

我遇到了一个非常奇怪的错误。我从C ++代码(execve)执行带有固定参数的命令。几周内一切正常。

最后,我在一个测试系统上遇到了一个错误。经过一些监视和测试,我了解到问题是二进制文件未执行。

我没有日志,也没有错误,只是没有调用二进制文件(为确保问题不在二进制文件上,我已经用日志脚本替换了目标二进制文件,并且他没有执行。)

重新启动已经解决了问题,但是我无法弄清楚为什么经过数十次固定的args命令执行失败。

我遇到了linux有一些限制吗?有失败的代码:

int spawnWithRet(const std::string& file, const std::vector<std::string>& args)
{
    auto procArgs = prepareProcessArguments(file, args);
    pid_t pID = fork();
    if (pID == 0)
    {
        execCmd(file, procArgs);
        exit(0);
    }
    else
    {
        int status = -1;
        // We are the parent process
        if (waitpid(pID, &status, 0) < 0)
        {
            return status;
        }

        return status;
    }
}


void execCmd(const std::string& file, const std::vector<char*>& args)
{
    execv(file.c_str(), args.data());
    // If we reach this statement the process launch failed.
    // We have no way to inform the parent because we are detached from it.
    // Just log and continue to exit.
    sdn::log::Logger m_lg;
    sdn::log::configureLogger(m_lg, "LinuxT", "spawn");
    BOOST_LOG_SEV(m_lg, sdn::log::Sev::trace) << "Failed to execute [" << file << "]: [" << strerror(errno) << "].";
}


std::vector<char*> prepareProcessArguments(const std::string& file, const std::vector<std::string>& args)
{
    // Prepare process arguments.
    auto procArgs = std::vector<char*>{};
    procArgs.reserve(args.size() + 2); // +2 to include process name + terminator arg.
    // First add the process.
    procArgs.push_back(const_cast<char*>(file.c_str()));
    // Then the args.
    std::transform(args.begin(), args.end(), std::back_inserter(procArgs), [](const std::string& s) { return const_cast<char*>(s.c_str()); });
    // Last argument: a NULL pointer.
    procArgs.push_back(NULL);
    return procArgs;
}

Linux版本: 3.14.49-ge9cd4cc819#2 Fri Oct 26 09:41:36 CEST 2018 armv7l GNU / Linux

命令: / usr / sbin / pppd / dev / ttyACM3 4000000 debug lcp-echo-failure 5 lcp-echo-interval 30 nodefaultroute noipdefault usepeerdns noauth unit 3 connect“

0 个答案:

没有答案