Async  1.5.0
AsyncExec.h
Go to the documentation of this file.
1 
32 #ifndef ASYNC_EXEC_INCLUDED
33 #define ASYNC_EXEC_INCLUDED
34 
35 
36 /****************************************************************************
37  *
38  * System Includes
39  *
40  ****************************************************************************/
41 
42 #include <unistd.h>
43 #include <signal.h>
44 #include <sigc++/sigc++.h>
45 
46 #include <vector>
47 #include <string>
48 #include <map>
49 
50 
51 /****************************************************************************
52  *
53  * Project Includes
54  *
55  ****************************************************************************/
56 
57 #include <AsyncFdWatch.h>
58 
59 
60 /****************************************************************************
61  *
62  * Local Includes
63  *
64  ****************************************************************************/
65 
66 
67 
68 /****************************************************************************
69  *
70  * Forward declarations
71  *
72  ****************************************************************************/
73 
74 
75 
76 /****************************************************************************
77  *
78  * Namespace
79  *
80  ****************************************************************************/
81 
82 namespace Async
83 {
84 
85 
86 /****************************************************************************
87  *
88  * Forward declarations of classes inside of the declared namespace
89  *
90  ****************************************************************************/
91 
92 class Timer;
93 
94 
95 /****************************************************************************
96  *
97  * Defines & typedefs
98  *
99  ****************************************************************************/
100 
101 
102 
103 /****************************************************************************
104  *
105  * Exported Global Variables
106  *
107  ****************************************************************************/
108 
109 
110 
111 /****************************************************************************
112  *
113  * Class definitions
114  *
115  ****************************************************************************/
116 
131 class Exec : public sigc::trackable
132 {
133  public:
137  explicit Exec(const std::string &cmdline="");
138 
142  ~Exec(void);
143 
153  void setCommandLine(const std::string &cmdline);
154 
159  const std::string &command(void) const { return args[0]; }
160 
165  void appendArgument(const std::string &arg);
166 
177  bool nice(int inc=10);
178 
188  void setTimeout(int time_s);
189 
201  bool run(void);
202 
209  bool writeStdin(const char *buf, int cnt);
210 
216  bool writeStdin(const std::string &str);
217 
223  bool kill(int sig=SIGTERM);
224 
233  bool closeStdin(void);
234 
242  bool ifExited(void) const;
243 
251  bool ifSignaled(void) const;
252 
262  int exitStatus(void) const;
263 
273  int termSig(void) const;
274 
283  sigc::signal<void, const char *, int> stdoutData;
284 
293  sigc::signal<void, const char *, int> stderrData;
294 
298  sigc::signal<void> stdoutClosed;
299 
303  sigc::signal<void> stderrClosed;
304 
312  sigc::signal<void> exited;
313 
314  protected:
315 
316  private:
317  typedef std::map<pid_t, Exec*> ExecMap;
318 
319  static ExecMap execs;
320  static int sigchld_pipe[2];
321  static Async::FdWatch *sigchld_watch;
322  static struct sigaction old_sigact;
323 
324  std::vector<std::string> args;
325  pid_t pid;
326  Async::FdWatch *stdout_watch;
327  Async::FdWatch *stderr_watch;
328  int stdin_fd;
329  int status;
330  int nice_value;
331  Async::Timer *timeout_timer;
332  bool pending_term;
333 
334  static void handleSigChld(int signal_number, siginfo_t *info,
335  void *context);
336  static void sigchldReceived(void);
337 
338  Exec(const Exec&);
339  Exec& operator=(const Exec&);
340  void stdoutActivity(Async::FdWatch *w);
341  void stderrActivity(Async::FdWatch *w);
342  void subprocessExited(void);
343  void handleTimeout(void);
344 
345 }; /* class Exec */
346 
347 
348 } /* namespace */
349 
350 #endif /* ASYNC_EXEC_INCLUDED */
351 
352 
353 
354 /*
355  * This file has not been truncated
356  */
sigc::signal< void > stdoutClosed
A signal that is emitted when the subprocess close its stdout.
Definition: AsyncExec.h:298
Execute external commands.
Definition: AsyncExec.h:131
~Exec(void)
Destructor.
sigc::signal< void > stderrClosed
A signal that is emitted when the subprocess close its stderr.
Definition: AsyncExec.h:303
sigc::signal< void > exited
A signal that is emitted when the subprocess exits.
Definition: AsyncExec.h:312
sigc::signal< void, const char *, int > stdoutData
A signal that is emitted when the subprocess write to stdout.
Definition: AsyncExec.h:283
void appendArgument(const std::string &arg)
Append a command line argument to a command.
bool closeStdin(void)
Close the stdin pipe to the subprocess.
A class that produces timer events.
Definition: AsyncTimer.h:116
A class for watching file descriptors.
Definition: AsyncFdWatch.h:119
bool ifExited(void) const
Check if the subprocess exited in a normal way.
bool writeStdin(const char *buf, int cnt)
Write data to stdin on the subprocess.
bool kill(int sig=SIGTERM)
Send a UNIX signal to the subprocess.
Namespace for the asynchronous programming classes.
Exec(const std::string &cmdline="")
Default constructor.
int termSig(void) const
Read the UNIX signal number that caused the subprocess to stop.
bool nice(int inc=10)
Modify the nice value for the child subprocess.
const std::string & command(void) const
Get the command name for the command.
Definition: AsyncExec.h:159
int exitStatus(void) const
Read the exit code of the subprocess.
void setCommandLine(const std::string &cmdline)
Set the command line to use.
void setTimeout(int time_s)
Set a timeout on the allowed runtime for the subprocess.
bool run(void)
Run the command.
Contains a watch for file descriptors.
bool ifSignaled(void) const
Check if the subprocess exited due to receiving a UNIX signal.
sigc::signal< void, const char *, int > stderrData
A signal that is emitted when the subprocess write to stderr.
Definition: AsyncExec.h:293