Back to index

nordugrid-arc-nox  1.1.0~rc6
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes
RunPlugin Class Reference

#include <run_plugin.h>

Collaboration diagram for RunPlugin:
Collaboration graph
[legend]

List of all members.

Classes

union  lib_plugin_t

Public Types

typedef void(* substitute_t )(std::string &str, void *arg)

Public Member Functions

 RunPlugin (void)
 RunPlugin (const std::string &cmd)
 RunPlugin (char const *const *args)
void operator= (const std::string &cmd)
void operator= (char const *const *args)
bool run (void)
bool run (substitute_t subst, void *arg)
int result (void) const
void timeout (int t)
void stdin_channel (const std::string &s)
const std::string & stdout_channel (void) const
const std::string & stderr_channel (void) const
 operator bool (void) const

Private Member Functions

void set (const std::string &cmd)
void set (char const *const *args)

Private Attributes

std::list< std::string > args_
std::string lib
std::string stdin_
std::string stdout_
std::string stderr_
int timeout_
int result_

Detailed Description

Definition at line 14 of file run_plugin.h.


Member Typedef Documentation

typedef void(* RunPlugin::substitute_t)(std::string &str, void *arg)

Definition at line 26 of file run_plugin.h.


Constructor & Destructor Documentation

RunPlugin::RunPlugin ( void  ) [inline]

Definition at line 31 of file run_plugin.h.

:timeout_(10),result_(0) { };
RunPlugin::RunPlugin ( const std::string &  cmd) [inline]

Definition at line 32 of file run_plugin.h.

:timeout_(10),result_(0) { set(cmd); };
RunPlugin::RunPlugin ( char const *const *  args) [inline]

Definition at line 33 of file run_plugin.h.

:timeout_(10),result_(0) { set(args); };

Member Function Documentation

RunPlugin::operator bool ( void  ) const [inline]

Definition at line 43 of file run_plugin.h.

{ return (args_.size() > 0); };
void RunPlugin::operator= ( const std::string &  cmd) [inline]

Definition at line 34 of file run_plugin.h.

{ set(cmd); };
void RunPlugin::operator= ( char const *const *  args) [inline]

Definition at line 35 of file run_plugin.h.

{ set(args); };
int RunPlugin::result ( void  ) const [inline]

Definition at line 38 of file run_plugin.h.

{ return result_; };

Here is the caller graph for this function:

bool RunPlugin::run ( void  )

