25#include <boost/make_shared.hpp>
28namespace ph = std::placeholders;
56class processSpawnImpl;
87 const std::string& executable,
90 const bool inherit_env);
98 std::string
getCommandLine(std::unordered_set<std::string> redact_args = {})
const;
114 pid_t
spawn(
bool dismiss);
153 class IOSignalSetInitializer {
159 IOSignalSetInitializer(
IOServicePtr io_service) : io_service_(io_service) {
164 boost::make_shared<IOSignalSet>(io_service,
165 std::bind(&ProcessSpawnImpl::waitForProcess, ph::_1,
167 io_signal_set_->add(SIGCHLD);
171 ~IOSignalSetInitializer() {
172 io_signal_set_->remove(SIGCHLD);
173 io_signal_set_.reset();
174 io_service_->stopAndPoll();
208 char* allocateInternal(
const std::string& src);
218 static void waitForProcess(
int signum, pid_t
const wpid = -1,
219 bool const sync =
false);
229 std::string executable_;
232 boost::shared_ptr<char*[]> args_;
235 boost::shared_ptr<char*[]> vars_;
238 typedef boost::shared_ptr<char[]> CStringPtr;
241 std::vector<CStringPtr> storage_;
247 static std::mutex mutex_;
251std::mutex ProcessSpawnImpl::mutex_;
253void ProcessSpawnImpl::IOSignalSetInitializer::initIOSignalSet(
IOServicePtr io_service) {
254 static IOSignalSetInitializer init(io_service);
258 const std::string& executable,
261 const bool inherit_env)
262 : mode_(mode), executable_(executable), args_(new char*[args.size() + 2]),
272 vars_size = i + vars.size();
274 vars_size = vars.size();
277 vars_ = boost::shared_ptr<char*[]>(
new char*[vars_size + 1]);
281 if (stat(executable_.c_str(), &st)) {
285 if (!(st.st_mode & S_IEXEC)) {
292 memset(args_.get(), 0, (args.size() + 2) *
sizeof(
char*));
293 memset(vars_.get(), 0, (vars_size + 1) *
sizeof(
char*));
295 args_[0] = allocateInternal(executable_);
297 for (
size_t i = 1; i <= args.size(); ++i) {
298 args_[i] = allocateInternal(args[i - 1]);
304 vars_[i] = allocateInternal(
environ[i]);
308 for (
size_t j = 0; j < vars.size(); ++j) {
309 vars_[i + j] = allocateInternal(vars[j]);
315 lock_guard<std::mutex> lk(mutex_);
316 process_collection_.erase(
this);
322 std::ostringstream s;
328 bool redact_next =
false;
329 while (args_[i] != NULL) {
334 if (redact_args.count(args_[i])) {
338 s <<
" " << args_[i];
348 lock_guard<std::mutex> lk(mutex_);
357 }
else if (pid == 0) {
361 pthread_sigmask(SIG_SETMASK, &sset, 0);
363 execve(executable_.c_str(), args_.get(), vars_.get());
376 waitForProcess(SIGCHLD, pid,
true);
384 lock_guard<std::mutex> lk(mutex_);
385 ProcessStates::const_iterator proc;
386 if (process_collection_.find(
this) == process_collection_.end() ||
387 (proc = process_collection_[
this].find(pid)) == process_collection_[
this].end()) {
390 return (proc->second->running_);
395 lock_guard<std::mutex> lk(mutex_);
396 if (process_collection_.find(
this) != process_collection_.end()) {
397 for (
auto const& proc : process_collection_[
this]) {
398 if (proc.second->running_) {
408 lock_guard<std::mutex> lk(mutex_);
409 ProcessStates::const_iterator proc;
410 if (process_collection_.find(
this) == process_collection_.end() ||
411 (proc = process_collection_[
this].find(pid)) == process_collection_[
this].end()) {
414 return (WEXITSTATUS(proc->second->status_));
418ProcessSpawnImpl::allocateInternal(
const std::string& src) {
419 const size_t src_len = src.length();
420 storage_.push_back(CStringPtr(
new char[src_len + 1]));
422 char* dest = storage_[storage_.size() - 1].get();
424 src.copy(dest, src_len);
426 dest[src_len] =
'\0';
431ProcessSpawnImpl::waitForProcess(
int ,
436 unique_lock<std::mutex> lk{mutex_, std::defer_lock};
446 pid_t pid = waitpid(wpid, &status, sync ? 0 : WNOHANG);
451 if (errno == EINTR) {
457 isc_throw(InvalidOperation,
"process with pid " << wpid <<
" has returned " << pid
458 <<
" from waitpid in sync mode, errno: "
460 }
else if (pid == 0) {
465 for (
auto const& instance : process_collection_) {
466 auto const& proc = instance.second.find(pid);
469 if (proc != instance.second.end()) {
470 proc->second->status_ = status;
471 proc->second->running_ =
false;
486 "process (pid: " << pid <<
") which is still running");
488 lock_guard<std::mutex> lk(mutex_);
489 if (process_collection_.find(
this) != process_collection_.end()) {
490 process_collection_[
this].erase(pid);
497 const std::string& executable,
500 const bool inherit_env )
506 return (impl_->getCommandLine(redact_args));
511 return (impl_->spawn(dismiss));
516 return (impl_->isRunning(pid));
521 return (impl_->isAnyRunning());
526 return (impl_->getExitStatus(pid));
531 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.
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.
std::string getCommandLine(std::unordered_set< std::string > redact_args={}) const
Returns full command line, including arguments, for 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.
std::string getCommandLine(std::unordered_set< std::string > redact_args={}) const
Returns full command line, including arguments, for the process.
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.
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.
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