use exec for lokinet-vpn

This commit is contained in:
jeff 2020-08-19 14:40:58 +00:00 committed by Jeff Becker
parent 3619356406
commit bc8e867dfb
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

View File

@ -5,6 +5,7 @@
#include <vector> #include <vector>
#include <array> #include <array>
#include <net/net.hpp> #include <net/net.hpp>
#include <sys/wait.h>
#ifdef _WIN32 #ifdef _WIN32
// add the unholy windows headers for iphlpapi // add the unholy windows headers for iphlpapi
@ -275,7 +276,35 @@ void
Execute(std::string cmd) Execute(std::string cmd)
{ {
std::cout << cmd << std::endl; std::cout << cmd << std::endl;
system(cmd.c_str()); std::vector<std::string> parts_str;
std::vector<const char *> parts_raw;
std::stringstream in(cmd);
for(std::string part; std::getline(in, part, ' '); )
{
if(part.empty())
continue;
parts_str.push_back(part);
}
for(const auto & part : parts_str)
{
parts_raw.push_back(part.c_str());
}
parts_raw.push_back(nullptr);
const auto pid = fork();
if(pid == -1)
{
throw std::runtime_error("failed to fork");
}
else if(pid == 0)
{
char * const * args = (char * const *) &parts_raw[1];
exit(execv(parts_raw[0], args));
}
else
{
waitpid(pid, 0, 0);
}
} }
void void