Async 1.8.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
82namespace Async
83{
84
85
86/****************************************************************************
87 *
88 * Forward declarations of classes inside of the declared namespace
89 *
90 ****************************************************************************/
91
92class 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
131class Exec : public sigc::trackable
132{
133 public:
134 using Environment = std::vector<std::pair<std::string,std::string>>;
135
139 explicit Exec(const std::string &cmdline="");
140
144 ~Exec(void);
145
155 void setCommandLine(const std::string &cmdline);
156
161 const std::string &command(void) const { return args[0]; }
162
167 void appendArgument(const std::string &arg);
168
178
187 void addEnvironmentVar(const std::string& name, const std::string& val);
188
197
208 bool nice(int inc=10);
209
219 void setTimeout(int time_s);
220
232 bool run(void);
233
240 bool writeStdin(const char *buf, int cnt);
241
247 bool writeStdin(const std::string &str);
248
254 bool kill(int sig=SIGTERM);
255
264 bool closeStdin(void);
265
273 bool ifExited(void) const;
274
282 bool ifSignaled(void) const;
283
293 int exitStatus(void) const;
294
304 int termSig(void) const;
305
314 sigc::signal<void, const char *, int> stdoutData;
315
324 sigc::signal<void, const char *, int> stderrData;
325
329 sigc::signal<void> stdoutClosed;
330
334 sigc::signal<void> stderrClosed;
335
343 sigc::signal<void> exited;
344
345 protected:
346
347 private:
348 typedef std::map<pid_t, Exec*> ExecMap;
349
350 static ExecMap execs;
351 static int sigchld_pipe[2];
352 static Async::FdWatch *sigchld_watch;
353 static struct sigaction old_sigact;
354
355 std::vector<std::string> args;
356 std::vector<std::string> env;
357 pid_t pid;
358 Async::FdWatch *stdout_watch;
359 Async::FdWatch *stderr_watch;
360 int stdin_fd;
361 int status;
362 int nice_value;
363 Async::Timer *timeout_timer;
364 bool pending_term;
365 bool clear_env = false;
366
367 static void handleSigChld(int signal_number, siginfo_t *info,
368 void *context);
369 static void sigchldReceived(void);
370
371 Exec(const Exec&);
372 Exec& operator=(const Exec&);
373 void stdoutActivity(Async::FdWatch *w);
374 void stderrActivity(Async::FdWatch *w);
375 void subprocessExited(void);
376 void handleTimeout(void);
377
378}; /* class Exec */
379
380
381} /* namespace */
382
383#endif /* ASYNC_EXEC_INCLUDED */
384
385
386
387/*
388 * This file has not been truncated
389 */
Contains a watch for file descriptors.
Execute external commands.
Definition AsyncExec.h:132
void addEnvironmentVar(const std::string &name, const std::string &val)
Add an additional environment variable.
sigc::signal< void, const char *, int > stderrData
A signal that is emitted when the subprocess write to stderr.
Definition AsyncExec.h:324
void addEnvironmentVars(const Environment &env)
Add multiple environment variables.
sigc::signal< void > stdoutClosed
A signal that is emitted when the subprocess close its stdout.
Definition AsyncExec.h:329
bool closeStdin(void)
Close the stdin pipe to the subprocess.
bool writeStdin(const std::string &str)
Write data to stdin on the subprocess.
int exitStatus(void) const
Read the exit code of the subprocess.
bool kill(int sig=SIGTERM)
Send a UNIX signal to the subprocess.
const std::string & command(void) const
Get the command name for the command.
Definition AsyncExec.h:161
void setTimeout(int time_s)
Set a timeout on the allowed runtime for the subprocess.
Exec(const std::string &cmdline="")
Default constructor.
bool ifExited(void) const
Check if the subprocess exited in a normal way.
void clearEnvironment(void)
Clear the environment.
bool run(void)
Run the command.
int termSig(void) const
Read the UNIX signal number that caused the subprocess to stop.
void appendArgument(const std::string &arg)
Append a command line argument to a command.
sigc::signal< void > exited
A signal that is emitted when the subprocess exits.
Definition AsyncExec.h:343
bool writeStdin(const char *buf, int cnt)
Write data to stdin on the subprocess.
bool ifSignaled(void) const
Check if the subprocess exited due to receiving a UNIX signal.
void setCommandLine(const std::string &cmdline)
Set the command line to use.
bool nice(int inc=10)
Modify the nice value for the child subprocess.
sigc::signal< void > stderrClosed
A signal that is emitted when the subprocess close its stderr.
Definition AsyncExec.h:334
sigc::signal< void, const char *, int > stdoutData
A signal that is emitted when the subprocess write to stdout.
Definition AsyncExec.h:314
~Exec(void)
Destructor.
std::vector< std::pair< std::string, std::string > > Environment
Definition AsyncExec.h:134
A class for watching file descriptors.
A class that produces timer events.
Definition AsyncTimer.h:117
Namespace for the asynchronous programming classes.