void Dumping::exec_command()

in src/filtration/dumping.cpp [63:106]


void Dumping::exec_command() const
{
    if(command.empty()) return;

    NST::utils::Log::flush(); // flush buffer

    if(pid_t pid = fork()) // spawn child process
    {
        // parent process
        LOG("Try to execute(%s %s) in %u child process", command.c_str(), name.c_str(), pid);
        NST::utils::Log::flush(); // flush buffer
        return;
    }
    else
    {
        // child process
        std::istringstream       ss(command);
        std::vector<std::string> tokens;
        std::vector<char*>       args;

        // TODO: this parser doesn't work with dual quotes, like rm "a file.cpp"
        for(std::string arg; ss >> arg;)
        {
            tokens.emplace_back(arg);
        }

        for(const std::string &token : tokens)
        {
            args.emplace_back(const_cast<char*>(token.c_str()));
        }

        args.push_back(const_cast<char*>(name.c_str()));
        args.push_back(nullptr); // need termination null pointer

        NST::utils::Log::flush(); // flush buffer
        if(execvp(args[0], &args[0]) == -1)
        {
            LOG("execvp(%s,%s %s) return: %s", args[0], command.c_str(), name.c_str(), strerror(errno));
        }

        LOG("child process %u will be terminated.", getpid());
        std::terminate();
    }
}