1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PluginScript.cc
12 #include <sys/types.h>
18 #include "zypp/base/LogTools.h"
19 #include "zypp/base/DefaultIntegral.h"
20 #include "zypp/base/String.h"
21 #include "zypp/base/Signal.h"
22 #include "zypp/base/IOStream.h"
24 #include "zypp/PluginScript.h"
25 #include "zypp/ExternalProgram.h"
26 #include "zypp/PathInfo.h"
30 ///////////////////////////////////////////////////////////////////
32 { /////////////////////////////////////////////////////////////////
36 const char * PLUGIN_DEBUG = getenv( "ZYPP_PLUGIN_DEBUG" );
38 /** Dump buffer string if PLUGIN_DEBUG is on.
41 struct PluginDebugBuffer
43 PluginDebugBuffer( const std::string & buffer_r ) : _buffer( buffer_r ) {}
48 if ( _buffer.empty() )
50 _DBG("PLUGIN") << "< (empty)" << endl;
54 std::istringstream datas( _buffer );
55 iostr::copyIndent( datas, _DBG("PLUGIN"), "< " ) << endl;
59 const std::string & _buffer;
62 /** Dump programms stderr.
65 struct PluginDumpStderr
67 PluginDumpStderr( ExternalProgramWithStderr & prog_r ) : _prog( prog_r ) {}
71 while ( _prog.stderrGetline( line ) )
72 _WAR("PLUGIN") << "! " << line << endl;
74 ExternalProgramWithStderr & _prog;
77 inline void setBlocking( FILE * file_r, bool yesno_r = true )
80 ZYPP_THROW( PluginScriptException( "setNonBlocking" ) );
82 int fd = ::fileno( file_r );
84 ZYPP_THROW( PluginScriptException( "setNonBlocking" ) );
86 int flags = ::fcntl( fd, F_GETFL );
88 ZYPP_THROW( PluginScriptException( "setNonBlocking" ) );
92 else if ( flags & O_NONBLOCK )
95 flags = ::fcntl( fd, F_SETFL, flags );
97 ZYPP_THROW( PluginScriptException( "setNonBlocking" ) );
100 inline void setNonBlocking( FILE * file_r, bool yesno_r = true )
101 { setBlocking( file_r, !yesno_r ); }
104 ///////////////////////////////////////////////////////////////////
106 // CLASS NAME : PluginScript::Impl
108 /** \ref PluginScript implementation. */
109 struct PluginScript::Impl
112 Impl( const Pathname & script_r = Pathname(), const Arguments & args_r = Arguments() )
113 : _sendTimeout( _defaultSendTimeout )
114 , _receiveTimeout( _defaultReceiveTimeout )
115 , _script( script_r )
120 { try { close(); } catch(...) {} }
123 static long _defaultSendTimeout;
124 static long _defaultReceiveTimeout;
127 long _receiveTimeout;
130 const Pathname & script() const
133 const Arguments & args() const
137 { return _cmd ? _cmd->getpid() : NotConnected; }
140 { return _cmd != nullptr; }
142 int lastReturn() const
143 { return _lastReturn; }
145 const std::string & lastExecError() const
146 { return _lastExecError; }
149 void open( const Pathname & script_r = Pathname(), const Arguments & args_r = Arguments() );
153 void send( const PluginFrame & frame_r ) const;
155 PluginFrame receive() const;
160 scoped_ptr<ExternalProgramWithStderr> _cmd;
161 DefaultIntegral<int,0> _lastReturn;
162 std::string _lastExecError;
164 ///////////////////////////////////////////////////////////////////
166 /** \relates PluginScrip::Impl Stream output */
167 inline std::ostream & operator<<( std::ostream & str, const PluginScript::Impl & obj )
169 return dumpRangeLine( str << "PluginScript[" << obj.getPid() << "] " << obj.script(),
170 obj.args().begin(), obj.args().end() );
173 ///////////////////////////////////////////////////////////////////
177 const long PLUGIN_TIMEOUT = str::strtonum<long>( getenv( "ZYPP_PLUGIN_TIMEOUT" ) );
178 const long PLUGIN_SEND_TIMEOUT = str::strtonum<long>( getenv( "ZYPP_PLUGIN_SEND_TIMEOUT" ) );
179 const long PLUGIN_RECEIVE_TIMEOUT = str::strtonum<long>( getenv( "ZYPP_PLUGIN_RECEIVE_TIMEOUT" ) );
182 long PluginScript::Impl::_defaultSendTimeout = ( PLUGIN_SEND_TIMEOUT > 0 ? PLUGIN_SEND_TIMEOUT
183 : ( PLUGIN_TIMEOUT > 0 ? PLUGIN_TIMEOUT : 30 ) );
184 long PluginScript::Impl::_defaultReceiveTimeout = ( PLUGIN_RECEIVE_TIMEOUT > 0 ? PLUGIN_RECEIVE_TIMEOUT
185 : ( PLUGIN_TIMEOUT > 0 ? PLUGIN_TIMEOUT : 30 ) );
187 ///////////////////////////////////////////////////////////////////
189 void PluginScript::Impl::open( const Pathname & script_r, const Arguments & args_r )
191 dumpRangeLine( DBG << "Open " << script_r, args_r.begin(), args_r.end() ) << endl;
194 ZYPP_THROW( PluginScriptException( "Already connected", str::Str() << *this ) );
197 PathInfo pi( script_r );
198 if ( ! ( pi.isFile() && pi.isX() ) )
199 ZYPP_THROW( PluginScriptException( "Script is not executable", str::Str() << pi ) );
202 // go and launch script
203 // TODO: ExternalProgram::maybe use Stderr_To_FileDesc for script loging
205 args.reserve( args_r.size()+1 );
206 args.push_back( script_r.asString() );
207 args.insert( args.end(), args_r.begin(), args_r.end() );
208 _cmd.reset( new ExternalProgramWithStderr( args ) );
210 // Be protected against full pipe, etc.
211 setNonBlocking( _cmd->outputFile() );
212 setNonBlocking( _cmd->inputFile() );
214 // store running scripts data
218 _lastExecError.clear();
220 DBG << *this << endl;
223 int PluginScript::Impl::close()
227 DBG << "Close:" << *this << endl;
230 // do not kill script if _DISCONNECT is ACKed.
231 send( PluginFrame( "_DISCONNECT" ) );
232 PluginFrame ret( receive() );
233 if ( ret.isAckCommand() )
236 str::strtonum( ret.getHeaderNT( "exit" ), _lastReturn.get() );
237 _lastExecError = ret.body();
246 _lastReturn = _cmd->close();
247 _lastExecError = _cmd->execError();
249 DBG << *this << " -> [" << _lastReturn << "] " << _lastExecError << endl;
255 void PluginScript::Impl::send( const PluginFrame & frame_r ) const
258 ZYPP_THROW( PluginScriptNotConnected( "Not connected", str::Str() << *this ) );
260 if ( frame_r.command().empty() )
261 WAR << "Send: No command in frame" << frame_r << endl;
263 // prepare frame data to write
266 std::ostringstream datas;
267 frame_r.writeTo( datas );
268 datas.str().swap( data );
270 DBG << "->send " << frame_r << endl;
274 std::istringstream datas( data );
275 iostr::copyIndent( datas, _DBG("PLUGIN") ) << endl;
278 // try writing the pipe....
279 FILE * filep = _cmd->outputFile();
281 ZYPP_THROW( PluginScriptException( "Bad file pointer." ) );
283 int fd = ::fileno( filep );
285 ZYPP_THROW( PluginScriptException( "Bad file descriptor" ) );
287 //DBG << " ->[" << fd << " " << (::feof(filep)?'e':'_') << (::ferror(filep)?'F':'_') << "]" << endl;
289 PluginDumpStderr _dump( *_cmd ); // dump scripts stderr before leaving
290 SignalSaver sigsav( SIGPIPE, SIG_IGN );
291 const char * buffer = data.c_str();
292 ssize_t buffsize = data.size();
299 tv.tv_sec = _sendTimeout;
302 int retval = select( fd+1, NULL, &wfds, NULL, &tv );
303 if ( retval > 0 ) // FD_ISSET( fd, &wfds ) will be true.
305 //DBG << "Ready to write..." << endl;
306 ssize_t ret = ::write( fd, buffer, buffsize );
307 if ( ret == buffsize )
309 //DBG << "::write(" << buffsize << ") -> " << ret << endl;
315 //WAR << "::write(" << buffsize << ") -> " << ret << " INCOMPLETE..." << endl;
318 buffer += ret; // -> continue
320 else // ( retval == -1 )
322 if ( errno != EINTR )
324 ERR << "write(): " << Errno() << endl;
325 if ( errno == EPIPE )
326 ZYPP_THROW( PluginScriptDiedUnexpectedly( "Send: script died unexpectedly", str::Str() << Errno() ) );
328 ZYPP_THROW( PluginScriptException( "Send: send error", str::Str() << Errno() ) );
332 else if ( retval == 0 )
334 WAR << "Not ready to write within timeout." << endl;
335 ZYPP_THROW( PluginScriptSendTimeout( "Not ready to write within timeout." ) );
337 else // ( retval == -1 )
339 if ( errno != EINTR )
341 ERR << "select(): " << Errno() << endl;
342 ZYPP_THROW( PluginScriptException( "Error waiting on file descriptor", str::Str() << Errno() ) );
349 PluginFrame PluginScript::Impl::receive() const
352 ZYPP_THROW( PluginScriptNotConnected( "Not connected", str::Str() << *this ) );
354 // try reading the pipe....
355 FILE * filep = _cmd->inputFile();
357 ZYPP_THROW( PluginScriptException( "Bad file pointer." ) );
359 int fd = ::fileno( filep );
361 ZYPP_THROW( PluginScriptException( "Bad file descriptor" ) );
366 PluginDebugBuffer _debug( data ); // dump receive buffer if PLUGIN_DEBUG
367 PluginDumpStderr _dump( *_cmd ); // dump scripts stderr before leaving
369 int ch = fgetc( filep );
372 data.push_back( ch );
376 else if ( ::feof( filep ) )
378 WAR << "Unexpected EOF" << endl;
379 ZYPP_THROW( PluginScriptDiedUnexpectedly( "Receive: script died unexpectedly", str::Str() << Errno() ) );
381 else if ( errno != EINTR )
383 if ( errno == EWOULDBLOCK )
385 // wait a while for fd to become ready for reading...
391 tv.tv_sec = _receiveTimeout;
394 int retval = select( fd+1, &rfds, NULL, NULL, &tv );
395 if ( retval > 0 ) // FD_ISSET( fd, &rfds ) will be true.
399 else if ( retval == 0 )
401 WAR << "Not ready to read within timeout." << endl;
402 ZYPP_THROW( PluginScriptReceiveTimeout( "Not ready to read within timeout." ) );
404 else // ( retval == -1 )
406 if ( errno != EINTR )
408 ERR << "select(): " << Errno() << endl;
409 ZYPP_THROW( PluginScriptException( "Error waiting on file descriptor", str::Str() << Errno() ) );
415 ERR << "read(): " << Errno() << endl;
416 ZYPP_THROW( PluginScriptException( "Receive: receive error", str::Str() << Errno() ) );
421 // DBG << " <-read " << data.size() << endl;
422 std::istringstream datas( data );
423 PluginFrame ret( datas );
424 DBG << "<-" << ret << endl;
428 ///////////////////////////////////////////////////////////////////
430 // CLASS NAME : PluginScript
432 ///////////////////////////////////////////////////////////////////
434 const pid_t PluginScript::NotConnected( -1 );
436 long PluginScript::defaultSendTimeout()
437 { return Impl::_defaultSendTimeout; }
439 long PluginScript::defaultReceiveTimeout()
440 { return Impl::_defaultReceiveTimeout; }
442 void PluginScript::defaultSendTimeout( long newval_r )
443 { Impl::_defaultSendTimeout = newval_r > 0 ? newval_r : 0; }
445 void PluginScript::defaultReceiveTimeout( long newval_r )
446 { Impl::_defaultReceiveTimeout = newval_r > 0 ? newval_r : 0; }
448 long PluginScript::sendTimeout() const
449 { return _pimpl->_sendTimeout; }
451 long PluginScript::receiveTimeout() const
452 { return _pimpl->_receiveTimeout; }
454 void PluginScript::sendTimeout( long newval_r )
455 { _pimpl->_sendTimeout = newval_r > 0 ? newval_r : 0; }
457 void PluginScript::receiveTimeout( long newval_r )
458 { _pimpl->_receiveTimeout = newval_r > 0 ? newval_r : 0; }
460 PluginScript::PluginScript()
464 PluginScript::PluginScript( const Pathname & script_r )
465 : _pimpl( new Impl( script_r ) )
468 PluginScript::PluginScript( const Pathname & script_r, const Arguments & args_r )
469 : _pimpl( new Impl( script_r, args_r ) )
472 const Pathname & PluginScript::script() const
473 { return _pimpl->script(); }
475 const PluginScript::Arguments & PluginScript::args() const
476 { return _pimpl->args(); }
478 bool PluginScript::isOpen() const
479 { return _pimpl->isOpen(); }
481 pid_t PluginScript::getPid() const
482 { return _pimpl->getPid(); }
484 int PluginScript::lastReturn() const
485 { return _pimpl->lastReturn(); }
487 const std::string & PluginScript::lastExecError() const
488 { return _pimpl->lastExecError(); }
490 void PluginScript::open()
491 { _pimpl->open( _pimpl->script(), _pimpl->args() ); }
493 void PluginScript::open( const Pathname & script_r )
494 { _pimpl->open( script_r ); }
496 void PluginScript::open( const Pathname & script_r, const Arguments & args_r )
497 { _pimpl->open( script_r, args_r ); }
499 int PluginScript::close()
500 { return _pimpl->close(); }
502 void PluginScript::send( const PluginFrame & frame_r ) const
503 { _pimpl->send( frame_r ); }
505 PluginFrame PluginScript::receive() const
506 { return _pimpl->receive(); }
508 ///////////////////////////////////////////////////////////////////
510 std::ostream & operator<<( std::ostream & str, const PluginScript & obj )
511 { return str << *obj._pimpl; }
513 /////////////////////////////////////////////////////////////////
515 ///////////////////////////////////////////////////////////////////