libzypp  10.5.0
zypp::ExternalProgram Class Reference

Execute a program and give access to its io An object of this class encapsulates the execution of an external program. More...

#include <ExternalProgram.h>

Inheritance diagram for zypp::ExternalProgram:

List of all members.

Public Types

enum  Stderr_Disposition { Normal_Stderr, Discard_Stderr, Stderr_To_Stdout, Stderr_To_FileDesc }
 Define symbols for different policies on the handling of stderr. More...
typedef std::vector< std::string > Arguments
typedef std::map< std::string,
std::string > 
Environment
 For passing additional environment variables to set.

Public Member Functions

 ExternalProgram (std::string commandline, Stderr_Disposition stderr_disp=Normal_Stderr, bool use_pty=false, int stderr_fd=-1, bool default_locale=false, const Pathname &root="")
 Start the external program by using the shell /bin/sh with the option -c.
 ExternalProgram ()
 Start an external program by giving the arguments as an arry of char *pointers.
 ExternalProgram (const Arguments &argv, Stderr_Disposition stderr_disp=Normal_Stderr, bool use_pty=false, int stderr_fd=-1, bool default_locale=false, const Pathname &root="")
 ExternalProgram (const Arguments &argv, const Environment &environment, Stderr_Disposition stderr_disp=Normal_Stderr, bool use_pty=false, int stderr_fd=-1, bool default_locale=false, const Pathname &root="")
 ExternalProgram (const char *const *argv, Stderr_Disposition stderr_disp=Normal_Stderr, bool use_pty=false, int stderr_fd=-1, bool default_locale=false, const Pathname &root="")
 ExternalProgram (const char *const *argv, const Environment &environment, Stderr_Disposition stderr_disp=Normal_Stderr, bool use_pty=false, int stderr_fd=-1, bool default_locale=false, const Pathname &root="")
 ExternalProgram (const char *binpath, const char *const *argv_1, bool use_pty=false)
 ExternalProgram (const char *binpath, const char *const *argv_1, const Environment &environment, bool use_pty=false)
 ~ExternalProgram ()
int close ()
 Wait for the progamm to complete.
bool kill ()
 Kill the program.
bool running ()
 Return whether program is running.
pid_t getpid ()
 return pid
const std::string & command () const
 The command we're executing.
const std::string & execError () const
 Some detail telling why the execution failed, if it failed.
std::ostream & operator>> (std::ostream &out_r)
 Redirect all command output to an ostream.

Static Public Member Functions

static void renumber_fd (int origfd, int newfd)
 origfd will be accessible as newfd and closed (unless they were equal)

Protected Member Functions

int checkStatus (int)

Private Member Functions

void start_program (const char *const *argv, const Environment &environment, Stderr_Disposition stderr_disp=Normal_Stderr, int stderr_fd=-1, bool default_locale=false, const char *root=NULL)

Private Attributes

bool use_pty
 Set to true, if a pair of ttys is used for communication instead of a pair of pipes.
pid_t pid
int _exitStatus
std::string _command
 Store the command we're executing.
std::string _execError
 Remember execution errors like failed fork/exec.

Detailed Description

Execute a program and give access to its io An object of this class encapsulates the execution of an external program.

It starts the program using fork and some exec.. call, gives you access to the program's stdio and closes the program after use.

 const char* argv[] =
 {
     "/usr/bin/foo,
     "--option1",
     "--option2",
     NULL
 };

 ExternalProgram prog( argv,
                        ExternalProgram::Discard_Stderr,
                        false, -1, true);
 string line;
 for(line = prog.receiveLine();
     ! line.empty();
     line = prog.receiveLine() )
 {
     stream << line;
 }
 prog.close();

Definition at line 56 of file ExternalProgram.h.


Member Typedef Documentation

typedef std::vector<std::string> zypp::ExternalProgram::Arguments

Definition at line 61 of file ExternalProgram.h.

typedef std::map<std::string,std::string> zypp::ExternalProgram::Environment

For passing additional environment variables to set.

Definition at line 78 of file ExternalProgram.h.


Member Enumeration Documentation

Define symbols for different policies on the handling of stderr.

Enumerator:
Normal_Stderr 
Discard_Stderr 
Stderr_To_Stdout 
Stderr_To_FileDesc 

Definition at line 67 of file ExternalProgram.h.


Constructor & Destructor Documentation

zypp::ExternalProgram::ExternalProgram ( std::string  commandline,
Stderr_Disposition  stderr_disp = Normal_Stderr,
bool  use_pty = false,
int  stderr_fd = -1,
bool  default_locale = false,
const Pathname &  root = "" 
)

Start the external program by using the shell /bin/sh with the option -c.

You can use io direction symbols < and >.

Parameters:
commandlinea shell commandline that is appended to /bin/sh -c.
default_localewhether to set LC_ALL=C before starting
rootdirectory to chroot into, / or empty to not chroot

Definition at line 42 of file ExternalProgram.cc.

zypp::ExternalProgram::ExternalProgram ( )

Start an external program by giving the arguments as an arry of char *pointers.

If environment is provided, varaiables will be added to the childs environment, overwriting existing ones.

