Switch to unified view

a/src/utils/execmd.h b/src/utils/execmd.h
...
...
14
 *   Free Software Foundation, Inc.,
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
16
 */
17
#ifndef _EXECMD_H_INCLUDED_
17
#ifndef _EXECMD_H_INCLUDED_
18
#define _EXECMD_H_INCLUDED_
18
#define _EXECMD_H_INCLUDED_
19
19
#include "autoconfig.h"
20
#include <signal.h>
21
20
22
#include <string>
21
#include <string>
23
#include <vector>
22
#include <vector>
24
#include <stack>
23
#include <stack>
25
26
#include "netcon.h"
27
24
28
/** 
25
/** 
29
 * Callback function object to advise of new data arrival, or just periodic 
26
 * Callback function object to advise of new data arrival, or just periodic 
30
 * heartbeat if cnt is 0. 
27
 * heartbeat if cnt is 0. 
31
 *
28
 *
...
...
40
    virtual void newData(int cnt) = 0;
37
    virtual void newData(int cnt) = 0;
41
};
38
};
42
39
43
/** 
40
/** 
44
 * Callback function object to get more input data. Data has to be provided
41
 * Callback function object to get more input data. Data has to be provided
45
 * in the initial input string, set it to empty to signify eof.
42
 * into the initial input string, set it to empty to signify eof.
46
 */
43
 */