Definition at line 88 of file run_plugin.cpp.

                        {
  if(args_.size() == 0) return true;
  char** args = (char**)malloc(sizeof(char*)*(args_.size()+1));
  if(args == NULL) return false;
  int n = 0;
  for(std::list<std::string>::iterator i = args_.begin();
              i!=args_.end();++i,++n) args[n]=(char*)(i->c_str());
  args[n]=NULL;
  if(lib.length() == 0) {
    bool r = false;
    Arc::Run re(args_);
    re.AssignStdin(stdin_);
    re.AssignStdout(stdout_);
    re.AssignStderr(stderr_);
    if(re.Start()) {
      if(re.Wait(timeout_)) {
        result_=re.Result();
        r=true;
      } else {
        re.Kill(0);
      };
    };
    if(!r) {
      free(args);
      return false;
    };
  } else {
#ifndef WIN32
    void* lib_h = dlopen(lib.c_str(),RTLD_NOW);
    if(lib_h == NULL) { free(args); return false; };
    lib_plugin_t f;
    f.v = dlsym(lib_h,args[0]);
    if(f.v == NULL) { dlclose(lib_h); free(args); return false; };
    result_ = (*f.f)(args[1],args[2],args[3],args[4],args[5],
                   args[6],args[7],args[8],args[9],args[10],
                   args[11],args[12],args[13],args[14],args[15],
                   args[16],args[17],args[18],args[19],args[20],
                   args[21],args[22],args[23],args[24],args[25],
                   args[26],args[27],args[28],args[29],args[30],
                   args[31],args[32],args[33],args[34],args[35],
                   args[36],args[37],args[38],args[39],args[40],
                   args[41],args[42],args[43],args[44],args[45],
                   args[56],args[57],args[58],args[59],args[60],
                   args[61],args[62],args[63],args[64],args[65],
                   args[66],args[67],args[68],args[69],args[70],
                   args[71],args[72],args[73],args[74],args[75],
                   args[76],args[77],args[78],args[79],args[80],
                   args[81],args[82],args[83],args[84],args[85],
                   args[86],args[87],args[88],args[89],args[90],
                   args[91],args[92],args[93],args[94],args[95],
                   args[96],args[97],args[98],args[99],args[100]);
    dlclose(lib_h);
#else
#warning Implement calling function from library for Windows
    result=-1;
#endif
  };
  free(args);
  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool RunPlugin::run ( substitute_t  subst,
void *  arg 
)

Definition at line 149 of file run_plugin.cpp.

                                                {
  result_=0; stdout_=""; stderr_="";
  if(subst == NULL) return run();
  if(args_.size() == 0) return true;
  char** args = (char**)malloc(sizeof(char*)*(args_.size()+1));
  if(args == NULL) return false;
  std::list<std::string> args__;
  for(std::list<std::string>::iterator i = args_.begin();i!=args_.end();++i) {
    args__.push_back(*i);
  };
  for(std::list<std::string>::iterator i = args__.begin();i!=args__.end();++i) {
    (*subst)(*i,arg);
  };
  int n = 0;
  for(std::list<std::string>::iterator i = args__.begin();
              i!=args__.end();++i,++n) args[n]=(char*)(i->c_str());
  args[n]=NULL;
  if(lib.length() == 0) {
    bool r = false;
    Arc::Run re(args_);
    re.AssignStdin(stdin_);
    re.AssignStdout(stdout_);
    re.AssignStderr(stderr_);
    if(re.Start()) {
      if(re.Wait(timeout_)) {
        result_=re.Result();
        r=true;
      } else {
        re.Kill(0);
      };
    };
    if(!r) {
      free(args);
      return false;
    };
  } else {
#ifndef WIN32
    void* lib_h = dlopen(lib.c_str(),RTLD_NOW);
    if(lib_h == NULL) { 
      free(args); return false;
    };
    lib_plugin_t f;
    f.v = dlsym(lib_h,args[0]);
    if(f.v == NULL) { 
      dlclose(lib_h); free(args); return false;
    };
    result_ = (*f.f)(args[1],args[2],args[3],args[4],args[5],
                   args[6],args[7],args[8],args[9],args[10],
                   args[11],args[12],args[13],args[14],args[15],
                   args[16],args[17],args[18],args[19],args[20],
                   args[21],args[22],args[23],args[24],args[25],
                   args[26],args[27],args[28],args[29],args[30],
                   args[31],args[32],args[33],args[34],args[35],
                   args[36],args[37],args[38],args[39],args[40],
                   args[41],args[42],args[43],args[44],args[45],
                   args[56],args[57],args[58],args[59],args[60],
                   args[61],args[62],args[63],args[64],args[65],
                   args[66],args[67],args[68],args[69],args[70],
                   args[71],args[72],args[73],args[74],args[75],
                   args[76],args[77],args[78],args[79],args[80],
                   args[81],args[82],args[83],args[84],args[85],
                   args[86],args[87],args[88],args[89],args[90],
                   args[91],args[92],args[93],args[94],args[95],
                   args[96],args[97],args[98],args[99],args[100]);
    dlclose(lib_h);
#else
#warning Implement calling function from library for Windows
    result=-1;
#endif
  };
  free(args);
  return true;
}

Here is the call graph for this function:

void RunPlugin::set ( const std::string &  cmd) [private]

Definition at line 52 of file run_plugin.cpp.

                                        {
  args_.resize(0); lib="";
  char** args = string_to_args(cmd);
  if(args == NULL) return;
  for(char** arg = args;*arg;arg++) {
    args_.push_back(std::string(*arg));
  };
  free_args(args);
  if(args_.size() == 0) return;
  std::string& exc = *(args_.begin());
  if(exc[0] == '/') return;
  std::string::size_type n = exc.find('@');
  if(n == std::string::npos) return;
  std::string::size_type p = exc.find('/');
  if((p != std::string::npos) && (p < n)) return;
  lib=exc.substr(n+1); exc.resize(n);
  if(lib[0] != '/') lib="./"+lib;
}

Here is the call graph for this function:

void RunPlugin::set ( char const *const *  args) [private]

Definition at line 71 of file run_plugin.cpp.

                                             {
  args_.resize(0); lib="";
  if(args == NULL) return;
  for(char const * const * arg = args;*arg;arg++) {
    args_.push_back(std::string(*arg));
  };
  if(args_.size() == 0) return;
  std::string& exc = *(args_.begin());
  if(exc[0] == '/') return;
  std::string::size_type n = exc.find('@');
  if(n == std::string::npos) return;
  std::string::size_type p = exc.find('/');
  if((p != std::string::npos) && (p < n)) return;
  lib=exc.substr(n+1); exc.resize(n);
  if(lib[0] != '/') lib="./"+lib;
}
const std::string& RunPlugin::stderr_channel ( void  ) const [inline]

Definition at line 42 of file run_plugin.h.

{ return stderr_; };
void RunPlugin::stdin_channel ( const std::string &  s) [inline]

Definition at line 40 of file run_plugin.h.

{ stdin_=s; };
const std::string& RunPlugin::stdout_channel ( void  ) const [inline]

Definition at line 41 of file run_plugin.h.

{ return stdout_; };
void RunPlugin::timeout ( int  t) [inline]

Definition at line 39 of file run_plugin.h.

{ timeout_=t; };

Here is the caller graph for this function:


Member Data Documentation

std::list<std::string> RunPlugin::args_ [private]

Definition at line 16 of file run_plugin.h.

std::string RunPlugin::lib [private]

Definition at line 17 of file run_plugin.h.

int RunPlugin::result_ [private]

Definition at line 22 of file run_plugin.h.

std::string RunPlugin::stderr_ [private]

Definition at line 20 of file run_plugin.h.

std::string RunPlugin::stdin_ [private]

Definition at line 18 of file run_plugin.h.

std::string RunPlugin::stdout_ [private]

Definition at line 19 of file run_plugin.h.

int RunPlugin::timeout_ [private]

Definition at line 21 of file run_plugin.h.


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