Stdin redirection: If the 1st argument starts with a '<', the remaining part is treated as file opened for reading on standard input (or /dev/null if empty).

   // cat file /tmp/x
   const char* argv[] = { "</tmp/x", "cat", NULL };
   ExternalProgram prog( argv );

Definition at line 35 of file ExternalProgram.cc.

zypp::ExternalProgram::ExternalProgram ( const Arguments argv,
Stderr_Disposition  stderr_disp = Normal_Stderr,
bool  use_pty = false,
int  stderr_fd = -1,
bool  default_locale = false,
const Pathname &  root = "" 
)

Definition at line 67 of file ExternalProgram.cc.

zypp::ExternalProgram::ExternalProgram ( const Arguments argv,
const Environment environment,
Stderr_Disposition  stderr_disp = Normal_Stderr,
bool  use_pty = false,
int  stderr_fd = -1,
bool  default_locale = false,
const Pathname &  root = "" 
)

Definition at line 94 of file ExternalProgram.cc.

zypp::ExternalProgram::ExternalProgram ( const char *const *  argv,
Stderr_Disposition  stderr_disp = Normal_Stderr,
bool  use_pty = false,
int  stderr_fd = -1,
bool  default_locale = false,
const Pathname &  root = "" 
)

Definition at line 124 of file ExternalProgram.cc.

zypp::ExternalProgram::ExternalProgram ( const char *const *  argv,
const Environment environment,
Stderr_Disposition  stderr_disp = Normal_Stderr,
bool  use_pty = false,
int  stderr_fd = -1,
bool  default_locale = false,
const Pathname &  root = "" 
)

Definition at line 143 of file ExternalProgram.cc.

zypp::ExternalProgram::ExternalProgram ( const char *  binpath,
const char *const *  argv_1,
bool  use_pty = false 
)

Definition at line 159 of file ExternalProgram.cc.

zypp::ExternalProgram::ExternalProgram ( const char *  binpath,
const char *const *  argv_1,
const Environment environment,
bool  use_pty = false 
)

Definition at line 175 of file ExternalProgram.cc.

zypp::ExternalProgram::~ExternalProgram ( )

Definition at line 190 of file ExternalProgram.cc.


Member Function Documentation

int zypp::ExternalProgram::close ( ) [virtual]

Wait for the progamm to complete.

Reimplemented from zypp::externalprogram::ExternalDataSource.

Definition at line 393 of file ExternalProgram.cc.

bool zypp::ExternalProgram::kill ( )

Kill the program.

Definition at line 508 of file ExternalProgram.cc.

bool zypp::ExternalProgram::running ( )

Return whether program is running.

Definition at line 520 of file ExternalProgram.cc.

pid_t zypp::ExternalProgram::getpid ( ) [inline]

return pid

Definition at line 156 of file ExternalProgram.h.

const std::string& zypp::ExternalProgram::command ( ) const [inline]

The command we're executing.

Definition at line 159 of file ExternalProgram.h.

const std::string& zypp::ExternalProgram::execError ( ) const [inline]

Some detail telling why the execution failed, if it failed.

Empty if the command is still running or successfully completed.

  • Can't open pty (s).
  • Can't open pipe (s).
  • Can't fork (s).
  • Command exited with status d.
  • Command was killed by signal d (s).

Definition at line 171 of file ExternalProgram.h.

void zypp::ExternalProgram::renumber_fd ( int  origfd,
int  newfd 
) [static]

origfd will be accessible as newfd and closed (unless they were equal)

Definition at line 544 of file ExternalProgram.cc.

std::ostream & zypp::ExternalProgram::operator>> ( std::ostream &  out_r)

Redirect all command output to an ostream.

Returns when the command has completed.

   std::ostringstream s;
   ExternalProgram("pwd") >> s;
   SEC << s.str() << endl;
   std::ostringstream s;
   ExternalProgram prog("ls -l wrzl");
   prog >> s;
   if ( prog.close() == 0 )
     MIL << s.str() << endl;
   else
     ERR << prog.execError() << endl;

Definition at line 556 of file ExternalProgram.cc.

int zypp::ExternalProgram::checkStatus ( int  status) [protected]

Definition at line 468 of file ExternalProgram.cc.

void zypp::ExternalProgram::start_program ( const char *const *  argv,
const Environment environment,
Stderr_Disposition  stderr_disp = Normal_Stderr,
int  stderr_fd = -1,
bool  default_locale = false,
const char *  root = NULL 
) [private]

Definition at line 196 of file ExternalProgram.cc.


Member Data Documentation

Set to true, if a pair of ttys is used for communication instead of a pair of pipes.

Definition at line 210 of file ExternalProgram.h.

pid_t zypp::ExternalProgram::pid [private]

Definition at line 212 of file ExternalProgram.h.

Definition at line 213 of file ExternalProgram.h.

std::string zypp::ExternalProgram::_command [private]

Store the command we're executing.

Definition at line 215 of file ExternalProgram.h.

std::string zypp::ExternalProgram::_execError [private]

Remember execution errors like failed fork/exec.

Definition at line 217 of file ExternalProgram.h.


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