47
class ExecCmdProvide {
44
class ExecCmdProvide {
48
 public:
45
 public:
49
    virtual ~ExecCmdProvide() {}
46
    virtual ~ExecCmdProvide() {}
50
    virtual void newData() = 0;
47
    virtual void newData() = 0;
...
...
91
     */
88
     */
92
    void setrlimit_as(int mbytes);
89
    void setrlimit_as(int mbytes);
93
90
94
    /** 
91
    /** 
95
     * Set function objects to call whenever new data is available or on
92
     * Set function objects to call whenever new data is available or on
96
     * select timeout / whenever new data is needed to send. Must be called
93
     * select timeout. The data itself is stored in the output string.
97
     * before doexec()
94
     * Must be set before calling doexec.
98
     */
95
     */
99
    void setAdvise(ExecCmdAdvise *adv) {m_advise = adv;}
96
    void setAdvise(ExecCmdAdvise *adv);
97
    /*
98
     * Set function object to call whenever new data is needed. The
99
     * data should be stored in the input string. Must be set before
100
     * calling doexec()
101
     */
100
    void setProvide(ExecCmdProvide *p) {m_provide = p;}
102
    void setProvide(ExecCmdProvide *p);
101
103
102
    /**
104
    /**
103
     * Set select timeout in milliseconds. The default is 1 S. 
105
     * Set select timeout in milliseconds. The default is 1 S. 
104
     * This is NOT a time after which an error will occur, but the period of
106
     * This is NOT a time after which an error will occur, but the period of
105
     * the calls to the cancellation check routine.
107
     * the calls to the advise routine (which normally checks for cancellation).
106
     */
108
     */
107
    void setTimeout(int mS) {if (mS > 30) m_timeoutMs = mS;}
109
    void setTimeout(int mS);
108
110
109
    /** 
111
    /** 
110
     * Set destination for stderr data. The default is to let it alone (will 
112
     * Set destination for stderr data. The default is to let it alone (will 
111
     * usually go to the terminal or to wherever the desktop messages go).
113
     * usually go to the terminal or to wherever the desktop messages go).
112
     * There is currently no option to put stderr data into a program variable
114
     * There is currently no option to put stderr data into a program variable
113
     * If the parameter can't be opened for writing, the command's
115
     * If the parameter can't be opened for writing, the command's
114
     * stderr will be closed.
116
     * stderr will be closed.
115
     */
117
     */
116
    void setStderr(const std::string &stderrFile) {m_stderrFile = stderrFile;}
118
    void setStderr(const std::string &stderrFile);
117
119
118
    /**
120
    /**
119
     * Execute command. 
121
     * Execute command. 
120
     *
122
     *
121
     * Both input and output can be specified, and asynchronous 
123
     * Both input and output can be specified, and asynchronous 
...
...
128
     * @param cmd the program to execute. This must be an absolute file name 
130
     * @param cmd the program to execute. This must be an absolute file name 
129
     *   or exist in the PATH.
131
     *   or exist in the PATH.
130
     * @param args the argument vector (NOT including argv[0]).
132
     * @param args the argument vector (NOT including argv[0]).
131
     * @param input Input to send TO the command.
133
     * @param input Input to send TO the command.
132
     * @param output Output FROM the command.
134
     * @param output Output FROM the command.
133
     * @return the exec ouput status (0 if ok), or -1
135
     * @return the exec output status (0 if ok), or -1
134
     */
136
     */
135
    int doexec(const std::string &cmd, const std::vector<std::string>& args, 
137
    int doexec(const std::string &cmd, const std::vector<std::string>& args, 
136
           const std::string *input = 0, 
138
           const std::string *input = 0, 
137
           std::string *output = 0);
139
           std::string *output = 0);
138
140
...
...
149
    /** Wait with WNOHANG set. 
151
    /** Wait with WNOHANG set. 
150
    @return true if process exited, false else.
152
    @return true if process exited, false else.
151
    @param O: status, the wait(2) call's status value */
153
    @param O: status, the wait(2) call's status value */
152
    bool maybereap(int *status);
154
    bool maybereap(int *status);
153
155
154
    pid_t getChildPid() {return m_pid;}
156
    pid_t getChildPid();
155
157
156
    /** 
158
    /** 
157
     * Cancel/kill command. This can be called from another thread or
159
     * Cancel/kill command. This can be called from another thread or
158
     * from the advise callback, which could also raise an exception to 
160
     * from the advise callback, which could also raise an exception to 
159
     * accomplish the same thing
161
     * accomplish the same thing
160
     */
162
     */
161
    void setKill() {m_killRequest = true;}
163
    void setKill();
162
164
163
    /**
165
    /**
164
     * Get rid of current process (become ready for start). 
166
     * Get rid of current process (become ready for start). 
165
     */
167
     */
166
    void zapChild() {setKill(); (void)wait();}
168
    void zapChild();
167
169
168
    ExecCmd()
170
    ExecCmd();
169
  : m_advise(0), m_provide(0), m_timeoutMs(1000), m_rlimit_as_mbytes(0)
170
    {
171
  reset();
172
    }
173
    ~ExecCmd();
171
    ~ExecCmd();
174
172
175
    /**
173
    /**
176
     * Utility routine: check if/where a command is found according to the
174
     * Utility routine: check if/where a command is found according to the
177
     * current PATH (or the specified one
175
     * current PATH (or the specified one
...
...
188
     * @param out output: what the command printed
186
     * @param out output: what the command printed
189
     * @return true if exec status was 0
187
     * @return true if exec status was 0
190
     */
188
     */
191
    static bool backtick(const std::vector<std::string> cmd, std::string& out);
189
    static bool backtick(const std::vector<std::string> cmd, std::string& out);
192
190
193
    friend class ExecCmdRsrc;
191
    class Internal;
194
 private:
192
 private:
195
    static bool      o_useVfork;
193
    Internal *m;
196
197
    std::vector<std::string>   m_env;
198
    ExecCmdAdvise   *m_advise;
199
    ExecCmdProvide  *m_provide;
200
    bool             m_killRequest;
201
    int              m_timeoutMs;
202
    int              m_rlimit_as_mbytes;
203
    std::string           m_stderrFile;
204
    // Pipe for data going to the command
205
    int              m_pipein[2];
206
    NetconP          m_tocmd;
207
    // Pipe for data coming out
208
    int              m_pipeout[2];
209
    NetconP          m_fromcmd;
210
    // Subprocess id
211
    pid_t            m_pid;
212
    // Saved sigmask
213
    sigset_t         m_blkcld;
214
215
    // Reset internal state indicators. Any resources should have been
216
    // previously freed
217
    void reset() {
218
  m_killRequest = false;
219
  m_pipein[0] = m_pipein[1] = m_pipeout[0] = m_pipeout[1] = -1;
220
  m_pid = -1;
221
  sigemptyset(&m_blkcld);
222
    }
223
    // Child process code
224
    inline void dochild(const std::string &cmd, const char **argv, 
225
          const char **envv, bool has_input, bool has_output);
226
    /* Copyconst and assignment private and forbidden */
194
    /* Copyconst and assignment are private and forbidden */
227
    ExecCmd(const ExecCmd &) {}
195
    ExecCmd(const ExecCmd &) {}
228
    ExecCmd& operator=(const ExecCmd &) {return *this;};
196
    ExecCmd& operator=(const ExecCmd &) {return *this;};
229
};
197
};
230
198
231
199