Back to index

nordugrid-arc-nox  1.1.0~rc6
run_function.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 #include <unistd.h>
00005 
00006 #include <arc/Logger.h>
00007 #include <arc/Run.h>
00008 
00009 #include "run_function.h"
00010 
00011 
00012 static Arc::Logger& logger = Arc::Logger::getRootLogger();
00013 
00014 
00015 int RunFunction::run(const JobUser& user,const char* cmdname,int (*func)(void*),void* arg,int timeout) {
00016   if(func == NULL) return -1;
00017   std::string fake_cmd("/bin/true");
00018   Arc::Run re(fake_cmd);
00019   if(!re) {
00020     logger.msg(Arc::ERROR,"%s: Failure creating slot for child process",cmdname?cmdname:"");
00021     return -1;
00022   };
00023   RunFunction* rf = new RunFunction(user,cmdname,func,arg);
00024   if(!rf) {
00025     logger.msg(Arc::ERROR,"%s: Failure creating data storage for child process",cmdname?cmdname:"");
00026     return -1;
00027   };
00028   re.AssignInitializer(&initializer,rf);
00029   re.KeepStdin(true);
00030   re.KeepStdout(true);
00031   re.KeepStderr(true);
00032   if(!re.Start()) {
00033     delete rf;
00034     logger.msg(Arc::ERROR,"%s: Failure starting child process",cmdname?cmdname:"");
00035     return -1;
00036   };
00037   delete rf;
00038   if(!re.Wait(timeout)) {
00039     logger.msg(Arc::ERROR,"%s: Failure waiting for child process to finish",cmdname?cmdname:"");
00040     return -1;
00041   };
00042   return re.Result();
00043 }
00044 
00045 void RunFunction::initializer(void* arg) {
00046 #ifdef WIN32
00047 #error This functionality is not available in Windows environment
00048 #else
00049   // child
00050   RunFunction* it = (RunFunction*)arg;
00051   /* change user */
00052   if(!(it->user_.SwitchUser(true))) {
00053     std::cerr<<it->cmdname_<<": Failed switching user"<<std::endl;
00054     _exit(-1);
00055   };
00056   int r = (*(it->func_))(it->arg_);
00057   _exit(r);
00058 #endif
00059 }
00060