35 #include <sys/types.h>
38 #include <sys/types.h>
39 #include "XrdSys/XrdWin32.hh"
56 if (myStream)
delete myStream;
71 if (!myStream)
return EPIPE;
72 feedHelper.
Lock(&feedMutex);
76 if (!myStream->
isAlive() && !Restart())
77 {
if (eDest) eDest->
Emsg(
"Prog" "Unable to restart", Arg[0]);
83 if (!myStream->
Put((
const char **)data, (
const int *)dlen))
return 0;
87 {
if (eDest) eDest->
Emsg(
"Prog", rc,
"restart", Arg[0]);
90 if (!myStream->
Put((
const char **)data, (
const int *)dlen))
return 0;
100 #define runWithVec(strmP, theRC)\
101 const char *argV[4]; int argC = 0;\
102 if (arg1) argV[argC++] = arg1;\
103 if (arg2) argV[argC++] = arg2;\
104 if (arg3) argV[argC++] = arg3;\
105 if (arg4) argV[argC++] = arg4;\
106 theRC = Run(strmP, argV, argC)
109 const char *envV[])
const
112 int rc, totArgs = numArgs + argC;
117 {
if (eDest) eDest->
Emsg(
"Run",
"No program specified");
123 myArgs = (
char**)alloca(
sizeof(
char *) * (totArgs + 1));
124 if (numArgs) memcpy(myArgs, Arg,
sizeof(
char*)*numArgs);
125 if (argC) memcpy(&myArgs[numArgs], argV,
sizeof(
char*)*argC);
130 if (myProc)
return (*myProc)(Sp, myArgs, totArgs);
136 progEnv.
PutPtr(
"XrdEnvars**", (
void *)envV);
137 rc = Sp->
Exec(myArgs, 1, theEFD);
139 }
else rc = Sp->
Exec(myArgs, 1, theEFD);
145 if (eDest) eDest->
Emsg(
"Run", rc,
"execute", Arg[0]);
164 rc =
Run(&cmd, argV, argC, envV);
170 if (eDest && *lp) eDest->
Emsg(
"Run", lp);
180 const char *arg3,
const char *arg4)
const
193 const char *arg3,
const char *arg4)
const
207 if (eDest && *lp) eDest->
Emsg(
"Run", lp);
217 const char *arg1,
const char *arg2,
218 const char *arg3,
const char *arg4)
const
231 if (outBuff && outBsz > 0)
233 {
while (*lp && *lp ==
' ') lp++;
234 if ((n = strlen(lp)))
236 while(*tp-- ==
' ') n--;
237 if (n >= outBsz) n = outBsz-1;
238 strncpy(outBuff, lp, n); outBuff += n;
264 if (myProc)
return 0;
275 sprintf(buff,
"%d", WTERMSIG(rc));
276 eDest->
Emsg(
"Run", Arg[0],
"killed by signal", buff);
281 {rc = WEXITSTATUS(rc);
284 sprintf(buff,
"%d", rc);
285 eDest->
Emsg(
"Run", Arg[0],
"ended with status", buff);
299 static const int maxArgs = 65;
306 if (!errP) errP = eDest;
308 ArgBuff = strdup(prog);
315 {
if (!rc || !argV[0])
316 {
const char *pgm = (Proc ?
"procedure" :
"program");
317 errP->
Emsg(
"Run", pgm,
"name not specified.");
318 }
else errP->
Emsg(
"Run", rc,
"set up", argV[0]);
320 return (rc ? rc : -EINVAL);
327 Arg =
new char*[rc+1];
328 memcpy(Arg, argV,
sizeof(
char *) * (rc+1));
332 if ((myProc = Proc))
return 0;
338 if (errP) errP->
Emsg(
"Run", rc,
"set up", Arg[0]);
354 if (myStream)
return EBUSY;
355 if (!(myStream =
new XrdOucStream(eDest)))
return ENOMEM;
360 return Run(myStream);
370 void XrdOucProg::Reset()
373 if (ArgBuff) {free(ArgBuff); ArgBuff = 0;}
374 if (numArgs)
delete [] Arg;
383 int XrdOucProg::Restart()
386 return Run(myStream);
#define runWithVec(strmP, theRC)
int access(const char *path, int amode)
void PutPtr(const char *varname, void *value)
int RunDone(XrdOucStream &cmd) const
int Run(XrdOucStream *Sp, const char *argV[], int argc=0, const char *envV[]=0) const
int Feed(const char *data[], const int dlen[])
int Setup(const char *prog, XrdSysError *errP=0, int(*Proc)(XrdOucStream *, char **, int)=0)
int Put(const char *data, const int dlen)
int Exec(const char *, int inrd=0, int efd=0)
XrdOucEnv * SetEnv(XrdOucEnv *newEnv)
static int argList(char *args, char **argV, int argC)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
void Lock(XrdSysMutex *Mutex)