23#include <boost/make_shared.hpp>
26namespace ph = std::placeholders;
81 const std::string& executable,
105 pid_t
spawn(
bool dismiss);
144 class IOSignalSetInitializer {
154 io_signal_set_ = boost::make_shared<IOSignalSet>(io_service,
155 std::bind(&ProcessSpawnImpl::waitForProcess, ph::_1));
156 io_signal_set_->add(SIGCHLD);
160 ~IOSignalSetInitializer() {
161 io_signal_set_->remove(SIGCHLD);
192 char* allocateInternal(
const std::string& src);
201 static bool waitForProcess(
int signum);
207 std::string executable_;
210 boost::shared_ptr<char*[]> args_;
213 boost::shared_ptr<char*[]> vars_;
216 typedef boost::shared_ptr<char[]> CStringPtr;
219 std::vector<CStringPtr> storage_;
225 static std::mutex mutex_;
232std::mutex ProcessSpawnImpl::mutex_;
234void ProcessSpawnImpl::IOSignalSetInitializer::initIOSignalSet(
IOServicePtr io_service) {
235 static IOSignalSetInitializer init(io_service);
239 const std::string& executable,
242 : executable_(executable), args_(new char*[args.size() + 2]),
243 vars_(new char*[vars.size() + 1]), store_(false), io_service_(io_service) {
247 if (stat(executable_.c_str(), &st)) {
251 if (!(st.st_mode & S_IEXEC)) {
258 memset(args_.get(), 0, (args.size() + 2) *
sizeof(
char*));
259 memset(vars_.get(), 0, (vars.size() + 1) *
sizeof(
char*));
261 args_[0] = allocateInternal(executable_);
263 for (
int i = 1; i <= args.size(); ++i) {
264 args_[i] = allocateInternal(args[i - 1]);
267 for (
int i = 0; i < vars.size(); ++i) {
268 vars_[i] = allocateInternal(vars[i]);
274 lock_guard<std::mutex> lk(mutex_);
275 process_collection_.erase(
this);
281 std::ostringstream s;
287 while (args_[i] != NULL) {
288 s <<
" " << args_[i];
296 lock_guard<std::mutex> lk(mutex_);
297 ProcessSpawnImpl::IOSignalSetInitializer::initIOSignalSet(io_service_);
303 }
else if (pid == 0) {
307 pthread_sigmask(SIG_SETMASK, &sset, 0);
309 execve(executable_.c_str(), args_.get(), vars_.get());
325 lock_guard<std::mutex> lk(mutex_);
326 ProcessStates::const_iterator proc;
327 if (process_collection_.find(
this) == process_collection_.end() ||
328 (proc = process_collection_[
this].find(pid)) == process_collection_[
this].end()) {
330 <<
"' hasn't been spawned and it status cannot be"
333 return (proc->second->running_);
338 lock_guard<std::mutex> lk(mutex_);
339 if (process_collection_.find(
this) != process_collection_.end()) {
340 for (
auto const& proc : process_collection_[
this]) {
341 if (proc.second->running_) {
351 lock_guard<std::mutex> lk(mutex_);
352 ProcessStates::const_iterator proc;
353 if (process_collection_.find(
this) == process_collection_.end() ||
354 (proc = process_collection_[
this].find(pid)) == process_collection_[
this].end()) {
356 <<
"' hasn't been spawned and it status cannot be"
359 return (WEXITSTATUS(proc->second->status_));
363ProcessSpawnImpl::allocateInternal(
const std::string& src) {
364 const size_t src_len = src.length();
365 storage_.push_back(CStringPtr(
new char[src_len + 1]));
367 char* dest = storage_[storage_.size() - 1].get();
369 src.copy(dest, src_len);
371 dest[src_len] =
'\0';
376ProcessSpawnImpl::waitForProcess(
int) {
377 lock_guard<std::mutex> lk(mutex_);
380 pid_t pid = waitpid(-1, &status, WNOHANG);
384 for (
auto const& instance : process_collection_) {
385 auto const& proc = instance.second.find(pid);
388 if (proc != instance.second.end()) {
390 proc->second->status_ = status;
391 proc->second->running_ =
false;
402 "process (pid: " << pid <<
") which is still running");
404 lock_guard<std::mutex> lk(mutex_);
405 if (process_collection_.find(
this) != process_collection_.end()) {
406 process_collection_[
this].erase(pid);
411 const std::string& executable,
419 return (impl_->getCommandLine());
424 return (impl_->spawn(dismiss));
429 return (impl_->isRunning(pid));
434 return (impl_->isAnyRunning());
439 return (impl_->getExitStatus(pid));
444 return (impl_->clearState(pid));
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
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(IOServicePtr io_service, const std::string &executable, const ProcessArgs &args, const ProcessEnvVars &vars)
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.
ProcessSpawn(isc::asiolink::IOServicePtr io_service, const std::string &executable, const ProcessArgs &args=ProcessArgs(), const ProcessEnvVars &vars=ProcessEnvVars())
Constructor.
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.
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.
#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