25#include <boost/make_shared.hpp>
28namespace ph = std::placeholders;
56class processSpawnImpl;
87 const std::string& executable,
90 const bool inherit_env);
112 pid_t
spawn(
bool dismiss);
151 class IOSignalSetInitializer {
162 boost::make_shared<IOSignalSet>(io_service,
163 std::bind(&ProcessSpawnImpl::waitForProcess, ph::_1,
165 io_signal_set_->add(SIGCHLD);
169 ~IOSignalSetInitializer() {
170 io_signal_set_->remove(SIGCHLD);
201 char* allocateInternal(
const std::string& src);
211 static void waitForProcess(
int signum, pid_t
const wpid = -1,
212 bool const sync =
false);
222 std::string executable_;
225 boost::shared_ptr<char*[]> args_;
228 boost::shared_ptr<char*[]> vars_;
231 typedef boost::shared_ptr<char[]> CStringPtr;
234 std::vector<CStringPtr> storage_;
240 static std::mutex mutex_;
244std::mutex ProcessSpawnImpl::mutex_;
246void ProcessSpawnImpl::IOSignalSetInitializer::initIOSignalSet(
IOServicePtr io_service) {
247 static IOSignalSetInitializer init(io_service);
251 const std::string& executable,
254 const bool inherit_env)
255 : mode_(mode), executable_(executable), args_(new char*[args.size() + 2]),
265 vars_size = i + vars.size();
267 vars_size = vars.size();
270 vars_ = boost::shared_ptr<char*[]>(
new char*[vars_size + 1]);
274 if (stat(executable_.c_str(), &st)) {
278 if (!(st.st_mode & S_IEXEC)) {
285 memset(args_.get(), 0, (args.size() + 2) *
sizeof(
char*));
286 memset(vars_.get(), 0, (vars_size + 1) *
sizeof(
char*));
288 args_[0] = allocateInternal(executable_);
290 for (
size_t i = 1; i <= args.size(); ++i) {
291 args_[i] = allocateInternal(args[i - 1]);
297 vars_[i] = allocateInternal(
environ[i]);
301 for (
size_t j = 0; j < vars.size(); ++j) {
302 vars_[i + j] = allocateInternal(vars[j]);
308 lock_guard<std::mutex> lk(mutex_);
309 process_collection_.erase(
this);
315 std::ostringstream s;
321 while (args_[i] != NULL) {
322 s <<
" " << args_[i];
330 lock_guard<std::mutex> lk(mutex_);
339 }
else if (pid == 0) {
343 pthread_sigmask(SIG_SETMASK, &sset, 0);
345 execve(executable_.c_str(), args_.get(), vars_.get());
358 waitForProcess(SIGCHLD, pid,
true);
366 lock_guard<std::mutex> lk(mutex_);
367 ProcessStates::const_iterator proc;
368 if (process_collection_.find(
this) == process_collection_.end() ||
369 (proc = process_collection_[
this].find(pid)) == process_collection_[
this].end()) {
372 return (proc->second->running_);
377 lock_guard<std::mutex> lk(mutex_);
378 if (process_collection_.find(
this) != process_collection_.end()) {
379 for (
auto const& proc : process_collection_[
this]) {
380 if (proc.second->running_) {
390 lock_guard<std::mutex> lk(mutex_);
391 ProcessStates::const_iterator proc;
392 if (process_collection_.find(
this) == process_collection_.end() ||
393 (proc = process_collection_[
this].find(pid)) == process_collection_[
this].end()) {
396 return (WEXITSTATUS(proc->second->status_));
400ProcessSpawnImpl::allocateInternal(
const std::string& src) {
401 const size_t src_len = src.length();
402 storage_.push_back(CStringPtr(
new char[src_len + 1]));
404 char* dest = storage_[storage_.size() - 1].get();
406 src.copy(dest, src_len);
408 dest[src_len] =
'\0';
413ProcessSpawnImpl::waitForProcess(
int ,
418 unique_lock<std::mutex> lk{mutex_, std::defer_lock};
428 pid_t pid = waitpid(wpid, &status, sync ? 0 : WNOHANG);
433 if (errno == EINTR) {
439 isc_throw(InvalidOperation,
"process with pid " << wpid <<
" has returned " << pid
440 <<
" from waitpid in sync mode, errno: "
442 }
else if (pid == 0) {
447 for (
auto const& instance : process_collection_) {
448 auto const& proc = instance.second.find(pid);
451 if (proc != instance.second.end()) {
452 proc->second->status_ = status;
453 proc->second->running_ =
false;
468 "process (pid: " << pid <<
") which is still running");
470 lock_guard<std::mutex> lk(mutex_);
471 if (process_collection_.find(
this) != process_collection_.end()) {
472 process_collection_[
this].erase(pid);
479 const std::string& executable,
482 const bool inherit_env )
488 return (impl_->getCommandLine());
493 return (impl_->spawn(dismiss));
498 return (impl_->isRunning(pid));
503 return (impl_->isAnyRunning());
508 return (impl_->getExitStatus(pid));
513 return (impl_->clearState(pid));
A generic exception that is thrown if a function is called in a prohibited way.
Exception thrown when error occurs during spawning a process.
Implementation of the ProcessSpawn class.
void clearState(const pid_t pid)
Removes the status of the process with a specified PID.
bool isAnyRunning() const
Checks if any of the spawned processes is still running.
std::string getCommandLine() const
Returns full command line, including arguments, for the process.
ProcessSpawnImpl(const ProcessSpawn::SpawnMode mode, const std::string &executable, const ProcessArgs &args, const ProcessEnvVars &vars, const bool inherit_env)
Constructor.
pid_t spawn(bool dismiss)
Spawn the new process.
~ProcessSpawnImpl()
Destructor.
bool isRunning(const pid_t pid) const
Checks if the process is still running.
int getExitStatus(const pid_t pid) const
Returns exit status of the process.
bool isAnyRunning() const
Checks if any of the spawned processes is still running.
int getExitStatus(const pid_t pid) const
Returns exit status of the process.
void clearState(const pid_t pid)
Removes the status of the process with a specified PID.
ProcessSpawn(const SpawnMode mode, const std::string &executable, const ProcessArgs &args=ProcessArgs(), const ProcessEnvVars &vars=ProcessEnvVars(), const bool inherit_env=false)
Constructor.
bool isRunning(const pid_t pid) const
Checks if the process is still running.
std::string getCommandLine() const
Returns full command line, including arguments, for the process.
pid_t spawn(bool dismiss=false)
Spawn the new process.
static isc::asiolink::IOServicePtr getIOService()
Get the I/O service.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A wrapper interface for the ASIO library.
std::map< const ProcessSpawnImpl *, ProcessStates > ProcessCollection
ProcessCollection container which stores all ProcessStates for each instance of ProcessSpawnImpl.
std::vector< std::string > ProcessArgs
Type of the container holding arguments of the executable being run as a background process.
boost::shared_ptr< ProcessState > ProcessStatePtr
Defines a pointer to a ProcessState.
std::vector< std::string > ProcessEnvVars
Type of the container holding environment variables of the executable being run as a background proce...
std::map< pid_t, ProcessStatePtr > ProcessStates
ProcessStates container which stores a ProcessState for each process identified by PID.
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
boost::shared_ptr< IOSignalSet > IOSignalSetPtr
Defines a pointer to an IOSignalSet.
Defines the logger used by the top-level component of kea-lfc.
bool running_
true until the exit status is collected
ProcessState()
Constructor.
int status_
0 or the exit status