child_process.h
Go to the documentation of this file.
/*
* Copyright © 2013 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Thomas Voß <thomas.voss@canonical.com>
*/
#ifndef CORE_POSIX_CHILD_PROCESS_H_
#define CORE_POSIX_CHILD_PROCESS_H_
#include <core/posix/process.h>
#include <core/posix/standard_stream.h>
#include <core/posix/visibility.h>
#include <core/signal.h>
#include <iosfwd>
#include <functional>
namespace core
{
namespace posix
{
class CORE_POSIX_DLL_PUBLIC ChildProcess : public Process
{
public:
class DeathObserver
{
public:
static std::unique_ptr<DeathObserver> create_once_with_signal_trap(
std::shared_ptr<SignalTrap> trap);
DeathObserver(const DeathObserver&) = delete;
virtual ~DeathObserver() = default;
DeathObserver& operator=(const DeathObserver&) = delete;
bool operator==(const DeathObserver&) const = delete;
virtual bool add(const ChildProcess& child) = 0;
virtual bool has(const ChildProcess& child) const = 0;
virtual const core::Signal<ChildProcess>& child_died() const = 0;
virtual void on_sig_child() = 0;
protected:
DeathObserver() = default;
};
static ChildProcess invalid();
~ChildProcess();
wait::Result wait_for(const wait::Flags& flags);
std::istream& cerr();
std::ostream& cin();
std::istream& cout();
private:
friend ChildProcess fork(const std::function<posix::exit::Status()>&, const StandardStream&);
friend ChildProcess vfork(const std::function<posix::exit::Status()>&, const StandardStream&);
class CORE_POSIX_DLL_LOCAL Pipe
{
public:
static Pipe invalid();
Pipe();
Pipe(const Pipe& rhs);
~Pipe();
Pipe& operator=(const Pipe& rhs);
int read_fd() const;
void close_read_fd();
int write_fd() const;
void close_write_fd();
private:
Pipe(int fds[2]);
int fds[2];
};
CORE_POSIX_DLL_LOCAL ChildProcess(pid_t pid,
const Pipe& stdin,
const Pipe& stdout,
const Pipe& stderr);
struct CORE_POSIX_DLL_LOCAL Private;
std::shared_ptr<Private> d;
};
}
}
#endif // CORE_POSIX_CHILD_PROCESS_H_
core::posix::ChildProcess::DeathObserver
The DeathObserver class observes child process' states and emits a signal when a monitored child has ...
Definition child_process.h:57
core::posix::ChildProcess::DeathObserver::add
virtual bool add(const ChildProcess &child)=0
add adds the specified child to the list of observed child processes.
core::posix::ChildProcess::DeathObserver::operator=
DeathObserver & operator=(const DeathObserver &)=delete
core::posix::ChildProcess::DeathObserver::has
virtual bool has(const ChildProcess &child) const =0
has checks whether the specified child is observed.
core::posix::ChildProcess::DeathObserver::~DeathObserver
virtual ~DeathObserver()=default
core::posix::ChildProcess::DeathObserver::on_sig_child
virtual void on_sig_child()=0
Checks and reaps all child processes registered with the observer instance.
core::posix::ChildProcess::DeathObserver::operator==
bool operator==(const DeathObserver &) const =delete
core::posix::ChildProcess::DeathObserver::DeathObserver
DeathObserver(const DeathObserver &)=delete
core::posix::ChildProcess::DeathObserver::child_died
virtual const core::Signal< ChildProcess > & child_died() const =0
child_died is emitted whenever an observed child ceases to exist.
core::posix::ChildProcess::DeathObserver::DeathObserver
DeathObserver()=default
core::posix::ChildProcess
The Process class models a child process of this process.
Definition child_process.h:44
core::posix::Process
The Process class models a process and possible operations on it.
Definition process.h:45
core::posix::exit::Status
Status
The Status enum wrap's the posix exit status.
Definition exit.h:34
core::posix::wait::Flags
Flags
Flags enumerates different behavior when waiting for a child process to change state.
Definition wait.h:43
core::posix::vfork
CORE_POSIX_DLL_PUBLIC ChildProcess vfork(const std::function< posix::exit::Status()> &main, const StandardStream &flags)
fork vforks a new process and executes the provided main function in the newly forked process.
Definition fork.cpp:122
core::posix::fork
CORE_POSIX_DLL_PUBLIC ChildProcess fork(const std::function< posix::exit::Status()> &main, const StandardStream &flags)
fork forks a new process and executes the provided main function in the newly forked process.
Definition fork.cpp:57
core::posix::StandardStream
StandardStream
The StandardStream enum wraps the POSIX standard streams.
Definition standard_stream.h:34
core
Definition child_process.h:32
process.h
standard_stream.h
core::posix::wait::Result
The Result struct encapsulates the result of waiting for a process state change.
Definition wait.h:55
visibility.h
CORE_POSIX_DLL_PUBLIC
#define CORE_POSIX_DLL_PUBLIC
Definition visibility.h:26
CORE_POSIX_DLL_LOCAL
#define CORE_POSIX_DLL_LOCAL
Definition visibility.h:27