Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::process Class Reference

start and manage an local process More...

#include <process.hpp>

Inheritance diagram for fc::process:
Collaboration diagram for fc::process:

Classes

class  impl
 

Public Member Functions

 process ()
 
 ~process ()
 
virtual iprocessexec (const fc::path &exe, std::vector< std::string > args, const fc::path &work_dir=fc::path(), int opts=open_all)
 
virtual int result (const microseconds &timeout=microseconds::maximum())
 
virtual void kill ()
 
virtual fc::buffered_ostream_ptr in_stream ()
 returns a stream that writes to the process' stdin
 
virtual fc::buffered_istream_ptr out_stream ()
 returns a stream that reads from the process' stdout
 
virtual fc::buffered_istream_ptr err_stream ()
 returns a stream that reads from the process' stderr
 
- Public Member Functions inherited from fc::iprocess
virtual ~iprocess ()
 

Additional Inherited Members

- Public Types inherited from fc::iprocess
enum  exec_opts {
  open_none = 0 , open_stdin = 0x01 , open_stdout = 0x02 , open_stderr = 0x04 ,
  open_all = open_stdin|open_stdout|open_stderr , suppress_console = 0x08
}
 

Detailed Description

Note
this class implements reference semantics.

Definition at line 12 of file process.hpp.

Constructor & Destructor Documentation

◆ process()

fc::process::process ( )

Definition at line 71 of file process.cpp.

72:my( new process::impl() ){}

◆ ~process()

fc::process::~process ( )

Definition at line 73 of file process.cpp.

73{}

Member Function Documentation

◆ err_stream()

fc::buffered_istream_ptr fc::process::err_stream ( )
virtual

Implements fc::iprocess.

Definition at line 181 of file process.cpp.

181 {
182 return my->_err;
183}

◆ exec()

iprocess & fc::process::exec ( const fc::path & exe,
std::vector< std::string > args,
const fc::path & work_dir = fc::path(),
int opts = open_all )
virtual
Returns
*this

Implements fc::iprocess.

Definition at line 75 of file process.cpp.

78{
79
80 my->pctx.work_dir = work_dir.string();
81 my->pctx.suppress_console = (opt & suppress_console) != 0;
82
83 if( opt&open_stdout)
84 my->pctx.streams[boost::process::stdout_id] = bp::behavior::async_pipe();
85 else
86 my->pctx.streams[boost::process::stdout_id] = bp::behavior::null();
87
88
89 if( opt& open_stderr )
90 my->pctx.streams[boost::process::stderr_id] = bp::behavior::async_pipe();
91 else
92 my->pctx.streams[boost::process::stderr_id] = bp::behavior::null();
93
94 if( opt& open_stdin )
95 my->pctx.streams[boost::process::stdin_id] = bp::behavior::async_pipe();
96 else
97 my->pctx.streams[boost::process::stdin_id] = bp::behavior::close();
98
99 /*
100 std::vector<std::string> a;
101 a.reserve(size_t(args.size()));
102 for( uint32_t i = 0; i < args.size(); ++i ) {
103 a.push_back( fc::move(args[i]) );
104 }
105 */
106 my->child.reset( new bp::child( bp::create_child( exe.string(), fc::move(args), my->pctx ) ) );
107
108 if( opt & open_stdout ) {
109 bp::handle outh = my->child->get_handle( bp::stdout_id );
110 my->_outp.reset( new bp::pipe( fc::asio::default_io_service(), outh.release() ) );
111 }
112 if( opt & open_stderr ) {
113 bp::handle errh = my->child->get_handle( bp::stderr_id );
114 my->_errp.reset( new bp::pipe( fc::asio::default_io_service(), errh.release() ) );
115 }
116 if( opt & open_stdin ) {
117 bp::handle inh = my->child->get_handle( bp::stdin_id );
118 my->_inp.reset( new bp::pipe( fc::asio::default_io_service(), inh.release() ) );
119 }
120
121
122 promise<int>::ptr p(new promise<int>("process"));
123 my->stat.async_wait( my->child->get_id(), [=]( const boost::system::error_code& ec, int exit_code )
124 {
125 //slog( "process::result %d", exit_code );
126 if( !ec ) {
127 #ifdef BOOST_POSIX_API
128 if( WIFEXITED(exit_code) ) p->set_value( WEXITSTATUS(exit_code) );
129 else
130 {
131 p->set_exception(
132 fc::exception_ptr( new fc::exception(
133 FC_LOG_MESSAGE( error, "process exited with: ${message} ",
134 ("message", strsignal(WTERMSIG(exit_code))) ) ) ) );
135 }
136 #else
137 p->set_value(exit_code);
138 #endif
139 }
140 else
141 {
142 p->set_exception(
143 fc::exception_ptr( new fc::exception(
144 FC_LOG_MESSAGE( error, "process exited with: ${message} ",
145 ("message", boost::system::system_error(ec).what())) ) ) );
146 }
147 });
148 if( opt & open_stdin )
149 my->_in = std::make_shared<buffered_ostream>(std::make_shared<fc::asio::ostream<bp::pipe>>(my->_inp));
150 if( opt & open_stdout )
151 my->_out = std::make_shared<buffered_istream>(std::make_shared<fc::asio::istream<bp::pipe>>(my->_outp));
152 if( opt & open_stderr )
153 my->_err = std::make_shared<buffered_istream>(std::make_shared<fc::asio::istream<bp::pipe>>(my->_errp));
154 my->_exited = p;
155 return *this;
156}
const mie::Vuint & p
Definition bn.cpp:27
std::string string() const
Here is the call graph for this function:

◆ in_stream()

fc::buffered_ostream_ptr fc::process::in_stream ( )
virtual

Implements fc::iprocess.

Definition at line 168 of file process.cpp.

168 {
169 return my->_in;
170}

◆ kill()

void fc::process::kill ( )
virtual

Forcefully kills the process.

Implements fc::iprocess.

Definition at line 161 of file process.cpp.

161 {
162 my->child->terminate();
163}

◆ out_stream()

fc::buffered_istream_ptr fc::process::out_stream ( )
virtual

Implements fc::iprocess.

Definition at line 175 of file process.cpp.

175 {
176 return my->_out;
177}

◆ result()

int fc::process::result ( const microseconds & timeout = microseconds::maximum())
virtual
Returns
blocks until the process exits

Implements fc::iprocess.

Definition at line 185 of file process.cpp.

186{
187 return my->_exited.wait(timeout);
188}

The documentation for this class was generated from the following files: