2016-10-18 17:17:40 +05:00
|
|
|
#include <cstring>
|
|
|
|
#include <errno.h>
|
2017-10-17 19:29:56 +05:00
|
|
|
#include <iostream>
|
|
|
|
#include <nntpchan/exec_frontend.hpp>
|
2016-10-18 17:17:40 +05:00
|
|
|
#include <sys/wait.h>
|
2017-10-17 19:29:56 +05:00
|
|
|
#include <unistd.h>
|
2016-10-18 17:17:40 +05:00
|
|
|
|
|
|
|
namespace nntpchan
|
|
|
|
{
|
2018-05-06 19:21:58 +05:00
|
|
|
ExecFrontend::ExecFrontend(const std::string &fname, char * const* env) : m_Environ(env), m_exec(fname) {}
|
2016-10-18 17:17:40 +05:00
|
|
|
|
2017-10-17 19:29:56 +05:00
|
|
|
ExecFrontend::~ExecFrontend() {}
|
2016-10-18 17:17:40 +05:00
|
|
|
|
2017-10-17 19:29:56 +05:00
|
|
|
void ExecFrontend::ProcessNewMessage(const fs::path &fpath) { Exec({"post", fpath}); }
|
|
|
|
|
|
|
|
bool ExecFrontend::AcceptsNewsgroup(const std::string &newsgroup) { return Exec({"newsgroup", newsgroup}) == 0; }
|
2016-10-18 17:17:40 +05:00
|
|
|
|
2017-10-17 19:29:56 +05:00
|
|
|
bool ExecFrontend::AcceptsMessage(const std::string &msgid) { return Exec({"msgid", msgid}) == 0; }
|
|
|
|
|
|
|
|
int ExecFrontend::Exec(std::deque<std::string> args)
|
|
|
|
{
|
|
|
|
// set up arguments
|
|
|
|
const char **cargs = new char const *[args.size() + 2];
|
|
|
|
std::size_t l = 0;
|
|
|
|
cargs[l++] = m_exec.c_str();
|
|
|
|
while (args.size())
|
2016-10-18 17:17:40 +05:00
|
|
|
{
|
2017-10-17 19:29:56 +05:00
|
|
|
cargs[l++] = args.front().c_str();
|
|
|
|
args.pop_front();
|
2016-10-18 17:17:40 +05:00
|
|
|
}
|
2017-10-17 19:29:56 +05:00
|
|
|
cargs[l] = 0;
|
|
|
|
int retcode = 0;
|
|
|
|
pid_t child = fork();
|
|
|
|
if (child)
|
2016-10-18 17:17:40 +05:00
|
|
|
{
|
2017-10-17 19:29:56 +05:00
|
|
|
waitpid(child, &retcode, 0);
|
2016-10-18 17:17:40 +05:00
|
|
|
}
|
2017-10-17 19:29:56 +05:00
|
|
|
else
|
2016-10-18 17:17:40 +05:00
|
|
|
{
|
2018-05-06 19:21:58 +05:00
|
|
|
int r = execvpe(m_exec.c_str(), (char *const *)cargs, m_Environ);
|
2017-10-17 19:29:56 +05:00
|
|
|
if (r == -1)
|
|
|
|
{
|
|
|
|
std::cout << strerror(errno) << std::endl;
|
|
|
|
exit(errno);
|
2016-10-18 17:17:40 +05:00
|
|
|
}
|
2017-10-17 19:29:56 +05:00
|
|
|
else
|
|
|
|
exit(r);
|
2016-10-18 17:17:40 +05:00
|
|
|
}
|
2017-10-17 19:29:56 +05:00
|
|
|
return retcode;
|
|
|
|
}
|
2016-10-18 17:17:40 +05:00
|
|
|
}
|