XRootD
XrdOucProg.hh
Go to the documentation of this file.
1 #ifndef __OOUC_PROG__
2 #define __OOUC_PROG__
3 /******************************************************************************/
4 /* */
5 /* X r d O u c P r o g . h h */
6 /* */
7 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Deprtment of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 #include <sys/types.h>
33 
34 class XrdSysError;
35 class XrdOucStream;
36 
38 {
39 public:
40 
41 // When creating an Prog object, you may pass an optional error routing object.
42 // If you do so, error messages and all command output will be writen via the
43 // error object. Otherwise, errors will be returned quietly.
44 //
45  XrdOucProg(XrdSysError *errobj=0, int efd=-1)
46  : eDest(errobj), myStream(0), myProc(0), ArgBuff(0),
47  Arg(&ArgBuff), numArgs(0), theEFD(efd) {}
48 
49  ~XrdOucProg();
50 
51 // Feed() send a data to the program started by Start(). Several variations
52 // exist to accomodate various needs. Note that should the program not be
53 // running when Feed() is called, it is restarted.
54 //
55 int Feed(const char *data[], const int dlen[]);
56 
57 int Feed(const char *data, int dlen)
58  {const char *myData[2] = {data, 0};
59  const int myDlen[2] = {dlen, 0};
60  return Feed(myData, myDlen);
61  }
62 
63 int Feed(const char *data) {return Feed(data, (int)strlen(data));}
64 
65 // getStream() returns the stream created by Start(). Use the object to get
66 // lines written by the started program.
67 //
68 XrdOucStream *getStream() const {return myStream;}
69 
70 // Return true if this program is a local in-line executable.
71 //
72 bool isLocal() {return myProc != 0;}
73 
74 // Run executes the command that was passed via Setup(). You may pass
75 // additional arguments to be appended to the existing ones. The
76 // method also allows envars to be set in forked process via envV vector
77 // which contains strings "var=val" and terminaes with a null pointer.
78 // When using the form without a stream pointer, command output is thrown away.
79 //
80 int Run(XrdOucStream *Sp, const char *argV[], int argc=0,
81  const char *envV[]=0) const;
82 
83 int Run(const char *argV[], int argC, const char *envV[]=0) const;
84 
85 // Run executes the command that was passed via Setup(). You may pass
86 // up to four additional arguments that will be added to the end of any
87 // existing arguments. The ending status code of the program is returned.
88 //
89 int Run(XrdOucStream *Sp, const char *arg1=0, const char *arg2=0,
90  const char *arg3=0, const char *arg4=0) const;
91 
92 int Run(const char *arg1=0, const char *arg2=0,
93  const char *arg3=0, const char *arg4=0) const;
94 
95 int Run(char *outBuff, int outBsz,
96  const char *arg1=0, const char *arg2=0,
97  const char *arg3=0, const char *arg4=0) const;
98 
99 // RunDone should be called to drain the output stream and get the ending
100 // status of the running process.
101 //
102 int RunDone(XrdOucStream &cmd) const;
103 
104 // Start executes the command that was passed via Setup(). The started
105 // program is expected to linger so that you can send directives to it
106 // via its standard in. Use Feed() to do this. If the output of the command
107 // is wanted, use getStream() to get the stream object and use it to read
108 // lines the program sends to standard out.
109 //
110 int Start(void);
111 
112 // Setup takes a command string and sets up a parameter list. If a Proc pointer
113 // is passed, then the command executes via that function. Otherwise, it checks
114 // that the program (first token) is executable.
115 // Zero is returned upon success, otherwise a -errno is returned,
116 //
117 int Setup(const char *prog,
118  XrdSysError *errP=0,
119  int (*Proc)(XrdOucStream *, char **, int)=0
120  );
121 
122 /******************************************************************************/
123 
124 private:
125  void Reset();
126  int Restart();
127  XrdSysError *eDest;
128  XrdOucStream *myStream;
129  int (*myProc)(XrdOucStream *, char **, int);
130  char *ArgBuff;
131  char **Arg;
132  int numArgs;
133  int theEFD;
134 };
135 #endif
int Feed(const char *data, int dlen)
Definition: XrdOucProg.hh:57
XrdOucProg(XrdSysError *errobj=0, int efd=-1)
Definition: XrdOucProg.hh:45
bool isLocal()
Definition: XrdOucProg.hh:72
int Feed(const char *data)
Definition: XrdOucProg.hh:63
int Start(void)
Definition: XrdOucProg.cc:349
int RunDone(XrdOucStream &cmd) const
Definition: XrdOucProg.cc:257
XrdOucStream * getStream() const
Definition: XrdOucProg.hh:68
int Run(XrdOucStream *Sp, const char *argV[], int argc=0, const char *envV[]=0) const
Definition: XrdOucProg.cc:108
int Feed(const char *data[], const int dlen[])
Definition: XrdOucProg.cc:63
int Setup(const char *prog, XrdSysError *errP=0, int(*Proc)(XrdOucStream *, char **, int)=0)
Definition: XrdOucProg.cc:296